blob: b4fd9d591bb252fd4a5664692ec2352c230d4968 [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
4099 {
4100 res = arg1 >> arg2;
4101 // clear the topmost sign bit
4102 res &= ~((uvarnumber_T)1
4103 << MAX_LSHIFT_BITS);
4104 }
4105 break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004106 default: break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004107 }
4108
Bram Moolenaar4c137212021-04-19 16:48:48 +02004109 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004110 if (iptr->isn_type == ISN_COMPARENR)
4111 {
4112 tv1->v_type = VAR_BOOL;
4113 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4114 }
4115 else
4116 tv1->vval.v_number = res;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004117 if (div_zero)
4118 {
4119 SOURCING_LNUM = iptr->isn_lnum;
4120 emsg(_(e_divide_by_zero));
4121 goto on_error;
4122 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004123 }
4124 break;
4125
4126 // Computation with two float arguments
4127 case ISN_OPFLOAT:
4128 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01004129#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004130 {
4131 typval_T *tv1 = STACK_TV_BOT(-2);
4132 typval_T *tv2 = STACK_TV_BOT(-1);
4133 float_T arg1 = tv1->vval.v_float;
4134 float_T arg2 = tv2->vval.v_float;
4135 float_T res = 0;
4136 int cmp = FALSE;
4137
4138 switch (iptr->isn_arg.op.op_type)
4139 {
4140 case EXPR_MULT: res = arg1 * arg2; break;
4141 case EXPR_DIV: res = arg1 / arg2; break;
4142 case EXPR_SUB: res = arg1 - arg2; break;
4143 case EXPR_ADD: res = arg1 + arg2; break;
4144
4145 case EXPR_EQUAL: cmp = arg1 == arg2; break;
4146 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
4147 case EXPR_GREATER: cmp = arg1 > arg2; break;
4148 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
4149 case EXPR_SMALLER: cmp = arg1 < arg2; break;
4150 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
4151 default: cmp = 0; break;
4152 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004153 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004154 if (iptr->isn_type == ISN_COMPAREFLOAT)
4155 {
4156 tv1->v_type = VAR_BOOL;
4157 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
4158 }
4159 else
4160 tv1->vval.v_float = res;
4161 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01004162#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004163 break;
4164
4165 case ISN_COMPARELIST:
Bram Moolenaar265f8112021-12-19 12:33:05 +00004166 case ISN_COMPAREDICT:
4167 case ISN_COMPAREFUNC:
4168 case ISN_COMPARESTRING:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004169 case ISN_COMPAREBLOB:
4170 {
4171 typval_T *tv1 = STACK_TV_BOT(-2);
4172 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar265f8112021-12-19 12:33:05 +00004173 exprtype_T exprtype = iptr->isn_arg.op.op_type;
4174 int ic = iptr->isn_arg.op.op_ic;
4175 int res = FALSE;
4176 int status = OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004177
Bram Moolenaar265f8112021-12-19 12:33:05 +00004178 SOURCING_LNUM = iptr->isn_lnum;
4179 if (iptr->isn_type == ISN_COMPARELIST)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004180 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00004181 status = typval_compare_list(tv1, tv2,
4182 exprtype, ic, &res);
4183 }
4184 else if (iptr->isn_type == ISN_COMPAREDICT)
4185 {
4186 status = typval_compare_dict(tv1, tv2,
4187 exprtype, ic, &res);
4188 }
4189 else if (iptr->isn_type == ISN_COMPAREFUNC)
4190 {
4191 status = typval_compare_func(tv1, tv2,
4192 exprtype, ic, &res);
4193 }
4194 else if (iptr->isn_type == ISN_COMPARESTRING)
4195 {
4196 status = typval_compare_string(tv1, tv2,
4197 exprtype, ic, &res);
4198 }
4199 else
4200 {
4201 status = typval_compare_blob(tv1, tv2, exprtype, &res);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004202 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004203 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004204 clear_tv(tv1);
4205 clear_tv(tv2);
4206 tv1->v_type = VAR_BOOL;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004207 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4208 if (status == FAIL)
4209 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004210 }
4211 break;
4212
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004213 case ISN_COMPAREANY:
4214 {
4215 typval_T *tv1 = STACK_TV_BOT(-2);
4216 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar657137c2021-01-09 15:45:23 +01004217 exprtype_T exprtype = iptr->isn_arg.op.op_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004218 int ic = iptr->isn_arg.op.op_ic;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004219 int status;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004220
Bram Moolenaareb26f432020-09-14 16:50:05 +02004221 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004222 status = typval_compare(tv1, tv2, exprtype, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004223 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004224 --ectx->ec_stack.ga_len;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004225 if (status == FAIL)
4226 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004227 }
4228 break;
4229
4230 case ISN_ADDLIST:
4231 case ISN_ADDBLOB:
4232 {
4233 typval_T *tv1 = STACK_TV_BOT(-2);
4234 typval_T *tv2 = STACK_TV_BOT(-1);
4235
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004236 // add two lists or blobs
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004237 if (iptr->isn_type == ISN_ADDLIST)
Bram Moolenaar07802042021-09-09 23:01:14 +02004238 {
4239 if (iptr->isn_arg.op.op_type == EXPR_APPEND
4240 && tv1->vval.v_list != NULL)
4241 list_extend(tv1->vval.v_list, tv2->vval.v_list,
4242 NULL);
4243 else
4244 eval_addlist(tv1, tv2);
4245 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004246 else
4247 eval_addblob(tv1, tv2);
4248 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004249 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004250 }
4251 break;
4252
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004253 case ISN_LISTAPPEND:
4254 {
4255 typval_T *tv1 = STACK_TV_BOT(-2);
4256 typval_T *tv2 = STACK_TV_BOT(-1);
4257 list_T *l = tv1->vval.v_list;
4258
4259 // add an item to a list
Bram Moolenaar1f4a3452022-01-01 18:29:21 +00004260 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004261 if (l == NULL)
4262 {
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004263 emsg(_(e_cannot_add_to_null_list));
4264 goto on_error;
4265 }
Bram Moolenaar1f4a3452022-01-01 18:29:21 +00004266 if (value_check_lock(l->lv_lock, NULL, FALSE))
4267 goto on_error;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004268 if (list_append_tv(l, tv2) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004269 goto theend;
Bram Moolenaar955347c2020-10-19 23:01:46 +02004270 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004271 --ectx->ec_stack.ga_len;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004272 }
4273 break;
4274
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02004275 case ISN_BLOBAPPEND:
4276 {
4277 typval_T *tv1 = STACK_TV_BOT(-2);
4278 typval_T *tv2 = STACK_TV_BOT(-1);
4279 blob_T *b = tv1->vval.v_blob;
4280 int error = FALSE;
4281 varnumber_T n;
4282
4283 // add a number to a blob
4284 if (b == NULL)
4285 {
4286 SOURCING_LNUM = iptr->isn_lnum;
4287 emsg(_(e_cannot_add_to_null_blob));
4288 goto on_error;
4289 }
4290 n = tv_get_number_chk(tv2, &error);
4291 if (error)
4292 goto on_error;
4293 ga_append(&b->bv_ga, (int)n);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004294 --ectx->ec_stack.ga_len;
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02004295 }
4296 break;
4297
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004298 // Computation with two arguments of unknown type
4299 case ISN_OPANY:
4300 {
4301 typval_T *tv1 = STACK_TV_BOT(-2);
4302 typval_T *tv2 = STACK_TV_BOT(-1);
4303 varnumber_T n1, n2;
4304#ifdef FEAT_FLOAT
4305 float_T f1 = 0, f2 = 0;
4306#endif
4307 int error = FALSE;
4308
4309 if (iptr->isn_arg.op.op_type == EXPR_ADD)
4310 {
4311 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
4312 {
4313 eval_addlist(tv1, tv2);
4314 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004315 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004316 break;
4317 }
4318 else if (tv1->v_type == VAR_BLOB
4319 && tv2->v_type == VAR_BLOB)
4320 {
4321 eval_addblob(tv1, tv2);
4322 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004323 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004324 break;
4325 }
4326 }
4327#ifdef FEAT_FLOAT
4328 if (tv1->v_type == VAR_FLOAT)
4329 {
4330 f1 = tv1->vval.v_float;
4331 n1 = 0;
4332 }
4333 else
4334#endif
4335 {
Bram Moolenaarf665e972020-12-05 19:17:16 +01004336 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004337 n1 = tv_get_number_chk(tv1, &error);
4338 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02004339 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004340#ifdef FEAT_FLOAT
4341 if (tv2->v_type == VAR_FLOAT)
4342 f1 = n1;
4343#endif
4344 }
4345#ifdef FEAT_FLOAT
4346 if (tv2->v_type == VAR_FLOAT)
4347 {
4348 f2 = tv2->vval.v_float;
4349 n2 = 0;
4350 }
4351 else
4352#endif
4353 {
4354 n2 = tv_get_number_chk(tv2, &error);
4355 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02004356 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004357#ifdef FEAT_FLOAT
4358 if (tv1->v_type == VAR_FLOAT)
4359 f2 = n2;
4360#endif
4361 }
4362#ifdef FEAT_FLOAT
4363 // if there is a float on either side the result is a float
4364 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
4365 {
4366 switch (iptr->isn_arg.op.op_type)
4367 {
4368 case EXPR_MULT: f1 = f1 * f2; break;
4369 case EXPR_DIV: f1 = f1 / f2; break;
4370 case EXPR_SUB: f1 = f1 - f2; break;
4371 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004372 default: SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004373 emsg(_(e_cannot_use_percent_with_float));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004374 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004375 }
4376 clear_tv(tv1);
4377 clear_tv(tv2);
4378 tv1->v_type = VAR_FLOAT;
4379 tv1->vval.v_float = f1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004380 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004381 }
4382 else
4383#endif
4384 {
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004385 int failed = FALSE;
4386
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004387 switch (iptr->isn_arg.op.op_type)
4388 {
4389 case EXPR_MULT: n1 = n1 * n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004390 case EXPR_DIV: n1 = num_divide(n1, n2, &failed);
4391 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01004392 goto on_error;
4393 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004394 case EXPR_SUB: n1 = n1 - n2; break;
4395 case EXPR_ADD: n1 = n1 + n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004396 default: n1 = num_modulus(n1, n2, &failed);
4397 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01004398 goto on_error;
4399 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004400 }
4401 clear_tv(tv1);
4402 clear_tv(tv2);
4403 tv1->v_type = VAR_NUMBER;
4404 tv1->vval.v_number = n1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004405 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004406 }
4407 }
4408 break;
4409
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004410 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004411 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004412 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004413 int is_slice = iptr->isn_type == ISN_STRSLICE;
4414 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004415 char_u *res;
4416
4417 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004418 // string slice: string is at stack-3, first index at
4419 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004420 if (is_slice)
4421 {
4422 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004423 n1 = tv->vval.v_number;
4424 }
4425
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004426 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004427 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004428
Bram Moolenaar4c137212021-04-19 16:48:48 +02004429 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004430 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004431 if (is_slice)
4432 // Slice: Select the characters from the string
Bram Moolenaar6601b622021-01-13 21:47:15 +01004433 res = string_slice(tv->vval.v_string, n1, n2, FALSE);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004434 else
4435 // Index: The resulting variable is a string of a
Bram Moolenaar0289a092021-03-14 18:40:19 +01004436 // single character (including composing characters).
4437 // If the index is too big or negative the result is
4438 // empty.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004439 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004440 vim_free(tv->vval.v_string);
4441 tv->vval.v_string = res;
4442 }
4443 break;
4444
4445 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02004446 case ISN_LISTSLICE:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004447 case ISN_BLOBINDEX:
4448 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004449 {
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004450 int is_slice = iptr->isn_type == ISN_LISTSLICE
4451 || iptr->isn_type == ISN_BLOBSLICE;
4452 int is_blob = iptr->isn_type == ISN_BLOBINDEX
4453 || iptr->isn_type == ISN_BLOBSLICE;
Bram Moolenaared591872020-08-15 22:14:53 +02004454 varnumber_T n1, n2;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004455 typval_T *val_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004456
4457 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02004458 // list slice: list is at stack-3, indexes at stack-2 and
4459 // stack-1
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004460 // Same for blob.
4461 val_tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004462
4463 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02004464 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004465 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02004466
4467 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004468 {
Bram Moolenaared591872020-08-15 22:14:53 +02004469 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02004470 n1 = tv->vval.v_number;
4471 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004472 }
Bram Moolenaared591872020-08-15 22:14:53 +02004473
Bram Moolenaar4c137212021-04-19 16:48:48 +02004474 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02004475 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02004476 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004477 if (is_blob)
4478 {
4479 if (blob_slice_or_index(val_tv->vval.v_blob, is_slice,
4480 n1, n2, FALSE, tv) == FAIL)
4481 goto on_error;
4482 }
4483 else
4484 {
4485 if (list_slice_or_index(val_tv->vval.v_list, is_slice,
4486 n1, n2, FALSE, tv, TRUE) == FAIL)
4487 goto on_error;
4488 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004489 }
4490 break;
4491
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004492 case ISN_ANYINDEX:
4493 case ISN_ANYSLICE:
4494 {
4495 int is_slice = iptr->isn_type == ISN_ANYSLICE;
4496 typval_T *var1, *var2;
4497 int res;
4498
4499 // index: composite is at stack-2, index at stack-1
4500 // slice: composite is at stack-3, indexes at stack-2 and
4501 // stack-1
4502 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02004503 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004504 if (check_can_index(tv, TRUE, TRUE) == FAIL)
4505 goto on_error;
4506 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
4507 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
Bram Moolenaar6601b622021-01-13 21:47:15 +01004508 res = eval_index_inner(tv, is_slice, var1, var2,
4509 FALSE, NULL, -1, TRUE);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004510 clear_tv(var1);
4511 if (is_slice)
4512 clear_tv(var2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004513 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004514 if (res == FAIL)
4515 goto on_error;
4516 }
4517 break;
4518
Bram Moolenaar9af78762020-06-16 11:34:42 +02004519 case ISN_SLICE:
4520 {
4521 list_T *list;
4522 int count = iptr->isn_arg.number;
4523
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02004524 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02004525 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02004526 list = tv->vval.v_list;
4527
4528 // no error for short list, expect it to be checked earlier
4529 if (list != NULL && list->lv_len >= count)
4530 {
4531 list_T *newlist = list_slice(list,
4532 count, list->lv_len - 1);
4533
4534 if (newlist != NULL)
4535 {
4536 list_unref(list);
4537 tv->vval.v_list = newlist;
4538 ++newlist->lv_refcount;
4539 }
4540 }
4541 }
4542 break;
4543
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004544 case ISN_GETITEM:
4545 {
4546 listitem_T *li;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02004547 getitem_T *gi = &iptr->isn_arg.getitem;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004548
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02004549 // Get list item: list is at stack-1, push item.
4550 // List type and length is checked for when compiling.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02004551 tv = STACK_TV_BOT(-1 - gi->gi_with_op);
4552 li = list_find(tv->vval.v_list, gi->gi_index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004553
Bram Moolenaar35578162021-08-02 19:10:38 +02004554 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004555 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004556 ++ectx->ec_stack.ga_len;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004557 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004558
4559 // Useful when used in unpack assignment. Reset at
4560 // ISN_DROP.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02004561 ectx->ec_where.wt_index = gi->gi_index + 1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004562 ectx->ec_where.wt_variable = TRUE;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004563 }
4564 break;
4565
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004566 case ISN_MEMBER:
4567 {
4568 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004569 char_u *key;
4570 dictitem_T *di;
4571
4572 // dict member: dict is at stack-2, key at stack-1
4573 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02004574 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004575 dict = tv->vval.v_dict;
4576
4577 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02004578 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004579 key = tv->vval.v_string;
Bram Moolenaar086fc9a2020-10-30 18:33:02 +01004580 if (key == NULL)
4581 key = (char_u *)"";
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02004582
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004583 if ((di = dict_find(dict, key, -1)) == NULL)
4584 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004585 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004586 semsg(_(e_key_not_present_in_dictionary), key);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004587
4588 // If :silent! is used we will continue, make sure the
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004589 // stack contents makes sense and the dict stack is
4590 // updated.
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004591 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004592 --ectx->ec_stack.ga_len;
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004593 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004594 (void) dict_stack_save(tv);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004595 tv->v_type = VAR_NUMBER;
4596 tv->vval.v_number = 0;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004597 goto on_fatal_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004598 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004599 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004600 --ectx->ec_stack.ga_len;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004601 // Put the dict used on the dict stack, it might be used by
4602 // a dict function later.
Bram Moolenaar50788ef2020-07-05 16:51:26 +02004603 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004604 if (dict_stack_save(tv) == FAIL)
4605 goto on_fatal_error;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02004606 copy_tv(&di->di_tv, tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004607 }
4608 break;
4609
4610 // dict member with string key
4611 case ISN_STRINGMEMBER:
4612 {
4613 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004614 dictitem_T *di;
4615
4616 tv = STACK_TV_BOT(-1);
4617 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
4618 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004619 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004620 emsg(_(e_dictionary_required));
Bram Moolenaard032f342020-07-18 18:13:02 +02004621 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004622 }
4623 dict = tv->vval.v_dict;
4624
4625 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
4626 == NULL)
4627 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004628 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar381692b2022-02-02 20:01:27 +00004629 semsg(_(e_key_not_present_in_dictionary),
4630 iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02004631 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004632 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004633 // Put the dict used on the dict stack, it might be used by
4634 // a dict function later.
4635 if (dict_stack_save(tv) == FAIL)
4636 goto on_fatal_error;
4637
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004638 copy_tv(&di->di_tv, tv);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004639 }
4640 break;
4641
4642 case ISN_CLEARDICT:
4643 dict_stack_drop();
4644 break;
4645
4646 case ISN_USEDICT:
4647 {
4648 typval_T *dict_tv = dict_stack_get_tv();
4649
4650 // Turn "dict.Func" into a partial for "Func" bound to
4651 // "dict". Don't do this when "Func" is already a partial
4652 // that was bound explicitly (pt_auto is FALSE).
4653 tv = STACK_TV_BOT(-1);
4654 if (dict_tv != NULL
4655 && dict_tv->v_type == VAR_DICT
4656 && dict_tv->vval.v_dict != NULL
4657 && (tv->v_type == VAR_FUNC
4658 || (tv->v_type == VAR_PARTIAL
4659 && (tv->vval.v_partial->pt_auto
4660 || tv->vval.v_partial->pt_dict == NULL))))
4661 dict_tv->vval.v_dict =
4662 make_partial(dict_tv->vval.v_dict, tv);
4663 dict_stack_drop();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004664 }
4665 break;
4666
4667 case ISN_NEGATENR:
4668 tv = STACK_TV_BOT(-1);
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00004669 // CHECKTYPE should have checked the variable type
Bram Moolenaarc58164c2020-03-29 18:40:30 +02004670#ifdef FEAT_FLOAT
4671 if (tv->v_type == VAR_FLOAT)
4672 tv->vval.v_float = -tv->vval.v_float;
4673 else
4674#endif
4675 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004676 break;
4677
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004678 case ISN_CHECKTYPE:
4679 {
4680 checktype_T *ct = &iptr->isn_arg.type;
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01004681 int save_wt_variable = ectx->ec_where.wt_variable;
Bram Moolenaarb1040dc2022-05-18 11:00:48 +01004682 int r;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004683
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01004684 tv = STACK_TV_BOT((int)ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02004685 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004686 if (!ectx->ec_where.wt_variable)
4687 ectx->ec_where.wt_index = ct->ct_arg_idx;
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01004688 ectx->ec_where.wt_variable = ct->ct_is_var;
Bram Moolenaarb1040dc2022-05-18 11:00:48 +01004689 r = check_typval_type(ct->ct_type, tv, ectx->ec_where);
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01004690 ectx->ec_where.wt_variable = save_wt_variable;
Bram Moolenaarb1040dc2022-05-18 11:00:48 +01004691 if (r == FAIL)
4692 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004693 if (!ectx->ec_where.wt_variable)
4694 ectx->ec_where.wt_index = 0;
Bram Moolenaar5e654232020-09-16 15:22:00 +02004695
4696 // number 0 is FALSE, number 1 is TRUE
4697 if (tv->v_type == VAR_NUMBER
4698 && ct->ct_type->tt_type == VAR_BOOL
4699 && (tv->vval.v_number == 0
4700 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004701 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02004702 tv->v_type = VAR_BOOL;
4703 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02004704 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004705 }
4706 }
4707 break;
4708
Bram Moolenaar9af78762020-06-16 11:34:42 +02004709 case ISN_CHECKLEN:
4710 {
4711 int min_len = iptr->isn_arg.checklen.cl_min_len;
4712 list_T *list = NULL;
4713
4714 tv = STACK_TV_BOT(-1);
4715 if (tv->v_type == VAR_LIST)
4716 list = tv->vval.v_list;
4717 if (list == NULL || list->lv_len < min_len
4718 || (list->lv_len > min_len
4719 && !iptr->isn_arg.checklen.cl_more_OK))
4720 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004721 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004722 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02004723 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004724 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02004725 }
4726 }
4727 break;
4728
Bram Moolenaaraa210a32021-01-02 15:41:03 +01004729 case ISN_SETTYPE:
Bram Moolenaar381692b2022-02-02 20:01:27 +00004730 set_tv_type(STACK_TV_BOT(-1), iptr->isn_arg.type.ct_type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01004731 break;
4732
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004733 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004734 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004735 {
4736 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004737 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004738
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004739 if (iptr->isn_type == ISN_2BOOL)
4740 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004741 tv = STACK_TV_BOT(iptr->isn_arg.tobool.offset);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004742 n = tv2bool(tv);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004743 if (iptr->isn_arg.tobool.invert)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004744 n = !n;
4745 }
4746 else
4747 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004748 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004749 SOURCING_LNUM = iptr->isn_lnum;
4750 n = tv_get_bool_chk(tv, &error);
4751 if (error)
4752 goto on_error;
4753 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004754 clear_tv(tv);
4755 tv->v_type = VAR_BOOL;
4756 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
4757 }
4758 break;
4759
4760 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02004761 case ISN_2STRING_ANY:
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01004762 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004763 if (do_2string(STACK_TV_BOT(iptr->isn_arg.tostring.offset),
4764 iptr->isn_type == ISN_2STRING_ANY,
4765 iptr->isn_arg.tostring.tolerant) == FAIL)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01004766 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004767 break;
4768
Bram Moolenaar08597872020-12-10 19:43:40 +01004769 case ISN_RANGE:
4770 {
4771 exarg_T ea;
4772 char *errormsg;
4773
Bram Moolenaarece0b872021-01-08 20:40:45 +01004774 ea.line2 = 0;
Bram Moolenaard1510ee2021-01-04 16:15:58 +01004775 ea.addr_count = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01004776 ea.addr_type = ADDR_LINES;
4777 ea.cmd = iptr->isn_arg.string;
Bram Moolenaarece0b872021-01-08 20:40:45 +01004778 ea.skip = FALSE;
Bram Moolenaar08597872020-12-10 19:43:40 +01004779 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaarece0b872021-01-08 20:40:45 +01004780 goto on_error;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004781
Bram Moolenaar35578162021-08-02 19:10:38 +02004782 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004783 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004784 ++ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004785 tv = STACK_TV_BOT(-1);
4786 tv->v_type = VAR_NUMBER;
4787 tv->v_lock = 0;
Bram Moolenaar06651632022-04-27 17:54:25 +01004788 tv->vval.v_number = ea.line2;
Bram Moolenaar08597872020-12-10 19:43:40 +01004789 }
4790 break;
4791
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004792 case ISN_PUT:
4793 {
4794 int regname = iptr->isn_arg.put.put_regname;
4795 linenr_T lnum = iptr->isn_arg.put.put_lnum;
4796 char_u *expr = NULL;
4797 int dir = FORWARD;
4798
Bram Moolenaar08597872020-12-10 19:43:40 +01004799 if (lnum < -2)
4800 {
4801 // line number was put on the stack by ISN_RANGE
4802 tv = STACK_TV_BOT(-1);
4803 curwin->w_cursor.lnum = tv->vval.v_number;
4804 if (lnum == LNUM_VARIABLE_RANGE_ABOVE)
4805 dir = BACKWARD;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004806 --ectx->ec_stack.ga_len;
Bram Moolenaar08597872020-12-10 19:43:40 +01004807 }
4808 else if (lnum == -2)
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004809 // :put! above cursor
4810 dir = BACKWARD;
4811 else if (lnum >= 0)
Bram Moolenaar4e713ba2022-02-07 15:31:37 +00004812 {
4813 curwin->w_cursor.lnum = lnum;
4814 if (lnum == 0)
4815 // check_cursor() below will move to line 1
4816 dir = BACKWARD;
4817 }
Bram Moolenaara28639e2021-01-19 22:48:09 +01004818
4819 if (regname == '=')
4820 {
4821 tv = STACK_TV_BOT(-1);
4822 if (tv->v_type == VAR_STRING)
4823 expr = tv->vval.v_string;
4824 else
4825 {
4826 expr = typval2string(tv, TRUE); // allocates value
4827 clear_tv(tv);
4828 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004829 --ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004830 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004831 check_cursor();
4832 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
4833 vim_free(expr);
4834 }
4835 break;
4836
Bram Moolenaar02194d22020-10-24 23:08:38 +02004837 case ISN_CMDMOD:
Bram Moolenaar4c137212021-04-19 16:48:48 +02004838 ectx->ec_funclocal.floc_save_cmdmod = cmdmod;
4839 ectx->ec_funclocal.floc_restore_cmdmod = TRUE;
4840 ectx->ec_funclocal.floc_restore_cmdmod_stacklen =
4841 ectx->ec_stack.ga_len;
Bram Moolenaar02194d22020-10-24 23:08:38 +02004842 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
4843 apply_cmdmod(&cmdmod);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004844 break;
4845
Bram Moolenaar02194d22020-10-24 23:08:38 +02004846 case ISN_CMDMOD_REV:
4847 // filter regprog is owned by the instruction, don't free it
4848 cmdmod.cmod_filter_regmatch.regprog = NULL;
4849 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004850 cmdmod = ectx->ec_funclocal.floc_save_cmdmod;
4851 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004852 break;
4853
Bram Moolenaar792f7862020-11-23 08:31:18 +01004854 case ISN_UNPACK:
4855 {
4856 int count = iptr->isn_arg.unpack.unp_count;
4857 int semicolon = iptr->isn_arg.unpack.unp_semicolon;
4858 list_T *l;
4859 listitem_T *li;
4860 int i;
4861
4862 // Check there is a valid list to unpack.
4863 tv = STACK_TV_BOT(-1);
4864 if (tv->v_type != VAR_LIST)
4865 {
4866 SOURCING_LNUM = iptr->isn_lnum;
4867 emsg(_(e_for_argument_must_be_sequence_of_lists));
4868 goto on_error;
4869 }
4870 l = tv->vval.v_list;
4871 if (l == NULL
4872 || l->lv_len < (semicolon ? count - 1 : count))
4873 {
4874 SOURCING_LNUM = iptr->isn_lnum;
4875 emsg(_(e_list_value_does_not_have_enough_items));
4876 goto on_error;
4877 }
4878 else if (!semicolon && l->lv_len > count)
4879 {
4880 SOURCING_LNUM = iptr->isn_lnum;
4881 emsg(_(e_list_value_has_more_items_than_targets));
4882 goto on_error;
4883 }
4884
4885 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar35578162021-08-02 19:10:38 +02004886 if (GA_GROW_FAILS(&ectx->ec_stack, count - 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004887 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004888 ectx->ec_stack.ga_len += count - 1;
Bram Moolenaar792f7862020-11-23 08:31:18 +01004889
4890 // Variable after semicolon gets a list with the remaining
4891 // items.
4892 if (semicolon)
4893 {
4894 list_T *rem_list =
4895 list_alloc_with_items(l->lv_len - count + 1);
4896
4897 if (rem_list == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004898 goto theend;
Bram Moolenaar792f7862020-11-23 08:31:18 +01004899 tv = STACK_TV_BOT(-count);
4900 tv->vval.v_list = rem_list;
4901 ++rem_list->lv_refcount;
4902 tv->v_lock = 0;
4903 li = l->lv_first;
4904 for (i = 0; i < count - 1; ++i)
4905 li = li->li_next;
4906 for (i = 0; li != NULL; ++i)
4907 {
Bram Moolenaar61efa162022-03-18 13:10:48 +00004908 typval_T tvcopy;
4909
4910 copy_tv(&li->li_tv, &tvcopy);
4911 list_set_item(rem_list, i, &tvcopy);
Bram Moolenaar792f7862020-11-23 08:31:18 +01004912 li = li->li_next;
4913 }
4914 --count;
4915 }
4916
4917 // Produce the values in reverse order, first item last.
4918 li = l->lv_first;
4919 for (i = 0; i < count; ++i)
4920 {
4921 tv = STACK_TV_BOT(-i - 1);
4922 copy_tv(&li->li_tv, tv);
4923 li = li->li_next;
4924 }
4925
4926 list_unref(l);
4927 }
4928 break;
4929
Bram Moolenaarb2049902021-01-24 12:53:53 +01004930 case ISN_PROF_START:
4931 case ISN_PROF_END:
4932 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01004933#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01004934 funccall_T cookie;
4935 ufunc_T *cur_ufunc =
4936 (((dfunc_T *)def_functions.ga_data)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02004937 + ectx->ec_dfunc_idx)->df_ufunc;
Bram Moolenaarb2049902021-01-24 12:53:53 +01004938
4939 cookie.func = cur_ufunc;
4940 if (iptr->isn_type == ISN_PROF_START)
4941 {
4942 func_line_start(&cookie, iptr->isn_lnum);
4943 // if we get here the instruction is executed
4944 func_line_exec(&cookie);
4945 }
4946 else
4947 func_line_end(&cookie);
Bram Moolenaarf002a412021-01-24 13:34:18 +01004948#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01004949 }
4950 break;
4951
Bram Moolenaare99d4222021-06-13 14:01:26 +02004952 case ISN_DEBUG:
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02004953 handle_debug(iptr, ectx);
Bram Moolenaare99d4222021-06-13 14:01:26 +02004954 break;
4955
Bram Moolenaar389df252020-07-09 21:20:47 +02004956 case ISN_SHUFFLE:
4957 {
Bram Moolenaar792f7862020-11-23 08:31:18 +01004958 typval_T tmp_tv;
4959 int item = iptr->isn_arg.shuffle.shfl_item;
4960 int up = iptr->isn_arg.shuffle.shfl_up;
Bram Moolenaar389df252020-07-09 21:20:47 +02004961
4962 tmp_tv = *STACK_TV_BOT(-item);
4963 for ( ; up > 0 && item > 1; --up)
4964 {
4965 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
4966 --item;
4967 }
4968 *STACK_TV_BOT(-item) = tmp_tv;
4969 }
4970 break;
4971
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004972 case ISN_DROP:
Bram Moolenaar4c137212021-04-19 16:48:48 +02004973 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004974 clear_tv(STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02004975 ectx->ec_where.wt_index = 0;
4976 ectx->ec_where.wt_variable = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004977 break;
4978 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004979 continue;
4980
Bram Moolenaard032f342020-07-18 18:13:02 +02004981func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02004982 // Restore previous function. If the frame pointer is where we started
4983 // then there is none and we are done.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004984 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02004985 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02004986
Bram Moolenaar4c137212021-04-19 16:48:48 +02004987 if (func_return(ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004988 // only fails when out of memory
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004989 goto theend;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02004990 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02004991
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004992on_error:
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004993 // Jump here for an error that does not require aborting execution.
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004994 // If "emsg_silent" is set then ignore the error, unless it was set
4995 // when calling the function.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004996 if (did_emsg_cumul + did_emsg == ectx->ec_did_emsg_before
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004997 && emsg_silent && did_emsg_def == 0)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004998 {
4999 // If a sequence of instructions causes an error while ":silent!"
5000 // was used, restore the stack length and jump ahead to restoring
5001 // the cmdmod.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005002 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaarf9041332021-01-21 19:41:16 +01005003 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005004 while (ectx->ec_stack.ga_len
5005 > ectx->ec_funclocal.floc_restore_cmdmod_stacklen)
Bram Moolenaarf9041332021-01-21 19:41:16 +01005006 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005007 --ectx->ec_stack.ga_len;
Bram Moolenaarf9041332021-01-21 19:41:16 +01005008 clear_tv(STACK_TV_BOT(0));
5009 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005010 while (ectx->ec_instr[ectx->ec_iidx].isn_type != ISN_CMDMOD_REV)
5011 ++ectx->ec_iidx;
Bram Moolenaarf9041332021-01-21 19:41:16 +01005012 }
Bram Moolenaarcd030c42020-10-30 21:49:40 +01005013 continue;
Bram Moolenaarf9041332021-01-21 19:41:16 +01005014 }
Bram Moolenaaraf0df472020-12-02 20:51:22 +01005015on_fatal_error:
5016 // Jump here for an error that messes up the stack.
Bram Moolenaar171fb922020-10-28 16:54:47 +01005017 // If we are not inside a try-catch started here, abort execution.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005018 if (trylevel <= ectx->ec_trylevel_at_start)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005019 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005020 }
5021
5022done:
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005023 ret = OK;
5024theend:
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005025 dict_stack_clear(dict_stack_len_at_start);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005026 ectx->ec_trylevel_at_start = save_trylevel_at_start;
5027 return ret;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005028}
5029
5030/*
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005031 * Execute the instructions from a VAR_INSTR typeval and put the result in
5032 * "rettv".
5033 * Return OK or FAIL.
5034 */
5035 int
5036exe_typval_instr(typval_T *tv, typval_T *rettv)
5037{
5038 ectx_T *ectx = tv->vval.v_instr->instr_ectx;
5039 isn_T *save_instr = ectx->ec_instr;
5040 int save_iidx = ectx->ec_iidx;
5041 int res;
5042
LemonBoyf3b48952022-05-05 13:53:03 +01005043 // Initialize rettv so that it is safe for caller to invoke clear_tv(rettv)
5044 // even when the compilation fails.
5045 rettv->v_type = VAR_UNKNOWN;
5046
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005047 ectx->ec_instr = tv->vval.v_instr->instr_instr;
5048 res = exec_instructions(ectx);
5049 if (res == OK)
5050 {
5051 *rettv = *STACK_TV_BOT(-1);
5052 --ectx->ec_stack.ga_len;
5053 }
5054
5055 ectx->ec_instr = save_instr;
5056 ectx->ec_iidx = save_iidx;
5057
5058 return res;
5059}
5060
5061/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02005062 * Execute the instructions from an ISN_SUBSTITUTE command, which are in
5063 * "substitute_instr".
5064 */
5065 char_u *
5066exe_substitute_instr(void)
5067{
5068 ectx_T *ectx = substitute_instr->subs_ectx;
5069 isn_T *save_instr = ectx->ec_instr;
5070 int save_iidx = ectx->ec_iidx;
5071 char_u *res;
5072
5073 ectx->ec_instr = substitute_instr->subs_instr;
5074 if (exec_instructions(ectx) == OK)
Bram Moolenaar90193e62021-04-04 20:49:50 +02005075 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005076 typval_T *tv = STACK_TV_BOT(-1);
5077
Bram Moolenaar27523602021-06-05 21:36:19 +02005078 res = typval2string(tv, TRUE);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005079 --ectx->ec_stack.ga_len;
5080 clear_tv(tv);
Bram Moolenaar90193e62021-04-04 20:49:50 +02005081 }
5082 else
5083 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005084 substitute_instr->subs_status = FAIL;
5085 res = vim_strsave((char_u *)"");
Bram Moolenaar90193e62021-04-04 20:49:50 +02005086 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005087
Bram Moolenaar4c137212021-04-19 16:48:48 +02005088 ectx->ec_instr = save_instr;
5089 ectx->ec_iidx = save_iidx;
5090
5091 return res;
5092}
5093
5094/*
5095 * Call a "def" function from old Vim script.
5096 * Return OK or FAIL.
5097 */
5098 int
5099call_def_function(
5100 ufunc_T *ufunc,
5101 int argc_arg, // nr of arguments
5102 typval_T *argv, // arguments
5103 partial_T *partial, // optional partial for context
5104 typval_T *rettv) // return value
5105{
5106 ectx_T ectx; // execution context
5107 int argc = argc_arg;
5108 typval_T *tv;
5109 int idx;
5110 int ret = FAIL;
5111 int defcount = ufunc->uf_args.ga_len - argc;
5112 sctx_T save_current_sctx = current_sctx;
5113 int did_emsg_before = did_emsg_cumul + did_emsg;
5114 int save_suppress_errthrow = suppress_errthrow;
5115 msglist_T **saved_msg_list = NULL;
5116 msglist_T *private_msg_list = NULL;
5117 int save_emsg_silent_def = emsg_silent_def;
5118 int save_did_emsg_def = did_emsg_def;
5119 int orig_funcdepth;
Bram Moolenaare99d4222021-06-13 14:01:26 +02005120 int orig_nesting_level = ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005121
5122// Get pointer to item in the stack.
5123#undef STACK_TV
5124#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
5125
5126// Get pointer to item at the bottom of the stack, -1 is the bottom.
5127#undef STACK_TV_BOT
5128#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
5129
5130// Get pointer to a local variable on the stack. Negative for arguments.
5131#undef STACK_TV_VAR
5132#define STACK_TV_VAR(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_frame_idx + STACK_FRAME_SIZE + idx)
5133
5134 if (ufunc->uf_def_status == UF_NOT_COMPILED
5135 || ufunc->uf_def_status == UF_COMPILE_ERROR
Bram Moolenaar139575d2022-03-15 19:29:30 +00005136 || (func_needs_compiling(ufunc, get_compile_type(ufunc))
5137 && compile_def_function(ufunc, FALSE,
5138 get_compile_type(ufunc), NULL) == FAIL))
Bram Moolenaar4c137212021-04-19 16:48:48 +02005139 {
5140 if (did_emsg_cumul + did_emsg == did_emsg_before)
5141 semsg(_(e_function_is_not_compiled_str),
5142 printable_func_name(ufunc));
5143 return FAIL;
5144 }
5145
5146 {
5147 // Check the function was really compiled.
5148 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5149 + ufunc->uf_dfunc_idx;
5150 if (INSTRUCTIONS(dfunc) == NULL)
5151 {
5152 iemsg("using call_def_function() on not compiled function");
5153 return FAIL;
5154 }
5155 }
5156
5157 // If depth of calling is getting too high, don't execute the function.
5158 orig_funcdepth = funcdepth_get();
5159 if (funcdepth_increment() == FAIL)
5160 return FAIL;
5161
5162 CLEAR_FIELD(ectx);
5163 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
5164 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
Bram Moolenaar35578162021-08-02 19:10:38 +02005165 if (GA_GROW_FAILS(&ectx.ec_stack, 20))
Bram Moolenaar4c137212021-04-19 16:48:48 +02005166 {
5167 funcdepth_decrement();
5168 return FAIL;
5169 }
5170 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
5171 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
5172 ectx.ec_did_emsg_before = did_emsg_before;
Bram Moolenaare99d4222021-06-13 14:01:26 +02005173 ++ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005174
5175 idx = argc - ufunc->uf_args.ga_len;
5176 if (idx > 0 && ufunc->uf_va_name == NULL)
5177 {
5178 if (idx == 1)
5179 emsg(_(e_one_argument_too_many));
5180 else
5181 semsg(_(e_nr_arguments_too_many), idx);
5182 goto failed_early;
5183 }
Bram Moolenaarc6d71532021-06-05 18:49:38 +02005184 idx = argc - ufunc->uf_args.ga_len + ufunc->uf_def_args.ga_len;
5185 if (idx < 0)
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02005186 {
5187 if (idx == -1)
5188 emsg(_(e_one_argument_too_few));
5189 else
5190 semsg(_(e_nr_arguments_too_few), -idx);
5191 goto failed_early;
5192 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005193
5194 // Put arguments on the stack, but no more than what the function expects.
5195 // A lambda can be called with more arguments than it uses.
5196 for (idx = 0; idx < argc
5197 && (ufunc->uf_va_name != NULL || idx < ufunc->uf_args.ga_len);
5198 ++idx)
5199 {
5200 if (idx >= ufunc->uf_args.ga_len - ufunc->uf_def_args.ga_len
5201 && argv[idx].v_type == VAR_SPECIAL
5202 && argv[idx].vval.v_number == VVAL_NONE)
5203 {
5204 // Use the default value.
5205 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
5206 }
5207 else
5208 {
5209 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
5210 && check_typval_arg_type(
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02005211 ufunc->uf_arg_types[idx], &argv[idx],
5212 NULL, idx + 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005213 goto failed_early;
5214 copy_tv(&argv[idx], STACK_TV_BOT(0));
5215 }
5216 ++ectx.ec_stack.ga_len;
5217 }
5218
5219 // Turn varargs into a list. Empty list if no args.
5220 if (ufunc->uf_va_name != NULL)
5221 {
5222 int vararg_count = argc - ufunc->uf_args.ga_len;
5223
5224 if (vararg_count < 0)
5225 vararg_count = 0;
5226 else
5227 argc -= vararg_count;
5228 if (exe_newlist(vararg_count, &ectx) == FAIL)
5229 goto failed_early;
5230
5231 // Check the type of the list items.
5232 tv = STACK_TV_BOT(-1);
5233 if (ufunc->uf_va_type != NULL
5234 && ufunc->uf_va_type != &t_list_any
5235 && ufunc->uf_va_type->tt_member != &t_any
5236 && tv->vval.v_list != NULL)
5237 {
5238 type_T *expected = ufunc->uf_va_type->tt_member;
5239 listitem_T *li = tv->vval.v_list->lv_first;
5240
5241 for (idx = 0; idx < vararg_count; ++idx)
5242 {
5243 if (check_typval_arg_type(expected, &li->li_tv,
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02005244 NULL, argc + idx + 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005245 goto failed_early;
5246 li = li->li_next;
5247 }
5248 }
5249
5250 if (defcount > 0)
5251 // Move varargs list to below missing default arguments.
5252 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
5253 --ectx.ec_stack.ga_len;
5254 }
5255
5256 // Make space for omitted arguments, will store default value below.
5257 // Any varargs list goes after them.
5258 if (defcount > 0)
5259 for (idx = 0; idx < defcount; ++idx)
5260 {
5261 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
5262 ++ectx.ec_stack.ga_len;
5263 }
5264 if (ufunc->uf_va_name != NULL)
5265 ++ectx.ec_stack.ga_len;
5266
5267 // Frame pointer points to just after arguments.
5268 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
5269 ectx.ec_initial_frame_idx = ectx.ec_frame_idx;
5270
5271 {
5272 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5273 + ufunc->uf_dfunc_idx;
5274 ufunc_T *base_ufunc = dfunc->df_ufunc;
5275
5276 // "uf_partial" is on the ufunc that "df_ufunc" points to, as is done
5277 // by copy_func().
5278 if (partial != NULL || base_ufunc->uf_partial != NULL)
5279 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005280 ectx.ec_outer_ref = ALLOC_CLEAR_ONE(outer_ref_T);
5281 if (ectx.ec_outer_ref == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005282 goto failed_early;
5283 if (partial != NULL)
5284 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00005285 outer_T *outer = get_pt_outer(partial);
5286
5287 if (outer->out_stack == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005288 {
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00005289 if (current_ectx != NULL)
5290 {
5291 if (current_ectx->ec_outer_ref != NULL
5292 && current_ectx->ec_outer_ref->or_outer != NULL)
5293 ectx.ec_outer_ref->or_outer =
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005294 current_ectx->ec_outer_ref->or_outer;
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00005295 }
5296 // Should there be an error here?
Bram Moolenaar4c137212021-04-19 16:48:48 +02005297 }
5298 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005299 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00005300 ectx.ec_outer_ref->or_outer = outer;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005301 ++partial->pt_refcount;
5302 ectx.ec_outer_ref->or_partial = partial;
5303 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005304 }
5305 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005306 {
5307 ectx.ec_outer_ref->or_outer = &base_ufunc->uf_partial->pt_outer;
5308 ++base_ufunc->uf_partial->pt_refcount;
5309 ectx.ec_outer_ref->or_partial = base_ufunc->uf_partial;
5310 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005311 }
5312 }
5313
5314 // dummy frame entries
5315 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
5316 {
5317 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
5318 ++ectx.ec_stack.ga_len;
5319 }
5320
5321 {
5322 // Reserve space for local variables and any closure reference count.
5323 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5324 + ufunc->uf_dfunc_idx;
5325
Bram Moolenaar5cd64792021-12-25 18:23:24 +00005326 // Initialize variables to zero. That avoids having to generate
5327 // initializing instructions for "var nr: number", "var x: any", etc.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005328 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaar5cd64792021-12-25 18:23:24 +00005329 {
5330 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
5331 STACK_TV_VAR(idx)->vval.v_number = 0;
5332 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005333 ectx.ec_stack.ga_len += dfunc->df_varcount;
5334 if (dfunc->df_has_closure)
5335 {
5336 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
5337 STACK_TV_VAR(idx)->vval.v_number = 0;
5338 ++ectx.ec_stack.ga_len;
5339 }
5340
5341 ectx.ec_instr = INSTRUCTIONS(dfunc);
5342 }
5343
5344 // Following errors are in the function, not the caller.
5345 // Commands behave like vim9script.
5346 estack_push_ufunc(ufunc, 1);
5347 current_sctx = ufunc->uf_script_ctx;
5348 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
5349
5350 // Use a specific location for storing error messages to be converted to an
5351 // exception.
5352 saved_msg_list = msg_list;
5353 msg_list = &private_msg_list;
5354
5355 // Do turn errors into exceptions.
5356 suppress_errthrow = FALSE;
5357
5358 // Do not delete the function while executing it.
5359 ++ufunc->uf_calls;
5360
5361 // When ":silent!" was used before calling then we still abort the
5362 // function. If ":silent!" is used in the function then we don't.
5363 emsg_silent_def = emsg_silent;
5364 did_emsg_def = 0;
5365
5366 ectx.ec_where.wt_index = 0;
5367 ectx.ec_where.wt_variable = FALSE;
5368
5369 // Execute the instructions until done.
5370 ret = exec_instructions(&ectx);
5371 if (ret == OK)
5372 {
5373 // function finished, get result from the stack.
5374 if (ufunc->uf_ret_type == &t_void)
5375 {
5376 rettv->v_type = VAR_VOID;
5377 }
5378 else
5379 {
5380 tv = STACK_TV_BOT(-1);
5381 *rettv = *tv;
5382 tv->v_type = VAR_UNKNOWN;
5383 }
5384 }
5385
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01005386 // When failed need to unwind the call stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005387 while (ectx.ec_frame_idx != ectx.ec_initial_frame_idx)
5388 func_return(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02005389
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005390 // Deal with any remaining closures, they may be in use somewhere.
5391 if (ectx.ec_funcrefs.ga_len > 0)
Bram Moolenaarf112f302020-12-20 17:47:52 +01005392 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005393 handle_closure_in_use(&ectx, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00005394 ga_clear(&ectx.ec_funcrefs);
Bram Moolenaarf112f302020-12-20 17:47:52 +01005395 }
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005396
Bram Moolenaaree8580e2020-08-28 17:19:07 +02005397 estack_pop();
5398 current_sctx = save_current_sctx;
5399
Bram Moolenaar2336c372021-12-06 15:06:54 +00005400 if (--ufunc->uf_calls <= 0 && ufunc->uf_refcount <= 0)
5401 // Function was unreferenced while being used, free it now.
5402 func_clear_free(ufunc, FALSE);
Bram Moolenaarc970e422021-03-17 15:03:04 +01005403
Bram Moolenaar352134b2020-10-17 22:04:08 +02005404 if (*msg_list != NULL && saved_msg_list != NULL)
5405 {
5406 msglist_T **plist = saved_msg_list;
5407
5408 // Append entries from the current msg_list (uncaught exceptions) to
5409 // the saved msg_list.
5410 while (*plist != NULL)
5411 plist = &(*plist)->next;
5412
5413 *plist = *msg_list;
5414 }
5415 msg_list = saved_msg_list;
5416
Bram Moolenaar4c137212021-04-19 16:48:48 +02005417 if (ectx.ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar02194d22020-10-24 23:08:38 +02005418 {
5419 cmdmod.cmod_filter_regmatch.regprog = NULL;
5420 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005421 cmdmod = ectx.ec_funclocal.floc_save_cmdmod;
Bram Moolenaar02194d22020-10-24 23:08:38 +02005422 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +01005423 emsg_silent_def = save_emsg_silent_def;
5424 did_emsg_def += save_did_emsg_def;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02005425
Bram Moolenaaree8580e2020-08-28 17:19:07 +02005426failed_early:
Bram Moolenaar0d1f55c2022-04-05 17:30:29 +01005427 // Free all arguments and local variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005428 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
5429 clear_tv(STACK_TV(idx));
Bram Moolenaare99d4222021-06-13 14:01:26 +02005430 ex_nesting_level = orig_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02005431
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005432 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01005433 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005434 if (ectx.ec_outer_ref != NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +01005435 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005436 if (ectx.ec_outer_ref->or_outer_allocated)
5437 vim_free(ectx.ec_outer_ref->or_outer);
5438 partial_unref(ectx.ec_outer_ref->or_partial);
5439 vim_free(ectx.ec_outer_ref);
Bram Moolenaar0186e582021-01-10 18:33:11 +01005440 }
5441
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02005442 // Not sure if this is necessary.
5443 suppress_errthrow = save_suppress_errthrow;
5444
Bram Moolenaarb5841b92021-07-15 18:09:53 +02005445 if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before
5446 && !need_rethrow)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005447 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02005448 printable_func_name(ufunc));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01005449 funcdepth_restore(orig_funcdepth);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005450 return ret;
5451}
5452
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005453/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02005454 * List instructions "instr" up to "instr_count" or until ISN_FINISH.
5455 * "ufunc" has the source lines, NULL for the instructions of ISN_SUBSTITUTE.
5456 * "pfx" is prefixed to every line.
5457 */
5458 static void
5459list_instructions(char *pfx, isn_T *instr, int instr_count, ufunc_T *ufunc)
5460{
5461 int line_idx = 0;
5462 int prev_current = 0;
5463 int current;
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02005464 int def_arg_idx = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005465
5466 for (current = 0; current < instr_count; ++current)
5467 {
5468 isn_T *iptr = &instr[current];
5469 char *line;
5470
5471 if (ufunc != NULL)
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02005472 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005473 while (line_idx < iptr->isn_lnum
5474 && line_idx < ufunc->uf_lines.ga_len)
5475 {
5476 if (current > prev_current)
5477 {
5478 msg_puts("\n\n");
5479 prev_current = current;
5480 }
5481 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
5482 if (line != NULL)
5483 msg(line);
5484 }
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02005485 if (iptr->isn_type == ISN_JUMP_IF_ARG_SET)
5486 {
5487 int first_def_arg = ufunc->uf_args.ga_len
5488 - ufunc->uf_def_args.ga_len;
5489
5490 if (def_arg_idx > 0)
5491 msg_puts("\n\n");
5492 msg_start();
5493 msg_puts(" ");
5494 msg_puts(((char **)(ufunc->uf_args.ga_data))[
5495 first_def_arg + def_arg_idx]);
5496 msg_puts(" = ");
5497 msg_puts(((char **)(ufunc->uf_def_args.ga_data))[def_arg_idx++]);
5498 msg_clr_eos();
5499 msg_end();
5500 }
5501 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005502
5503 switch (iptr->isn_type)
5504 {
5505 case ISN_EXEC:
5506 smsg("%s%4d EXEC %s", pfx, current, iptr->isn_arg.string);
5507 break;
Bram Moolenaar20677332021-06-06 17:02:53 +02005508 case ISN_EXEC_SPLIT:
5509 smsg("%s%4d EXEC_SPLIT %s", pfx, current, iptr->isn_arg.string);
5510 break;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00005511 case ISN_EXECRANGE:
5512 smsg("%s%4d EXECRANGE %s", pfx, current, iptr->isn_arg.string);
5513 break;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005514 case ISN_LEGACY_EVAL:
5515 smsg("%s%4d EVAL legacy %s", pfx, current,
5516 iptr->isn_arg.string);
5517 break;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02005518 case ISN_REDIRSTART:
5519 smsg("%s%4d REDIR", pfx, current);
5520 break;
5521 case ISN_REDIREND:
5522 smsg("%s%4d REDIR END%s", pfx, current,
5523 iptr->isn_arg.number ? " append" : "");
5524 break;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005525 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005526#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005527 smsg("%s%4d CEXPR pre %s", pfx, current,
5528 cexpr_get_auname(iptr->isn_arg.number));
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005529#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005530 break;
5531 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005532#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005533 {
5534 cexprref_T *cer = iptr->isn_arg.cexpr.cexpr_ref;
5535
5536 smsg("%s%4d CEXPR core %s%s \"%s\"", pfx, current,
5537 cexpr_get_auname(cer->cer_cmdidx),
5538 cer->cer_forceit ? "!" : "",
5539 cer->cer_cmdline);
5540 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005541#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005542 break;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005543 case ISN_INSTR:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01005544 smsg("%s%4d INSTR", pfx, current);
5545 list_instructions(" ", iptr->isn_arg.instr, INT_MAX, NULL);
5546 msg(" -------------");
5547 break;
5548 case ISN_SOURCE:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005549 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01005550 scriptitem_T *si = SCRIPT_ITEM(iptr->isn_arg.number);
5551
5552 smsg("%s%4d SOURCE %s", pfx, current, si->sn_name);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005553 }
5554 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005555 case ISN_SUBSTITUTE:
5556 {
5557 subs_T *subs = &iptr->isn_arg.subs;
5558
5559 smsg("%s%4d SUBSTITUTE %s", pfx, current, subs->subs_cmd);
5560 list_instructions(" ", subs->subs_instr, INT_MAX, NULL);
5561 msg(" -------------");
5562 }
5563 break;
5564 case ISN_EXECCONCAT:
5565 smsg("%s%4d EXECCONCAT %lld", pfx, current,
5566 (varnumber_T)iptr->isn_arg.number);
5567 break;
5568 case ISN_ECHO:
5569 {
5570 echo_T *echo = &iptr->isn_arg.echo;
5571
5572 smsg("%s%4d %s %d", pfx, current,
5573 echo->echo_with_white ? "ECHO" : "ECHON",
5574 echo->echo_count);
5575 }
5576 break;
5577 case ISN_EXECUTE:
5578 smsg("%s%4d EXECUTE %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02005579 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005580 break;
5581 case ISN_ECHOMSG:
5582 smsg("%s%4d ECHOMSG %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02005583 (varnumber_T)(iptr->isn_arg.number));
5584 break;
5585 case ISN_ECHOCONSOLE:
5586 smsg("%s%4d ECHOCONSOLE %lld", pfx, current,
5587 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005588 break;
5589 case ISN_ECHOERR:
5590 smsg("%s%4d ECHOERR %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02005591 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005592 break;
5593 case ISN_LOAD:
5594 {
5595 if (iptr->isn_arg.number < 0)
5596 smsg("%s%4d LOAD arg[%lld]", pfx, current,
5597 (varnumber_T)(iptr->isn_arg.number
5598 + STACK_FRAME_SIZE));
5599 else
5600 smsg("%s%4d LOAD $%lld", pfx, current,
5601 (varnumber_T)(iptr->isn_arg.number));
5602 }
5603 break;
5604 case ISN_LOADOUTER:
5605 {
Bram Moolenaar95e4dd82022-04-27 22:15:40 +01005606 if (iptr->isn_arg.outer.outer_idx < 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005607 smsg("%s%4d LOADOUTER level %d arg[%d]", pfx, current,
5608 iptr->isn_arg.outer.outer_depth,
5609 iptr->isn_arg.outer.outer_idx
5610 + STACK_FRAME_SIZE);
5611 else
5612 smsg("%s%4d LOADOUTER level %d $%d", pfx, current,
5613 iptr->isn_arg.outer.outer_depth,
5614 iptr->isn_arg.outer.outer_idx);
5615 }
5616 break;
5617 case ISN_LOADV:
5618 smsg("%s%4d LOADV v:%s", pfx, current,
5619 get_vim_var_name(iptr->isn_arg.number));
5620 break;
5621 case ISN_LOADSCRIPT:
5622 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005623 scriptref_T *sref = iptr->isn_arg.script.scriptref;
5624 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
5625 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005626
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005627 sv = get_script_svar(sref, -1);
5628 if (sv == NULL)
5629 smsg("%s%4d LOADSCRIPT [deleted] from %s",
5630 pfx, current, si->sn_name);
5631 else
5632 smsg("%s%4d LOADSCRIPT %s-%d from %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02005633 sv->sv_name,
5634 sref->sref_idx,
5635 si->sn_name);
5636 }
5637 break;
5638 case ISN_LOADS:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01005639 case ISN_LOADEXPORT:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005640 {
5641 scriptitem_T *si = SCRIPT_ITEM(
5642 iptr->isn_arg.loadstore.ls_sid);
5643
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01005644 smsg("%s%4d %s s:%s from %s", pfx, current,
5645 iptr->isn_type == ISN_LOADS ? "LOADS"
5646 : "LOADEXPORT",
5647 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005648 }
5649 break;
5650 case ISN_LOADAUTO:
5651 smsg("%s%4d LOADAUTO %s", pfx, current, iptr->isn_arg.string);
5652 break;
5653 case ISN_LOADG:
5654 smsg("%s%4d LOADG g:%s", pfx, current, iptr->isn_arg.string);
5655 break;
5656 case ISN_LOADB:
5657 smsg("%s%4d LOADB b:%s", pfx, current, iptr->isn_arg.string);
5658 break;
5659 case ISN_LOADW:
5660 smsg("%s%4d LOADW w:%s", pfx, current, iptr->isn_arg.string);
5661 break;
5662 case ISN_LOADT:
5663 smsg("%s%4d LOADT t:%s", pfx, current, iptr->isn_arg.string);
5664 break;
5665 case ISN_LOADGDICT:
5666 smsg("%s%4d LOAD g:", pfx, current);
5667 break;
5668 case ISN_LOADBDICT:
5669 smsg("%s%4d LOAD b:", pfx, current);
5670 break;
5671 case ISN_LOADWDICT:
5672 smsg("%s%4d LOAD w:", pfx, current);
5673 break;
5674 case ISN_LOADTDICT:
5675 smsg("%s%4d LOAD t:", pfx, current);
5676 break;
5677 case ISN_LOADOPT:
5678 smsg("%s%4d LOADOPT %s", pfx, current, iptr->isn_arg.string);
5679 break;
5680 case ISN_LOADENV:
5681 smsg("%s%4d LOADENV %s", pfx, current, iptr->isn_arg.string);
5682 break;
5683 case ISN_LOADREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005684 smsg("%s%4d LOADREG @%c", pfx, current,
5685 (int)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005686 break;
5687
5688 case ISN_STORE:
5689 if (iptr->isn_arg.number < 0)
5690 smsg("%s%4d STORE arg[%lld]", pfx, current,
5691 iptr->isn_arg.number + STACK_FRAME_SIZE);
5692 else
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005693 smsg("%s%4d STORE $%lld", pfx, current,
5694 iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005695 break;
5696 case ISN_STOREOUTER:
Bram Moolenaarf6ced982022-04-28 12:00:49 +01005697 smsg("%s%4d STOREOUTER level %d $%d", pfx, current,
5698 iptr->isn_arg.outer.outer_depth,
5699 iptr->isn_arg.outer.outer_idx);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005700 break;
5701 case ISN_STOREV:
5702 smsg("%s%4d STOREV v:%s", pfx, current,
5703 get_vim_var_name(iptr->isn_arg.number));
5704 break;
5705 case ISN_STOREAUTO:
5706 smsg("%s%4d STOREAUTO %s", pfx, current, iptr->isn_arg.string);
5707 break;
5708 case ISN_STOREG:
5709 smsg("%s%4d STOREG %s", pfx, current, iptr->isn_arg.string);
5710 break;
5711 case ISN_STOREB:
5712 smsg("%s%4d STOREB %s", pfx, current, iptr->isn_arg.string);
5713 break;
5714 case ISN_STOREW:
5715 smsg("%s%4d STOREW %s", pfx, current, iptr->isn_arg.string);
5716 break;
5717 case ISN_STORET:
5718 smsg("%s%4d STORET %s", pfx, current, iptr->isn_arg.string);
5719 break;
5720 case ISN_STORES:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01005721 case ISN_STOREEXPORT:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005722 {
5723 scriptitem_T *si = SCRIPT_ITEM(
5724 iptr->isn_arg.loadstore.ls_sid);
5725
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01005726 smsg("%s%4d %s %s in %s", pfx, current,
5727 iptr->isn_type == ISN_STORES
5728 ? "STORES" : "STOREEXPORT",
Bram Moolenaar4c137212021-04-19 16:48:48 +02005729 iptr->isn_arg.loadstore.ls_name, si->sn_name);
5730 }
5731 break;
5732 case ISN_STORESCRIPT:
5733 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005734 scriptref_T *sref = iptr->isn_arg.script.scriptref;
5735 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
5736 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005737
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005738 sv = get_script_svar(sref, -1);
5739 if (sv == NULL)
5740 smsg("%s%4d STORESCRIPT [deleted] in %s",
5741 pfx, current, si->sn_name);
5742 else
5743 smsg("%s%4d STORESCRIPT %s-%d in %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02005744 sv->sv_name,
5745 sref->sref_idx,
5746 si->sn_name);
5747 }
5748 break;
5749 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00005750 case ISN_STOREFUNCOPT:
5751 smsg("%s%4d %s &%s", pfx, current,
5752 iptr->isn_type == ISN_STOREOPT ? "STOREOPT" : "STOREFUNCOPT",
Bram Moolenaar4c137212021-04-19 16:48:48 +02005753 iptr->isn_arg.storeopt.so_name);
5754 break;
5755 case ISN_STOREENV:
5756 smsg("%s%4d STOREENV $%s", pfx, current, iptr->isn_arg.string);
5757 break;
5758 case ISN_STOREREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005759 smsg("%s%4d STOREREG @%c", pfx, current,
5760 (int)iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005761 break;
5762 case ISN_STORENR:
5763 smsg("%s%4d STORE %lld in $%d", pfx, current,
5764 iptr->isn_arg.storenr.stnr_val,
5765 iptr->isn_arg.storenr.stnr_idx);
5766 break;
5767
5768 case ISN_STOREINDEX:
5769 smsg("%s%4d STOREINDEX %s", pfx, current,
5770 vartype_name(iptr->isn_arg.vartype));
5771 break;
5772
5773 case ISN_STORERANGE:
5774 smsg("%s%4d STORERANGE", pfx, current);
5775 break;
5776
5777 // constants
5778 case ISN_PUSHNR:
5779 smsg("%s%4d PUSHNR %lld", pfx, current,
5780 (varnumber_T)(iptr->isn_arg.number));
5781 break;
5782 case ISN_PUSHBOOL:
5783 case ISN_PUSHSPEC:
5784 smsg("%s%4d PUSH %s", pfx, current,
5785 get_var_special_name(iptr->isn_arg.number));
5786 break;
5787 case ISN_PUSHF:
5788#ifdef FEAT_FLOAT
5789 smsg("%s%4d PUSHF %g", pfx, current, iptr->isn_arg.fnumber);
5790#endif
5791 break;
5792 case ISN_PUSHS:
5793 smsg("%s%4d PUSHS \"%s\"", pfx, current, iptr->isn_arg.string);
5794 break;
5795 case ISN_PUSHBLOB:
5796 {
5797 char_u *r;
5798 char_u numbuf[NUMBUFLEN];
5799 char_u *tofree;
5800
5801 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
5802 smsg("%s%4d PUSHBLOB %s", pfx, current, r);
5803 vim_free(tofree);
5804 }
5805 break;
5806 case ISN_PUSHFUNC:
5807 {
5808 char *name = (char *)iptr->isn_arg.string;
5809
5810 smsg("%s%4d PUSHFUNC \"%s\"", pfx, current,
5811 name == NULL ? "[none]" : name);
5812 }
5813 break;
5814 case ISN_PUSHCHANNEL:
5815#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar397a87a2022-03-20 21:14:15 +00005816 smsg("%s%4d PUSHCHANNEL 0", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005817#endif
5818 break;
5819 case ISN_PUSHJOB:
5820#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar397a87a2022-03-20 21:14:15 +00005821 smsg("%s%4d PUSHJOB \"no process\"", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005822#endif
5823 break;
5824 case ISN_PUSHEXC:
5825 smsg("%s%4d PUSH v:exception", pfx, current);
5826 break;
Bram Moolenaar06b77222022-01-25 15:51:56 +00005827 case ISN_AUTOLOAD:
5828 smsg("%s%4d AUTOLOAD %s", pfx, current, iptr->isn_arg.string);
5829 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005830 case ISN_UNLET:
5831 smsg("%s%4d UNLET%s %s", pfx, current,
5832 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
5833 iptr->isn_arg.unlet.ul_name);
5834 break;
5835 case ISN_UNLETENV:
5836 smsg("%s%4d UNLETENV%s $%s", pfx, current,
5837 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
5838 iptr->isn_arg.unlet.ul_name);
5839 break;
5840 case ISN_UNLETINDEX:
5841 smsg("%s%4d UNLETINDEX", pfx, current);
5842 break;
5843 case ISN_UNLETRANGE:
5844 smsg("%s%4d UNLETRANGE", pfx, current);
5845 break;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02005846 case ISN_LOCKUNLOCK:
5847 smsg("%s%4d LOCKUNLOCK %s", pfx, current, iptr->isn_arg.string);
5848 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005849 case ISN_LOCKCONST:
5850 smsg("%s%4d LOCKCONST", pfx, current);
5851 break;
5852 case ISN_NEWLIST:
5853 smsg("%s%4d NEWLIST size %lld", pfx, current,
5854 (varnumber_T)(iptr->isn_arg.number));
5855 break;
5856 case ISN_NEWDICT:
5857 smsg("%s%4d NEWDICT size %lld", pfx, current,
5858 (varnumber_T)(iptr->isn_arg.number));
5859 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00005860 case ISN_NEWPARTIAL:
5861 smsg("%s%4d NEWPARTIAL", pfx, current);
5862 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005863
5864 // function call
5865 case ISN_BCALL:
5866 {
5867 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
5868
5869 smsg("%s%4d BCALL %s(argc %d)", pfx, current,
5870 internal_func_name(cbfunc->cbf_idx),
5871 cbfunc->cbf_argcount);
5872 }
5873 break;
5874 case ISN_DCALL:
5875 {
5876 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
5877 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
5878 + cdfunc->cdf_idx;
5879
5880 smsg("%s%4d DCALL %s(argc %d)", pfx, current,
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005881 printable_func_name(df->df_ufunc),
5882 cdfunc->cdf_argcount);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005883 }
5884 break;
5885 case ISN_UCALL:
5886 {
5887 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
5888
5889 smsg("%s%4d UCALL %s(argc %d)", pfx, current,
5890 cufunc->cuf_name, cufunc->cuf_argcount);
5891 }
5892 break;
5893 case ISN_PCALL:
5894 {
5895 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
5896
5897 smsg("%s%4d PCALL%s (argc %d)", pfx, current,
5898 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
5899 }
5900 break;
5901 case ISN_PCALL_END:
5902 smsg("%s%4d PCALL end", pfx, current);
5903 break;
5904 case ISN_RETURN:
5905 smsg("%s%4d RETURN", pfx, current);
5906 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02005907 case ISN_RETURN_VOID:
5908 smsg("%s%4d RETURN void", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005909 break;
5910 case ISN_FUNCREF:
5911 {
5912 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar38453522021-11-28 22:00:12 +00005913 char_u *name;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005914
Bram Moolenaar38453522021-11-28 22:00:12 +00005915 if (funcref->fr_func_name == NULL)
5916 {
5917 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
5918 + funcref->fr_dfunc_idx;
5919 name = df->df_ufunc->uf_name;
5920 }
5921 else
5922 name = funcref->fr_func_name;
5923 smsg("%s%4d FUNCREF %s", pfx, current, name);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005924 }
5925 break;
5926
5927 case ISN_NEWFUNC:
5928 {
5929 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
5930
5931 smsg("%s%4d NEWFUNC %s %s", pfx, current,
5932 newfunc->nf_lambda, newfunc->nf_global);
5933 }
5934 break;
5935
5936 case ISN_DEF:
5937 {
5938 char_u *name = iptr->isn_arg.string;
5939
5940 smsg("%s%4d DEF %s", pfx, current,
5941 name == NULL ? (char_u *)"" : name);
5942 }
5943 break;
5944
5945 case ISN_JUMP:
5946 {
5947 char *when = "?";
5948
5949 switch (iptr->isn_arg.jump.jump_when)
5950 {
5951 case JUMP_ALWAYS:
5952 when = "JUMP";
5953 break;
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005954 case JUMP_NEVER:
5955 iemsg("JUMP_NEVER should not be used");
5956 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005957 case JUMP_AND_KEEP_IF_TRUE:
5958 when = "JUMP_AND_KEEP_IF_TRUE";
5959 break;
5960 case JUMP_IF_FALSE:
5961 when = "JUMP_IF_FALSE";
5962 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005963 case JUMP_IF_COND_FALSE:
5964 when = "JUMP_IF_COND_FALSE";
5965 break;
5966 case JUMP_IF_COND_TRUE:
5967 when = "JUMP_IF_COND_TRUE";
5968 break;
5969 }
5970 smsg("%s%4d %s -> %d", pfx, current, when,
5971 iptr->isn_arg.jump.jump_where);
5972 }
5973 break;
5974
5975 case ISN_JUMP_IF_ARG_SET:
5976 smsg("%s%4d JUMP_IF_ARG_SET arg[%d] -> %d", pfx, current,
5977 iptr->isn_arg.jumparg.jump_arg_off + STACK_FRAME_SIZE,
5978 iptr->isn_arg.jump.jump_where);
5979 break;
5980
5981 case ISN_FOR:
5982 {
5983 forloop_T *forloop = &iptr->isn_arg.forloop;
5984
5985 smsg("%s%4d FOR $%d -> %d", pfx, current,
5986 forloop->for_idx, forloop->for_end);
5987 }
5988 break;
5989
5990 case ISN_TRY:
5991 {
Bram Moolenaar0d807102021-12-21 09:42:09 +00005992 try_T *try = &iptr->isn_arg.tryref;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005993
5994 if (try->try_ref->try_finally == 0)
5995 smsg("%s%4d TRY catch -> %d, endtry -> %d",
5996 pfx, current,
5997 try->try_ref->try_catch,
5998 try->try_ref->try_endtry);
5999 else
6000 smsg("%s%4d TRY catch -> %d, finally -> %d, endtry -> %d",
6001 pfx, current,
6002 try->try_ref->try_catch,
6003 try->try_ref->try_finally,
6004 try->try_ref->try_endtry);
6005 }
6006 break;
6007 case ISN_CATCH:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006008 smsg("%s%4d CATCH", pfx, current);
6009 break;
6010 case ISN_TRYCONT:
6011 {
6012 trycont_T *trycont = &iptr->isn_arg.trycont;
6013
6014 smsg("%s%4d TRY-CONTINUE %d level%s -> %d", pfx, current,
6015 trycont->tct_levels,
6016 trycont->tct_levels == 1 ? "" : "s",
6017 trycont->tct_where);
6018 }
6019 break;
6020 case ISN_FINALLY:
6021 smsg("%s%4d FINALLY", pfx, current);
6022 break;
6023 case ISN_ENDTRY:
6024 smsg("%s%4d ENDTRY", pfx, current);
6025 break;
6026 case ISN_THROW:
6027 smsg("%s%4d THROW", pfx, current);
6028 break;
6029
6030 // expression operations on number
6031 case ISN_OPNR:
6032 case ISN_OPFLOAT:
6033 case ISN_OPANY:
6034 {
6035 char *what;
6036 char *ins;
6037
6038 switch (iptr->isn_arg.op.op_type)
6039 {
6040 case EXPR_MULT: what = "*"; break;
6041 case EXPR_DIV: what = "/"; break;
6042 case EXPR_REM: what = "%"; break;
6043 case EXPR_SUB: what = "-"; break;
6044 case EXPR_ADD: what = "+"; break;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01006045 case EXPR_LSHIFT: what = "<<"; break;
6046 case EXPR_RSHIFT: what = ">>"; break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006047 default: what = "???"; break;
6048 }
6049 switch (iptr->isn_type)
6050 {
6051 case ISN_OPNR: ins = "OPNR"; break;
6052 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
6053 case ISN_OPANY: ins = "OPANY"; break;
6054 default: ins = "???"; break;
6055 }
6056 smsg("%s%4d %s %s", pfx, current, ins, what);
6057 }
6058 break;
6059
6060 case ISN_COMPAREBOOL:
6061 case ISN_COMPARESPECIAL:
Bram Moolenaar7a222242022-03-01 19:23:24 +00006062 case ISN_COMPARENULL:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006063 case ISN_COMPARENR:
6064 case ISN_COMPAREFLOAT:
6065 case ISN_COMPARESTRING:
6066 case ISN_COMPAREBLOB:
6067 case ISN_COMPARELIST:
6068 case ISN_COMPAREDICT:
6069 case ISN_COMPAREFUNC:
6070 case ISN_COMPAREANY:
6071 {
6072 char *p;
6073 char buf[10];
6074 char *type;
6075
6076 switch (iptr->isn_arg.op.op_type)
6077 {
6078 case EXPR_EQUAL: p = "=="; break;
6079 case EXPR_NEQUAL: p = "!="; break;
6080 case EXPR_GREATER: p = ">"; break;
6081 case EXPR_GEQUAL: p = ">="; break;
6082 case EXPR_SMALLER: p = "<"; break;
6083 case EXPR_SEQUAL: p = "<="; break;
6084 case EXPR_MATCH: p = "=~"; break;
6085 case EXPR_IS: p = "is"; break;
6086 case EXPR_ISNOT: p = "isnot"; break;
6087 case EXPR_NOMATCH: p = "!~"; break;
6088 default: p = "???"; break;
6089 }
6090 STRCPY(buf, p);
6091 if (iptr->isn_arg.op.op_ic == TRUE)
6092 strcat(buf, "?");
6093 switch(iptr->isn_type)
6094 {
6095 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
6096 case ISN_COMPARESPECIAL:
6097 type = "COMPARESPECIAL"; break;
Bram Moolenaar7a222242022-03-01 19:23:24 +00006098 case ISN_COMPARENULL: type = "COMPARENULL"; break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006099 case ISN_COMPARENR: type = "COMPARENR"; break;
6100 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
6101 case ISN_COMPARESTRING:
6102 type = "COMPARESTRING"; break;
6103 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
6104 case ISN_COMPARELIST: type = "COMPARELIST"; break;
6105 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
6106 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
6107 case ISN_COMPAREANY: type = "COMPAREANY"; break;
6108 default: type = "???"; break;
6109 }
6110
6111 smsg("%s%4d %s %s", pfx, current, type, buf);
6112 }
6113 break;
6114
6115 case ISN_ADDLIST: smsg("%s%4d ADDLIST", pfx, current); break;
6116 case ISN_ADDBLOB: smsg("%s%4d ADDBLOB", pfx, current); break;
6117
6118 // expression operations
LemonBoy372bcce2022-04-25 12:43:20 +01006119 case ISN_CONCAT:
6120 smsg("%s%4d CONCAT size %lld", pfx, current,
6121 (varnumber_T)(iptr->isn_arg.number));
6122 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006123 case ISN_STRINDEX: smsg("%s%4d STRINDEX", pfx, current); break;
6124 case ISN_STRSLICE: smsg("%s%4d STRSLICE", pfx, current); break;
6125 case ISN_BLOBINDEX: smsg("%s%4d BLOBINDEX", pfx, current); break;
6126 case ISN_BLOBSLICE: smsg("%s%4d BLOBSLICE", pfx, current); break;
6127 case ISN_LISTAPPEND: smsg("%s%4d LISTAPPEND", pfx, current); break;
6128 case ISN_BLOBAPPEND: smsg("%s%4d BLOBAPPEND", pfx, current); break;
6129 case ISN_LISTINDEX: smsg("%s%4d LISTINDEX", pfx, current); break;
6130 case ISN_LISTSLICE: smsg("%s%4d LISTSLICE", pfx, current); break;
6131 case ISN_ANYINDEX: smsg("%s%4d ANYINDEX", pfx, current); break;
6132 case ISN_ANYSLICE: smsg("%s%4d ANYSLICE", pfx, current); break;
6133 case ISN_SLICE: smsg("%s%4d SLICE %lld",
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02006134 pfx, current, iptr->isn_arg.number); break;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02006135 case ISN_GETITEM: smsg("%s%4d ITEM %lld%s", pfx, current,
6136 iptr->isn_arg.getitem.gi_index,
6137 iptr->isn_arg.getitem.gi_with_op ?
6138 " with op" : ""); break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006139 case ISN_MEMBER: smsg("%s%4d MEMBER", pfx, current); break;
6140 case ISN_STRINGMEMBER: smsg("%s%4d MEMBER %s", pfx, current,
6141 iptr->isn_arg.string); break;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02006142 case ISN_CLEARDICT: smsg("%s%4d CLEARDICT", pfx, current); break;
6143 case ISN_USEDICT: smsg("%s%4d USEDICT", pfx, current); break;
6144
Bram Moolenaar4c137212021-04-19 16:48:48 +02006145 case ISN_NEGATENR: smsg("%s%4d NEGATENR", pfx, current); break;
6146
Bram Moolenaar4c137212021-04-19 16:48:48 +02006147 case ISN_CHECKTYPE:
6148 {
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01006149 checktype_T *ct = &iptr->isn_arg.type;
6150 char *tofree;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006151
6152 if (ct->ct_arg_idx == 0)
6153 smsg("%s%4d CHECKTYPE %s stack[%d]", pfx, current,
6154 type_name(ct->ct_type, &tofree),
6155 (int)ct->ct_off);
6156 else
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01006157 smsg("%s%4d CHECKTYPE %s stack[%d] %s %d",
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02006158 pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02006159 type_name(ct->ct_type, &tofree),
6160 (int)ct->ct_off,
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01006161 ct->ct_is_var ? "var": "arg",
Bram Moolenaar4c137212021-04-19 16:48:48 +02006162 (int)ct->ct_arg_idx);
6163 vim_free(tofree);
6164 break;
6165 }
6166 case ISN_CHECKLEN: smsg("%s%4d CHECKLEN %s%d", pfx, current,
6167 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
6168 iptr->isn_arg.checklen.cl_min_len);
6169 break;
6170 case ISN_SETTYPE:
6171 {
6172 char *tofree;
6173
6174 smsg("%s%4d SETTYPE %s", pfx, current,
6175 type_name(iptr->isn_arg.type.ct_type, &tofree));
6176 vim_free(tofree);
6177 break;
6178 }
6179 case ISN_COND2BOOL: smsg("%s%4d COND2BOOL", pfx, current); break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006180 case ISN_2BOOL: if (iptr->isn_arg.tobool.invert)
6181 smsg("%s%4d INVERT %d (!val)", pfx, current,
6182 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006183 else
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006184 smsg("%s%4d 2BOOL %d (!!val)", pfx, current,
6185 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006186 break;
6187 case ISN_2STRING: smsg("%s%4d 2STRING stack[%lld]", pfx, current,
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006188 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006189 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006190 case ISN_2STRING_ANY: smsg("%s%4d 2STRING_ANY stack[%lld]",
6191 pfx, current,
6192 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006193 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006194 case ISN_RANGE: smsg("%s%4d RANGE %s", pfx, current,
6195 iptr->isn_arg.string);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006196 break;
6197 case ISN_PUT:
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01006198 if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE)
Bram Moolenaar4c137212021-04-19 16:48:48 +02006199 smsg("%s%4d PUT %c above range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006200 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006201 else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE)
6202 smsg("%s%4d PUT %c range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006203 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006204 else
6205 smsg("%s%4d PUT %c %ld", pfx, current,
6206 iptr->isn_arg.put.put_regname,
6207 (long)iptr->isn_arg.put.put_lnum);
6208 break;
6209
Bram Moolenaar4c137212021-04-19 16:48:48 +02006210 case ISN_CMDMOD:
6211 {
6212 char_u *buf;
6213 size_t len = produce_cmdmods(
6214 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
6215
6216 buf = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02006217 if (likely(buf != NULL))
Bram Moolenaar4c137212021-04-19 16:48:48 +02006218 {
6219 (void)produce_cmdmods(
6220 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
6221 smsg("%s%4d CMDMOD %s", pfx, current, buf);
6222 vim_free(buf);
6223 }
6224 break;
6225 }
6226 case ISN_CMDMOD_REV: smsg("%s%4d CMDMOD_REV", pfx, current); break;
6227
6228 case ISN_PROF_START:
Bram Moolenaare99d4222021-06-13 14:01:26 +02006229 smsg("%s%4d PROFILE START line %d", pfx, current,
6230 iptr->isn_lnum);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006231 break;
6232
6233 case ISN_PROF_END:
6234 smsg("%s%4d PROFILE END", pfx, current);
6235 break;
6236
Bram Moolenaare99d4222021-06-13 14:01:26 +02006237 case ISN_DEBUG:
Bram Moolenaar8cec9272021-06-23 20:20:53 +02006238 smsg("%s%4d DEBUG line %d-%d varcount %lld", pfx, current,
6239 iptr->isn_arg.debug.dbg_break_lnum + 1,
6240 iptr->isn_lnum,
6241 iptr->isn_arg.debug.dbg_var_names_len);
Bram Moolenaare99d4222021-06-13 14:01:26 +02006242 break;
6243
Bram Moolenaar4c137212021-04-19 16:48:48 +02006244 case ISN_UNPACK: smsg("%s%4d UNPACK %d%s", pfx, current,
6245 iptr->isn_arg.unpack.unp_count,
6246 iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : "");
6247 break;
6248 case ISN_SHUFFLE: smsg("%s%4d SHUFFLE %d up %d", pfx, current,
6249 iptr->isn_arg.shuffle.shfl_item,
6250 iptr->isn_arg.shuffle.shfl_up);
6251 break;
6252 case ISN_DROP: smsg("%s%4d DROP", pfx, current); break;
6253
6254 case ISN_FINISH: // End of list of instructions for ISN_SUBSTITUTE.
6255 return;
6256 }
6257
6258 out_flush(); // output one line at a time
6259 ui_breakcheck();
6260 if (got_int)
6261 break;
6262 }
6263}
6264
6265/*
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02006266 * Handle command line completion for the :disassemble command.
6267 */
6268 void
6269set_context_in_disassemble_cmd(expand_T *xp, char_u *arg)
6270{
6271 char_u *p;
6272
6273 // Default: expand user functions, "debug" and "profile"
6274 xp->xp_context = EXPAND_DISASSEMBLE;
6275 xp->xp_pattern = arg;
6276
6277 // first argument already typed: only user function names
6278 if (*arg != NUL && *(p = skiptowhite(arg)) != NUL)
6279 {
6280 xp->xp_context = EXPAND_USER_FUNC;
6281 xp->xp_pattern = skipwhite(p);
6282 }
6283}
6284
6285/*
6286 * Function given to ExpandGeneric() to obtain the list of :disassemble
6287 * arguments.
6288 */
6289 char_u *
6290get_disassemble_argument(expand_T *xp, int idx)
6291{
6292 if (idx == 0)
6293 return (char_u *)"debug";
6294 if (idx == 1)
6295 return (char_u *)"profile";
6296 return get_user_func_name(xp, idx - 2);
6297}
6298
6299/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01006300 * ":disassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01006301 * We don't really need this at runtime, but we do have tests that require it,
6302 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006303 */
6304 void
6305ex_disassemble(exarg_T *eap)
6306{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01006307 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01006308 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006309 dfunc_T *dfunc;
Bram Moolenaardafef512022-05-21 21:55:55 +01006310 isn_T *instr = NULL; // init to shut up gcc warning
6311 int instr_count = 0; // init to shut up gcc warning
Bram Moolenaar5a01caa2022-05-21 18:56:58 +01006312 compiletype_T compile_type = CT_NONE;
Bram Moolenaare99d4222021-06-13 14:01:26 +02006313
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01006314 ufunc = find_func_by_name(arg, &compile_type);
Bram Moolenaara26b9702020-04-18 19:53:28 +02006315 if (ufunc == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006316 return;
Bram Moolenaare99d4222021-06-13 14:01:26 +02006317 if (func_needs_compiling(ufunc, compile_type)
6318 && compile_def_function(ufunc, FALSE, compile_type, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02006319 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006320 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006321 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006322 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006323 return;
6324 }
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006325 msg((char *)printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006326
6327 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
Bram Moolenaare99d4222021-06-13 14:01:26 +02006328 switch (compile_type)
6329 {
6330 case CT_PROFILE:
Bram Moolenaarf002a412021-01-24 13:34:18 +01006331#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02006332 instr = dfunc->df_instr_prof;
6333 instr_count = dfunc->df_instr_prof_count;
6334 break;
Bram Moolenaarf002a412021-01-24 13:34:18 +01006335#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02006336 // FALLTHROUGH
6337 case CT_NONE:
6338 instr = dfunc->df_instr;
6339 instr_count = dfunc->df_instr_count;
6340 break;
6341 case CT_DEBUG:
6342 instr = dfunc->df_instr_debug;
6343 instr_count = dfunc->df_instr_debug_count;
6344 break;
6345 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006346
Bram Moolenaar4c137212021-04-19 16:48:48 +02006347 list_instructions("", instr, instr_count, ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006348}
6349
6350/*
Bram Moolenaar13106602020-10-04 16:06:05 +02006351 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006352 * list, etc. Mostly like what JavaScript does, except that empty list and
6353 * empty dictionary are FALSE.
6354 */
6355 int
6356tv2bool(typval_T *tv)
6357{
6358 switch (tv->v_type)
6359 {
6360 case VAR_NUMBER:
6361 return tv->vval.v_number != 0;
6362 case VAR_FLOAT:
6363#ifdef FEAT_FLOAT
6364 return tv->vval.v_float != 0.0;
6365#else
6366 break;
6367#endif
6368 case VAR_PARTIAL:
6369 return tv->vval.v_partial != NULL;
6370 case VAR_FUNC:
6371 case VAR_STRING:
6372 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
6373 case VAR_LIST:
6374 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
6375 case VAR_DICT:
6376 return tv->vval.v_dict != NULL
6377 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
6378 case VAR_BOOL:
6379 case VAR_SPECIAL:
6380 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
6381 case VAR_JOB:
6382#ifdef FEAT_JOB_CHANNEL
6383 return tv->vval.v_job != NULL;
6384#else
6385 break;
6386#endif
6387 case VAR_CHANNEL:
6388#ifdef FEAT_JOB_CHANNEL
6389 return tv->vval.v_channel != NULL;
6390#else
6391 break;
6392#endif
6393 case VAR_BLOB:
6394 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
6395 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006396 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006397 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006398 case VAR_INSTR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006399 break;
6400 }
6401 return FALSE;
6402}
6403
Bram Moolenaarea2d4072020-11-12 12:08:51 +01006404 void
6405emsg_using_string_as(typval_T *tv, int as_number)
6406{
6407 semsg(_(as_number ? e_using_string_as_number_str
6408 : e_using_string_as_bool_str),
6409 tv->vval.v_string == NULL
6410 ? (char_u *)"" : tv->vval.v_string);
6411}
6412
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006413/*
6414 * If "tv" is a string give an error and return FAIL.
6415 */
6416 int
6417check_not_string(typval_T *tv)
6418{
6419 if (tv->v_type == VAR_STRING)
6420 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +01006421 emsg_using_string_as(tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006422 clear_tv(tv);
6423 return FAIL;
6424 }
6425 return OK;
6426}
6427
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006428
6429#endif // FEAT_EVAL