blob: cbf9af7ad36f4e00f79d65e133ec04b6d4ae73cf [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 Moolenaar1d84f762022-09-03 21:35:53 +0100104// Get pointer to item in the stack.
105#define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
106
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100107// Get pointer to item relative to the bottom of the stack, -1 is the last one.
Bram Moolenaar11107ba2020-08-15 21:10:16 +0200108#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 +0100109
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100110// Get pointer to a local variable on the stack. Negative for arguments.
111#define STACK_TV_VAR(idx) (((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx + STACK_FRAME_SIZE + idx)
112
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200113 void
114to_string_error(vartype_T vartype)
115{
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200116 semsg(_(e_cannot_convert_str_to_string), vartype_name(vartype));
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200117}
118
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100119/*
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100120 * Return the number of arguments, including optional arguments and any vararg.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100121 */
122 static int
123ufunc_argcount(ufunc_T *ufunc)
124{
125 return ufunc->uf_args.ga_len + (ufunc->uf_va_name != NULL ? 1 : 0);
126}
127
128/*
LemonBoy372bcce2022-04-25 12:43:20 +0100129 * Create a new string from "count" items at the bottom of the stack.
130 * A trailing NUL is appended.
131 * When "count" is zero an empty string is added to the stack.
132 */
133 static int
134exe_concat(int count, ectx_T *ectx)
135{
136 int idx;
137 int len = 0;
138 typval_T *tv;
139 garray_T ga;
140
141 ga_init2(&ga, sizeof(char), 1);
142 // Preallocate enough space for the whole string to avoid having to grow
143 // and copy.
144 for (idx = 0; idx < count; ++idx)
145 {
146 tv = STACK_TV_BOT(idx - count);
147 if (tv->vval.v_string != NULL)
148 len += (int)STRLEN(tv->vval.v_string);
149 }
150
151 if (ga_grow(&ga, len + 1) == FAIL)
152 return FAIL;
153
154 for (idx = 0; idx < count; ++idx)
155 {
156 tv = STACK_TV_BOT(idx - count);
157 ga_concat(&ga, tv->vval.v_string);
158 clear_tv(tv);
159 }
160
161 // add a terminating NUL
162 ga_append(&ga, NUL);
163
164 ectx->ec_stack.ga_len -= count - 1;
165 STACK_TV_BOT(-1)->vval.v_string = ga.ga_data;
166
167 return OK;
168}
169
170/*
Bram Moolenaarfe270812020-04-11 22:31:27 +0200171 * Create a new list from "count" items at the bottom of the stack.
172 * When "count" is zero an empty list is added to the stack.
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100173 * When "count" is -1 a NULL list is added to the stack.
Bram Moolenaarfe270812020-04-11 22:31:27 +0200174 */
175 static int
176exe_newlist(int count, ectx_T *ectx)
177{
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100178 list_T *list = NULL;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200179 int idx;
180 typval_T *tv;
181
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100182 if (count >= 0)
183 {
184 list = list_alloc_with_items(count);
185 if (list == NULL)
186 return FAIL;
187 for (idx = 0; idx < count; ++idx)
188 list_set_item(list, idx, STACK_TV_BOT(idx - count));
189 }
Bram Moolenaarfe270812020-04-11 22:31:27 +0200190
191 if (count > 0)
192 ectx->ec_stack.ga_len -= count - 1;
Bram Moolenaar35578162021-08-02 19:10:38 +0200193 else if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100194 {
195 list_unref(list);
Bram Moolenaarfe270812020-04-11 22:31:27 +0200196 return FAIL;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100197 }
Bram Moolenaarfe270812020-04-11 22:31:27 +0200198 else
199 ++ectx->ec_stack.ga_len;
200 tv = STACK_TV_BOT(-1);
201 tv->v_type = VAR_LIST;
202 tv->vval.v_list = list;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100203 if (list != NULL)
204 ++list->lv_refcount;
205 return OK;
206}
207
208/*
209 * Implementation of ISN_NEWDICT.
210 * Returns FAIL on total failure, MAYBE on error.
211 */
212 static int
213exe_newdict(int count, ectx_T *ectx)
214{
215 dict_T *dict = NULL;
216 dictitem_T *item;
217 char_u *key;
218 int idx;
219 typval_T *tv;
220
221 if (count >= 0)
222 {
223 dict = dict_alloc();
224 if (unlikely(dict == NULL))
225 return FAIL;
226 for (idx = 0; idx < count; ++idx)
227 {
228 // have already checked key type is VAR_STRING
229 tv = STACK_TV_BOT(2 * (idx - count));
230 // check key is unique
231 key = tv->vval.v_string == NULL
232 ? (char_u *)"" : tv->vval.v_string;
233 item = dict_find(dict, key, -1);
234 if (item != NULL)
235 {
236 semsg(_(e_duplicate_key_in_dicitonary), key);
237 dict_unref(dict);
238 return MAYBE;
239 }
240 item = dictitem_alloc(key);
241 clear_tv(tv);
242 if (unlikely(item == NULL))
243 {
244 dict_unref(dict);
245 return FAIL;
246 }
Bram Moolenaar0d1f55c2022-04-05 17:30:29 +0100247 tv = STACK_TV_BOT(2 * (idx - count) + 1);
248 item->di_tv = *tv;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100249 item->di_tv.v_lock = 0;
Bram Moolenaar0d1f55c2022-04-05 17:30:29 +0100250 tv->v_type = VAR_UNKNOWN;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100251 if (dict_add(dict, item) == FAIL)
252 {
253 // can this ever happen?
254 dict_unref(dict);
255 return FAIL;
256 }
257 }
258 }
259
260 if (count > 0)
261 ectx->ec_stack.ga_len -= 2 * count - 1;
262 else if (GA_GROW_FAILS(&ectx->ec_stack, 1))
263 return FAIL;
264 else
265 ++ectx->ec_stack.ga_len;
266 tv = STACK_TV_BOT(-1);
267 tv->v_type = VAR_DICT;
268 tv->v_lock = 0;
269 tv->vval.v_dict = dict;
270 if (dict != NULL)
271 ++dict->dv_refcount;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200272 return OK;
273}
274
275/*
Bram Moolenaar4f8f5422021-06-20 19:28:14 +0200276 * If debug_tick changed check if "ufunc" has a breakpoint and update
277 * "uf_has_breakpoint".
278 */
Bram Moolenaar96923b72022-03-15 15:57:04 +0000279 void
Bram Moolenaar4f8f5422021-06-20 19:28:14 +0200280update_has_breakpoint(ufunc_T *ufunc)
281{
282 if (ufunc->uf_debug_tick != debug_tick)
283 {
284 linenr_T breakpoint;
285
286 ufunc->uf_debug_tick = debug_tick;
287 breakpoint = dbg_find_breakpoint(FALSE, ufunc->uf_name, 0);
288 ufunc->uf_has_breakpoint = breakpoint > 0;
289 }
290}
291
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +0200292static garray_T dict_stack = GA_EMPTY;
293
294/*
295 * Put a value on the dict stack. This consumes "tv".
296 */
297 static int
298dict_stack_save(typval_T *tv)
299{
300 if (dict_stack.ga_growsize == 0)
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000301 ga_init2(&dict_stack, sizeof(typval_T), 10);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +0200302 if (ga_grow(&dict_stack, 1) == FAIL)
303 return FAIL;
304 ((typval_T *)dict_stack.ga_data)[dict_stack.ga_len] = *tv;
305 ++dict_stack.ga_len;
306 return OK;
307}
308
309/*
310 * Get the typval at top of the dict stack.
311 */
312 static typval_T *
313dict_stack_get_tv(void)
314{
315 if (dict_stack.ga_len == 0)
316 return NULL;
317 return ((typval_T *)dict_stack.ga_data) + dict_stack.ga_len - 1;
318}
319
320/*
321 * Get the dict at top of the dict stack.
322 */
323 static dict_T *
324dict_stack_get_dict(void)
325{
326 typval_T *tv;
327
328 if (dict_stack.ga_len == 0)
329 return NULL;
330 tv = ((typval_T *)dict_stack.ga_data) + dict_stack.ga_len - 1;
331 if (tv->v_type == VAR_DICT)
332 return tv->vval.v_dict;
333 return NULL;
334}
335
336/*
337 * Drop an item from the dict stack.
338 */
339 static void
340dict_stack_drop(void)
341{
342 if (dict_stack.ga_len == 0)
343 {
344 iemsg("Dict stack underflow");
345 return;
346 }
347 --dict_stack.ga_len;
348 clear_tv(((typval_T *)dict_stack.ga_data) + dict_stack.ga_len);
349}
350
351/*
352 * Drop items from the dict stack until the length is equal to "len".
353 */
354 static void
355dict_stack_clear(int len)
356{
357 while (dict_stack.ga_len > len)
358 dict_stack_drop();
359}
360
Bram Moolenaar4f8f5422021-06-20 19:28:14 +0200361/*
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +0000362 * Get a pointer to useful "pt_outer" of "pt".
363 */
364 static outer_T *
365get_pt_outer(partial_T *pt)
366{
367 partial_T *ptref = pt->pt_outer_partial;
368
369 if (ptref == NULL)
370 return &pt->pt_outer;
371
372 // partial using partial (recursively)
373 while (ptref->pt_outer_partial != NULL)
374 ptref = ptref->pt_outer_partial;
375 return &ptref->pt_outer;
376}
377
378/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100379 * Call compiled function "cdf_idx" from compiled code.
Bram Moolenaarab360522021-01-10 14:02:28 +0100380 * This adds a stack frame and sets the instruction pointer to the start of the
381 * called function.
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200382 * If "pt" is not null use "pt->pt_outer" for ec_outer_ref->or_outer.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100383 *
384 * Stack has:
385 * - current arguments (already there)
386 * - omitted optional argument (default values) added here
387 * - stack frame:
388 * - pointer to calling function
389 * - Index of next instruction in calling function
390 * - previous frame pointer
391 * - reserved space for local variables
392 */
393 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100394call_dfunc(
395 int cdf_idx,
396 partial_T *pt,
397 int argcount_arg,
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100398 ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100399{
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100400 int argcount = argcount_arg;
401 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + cdf_idx;
402 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200403 int did_emsg_before = did_emsg_cumul + did_emsg;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100404 int arg_to_add;
405 int vararg_count = 0;
406 int varcount;
407 int idx;
408 estack_T *entry;
409 funclocal_T *floc = NULL;
Bram Moolenaar648594e2021-07-11 17:55:01 +0200410 int res = OK;
Bram Moolenaar139575d2022-03-15 19:29:30 +0000411 compiletype_T compile_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100412
413 if (dfunc->df_deleted)
414 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100415 // don't use ufunc->uf_name, it may have been freed
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000416 emsg_funcname(e_function_was_deleted_str,
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100417 dfunc->df_name == NULL ? (char_u *)"unknown" : dfunc->df_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100418 return FAIL;
419 }
420
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100421#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +0100422 if (do_profiling == PROF_YES)
423 {
Bram Moolenaar35578162021-08-02 19:10:38 +0200424 if (GA_GROW_OK(&profile_info_ga, 1))
Bram Moolenaar12d26532021-02-19 19:13:21 +0100425 {
426 profinfo_T *info = ((profinfo_T *)profile_info_ga.ga_data)
427 + profile_info_ga.ga_len;
428 ++profile_info_ga.ga_len;
429 CLEAR_POINTER(info);
430 profile_may_start_func(info, ufunc,
431 (((dfunc_T *)def_functions.ga_data)
432 + ectx->ec_dfunc_idx)->df_ufunc);
433 }
Bram Moolenaar12d26532021-02-19 19:13:21 +0100434 }
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100435#endif
436
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200437 // When debugging and using "cont" switches to the not-debugged
438 // instructions, may need to still compile them.
Bram Moolenaar139575d2022-03-15 19:29:30 +0000439 compile_type = get_compile_type(ufunc);
440 if (func_needs_compiling(ufunc, compile_type))
Bram Moolenaar648594e2021-07-11 17:55:01 +0200441 {
Bram Moolenaar139575d2022-03-15 19:29:30 +0000442 res = compile_def_function(ufunc, FALSE, compile_type, NULL);
Bram Moolenaar648594e2021-07-11 17:55:01 +0200443
444 // compile_def_function() may cause def_functions.ga_data to change
445 dfunc = ((dfunc_T *)def_functions.ga_data) + cdf_idx;
446 }
447 if (res == FAIL || INSTRUCTIONS(dfunc) == NULL)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200448 {
449 if (did_emsg_cumul + did_emsg == did_emsg_before)
450 semsg(_(e_function_is_not_compiled_str),
451 printable_func_name(ufunc));
452 return FAIL;
453 }
454
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200455 if (ufunc->uf_va_name != NULL)
456 {
Bram Moolenaarfe270812020-04-11 22:31:27 +0200457 // Need to make a list out of the vararg arguments.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200458 // Stack at time of call with 2 varargs:
459 // normal_arg
460 // optional_arg
461 // vararg_1
462 // vararg_2
Bram Moolenaarfe270812020-04-11 22:31:27 +0200463 // After creating the list:
464 // normal_arg
465 // optional_arg
466 // vararg-list
467 // With missing optional arguments we get:
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200468 // normal_arg
Bram Moolenaarfe270812020-04-11 22:31:27 +0200469 // After creating the list
470 // normal_arg
471 // (space for optional_arg)
472 // vararg-list
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200473 vararg_count = argcount - ufunc->uf_args.ga_len;
474 if (vararg_count < 0)
475 vararg_count = 0;
476 else
477 argcount -= vararg_count;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200478 if (exe_newlist(vararg_count, ectx) == FAIL)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200479 return FAIL;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200480
481 vararg_count = 1;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200482 }
483
Bram Moolenaarfe270812020-04-11 22:31:27 +0200484 arg_to_add = ufunc->uf_args.ga_len - argcount;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200485 if (arg_to_add < 0)
486 {
Matvey Tarasovd14bb1a2022-06-29 13:18:27 +0100487 semsg(NGETTEXT(e_one_argument_too_many, e_nr_arguments_too_many,
488 -arg_to_add), -arg_to_add);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200489 return FAIL;
490 }
Bram Moolenaarcd1cda22022-02-16 21:48:25 +0000491 else if (arg_to_add > ufunc->uf_def_args.ga_len)
492 {
493 int missing = arg_to_add - ufunc->uf_def_args.ga_len;
494
Matvey Tarasovd14bb1a2022-06-29 13:18:27 +0100495 semsg(NGETTEXT(e_one_argument_too_few, e_nr_arguments_too_few,
496 missing), missing);
Bram Moolenaarcd1cda22022-02-16 21:48:25 +0000497 return FAIL;
498 }
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200499
500 // Reserve space for:
501 // - missing arguments
502 // - stack frame
503 // - local variables
504 // - if needed: a counter for number of closures created in
505 // ectx->ec_funcrefs.
506 varcount = dfunc->df_varcount + dfunc->df_has_closure;
Bram Moolenaar35578162021-08-02 19:10:38 +0200507 if (GA_GROW_FAILS(&ectx->ec_stack, arg_to_add + STACK_FRAME_SIZE + varcount))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100508 return FAIL;
509
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100510 // If depth of calling is getting too high, don't execute the function.
511 if (funcdepth_increment() == FAIL)
512 return FAIL;
Bram Moolenaare99d4222021-06-13 14:01:26 +0200513 ++ex_nesting_level;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100514
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100515 // Only make a copy of funclocal if it contains something to restore.
Bram Moolenaar4c137212021-04-19 16:48:48 +0200516 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100517 {
518 floc = ALLOC_ONE(funclocal_T);
519 if (floc == NULL)
520 return FAIL;
Bram Moolenaar4c137212021-04-19 16:48:48 +0200521 *floc = ectx->ec_funclocal;
522 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100523 }
524
Bram Moolenaarfe270812020-04-11 22:31:27 +0200525 // Move the vararg-list to below the missing optional arguments.
526 if (vararg_count > 0 && arg_to_add > 0)
527 *STACK_TV_BOT(arg_to_add - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100528
529 // Reserve space for omitted optional arguments, filled in soon.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200530 for (idx = 0; idx < arg_to_add; ++idx)
Bram Moolenaarfe270812020-04-11 22:31:27 +0200531 STACK_TV_BOT(idx - vararg_count)->v_type = VAR_UNKNOWN;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200532 ectx->ec_stack.ga_len += arg_to_add;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100533
534 // Store current execution state in stack frame for ISN_RETURN.
Bram Moolenaar0186e582021-01-10 18:33:11 +0100535 STACK_TV_BOT(STACK_FRAME_FUNC_OFF)->vval.v_number = ectx->ec_dfunc_idx;
536 STACK_TV_BOT(STACK_FRAME_IIDX_OFF)->vval.v_number = ectx->ec_iidx;
Bram Moolenaar5930ddc2021-04-26 20:32:59 +0200537 STACK_TV_BOT(STACK_FRAME_INSTR_OFF)->vval.v_string = (void *)ectx->ec_instr;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200538 STACK_TV_BOT(STACK_FRAME_OUTER_OFF)->vval.v_string =
539 (void *)ectx->ec_outer_ref;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100540 STACK_TV_BOT(STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string = (void *)floc;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100541 STACK_TV_BOT(STACK_FRAME_IDX_OFF)->vval.v_number = ectx->ec_frame_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200542 ectx->ec_frame_idx = ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100543
544 // Initialize local variables
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200545 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaar5cd64792021-12-25 18:23:24 +0000546 {
547 typval_T *tv = STACK_TV_BOT(STACK_FRAME_SIZE + idx);
548
549 tv->v_type = VAR_NUMBER;
550 tv->vval.v_number = 0;
551 }
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200552 if (dfunc->df_has_closure)
553 {
554 typval_T *tv = STACK_TV_BOT(STACK_FRAME_SIZE + dfunc->df_varcount);
555
556 tv->v_type = VAR_NUMBER;
557 tv->vval.v_number = 0;
558 }
559 ectx->ec_stack.ga_len += STACK_FRAME_SIZE + varcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100560
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +0100561 if (pt != NULL || ufunc->uf_partial != NULL
562 || (ufunc->uf_flags & FC_CLOSURE))
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100563 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200564 outer_ref_T *ref = ALLOC_CLEAR_ONE(outer_ref_T);
Bram Moolenaar0186e582021-01-10 18:33:11 +0100565
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200566 if (ref == NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +0100567 return FAIL;
568 if (pt != NULL)
569 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +0000570 ref->or_outer = get_pt_outer(pt);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200571 ++pt->pt_refcount;
572 ref->or_partial = pt;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100573 }
574 else if (ufunc->uf_partial != NULL)
575 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +0000576 ref->or_outer = get_pt_outer(ufunc->uf_partial);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200577 ++ufunc->uf_partial->pt_refcount;
578 ref->or_partial = ufunc->uf_partial;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100579 }
580 else
581 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200582 ref->or_outer = ALLOC_CLEAR_ONE(outer_T);
Dominique Pelle5a9e5842021-07-24 19:32:12 +0200583 if (unlikely(ref->or_outer == NULL))
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200584 {
585 vim_free(ref);
586 return FAIL;
587 }
588 ref->or_outer_allocated = TRUE;
589 ref->or_outer->out_stack = &ectx->ec_stack;
590 ref->or_outer->out_frame_idx = ectx->ec_frame_idx;
591 if (ectx->ec_outer_ref != NULL)
592 ref->or_outer->out_up = ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100593 }
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200594 ectx->ec_outer_ref = ref;
Bram Moolenaarab360522021-01-10 14:02:28 +0100595 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100596 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200597 ectx->ec_outer_ref = NULL;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100598
Bram Moolenaarc970e422021-03-17 15:03:04 +0100599 ++ufunc->uf_calls;
600
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100601 // Set execution state to the start of the called function.
602 ectx->ec_dfunc_idx = cdf_idx;
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100603 ectx->ec_instr = INSTRUCTIONS(dfunc);
Bram Moolenaarb2049902021-01-24 12:53:53 +0100604 entry = estack_push_ufunc(ufunc, 1);
Bram Moolenaarc620c052020-07-08 15:16:19 +0200605 if (entry != NULL)
606 {
607 // Set the script context to the script where the function was defined.
Bram Moolenaarc70fe462021-04-17 17:59:19 +0200608 // Save the current context so it can be restored on return.
609 entry->es_save_sctx = current_sctx;
610 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200611 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100612
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +0200613 // Start execution at the first instruction.
614 ectx->ec_iidx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100615
616 return OK;
617}
618
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000619// Double linked list of funcstack_T in use.
620static funcstack_T *first_funcstack = NULL;
621
622 static void
623add_funcstack_to_list(funcstack_T *funcstack)
624{
625 // Link in list of funcstacks.
626 if (first_funcstack != NULL)
627 first_funcstack->fs_prev = funcstack;
628 funcstack->fs_next = first_funcstack;
629 funcstack->fs_prev = NULL;
630 first_funcstack = funcstack;
631}
632
633 static void
634remove_funcstack_from_list(funcstack_T *funcstack)
635{
636 if (funcstack->fs_prev == NULL)
637 first_funcstack = funcstack->fs_next;
638 else
639 funcstack->fs_prev->fs_next = funcstack->fs_next;
640 if (funcstack->fs_next != NULL)
641 funcstack->fs_next->fs_prev = funcstack->fs_prev;
642}
643
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100644/*
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200645 * Used when returning from a function: Check if any closure is still
646 * referenced. If so then move the arguments and variables to a separate piece
647 * of stack to be used when the closure is called.
648 * When "free_arguments" is TRUE the arguments are to be freed.
649 * Returns FAIL when out of memory.
650 */
651 static int
652handle_closure_in_use(ectx_T *ectx, int free_arguments)
653{
654 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
655 + ectx->ec_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200656 int argcount;
657 int top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200658 int idx;
659 typval_T *tv;
660 int closure_in_use = FALSE;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200661 garray_T *gap = &ectx->ec_funcrefs;
662 varnumber_T closure_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200663
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200664 if (dfunc->df_ufunc == NULL)
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200665 return OK; // function was freed
666 if (dfunc->df_has_closure == 0)
667 return OK; // no closures
668 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + dfunc->df_varcount);
669 closure_count = tv->vval.v_number;
670 if (closure_count == 0)
671 return OK; // no funcrefs created
672
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200673 argcount = ufunc_argcount(dfunc->df_ufunc);
674 top = ectx->ec_frame_idx - argcount;
675
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200676 // Check if any created closure is still in use.
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200677 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200678 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200679 partial_T *pt;
680 int off = gap->ga_len - closure_count + idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200681
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200682 if (off < 0)
683 continue; // count is off or already done
684 pt = ((partial_T **)gap->ga_data)[off];
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200685 if (pt->pt_refcount > 1)
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200686 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200687 int refcount = pt->pt_refcount;
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200688 int i;
689
Dominique Pelleaf4a61a2021-12-27 17:21:41 +0000690 // A Reference in a local variable doesn't count, it gets
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200691 // unreferenced on return.
692 for (i = 0; i < dfunc->df_varcount; ++i)
693 {
694 typval_T *stv = STACK_TV(ectx->ec_frame_idx
695 + STACK_FRAME_SIZE + i);
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200696 if (stv->v_type == VAR_PARTIAL && pt == stv->vval.v_partial)
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200697 --refcount;
698 }
699 if (refcount > 1)
700 {
701 closure_in_use = TRUE;
702 break;
703 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200704 }
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200705 }
706
707 if (closure_in_use)
708 {
709 funcstack_T *funcstack = ALLOC_CLEAR_ONE(funcstack_T);
710 typval_T *stack;
711
712 // A closure is using the arguments and/or local variables.
713 // Move them to the called function.
714 if (funcstack == NULL)
715 return FAIL;
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000716
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200717 funcstack->fs_var_offset = argcount + STACK_FRAME_SIZE;
718 funcstack->fs_ga.ga_len = funcstack->fs_var_offset + dfunc->df_varcount;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200719 stack = ALLOC_CLEAR_MULT(typval_T, funcstack->fs_ga.ga_len);
720 funcstack->fs_ga.ga_data = stack;
721 if (stack == NULL)
722 {
723 vim_free(funcstack);
724 return FAIL;
725 }
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000726 add_funcstack_to_list(funcstack);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200727
728 // Move or copy the arguments.
729 for (idx = 0; idx < argcount; ++idx)
730 {
731 tv = STACK_TV(top + idx);
732 if (free_arguments)
733 {
734 *(stack + idx) = *tv;
735 tv->v_type = VAR_UNKNOWN;
736 }
737 else
738 copy_tv(tv, stack + idx);
739 }
740 // Move the local variables.
741 for (idx = 0; idx < dfunc->df_varcount; ++idx)
742 {
743 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + idx);
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200744
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200745 // A partial created for a local function, that is also used as a
746 // local variable, has a reference count for the variable, thus
747 // will never go down to zero. When all these refcounts are one
748 // then the funcstack is unused. We need to count how many we have
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000749 // so we know when to check.
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200750 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL)
751 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200752 int i;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200753
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200754 for (i = 0; i < closure_count; ++i)
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200755 if (tv->vval.v_partial == ((partial_T **)gap->ga_data)[
756 gap->ga_len - closure_count + i])
757 ++funcstack->fs_min_refcount;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200758 }
759
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200760 *(stack + funcstack->fs_var_offset + idx) = *tv;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200761 tv->v_type = VAR_UNKNOWN;
762 }
763
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200764 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200765 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200766 partial_T *pt = ((partial_T **)gap->ga_data)[gap->ga_len
767 - closure_count + idx];
768 if (pt->pt_refcount > 1)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200769 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200770 ++funcstack->fs_refcount;
771 pt->pt_funcstack = funcstack;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100772 pt->pt_outer.out_stack = &funcstack->fs_ga;
773 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx - top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200774 }
775 }
776 }
777
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200778 for (idx = 0; idx < closure_count; ++idx)
779 partial_unref(((partial_T **)gap->ga_data)[gap->ga_len
780 - closure_count + idx]);
781 gap->ga_len -= closure_count;
782 if (gap->ga_len == 0)
783 ga_clear(gap);
784
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200785 return OK;
786}
787
788/*
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200789 * Called when a partial is freed or its reference count goes down to one. The
790 * funcstack may be the only reference to the partials in the local variables.
791 * Go over all of them, the funcref and can be freed if all partials
792 * referencing the funcstack have a reference count of one.
793 */
794 void
795funcstack_check_refcount(funcstack_T *funcstack)
796{
797 int i;
798 garray_T *gap = &funcstack->fs_ga;
799 int done = 0;
800
801 if (funcstack->fs_refcount > funcstack->fs_min_refcount)
802 return;
803 for (i = funcstack->fs_var_offset; i < gap->ga_len; ++i)
804 {
805 typval_T *tv = ((typval_T *)gap->ga_data) + i;
806
807 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL
808 && tv->vval.v_partial->pt_funcstack == funcstack
809 && tv->vval.v_partial->pt_refcount == 1)
810 ++done;
811 }
812 if (done == funcstack->fs_min_refcount)
813 {
814 typval_T *stack = gap->ga_data;
815
816 // All partials referencing the funcstack have a reference count of
817 // one, thus the funcstack is no longer of use.
818 for (i = 0; i < gap->ga_len; ++i)
819 clear_tv(stack + i);
820 vim_free(stack);
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000821 remove_funcstack_from_list(funcstack);
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200822 vim_free(funcstack);
823 }
824}
825
826/*
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000827 * For garbage collecting: set references in all variables referenced by
828 * all funcstacks.
829 */
830 int
831set_ref_in_funcstacks(int copyID)
832{
833 funcstack_T *funcstack;
834
835 for (funcstack = first_funcstack; funcstack != NULL;
836 funcstack = funcstack->fs_next)
837 {
838 typval_T *stack = funcstack->fs_ga.ga_data;
839 int i;
840
841 for (i = 0; i < funcstack->fs_ga.ga_len; ++i)
842 if (set_ref_in_item(stack + i, copyID, NULL, NULL))
843 return TRUE; // abort
844 }
845 return FALSE;
846}
847
Bram Moolenaar806a2732022-09-04 15:40:36 +0100848// Ugly static to avoid passing the execution context around through many
849// layers.
850static ectx_T *current_ectx = NULL;
851
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000852/*
Bram Moolenaar806a2732022-09-04 15:40:36 +0100853 * Return TRUE if currently executing a :def function.
854 * Can be used by builtin functions only.
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100855 */
Bram Moolenaar806a2732022-09-04 15:40:36 +0100856 int
857in_def_function(void)
858{
859 return current_ectx != NULL;
860}
861
862/*
Bram Moolenaar98aff652022-09-06 21:02:35 +0100863 * Clear "current_ectx" and return the previous value. To be used when calling
864 * a user function.
865 */
866 ectx_T *
867clear_currrent_ectx(void)
868{
869 ectx_T *r = current_ectx;
870
871 current_ectx = NULL;
872 return r;
873}
874
875 void
876restore_current_ectx(ectx_T *ectx)
877{
878 if (current_ectx != NULL)
879 iemsg("Restoring current_ectx while it is not NULL");
880 current_ectx = ectx;
881}
882
883/*
Bram Moolenaar806a2732022-09-04 15:40:36 +0100884 * Add an entry for a deferred function call to the currently executing
885 * function.
886 * Return the list or NULL when failed.
887 */
888 static list_T *
889add_defer_item(int var_idx, int argcount, ectx_T *ectx)
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100890{
891 typval_T *defer_tv = STACK_TV_VAR(var_idx);
892 list_T *defer_l;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100893 list_T *l;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100894 typval_T listval;
895
896 if (defer_tv->v_type != VAR_LIST)
897 {
Bram Moolenaara5348f22022-09-04 11:42:22 +0100898 // first time, allocate the list
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100899 if (rettv_list_alloc(defer_tv) == FAIL)
Bram Moolenaar806a2732022-09-04 15:40:36 +0100900 return NULL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100901 }
902 defer_l = defer_tv->vval.v_list;
903
904 l = list_alloc_with_items(argcount + 1);
905 if (l == NULL)
Bram Moolenaar806a2732022-09-04 15:40:36 +0100906 return NULL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100907 listval.v_type = VAR_LIST;
908 listval.vval.v_list = l;
909 listval.v_lock = 0;
Bram Moolenaara5348f22022-09-04 11:42:22 +0100910 if (list_insert_tv(defer_l, &listval, defer_l->lv_first) == FAIL)
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100911 {
912 vim_free(l);
Bram Moolenaar806a2732022-09-04 15:40:36 +0100913 return NULL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100914 }
915
Bram Moolenaar806a2732022-09-04 15:40:36 +0100916 return l;
917}
918
919/*
920 * Handle ISN_DEFER. Stack has a function reference and "argcount" arguments.
921 * The local variable that lists deferred functions is "var_idx".
922 * Returns OK or FAIL.
923 */
924 static int
925defer_command(int var_idx, int argcount, ectx_T *ectx)
926{
927 list_T *l = add_defer_item(var_idx, argcount, ectx);
928 int i;
929 typval_T *func_tv;
930
931 if (l == NULL)
932 return FAIL;
933
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100934 func_tv = STACK_TV_BOT(-argcount - 1);
Bram Moolenaarf5fec052022-09-11 11:49:22 +0100935 if (func_tv->v_type != VAR_FUNC && func_tv->v_type != VAR_PARTIAL)
936 {
937 semsg(_(e_expected_str_but_got_str),
938 "function or partial",
939 vartype_name(func_tv->v_type));
940 return FAIL;
941 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100942 list_set_item(l, 0, func_tv);
943
Bram Moolenaarf5fec052022-09-11 11:49:22 +0100944 for (i = 0; i < argcount; ++i)
945 list_set_item(l, i + 1, STACK_TV_BOT(-argcount + i));
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100946 ectx->ec_stack.ga_len -= argcount + 1;
947 return OK;
948}
949
950/*
Bram Moolenaar806a2732022-09-04 15:40:36 +0100951 * Add a deferred function "name" with one argument "arg_tv".
952 * Consumes "name", also on failure.
953 * Only to be called when in_def_function() returns TRUE.
954 */
955 int
956add_defer_function(char_u *name, int argcount, typval_T *argvars)
957{
958 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
959 + current_ectx->ec_dfunc_idx;
960 list_T *l;
961 typval_T func_tv;
962 int i;
963
964 if (dfunc->df_defer_var_idx == 0)
965 {
966 iemsg("df_defer_var_idx is zero");
Bram Moolenaar31ea6bf2022-09-05 10:47:13 +0100967 vim_free(name);
Bram Moolenaar806a2732022-09-04 15:40:36 +0100968 return FAIL;
969 }
Bram Moolenaar806a2732022-09-04 15:40:36 +0100970
Bram Moolenaarf5fec052022-09-11 11:49:22 +0100971 l = add_defer_item(dfunc->df_defer_var_idx - 1, argcount, current_ectx);
Bram Moolenaar806a2732022-09-04 15:40:36 +0100972 if (l == NULL)
973 {
Bram Moolenaar31ea6bf2022-09-05 10:47:13 +0100974 vim_free(name);
Bram Moolenaar806a2732022-09-04 15:40:36 +0100975 return FAIL;
976 }
977
Bram Moolenaar31ea6bf2022-09-05 10:47:13 +0100978 func_tv.v_type = VAR_FUNC;
979 func_tv.v_lock = 0;
980 func_tv.vval.v_string = name;
Bram Moolenaar806a2732022-09-04 15:40:36 +0100981 list_set_item(l, 0, &func_tv);
Bram Moolenaar31ea6bf2022-09-05 10:47:13 +0100982
Bram Moolenaar806a2732022-09-04 15:40:36 +0100983 for (i = 0; i < argcount; ++i)
984 list_set_item(l, i + 1, argvars + i);
985 return OK;
986}
987
988/*
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100989 * Invoked when returning from a function: Invoke any deferred calls.
990 */
991 static void
992invoke_defer_funcs(ectx_T *ectx)
993{
994 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
995 + ectx->ec_dfunc_idx;
996 typval_T *defer_tv = STACK_TV_VAR(dfunc->df_defer_var_idx - 1);
997 listitem_T *li;
998
999 if (defer_tv->v_type != VAR_LIST)
1000 return; // no function added
1001 for (li = defer_tv->vval.v_list->lv_first; li != NULL; li = li->li_next)
1002 {
1003 list_T *l = li->li_tv.vval.v_list;
1004 typval_T rettv;
1005 typval_T argvars[MAX_FUNC_ARGS];
1006 int i;
1007 listitem_T *arg_li = l->lv_first;
1008 funcexe_T funcexe;
1009
1010 for (i = 0; i < l->lv_len - 1; ++i)
1011 {
1012 arg_li = arg_li->li_next;
1013 argvars[i] = arg_li->li_tv;
1014 }
1015
1016 CLEAR_FIELD(funcexe);
1017 funcexe.fe_evaluate = TRUE;
1018 rettv.v_type = VAR_UNKNOWN;
1019 (void)call_func(l->lv_first->li_tv.vval.v_string, -1,
1020 &rettv, l->lv_len - 1, argvars, &funcexe);
1021 clear_tv(&rettv);
1022 }
1023}
1024
1025/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001026 * Return from the current function.
1027 */
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001028 static int
Bram Moolenaar4c137212021-04-19 16:48:48 +02001029func_return(ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001030{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001031 int idx;
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01001032 int ret_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001033 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1034 + ectx->ec_dfunc_idx;
1035 int argcount = ufunc_argcount(dfunc->df_ufunc);
1036 int top = ectx->ec_frame_idx - argcount;
Bram Moolenaarc620c052020-07-08 15:16:19 +02001037 estack_T *entry;
Bram Moolenaar12d26532021-02-19 19:13:21 +01001038 int prev_dfunc_idx = STACK_TV(ectx->ec_frame_idx
1039 + STACK_FRAME_FUNC_OFF)->vval.v_number;
Bram Moolenaarb06b50d2021-04-26 21:39:25 +02001040 funclocal_T *floc;
1041#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01001042 dfunc_T *prev_dfunc = ((dfunc_T *)def_functions.ga_data)
1043 + prev_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001044
Bram Moolenaar12d26532021-02-19 19:13:21 +01001045 if (do_profiling == PROF_YES)
1046 {
1047 ufunc_T *caller = prev_dfunc->df_ufunc;
1048
1049 if (dfunc->df_ufunc->uf_profiling
1050 || (caller != NULL && caller->uf_profiling))
1051 {
1052 profile_may_end_func(((profinfo_T *)profile_info_ga.ga_data)
1053 + profile_info_ga.ga_len - 1, dfunc->df_ufunc, caller);
1054 --profile_info_ga.ga_len;
1055 }
1056 }
1057#endif
Bram Moolenaar919c12c2021-12-14 14:29:16 +00001058
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001059 if (dfunc->df_defer_var_idx > 0)
1060 invoke_defer_funcs(ectx);
1061
Bram Moolenaar919c12c2021-12-14 14:29:16 +00001062 // No check for uf_refcount being zero, cannot think of a way that would
1063 // happen.
Bram Moolenaarc970e422021-03-17 15:03:04 +01001064 --dfunc->df_ufunc->uf_calls;
1065
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001066 // execution context goes one level up
Bram Moolenaarc620c052020-07-08 15:16:19 +02001067 entry = estack_pop();
1068 if (entry != NULL)
Bram Moolenaarc70fe462021-04-17 17:59:19 +02001069 current_sctx = entry->es_save_sctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001070
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001071 if (handle_closure_in_use(ectx, TRUE) == FAIL)
1072 return FAIL;
1073
1074 // Clear the arguments.
1075 for (idx = top; idx < ectx->ec_frame_idx; ++idx)
1076 clear_tv(STACK_TV(idx));
1077
1078 // Clear local variables and temp values, but not the return value.
1079 for (idx = ectx->ec_frame_idx + STACK_FRAME_SIZE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001080 idx < ectx->ec_stack.ga_len - 1; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001081 clear_tv(STACK_TV(idx));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001082
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01001083 // The return value should be on top of the stack. However, when aborting
1084 // it may not be there and ec_frame_idx is the top of the stack.
1085 ret_idx = ectx->ec_stack.ga_len - 1;
Bram Moolenaar0186e582021-01-10 18:33:11 +01001086 if (ret_idx == ectx->ec_frame_idx + STACK_FRAME_IDX_OFF)
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01001087 ret_idx = 0;
1088
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001089 if (ectx->ec_outer_ref != NULL)
1090 {
1091 if (ectx->ec_outer_ref->or_outer_allocated)
1092 vim_free(ectx->ec_outer_ref->or_outer);
1093 partial_unref(ectx->ec_outer_ref->or_partial);
1094 vim_free(ectx->ec_outer_ref);
1095 }
Bram Moolenaar0186e582021-01-10 18:33:11 +01001096
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001097 // Restore the previous frame.
Bram Moolenaar12d26532021-02-19 19:13:21 +01001098 ectx->ec_dfunc_idx = prev_dfunc_idx;
Bram Moolenaar0186e582021-01-10 18:33:11 +01001099 ectx->ec_iidx = STACK_TV(ectx->ec_frame_idx
1100 + STACK_FRAME_IIDX_OFF)->vval.v_number;
Bram Moolenaar5930ddc2021-04-26 20:32:59 +02001101 ectx->ec_instr = (void *)STACK_TV(ectx->ec_frame_idx
1102 + STACK_FRAME_INSTR_OFF)->vval.v_string;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001103 ectx->ec_outer_ref = (void *)STACK_TV(ectx->ec_frame_idx
Bram Moolenaar0186e582021-01-10 18:33:11 +01001104 + STACK_FRAME_OUTER_OFF)->vval.v_string;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001105 floc = (void *)STACK_TV(ectx->ec_frame_idx
1106 + STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string;
Bram Moolenaar5366e1a2020-10-01 13:01:34 +02001107 // restoring ec_frame_idx must be last
Bram Moolenaar0186e582021-01-10 18:33:11 +01001108 ectx->ec_frame_idx = STACK_TV(ectx->ec_frame_idx
1109 + STACK_FRAME_IDX_OFF)->vval.v_number;
Bram Moolenaard386e922021-04-25 14:48:49 +02001110
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001111 if (floc == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02001112 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001113 else
1114 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02001115 ectx->ec_funclocal = *floc;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001116 vim_free(floc);
1117 }
1118
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01001119 if (ret_idx > 0)
1120 {
1121 // Reset the stack to the position before the call, with a spot for the
1122 // return value, moved there from above the frame.
1123 ectx->ec_stack.ga_len = top + 1;
1124 *STACK_TV_BOT(-1) = *STACK_TV(ret_idx);
1125 }
1126 else
1127 // Reset the stack to the position before the call.
1128 ectx->ec_stack.ga_len = top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001129
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001130 funcdepth_decrement();
Bram Moolenaare99d4222021-06-13 14:01:26 +02001131 --ex_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001132 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001133}
1134
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001135/*
1136 * Prepare arguments and rettv for calling a builtin or user function.
1137 */
1138 static int
1139call_prepare(int argcount, typval_T *argvars, ectx_T *ectx)
1140{
1141 int idx;
1142 typval_T *tv;
1143
1144 // Move arguments from bottom of the stack to argvars[] and add terminator.
1145 for (idx = 0; idx < argcount; ++idx)
1146 argvars[idx] = *STACK_TV_BOT(idx - argcount);
1147 argvars[argcount].v_type = VAR_UNKNOWN;
1148
1149 // Result replaces the arguments on the stack.
1150 if (argcount > 0)
1151 ectx->ec_stack.ga_len -= argcount - 1;
Bram Moolenaar35578162021-08-02 19:10:38 +02001152 else if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001153 return FAIL;
1154 else
1155 ++ectx->ec_stack.ga_len;
1156
1157 // Default return value is zero.
1158 tv = STACK_TV_BOT(-1);
1159 tv->v_type = VAR_NUMBER;
1160 tv->vval.v_number = 0;
Bram Moolenaar24565cf2022-03-28 18:16:52 +01001161 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001162
1163 return OK;
1164}
1165
1166/*
1167 * Call a builtin function by index.
1168 */
1169 static int
1170call_bfunc(int func_idx, int argcount, ectx_T *ectx)
1171{
1172 typval_T argvars[MAX_FUNC_ARGS];
1173 int idx;
Bram Moolenaar171fb922020-10-28 16:54:47 +01001174 int did_emsg_before = did_emsg;
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001175 ectx_T *prev_ectx = current_ectx;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001176 char *save_func_name = ectx->ec_where.wt_func_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001177
1178 if (call_prepare(argcount, argvars, ectx) == FAIL)
1179 return FAIL;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001180 ectx->ec_where.wt_func_name = internal_func_name(func_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001181
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001182 // Call the builtin function. Set "current_ectx" so that when it
1183 // recursively invokes call_def_function() a closure context can be set.
1184 current_ectx = ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001185 call_internal_func_by_idx(func_idx, argvars, STACK_TV_BOT(-1));
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001186 current_ectx = prev_ectx;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001187 ectx->ec_where.wt_func_name = save_func_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001188
1189 // Clear the arguments.
1190 for (idx = 0; idx < argcount; ++idx)
1191 clear_tv(&argvars[idx]);
Bram Moolenaar015f4262020-05-05 21:25:22 +02001192
Bram Moolenaar57f799e2020-12-12 20:42:19 +01001193 if (did_emsg > did_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +02001194 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001195 return OK;
1196}
1197
1198/*
1199 * Execute a user defined function.
Bram Moolenaarab360522021-01-10 14:02:28 +01001200 * If the function is compiled this will add a stack frame and set the
1201 * instruction pointer at the start of the function.
1202 * Otherwise the function is called here.
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001203 * If "pt" is not null use "pt->pt_outer" for ec_outer_ref->or_outer.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001204 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001205 */
1206 static int
Bram Moolenaar0186e582021-01-10 18:33:11 +01001207call_ufunc(
1208 ufunc_T *ufunc,
1209 partial_T *pt,
1210 int argcount,
1211 ectx_T *ectx,
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001212 isn_T *iptr,
1213 dict_T *selfdict)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001214{
1215 typval_T argvars[MAX_FUNC_ARGS];
1216 funcexe_T funcexe;
1217 int error;
1218 int idx;
Bram Moolenaar28ee8922020-10-28 20:20:00 +01001219 int did_emsg_before = did_emsg;
Bram Moolenaar139575d2022-03-15 19:29:30 +00001220 compiletype_T compile_type = get_compile_type(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001221
Bram Moolenaare99d4222021-06-13 14:01:26 +02001222 if (func_needs_compiling(ufunc, compile_type)
1223 && compile_def_function(ufunc, FALSE, compile_type, NULL)
1224 == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02001225 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001226 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001227 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +01001228 error = check_user_func_argcount(ufunc, argcount);
Bram Moolenaar50824712020-12-20 21:10:17 +01001229 if (error != FCERR_UNKNOWN)
1230 {
1231 if (error == FCERR_TOOMANY)
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001232 semsg(_(e_too_many_arguments_for_function_str),
1233 printable_func_name(ufunc));
Bram Moolenaar50824712020-12-20 21:10:17 +01001234 else
Bram Moolenaare1242042021-12-16 20:56:57 +00001235 semsg(_(e_not_enough_arguments_for_function_str),
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001236 printable_func_name(ufunc));
Bram Moolenaar50824712020-12-20 21:10:17 +01001237 return FAIL;
1238 }
1239
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001240 // The function has been compiled, can call it quickly. For a function
1241 // that was defined later: we can call it directly next time.
1242 if (iptr != NULL)
1243 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001244 delete_instr(iptr);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001245 iptr->isn_type = ISN_DCALL;
1246 iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1247 iptr->isn_arg.dfunc.cdf_argcount = argcount;
1248 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001249 return call_dfunc(ufunc->uf_dfunc_idx, pt, argcount, ectx);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001250 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001251
1252 if (call_prepare(argcount, argvars, ectx) == FAIL)
1253 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +02001254 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00001255 funcexe.fe_evaluate = TRUE;
1256 funcexe.fe_selfdict = selfdict != NULL ? selfdict : dict_stack_get_dict();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001257
1258 // Call the user function. Result goes in last position on the stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001259 error = call_user_func_check(ufunc, argcount, argvars,
Bram Moolenaar851f86b2021-12-13 14:26:44 +00001260 STACK_TV_BOT(-1), &funcexe, funcexe.fe_selfdict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001261
1262 // Clear the arguments.
1263 for (idx = 0; idx < argcount; ++idx)
1264 clear_tv(&argvars[idx]);
1265
1266 if (error != FCERR_NONE)
1267 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001268 user_func_error(error, printable_func_name(ufunc), &funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001269 return FAIL;
1270 }
Bram Moolenaar28ee8922020-10-28 20:20:00 +01001271 if (did_emsg > did_emsg_before)
Bram Moolenaared677f52020-08-12 16:38:10 +02001272 // Error other than from calling the function itself.
1273 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001274 return OK;
1275}
1276
1277/*
Bram Moolenaara91a7132021-03-25 21:12:15 +01001278 * If command modifiers were applied restore them.
1279 */
1280 static void
1281may_restore_cmdmod(funclocal_T *funclocal)
1282{
1283 if (funclocal->floc_restore_cmdmod)
1284 {
1285 cmdmod.cmod_filter_regmatch.regprog = NULL;
1286 undo_cmdmod(&cmdmod);
1287 cmdmod = funclocal->floc_save_cmdmod;
1288 funclocal->floc_restore_cmdmod = FALSE;
1289 }
1290}
1291
1292/*
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001293 * Return TRUE if an error was given (not caught in try/catch) or CTRL-C was
1294 * pressed.
Bram Moolenaara1773442020-08-12 15:21:22 +02001295 */
1296 static int
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001297vim9_aborting(int prev_uncaught_emsg)
Bram Moolenaara1773442020-08-12 15:21:22 +02001298{
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001299 return uncaught_emsg > prev_uncaught_emsg || got_int || did_throw;
Bram Moolenaara1773442020-08-12 15:21:22 +02001300}
1301
1302/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001303 * Execute a function by "name".
1304 * This can be a builtin function or a user function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001305 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001306 * Returns FAIL if not found without an error message.
1307 */
1308 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001309call_by_name(
1310 char_u *name,
1311 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001312 ectx_T *ectx,
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001313 isn_T *iptr,
1314 dict_T *selfdict)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001315{
1316 ufunc_T *ufunc;
1317
1318 if (builtin_function(name, -1))
1319 {
1320 int func_idx = find_internal_func(name);
1321
Bram Moolenaar1983f1a2022-02-28 20:55:02 +00001322 if (func_idx < 0) // Impossible?
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001323 return FAIL;
Bram Moolenaar389df252020-07-09 21:20:47 +02001324 if (check_internal_func(func_idx, argcount) < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001325 return FAIL;
1326 return call_bfunc(func_idx, argcount, ectx);
1327 }
1328
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00001329 ufunc = find_func(name, FALSE);
Bram Moolenaara1773442020-08-12 15:21:22 +02001330
1331 if (ufunc == NULL)
1332 {
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001333 int prev_uncaught_emsg = uncaught_emsg;
Bram Moolenaara1773442020-08-12 15:21:22 +02001334
1335 if (script_autoload(name, TRUE))
1336 // loaded a package, search for the function again
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00001337 ufunc = find_func(name, FALSE);
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001338
1339 if (vim9_aborting(prev_uncaught_emsg))
Bram Moolenaara1773442020-08-12 15:21:22 +02001340 return FAIL; // bail out if loading the script caused an error
1341 }
1342
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001343 if (ufunc != NULL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001344 {
Bram Moolenaar6ce46b92021-08-07 15:35:36 +02001345 if (ufunc->uf_arg_types != NULL || ufunc->uf_va_type != NULL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001346 {
1347 int i;
1348 typval_T *argv = STACK_TV_BOT(0) - argcount;
1349
1350 // The function can change at runtime, check that the argument
1351 // types are correct.
1352 for (i = 0; i < argcount; ++i)
1353 {
Bram Moolenaare3ffcd92021-03-08 21:47:13 +01001354 type_T *type = NULL;
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001355
Bram Moolenaar6ce46b92021-08-07 15:35:36 +02001356 if (i < ufunc->uf_args.ga_len && ufunc->uf_arg_types != NULL)
Bram Moolenaare3ffcd92021-03-08 21:47:13 +01001357 type = ufunc->uf_arg_types[i];
1358 else if (ufunc->uf_va_type != NULL)
1359 type = ufunc->uf_va_type->tt_member;
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001360 if (type != NULL && check_typval_arg_type(type,
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001361 &argv[i], NULL, i + 1) == FAIL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001362 return FAIL;
1363 }
1364 }
1365
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001366 return call_ufunc(ufunc, NULL, argcount, ectx, iptr, selfdict);
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001367 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001368
1369 return FAIL;
1370}
1371
1372 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001373call_partial(
1374 typval_T *tv,
1375 int argcount_arg,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001376 ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001377{
Bram Moolenaara90afb92020-07-15 22:38:56 +02001378 int argcount = argcount_arg;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001379 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001380 int called_emsg_before = called_emsg;
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +01001381 int res = FAIL;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001382 dict_T *selfdict = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001383
1384 if (tv->v_type == VAR_PARTIAL)
1385 {
Bram Moolenaara90afb92020-07-15 22:38:56 +02001386 partial_T *pt = tv->vval.v_partial;
1387 int i;
1388
1389 if (pt->pt_argc > 0)
1390 {
1391 // Make space for arguments from the partial, shift the "argcount"
1392 // arguments up.
Bram Moolenaar35578162021-08-02 19:10:38 +02001393 if (GA_GROW_FAILS(&ectx->ec_stack, pt->pt_argc))
Bram Moolenaara90afb92020-07-15 22:38:56 +02001394 return FAIL;
1395 for (i = 1; i <= argcount; ++i)
1396 *STACK_TV_BOT(-i + pt->pt_argc) = *STACK_TV_BOT(-i);
1397 ectx->ec_stack.ga_len += pt->pt_argc;
1398 argcount += pt->pt_argc;
1399
1400 // copy the arguments from the partial onto the stack
1401 for (i = 0; i < pt->pt_argc; ++i)
1402 copy_tv(&pt->pt_argv[i], STACK_TV_BOT(-argcount + i));
1403 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001404 selfdict = pt->pt_dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001405
1406 if (pt->pt_func != NULL)
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001407 return call_ufunc(pt->pt_func, pt, argcount, ectx, NULL, selfdict);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001408
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001409 name = pt->pt_name;
1410 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001411 else if (tv->v_type == VAR_FUNC)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001412 name = tv->vval.v_string;
Bram Moolenaar95006e32020-08-29 17:47:08 +02001413 if (name != NULL)
1414 {
1415 char_u fname_buf[FLEN_FIXED + 1];
1416 char_u *tofree = NULL;
1417 int error = FCERR_NONE;
1418 char_u *fname;
1419
1420 // May need to translate <SNR>123_ to K_SNR.
1421 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
1422 if (error != FCERR_NONE)
1423 res = FAIL;
1424 else
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001425 res = call_by_name(fname, argcount, ectx, NULL, selfdict);
Bram Moolenaar95006e32020-08-29 17:47:08 +02001426 vim_free(tofree);
1427 }
1428
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +01001429 if (res == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001430 {
1431 if (called_emsg == called_emsg_before)
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001432 emsg_funcname(e_unknown_function_str,
Bram Moolenaar015f4262020-05-05 21:25:22 +02001433 name == NULL ? (char_u *)"[unknown]" : name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001434 return FAIL;
1435 }
1436 return OK;
1437}
1438
1439/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001440 * Check if "lock" is VAR_LOCKED or VAR_FIXED. If so give an error and return
1441 * TRUE.
1442 */
1443 static int
1444error_if_locked(int lock, char *error)
1445{
1446 if (lock & (VAR_LOCKED | VAR_FIXED))
1447 {
1448 emsg(_(error));
1449 return TRUE;
1450 }
1451 return FALSE;
1452}
1453
1454/*
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001455 * Give an error if "tv" is not a number and return FAIL.
1456 */
1457 static int
1458check_for_number(typval_T *tv)
1459{
1460 if (tv->v_type != VAR_NUMBER)
1461 {
1462 semsg(_(e_expected_str_but_got_str),
1463 vartype_name(VAR_NUMBER), vartype_name(tv->v_type));
1464 return FAIL;
1465 }
1466 return OK;
1467}
1468
1469/*
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001470 * Store "tv" in variable "name".
1471 * This is for s: and g: variables.
1472 */
1473 static void
1474store_var(char_u *name, typval_T *tv)
1475{
1476 funccal_entry_T entry;
Bram Moolenaard877a572021-04-01 19:42:48 +02001477 int flags = ASSIGN_DECL;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001478
Bram Moolenaard877a572021-04-01 19:42:48 +02001479 if (tv->v_lock)
1480 flags |= ASSIGN_CONST;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001481 save_funccal(&entry);
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001482 set_var_const(name, 0, NULL, tv, FALSE, flags, 0);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001483 restore_funccal();
1484}
1485
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001486/*
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001487 * Convert "tv" to a string.
1488 * Return FAIL if not allowed.
1489 */
1490 static int
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001491do_2string(typval_T *tv, int is_2string_any, int tolerant)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001492{
1493 if (tv->v_type != VAR_STRING)
1494 {
1495 char_u *str;
1496
1497 if (is_2string_any)
1498 {
1499 switch (tv->v_type)
1500 {
1501 case VAR_SPECIAL:
1502 case VAR_BOOL:
1503 case VAR_NUMBER:
1504 case VAR_FLOAT:
1505 case VAR_BLOB: break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001506
1507 case VAR_LIST:
1508 if (tolerant)
1509 {
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001510 char_u *s, *e, *p;
1511 garray_T ga;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001512
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001513 ga_init2(&ga, sizeof(char_u *), 1);
1514
1515 // Convert to NL separated items, then
1516 // escape the items and replace the NL with
1517 // a space.
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001518 str = typval2string(tv, TRUE);
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001519 if (str == NULL)
1520 return FAIL;
1521 s = str;
1522 while ((e = vim_strchr(s, '\n')) != NULL)
1523 {
1524 *e = NUL;
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +01001525 p = vim_strsave_fnameescape(s,
1526 VSE_NONE);
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001527 if (p != NULL)
1528 {
1529 ga_concat(&ga, p);
1530 ga_concat(&ga, (char_u *)" ");
1531 vim_free(p);
1532 }
1533 s = e + 1;
1534 }
1535 vim_free(str);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001536 clear_tv(tv);
1537 tv->v_type = VAR_STRING;
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001538 tv->vval.v_string = ga.ga_data;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001539 return OK;
1540 }
1541 // FALLTHROUGH
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001542 default: to_string_error(tv->v_type);
1543 return FAIL;
1544 }
1545 }
Bram Moolenaar34453202021-01-31 13:08:38 +01001546 str = typval_tostring(tv, TRUE);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001547 clear_tv(tv);
1548 tv->v_type = VAR_STRING;
1549 tv->vval.v_string = str;
1550 }
1551 return OK;
1552}
1553
1554/*
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001555 * When the value of "sv" is a null list of dict, allocate it.
1556 */
1557 static void
Bram Moolenaar859cc212022-03-28 15:22:35 +01001558allocate_if_null(svar_T *sv)
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001559{
Bram Moolenaar859cc212022-03-28 15:22:35 +01001560 typval_T *tv = sv->sv_tv;
1561
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001562 switch (tv->v_type)
1563 {
1564 case VAR_LIST:
Bram Moolenaar859cc212022-03-28 15:22:35 +01001565 if (tv->vval.v_list == NULL && sv->sv_type != &t_list_empty)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001566 (void)rettv_list_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001567 break;
1568 case VAR_DICT:
Bram Moolenaar859cc212022-03-28 15:22:35 +01001569 if (tv->vval.v_dict == NULL && sv->sv_type != &t_dict_empty)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001570 (void)rettv_dict_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001571 break;
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02001572 case VAR_BLOB:
Bram Moolenaar859cc212022-03-28 15:22:35 +01001573 if (tv->vval.v_blob == NULL && sv->sv_type != &t_blob_null)
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02001574 (void)rettv_blob_alloc(tv);
1575 break;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001576 default:
1577 break;
1578 }
1579}
Bram Moolenaard3aac292020-04-19 14:32:17 +02001580
Bram Moolenaare7525c52021-01-09 13:20:37 +01001581/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001582 * Return the character "str[index]" where "index" is the character index,
1583 * including composing characters.
1584 * If "index" is out of range NULL is returned.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001585 */
1586 char_u *
1587char_from_string(char_u *str, varnumber_T index)
1588{
1589 size_t nbyte = 0;
1590 varnumber_T nchar = index;
1591 size_t slen;
1592
1593 if (str == NULL)
1594 return NULL;
1595 slen = STRLEN(str);
1596
Bram Moolenaarff871402021-03-26 13:34:05 +01001597 // Do the same as for a list: a negative index counts from the end.
1598 // Optimization to check the first byte to be below 0x80 (and no composing
1599 // character follows) makes this a lot faster.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001600 if (index < 0)
1601 {
1602 int clen = 0;
1603
1604 for (nbyte = 0; nbyte < slen; ++clen)
Bram Moolenaarff871402021-03-26 13:34:05 +01001605 {
1606 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1607 ++nbyte;
1608 else if (enc_utf8)
1609 nbyte += utfc_ptr2len(str + nbyte);
1610 else
1611 nbyte += mb_ptr2len(str + nbyte);
1612 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001613 nchar = clen + index;
1614 if (nchar < 0)
1615 // unlike list: index out of range results in empty string
1616 return NULL;
1617 }
1618
1619 for (nbyte = 0; nchar > 0 && nbyte < slen; --nchar)
Bram Moolenaarff871402021-03-26 13:34:05 +01001620 {
1621 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1622 ++nbyte;
1623 else if (enc_utf8)
1624 nbyte += utfc_ptr2len(str + nbyte);
1625 else
1626 nbyte += mb_ptr2len(str + nbyte);
1627 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001628 if (nbyte >= slen)
1629 return NULL;
Bram Moolenaar0289a092021-03-14 18:40:19 +01001630 return vim_strnsave(str + nbyte, mb_ptr2len(str + nbyte));
Bram Moolenaare7525c52021-01-09 13:20:37 +01001631}
1632
1633/*
1634 * Get the byte index for character index "idx" in string "str" with length
Bram Moolenaar0289a092021-03-14 18:40:19 +01001635 * "str_len". Composing characters are included.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001636 * If going over the end return "str_len".
1637 * If "idx" is negative count from the end, -1 is the last character.
1638 * When going over the start return -1.
1639 */
1640 static long
1641char_idx2byte(char_u *str, size_t str_len, varnumber_T idx)
1642{
1643 varnumber_T nchar = idx;
1644 size_t nbyte = 0;
1645
1646 if (nchar >= 0)
1647 {
1648 while (nchar > 0 && nbyte < str_len)
1649 {
Bram Moolenaar0289a092021-03-14 18:40:19 +01001650 nbyte += mb_ptr2len(str + nbyte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001651 --nchar;
1652 }
1653 }
1654 else
1655 {
1656 nbyte = str_len;
1657 while (nchar < 0 && nbyte > 0)
1658 {
1659 --nbyte;
1660 nbyte -= mb_head_off(str, str + nbyte);
1661 ++nchar;
1662 }
1663 if (nchar < 0)
1664 return -1;
1665 }
1666 return (long)nbyte;
1667}
1668
1669/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001670 * Return the slice "str[first : last]" using character indexes. Composing
1671 * characters are included.
Bram Moolenaar6601b622021-01-13 21:47:15 +01001672 * "exclusive" is TRUE for slice().
Bram Moolenaare7525c52021-01-09 13:20:37 +01001673 * Return NULL when the result is empty.
1674 */
1675 char_u *
Bram Moolenaar6601b622021-01-13 21:47:15 +01001676string_slice(char_u *str, varnumber_T first, varnumber_T last, int exclusive)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001677{
1678 long start_byte, end_byte;
1679 size_t slen;
1680
1681 if (str == NULL)
1682 return NULL;
1683 slen = STRLEN(str);
1684 start_byte = char_idx2byte(str, slen, first);
1685 if (start_byte < 0)
1686 start_byte = 0; // first index very negative: use zero
Bram Moolenaar6601b622021-01-13 21:47:15 +01001687 if ((last == -1 && !exclusive) || last == VARNUM_MAX)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001688 end_byte = (long)slen;
1689 else
1690 {
1691 end_byte = char_idx2byte(str, slen, last);
Bram Moolenaar6601b622021-01-13 21:47:15 +01001692 if (!exclusive && end_byte >= 0 && end_byte < (long)slen)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001693 // end index is inclusive
Bram Moolenaar0289a092021-03-14 18:40:19 +01001694 end_byte += mb_ptr2len(str + end_byte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001695 }
1696
1697 if (start_byte >= (long)slen || end_byte <= start_byte)
1698 return NULL;
1699 return vim_strnsave(str + start_byte, end_byte - start_byte);
1700}
1701
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001702/*
1703 * Get a script variable for ISN_STORESCRIPT and ISN_LOADSCRIPT.
1704 * When "dfunc_idx" is negative don't give an error.
1705 * Returns NULL for an error.
1706 */
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001707 static svar_T *
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001708get_script_svar(scriptref_T *sref, int dfunc_idx)
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001709{
1710 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001711 dfunc_T *dfunc = dfunc_idx < 0 ? NULL
1712 : ((dfunc_T *)def_functions.ga_data) + dfunc_idx;
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001713 svar_T *sv;
1714
1715 if (sref->sref_seq != si->sn_script_seq)
1716 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001717 // The script was reloaded after the function was compiled, the
1718 // script_idx may not be valid.
1719 if (dfunc != NULL)
1720 semsg(_(e_script_variable_invalid_after_reload_in_function_str),
1721 printable_func_name(dfunc->df_ufunc));
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001722 return NULL;
1723 }
1724 sv = ((svar_T *)si->sn_var_vals.ga_data) + sref->sref_idx;
Bram Moolenaar6de22962022-09-09 21:35:36 +01001725 if (sv->sv_name == NULL)
1726 {
1727 if (dfunc != NULL)
1728 emsg(_(e_script_variable_was_deleted));
1729 return NULL;
1730 }
Bram Moolenaar60dc8272021-07-29 22:48:54 +02001731 if (!equal_type(sv->sv_type, sref->sref_type, 0))
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001732 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001733 if (dfunc != NULL)
1734 emsg(_(e_script_variable_type_changed));
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001735 return NULL;
1736 }
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001737
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01001738 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0
1739 && sref->sref_sid != current_sctx.sc_sid)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001740 {
1741 if (dfunc != NULL)
1742 semsg(_(e_item_not_exported_in_script_str), sv->sv_name);
1743 return NULL;
1744 }
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001745 return sv;
1746}
1747
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001748/*
Bram Moolenaar20677332021-06-06 17:02:53 +02001749 * Function passed to do_cmdline() for splitting a script joined by NL
1750 * characters.
1751 */
1752 static char_u *
1753get_split_sourceline(
1754 int c UNUSED,
1755 void *cookie,
1756 int indent UNUSED,
1757 getline_opt_T options UNUSED)
1758{
1759 source_cookie_T *sp = (source_cookie_T *)cookie;
1760 char_u *p;
1761 char_u *line;
1762
Bram Moolenaar20677332021-06-06 17:02:53 +02001763 p = vim_strchr(sp->nextline, '\n');
1764 if (p == NULL)
1765 {
1766 line = vim_strsave(sp->nextline);
1767 sp->nextline += STRLEN(sp->nextline);
1768 }
1769 else
1770 {
1771 line = vim_strnsave(sp->nextline, p - sp->nextline);
1772 sp->nextline = p + 1;
1773 }
1774 return line;
1775}
1776
1777/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001778 * Execute a function by "name".
1779 * This can be a builtin function, user function or a funcref.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001780 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001781 */
1782 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001783call_eval_func(
1784 char_u *name,
1785 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001786 ectx_T *ectx,
1787 isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001788{
Bram Moolenaared677f52020-08-12 16:38:10 +02001789 int called_emsg_before = called_emsg;
1790 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001791
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001792 res = call_by_name(name, argcount, ectx, iptr, NULL);
Bram Moolenaared677f52020-08-12 16:38:10 +02001793 if (res == FAIL && called_emsg == called_emsg_before)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001794 {
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001795 dictitem_T *v;
1796
1797 v = find_var(name, NULL, FALSE);
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001798 if (v == NULL || (v->di_tv.v_type != VAR_PARTIAL
1799 && v->di_tv.v_type != VAR_FUNC))
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001800 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001801 emsg_funcname(e_unknown_function_str, name);
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001802 return FAIL;
1803 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001804 return call_partial(&v->di_tv, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001805 }
Bram Moolenaared677f52020-08-12 16:38:10 +02001806 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001807}
1808
1809/*
Bram Moolenaarf112f302020-12-20 17:47:52 +01001810 * When a function reference is used, fill a partial with the information
1811 * needed, especially when it is used as a closure.
1812 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001813 int
Bram Moolenaarf112f302020-12-20 17:47:52 +01001814fill_partial_and_closure(partial_T *pt, ufunc_T *ufunc, ectx_T *ectx)
1815{
1816 pt->pt_func = ufunc;
1817 pt->pt_refcount = 1;
1818
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001819 if (ufunc->uf_flags & FC_CLOSURE)
Bram Moolenaarf112f302020-12-20 17:47:52 +01001820 {
1821 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1822 + ectx->ec_dfunc_idx;
1823
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001824 // The closure may need to find arguments and local variables in the
1825 // current stack.
Bram Moolenaar0186e582021-01-10 18:33:11 +01001826 pt->pt_outer.out_stack = &ectx->ec_stack;
1827 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001828 if (ectx->ec_outer_ref != NULL)
1829 {
1830 // The current context already has a context, link to that one.
1831 pt->pt_outer.out_up = ectx->ec_outer_ref->or_outer;
1832 if (ectx->ec_outer_ref->or_partial != NULL)
1833 {
1834 pt->pt_outer.out_up_partial = ectx->ec_outer_ref->or_partial;
1835 ++pt->pt_outer.out_up_partial->pt_refcount;
1836 }
1837 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01001838
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001839 // If this function returns and the closure is still being used, we
1840 // need to make a copy of the context (arguments and local variables).
1841 // Store a reference to the partial so we can handle that.
Bram Moolenaar35578162021-08-02 19:10:38 +02001842 if (GA_GROW_FAILS(&ectx->ec_funcrefs, 1))
Bram Moolenaarf112f302020-12-20 17:47:52 +01001843 {
1844 vim_free(pt);
1845 return FAIL;
1846 }
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001847 // Extra variable keeps the count of closures created in the current
1848 // function call.
Bram Moolenaarf112f302020-12-20 17:47:52 +01001849 ++(((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx
1850 + STACK_FRAME_SIZE + dfunc->df_varcount)->vval.v_number;
1851
1852 ((partial_T **)ectx->ec_funcrefs.ga_data)
1853 [ectx->ec_funcrefs.ga_len] = pt;
1854 ++pt->pt_refcount;
1855 ++ectx->ec_funcrefs.ga_len;
1856 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001857 ++ufunc->uf_refcount;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001858 return OK;
1859}
1860
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001861/*
1862 * Execute iptr->isn_arg.string as an Ex command.
1863 */
1864 static int
1865exec_command(isn_T *iptr)
1866{
1867 source_cookie_T cookie;
1868
1869 SOURCING_LNUM = iptr->isn_lnum;
1870 // Pass getsourceline to get an error for a missing ":end"
1871 // command.
1872 CLEAR_FIELD(cookie);
1873 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1874 if (do_cmdline(iptr->isn_arg.string,
1875 getsourceline, &cookie,
1876 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED) == FAIL
1877 || did_emsg)
1878 return FAIL;
1879 return OK;
1880}
1881
Bram Moolenaar89445512022-04-14 12:58:23 +01001882/*
1883 * If script "sid" is not loaded yet then load it now.
1884 * Caller must make sure "sid" is a valid script ID.
1885 * "loaded" is set to TRUE if the script had to be loaded.
1886 * Returns FAIL if loading fails, OK if already loaded or loaded now.
1887 */
1888 int
1889may_load_script(int sid, int *loaded)
1890{
1891 scriptitem_T *si = SCRIPT_ITEM(sid);
1892
1893 if (si->sn_state == SN_STATE_NOT_LOADED)
1894 {
1895 *loaded = TRUE;
1896 if (do_source(si->sn_name, FALSE, DOSO_NONE, NULL) == FAIL)
1897 {
1898 semsg(_(e_cant_open_file_str), si->sn_name);
1899 return FAIL;
1900 }
1901 }
1902 return OK;
1903}
1904
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001905// used for v_instr of typval of VAR_INSTR
1906struct instr_S {
1907 ectx_T *instr_ectx;
1908 isn_T *instr_instr;
1909};
1910
Bram Moolenaar4c137212021-04-19 16:48:48 +02001911// used for substitute_instr
1912typedef struct subs_expr_S {
1913 ectx_T *subs_ectx;
1914 isn_T *subs_instr;
1915 int subs_status;
1916} subs_expr_T;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001917
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001918// Set when calling do_debug().
1919static ectx_T *debug_context = NULL;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001920static int debug_var_count;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001921
1922/*
1923 * When debugging lookup "name" and return the typeval.
1924 * When not found return NULL.
1925 */
1926 typval_T *
1927lookup_debug_var(char_u *name)
1928{
1929 int idx;
1930 dfunc_T *dfunc;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001931 ufunc_T *ufunc;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001932 ectx_T *ectx = debug_context;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001933 int varargs_off;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001934
1935 if (ectx == NULL)
1936 return NULL;
1937 dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
1938
1939 // Go through the local variable names, from last to first.
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001940 for (idx = debug_var_count - 1; idx >= 0; --idx)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001941 {
Bram Moolenaare406ff82022-03-10 20:47:43 +00001942 char_u *varname = ((char_u **)dfunc->df_var_names.ga_data)[idx];
1943
1944 // the variable name may be NULL when not available in this block
1945 if (varname != NULL && STRCMP(varname, name) == 0)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001946 return STACK_TV_VAR(idx);
1947 }
1948
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001949 // Go through argument names.
1950 ufunc = dfunc->df_ufunc;
1951 varargs_off = ufunc->uf_va_name == NULL ? 0 : 1;
1952 for (idx = 0; idx < ufunc->uf_args.ga_len; ++idx)
1953 if (STRCMP(((char_u **)(ufunc->uf_args.ga_data))[idx], name) == 0)
1954 return STACK_TV(ectx->ec_frame_idx - ufunc->uf_args.ga_len
1955 - varargs_off + idx);
1956 if (ufunc->uf_va_name != NULL && STRCMP(ufunc->uf_va_name, name) == 0)
1957 return STACK_TV(ectx->ec_frame_idx - 1);
1958
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001959 return NULL;
1960}
1961
Bram Moolenaar26a44842021-09-02 18:49:06 +02001962/*
1963 * Return TRUE if there might be a breakpoint in "ufunc", which is when a
1964 * breakpoint was set in that function or when there is any expression.
1965 */
1966 int
1967may_break_in_function(ufunc_T *ufunc)
1968{
1969 return ufunc->uf_has_breakpoint || debug_has_expr_breakpoint();
1970}
1971
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001972 static void
1973handle_debug(isn_T *iptr, ectx_T *ectx)
1974{
1975 char_u *line;
1976 ufunc_T *ufunc = (((dfunc_T *)def_functions.ga_data)
1977 + ectx->ec_dfunc_idx)->df_ufunc;
1978 isn_T *ni;
1979 int end_lnum = iptr->isn_lnum;
1980 garray_T ga;
1981 int lnum;
1982
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02001983 if (ex_nesting_level > debug_break_level)
1984 {
1985 linenr_T breakpoint;
1986
Bram Moolenaar26a44842021-09-02 18:49:06 +02001987 if (!may_break_in_function(ufunc))
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02001988 return;
1989
1990 // check for the next breakpoint if needed
1991 breakpoint = dbg_find_breakpoint(FALSE, ufunc->uf_name,
Bram Moolenaar8cec9272021-06-23 20:20:53 +02001992 iptr->isn_arg.debug.dbg_break_lnum);
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02001993 if (breakpoint <= 0 || breakpoint > iptr->isn_lnum)
1994 return;
1995 }
1996
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001997 SOURCING_LNUM = iptr->isn_lnum;
1998 debug_context = ectx;
Bram Moolenaar8cec9272021-06-23 20:20:53 +02001999 debug_var_count = iptr->isn_arg.debug.dbg_var_names_len;
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002000
2001 for (ni = iptr + 1; ni->isn_type != ISN_FINISH; ++ni)
2002 if (ni->isn_type == ISN_DEBUG
2003 || ni->isn_type == ISN_RETURN
2004 || ni->isn_type == ISN_RETURN_VOID)
2005 {
Bram Moolenaar112bed02021-11-23 22:16:34 +00002006 end_lnum = ni->isn_lnum + (ni->isn_type == ISN_DEBUG ? 0 : 1);
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002007 break;
2008 }
2009
2010 if (end_lnum > iptr->isn_lnum)
2011 {
2012 ga_init2(&ga, sizeof(char_u *), 10);
Bram Moolenaar310091d2021-12-23 21:14:37 +00002013 for (lnum = iptr->isn_lnum; lnum < end_lnum
2014 && lnum <= ufunc->uf_lines.ga_len; ++lnum)
Bram Moolenaar59b50c32021-06-17 22:27:48 +02002015 {
Bram Moolenaar303215d2021-07-07 20:10:43 +02002016 char_u *p = ((char_u **)ufunc->uf_lines.ga_data)[lnum - 1];
Bram Moolenaar59b50c32021-06-17 22:27:48 +02002017
Bram Moolenaar303215d2021-07-07 20:10:43 +02002018 if (p == NULL)
2019 continue; // left over from continuation line
2020 p = skipwhite(p);
Bram Moolenaar59b50c32021-06-17 22:27:48 +02002021 if (*p == '#')
2022 break;
Bram Moolenaar35578162021-08-02 19:10:38 +02002023 if (GA_GROW_OK(&ga, 1))
Bram Moolenaar59b50c32021-06-17 22:27:48 +02002024 ((char_u **)(ga.ga_data))[ga.ga_len++] = p;
2025 if (STRNCMP(p, "def ", 4) == 0)
2026 break;
2027 }
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002028 line = ga_concat_strings(&ga, " ");
2029 vim_free(ga.ga_data);
2030 }
2031 else
2032 line = ((char_u **)ufunc->uf_lines.ga_data)[iptr->isn_lnum - 1];
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002033
Dominique Pelle6e969552021-06-17 13:53:41 +02002034 do_debug(line == NULL ? (char_u *)"[empty]" : line);
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002035 debug_context = NULL;
2036
2037 if (end_lnum > iptr->isn_lnum)
2038 vim_free(line);
2039}
2040
Bram Moolenaar4c137212021-04-19 16:48:48 +02002041/*
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00002042 * Store a value in a list, dict or blob variable.
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002043 * Returns OK, FAIL or NOTDONE (uncatchable error).
2044 */
2045 static int
2046execute_storeindex(isn_T *iptr, ectx_T *ectx)
2047{
2048 vartype_T dest_type = iptr->isn_arg.vartype;
2049 typval_T *tv;
2050 typval_T *tv_idx = STACK_TV_BOT(-2);
2051 typval_T *tv_dest = STACK_TV_BOT(-1);
2052 int status = OK;
2053
2054 // Stack contains:
2055 // -3 value to be stored
2056 // -2 index
2057 // -1 dict or list
2058 tv = STACK_TV_BOT(-3);
2059 SOURCING_LNUM = iptr->isn_lnum;
2060 if (dest_type == VAR_ANY)
2061 {
2062 dest_type = tv_dest->v_type;
2063 if (dest_type == VAR_DICT)
2064 status = do_2string(tv_idx, TRUE, FALSE);
2065 else if (dest_type == VAR_LIST && tv_idx->v_type != VAR_NUMBER)
2066 {
2067 emsg(_(e_number_expected));
2068 status = FAIL;
2069 }
2070 }
Bram Moolenaare08be092022-02-17 13:08:26 +00002071
2072 if (status == OK)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002073 {
Bram Moolenaare08be092022-02-17 13:08:26 +00002074 if (dest_type == VAR_LIST)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002075 {
Bram Moolenaare08be092022-02-17 13:08:26 +00002076 long lidx = (long)tv_idx->vval.v_number;
2077 list_T *list = tv_dest->vval.v_list;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002078
Bram Moolenaare08be092022-02-17 13:08:26 +00002079 if (list == NULL)
2080 {
2081 emsg(_(e_list_not_set));
2082 return FAIL;
2083 }
2084 if (lidx < 0 && list->lv_len + lidx >= 0)
2085 // negative index is relative to the end
2086 lidx = list->lv_len + lidx;
2087 if (lidx < 0 || lidx > list->lv_len)
2088 {
2089 semsg(_(e_list_index_out_of_range_nr), lidx);
2090 return FAIL;
2091 }
2092 if (lidx < list->lv_len)
2093 {
2094 listitem_T *li = list_find(list, lidx);
2095
2096 if (error_if_locked(li->li_tv.v_lock,
Bram Moolenaar70c43d82022-01-26 21:01:15 +00002097 e_cannot_change_locked_list_item))
Bram Moolenaare08be092022-02-17 13:08:26 +00002098 return FAIL;
2099 // overwrite existing list item
2100 clear_tv(&li->li_tv);
2101 li->li_tv = *tv;
2102 }
2103 else
2104 {
2105 if (error_if_locked(list->lv_lock, e_cannot_change_locked_list))
2106 return FAIL;
2107 // append to list, only fails when out of memory
2108 if (list_append_tv(list, tv) == FAIL)
2109 return NOTDONE;
2110 clear_tv(tv);
2111 }
2112 }
2113 else if (dest_type == VAR_DICT)
2114 {
2115 char_u *key = tv_idx->vval.v_string;
2116 dict_T *dict = tv_dest->vval.v_dict;
2117 dictitem_T *di;
2118
2119 SOURCING_LNUM = iptr->isn_lnum;
2120 if (dict == NULL)
2121 {
2122 emsg(_(e_dictionary_not_set));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002123 return FAIL;
Bram Moolenaare08be092022-02-17 13:08:26 +00002124 }
2125 if (key == NULL)
2126 key = (char_u *)"";
2127 di = dict_find(dict, key, -1);
2128 if (di != NULL)
2129 {
2130 if (error_if_locked(di->di_tv.v_lock,
2131 e_cannot_change_dict_item))
2132 return FAIL;
2133 // overwrite existing value
2134 clear_tv(&di->di_tv);
2135 di->di_tv = *tv;
2136 }
2137 else
2138 {
2139 if (error_if_locked(dict->dv_lock, e_cannot_change_dict))
2140 return FAIL;
2141 // add to dict, only fails when out of memory
2142 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
2143 return NOTDONE;
2144 clear_tv(tv);
2145 }
2146 }
2147 else if (dest_type == VAR_BLOB)
2148 {
2149 long lidx = (long)tv_idx->vval.v_number;
2150 blob_T *blob = tv_dest->vval.v_blob;
2151 varnumber_T nr;
2152 int error = FALSE;
2153 int len;
2154
2155 if (blob == NULL)
2156 {
2157 emsg(_(e_blob_not_set));
2158 return FAIL;
2159 }
2160 len = blob_len(blob);
2161 if (lidx < 0 && len + lidx >= 0)
2162 // negative index is relative to the end
2163 lidx = len + lidx;
2164
2165 // Can add one byte at the end.
2166 if (lidx < 0 || lidx > len)
2167 {
2168 semsg(_(e_blob_index_out_of_range_nr), lidx);
2169 return FAIL;
2170 }
2171 if (value_check_lock(blob->bv_lock, (char_u *)"blob", FALSE))
2172 return FAIL;
2173 nr = tv_get_number_chk(tv, &error);
2174 if (error)
2175 return FAIL;
2176 blob_set_append(blob, lidx, nr);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002177 }
2178 else
2179 {
Bram Moolenaare08be092022-02-17 13:08:26 +00002180 status = FAIL;
2181 semsg(_(e_cannot_index_str), vartype_name(dest_type));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002182 }
2183 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002184
2185 clear_tv(tv_idx);
2186 clear_tv(tv_dest);
2187 ectx->ec_stack.ga_len -= 3;
2188 if (status == FAIL)
2189 {
2190 clear_tv(tv);
2191 return FAIL;
2192 }
2193 return OK;
2194}
2195
2196/*
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002197 * Store a value in a list or blob range.
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002198 */
2199 static int
2200execute_storerange(isn_T *iptr, ectx_T *ectx)
2201{
2202 typval_T *tv;
2203 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2204 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2205 typval_T *tv_dest = STACK_TV_BOT(-1);
2206 int status = OK;
2207
2208 // Stack contains:
2209 // -4 value to be stored
2210 // -3 first index or "none"
2211 // -2 second index or "none"
2212 // -1 destination list or blob
2213 tv = STACK_TV_BOT(-4);
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002214 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002215 if (tv_dest->v_type == VAR_LIST)
2216 {
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002217 long n1;
2218 long n2;
2219 listitem_T *li1;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002220
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002221 n1 = (long)tv_get_number_chk(tv_idx1, NULL);
2222 if (tv_idx2->v_type == VAR_SPECIAL
2223 && tv_idx2->vval.v_number == VVAL_NONE)
2224 n2 = list_len(tv_dest->vval.v_list) - 1;
2225 else
2226 n2 = (long)tv_get_number_chk(tv_idx2, NULL);
2227
Bram Moolenaar22ebd172022-04-01 15:26:58 +01002228 li1 = check_range_index_one(tv_dest->vval.v_list, &n1, TRUE, FALSE);
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002229 if (li1 == NULL)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002230 status = FAIL;
2231 else
2232 {
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002233 status = check_range_index_two(tv_dest->vval.v_list,
2234 &n1, li1, &n2, FALSE);
2235 if (status != FAIL)
2236 status = list_assign_range(
2237 tv_dest->vval.v_list,
2238 tv->vval.v_list,
2239 n1,
2240 n2,
2241 tv_idx2->v_type == VAR_SPECIAL,
2242 (char_u *)"=",
2243 (char_u *)"[unknown]");
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002244 }
2245 }
2246 else if (tv_dest->v_type == VAR_BLOB)
2247 {
2248 varnumber_T n1;
2249 varnumber_T n2;
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002250 long bloblen;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002251
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002252 n1 = tv_get_number_chk(tv_idx1, NULL);
2253 if (tv_idx2->v_type == VAR_SPECIAL
2254 && tv_idx2->vval.v_number == VVAL_NONE)
2255 n2 = blob_len(tv_dest->vval.v_blob) - 1;
2256 else
2257 n2 = tv_get_number_chk(tv_idx2, NULL);
2258 bloblen = blob_len(tv_dest->vval.v_blob);
2259
2260 if (check_blob_index(bloblen, n1, FALSE) == FAIL
2261 || check_blob_range(bloblen, n1, n2, FALSE) == FAIL)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002262 status = FAIL;
2263 else
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002264 status = blob_set_range(tv_dest->vval.v_blob, n1, n2, tv);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002265 }
2266 else
2267 {
2268 status = FAIL;
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002269 emsg(_(e_list_or_blob_required));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002270 }
2271
2272 clear_tv(tv_idx1);
2273 clear_tv(tv_idx2);
2274 clear_tv(tv_dest);
2275 ectx->ec_stack.ga_len -= 4;
2276 clear_tv(tv);
2277
2278 return status;
2279}
2280
2281/*
2282 * Unlet item in list or dict variable.
2283 */
2284 static int
2285execute_unletindex(isn_T *iptr, ectx_T *ectx)
2286{
2287 typval_T *tv_idx = STACK_TV_BOT(-2);
2288 typval_T *tv_dest = STACK_TV_BOT(-1);
2289 int status = OK;
2290
2291 // Stack contains:
2292 // -2 index
2293 // -1 dict or list
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002294 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002295 if (tv_dest->v_type == VAR_DICT)
2296 {
2297 // unlet a dict item, index must be a string
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002298 if (tv_idx->v_type != VAR_STRING && tv_idx->v_type != VAR_NUMBER)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002299 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002300 semsg(_(e_expected_str_but_got_str),
2301 vartype_name(VAR_STRING),
2302 vartype_name(tv_idx->v_type));
2303 status = FAIL;
2304 }
2305 else
2306 {
2307 dict_T *d = tv_dest->vval.v_dict;
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002308 char_u *key;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002309 dictitem_T *di = NULL;
2310
2311 if (d != NULL && value_check_lock(
2312 d->dv_lock, NULL, FALSE))
2313 status = FAIL;
2314 else
2315 {
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002316 if (tv_idx->v_type == VAR_STRING)
2317 {
2318 key = tv_idx->vval.v_string;
2319 if (key == NULL)
2320 key = (char_u *)"";
2321 }
2322 else
2323 {
2324 key = tv_get_string(tv_idx);
2325 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002326 if (d != NULL)
2327 di = dict_find(d, key, (int)STRLEN(key));
2328 if (di == NULL)
2329 {
2330 // NULL dict is equivalent to empty dict
2331 semsg(_(e_key_not_present_in_dictionary),
2332 key);
2333 status = FAIL;
2334 }
2335 else if (var_check_fixed(di->di_flags,
2336 NULL, FALSE)
2337 || var_check_ro(di->di_flags,
2338 NULL, FALSE))
2339 status = FAIL;
2340 else
2341 dictitem_remove(d, di);
2342 }
2343 }
2344 }
2345 else if (tv_dest->v_type == VAR_LIST)
2346 {
2347 // unlet a List item, index must be a number
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002348 if (check_for_number(tv_idx) == FAIL)
2349 {
2350 status = FAIL;
2351 }
2352 else
2353 {
2354 list_T *l = tv_dest->vval.v_list;
2355 long n = (long)tv_idx->vval.v_number;
2356
2357 if (l != NULL && value_check_lock(
2358 l->lv_lock, NULL, FALSE))
2359 status = FAIL;
2360 else
2361 {
2362 listitem_T *li = list_find(l, n);
2363
2364 if (li == NULL)
2365 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002366 semsg(_(e_list_index_out_of_range_nr), n);
2367 status = FAIL;
2368 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002369 else
2370 listitem_remove(l, li);
2371 }
2372 }
2373 }
2374 else
2375 {
2376 status = FAIL;
2377 semsg(_(e_cannot_index_str),
2378 vartype_name(tv_dest->v_type));
2379 }
2380
2381 clear_tv(tv_idx);
2382 clear_tv(tv_dest);
2383 ectx->ec_stack.ga_len -= 2;
2384
2385 return status;
2386}
2387
2388/*
2389 * Unlet a range of items in a list variable.
2390 */
2391 static int
2392execute_unletrange(isn_T *iptr, ectx_T *ectx)
2393{
2394 // Stack contains:
2395 // -3 index1
2396 // -2 index2
2397 // -1 dict or list
2398 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2399 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2400 typval_T *tv_dest = STACK_TV_BOT(-1);
2401 int status = OK;
2402
2403 if (tv_dest->v_type == VAR_LIST)
2404 {
2405 // indexes must be a number
2406 SOURCING_LNUM = iptr->isn_lnum;
2407 if (check_for_number(tv_idx1) == FAIL
2408 || (tv_idx2->v_type != VAR_SPECIAL
2409 && check_for_number(tv_idx2) == FAIL))
2410 {
2411 status = FAIL;
2412 }
2413 else
2414 {
2415 list_T *l = tv_dest->vval.v_list;
2416 long n1 = (long)tv_idx1->vval.v_number;
2417 long n2 = tv_idx2->v_type == VAR_SPECIAL
2418 ? 0 : (long)tv_idx2->vval.v_number;
2419 listitem_T *li;
2420
2421 li = list_find_index(l, &n1);
2422 if (li == NULL)
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002423 {
2424 semsg(_(e_list_index_out_of_range_nr),
2425 (long)tv_idx1->vval.v_number);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002426 status = FAIL;
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002427 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002428 else
2429 {
2430 if (n1 < 0)
2431 n1 = list_idx_of_item(l, li);
2432 if (n2 < 0)
2433 {
2434 listitem_T *li2 = list_find(l, n2);
2435
2436 if (li2 == NULL)
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002437 {
2438 semsg(_(e_list_index_out_of_range_nr), n2);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002439 status = FAIL;
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002440 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002441 else
2442 n2 = list_idx_of_item(l, li2);
2443 }
2444 if (status != FAIL
2445 && tv_idx2->v_type != VAR_SPECIAL
2446 && n2 < n1)
2447 {
2448 semsg(_(e_list_index_out_of_range_nr), n2);
2449 status = FAIL;
2450 }
Bram Moolenaar6b8c7ba2022-03-20 17:46:06 +00002451 if (status != FAIL)
2452 list_unlet_range(l, li, n1,
2453 tv_idx2->v_type != VAR_SPECIAL, n2);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002454 }
2455 }
2456 }
2457 else
2458 {
2459 status = FAIL;
2460 SOURCING_LNUM = iptr->isn_lnum;
2461 semsg(_(e_cannot_index_str),
2462 vartype_name(tv_dest->v_type));
2463 }
2464
2465 clear_tv(tv_idx1);
2466 clear_tv(tv_idx2);
2467 clear_tv(tv_dest);
2468 ectx->ec_stack.ga_len -= 3;
2469
2470 return status;
2471}
2472
2473/*
2474 * Top of a for loop.
2475 */
2476 static int
2477execute_for(isn_T *iptr, ectx_T *ectx)
2478{
2479 typval_T *tv;
2480 typval_T *ltv = STACK_TV_BOT(-1);
2481 typval_T *idxtv =
2482 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
2483
2484 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
2485 return FAIL;
2486 if (ltv->v_type == VAR_LIST)
2487 {
2488 list_T *list = ltv->vval.v_list;
2489
2490 // push the next item from the list
2491 ++idxtv->vval.v_number;
2492 if (list == NULL
2493 || idxtv->vval.v_number >= list->lv_len)
2494 {
2495 // past the end of the list, jump to "endfor"
2496 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
2497 may_restore_cmdmod(&ectx->ec_funclocal);
2498 }
2499 else if (list->lv_first == &range_list_item)
2500 {
2501 // non-materialized range() list
2502 tv = STACK_TV_BOT(0);
2503 tv->v_type = VAR_NUMBER;
2504 tv->v_lock = 0;
2505 tv->vval.v_number = list_find_nr(
2506 list, idxtv->vval.v_number, NULL);
2507 ++ectx->ec_stack.ga_len;
2508 }
2509 else
2510 {
2511 listitem_T *li = list_find(list,
2512 idxtv->vval.v_number);
2513
2514 copy_tv(&li->li_tv, STACK_TV_BOT(0));
2515 ++ectx->ec_stack.ga_len;
2516 }
2517 }
2518 else if (ltv->v_type == VAR_STRING)
2519 {
2520 char_u *str = ltv->vval.v_string;
2521
2522 // The index is for the last byte of the previous
2523 // character.
2524 ++idxtv->vval.v_number;
2525 if (str == NULL || str[idxtv->vval.v_number] == NUL)
2526 {
2527 // past the end of the string, jump to "endfor"
2528 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
2529 may_restore_cmdmod(&ectx->ec_funclocal);
2530 }
2531 else
2532 {
2533 int clen = mb_ptr2len(str + idxtv->vval.v_number);
2534
2535 // Push the next character from the string.
2536 tv = STACK_TV_BOT(0);
2537 tv->v_type = VAR_STRING;
2538 tv->vval.v_string = vim_strnsave(
2539 str + idxtv->vval.v_number, clen);
2540 ++ectx->ec_stack.ga_len;
2541 idxtv->vval.v_number += clen - 1;
2542 }
2543 }
2544 else if (ltv->v_type == VAR_BLOB)
2545 {
2546 blob_T *blob = ltv->vval.v_blob;
2547
2548 // When we get here the first time make a copy of the
2549 // blob, so that the iteration still works when it is
2550 // changed.
2551 if (idxtv->vval.v_number == -1 && blob != NULL)
2552 {
2553 blob_copy(blob, ltv);
2554 blob_unref(blob);
2555 blob = ltv->vval.v_blob;
2556 }
2557
2558 // The index is for the previous byte.
2559 ++idxtv->vval.v_number;
2560 if (blob == NULL
2561 || idxtv->vval.v_number >= blob_len(blob))
2562 {
2563 // past the end of the blob, jump to "endfor"
2564 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
2565 may_restore_cmdmod(&ectx->ec_funclocal);
2566 }
2567 else
2568 {
2569 // Push the next byte from the blob.
2570 tv = STACK_TV_BOT(0);
2571 tv->v_type = VAR_NUMBER;
2572 tv->vval.v_number = blob_get(blob,
2573 idxtv->vval.v_number);
2574 ++ectx->ec_stack.ga_len;
2575 }
2576 }
2577 else
2578 {
2579 semsg(_(e_for_loop_on_str_not_supported),
2580 vartype_name(ltv->v_type));
2581 return FAIL;
2582 }
2583 return OK;
2584}
2585
2586/*
Bram Moolenaar06b77222022-01-25 15:51:56 +00002587 * Load instruction for w:/b:/g:/t: variable.
2588 * "isn_type" is used instead of "iptr->isn_type".
2589 */
2590 static int
2591load_namespace_var(ectx_T *ectx, isntype_T isn_type, isn_T *iptr)
2592{
2593 dictitem_T *di = NULL;
2594 hashtab_T *ht = NULL;
2595 char namespace;
2596
2597 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
2598 return NOTDONE;
2599 switch (isn_type)
2600 {
2601 case ISN_LOADG:
2602 ht = get_globvar_ht();
2603 namespace = 'g';
2604 break;
2605 case ISN_LOADB:
2606 ht = &curbuf->b_vars->dv_hashtab;
2607 namespace = 'b';
2608 break;
2609 case ISN_LOADW:
2610 ht = &curwin->w_vars->dv_hashtab;
2611 namespace = 'w';
2612 break;
2613 case ISN_LOADT:
2614 ht = &curtab->tp_vars->dv_hashtab;
2615 namespace = 't';
2616 break;
2617 default: // Cannot reach here
2618 return NOTDONE;
2619 }
2620 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
2621
Bram Moolenaar06b77222022-01-25 15:51:56 +00002622 if (di == NULL)
2623 {
Bram Moolenaarfe732552022-02-22 19:39:13 +00002624 if (isn_type == ISN_LOADG)
2625 {
2626 ufunc_T *ufunc = find_func(iptr->isn_arg.string, TRUE);
2627
2628 // g:Something could be a function
2629 if (ufunc != NULL)
2630 {
2631 typval_T *tv = STACK_TV_BOT(0);
2632
2633 ++ectx->ec_stack.ga_len;
2634 tv->v_type = VAR_FUNC;
2635 tv->vval.v_string = alloc(STRLEN(iptr->isn_arg.string) + 3);
2636 if (tv->vval.v_string == NULL)
2637 return FAIL;
2638 STRCPY(tv->vval.v_string, "g:");
2639 STRCPY(tv->vval.v_string + 2, iptr->isn_arg.string);
2640 return OK;
2641 }
2642 }
Bram Moolenaar06b77222022-01-25 15:51:56 +00002643 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarfe732552022-02-22 19:39:13 +00002644 if (vim_strchr(iptr->isn_arg.string, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar06b77222022-01-25 15:51:56 +00002645 // no check if the item exists in the script but
2646 // isn't exported, it is too complicated
Bram Moolenaarfe732552022-02-22 19:39:13 +00002647 semsg(_(e_item_not_found_in_script_str), iptr->isn_arg.string);
Bram Moolenaar06b77222022-01-25 15:51:56 +00002648 else
2649 semsg(_(e_undefined_variable_char_str),
Bram Moolenaarfe732552022-02-22 19:39:13 +00002650 namespace, iptr->isn_arg.string);
Bram Moolenaar06b77222022-01-25 15:51:56 +00002651 return FAIL;
2652 }
2653 else
2654 {
2655 copy_tv(&di->di_tv, STACK_TV_BOT(0));
2656 ++ectx->ec_stack.ga_len;
2657 }
2658 return OK;
2659}
2660
2661/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02002662 * Execute instructions in execution context "ectx".
2663 * Return OK or FAIL;
2664 */
2665 static int
2666exec_instructions(ectx_T *ectx)
2667{
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002668 int ret = FAIL;
2669 int save_trylevel_at_start = ectx->ec_trylevel_at_start;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02002670 int dict_stack_len_at_start = dict_stack.ga_len;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01002671
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02002672 // Start execution at the first instruction.
Bram Moolenaar4c137212021-04-19 16:48:48 +02002673 ectx->ec_iidx = 0;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01002674
Bram Moolenaarff652882021-05-16 15:24:49 +02002675 // Only catch exceptions in this instruction list.
2676 ectx->ec_trylevel_at_start = trylevel;
2677
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002678 for (;;)
2679 {
Bram Moolenaara7490192021-07-22 12:26:14 +02002680 static int breakcheck_count = 0; // using "static" makes it faster
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002681 isn_T *iptr;
Bram Moolenaara7490192021-07-22 12:26:14 +02002682 typval_T *tv;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002683
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002684 if (unlikely(++breakcheck_count >= 100))
Bram Moolenaar270d0382020-05-15 21:42:53 +02002685 {
2686 line_breakcheck();
2687 breakcheck_count = 0;
2688 }
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002689 if (unlikely(got_int))
Bram Moolenaar20431c92020-03-20 18:39:46 +01002690 {
2691 // Turn CTRL-C into an exception.
2692 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +01002693 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002694 goto theend;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002695 did_throw = TRUE;
2696 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002697
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002698 if (unlikely(did_emsg && msg_list != NULL && *msg_list != NULL))
Bram Moolenaara26b9702020-04-18 19:53:28 +02002699 {
2700 // Turn an error message into an exception.
2701 did_emsg = FALSE;
2702 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002703 goto theend;
Bram Moolenaara26b9702020-04-18 19:53:28 +02002704 did_throw = TRUE;
2705 *msg_list = NULL;
2706 }
2707
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002708 if (unlikely(did_throw))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002709 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002710 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002711 trycmd_T *trycmd = NULL;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002712 int index = trystack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002713
2714 // An exception jumps to the first catch, finally, or returns from
2715 // the current function.
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002716 while (index > 0)
2717 {
2718 trycmd = ((trycmd_T *)trystack->ga_data) + index - 1;
Bram Moolenaar834193a2021-06-30 20:39:15 +02002719 if (!trycmd->tcd_in_catch || trycmd->tcd_finally_idx != 0)
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002720 break;
2721 // In the catch and finally block of this try we have to go up
2722 // one level.
2723 --index;
2724 trycmd = NULL;
2725 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02002726 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002727 {
Bram Moolenaar834193a2021-06-30 20:39:15 +02002728 if (trycmd->tcd_in_catch)
2729 {
2730 // exception inside ":catch", jump to ":finally" once
2731 ectx->ec_iidx = trycmd->tcd_finally_idx;
2732 trycmd->tcd_finally_idx = 0;
2733 }
2734 else
2735 // jump to first ":catch"
2736 ectx->ec_iidx = trycmd->tcd_catch_idx;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002737 trycmd->tcd_in_catch = TRUE;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002738 did_throw = FALSE; // don't come back here until :endtry
2739 trycmd->tcd_did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002740 }
2741 else
2742 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02002743 // Not inside try or need to return from current functions.
2744 // Push a dummy return value.
Bram Moolenaar35578162021-08-02 19:10:38 +02002745 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002746 goto theend;
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02002747 tv = STACK_TV_BOT(0);
2748 tv->v_type = VAR_NUMBER;
2749 tv->vval.v_number = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002750 ++ectx->ec_stack.ga_len;
2751 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002752 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02002753 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +01002754 need_rethrow = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002755 if (handle_closure_in_use(ectx, FALSE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002756 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002757 goto done;
2758 }
2759
Bram Moolenaar4c137212021-04-19 16:48:48 +02002760 if (func_return(ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002761 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002762 }
2763 continue;
2764 }
2765
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002766 /*
2767 * Big switch on the instruction. Most compilers will be turning this
2768 * into an efficient lookup table, since the "case" values are an enum
2769 * with sequential numbers. It may look ugly, but it should be the
2770 * most efficient way.
2771 */
Bram Moolenaar4c137212021-04-19 16:48:48 +02002772 iptr = &ectx->ec_instr[ectx->ec_iidx++];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002773 switch (iptr->isn_type)
2774 {
2775 // execute Ex command line
2776 case ISN_EXEC:
Bram Moolenaaraacc9662021-08-13 19:40:51 +02002777 if (exec_command(iptr) == FAIL)
2778 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002779 break;
2780
Bram Moolenaar20677332021-06-06 17:02:53 +02002781 // execute Ex command line split at NL characters.
2782 case ISN_EXEC_SPLIT:
2783 {
2784 source_cookie_T cookie;
Bram Moolenaar518df272021-06-06 17:34:13 +02002785 char_u *line;
Bram Moolenaar20677332021-06-06 17:02:53 +02002786
2787 SOURCING_LNUM = iptr->isn_lnum;
2788 CLEAR_FIELD(cookie);
2789 cookie.sourcing_lnum = iptr->isn_lnum - 1;
2790 cookie.nextline = iptr->isn_arg.string;
Bram Moolenaar518df272021-06-06 17:34:13 +02002791 line = get_split_sourceline(0, &cookie, 0, 0);
2792 if (do_cmdline(line,
Bram Moolenaar20677332021-06-06 17:02:53 +02002793 get_split_sourceline, &cookie,
2794 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED)
2795 == FAIL
2796 || did_emsg)
Bram Moolenaar518df272021-06-06 17:34:13 +02002797 {
2798 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02002799 goto on_error;
Bram Moolenaar518df272021-06-06 17:34:13 +02002800 }
2801 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02002802 }
2803 break;
2804
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002805 // execute Ex command line that is only a range
2806 case ISN_EXECRANGE:
2807 {
2808 exarg_T ea;
2809 char *error = NULL;
2810
2811 CLEAR_FIELD(ea);
2812 ea.cmdidx = CMD_SIZE;
2813 ea.addr_type = ADDR_LINES;
2814 ea.cmd = iptr->isn_arg.string;
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00002815 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002816 parse_cmd_address(&ea, &error, FALSE);
Bram Moolenaar01a4dcb2021-12-04 13:15:10 +00002817 if (ea.cmd == NULL)
2818 goto on_error;
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00002819 // error is always NULL when using ADDR_LINES
2820 error = ex_range_without_command(&ea);
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002821 if (error != NULL)
2822 {
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002823 emsg(error);
2824 goto on_error;
2825 }
2826 }
2827 break;
2828
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02002829 // Evaluate an expression with legacy syntax, push it onto the
2830 // stack.
2831 case ISN_LEGACY_EVAL:
2832 {
2833 char_u *arg = iptr->isn_arg.string;
2834 int res;
2835 int save_flags = cmdmod.cmod_flags;
2836
Bram Moolenaar35578162021-08-02 19:10:38 +02002837 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002838 goto theend;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02002839 tv = STACK_TV_BOT(0);
2840 init_tv(tv);
2841 cmdmod.cmod_flags |= CMOD_LEGACY;
2842 res = eval0(arg, tv, NULL, &EVALARG_EVALUATE);
2843 cmdmod.cmod_flags = save_flags;
2844 if (res == FAIL)
2845 goto on_error;
2846 ++ectx->ec_stack.ga_len;
2847 }
2848 break;
2849
Bram Moolenaarf18332f2021-05-07 17:55:55 +02002850 // push typeval VAR_INSTR with instructions to be executed
2851 case ISN_INSTR:
2852 {
Bram Moolenaar35578162021-08-02 19:10:38 +02002853 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002854 goto theend;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02002855 tv = STACK_TV_BOT(0);
2856 tv->vval.v_instr = ALLOC_ONE(instr_T);
2857 if (tv->vval.v_instr == NULL)
2858 goto on_error;
2859 ++ectx->ec_stack.ga_len;
2860
2861 tv->v_type = VAR_INSTR;
2862 tv->vval.v_instr->instr_ectx = ectx;
2863 tv->vval.v_instr->instr_instr = iptr->isn_arg.instr;
2864 }
2865 break;
2866
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002867 case ISN_SOURCE:
2868 {
Bram Moolenaar89445512022-04-14 12:58:23 +01002869 int notused;
LemonBoy77142312022-04-15 20:50:46 +01002870
Bram Moolenaar89445512022-04-14 12:58:23 +01002871 SOURCING_LNUM = iptr->isn_lnum;
2872 if (may_load_script((int)iptr->isn_arg.number, &notused)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002873 == FAIL)
Bram Moolenaar89445512022-04-14 12:58:23 +01002874 goto on_error;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002875 }
2876 break;
2877
Bram Moolenaar4c137212021-04-19 16:48:48 +02002878 // execute :substitute with an expression
2879 case ISN_SUBSTITUTE:
2880 {
2881 subs_T *subs = &iptr->isn_arg.subs;
2882 source_cookie_T cookie;
2883 struct subs_expr_S *save_instr = substitute_instr;
2884 struct subs_expr_S subs_instr;
2885 int res;
2886
2887 subs_instr.subs_ectx = ectx;
2888 subs_instr.subs_instr = subs->subs_instr;
2889 subs_instr.subs_status = OK;
2890 substitute_instr = &subs_instr;
2891
2892 SOURCING_LNUM = iptr->isn_lnum;
2893 // This is very much like ISN_EXEC
2894 CLEAR_FIELD(cookie);
2895 cookie.sourcing_lnum = iptr->isn_lnum - 1;
2896 res = do_cmdline(subs->subs_cmd,
2897 getsourceline, &cookie,
2898 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
2899 substitute_instr = save_instr;
2900
2901 if (res == FAIL || did_emsg
2902 || subs_instr.subs_status == FAIL)
2903 goto on_error;
2904 }
2905 break;
2906
2907 case ISN_FINISH:
2908 goto done;
2909
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02002910 case ISN_REDIRSTART:
2911 // create a dummy entry for var_redir_str()
2912 if (alloc_redir_lval() == FAIL)
2913 goto on_error;
2914
2915 // The output is stored in growarray "redir_ga" until
2916 // redirection ends.
2917 init_redir_ga();
2918 redir_vname = 1;
2919 break;
2920
2921 case ISN_REDIREND:
2922 {
2923 char_u *res = get_clear_redir_ga();
2924
2925 // End redirection, put redirected text on the stack.
2926 clear_redir_lval();
2927 redir_vname = 0;
2928
Bram Moolenaar35578162021-08-02 19:10:38 +02002929 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02002930 {
2931 vim_free(res);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002932 goto theend;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02002933 }
2934 tv = STACK_TV_BOT(0);
2935 tv->v_type = VAR_STRING;
2936 tv->vval.v_string = res;
2937 ++ectx->ec_stack.ga_len;
2938 }
2939 break;
2940
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002941 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02002942#ifdef FEAT_QUICKFIX
Bram Moolenaar397a87a2022-03-20 21:14:15 +00002943 force_abort = TRUE;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002944 if (trigger_cexpr_autocmd(iptr->isn_arg.number) == FAIL)
2945 goto on_error;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00002946 force_abort = FALSE;
Bram Moolenaarb7c97812021-05-05 22:51:39 +02002947#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002948 break;
2949
2950 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02002951#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002952 {
2953 exarg_T ea;
2954 int res;
2955
2956 CLEAR_FIELD(ea);
2957 ea.cmdidx = iptr->isn_arg.cexpr.cexpr_ref->cer_cmdidx;
2958 ea.forceit = iptr->isn_arg.cexpr.cexpr_ref->cer_forceit;
2959 ea.cmdlinep = &iptr->isn_arg.cexpr.cexpr_ref->cer_cmdline;
2960 --ectx->ec_stack.ga_len;
2961 tv = STACK_TV_BOT(0);
Bram Moolenaarbd683e32022-07-18 17:49:03 +01002962 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002963 res = cexpr_core(&ea, tv);
2964 clear_tv(tv);
2965 if (res == FAIL)
2966 goto on_error;
2967 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02002968#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002969 break;
2970
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002971 // execute Ex command from pieces on the stack
2972 case ISN_EXECCONCAT:
2973 {
2974 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +02002975 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002976 int pass;
2977 int i;
2978 char_u *cmd = NULL;
2979 char_u *str;
2980
2981 for (pass = 1; pass <= 2; ++pass)
2982 {
2983 for (i = 0; i < count; ++i)
2984 {
2985 tv = STACK_TV_BOT(i - count);
2986 str = tv->vval.v_string;
2987 if (str != NULL && *str != NUL)
2988 {
2989 if (pass == 2)
2990 STRCPY(cmd + len, str);
2991 len += STRLEN(str);
2992 }
2993 if (pass == 2)
2994 clear_tv(tv);
2995 }
2996 if (pass == 1)
2997 {
2998 cmd = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002999 if (unlikely(cmd == NULL))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003000 goto theend;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003001 len = 0;
3002 }
3003 }
3004
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02003005 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003006 do_cmdline_cmd(cmd);
3007 vim_free(cmd);
3008 }
3009 break;
3010
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003011 // execute :echo {string} ...
3012 case ISN_ECHO:
3013 {
3014 int count = iptr->isn_arg.echo.echo_count;
3015 int atstart = TRUE;
3016 int needclr = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003017 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003018
3019 for (idx = 0; idx < count; ++idx)
3020 {
3021 tv = STACK_TV_BOT(idx - count);
3022 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
3023 &atstart, &needclr);
3024 clear_tv(tv);
3025 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01003026 if (needclr)
3027 msg_clr_eos();
Bram Moolenaar4c137212021-04-19 16:48:48 +02003028 ectx->ec_stack.ga_len -= count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003029 }
3030 break;
3031
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003032 // :execute {string} ...
3033 // :echomsg {string} ...
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01003034 // :echowindow {string} ...
Bram Moolenaar7de62622021-08-07 15:05:47 +02003035 // :echoconsole {string} ...
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003036 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01003037 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003038 case ISN_ECHOMSG:
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01003039 case ISN_ECHOWINDOW:
Bram Moolenaar7de62622021-08-07 15:05:47 +02003040 case ISN_ECHOCONSOLE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003041 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01003042 {
3043 int count = iptr->isn_arg.number;
3044 garray_T ga;
3045 char_u buf[NUMBUFLEN];
3046 char_u *p;
3047 int len;
3048 int failed = FALSE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003049 int idx;
Bram Moolenaarad39c092020-02-26 18:23:43 +01003050
3051 ga_init2(&ga, 1, 80);
3052 for (idx = 0; idx < count; ++idx)
3053 {
3054 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003055 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01003056 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003057 if (tv->v_type == VAR_CHANNEL
3058 || tv->v_type == VAR_JOB)
3059 {
3060 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar68db9962021-05-09 23:19:22 +02003061 semsg(_(e_using_invalid_value_as_string_str),
3062 vartype_name(tv->v_type));
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003063 break;
3064 }
3065 else
3066 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01003067 }
3068 else
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003069 p = tv_stringify(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01003070
3071 len = (int)STRLEN(p);
Bram Moolenaar35578162021-08-02 19:10:38 +02003072 if (GA_GROW_FAILS(&ga, len + 2))
Bram Moolenaarad39c092020-02-26 18:23:43 +01003073 failed = TRUE;
3074 else
3075 {
3076 if (ga.ga_len > 0)
3077 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
3078 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
3079 ga.ga_len += len;
3080 }
3081 clear_tv(tv);
3082 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003083 ectx->ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003084 if (failed)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003085 {
3086 ga_clear(&ga);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003087 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003088 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01003089
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003090 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003091 {
3092 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02003093 {
3094 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003095 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01003096 if (did_emsg)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003097 {
3098 ga_clear(&ga);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01003099 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003100 }
Bram Moolenaar430deb12020-08-23 16:29:11 +02003101 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003102 else
3103 {
3104 msg_sb_eol();
3105 if (iptr->isn_type == ISN_ECHOMSG)
3106 {
3107 msg_attr(ga.ga_data, echo_attr);
3108 out_flush();
3109 }
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01003110#ifdef HAS_MESSAGE_WINDOW
3111 else if (iptr->isn_type == ISN_ECHOWINDOW)
3112 {
3113 start_echowindow();
3114 msg_attr(ga.ga_data, echo_attr);
3115 end_echowindow();
3116 }
3117#endif
Bram Moolenaar7de62622021-08-07 15:05:47 +02003118 else if (iptr->isn_type == ISN_ECHOCONSOLE)
3119 {
3120 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data),
3121 TRUE);
3122 ui_write((char_u *)"\r\n", 2, TRUE);
3123 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003124 else
3125 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003126 SOURCING_LNUM = iptr->isn_lnum;
3127 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003128 }
3129 }
3130 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01003131 ga_clear(&ga);
3132 }
3133 break;
3134
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003135 // load local variable or argument
3136 case ISN_LOAD:
Bram Moolenaar35578162021-08-02 19:10:38 +02003137 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003138 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003139 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003140 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003141 break;
3142
3143 // load v: variable
3144 case ISN_LOADV:
Bram Moolenaar35578162021-08-02 19:10:38 +02003145 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003146 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003147 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003148 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003149 break;
3150
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003151 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003152 case ISN_LOADSCRIPT:
3153 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01003154 scriptref_T *sref = iptr->isn_arg.script.scriptref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003155 svar_T *sv;
3156
Bram Moolenaar6db660b2021-08-01 14:08:54 +02003157 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003158 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003159 goto theend;
Bram Moolenaar859cc212022-03-28 15:22:35 +01003160 allocate_if_null(sv);
Bram Moolenaar35578162021-08-02 19:10:38 +02003161 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003162 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003163 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003164 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003165 }
3166 break;
3167
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003168 // load s: variable in old script or autoload import
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003169 case ISN_LOADS:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003170 case ISN_LOADEXPORT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003171 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003172 int sid = iptr->isn_arg.loadstore.ls_sid;
3173 hashtab_T *ht = &SCRIPT_VARS(sid);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003174 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003175 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003176
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003177 if (di == NULL)
3178 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003179 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003180 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003181 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003182 }
3183 else
3184 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003185 if (iptr->isn_type == ISN_LOADEXPORT)
3186 {
3187 int idx = get_script_item_idx(sid, name, 0,
3188 NULL, NULL);
3189 svar_T *sv;
3190
3191 if (idx >= 0)
3192 {
3193 sv = ((svar_T *)SCRIPT_ITEM(sid)
3194 ->sn_var_vals.ga_data) + idx;
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003195 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003196 {
3197 SOURCING_LNUM = iptr->isn_lnum;
3198 semsg(_(e_item_not_exported_in_script_str),
3199 name);
3200 goto on_error;
3201 }
3202 }
3203 }
Bram Moolenaar35578162021-08-02 19:10:38 +02003204 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003205 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003206 copy_tv(&di->di_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003207 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003208 }
3209 }
3210 break;
3211
Bram Moolenaard3aac292020-04-19 14:32:17 +02003212 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003213 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02003214 case ISN_LOADB:
3215 case ISN_LOADW:
3216 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003217 {
Bram Moolenaar06b77222022-01-25 15:51:56 +00003218 int res = load_namespace_var(ectx, iptr->isn_type, iptr);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003219
Bram Moolenaar06b77222022-01-25 15:51:56 +00003220 if (res == NOTDONE)
Bram Moolenaarcbbc48f2022-01-18 12:58:28 +00003221 goto theend;
Bram Moolenaar06b77222022-01-25 15:51:56 +00003222 if (res == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003223 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003224 }
Bram Moolenaar06b77222022-01-25 15:51:56 +00003225
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003226 break;
3227
Bram Moolenaar03290b82020-12-19 16:30:44 +01003228 // load autoload variable
3229 case ISN_LOADAUTO:
3230 {
3231 char_u *name = iptr->isn_arg.string;
3232
Bram Moolenaar35578162021-08-02 19:10:38 +02003233 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003234 goto theend;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003235 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003236 if (eval_variable(name, (int)STRLEN(name), 0,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003237 STACK_TV_BOT(0), NULL, EVAL_VAR_VERBOSE) == FAIL)
Bram Moolenaar03290b82020-12-19 16:30:44 +01003238 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003239 ++ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003240 }
3241 break;
3242
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003243 // load g:/b:/w:/t: namespace
3244 case ISN_LOADGDICT:
3245 case ISN_LOADBDICT:
3246 case ISN_LOADWDICT:
3247 case ISN_LOADTDICT:
3248 {
3249 dict_T *d = NULL;
3250
3251 switch (iptr->isn_type)
3252 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02003253 case ISN_LOADGDICT: d = get_globvar_dict(); break;
3254 case ISN_LOADBDICT: d = curbuf->b_vars; break;
3255 case ISN_LOADWDICT: d = curwin->w_vars; break;
3256 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003257 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003258 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003259 }
Bram Moolenaar35578162021-08-02 19:10:38 +02003260 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003261 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003262 tv = STACK_TV_BOT(0);
3263 tv->v_type = VAR_DICT;
3264 tv->v_lock = 0;
3265 tv->vval.v_dict = d;
Bram Moolenaar1bd3cb22021-02-24 12:27:31 +01003266 ++d->dv_refcount;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003267 ++ectx->ec_stack.ga_len;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003268 }
3269 break;
3270
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003271 // load &option
3272 case ISN_LOADOPT:
3273 {
3274 typval_T optval;
3275 char_u *name = iptr->isn_arg.string;
3276
Bram Moolenaara8c17702020-04-01 21:17:24 +02003277 // This is not expected to fail, name is checked during
3278 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar35578162021-08-02 19:10:38 +02003279 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003280 goto theend;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003281 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003282 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003283 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003284 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003285 }
3286 break;
3287
3288 // load $ENV
3289 case ISN_LOADENV:
3290 {
3291 typval_T optval;
3292 char_u *name = iptr->isn_arg.string;
3293
Bram Moolenaar35578162021-08-02 19:10:38 +02003294 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003295 goto theend;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003296 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003297 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003298 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003299 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003300 }
3301 break;
3302
3303 // load @register
3304 case ISN_LOADREG:
Bram Moolenaar35578162021-08-02 19:10:38 +02003305 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003306 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003307 tv = STACK_TV_BOT(0);
3308 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003309 tv->v_lock = 0;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02003310 // This may result in NULL, which should be equivalent to an
3311 // empty string.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003312 tv->vval.v_string = get_reg_contents(
3313 iptr->isn_arg.number, GREG_EXPR_SRC);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003314 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003315 break;
3316
3317 // store local variable
3318 case ISN_STORE:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003319 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003320 tv = STACK_TV_VAR(iptr->isn_arg.number);
3321 clear_tv(tv);
3322 *tv = *STACK_TV_BOT(0);
3323 break;
3324
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003325 // store s: variable in old script or autoload import
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003326 case ISN_STORES:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003327 case ISN_STOREEXPORT:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003328 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003329 int sid = iptr->isn_arg.loadstore.ls_sid;
3330 hashtab_T *ht = &SCRIPT_VARS(sid);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003331 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003332 dictitem_T *di = find_var_in_ht(ht, 0,
3333 iptr->isn_type == ISN_STORES
3334 ? name + 2 : name, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003335
Bram Moolenaar4c137212021-04-19 16:48:48 +02003336 --ectx->ec_stack.ga_len;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003337 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003338 if (di == NULL)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003339 {
3340 if (iptr->isn_type == ISN_STOREEXPORT)
3341 {
3342 semsg(_(e_undefined_variable_str), name);
Bram Moolenaard1d26842022-03-31 10:13:47 +01003343 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003344 goto on_error;
3345 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003346 store_var(name, STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003347 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003348 else
3349 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003350 if (iptr->isn_type == ISN_STOREEXPORT)
3351 {
3352 int idx = get_script_item_idx(sid, name, 0,
3353 NULL, NULL);
3354
Bram Moolenaar06651632022-04-27 17:54:25 +01003355 // can this ever fail?
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003356 if (idx >= 0)
3357 {
3358 svar_T *sv = ((svar_T *)SCRIPT_ITEM(sid)
3359 ->sn_var_vals.ga_data) + idx;
3360
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003361 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003362 {
3363 semsg(_(e_item_not_exported_in_script_str),
3364 name);
Bram Moolenaard1d26842022-03-31 10:13:47 +01003365 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003366 goto on_error;
3367 }
3368 }
3369 }
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02003370 if (var_check_permission(di, name) == FAIL)
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003371 {
3372 clear_tv(STACK_TV_BOT(0));
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02003373 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003374 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003375 clear_tv(&di->di_tv);
3376 di->di_tv = *STACK_TV_BOT(0);
3377 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003378 }
3379 break;
3380
3381 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003382 case ISN_STORESCRIPT:
3383 {
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003384 scriptref_T *sref = iptr->isn_arg.script.scriptref;
3385 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003386
Bram Moolenaar6db660b2021-08-01 14:08:54 +02003387 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003388 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003389 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003390 --ectx->ec_stack.ga_len;
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003391
3392 // "const" and "final" are checked at compile time, locking
3393 // the value needs to be checked here.
3394 SOURCING_LNUM = iptr->isn_lnum;
3395 if (value_check_lock(sv->sv_tv->v_lock, sv->sv_name, FALSE))
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003396 {
3397 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003398 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003399 }
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003400
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003401 clear_tv(sv->sv_tv);
3402 *sv->sv_tv = *STACK_TV_BOT(0);
3403 }
3404 break;
3405
3406 // store option
3407 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003408 case ISN_STOREFUNCOPT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003409 {
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003410 char_u *opt_name = iptr->isn_arg.storeopt.so_name;
3411 int opt_flags = iptr->isn_arg.storeopt.so_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003412 long n = 0;
3413 char_u *s = NULL;
3414 char *msg;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003415 char_u numbuf[NUMBUFLEN];
3416 char_u *tofree = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003417
Bram Moolenaar4c137212021-04-19 16:48:48 +02003418 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003419 tv = STACK_TV_BOT(0);
3420 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01003421 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003422 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01003423 if (s == NULL)
3424 s = (char_u *)"";
3425 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003426 else if (iptr->isn_type == ISN_STOREFUNCOPT)
3427 {
3428 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003429 // If the option can be set to a function reference or
3430 // a lambda and the passed value is a function
3431 // reference, then convert it to the name (string) of
3432 // the function reference.
3433 s = tv2string(tv, &tofree, numbuf, 0);
3434 if (s == NULL || *s == NUL)
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003435 {
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003436 // cannot happen?
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003437 clear_tv(tv);
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003438 vim_free(tofree);
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003439 goto on_error;
3440 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003441 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003442 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003443 // must be VAR_NUMBER, CHECKTYPE makes sure
3444 n = tv->vval.v_number;
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003445 msg = set_option_value(opt_name, n, s, opt_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02003446 clear_tv(tv);
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003447 vim_free(tofree);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003448 if (msg != NULL)
3449 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003450 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003451 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02003452 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003453 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003454 }
3455 break;
3456
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003457 // store $ENV
3458 case ISN_STOREENV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003459 --ectx->ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003460 tv = STACK_TV_BOT(0);
3461 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
3462 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003463 break;
3464
3465 // store @r
3466 case ISN_STOREREG:
3467 {
3468 int reg = iptr->isn_arg.number;
3469
Bram Moolenaar4c137212021-04-19 16:48:48 +02003470 --ectx->ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01003471 tv = STACK_TV_BOT(0);
Bram Moolenaar74f4a962021-06-17 21:03:07 +02003472 write_reg_contents(reg, tv_get_string(tv), -1, FALSE);
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01003473 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003474 }
3475 break;
3476
3477 // store v: variable
3478 case ISN_STOREV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003479 --ectx->ec_stack.ga_len;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003480 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
3481 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003482 // should not happen, type is checked when compiling
3483 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003484 break;
3485
Bram Moolenaard3aac292020-04-19 14:32:17 +02003486 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003487 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02003488 case ISN_STOREB:
3489 case ISN_STOREW:
3490 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003491 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003492 dictitem_T *di;
3493 hashtab_T *ht;
3494 char_u *name = iptr->isn_arg.string + 2;
3495
Bram Moolenaard3aac292020-04-19 14:32:17 +02003496 switch (iptr->isn_type)
3497 {
3498 case ISN_STOREG:
3499 ht = get_globvar_ht();
3500 break;
3501 case ISN_STOREB:
3502 ht = &curbuf->b_vars->dv_hashtab;
3503 break;
3504 case ISN_STOREW:
3505 ht = &curwin->w_vars->dv_hashtab;
3506 break;
3507 case ISN_STORET:
3508 ht = &curtab->tp_vars->dv_hashtab;
3509 break;
3510 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003511 goto theend;
Bram Moolenaard3aac292020-04-19 14:32:17 +02003512 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003513
Bram Moolenaar4c137212021-04-19 16:48:48 +02003514 --ectx->ec_stack.ga_len;
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003515 di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003516 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003517 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003518 else
3519 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003520 SOURCING_LNUM = iptr->isn_lnum;
3521 if (var_check_permission(di, name) == FAIL)
3522 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003523 clear_tv(&di->di_tv);
3524 di->di_tv = *STACK_TV_BOT(0);
3525 }
3526 }
3527 break;
3528
Bram Moolenaar03290b82020-12-19 16:30:44 +01003529 // store an autoload variable
3530 case ISN_STOREAUTO:
3531 SOURCING_LNUM = iptr->isn_lnum;
3532 set_var(iptr->isn_arg.string, STACK_TV_BOT(-1), TRUE);
3533 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003534 --ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003535 break;
3536
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003537 // store number in local variable
3538 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01003539 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003540 clear_tv(tv);
3541 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01003542 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003543 break;
3544
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01003545 // store value in list or dict variable
3546 case ISN_STOREINDEX:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003547 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003548 int res = execute_storeindex(iptr, ectx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003549
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003550 if (res == FAIL)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02003551 goto on_error;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003552 if (res == NOTDONE)
3553 goto theend;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003554 }
3555 break;
3556
Bram Moolenaarea5c8982022-02-17 14:42:02 +00003557 // store value in list or blob range
Bram Moolenaar68452172021-04-12 21:21:02 +02003558 case ISN_STORERANGE:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003559 if (execute_storerange(iptr, ectx) == FAIL)
3560 goto on_error;
Bram Moolenaar68452172021-04-12 21:21:02 +02003561 break;
3562
Bram Moolenaar0186e582021-01-10 18:33:11 +01003563 // load or store variable or argument from outer scope
3564 case ISN_LOADOUTER:
3565 case ISN_STOREOUTER:
3566 {
3567 int depth = iptr->isn_arg.outer.outer_depth;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02003568 outer_T *outer = ectx->ec_outer_ref == NULL ? NULL
3569 : ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003570
3571 while (depth > 1 && outer != NULL)
3572 {
3573 outer = outer->out_up;
3574 --depth;
3575 }
3576 if (outer == NULL)
3577 {
3578 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar69c76172021-12-02 16:38:52 +00003579 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx
3580 || ectx->ec_outer_ref == NULL)
3581 // Possibly :def function called from legacy
3582 // context.
3583 emsg(_(e_closure_called_from_invalid_context));
3584 else
3585 iemsg("LOADOUTER depth more than scope levels");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003586 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003587 }
3588 tv = ((typval_T *)outer->out_stack->ga_data)
3589 + outer->out_frame_idx + STACK_FRAME_SIZE
3590 + iptr->isn_arg.outer.outer_idx;
3591 if (iptr->isn_type == ISN_LOADOUTER)
3592 {
Bram Moolenaar35578162021-08-02 19:10:38 +02003593 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003594 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003595 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003596 ++ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003597 }
3598 else
3599 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003600 --ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003601 clear_tv(tv);
3602 *tv = *STACK_TV_BOT(0);
3603 }
3604 }
3605 break;
3606
Bram Moolenaar752fc692021-01-04 21:57:11 +01003607 // unlet item in list or dict variable
3608 case ISN_UNLETINDEX:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003609 if (execute_unletindex(iptr, ectx) == FAIL)
3610 goto on_error;
Bram Moolenaar752fc692021-01-04 21:57:11 +01003611 break;
3612
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01003613 // unlet range of items in list variable
3614 case ISN_UNLETRANGE:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003615 if (execute_unletrange(iptr, ectx) == FAIL)
3616 goto on_error;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01003617 break;
3618
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003619 // push constant
3620 case ISN_PUSHNR:
3621 case ISN_PUSHBOOL:
3622 case ISN_PUSHSPEC:
3623 case ISN_PUSHF:
3624 case ISN_PUSHS:
3625 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003626 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003627 case ISN_PUSHCHANNEL:
3628 case ISN_PUSHJOB:
Bram Moolenaar35578162021-08-02 19:10:38 +02003629 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003630 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003631 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003632 tv->v_lock = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003633 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003634 switch (iptr->isn_type)
3635 {
3636 case ISN_PUSHNR:
3637 tv->v_type = VAR_NUMBER;
3638 tv->vval.v_number = iptr->isn_arg.number;
3639 break;
3640 case ISN_PUSHBOOL:
3641 tv->v_type = VAR_BOOL;
3642 tv->vval.v_number = iptr->isn_arg.number;
3643 break;
3644 case ISN_PUSHSPEC:
3645 tv->v_type = VAR_SPECIAL;
3646 tv->vval.v_number = iptr->isn_arg.number;
3647 break;
3648#ifdef FEAT_FLOAT
3649 case ISN_PUSHF:
3650 tv->v_type = VAR_FLOAT;
3651 tv->vval.v_float = iptr->isn_arg.fnumber;
3652 break;
3653#endif
3654 case ISN_PUSHBLOB:
3655 blob_copy(iptr->isn_arg.blob, tv);
3656 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003657 case ISN_PUSHFUNC:
3658 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01003659 if (iptr->isn_arg.string == NULL)
3660 tv->vval.v_string = NULL;
3661 else
3662 tv->vval.v_string =
3663 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003664 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003665 case ISN_PUSHCHANNEL:
3666#ifdef FEAT_JOB_CHANNEL
3667 tv->v_type = VAR_CHANNEL;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003668 tv->vval.v_channel = NULL;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003669#endif
3670 break;
3671 case ISN_PUSHJOB:
3672#ifdef FEAT_JOB_CHANNEL
3673 tv->v_type = VAR_JOB;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003674 tv->vval.v_job = NULL;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003675#endif
3676 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003677 default:
3678 tv->v_type = VAR_STRING;
Bram Moolenaarf8691002022-03-10 12:20:53 +00003679 tv->vval.v_string = iptr->isn_arg.string == NULL
3680 ? NULL : vim_strsave(iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003681 }
3682 break;
3683
Bram Moolenaar06b77222022-01-25 15:51:56 +00003684 case ISN_AUTOLOAD:
3685 {
3686 char_u *name = iptr->isn_arg.string;
3687
3688 (void)script_autoload(name, FALSE);
3689 if (find_func(name, TRUE))
3690 {
3691 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
3692 goto theend;
3693 tv = STACK_TV_BOT(0);
3694 tv->v_lock = 0;
3695 ++ectx->ec_stack.ga_len;
3696 tv->v_type = VAR_FUNC;
3697 tv->vval.v_string = vim_strsave(name);
3698 }
3699 else
3700 {
3701 int res = load_namespace_var(ectx, ISN_LOADG, iptr);
3702
3703 if (res == NOTDONE)
3704 goto theend;
3705 if (res == FAIL)
3706 goto on_error;
3707 }
3708 }
3709 break;
3710
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003711 case ISN_UNLET:
3712 if (do_unlet(iptr->isn_arg.unlet.ul_name,
3713 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003714 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003715 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02003716 case ISN_UNLETENV:
LemonBoy77142312022-04-15 20:50:46 +01003717 vim_unsetenv_ext(iptr->isn_arg.unlet.ul_name);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02003718 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003719
Bram Moolenaaraacc9662021-08-13 19:40:51 +02003720 case ISN_LOCKUNLOCK:
3721 {
3722 typval_T *lval_root_save = lval_root;
3723 int res;
3724
3725 // Stack has the local variable, argument the whole :lock
3726 // or :unlock command, like ISN_EXEC.
3727 --ectx->ec_stack.ga_len;
3728 lval_root = STACK_TV_BOT(0);
3729 res = exec_command(iptr);
3730 clear_tv(lval_root);
3731 lval_root = lval_root_save;
3732 if (res == FAIL)
3733 goto on_error;
3734 }
3735 break;
3736
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02003737 case ISN_LOCKCONST:
3738 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
3739 break;
3740
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003741 // create a list from items on the stack; uses a single allocation
3742 // for the list header and the items
3743 case ISN_NEWLIST:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003744 if (exe_newlist(iptr->isn_arg.number, ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003745 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003746 break;
3747
3748 // create a dict from items on the stack
3749 case ISN_NEWDICT:
3750 {
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01003751 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003752
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01003753 SOURCING_LNUM = iptr->isn_lnum;
3754 res = exe_newdict(iptr->isn_arg.number, ectx);
3755 if (res == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003756 goto theend;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01003757 if (res == MAYBE)
3758 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003759 }
3760 break;
3761
LemonBoy372bcce2022-04-25 12:43:20 +01003762 case ISN_CONCAT:
3763 if (exe_concat(iptr->isn_arg.number, ectx) == FAIL)
3764 goto theend;
3765 break;
3766
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003767 // create a partial with NULL value
3768 case ISN_NEWPARTIAL:
3769 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
3770 goto theend;
3771 ++ectx->ec_stack.ga_len;
3772 tv = STACK_TV_BOT(-1);
3773 tv->v_type = VAR_PARTIAL;
3774 tv->v_lock = 0;
3775 tv->vval.v_partial = NULL;
3776 break;
3777
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003778 // call a :def function
3779 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02003780 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003781 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
3782 NULL,
3783 iptr->isn_arg.dfunc.cdf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003784 ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003785 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003786 break;
3787
3788 // call a builtin function
3789 case ISN_BCALL:
3790 SOURCING_LNUM = iptr->isn_lnum;
3791 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
3792 iptr->isn_arg.bfunc.cbf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003793 ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02003794 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003795 break;
3796
3797 // call a funcref or partial
3798 case ISN_PCALL:
3799 {
3800 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
3801 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003802 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003803
3804 SOURCING_LNUM = iptr->isn_lnum;
3805 if (pfunc->cpf_top)
3806 {
3807 // funcref is above the arguments
3808 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
3809 }
3810 else
3811 {
3812 // Get the funcref from the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02003813 --ectx->ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003814 partial_tv = *STACK_TV_BOT(0);
3815 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003816 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003817 r = call_partial(tv, pfunc->cpf_argcount, ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003818 if (tv == &partial_tv)
3819 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003820 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02003821 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003822 }
3823 break;
3824
Bram Moolenaarbd5da372020-03-31 23:13:10 +02003825 case ISN_PCALL_END:
3826 // PCALL finished, arguments have been consumed and replaced by
3827 // the return value. Now clear the funcref from the stack,
3828 // and move the return value in its place.
Bram Moolenaar4c137212021-04-19 16:48:48 +02003829 --ectx->ec_stack.ga_len;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02003830 clear_tv(STACK_TV_BOT(-1));
3831 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
3832 break;
3833
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003834 // call a user defined function or funcref/partial
3835 case ISN_UCALL:
3836 {
3837 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
3838
3839 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003840 if (call_eval_func(cufunc->cuf_name, cufunc->cuf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003841 ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02003842 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003843 }
3844 break;
3845
Bram Moolenaar1d84f762022-09-03 21:35:53 +01003846 // :defer func(arg)
3847 case ISN_DEFER:
Bram Moolenaar806a2732022-09-04 15:40:36 +01003848 if (defer_command(iptr->isn_arg.defer.defer_var_idx,
Bram Moolenaar1d84f762022-09-03 21:35:53 +01003849 iptr->isn_arg.defer.defer_argcount, ectx) == FAIL)
3850 goto on_error;
3851 break;
3852
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02003853 // return from a :def function call without a value
3854 case ISN_RETURN_VOID:
Bram Moolenaar35578162021-08-02 19:10:38 +02003855 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003856 goto theend;
Bram Moolenaar299f3032021-01-08 20:53:09 +01003857 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003858 ++ectx->ec_stack.ga_len;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02003859 tv->v_type = VAR_VOID;
Bram Moolenaar299f3032021-01-08 20:53:09 +01003860 tv->vval.v_number = 0;
3861 tv->v_lock = 0;
3862 // FALLTHROUGH
3863
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02003864 // return from a :def function call with what is on the stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003865 case ISN_RETURN:
3866 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003867 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003868 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01003869
3870 if (trystack->ga_len > 0)
3871 trycmd = ((trycmd_T *)trystack->ga_data)
3872 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003873 if (trycmd != NULL
Bram Moolenaar4c137212021-04-19 16:48:48 +02003874 && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003875 {
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01003876 // jump to ":finally" or ":endtry"
3877 if (trycmd->tcd_finally_idx != 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02003878 ectx->ec_iidx = trycmd->tcd_finally_idx;
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01003879 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02003880 ectx->ec_iidx = trycmd->tcd_endtry_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003881 trycmd->tcd_return = TRUE;
3882 }
3883 else
Bram Moolenaard032f342020-07-18 18:13:02 +02003884 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003885 }
3886 break;
3887
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02003888 // push a partial, a reference to a compiled function
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003889 case ISN_FUNCREF:
3890 {
Bram Moolenaarf112f302020-12-20 17:47:52 +01003891 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar38453522021-11-28 22:00:12 +00003892 ufunc_T *ufunc;
3893 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003894
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003895 if (pt == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003896 goto theend;
Bram Moolenaar35578162021-08-02 19:10:38 +02003897 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003898 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003899 vim_free(pt);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003900 goto theend;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003901 }
Bram Moolenaar38453522021-11-28 22:00:12 +00003902 if (funcref->fr_func_name == NULL)
3903 {
3904 dfunc_T *pt_dfunc = ((dfunc_T *)def_functions.ga_data)
3905 + funcref->fr_dfunc_idx;
3906
3907 ufunc = pt_dfunc->df_ufunc;
3908 }
3909 else
3910 {
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003911 ufunc = find_func(funcref->fr_func_name, FALSE);
Bram Moolenaar38453522021-11-28 22:00:12 +00003912 }
Bram Moolenaar56acd1f2022-02-18 13:24:52 +00003913 if (ufunc == NULL)
3914 {
3915 SOURCING_LNUM = iptr->isn_lnum;
3916 iemsg("ufunc unexpectedly NULL for FUNCREF");
3917 goto theend;
3918 }
Bram Moolenaar38453522021-11-28 22:00:12 +00003919 if (fill_partial_and_closure(pt, ufunc, ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003920 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003921 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003922 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003923 tv->vval.v_partial = pt;
3924 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003925 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003926 }
3927 break;
3928
Bram Moolenaar38ddf332020-07-31 22:05:04 +02003929 // Create a global function from a lambda.
3930 case ISN_NEWFUNC:
3931 {
3932 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
3933
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01003934 if (copy_func(newfunc->nf_lambda, newfunc->nf_global,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003935 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003936 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02003937 }
3938 break;
3939
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01003940 // List functions
3941 case ISN_DEF:
3942 if (iptr->isn_arg.string == NULL)
3943 list_functions(NULL);
3944 else
3945 {
Bram Moolenaar14336722022-01-08 16:02:59 +00003946 exarg_T ea;
3947 garray_T lines_to_free;
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01003948
3949 CLEAR_FIELD(ea);
3950 ea.cmd = ea.arg = iptr->isn_arg.string;
Bram Moolenaar14336722022-01-08 16:02:59 +00003951 ga_init2(&lines_to_free, sizeof(char_u *), 50);
3952 define_function(&ea, NULL, &lines_to_free);
3953 ga_clear_strings(&lines_to_free);
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01003954 }
3955 break;
3956
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003957 // jump if a condition is met
3958 case ISN_JUMP:
3959 {
3960 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003961 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003962 int jump = TRUE;
3963
3964 if (when != JUMP_ALWAYS)
3965 {
3966 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003967 if (when == JUMP_IF_COND_FALSE
Bram Moolenaar13106602020-10-04 16:06:05 +02003968 || when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003969 || when == JUMP_IF_COND_TRUE)
3970 {
3971 SOURCING_LNUM = iptr->isn_lnum;
3972 jump = tv_get_bool_chk(tv, &error);
3973 if (error)
3974 goto on_error;
3975 }
3976 else
3977 jump = tv2bool(tv);
Bram Moolenaarf6ced982022-04-28 12:00:49 +01003978 if (when == JUMP_IF_FALSE || when == JUMP_IF_COND_FALSE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003979 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01003980 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003981 {
3982 // drop the value from the stack
3983 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003984 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003985 }
3986 }
3987 if (jump)
Bram Moolenaar4c137212021-04-19 16:48:48 +02003988 ectx->ec_iidx = iptr->isn_arg.jump.jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003989 }
3990 break;
3991
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02003992 // Jump if an argument with a default value was already set and not
3993 // v:none.
3994 case ISN_JUMP_IF_ARG_SET:
3995 tv = STACK_TV_VAR(iptr->isn_arg.jumparg.jump_arg_off);
3996 if (tv->v_type != VAR_UNKNOWN
3997 && !(tv->v_type == VAR_SPECIAL
3998 && tv->vval.v_number == VVAL_NONE))
Bram Moolenaar4c137212021-04-19 16:48:48 +02003999 ectx->ec_iidx = iptr->isn_arg.jumparg.jump_where;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02004000 break;
4001
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004002 // top of a for loop
4003 case ISN_FOR:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00004004 if (execute_for(iptr, ectx) == FAIL)
4005 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004006 break;
4007
4008 // start of ":try" block
4009 case ISN_TRY:
4010 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01004011 trycmd_T *trycmd = NULL;
4012
Bram Moolenaar35578162021-08-02 19:10:38 +02004013 if (GA_GROW_FAILS(&ectx->ec_trystack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004014 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004015 trycmd = ((trycmd_T *)ectx->ec_trystack.ga_data)
4016 + ectx->ec_trystack.ga_len;
4017 ++ectx->ec_trystack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004018 ++trylevel;
Bram Moolenaar8d4be892021-02-13 18:33:02 +01004019 CLEAR_POINTER(trycmd);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004020 trycmd->tcd_frame_idx = ectx->ec_frame_idx;
4021 trycmd->tcd_stack_len = ectx->ec_stack.ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01004022 trycmd->tcd_catch_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00004023 iptr->isn_arg.tryref.try_ref->try_catch;
Bram Moolenaara91a7132021-03-25 21:12:15 +01004024 trycmd->tcd_finally_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00004025 iptr->isn_arg.tryref.try_ref->try_finally;
Bram Moolenaara91a7132021-03-25 21:12:15 +01004026 trycmd->tcd_endtry_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00004027 iptr->isn_arg.tryref.try_ref->try_endtry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004028 }
4029 break;
4030
4031 case ISN_PUSHEXC:
4032 if (current_exception == NULL)
4033 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004034 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004035 iemsg("Evaluating catch while current_exception is NULL");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004036 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004037 }
Bram Moolenaar35578162021-08-02 19:10:38 +02004038 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004039 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004040 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004041 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004042 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02004043 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004044 tv->vval.v_string = vim_strsave(
4045 (char_u *)current_exception->value);
4046 break;
4047
4048 case ISN_CATCH:
4049 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004050 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar06651632022-04-27 17:54:25 +01004051 trycmd_T *trycmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004052
Bram Moolenaar4c137212021-04-19 16:48:48 +02004053 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar06651632022-04-27 17:54:25 +01004054 trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004055 + trystack->ga_len - 1;
Bram Moolenaar06651632022-04-27 17:54:25 +01004056 trycmd->tcd_caught = TRUE;
4057 trycmd->tcd_did_throw = FALSE;
4058
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004059 did_emsg = got_int = did_throw = FALSE;
Bram Moolenaar1430cee2021-01-17 19:20:32 +01004060 force_abort = need_rethrow = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004061 catch_exception(current_exception);
4062 }
4063 break;
4064
Bram Moolenaarc150c092021-02-13 15:02:46 +01004065 case ISN_TRYCONT:
4066 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004067 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004068 trycont_T *trycont = &iptr->isn_arg.trycont;
4069 int i;
4070 trycmd_T *trycmd;
4071 int iidx = trycont->tct_where;
4072
4073 if (trystack->ga_len < trycont->tct_levels)
4074 {
4075 siemsg("TRYCONT: expected %d levels, found %d",
4076 trycont->tct_levels, trystack->ga_len);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004077 goto theend;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004078 }
4079 // Make :endtry jump to any outer try block and the last
4080 // :endtry inside the loop to the loop start.
4081 for (i = trycont->tct_levels; i > 0; --i)
4082 {
4083 trycmd = ((trycmd_T *)trystack->ga_data)
4084 + trystack->ga_len - i;
Bram Moolenaar2e34c342021-03-14 12:13:33 +01004085 // Add one to tcd_cont to be able to jump to
4086 // instruction with index zero.
4087 trycmd->tcd_cont = iidx + 1;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01004088 iidx = trycmd->tcd_finally_idx == 0
4089 ? trycmd->tcd_endtry_idx : trycmd->tcd_finally_idx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004090 }
4091 // jump to :finally or :endtry of current try statement
Bram Moolenaar4c137212021-04-19 16:48:48 +02004092 ectx->ec_iidx = iidx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004093 }
4094 break;
4095
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01004096 case ISN_FINALLY:
4097 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004098 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01004099 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
4100 + trystack->ga_len - 1;
4101
4102 // Reset the index to avoid a return statement jumps here
4103 // again.
4104 trycmd->tcd_finally_idx = 0;
4105 break;
4106 }
4107
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004108 // end of ":try" block
4109 case ISN_ENDTRY:
4110 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004111 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar06651632022-04-27 17:54:25 +01004112 trycmd_T *trycmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004113
Bram Moolenaar06651632022-04-27 17:54:25 +01004114 --trystack->ga_len;
4115 --trylevel;
4116 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len;
4117 if (trycmd->tcd_did_throw)
4118 did_throw = TRUE;
4119 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004120 {
Bram Moolenaar06651632022-04-27 17:54:25 +01004121 // discard the exception
4122 if (caught_stack == current_exception)
4123 caught_stack = caught_stack->caught;
4124 discard_current_exception();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004125 }
Bram Moolenaar06651632022-04-27 17:54:25 +01004126
4127 if (trycmd->tcd_return)
4128 goto func_return;
4129
4130 while (ectx->ec_stack.ga_len > trycmd->tcd_stack_len)
4131 {
4132 --ectx->ec_stack.ga_len;
4133 clear_tv(STACK_TV_BOT(0));
4134 }
4135 if (trycmd->tcd_cont != 0)
4136 // handling :continue: jump to outer try block or
4137 // start of the loop
4138 ectx->ec_iidx = trycmd->tcd_cont - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004139 }
4140 break;
4141
4142 case ISN_THROW:
Bram Moolenaar8f81b222021-01-14 21:47:06 +01004143 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004144 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02004145
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004146 if (trystack->ga_len == 0 && trylevel == 0 && emsg_silent)
4147 {
4148 // throwing an exception while using "silent!" causes
4149 // the function to abort but not display an error.
4150 tv = STACK_TV_BOT(-1);
4151 clear_tv(tv);
4152 tv->v_type = VAR_NUMBER;
4153 tv->vval.v_number = 0;
4154 goto done;
4155 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004156 --ectx->ec_stack.ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004157 tv = STACK_TV_BOT(0);
4158 if (tv->vval.v_string == NULL
4159 || *skipwhite(tv->vval.v_string) == NUL)
4160 {
4161 vim_free(tv->vval.v_string);
4162 SOURCING_LNUM = iptr->isn_lnum;
4163 emsg(_(e_throw_with_empty_string));
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004164 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004165 }
4166
4167 // Inside a "catch" we need to first discard the caught
4168 // exception.
4169 if (trystack->ga_len > 0)
4170 {
4171 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
4172 + trystack->ga_len - 1;
4173 if (trycmd->tcd_caught && current_exception != NULL)
4174 {
4175 // discard the exception
4176 if (caught_stack == current_exception)
4177 caught_stack = caught_stack->caught;
4178 discard_current_exception();
4179 trycmd->tcd_caught = FALSE;
4180 }
4181 }
4182
Bram Moolenaar90a57162022-02-12 14:23:17 +00004183 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004184 if (throw_exception(tv->vval.v_string, ET_USER, NULL)
4185 == FAIL)
4186 {
4187 vim_free(tv->vval.v_string);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004188 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004189 }
4190 did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004191 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004192 break;
4193
4194 // compare with special values
4195 case ISN_COMPAREBOOL:
4196 case ISN_COMPARESPECIAL:
4197 {
4198 typval_T *tv1 = STACK_TV_BOT(-2);
4199 typval_T *tv2 = STACK_TV_BOT(-1);
4200 varnumber_T arg1 = tv1->vval.v_number;
4201 varnumber_T arg2 = tv2->vval.v_number;
4202 int res;
4203
Bram Moolenaar06651632022-04-27 17:54:25 +01004204 if (iptr->isn_arg.op.op_type == EXPR_EQUAL)
4205 res = arg1 == arg2;
4206 else
4207 res = arg1 != arg2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004208
Bram Moolenaar4c137212021-04-19 16:48:48 +02004209 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004210 tv1->v_type = VAR_BOOL;
4211 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4212 }
4213 break;
4214
Bram Moolenaar7a222242022-03-01 19:23:24 +00004215 case ISN_COMPARENULL:
4216 {
4217 typval_T *tv1 = STACK_TV_BOT(-2);
4218 typval_T *tv2 = STACK_TV_BOT(-1);
4219 int res;
4220
4221 res = typval_compare_null(tv1, tv2);
4222 if (res == MAYBE)
4223 goto on_error;
4224 if (iptr->isn_arg.op.op_type == EXPR_NEQUAL)
4225 res = !res;
4226 clear_tv(tv1);
4227 clear_tv(tv2);
4228 --ectx->ec_stack.ga_len;
4229 tv1->v_type = VAR_BOOL;
4230 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4231 }
4232 break;
4233
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004234 // Operation with two number arguments
4235 case ISN_OPNR:
4236 case ISN_COMPARENR:
4237 {
4238 typval_T *tv1 = STACK_TV_BOT(-2);
4239 typval_T *tv2 = STACK_TV_BOT(-1);
4240 varnumber_T arg1 = tv1->vval.v_number;
4241 varnumber_T arg2 = tv2->vval.v_number;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004242 varnumber_T res = 0;
4243 int div_zero = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004244
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004245 if (iptr->isn_arg.op.op_type == EXPR_LSHIFT
4246 || iptr->isn_arg.op.op_type == EXPR_RSHIFT)
4247 {
4248 if (arg2 < 0)
4249 {
4250 SOURCING_LNUM = iptr->isn_lnum;
4251 emsg(_(e_bitshift_ops_must_be_postive));
4252 goto on_error;
4253 }
4254 }
4255
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004256 switch (iptr->isn_arg.op.op_type)
4257 {
4258 case EXPR_MULT: res = arg1 * arg2; break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004259 case EXPR_DIV: if (arg2 == 0)
4260 div_zero = TRUE;
4261 else
4262 res = arg1 / arg2;
4263 break;
4264 case EXPR_REM: if (arg2 == 0)
4265 div_zero = TRUE;
4266 else
4267 res = arg1 % arg2;
4268 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004269 case EXPR_SUB: res = arg1 - arg2; break;
4270 case EXPR_ADD: res = arg1 + arg2; break;
4271
4272 case EXPR_EQUAL: res = arg1 == arg2; break;
4273 case EXPR_NEQUAL: res = arg1 != arg2; break;
4274 case EXPR_GREATER: res = arg1 > arg2; break;
4275 case EXPR_GEQUAL: res = arg1 >= arg2; break;
4276 case EXPR_SMALLER: res = arg1 < arg2; break;
4277 case EXPR_SEQUAL: res = arg1 <= arg2; break;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004278 case EXPR_LSHIFT: if (arg2 > MAX_LSHIFT_BITS)
4279 res = 0;
4280 else
Bram Moolenaar68e64d22022-05-22 22:07:52 +01004281 res = (uvarnumber_T)arg1 << arg2;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004282 break;
4283 case EXPR_RSHIFT: if (arg2 > MAX_LSHIFT_BITS)
4284 res = 0;
4285 else
Bram Moolenaar338bf582022-05-22 20:16:32 +01004286 res = (uvarnumber_T)arg1 >> arg2;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004287 break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004288 default: break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004289 }
4290
Bram Moolenaar4c137212021-04-19 16:48:48 +02004291 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004292 if (iptr->isn_type == ISN_COMPARENR)
4293 {
4294 tv1->v_type = VAR_BOOL;
4295 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4296 }
4297 else
4298 tv1->vval.v_number = res;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004299 if (div_zero)
4300 {
4301 SOURCING_LNUM = iptr->isn_lnum;
4302 emsg(_(e_divide_by_zero));
4303 goto on_error;
4304 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004305 }
4306 break;
4307
4308 // Computation with two float arguments
4309 case ISN_OPFLOAT:
4310 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01004311#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004312 {
4313 typval_T *tv1 = STACK_TV_BOT(-2);
4314 typval_T *tv2 = STACK_TV_BOT(-1);
4315 float_T arg1 = tv1->vval.v_float;
4316 float_T arg2 = tv2->vval.v_float;
4317 float_T res = 0;
4318 int cmp = FALSE;
4319
4320 switch (iptr->isn_arg.op.op_type)
4321 {
4322 case EXPR_MULT: res = arg1 * arg2; break;
4323 case EXPR_DIV: res = arg1 / arg2; break;
4324 case EXPR_SUB: res = arg1 - arg2; break;
4325 case EXPR_ADD: res = arg1 + arg2; break;
4326
4327 case EXPR_EQUAL: cmp = arg1 == arg2; break;
4328 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
4329 case EXPR_GREATER: cmp = arg1 > arg2; break;
4330 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
4331 case EXPR_SMALLER: cmp = arg1 < arg2; break;
4332 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
4333 default: cmp = 0; break;
4334 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004335 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004336 if (iptr->isn_type == ISN_COMPAREFLOAT)
4337 {
4338 tv1->v_type = VAR_BOOL;
4339 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
4340 }
4341 else
4342 tv1->vval.v_float = res;
4343 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01004344#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004345 break;
4346
4347 case ISN_COMPARELIST:
Bram Moolenaar265f8112021-12-19 12:33:05 +00004348 case ISN_COMPAREDICT:
4349 case ISN_COMPAREFUNC:
4350 case ISN_COMPARESTRING:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004351 case ISN_COMPAREBLOB:
4352 {
4353 typval_T *tv1 = STACK_TV_BOT(-2);
4354 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar265f8112021-12-19 12:33:05 +00004355 exprtype_T exprtype = iptr->isn_arg.op.op_type;
4356 int ic = iptr->isn_arg.op.op_ic;
4357 int res = FALSE;
4358 int status = OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004359
Bram Moolenaar265f8112021-12-19 12:33:05 +00004360 SOURCING_LNUM = iptr->isn_lnum;
4361 if (iptr->isn_type == ISN_COMPARELIST)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004362 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00004363 status = typval_compare_list(tv1, tv2,
4364 exprtype, ic, &res);
4365 }
4366 else if (iptr->isn_type == ISN_COMPAREDICT)
4367 {
4368 status = typval_compare_dict(tv1, tv2,
4369 exprtype, ic, &res);
4370 }
4371 else if (iptr->isn_type == ISN_COMPAREFUNC)
4372 {
4373 status = typval_compare_func(tv1, tv2,
4374 exprtype, ic, &res);
4375 }
4376 else if (iptr->isn_type == ISN_COMPARESTRING)
4377 {
4378 status = typval_compare_string(tv1, tv2,
4379 exprtype, ic, &res);
4380 }
4381 else
4382 {
4383 status = typval_compare_blob(tv1, tv2, exprtype, &res);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004384 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004385 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004386 clear_tv(tv1);
4387 clear_tv(tv2);
4388 tv1->v_type = VAR_BOOL;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004389 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4390 if (status == FAIL)
4391 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004392 }
4393 break;
4394
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004395 case ISN_COMPAREANY:
4396 {
4397 typval_T *tv1 = STACK_TV_BOT(-2);
4398 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar657137c2021-01-09 15:45:23 +01004399 exprtype_T exprtype = iptr->isn_arg.op.op_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004400 int ic = iptr->isn_arg.op.op_ic;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004401 int status;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004402
Bram Moolenaareb26f432020-09-14 16:50:05 +02004403 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004404 status = typval_compare(tv1, tv2, exprtype, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004405 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004406 --ectx->ec_stack.ga_len;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004407 if (status == FAIL)
4408 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004409 }
4410 break;
4411
4412 case ISN_ADDLIST:
4413 case ISN_ADDBLOB:
4414 {
4415 typval_T *tv1 = STACK_TV_BOT(-2);
4416 typval_T *tv2 = STACK_TV_BOT(-1);
4417
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004418 // add two lists or blobs
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004419 if (iptr->isn_type == ISN_ADDLIST)
Bram Moolenaar07802042021-09-09 23:01:14 +02004420 {
4421 if (iptr->isn_arg.op.op_type == EXPR_APPEND
4422 && tv1->vval.v_list != NULL)
4423 list_extend(tv1->vval.v_list, tv2->vval.v_list,
4424 NULL);
4425 else
4426 eval_addlist(tv1, tv2);
4427 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004428 else
4429 eval_addblob(tv1, tv2);
4430 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004431 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004432 }
4433 break;
4434
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004435 case ISN_LISTAPPEND:
4436 {
4437 typval_T *tv1 = STACK_TV_BOT(-2);
4438 typval_T *tv2 = STACK_TV_BOT(-1);
4439 list_T *l = tv1->vval.v_list;
4440
4441 // add an item to a list
Bram Moolenaar1f4a3452022-01-01 18:29:21 +00004442 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004443 if (l == NULL)
4444 {
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004445 emsg(_(e_cannot_add_to_null_list));
4446 goto on_error;
4447 }
Bram Moolenaar1f4a3452022-01-01 18:29:21 +00004448 if (value_check_lock(l->lv_lock, NULL, FALSE))
4449 goto on_error;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004450 if (list_append_tv(l, tv2) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004451 goto theend;
Bram Moolenaar955347c2020-10-19 23:01:46 +02004452 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004453 --ectx->ec_stack.ga_len;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004454 }
4455 break;
4456
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02004457 case ISN_BLOBAPPEND:
4458 {
4459 typval_T *tv1 = STACK_TV_BOT(-2);
4460 typval_T *tv2 = STACK_TV_BOT(-1);
4461 blob_T *b = tv1->vval.v_blob;
4462 int error = FALSE;
4463 varnumber_T n;
4464
4465 // add a number to a blob
4466 if (b == NULL)
4467 {
4468 SOURCING_LNUM = iptr->isn_lnum;
4469 emsg(_(e_cannot_add_to_null_blob));
4470 goto on_error;
4471 }
4472 n = tv_get_number_chk(tv2, &error);
4473 if (error)
4474 goto on_error;
4475 ga_append(&b->bv_ga, (int)n);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004476 --ectx->ec_stack.ga_len;
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02004477 }
4478 break;
4479
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004480 // Computation with two arguments of unknown type
4481 case ISN_OPANY:
4482 {
4483 typval_T *tv1 = STACK_TV_BOT(-2);
4484 typval_T *tv2 = STACK_TV_BOT(-1);
4485 varnumber_T n1, n2;
4486#ifdef FEAT_FLOAT
4487 float_T f1 = 0, f2 = 0;
4488#endif
4489 int error = FALSE;
4490
4491 if (iptr->isn_arg.op.op_type == EXPR_ADD)
4492 {
4493 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
4494 {
4495 eval_addlist(tv1, tv2);
4496 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004497 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004498 break;
4499 }
4500 else if (tv1->v_type == VAR_BLOB
4501 && tv2->v_type == VAR_BLOB)
4502 {
4503 eval_addblob(tv1, tv2);
4504 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004505 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004506 break;
4507 }
4508 }
4509#ifdef FEAT_FLOAT
4510 if (tv1->v_type == VAR_FLOAT)
4511 {
4512 f1 = tv1->vval.v_float;
4513 n1 = 0;
4514 }
4515 else
4516#endif
4517 {
Bram Moolenaarf665e972020-12-05 19:17:16 +01004518 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004519 n1 = tv_get_number_chk(tv1, &error);
4520 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02004521 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004522#ifdef FEAT_FLOAT
4523 if (tv2->v_type == VAR_FLOAT)
4524 f1 = n1;
4525#endif
4526 }
4527#ifdef FEAT_FLOAT
4528 if (tv2->v_type == VAR_FLOAT)
4529 {
4530 f2 = tv2->vval.v_float;
4531 n2 = 0;
4532 }
4533 else
4534#endif
4535 {
4536 n2 = tv_get_number_chk(tv2, &error);
4537 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02004538 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004539#ifdef FEAT_FLOAT
4540 if (tv1->v_type == VAR_FLOAT)
4541 f2 = n2;
4542#endif
4543 }
4544#ifdef FEAT_FLOAT
4545 // if there is a float on either side the result is a float
4546 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
4547 {
4548 switch (iptr->isn_arg.op.op_type)
4549 {
4550 case EXPR_MULT: f1 = f1 * f2; break;
4551 case EXPR_DIV: f1 = f1 / f2; break;
4552 case EXPR_SUB: f1 = f1 - f2; break;
4553 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004554 default: SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004555 emsg(_(e_cannot_use_percent_with_float));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004556 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004557 }
4558 clear_tv(tv1);
4559 clear_tv(tv2);
4560 tv1->v_type = VAR_FLOAT;
4561 tv1->vval.v_float = f1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004562 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004563 }
4564 else
4565#endif
4566 {
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004567 int failed = FALSE;
4568
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004569 switch (iptr->isn_arg.op.op_type)
4570 {
4571 case EXPR_MULT: n1 = n1 * n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004572 case EXPR_DIV: n1 = num_divide(n1, n2, &failed);
4573 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01004574 goto on_error;
4575 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004576 case EXPR_SUB: n1 = n1 - n2; break;
4577 case EXPR_ADD: n1 = n1 + n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004578 default: n1 = num_modulus(n1, n2, &failed);
4579 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01004580 goto on_error;
4581 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004582 }
4583 clear_tv(tv1);
4584 clear_tv(tv2);
4585 tv1->v_type = VAR_NUMBER;
4586 tv1->vval.v_number = n1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004587 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004588 }
4589 }
4590 break;
4591
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004592 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004593 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004594 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004595 int is_slice = iptr->isn_type == ISN_STRSLICE;
4596 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004597 char_u *res;
4598
4599 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004600 // string slice: string is at stack-3, first index at
4601 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004602 if (is_slice)
4603 {
4604 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004605 n1 = tv->vval.v_number;
4606 }
4607
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004608 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004609 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004610
Bram Moolenaar4c137212021-04-19 16:48:48 +02004611 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004612 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004613 if (is_slice)
4614 // Slice: Select the characters from the string
Bram Moolenaar6601b622021-01-13 21:47:15 +01004615 res = string_slice(tv->vval.v_string, n1, n2, FALSE);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004616 else
4617 // Index: The resulting variable is a string of a
Bram Moolenaar0289a092021-03-14 18:40:19 +01004618 // single character (including composing characters).
4619 // If the index is too big or negative the result is
4620 // empty.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004621 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004622 vim_free(tv->vval.v_string);
4623 tv->vval.v_string = res;
4624 }
4625 break;
4626
4627 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02004628 case ISN_LISTSLICE:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004629 case ISN_BLOBINDEX:
4630 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004631 {
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004632 int is_slice = iptr->isn_type == ISN_LISTSLICE
4633 || iptr->isn_type == ISN_BLOBSLICE;
4634 int is_blob = iptr->isn_type == ISN_BLOBINDEX
4635 || iptr->isn_type == ISN_BLOBSLICE;
Bram Moolenaared591872020-08-15 22:14:53 +02004636 varnumber_T n1, n2;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004637 typval_T *val_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004638
4639 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02004640 // list slice: list is at stack-3, indexes at stack-2 and
4641 // stack-1
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004642 // Same for blob.
4643 val_tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004644
4645 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02004646 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004647 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02004648
4649 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004650 {
Bram Moolenaared591872020-08-15 22:14:53 +02004651 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02004652 n1 = tv->vval.v_number;
4653 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004654 }
Bram Moolenaared591872020-08-15 22:14:53 +02004655
Bram Moolenaar4c137212021-04-19 16:48:48 +02004656 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02004657 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02004658 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004659 if (is_blob)
4660 {
4661 if (blob_slice_or_index(val_tv->vval.v_blob, is_slice,
4662 n1, n2, FALSE, tv) == FAIL)
4663 goto on_error;
4664 }
4665 else
4666 {
4667 if (list_slice_or_index(val_tv->vval.v_list, is_slice,
4668 n1, n2, FALSE, tv, TRUE) == FAIL)
4669 goto on_error;
4670 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004671 }
4672 break;
4673
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004674 case ISN_ANYINDEX:
4675 case ISN_ANYSLICE:
4676 {
4677 int is_slice = iptr->isn_type == ISN_ANYSLICE;
4678 typval_T *var1, *var2;
4679 int res;
4680
4681 // index: composite is at stack-2, index at stack-1
4682 // slice: composite is at stack-3, indexes at stack-2 and
4683 // stack-1
4684 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02004685 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004686 if (check_can_index(tv, TRUE, TRUE) == FAIL)
4687 goto on_error;
4688 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
4689 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
Bram Moolenaar6601b622021-01-13 21:47:15 +01004690 res = eval_index_inner(tv, is_slice, var1, var2,
4691 FALSE, NULL, -1, TRUE);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004692 clear_tv(var1);
4693 if (is_slice)
4694 clear_tv(var2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004695 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004696 if (res == FAIL)
4697 goto on_error;
4698 }
4699 break;
4700
Bram Moolenaar9af78762020-06-16 11:34:42 +02004701 case ISN_SLICE:
4702 {
4703 list_T *list;
4704 int count = iptr->isn_arg.number;
4705
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02004706 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02004707 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02004708 list = tv->vval.v_list;
4709
4710 // no error for short list, expect it to be checked earlier
4711 if (list != NULL && list->lv_len >= count)
4712 {
4713 list_T *newlist = list_slice(list,
4714 count, list->lv_len - 1);
4715
4716 if (newlist != NULL)
4717 {
4718 list_unref(list);
4719 tv->vval.v_list = newlist;
4720 ++newlist->lv_refcount;
4721 }
4722 }
4723 }
4724 break;
4725
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004726 case ISN_GETITEM:
4727 {
4728 listitem_T *li;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02004729 getitem_T *gi = &iptr->isn_arg.getitem;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004730
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02004731 // Get list item: list is at stack-1, push item.
4732 // List type and length is checked for when compiling.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02004733 tv = STACK_TV_BOT(-1 - gi->gi_with_op);
4734 li = list_find(tv->vval.v_list, gi->gi_index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004735
Bram Moolenaar35578162021-08-02 19:10:38 +02004736 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004737 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004738 ++ectx->ec_stack.ga_len;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004739 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004740
4741 // Useful when used in unpack assignment. Reset at
4742 // ISN_DROP.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02004743 ectx->ec_where.wt_index = gi->gi_index + 1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004744 ectx->ec_where.wt_variable = TRUE;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004745 }
4746 break;
4747
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004748 case ISN_MEMBER:
4749 {
4750 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004751 char_u *key;
4752 dictitem_T *di;
4753
4754 // dict member: dict is at stack-2, key at stack-1
4755 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02004756 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004757 dict = tv->vval.v_dict;
4758
4759 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02004760 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004761 key = tv->vval.v_string;
Bram Moolenaar086fc9a2020-10-30 18:33:02 +01004762 if (key == NULL)
4763 key = (char_u *)"";
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02004764
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004765 if ((di = dict_find(dict, key, -1)) == NULL)
4766 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004767 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004768 semsg(_(e_key_not_present_in_dictionary), key);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004769
4770 // If :silent! is used we will continue, make sure the
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004771 // stack contents makes sense and the dict stack is
4772 // updated.
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004773 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004774 --ectx->ec_stack.ga_len;
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004775 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004776 (void) dict_stack_save(tv);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004777 tv->v_type = VAR_NUMBER;
4778 tv->vval.v_number = 0;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004779 goto on_fatal_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004780 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004781 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004782 --ectx->ec_stack.ga_len;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004783 // Put the dict used on the dict stack, it might be used by
4784 // a dict function later.
Bram Moolenaar50788ef2020-07-05 16:51:26 +02004785 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004786 if (dict_stack_save(tv) == FAIL)
4787 goto on_fatal_error;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02004788 copy_tv(&di->di_tv, tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004789 }
4790 break;
4791
4792 // dict member with string key
4793 case ISN_STRINGMEMBER:
4794 {
4795 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004796 dictitem_T *di;
4797
4798 tv = STACK_TV_BOT(-1);
4799 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
4800 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004801 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004802 emsg(_(e_dictionary_required));
Bram Moolenaard032f342020-07-18 18:13:02 +02004803 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004804 }
4805 dict = tv->vval.v_dict;
4806
4807 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
4808 == NULL)
4809 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004810 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar381692b2022-02-02 20:01:27 +00004811 semsg(_(e_key_not_present_in_dictionary),
4812 iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02004813 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004814 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004815 // Put the dict used on the dict stack, it might be used by
4816 // a dict function later.
4817 if (dict_stack_save(tv) == FAIL)
4818 goto on_fatal_error;
4819
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004820 copy_tv(&di->di_tv, tv);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004821 }
4822 break;
4823
4824 case ISN_CLEARDICT:
4825 dict_stack_drop();
4826 break;
4827
4828 case ISN_USEDICT:
4829 {
4830 typval_T *dict_tv = dict_stack_get_tv();
4831
4832 // Turn "dict.Func" into a partial for "Func" bound to
4833 // "dict". Don't do this when "Func" is already a partial
4834 // that was bound explicitly (pt_auto is FALSE).
4835 tv = STACK_TV_BOT(-1);
4836 if (dict_tv != NULL
4837 && dict_tv->v_type == VAR_DICT
4838 && dict_tv->vval.v_dict != NULL
4839 && (tv->v_type == VAR_FUNC
4840 || (tv->v_type == VAR_PARTIAL
4841 && (tv->vval.v_partial->pt_auto
4842 || tv->vval.v_partial->pt_dict == NULL))))
4843 dict_tv->vval.v_dict =
4844 make_partial(dict_tv->vval.v_dict, tv);
4845 dict_stack_drop();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004846 }
4847 break;
4848
4849 case ISN_NEGATENR:
4850 tv = STACK_TV_BOT(-1);
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00004851 // CHECKTYPE should have checked the variable type
Bram Moolenaarc58164c2020-03-29 18:40:30 +02004852#ifdef FEAT_FLOAT
4853 if (tv->v_type == VAR_FLOAT)
4854 tv->vval.v_float = -tv->vval.v_float;
4855 else
4856#endif
4857 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004858 break;
4859
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004860 case ISN_CHECKTYPE:
4861 {
4862 checktype_T *ct = &iptr->isn_arg.type;
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01004863 int save_wt_variable = ectx->ec_where.wt_variable;
Bram Moolenaarb1040dc2022-05-18 11:00:48 +01004864 int r;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004865
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01004866 tv = STACK_TV_BOT((int)ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02004867 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004868 if (!ectx->ec_where.wt_variable)
4869 ectx->ec_where.wt_index = ct->ct_arg_idx;
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01004870 ectx->ec_where.wt_variable = ct->ct_is_var;
Bram Moolenaarb1040dc2022-05-18 11:00:48 +01004871 r = check_typval_type(ct->ct_type, tv, ectx->ec_where);
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01004872 ectx->ec_where.wt_variable = save_wt_variable;
Bram Moolenaarb1040dc2022-05-18 11:00:48 +01004873 if (r == FAIL)
4874 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004875 if (!ectx->ec_where.wt_variable)
4876 ectx->ec_where.wt_index = 0;
Bram Moolenaar5e654232020-09-16 15:22:00 +02004877
4878 // number 0 is FALSE, number 1 is TRUE
4879 if (tv->v_type == VAR_NUMBER
4880 && ct->ct_type->tt_type == VAR_BOOL
4881 && (tv->vval.v_number == 0
4882 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004883 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02004884 tv->v_type = VAR_BOOL;
4885 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02004886 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004887 }
4888 }
4889 break;
4890
Bram Moolenaar9af78762020-06-16 11:34:42 +02004891 case ISN_CHECKLEN:
4892 {
4893 int min_len = iptr->isn_arg.checklen.cl_min_len;
4894 list_T *list = NULL;
4895
4896 tv = STACK_TV_BOT(-1);
4897 if (tv->v_type == VAR_LIST)
4898 list = tv->vval.v_list;
4899 if (list == NULL || list->lv_len < min_len
4900 || (list->lv_len > min_len
4901 && !iptr->isn_arg.checklen.cl_more_OK))
4902 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004903 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004904 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02004905 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004906 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02004907 }
4908 }
4909 break;
4910
Bram Moolenaaraa210a32021-01-02 15:41:03 +01004911 case ISN_SETTYPE:
Bram Moolenaar381692b2022-02-02 20:01:27 +00004912 set_tv_type(STACK_TV_BOT(-1), iptr->isn_arg.type.ct_type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01004913 break;
4914
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004915 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004916 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004917 {
4918 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004919 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004920
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004921 if (iptr->isn_type == ISN_2BOOL)
4922 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004923 tv = STACK_TV_BOT(iptr->isn_arg.tobool.offset);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004924 n = tv2bool(tv);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004925 if (iptr->isn_arg.tobool.invert)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004926 n = !n;
4927 }
4928 else
4929 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004930 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004931 SOURCING_LNUM = iptr->isn_lnum;
4932 n = tv_get_bool_chk(tv, &error);
4933 if (error)
4934 goto on_error;
4935 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004936 clear_tv(tv);
4937 tv->v_type = VAR_BOOL;
4938 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
4939 }
4940 break;
4941
4942 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02004943 case ISN_2STRING_ANY:
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01004944 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004945 if (do_2string(STACK_TV_BOT(iptr->isn_arg.tostring.offset),
4946 iptr->isn_type == ISN_2STRING_ANY,
4947 iptr->isn_arg.tostring.tolerant) == FAIL)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01004948 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004949 break;
4950
Bram Moolenaar08597872020-12-10 19:43:40 +01004951 case ISN_RANGE:
4952 {
4953 exarg_T ea;
4954 char *errormsg;
4955
Bram Moolenaarece0b872021-01-08 20:40:45 +01004956 ea.line2 = 0;
Bram Moolenaard1510ee2021-01-04 16:15:58 +01004957 ea.addr_count = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01004958 ea.addr_type = ADDR_LINES;
4959 ea.cmd = iptr->isn_arg.string;
Bram Moolenaarece0b872021-01-08 20:40:45 +01004960 ea.skip = FALSE;
Bram Moolenaar08597872020-12-10 19:43:40 +01004961 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaarece0b872021-01-08 20:40:45 +01004962 goto on_error;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004963
Bram Moolenaar35578162021-08-02 19:10:38 +02004964 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004965 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004966 ++ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004967 tv = STACK_TV_BOT(-1);
4968 tv->v_type = VAR_NUMBER;
4969 tv->v_lock = 0;
Bram Moolenaar06651632022-04-27 17:54:25 +01004970 tv->vval.v_number = ea.line2;
Bram Moolenaar08597872020-12-10 19:43:40 +01004971 }
4972 break;
4973
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004974 case ISN_PUT:
4975 {
4976 int regname = iptr->isn_arg.put.put_regname;
4977 linenr_T lnum = iptr->isn_arg.put.put_lnum;
4978 char_u *expr = NULL;
4979 int dir = FORWARD;
4980
Bram Moolenaar08597872020-12-10 19:43:40 +01004981 if (lnum < -2)
4982 {
4983 // line number was put on the stack by ISN_RANGE
4984 tv = STACK_TV_BOT(-1);
4985 curwin->w_cursor.lnum = tv->vval.v_number;
4986 if (lnum == LNUM_VARIABLE_RANGE_ABOVE)
4987 dir = BACKWARD;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004988 --ectx->ec_stack.ga_len;
Bram Moolenaar08597872020-12-10 19:43:40 +01004989 }
4990 else if (lnum == -2)
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004991 // :put! above cursor
4992 dir = BACKWARD;
4993 else if (lnum >= 0)
Bram Moolenaar4e713ba2022-02-07 15:31:37 +00004994 {
4995 curwin->w_cursor.lnum = lnum;
4996 if (lnum == 0)
4997 // check_cursor() below will move to line 1
4998 dir = BACKWARD;
4999 }
Bram Moolenaara28639e2021-01-19 22:48:09 +01005000
5001 if (regname == '=')
5002 {
5003 tv = STACK_TV_BOT(-1);
5004 if (tv->v_type == VAR_STRING)
5005 expr = tv->vval.v_string;
5006 else
5007 {
5008 expr = typval2string(tv, TRUE); // allocates value
5009 clear_tv(tv);
5010 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005011 --ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01005012 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02005013 check_cursor();
5014 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
5015 vim_free(expr);
5016 }
5017 break;
5018
Bram Moolenaar02194d22020-10-24 23:08:38 +02005019 case ISN_CMDMOD:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005020 ectx->ec_funclocal.floc_save_cmdmod = cmdmod;
5021 ectx->ec_funclocal.floc_restore_cmdmod = TRUE;
5022 ectx->ec_funclocal.floc_restore_cmdmod_stacklen =
5023 ectx->ec_stack.ga_len;
Bram Moolenaar02194d22020-10-24 23:08:38 +02005024 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
5025 apply_cmdmod(&cmdmod);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02005026 break;
5027
Bram Moolenaar02194d22020-10-24 23:08:38 +02005028 case ISN_CMDMOD_REV:
5029 // filter regprog is owned by the instruction, don't free it
5030 cmdmod.cmod_filter_regmatch.regprog = NULL;
5031 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005032 cmdmod = ectx->ec_funclocal.floc_save_cmdmod;
5033 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02005034 break;
5035
Bram Moolenaar792f7862020-11-23 08:31:18 +01005036 case ISN_UNPACK:
5037 {
5038 int count = iptr->isn_arg.unpack.unp_count;
5039 int semicolon = iptr->isn_arg.unpack.unp_semicolon;
5040 list_T *l;
5041 listitem_T *li;
5042 int i;
5043
5044 // Check there is a valid list to unpack.
5045 tv = STACK_TV_BOT(-1);
5046 if (tv->v_type != VAR_LIST)
5047 {
5048 SOURCING_LNUM = iptr->isn_lnum;
5049 emsg(_(e_for_argument_must_be_sequence_of_lists));
5050 goto on_error;
5051 }
5052 l = tv->vval.v_list;
5053 if (l == NULL
5054 || l->lv_len < (semicolon ? count - 1 : count))
5055 {
5056 SOURCING_LNUM = iptr->isn_lnum;
5057 emsg(_(e_list_value_does_not_have_enough_items));
5058 goto on_error;
5059 }
5060 else if (!semicolon && l->lv_len > count)
5061 {
5062 SOURCING_LNUM = iptr->isn_lnum;
5063 emsg(_(e_list_value_has_more_items_than_targets));
5064 goto on_error;
5065 }
5066
5067 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar35578162021-08-02 19:10:38 +02005068 if (GA_GROW_FAILS(&ectx->ec_stack, count - 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005069 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005070 ectx->ec_stack.ga_len += count - 1;
Bram Moolenaar792f7862020-11-23 08:31:18 +01005071
5072 // Variable after semicolon gets a list with the remaining
5073 // items.
5074 if (semicolon)
5075 {
5076 list_T *rem_list =
5077 list_alloc_with_items(l->lv_len - count + 1);
5078
5079 if (rem_list == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005080 goto theend;
Bram Moolenaar792f7862020-11-23 08:31:18 +01005081 tv = STACK_TV_BOT(-count);
5082 tv->vval.v_list = rem_list;
5083 ++rem_list->lv_refcount;
5084 tv->v_lock = 0;
5085 li = l->lv_first;
5086 for (i = 0; i < count - 1; ++i)
5087 li = li->li_next;
5088 for (i = 0; li != NULL; ++i)
5089 {
Bram Moolenaar61efa162022-03-18 13:10:48 +00005090 typval_T tvcopy;
5091
5092 copy_tv(&li->li_tv, &tvcopy);
5093 list_set_item(rem_list, i, &tvcopy);
Bram Moolenaar792f7862020-11-23 08:31:18 +01005094 li = li->li_next;
5095 }
5096 --count;
5097 }
5098
5099 // Produce the values in reverse order, first item last.
5100 li = l->lv_first;
5101 for (i = 0; i < count; ++i)
5102 {
5103 tv = STACK_TV_BOT(-i - 1);
5104 copy_tv(&li->li_tv, tv);
5105 li = li->li_next;
5106 }
5107
5108 list_unref(l);
5109 }
5110 break;
5111
Bram Moolenaarb2049902021-01-24 12:53:53 +01005112 case ISN_PROF_START:
5113 case ISN_PROF_END:
5114 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01005115#ifdef FEAT_PROFILE
Bram Moolenaarca16c602022-09-06 18:57:08 +01005116 funccall_T cookie;
5117 ufunc_T *cur_ufunc =
Bram Moolenaarb2049902021-01-24 12:53:53 +01005118 (((dfunc_T *)def_functions.ga_data)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02005119 + ectx->ec_dfunc_idx)->df_ufunc;
Bram Moolenaarb2049902021-01-24 12:53:53 +01005120
Bram Moolenaarca16c602022-09-06 18:57:08 +01005121 cookie.fc_func = cur_ufunc;
Bram Moolenaarb2049902021-01-24 12:53:53 +01005122 if (iptr->isn_type == ISN_PROF_START)
5123 {
5124 func_line_start(&cookie, iptr->isn_lnum);
5125 // if we get here the instruction is executed
5126 func_line_exec(&cookie);
5127 }
5128 else
5129 func_line_end(&cookie);
Bram Moolenaarf002a412021-01-24 13:34:18 +01005130#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01005131 }
5132 break;
5133
Bram Moolenaare99d4222021-06-13 14:01:26 +02005134 case ISN_DEBUG:
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02005135 handle_debug(iptr, ectx);
Bram Moolenaare99d4222021-06-13 14:01:26 +02005136 break;
5137
Bram Moolenaar389df252020-07-09 21:20:47 +02005138 case ISN_SHUFFLE:
5139 {
Bram Moolenaar792f7862020-11-23 08:31:18 +01005140 typval_T tmp_tv;
5141 int item = iptr->isn_arg.shuffle.shfl_item;
5142 int up = iptr->isn_arg.shuffle.shfl_up;
Bram Moolenaar389df252020-07-09 21:20:47 +02005143
5144 tmp_tv = *STACK_TV_BOT(-item);
5145 for ( ; up > 0 && item > 1; --up)
5146 {
5147 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
5148 --item;
5149 }
5150 *STACK_TV_BOT(-item) = tmp_tv;
5151 }
5152 break;
5153
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005154 case ISN_DROP:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005155 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005156 clear_tv(STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005157 ectx->ec_where.wt_index = 0;
5158 ectx->ec_where.wt_variable = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005159 break;
5160 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02005161 continue;
5162
Bram Moolenaard032f342020-07-18 18:13:02 +02005163func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02005164 // Restore previous function. If the frame pointer is where we started
5165 // then there is none and we are done.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005166 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02005167 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02005168
Bram Moolenaar4c137212021-04-19 16:48:48 +02005169 if (func_return(ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02005170 // only fails when out of memory
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005171 goto theend;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02005172 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02005173
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02005174on_error:
Bram Moolenaaraf0df472020-12-02 20:51:22 +01005175 // Jump here for an error that does not require aborting execution.
Bram Moolenaar56602ba2020-12-05 21:22:08 +01005176 // If "emsg_silent" is set then ignore the error, unless it was set
5177 // when calling the function.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005178 if (did_emsg_cumul + did_emsg == ectx->ec_did_emsg_before
Bram Moolenaar56602ba2020-12-05 21:22:08 +01005179 && emsg_silent && did_emsg_def == 0)
Bram Moolenaarf9041332021-01-21 19:41:16 +01005180 {
5181 // If a sequence of instructions causes an error while ":silent!"
5182 // was used, restore the stack length and jump ahead to restoring
5183 // the cmdmod.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005184 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaarf9041332021-01-21 19:41:16 +01005185 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005186 while (ectx->ec_stack.ga_len
5187 > ectx->ec_funclocal.floc_restore_cmdmod_stacklen)
Bram Moolenaarf9041332021-01-21 19:41:16 +01005188 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005189 --ectx->ec_stack.ga_len;
Bram Moolenaarf9041332021-01-21 19:41:16 +01005190 clear_tv(STACK_TV_BOT(0));
5191 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005192 while (ectx->ec_instr[ectx->ec_iidx].isn_type != ISN_CMDMOD_REV)
5193 ++ectx->ec_iidx;
Bram Moolenaarf9041332021-01-21 19:41:16 +01005194 }
Bram Moolenaarcd030c42020-10-30 21:49:40 +01005195 continue;
Bram Moolenaarf9041332021-01-21 19:41:16 +01005196 }
Bram Moolenaaraf0df472020-12-02 20:51:22 +01005197on_fatal_error:
5198 // Jump here for an error that messes up the stack.
Bram Moolenaar171fb922020-10-28 16:54:47 +01005199 // If we are not inside a try-catch started here, abort execution.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005200 if (trylevel <= ectx->ec_trylevel_at_start)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005201 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005202 }
5203
5204done:
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005205 ret = OK;
5206theend:
Bram Moolenaar58779852022-09-06 18:31:14 +01005207 may_invoke_defer_funcs(ectx);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01005208
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005209 dict_stack_clear(dict_stack_len_at_start);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005210 ectx->ec_trylevel_at_start = save_trylevel_at_start;
5211 return ret;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005212}
5213
5214/*
Bram Moolenaar806a2732022-09-04 15:40:36 +01005215 * Execute the instructions from a VAR_INSTR typval and put the result in
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005216 * "rettv".
5217 * Return OK or FAIL.
5218 */
5219 int
5220exe_typval_instr(typval_T *tv, typval_T *rettv)
5221{
5222 ectx_T *ectx = tv->vval.v_instr->instr_ectx;
5223 isn_T *save_instr = ectx->ec_instr;
5224 int save_iidx = ectx->ec_iidx;
5225 int res;
5226
LemonBoyf3b48952022-05-05 13:53:03 +01005227 // Initialize rettv so that it is safe for caller to invoke clear_tv(rettv)
5228 // even when the compilation fails.
5229 rettv->v_type = VAR_UNKNOWN;
5230
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005231 ectx->ec_instr = tv->vval.v_instr->instr_instr;
5232 res = exec_instructions(ectx);
5233 if (res == OK)
5234 {
5235 *rettv = *STACK_TV_BOT(-1);
5236 --ectx->ec_stack.ga_len;
5237 }
5238
5239 ectx->ec_instr = save_instr;
5240 ectx->ec_iidx = save_iidx;
5241
5242 return res;
5243}
5244
5245/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02005246 * Execute the instructions from an ISN_SUBSTITUTE command, which are in
5247 * "substitute_instr".
5248 */
5249 char_u *
5250exe_substitute_instr(void)
5251{
5252 ectx_T *ectx = substitute_instr->subs_ectx;
5253 isn_T *save_instr = ectx->ec_instr;
5254 int save_iidx = ectx->ec_iidx;
5255 char_u *res;
5256
5257 ectx->ec_instr = substitute_instr->subs_instr;
5258 if (exec_instructions(ectx) == OK)
Bram Moolenaar90193e62021-04-04 20:49:50 +02005259 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005260 typval_T *tv = STACK_TV_BOT(-1);
5261
Bram Moolenaar27523602021-06-05 21:36:19 +02005262 res = typval2string(tv, TRUE);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005263 --ectx->ec_stack.ga_len;
5264 clear_tv(tv);
Bram Moolenaar90193e62021-04-04 20:49:50 +02005265 }
5266 else
5267 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005268 substitute_instr->subs_status = FAIL;
5269 res = vim_strsave((char_u *)"");
Bram Moolenaar90193e62021-04-04 20:49:50 +02005270 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005271
Bram Moolenaar4c137212021-04-19 16:48:48 +02005272 ectx->ec_instr = save_instr;
5273 ectx->ec_iidx = save_iidx;
5274
5275 return res;
5276}
5277
5278/*
5279 * Call a "def" function from old Vim script.
5280 * Return OK or FAIL.
5281 */
5282 int
5283call_def_function(
5284 ufunc_T *ufunc,
5285 int argc_arg, // nr of arguments
5286 typval_T *argv, // arguments
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005287 int flags, // DEF_ flags
Bram Moolenaar4c137212021-04-19 16:48:48 +02005288 partial_T *partial, // optional partial for context
Bram Moolenaar58779852022-09-06 18:31:14 +01005289 funccall_T *funccal,
Bram Moolenaar4c137212021-04-19 16:48:48 +02005290 typval_T *rettv) // return value
5291{
5292 ectx_T ectx; // execution context
5293 int argc = argc_arg;
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005294 int partial_argc = partial == NULL
5295 || (flags & DEF_USE_PT_ARGV) == 0
5296 ? 0 : partial->pt_argc;
5297 int total_argc = argc + partial_argc;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005298 typval_T *tv;
5299 int idx;
5300 int ret = FAIL;
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005301 int defcount = ufunc->uf_args.ga_len - total_argc;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005302 sctx_T save_current_sctx = current_sctx;
5303 int did_emsg_before = did_emsg_cumul + did_emsg;
5304 int save_suppress_errthrow = suppress_errthrow;
5305 msglist_T **saved_msg_list = NULL;
5306 msglist_T *private_msg_list = NULL;
5307 int save_emsg_silent_def = emsg_silent_def;
5308 int save_did_emsg_def = did_emsg_def;
5309 int orig_funcdepth;
Bram Moolenaare99d4222021-06-13 14:01:26 +02005310 int orig_nesting_level = ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005311
5312// Get pointer to item in the stack.
5313#undef STACK_TV
5314#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
5315
5316// Get pointer to item at the bottom of the stack, -1 is the bottom.
5317#undef STACK_TV_BOT
5318#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
5319
5320// Get pointer to a local variable on the stack. Negative for arguments.
5321#undef STACK_TV_VAR
5322#define STACK_TV_VAR(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_frame_idx + STACK_FRAME_SIZE + idx)
5323
5324 if (ufunc->uf_def_status == UF_NOT_COMPILED
5325 || ufunc->uf_def_status == UF_COMPILE_ERROR
Bram Moolenaar139575d2022-03-15 19:29:30 +00005326 || (func_needs_compiling(ufunc, get_compile_type(ufunc))
5327 && compile_def_function(ufunc, FALSE,
5328 get_compile_type(ufunc), NULL) == FAIL))
Bram Moolenaar4c137212021-04-19 16:48:48 +02005329 {
5330 if (did_emsg_cumul + did_emsg == did_emsg_before)
5331 semsg(_(e_function_is_not_compiled_str),
5332 printable_func_name(ufunc));
5333 return FAIL;
5334 }
5335
5336 {
5337 // Check the function was really compiled.
5338 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5339 + ufunc->uf_dfunc_idx;
5340 if (INSTRUCTIONS(dfunc) == NULL)
5341 {
5342 iemsg("using call_def_function() on not compiled function");
5343 return FAIL;
5344 }
5345 }
5346
5347 // If depth of calling is getting too high, don't execute the function.
5348 orig_funcdepth = funcdepth_get();
5349 if (funcdepth_increment() == FAIL)
5350 return FAIL;
5351
5352 CLEAR_FIELD(ectx);
5353 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
5354 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
Bram Moolenaar35578162021-08-02 19:10:38 +02005355 if (GA_GROW_FAILS(&ectx.ec_stack, 20))
Bram Moolenaar4c137212021-04-19 16:48:48 +02005356 {
5357 funcdepth_decrement();
5358 return FAIL;
5359 }
5360 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
5361 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
5362 ectx.ec_did_emsg_before = did_emsg_before;
Bram Moolenaare99d4222021-06-13 14:01:26 +02005363 ++ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005364
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005365 idx = total_argc - ufunc->uf_args.ga_len;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005366 if (idx > 0 && ufunc->uf_va_name == NULL)
5367 {
Matvey Tarasovd14bb1a2022-06-29 13:18:27 +01005368 semsg(NGETTEXT(e_one_argument_too_many, e_nr_arguments_too_many,
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005369 idx), idx);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005370 goto failed_early;
5371 }
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005372 idx = total_argc - ufunc->uf_args.ga_len + ufunc->uf_def_args.ga_len;
Bram Moolenaarc6d71532021-06-05 18:49:38 +02005373 if (idx < 0)
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02005374 {
Matvey Tarasovd14bb1a2022-06-29 13:18:27 +01005375 semsg(NGETTEXT(e_one_argument_too_few, e_nr_arguments_too_few,
Bram Moolenaar98aff652022-09-06 21:02:35 +01005376 -idx), -idx);
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02005377 goto failed_early;
5378 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005379
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005380 // Put values from the partial and arguments on the stack, but no more than
5381 // what the function expects. A lambda can be called with more arguments
5382 // than it uses.
5383 for (idx = 0; idx < total_argc
Bram Moolenaar4c137212021-04-19 16:48:48 +02005384 && (ufunc->uf_va_name != NULL || idx < ufunc->uf_args.ga_len);
5385 ++idx)
5386 {
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005387 int argv_idx = idx - partial_argc;
5388
5389 tv = idx < partial_argc ? partial->pt_argv + idx : argv + argv_idx;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005390 if (idx >= ufunc->uf_args.ga_len - ufunc->uf_def_args.ga_len
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005391 && tv->v_type == VAR_SPECIAL
5392 && tv->vval.v_number == VVAL_NONE)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005393 {
5394 // Use the default value.
5395 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
5396 }
5397 else
5398 {
5399 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
5400 && check_typval_arg_type(
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005401 ufunc->uf_arg_types[idx], tv,
5402 NULL, argv_idx + 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005403 goto failed_early;
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005404 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005405 }
5406 ++ectx.ec_stack.ga_len;
5407 }
5408
5409 // Turn varargs into a list. Empty list if no args.
5410 if (ufunc->uf_va_name != NULL)
5411 {
5412 int vararg_count = argc - ufunc->uf_args.ga_len;
5413
5414 if (vararg_count < 0)
5415 vararg_count = 0;
5416 else
5417 argc -= vararg_count;
5418 if (exe_newlist(vararg_count, &ectx) == FAIL)
5419 goto failed_early;
5420
5421 // Check the type of the list items.
5422 tv = STACK_TV_BOT(-1);
5423 if (ufunc->uf_va_type != NULL
5424 && ufunc->uf_va_type != &t_list_any
5425 && ufunc->uf_va_type->tt_member != &t_any
5426 && tv->vval.v_list != NULL)
5427 {
5428 type_T *expected = ufunc->uf_va_type->tt_member;
5429 listitem_T *li = tv->vval.v_list->lv_first;
5430
5431 for (idx = 0; idx < vararg_count; ++idx)
5432 {
5433 if (check_typval_arg_type(expected, &li->li_tv,
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02005434 NULL, argc + idx + 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005435 goto failed_early;
5436 li = li->li_next;
5437 }
5438 }
5439
5440 if (defcount > 0)
5441 // Move varargs list to below missing default arguments.
5442 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
5443 --ectx.ec_stack.ga_len;
5444 }
5445
5446 // Make space for omitted arguments, will store default value below.
5447 // Any varargs list goes after them.
5448 if (defcount > 0)
5449 for (idx = 0; idx < defcount; ++idx)
5450 {
5451 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
5452 ++ectx.ec_stack.ga_len;
5453 }
5454 if (ufunc->uf_va_name != NULL)
5455 ++ectx.ec_stack.ga_len;
5456
5457 // Frame pointer points to just after arguments.
5458 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
5459 ectx.ec_initial_frame_idx = ectx.ec_frame_idx;
5460
5461 {
5462 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5463 + ufunc->uf_dfunc_idx;
5464 ufunc_T *base_ufunc = dfunc->df_ufunc;
5465
5466 // "uf_partial" is on the ufunc that "df_ufunc" points to, as is done
5467 // by copy_func().
5468 if (partial != NULL || base_ufunc->uf_partial != NULL)
5469 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005470 ectx.ec_outer_ref = ALLOC_CLEAR_ONE(outer_ref_T);
5471 if (ectx.ec_outer_ref == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005472 goto failed_early;
5473 if (partial != NULL)
5474 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00005475 outer_T *outer = get_pt_outer(partial);
5476
5477 if (outer->out_stack == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005478 {
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00005479 if (current_ectx != NULL)
5480 {
5481 if (current_ectx->ec_outer_ref != NULL
5482 && current_ectx->ec_outer_ref->or_outer != NULL)
5483 ectx.ec_outer_ref->or_outer =
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005484 current_ectx->ec_outer_ref->or_outer;
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00005485 }
5486 // Should there be an error here?
Bram Moolenaar4c137212021-04-19 16:48:48 +02005487 }
5488 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005489 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00005490 ectx.ec_outer_ref->or_outer = outer;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005491 ++partial->pt_refcount;
5492 ectx.ec_outer_ref->or_partial = partial;
5493 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005494 }
5495 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005496 {
5497 ectx.ec_outer_ref->or_outer = &base_ufunc->uf_partial->pt_outer;
5498 ++base_ufunc->uf_partial->pt_refcount;
5499 ectx.ec_outer_ref->or_partial = base_ufunc->uf_partial;
5500 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005501 }
5502 }
5503
5504 // dummy frame entries
5505 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
5506 {
5507 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
5508 ++ectx.ec_stack.ga_len;
5509 }
5510
5511 {
5512 // Reserve space for local variables and any closure reference count.
5513 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5514 + ufunc->uf_dfunc_idx;
5515
Bram Moolenaar5cd64792021-12-25 18:23:24 +00005516 // Initialize variables to zero. That avoids having to generate
5517 // initializing instructions for "var nr: number", "var x: any", etc.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005518 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaar5cd64792021-12-25 18:23:24 +00005519 {
5520 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
5521 STACK_TV_VAR(idx)->vval.v_number = 0;
5522 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005523 ectx.ec_stack.ga_len += dfunc->df_varcount;
5524 if (dfunc->df_has_closure)
5525 {
5526 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
5527 STACK_TV_VAR(idx)->vval.v_number = 0;
5528 ++ectx.ec_stack.ga_len;
5529 }
5530
5531 ectx.ec_instr = INSTRUCTIONS(dfunc);
5532 }
5533
Bram Moolenaar58779852022-09-06 18:31:14 +01005534 // Store the execution context in funccal, used by invoke_all_defer().
5535 if (funccal != NULL)
5536 funccal->fc_ectx = &ectx;
5537
Bram Moolenaar4c137212021-04-19 16:48:48 +02005538 // Following errors are in the function, not the caller.
5539 // Commands behave like vim9script.
5540 estack_push_ufunc(ufunc, 1);
5541 current_sctx = ufunc->uf_script_ctx;
5542 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
5543
5544 // Use a specific location for storing error messages to be converted to an
5545 // exception.
5546 saved_msg_list = msg_list;
5547 msg_list = &private_msg_list;
5548
5549 // Do turn errors into exceptions.
5550 suppress_errthrow = FALSE;
5551
5552 // Do not delete the function while executing it.
5553 ++ufunc->uf_calls;
5554
5555 // When ":silent!" was used before calling then we still abort the
5556 // function. If ":silent!" is used in the function then we don't.
5557 emsg_silent_def = emsg_silent;
5558 did_emsg_def = 0;
5559
5560 ectx.ec_where.wt_index = 0;
5561 ectx.ec_where.wt_variable = FALSE;
5562
5563 // Execute the instructions until done.
5564 ret = exec_instructions(&ectx);
5565 if (ret == OK)
5566 {
5567 // function finished, get result from the stack.
5568 if (ufunc->uf_ret_type == &t_void)
5569 {
5570 rettv->v_type = VAR_VOID;
5571 }
5572 else
5573 {
5574 tv = STACK_TV_BOT(-1);
5575 *rettv = *tv;
5576 tv->v_type = VAR_UNKNOWN;
5577 }
5578 }
5579
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01005580 // When failed need to unwind the call stack.
Bram Moolenaar58779852022-09-06 18:31:14 +01005581 unwind_def_callstack(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02005582
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005583 // Deal with any remaining closures, they may be in use somewhere.
5584 if (ectx.ec_funcrefs.ga_len > 0)
Bram Moolenaarf112f302020-12-20 17:47:52 +01005585 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005586 handle_closure_in_use(&ectx, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00005587 ga_clear(&ectx.ec_funcrefs);
Bram Moolenaarf112f302020-12-20 17:47:52 +01005588 }
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005589
Bram Moolenaaree8580e2020-08-28 17:19:07 +02005590 estack_pop();
5591 current_sctx = save_current_sctx;
5592
Bram Moolenaar2336c372021-12-06 15:06:54 +00005593 if (--ufunc->uf_calls <= 0 && ufunc->uf_refcount <= 0)
5594 // Function was unreferenced while being used, free it now.
5595 func_clear_free(ufunc, FALSE);
Bram Moolenaarc970e422021-03-17 15:03:04 +01005596
Bram Moolenaar352134b2020-10-17 22:04:08 +02005597 if (*msg_list != NULL && saved_msg_list != NULL)
5598 {
5599 msglist_T **plist = saved_msg_list;
5600
5601 // Append entries from the current msg_list (uncaught exceptions) to
5602 // the saved msg_list.
5603 while (*plist != NULL)
5604 plist = &(*plist)->next;
5605
5606 *plist = *msg_list;
5607 }
5608 msg_list = saved_msg_list;
5609
Bram Moolenaar4c137212021-04-19 16:48:48 +02005610 if (ectx.ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar02194d22020-10-24 23:08:38 +02005611 {
5612 cmdmod.cmod_filter_regmatch.regprog = NULL;
5613 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005614 cmdmod = ectx.ec_funclocal.floc_save_cmdmod;
Bram Moolenaar02194d22020-10-24 23:08:38 +02005615 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +01005616 emsg_silent_def = save_emsg_silent_def;
5617 did_emsg_def += save_did_emsg_def;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02005618
Bram Moolenaaree8580e2020-08-28 17:19:07 +02005619failed_early:
Bram Moolenaar0d1f55c2022-04-05 17:30:29 +01005620 // Free all arguments and local variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005621 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
5622 clear_tv(STACK_TV(idx));
Bram Moolenaare99d4222021-06-13 14:01:26 +02005623 ex_nesting_level = orig_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02005624
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005625 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01005626 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005627 if (ectx.ec_outer_ref != NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +01005628 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005629 if (ectx.ec_outer_ref->or_outer_allocated)
5630 vim_free(ectx.ec_outer_ref->or_outer);
5631 partial_unref(ectx.ec_outer_ref->or_partial);
5632 vim_free(ectx.ec_outer_ref);
Bram Moolenaar0186e582021-01-10 18:33:11 +01005633 }
5634
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02005635 // Not sure if this is necessary.
5636 suppress_errthrow = save_suppress_errthrow;
5637
Bram Moolenaarb5841b92021-07-15 18:09:53 +02005638 if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before
5639 && !need_rethrow)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005640 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02005641 printable_func_name(ufunc));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01005642 funcdepth_restore(orig_funcdepth);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005643 return ret;
5644}
5645
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005646/*
Bram Moolenaar58779852022-09-06 18:31:14 +01005647 * Called when a def function has finished (possibly failed).
5648 * Invoke all the function returns to clean up and invoke deferred functions,
5649 * except the toplevel one.
5650 */
5651 void
5652unwind_def_callstack(ectx_T *ectx)
5653{
5654 while (ectx->ec_frame_idx != ectx->ec_initial_frame_idx)
5655 func_return(ectx);
5656}
5657
5658/*
5659 * Invoke any deffered functions for the top function in "ectx".
5660 */
5661 void
5662may_invoke_defer_funcs(ectx_T *ectx)
5663{
5664 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
5665
5666 if (dfunc->df_defer_var_idx > 0)
5667 invoke_defer_funcs(ectx);
5668}
5669
5670/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02005671 * List instructions "instr" up to "instr_count" or until ISN_FINISH.
5672 * "ufunc" has the source lines, NULL for the instructions of ISN_SUBSTITUTE.
5673 * "pfx" is prefixed to every line.
5674 */
5675 static void
5676list_instructions(char *pfx, isn_T *instr, int instr_count, ufunc_T *ufunc)
5677{
5678 int line_idx = 0;
5679 int prev_current = 0;
5680 int current;
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02005681 int def_arg_idx = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005682
5683 for (current = 0; current < instr_count; ++current)
5684 {
5685 isn_T *iptr = &instr[current];
5686 char *line;
5687
5688 if (ufunc != NULL)
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02005689 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005690 while (line_idx < iptr->isn_lnum
5691 && line_idx < ufunc->uf_lines.ga_len)
5692 {
5693 if (current > prev_current)
5694 {
5695 msg_puts("\n\n");
5696 prev_current = current;
5697 }
5698 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
5699 if (line != NULL)
5700 msg(line);
5701 }
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02005702 if (iptr->isn_type == ISN_JUMP_IF_ARG_SET)
5703 {
5704 int first_def_arg = ufunc->uf_args.ga_len
5705 - ufunc->uf_def_args.ga_len;
5706
5707 if (def_arg_idx > 0)
5708 msg_puts("\n\n");
5709 msg_start();
5710 msg_puts(" ");
5711 msg_puts(((char **)(ufunc->uf_args.ga_data))[
5712 first_def_arg + def_arg_idx]);
5713 msg_puts(" = ");
5714 msg_puts(((char **)(ufunc->uf_def_args.ga_data))[def_arg_idx++]);
5715 msg_clr_eos();
5716 msg_end();
5717 }
5718 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005719
5720 switch (iptr->isn_type)
5721 {
5722 case ISN_EXEC:
5723 smsg("%s%4d EXEC %s", pfx, current, iptr->isn_arg.string);
5724 break;
Bram Moolenaar20677332021-06-06 17:02:53 +02005725 case ISN_EXEC_SPLIT:
5726 smsg("%s%4d EXEC_SPLIT %s", pfx, current, iptr->isn_arg.string);
5727 break;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00005728 case ISN_EXECRANGE:
5729 smsg("%s%4d EXECRANGE %s", pfx, current, iptr->isn_arg.string);
5730 break;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005731 case ISN_LEGACY_EVAL:
5732 smsg("%s%4d EVAL legacy %s", pfx, current,
5733 iptr->isn_arg.string);
5734 break;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02005735 case ISN_REDIRSTART:
5736 smsg("%s%4d REDIR", pfx, current);
5737 break;
5738 case ISN_REDIREND:
5739 smsg("%s%4d REDIR END%s", pfx, current,
5740 iptr->isn_arg.number ? " append" : "");
5741 break;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005742 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005743#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005744 smsg("%s%4d CEXPR pre %s", pfx, current,
5745 cexpr_get_auname(iptr->isn_arg.number));
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005746#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005747 break;
5748 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005749#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005750 {
5751 cexprref_T *cer = iptr->isn_arg.cexpr.cexpr_ref;
5752
5753 smsg("%s%4d CEXPR core %s%s \"%s\"", pfx, current,
5754 cexpr_get_auname(cer->cer_cmdidx),
5755 cer->cer_forceit ? "!" : "",
5756 cer->cer_cmdline);
5757 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005758#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005759 break;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005760 case ISN_INSTR:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01005761 smsg("%s%4d INSTR", pfx, current);
5762 list_instructions(" ", iptr->isn_arg.instr, INT_MAX, NULL);
5763 msg(" -------------");
5764 break;
5765 case ISN_SOURCE:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005766 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01005767 scriptitem_T *si = SCRIPT_ITEM(iptr->isn_arg.number);
5768
5769 smsg("%s%4d SOURCE %s", pfx, current, si->sn_name);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005770 }
5771 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005772 case ISN_SUBSTITUTE:
5773 {
5774 subs_T *subs = &iptr->isn_arg.subs;
5775
5776 smsg("%s%4d SUBSTITUTE %s", pfx, current, subs->subs_cmd);
5777 list_instructions(" ", subs->subs_instr, INT_MAX, NULL);
5778 msg(" -------------");
5779 }
5780 break;
5781 case ISN_EXECCONCAT:
5782 smsg("%s%4d EXECCONCAT %lld", pfx, current,
5783 (varnumber_T)iptr->isn_arg.number);
5784 break;
5785 case ISN_ECHO:
5786 {
5787 echo_T *echo = &iptr->isn_arg.echo;
5788
5789 smsg("%s%4d %s %d", pfx, current,
5790 echo->echo_with_white ? "ECHO" : "ECHON",
5791 echo->echo_count);
5792 }
5793 break;
5794 case ISN_EXECUTE:
5795 smsg("%s%4d EXECUTE %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02005796 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005797 break;
5798 case ISN_ECHOMSG:
5799 smsg("%s%4d ECHOMSG %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02005800 (varnumber_T)(iptr->isn_arg.number));
5801 break;
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01005802 case ISN_ECHOWINDOW:
5803 smsg("%s%4d ECHOWINDOW %lld", pfx, current,
5804 (varnumber_T)(iptr->isn_arg.number));
5805 break;
Bram Moolenaar7de62622021-08-07 15:05:47 +02005806 case ISN_ECHOCONSOLE:
5807 smsg("%s%4d ECHOCONSOLE %lld", pfx, current,
5808 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005809 break;
5810 case ISN_ECHOERR:
5811 smsg("%s%4d ECHOERR %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02005812 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005813 break;
5814 case ISN_LOAD:
5815 {
5816 if (iptr->isn_arg.number < 0)
5817 smsg("%s%4d LOAD arg[%lld]", pfx, current,
5818 (varnumber_T)(iptr->isn_arg.number
5819 + STACK_FRAME_SIZE));
5820 else
5821 smsg("%s%4d LOAD $%lld", pfx, current,
5822 (varnumber_T)(iptr->isn_arg.number));
5823 }
5824 break;
5825 case ISN_LOADOUTER:
5826 {
Bram Moolenaar95e4dd82022-04-27 22:15:40 +01005827 if (iptr->isn_arg.outer.outer_idx < 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005828 smsg("%s%4d LOADOUTER level %d arg[%d]", pfx, current,
5829 iptr->isn_arg.outer.outer_depth,
5830 iptr->isn_arg.outer.outer_idx
5831 + STACK_FRAME_SIZE);
5832 else
5833 smsg("%s%4d LOADOUTER level %d $%d", pfx, current,
5834 iptr->isn_arg.outer.outer_depth,
5835 iptr->isn_arg.outer.outer_idx);
5836 }
5837 break;
5838 case ISN_LOADV:
5839 smsg("%s%4d LOADV v:%s", pfx, current,
5840 get_vim_var_name(iptr->isn_arg.number));
5841 break;
5842 case ISN_LOADSCRIPT:
5843 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005844 scriptref_T *sref = iptr->isn_arg.script.scriptref;
5845 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
5846 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005847
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005848 sv = get_script_svar(sref, -1);
5849 if (sv == NULL)
5850 smsg("%s%4d LOADSCRIPT [deleted] from %s",
5851 pfx, current, si->sn_name);
5852 else
5853 smsg("%s%4d LOADSCRIPT %s-%d from %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02005854 sv->sv_name,
5855 sref->sref_idx,
5856 si->sn_name);
5857 }
5858 break;
5859 case ISN_LOADS:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01005860 case ISN_LOADEXPORT:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005861 {
5862 scriptitem_T *si = SCRIPT_ITEM(
5863 iptr->isn_arg.loadstore.ls_sid);
5864
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01005865 smsg("%s%4d %s s:%s from %s", pfx, current,
5866 iptr->isn_type == ISN_LOADS ? "LOADS"
5867 : "LOADEXPORT",
5868 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005869 }
5870 break;
5871 case ISN_LOADAUTO:
5872 smsg("%s%4d LOADAUTO %s", pfx, current, iptr->isn_arg.string);
5873 break;
5874 case ISN_LOADG:
5875 smsg("%s%4d LOADG g:%s", pfx, current, iptr->isn_arg.string);
5876 break;
5877 case ISN_LOADB:
5878 smsg("%s%4d LOADB b:%s", pfx, current, iptr->isn_arg.string);
5879 break;
5880 case ISN_LOADW:
5881 smsg("%s%4d LOADW w:%s", pfx, current, iptr->isn_arg.string);
5882 break;
5883 case ISN_LOADT:
5884 smsg("%s%4d LOADT t:%s", pfx, current, iptr->isn_arg.string);
5885 break;
5886 case ISN_LOADGDICT:
5887 smsg("%s%4d LOAD g:", pfx, current);
5888 break;
5889 case ISN_LOADBDICT:
5890 smsg("%s%4d LOAD b:", pfx, current);
5891 break;
5892 case ISN_LOADWDICT:
5893 smsg("%s%4d LOAD w:", pfx, current);
5894 break;
5895 case ISN_LOADTDICT:
5896 smsg("%s%4d LOAD t:", pfx, current);
5897 break;
5898 case ISN_LOADOPT:
5899 smsg("%s%4d LOADOPT %s", pfx, current, iptr->isn_arg.string);
5900 break;
5901 case ISN_LOADENV:
5902 smsg("%s%4d LOADENV %s", pfx, current, iptr->isn_arg.string);
5903 break;
5904 case ISN_LOADREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005905 smsg("%s%4d LOADREG @%c", pfx, current,
5906 (int)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005907 break;
5908
5909 case ISN_STORE:
5910 if (iptr->isn_arg.number < 0)
5911 smsg("%s%4d STORE arg[%lld]", pfx, current,
5912 iptr->isn_arg.number + STACK_FRAME_SIZE);
5913 else
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005914 smsg("%s%4d STORE $%lld", pfx, current,
5915 iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005916 break;
5917 case ISN_STOREOUTER:
Bram Moolenaarf6ced982022-04-28 12:00:49 +01005918 smsg("%s%4d STOREOUTER level %d $%d", pfx, current,
5919 iptr->isn_arg.outer.outer_depth,
5920 iptr->isn_arg.outer.outer_idx);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005921 break;
5922 case ISN_STOREV:
5923 smsg("%s%4d STOREV v:%s", pfx, current,
5924 get_vim_var_name(iptr->isn_arg.number));
5925 break;
5926 case ISN_STOREAUTO:
5927 smsg("%s%4d STOREAUTO %s", pfx, current, iptr->isn_arg.string);
5928 break;
5929 case ISN_STOREG:
5930 smsg("%s%4d STOREG %s", pfx, current, iptr->isn_arg.string);
5931 break;
5932 case ISN_STOREB:
5933 smsg("%s%4d STOREB %s", pfx, current, iptr->isn_arg.string);
5934 break;
5935 case ISN_STOREW:
5936 smsg("%s%4d STOREW %s", pfx, current, iptr->isn_arg.string);
5937 break;
5938 case ISN_STORET:
5939 smsg("%s%4d STORET %s", pfx, current, iptr->isn_arg.string);
5940 break;
5941 case ISN_STORES:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01005942 case ISN_STOREEXPORT:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005943 {
5944 scriptitem_T *si = SCRIPT_ITEM(
5945 iptr->isn_arg.loadstore.ls_sid);
5946
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01005947 smsg("%s%4d %s %s in %s", pfx, current,
5948 iptr->isn_type == ISN_STORES
5949 ? "STORES" : "STOREEXPORT",
Bram Moolenaar4c137212021-04-19 16:48:48 +02005950 iptr->isn_arg.loadstore.ls_name, si->sn_name);
5951 }
5952 break;
5953 case ISN_STORESCRIPT:
5954 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005955 scriptref_T *sref = iptr->isn_arg.script.scriptref;
5956 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
5957 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005958
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005959 sv = get_script_svar(sref, -1);
5960 if (sv == NULL)
5961 smsg("%s%4d STORESCRIPT [deleted] in %s",
5962 pfx, current, si->sn_name);
5963 else
5964 smsg("%s%4d STORESCRIPT %s-%d in %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02005965 sv->sv_name,
5966 sref->sref_idx,
5967 si->sn_name);
5968 }
5969 break;
5970 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00005971 case ISN_STOREFUNCOPT:
5972 smsg("%s%4d %s &%s", pfx, current,
5973 iptr->isn_type == ISN_STOREOPT ? "STOREOPT" : "STOREFUNCOPT",
Bram Moolenaar4c137212021-04-19 16:48:48 +02005974 iptr->isn_arg.storeopt.so_name);
5975 break;
5976 case ISN_STOREENV:
5977 smsg("%s%4d STOREENV $%s", pfx, current, iptr->isn_arg.string);
5978 break;
5979 case ISN_STOREREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005980 smsg("%s%4d STOREREG @%c", pfx, current,
5981 (int)iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005982 break;
5983 case ISN_STORENR:
5984 smsg("%s%4d STORE %lld in $%d", pfx, current,
5985 iptr->isn_arg.storenr.stnr_val,
5986 iptr->isn_arg.storenr.stnr_idx);
5987 break;
5988
5989 case ISN_STOREINDEX:
5990 smsg("%s%4d STOREINDEX %s", pfx, current,
5991 vartype_name(iptr->isn_arg.vartype));
5992 break;
5993
5994 case ISN_STORERANGE:
5995 smsg("%s%4d STORERANGE", pfx, current);
5996 break;
5997
5998 // constants
5999 case ISN_PUSHNR:
6000 smsg("%s%4d PUSHNR %lld", pfx, current,
6001 (varnumber_T)(iptr->isn_arg.number));
6002 break;
6003 case ISN_PUSHBOOL:
6004 case ISN_PUSHSPEC:
6005 smsg("%s%4d PUSH %s", pfx, current,
6006 get_var_special_name(iptr->isn_arg.number));
6007 break;
6008 case ISN_PUSHF:
6009#ifdef FEAT_FLOAT
6010 smsg("%s%4d PUSHF %g", pfx, current, iptr->isn_arg.fnumber);
6011#endif
6012 break;
6013 case ISN_PUSHS:
6014 smsg("%s%4d PUSHS \"%s\"", pfx, current, iptr->isn_arg.string);
6015 break;
6016 case ISN_PUSHBLOB:
6017 {
6018 char_u *r;
6019 char_u numbuf[NUMBUFLEN];
6020 char_u *tofree;
6021
6022 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
6023 smsg("%s%4d PUSHBLOB %s", pfx, current, r);
6024 vim_free(tofree);
6025 }
6026 break;
6027 case ISN_PUSHFUNC:
6028 {
6029 char *name = (char *)iptr->isn_arg.string;
6030
6031 smsg("%s%4d PUSHFUNC \"%s\"", pfx, current,
6032 name == NULL ? "[none]" : name);
6033 }
6034 break;
6035 case ISN_PUSHCHANNEL:
6036#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar397a87a2022-03-20 21:14:15 +00006037 smsg("%s%4d PUSHCHANNEL 0", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006038#endif
6039 break;
6040 case ISN_PUSHJOB:
6041#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar397a87a2022-03-20 21:14:15 +00006042 smsg("%s%4d PUSHJOB \"no process\"", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006043#endif
6044 break;
6045 case ISN_PUSHEXC:
6046 smsg("%s%4d PUSH v:exception", pfx, current);
6047 break;
Bram Moolenaar06b77222022-01-25 15:51:56 +00006048 case ISN_AUTOLOAD:
6049 smsg("%s%4d AUTOLOAD %s", pfx, current, iptr->isn_arg.string);
6050 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006051 case ISN_UNLET:
6052 smsg("%s%4d UNLET%s %s", pfx, current,
6053 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
6054 iptr->isn_arg.unlet.ul_name);
6055 break;
6056 case ISN_UNLETENV:
6057 smsg("%s%4d UNLETENV%s $%s", pfx, current,
6058 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
6059 iptr->isn_arg.unlet.ul_name);
6060 break;
6061 case ISN_UNLETINDEX:
6062 smsg("%s%4d UNLETINDEX", pfx, current);
6063 break;
6064 case ISN_UNLETRANGE:
6065 smsg("%s%4d UNLETRANGE", pfx, current);
6066 break;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02006067 case ISN_LOCKUNLOCK:
6068 smsg("%s%4d LOCKUNLOCK %s", pfx, current, iptr->isn_arg.string);
6069 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006070 case ISN_LOCKCONST:
6071 smsg("%s%4d LOCKCONST", pfx, current);
6072 break;
6073 case ISN_NEWLIST:
6074 smsg("%s%4d NEWLIST size %lld", pfx, current,
6075 (varnumber_T)(iptr->isn_arg.number));
6076 break;
6077 case ISN_NEWDICT:
6078 smsg("%s%4d NEWDICT size %lld", pfx, current,
6079 (varnumber_T)(iptr->isn_arg.number));
6080 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00006081 case ISN_NEWPARTIAL:
6082 smsg("%s%4d NEWPARTIAL", pfx, current);
6083 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006084
6085 // function call
6086 case ISN_BCALL:
6087 {
6088 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
6089
6090 smsg("%s%4d BCALL %s(argc %d)", pfx, current,
6091 internal_func_name(cbfunc->cbf_idx),
6092 cbfunc->cbf_argcount);
6093 }
6094 break;
6095 case ISN_DCALL:
6096 {
6097 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
6098 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
6099 + cdfunc->cdf_idx;
6100
6101 smsg("%s%4d DCALL %s(argc %d)", pfx, current,
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006102 printable_func_name(df->df_ufunc),
6103 cdfunc->cdf_argcount);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006104 }
6105 break;
6106 case ISN_UCALL:
6107 {
6108 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
6109
6110 smsg("%s%4d UCALL %s(argc %d)", pfx, current,
6111 cufunc->cuf_name, cufunc->cuf_argcount);
6112 }
6113 break;
6114 case ISN_PCALL:
6115 {
6116 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
6117
6118 smsg("%s%4d PCALL%s (argc %d)", pfx, current,
6119 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
6120 }
6121 break;
6122 case ISN_PCALL_END:
6123 smsg("%s%4d PCALL end", pfx, current);
6124 break;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006125 case ISN_DEFER:
6126 smsg("%s%4d DEFER %d args", pfx, current,
6127 (int)iptr->isn_arg.defer.defer_argcount);
6128 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006129 case ISN_RETURN:
6130 smsg("%s%4d RETURN", pfx, current);
6131 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02006132 case ISN_RETURN_VOID:
6133 smsg("%s%4d RETURN void", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006134 break;
6135 case ISN_FUNCREF:
6136 {
6137 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar38453522021-11-28 22:00:12 +00006138 char_u *name;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006139
Bram Moolenaar38453522021-11-28 22:00:12 +00006140 if (funcref->fr_func_name == NULL)
6141 {
6142 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
6143 + funcref->fr_dfunc_idx;
6144 name = df->df_ufunc->uf_name;
6145 }
6146 else
6147 name = funcref->fr_func_name;
6148 smsg("%s%4d FUNCREF %s", pfx, current, name);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006149 }
6150 break;
6151
6152 case ISN_NEWFUNC:
6153 {
6154 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
6155
6156 smsg("%s%4d NEWFUNC %s %s", pfx, current,
6157 newfunc->nf_lambda, newfunc->nf_global);
6158 }
6159 break;
6160
6161 case ISN_DEF:
6162 {
6163 char_u *name = iptr->isn_arg.string;
6164
6165 smsg("%s%4d DEF %s", pfx, current,
6166 name == NULL ? (char_u *)"" : name);
6167 }
6168 break;
6169
6170 case ISN_JUMP:
6171 {
6172 char *when = "?";
6173
6174 switch (iptr->isn_arg.jump.jump_when)
6175 {
6176 case JUMP_ALWAYS:
6177 when = "JUMP";
6178 break;
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02006179 case JUMP_NEVER:
6180 iemsg("JUMP_NEVER should not be used");
6181 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006182 case JUMP_AND_KEEP_IF_TRUE:
6183 when = "JUMP_AND_KEEP_IF_TRUE";
6184 break;
6185 case JUMP_IF_FALSE:
6186 when = "JUMP_IF_FALSE";
6187 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006188 case JUMP_IF_COND_FALSE:
6189 when = "JUMP_IF_COND_FALSE";
6190 break;
6191 case JUMP_IF_COND_TRUE:
6192 when = "JUMP_IF_COND_TRUE";
6193 break;
6194 }
6195 smsg("%s%4d %s -> %d", pfx, current, when,
6196 iptr->isn_arg.jump.jump_where);
6197 }
6198 break;
6199
6200 case ISN_JUMP_IF_ARG_SET:
6201 smsg("%s%4d JUMP_IF_ARG_SET arg[%d] -> %d", pfx, current,
6202 iptr->isn_arg.jumparg.jump_arg_off + STACK_FRAME_SIZE,
6203 iptr->isn_arg.jump.jump_where);
6204 break;
6205
6206 case ISN_FOR:
6207 {
6208 forloop_T *forloop = &iptr->isn_arg.forloop;
6209
6210 smsg("%s%4d FOR $%d -> %d", pfx, current,
6211 forloop->for_idx, forloop->for_end);
6212 }
6213 break;
6214
6215 case ISN_TRY:
6216 {
Bram Moolenaar0d807102021-12-21 09:42:09 +00006217 try_T *try = &iptr->isn_arg.tryref;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006218
6219 if (try->try_ref->try_finally == 0)
6220 smsg("%s%4d TRY catch -> %d, endtry -> %d",
6221 pfx, current,
6222 try->try_ref->try_catch,
6223 try->try_ref->try_endtry);
6224 else
6225 smsg("%s%4d TRY catch -> %d, finally -> %d, endtry -> %d",
6226 pfx, current,
6227 try->try_ref->try_catch,
6228 try->try_ref->try_finally,
6229 try->try_ref->try_endtry);
6230 }
6231 break;
6232 case ISN_CATCH:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006233 smsg("%s%4d CATCH", pfx, current);
6234 break;
6235 case ISN_TRYCONT:
6236 {
6237 trycont_T *trycont = &iptr->isn_arg.trycont;
6238
6239 smsg("%s%4d TRY-CONTINUE %d level%s -> %d", pfx, current,
6240 trycont->tct_levels,
6241 trycont->tct_levels == 1 ? "" : "s",
6242 trycont->tct_where);
6243 }
6244 break;
6245 case ISN_FINALLY:
6246 smsg("%s%4d FINALLY", pfx, current);
6247 break;
6248 case ISN_ENDTRY:
6249 smsg("%s%4d ENDTRY", pfx, current);
6250 break;
6251 case ISN_THROW:
6252 smsg("%s%4d THROW", pfx, current);
6253 break;
6254
6255 // expression operations on number
6256 case ISN_OPNR:
6257 case ISN_OPFLOAT:
6258 case ISN_OPANY:
6259 {
6260 char *what;
6261 char *ins;
6262
6263 switch (iptr->isn_arg.op.op_type)
6264 {
6265 case EXPR_MULT: what = "*"; break;
6266 case EXPR_DIV: what = "/"; break;
6267 case EXPR_REM: what = "%"; break;
6268 case EXPR_SUB: what = "-"; break;
6269 case EXPR_ADD: what = "+"; break;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01006270 case EXPR_LSHIFT: what = "<<"; break;
6271 case EXPR_RSHIFT: what = ">>"; break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006272 default: what = "???"; break;
6273 }
6274 switch (iptr->isn_type)
6275 {
6276 case ISN_OPNR: ins = "OPNR"; break;
6277 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
6278 case ISN_OPANY: ins = "OPANY"; break;
6279 default: ins = "???"; break;
6280 }
6281 smsg("%s%4d %s %s", pfx, current, ins, what);
6282 }
6283 break;
6284
6285 case ISN_COMPAREBOOL:
6286 case ISN_COMPARESPECIAL:
Bram Moolenaar7a222242022-03-01 19:23:24 +00006287 case ISN_COMPARENULL:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006288 case ISN_COMPARENR:
6289 case ISN_COMPAREFLOAT:
6290 case ISN_COMPARESTRING:
6291 case ISN_COMPAREBLOB:
6292 case ISN_COMPARELIST:
6293 case ISN_COMPAREDICT:
6294 case ISN_COMPAREFUNC:
6295 case ISN_COMPAREANY:
6296 {
6297 char *p;
6298 char buf[10];
6299 char *type;
6300
6301 switch (iptr->isn_arg.op.op_type)
6302 {
6303 case EXPR_EQUAL: p = "=="; break;
6304 case EXPR_NEQUAL: p = "!="; break;
6305 case EXPR_GREATER: p = ">"; break;
6306 case EXPR_GEQUAL: p = ">="; break;
6307 case EXPR_SMALLER: p = "<"; break;
6308 case EXPR_SEQUAL: p = "<="; break;
6309 case EXPR_MATCH: p = "=~"; break;
6310 case EXPR_IS: p = "is"; break;
6311 case EXPR_ISNOT: p = "isnot"; break;
6312 case EXPR_NOMATCH: p = "!~"; break;
6313 default: p = "???"; break;
6314 }
6315 STRCPY(buf, p);
6316 if (iptr->isn_arg.op.op_ic == TRUE)
6317 strcat(buf, "?");
6318 switch(iptr->isn_type)
6319 {
6320 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
6321 case ISN_COMPARESPECIAL:
6322 type = "COMPARESPECIAL"; break;
Bram Moolenaar7a222242022-03-01 19:23:24 +00006323 case ISN_COMPARENULL: type = "COMPARENULL"; break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006324 case ISN_COMPARENR: type = "COMPARENR"; break;
6325 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
6326 case ISN_COMPARESTRING:
6327 type = "COMPARESTRING"; break;
6328 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
6329 case ISN_COMPARELIST: type = "COMPARELIST"; break;
6330 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
6331 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
6332 case ISN_COMPAREANY: type = "COMPAREANY"; break;
6333 default: type = "???"; break;
6334 }
6335
6336 smsg("%s%4d %s %s", pfx, current, type, buf);
6337 }
6338 break;
6339
6340 case ISN_ADDLIST: smsg("%s%4d ADDLIST", pfx, current); break;
6341 case ISN_ADDBLOB: smsg("%s%4d ADDBLOB", pfx, current); break;
6342
6343 // expression operations
LemonBoy372bcce2022-04-25 12:43:20 +01006344 case ISN_CONCAT:
6345 smsg("%s%4d CONCAT size %lld", pfx, current,
6346 (varnumber_T)(iptr->isn_arg.number));
6347 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006348 case ISN_STRINDEX: smsg("%s%4d STRINDEX", pfx, current); break;
6349 case ISN_STRSLICE: smsg("%s%4d STRSLICE", pfx, current); break;
6350 case ISN_BLOBINDEX: smsg("%s%4d BLOBINDEX", pfx, current); break;
6351 case ISN_BLOBSLICE: smsg("%s%4d BLOBSLICE", pfx, current); break;
6352 case ISN_LISTAPPEND: smsg("%s%4d LISTAPPEND", pfx, current); break;
6353 case ISN_BLOBAPPEND: smsg("%s%4d BLOBAPPEND", pfx, current); break;
6354 case ISN_LISTINDEX: smsg("%s%4d LISTINDEX", pfx, current); break;
6355 case ISN_LISTSLICE: smsg("%s%4d LISTSLICE", pfx, current); break;
6356 case ISN_ANYINDEX: smsg("%s%4d ANYINDEX", pfx, current); break;
6357 case ISN_ANYSLICE: smsg("%s%4d ANYSLICE", pfx, current); break;
6358 case ISN_SLICE: smsg("%s%4d SLICE %lld",
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02006359 pfx, current, iptr->isn_arg.number); break;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02006360 case ISN_GETITEM: smsg("%s%4d ITEM %lld%s", pfx, current,
6361 iptr->isn_arg.getitem.gi_index,
6362 iptr->isn_arg.getitem.gi_with_op ?
6363 " with op" : ""); break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006364 case ISN_MEMBER: smsg("%s%4d MEMBER", pfx, current); break;
6365 case ISN_STRINGMEMBER: smsg("%s%4d MEMBER %s", pfx, current,
6366 iptr->isn_arg.string); break;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02006367 case ISN_CLEARDICT: smsg("%s%4d CLEARDICT", pfx, current); break;
6368 case ISN_USEDICT: smsg("%s%4d USEDICT", pfx, current); break;
6369
Bram Moolenaar4c137212021-04-19 16:48:48 +02006370 case ISN_NEGATENR: smsg("%s%4d NEGATENR", pfx, current); break;
6371
Bram Moolenaar4c137212021-04-19 16:48:48 +02006372 case ISN_CHECKTYPE:
6373 {
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01006374 checktype_T *ct = &iptr->isn_arg.type;
6375 char *tofree;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006376
6377 if (ct->ct_arg_idx == 0)
6378 smsg("%s%4d CHECKTYPE %s stack[%d]", pfx, current,
6379 type_name(ct->ct_type, &tofree),
6380 (int)ct->ct_off);
6381 else
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01006382 smsg("%s%4d CHECKTYPE %s stack[%d] %s %d",
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02006383 pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02006384 type_name(ct->ct_type, &tofree),
6385 (int)ct->ct_off,
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01006386 ct->ct_is_var ? "var": "arg",
Bram Moolenaar4c137212021-04-19 16:48:48 +02006387 (int)ct->ct_arg_idx);
6388 vim_free(tofree);
6389 break;
6390 }
6391 case ISN_CHECKLEN: smsg("%s%4d CHECKLEN %s%d", pfx, current,
6392 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
6393 iptr->isn_arg.checklen.cl_min_len);
6394 break;
6395 case ISN_SETTYPE:
6396 {
6397 char *tofree;
6398
6399 smsg("%s%4d SETTYPE %s", pfx, current,
6400 type_name(iptr->isn_arg.type.ct_type, &tofree));
6401 vim_free(tofree);
6402 break;
6403 }
6404 case ISN_COND2BOOL: smsg("%s%4d COND2BOOL", pfx, current); break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006405 case ISN_2BOOL: if (iptr->isn_arg.tobool.invert)
6406 smsg("%s%4d INVERT %d (!val)", pfx, current,
6407 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006408 else
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006409 smsg("%s%4d 2BOOL %d (!!val)", pfx, current,
6410 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006411 break;
6412 case ISN_2STRING: smsg("%s%4d 2STRING stack[%lld]", pfx, current,
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006413 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006414 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006415 case ISN_2STRING_ANY: smsg("%s%4d 2STRING_ANY stack[%lld]",
6416 pfx, current,
6417 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006418 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006419 case ISN_RANGE: smsg("%s%4d RANGE %s", pfx, current,
6420 iptr->isn_arg.string);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006421 break;
6422 case ISN_PUT:
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01006423 if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE)
Bram Moolenaar4c137212021-04-19 16:48:48 +02006424 smsg("%s%4d PUT %c above range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006425 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006426 else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE)
6427 smsg("%s%4d PUT %c range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006428 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006429 else
6430 smsg("%s%4d PUT %c %ld", pfx, current,
6431 iptr->isn_arg.put.put_regname,
6432 (long)iptr->isn_arg.put.put_lnum);
6433 break;
6434
Bram Moolenaar4c137212021-04-19 16:48:48 +02006435 case ISN_CMDMOD:
6436 {
6437 char_u *buf;
6438 size_t len = produce_cmdmods(
6439 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
6440
6441 buf = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02006442 if (likely(buf != NULL))
Bram Moolenaar4c137212021-04-19 16:48:48 +02006443 {
6444 (void)produce_cmdmods(
6445 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
6446 smsg("%s%4d CMDMOD %s", pfx, current, buf);
6447 vim_free(buf);
6448 }
6449 break;
6450 }
6451 case ISN_CMDMOD_REV: smsg("%s%4d CMDMOD_REV", pfx, current); break;
6452
6453 case ISN_PROF_START:
Bram Moolenaare99d4222021-06-13 14:01:26 +02006454 smsg("%s%4d PROFILE START line %d", pfx, current,
6455 iptr->isn_lnum);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006456 break;
6457
6458 case ISN_PROF_END:
6459 smsg("%s%4d PROFILE END", pfx, current);
6460 break;
6461
Bram Moolenaare99d4222021-06-13 14:01:26 +02006462 case ISN_DEBUG:
Bram Moolenaar8cec9272021-06-23 20:20:53 +02006463 smsg("%s%4d DEBUG line %d-%d varcount %lld", pfx, current,
6464 iptr->isn_arg.debug.dbg_break_lnum + 1,
6465 iptr->isn_lnum,
6466 iptr->isn_arg.debug.dbg_var_names_len);
Bram Moolenaare99d4222021-06-13 14:01:26 +02006467 break;
6468
Bram Moolenaar4c137212021-04-19 16:48:48 +02006469 case ISN_UNPACK: smsg("%s%4d UNPACK %d%s", pfx, current,
6470 iptr->isn_arg.unpack.unp_count,
6471 iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : "");
6472 break;
6473 case ISN_SHUFFLE: smsg("%s%4d SHUFFLE %d up %d", pfx, current,
6474 iptr->isn_arg.shuffle.shfl_item,
6475 iptr->isn_arg.shuffle.shfl_up);
6476 break;
6477 case ISN_DROP: smsg("%s%4d DROP", pfx, current); break;
6478
6479 case ISN_FINISH: // End of list of instructions for ISN_SUBSTITUTE.
6480 return;
6481 }
6482
6483 out_flush(); // output one line at a time
6484 ui_breakcheck();
6485 if (got_int)
6486 break;
6487 }
6488}
6489
6490/*
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02006491 * Handle command line completion for the :disassemble command.
6492 */
6493 void
6494set_context_in_disassemble_cmd(expand_T *xp, char_u *arg)
6495{
6496 char_u *p;
6497
6498 // Default: expand user functions, "debug" and "profile"
6499 xp->xp_context = EXPAND_DISASSEMBLE;
6500 xp->xp_pattern = arg;
6501
6502 // first argument already typed: only user function names
6503 if (*arg != NUL && *(p = skiptowhite(arg)) != NUL)
6504 {
6505 xp->xp_context = EXPAND_USER_FUNC;
6506 xp->xp_pattern = skipwhite(p);
6507 }
6508}
6509
6510/*
6511 * Function given to ExpandGeneric() to obtain the list of :disassemble
6512 * arguments.
6513 */
6514 char_u *
6515get_disassemble_argument(expand_T *xp, int idx)
6516{
6517 if (idx == 0)
6518 return (char_u *)"debug";
6519 if (idx == 1)
6520 return (char_u *)"profile";
6521 return get_user_func_name(xp, idx - 2);
6522}
6523
6524/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01006525 * ":disassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01006526 * We don't really need this at runtime, but we do have tests that require it,
6527 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006528 */
6529 void
6530ex_disassemble(exarg_T *eap)
6531{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01006532 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01006533 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006534 dfunc_T *dfunc;
Bram Moolenaardafef512022-05-21 21:55:55 +01006535 isn_T *instr = NULL; // init to shut up gcc warning
6536 int instr_count = 0; // init to shut up gcc warning
Bram Moolenaar5a01caa2022-05-21 18:56:58 +01006537 compiletype_T compile_type = CT_NONE;
Bram Moolenaare99d4222021-06-13 14:01:26 +02006538
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01006539 ufunc = find_func_by_name(arg, &compile_type);
Bram Moolenaara26b9702020-04-18 19:53:28 +02006540 if (ufunc == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006541 return;
Bram Moolenaare99d4222021-06-13 14:01:26 +02006542 if (func_needs_compiling(ufunc, compile_type)
6543 && compile_def_function(ufunc, FALSE, compile_type, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02006544 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006545 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006546 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006547 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006548 return;
6549 }
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006550 msg((char *)printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006551
6552 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
Bram Moolenaare99d4222021-06-13 14:01:26 +02006553 switch (compile_type)
6554 {
6555 case CT_PROFILE:
Bram Moolenaarf002a412021-01-24 13:34:18 +01006556#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02006557 instr = dfunc->df_instr_prof;
6558 instr_count = dfunc->df_instr_prof_count;
6559 break;
Bram Moolenaarf002a412021-01-24 13:34:18 +01006560#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02006561 // FALLTHROUGH
6562 case CT_NONE:
6563 instr = dfunc->df_instr;
6564 instr_count = dfunc->df_instr_count;
6565 break;
6566 case CT_DEBUG:
6567 instr = dfunc->df_instr_debug;
6568 instr_count = dfunc->df_instr_debug_count;
6569 break;
6570 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006571
Bram Moolenaar4c137212021-04-19 16:48:48 +02006572 list_instructions("", instr, instr_count, ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006573}
6574
6575/*
Bram Moolenaar13106602020-10-04 16:06:05 +02006576 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006577 * list, etc. Mostly like what JavaScript does, except that empty list and
6578 * empty dictionary are FALSE.
6579 */
6580 int
6581tv2bool(typval_T *tv)
6582{
6583 switch (tv->v_type)
6584 {
6585 case VAR_NUMBER:
6586 return tv->vval.v_number != 0;
6587 case VAR_FLOAT:
6588#ifdef FEAT_FLOAT
6589 return tv->vval.v_float != 0.0;
6590#else
6591 break;
6592#endif
6593 case VAR_PARTIAL:
6594 return tv->vval.v_partial != NULL;
6595 case VAR_FUNC:
6596 case VAR_STRING:
6597 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
6598 case VAR_LIST:
6599 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
6600 case VAR_DICT:
6601 return tv->vval.v_dict != NULL
6602 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
6603 case VAR_BOOL:
6604 case VAR_SPECIAL:
6605 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
6606 case VAR_JOB:
6607#ifdef FEAT_JOB_CHANNEL
6608 return tv->vval.v_job != NULL;
6609#else
6610 break;
6611#endif
6612 case VAR_CHANNEL:
6613#ifdef FEAT_JOB_CHANNEL
6614 return tv->vval.v_channel != NULL;
6615#else
6616 break;
6617#endif
6618 case VAR_BLOB:
6619 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
6620 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006621 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006622 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006623 case VAR_INSTR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006624 break;
6625 }
6626 return FALSE;
6627}
6628
Bram Moolenaarea2d4072020-11-12 12:08:51 +01006629 void
6630emsg_using_string_as(typval_T *tv, int as_number)
6631{
6632 semsg(_(as_number ? e_using_string_as_number_str
6633 : e_using_string_as_bool_str),
6634 tv->vval.v_string == NULL
6635 ? (char_u *)"" : tv->vval.v_string);
6636}
6637
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006638/*
6639 * If "tv" is a string give an error and return FAIL.
6640 */
6641 int
6642check_not_string(typval_T *tv)
6643{
6644 if (tv->v_type == VAR_STRING)
6645 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +01006646 emsg_using_string_as(tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006647 clear_tv(tv);
6648 return FAIL;
6649 }
6650 return OK;
6651}
6652
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006653
6654#endif // FEAT_EVAL