blob: 726b2d65f68350ec496d70dc939e4e60a3b76c00 [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 Moolenaar566badc2022-09-18 13:46:08 +0100203 tv->v_lock = 0;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100204 if (list != NULL)
205 ++list->lv_refcount;
206 return OK;
207}
208
209/*
210 * Implementation of ISN_NEWDICT.
211 * Returns FAIL on total failure, MAYBE on error.
212 */
213 static int
214exe_newdict(int count, ectx_T *ectx)
215{
216 dict_T *dict = NULL;
217 dictitem_T *item;
218 char_u *key;
219 int idx;
220 typval_T *tv;
221
222 if (count >= 0)
223 {
224 dict = dict_alloc();
225 if (unlikely(dict == NULL))
226 return FAIL;
227 for (idx = 0; idx < count; ++idx)
228 {
229 // have already checked key type is VAR_STRING
230 tv = STACK_TV_BOT(2 * (idx - count));
231 // check key is unique
Bram Moolenaara9fa8c52023-01-02 18:10:04 +0000232 key = tv->vval.v_string == NULL ? (char_u *)"" : tv->vval.v_string;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100233 item = dict_find(dict, key, -1);
234 if (item != NULL)
235 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +0000236 semsg(_(e_duplicate_key_in_dictionary_str), key);
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100237 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{
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000282 if (ufunc->uf_debug_tick == debug_tick)
283 return;
Bram Moolenaar4f8f5422021-06-20 19:28:14 +0200284
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000285 linenr_T breakpoint;
286
287 ufunc->uf_debug_tick = debug_tick;
288 breakpoint = dbg_find_breakpoint(FALSE, ufunc->uf_name, 0);
289 ufunc->uf_has_breakpoint = breakpoint > 0;
Bram Moolenaar4f8f5422021-06-20 19:28:14 +0200290}
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 Moolenaar0d89d8a2022-12-31 14:01:24 +0000379 * Check "argcount" arguments on the stack against what "ufunc" expects.
380 * "off" is the offset of arguments on the stack.
381 * Return OK or FAIL.
382 */
383 static int
384check_ufunc_arg_types(ufunc_T *ufunc, int argcount, int off, ectx_T *ectx)
385{
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000386 if (ufunc->uf_arg_types == NULL && ufunc->uf_va_type == NULL)
387 return OK;
388
389 typval_T *argv = STACK_TV_BOT(0) - argcount - off;
390
391 // The function can change at runtime, check that the argument
392 // types are correct.
393 for (int i = 0; i < argcount; ++i)
Bram Moolenaar0d89d8a2022-12-31 14:01:24 +0000394 {
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000395 type_T *type = NULL;
Bram Moolenaar0d89d8a2022-12-31 14:01:24 +0000396
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000397 // assume a v:none argument, using the default value, is always OK
398 if (argv[i].v_type == VAR_SPECIAL
399 && argv[i].vval.v_number == VVAL_NONE)
400 continue;
Bram Moolenaar0d89d8a2022-12-31 14:01:24 +0000401
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000402 if (i < ufunc->uf_args.ga_len && ufunc->uf_arg_types != NULL)
403 type = ufunc->uf_arg_types[i];
404 else if (ufunc->uf_va_type != NULL)
405 type = ufunc->uf_va_type->tt_member;
406 if (type != NULL && check_typval_arg_type(type,
407 &argv[i], NULL, i + 1) == FAIL)
408 return FAIL;
Bram Moolenaar0d89d8a2022-12-31 14:01:24 +0000409 }
410 return OK;
411}
412
413/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100414 * Call compiled function "cdf_idx" from compiled code.
Bram Moolenaarab360522021-01-10 14:02:28 +0100415 * This adds a stack frame and sets the instruction pointer to the start of the
416 * called function.
Bram Moolenaar5800c792022-09-22 17:34:01 +0100417 * If "pt_arg" is not NULL use "pt_arg->pt_outer" for ec_outer_ref->or_outer.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100418 *
419 * Stack has:
420 * - current arguments (already there)
421 * - omitted optional argument (default values) added here
422 * - stack frame:
423 * - pointer to calling function
424 * - Index of next instruction in calling function
425 * - previous frame pointer
426 * - reserved space for local variables
427 */
428 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100429call_dfunc(
430 int cdf_idx,
Bram Moolenaar5800c792022-09-22 17:34:01 +0100431 partial_T *pt_arg,
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100432 int argcount_arg,
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100433 ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100434{
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100435 int argcount = argcount_arg;
436 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + cdf_idx;
437 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200438 int did_emsg_before = did_emsg_cumul + did_emsg;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100439 int arg_to_add;
440 int vararg_count = 0;
441 int varcount;
442 int idx;
443 estack_T *entry;
444 funclocal_T *floc = NULL;
Bram Moolenaar648594e2021-07-11 17:55:01 +0200445 int res = OK;
Bram Moolenaar139575d2022-03-15 19:29:30 +0000446 compiletype_T compile_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100447
448 if (dfunc->df_deleted)
449 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100450 // don't use ufunc->uf_name, it may have been freed
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000451 emsg_funcname(e_function_was_deleted_str,
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100452 dfunc->df_name == NULL ? (char_u *)"unknown" : dfunc->df_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100453 return FAIL;
454 }
455
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100456#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +0100457 if (do_profiling == PROF_YES)
458 {
Bram Moolenaar35578162021-08-02 19:10:38 +0200459 if (GA_GROW_OK(&profile_info_ga, 1))
Bram Moolenaar12d26532021-02-19 19:13:21 +0100460 {
461 profinfo_T *info = ((profinfo_T *)profile_info_ga.ga_data)
462 + profile_info_ga.ga_len;
463 ++profile_info_ga.ga_len;
464 CLEAR_POINTER(info);
465 profile_may_start_func(info, ufunc,
466 (((dfunc_T *)def_functions.ga_data)
467 + ectx->ec_dfunc_idx)->df_ufunc);
468 }
Bram Moolenaar12d26532021-02-19 19:13:21 +0100469 }
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100470#endif
471
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200472 // When debugging and using "cont" switches to the not-debugged
473 // instructions, may need to still compile them.
Bram Moolenaar139575d2022-03-15 19:29:30 +0000474 compile_type = get_compile_type(ufunc);
475 if (func_needs_compiling(ufunc, compile_type))
Bram Moolenaar648594e2021-07-11 17:55:01 +0200476 {
Bram Moolenaar139575d2022-03-15 19:29:30 +0000477 res = compile_def_function(ufunc, FALSE, compile_type, NULL);
Bram Moolenaar648594e2021-07-11 17:55:01 +0200478
479 // compile_def_function() may cause def_functions.ga_data to change
480 dfunc = ((dfunc_T *)def_functions.ga_data) + cdf_idx;
481 }
482 if (res == FAIL || INSTRUCTIONS(dfunc) == NULL)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200483 {
484 if (did_emsg_cumul + did_emsg == did_emsg_before)
485 semsg(_(e_function_is_not_compiled_str),
486 printable_func_name(ufunc));
487 return FAIL;
488 }
489
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200490 if (ufunc->uf_va_name != NULL)
491 {
Bram Moolenaarfe270812020-04-11 22:31:27 +0200492 // Need to make a list out of the vararg arguments.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200493 // Stack at time of call with 2 varargs:
494 // normal_arg
495 // optional_arg
496 // vararg_1
497 // vararg_2
Bram Moolenaarfe270812020-04-11 22:31:27 +0200498 // After creating the list:
499 // normal_arg
500 // optional_arg
501 // vararg-list
502 // With missing optional arguments we get:
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200503 // normal_arg
Bram Moolenaarfe270812020-04-11 22:31:27 +0200504 // After creating the list
505 // normal_arg
506 // (space for optional_arg)
507 // vararg-list
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200508 vararg_count = argcount - ufunc->uf_args.ga_len;
509 if (vararg_count < 0)
510 vararg_count = 0;
511 else
512 argcount -= vararg_count;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200513 if (exe_newlist(vararg_count, ectx) == FAIL)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200514 return FAIL;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200515
516 vararg_count = 1;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200517 }
518
Bram Moolenaarfe270812020-04-11 22:31:27 +0200519 arg_to_add = ufunc->uf_args.ga_len - argcount;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200520 if (arg_to_add < 0)
521 {
Matvey Tarasovd14bb1a2022-06-29 13:18:27 +0100522 semsg(NGETTEXT(e_one_argument_too_many, e_nr_arguments_too_many,
523 -arg_to_add), -arg_to_add);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200524 return FAIL;
525 }
Bram Moolenaarcd1cda22022-02-16 21:48:25 +0000526 else if (arg_to_add > ufunc->uf_def_args.ga_len)
527 {
528 int missing = arg_to_add - ufunc->uf_def_args.ga_len;
529
Matvey Tarasovd14bb1a2022-06-29 13:18:27 +0100530 semsg(NGETTEXT(e_one_argument_too_few, e_nr_arguments_too_few,
531 missing), missing);
Bram Moolenaarcd1cda22022-02-16 21:48:25 +0000532 return FAIL;
533 }
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200534
Bram Moolenaar574950d2023-01-03 19:08:50 +0000535 // If this is an object method, the object is just before the arguments.
536 typval_T *obj = STACK_TV_BOT(0) - argcount - vararg_count - 1;
537
Bram Moolenaar0d89d8a2022-12-31 14:01:24 +0000538 // Check the argument types.
539 if (check_ufunc_arg_types(ufunc, argcount, vararg_count, ectx) == FAIL)
540 return FAIL;
541
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200542 // Reserve space for:
543 // - missing arguments
544 // - stack frame
545 // - local variables
546 // - if needed: a counter for number of closures created in
547 // ectx->ec_funcrefs.
548 varcount = dfunc->df_varcount + dfunc->df_has_closure;
Bram Moolenaarb46c0832022-09-15 17:19:37 +0100549 if (GA_GROW_FAILS(&ectx->ec_stack,
550 arg_to_add + STACK_FRAME_SIZE + varcount))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100551 return FAIL;
552
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100553 // If depth of calling is getting too high, don't execute the function.
554 if (funcdepth_increment() == FAIL)
555 return FAIL;
Bram Moolenaare99d4222021-06-13 14:01:26 +0200556 ++ex_nesting_level;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100557
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100558 // Only make a copy of funclocal if it contains something to restore.
Bram Moolenaar4c137212021-04-19 16:48:48 +0200559 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100560 {
561 floc = ALLOC_ONE(funclocal_T);
562 if (floc == NULL)
563 return FAIL;
Bram Moolenaar4c137212021-04-19 16:48:48 +0200564 *floc = ectx->ec_funclocal;
565 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100566 }
567
Bram Moolenaarfe270812020-04-11 22:31:27 +0200568 // Move the vararg-list to below the missing optional arguments.
569 if (vararg_count > 0 && arg_to_add > 0)
570 *STACK_TV_BOT(arg_to_add - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100571
572 // Reserve space for omitted optional arguments, filled in soon.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200573 for (idx = 0; idx < arg_to_add; ++idx)
Bram Moolenaarfe270812020-04-11 22:31:27 +0200574 STACK_TV_BOT(idx - vararg_count)->v_type = VAR_UNKNOWN;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200575 ectx->ec_stack.ga_len += arg_to_add;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100576
577 // Store current execution state in stack frame for ISN_RETURN.
Bram Moolenaar0186e582021-01-10 18:33:11 +0100578 STACK_TV_BOT(STACK_FRAME_FUNC_OFF)->vval.v_number = ectx->ec_dfunc_idx;
579 STACK_TV_BOT(STACK_FRAME_IIDX_OFF)->vval.v_number = ectx->ec_iidx;
Bram Moolenaar5930ddc2021-04-26 20:32:59 +0200580 STACK_TV_BOT(STACK_FRAME_INSTR_OFF)->vval.v_string = (void *)ectx->ec_instr;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200581 STACK_TV_BOT(STACK_FRAME_OUTER_OFF)->vval.v_string =
582 (void *)ectx->ec_outer_ref;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100583 STACK_TV_BOT(STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string = (void *)floc;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100584 STACK_TV_BOT(STACK_FRAME_IDX_OFF)->vval.v_number = ectx->ec_frame_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200585 ectx->ec_frame_idx = ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100586
Bram Moolenaar5800c792022-09-22 17:34:01 +0100587 // Initialize all local variables to number zero. Also initialize the
588 // variable that counts how many closures were created. This is used in
589 // handle_closure_in_use().
590 int initcount = dfunc->df_varcount + (dfunc->df_has_closure ? 1 : 0);
591 for (idx = 0; idx < initcount; ++idx)
Bram Moolenaar5cd64792021-12-25 18:23:24 +0000592 {
593 typval_T *tv = STACK_TV_BOT(STACK_FRAME_SIZE + idx);
594
595 tv->v_type = VAR_NUMBER;
596 tv->vval.v_number = 0;
597 }
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200598 ectx->ec_stack.ga_len += STACK_FRAME_SIZE + varcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100599
Bram Moolenaar574950d2023-01-03 19:08:50 +0000600 // For an object method move the object from just before the arguments to
601 // the first local variable.
602 if (ufunc->uf_flags & FC_OBJECT)
603 {
604 *STACK_TV_VAR(0) = *obj;
605 obj->v_type = VAR_UNKNOWN;
606 }
607
Bram Moolenaar5800c792022-09-22 17:34:01 +0100608 partial_T *pt = pt_arg != NULL ? pt_arg : ufunc->uf_partial;
609 if (pt != NULL || (ufunc->uf_flags & FC_CLOSURE))
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100610 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200611 outer_ref_T *ref = ALLOC_CLEAR_ONE(outer_ref_T);
Bram Moolenaar0186e582021-01-10 18:33:11 +0100612
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200613 if (ref == NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +0100614 return FAIL;
615 if (pt != NULL)
616 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +0000617 ref->or_outer = get_pt_outer(pt);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200618 ++pt->pt_refcount;
619 ref->or_partial = pt;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100620 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100621 else
622 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200623 ref->or_outer = ALLOC_CLEAR_ONE(outer_T);
Dominique Pelle5a9e5842021-07-24 19:32:12 +0200624 if (unlikely(ref->or_outer == NULL))
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200625 {
626 vim_free(ref);
627 return FAIL;
628 }
629 ref->or_outer_allocated = TRUE;
630 ref->or_outer->out_stack = &ectx->ec_stack;
631 ref->or_outer->out_frame_idx = ectx->ec_frame_idx;
632 if (ectx->ec_outer_ref != NULL)
633 ref->or_outer->out_up = ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100634 }
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200635 ectx->ec_outer_ref = ref;
Bram Moolenaarab360522021-01-10 14:02:28 +0100636 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100637 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200638 ectx->ec_outer_ref = NULL;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100639
Bram Moolenaarc970e422021-03-17 15:03:04 +0100640 ++ufunc->uf_calls;
641
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100642 // Set execution state to the start of the called function.
643 ectx->ec_dfunc_idx = cdf_idx;
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100644 ectx->ec_instr = INSTRUCTIONS(dfunc);
Bram Moolenaarb2049902021-01-24 12:53:53 +0100645 entry = estack_push_ufunc(ufunc, 1);
Bram Moolenaarc620c052020-07-08 15:16:19 +0200646 if (entry != NULL)
647 {
648 // Set the script context to the script where the function was defined.
Bram Moolenaarc70fe462021-04-17 17:59:19 +0200649 // Save the current context so it can be restored on return.
650 entry->es_save_sctx = current_sctx;
651 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200652 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100653
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +0200654 // Start execution at the first instruction.
655 ectx->ec_iidx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100656
657 return OK;
658}
659
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000660// Double linked list of funcstack_T in use.
661static funcstack_T *first_funcstack = NULL;
662
663 static void
664add_funcstack_to_list(funcstack_T *funcstack)
665{
666 // Link in list of funcstacks.
667 if (first_funcstack != NULL)
668 first_funcstack->fs_prev = funcstack;
669 funcstack->fs_next = first_funcstack;
670 funcstack->fs_prev = NULL;
671 first_funcstack = funcstack;
672}
673
674 static void
675remove_funcstack_from_list(funcstack_T *funcstack)
676{
677 if (funcstack->fs_prev == NULL)
678 first_funcstack = funcstack->fs_next;
679 else
680 funcstack->fs_prev->fs_next = funcstack->fs_next;
681 if (funcstack->fs_next != NULL)
682 funcstack->fs_next->fs_prev = funcstack->fs_prev;
683}
684
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100685/*
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200686 * Used when returning from a function: Check if any closure is still
687 * referenced. If so then move the arguments and variables to a separate piece
688 * of stack to be used when the closure is called.
689 * When "free_arguments" is TRUE the arguments are to be freed.
690 * Returns FAIL when out of memory.
691 */
692 static int
693handle_closure_in_use(ectx_T *ectx, int free_arguments)
694{
695 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
696 + ectx->ec_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200697 int argcount;
698 int top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200699 int idx;
700 typval_T *tv;
701 int closure_in_use = FALSE;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200702 garray_T *gap = &ectx->ec_funcrefs;
703 varnumber_T closure_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200704
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200705 if (dfunc->df_ufunc == NULL)
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200706 return OK; // function was freed
707 if (dfunc->df_has_closure == 0)
708 return OK; // no closures
709 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + dfunc->df_varcount);
710 closure_count = tv->vval.v_number;
711 if (closure_count == 0)
712 return OK; // no funcrefs created
713
Bram Moolenaar8fa745e2022-09-16 19:04:24 +0100714 // Compute "top": the first entry in the stack used by the function.
715 // This is the first argument (after that comes the stack frame and then
716 // the local variables).
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200717 argcount = ufunc_argcount(dfunc->df_ufunc);
718 top = ectx->ec_frame_idx - argcount;
719
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200720 // Check if any created closure is still in use.
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200721 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200722 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200723 partial_T *pt;
724 int off = gap->ga_len - closure_count + idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200725
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200726 if (off < 0)
727 continue; // count is off or already done
728 pt = ((partial_T **)gap->ga_data)[off];
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200729 if (pt->pt_refcount > 1)
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200730 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200731 int refcount = pt->pt_refcount;
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200732 int i;
733
Dominique Pelleaf4a61a2021-12-27 17:21:41 +0000734 // A Reference in a local variable doesn't count, it gets
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200735 // unreferenced on return.
736 for (i = 0; i < dfunc->df_varcount; ++i)
737 {
738 typval_T *stv = STACK_TV(ectx->ec_frame_idx
739 + STACK_FRAME_SIZE + i);
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200740 if (stv->v_type == VAR_PARTIAL && pt == stv->vval.v_partial)
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200741 --refcount;
742 }
743 if (refcount > 1)
744 {
745 closure_in_use = TRUE;
746 break;
747 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200748 }
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200749 }
750
751 if (closure_in_use)
752 {
753 funcstack_T *funcstack = ALLOC_CLEAR_ONE(funcstack_T);
754 typval_T *stack;
755
756 // A closure is using the arguments and/or local variables.
757 // Move them to the called function.
758 if (funcstack == NULL)
759 return FAIL;
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000760
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200761 funcstack->fs_var_offset = argcount + STACK_FRAME_SIZE;
Bram Moolenaar1936c762022-09-28 15:19:10 +0100762 funcstack->fs_ga.ga_len = funcstack->fs_var_offset
763 + dfunc->df_varcount;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200764 stack = ALLOC_CLEAR_MULT(typval_T, funcstack->fs_ga.ga_len);
765 funcstack->fs_ga.ga_data = stack;
766 if (stack == NULL)
767 {
768 vim_free(funcstack);
769 return FAIL;
770 }
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000771 add_funcstack_to_list(funcstack);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200772
773 // Move or copy the arguments.
774 for (idx = 0; idx < argcount; ++idx)
775 {
776 tv = STACK_TV(top + idx);
777 if (free_arguments)
778 {
779 *(stack + idx) = *tv;
780 tv->v_type = VAR_UNKNOWN;
781 }
782 else
783 copy_tv(tv, stack + idx);
784 }
Bram Moolenaar8fa745e2022-09-16 19:04:24 +0100785 // Skip the stack frame.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200786 // Move the local variables.
787 for (idx = 0; idx < dfunc->df_varcount; ++idx)
788 {
789 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + idx);
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200790
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200791 // A partial created for a local function, that is also used as a
792 // local variable, has a reference count for the variable, thus
793 // will never go down to zero. When all these refcounts are one
794 // then the funcstack is unused. We need to count how many we have
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000795 // so we know when to check.
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200796 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL)
797 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200798 int i;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200799
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200800 for (i = 0; i < closure_count; ++i)
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200801 if (tv->vval.v_partial == ((partial_T **)gap->ga_data)[
802 gap->ga_len - closure_count + i])
803 ++funcstack->fs_min_refcount;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200804 }
805
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200806 *(stack + funcstack->fs_var_offset + idx) = *tv;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200807 tv->v_type = VAR_UNKNOWN;
808 }
809
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200810 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200811 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200812 partial_T *pt = ((partial_T **)gap->ga_data)[gap->ga_len
813 - closure_count + idx];
814 if (pt->pt_refcount > 1)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200815 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200816 ++funcstack->fs_refcount;
817 pt->pt_funcstack = funcstack;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100818 pt->pt_outer.out_stack = &funcstack->fs_ga;
819 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx - top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200820 }
821 }
822 }
823
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200824 for (idx = 0; idx < closure_count; ++idx)
825 partial_unref(((partial_T **)gap->ga_data)[gap->ga_len
826 - closure_count + idx]);
827 gap->ga_len -= closure_count;
828 if (gap->ga_len == 0)
829 ga_clear(gap);
830
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200831 return OK;
832}
833
834/*
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200835 * Called when a partial is freed or its reference count goes down to one. The
836 * funcstack may be the only reference to the partials in the local variables.
837 * Go over all of them, the funcref and can be freed if all partials
838 * referencing the funcstack have a reference count of one.
Bram Moolenaaracd6b992022-09-17 16:27:39 +0100839 * Returns TRUE if the funcstack is freed, the partial referencing it will then
840 * also have been freed.
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200841 */
Bram Moolenaaracd6b992022-09-17 16:27:39 +0100842 int
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200843funcstack_check_refcount(funcstack_T *funcstack)
844{
Bram Moolenaaracd6b992022-09-17 16:27:39 +0100845 int i;
846 garray_T *gap = &funcstack->fs_ga;
847 int done = 0;
848 typval_T *stack;
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200849
850 if (funcstack->fs_refcount > funcstack->fs_min_refcount)
Bram Moolenaaracd6b992022-09-17 16:27:39 +0100851 return FALSE;
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200852 for (i = funcstack->fs_var_offset; i < gap->ga_len; ++i)
853 {
854 typval_T *tv = ((typval_T *)gap->ga_data) + i;
855
856 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL
857 && tv->vval.v_partial->pt_funcstack == funcstack
858 && tv->vval.v_partial->pt_refcount == 1)
859 ++done;
860 }
Bram Moolenaaracd6b992022-09-17 16:27:39 +0100861 if (done != funcstack->fs_min_refcount)
862 return FALSE;
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200863
Bram Moolenaaracd6b992022-09-17 16:27:39 +0100864 stack = gap->ga_data;
865
866 // All partials referencing the funcstack have a reference count of
867 // one, thus the funcstack is no longer of use.
868 for (i = 0; i < gap->ga_len; ++i)
869 clear_tv(stack + i);
870 vim_free(stack);
871 remove_funcstack_from_list(funcstack);
872 vim_free(funcstack);
873
874 return TRUE;
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200875}
876
877/*
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000878 * For garbage collecting: set references in all variables referenced by
879 * all funcstacks.
880 */
881 int
882set_ref_in_funcstacks(int copyID)
883{
884 funcstack_T *funcstack;
885
886 for (funcstack = first_funcstack; funcstack != NULL;
887 funcstack = funcstack->fs_next)
888 {
889 typval_T *stack = funcstack->fs_ga.ga_data;
890 int i;
891
892 for (i = 0; i < funcstack->fs_ga.ga_len; ++i)
893 if (set_ref_in_item(stack + i, copyID, NULL, NULL))
894 return TRUE; // abort
895 }
896 return FALSE;
897}
898
Bram Moolenaar806a2732022-09-04 15:40:36 +0100899// Ugly static to avoid passing the execution context around through many
900// layers.
901static ectx_T *current_ectx = NULL;
902
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000903/*
Bram Moolenaar806a2732022-09-04 15:40:36 +0100904 * Return TRUE if currently executing a :def function.
905 * Can be used by builtin functions only.
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100906 */
Bram Moolenaar806a2732022-09-04 15:40:36 +0100907 int
908in_def_function(void)
909{
910 return current_ectx != NULL;
911}
912
913/*
Bram Moolenaar98aff652022-09-06 21:02:35 +0100914 * Clear "current_ectx" and return the previous value. To be used when calling
915 * a user function.
916 */
917 ectx_T *
dundargocc57b5bc2022-11-02 13:30:51 +0000918clear_current_ectx(void)
Bram Moolenaar98aff652022-09-06 21:02:35 +0100919{
920 ectx_T *r = current_ectx;
921
922 current_ectx = NULL;
923 return r;
924}
925
926 void
927restore_current_ectx(ectx_T *ectx)
928{
929 if (current_ectx != NULL)
930 iemsg("Restoring current_ectx while it is not NULL");
931 current_ectx = ectx;
932}
933
934/*
Bram Moolenaar806a2732022-09-04 15:40:36 +0100935 * Add an entry for a deferred function call to the currently executing
936 * function.
937 * Return the list or NULL when failed.
938 */
939 static list_T *
940add_defer_item(int var_idx, int argcount, ectx_T *ectx)
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100941{
942 typval_T *defer_tv = STACK_TV_VAR(var_idx);
943 list_T *defer_l;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100944 list_T *l;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100945 typval_T listval;
946
947 if (defer_tv->v_type != VAR_LIST)
948 {
Bram Moolenaara5348f22022-09-04 11:42:22 +0100949 // first time, allocate the list
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100950 if (rettv_list_alloc(defer_tv) == FAIL)
Bram Moolenaar806a2732022-09-04 15:40:36 +0100951 return NULL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100952 }
953 defer_l = defer_tv->vval.v_list;
954
955 l = list_alloc_with_items(argcount + 1);
956 if (l == NULL)
Bram Moolenaar806a2732022-09-04 15:40:36 +0100957 return NULL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100958 listval.v_type = VAR_LIST;
959 listval.vval.v_list = l;
960 listval.v_lock = 0;
Bram Moolenaara5348f22022-09-04 11:42:22 +0100961 if (list_insert_tv(defer_l, &listval, defer_l->lv_first) == FAIL)
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100962 {
963 vim_free(l);
Bram Moolenaar806a2732022-09-04 15:40:36 +0100964 return NULL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100965 }
966
Bram Moolenaar806a2732022-09-04 15:40:36 +0100967 return l;
968}
969
970/*
971 * Handle ISN_DEFER. Stack has a function reference and "argcount" arguments.
972 * The local variable that lists deferred functions is "var_idx".
973 * Returns OK or FAIL.
974 */
975 static int
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +0000976defer_command(int var_idx, int has_obj, int argcount, ectx_T *ectx)
Bram Moolenaar806a2732022-09-04 15:40:36 +0100977{
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +0000978 int obj_off = has_obj ? 1 : 0;
979 list_T *l = add_defer_item(var_idx, argcount + obj_off, ectx);
Bram Moolenaar806a2732022-09-04 15:40:36 +0100980 int i;
981 typval_T *func_tv;
982
983 if (l == NULL)
984 return FAIL;
985
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100986 func_tv = STACK_TV_BOT(-argcount - 1);
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +0000987 if (has_obj ? func_tv->v_type != VAR_PARTIAL : func_tv->v_type != VAR_FUNC)
Bram Moolenaarf5fec052022-09-11 11:49:22 +0100988 {
989 semsg(_(e_expected_str_but_got_str),
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +0000990 has_obj ? "partial" : "function",
Bram Moolenaarf5fec052022-09-11 11:49:22 +0100991 vartype_name(func_tv->v_type));
992 return FAIL;
993 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100994 list_set_item(l, 0, func_tv);
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +0000995 if (has_obj)
996 list_set_item(l, 1, STACK_TV_BOT(-argcount - 2));
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100997
Bram Moolenaarf5fec052022-09-11 11:49:22 +0100998 for (i = 0; i < argcount; ++i)
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +0000999 list_set_item(l, i + 1 + obj_off, STACK_TV_BOT(-argcount + i));
1000 ectx->ec_stack.ga_len -= argcount + 1 + obj_off;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001001 return OK;
1002}
1003
1004/*
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001005 * Add a deferred call for "name" with arguments "argvars[argcount]".
Bram Moolenaar806a2732022-09-04 15:40:36 +01001006 * Consumes "name", also on failure.
1007 * Only to be called when in_def_function() returns TRUE.
1008 */
1009 int
1010add_defer_function(char_u *name, int argcount, typval_T *argvars)
1011{
1012 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1013 + current_ectx->ec_dfunc_idx;
1014 list_T *l;
1015 typval_T func_tv;
1016 int i;
1017
1018 if (dfunc->df_defer_var_idx == 0)
1019 {
1020 iemsg("df_defer_var_idx is zero");
Bram Moolenaar31ea6bf2022-09-05 10:47:13 +01001021 vim_free(name);
Bram Moolenaar806a2732022-09-04 15:40:36 +01001022 return FAIL;
1023 }
Bram Moolenaar806a2732022-09-04 15:40:36 +01001024
Bram Moolenaarf5fec052022-09-11 11:49:22 +01001025 l = add_defer_item(dfunc->df_defer_var_idx - 1, argcount, current_ectx);
Bram Moolenaar806a2732022-09-04 15:40:36 +01001026 if (l == NULL)
1027 {
Bram Moolenaar31ea6bf2022-09-05 10:47:13 +01001028 vim_free(name);
Bram Moolenaar806a2732022-09-04 15:40:36 +01001029 return FAIL;
1030 }
1031
Bram Moolenaar31ea6bf2022-09-05 10:47:13 +01001032 func_tv.v_type = VAR_FUNC;
1033 func_tv.v_lock = 0;
1034 func_tv.vval.v_string = name;
Bram Moolenaar806a2732022-09-04 15:40:36 +01001035 list_set_item(l, 0, &func_tv);
Bram Moolenaar31ea6bf2022-09-05 10:47:13 +01001036
Bram Moolenaar806a2732022-09-04 15:40:36 +01001037 for (i = 0; i < argcount; ++i)
1038 list_set_item(l, i + 1, argvars + i);
1039 return OK;
1040}
1041
1042/*
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001043 * Invoked when returning from a function: Invoke any deferred calls.
1044 */
1045 static void
1046invoke_defer_funcs(ectx_T *ectx)
1047{
1048 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1049 + ectx->ec_dfunc_idx;
1050 typval_T *defer_tv = STACK_TV_VAR(dfunc->df_defer_var_idx - 1);
1051 listitem_T *li;
1052
1053 if (defer_tv->v_type != VAR_LIST)
1054 return; // no function added
1055 for (li = defer_tv->vval.v_list->lv_first; li != NULL; li = li->li_next)
1056 {
1057 list_T *l = li->li_tv.vval.v_list;
1058 typval_T rettv;
1059 typval_T argvars[MAX_FUNC_ARGS];
1060 int i;
1061 listitem_T *arg_li = l->lv_first;
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001062 typval_T *functv = &l->lv_first->li_tv;
1063 int obj_off = functv->v_type == VAR_PARTIAL ? 1 : 0;
1064 int argcount = l->lv_len - 1 - obj_off;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001065
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001066 if (obj_off == 1)
1067 arg_li = arg_li->li_next; // second list item is the object
1068 for (i = 0; i < argcount; ++i)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001069 {
1070 arg_li = arg_li->li_next;
1071 argvars[i] = arg_li->li_tv;
1072 }
1073
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001074 funcexe_T funcexe;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001075 CLEAR_FIELD(funcexe);
1076 funcexe.fe_evaluate = TRUE;
1077 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001078 if (functv->v_type == VAR_PARTIAL)
1079 {
1080 funcexe.fe_partial = functv->vval.v_partial;
1081 funcexe.fe_object = l->lv_first->li_next->li_tv.vval.v_object;
1082 if (funcexe.fe_object != NULL)
1083 ++funcexe.fe_object->obj_refcount;
1084 }
1085 (void)call_func(functv->vval.v_string, -1,
1086 &rettv, argcount, argvars, &funcexe);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001087 clear_tv(&rettv);
1088 }
1089}
1090
1091/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001092 * Return from the current function.
1093 */
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001094 static int
Bram Moolenaar4c137212021-04-19 16:48:48 +02001095func_return(ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001096{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001097 int idx;
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01001098 int ret_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001099 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1100 + ectx->ec_dfunc_idx;
1101 int argcount = ufunc_argcount(dfunc->df_ufunc);
Bram Moolenaarc620c052020-07-08 15:16:19 +02001102 estack_T *entry;
Bram Moolenaar12d26532021-02-19 19:13:21 +01001103 int prev_dfunc_idx = STACK_TV(ectx->ec_frame_idx
1104 + STACK_FRAME_FUNC_OFF)->vval.v_number;
Bram Moolenaarb06b50d2021-04-26 21:39:25 +02001105 funclocal_T *floc;
1106#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01001107 dfunc_T *prev_dfunc = ((dfunc_T *)def_functions.ga_data)
1108 + prev_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001109
Bram Moolenaar12d26532021-02-19 19:13:21 +01001110 if (do_profiling == PROF_YES)
1111 {
1112 ufunc_T *caller = prev_dfunc->df_ufunc;
1113
1114 if (dfunc->df_ufunc->uf_profiling
1115 || (caller != NULL && caller->uf_profiling))
1116 {
1117 profile_may_end_func(((profinfo_T *)profile_info_ga.ga_data)
1118 + profile_info_ga.ga_len - 1, dfunc->df_ufunc, caller);
1119 --profile_info_ga.ga_len;
1120 }
1121 }
1122#endif
Bram Moolenaar919c12c2021-12-14 14:29:16 +00001123
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001124 if (dfunc->df_defer_var_idx > 0)
1125 invoke_defer_funcs(ectx);
1126
Bram Moolenaar919c12c2021-12-14 14:29:16 +00001127 // No check for uf_refcount being zero, cannot think of a way that would
1128 // happen.
Bram Moolenaarc970e422021-03-17 15:03:04 +01001129 --dfunc->df_ufunc->uf_calls;
1130
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001131 // execution context goes one level up
Bram Moolenaarc620c052020-07-08 15:16:19 +02001132 entry = estack_pop();
1133 if (entry != NULL)
Bram Moolenaarc70fe462021-04-17 17:59:19 +02001134 current_sctx = entry->es_save_sctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001135
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001136 if (handle_closure_in_use(ectx, TRUE) == FAIL)
1137 return FAIL;
1138
Bram Moolenaar574950d2023-01-03 19:08:50 +00001139 // Clear the arguments. If this was an object method also clear the
1140 // object, it is just before the arguments.
1141 int top = ectx->ec_frame_idx - argcount;
1142 if (dfunc->df_ufunc->uf_flags & FC_OBJECT)
1143 --top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001144 for (idx = top; idx < ectx->ec_frame_idx; ++idx)
1145 clear_tv(STACK_TV(idx));
1146
1147 // Clear local variables and temp values, but not the return value.
1148 for (idx = ectx->ec_frame_idx + STACK_FRAME_SIZE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001149 idx < ectx->ec_stack.ga_len - 1; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001150 clear_tv(STACK_TV(idx));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001151
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01001152 // The return value should be on top of the stack. However, when aborting
1153 // it may not be there and ec_frame_idx is the top of the stack.
1154 ret_idx = ectx->ec_stack.ga_len - 1;
Bram Moolenaar0186e582021-01-10 18:33:11 +01001155 if (ret_idx == ectx->ec_frame_idx + STACK_FRAME_IDX_OFF)
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01001156 ret_idx = 0;
1157
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001158 if (ectx->ec_outer_ref != NULL)
1159 {
1160 if (ectx->ec_outer_ref->or_outer_allocated)
1161 vim_free(ectx->ec_outer_ref->or_outer);
1162 partial_unref(ectx->ec_outer_ref->or_partial);
1163 vim_free(ectx->ec_outer_ref);
1164 }
Bram Moolenaar0186e582021-01-10 18:33:11 +01001165
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001166 // Restore the previous frame.
Bram Moolenaar12d26532021-02-19 19:13:21 +01001167 ectx->ec_dfunc_idx = prev_dfunc_idx;
Bram Moolenaar0186e582021-01-10 18:33:11 +01001168 ectx->ec_iidx = STACK_TV(ectx->ec_frame_idx
1169 + STACK_FRAME_IIDX_OFF)->vval.v_number;
Bram Moolenaar5930ddc2021-04-26 20:32:59 +02001170 ectx->ec_instr = (void *)STACK_TV(ectx->ec_frame_idx
1171 + STACK_FRAME_INSTR_OFF)->vval.v_string;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001172 ectx->ec_outer_ref = (void *)STACK_TV(ectx->ec_frame_idx
Bram Moolenaar0186e582021-01-10 18:33:11 +01001173 + STACK_FRAME_OUTER_OFF)->vval.v_string;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001174 floc = (void *)STACK_TV(ectx->ec_frame_idx
1175 + STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string;
Bram Moolenaar5366e1a2020-10-01 13:01:34 +02001176 // restoring ec_frame_idx must be last
Bram Moolenaar0186e582021-01-10 18:33:11 +01001177 ectx->ec_frame_idx = STACK_TV(ectx->ec_frame_idx
1178 + STACK_FRAME_IDX_OFF)->vval.v_number;
Bram Moolenaard386e922021-04-25 14:48:49 +02001179
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001180 if (floc == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02001181 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001182 else
1183 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02001184 ectx->ec_funclocal = *floc;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001185 vim_free(floc);
1186 }
1187
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01001188 if (ret_idx > 0)
1189 {
1190 // Reset the stack to the position before the call, with a spot for the
1191 // return value, moved there from above the frame.
1192 ectx->ec_stack.ga_len = top + 1;
1193 *STACK_TV_BOT(-1) = *STACK_TV(ret_idx);
1194 }
1195 else
1196 // Reset the stack to the position before the call.
1197 ectx->ec_stack.ga_len = top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001198
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001199 funcdepth_decrement();
Bram Moolenaare99d4222021-06-13 14:01:26 +02001200 --ex_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001201 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001202}
1203
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001204/*
1205 * Prepare arguments and rettv for calling a builtin or user function.
1206 */
1207 static int
1208call_prepare(int argcount, typval_T *argvars, ectx_T *ectx)
1209{
1210 int idx;
1211 typval_T *tv;
1212
1213 // Move arguments from bottom of the stack to argvars[] and add terminator.
1214 for (idx = 0; idx < argcount; ++idx)
1215 argvars[idx] = *STACK_TV_BOT(idx - argcount);
1216 argvars[argcount].v_type = VAR_UNKNOWN;
1217
1218 // Result replaces the arguments on the stack.
1219 if (argcount > 0)
1220 ectx->ec_stack.ga_len -= argcount - 1;
Bram Moolenaar35578162021-08-02 19:10:38 +02001221 else if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001222 return FAIL;
1223 else
1224 ++ectx->ec_stack.ga_len;
1225
1226 // Default return value is zero.
1227 tv = STACK_TV_BOT(-1);
1228 tv->v_type = VAR_NUMBER;
1229 tv->vval.v_number = 0;
Bram Moolenaar24565cf2022-03-28 18:16:52 +01001230 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001231
1232 return OK;
1233}
1234
1235/*
1236 * Call a builtin function by index.
1237 */
1238 static int
1239call_bfunc(int func_idx, int argcount, ectx_T *ectx)
1240{
1241 typval_T argvars[MAX_FUNC_ARGS];
1242 int idx;
Bram Moolenaar171fb922020-10-28 16:54:47 +01001243 int did_emsg_before = did_emsg;
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001244 ectx_T *prev_ectx = current_ectx;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001245 char *save_func_name = ectx->ec_where.wt_func_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001246
1247 if (call_prepare(argcount, argvars, ectx) == FAIL)
1248 return FAIL;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001249 ectx->ec_where.wt_func_name = internal_func_name(func_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001250
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001251 // Call the builtin function. Set "current_ectx" so that when it
1252 // recursively invokes call_def_function() a closure context can be set.
1253 current_ectx = ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001254 call_internal_func_by_idx(func_idx, argvars, STACK_TV_BOT(-1));
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001255 current_ectx = prev_ectx;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001256 ectx->ec_where.wt_func_name = save_func_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001257
1258 // Clear the arguments.
1259 for (idx = 0; idx < argcount; ++idx)
1260 clear_tv(&argvars[idx]);
Bram Moolenaar015f4262020-05-05 21:25:22 +02001261
Bram Moolenaar57f799e2020-12-12 20:42:19 +01001262 if (did_emsg > did_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +02001263 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001264 return OK;
1265}
1266
1267/*
1268 * Execute a user defined function.
Bram Moolenaarab360522021-01-10 14:02:28 +01001269 * If the function is compiled this will add a stack frame and set the
1270 * instruction pointer at the start of the function.
1271 * Otherwise the function is called here.
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001272 * If "pt" is not null use "pt->pt_outer" for ec_outer_ref->or_outer.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001273 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001274 */
1275 static int
Bram Moolenaar0186e582021-01-10 18:33:11 +01001276call_ufunc(
1277 ufunc_T *ufunc,
1278 partial_T *pt,
1279 int argcount,
1280 ectx_T *ectx,
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001281 isn_T *iptr,
1282 dict_T *selfdict)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001283{
1284 typval_T argvars[MAX_FUNC_ARGS];
1285 funcexe_T funcexe;
1286 int error;
1287 int idx;
Bram Moolenaar28ee8922020-10-28 20:20:00 +01001288 int did_emsg_before = did_emsg;
Bram Moolenaar139575d2022-03-15 19:29:30 +00001289 compiletype_T compile_type = get_compile_type(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001290
Bram Moolenaare99d4222021-06-13 14:01:26 +02001291 if (func_needs_compiling(ufunc, compile_type)
1292 && compile_def_function(ufunc, FALSE, compile_type, NULL)
1293 == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02001294 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001295 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001296 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +01001297 error = check_user_func_argcount(ufunc, argcount);
Bram Moolenaar50824712020-12-20 21:10:17 +01001298 if (error != FCERR_UNKNOWN)
1299 {
1300 if (error == FCERR_TOOMANY)
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001301 semsg(_(e_too_many_arguments_for_function_str),
1302 printable_func_name(ufunc));
Bram Moolenaar50824712020-12-20 21:10:17 +01001303 else
Bram Moolenaare1242042021-12-16 20:56:57 +00001304 semsg(_(e_not_enough_arguments_for_function_str),
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001305 printable_func_name(ufunc));
Bram Moolenaar50824712020-12-20 21:10:17 +01001306 return FAIL;
1307 }
1308
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001309 // The function has been compiled, can call it quickly. For a function
1310 // that was defined later: we can call it directly next time.
1311 if (iptr != NULL)
1312 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001313 delete_instr(iptr);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001314 iptr->isn_type = ISN_DCALL;
1315 iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1316 iptr->isn_arg.dfunc.cdf_argcount = argcount;
1317 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001318 return call_dfunc(ufunc->uf_dfunc_idx, pt, argcount, ectx);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001319 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001320
1321 if (call_prepare(argcount, argvars, ectx) == FAIL)
1322 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +02001323 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00001324 funcexe.fe_evaluate = TRUE;
1325 funcexe.fe_selfdict = selfdict != NULL ? selfdict : dict_stack_get_dict();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001326
1327 // Call the user function. Result goes in last position on the stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001328 error = call_user_func_check(ufunc, argcount, argvars,
Bram Moolenaar851f86b2021-12-13 14:26:44 +00001329 STACK_TV_BOT(-1), &funcexe, funcexe.fe_selfdict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001330
1331 // Clear the arguments.
1332 for (idx = 0; idx < argcount; ++idx)
1333 clear_tv(&argvars[idx]);
1334
1335 if (error != FCERR_NONE)
1336 {
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01001337 user_func_error(error, printable_func_name(ufunc),
1338 funcexe.fe_found_var);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001339 return FAIL;
1340 }
Bram Moolenaar28ee8922020-10-28 20:20:00 +01001341 if (did_emsg > did_emsg_before)
Bram Moolenaared677f52020-08-12 16:38:10 +02001342 // Error other than from calling the function itself.
1343 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001344 return OK;
1345}
1346
1347/*
Bram Moolenaara91a7132021-03-25 21:12:15 +01001348 * If command modifiers were applied restore them.
1349 */
1350 static void
1351may_restore_cmdmod(funclocal_T *funclocal)
1352{
1353 if (funclocal->floc_restore_cmdmod)
1354 {
1355 cmdmod.cmod_filter_regmatch.regprog = NULL;
1356 undo_cmdmod(&cmdmod);
1357 cmdmod = funclocal->floc_save_cmdmod;
1358 funclocal->floc_restore_cmdmod = FALSE;
1359 }
1360}
1361
1362/*
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001363 * Return TRUE if an error was given (not caught in try/catch) or CTRL-C was
1364 * pressed.
Bram Moolenaara1773442020-08-12 15:21:22 +02001365 */
1366 static int
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001367vim9_aborting(int prev_uncaught_emsg)
Bram Moolenaara1773442020-08-12 15:21:22 +02001368{
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001369 return uncaught_emsg > prev_uncaught_emsg || got_int || did_throw;
Bram Moolenaara1773442020-08-12 15:21:22 +02001370}
1371
1372/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001373 * Execute a function by "name".
1374 * This can be a builtin function or a user function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001375 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001376 * Returns FAIL if not found without an error message.
1377 */
1378 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001379call_by_name(
1380 char_u *name,
1381 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001382 ectx_T *ectx,
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001383 isn_T *iptr,
1384 dict_T *selfdict)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001385{
1386 ufunc_T *ufunc;
1387
1388 if (builtin_function(name, -1))
1389 {
1390 int func_idx = find_internal_func(name);
1391
Bram Moolenaar1983f1a2022-02-28 20:55:02 +00001392 if (func_idx < 0) // Impossible?
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001393 return FAIL;
Bram Moolenaar389df252020-07-09 21:20:47 +02001394 if (check_internal_func(func_idx, argcount) < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001395 return FAIL;
1396 return call_bfunc(func_idx, argcount, ectx);
1397 }
1398
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00001399 ufunc = find_func(name, FALSE);
Bram Moolenaara1773442020-08-12 15:21:22 +02001400
1401 if (ufunc == NULL)
1402 {
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001403 int prev_uncaught_emsg = uncaught_emsg;
Bram Moolenaara1773442020-08-12 15:21:22 +02001404
1405 if (script_autoload(name, TRUE))
1406 // loaded a package, search for the function again
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00001407 ufunc = find_func(name, FALSE);
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001408
1409 if (vim9_aborting(prev_uncaught_emsg))
Bram Moolenaara1773442020-08-12 15:21:22 +02001410 return FAIL; // bail out if loading the script caused an error
1411 }
1412
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001413 if (ufunc != NULL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001414 {
Bram Moolenaar0d89d8a2022-12-31 14:01:24 +00001415 if (check_ufunc_arg_types(ufunc, argcount, 0, ectx) == FAIL)
1416 return FAIL;
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001417
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001418 return call_ufunc(ufunc, NULL, argcount, ectx, iptr, selfdict);
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001419 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001420
1421 return FAIL;
1422}
1423
1424 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001425call_partial(
1426 typval_T *tv,
1427 int argcount_arg,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001428 ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001429{
Bram Moolenaara90afb92020-07-15 22:38:56 +02001430 int argcount = argcount_arg;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001431 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001432 int called_emsg_before = called_emsg;
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +01001433 int res = FAIL;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001434 dict_T *selfdict = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001435
1436 if (tv->v_type == VAR_PARTIAL)
1437 {
Bram Moolenaara90afb92020-07-15 22:38:56 +02001438 partial_T *pt = tv->vval.v_partial;
1439 int i;
1440
1441 if (pt->pt_argc > 0)
1442 {
1443 // Make space for arguments from the partial, shift the "argcount"
1444 // arguments up.
Bram Moolenaar35578162021-08-02 19:10:38 +02001445 if (GA_GROW_FAILS(&ectx->ec_stack, pt->pt_argc))
Bram Moolenaara90afb92020-07-15 22:38:56 +02001446 return FAIL;
1447 for (i = 1; i <= argcount; ++i)
1448 *STACK_TV_BOT(-i + pt->pt_argc) = *STACK_TV_BOT(-i);
1449 ectx->ec_stack.ga_len += pt->pt_argc;
1450 argcount += pt->pt_argc;
1451
1452 // copy the arguments from the partial onto the stack
1453 for (i = 0; i < pt->pt_argc; ++i)
1454 copy_tv(&pt->pt_argv[i], STACK_TV_BOT(-argcount + i));
1455 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001456 selfdict = pt->pt_dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001457
1458 if (pt->pt_func != NULL)
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001459 return call_ufunc(pt->pt_func, pt, argcount, ectx, NULL, selfdict);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001460
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001461 name = pt->pt_name;
1462 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001463 else if (tv->v_type == VAR_FUNC)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001464 name = tv->vval.v_string;
Bram Moolenaar95006e32020-08-29 17:47:08 +02001465 if (name != NULL)
1466 {
1467 char_u fname_buf[FLEN_FIXED + 1];
1468 char_u *tofree = NULL;
1469 int error = FCERR_NONE;
1470 char_u *fname;
1471
1472 // May need to translate <SNR>123_ to K_SNR.
1473 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
1474 if (error != FCERR_NONE)
1475 res = FAIL;
1476 else
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001477 res = call_by_name(fname, argcount, ectx, NULL, selfdict);
Bram Moolenaar95006e32020-08-29 17:47:08 +02001478 vim_free(tofree);
1479 }
1480
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +01001481 if (res == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001482 {
1483 if (called_emsg == called_emsg_before)
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001484 emsg_funcname(e_unknown_function_str,
Bram Moolenaar015f4262020-05-05 21:25:22 +02001485 name == NULL ? (char_u *)"[unknown]" : name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001486 return FAIL;
1487 }
1488 return OK;
1489}
1490
1491/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001492 * Check if "lock" is VAR_LOCKED or VAR_FIXED. If so give an error and return
1493 * TRUE.
1494 */
1495 static int
1496error_if_locked(int lock, char *error)
1497{
1498 if (lock & (VAR_LOCKED | VAR_FIXED))
1499 {
1500 emsg(_(error));
1501 return TRUE;
1502 }
1503 return FALSE;
1504}
1505
1506/*
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001507 * Give an error if "tv" is not a number and return FAIL.
1508 */
1509 static int
1510check_for_number(typval_T *tv)
1511{
1512 if (tv->v_type != VAR_NUMBER)
1513 {
1514 semsg(_(e_expected_str_but_got_str),
1515 vartype_name(VAR_NUMBER), vartype_name(tv->v_type));
1516 return FAIL;
1517 }
1518 return OK;
1519}
1520
1521/*
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001522 * Store "tv" in variable "name".
1523 * This is for s: and g: variables.
1524 */
1525 static void
1526store_var(char_u *name, typval_T *tv)
1527{
1528 funccal_entry_T entry;
Bram Moolenaard877a572021-04-01 19:42:48 +02001529 int flags = ASSIGN_DECL;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001530
Bram Moolenaard877a572021-04-01 19:42:48 +02001531 if (tv->v_lock)
1532 flags |= ASSIGN_CONST;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001533 save_funccal(&entry);
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001534 set_var_const(name, 0, NULL, tv, FALSE, flags, 0);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001535 restore_funccal();
1536}
1537
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001538/*
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001539 * Convert "tv" to a string.
1540 * Return FAIL if not allowed.
1541 */
1542 static int
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001543do_2string(typval_T *tv, int is_2string_any, int tolerant)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001544{
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00001545 if (tv->v_type == VAR_STRING)
1546 return OK;
1547
1548 char_u *str;
1549
1550 if (is_2string_any)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001551 {
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00001552 switch (tv->v_type)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001553 {
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00001554 case VAR_SPECIAL:
1555 case VAR_BOOL:
1556 case VAR_NUMBER:
1557 case VAR_FLOAT:
1558 case VAR_BLOB: break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001559
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00001560 case VAR_LIST:
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001561 if (tolerant)
1562 {
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001563 char_u *s, *e, *p;
1564 garray_T ga;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001565
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001566 ga_init2(&ga, sizeof(char_u *), 1);
1567
1568 // Convert to NL separated items, then
1569 // escape the items and replace the NL with
1570 // a space.
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001571 str = typval2string(tv, TRUE);
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001572 if (str == NULL)
1573 return FAIL;
1574 s = str;
1575 while ((e = vim_strchr(s, '\n')) != NULL)
1576 {
1577 *e = NUL;
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +01001578 p = vim_strsave_fnameescape(s,
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00001579 VSE_NONE);
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001580 if (p != NULL)
1581 {
1582 ga_concat(&ga, p);
1583 ga_concat(&ga, (char_u *)" ");
1584 vim_free(p);
1585 }
1586 s = e + 1;
1587 }
1588 vim_free(str);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001589 clear_tv(tv);
1590 tv->v_type = VAR_STRING;
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001591 tv->vval.v_string = ga.ga_data;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001592 return OK;
1593 }
1594 // FALLTHROUGH
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00001595 default: to_string_error(tv->v_type);
1596 return FAIL;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001597 }
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001598 }
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00001599 str = typval_tostring(tv, TRUE);
1600 clear_tv(tv);
1601 tv->v_type = VAR_STRING;
1602 tv->vval.v_string = str;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001603 return OK;
1604}
1605
1606/*
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001607 * When the value of "sv" is a null list of dict, allocate it.
1608 */
1609 static void
Bram Moolenaar859cc212022-03-28 15:22:35 +01001610allocate_if_null(svar_T *sv)
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001611{
Bram Moolenaar859cc212022-03-28 15:22:35 +01001612 typval_T *tv = sv->sv_tv;
1613
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001614 switch (tv->v_type)
1615 {
1616 case VAR_LIST:
Bram Moolenaar859cc212022-03-28 15:22:35 +01001617 if (tv->vval.v_list == NULL && sv->sv_type != &t_list_empty)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001618 (void)rettv_list_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001619 break;
1620 case VAR_DICT:
Bram Moolenaar859cc212022-03-28 15:22:35 +01001621 if (tv->vval.v_dict == NULL && sv->sv_type != &t_dict_empty)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001622 (void)rettv_dict_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001623 break;
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02001624 case VAR_BLOB:
Bram Moolenaar859cc212022-03-28 15:22:35 +01001625 if (tv->vval.v_blob == NULL && sv->sv_type != &t_blob_null)
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02001626 (void)rettv_blob_alloc(tv);
1627 break;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001628 default:
1629 break;
1630 }
1631}
Bram Moolenaard3aac292020-04-19 14:32:17 +02001632
Bram Moolenaare7525c52021-01-09 13:20:37 +01001633/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001634 * Return the character "str[index]" where "index" is the character index,
1635 * including composing characters.
1636 * If "index" is out of range NULL is returned.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001637 */
1638 char_u *
1639char_from_string(char_u *str, varnumber_T index)
1640{
1641 size_t nbyte = 0;
1642 varnumber_T nchar = index;
1643 size_t slen;
1644
1645 if (str == NULL)
1646 return NULL;
1647 slen = STRLEN(str);
1648
Bram Moolenaarff871402021-03-26 13:34:05 +01001649 // Do the same as for a list: a negative index counts from the end.
1650 // Optimization to check the first byte to be below 0x80 (and no composing
1651 // character follows) makes this a lot faster.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001652 if (index < 0)
1653 {
1654 int clen = 0;
1655
1656 for (nbyte = 0; nbyte < slen; ++clen)
Bram Moolenaarff871402021-03-26 13:34:05 +01001657 {
1658 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1659 ++nbyte;
1660 else if (enc_utf8)
1661 nbyte += utfc_ptr2len(str + nbyte);
1662 else
1663 nbyte += mb_ptr2len(str + nbyte);
1664 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001665 nchar = clen + index;
1666 if (nchar < 0)
1667 // unlike list: index out of range results in empty string
1668 return NULL;
1669 }
1670
1671 for (nbyte = 0; nchar > 0 && nbyte < slen; --nchar)
Bram Moolenaarff871402021-03-26 13:34:05 +01001672 {
1673 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1674 ++nbyte;
1675 else if (enc_utf8)
1676 nbyte += utfc_ptr2len(str + nbyte);
1677 else
1678 nbyte += mb_ptr2len(str + nbyte);
1679 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001680 if (nbyte >= slen)
1681 return NULL;
Bram Moolenaar0289a092021-03-14 18:40:19 +01001682 return vim_strnsave(str + nbyte, mb_ptr2len(str + nbyte));
Bram Moolenaare7525c52021-01-09 13:20:37 +01001683}
1684
1685/*
1686 * Get the byte index for character index "idx" in string "str" with length
Bram Moolenaar0289a092021-03-14 18:40:19 +01001687 * "str_len". Composing characters are included.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001688 * If going over the end return "str_len".
1689 * If "idx" is negative count from the end, -1 is the last character.
1690 * When going over the start return -1.
1691 */
1692 static long
1693char_idx2byte(char_u *str, size_t str_len, varnumber_T idx)
1694{
1695 varnumber_T nchar = idx;
1696 size_t nbyte = 0;
1697
1698 if (nchar >= 0)
1699 {
1700 while (nchar > 0 && nbyte < str_len)
1701 {
Bram Moolenaar0289a092021-03-14 18:40:19 +01001702 nbyte += mb_ptr2len(str + nbyte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001703 --nchar;
1704 }
1705 }
1706 else
1707 {
1708 nbyte = str_len;
1709 while (nchar < 0 && nbyte > 0)
1710 {
1711 --nbyte;
1712 nbyte -= mb_head_off(str, str + nbyte);
1713 ++nchar;
1714 }
1715 if (nchar < 0)
1716 return -1;
1717 }
1718 return (long)nbyte;
1719}
1720
1721/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001722 * Return the slice "str[first : last]" using character indexes. Composing
1723 * characters are included.
Bram Moolenaar6601b622021-01-13 21:47:15 +01001724 * "exclusive" is TRUE for slice().
Bram Moolenaare7525c52021-01-09 13:20:37 +01001725 * Return NULL when the result is empty.
1726 */
1727 char_u *
Bram Moolenaar6601b622021-01-13 21:47:15 +01001728string_slice(char_u *str, varnumber_T first, varnumber_T last, int exclusive)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001729{
1730 long start_byte, end_byte;
1731 size_t slen;
1732
1733 if (str == NULL)
1734 return NULL;
1735 slen = STRLEN(str);
1736 start_byte = char_idx2byte(str, slen, first);
1737 if (start_byte < 0)
1738 start_byte = 0; // first index very negative: use zero
Bram Moolenaar6601b622021-01-13 21:47:15 +01001739 if ((last == -1 && !exclusive) || last == VARNUM_MAX)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001740 end_byte = (long)slen;
1741 else
1742 {
1743 end_byte = char_idx2byte(str, slen, last);
Bram Moolenaar6601b622021-01-13 21:47:15 +01001744 if (!exclusive && end_byte >= 0 && end_byte < (long)slen)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001745 // end index is inclusive
Bram Moolenaar0289a092021-03-14 18:40:19 +01001746 end_byte += mb_ptr2len(str + end_byte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001747 }
1748
1749 if (start_byte >= (long)slen || end_byte <= start_byte)
1750 return NULL;
1751 return vim_strnsave(str + start_byte, end_byte - start_byte);
1752}
1753
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001754/*
1755 * Get a script variable for ISN_STORESCRIPT and ISN_LOADSCRIPT.
1756 * When "dfunc_idx" is negative don't give an error.
1757 * Returns NULL for an error.
1758 */
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001759 static svar_T *
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001760get_script_svar(scriptref_T *sref, int dfunc_idx)
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001761{
1762 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001763 dfunc_T *dfunc = dfunc_idx < 0 ? NULL
1764 : ((dfunc_T *)def_functions.ga_data) + dfunc_idx;
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001765 svar_T *sv;
1766
1767 if (sref->sref_seq != si->sn_script_seq)
1768 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001769 // The script was reloaded after the function was compiled, the
1770 // script_idx may not be valid.
1771 if (dfunc != NULL)
1772 semsg(_(e_script_variable_invalid_after_reload_in_function_str),
1773 printable_func_name(dfunc->df_ufunc));
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001774 return NULL;
1775 }
1776 sv = ((svar_T *)si->sn_var_vals.ga_data) + sref->sref_idx;
Bram Moolenaar6de22962022-09-09 21:35:36 +01001777 if (sv->sv_name == NULL)
1778 {
1779 if (dfunc != NULL)
1780 emsg(_(e_script_variable_was_deleted));
1781 return NULL;
1782 }
Bram Moolenaar60dc8272021-07-29 22:48:54 +02001783 if (!equal_type(sv->sv_type, sref->sref_type, 0))
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001784 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001785 if (dfunc != NULL)
1786 emsg(_(e_script_variable_type_changed));
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001787 return NULL;
1788 }
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001789
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01001790 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0
1791 && sref->sref_sid != current_sctx.sc_sid)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001792 {
1793 if (dfunc != NULL)
1794 semsg(_(e_item_not_exported_in_script_str), sv->sv_name);
1795 return NULL;
1796 }
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001797 return sv;
1798}
1799
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001800/*
Bram Moolenaar20677332021-06-06 17:02:53 +02001801 * Function passed to do_cmdline() for splitting a script joined by NL
1802 * characters.
1803 */
1804 static char_u *
1805get_split_sourceline(
1806 int c UNUSED,
1807 void *cookie,
1808 int indent UNUSED,
1809 getline_opt_T options UNUSED)
1810{
1811 source_cookie_T *sp = (source_cookie_T *)cookie;
1812 char_u *p;
1813 char_u *line;
1814
Bram Moolenaar20677332021-06-06 17:02:53 +02001815 p = vim_strchr(sp->nextline, '\n');
1816 if (p == NULL)
1817 {
1818 line = vim_strsave(sp->nextline);
1819 sp->nextline += STRLEN(sp->nextline);
1820 }
1821 else
1822 {
1823 line = vim_strnsave(sp->nextline, p - sp->nextline);
1824 sp->nextline = p + 1;
1825 }
1826 return line;
1827}
1828
1829/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001830 * Execute a function by "name".
1831 * This can be a builtin function, user function or a funcref.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001832 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001833 */
1834 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001835call_eval_func(
1836 char_u *name,
1837 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001838 ectx_T *ectx,
1839 isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001840{
Bram Moolenaared677f52020-08-12 16:38:10 +02001841 int called_emsg_before = called_emsg;
1842 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001843
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001844 res = call_by_name(name, argcount, ectx, iptr, NULL);
Bram Moolenaared677f52020-08-12 16:38:10 +02001845 if (res == FAIL && called_emsg == called_emsg_before)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001846 {
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001847 dictitem_T *v;
1848
1849 v = find_var(name, NULL, FALSE);
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001850 if (v == NULL || (v->di_tv.v_type != VAR_PARTIAL
1851 && v->di_tv.v_type != VAR_FUNC))
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001852 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001853 emsg_funcname(e_unknown_function_str, name);
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001854 return FAIL;
1855 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001856 return call_partial(&v->di_tv, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001857 }
Bram Moolenaared677f52020-08-12 16:38:10 +02001858 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001859}
1860
1861/*
Bram Moolenaarf112f302020-12-20 17:47:52 +01001862 * When a function reference is used, fill a partial with the information
1863 * needed, especially when it is used as a closure.
1864 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001865 int
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001866fill_partial_and_closure(
Bram Moolenaarcc341812022-09-19 15:54:34 +01001867 partial_T *pt,
1868 ufunc_T *ufunc,
1869 loopvarinfo_T *lvi,
1870 ectx_T *ectx)
Bram Moolenaarf112f302020-12-20 17:47:52 +01001871{
1872 pt->pt_func = ufunc;
1873 pt->pt_refcount = 1;
1874
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001875 if (ufunc->uf_flags & FC_CLOSURE)
Bram Moolenaarf112f302020-12-20 17:47:52 +01001876 {
1877 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1878 + ectx->ec_dfunc_idx;
1879
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001880 // The closure may need to find arguments and local variables of the
1881 // current function in the stack.
Bram Moolenaar0186e582021-01-10 18:33:11 +01001882 pt->pt_outer.out_stack = &ectx->ec_stack;
1883 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001884 if (ectx->ec_outer_ref != NULL)
1885 {
1886 // The current context already has a context, link to that one.
1887 pt->pt_outer.out_up = ectx->ec_outer_ref->or_outer;
1888 if (ectx->ec_outer_ref->or_partial != NULL)
1889 {
1890 pt->pt_outer.out_up_partial = ectx->ec_outer_ref->or_partial;
1891 ++pt->pt_outer.out_up_partial->pt_refcount;
1892 }
1893 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01001894
Bram Moolenaarcc341812022-09-19 15:54:34 +01001895 if (lvi != NULL)
1896 {
1897 int depth;
1898
1899 // The closure may need to find variables defined inside a loop,
1900 // for every nested loop. A new reference is made every time,
1901 // ISN_ENDLOOP will check if they are actually used.
1902 for (depth = 0; depth < lvi->lvi_depth; ++depth)
1903 {
1904 pt->pt_outer.out_loop[depth].stack = &ectx->ec_stack;
1905 pt->pt_outer.out_loop[depth].var_idx = ectx->ec_frame_idx
1906 + STACK_FRAME_SIZE + lvi->lvi_loop[depth].var_idx;
1907 pt->pt_outer.out_loop[depth].var_count =
1908 lvi->lvi_loop[depth].var_count;
1909 }
Bram Moolenaar6d313be2022-09-22 16:36:25 +01001910 pt->pt_outer.out_loop_size = lvi->lvi_depth;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001911 }
Bram Moolenaar6d313be2022-09-22 16:36:25 +01001912 else
1913 pt->pt_outer.out_loop_size = 0;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001914
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001915 // If the function currently executing returns and the closure is still
1916 // being referenced, we need to make a copy of the context (arguments
1917 // and local variables) so that the closure can use it later.
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001918 // Store a reference to the partial so we can handle that.
Bram Moolenaar35578162021-08-02 19:10:38 +02001919 if (GA_GROW_FAILS(&ectx->ec_funcrefs, 1))
Bram Moolenaarf112f302020-12-20 17:47:52 +01001920 {
1921 vim_free(pt);
1922 return FAIL;
1923 }
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001924 // Extra variable keeps the count of closures created in the current
1925 // function call.
Bram Moolenaarf112f302020-12-20 17:47:52 +01001926 ++(((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx
1927 + STACK_FRAME_SIZE + dfunc->df_varcount)->vval.v_number;
1928
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001929 ((partial_T **)ectx->ec_funcrefs.ga_data)[ectx->ec_funcrefs.ga_len]
1930 = pt;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001931 ++pt->pt_refcount;
1932 ++ectx->ec_funcrefs.ga_len;
1933 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001934 ++ufunc->uf_refcount;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001935 return OK;
1936}
1937
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001938/*
1939 * Execute iptr->isn_arg.string as an Ex command.
1940 */
1941 static int
1942exec_command(isn_T *iptr)
1943{
1944 source_cookie_T cookie;
1945
1946 SOURCING_LNUM = iptr->isn_lnum;
1947 // Pass getsourceline to get an error for a missing ":end"
1948 // command.
1949 CLEAR_FIELD(cookie);
1950 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1951 if (do_cmdline(iptr->isn_arg.string,
1952 getsourceline, &cookie,
1953 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED) == FAIL
1954 || did_emsg)
1955 return FAIL;
1956 return OK;
1957}
1958
Bram Moolenaar89445512022-04-14 12:58:23 +01001959/*
1960 * If script "sid" is not loaded yet then load it now.
1961 * Caller must make sure "sid" is a valid script ID.
1962 * "loaded" is set to TRUE if the script had to be loaded.
1963 * Returns FAIL if loading fails, OK if already loaded or loaded now.
1964 */
1965 int
1966may_load_script(int sid, int *loaded)
1967{
1968 scriptitem_T *si = SCRIPT_ITEM(sid);
1969
1970 if (si->sn_state == SN_STATE_NOT_LOADED)
1971 {
1972 *loaded = TRUE;
1973 if (do_source(si->sn_name, FALSE, DOSO_NONE, NULL) == FAIL)
1974 {
1975 semsg(_(e_cant_open_file_str), si->sn_name);
1976 return FAIL;
1977 }
1978 }
1979 return OK;
1980}
1981
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001982// used for v_instr of typval of VAR_INSTR
1983struct instr_S {
1984 ectx_T *instr_ectx;
1985 isn_T *instr_instr;
1986};
1987
Bram Moolenaar4c137212021-04-19 16:48:48 +02001988// used for substitute_instr
1989typedef struct subs_expr_S {
1990 ectx_T *subs_ectx;
1991 isn_T *subs_instr;
1992 int subs_status;
1993} subs_expr_T;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001994
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001995// Set when calling do_debug().
1996static ectx_T *debug_context = NULL;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001997static int debug_var_count;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001998
1999/*
2000 * When debugging lookup "name" and return the typeval.
2001 * When not found return NULL.
2002 */
2003 typval_T *
2004lookup_debug_var(char_u *name)
2005{
2006 int idx;
2007 dfunc_T *dfunc;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02002008 ufunc_T *ufunc;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002009 ectx_T *ectx = debug_context;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02002010 int varargs_off;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002011
2012 if (ectx == NULL)
2013 return NULL;
2014 dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
2015
2016 // Go through the local variable names, from last to first.
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02002017 for (idx = debug_var_count - 1; idx >= 0; --idx)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002018 {
Bram Moolenaare406ff82022-03-10 20:47:43 +00002019 char_u *varname = ((char_u **)dfunc->df_var_names.ga_data)[idx];
2020
2021 // the variable name may be NULL when not available in this block
2022 if (varname != NULL && STRCMP(varname, name) == 0)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002023 return STACK_TV_VAR(idx);
2024 }
2025
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02002026 // Go through argument names.
2027 ufunc = dfunc->df_ufunc;
2028 varargs_off = ufunc->uf_va_name == NULL ? 0 : 1;
2029 for (idx = 0; idx < ufunc->uf_args.ga_len; ++idx)
2030 if (STRCMP(((char_u **)(ufunc->uf_args.ga_data))[idx], name) == 0)
2031 return STACK_TV(ectx->ec_frame_idx - ufunc->uf_args.ga_len
2032 - varargs_off + idx);
2033 if (ufunc->uf_va_name != NULL && STRCMP(ufunc->uf_va_name, name) == 0)
2034 return STACK_TV(ectx->ec_frame_idx - 1);
2035
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002036 return NULL;
2037}
2038
Bram Moolenaar26a44842021-09-02 18:49:06 +02002039/*
2040 * Return TRUE if there might be a breakpoint in "ufunc", which is when a
2041 * breakpoint was set in that function or when there is any expression.
2042 */
2043 int
2044may_break_in_function(ufunc_T *ufunc)
2045{
2046 return ufunc->uf_has_breakpoint || debug_has_expr_breakpoint();
2047}
2048
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002049 static void
2050handle_debug(isn_T *iptr, ectx_T *ectx)
2051{
2052 char_u *line;
2053 ufunc_T *ufunc = (((dfunc_T *)def_functions.ga_data)
2054 + ectx->ec_dfunc_idx)->df_ufunc;
2055 isn_T *ni;
2056 int end_lnum = iptr->isn_lnum;
2057 garray_T ga;
2058 int lnum;
2059
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02002060 if (ex_nesting_level > debug_break_level)
2061 {
2062 linenr_T breakpoint;
2063
Bram Moolenaar26a44842021-09-02 18:49:06 +02002064 if (!may_break_in_function(ufunc))
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02002065 return;
2066
2067 // check for the next breakpoint if needed
2068 breakpoint = dbg_find_breakpoint(FALSE, ufunc->uf_name,
Bram Moolenaar8cec9272021-06-23 20:20:53 +02002069 iptr->isn_arg.debug.dbg_break_lnum);
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02002070 if (breakpoint <= 0 || breakpoint > iptr->isn_lnum)
2071 return;
2072 }
2073
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002074 SOURCING_LNUM = iptr->isn_lnum;
2075 debug_context = ectx;
Bram Moolenaar8cec9272021-06-23 20:20:53 +02002076 debug_var_count = iptr->isn_arg.debug.dbg_var_names_len;
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002077
2078 for (ni = iptr + 1; ni->isn_type != ISN_FINISH; ++ni)
2079 if (ni->isn_type == ISN_DEBUG
2080 || ni->isn_type == ISN_RETURN
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002081 || ni->isn_type == ISN_RETURN_OBJECT
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002082 || ni->isn_type == ISN_RETURN_VOID)
2083 {
Bram Moolenaar112bed02021-11-23 22:16:34 +00002084 end_lnum = ni->isn_lnum + (ni->isn_type == ISN_DEBUG ? 0 : 1);
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002085 break;
2086 }
2087
2088 if (end_lnum > iptr->isn_lnum)
2089 {
2090 ga_init2(&ga, sizeof(char_u *), 10);
Bram Moolenaar310091d2021-12-23 21:14:37 +00002091 for (lnum = iptr->isn_lnum; lnum < end_lnum
2092 && lnum <= ufunc->uf_lines.ga_len; ++lnum)
Bram Moolenaar59b50c32021-06-17 22:27:48 +02002093 {
Bram Moolenaar303215d2021-07-07 20:10:43 +02002094 char_u *p = ((char_u **)ufunc->uf_lines.ga_data)[lnum - 1];
Bram Moolenaar59b50c32021-06-17 22:27:48 +02002095
Bram Moolenaar303215d2021-07-07 20:10:43 +02002096 if (p == NULL)
2097 continue; // left over from continuation line
2098 p = skipwhite(p);
Bram Moolenaar59b50c32021-06-17 22:27:48 +02002099 if (*p == '#')
2100 break;
Bram Moolenaar35578162021-08-02 19:10:38 +02002101 if (GA_GROW_OK(&ga, 1))
Bram Moolenaar59b50c32021-06-17 22:27:48 +02002102 ((char_u **)(ga.ga_data))[ga.ga_len++] = p;
2103 if (STRNCMP(p, "def ", 4) == 0)
2104 break;
2105 }
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002106 line = ga_concat_strings(&ga, " ");
2107 vim_free(ga.ga_data);
2108 }
2109 else
2110 line = ((char_u **)ufunc->uf_lines.ga_data)[iptr->isn_lnum - 1];
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002111
Dominique Pelle6e969552021-06-17 13:53:41 +02002112 do_debug(line == NULL ? (char_u *)"[empty]" : line);
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002113 debug_context = NULL;
2114
2115 if (end_lnum > iptr->isn_lnum)
2116 vim_free(line);
2117}
2118
Bram Moolenaar4c137212021-04-19 16:48:48 +02002119/*
Bram Moolenaar65b0d162022-12-13 18:43:22 +00002120 * Store a value in a list, dict, blob or object variable.
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002121 * Returns OK, FAIL or NOTDONE (uncatchable error).
2122 */
2123 static int
2124execute_storeindex(isn_T *iptr, ectx_T *ectx)
2125{
Bram Moolenaarf7d1c6e2023-01-16 20:47:57 +00002126 vartype_T dest_type = iptr->isn_arg.storeindex.si_vartype;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002127 typval_T *tv;
2128 typval_T *tv_idx = STACK_TV_BOT(-2);
2129 typval_T *tv_dest = STACK_TV_BOT(-1);
2130 int status = OK;
2131
2132 // Stack contains:
2133 // -3 value to be stored
2134 // -2 index
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002135 // -1 dict, list, blob or object
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002136 tv = STACK_TV_BOT(-3);
2137 SOURCING_LNUM = iptr->isn_lnum;
2138 if (dest_type == VAR_ANY)
2139 {
2140 dest_type = tv_dest->v_type;
2141 if (dest_type == VAR_DICT)
2142 status = do_2string(tv_idx, TRUE, FALSE);
2143 else if (dest_type == VAR_LIST && tv_idx->v_type != VAR_NUMBER)
2144 {
2145 emsg(_(e_number_expected));
2146 status = FAIL;
2147 }
2148 }
Bram Moolenaare08be092022-02-17 13:08:26 +00002149
2150 if (status == OK)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002151 {
Bram Moolenaare08be092022-02-17 13:08:26 +00002152 if (dest_type == VAR_LIST)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002153 {
Bram Moolenaare08be092022-02-17 13:08:26 +00002154 long lidx = (long)tv_idx->vval.v_number;
2155 list_T *list = tv_dest->vval.v_list;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002156
Bram Moolenaare08be092022-02-17 13:08:26 +00002157 if (list == NULL)
2158 {
2159 emsg(_(e_list_not_set));
2160 return FAIL;
2161 }
2162 if (lidx < 0 && list->lv_len + lidx >= 0)
2163 // negative index is relative to the end
2164 lidx = list->lv_len + lidx;
2165 if (lidx < 0 || lidx > list->lv_len)
2166 {
2167 semsg(_(e_list_index_out_of_range_nr), lidx);
2168 return FAIL;
2169 }
2170 if (lidx < list->lv_len)
2171 {
2172 listitem_T *li = list_find(list, lidx);
2173
2174 if (error_if_locked(li->li_tv.v_lock,
Bram Moolenaar70c43d82022-01-26 21:01:15 +00002175 e_cannot_change_locked_list_item))
Bram Moolenaare08be092022-02-17 13:08:26 +00002176 return FAIL;
2177 // overwrite existing list item
2178 clear_tv(&li->li_tv);
2179 li->li_tv = *tv;
2180 }
2181 else
2182 {
2183 if (error_if_locked(list->lv_lock, e_cannot_change_locked_list))
2184 return FAIL;
2185 // append to list, only fails when out of memory
2186 if (list_append_tv(list, tv) == FAIL)
2187 return NOTDONE;
2188 clear_tv(tv);
2189 }
2190 }
2191 else if (dest_type == VAR_DICT)
2192 {
2193 char_u *key = tv_idx->vval.v_string;
2194 dict_T *dict = tv_dest->vval.v_dict;
2195 dictitem_T *di;
2196
2197 SOURCING_LNUM = iptr->isn_lnum;
2198 if (dict == NULL)
2199 {
2200 emsg(_(e_dictionary_not_set));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002201 return FAIL;
Bram Moolenaare08be092022-02-17 13:08:26 +00002202 }
2203 if (key == NULL)
2204 key = (char_u *)"";
2205 di = dict_find(dict, key, -1);
2206 if (di != NULL)
2207 {
2208 if (error_if_locked(di->di_tv.v_lock,
2209 e_cannot_change_dict_item))
2210 return FAIL;
2211 // overwrite existing value
2212 clear_tv(&di->di_tv);
2213 di->di_tv = *tv;
2214 }
2215 else
2216 {
2217 if (error_if_locked(dict->dv_lock, e_cannot_change_dict))
2218 return FAIL;
2219 // add to dict, only fails when out of memory
2220 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
2221 return NOTDONE;
2222 clear_tv(tv);
2223 }
2224 }
2225 else if (dest_type == VAR_BLOB)
2226 {
2227 long lidx = (long)tv_idx->vval.v_number;
2228 blob_T *blob = tv_dest->vval.v_blob;
Bram Moolenaar65b0d162022-12-13 18:43:22 +00002229 varnumber_T nr;
2230 int error = FALSE;
2231 int len;
Bram Moolenaare08be092022-02-17 13:08:26 +00002232
2233 if (blob == NULL)
2234 {
2235 emsg(_(e_blob_not_set));
2236 return FAIL;
2237 }
2238 len = blob_len(blob);
2239 if (lidx < 0 && len + lidx >= 0)
2240 // negative index is relative to the end
2241 lidx = len + lidx;
2242
2243 // Can add one byte at the end.
2244 if (lidx < 0 || lidx > len)
2245 {
2246 semsg(_(e_blob_index_out_of_range_nr), lidx);
2247 return FAIL;
2248 }
2249 if (value_check_lock(blob->bv_lock, (char_u *)"blob", FALSE))
2250 return FAIL;
2251 nr = tv_get_number_chk(tv, &error);
2252 if (error)
2253 return FAIL;
2254 blob_set_append(blob, lidx, nr);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002255 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002256 else if (dest_type == VAR_CLASS || dest_type == VAR_OBJECT)
2257 {
2258 long idx = (long)tv_idx->vval.v_number;
2259 object_T *obj = tv_dest->vval.v_object;
2260 typval_T *otv = (typval_T *)(obj + 1);
Bram Moolenaarf7d1c6e2023-01-16 20:47:57 +00002261
2262 class_T *itf = iptr->isn_arg.storeindex.si_class;
2263 if (itf != NULL)
2264 // convert interface member index to class member index
2265 idx = object_index_from_itf_index(itf, idx, obj->obj_class);
2266
Bram Moolenaar65b0d162022-12-13 18:43:22 +00002267 clear_tv(&otv[idx]);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002268 otv[idx] = *tv;
2269 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002270 else
2271 {
Bram Moolenaare08be092022-02-17 13:08:26 +00002272 status = FAIL;
2273 semsg(_(e_cannot_index_str), vartype_name(dest_type));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002274 }
2275 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002276
2277 clear_tv(tv_idx);
2278 clear_tv(tv_dest);
2279 ectx->ec_stack.ga_len -= 3;
2280 if (status == FAIL)
2281 {
2282 clear_tv(tv);
2283 return FAIL;
2284 }
2285 return OK;
2286}
2287
2288/*
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002289 * Store a value in a list or blob range.
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002290 */
2291 static int
2292execute_storerange(isn_T *iptr, ectx_T *ectx)
2293{
2294 typval_T *tv;
2295 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2296 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2297 typval_T *tv_dest = STACK_TV_BOT(-1);
2298 int status = OK;
2299
2300 // Stack contains:
2301 // -4 value to be stored
2302 // -3 first index or "none"
2303 // -2 second index or "none"
2304 // -1 destination list or blob
2305 tv = STACK_TV_BOT(-4);
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002306 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002307 if (tv_dest->v_type == VAR_LIST)
2308 {
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002309 long n1;
2310 long n2;
2311 listitem_T *li1;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002312
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002313 n1 = (long)tv_get_number_chk(tv_idx1, NULL);
2314 if (tv_idx2->v_type == VAR_SPECIAL
2315 && tv_idx2->vval.v_number == VVAL_NONE)
2316 n2 = list_len(tv_dest->vval.v_list) - 1;
2317 else
2318 n2 = (long)tv_get_number_chk(tv_idx2, NULL);
2319
Bram Moolenaar22ebd172022-04-01 15:26:58 +01002320 li1 = check_range_index_one(tv_dest->vval.v_list, &n1, TRUE, FALSE);
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002321 if (li1 == NULL)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002322 status = FAIL;
2323 else
2324 {
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002325 status = check_range_index_two(tv_dest->vval.v_list,
2326 &n1, li1, &n2, FALSE);
2327 if (status != FAIL)
2328 status = list_assign_range(
2329 tv_dest->vval.v_list,
2330 tv->vval.v_list,
2331 n1,
2332 n2,
2333 tv_idx2->v_type == VAR_SPECIAL,
2334 (char_u *)"=",
2335 (char_u *)"[unknown]");
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002336 }
2337 }
2338 else if (tv_dest->v_type == VAR_BLOB)
2339 {
2340 varnumber_T n1;
2341 varnumber_T n2;
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002342 long bloblen;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002343
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002344 n1 = tv_get_number_chk(tv_idx1, NULL);
2345 if (tv_idx2->v_type == VAR_SPECIAL
2346 && tv_idx2->vval.v_number == VVAL_NONE)
2347 n2 = blob_len(tv_dest->vval.v_blob) - 1;
2348 else
2349 n2 = tv_get_number_chk(tv_idx2, NULL);
2350 bloblen = blob_len(tv_dest->vval.v_blob);
2351
2352 if (check_blob_index(bloblen, n1, FALSE) == FAIL
2353 || check_blob_range(bloblen, n1, n2, FALSE) == FAIL)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002354 status = FAIL;
2355 else
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002356 status = blob_set_range(tv_dest->vval.v_blob, n1, n2, tv);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002357 }
2358 else
2359 {
2360 status = FAIL;
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002361 emsg(_(e_list_or_blob_required));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002362 }
2363
2364 clear_tv(tv_idx1);
2365 clear_tv(tv_idx2);
2366 clear_tv(tv_dest);
2367 ectx->ec_stack.ga_len -= 4;
2368 clear_tv(tv);
2369
2370 return status;
2371}
2372
2373/*
2374 * Unlet item in list or dict variable.
2375 */
2376 static int
2377execute_unletindex(isn_T *iptr, ectx_T *ectx)
2378{
2379 typval_T *tv_idx = STACK_TV_BOT(-2);
2380 typval_T *tv_dest = STACK_TV_BOT(-1);
2381 int status = OK;
2382
2383 // Stack contains:
2384 // -2 index
2385 // -1 dict or list
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002386 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002387 if (tv_dest->v_type == VAR_DICT)
2388 {
2389 // unlet a dict item, index must be a string
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002390 if (tv_idx->v_type != VAR_STRING && tv_idx->v_type != VAR_NUMBER)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002391 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002392 semsg(_(e_expected_str_but_got_str),
2393 vartype_name(VAR_STRING),
2394 vartype_name(tv_idx->v_type));
2395 status = FAIL;
2396 }
2397 else
2398 {
2399 dict_T *d = tv_dest->vval.v_dict;
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002400 char_u *key;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002401 dictitem_T *di = NULL;
2402
2403 if (d != NULL && value_check_lock(
2404 d->dv_lock, NULL, FALSE))
2405 status = FAIL;
2406 else
2407 {
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002408 if (tv_idx->v_type == VAR_STRING)
2409 {
2410 key = tv_idx->vval.v_string;
2411 if (key == NULL)
2412 key = (char_u *)"";
2413 }
2414 else
2415 {
2416 key = tv_get_string(tv_idx);
2417 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002418 if (d != NULL)
2419 di = dict_find(d, key, (int)STRLEN(key));
2420 if (di == NULL)
2421 {
2422 // NULL dict is equivalent to empty dict
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00002423 semsg(_(e_key_not_present_in_dictionary_str), key);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002424 status = FAIL;
2425 }
2426 else if (var_check_fixed(di->di_flags,
2427 NULL, FALSE)
2428 || var_check_ro(di->di_flags,
2429 NULL, FALSE))
2430 status = FAIL;
2431 else
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002432 dictitem_remove(d, di, "unlet");
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002433 }
2434 }
2435 }
2436 else if (tv_dest->v_type == VAR_LIST)
2437 {
2438 // unlet a List item, index must be a number
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002439 if (check_for_number(tv_idx) == FAIL)
2440 {
2441 status = FAIL;
2442 }
2443 else
2444 {
2445 list_T *l = tv_dest->vval.v_list;
2446 long n = (long)tv_idx->vval.v_number;
2447
2448 if (l != NULL && value_check_lock(
2449 l->lv_lock, NULL, FALSE))
2450 status = FAIL;
2451 else
2452 {
2453 listitem_T *li = list_find(l, n);
2454
2455 if (li == NULL)
2456 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002457 semsg(_(e_list_index_out_of_range_nr), n);
2458 status = FAIL;
2459 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002460 else
2461 listitem_remove(l, li);
2462 }
2463 }
2464 }
2465 else
2466 {
2467 status = FAIL;
2468 semsg(_(e_cannot_index_str),
2469 vartype_name(tv_dest->v_type));
2470 }
2471
2472 clear_tv(tv_idx);
2473 clear_tv(tv_dest);
2474 ectx->ec_stack.ga_len -= 2;
2475
2476 return status;
2477}
2478
2479/*
2480 * Unlet a range of items in a list variable.
2481 */
2482 static int
2483execute_unletrange(isn_T *iptr, ectx_T *ectx)
2484{
2485 // Stack contains:
2486 // -3 index1
2487 // -2 index2
2488 // -1 dict or list
2489 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2490 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2491 typval_T *tv_dest = STACK_TV_BOT(-1);
2492 int status = OK;
2493
2494 if (tv_dest->v_type == VAR_LIST)
2495 {
2496 // indexes must be a number
2497 SOURCING_LNUM = iptr->isn_lnum;
2498 if (check_for_number(tv_idx1) == FAIL
2499 || (tv_idx2->v_type != VAR_SPECIAL
2500 && check_for_number(tv_idx2) == FAIL))
2501 {
2502 status = FAIL;
2503 }
2504 else
2505 {
2506 list_T *l = tv_dest->vval.v_list;
2507 long n1 = (long)tv_idx1->vval.v_number;
2508 long n2 = tv_idx2->v_type == VAR_SPECIAL
2509 ? 0 : (long)tv_idx2->vval.v_number;
2510 listitem_T *li;
2511
2512 li = list_find_index(l, &n1);
2513 if (li == NULL)
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002514 {
2515 semsg(_(e_list_index_out_of_range_nr),
2516 (long)tv_idx1->vval.v_number);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002517 status = FAIL;
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002518 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002519 else
2520 {
2521 if (n1 < 0)
2522 n1 = list_idx_of_item(l, li);
2523 if (n2 < 0)
2524 {
2525 listitem_T *li2 = list_find(l, n2);
2526
2527 if (li2 == NULL)
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002528 {
2529 semsg(_(e_list_index_out_of_range_nr), n2);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002530 status = FAIL;
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002531 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002532 else
2533 n2 = list_idx_of_item(l, li2);
2534 }
2535 if (status != FAIL
2536 && tv_idx2->v_type != VAR_SPECIAL
2537 && n2 < n1)
2538 {
2539 semsg(_(e_list_index_out_of_range_nr), n2);
2540 status = FAIL;
2541 }
Bram Moolenaar6b8c7ba2022-03-20 17:46:06 +00002542 if (status != FAIL)
2543 list_unlet_range(l, li, n1,
2544 tv_idx2->v_type != VAR_SPECIAL, n2);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002545 }
2546 }
2547 }
2548 else
2549 {
2550 status = FAIL;
2551 SOURCING_LNUM = iptr->isn_lnum;
2552 semsg(_(e_cannot_index_str),
2553 vartype_name(tv_dest->v_type));
2554 }
2555
2556 clear_tv(tv_idx1);
2557 clear_tv(tv_idx2);
2558 clear_tv(tv_dest);
2559 ectx->ec_stack.ga_len -= 3;
2560
2561 return status;
2562}
2563
2564/*
2565 * Top of a for loop.
2566 */
2567 static int
2568execute_for(isn_T *iptr, ectx_T *ectx)
2569{
2570 typval_T *tv;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002571 int jump = FALSE;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002572 typval_T *ltv = STACK_TV_BOT(-1);
2573 typval_T *idxtv =
Bram Moolenaarcc341812022-09-19 15:54:34 +01002574 STACK_TV_VAR(iptr->isn_arg.forloop.for_loop_idx);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002575
2576 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
2577 return FAIL;
2578 if (ltv->v_type == VAR_LIST)
2579 {
2580 list_T *list = ltv->vval.v_list;
2581
2582 // push the next item from the list
2583 ++idxtv->vval.v_number;
2584 if (list == NULL
2585 || idxtv->vval.v_number >= list->lv_len)
2586 {
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002587 jump = TRUE;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002588 }
2589 else if (list->lv_first == &range_list_item)
2590 {
2591 // non-materialized range() list
2592 tv = STACK_TV_BOT(0);
2593 tv->v_type = VAR_NUMBER;
2594 tv->v_lock = 0;
2595 tv->vval.v_number = list_find_nr(
2596 list, idxtv->vval.v_number, NULL);
2597 ++ectx->ec_stack.ga_len;
2598 }
2599 else
2600 {
2601 listitem_T *li = list_find(list,
2602 idxtv->vval.v_number);
2603
2604 copy_tv(&li->li_tv, STACK_TV_BOT(0));
2605 ++ectx->ec_stack.ga_len;
2606 }
2607 }
2608 else if (ltv->v_type == VAR_STRING)
2609 {
2610 char_u *str = ltv->vval.v_string;
2611
2612 // The index is for the last byte of the previous
2613 // character.
2614 ++idxtv->vval.v_number;
2615 if (str == NULL || str[idxtv->vval.v_number] == NUL)
2616 {
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002617 jump = TRUE;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002618 }
2619 else
2620 {
2621 int clen = mb_ptr2len(str + idxtv->vval.v_number);
2622
2623 // Push the next character from the string.
2624 tv = STACK_TV_BOT(0);
2625 tv->v_type = VAR_STRING;
2626 tv->vval.v_string = vim_strnsave(
2627 str + idxtv->vval.v_number, clen);
2628 ++ectx->ec_stack.ga_len;
2629 idxtv->vval.v_number += clen - 1;
2630 }
2631 }
2632 else if (ltv->v_type == VAR_BLOB)
2633 {
2634 blob_T *blob = ltv->vval.v_blob;
2635
2636 // When we get here the first time make a copy of the
2637 // blob, so that the iteration still works when it is
2638 // changed.
2639 if (idxtv->vval.v_number == -1 && blob != NULL)
2640 {
2641 blob_copy(blob, ltv);
2642 blob_unref(blob);
2643 blob = ltv->vval.v_blob;
2644 }
2645
2646 // The index is for the previous byte.
2647 ++idxtv->vval.v_number;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002648 if (blob == NULL || idxtv->vval.v_number >= blob_len(blob))
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002649 {
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002650 jump = TRUE;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002651 }
2652 else
2653 {
2654 // Push the next byte from the blob.
2655 tv = STACK_TV_BOT(0);
2656 tv->v_type = VAR_NUMBER;
2657 tv->vval.v_number = blob_get(blob,
2658 idxtv->vval.v_number);
2659 ++ectx->ec_stack.ga_len;
2660 }
2661 }
2662 else
2663 {
2664 semsg(_(e_for_loop_on_str_not_supported),
2665 vartype_name(ltv->v_type));
2666 return FAIL;
2667 }
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002668
2669 if (jump)
2670 {
2671 // past the end of the list/string/blob, jump to "endfor"
2672 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
2673 may_restore_cmdmod(&ectx->ec_funclocal);
2674 }
2675 else
2676 {
2677 // Store the current number of funcrefs, this may be used in
2678 // ISN_LOOPEND. The variable index is always one more than the loop
2679 // variable index.
Bram Moolenaarcc341812022-09-19 15:54:34 +01002680 tv = STACK_TV_VAR(iptr->isn_arg.forloop.for_loop_idx + 1);
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002681 tv->vval.v_number = ectx->ec_funcrefs.ga_len;
2682 }
2683
2684 return OK;
2685}
2686
2687/*
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002688 * Code for handling variables declared inside a loop and used in a closure.
2689 * This is very similar to what is done with funcstack_T. The difference is
2690 * that the funcstack_T has the scope of a function, while a loopvars_T has the
2691 * scope of the block inside a loop and each loop may have its own.
2692 */
2693
2694// Double linked list of loopvars_T in use.
2695static loopvars_T *first_loopvars = NULL;
2696
2697 static void
2698add_loopvars_to_list(loopvars_T *loopvars)
2699{
2700 // Link in list of loopvarss.
2701 if (first_loopvars != NULL)
2702 first_loopvars->lvs_prev = loopvars;
2703 loopvars->lvs_next = first_loopvars;
2704 loopvars->lvs_prev = NULL;
2705 first_loopvars = loopvars;
2706}
2707
2708 static void
2709remove_loopvars_from_list(loopvars_T *loopvars)
2710{
2711 if (loopvars->lvs_prev == NULL)
2712 first_loopvars = loopvars->lvs_next;
2713 else
2714 loopvars->lvs_prev->lvs_next = loopvars->lvs_next;
2715 if (loopvars->lvs_next != NULL)
2716 loopvars->lvs_next->lvs_prev = loopvars->lvs_prev;
2717}
2718
2719/*
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002720 * End of a for or while loop: Handle any variables used by a closure.
2721 */
2722 static int
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002723execute_endloop(isn_T *iptr, ectx_T *ectx)
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002724{
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002725 endloop_T *endloop = &iptr->isn_arg.endloop;
2726 typval_T *tv_refcount = STACK_TV_VAR(endloop->end_funcref_idx);
2727 int prev_closure_count = tv_refcount->vval.v_number;
Bram Moolenaarcc341812022-09-19 15:54:34 +01002728 int depth = endloop->end_depth;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002729 garray_T *gap = &ectx->ec_funcrefs;
2730 int closure_in_use = FALSE;
2731 loopvars_T *loopvars;
2732 typval_T *stack;
2733 int idx;
2734
Bram Moolenaarcc341812022-09-19 15:54:34 +01002735 // Check if any created closure is still being referenced and loopvars have
2736 // not been saved yet for the current depth.
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002737 for (idx = prev_closure_count; idx < gap->ga_len; ++idx)
2738 {
2739 partial_T *pt = ((partial_T **)gap->ga_data)[idx];
2740
Bram Moolenaarcc341812022-09-19 15:54:34 +01002741 if (pt->pt_refcount > 1 && pt->pt_loopvars[depth] == NULL)
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002742 {
2743 int refcount = pt->pt_refcount;
2744 int i;
2745
2746 // A Reference in a variable inside the loop doesn't count, it gets
2747 // unreferenced at the end of the loop.
2748 for (i = 0; i < endloop->end_var_count; ++i)
2749 {
2750 typval_T *stv = STACK_TV_VAR(endloop->end_var_idx + i);
2751
2752 if (stv->v_type == VAR_PARTIAL && pt == stv->vval.v_partial)
2753 --refcount;
2754 }
2755 if (refcount > 1)
2756 {
2757 closure_in_use = TRUE;
2758 break;
2759 }
2760 }
2761 }
2762
2763 // If no function reference were created since the start of the loop block
2764 // or it is no longer referenced there is nothing to do.
2765 if (!closure_in_use)
2766 return OK;
2767
2768 // A closure is using variables declared inside the loop.
2769 // Move them to the called function.
2770 loopvars = ALLOC_CLEAR_ONE(loopvars_T);
2771 if (loopvars == NULL)
2772 return FAIL;
2773
2774 loopvars->lvs_ga.ga_len = endloop->end_var_count;
2775 stack = ALLOC_CLEAR_MULT(typval_T, loopvars->lvs_ga.ga_len);
2776 loopvars->lvs_ga.ga_data = stack;
2777 if (stack == NULL)
2778 {
2779 vim_free(loopvars);
2780 return FAIL;
2781 }
2782 add_loopvars_to_list(loopvars);
2783
2784 // Move the variable values.
2785 for (idx = 0; idx < endloop->end_var_count; ++idx)
2786 {
2787 typval_T *tv = STACK_TV_VAR(endloop->end_var_idx + idx);
2788
2789 *(stack + idx) = *tv;
2790 tv->v_type = VAR_UNKNOWN;
2791 }
2792
2793 for (idx = prev_closure_count; idx < gap->ga_len; ++idx)
2794 {
2795 partial_T *pt = ((partial_T **)gap->ga_data)[idx];
2796
Bram Moolenaarcc341812022-09-19 15:54:34 +01002797 if (pt->pt_refcount > 1 && pt->pt_loopvars[depth] == NULL)
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002798 {
2799 ++loopvars->lvs_refcount;
Bram Moolenaarcc341812022-09-19 15:54:34 +01002800 pt->pt_loopvars[depth] = loopvars;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002801
Bram Moolenaarcc341812022-09-19 15:54:34 +01002802 pt->pt_outer.out_loop[depth].stack = &loopvars->lvs_ga;
2803 pt->pt_outer.out_loop[depth].var_idx -=
2804 ectx->ec_frame_idx + STACK_FRAME_SIZE + endloop->end_var_idx;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002805 }
2806 }
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002807
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002808 return OK;
2809}
2810
2811/*
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002812 * Called when a partial is freed or its reference count goes down to one. The
2813 * loopvars may be the only reference to the partials in the local variables.
2814 * Go over all of them, the funcref and can be freed if all partials
2815 * referencing the loopvars have a reference count of one.
Bram Moolenaarcc341812022-09-19 15:54:34 +01002816 * Return TRUE if it was freed.
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002817 */
Bram Moolenaarcc341812022-09-19 15:54:34 +01002818 int
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002819loopvars_check_refcount(loopvars_T *loopvars)
2820{
2821 int i;
2822 garray_T *gap = &loopvars->lvs_ga;
2823 int done = 0;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002824 typval_T *stack = gap->ga_data;
2825
Bram Moolenaarcc341812022-09-19 15:54:34 +01002826 if (loopvars->lvs_refcount > loopvars->lvs_min_refcount)
2827 return FALSE;
2828 for (i = 0; i < gap->ga_len; ++i)
2829 {
2830 typval_T *tv = ((typval_T *)gap->ga_data) + i;
2831
2832 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL
2833 && tv->vval.v_partial->pt_refcount == 1)
2834 {
2835 int depth;
2836
2837 for (depth = 0; depth < MAX_LOOP_DEPTH; ++depth)
2838 if (tv->vval.v_partial->pt_loopvars[depth] == loopvars)
2839 ++done;
2840 }
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002841 }
Bram Moolenaarcc341812022-09-19 15:54:34 +01002842 if (done != loopvars->lvs_min_refcount)
2843 return FALSE;
2844
2845 // All partials referencing the loopvars have a reference count of
2846 // one, thus the loopvars is no longer of use.
2847 stack = gap->ga_data;
2848 for (i = 0; i < gap->ga_len; ++i)
2849 clear_tv(stack + i);
2850 vim_free(stack);
2851 remove_loopvars_from_list(loopvars);
2852 vim_free(loopvars);
2853 return TRUE;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002854}
2855
2856/*
2857 * For garbage collecting: set references in all variables referenced by
2858 * all loopvars.
2859 */
2860 int
2861set_ref_in_loopvars(int copyID)
2862{
2863 loopvars_T *loopvars;
2864
2865 for (loopvars = first_loopvars; loopvars != NULL;
2866 loopvars = loopvars->lvs_next)
2867 {
2868 typval_T *stack = loopvars->lvs_ga.ga_data;
2869 int i;
2870
2871 for (i = 0; i < loopvars->lvs_ga.ga_len; ++i)
2872 if (set_ref_in_item(stack + i, copyID, NULL, NULL))
2873 return TRUE; // abort
2874 }
2875 return FALSE;
2876}
2877
2878/*
Bram Moolenaar06b77222022-01-25 15:51:56 +00002879 * Load instruction for w:/b:/g:/t: variable.
2880 * "isn_type" is used instead of "iptr->isn_type".
2881 */
2882 static int
2883load_namespace_var(ectx_T *ectx, isntype_T isn_type, isn_T *iptr)
2884{
2885 dictitem_T *di = NULL;
2886 hashtab_T *ht = NULL;
2887 char namespace;
2888
2889 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
2890 return NOTDONE;
2891 switch (isn_type)
2892 {
2893 case ISN_LOADG:
2894 ht = get_globvar_ht();
2895 namespace = 'g';
2896 break;
2897 case ISN_LOADB:
2898 ht = &curbuf->b_vars->dv_hashtab;
2899 namespace = 'b';
2900 break;
2901 case ISN_LOADW:
2902 ht = &curwin->w_vars->dv_hashtab;
2903 namespace = 'w';
2904 break;
2905 case ISN_LOADT:
2906 ht = &curtab->tp_vars->dv_hashtab;
2907 namespace = 't';
2908 break;
2909 default: // Cannot reach here
2910 return NOTDONE;
2911 }
2912 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
2913
Bram Moolenaar06b77222022-01-25 15:51:56 +00002914 if (di == NULL)
2915 {
Bram Moolenaarfe732552022-02-22 19:39:13 +00002916 if (isn_type == ISN_LOADG)
2917 {
2918 ufunc_T *ufunc = find_func(iptr->isn_arg.string, TRUE);
2919
2920 // g:Something could be a function
2921 if (ufunc != NULL)
2922 {
2923 typval_T *tv = STACK_TV_BOT(0);
2924
2925 ++ectx->ec_stack.ga_len;
2926 tv->v_type = VAR_FUNC;
2927 tv->vval.v_string = alloc(STRLEN(iptr->isn_arg.string) + 3);
2928 if (tv->vval.v_string == NULL)
2929 return FAIL;
2930 STRCPY(tv->vval.v_string, "g:");
2931 STRCPY(tv->vval.v_string + 2, iptr->isn_arg.string);
2932 return OK;
2933 }
2934 }
Bram Moolenaar06b77222022-01-25 15:51:56 +00002935 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarfe732552022-02-22 19:39:13 +00002936 if (vim_strchr(iptr->isn_arg.string, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar06b77222022-01-25 15:51:56 +00002937 // no check if the item exists in the script but
2938 // isn't exported, it is too complicated
Bram Moolenaarfe732552022-02-22 19:39:13 +00002939 semsg(_(e_item_not_found_in_script_str), iptr->isn_arg.string);
Bram Moolenaar06b77222022-01-25 15:51:56 +00002940 else
2941 semsg(_(e_undefined_variable_char_str),
Bram Moolenaarfe732552022-02-22 19:39:13 +00002942 namespace, iptr->isn_arg.string);
Bram Moolenaar06b77222022-01-25 15:51:56 +00002943 return FAIL;
2944 }
2945 else
2946 {
2947 copy_tv(&di->di_tv, STACK_TV_BOT(0));
2948 ++ectx->ec_stack.ga_len;
2949 }
2950 return OK;
2951}
2952
2953/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02002954 * Execute instructions in execution context "ectx".
2955 * Return OK or FAIL;
2956 */
2957 static int
2958exec_instructions(ectx_T *ectx)
2959{
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002960 int ret = FAIL;
2961 int save_trylevel_at_start = ectx->ec_trylevel_at_start;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02002962 int dict_stack_len_at_start = dict_stack.ga_len;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01002963
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02002964 // Start execution at the first instruction.
Bram Moolenaar4c137212021-04-19 16:48:48 +02002965 ectx->ec_iidx = 0;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01002966
Bram Moolenaarff652882021-05-16 15:24:49 +02002967 // Only catch exceptions in this instruction list.
2968 ectx->ec_trylevel_at_start = trylevel;
2969
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002970 for (;;)
2971 {
Bram Moolenaara7490192021-07-22 12:26:14 +02002972 static int breakcheck_count = 0; // using "static" makes it faster
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002973 isn_T *iptr;
Bram Moolenaara7490192021-07-22 12:26:14 +02002974 typval_T *tv;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002975
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002976 if (unlikely(++breakcheck_count >= 100))
Bram Moolenaar270d0382020-05-15 21:42:53 +02002977 {
2978 line_breakcheck();
2979 breakcheck_count = 0;
2980 }
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002981 if (unlikely(got_int))
Bram Moolenaar20431c92020-03-20 18:39:46 +01002982 {
2983 // Turn CTRL-C into an exception.
2984 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +01002985 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002986 goto theend;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002987 did_throw = TRUE;
2988 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002989
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002990 if (unlikely(did_emsg && msg_list != NULL && *msg_list != NULL))
Bram Moolenaara26b9702020-04-18 19:53:28 +02002991 {
2992 // Turn an error message into an exception.
2993 did_emsg = FALSE;
2994 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002995 goto theend;
Bram Moolenaara26b9702020-04-18 19:53:28 +02002996 did_throw = TRUE;
2997 *msg_list = NULL;
2998 }
2999
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003000 if (unlikely(did_throw))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003001 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003002 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003003 trycmd_T *trycmd = NULL;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003004 int index = trystack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003005
3006 // An exception jumps to the first catch, finally, or returns from
3007 // the current function.
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003008 while (index > 0)
3009 {
3010 trycmd = ((trycmd_T *)trystack->ga_data) + index - 1;
Bram Moolenaar834193a2021-06-30 20:39:15 +02003011 if (!trycmd->tcd_in_catch || trycmd->tcd_finally_idx != 0)
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003012 break;
3013 // In the catch and finally block of this try we have to go up
3014 // one level.
3015 --index;
3016 trycmd = NULL;
3017 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003018 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003019 {
Bram Moolenaar834193a2021-06-30 20:39:15 +02003020 if (trycmd->tcd_in_catch)
3021 {
3022 // exception inside ":catch", jump to ":finally" once
3023 ectx->ec_iidx = trycmd->tcd_finally_idx;
3024 trycmd->tcd_finally_idx = 0;
3025 }
3026 else
3027 // jump to first ":catch"
3028 ectx->ec_iidx = trycmd->tcd_catch_idx;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003029 trycmd->tcd_in_catch = TRUE;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003030 did_throw = FALSE; // don't come back here until :endtry
3031 trycmd->tcd_did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003032 }
3033 else
3034 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02003035 // Not inside try or need to return from current functions.
3036 // Push a dummy return value.
Bram Moolenaar35578162021-08-02 19:10:38 +02003037 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003038 goto theend;
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02003039 tv = STACK_TV_BOT(0);
3040 tv->v_type = VAR_NUMBER;
3041 tv->vval.v_number = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003042 ++ectx->ec_stack.ga_len;
3043 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003044 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02003045 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +01003046 need_rethrow = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003047 if (handle_closure_in_use(ectx, FALSE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003048 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003049 goto done;
3050 }
3051
Bram Moolenaar4c137212021-04-19 16:48:48 +02003052 if (func_return(ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003053 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003054 }
3055 continue;
3056 }
3057
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003058 /*
3059 * Big switch on the instruction. Most compilers will be turning this
3060 * into an efficient lookup table, since the "case" values are an enum
3061 * with sequential numbers. It may look ugly, but it should be the
3062 * most efficient way.
3063 */
Bram Moolenaar4c137212021-04-19 16:48:48 +02003064 iptr = &ectx->ec_instr[ectx->ec_iidx++];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003065 switch (iptr->isn_type)
3066 {
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00003067 // Constructor, first instruction in a new() method.
Bram Moolenaar00b28d62022-12-08 15:32:33 +00003068 case ISN_CONSTRUCT:
3069 // "this" is always the local variable at index zero
3070 tv = STACK_TV_VAR(0);
3071 tv->v_type = VAR_OBJECT;
3072 tv->vval.v_object = alloc_clear(
3073 iptr->isn_arg.construct.construct_size);
3074 tv->vval.v_object->obj_class =
3075 iptr->isn_arg.construct.construct_class;
Bram Moolenaard28d7b92022-12-08 20:42:00 +00003076 ++tv->vval.v_object->obj_class->class_refcount;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00003077 tv->vval.v_object->obj_refcount = 1;
Bram Moolenaard28d7b92022-12-08 20:42:00 +00003078 object_created(tv->vval.v_object);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00003079 break;
3080
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003081 // execute Ex command line
3082 case ISN_EXEC:
Bram Moolenaaraacc9662021-08-13 19:40:51 +02003083 if (exec_command(iptr) == FAIL)
3084 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003085 break;
3086
Bram Moolenaar20677332021-06-06 17:02:53 +02003087 // execute Ex command line split at NL characters.
3088 case ISN_EXEC_SPLIT:
3089 {
3090 source_cookie_T cookie;
Bram Moolenaar518df272021-06-06 17:34:13 +02003091 char_u *line;
Bram Moolenaar20677332021-06-06 17:02:53 +02003092
3093 SOURCING_LNUM = iptr->isn_lnum;
3094 CLEAR_FIELD(cookie);
3095 cookie.sourcing_lnum = iptr->isn_lnum - 1;
3096 cookie.nextline = iptr->isn_arg.string;
Bram Moolenaar518df272021-06-06 17:34:13 +02003097 line = get_split_sourceline(0, &cookie, 0, 0);
3098 if (do_cmdline(line,
Bram Moolenaar20677332021-06-06 17:02:53 +02003099 get_split_sourceline, &cookie,
3100 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED)
3101 == FAIL
3102 || did_emsg)
Bram Moolenaar518df272021-06-06 17:34:13 +02003103 {
3104 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02003105 goto on_error;
Bram Moolenaar518df272021-06-06 17:34:13 +02003106 }
3107 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02003108 }
3109 break;
3110
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00003111 // execute Ex command line that is only a range
3112 case ISN_EXECRANGE:
3113 {
3114 exarg_T ea;
3115 char *error = NULL;
3116
3117 CLEAR_FIELD(ea);
3118 ea.cmdidx = CMD_SIZE;
3119 ea.addr_type = ADDR_LINES;
3120 ea.cmd = iptr->isn_arg.string;
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00003121 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00003122 parse_cmd_address(&ea, &error, FALSE);
Bram Moolenaar01a4dcb2021-12-04 13:15:10 +00003123 if (ea.cmd == NULL)
3124 goto on_error;
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00003125 // error is always NULL when using ADDR_LINES
3126 error = ex_range_without_command(&ea);
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00003127 if (error != NULL)
3128 {
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00003129 emsg(error);
3130 goto on_error;
3131 }
3132 }
3133 break;
3134
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02003135 // Evaluate an expression with legacy syntax, push it onto the
3136 // stack.
3137 case ISN_LEGACY_EVAL:
3138 {
3139 char_u *arg = iptr->isn_arg.string;
3140 int res;
3141 int save_flags = cmdmod.cmod_flags;
3142
Bram Moolenaar35578162021-08-02 19:10:38 +02003143 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003144 goto theend;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02003145 tv = STACK_TV_BOT(0);
3146 init_tv(tv);
3147 cmdmod.cmod_flags |= CMOD_LEGACY;
3148 res = eval0(arg, tv, NULL, &EVALARG_EVALUATE);
3149 cmdmod.cmod_flags = save_flags;
3150 if (res == FAIL)
3151 goto on_error;
3152 ++ectx->ec_stack.ga_len;
3153 }
3154 break;
3155
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003156 // push typeval VAR_INSTR with instructions to be executed
3157 case ISN_INSTR:
3158 {
Bram Moolenaar35578162021-08-02 19:10:38 +02003159 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003160 goto theend;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003161 tv = STACK_TV_BOT(0);
3162 tv->vval.v_instr = ALLOC_ONE(instr_T);
3163 if (tv->vval.v_instr == NULL)
3164 goto on_error;
3165 ++ectx->ec_stack.ga_len;
3166
3167 tv->v_type = VAR_INSTR;
3168 tv->vval.v_instr->instr_ectx = ectx;
3169 tv->vval.v_instr->instr_instr = iptr->isn_arg.instr;
3170 }
3171 break;
3172
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003173 case ISN_SOURCE:
3174 {
Bram Moolenaar89445512022-04-14 12:58:23 +01003175 int notused;
LemonBoy77142312022-04-15 20:50:46 +01003176
Bram Moolenaar89445512022-04-14 12:58:23 +01003177 SOURCING_LNUM = iptr->isn_lnum;
3178 if (may_load_script((int)iptr->isn_arg.number, &notused)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003179 == FAIL)
Bram Moolenaar89445512022-04-14 12:58:23 +01003180 goto on_error;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003181 }
3182 break;
3183
Bram Moolenaar4c137212021-04-19 16:48:48 +02003184 // execute :substitute with an expression
3185 case ISN_SUBSTITUTE:
3186 {
3187 subs_T *subs = &iptr->isn_arg.subs;
3188 source_cookie_T cookie;
3189 struct subs_expr_S *save_instr = substitute_instr;
3190 struct subs_expr_S subs_instr;
3191 int res;
3192
3193 subs_instr.subs_ectx = ectx;
3194 subs_instr.subs_instr = subs->subs_instr;
3195 subs_instr.subs_status = OK;
3196 substitute_instr = &subs_instr;
3197
3198 SOURCING_LNUM = iptr->isn_lnum;
3199 // This is very much like ISN_EXEC
3200 CLEAR_FIELD(cookie);
3201 cookie.sourcing_lnum = iptr->isn_lnum - 1;
3202 res = do_cmdline(subs->subs_cmd,
3203 getsourceline, &cookie,
3204 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
3205 substitute_instr = save_instr;
3206
3207 if (res == FAIL || did_emsg
3208 || subs_instr.subs_status == FAIL)
3209 goto on_error;
3210 }
3211 break;
3212
3213 case ISN_FINISH:
3214 goto done;
3215
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02003216 case ISN_REDIRSTART:
3217 // create a dummy entry for var_redir_str()
3218 if (alloc_redir_lval() == FAIL)
3219 goto on_error;
3220
3221 // The output is stored in growarray "redir_ga" until
3222 // redirection ends.
3223 init_redir_ga();
3224 redir_vname = 1;
3225 break;
3226
3227 case ISN_REDIREND:
3228 {
3229 char_u *res = get_clear_redir_ga();
3230
3231 // End redirection, put redirected text on the stack.
3232 clear_redir_lval();
3233 redir_vname = 0;
3234
Bram Moolenaar35578162021-08-02 19:10:38 +02003235 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02003236 {
3237 vim_free(res);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003238 goto theend;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02003239 }
3240 tv = STACK_TV_BOT(0);
3241 tv->v_type = VAR_STRING;
3242 tv->vval.v_string = res;
3243 ++ectx->ec_stack.ga_len;
3244 }
3245 break;
3246
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003247 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02003248#ifdef FEAT_QUICKFIX
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003249 force_abort = TRUE;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003250 if (trigger_cexpr_autocmd(iptr->isn_arg.number) == FAIL)
3251 goto on_error;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003252 force_abort = FALSE;
Bram Moolenaarb7c97812021-05-05 22:51:39 +02003253#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003254 break;
3255
3256 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02003257#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003258 {
3259 exarg_T ea;
3260 int res;
3261
3262 CLEAR_FIELD(ea);
3263 ea.cmdidx = iptr->isn_arg.cexpr.cexpr_ref->cer_cmdidx;
3264 ea.forceit = iptr->isn_arg.cexpr.cexpr_ref->cer_forceit;
3265 ea.cmdlinep = &iptr->isn_arg.cexpr.cexpr_ref->cer_cmdline;
3266 --ectx->ec_stack.ga_len;
3267 tv = STACK_TV_BOT(0);
Bram Moolenaarbd683e32022-07-18 17:49:03 +01003268 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003269 res = cexpr_core(&ea, tv);
3270 clear_tv(tv);
3271 if (res == FAIL)
3272 goto on_error;
3273 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02003274#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003275 break;
3276
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003277 // execute Ex command from pieces on the stack
3278 case ISN_EXECCONCAT:
3279 {
3280 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +02003281 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003282 int pass;
3283 int i;
3284 char_u *cmd = NULL;
3285 char_u *str;
3286
3287 for (pass = 1; pass <= 2; ++pass)
3288 {
3289 for (i = 0; i < count; ++i)
3290 {
3291 tv = STACK_TV_BOT(i - count);
3292 str = tv->vval.v_string;
3293 if (str != NULL && *str != NUL)
3294 {
3295 if (pass == 2)
3296 STRCPY(cmd + len, str);
3297 len += STRLEN(str);
3298 }
3299 if (pass == 2)
3300 clear_tv(tv);
3301 }
3302 if (pass == 1)
3303 {
3304 cmd = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003305 if (unlikely(cmd == NULL))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003306 goto theend;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003307 len = 0;
3308 }
3309 }
3310
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02003311 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003312 do_cmdline_cmd(cmd);
3313 vim_free(cmd);
3314 }
3315 break;
3316
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003317 // execute :echo {string} ...
3318 case ISN_ECHO:
3319 {
3320 int count = iptr->isn_arg.echo.echo_count;
3321 int atstart = TRUE;
3322 int needclr = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003323 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003324
3325 for (idx = 0; idx < count; ++idx)
3326 {
3327 tv = STACK_TV_BOT(idx - count);
3328 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
3329 &atstart, &needclr);
3330 clear_tv(tv);
3331 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01003332 if (needclr)
3333 msg_clr_eos();
Bram Moolenaar4c137212021-04-19 16:48:48 +02003334 ectx->ec_stack.ga_len -= count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003335 }
3336 break;
3337
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003338 // :execute {string} ...
3339 // :echomsg {string} ...
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01003340 // :echowindow {string} ...
Bram Moolenaar7de62622021-08-07 15:05:47 +02003341 // :echoconsole {string} ...
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003342 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01003343 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003344 case ISN_ECHOMSG:
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01003345 case ISN_ECHOWINDOW:
Bram Moolenaar7de62622021-08-07 15:05:47 +02003346 case ISN_ECHOCONSOLE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003347 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01003348 {
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01003349 int count;
Bram Moolenaarad39c092020-02-26 18:23:43 +01003350 garray_T ga;
3351 char_u buf[NUMBUFLEN];
3352 char_u *p;
3353 int len;
3354 int failed = FALSE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003355 int idx;
Bram Moolenaarad39c092020-02-26 18:23:43 +01003356
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01003357 if (iptr->isn_type == ISN_ECHOWINDOW)
3358 count = iptr->isn_arg.echowin.ewin_count;
3359 else
3360 count = iptr->isn_arg.number;
Bram Moolenaarad39c092020-02-26 18:23:43 +01003361 ga_init2(&ga, 1, 80);
3362 for (idx = 0; idx < count; ++idx)
3363 {
3364 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003365 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01003366 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003367 if (tv->v_type == VAR_CHANNEL
3368 || tv->v_type == VAR_JOB)
3369 {
3370 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar68db9962021-05-09 23:19:22 +02003371 semsg(_(e_using_invalid_value_as_string_str),
3372 vartype_name(tv->v_type));
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003373 break;
3374 }
3375 else
3376 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01003377 }
3378 else
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003379 p = tv_stringify(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01003380
3381 len = (int)STRLEN(p);
Bram Moolenaar35578162021-08-02 19:10:38 +02003382 if (GA_GROW_FAILS(&ga, len + 2))
Bram Moolenaarad39c092020-02-26 18:23:43 +01003383 failed = TRUE;
3384 else
3385 {
3386 if (ga.ga_len > 0)
3387 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
3388 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
3389 ga.ga_len += len;
3390 }
3391 clear_tv(tv);
3392 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003393 ectx->ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003394 if (failed)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003395 {
3396 ga_clear(&ga);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003397 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003398 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01003399
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003400 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003401 {
3402 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02003403 {
3404 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003405 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01003406 if (did_emsg)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003407 {
3408 ga_clear(&ga);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01003409 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003410 }
Bram Moolenaar430deb12020-08-23 16:29:11 +02003411 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003412 else
3413 {
3414 msg_sb_eol();
3415 if (iptr->isn_type == ISN_ECHOMSG)
3416 {
3417 msg_attr(ga.ga_data, echo_attr);
3418 out_flush();
3419 }
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01003420#ifdef HAS_MESSAGE_WINDOW
3421 else if (iptr->isn_type == ISN_ECHOWINDOW)
3422 {
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01003423 start_echowindow(
3424 iptr->isn_arg.echowin.ewin_time);
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01003425 msg_attr(ga.ga_data, echo_attr);
3426 end_echowindow();
3427 }
3428#endif
Bram Moolenaar7de62622021-08-07 15:05:47 +02003429 else if (iptr->isn_type == ISN_ECHOCONSOLE)
3430 {
3431 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data),
3432 TRUE);
3433 ui_write((char_u *)"\r\n", 2, TRUE);
3434 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003435 else
3436 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003437 SOURCING_LNUM = iptr->isn_lnum;
3438 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003439 }
3440 }
3441 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01003442 ga_clear(&ga);
3443 }
3444 break;
3445
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003446 // load local variable or argument
3447 case ISN_LOAD:
Bram Moolenaar35578162021-08-02 19:10:38 +02003448 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003449 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003450 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003451 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003452 break;
3453
3454 // load v: variable
3455 case ISN_LOADV:
Bram Moolenaar35578162021-08-02 19:10:38 +02003456 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003457 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003458 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003459 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003460 break;
3461
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003462 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003463 case ISN_LOADSCRIPT:
3464 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01003465 scriptref_T *sref = iptr->isn_arg.script.scriptref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003466 svar_T *sv;
3467
Bram Moolenaar6db660b2021-08-01 14:08:54 +02003468 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003469 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003470 goto theend;
Bram Moolenaar859cc212022-03-28 15:22:35 +01003471 allocate_if_null(sv);
Bram Moolenaar35578162021-08-02 19:10:38 +02003472 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003473 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003474 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003475 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003476 }
3477 break;
3478
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003479 // load s: variable in old script or autoload import
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003480 case ISN_LOADS:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003481 case ISN_LOADEXPORT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003482 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003483 int sid = iptr->isn_arg.loadstore.ls_sid;
3484 hashtab_T *ht = &SCRIPT_VARS(sid);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003485 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003486 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003487
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003488 if (di == NULL)
3489 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003490 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003491 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003492 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003493 }
3494 else
3495 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003496 if (iptr->isn_type == ISN_LOADEXPORT)
3497 {
3498 int idx = get_script_item_idx(sid, name, 0,
3499 NULL, NULL);
3500 svar_T *sv;
3501
3502 if (idx >= 0)
3503 {
3504 sv = ((svar_T *)SCRIPT_ITEM(sid)
3505 ->sn_var_vals.ga_data) + idx;
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003506 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003507 {
3508 SOURCING_LNUM = iptr->isn_lnum;
3509 semsg(_(e_item_not_exported_in_script_str),
3510 name);
3511 goto on_error;
3512 }
3513 }
3514 }
Bram Moolenaar35578162021-08-02 19:10:38 +02003515 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003516 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003517 copy_tv(&di->di_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003518 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003519 }
3520 }
3521 break;
3522
Bram Moolenaard3aac292020-04-19 14:32:17 +02003523 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003524 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02003525 case ISN_LOADB:
3526 case ISN_LOADW:
3527 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003528 {
Bram Moolenaar06b77222022-01-25 15:51:56 +00003529 int res = load_namespace_var(ectx, iptr->isn_type, iptr);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003530
Bram Moolenaar06b77222022-01-25 15:51:56 +00003531 if (res == NOTDONE)
Bram Moolenaarcbbc48f2022-01-18 12:58:28 +00003532 goto theend;
Bram Moolenaar06b77222022-01-25 15:51:56 +00003533 if (res == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003534 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003535 }
Bram Moolenaar06b77222022-01-25 15:51:56 +00003536
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003537 break;
3538
Bram Moolenaar03290b82020-12-19 16:30:44 +01003539 // load autoload variable
3540 case ISN_LOADAUTO:
3541 {
3542 char_u *name = iptr->isn_arg.string;
3543
Bram Moolenaar35578162021-08-02 19:10:38 +02003544 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003545 goto theend;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003546 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003547 if (eval_variable(name, (int)STRLEN(name), 0,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003548 STACK_TV_BOT(0), NULL, EVAL_VAR_VERBOSE) == FAIL)
Bram Moolenaar03290b82020-12-19 16:30:44 +01003549 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003550 ++ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003551 }
3552 break;
3553
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003554 // load g:/b:/w:/t: namespace
3555 case ISN_LOADGDICT:
3556 case ISN_LOADBDICT:
3557 case ISN_LOADWDICT:
3558 case ISN_LOADTDICT:
3559 {
3560 dict_T *d = NULL;
3561
3562 switch (iptr->isn_type)
3563 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02003564 case ISN_LOADGDICT: d = get_globvar_dict(); break;
3565 case ISN_LOADBDICT: d = curbuf->b_vars; break;
3566 case ISN_LOADWDICT: d = curwin->w_vars; break;
3567 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003568 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003569 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003570 }
Bram Moolenaar35578162021-08-02 19:10:38 +02003571 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003572 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003573 tv = STACK_TV_BOT(0);
3574 tv->v_type = VAR_DICT;
3575 tv->v_lock = 0;
3576 tv->vval.v_dict = d;
Bram Moolenaar1bd3cb22021-02-24 12:27:31 +01003577 ++d->dv_refcount;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003578 ++ectx->ec_stack.ga_len;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003579 }
3580 break;
3581
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003582 // load &option
3583 case ISN_LOADOPT:
3584 {
3585 typval_T optval;
3586 char_u *name = iptr->isn_arg.string;
3587
Bram Moolenaara8c17702020-04-01 21:17:24 +02003588 // This is not expected to fail, name is checked during
3589 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar35578162021-08-02 19:10:38 +02003590 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003591 goto theend;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003592 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003593 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003594 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003595 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003596 }
3597 break;
3598
3599 // load $ENV
3600 case ISN_LOADENV:
3601 {
3602 typval_T optval;
3603 char_u *name = iptr->isn_arg.string;
3604
Bram Moolenaar35578162021-08-02 19:10:38 +02003605 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003606 goto theend;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003607 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003608 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003609 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003610 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003611 }
3612 break;
3613
3614 // load @register
3615 case ISN_LOADREG:
Bram Moolenaar35578162021-08-02 19:10:38 +02003616 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003617 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003618 tv = STACK_TV_BOT(0);
3619 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003620 tv->v_lock = 0;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02003621 // This may result in NULL, which should be equivalent to an
3622 // empty string.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003623 tv->vval.v_string = get_reg_contents(
3624 iptr->isn_arg.number, GREG_EXPR_SRC);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003625 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003626 break;
3627
3628 // store local variable
3629 case ISN_STORE:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003630 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003631 tv = STACK_TV_VAR(iptr->isn_arg.number);
3632 clear_tv(tv);
3633 *tv = *STACK_TV_BOT(0);
3634 break;
3635
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003636 // store s: variable in old script or autoload import
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003637 case ISN_STORES:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003638 case ISN_STOREEXPORT:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003639 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003640 int sid = iptr->isn_arg.loadstore.ls_sid;
3641 hashtab_T *ht = &SCRIPT_VARS(sid);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003642 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003643 dictitem_T *di = find_var_in_ht(ht, 0,
3644 iptr->isn_type == ISN_STORES
3645 ? name + 2 : name, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003646
Bram Moolenaar4c137212021-04-19 16:48:48 +02003647 --ectx->ec_stack.ga_len;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003648 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003649 if (di == NULL)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003650 {
3651 if (iptr->isn_type == ISN_STOREEXPORT)
3652 {
3653 semsg(_(e_undefined_variable_str), name);
Bram Moolenaard1d26842022-03-31 10:13:47 +01003654 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003655 goto on_error;
3656 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003657 store_var(name, STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003658 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003659 else
3660 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003661 if (iptr->isn_type == ISN_STOREEXPORT)
3662 {
3663 int idx = get_script_item_idx(sid, name, 0,
3664 NULL, NULL);
3665
Bram Moolenaar06651632022-04-27 17:54:25 +01003666 // can this ever fail?
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003667 if (idx >= 0)
3668 {
3669 svar_T *sv = ((svar_T *)SCRIPT_ITEM(sid)
3670 ->sn_var_vals.ga_data) + idx;
3671
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003672 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003673 {
3674 semsg(_(e_item_not_exported_in_script_str),
3675 name);
Bram Moolenaard1d26842022-03-31 10:13:47 +01003676 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003677 goto on_error;
3678 }
3679 }
3680 }
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02003681 if (var_check_permission(di, name) == FAIL)
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003682 {
3683 clear_tv(STACK_TV_BOT(0));
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02003684 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003685 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003686 clear_tv(&di->di_tv);
3687 di->di_tv = *STACK_TV_BOT(0);
3688 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003689 }
3690 break;
3691
3692 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003693 case ISN_STORESCRIPT:
3694 {
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003695 scriptref_T *sref = iptr->isn_arg.script.scriptref;
3696 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003697
Bram Moolenaar6db660b2021-08-01 14:08:54 +02003698 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003699 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003700 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003701 --ectx->ec_stack.ga_len;
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003702
3703 // "const" and "final" are checked at compile time, locking
3704 // the value needs to be checked here.
3705 SOURCING_LNUM = iptr->isn_lnum;
3706 if (value_check_lock(sv->sv_tv->v_lock, sv->sv_name, FALSE))
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003707 {
3708 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003709 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003710 }
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003711
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003712 clear_tv(sv->sv_tv);
3713 *sv->sv_tv = *STACK_TV_BOT(0);
3714 }
3715 break;
3716
3717 // store option
3718 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003719 case ISN_STOREFUNCOPT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003720 {
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003721 char_u *opt_name = iptr->isn_arg.storeopt.so_name;
3722 int opt_flags = iptr->isn_arg.storeopt.so_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003723 long n = 0;
3724 char_u *s = NULL;
3725 char *msg;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003726 char_u numbuf[NUMBUFLEN];
3727 char_u *tofree = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003728
Bram Moolenaar4c137212021-04-19 16:48:48 +02003729 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003730 tv = STACK_TV_BOT(0);
3731 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01003732 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003733 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01003734 if (s == NULL)
3735 s = (char_u *)"";
3736 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003737 else if (iptr->isn_type == ISN_STOREFUNCOPT)
3738 {
3739 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003740 // If the option can be set to a function reference or
3741 // a lambda and the passed value is a function
3742 // reference, then convert it to the name (string) of
3743 // the function reference.
3744 s = tv2string(tv, &tofree, numbuf, 0);
3745 if (s == NULL || *s == NUL)
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003746 {
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003747 // cannot happen?
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003748 clear_tv(tv);
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003749 vim_free(tofree);
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003750 goto on_error;
3751 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003752 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003753 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003754 // must be VAR_NUMBER, CHECKTYPE makes sure
3755 n = tv->vval.v_number;
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003756 msg = set_option_value(opt_name, n, s, opt_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02003757 clear_tv(tv);
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003758 vim_free(tofree);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003759 if (msg != NULL)
3760 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003761 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003762 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02003763 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003764 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003765 }
3766 break;
3767
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003768 // store $ENV
3769 case ISN_STOREENV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003770 --ectx->ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003771 tv = STACK_TV_BOT(0);
3772 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
3773 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003774 break;
3775
3776 // store @r
3777 case ISN_STOREREG:
3778 {
3779 int reg = iptr->isn_arg.number;
3780
Bram Moolenaar4c137212021-04-19 16:48:48 +02003781 --ectx->ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01003782 tv = STACK_TV_BOT(0);
Bram Moolenaar74f4a962021-06-17 21:03:07 +02003783 write_reg_contents(reg, tv_get_string(tv), -1, FALSE);
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01003784 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003785 }
3786 break;
3787
3788 // store v: variable
3789 case ISN_STOREV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003790 --ectx->ec_stack.ga_len;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003791 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
3792 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003793 // should not happen, type is checked when compiling
3794 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003795 break;
3796
Bram Moolenaard3aac292020-04-19 14:32:17 +02003797 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003798 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02003799 case ISN_STOREB:
3800 case ISN_STOREW:
3801 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003802 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003803 dictitem_T *di;
3804 hashtab_T *ht;
3805 char_u *name = iptr->isn_arg.string + 2;
3806
Bram Moolenaard3aac292020-04-19 14:32:17 +02003807 switch (iptr->isn_type)
3808 {
3809 case ISN_STOREG:
3810 ht = get_globvar_ht();
3811 break;
3812 case ISN_STOREB:
3813 ht = &curbuf->b_vars->dv_hashtab;
3814 break;
3815 case ISN_STOREW:
3816 ht = &curwin->w_vars->dv_hashtab;
3817 break;
3818 case ISN_STORET:
3819 ht = &curtab->tp_vars->dv_hashtab;
3820 break;
3821 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003822 goto theend;
Bram Moolenaard3aac292020-04-19 14:32:17 +02003823 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003824
Bram Moolenaar4c137212021-04-19 16:48:48 +02003825 --ectx->ec_stack.ga_len;
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003826 di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003827 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003828 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003829 else
3830 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003831 SOURCING_LNUM = iptr->isn_lnum;
3832 if (var_check_permission(di, name) == FAIL)
3833 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003834 clear_tv(&di->di_tv);
3835 di->di_tv = *STACK_TV_BOT(0);
3836 }
3837 }
3838 break;
3839
Bram Moolenaar03290b82020-12-19 16:30:44 +01003840 // store an autoload variable
3841 case ISN_STOREAUTO:
3842 SOURCING_LNUM = iptr->isn_lnum;
3843 set_var(iptr->isn_arg.string, STACK_TV_BOT(-1), TRUE);
3844 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003845 --ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003846 break;
3847
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003848 // store number in local variable
3849 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01003850 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003851 clear_tv(tv);
3852 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01003853 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003854 break;
3855
Bram Moolenaar590162c2022-12-24 21:24:06 +00003856 // Store a value in a list, dict, blob or object variable.
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01003857 case ISN_STOREINDEX:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003858 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003859 int res = execute_storeindex(iptr, ectx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003860
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003861 if (res == FAIL)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02003862 goto on_error;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003863 if (res == NOTDONE)
3864 goto theend;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003865 }
3866 break;
3867
Bram Moolenaarea5c8982022-02-17 14:42:02 +00003868 // store value in list or blob range
Bram Moolenaar68452172021-04-12 21:21:02 +02003869 case ISN_STORERANGE:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003870 if (execute_storerange(iptr, ectx) == FAIL)
3871 goto on_error;
Bram Moolenaar68452172021-04-12 21:21:02 +02003872 break;
3873
Bram Moolenaard505d172022-12-18 21:42:55 +00003874 case ISN_LOAD_CLASSMEMBER:
3875 {
3876 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
3877 goto theend;
3878 classmember_T *cm = &iptr->isn_arg.classmember;
3879 *STACK_TV_BOT(0) =
3880 cm->cm_class->class_members_tv[cm->cm_idx];
3881 ++ectx->ec_stack.ga_len;
3882 }
3883 break;
3884
3885 case ISN_STORE_CLASSMEMBER:
3886 {
3887 classmember_T *cm = &iptr->isn_arg.classmember;
3888 tv = &cm->cm_class->class_members_tv[cm->cm_idx];
3889 clear_tv(tv);
3890 *tv = *STACK_TV_BOT(-1);
3891 --ectx->ec_stack.ga_len;
3892 }
3893 break;
3894
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01003895 // Load or store variable or argument from outer scope.
Bram Moolenaar0186e582021-01-10 18:33:11 +01003896 case ISN_LOADOUTER:
3897 case ISN_STOREOUTER:
3898 {
3899 int depth = iptr->isn_arg.outer.outer_depth;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02003900 outer_T *outer = ectx->ec_outer_ref == NULL ? NULL
3901 : ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003902
3903 while (depth > 1 && outer != NULL)
3904 {
3905 outer = outer->out_up;
3906 --depth;
3907 }
3908 if (outer == NULL)
3909 {
3910 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar69c76172021-12-02 16:38:52 +00003911 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx
3912 || ectx->ec_outer_ref == NULL)
3913 // Possibly :def function called from legacy
3914 // context.
3915 emsg(_(e_closure_called_from_invalid_context));
3916 else
3917 iemsg("LOADOUTER depth more than scope levels");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003918 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003919 }
Bram Moolenaarcc341812022-09-19 15:54:34 +01003920 if (depth < 0)
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01003921 // Variable declared in loop. May be copied if the
3922 // loop block has already ended.
Bram Moolenaarcc341812022-09-19 15:54:34 +01003923 tv = ((typval_T *)outer->out_loop[-depth - 1]
3924 .stack->ga_data)
3925 + outer->out_loop[-depth - 1].var_idx
3926 + iptr->isn_arg.outer.outer_idx;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01003927 else
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01003928 // Variable declared in a function. May be copied if
3929 // the function has already returned.
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01003930 tv = ((typval_T *)outer->out_stack->ga_data)
3931 + outer->out_frame_idx + STACK_FRAME_SIZE
3932 + iptr->isn_arg.outer.outer_idx;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003933 if (iptr->isn_type == ISN_LOADOUTER)
3934 {
Bram Moolenaar35578162021-08-02 19:10:38 +02003935 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003936 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003937 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003938 ++ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003939 }
3940 else
3941 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003942 --ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003943 clear_tv(tv);
3944 *tv = *STACK_TV_BOT(0);
3945 }
3946 }
3947 break;
3948
Bram Moolenaar752fc692021-01-04 21:57:11 +01003949 // unlet item in list or dict variable
3950 case ISN_UNLETINDEX:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003951 if (execute_unletindex(iptr, ectx) == FAIL)
3952 goto on_error;
Bram Moolenaar752fc692021-01-04 21:57:11 +01003953 break;
3954
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01003955 // unlet range of items in list variable
3956 case ISN_UNLETRANGE:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003957 if (execute_unletrange(iptr, ectx) == FAIL)
3958 goto on_error;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01003959 break;
3960
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003961 // push constant
3962 case ISN_PUSHNR:
3963 case ISN_PUSHBOOL:
3964 case ISN_PUSHSPEC:
3965 case ISN_PUSHF:
3966 case ISN_PUSHS:
3967 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003968 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003969 case ISN_PUSHCHANNEL:
3970 case ISN_PUSHJOB:
Bram Moolenaar35578162021-08-02 19:10:38 +02003971 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003972 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003973 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003974 tv->v_lock = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003975 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003976 switch (iptr->isn_type)
3977 {
3978 case ISN_PUSHNR:
3979 tv->v_type = VAR_NUMBER;
3980 tv->vval.v_number = iptr->isn_arg.number;
3981 break;
3982 case ISN_PUSHBOOL:
3983 tv->v_type = VAR_BOOL;
3984 tv->vval.v_number = iptr->isn_arg.number;
3985 break;
3986 case ISN_PUSHSPEC:
3987 tv->v_type = VAR_SPECIAL;
3988 tv->vval.v_number = iptr->isn_arg.number;
3989 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003990 case ISN_PUSHF:
3991 tv->v_type = VAR_FLOAT;
3992 tv->vval.v_float = iptr->isn_arg.fnumber;
3993 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003994 case ISN_PUSHBLOB:
3995 blob_copy(iptr->isn_arg.blob, tv);
3996 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003997 case ISN_PUSHFUNC:
3998 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01003999 if (iptr->isn_arg.string == NULL)
4000 tv->vval.v_string = NULL;
4001 else
4002 tv->vval.v_string =
4003 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004004 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004005 case ISN_PUSHCHANNEL:
4006#ifdef FEAT_JOB_CHANNEL
4007 tv->v_type = VAR_CHANNEL;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00004008 tv->vval.v_channel = NULL;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004009#endif
4010 break;
4011 case ISN_PUSHJOB:
4012#ifdef FEAT_JOB_CHANNEL
4013 tv->v_type = VAR_JOB;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00004014 tv->vval.v_job = NULL;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004015#endif
4016 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004017 default:
4018 tv->v_type = VAR_STRING;
Bram Moolenaarf8691002022-03-10 12:20:53 +00004019 tv->vval.v_string = iptr->isn_arg.string == NULL
4020 ? NULL : vim_strsave(iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004021 }
4022 break;
4023
Bram Moolenaar06b77222022-01-25 15:51:56 +00004024 case ISN_AUTOLOAD:
4025 {
4026 char_u *name = iptr->isn_arg.string;
4027
4028 (void)script_autoload(name, FALSE);
4029 if (find_func(name, TRUE))
4030 {
4031 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
4032 goto theend;
4033 tv = STACK_TV_BOT(0);
4034 tv->v_lock = 0;
4035 ++ectx->ec_stack.ga_len;
4036 tv->v_type = VAR_FUNC;
4037 tv->vval.v_string = vim_strsave(name);
4038 }
4039 else
4040 {
4041 int res = load_namespace_var(ectx, ISN_LOADG, iptr);
4042
4043 if (res == NOTDONE)
4044 goto theend;
4045 if (res == FAIL)
4046 goto on_error;
4047 }
4048 }
4049 break;
4050
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004051 case ISN_UNLET:
4052 if (do_unlet(iptr->isn_arg.unlet.ul_name,
4053 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02004054 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004055 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02004056 case ISN_UNLETENV:
LemonBoy77142312022-04-15 20:50:46 +01004057 vim_unsetenv_ext(iptr->isn_arg.unlet.ul_name);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02004058 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004059
Bram Moolenaaraacc9662021-08-13 19:40:51 +02004060 case ISN_LOCKUNLOCK:
4061 {
4062 typval_T *lval_root_save = lval_root;
4063 int res;
4064
4065 // Stack has the local variable, argument the whole :lock
4066 // or :unlock command, like ISN_EXEC.
4067 --ectx->ec_stack.ga_len;
4068 lval_root = STACK_TV_BOT(0);
4069 res = exec_command(iptr);
4070 clear_tv(lval_root);
4071 lval_root = lval_root_save;
4072 if (res == FAIL)
4073 goto on_error;
4074 }
4075 break;
4076
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02004077 case ISN_LOCKCONST:
4078 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
4079 break;
4080
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004081 // create a list from items on the stack; uses a single allocation
4082 // for the list header and the items
4083 case ISN_NEWLIST:
Bram Moolenaar4c137212021-04-19 16:48:48 +02004084 if (exe_newlist(iptr->isn_arg.number, ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004085 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004086 break;
4087
4088 // create a dict from items on the stack
4089 case ISN_NEWDICT:
4090 {
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01004091 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004092
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01004093 SOURCING_LNUM = iptr->isn_lnum;
4094 res = exe_newdict(iptr->isn_arg.number, ectx);
4095 if (res == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004096 goto theend;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01004097 if (res == MAYBE)
4098 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004099 }
4100 break;
4101
LemonBoy372bcce2022-04-25 12:43:20 +01004102 case ISN_CONCAT:
4103 if (exe_concat(iptr->isn_arg.number, ectx) == FAIL)
4104 goto theend;
4105 break;
4106
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004107 // create a partial with NULL value
4108 case ISN_NEWPARTIAL:
4109 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
4110 goto theend;
4111 ++ectx->ec_stack.ga_len;
4112 tv = STACK_TV_BOT(-1);
4113 tv->v_type = VAR_PARTIAL;
4114 tv->v_lock = 0;
4115 tv->vval.v_partial = NULL;
4116 break;
4117
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004118 // call a :def function
4119 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02004120 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01004121 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
4122 NULL,
4123 iptr->isn_arg.dfunc.cdf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02004124 ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02004125 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004126 break;
4127
4128 // call a builtin function
4129 case ISN_BCALL:
4130 SOURCING_LNUM = iptr->isn_lnum;
4131 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
4132 iptr->isn_arg.bfunc.cbf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02004133 ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004134 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004135 break;
4136
4137 // call a funcref or partial
4138 case ISN_PCALL:
4139 {
4140 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
4141 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02004142 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004143
4144 SOURCING_LNUM = iptr->isn_lnum;
4145 if (pfunc->cpf_top)
4146 {
4147 // funcref is above the arguments
4148 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
4149 }
4150 else
4151 {
4152 // Get the funcref from the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004153 --ectx->ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02004154 partial_tv = *STACK_TV_BOT(0);
4155 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004156 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004157 r = call_partial(tv, pfunc->cpf_argcount, ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02004158 if (tv == &partial_tv)
4159 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004160 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004161 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004162 }
4163 break;
4164
Bram Moolenaarbd5da372020-03-31 23:13:10 +02004165 case ISN_PCALL_END:
4166 // PCALL finished, arguments have been consumed and replaced by
4167 // the return value. Now clear the funcref from the stack,
4168 // and move the return value in its place.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004169 --ectx->ec_stack.ga_len;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02004170 clear_tv(STACK_TV_BOT(-1));
4171 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
4172 break;
4173
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004174 // call a user defined function or funcref/partial
4175 case ISN_UCALL:
4176 {
4177 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
4178
4179 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01004180 if (call_eval_func(cufunc->cuf_name, cufunc->cuf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02004181 ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004182 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004183 }
4184 break;
4185
Bram Moolenaar1d84f762022-09-03 21:35:53 +01004186 // :defer func(arg)
4187 case ISN_DEFER:
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00004188 case ISN_DEFEROBJ:
Bram Moolenaar806a2732022-09-04 15:40:36 +01004189 if (defer_command(iptr->isn_arg.defer.defer_var_idx,
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00004190 iptr->isn_type == ISN_DEFEROBJ,
Bram Moolenaar1d84f762022-09-03 21:35:53 +01004191 iptr->isn_arg.defer.defer_argcount, ectx) == FAIL)
4192 goto on_error;
4193 break;
4194
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004195 // Return from a :def function call without a value.
4196 // Return from a constructor.
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02004197 case ISN_RETURN_VOID:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004198 case ISN_RETURN_OBJECT:
Bram Moolenaar35578162021-08-02 19:10:38 +02004199 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004200 goto theend;
Bram Moolenaar299f3032021-01-08 20:53:09 +01004201 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004202 ++ectx->ec_stack.ga_len;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004203 if (iptr->isn_type == ISN_RETURN_VOID)
4204 {
4205 tv->v_type = VAR_VOID;
4206 tv->vval.v_number = 0;
4207 tv->v_lock = 0;
4208 }
4209 else
4210 {
4211 *tv = *STACK_TV_VAR(0);
4212 ++tv->vval.v_object->obj_refcount;
4213 }
Bram Moolenaar299f3032021-01-08 20:53:09 +01004214 // FALLTHROUGH
4215
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02004216 // return from a :def function call with what is on the stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004217 case ISN_RETURN:
4218 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004219 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01004220 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01004221
4222 if (trystack->ga_len > 0)
4223 trycmd = ((trycmd_T *)trystack->ga_data)
4224 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004225 if (trycmd != NULL
Bram Moolenaar4c137212021-04-19 16:48:48 +02004226 && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004227 {
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01004228 // jump to ":finally" or ":endtry"
4229 if (trycmd->tcd_finally_idx != 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004230 ectx->ec_iidx = trycmd->tcd_finally_idx;
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01004231 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02004232 ectx->ec_iidx = trycmd->tcd_endtry_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004233 trycmd->tcd_return = TRUE;
4234 }
4235 else
Bram Moolenaard032f342020-07-18 18:13:02 +02004236 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004237 }
4238 break;
4239
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004240 // push a partial, a reference to a compiled function
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004241 case ISN_FUNCREF:
4242 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004243 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
4244 ufunc_T *ufunc;
4245 funcref_T *funcref = &iptr->isn_arg.funcref;
4246 funcref_extra_T *extra = funcref->fr_extra;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004247
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004248 if (pt == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004249 goto theend;
Bram Moolenaar35578162021-08-02 19:10:38 +02004250 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02004251 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004252 vim_free(pt);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004253 goto theend;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004254 }
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004255 if (extra == NULL || extra->fre_func_name == NULL)
Bram Moolenaar38453522021-11-28 22:00:12 +00004256 {
4257 dfunc_T *pt_dfunc = ((dfunc_T *)def_functions.ga_data)
4258 + funcref->fr_dfunc_idx;
4259
4260 ufunc = pt_dfunc->df_ufunc;
4261 }
4262 else
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004263 ufunc = find_func(extra->fre_func_name, FALSE);
Bram Moolenaar56acd1f2022-02-18 13:24:52 +00004264 if (ufunc == NULL)
4265 {
4266 SOURCING_LNUM = iptr->isn_lnum;
4267 iemsg("ufunc unexpectedly NULL for FUNCREF");
4268 goto theend;
4269 }
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004270 if (fill_partial_and_closure(pt, ufunc,
Bram Moolenaarcc341812022-09-19 15:54:34 +01004271 extra == NULL ? NULL : &extra->fre_loopvar_info,
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004272 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004273 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004274 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004275 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004276 tv->vval.v_partial = pt;
4277 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02004278 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004279 }
4280 break;
4281
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004282 // Create a global function from a lambda.
4283 case ISN_NEWFUNC:
4284 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004285 newfuncarg_T *arg = iptr->isn_arg.newfunc.nf_arg;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004286
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004287 if (copy_lambda_to_global_func(arg->nfa_lambda,
Bram Moolenaarcc341812022-09-19 15:54:34 +01004288 arg->nfa_global, &arg->nfa_loopvar_info,
4289 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004290 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004291 }
4292 break;
4293
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004294 // List functions
4295 case ISN_DEF:
4296 if (iptr->isn_arg.string == NULL)
4297 list_functions(NULL);
4298 else
4299 {
Bram Moolenaar14336722022-01-08 16:02:59 +00004300 exarg_T ea;
4301 garray_T lines_to_free;
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004302
4303 CLEAR_FIELD(ea);
4304 ea.cmd = ea.arg = iptr->isn_arg.string;
Bram Moolenaar14336722022-01-08 16:02:59 +00004305 ga_init2(&lines_to_free, sizeof(char_u *), 50);
Bram Moolenaar554d0312023-01-05 19:59:18 +00004306 define_function(&ea, NULL, &lines_to_free, 0);
Bram Moolenaar14336722022-01-08 16:02:59 +00004307 ga_clear_strings(&lines_to_free);
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004308 }
4309 break;
4310
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004311 // jump if a condition is met
4312 case ISN_JUMP:
4313 {
4314 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004315 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004316 int jump = TRUE;
4317
4318 if (when != JUMP_ALWAYS)
4319 {
4320 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004321 if (when == JUMP_IF_COND_FALSE
Bram Moolenaar13106602020-10-04 16:06:05 +02004322 || when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004323 || when == JUMP_IF_COND_TRUE)
4324 {
4325 SOURCING_LNUM = iptr->isn_lnum;
4326 jump = tv_get_bool_chk(tv, &error);
4327 if (error)
4328 goto on_error;
4329 }
4330 else
4331 jump = tv2bool(tv);
Bram Moolenaarf6ced982022-04-28 12:00:49 +01004332 if (when == JUMP_IF_FALSE || when == JUMP_IF_COND_FALSE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004333 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01004334 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004335 {
4336 // drop the value from the stack
4337 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004338 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004339 }
4340 }
4341 if (jump)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004342 ectx->ec_iidx = iptr->isn_arg.jump.jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004343 }
4344 break;
4345
Bram Moolenaarb46c0832022-09-15 17:19:37 +01004346 // "while": jump to end if a condition is false
4347 case ISN_WHILE:
4348 {
4349 int error = FALSE;
4350 int jump = TRUE;
4351
4352 tv = STACK_TV_BOT(-1);
4353 SOURCING_LNUM = iptr->isn_lnum;
4354 jump = !tv_get_bool_chk(tv, &error);
4355 if (error)
4356 goto on_error;
4357 // drop the value from the stack
4358 clear_tv(tv);
4359 --ectx->ec_stack.ga_len;
4360 if (jump)
4361 ectx->ec_iidx = iptr->isn_arg.whileloop.while_end;
4362
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01004363 // Store the current funcref count, may be used by
Bram Moolenaarcc341812022-09-19 15:54:34 +01004364 // ISN_ENDLOOP later
Bram Moolenaarb46c0832022-09-15 17:19:37 +01004365 tv = STACK_TV_VAR(
4366 iptr->isn_arg.whileloop.while_funcref_idx);
4367 tv->vval.v_number = ectx->ec_funcrefs.ga_len;
4368 }
4369 break;
4370
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02004371 // Jump if an argument with a default value was already set and not
4372 // v:none.
4373 case ISN_JUMP_IF_ARG_SET:
Bram Moolenaar65b0d162022-12-13 18:43:22 +00004374 case ISN_JUMP_IF_ARG_NOT_SET:
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02004375 tv = STACK_TV_VAR(iptr->isn_arg.jumparg.jump_arg_off);
Bram Moolenaar65b0d162022-12-13 18:43:22 +00004376 int arg_set = tv->v_type != VAR_UNKNOWN
4377 && !(tv->v_type == VAR_SPECIAL
4378 && tv->vval.v_number == VVAL_NONE);
4379 if (iptr->isn_type == ISN_JUMP_IF_ARG_SET ? arg_set : !arg_set)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004380 ectx->ec_iidx = iptr->isn_arg.jumparg.jump_where;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02004381 break;
4382
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004383 // top of a for loop
4384 case ISN_FOR:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00004385 if (execute_for(iptr, ectx) == FAIL)
4386 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004387 break;
4388
Bram Moolenaarb46c0832022-09-15 17:19:37 +01004389 // end of a for or while loop
4390 case ISN_ENDLOOP:
4391 if (execute_endloop(iptr, ectx) == FAIL)
4392 goto theend;
4393 break;
4394
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004395 // start of ":try" block
4396 case ISN_TRY:
4397 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01004398 trycmd_T *trycmd = NULL;
4399
Bram Moolenaar35578162021-08-02 19:10:38 +02004400 if (GA_GROW_FAILS(&ectx->ec_trystack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004401 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004402 trycmd = ((trycmd_T *)ectx->ec_trystack.ga_data)
4403 + ectx->ec_trystack.ga_len;
4404 ++ectx->ec_trystack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004405 ++trylevel;
Bram Moolenaar8d4be892021-02-13 18:33:02 +01004406 CLEAR_POINTER(trycmd);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004407 trycmd->tcd_frame_idx = ectx->ec_frame_idx;
4408 trycmd->tcd_stack_len = ectx->ec_stack.ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01004409 trycmd->tcd_catch_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00004410 iptr->isn_arg.tryref.try_ref->try_catch;
Bram Moolenaara91a7132021-03-25 21:12:15 +01004411 trycmd->tcd_finally_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00004412 iptr->isn_arg.tryref.try_ref->try_finally;
Bram Moolenaara91a7132021-03-25 21:12:15 +01004413 trycmd->tcd_endtry_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00004414 iptr->isn_arg.tryref.try_ref->try_endtry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004415 }
4416 break;
4417
4418 case ISN_PUSHEXC:
4419 if (current_exception == NULL)
4420 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004421 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004422 iemsg("Evaluating catch while current_exception is NULL");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004423 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004424 }
Bram Moolenaar35578162021-08-02 19:10:38 +02004425 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004426 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004427 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004428 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004429 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02004430 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004431 tv->vval.v_string = vim_strsave(
4432 (char_u *)current_exception->value);
4433 break;
4434
4435 case ISN_CATCH:
4436 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004437 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar06651632022-04-27 17:54:25 +01004438 trycmd_T *trycmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004439
Bram Moolenaar4c137212021-04-19 16:48:48 +02004440 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar06651632022-04-27 17:54:25 +01004441 trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004442 + trystack->ga_len - 1;
Bram Moolenaar06651632022-04-27 17:54:25 +01004443 trycmd->tcd_caught = TRUE;
4444 trycmd->tcd_did_throw = FALSE;
4445
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004446 did_emsg = got_int = did_throw = FALSE;
Bram Moolenaar1430cee2021-01-17 19:20:32 +01004447 force_abort = need_rethrow = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004448 catch_exception(current_exception);
4449 }
4450 break;
4451
Bram Moolenaarc150c092021-02-13 15:02:46 +01004452 case ISN_TRYCONT:
4453 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004454 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004455 trycont_T *trycont = &iptr->isn_arg.trycont;
4456 int i;
4457 trycmd_T *trycmd;
4458 int iidx = trycont->tct_where;
4459
4460 if (trystack->ga_len < trycont->tct_levels)
4461 {
4462 siemsg("TRYCONT: expected %d levels, found %d",
4463 trycont->tct_levels, trystack->ga_len);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004464 goto theend;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004465 }
4466 // Make :endtry jump to any outer try block and the last
4467 // :endtry inside the loop to the loop start.
4468 for (i = trycont->tct_levels; i > 0; --i)
4469 {
4470 trycmd = ((trycmd_T *)trystack->ga_data)
4471 + trystack->ga_len - i;
Bram Moolenaar2e34c342021-03-14 12:13:33 +01004472 // Add one to tcd_cont to be able to jump to
4473 // instruction with index zero.
4474 trycmd->tcd_cont = iidx + 1;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01004475 iidx = trycmd->tcd_finally_idx == 0
4476 ? trycmd->tcd_endtry_idx : trycmd->tcd_finally_idx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004477 }
4478 // jump to :finally or :endtry of current try statement
Bram Moolenaar4c137212021-04-19 16:48:48 +02004479 ectx->ec_iidx = iidx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004480 }
4481 break;
4482
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01004483 case ISN_FINALLY:
4484 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004485 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01004486 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
4487 + trystack->ga_len - 1;
4488
4489 // Reset the index to avoid a return statement jumps here
4490 // again.
4491 trycmd->tcd_finally_idx = 0;
4492 break;
4493 }
4494
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004495 // end of ":try" block
4496 case ISN_ENDTRY:
4497 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004498 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar06651632022-04-27 17:54:25 +01004499 trycmd_T *trycmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004500
Bram Moolenaar06651632022-04-27 17:54:25 +01004501 --trystack->ga_len;
4502 --trylevel;
4503 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len;
4504 if (trycmd->tcd_did_throw)
4505 did_throw = TRUE;
4506 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004507 {
Bram Moolenaar06651632022-04-27 17:54:25 +01004508 // discard the exception
4509 if (caught_stack == current_exception)
4510 caught_stack = caught_stack->caught;
4511 discard_current_exception();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004512 }
Bram Moolenaar06651632022-04-27 17:54:25 +01004513
4514 if (trycmd->tcd_return)
4515 goto func_return;
4516
4517 while (ectx->ec_stack.ga_len > trycmd->tcd_stack_len)
4518 {
4519 --ectx->ec_stack.ga_len;
4520 clear_tv(STACK_TV_BOT(0));
4521 }
4522 if (trycmd->tcd_cont != 0)
4523 // handling :continue: jump to outer try block or
4524 // start of the loop
4525 ectx->ec_iidx = trycmd->tcd_cont - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004526 }
4527 break;
4528
4529 case ISN_THROW:
Bram Moolenaar8f81b222021-01-14 21:47:06 +01004530 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004531 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02004532
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004533 if (trystack->ga_len == 0 && trylevel == 0 && emsg_silent)
4534 {
4535 // throwing an exception while using "silent!" causes
4536 // the function to abort but not display an error.
4537 tv = STACK_TV_BOT(-1);
4538 clear_tv(tv);
4539 tv->v_type = VAR_NUMBER;
4540 tv->vval.v_number = 0;
4541 goto done;
4542 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004543 --ectx->ec_stack.ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004544 tv = STACK_TV_BOT(0);
4545 if (tv->vval.v_string == NULL
4546 || *skipwhite(tv->vval.v_string) == NUL)
4547 {
4548 vim_free(tv->vval.v_string);
4549 SOURCING_LNUM = iptr->isn_lnum;
4550 emsg(_(e_throw_with_empty_string));
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004551 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004552 }
4553
4554 // Inside a "catch" we need to first discard the caught
4555 // exception.
4556 if (trystack->ga_len > 0)
4557 {
4558 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
4559 + trystack->ga_len - 1;
4560 if (trycmd->tcd_caught && current_exception != NULL)
4561 {
4562 // discard the exception
4563 if (caught_stack == current_exception)
4564 caught_stack = caught_stack->caught;
4565 discard_current_exception();
4566 trycmd->tcd_caught = FALSE;
4567 }
4568 }
4569
Bram Moolenaar90a57162022-02-12 14:23:17 +00004570 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004571 if (throw_exception(tv->vval.v_string, ET_USER, NULL)
4572 == FAIL)
4573 {
4574 vim_free(tv->vval.v_string);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004575 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004576 }
4577 did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004578 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004579 break;
4580
4581 // compare with special values
4582 case ISN_COMPAREBOOL:
4583 case ISN_COMPARESPECIAL:
4584 {
4585 typval_T *tv1 = STACK_TV_BOT(-2);
4586 typval_T *tv2 = STACK_TV_BOT(-1);
4587 varnumber_T arg1 = tv1->vval.v_number;
4588 varnumber_T arg2 = tv2->vval.v_number;
4589 int res;
4590
Bram Moolenaar06651632022-04-27 17:54:25 +01004591 if (iptr->isn_arg.op.op_type == EXPR_EQUAL)
4592 res = arg1 == arg2;
4593 else
4594 res = arg1 != arg2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004595
Bram Moolenaar4c137212021-04-19 16:48:48 +02004596 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004597 tv1->v_type = VAR_BOOL;
4598 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4599 }
4600 break;
4601
Bram Moolenaar7a222242022-03-01 19:23:24 +00004602 case ISN_COMPARENULL:
4603 {
4604 typval_T *tv1 = STACK_TV_BOT(-2);
4605 typval_T *tv2 = STACK_TV_BOT(-1);
4606 int res;
4607
4608 res = typval_compare_null(tv1, tv2);
4609 if (res == MAYBE)
4610 goto on_error;
4611 if (iptr->isn_arg.op.op_type == EXPR_NEQUAL)
4612 res = !res;
4613 clear_tv(tv1);
4614 clear_tv(tv2);
4615 --ectx->ec_stack.ga_len;
4616 tv1->v_type = VAR_BOOL;
4617 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4618 }
4619 break;
4620
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004621 // Operation with two number arguments
4622 case ISN_OPNR:
4623 case ISN_COMPARENR:
4624 {
4625 typval_T *tv1 = STACK_TV_BOT(-2);
4626 typval_T *tv2 = STACK_TV_BOT(-1);
4627 varnumber_T arg1 = tv1->vval.v_number;
4628 varnumber_T arg2 = tv2->vval.v_number;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004629 varnumber_T res = 0;
4630 int div_zero = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004631
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004632 if (iptr->isn_arg.op.op_type == EXPR_LSHIFT
4633 || iptr->isn_arg.op.op_type == EXPR_RSHIFT)
4634 {
4635 if (arg2 < 0)
4636 {
4637 SOURCING_LNUM = iptr->isn_lnum;
dundargocc57b5bc2022-11-02 13:30:51 +00004638 emsg(_(e_bitshift_ops_must_be_positive));
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004639 goto on_error;
4640 }
4641 }
4642
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004643 switch (iptr->isn_arg.op.op_type)
4644 {
4645 case EXPR_MULT: res = arg1 * arg2; break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004646 case EXPR_DIV: if (arg2 == 0)
4647 div_zero = TRUE;
4648 else
4649 res = arg1 / arg2;
4650 break;
4651 case EXPR_REM: if (arg2 == 0)
4652 div_zero = TRUE;
4653 else
4654 res = arg1 % arg2;
4655 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004656 case EXPR_SUB: res = arg1 - arg2; break;
4657 case EXPR_ADD: res = arg1 + arg2; break;
4658
4659 case EXPR_EQUAL: res = arg1 == arg2; break;
4660 case EXPR_NEQUAL: res = arg1 != arg2; break;
4661 case EXPR_GREATER: res = arg1 > arg2; break;
4662 case EXPR_GEQUAL: res = arg1 >= arg2; break;
4663 case EXPR_SMALLER: res = arg1 < arg2; break;
4664 case EXPR_SEQUAL: res = arg1 <= arg2; break;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004665 case EXPR_LSHIFT: if (arg2 > MAX_LSHIFT_BITS)
4666 res = 0;
4667 else
Bram Moolenaar68e64d22022-05-22 22:07:52 +01004668 res = (uvarnumber_T)arg1 << arg2;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004669 break;
4670 case EXPR_RSHIFT: if (arg2 > MAX_LSHIFT_BITS)
4671 res = 0;
4672 else
Bram Moolenaar338bf582022-05-22 20:16:32 +01004673 res = (uvarnumber_T)arg1 >> arg2;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004674 break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004675 default: break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004676 }
4677
Bram Moolenaar4c137212021-04-19 16:48:48 +02004678 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004679 if (iptr->isn_type == ISN_COMPARENR)
4680 {
4681 tv1->v_type = VAR_BOOL;
4682 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4683 }
4684 else
4685 tv1->vval.v_number = res;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004686 if (div_zero)
4687 {
4688 SOURCING_LNUM = iptr->isn_lnum;
4689 emsg(_(e_divide_by_zero));
4690 goto on_error;
4691 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004692 }
4693 break;
4694
4695 // Computation with two float arguments
4696 case ISN_OPFLOAT:
4697 case ISN_COMPAREFLOAT:
4698 {
4699 typval_T *tv1 = STACK_TV_BOT(-2);
4700 typval_T *tv2 = STACK_TV_BOT(-1);
4701 float_T arg1 = tv1->vval.v_float;
4702 float_T arg2 = tv2->vval.v_float;
4703 float_T res = 0;
4704 int cmp = FALSE;
4705
4706 switch (iptr->isn_arg.op.op_type)
4707 {
4708 case EXPR_MULT: res = arg1 * arg2; break;
4709 case EXPR_DIV: res = arg1 / arg2; break;
4710 case EXPR_SUB: res = arg1 - arg2; break;
4711 case EXPR_ADD: res = arg1 + arg2; break;
4712
4713 case EXPR_EQUAL: cmp = arg1 == arg2; break;
4714 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
4715 case EXPR_GREATER: cmp = arg1 > arg2; break;
4716 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
4717 case EXPR_SMALLER: cmp = arg1 < arg2; break;
4718 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
4719 default: cmp = 0; break;
4720 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004721 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004722 if (iptr->isn_type == ISN_COMPAREFLOAT)
4723 {
4724 tv1->v_type = VAR_BOOL;
4725 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
4726 }
4727 else
4728 tv1->vval.v_float = res;
4729 }
4730 break;
4731
4732 case ISN_COMPARELIST:
Bram Moolenaar265f8112021-12-19 12:33:05 +00004733 case ISN_COMPAREDICT:
4734 case ISN_COMPAREFUNC:
4735 case ISN_COMPARESTRING:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004736 case ISN_COMPAREBLOB:
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00004737 case ISN_COMPARECLASS:
4738 case ISN_COMPAREOBJECT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004739 {
4740 typval_T *tv1 = STACK_TV_BOT(-2);
4741 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar265f8112021-12-19 12:33:05 +00004742 exprtype_T exprtype = iptr->isn_arg.op.op_type;
4743 int ic = iptr->isn_arg.op.op_ic;
4744 int res = FALSE;
4745 int status = OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004746
Bram Moolenaar265f8112021-12-19 12:33:05 +00004747 SOURCING_LNUM = iptr->isn_lnum;
4748 if (iptr->isn_type == ISN_COMPARELIST)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004749 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00004750 status = typval_compare_list(tv1, tv2,
4751 exprtype, ic, &res);
4752 }
4753 else if (iptr->isn_type == ISN_COMPAREDICT)
4754 {
4755 status = typval_compare_dict(tv1, tv2,
4756 exprtype, ic, &res);
4757 }
4758 else if (iptr->isn_type == ISN_COMPAREFUNC)
4759 {
4760 status = typval_compare_func(tv1, tv2,
4761 exprtype, ic, &res);
4762 }
4763 else if (iptr->isn_type == ISN_COMPARESTRING)
4764 {
4765 status = typval_compare_string(tv1, tv2,
4766 exprtype, ic, &res);
4767 }
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00004768 else if (iptr->isn_type == ISN_COMPAREBLOB)
Bram Moolenaar265f8112021-12-19 12:33:05 +00004769 {
4770 status = typval_compare_blob(tv1, tv2, exprtype, &res);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004771 }
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00004772 else if (iptr->isn_type == ISN_COMPARECLASS)
4773 {
Bram Moolenaar03ff0c62023-01-02 20:38:01 +00004774 status = typval_compare_class(tv1, tv2,
4775 exprtype, FALSE, &res);
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00004776 }
4777 else // ISN_COMPAREOBJECT
4778 {
4779 status = typval_compare_object(tv1, tv2,
Bram Moolenaar03ff0c62023-01-02 20:38:01 +00004780 exprtype, FALSE, &res);
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00004781 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004782 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004783 clear_tv(tv1);
4784 clear_tv(tv2);
4785 tv1->v_type = VAR_BOOL;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004786 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4787 if (status == FAIL)
4788 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004789 }
4790 break;
4791
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004792 case ISN_COMPAREANY:
4793 {
4794 typval_T *tv1 = STACK_TV_BOT(-2);
4795 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar657137c2021-01-09 15:45:23 +01004796 exprtype_T exprtype = iptr->isn_arg.op.op_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004797 int ic = iptr->isn_arg.op.op_ic;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004798 int status;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004799
Bram Moolenaareb26f432020-09-14 16:50:05 +02004800 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004801 status = typval_compare(tv1, tv2, exprtype, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004802 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004803 --ectx->ec_stack.ga_len;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004804 if (status == FAIL)
4805 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004806 }
4807 break;
4808
4809 case ISN_ADDLIST:
4810 case ISN_ADDBLOB:
4811 {
4812 typval_T *tv1 = STACK_TV_BOT(-2);
4813 typval_T *tv2 = STACK_TV_BOT(-1);
4814
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004815 // add two lists or blobs
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004816 if (iptr->isn_type == ISN_ADDLIST)
Bram Moolenaar07802042021-09-09 23:01:14 +02004817 {
4818 if (iptr->isn_arg.op.op_type == EXPR_APPEND
4819 && tv1->vval.v_list != NULL)
4820 list_extend(tv1->vval.v_list, tv2->vval.v_list,
4821 NULL);
4822 else
4823 eval_addlist(tv1, tv2);
4824 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004825 else
4826 eval_addblob(tv1, tv2);
4827 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004828 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004829 }
4830 break;
4831
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004832 case ISN_LISTAPPEND:
4833 {
4834 typval_T *tv1 = STACK_TV_BOT(-2);
4835 typval_T *tv2 = STACK_TV_BOT(-1);
4836 list_T *l = tv1->vval.v_list;
4837
4838 // add an item to a list
Bram Moolenaar1f4a3452022-01-01 18:29:21 +00004839 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004840 if (l == NULL)
4841 {
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004842 emsg(_(e_cannot_add_to_null_list));
4843 goto on_error;
4844 }
Bram Moolenaar1f4a3452022-01-01 18:29:21 +00004845 if (value_check_lock(l->lv_lock, NULL, FALSE))
4846 goto on_error;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004847 if (list_append_tv(l, tv2) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004848 goto theend;
Bram Moolenaar955347c2020-10-19 23:01:46 +02004849 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004850 --ectx->ec_stack.ga_len;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004851 }
4852 break;
4853
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02004854 case ISN_BLOBAPPEND:
4855 {
4856 typval_T *tv1 = STACK_TV_BOT(-2);
4857 typval_T *tv2 = STACK_TV_BOT(-1);
4858 blob_T *b = tv1->vval.v_blob;
4859 int error = FALSE;
4860 varnumber_T n;
4861
4862 // add a number to a blob
4863 if (b == NULL)
4864 {
4865 SOURCING_LNUM = iptr->isn_lnum;
4866 emsg(_(e_cannot_add_to_null_blob));
4867 goto on_error;
4868 }
4869 n = tv_get_number_chk(tv2, &error);
4870 if (error)
4871 goto on_error;
4872 ga_append(&b->bv_ga, (int)n);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004873 --ectx->ec_stack.ga_len;
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02004874 }
4875 break;
4876
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004877 // Computation with two arguments of unknown type
4878 case ISN_OPANY:
4879 {
4880 typval_T *tv1 = STACK_TV_BOT(-2);
4881 typval_T *tv2 = STACK_TV_BOT(-1);
4882 varnumber_T n1, n2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004883 float_T f1 = 0, f2 = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004884 int error = FALSE;
4885
4886 if (iptr->isn_arg.op.op_type == EXPR_ADD)
4887 {
4888 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
4889 {
4890 eval_addlist(tv1, tv2);
4891 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004892 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004893 break;
4894 }
4895 else if (tv1->v_type == VAR_BLOB
4896 && tv2->v_type == VAR_BLOB)
4897 {
4898 eval_addblob(tv1, tv2);
4899 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004900 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004901 break;
4902 }
4903 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004904 if (tv1->v_type == VAR_FLOAT)
4905 {
4906 f1 = tv1->vval.v_float;
4907 n1 = 0;
4908 }
4909 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004910 {
Bram Moolenaarf665e972020-12-05 19:17:16 +01004911 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004912 n1 = tv_get_number_chk(tv1, &error);
4913 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02004914 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004915 if (tv2->v_type == VAR_FLOAT)
4916 f1 = n1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004917 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004918 if (tv2->v_type == VAR_FLOAT)
4919 {
4920 f2 = tv2->vval.v_float;
4921 n2 = 0;
4922 }
4923 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004924 {
4925 n2 = tv_get_number_chk(tv2, &error);
4926 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02004927 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004928 if (tv1->v_type == VAR_FLOAT)
4929 f2 = n2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004930 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004931 // if there is a float on either side the result is a float
4932 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
4933 {
4934 switch (iptr->isn_arg.op.op_type)
4935 {
4936 case EXPR_MULT: f1 = f1 * f2; break;
4937 case EXPR_DIV: f1 = f1 / f2; break;
4938 case EXPR_SUB: f1 = f1 - f2; break;
4939 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004940 default: SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004941 emsg(_(e_cannot_use_percent_with_float));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004942 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004943 }
4944 clear_tv(tv1);
4945 clear_tv(tv2);
4946 tv1->v_type = VAR_FLOAT;
4947 tv1->vval.v_float = f1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004948 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004949 }
4950 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004951 {
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004952 int failed = FALSE;
4953
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004954 switch (iptr->isn_arg.op.op_type)
4955 {
4956 case EXPR_MULT: n1 = n1 * n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004957 case EXPR_DIV: n1 = num_divide(n1, n2, &failed);
4958 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01004959 goto on_error;
4960 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004961 case EXPR_SUB: n1 = n1 - n2; break;
4962 case EXPR_ADD: n1 = n1 + n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004963 default: n1 = num_modulus(n1, n2, &failed);
4964 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01004965 goto on_error;
4966 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004967 }
4968 clear_tv(tv1);
4969 clear_tv(tv2);
4970 tv1->v_type = VAR_NUMBER;
4971 tv1->vval.v_number = n1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004972 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004973 }
4974 }
4975 break;
4976
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004977 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004978 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004979 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004980 int is_slice = iptr->isn_type == ISN_STRSLICE;
4981 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004982 char_u *res;
4983
4984 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004985 // string slice: string is at stack-3, first index at
4986 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004987 if (is_slice)
4988 {
4989 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004990 n1 = tv->vval.v_number;
4991 }
4992
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004993 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004994 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004995
Bram Moolenaar4c137212021-04-19 16:48:48 +02004996 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004997 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004998 if (is_slice)
4999 // Slice: Select the characters from the string
Bram Moolenaar6601b622021-01-13 21:47:15 +01005000 res = string_slice(tv->vval.v_string, n1, n2, FALSE);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005001 else
5002 // Index: The resulting variable is a string of a
Bram Moolenaar0289a092021-03-14 18:40:19 +01005003 // single character (including composing characters).
5004 // If the index is too big or negative the result is
5005 // empty.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005006 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005007 vim_free(tv->vval.v_string);
5008 tv->vval.v_string = res;
5009 }
5010 break;
5011
5012 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02005013 case ISN_LISTSLICE:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02005014 case ISN_BLOBINDEX:
5015 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005016 {
Bram Moolenaarcfc30232021-04-11 20:26:34 +02005017 int is_slice = iptr->isn_type == ISN_LISTSLICE
5018 || iptr->isn_type == ISN_BLOBSLICE;
5019 int is_blob = iptr->isn_type == ISN_BLOBINDEX
5020 || iptr->isn_type == ISN_BLOBSLICE;
Bram Moolenaared591872020-08-15 22:14:53 +02005021 varnumber_T n1, n2;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02005022 typval_T *val_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005023
5024 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02005025 // list slice: list is at stack-3, indexes at stack-2 and
5026 // stack-1
Bram Moolenaarcfc30232021-04-11 20:26:34 +02005027 // Same for blob.
5028 val_tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005029
5030 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02005031 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005032 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02005033
5034 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005035 {
Bram Moolenaared591872020-08-15 22:14:53 +02005036 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02005037 n1 = tv->vval.v_number;
5038 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005039 }
Bram Moolenaared591872020-08-15 22:14:53 +02005040
Bram Moolenaar4c137212021-04-19 16:48:48 +02005041 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02005042 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02005043 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02005044 if (is_blob)
5045 {
5046 if (blob_slice_or_index(val_tv->vval.v_blob, is_slice,
5047 n1, n2, FALSE, tv) == FAIL)
5048 goto on_error;
5049 }
5050 else
5051 {
5052 if (list_slice_or_index(val_tv->vval.v_list, is_slice,
5053 n1, n2, FALSE, tv, TRUE) == FAIL)
5054 goto on_error;
5055 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005056 }
5057 break;
5058
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005059 case ISN_ANYINDEX:
5060 case ISN_ANYSLICE:
5061 {
5062 int is_slice = iptr->isn_type == ISN_ANYSLICE;
5063 typval_T *var1, *var2;
5064 int res;
5065
5066 // index: composite is at stack-2, index at stack-1
5067 // slice: composite is at stack-3, indexes at stack-2 and
5068 // stack-1
5069 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02005070 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005071 if (check_can_index(tv, TRUE, TRUE) == FAIL)
5072 goto on_error;
5073 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
5074 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
Bram Moolenaar6601b622021-01-13 21:47:15 +01005075 res = eval_index_inner(tv, is_slice, var1, var2,
5076 FALSE, NULL, -1, TRUE);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005077 clear_tv(var1);
5078 if (is_slice)
5079 clear_tv(var2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005080 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005081 if (res == FAIL)
5082 goto on_error;
5083 }
5084 break;
5085
Bram Moolenaar9af78762020-06-16 11:34:42 +02005086 case ISN_SLICE:
5087 {
5088 list_T *list;
5089 int count = iptr->isn_arg.number;
5090
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02005091 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02005092 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02005093 list = tv->vval.v_list;
5094
5095 // no error for short list, expect it to be checked earlier
5096 if (list != NULL && list->lv_len >= count)
5097 {
5098 list_T *newlist = list_slice(list,
5099 count, list->lv_len - 1);
5100
5101 if (newlist != NULL)
5102 {
5103 list_unref(list);
5104 tv->vval.v_list = newlist;
5105 ++newlist->lv_refcount;
5106 }
5107 }
5108 }
5109 break;
5110
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005111 case ISN_GETITEM:
5112 {
5113 listitem_T *li;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02005114 getitem_T *gi = &iptr->isn_arg.getitem;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005115
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02005116 // Get list item: list is at stack-1, push item.
5117 // List type and length is checked for when compiling.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02005118 tv = STACK_TV_BOT(-1 - gi->gi_with_op);
5119 li = list_find(tv->vval.v_list, gi->gi_index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005120
Bram Moolenaar35578162021-08-02 19:10:38 +02005121 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005122 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005123 ++ectx->ec_stack.ga_len;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005124 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
Bram Moolenaarf785aa12021-02-11 21:19:34 +01005125
5126 // Useful when used in unpack assignment. Reset at
5127 // ISN_DROP.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02005128 ectx->ec_where.wt_index = gi->gi_index + 1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005129 ectx->ec_where.wt_variable = TRUE;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005130 }
5131 break;
5132
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005133 case ISN_MEMBER:
5134 {
5135 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005136 char_u *key;
5137 dictitem_T *di;
5138
5139 // dict member: dict is at stack-2, key at stack-1
5140 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02005141 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005142 dict = tv->vval.v_dict;
5143
5144 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02005145 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005146 key = tv->vval.v_string;
Bram Moolenaar086fc9a2020-10-30 18:33:02 +01005147 if (key == NULL)
5148 key = (char_u *)"";
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02005149
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005150 if ((di = dict_find(dict, key, -1)) == NULL)
5151 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005152 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00005153 semsg(_(e_key_not_present_in_dictionary_str), key);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01005154
5155 // If :silent! is used we will continue, make sure the
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005156 // stack contents makes sense and the dict stack is
5157 // updated.
Bram Moolenaar4029cab2020-12-05 18:13:27 +01005158 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005159 --ectx->ec_stack.ga_len;
Bram Moolenaar4029cab2020-12-05 18:13:27 +01005160 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005161 (void) dict_stack_save(tv);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01005162 tv->v_type = VAR_NUMBER;
5163 tv->vval.v_number = 0;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01005164 goto on_fatal_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005165 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005166 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005167 --ectx->ec_stack.ga_len;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005168 // Put the dict used on the dict stack, it might be used by
5169 // a dict function later.
Bram Moolenaar50788ef2020-07-05 16:51:26 +02005170 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005171 if (dict_stack_save(tv) == FAIL)
5172 goto on_fatal_error;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02005173 copy_tv(&di->di_tv, tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005174 }
5175 break;
5176
5177 // dict member with string key
5178 case ISN_STRINGMEMBER:
5179 {
5180 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005181 dictitem_T *di;
5182
5183 tv = STACK_TV_BOT(-1);
5184 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
5185 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005186 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005187 emsg(_(e_dictionary_required));
Bram Moolenaard032f342020-07-18 18:13:02 +02005188 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005189 }
5190 dict = tv->vval.v_dict;
5191
5192 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
5193 == NULL)
5194 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005195 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00005196 semsg(_(e_key_not_present_in_dictionary_str),
Bram Moolenaar381692b2022-02-02 20:01:27 +00005197 iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02005198 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005199 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005200 // Put the dict used on the dict stack, it might be used by
5201 // a dict function later.
5202 if (dict_stack_save(tv) == FAIL)
5203 goto on_fatal_error;
5204
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005205 copy_tv(&di->di_tv, tv);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005206 }
5207 break;
5208
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005209 case ISN_GET_OBJ_MEMBER:
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00005210 case ISN_GET_ITF_MEMBER:
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005211 {
5212 tv = STACK_TV_BOT(-1);
5213 if (tv->v_type != VAR_OBJECT)
5214 {
5215 SOURCING_LNUM = iptr->isn_lnum;
5216 garray_T type_list;
5217 ga_init2(&type_list, sizeof(type_T *), 10);
5218 type_T *type = typval2type(tv, get_copyID(),
5219 &type_list, TVTT_DO_MEMBER);
5220 char *tofree = NULL;
5221 char *typename = type_name(type, &tofree);
5222 semsg(_(e_object_required_found_str), typename);
5223 vim_free(tofree);
5224 clear_type_list(&type_list);
5225 goto on_error;
5226 }
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00005227
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005228 object_T *obj = tv->vval.v_object;
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00005229 int idx;
5230 if (iptr->isn_type == ISN_GET_OBJ_MEMBER)
5231 idx = iptr->isn_arg.number;
5232 else
5233 {
5234 idx = iptr->isn_arg.classmember.cm_idx;
5235 // convert the interface index to the object index
5236 idx = object_index_from_itf_index(
5237 iptr->isn_arg.classmember.cm_class,
5238 idx, obj->obj_class);
5239 }
5240
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005241 // the members are located right after the object struct
5242 typval_T *mtv = ((typval_T *)(obj + 1)) + idx;
Bram Moolenaar590162c2022-12-24 21:24:06 +00005243 copy_tv(mtv, tv);
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005244
5245 // Unreference the object after getting the member, it may
5246 // be freed.
5247 object_unref(obj);
5248 }
5249 break;
5250
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005251 case ISN_STORE_THIS:
5252 {
5253 int idx = iptr->isn_arg.number;
5254 object_T *obj = STACK_TV_VAR(0)->vval.v_object;
5255 // the members are located right after the object struct
5256 typval_T *mtv = ((typval_T *)(obj + 1)) + idx;
5257 clear_tv(mtv);
5258 *mtv = *STACK_TV_BOT(-1);
5259 --ectx->ec_stack.ga_len;
5260 }
5261 break;
5262
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005263 case ISN_CLEARDICT:
5264 dict_stack_drop();
5265 break;
5266
5267 case ISN_USEDICT:
5268 {
5269 typval_T *dict_tv = dict_stack_get_tv();
5270
5271 // Turn "dict.Func" into a partial for "Func" bound to
5272 // "dict". Don't do this when "Func" is already a partial
5273 // that was bound explicitly (pt_auto is FALSE).
5274 tv = STACK_TV_BOT(-1);
5275 if (dict_tv != NULL
5276 && dict_tv->v_type == VAR_DICT
5277 && dict_tv->vval.v_dict != NULL
5278 && (tv->v_type == VAR_FUNC
5279 || (tv->v_type == VAR_PARTIAL
5280 && (tv->vval.v_partial->pt_auto
5281 || tv->vval.v_partial->pt_dict == NULL))))
5282 dict_tv->vval.v_dict =
5283 make_partial(dict_tv->vval.v_dict, tv);
5284 dict_stack_drop();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005285 }
5286 break;
5287
5288 case ISN_NEGATENR:
5289 tv = STACK_TV_BOT(-1);
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00005290 // CHECKTYPE should have checked the variable type
Bram Moolenaarc58164c2020-03-29 18:40:30 +02005291 if (tv->v_type == VAR_FLOAT)
5292 tv->vval.v_float = -tv->vval.v_float;
5293 else
Bram Moolenaarc58164c2020-03-29 18:40:30 +02005294 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005295 break;
5296
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005297 case ISN_CHECKTYPE:
5298 {
5299 checktype_T *ct = &iptr->isn_arg.type;
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01005300 int save_wt_variable = ectx->ec_where.wt_variable;
Bram Moolenaarb1040dc2022-05-18 11:00:48 +01005301 int r;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005302
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01005303 tv = STACK_TV_BOT((int)ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02005304 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005305 if (!ectx->ec_where.wt_variable)
5306 ectx->ec_where.wt_index = ct->ct_arg_idx;
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01005307 ectx->ec_where.wt_variable = ct->ct_is_var;
Bram Moolenaarb1040dc2022-05-18 11:00:48 +01005308 r = check_typval_type(ct->ct_type, tv, ectx->ec_where);
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01005309 ectx->ec_where.wt_variable = save_wt_variable;
Bram Moolenaarb1040dc2022-05-18 11:00:48 +01005310 if (r == FAIL)
5311 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005312 if (!ectx->ec_where.wt_variable)
5313 ectx->ec_where.wt_index = 0;
Bram Moolenaar5e654232020-09-16 15:22:00 +02005314
5315 // number 0 is FALSE, number 1 is TRUE
5316 if (tv->v_type == VAR_NUMBER
5317 && ct->ct_type->tt_type == VAR_BOOL
5318 && (tv->vval.v_number == 0
5319 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005320 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02005321 tv->v_type = VAR_BOOL;
5322 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02005323 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005324 }
5325 }
5326 break;
5327
Bram Moolenaar9af78762020-06-16 11:34:42 +02005328 case ISN_CHECKLEN:
5329 {
5330 int min_len = iptr->isn_arg.checklen.cl_min_len;
5331 list_T *list = NULL;
5332
5333 tv = STACK_TV_BOT(-1);
5334 if (tv->v_type == VAR_LIST)
5335 list = tv->vval.v_list;
5336 if (list == NULL || list->lv_len < min_len
5337 || (list->lv_len > min_len
5338 && !iptr->isn_arg.checklen.cl_more_OK))
5339 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005340 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005341 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02005342 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02005343 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02005344 }
5345 }
5346 break;
5347
Bram Moolenaaraa210a32021-01-02 15:41:03 +01005348 case ISN_SETTYPE:
Bram Moolenaar381692b2022-02-02 20:01:27 +00005349 set_tv_type(STACK_TV_BOT(-1), iptr->isn_arg.type.ct_type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01005350 break;
5351
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005352 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005353 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005354 {
5355 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005356 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005357
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005358 if (iptr->isn_type == ISN_2BOOL)
5359 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005360 tv = STACK_TV_BOT(iptr->isn_arg.tobool.offset);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005361 n = tv2bool(tv);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005362 if (iptr->isn_arg.tobool.invert)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005363 n = !n;
5364 }
5365 else
5366 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005367 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005368 SOURCING_LNUM = iptr->isn_lnum;
5369 n = tv_get_bool_chk(tv, &error);
5370 if (error)
5371 goto on_error;
5372 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005373 clear_tv(tv);
5374 tv->v_type = VAR_BOOL;
5375 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
5376 }
5377 break;
5378
5379 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02005380 case ISN_2STRING_ANY:
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01005381 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005382 if (do_2string(STACK_TV_BOT(iptr->isn_arg.tostring.offset),
5383 iptr->isn_type == ISN_2STRING_ANY,
5384 iptr->isn_arg.tostring.tolerant) == FAIL)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01005385 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005386 break;
5387
Bram Moolenaar08597872020-12-10 19:43:40 +01005388 case ISN_RANGE:
5389 {
5390 exarg_T ea;
5391 char *errormsg;
5392
Bram Moolenaarece0b872021-01-08 20:40:45 +01005393 ea.line2 = 0;
Bram Moolenaard1510ee2021-01-04 16:15:58 +01005394 ea.addr_count = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01005395 ea.addr_type = ADDR_LINES;
5396 ea.cmd = iptr->isn_arg.string;
Bram Moolenaarece0b872021-01-08 20:40:45 +01005397 ea.skip = FALSE;
Bram Moolenaar08597872020-12-10 19:43:40 +01005398 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaarece0b872021-01-08 20:40:45 +01005399 goto on_error;
Bram Moolenaara28639e2021-01-19 22:48:09 +01005400
Bram Moolenaar35578162021-08-02 19:10:38 +02005401 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005402 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005403 ++ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01005404 tv = STACK_TV_BOT(-1);
5405 tv->v_type = VAR_NUMBER;
5406 tv->v_lock = 0;
Bram Moolenaar06651632022-04-27 17:54:25 +01005407 tv->vval.v_number = ea.line2;
Bram Moolenaar08597872020-12-10 19:43:40 +01005408 }
5409 break;
5410
Bram Moolenaarc3516f72020-09-08 22:45:35 +02005411 case ISN_PUT:
5412 {
5413 int regname = iptr->isn_arg.put.put_regname;
5414 linenr_T lnum = iptr->isn_arg.put.put_lnum;
5415 char_u *expr = NULL;
5416 int dir = FORWARD;
5417
Bram Moolenaar08597872020-12-10 19:43:40 +01005418 if (lnum < -2)
5419 {
5420 // line number was put on the stack by ISN_RANGE
5421 tv = STACK_TV_BOT(-1);
5422 curwin->w_cursor.lnum = tv->vval.v_number;
5423 if (lnum == LNUM_VARIABLE_RANGE_ABOVE)
5424 dir = BACKWARD;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005425 --ectx->ec_stack.ga_len;
Bram Moolenaar08597872020-12-10 19:43:40 +01005426 }
5427 else if (lnum == -2)
Bram Moolenaarc3516f72020-09-08 22:45:35 +02005428 // :put! above cursor
5429 dir = BACKWARD;
5430 else if (lnum >= 0)
Bram Moolenaar4e713ba2022-02-07 15:31:37 +00005431 {
5432 curwin->w_cursor.lnum = lnum;
5433 if (lnum == 0)
5434 // check_cursor() below will move to line 1
5435 dir = BACKWARD;
5436 }
Bram Moolenaara28639e2021-01-19 22:48:09 +01005437
5438 if (regname == '=')
5439 {
5440 tv = STACK_TV_BOT(-1);
5441 if (tv->v_type == VAR_STRING)
5442 expr = tv->vval.v_string;
5443 else
5444 {
5445 expr = typval2string(tv, TRUE); // allocates value
5446 clear_tv(tv);
5447 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005448 --ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01005449 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02005450 check_cursor();
5451 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
5452 vim_free(expr);
5453 }
5454 break;
5455
Bram Moolenaar02194d22020-10-24 23:08:38 +02005456 case ISN_CMDMOD:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005457 ectx->ec_funclocal.floc_save_cmdmod = cmdmod;
5458 ectx->ec_funclocal.floc_restore_cmdmod = TRUE;
5459 ectx->ec_funclocal.floc_restore_cmdmod_stacklen =
5460 ectx->ec_stack.ga_len;
Bram Moolenaar02194d22020-10-24 23:08:38 +02005461 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
5462 apply_cmdmod(&cmdmod);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02005463 break;
5464
Bram Moolenaar02194d22020-10-24 23:08:38 +02005465 case ISN_CMDMOD_REV:
5466 // filter regprog is owned by the instruction, don't free it
5467 cmdmod.cmod_filter_regmatch.regprog = NULL;
5468 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005469 cmdmod = ectx->ec_funclocal.floc_save_cmdmod;
5470 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02005471 break;
5472
Bram Moolenaar792f7862020-11-23 08:31:18 +01005473 case ISN_UNPACK:
5474 {
5475 int count = iptr->isn_arg.unpack.unp_count;
5476 int semicolon = iptr->isn_arg.unpack.unp_semicolon;
5477 list_T *l;
5478 listitem_T *li;
5479 int i;
5480
5481 // Check there is a valid list to unpack.
5482 tv = STACK_TV_BOT(-1);
5483 if (tv->v_type != VAR_LIST)
5484 {
5485 SOURCING_LNUM = iptr->isn_lnum;
5486 emsg(_(e_for_argument_must_be_sequence_of_lists));
5487 goto on_error;
5488 }
5489 l = tv->vval.v_list;
5490 if (l == NULL
5491 || l->lv_len < (semicolon ? count - 1 : count))
5492 {
5493 SOURCING_LNUM = iptr->isn_lnum;
5494 emsg(_(e_list_value_does_not_have_enough_items));
5495 goto on_error;
5496 }
5497 else if (!semicolon && l->lv_len > count)
5498 {
5499 SOURCING_LNUM = iptr->isn_lnum;
5500 emsg(_(e_list_value_has_more_items_than_targets));
5501 goto on_error;
5502 }
5503
5504 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar35578162021-08-02 19:10:38 +02005505 if (GA_GROW_FAILS(&ectx->ec_stack, count - 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005506 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005507 ectx->ec_stack.ga_len += count - 1;
Bram Moolenaar792f7862020-11-23 08:31:18 +01005508
5509 // Variable after semicolon gets a list with the remaining
5510 // items.
5511 if (semicolon)
5512 {
5513 list_T *rem_list =
5514 list_alloc_with_items(l->lv_len - count + 1);
5515
5516 if (rem_list == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005517 goto theend;
Bram Moolenaar792f7862020-11-23 08:31:18 +01005518 tv = STACK_TV_BOT(-count);
5519 tv->vval.v_list = rem_list;
5520 ++rem_list->lv_refcount;
5521 tv->v_lock = 0;
5522 li = l->lv_first;
5523 for (i = 0; i < count - 1; ++i)
5524 li = li->li_next;
5525 for (i = 0; li != NULL; ++i)
5526 {
Bram Moolenaar61efa162022-03-18 13:10:48 +00005527 typval_T tvcopy;
5528
5529 copy_tv(&li->li_tv, &tvcopy);
5530 list_set_item(rem_list, i, &tvcopy);
Bram Moolenaar792f7862020-11-23 08:31:18 +01005531 li = li->li_next;
5532 }
5533 --count;
5534 }
5535
5536 // Produce the values in reverse order, first item last.
5537 li = l->lv_first;
5538 for (i = 0; i < count; ++i)
5539 {
5540 tv = STACK_TV_BOT(-i - 1);
5541 copy_tv(&li->li_tv, tv);
5542 li = li->li_next;
5543 }
5544
5545 list_unref(l);
5546 }
5547 break;
5548
Bram Moolenaarb2049902021-01-24 12:53:53 +01005549 case ISN_PROF_START:
5550 case ISN_PROF_END:
5551 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01005552#ifdef FEAT_PROFILE
Bram Moolenaarca16c602022-09-06 18:57:08 +01005553 funccall_T cookie;
5554 ufunc_T *cur_ufunc =
Bram Moolenaarb2049902021-01-24 12:53:53 +01005555 (((dfunc_T *)def_functions.ga_data)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02005556 + ectx->ec_dfunc_idx)->df_ufunc;
Bram Moolenaarb2049902021-01-24 12:53:53 +01005557
Bram Moolenaarca16c602022-09-06 18:57:08 +01005558 cookie.fc_func = cur_ufunc;
Bram Moolenaarb2049902021-01-24 12:53:53 +01005559 if (iptr->isn_type == ISN_PROF_START)
5560 {
5561 func_line_start(&cookie, iptr->isn_lnum);
5562 // if we get here the instruction is executed
5563 func_line_exec(&cookie);
5564 }
5565 else
5566 func_line_end(&cookie);
Bram Moolenaarf002a412021-01-24 13:34:18 +01005567#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01005568 }
5569 break;
5570
Bram Moolenaare99d4222021-06-13 14:01:26 +02005571 case ISN_DEBUG:
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02005572 handle_debug(iptr, ectx);
Bram Moolenaare99d4222021-06-13 14:01:26 +02005573 break;
5574
Bram Moolenaar389df252020-07-09 21:20:47 +02005575 case ISN_SHUFFLE:
5576 {
Bram Moolenaar792f7862020-11-23 08:31:18 +01005577 typval_T tmp_tv;
5578 int item = iptr->isn_arg.shuffle.shfl_item;
5579 int up = iptr->isn_arg.shuffle.shfl_up;
Bram Moolenaar389df252020-07-09 21:20:47 +02005580
5581 tmp_tv = *STACK_TV_BOT(-item);
5582 for ( ; up > 0 && item > 1; --up)
5583 {
5584 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
5585 --item;
5586 }
5587 *STACK_TV_BOT(-item) = tmp_tv;
5588 }
5589 break;
5590
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005591 case ISN_DROP:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005592 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005593 clear_tv(STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005594 ectx->ec_where.wt_index = 0;
5595 ectx->ec_where.wt_variable = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005596 break;
5597 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02005598 continue;
5599
Bram Moolenaard032f342020-07-18 18:13:02 +02005600func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02005601 // Restore previous function. If the frame pointer is where we started
5602 // then there is none and we are done.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005603 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02005604 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02005605
Bram Moolenaar4c137212021-04-19 16:48:48 +02005606 if (func_return(ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02005607 // only fails when out of memory
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005608 goto theend;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02005609 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02005610
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02005611on_error:
Bram Moolenaaraf0df472020-12-02 20:51:22 +01005612 // Jump here for an error that does not require aborting execution.
Bram Moolenaar56602ba2020-12-05 21:22:08 +01005613 // If "emsg_silent" is set then ignore the error, unless it was set
5614 // when calling the function.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005615 if (did_emsg_cumul + did_emsg == ectx->ec_did_emsg_before
Bram Moolenaar56602ba2020-12-05 21:22:08 +01005616 && emsg_silent && did_emsg_def == 0)
Bram Moolenaarf9041332021-01-21 19:41:16 +01005617 {
5618 // If a sequence of instructions causes an error while ":silent!"
5619 // was used, restore the stack length and jump ahead to restoring
5620 // the cmdmod.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005621 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaarf9041332021-01-21 19:41:16 +01005622 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005623 while (ectx->ec_stack.ga_len
5624 > ectx->ec_funclocal.floc_restore_cmdmod_stacklen)
Bram Moolenaarf9041332021-01-21 19:41:16 +01005625 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005626 --ectx->ec_stack.ga_len;
Bram Moolenaarf9041332021-01-21 19:41:16 +01005627 clear_tv(STACK_TV_BOT(0));
5628 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005629 while (ectx->ec_instr[ectx->ec_iidx].isn_type != ISN_CMDMOD_REV)
5630 ++ectx->ec_iidx;
Bram Moolenaarf9041332021-01-21 19:41:16 +01005631 }
Bram Moolenaarcd030c42020-10-30 21:49:40 +01005632 continue;
Bram Moolenaarf9041332021-01-21 19:41:16 +01005633 }
Bram Moolenaaraf0df472020-12-02 20:51:22 +01005634on_fatal_error:
5635 // Jump here for an error that messes up the stack.
Bram Moolenaar171fb922020-10-28 16:54:47 +01005636 // If we are not inside a try-catch started here, abort execution.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005637 if (trylevel <= ectx->ec_trylevel_at_start)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005638 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005639 }
5640
5641done:
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005642 ret = OK;
5643theend:
Bram Moolenaar58779852022-09-06 18:31:14 +01005644 may_invoke_defer_funcs(ectx);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01005645
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005646 dict_stack_clear(dict_stack_len_at_start);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005647 ectx->ec_trylevel_at_start = save_trylevel_at_start;
5648 return ret;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005649}
5650
5651/*
Bram Moolenaar806a2732022-09-04 15:40:36 +01005652 * Execute the instructions from a VAR_INSTR typval and put the result in
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005653 * "rettv".
5654 * Return OK or FAIL.
5655 */
5656 int
5657exe_typval_instr(typval_T *tv, typval_T *rettv)
5658{
5659 ectx_T *ectx = tv->vval.v_instr->instr_ectx;
5660 isn_T *save_instr = ectx->ec_instr;
5661 int save_iidx = ectx->ec_iidx;
5662 int res;
5663
LemonBoyf3b48952022-05-05 13:53:03 +01005664 // Initialize rettv so that it is safe for caller to invoke clear_tv(rettv)
5665 // even when the compilation fails.
5666 rettv->v_type = VAR_UNKNOWN;
5667
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005668 ectx->ec_instr = tv->vval.v_instr->instr_instr;
5669 res = exec_instructions(ectx);
5670 if (res == OK)
5671 {
5672 *rettv = *STACK_TV_BOT(-1);
5673 --ectx->ec_stack.ga_len;
5674 }
5675
5676 ectx->ec_instr = save_instr;
5677 ectx->ec_iidx = save_iidx;
5678
5679 return res;
5680}
5681
5682/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02005683 * Execute the instructions from an ISN_SUBSTITUTE command, which are in
5684 * "substitute_instr".
5685 */
5686 char_u *
5687exe_substitute_instr(void)
5688{
5689 ectx_T *ectx = substitute_instr->subs_ectx;
5690 isn_T *save_instr = ectx->ec_instr;
5691 int save_iidx = ectx->ec_iidx;
5692 char_u *res;
5693
5694 ectx->ec_instr = substitute_instr->subs_instr;
5695 if (exec_instructions(ectx) == OK)
Bram Moolenaar90193e62021-04-04 20:49:50 +02005696 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005697 typval_T *tv = STACK_TV_BOT(-1);
5698
Bram Moolenaar27523602021-06-05 21:36:19 +02005699 res = typval2string(tv, TRUE);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005700 --ectx->ec_stack.ga_len;
5701 clear_tv(tv);
Bram Moolenaar90193e62021-04-04 20:49:50 +02005702 }
5703 else
5704 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005705 substitute_instr->subs_status = FAIL;
5706 res = vim_strsave((char_u *)"");
Bram Moolenaar90193e62021-04-04 20:49:50 +02005707 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005708
Bram Moolenaar4c137212021-04-19 16:48:48 +02005709 ectx->ec_instr = save_instr;
5710 ectx->ec_iidx = save_iidx;
5711
5712 return res;
5713}
5714
5715/*
5716 * Call a "def" function from old Vim script.
5717 * Return OK or FAIL.
5718 */
5719 int
5720call_def_function(
5721 ufunc_T *ufunc,
5722 int argc_arg, // nr of arguments
5723 typval_T *argv, // arguments
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005724 int flags, // DEF_ flags
Bram Moolenaar4c137212021-04-19 16:48:48 +02005725 partial_T *partial, // optional partial for context
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005726 object_T *object, // object, e.g. for this.Func()
Bram Moolenaar58779852022-09-06 18:31:14 +01005727 funccall_T *funccal,
Bram Moolenaar4c137212021-04-19 16:48:48 +02005728 typval_T *rettv) // return value
5729{
5730 ectx_T ectx; // execution context
5731 int argc = argc_arg;
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005732 int partial_argc = partial == NULL
5733 || (flags & DEF_USE_PT_ARGV) == 0
5734 ? 0 : partial->pt_argc;
5735 int total_argc = argc + partial_argc;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005736 typval_T *tv;
5737 int idx;
5738 int ret = FAIL;
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005739 int defcount = ufunc->uf_args.ga_len - total_argc;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005740 sctx_T save_current_sctx = current_sctx;
5741 int did_emsg_before = did_emsg_cumul + did_emsg;
5742 int save_suppress_errthrow = suppress_errthrow;
5743 msglist_T **saved_msg_list = NULL;
5744 msglist_T *private_msg_list = NULL;
5745 int save_emsg_silent_def = emsg_silent_def;
5746 int save_did_emsg_def = did_emsg_def;
5747 int orig_funcdepth;
Bram Moolenaare99d4222021-06-13 14:01:26 +02005748 int orig_nesting_level = ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005749
5750// Get pointer to item in the stack.
5751#undef STACK_TV
5752#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
5753
5754// Get pointer to item at the bottom of the stack, -1 is the bottom.
5755#undef STACK_TV_BOT
5756#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
5757
5758// Get pointer to a local variable on the stack. Negative for arguments.
5759#undef STACK_TV_VAR
5760#define STACK_TV_VAR(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_frame_idx + STACK_FRAME_SIZE + idx)
5761
5762 if (ufunc->uf_def_status == UF_NOT_COMPILED
5763 || ufunc->uf_def_status == UF_COMPILE_ERROR
Bram Moolenaar139575d2022-03-15 19:29:30 +00005764 || (func_needs_compiling(ufunc, get_compile_type(ufunc))
5765 && compile_def_function(ufunc, FALSE,
5766 get_compile_type(ufunc), NULL) == FAIL))
Bram Moolenaar4c137212021-04-19 16:48:48 +02005767 {
5768 if (did_emsg_cumul + did_emsg == did_emsg_before)
5769 semsg(_(e_function_is_not_compiled_str),
5770 printable_func_name(ufunc));
5771 return FAIL;
5772 }
5773
5774 {
5775 // Check the function was really compiled.
5776 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5777 + ufunc->uf_dfunc_idx;
Bram Moolenaarcc341812022-09-19 15:54:34 +01005778 if (dfunc->df_ufunc == NULL)
5779 {
5780 semsg(_(e_function_was_deleted_str), printable_func_name(ufunc));
5781 return FAIL;
5782 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005783 if (INSTRUCTIONS(dfunc) == NULL)
5784 {
5785 iemsg("using call_def_function() on not compiled function");
5786 return FAIL;
5787 }
5788 }
5789
5790 // If depth of calling is getting too high, don't execute the function.
5791 orig_funcdepth = funcdepth_get();
5792 if (funcdepth_increment() == FAIL)
5793 return FAIL;
5794
5795 CLEAR_FIELD(ectx);
5796 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
5797 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
Bram Moolenaar35578162021-08-02 19:10:38 +02005798 if (GA_GROW_FAILS(&ectx.ec_stack, 20))
Bram Moolenaar4c137212021-04-19 16:48:48 +02005799 {
5800 funcdepth_decrement();
5801 return FAIL;
5802 }
5803 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
5804 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
5805 ectx.ec_did_emsg_before = did_emsg_before;
Bram Moolenaare99d4222021-06-13 14:01:26 +02005806 ++ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005807
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005808 idx = total_argc - ufunc->uf_args.ga_len;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005809 if (idx > 0 && ufunc->uf_va_name == NULL)
5810 {
Matvey Tarasovd14bb1a2022-06-29 13:18:27 +01005811 semsg(NGETTEXT(e_one_argument_too_many, e_nr_arguments_too_many,
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005812 idx), idx);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005813 goto failed_early;
5814 }
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005815 idx = total_argc - ufunc->uf_args.ga_len + ufunc->uf_def_args.ga_len;
Bram Moolenaarc6d71532021-06-05 18:49:38 +02005816 if (idx < 0)
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02005817 {
Matvey Tarasovd14bb1a2022-06-29 13:18:27 +01005818 semsg(NGETTEXT(e_one_argument_too_few, e_nr_arguments_too_few,
Bram Moolenaar98aff652022-09-06 21:02:35 +01005819 -idx), -idx);
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02005820 goto failed_early;
5821 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005822
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005823 // Put values from the partial and arguments on the stack, but no more than
5824 // what the function expects. A lambda can be called with more arguments
5825 // than it uses.
5826 for (idx = 0; idx < total_argc
Bram Moolenaar4c137212021-04-19 16:48:48 +02005827 && (ufunc->uf_va_name != NULL || idx < ufunc->uf_args.ga_len);
5828 ++idx)
5829 {
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005830 int argv_idx = idx - partial_argc;
5831
5832 tv = idx < partial_argc ? partial->pt_argv + idx : argv + argv_idx;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005833 if (idx >= ufunc->uf_args.ga_len - ufunc->uf_def_args.ga_len
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005834 && tv->v_type == VAR_SPECIAL
5835 && tv->vval.v_number == VVAL_NONE)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005836 {
5837 // Use the default value.
5838 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
5839 }
5840 else
5841 {
Bram Moolenaar47bba532023-01-20 18:49:46 +00005842 int done = FALSE;
5843 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len)
5844 {
5845 type_T *expected = ufunc->uf_arg_types[idx];
5846 if (expected->tt_type == VAR_FLOAT && tv->v_type == VAR_NUMBER)
5847 {
5848 // When a float is expected and a number was given, convert
5849 // the value.
5850 STACK_TV_BOT(0)->v_type = VAR_FLOAT;
5851 STACK_TV_BOT(0)->v_lock = 0;
5852 STACK_TV_BOT(0)->vval.v_float = tv->vval.v_number;
5853 done = TRUE;
5854 }
5855 else if (check_typval_arg_type(expected, tv,
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005856 NULL, argv_idx + 1) == FAIL)
Bram Moolenaar47bba532023-01-20 18:49:46 +00005857 goto failed_early;
5858 }
5859 if (!done)
5860 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005861 }
5862 ++ectx.ec_stack.ga_len;
5863 }
5864
5865 // Turn varargs into a list. Empty list if no args.
5866 if (ufunc->uf_va_name != NULL)
5867 {
5868 int vararg_count = argc - ufunc->uf_args.ga_len;
5869
5870 if (vararg_count < 0)
5871 vararg_count = 0;
5872 else
5873 argc -= vararg_count;
5874 if (exe_newlist(vararg_count, &ectx) == FAIL)
5875 goto failed_early;
5876
5877 // Check the type of the list items.
5878 tv = STACK_TV_BOT(-1);
5879 if (ufunc->uf_va_type != NULL
5880 && ufunc->uf_va_type != &t_list_any
5881 && ufunc->uf_va_type->tt_member != &t_any
5882 && tv->vval.v_list != NULL)
5883 {
5884 type_T *expected = ufunc->uf_va_type->tt_member;
5885 listitem_T *li = tv->vval.v_list->lv_first;
5886
5887 for (idx = 0; idx < vararg_count; ++idx)
5888 {
5889 if (check_typval_arg_type(expected, &li->li_tv,
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02005890 NULL, argc + idx + 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005891 goto failed_early;
5892 li = li->li_next;
5893 }
5894 }
5895
5896 if (defcount > 0)
5897 // Move varargs list to below missing default arguments.
5898 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
5899 --ectx.ec_stack.ga_len;
5900 }
5901
5902 // Make space for omitted arguments, will store default value below.
5903 // Any varargs list goes after them.
5904 if (defcount > 0)
5905 for (idx = 0; idx < defcount; ++idx)
5906 {
5907 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
5908 ++ectx.ec_stack.ga_len;
5909 }
5910 if (ufunc->uf_va_name != NULL)
5911 ++ectx.ec_stack.ga_len;
5912
5913 // Frame pointer points to just after arguments.
5914 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
5915 ectx.ec_initial_frame_idx = ectx.ec_frame_idx;
5916
5917 {
5918 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5919 + ufunc->uf_dfunc_idx;
5920 ufunc_T *base_ufunc = dfunc->df_ufunc;
5921
5922 // "uf_partial" is on the ufunc that "df_ufunc" points to, as is done
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01005923 // by copy_lambda_to_global_func().
Bram Moolenaar4c137212021-04-19 16:48:48 +02005924 if (partial != NULL || base_ufunc->uf_partial != NULL)
5925 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005926 ectx.ec_outer_ref = ALLOC_CLEAR_ONE(outer_ref_T);
5927 if (ectx.ec_outer_ref == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005928 goto failed_early;
5929 if (partial != NULL)
5930 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00005931 outer_T *outer = get_pt_outer(partial);
5932
Bram Moolenaar6d313be2022-09-22 16:36:25 +01005933 if (outer->out_stack == NULL && outer->out_loop_size == 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005934 {
Bram Moolenaar6d313be2022-09-22 16:36:25 +01005935 // no stack was set
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00005936 if (current_ectx != NULL)
5937 {
5938 if (current_ectx->ec_outer_ref != NULL
5939 && current_ectx->ec_outer_ref->or_outer != NULL)
5940 ectx.ec_outer_ref->or_outer =
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005941 current_ectx->ec_outer_ref->or_outer;
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00005942 }
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005943 // else: should there be an error here?
Bram Moolenaar4c137212021-04-19 16:48:48 +02005944 }
5945 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005946 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00005947 ectx.ec_outer_ref->or_outer = outer;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005948 ++partial->pt_refcount;
5949 ectx.ec_outer_ref->or_partial = partial;
5950 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005951 }
5952 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005953 {
5954 ectx.ec_outer_ref->or_outer = &base_ufunc->uf_partial->pt_outer;
5955 ++base_ufunc->uf_partial->pt_refcount;
5956 ectx.ec_outer_ref->or_partial = base_ufunc->uf_partial;
5957 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005958 }
5959 }
5960
5961 // dummy frame entries
5962 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
5963 {
5964 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
5965 ++ectx.ec_stack.ga_len;
5966 }
5967
5968 {
5969 // Reserve space for local variables and any closure reference count.
5970 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5971 + ufunc->uf_dfunc_idx;
5972
Bram Moolenaar5cd64792021-12-25 18:23:24 +00005973 // Initialize variables to zero. That avoids having to generate
5974 // initializing instructions for "var nr: number", "var x: any", etc.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005975 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaar5cd64792021-12-25 18:23:24 +00005976 {
5977 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
5978 STACK_TV_VAR(idx)->vval.v_number = 0;
5979 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005980 ectx.ec_stack.ga_len += dfunc->df_varcount;
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005981
5982 if (object != NULL)
5983 {
5984 // the object is always the variable at index zero
5985 tv = STACK_TV_VAR(0);
5986 tv->v_type = VAR_OBJECT;
5987 tv->vval.v_object = object;
5988 }
5989
Bram Moolenaar4c137212021-04-19 16:48:48 +02005990 if (dfunc->df_has_closure)
5991 {
Bram Moolenaarcc341812022-09-19 15:54:34 +01005992 // Initialize the variable that counts how many closures were
5993 // created. This is used in handle_closure_in_use().
Bram Moolenaar4c137212021-04-19 16:48:48 +02005994 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
5995 STACK_TV_VAR(idx)->vval.v_number = 0;
5996 ++ectx.ec_stack.ga_len;
5997 }
5998
5999 ectx.ec_instr = INSTRUCTIONS(dfunc);
6000 }
6001
Bram Moolenaar58779852022-09-06 18:31:14 +01006002 // Store the execution context in funccal, used by invoke_all_defer().
6003 if (funccal != NULL)
6004 funccal->fc_ectx = &ectx;
6005
Bram Moolenaar4c137212021-04-19 16:48:48 +02006006 // Following errors are in the function, not the caller.
6007 // Commands behave like vim9script.
6008 estack_push_ufunc(ufunc, 1);
6009 current_sctx = ufunc->uf_script_ctx;
6010 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
6011
6012 // Use a specific location for storing error messages to be converted to an
6013 // exception.
6014 saved_msg_list = msg_list;
6015 msg_list = &private_msg_list;
6016
6017 // Do turn errors into exceptions.
6018 suppress_errthrow = FALSE;
6019
6020 // Do not delete the function while executing it.
6021 ++ufunc->uf_calls;
6022
6023 // When ":silent!" was used before calling then we still abort the
6024 // function. If ":silent!" is used in the function then we don't.
6025 emsg_silent_def = emsg_silent;
6026 did_emsg_def = 0;
6027
6028 ectx.ec_where.wt_index = 0;
6029 ectx.ec_where.wt_variable = FALSE;
6030
Bram Moolenaar5800c792022-09-22 17:34:01 +01006031 /*
6032 * Execute the instructions until done.
6033 */
Bram Moolenaar4c137212021-04-19 16:48:48 +02006034 ret = exec_instructions(&ectx);
6035 if (ret == OK)
6036 {
6037 // function finished, get result from the stack.
6038 if (ufunc->uf_ret_type == &t_void)
6039 {
6040 rettv->v_type = VAR_VOID;
6041 }
6042 else
6043 {
6044 tv = STACK_TV_BOT(-1);
6045 *rettv = *tv;
6046 tv->v_type = VAR_UNKNOWN;
6047 }
6048 }
6049
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01006050 // When failed need to unwind the call stack.
Bram Moolenaar58779852022-09-06 18:31:14 +01006051 unwind_def_callstack(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02006052
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02006053 // Deal with any remaining closures, they may be in use somewhere.
6054 if (ectx.ec_funcrefs.ga_len > 0)
Bram Moolenaarf112f302020-12-20 17:47:52 +01006055 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02006056 handle_closure_in_use(&ectx, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00006057 ga_clear(&ectx.ec_funcrefs);
Bram Moolenaarf112f302020-12-20 17:47:52 +01006058 }
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02006059
Bram Moolenaaree8580e2020-08-28 17:19:07 +02006060 estack_pop();
6061 current_sctx = save_current_sctx;
6062
Bram Moolenaar2336c372021-12-06 15:06:54 +00006063 if (--ufunc->uf_calls <= 0 && ufunc->uf_refcount <= 0)
6064 // Function was unreferenced while being used, free it now.
6065 func_clear_free(ufunc, FALSE);
Bram Moolenaarc970e422021-03-17 15:03:04 +01006066
Bram Moolenaar352134b2020-10-17 22:04:08 +02006067 if (*msg_list != NULL && saved_msg_list != NULL)
6068 {
6069 msglist_T **plist = saved_msg_list;
6070
6071 // Append entries from the current msg_list (uncaught exceptions) to
6072 // the saved msg_list.
6073 while (*plist != NULL)
6074 plist = &(*plist)->next;
6075
6076 *plist = *msg_list;
6077 }
6078 msg_list = saved_msg_list;
6079
Bram Moolenaar4c137212021-04-19 16:48:48 +02006080 if (ectx.ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar02194d22020-10-24 23:08:38 +02006081 {
6082 cmdmod.cmod_filter_regmatch.regprog = NULL;
6083 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006084 cmdmod = ectx.ec_funclocal.floc_save_cmdmod;
Bram Moolenaar02194d22020-10-24 23:08:38 +02006085 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +01006086 emsg_silent_def = save_emsg_silent_def;
6087 did_emsg_def += save_did_emsg_def;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02006088
Bram Moolenaaree8580e2020-08-28 17:19:07 +02006089failed_early:
Bram Moolenaar0d1f55c2022-04-05 17:30:29 +01006090 // Free all arguments and local variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006091 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
Bram Moolenaar1936c762022-09-28 15:19:10 +01006092 {
6093 tv = STACK_TV(idx);
6094 if (tv->v_type != VAR_NUMBER && tv->v_type != VAR_UNKNOWN)
6095 clear_tv(tv);
6096 }
Bram Moolenaare99d4222021-06-13 14:01:26 +02006097 ex_nesting_level = orig_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02006098
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006099 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01006100 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006101 if (ectx.ec_outer_ref != NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +01006102 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006103 if (ectx.ec_outer_ref->or_outer_allocated)
6104 vim_free(ectx.ec_outer_ref->or_outer);
6105 partial_unref(ectx.ec_outer_ref->or_partial);
6106 vim_free(ectx.ec_outer_ref);
Bram Moolenaar0186e582021-01-10 18:33:11 +01006107 }
6108
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02006109 // Not sure if this is necessary.
6110 suppress_errthrow = save_suppress_errthrow;
6111
Bram Moolenaarb5841b92021-07-15 18:09:53 +02006112 if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before
6113 && !need_rethrow)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006114 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02006115 printable_func_name(ufunc));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01006116 funcdepth_restore(orig_funcdepth);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006117 return ret;
6118}
6119
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006120/*
Bram Moolenaar58779852022-09-06 18:31:14 +01006121 * Called when a def function has finished (possibly failed).
6122 * Invoke all the function returns to clean up and invoke deferred functions,
6123 * except the toplevel one.
6124 */
6125 void
6126unwind_def_callstack(ectx_T *ectx)
6127{
6128 while (ectx->ec_frame_idx != ectx->ec_initial_frame_idx)
6129 func_return(ectx);
6130}
6131
6132/*
dundargocc57b5bc2022-11-02 13:30:51 +00006133 * Invoke any deferred functions for the top function in "ectx".
Bram Moolenaar58779852022-09-06 18:31:14 +01006134 */
6135 void
6136may_invoke_defer_funcs(ectx_T *ectx)
6137{
6138 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
6139
6140 if (dfunc->df_defer_var_idx > 0)
6141 invoke_defer_funcs(ectx);
6142}
6143
6144/*
Bram Moolenaarcc341812022-09-19 15:54:34 +01006145 * Return loopvarinfo in a printable form in allocated memory.
6146 */
6147 static char_u *
6148printable_loopvarinfo(loopvarinfo_T *lvi)
6149{
6150 garray_T ga;
6151 int depth;
6152
6153 ga_init2(&ga, 1, 100);
6154 for (depth = 0; depth < lvi->lvi_depth; ++depth)
6155 {
6156 if (ga_grow(&ga, 50) == FAIL)
6157 break;
6158 if (lvi->lvi_loop[depth].var_idx == 0)
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +01006159 STRCPY((char *)ga.ga_data + ga.ga_len, " -");
Bram Moolenaarcc341812022-09-19 15:54:34 +01006160 else
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +01006161 vim_snprintf((char *)ga.ga_data + ga.ga_len, 50, " $%d-$%d",
Bram Moolenaarcc341812022-09-19 15:54:34 +01006162 lvi->lvi_loop[depth].var_idx,
6163 lvi->lvi_loop[depth].var_idx
6164 + lvi->lvi_loop[depth].var_count - 1);
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +01006165 ga.ga_len = (int)STRLEN(ga.ga_data);
Bram Moolenaarcc341812022-09-19 15:54:34 +01006166 }
6167 return ga.ga_data;
6168}
6169
6170/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02006171 * List instructions "instr" up to "instr_count" or until ISN_FINISH.
6172 * "ufunc" has the source lines, NULL for the instructions of ISN_SUBSTITUTE.
6173 * "pfx" is prefixed to every line.
6174 */
6175 static void
6176list_instructions(char *pfx, isn_T *instr, int instr_count, ufunc_T *ufunc)
6177{
6178 int line_idx = 0;
6179 int prev_current = 0;
6180 int current;
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02006181 int def_arg_idx = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006182
6183 for (current = 0; current < instr_count; ++current)
6184 {
6185 isn_T *iptr = &instr[current];
6186 char *line;
6187
6188 if (ufunc != NULL)
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02006189 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02006190 while (line_idx < iptr->isn_lnum
6191 && line_idx < ufunc->uf_lines.ga_len)
6192 {
6193 if (current > prev_current)
6194 {
6195 msg_puts("\n\n");
6196 prev_current = current;
6197 }
6198 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
6199 if (line != NULL)
6200 msg(line);
6201 }
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02006202 if (iptr->isn_type == ISN_JUMP_IF_ARG_SET)
6203 {
6204 int first_def_arg = ufunc->uf_args.ga_len
6205 - ufunc->uf_def_args.ga_len;
6206
6207 if (def_arg_idx > 0)
6208 msg_puts("\n\n");
6209 msg_start();
6210 msg_puts(" ");
6211 msg_puts(((char **)(ufunc->uf_args.ga_data))[
6212 first_def_arg + def_arg_idx]);
6213 msg_puts(" = ");
6214 msg_puts(((char **)(ufunc->uf_def_args.ga_data))[def_arg_idx++]);
6215 msg_clr_eos();
6216 msg_end();
6217 }
6218 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006219
6220 switch (iptr->isn_type)
6221 {
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006222 case ISN_CONSTRUCT:
6223 smsg("%s%4d NEW %s size %d", pfx, current,
6224 iptr->isn_arg.construct.construct_class->class_name,
6225 (int)iptr->isn_arg.construct.construct_size);
6226 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006227 case ISN_EXEC:
6228 smsg("%s%4d EXEC %s", pfx, current, iptr->isn_arg.string);
6229 break;
Bram Moolenaar20677332021-06-06 17:02:53 +02006230 case ISN_EXEC_SPLIT:
6231 smsg("%s%4d EXEC_SPLIT %s", pfx, current, iptr->isn_arg.string);
6232 break;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00006233 case ISN_EXECRANGE:
6234 smsg("%s%4d EXECRANGE %s", pfx, current, iptr->isn_arg.string);
6235 break;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02006236 case ISN_LEGACY_EVAL:
6237 smsg("%s%4d EVAL legacy %s", pfx, current,
6238 iptr->isn_arg.string);
6239 break;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02006240 case ISN_REDIRSTART:
6241 smsg("%s%4d REDIR", pfx, current);
6242 break;
6243 case ISN_REDIREND:
6244 smsg("%s%4d REDIR END%s", pfx, current,
6245 iptr->isn_arg.number ? " append" : "");
6246 break;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006247 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02006248#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006249 smsg("%s%4d CEXPR pre %s", pfx, current,
6250 cexpr_get_auname(iptr->isn_arg.number));
Bram Moolenaarb7c97812021-05-05 22:51:39 +02006251#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006252 break;
6253 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02006254#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006255 {
6256 cexprref_T *cer = iptr->isn_arg.cexpr.cexpr_ref;
6257
6258 smsg("%s%4d CEXPR core %s%s \"%s\"", pfx, current,
6259 cexpr_get_auname(cer->cer_cmdidx),
6260 cer->cer_forceit ? "!" : "",
6261 cer->cer_cmdline);
6262 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02006263#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006264 break;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006265 case ISN_INSTR:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006266 smsg("%s%4d INSTR", pfx, current);
6267 list_instructions(" ", iptr->isn_arg.instr, INT_MAX, NULL);
6268 msg(" -------------");
6269 break;
6270 case ISN_SOURCE:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006271 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006272 scriptitem_T *si = SCRIPT_ITEM(iptr->isn_arg.number);
6273
6274 smsg("%s%4d SOURCE %s", pfx, current, si->sn_name);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006275 }
6276 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006277 case ISN_SUBSTITUTE:
6278 {
6279 subs_T *subs = &iptr->isn_arg.subs;
6280
6281 smsg("%s%4d SUBSTITUTE %s", pfx, current, subs->subs_cmd);
6282 list_instructions(" ", subs->subs_instr, INT_MAX, NULL);
6283 msg(" -------------");
6284 }
6285 break;
6286 case ISN_EXECCONCAT:
6287 smsg("%s%4d EXECCONCAT %lld", pfx, current,
6288 (varnumber_T)iptr->isn_arg.number);
6289 break;
6290 case ISN_ECHO:
6291 {
6292 echo_T *echo = &iptr->isn_arg.echo;
6293
6294 smsg("%s%4d %s %d", pfx, current,
6295 echo->echo_with_white ? "ECHO" : "ECHON",
6296 echo->echo_count);
6297 }
6298 break;
6299 case ISN_EXECUTE:
6300 smsg("%s%4d EXECUTE %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02006301 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006302 break;
6303 case ISN_ECHOMSG:
6304 smsg("%s%4d ECHOMSG %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02006305 (varnumber_T)(iptr->isn_arg.number));
6306 break;
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01006307 case ISN_ECHOWINDOW:
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01006308 if (iptr->isn_arg.echowin.ewin_time > 0)
6309 smsg("%s%4d ECHOWINDOW %d (%ld sec)", pfx, current,
6310 iptr->isn_arg.echowin.ewin_count,
6311 iptr->isn_arg.echowin.ewin_time);
6312 else
6313 smsg("%s%4d ECHOWINDOW %d", pfx, current,
6314 iptr->isn_arg.echowin.ewin_count);
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01006315 break;
Bram Moolenaar7de62622021-08-07 15:05:47 +02006316 case ISN_ECHOCONSOLE:
6317 smsg("%s%4d ECHOCONSOLE %lld", pfx, current,
6318 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006319 break;
6320 case ISN_ECHOERR:
6321 smsg("%s%4d ECHOERR %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02006322 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006323 break;
6324 case ISN_LOAD:
6325 {
6326 if (iptr->isn_arg.number < 0)
6327 smsg("%s%4d LOAD arg[%lld]", pfx, current,
6328 (varnumber_T)(iptr->isn_arg.number
6329 + STACK_FRAME_SIZE));
6330 else
6331 smsg("%s%4d LOAD $%lld", pfx, current,
6332 (varnumber_T)(iptr->isn_arg.number));
6333 }
6334 break;
6335 case ISN_LOADOUTER:
6336 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006337 isn_outer_T *outer = &iptr->isn_arg.outer;
6338
6339 if (outer->outer_idx < 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02006340 smsg("%s%4d LOADOUTER level %d arg[%d]", pfx, current,
Bram Moolenaarcc341812022-09-19 15:54:34 +01006341 outer->outer_depth,
6342 outer->outer_idx + STACK_FRAME_SIZE);
6343 else if (outer->outer_depth < 0)
6344 smsg("%s%4d LOADOUTER $%d in loop level %d",
6345 pfx, current,
6346 outer->outer_idx,
6347 -outer->outer_depth);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006348 else
6349 smsg("%s%4d LOADOUTER level %d $%d", pfx, current,
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006350 outer->outer_depth,
6351 outer->outer_idx);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006352 }
6353 break;
6354 case ISN_LOADV:
6355 smsg("%s%4d LOADV v:%s", pfx, current,
6356 get_vim_var_name(iptr->isn_arg.number));
6357 break;
6358 case ISN_LOADSCRIPT:
6359 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006360 scriptref_T *sref = iptr->isn_arg.script.scriptref;
6361 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
6362 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006363
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006364 sv = get_script_svar(sref, -1);
6365 if (sv == NULL)
6366 smsg("%s%4d LOADSCRIPT [deleted] from %s",
6367 pfx, current, si->sn_name);
6368 else
6369 smsg("%s%4d LOADSCRIPT %s-%d from %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02006370 sv->sv_name,
6371 sref->sref_idx,
6372 si->sn_name);
6373 }
6374 break;
6375 case ISN_LOADS:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006376 case ISN_LOADEXPORT:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006377 {
6378 scriptitem_T *si = SCRIPT_ITEM(
6379 iptr->isn_arg.loadstore.ls_sid);
6380
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006381 smsg("%s%4d %s s:%s from %s", pfx, current,
6382 iptr->isn_type == ISN_LOADS ? "LOADS"
6383 : "LOADEXPORT",
6384 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006385 }
6386 break;
6387 case ISN_LOADAUTO:
6388 smsg("%s%4d LOADAUTO %s", pfx, current, iptr->isn_arg.string);
6389 break;
6390 case ISN_LOADG:
6391 smsg("%s%4d LOADG g:%s", pfx, current, iptr->isn_arg.string);
6392 break;
6393 case ISN_LOADB:
6394 smsg("%s%4d LOADB b:%s", pfx, current, iptr->isn_arg.string);
6395 break;
6396 case ISN_LOADW:
6397 smsg("%s%4d LOADW w:%s", pfx, current, iptr->isn_arg.string);
6398 break;
6399 case ISN_LOADT:
6400 smsg("%s%4d LOADT t:%s", pfx, current, iptr->isn_arg.string);
6401 break;
6402 case ISN_LOADGDICT:
6403 smsg("%s%4d LOAD g:", pfx, current);
6404 break;
6405 case ISN_LOADBDICT:
6406 smsg("%s%4d LOAD b:", pfx, current);
6407 break;
6408 case ISN_LOADWDICT:
6409 smsg("%s%4d LOAD w:", pfx, current);
6410 break;
6411 case ISN_LOADTDICT:
6412 smsg("%s%4d LOAD t:", pfx, current);
6413 break;
6414 case ISN_LOADOPT:
6415 smsg("%s%4d LOADOPT %s", pfx, current, iptr->isn_arg.string);
6416 break;
6417 case ISN_LOADENV:
6418 smsg("%s%4d LOADENV %s", pfx, current, iptr->isn_arg.string);
6419 break;
6420 case ISN_LOADREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006421 smsg("%s%4d LOADREG @%c", pfx, current,
6422 (int)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006423 break;
6424
6425 case ISN_STORE:
6426 if (iptr->isn_arg.number < 0)
6427 smsg("%s%4d STORE arg[%lld]", pfx, current,
6428 iptr->isn_arg.number + STACK_FRAME_SIZE);
6429 else
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006430 smsg("%s%4d STORE $%lld", pfx, current,
6431 iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006432 break;
6433 case ISN_STOREOUTER:
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006434 {
6435 isn_outer_T *outer = &iptr->isn_arg.outer;
6436
6437 if (outer->outer_depth == OUTER_LOOP_DEPTH)
6438 smsg("%s%4d STOREOUTER level 1 $%d in loop",
6439 pfx, current, outer->outer_idx);
6440 else
6441 smsg("%s%4d STOREOUTER level %d $%d", pfx, current,
6442 outer->outer_depth, outer->outer_idx);
6443 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006444 break;
6445 case ISN_STOREV:
6446 smsg("%s%4d STOREV v:%s", pfx, current,
6447 get_vim_var_name(iptr->isn_arg.number));
6448 break;
6449 case ISN_STOREAUTO:
6450 smsg("%s%4d STOREAUTO %s", pfx, current, iptr->isn_arg.string);
6451 break;
6452 case ISN_STOREG:
6453 smsg("%s%4d STOREG %s", pfx, current, iptr->isn_arg.string);
6454 break;
6455 case ISN_STOREB:
6456 smsg("%s%4d STOREB %s", pfx, current, iptr->isn_arg.string);
6457 break;
6458 case ISN_STOREW:
6459 smsg("%s%4d STOREW %s", pfx, current, iptr->isn_arg.string);
6460 break;
6461 case ISN_STORET:
6462 smsg("%s%4d STORET %s", pfx, current, iptr->isn_arg.string);
6463 break;
6464 case ISN_STORES:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006465 case ISN_STOREEXPORT:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006466 {
6467 scriptitem_T *si = SCRIPT_ITEM(
6468 iptr->isn_arg.loadstore.ls_sid);
6469
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006470 smsg("%s%4d %s %s in %s", pfx, current,
6471 iptr->isn_type == ISN_STORES
6472 ? "STORES" : "STOREEXPORT",
Bram Moolenaar4c137212021-04-19 16:48:48 +02006473 iptr->isn_arg.loadstore.ls_name, si->sn_name);
6474 }
6475 break;
6476 case ISN_STORESCRIPT:
6477 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006478 scriptref_T *sref = iptr->isn_arg.script.scriptref;
6479 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
6480 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006481
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006482 sv = get_script_svar(sref, -1);
6483 if (sv == NULL)
6484 smsg("%s%4d STORESCRIPT [deleted] in %s",
6485 pfx, current, si->sn_name);
6486 else
6487 smsg("%s%4d STORESCRIPT %s-%d in %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02006488 sv->sv_name,
6489 sref->sref_idx,
6490 si->sn_name);
6491 }
6492 break;
6493 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00006494 case ISN_STOREFUNCOPT:
6495 smsg("%s%4d %s &%s", pfx, current,
6496 iptr->isn_type == ISN_STOREOPT ? "STOREOPT" : "STOREFUNCOPT",
Bram Moolenaar4c137212021-04-19 16:48:48 +02006497 iptr->isn_arg.storeopt.so_name);
6498 break;
6499 case ISN_STOREENV:
6500 smsg("%s%4d STOREENV $%s", pfx, current, iptr->isn_arg.string);
6501 break;
6502 case ISN_STOREREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006503 smsg("%s%4d STOREREG @%c", pfx, current,
6504 (int)iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006505 break;
6506 case ISN_STORENR:
6507 smsg("%s%4d STORE %lld in $%d", pfx, current,
6508 iptr->isn_arg.storenr.stnr_val,
6509 iptr->isn_arg.storenr.stnr_idx);
6510 break;
6511
6512 case ISN_STOREINDEX:
6513 smsg("%s%4d STOREINDEX %s", pfx, current,
Bram Moolenaarf7d1c6e2023-01-16 20:47:57 +00006514 vartype_name(iptr->isn_arg.storeindex.si_vartype));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006515 break;
6516
6517 case ISN_STORERANGE:
6518 smsg("%s%4d STORERANGE", pfx, current);
6519 break;
6520
Bram Moolenaard505d172022-12-18 21:42:55 +00006521 case ISN_LOAD_CLASSMEMBER:
6522 case ISN_STORE_CLASSMEMBER:
6523 {
6524 class_T *cl = iptr->isn_arg.classmember.cm_class;
6525 int idx = iptr->isn_arg.classmember.cm_idx;
6526 ocmember_T *ocm = &cl->class_class_members[idx];
6527 smsg("%s%4d %s CLASSMEMBER %s.%s", pfx, current,
6528 iptr->isn_type == ISN_LOAD_CLASSMEMBER
6529 ? "LOAD" : "STORE",
6530 cl->class_name, ocm->ocm_name);
6531 }
6532 break;
6533
Bram Moolenaar4c137212021-04-19 16:48:48 +02006534 // constants
6535 case ISN_PUSHNR:
6536 smsg("%s%4d PUSHNR %lld", pfx, current,
6537 (varnumber_T)(iptr->isn_arg.number));
6538 break;
6539 case ISN_PUSHBOOL:
6540 case ISN_PUSHSPEC:
6541 smsg("%s%4d PUSH %s", pfx, current,
6542 get_var_special_name(iptr->isn_arg.number));
6543 break;
6544 case ISN_PUSHF:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006545 smsg("%s%4d PUSHF %g", pfx, current, iptr->isn_arg.fnumber);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006546 break;
6547 case ISN_PUSHS:
6548 smsg("%s%4d PUSHS \"%s\"", pfx, current, iptr->isn_arg.string);
6549 break;
6550 case ISN_PUSHBLOB:
6551 {
6552 char_u *r;
6553 char_u numbuf[NUMBUFLEN];
6554 char_u *tofree;
6555
6556 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
6557 smsg("%s%4d PUSHBLOB %s", pfx, current, r);
6558 vim_free(tofree);
6559 }
6560 break;
6561 case ISN_PUSHFUNC:
6562 {
6563 char *name = (char *)iptr->isn_arg.string;
6564
6565 smsg("%s%4d PUSHFUNC \"%s\"", pfx, current,
6566 name == NULL ? "[none]" : name);
6567 }
6568 break;
6569 case ISN_PUSHCHANNEL:
6570#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar397a87a2022-03-20 21:14:15 +00006571 smsg("%s%4d PUSHCHANNEL 0", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006572#endif
6573 break;
6574 case ISN_PUSHJOB:
6575#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar397a87a2022-03-20 21:14:15 +00006576 smsg("%s%4d PUSHJOB \"no process\"", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006577#endif
6578 break;
6579 case ISN_PUSHEXC:
6580 smsg("%s%4d PUSH v:exception", pfx, current);
6581 break;
Bram Moolenaar06b77222022-01-25 15:51:56 +00006582 case ISN_AUTOLOAD:
6583 smsg("%s%4d AUTOLOAD %s", pfx, current, iptr->isn_arg.string);
6584 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006585 case ISN_UNLET:
6586 smsg("%s%4d UNLET%s %s", pfx, current,
6587 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
6588 iptr->isn_arg.unlet.ul_name);
6589 break;
6590 case ISN_UNLETENV:
6591 smsg("%s%4d UNLETENV%s $%s", pfx, current,
6592 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
6593 iptr->isn_arg.unlet.ul_name);
6594 break;
6595 case ISN_UNLETINDEX:
6596 smsg("%s%4d UNLETINDEX", pfx, current);
6597 break;
6598 case ISN_UNLETRANGE:
6599 smsg("%s%4d UNLETRANGE", pfx, current);
6600 break;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02006601 case ISN_LOCKUNLOCK:
6602 smsg("%s%4d LOCKUNLOCK %s", pfx, current, iptr->isn_arg.string);
6603 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006604 case ISN_LOCKCONST:
6605 smsg("%s%4d LOCKCONST", pfx, current);
6606 break;
6607 case ISN_NEWLIST:
6608 smsg("%s%4d NEWLIST size %lld", pfx, current,
Bram Moolenaar1936c762022-09-28 15:19:10 +01006609 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006610 break;
6611 case ISN_NEWDICT:
6612 smsg("%s%4d NEWDICT size %lld", pfx, current,
Bram Moolenaar1936c762022-09-28 15:19:10 +01006613 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006614 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00006615 case ISN_NEWPARTIAL:
6616 smsg("%s%4d NEWPARTIAL", pfx, current);
6617 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006618
6619 // function call
6620 case ISN_BCALL:
6621 {
6622 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
6623
6624 smsg("%s%4d BCALL %s(argc %d)", pfx, current,
6625 internal_func_name(cbfunc->cbf_idx),
6626 cbfunc->cbf_argcount);
6627 }
6628 break;
6629 case ISN_DCALL:
6630 {
6631 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
6632 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
6633 + cdfunc->cdf_idx;
6634
6635 smsg("%s%4d DCALL %s(argc %d)", pfx, current,
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006636 printable_func_name(df->df_ufunc),
6637 cdfunc->cdf_argcount);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006638 }
6639 break;
6640 case ISN_UCALL:
6641 {
6642 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
6643
6644 smsg("%s%4d UCALL %s(argc %d)", pfx, current,
6645 cufunc->cuf_name, cufunc->cuf_argcount);
6646 }
6647 break;
6648 case ISN_PCALL:
6649 {
6650 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
6651
6652 smsg("%s%4d PCALL%s (argc %d)", pfx, current,
6653 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
6654 }
6655 break;
6656 case ISN_PCALL_END:
6657 smsg("%s%4d PCALL end", pfx, current);
6658 break;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006659 case ISN_DEFER:
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00006660 case ISN_DEFEROBJ:
6661 smsg("%s%4d %s %d args", pfx, current,
6662 iptr->isn_type == ISN_DEFER ? "DEFER" : "DEFEROBJ",
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006663 (int)iptr->isn_arg.defer.defer_argcount);
6664 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006665 case ISN_RETURN:
6666 smsg("%s%4d RETURN", pfx, current);
6667 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02006668 case ISN_RETURN_VOID:
6669 smsg("%s%4d RETURN void", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006670 break;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006671 case ISN_RETURN_OBJECT:
6672 smsg("%s%4d RETURN object", pfx, current);
6673 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006674 case ISN_FUNCREF:
6675 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006676 funcref_T *funcref = &iptr->isn_arg.funcref;
6677 funcref_extra_T *extra = funcref->fr_extra;
6678 char_u *name;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006679
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006680 if (extra == NULL || extra->fre_func_name == NULL)
Bram Moolenaar38453522021-11-28 22:00:12 +00006681 {
6682 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
6683 + funcref->fr_dfunc_idx;
6684 name = df->df_ufunc->uf_name;
6685 }
6686 else
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006687 name = extra->fre_func_name;
Bram Moolenaarcc341812022-09-19 15:54:34 +01006688 if (extra == NULL || extra->fre_loopvar_info.lvi_depth == 0)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006689 smsg("%s%4d FUNCREF %s", pfx, current, name);
6690 else
Bram Moolenaarcc341812022-09-19 15:54:34 +01006691 {
6692 char_u *info = printable_loopvarinfo(
6693 &extra->fre_loopvar_info);
6694
6695 smsg("%s%4d FUNCREF %s vars %s", pfx, current,
6696 name, info);
6697 vim_free(info);
6698 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006699 }
6700 break;
6701
6702 case ISN_NEWFUNC:
6703 {
Bram Moolenaarcc341812022-09-19 15:54:34 +01006704 newfuncarg_T *arg = iptr->isn_arg.newfunc.nf_arg;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006705
Bram Moolenaarcc341812022-09-19 15:54:34 +01006706 if (arg->nfa_loopvar_info.lvi_depth == 0)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006707 smsg("%s%4d NEWFUNC %s %s", pfx, current,
6708 arg->nfa_lambda, arg->nfa_global);
6709 else
Bram Moolenaarcc341812022-09-19 15:54:34 +01006710 {
6711 char_u *info = printable_loopvarinfo(
6712 &arg->nfa_loopvar_info);
6713
6714 smsg("%s%4d NEWFUNC %s %s vars %s", pfx, current,
6715 arg->nfa_lambda, arg->nfa_global, info);
6716 vim_free(info);
6717 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006718 }
6719 break;
6720
6721 case ISN_DEF:
6722 {
6723 char_u *name = iptr->isn_arg.string;
6724
6725 smsg("%s%4d DEF %s", pfx, current,
6726 name == NULL ? (char_u *)"" : name);
6727 }
6728 break;
6729
6730 case ISN_JUMP:
6731 {
6732 char *when = "?";
6733
6734 switch (iptr->isn_arg.jump.jump_when)
6735 {
6736 case JUMP_ALWAYS:
6737 when = "JUMP";
6738 break;
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02006739 case JUMP_NEVER:
6740 iemsg("JUMP_NEVER should not be used");
6741 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006742 case JUMP_AND_KEEP_IF_TRUE:
6743 when = "JUMP_AND_KEEP_IF_TRUE";
6744 break;
6745 case JUMP_IF_FALSE:
6746 when = "JUMP_IF_FALSE";
6747 break;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01006748 case JUMP_WHILE_FALSE:
6749 when = "JUMP_WHILE_FALSE"; // unused
6750 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006751 case JUMP_IF_COND_FALSE:
6752 when = "JUMP_IF_COND_FALSE";
6753 break;
6754 case JUMP_IF_COND_TRUE:
6755 when = "JUMP_IF_COND_TRUE";
6756 break;
6757 }
6758 smsg("%s%4d %s -> %d", pfx, current, when,
6759 iptr->isn_arg.jump.jump_where);
6760 }
6761 break;
6762
6763 case ISN_JUMP_IF_ARG_SET:
6764 smsg("%s%4d JUMP_IF_ARG_SET arg[%d] -> %d", pfx, current,
6765 iptr->isn_arg.jumparg.jump_arg_off + STACK_FRAME_SIZE,
6766 iptr->isn_arg.jump.jump_where);
6767 break;
6768
Bram Moolenaar65b0d162022-12-13 18:43:22 +00006769 case ISN_JUMP_IF_ARG_NOT_SET:
6770 smsg("%s%4d JUMP_IF_ARG_NOT_SET arg[%d] -> %d", pfx, current,
6771 iptr->isn_arg.jumparg.jump_arg_off + STACK_FRAME_SIZE,
6772 iptr->isn_arg.jump.jump_where);
6773 break;
6774
Bram Moolenaar4c137212021-04-19 16:48:48 +02006775 case ISN_FOR:
6776 {
6777 forloop_T *forloop = &iptr->isn_arg.forloop;
6778
6779 smsg("%s%4d FOR $%d -> %d", pfx, current,
Bram Moolenaarcc341812022-09-19 15:54:34 +01006780 forloop->for_loop_idx, forloop->for_end);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006781 }
6782 break;
6783
Bram Moolenaarb46c0832022-09-15 17:19:37 +01006784 case ISN_ENDLOOP:
6785 {
6786 endloop_T *endloop = &iptr->isn_arg.endloop;
6787
Bram Moolenaarcc341812022-09-19 15:54:34 +01006788 smsg("%s%4d ENDLOOP ref $%d save $%d-$%d depth %d",
6789 pfx, current,
Bram Moolenaarb46c0832022-09-15 17:19:37 +01006790 endloop->end_funcref_idx,
6791 endloop->end_var_idx,
Bram Moolenaarcc341812022-09-19 15:54:34 +01006792 endloop->end_var_idx + endloop->end_var_count - 1,
6793 endloop->end_depth);
Bram Moolenaarb46c0832022-09-15 17:19:37 +01006794 }
6795 break;
6796
6797 case ISN_WHILE:
6798 {
6799 whileloop_T *whileloop = &iptr->isn_arg.whileloop;
6800
6801 smsg("%s%4d WHILE $%d -> %d", pfx, current,
6802 whileloop->while_funcref_idx,
6803 whileloop->while_end);
6804 }
6805 break;
6806
Bram Moolenaar4c137212021-04-19 16:48:48 +02006807 case ISN_TRY:
6808 {
Bram Moolenaar0d807102021-12-21 09:42:09 +00006809 try_T *try = &iptr->isn_arg.tryref;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006810
6811 if (try->try_ref->try_finally == 0)
6812 smsg("%s%4d TRY catch -> %d, endtry -> %d",
6813 pfx, current,
6814 try->try_ref->try_catch,
6815 try->try_ref->try_endtry);
6816 else
6817 smsg("%s%4d TRY catch -> %d, finally -> %d, endtry -> %d",
6818 pfx, current,
6819 try->try_ref->try_catch,
6820 try->try_ref->try_finally,
6821 try->try_ref->try_endtry);
6822 }
6823 break;
6824 case ISN_CATCH:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006825 smsg("%s%4d CATCH", pfx, current);
6826 break;
6827 case ISN_TRYCONT:
6828 {
6829 trycont_T *trycont = &iptr->isn_arg.trycont;
6830
6831 smsg("%s%4d TRY-CONTINUE %d level%s -> %d", pfx, current,
6832 trycont->tct_levels,
6833 trycont->tct_levels == 1 ? "" : "s",
6834 trycont->tct_where);
6835 }
6836 break;
6837 case ISN_FINALLY:
6838 smsg("%s%4d FINALLY", pfx, current);
6839 break;
6840 case ISN_ENDTRY:
6841 smsg("%s%4d ENDTRY", pfx, current);
6842 break;
6843 case ISN_THROW:
6844 smsg("%s%4d THROW", pfx, current);
6845 break;
6846
6847 // expression operations on number
6848 case ISN_OPNR:
6849 case ISN_OPFLOAT:
6850 case ISN_OPANY:
6851 {
6852 char *what;
6853 char *ins;
6854
6855 switch (iptr->isn_arg.op.op_type)
6856 {
6857 case EXPR_MULT: what = "*"; break;
6858 case EXPR_DIV: what = "/"; break;
6859 case EXPR_REM: what = "%"; break;
6860 case EXPR_SUB: what = "-"; break;
6861 case EXPR_ADD: what = "+"; break;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01006862 case EXPR_LSHIFT: what = "<<"; break;
6863 case EXPR_RSHIFT: what = ">>"; break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006864 default: what = "???"; break;
6865 }
6866 switch (iptr->isn_type)
6867 {
6868 case ISN_OPNR: ins = "OPNR"; break;
6869 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
6870 case ISN_OPANY: ins = "OPANY"; break;
6871 default: ins = "???"; break;
6872 }
6873 smsg("%s%4d %s %s", pfx, current, ins, what);
6874 }
6875 break;
6876
6877 case ISN_COMPAREBOOL:
6878 case ISN_COMPARESPECIAL:
Bram Moolenaar7a222242022-03-01 19:23:24 +00006879 case ISN_COMPARENULL:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006880 case ISN_COMPARENR:
6881 case ISN_COMPAREFLOAT:
6882 case ISN_COMPARESTRING:
6883 case ISN_COMPAREBLOB:
6884 case ISN_COMPARELIST:
6885 case ISN_COMPAREDICT:
6886 case ISN_COMPAREFUNC:
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00006887 case ISN_COMPARECLASS:
6888 case ISN_COMPAREOBJECT:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006889 case ISN_COMPAREANY:
6890 {
6891 char *p;
6892 char buf[10];
6893 char *type;
6894
6895 switch (iptr->isn_arg.op.op_type)
6896 {
6897 case EXPR_EQUAL: p = "=="; break;
6898 case EXPR_NEQUAL: p = "!="; break;
6899 case EXPR_GREATER: p = ">"; break;
6900 case EXPR_GEQUAL: p = ">="; break;
6901 case EXPR_SMALLER: p = "<"; break;
6902 case EXPR_SEQUAL: p = "<="; break;
6903 case EXPR_MATCH: p = "=~"; break;
6904 case EXPR_IS: p = "is"; break;
6905 case EXPR_ISNOT: p = "isnot"; break;
6906 case EXPR_NOMATCH: p = "!~"; break;
6907 default: p = "???"; break;
6908 }
6909 STRCPY(buf, p);
6910 if (iptr->isn_arg.op.op_ic == TRUE)
6911 strcat(buf, "?");
6912 switch(iptr->isn_type)
6913 {
6914 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
6915 case ISN_COMPARESPECIAL:
6916 type = "COMPARESPECIAL"; break;
Bram Moolenaar7a222242022-03-01 19:23:24 +00006917 case ISN_COMPARENULL: type = "COMPARENULL"; break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006918 case ISN_COMPARENR: type = "COMPARENR"; break;
6919 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
6920 case ISN_COMPARESTRING:
6921 type = "COMPARESTRING"; break;
6922 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
6923 case ISN_COMPARELIST: type = "COMPARELIST"; break;
6924 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
6925 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00006926 case ISN_COMPARECLASS: type = "COMPARECLASS"; break;
6927 case ISN_COMPAREOBJECT:
6928 type = "COMPAREOBJECT"; break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006929 case ISN_COMPAREANY: type = "COMPAREANY"; break;
6930 default: type = "???"; break;
6931 }
6932
6933 smsg("%s%4d %s %s", pfx, current, type, buf);
6934 }
6935 break;
6936
6937 case ISN_ADDLIST: smsg("%s%4d ADDLIST", pfx, current); break;
6938 case ISN_ADDBLOB: smsg("%s%4d ADDBLOB", pfx, current); break;
6939
6940 // expression operations
LemonBoy372bcce2022-04-25 12:43:20 +01006941 case ISN_CONCAT:
6942 smsg("%s%4d CONCAT size %lld", pfx, current,
6943 (varnumber_T)(iptr->isn_arg.number));
6944 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006945 case ISN_STRINDEX: smsg("%s%4d STRINDEX", pfx, current); break;
6946 case ISN_STRSLICE: smsg("%s%4d STRSLICE", pfx, current); break;
6947 case ISN_BLOBINDEX: smsg("%s%4d BLOBINDEX", pfx, current); break;
6948 case ISN_BLOBSLICE: smsg("%s%4d BLOBSLICE", pfx, current); break;
6949 case ISN_LISTAPPEND: smsg("%s%4d LISTAPPEND", pfx, current); break;
6950 case ISN_BLOBAPPEND: smsg("%s%4d BLOBAPPEND", pfx, current); break;
6951 case ISN_LISTINDEX: smsg("%s%4d LISTINDEX", pfx, current); break;
6952 case ISN_LISTSLICE: smsg("%s%4d LISTSLICE", pfx, current); break;
6953 case ISN_ANYINDEX: smsg("%s%4d ANYINDEX", pfx, current); break;
6954 case ISN_ANYSLICE: smsg("%s%4d ANYSLICE", pfx, current); break;
6955 case ISN_SLICE: smsg("%s%4d SLICE %lld",
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02006956 pfx, current, iptr->isn_arg.number); break;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02006957 case ISN_GETITEM: smsg("%s%4d ITEM %lld%s", pfx, current,
6958 iptr->isn_arg.getitem.gi_index,
6959 iptr->isn_arg.getitem.gi_with_op ?
6960 " with op" : ""); break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006961 case ISN_MEMBER: smsg("%s%4d MEMBER", pfx, current); break;
6962 case ISN_STRINGMEMBER: smsg("%s%4d MEMBER %s", pfx, current,
6963 iptr->isn_arg.string); break;
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00006964 case ISN_GET_OBJ_MEMBER: smsg("%s%4d OBJ_MEMBER %d", pfx, current,
6965 (int)iptr->isn_arg.number); break;
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00006966 case ISN_GET_ITF_MEMBER: smsg("%s%4d ITF_MEMBER %d on %s",
6967 pfx, current,
6968 (int)iptr->isn_arg.classmember.cm_idx,
6969 iptr->isn_arg.classmember.cm_class->class_name);
6970 break;
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00006971 case ISN_STORE_THIS: smsg("%s%4d STORE_THIS %d", pfx, current,
Bram Moolenaarffdaca92022-12-09 21:41:48 +00006972 (int)iptr->isn_arg.number); break;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02006973 case ISN_CLEARDICT: smsg("%s%4d CLEARDICT", pfx, current); break;
6974 case ISN_USEDICT: smsg("%s%4d USEDICT", pfx, current); break;
6975
Bram Moolenaar4c137212021-04-19 16:48:48 +02006976 case ISN_NEGATENR: smsg("%s%4d NEGATENR", pfx, current); break;
6977
Bram Moolenaar4c137212021-04-19 16:48:48 +02006978 case ISN_CHECKTYPE:
6979 {
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01006980 checktype_T *ct = &iptr->isn_arg.type;
Bram Moolenaarc6951a72022-12-29 20:56:24 +00006981 char *tofree = NULL;
6982 char *typename;
6983
6984 if (ct->ct_type->tt_type == VAR_FLOAT
6985 && (ct->ct_type->tt_flags & TTFLAG_NUMBER_OK))
6986 typename = "float|number";
6987 else
6988 typename = type_name(ct->ct_type, &tofree);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006989
6990 if (ct->ct_arg_idx == 0)
6991 smsg("%s%4d CHECKTYPE %s stack[%d]", pfx, current,
Bram Moolenaarc6951a72022-12-29 20:56:24 +00006992 typename,
Bram Moolenaar4c137212021-04-19 16:48:48 +02006993 (int)ct->ct_off);
6994 else
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01006995 smsg("%s%4d CHECKTYPE %s stack[%d] %s %d",
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02006996 pfx, current,
Bram Moolenaarc6951a72022-12-29 20:56:24 +00006997 typename,
Bram Moolenaar4c137212021-04-19 16:48:48 +02006998 (int)ct->ct_off,
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01006999 ct->ct_is_var ? "var": "arg",
Bram Moolenaar4c137212021-04-19 16:48:48 +02007000 (int)ct->ct_arg_idx);
7001 vim_free(tofree);
7002 break;
7003 }
7004 case ISN_CHECKLEN: smsg("%s%4d CHECKLEN %s%d", pfx, current,
7005 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
7006 iptr->isn_arg.checklen.cl_min_len);
7007 break;
7008 case ISN_SETTYPE:
7009 {
7010 char *tofree;
7011
7012 smsg("%s%4d SETTYPE %s", pfx, current,
7013 type_name(iptr->isn_arg.type.ct_type, &tofree));
7014 vim_free(tofree);
7015 break;
7016 }
7017 case ISN_COND2BOOL: smsg("%s%4d COND2BOOL", pfx, current); break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007018 case ISN_2BOOL: if (iptr->isn_arg.tobool.invert)
7019 smsg("%s%4d INVERT %d (!val)", pfx, current,
7020 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007021 else
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007022 smsg("%s%4d 2BOOL %d (!!val)", pfx, current,
7023 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007024 break;
7025 case ISN_2STRING: smsg("%s%4d 2STRING stack[%lld]", pfx, current,
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007026 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02007027 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007028 case ISN_2STRING_ANY: smsg("%s%4d 2STRING_ANY stack[%lld]",
7029 pfx, current,
7030 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02007031 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007032 case ISN_RANGE: smsg("%s%4d RANGE %s", pfx, current,
7033 iptr->isn_arg.string);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007034 break;
7035 case ISN_PUT:
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01007036 if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE)
Bram Moolenaar4c137212021-04-19 16:48:48 +02007037 smsg("%s%4d PUT %c above range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007038 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007039 else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE)
7040 smsg("%s%4d PUT %c range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007041 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007042 else
7043 smsg("%s%4d PUT %c %ld", pfx, current,
7044 iptr->isn_arg.put.put_regname,
7045 (long)iptr->isn_arg.put.put_lnum);
7046 break;
7047
Bram Moolenaar4c137212021-04-19 16:48:48 +02007048 case ISN_CMDMOD:
7049 {
7050 char_u *buf;
7051 size_t len = produce_cmdmods(
7052 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
7053
7054 buf = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02007055 if (likely(buf != NULL))
Bram Moolenaar4c137212021-04-19 16:48:48 +02007056 {
7057 (void)produce_cmdmods(
7058 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
7059 smsg("%s%4d CMDMOD %s", pfx, current, buf);
7060 vim_free(buf);
7061 }
7062 break;
7063 }
7064 case ISN_CMDMOD_REV: smsg("%s%4d CMDMOD_REV", pfx, current); break;
7065
7066 case ISN_PROF_START:
Bram Moolenaare99d4222021-06-13 14:01:26 +02007067 smsg("%s%4d PROFILE START line %d", pfx, current,
7068 iptr->isn_lnum);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007069 break;
7070
7071 case ISN_PROF_END:
7072 smsg("%s%4d PROFILE END", pfx, current);
7073 break;
7074
Bram Moolenaare99d4222021-06-13 14:01:26 +02007075 case ISN_DEBUG:
Bram Moolenaar8cec9272021-06-23 20:20:53 +02007076 smsg("%s%4d DEBUG line %d-%d varcount %lld", pfx, current,
7077 iptr->isn_arg.debug.dbg_break_lnum + 1,
7078 iptr->isn_lnum,
7079 iptr->isn_arg.debug.dbg_var_names_len);
Bram Moolenaare99d4222021-06-13 14:01:26 +02007080 break;
7081
Bram Moolenaar4c137212021-04-19 16:48:48 +02007082 case ISN_UNPACK: smsg("%s%4d UNPACK %d%s", pfx, current,
7083 iptr->isn_arg.unpack.unp_count,
7084 iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : "");
7085 break;
7086 case ISN_SHUFFLE: smsg("%s%4d SHUFFLE %d up %d", pfx, current,
7087 iptr->isn_arg.shuffle.shfl_item,
7088 iptr->isn_arg.shuffle.shfl_up);
7089 break;
7090 case ISN_DROP: smsg("%s%4d DROP", pfx, current); break;
7091
7092 case ISN_FINISH: // End of list of instructions for ISN_SUBSTITUTE.
7093 return;
7094 }
7095
7096 out_flush(); // output one line at a time
7097 ui_breakcheck();
7098 if (got_int)
7099 break;
7100 }
7101}
7102
7103/*
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02007104 * Handle command line completion for the :disassemble command.
7105 */
7106 void
7107set_context_in_disassemble_cmd(expand_T *xp, char_u *arg)
7108{
7109 char_u *p;
7110
7111 // Default: expand user functions, "debug" and "profile"
7112 xp->xp_context = EXPAND_DISASSEMBLE;
7113 xp->xp_pattern = arg;
7114
7115 // first argument already typed: only user function names
7116 if (*arg != NUL && *(p = skiptowhite(arg)) != NUL)
7117 {
7118 xp->xp_context = EXPAND_USER_FUNC;
7119 xp->xp_pattern = skipwhite(p);
7120 }
7121}
7122
7123/*
7124 * Function given to ExpandGeneric() to obtain the list of :disassemble
7125 * arguments.
7126 */
7127 char_u *
7128get_disassemble_argument(expand_T *xp, int idx)
7129{
7130 if (idx == 0)
7131 return (char_u *)"debug";
7132 if (idx == 1)
7133 return (char_u *)"profile";
7134 return get_user_func_name(xp, idx - 2);
7135}
7136
7137/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01007138 * ":disassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01007139 * We don't really need this at runtime, but we do have tests that require it,
7140 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007141 */
7142 void
7143ex_disassemble(exarg_T *eap)
7144{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01007145 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01007146 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007147 dfunc_T *dfunc;
Bram Moolenaardafef512022-05-21 21:55:55 +01007148 isn_T *instr = NULL; // init to shut up gcc warning
7149 int instr_count = 0; // init to shut up gcc warning
Bram Moolenaar5a01caa2022-05-21 18:56:58 +01007150 compiletype_T compile_type = CT_NONE;
Bram Moolenaare99d4222021-06-13 14:01:26 +02007151
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01007152 ufunc = find_func_by_name(arg, &compile_type);
Bram Moolenaara26b9702020-04-18 19:53:28 +02007153 if (ufunc == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007154 return;
Bram Moolenaare99d4222021-06-13 14:01:26 +02007155 if (func_needs_compiling(ufunc, compile_type)
7156 && compile_def_function(ufunc, FALSE, compile_type, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02007157 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007158 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007159 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007160 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007161 return;
7162 }
Bram Moolenaar6db660b2021-08-01 14:08:54 +02007163 msg((char *)printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007164
7165 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
Bram Moolenaare99d4222021-06-13 14:01:26 +02007166 switch (compile_type)
7167 {
7168 case CT_PROFILE:
Bram Moolenaarf002a412021-01-24 13:34:18 +01007169#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02007170 instr = dfunc->df_instr_prof;
7171 instr_count = dfunc->df_instr_prof_count;
7172 break;
Bram Moolenaarf002a412021-01-24 13:34:18 +01007173#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02007174 // FALLTHROUGH
7175 case CT_NONE:
7176 instr = dfunc->df_instr;
7177 instr_count = dfunc->df_instr_count;
7178 break;
7179 case CT_DEBUG:
7180 instr = dfunc->df_instr_debug;
7181 instr_count = dfunc->df_instr_debug_count;
7182 break;
7183 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007184
Bram Moolenaar4c137212021-04-19 16:48:48 +02007185 list_instructions("", instr, instr_count, ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007186}
7187
7188/*
Bram Moolenaar13106602020-10-04 16:06:05 +02007189 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007190 * list, etc. Mostly like what JavaScript does, except that empty list and
7191 * empty dictionary are FALSE.
7192 */
7193 int
7194tv2bool(typval_T *tv)
7195{
7196 switch (tv->v_type)
7197 {
7198 case VAR_NUMBER:
7199 return tv->vval.v_number != 0;
7200 case VAR_FLOAT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007201 return tv->vval.v_float != 0.0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007202 case VAR_PARTIAL:
7203 return tv->vval.v_partial != NULL;
7204 case VAR_FUNC:
7205 case VAR_STRING:
7206 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
7207 case VAR_LIST:
7208 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
7209 case VAR_DICT:
7210 return tv->vval.v_dict != NULL
7211 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
7212 case VAR_BOOL:
7213 case VAR_SPECIAL:
7214 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
7215 case VAR_JOB:
7216#ifdef FEAT_JOB_CHANNEL
7217 return tv->vval.v_job != NULL;
7218#else
7219 break;
7220#endif
7221 case VAR_CHANNEL:
7222#ifdef FEAT_JOB_CHANNEL
7223 return tv->vval.v_channel != NULL;
7224#else
7225 break;
7226#endif
7227 case VAR_BLOB:
7228 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
7229 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02007230 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007231 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02007232 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00007233 case VAR_CLASS:
7234 case VAR_OBJECT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007235 break;
7236 }
7237 return FALSE;
7238}
7239
Bram Moolenaarea2d4072020-11-12 12:08:51 +01007240 void
7241emsg_using_string_as(typval_T *tv, int as_number)
7242{
7243 semsg(_(as_number ? e_using_string_as_number_str
7244 : e_using_string_as_bool_str),
7245 tv->vval.v_string == NULL
7246 ? (char_u *)"" : tv->vval.v_string);
7247}
7248
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007249/*
7250 * If "tv" is a string give an error and return FAIL.
7251 */
7252 int
7253check_not_string(typval_T *tv)
7254{
7255 if (tv->v_type == VAR_STRING)
7256 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +01007257 emsg_using_string_as(tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007258 clear_tv(tv);
7259 return FAIL;
7260 }
7261 return OK;
7262}
7263
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007264
7265#endif // FEAT_EVAL