blob: 7c61ff418ea0b9481e58693bf45a4ce712c070c5 [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
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00001055 FOR_ALL_LIST_ITEMS(defer_tv->vval.v_list, li)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001056 {
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
zeertzjqa1f2b5d2023-04-18 21:04:53 +01001066 if (functv->vval.v_string == NULL)
1067 // already being called, can happen if function does ":qa"
1068 continue;
1069
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001070 if (obj_off == 1)
1071 arg_li = arg_li->li_next; // second list item is the object
1072 for (i = 0; i < argcount; ++i)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001073 {
1074 arg_li = arg_li->li_next;
1075 argvars[i] = arg_li->li_tv;
1076 }
1077
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001078 funcexe_T funcexe;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001079 CLEAR_FIELD(funcexe);
1080 funcexe.fe_evaluate = TRUE;
1081 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001082 if (functv->v_type == VAR_PARTIAL)
1083 {
1084 funcexe.fe_partial = functv->vval.v_partial;
1085 funcexe.fe_object = l->lv_first->li_next->li_tv.vval.v_object;
1086 if (funcexe.fe_object != NULL)
1087 ++funcexe.fe_object->obj_refcount;
1088 }
zeertzjqa1f2b5d2023-04-18 21:04:53 +01001089
1090 char_u *name = functv->vval.v_string;
1091 functv->vval.v_string = NULL;
1092
1093 (void)call_func(name, -1, &rettv, argcount, argvars, &funcexe);
1094
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001095 clear_tv(&rettv);
zeertzjqa1f2b5d2023-04-18 21:04:53 +01001096 vim_free(name);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001097 }
1098}
1099
1100/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001101 * Return from the current function.
1102 */
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001103 static int
Bram Moolenaar4c137212021-04-19 16:48:48 +02001104func_return(ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001105{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001106 int idx;
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01001107 int ret_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001108 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1109 + ectx->ec_dfunc_idx;
1110 int argcount = ufunc_argcount(dfunc->df_ufunc);
Bram Moolenaarc620c052020-07-08 15:16:19 +02001111 estack_T *entry;
Bram Moolenaar12d26532021-02-19 19:13:21 +01001112 int prev_dfunc_idx = STACK_TV(ectx->ec_frame_idx
1113 + STACK_FRAME_FUNC_OFF)->vval.v_number;
Bram Moolenaarb06b50d2021-04-26 21:39:25 +02001114 funclocal_T *floc;
1115#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01001116 dfunc_T *prev_dfunc = ((dfunc_T *)def_functions.ga_data)
1117 + prev_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001118
Bram Moolenaar12d26532021-02-19 19:13:21 +01001119 if (do_profiling == PROF_YES)
1120 {
1121 ufunc_T *caller = prev_dfunc->df_ufunc;
1122
1123 if (dfunc->df_ufunc->uf_profiling
1124 || (caller != NULL && caller->uf_profiling))
1125 {
1126 profile_may_end_func(((profinfo_T *)profile_info_ga.ga_data)
1127 + profile_info_ga.ga_len - 1, dfunc->df_ufunc, caller);
1128 --profile_info_ga.ga_len;
1129 }
1130 }
1131#endif
Bram Moolenaar919c12c2021-12-14 14:29:16 +00001132
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001133 if (dfunc->df_defer_var_idx > 0)
1134 invoke_defer_funcs(ectx);
1135
Bram Moolenaar919c12c2021-12-14 14:29:16 +00001136 // No check for uf_refcount being zero, cannot think of a way that would
1137 // happen.
Bram Moolenaarc970e422021-03-17 15:03:04 +01001138 --dfunc->df_ufunc->uf_calls;
1139
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001140 // execution context goes one level up
Bram Moolenaarc620c052020-07-08 15:16:19 +02001141 entry = estack_pop();
1142 if (entry != NULL)
Bram Moolenaarc70fe462021-04-17 17:59:19 +02001143 current_sctx = entry->es_save_sctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001144
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001145 if (handle_closure_in_use(ectx, TRUE) == FAIL)
1146 return FAIL;
1147
Bram Moolenaar574950d2023-01-03 19:08:50 +00001148 // Clear the arguments. If this was an object method also clear the
1149 // object, it is just before the arguments.
1150 int top = ectx->ec_frame_idx - argcount;
1151 if (dfunc->df_ufunc->uf_flags & FC_OBJECT)
1152 --top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001153 for (idx = top; idx < ectx->ec_frame_idx; ++idx)
1154 clear_tv(STACK_TV(idx));
1155
1156 // Clear local variables and temp values, but not the return value.
1157 for (idx = ectx->ec_frame_idx + STACK_FRAME_SIZE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001158 idx < ectx->ec_stack.ga_len - 1; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001159 clear_tv(STACK_TV(idx));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001160
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01001161 // The return value should be on top of the stack. However, when aborting
1162 // it may not be there and ec_frame_idx is the top of the stack.
1163 ret_idx = ectx->ec_stack.ga_len - 1;
Bram Moolenaar0186e582021-01-10 18:33:11 +01001164 if (ret_idx == ectx->ec_frame_idx + STACK_FRAME_IDX_OFF)
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01001165 ret_idx = 0;
1166
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001167 if (ectx->ec_outer_ref != NULL)
1168 {
1169 if (ectx->ec_outer_ref->or_outer_allocated)
1170 vim_free(ectx->ec_outer_ref->or_outer);
1171 partial_unref(ectx->ec_outer_ref->or_partial);
1172 vim_free(ectx->ec_outer_ref);
1173 }
Bram Moolenaar0186e582021-01-10 18:33:11 +01001174
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001175 // Restore the previous frame.
Bram Moolenaar12d26532021-02-19 19:13:21 +01001176 ectx->ec_dfunc_idx = prev_dfunc_idx;
Bram Moolenaar0186e582021-01-10 18:33:11 +01001177 ectx->ec_iidx = STACK_TV(ectx->ec_frame_idx
1178 + STACK_FRAME_IIDX_OFF)->vval.v_number;
Bram Moolenaar5930ddc2021-04-26 20:32:59 +02001179 ectx->ec_instr = (void *)STACK_TV(ectx->ec_frame_idx
1180 + STACK_FRAME_INSTR_OFF)->vval.v_string;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001181 ectx->ec_outer_ref = (void *)STACK_TV(ectx->ec_frame_idx
Bram Moolenaar0186e582021-01-10 18:33:11 +01001182 + STACK_FRAME_OUTER_OFF)->vval.v_string;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001183 floc = (void *)STACK_TV(ectx->ec_frame_idx
1184 + STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string;
Bram Moolenaar5366e1a2020-10-01 13:01:34 +02001185 // restoring ec_frame_idx must be last
Bram Moolenaar0186e582021-01-10 18:33:11 +01001186 ectx->ec_frame_idx = STACK_TV(ectx->ec_frame_idx
1187 + STACK_FRAME_IDX_OFF)->vval.v_number;
Bram Moolenaard386e922021-04-25 14:48:49 +02001188
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001189 if (floc == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02001190 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001191 else
1192 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02001193 ectx->ec_funclocal = *floc;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001194 vim_free(floc);
1195 }
1196
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01001197 if (ret_idx > 0)
1198 {
1199 // Reset the stack to the position before the call, with a spot for the
1200 // return value, moved there from above the frame.
1201 ectx->ec_stack.ga_len = top + 1;
1202 *STACK_TV_BOT(-1) = *STACK_TV(ret_idx);
1203 }
1204 else
1205 // Reset the stack to the position before the call.
1206 ectx->ec_stack.ga_len = top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001207
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001208 funcdepth_decrement();
Bram Moolenaare99d4222021-06-13 14:01:26 +02001209 --ex_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001210 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001211}
1212
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001213/*
1214 * Prepare arguments and rettv for calling a builtin or user function.
1215 */
1216 static int
1217call_prepare(int argcount, typval_T *argvars, ectx_T *ectx)
1218{
1219 int idx;
1220 typval_T *tv;
1221
1222 // Move arguments from bottom of the stack to argvars[] and add terminator.
1223 for (idx = 0; idx < argcount; ++idx)
1224 argvars[idx] = *STACK_TV_BOT(idx - argcount);
1225 argvars[argcount].v_type = VAR_UNKNOWN;
1226
1227 // Result replaces the arguments on the stack.
1228 if (argcount > 0)
1229 ectx->ec_stack.ga_len -= argcount - 1;
Bram Moolenaar35578162021-08-02 19:10:38 +02001230 else if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001231 return FAIL;
1232 else
1233 ++ectx->ec_stack.ga_len;
1234
1235 // Default return value is zero.
1236 tv = STACK_TV_BOT(-1);
1237 tv->v_type = VAR_NUMBER;
1238 tv->vval.v_number = 0;
Bram Moolenaar24565cf2022-03-28 18:16:52 +01001239 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001240
1241 return OK;
1242}
1243
1244/*
1245 * Call a builtin function by index.
1246 */
1247 static int
1248call_bfunc(int func_idx, int argcount, ectx_T *ectx)
1249{
1250 typval_T argvars[MAX_FUNC_ARGS];
1251 int idx;
Bram Moolenaar171fb922020-10-28 16:54:47 +01001252 int did_emsg_before = did_emsg;
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001253 ectx_T *prev_ectx = current_ectx;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001254 char *save_func_name = ectx->ec_where.wt_func_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001255
1256 if (call_prepare(argcount, argvars, ectx) == FAIL)
1257 return FAIL;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001258 ectx->ec_where.wt_func_name = internal_func_name(func_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001259
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001260 // Call the builtin function. Set "current_ectx" so that when it
1261 // recursively invokes call_def_function() a closure context can be set.
1262 current_ectx = ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001263 call_internal_func_by_idx(func_idx, argvars, STACK_TV_BOT(-1));
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001264 current_ectx = prev_ectx;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001265 ectx->ec_where.wt_func_name = save_func_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001266
1267 // Clear the arguments.
1268 for (idx = 0; idx < argcount; ++idx)
1269 clear_tv(&argvars[idx]);
Bram Moolenaar015f4262020-05-05 21:25:22 +02001270
Bram Moolenaar57f799e2020-12-12 20:42:19 +01001271 if (did_emsg > did_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +02001272 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001273 return OK;
1274}
1275
1276/*
1277 * Execute a user defined function.
Bram Moolenaarab360522021-01-10 14:02:28 +01001278 * If the function is compiled this will add a stack frame and set the
1279 * instruction pointer at the start of the function.
1280 * Otherwise the function is called here.
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001281 * If "pt" is not null use "pt->pt_outer" for ec_outer_ref->or_outer.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001282 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001283 */
1284 static int
Bram Moolenaar0186e582021-01-10 18:33:11 +01001285call_ufunc(
1286 ufunc_T *ufunc,
1287 partial_T *pt,
1288 int argcount,
1289 ectx_T *ectx,
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001290 isn_T *iptr,
1291 dict_T *selfdict)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001292{
1293 typval_T argvars[MAX_FUNC_ARGS];
1294 funcexe_T funcexe;
Bram Moolenaar0917e862023-02-18 14:42:44 +00001295 funcerror_T error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001296 int idx;
Bram Moolenaar28ee8922020-10-28 20:20:00 +01001297 int did_emsg_before = did_emsg;
Bram Moolenaar139575d2022-03-15 19:29:30 +00001298 compiletype_T compile_type = get_compile_type(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001299
Bram Moolenaare99d4222021-06-13 14:01:26 +02001300 if (func_needs_compiling(ufunc, compile_type)
1301 && compile_def_function(ufunc, FALSE, compile_type, NULL)
1302 == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02001303 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001304 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001305 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +01001306 error = check_user_func_argcount(ufunc, argcount);
Bram Moolenaar50824712020-12-20 21:10:17 +01001307 if (error != FCERR_UNKNOWN)
1308 {
1309 if (error == FCERR_TOOMANY)
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001310 semsg(_(e_too_many_arguments_for_function_str),
1311 printable_func_name(ufunc));
Bram Moolenaar50824712020-12-20 21:10:17 +01001312 else
Bram Moolenaare1242042021-12-16 20:56:57 +00001313 semsg(_(e_not_enough_arguments_for_function_str),
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001314 printable_func_name(ufunc));
Bram Moolenaar50824712020-12-20 21:10:17 +01001315 return FAIL;
1316 }
1317
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001318 // The function has been compiled, can call it quickly. For a function
1319 // that was defined later: we can call it directly next time.
1320 if (iptr != NULL)
1321 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001322 delete_instr(iptr);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001323 iptr->isn_type = ISN_DCALL;
1324 iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1325 iptr->isn_arg.dfunc.cdf_argcount = argcount;
1326 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001327 return call_dfunc(ufunc->uf_dfunc_idx, pt, argcount, ectx);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001328 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001329
1330 if (call_prepare(argcount, argvars, ectx) == FAIL)
1331 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +02001332 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00001333 funcexe.fe_evaluate = TRUE;
1334 funcexe.fe_selfdict = selfdict != NULL ? selfdict : dict_stack_get_dict();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001335
1336 // Call the user function. Result goes in last position on the stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001337 error = call_user_func_check(ufunc, argcount, argvars,
Bram Moolenaar851f86b2021-12-13 14:26:44 +00001338 STACK_TV_BOT(-1), &funcexe, funcexe.fe_selfdict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001339
1340 // Clear the arguments.
1341 for (idx = 0; idx < argcount; ++idx)
1342 clear_tv(&argvars[idx]);
1343
1344 if (error != FCERR_NONE)
1345 {
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01001346 user_func_error(error, printable_func_name(ufunc),
1347 funcexe.fe_found_var);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001348 return FAIL;
1349 }
Bram Moolenaar28ee8922020-10-28 20:20:00 +01001350 if (did_emsg > did_emsg_before)
Bram Moolenaared677f52020-08-12 16:38:10 +02001351 // Error other than from calling the function itself.
1352 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001353 return OK;
1354}
1355
1356/*
Bram Moolenaara91a7132021-03-25 21:12:15 +01001357 * If command modifiers were applied restore them.
1358 */
1359 static void
1360may_restore_cmdmod(funclocal_T *funclocal)
1361{
1362 if (funclocal->floc_restore_cmdmod)
1363 {
1364 cmdmod.cmod_filter_regmatch.regprog = NULL;
1365 undo_cmdmod(&cmdmod);
1366 cmdmod = funclocal->floc_save_cmdmod;
1367 funclocal->floc_restore_cmdmod = FALSE;
1368 }
1369}
1370
1371/*
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001372 * Return TRUE if an error was given (not caught in try/catch) or CTRL-C was
1373 * pressed.
Bram Moolenaara1773442020-08-12 15:21:22 +02001374 */
1375 static int
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001376vim9_aborting(int prev_uncaught_emsg)
Bram Moolenaara1773442020-08-12 15:21:22 +02001377{
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001378 return uncaught_emsg > prev_uncaught_emsg || got_int || did_throw;
Bram Moolenaara1773442020-08-12 15:21:22 +02001379}
1380
1381/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001382 * Execute a function by "name".
1383 * This can be a builtin function or a user function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001384 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001385 * Returns FAIL if not found without an error message.
1386 */
1387 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001388call_by_name(
1389 char_u *name,
1390 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001391 ectx_T *ectx,
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001392 isn_T *iptr,
1393 dict_T *selfdict)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001394{
1395 ufunc_T *ufunc;
1396
1397 if (builtin_function(name, -1))
1398 {
1399 int func_idx = find_internal_func(name);
1400
Bram Moolenaar1983f1a2022-02-28 20:55:02 +00001401 if (func_idx < 0) // Impossible?
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001402 return FAIL;
Bram Moolenaar389df252020-07-09 21:20:47 +02001403 if (check_internal_func(func_idx, argcount) < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001404 return FAIL;
1405 return call_bfunc(func_idx, argcount, ectx);
1406 }
1407
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00001408 ufunc = find_func(name, FALSE);
Bram Moolenaara1773442020-08-12 15:21:22 +02001409
1410 if (ufunc == NULL)
1411 {
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001412 int prev_uncaught_emsg = uncaught_emsg;
Bram Moolenaara1773442020-08-12 15:21:22 +02001413
1414 if (script_autoload(name, TRUE))
1415 // loaded a package, search for the function again
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00001416 ufunc = find_func(name, FALSE);
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001417
1418 if (vim9_aborting(prev_uncaught_emsg))
Bram Moolenaara1773442020-08-12 15:21:22 +02001419 return FAIL; // bail out if loading the script caused an error
1420 }
1421
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001422 if (ufunc != NULL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001423 {
Bram Moolenaar0d89d8a2022-12-31 14:01:24 +00001424 if (check_ufunc_arg_types(ufunc, argcount, 0, ectx) == FAIL)
1425 return FAIL;
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001426
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001427 return call_ufunc(ufunc, NULL, argcount, ectx, iptr, selfdict);
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001428 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001429
1430 return FAIL;
1431}
1432
1433 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001434call_partial(
1435 typval_T *tv,
1436 int argcount_arg,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001437 ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001438{
Bram Moolenaara90afb92020-07-15 22:38:56 +02001439 int argcount = argcount_arg;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001440 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001441 int called_emsg_before = called_emsg;
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +01001442 int res = FAIL;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001443 dict_T *selfdict = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001444
1445 if (tv->v_type == VAR_PARTIAL)
1446 {
Bram Moolenaara90afb92020-07-15 22:38:56 +02001447 partial_T *pt = tv->vval.v_partial;
1448 int i;
1449
1450 if (pt->pt_argc > 0)
1451 {
1452 // Make space for arguments from the partial, shift the "argcount"
1453 // arguments up.
Bram Moolenaar35578162021-08-02 19:10:38 +02001454 if (GA_GROW_FAILS(&ectx->ec_stack, pt->pt_argc))
Bram Moolenaara90afb92020-07-15 22:38:56 +02001455 return FAIL;
1456 for (i = 1; i <= argcount; ++i)
1457 *STACK_TV_BOT(-i + pt->pt_argc) = *STACK_TV_BOT(-i);
1458 ectx->ec_stack.ga_len += pt->pt_argc;
1459 argcount += pt->pt_argc;
1460
1461 // copy the arguments from the partial onto the stack
1462 for (i = 0; i < pt->pt_argc; ++i)
1463 copy_tv(&pt->pt_argv[i], STACK_TV_BOT(-argcount + i));
1464 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001465 selfdict = pt->pt_dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001466
1467 if (pt->pt_func != NULL)
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001468 return call_ufunc(pt->pt_func, pt, argcount, ectx, NULL, selfdict);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001469
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001470 name = pt->pt_name;
1471 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001472 else if (tv->v_type == VAR_FUNC)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001473 name = tv->vval.v_string;
Bram Moolenaar95006e32020-08-29 17:47:08 +02001474 if (name != NULL)
1475 {
Bram Moolenaar0917e862023-02-18 14:42:44 +00001476 char_u fname_buf[FLEN_FIXED + 1];
1477 char_u *tofree = NULL;
1478 funcerror_T error = FCERR_NONE;
1479 char_u *fname;
Bram Moolenaar95006e32020-08-29 17:47:08 +02001480
1481 // May need to translate <SNR>123_ to K_SNR.
1482 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
1483 if (error != FCERR_NONE)
1484 res = FAIL;
1485 else
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001486 res = call_by_name(fname, argcount, ectx, NULL, selfdict);
Bram Moolenaar95006e32020-08-29 17:47:08 +02001487 vim_free(tofree);
1488 }
1489
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +01001490 if (res == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001491 {
1492 if (called_emsg == called_emsg_before)
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001493 emsg_funcname(e_unknown_function_str,
Bram Moolenaar015f4262020-05-05 21:25:22 +02001494 name == NULL ? (char_u *)"[unknown]" : name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001495 return FAIL;
1496 }
1497 return OK;
1498}
1499
1500/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001501 * Check if "lock" is VAR_LOCKED or VAR_FIXED. If so give an error and return
1502 * TRUE.
1503 */
1504 static int
1505error_if_locked(int lock, char *error)
1506{
1507 if (lock & (VAR_LOCKED | VAR_FIXED))
1508 {
1509 emsg(_(error));
1510 return TRUE;
1511 }
1512 return FALSE;
1513}
1514
1515/*
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001516 * Give an error if "tv" is not a number and return FAIL.
1517 */
1518 static int
1519check_for_number(typval_T *tv)
1520{
1521 if (tv->v_type != VAR_NUMBER)
1522 {
1523 semsg(_(e_expected_str_but_got_str),
1524 vartype_name(VAR_NUMBER), vartype_name(tv->v_type));
1525 return FAIL;
1526 }
1527 return OK;
1528}
1529
1530/*
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001531 * Store "tv" in variable "name".
1532 * This is for s: and g: variables.
1533 */
1534 static void
1535store_var(char_u *name, typval_T *tv)
1536{
1537 funccal_entry_T entry;
Bram Moolenaard877a572021-04-01 19:42:48 +02001538 int flags = ASSIGN_DECL;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001539
Bram Moolenaard877a572021-04-01 19:42:48 +02001540 if (tv->v_lock)
1541 flags |= ASSIGN_CONST;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001542 save_funccal(&entry);
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001543 set_var_const(name, 0, NULL, tv, FALSE, flags, 0);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001544 restore_funccal();
1545}
1546
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001547/*
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001548 * Convert "tv" to a string.
1549 * Return FAIL if not allowed.
1550 */
1551 static int
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001552do_2string(typval_T *tv, int is_2string_any, int tolerant)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001553{
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00001554 if (tv->v_type == VAR_STRING)
1555 return OK;
1556
1557 char_u *str;
1558
1559 if (is_2string_any)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001560 {
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00001561 switch (tv->v_type)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001562 {
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00001563 case VAR_SPECIAL:
1564 case VAR_BOOL:
1565 case VAR_NUMBER:
1566 case VAR_FLOAT:
1567 case VAR_BLOB: break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001568
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00001569 case VAR_LIST:
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001570 if (tolerant)
1571 {
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001572 char_u *s, *e, *p;
1573 garray_T ga;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001574
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001575 ga_init2(&ga, sizeof(char_u *), 1);
1576
1577 // Convert to NL separated items, then
1578 // escape the items and replace the NL with
1579 // a space.
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001580 str = typval2string(tv, TRUE);
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001581 if (str == NULL)
1582 return FAIL;
1583 s = str;
1584 while ((e = vim_strchr(s, '\n')) != NULL)
1585 {
1586 *e = NUL;
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +01001587 p = vim_strsave_fnameescape(s,
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00001588 VSE_NONE);
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001589 if (p != NULL)
1590 {
1591 ga_concat(&ga, p);
1592 ga_concat(&ga, (char_u *)" ");
1593 vim_free(p);
1594 }
1595 s = e + 1;
1596 }
1597 vim_free(str);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001598 clear_tv(tv);
1599 tv->v_type = VAR_STRING;
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001600 tv->vval.v_string = ga.ga_data;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001601 return OK;
1602 }
1603 // FALLTHROUGH
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00001604 default: to_string_error(tv->v_type);
1605 return FAIL;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001606 }
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001607 }
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00001608 str = typval_tostring(tv, TRUE);
1609 clear_tv(tv);
1610 tv->v_type = VAR_STRING;
1611 tv->vval.v_string = str;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001612 return OK;
1613}
1614
1615/*
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001616 * When the value of "sv" is a null list of dict, allocate it.
1617 */
1618 static void
Bram Moolenaar859cc212022-03-28 15:22:35 +01001619allocate_if_null(svar_T *sv)
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001620{
Bram Moolenaar859cc212022-03-28 15:22:35 +01001621 typval_T *tv = sv->sv_tv;
1622
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001623 switch (tv->v_type)
1624 {
1625 case VAR_LIST:
Bram Moolenaar859cc212022-03-28 15:22:35 +01001626 if (tv->vval.v_list == NULL && sv->sv_type != &t_list_empty)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001627 (void)rettv_list_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001628 break;
1629 case VAR_DICT:
Bram Moolenaar859cc212022-03-28 15:22:35 +01001630 if (tv->vval.v_dict == NULL && sv->sv_type != &t_dict_empty)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001631 (void)rettv_dict_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001632 break;
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02001633 case VAR_BLOB:
Bram Moolenaar859cc212022-03-28 15:22:35 +01001634 if (tv->vval.v_blob == NULL && sv->sv_type != &t_blob_null)
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02001635 (void)rettv_blob_alloc(tv);
1636 break;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001637 default:
1638 break;
1639 }
1640}
Bram Moolenaard3aac292020-04-19 14:32:17 +02001641
Bram Moolenaare7525c52021-01-09 13:20:37 +01001642/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001643 * Return the character "str[index]" where "index" is the character index,
1644 * including composing characters.
1645 * If "index" is out of range NULL is returned.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001646 */
1647 char_u *
1648char_from_string(char_u *str, varnumber_T index)
1649{
1650 size_t nbyte = 0;
1651 varnumber_T nchar = index;
1652 size_t slen;
1653
1654 if (str == NULL)
1655 return NULL;
1656 slen = STRLEN(str);
1657
Bram Moolenaarff871402021-03-26 13:34:05 +01001658 // Do the same as for a list: a negative index counts from the end.
1659 // Optimization to check the first byte to be below 0x80 (and no composing
1660 // character follows) makes this a lot faster.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001661 if (index < 0)
1662 {
1663 int clen = 0;
1664
1665 for (nbyte = 0; nbyte < slen; ++clen)
Bram Moolenaarff871402021-03-26 13:34:05 +01001666 {
1667 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1668 ++nbyte;
1669 else if (enc_utf8)
1670 nbyte += utfc_ptr2len(str + nbyte);
1671 else
1672 nbyte += mb_ptr2len(str + nbyte);
1673 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001674 nchar = clen + index;
1675 if (nchar < 0)
1676 // unlike list: index out of range results in empty string
1677 return NULL;
1678 }
1679
1680 for (nbyte = 0; nchar > 0 && nbyte < slen; --nchar)
Bram Moolenaarff871402021-03-26 13:34:05 +01001681 {
1682 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1683 ++nbyte;
1684 else if (enc_utf8)
1685 nbyte += utfc_ptr2len(str + nbyte);
1686 else
1687 nbyte += mb_ptr2len(str + nbyte);
1688 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001689 if (nbyte >= slen)
1690 return NULL;
Bram Moolenaar0289a092021-03-14 18:40:19 +01001691 return vim_strnsave(str + nbyte, mb_ptr2len(str + nbyte));
Bram Moolenaare7525c52021-01-09 13:20:37 +01001692}
1693
1694/*
1695 * Get the byte index for character index "idx" in string "str" with length
Bram Moolenaar0289a092021-03-14 18:40:19 +01001696 * "str_len". Composing characters are included.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001697 * If going over the end return "str_len".
1698 * If "idx" is negative count from the end, -1 is the last character.
1699 * When going over the start return -1.
1700 */
1701 static long
1702char_idx2byte(char_u *str, size_t str_len, varnumber_T idx)
1703{
1704 varnumber_T nchar = idx;
1705 size_t nbyte = 0;
1706
1707 if (nchar >= 0)
1708 {
1709 while (nchar > 0 && nbyte < str_len)
1710 {
Bram Moolenaar0289a092021-03-14 18:40:19 +01001711 nbyte += mb_ptr2len(str + nbyte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001712 --nchar;
1713 }
1714 }
1715 else
1716 {
1717 nbyte = str_len;
1718 while (nchar < 0 && nbyte > 0)
1719 {
1720 --nbyte;
1721 nbyte -= mb_head_off(str, str + nbyte);
1722 ++nchar;
1723 }
1724 if (nchar < 0)
1725 return -1;
1726 }
1727 return (long)nbyte;
1728}
1729
1730/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001731 * Return the slice "str[first : last]" using character indexes. Composing
1732 * characters are included.
Bram Moolenaar6601b622021-01-13 21:47:15 +01001733 * "exclusive" is TRUE for slice().
Bram Moolenaare7525c52021-01-09 13:20:37 +01001734 * Return NULL when the result is empty.
1735 */
1736 char_u *
Bram Moolenaar6601b622021-01-13 21:47:15 +01001737string_slice(char_u *str, varnumber_T first, varnumber_T last, int exclusive)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001738{
1739 long start_byte, end_byte;
1740 size_t slen;
1741
1742 if (str == NULL)
1743 return NULL;
1744 slen = STRLEN(str);
1745 start_byte = char_idx2byte(str, slen, first);
1746 if (start_byte < 0)
1747 start_byte = 0; // first index very negative: use zero
Bram Moolenaar6601b622021-01-13 21:47:15 +01001748 if ((last == -1 && !exclusive) || last == VARNUM_MAX)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001749 end_byte = (long)slen;
1750 else
1751 {
1752 end_byte = char_idx2byte(str, slen, last);
Bram Moolenaar6601b622021-01-13 21:47:15 +01001753 if (!exclusive && end_byte >= 0 && end_byte < (long)slen)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001754 // end index is inclusive
Bram Moolenaar0289a092021-03-14 18:40:19 +01001755 end_byte += mb_ptr2len(str + end_byte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001756 }
1757
1758 if (start_byte >= (long)slen || end_byte <= start_byte)
1759 return NULL;
1760 return vim_strnsave(str + start_byte, end_byte - start_byte);
1761}
1762
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001763/*
1764 * Get a script variable for ISN_STORESCRIPT and ISN_LOADSCRIPT.
1765 * When "dfunc_idx" is negative don't give an error.
1766 * Returns NULL for an error.
1767 */
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001768 static svar_T *
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001769get_script_svar(scriptref_T *sref, int dfunc_idx)
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001770{
1771 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001772 dfunc_T *dfunc = dfunc_idx < 0 ? NULL
1773 : ((dfunc_T *)def_functions.ga_data) + dfunc_idx;
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001774 svar_T *sv;
1775
1776 if (sref->sref_seq != si->sn_script_seq)
1777 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001778 // The script was reloaded after the function was compiled, the
1779 // script_idx may not be valid.
1780 if (dfunc != NULL)
1781 semsg(_(e_script_variable_invalid_after_reload_in_function_str),
1782 printable_func_name(dfunc->df_ufunc));
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001783 return NULL;
1784 }
1785 sv = ((svar_T *)si->sn_var_vals.ga_data) + sref->sref_idx;
Bram Moolenaar6de22962022-09-09 21:35:36 +01001786 if (sv->sv_name == NULL)
1787 {
1788 if (dfunc != NULL)
1789 emsg(_(e_script_variable_was_deleted));
1790 return NULL;
1791 }
Bram Moolenaar60dc8272021-07-29 22:48:54 +02001792 if (!equal_type(sv->sv_type, sref->sref_type, 0))
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001793 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001794 if (dfunc != NULL)
1795 emsg(_(e_script_variable_type_changed));
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001796 return NULL;
1797 }
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001798
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01001799 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0
1800 && sref->sref_sid != current_sctx.sc_sid)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001801 {
1802 if (dfunc != NULL)
1803 semsg(_(e_item_not_exported_in_script_str), sv->sv_name);
1804 return NULL;
1805 }
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001806 return sv;
1807}
1808
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001809/*
Bram Moolenaar20677332021-06-06 17:02:53 +02001810 * Function passed to do_cmdline() for splitting a script joined by NL
1811 * characters.
1812 */
1813 static char_u *
1814get_split_sourceline(
1815 int c UNUSED,
1816 void *cookie,
1817 int indent UNUSED,
1818 getline_opt_T options UNUSED)
1819{
1820 source_cookie_T *sp = (source_cookie_T *)cookie;
1821 char_u *p;
1822 char_u *line;
1823
Bram Moolenaar20677332021-06-06 17:02:53 +02001824 p = vim_strchr(sp->nextline, '\n');
1825 if (p == NULL)
1826 {
1827 line = vim_strsave(sp->nextline);
1828 sp->nextline += STRLEN(sp->nextline);
1829 }
1830 else
1831 {
1832 line = vim_strnsave(sp->nextline, p - sp->nextline);
1833 sp->nextline = p + 1;
1834 }
1835 return line;
1836}
1837
1838/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001839 * Execute a function by "name".
1840 * This can be a builtin function, user function or a funcref.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001841 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001842 */
1843 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001844call_eval_func(
1845 char_u *name,
1846 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001847 ectx_T *ectx,
1848 isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001849{
Bram Moolenaared677f52020-08-12 16:38:10 +02001850 int called_emsg_before = called_emsg;
1851 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001852
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001853 res = call_by_name(name, argcount, ectx, iptr, NULL);
Bram Moolenaared677f52020-08-12 16:38:10 +02001854 if (res == FAIL && called_emsg == called_emsg_before)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001855 {
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001856 dictitem_T *v;
1857
1858 v = find_var(name, NULL, FALSE);
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001859 if (v == NULL || (v->di_tv.v_type != VAR_PARTIAL
1860 && v->di_tv.v_type != VAR_FUNC))
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001861 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001862 emsg_funcname(e_unknown_function_str, name);
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001863 return FAIL;
1864 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001865 return call_partial(&v->di_tv, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001866 }
Bram Moolenaared677f52020-08-12 16:38:10 +02001867 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001868}
1869
1870/*
Bram Moolenaarf112f302020-12-20 17:47:52 +01001871 * When a function reference is used, fill a partial with the information
1872 * needed, especially when it is used as a closure.
1873 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001874 int
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001875fill_partial_and_closure(
Bram Moolenaarcc341812022-09-19 15:54:34 +01001876 partial_T *pt,
1877 ufunc_T *ufunc,
1878 loopvarinfo_T *lvi,
1879 ectx_T *ectx)
Bram Moolenaarf112f302020-12-20 17:47:52 +01001880{
1881 pt->pt_func = ufunc;
1882 pt->pt_refcount = 1;
1883
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001884 if (ufunc->uf_flags & FC_CLOSURE)
Bram Moolenaarf112f302020-12-20 17:47:52 +01001885 {
1886 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1887 + ectx->ec_dfunc_idx;
1888
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001889 // The closure may need to find arguments and local variables of the
1890 // current function in the stack.
Bram Moolenaar0186e582021-01-10 18:33:11 +01001891 pt->pt_outer.out_stack = &ectx->ec_stack;
1892 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001893 if (ectx->ec_outer_ref != NULL)
1894 {
1895 // The current context already has a context, link to that one.
1896 pt->pt_outer.out_up = ectx->ec_outer_ref->or_outer;
1897 if (ectx->ec_outer_ref->or_partial != NULL)
1898 {
1899 pt->pt_outer.out_up_partial = ectx->ec_outer_ref->or_partial;
1900 ++pt->pt_outer.out_up_partial->pt_refcount;
1901 }
1902 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01001903
Bram Moolenaarcc341812022-09-19 15:54:34 +01001904 if (lvi != NULL)
1905 {
1906 int depth;
1907
1908 // The closure may need to find variables defined inside a loop,
1909 // for every nested loop. A new reference is made every time,
1910 // ISN_ENDLOOP will check if they are actually used.
1911 for (depth = 0; depth < lvi->lvi_depth; ++depth)
1912 {
1913 pt->pt_outer.out_loop[depth].stack = &ectx->ec_stack;
1914 pt->pt_outer.out_loop[depth].var_idx = ectx->ec_frame_idx
1915 + STACK_FRAME_SIZE + lvi->lvi_loop[depth].var_idx;
1916 pt->pt_outer.out_loop[depth].var_count =
1917 lvi->lvi_loop[depth].var_count;
1918 }
Bram Moolenaar6d313be2022-09-22 16:36:25 +01001919 pt->pt_outer.out_loop_size = lvi->lvi_depth;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001920 }
Bram Moolenaar6d313be2022-09-22 16:36:25 +01001921 else
1922 pt->pt_outer.out_loop_size = 0;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001923
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001924 // If the function currently executing returns and the closure is still
1925 // being referenced, we need to make a copy of the context (arguments
1926 // and local variables) so that the closure can use it later.
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001927 // Store a reference to the partial so we can handle that.
Bram Moolenaar35578162021-08-02 19:10:38 +02001928 if (GA_GROW_FAILS(&ectx->ec_funcrefs, 1))
Bram Moolenaarf112f302020-12-20 17:47:52 +01001929 {
1930 vim_free(pt);
1931 return FAIL;
1932 }
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001933 // Extra variable keeps the count of closures created in the current
1934 // function call.
Bram Moolenaarf112f302020-12-20 17:47:52 +01001935 ++(((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx
1936 + STACK_FRAME_SIZE + dfunc->df_varcount)->vval.v_number;
1937
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001938 ((partial_T **)ectx->ec_funcrefs.ga_data)[ectx->ec_funcrefs.ga_len]
1939 = pt;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001940 ++pt->pt_refcount;
1941 ++ectx->ec_funcrefs.ga_len;
1942 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001943 ++ufunc->uf_refcount;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001944 return OK;
1945}
1946
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001947/*
1948 * Execute iptr->isn_arg.string as an Ex command.
1949 */
1950 static int
1951exec_command(isn_T *iptr)
1952{
1953 source_cookie_T cookie;
1954
1955 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00001956 // Pass getsourceline to get an error for a missing ":end" command.
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001957 CLEAR_FIELD(cookie);
1958 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1959 if (do_cmdline(iptr->isn_arg.string,
1960 getsourceline, &cookie,
1961 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED) == FAIL
1962 || did_emsg)
1963 return FAIL;
1964 return OK;
1965}
1966
Bram Moolenaar89445512022-04-14 12:58:23 +01001967/*
1968 * If script "sid" is not loaded yet then load it now.
1969 * Caller must make sure "sid" is a valid script ID.
1970 * "loaded" is set to TRUE if the script had to be loaded.
1971 * Returns FAIL if loading fails, OK if already loaded or loaded now.
1972 */
1973 int
1974may_load_script(int sid, int *loaded)
1975{
1976 scriptitem_T *si = SCRIPT_ITEM(sid);
1977
1978 if (si->sn_state == SN_STATE_NOT_LOADED)
1979 {
1980 *loaded = TRUE;
1981 if (do_source(si->sn_name, FALSE, DOSO_NONE, NULL) == FAIL)
1982 {
1983 semsg(_(e_cant_open_file_str), si->sn_name);
1984 return FAIL;
1985 }
1986 }
1987 return OK;
1988}
1989
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001990// used for v_instr of typval of VAR_INSTR
1991struct instr_S {
1992 ectx_T *instr_ectx;
1993 isn_T *instr_instr;
1994};
1995
Bram Moolenaar4c137212021-04-19 16:48:48 +02001996// used for substitute_instr
1997typedef struct subs_expr_S {
1998 ectx_T *subs_ectx;
1999 isn_T *subs_instr;
2000 int subs_status;
2001} subs_expr_T;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002002
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002003// Set when calling do_debug().
2004static ectx_T *debug_context = NULL;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02002005static int debug_var_count;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002006
2007/*
2008 * When debugging lookup "name" and return the typeval.
2009 * When not found return NULL.
2010 */
2011 typval_T *
2012lookup_debug_var(char_u *name)
2013{
2014 int idx;
2015 dfunc_T *dfunc;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02002016 ufunc_T *ufunc;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002017 ectx_T *ectx = debug_context;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02002018 int varargs_off;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002019
2020 if (ectx == NULL)
2021 return NULL;
2022 dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
2023
2024 // Go through the local variable names, from last to first.
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02002025 for (idx = debug_var_count - 1; idx >= 0; --idx)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002026 {
Bram Moolenaare406ff82022-03-10 20:47:43 +00002027 char_u *varname = ((char_u **)dfunc->df_var_names.ga_data)[idx];
2028
2029 // the variable name may be NULL when not available in this block
2030 if (varname != NULL && STRCMP(varname, name) == 0)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002031 return STACK_TV_VAR(idx);
2032 }
2033
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02002034 // Go through argument names.
2035 ufunc = dfunc->df_ufunc;
2036 varargs_off = ufunc->uf_va_name == NULL ? 0 : 1;
2037 for (idx = 0; idx < ufunc->uf_args.ga_len; ++idx)
2038 if (STRCMP(((char_u **)(ufunc->uf_args.ga_data))[idx], name) == 0)
2039 return STACK_TV(ectx->ec_frame_idx - ufunc->uf_args.ga_len
2040 - varargs_off + idx);
2041 if (ufunc->uf_va_name != NULL && STRCMP(ufunc->uf_va_name, name) == 0)
2042 return STACK_TV(ectx->ec_frame_idx - 1);
2043
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002044 return NULL;
2045}
2046
Bram Moolenaar26a44842021-09-02 18:49:06 +02002047/*
2048 * Return TRUE if there might be a breakpoint in "ufunc", which is when a
2049 * breakpoint was set in that function or when there is any expression.
2050 */
2051 int
2052may_break_in_function(ufunc_T *ufunc)
2053{
2054 return ufunc->uf_has_breakpoint || debug_has_expr_breakpoint();
2055}
2056
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002057 static void
2058handle_debug(isn_T *iptr, ectx_T *ectx)
2059{
2060 char_u *line;
2061 ufunc_T *ufunc = (((dfunc_T *)def_functions.ga_data)
2062 + ectx->ec_dfunc_idx)->df_ufunc;
2063 isn_T *ni;
2064 int end_lnum = iptr->isn_lnum;
2065 garray_T ga;
2066 int lnum;
2067
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02002068 if (ex_nesting_level > debug_break_level)
2069 {
2070 linenr_T breakpoint;
2071
Bram Moolenaar26a44842021-09-02 18:49:06 +02002072 if (!may_break_in_function(ufunc))
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02002073 return;
2074
2075 // check for the next breakpoint if needed
2076 breakpoint = dbg_find_breakpoint(FALSE, ufunc->uf_name,
Bram Moolenaar8cec9272021-06-23 20:20:53 +02002077 iptr->isn_arg.debug.dbg_break_lnum);
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02002078 if (breakpoint <= 0 || breakpoint > iptr->isn_lnum)
2079 return;
2080 }
2081
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002082 SOURCING_LNUM = iptr->isn_lnum;
2083 debug_context = ectx;
Bram Moolenaar8cec9272021-06-23 20:20:53 +02002084 debug_var_count = iptr->isn_arg.debug.dbg_var_names_len;
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002085
2086 for (ni = iptr + 1; ni->isn_type != ISN_FINISH; ++ni)
2087 if (ni->isn_type == ISN_DEBUG
2088 || ni->isn_type == ISN_RETURN
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002089 || ni->isn_type == ISN_RETURN_OBJECT
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002090 || ni->isn_type == ISN_RETURN_VOID)
2091 {
Bram Moolenaar112bed02021-11-23 22:16:34 +00002092 end_lnum = ni->isn_lnum + (ni->isn_type == ISN_DEBUG ? 0 : 1);
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002093 break;
2094 }
2095
2096 if (end_lnum > iptr->isn_lnum)
2097 {
2098 ga_init2(&ga, sizeof(char_u *), 10);
Bram Moolenaar310091d2021-12-23 21:14:37 +00002099 for (lnum = iptr->isn_lnum; lnum < end_lnum
2100 && lnum <= ufunc->uf_lines.ga_len; ++lnum)
Bram Moolenaar59b50c32021-06-17 22:27:48 +02002101 {
Bram Moolenaar303215d2021-07-07 20:10:43 +02002102 char_u *p = ((char_u **)ufunc->uf_lines.ga_data)[lnum - 1];
Bram Moolenaar59b50c32021-06-17 22:27:48 +02002103
Bram Moolenaar303215d2021-07-07 20:10:43 +02002104 if (p == NULL)
2105 continue; // left over from continuation line
2106 p = skipwhite(p);
Bram Moolenaar59b50c32021-06-17 22:27:48 +02002107 if (*p == '#')
2108 break;
Bram Moolenaar35578162021-08-02 19:10:38 +02002109 if (GA_GROW_OK(&ga, 1))
Bram Moolenaar59b50c32021-06-17 22:27:48 +02002110 ((char_u **)(ga.ga_data))[ga.ga_len++] = p;
2111 if (STRNCMP(p, "def ", 4) == 0)
2112 break;
2113 }
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002114 line = ga_concat_strings(&ga, " ");
2115 vim_free(ga.ga_data);
2116 }
2117 else
2118 line = ((char_u **)ufunc->uf_lines.ga_data)[iptr->isn_lnum - 1];
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002119
Dominique Pelle6e969552021-06-17 13:53:41 +02002120 do_debug(line == NULL ? (char_u *)"[empty]" : line);
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002121 debug_context = NULL;
2122
2123 if (end_lnum > iptr->isn_lnum)
2124 vim_free(line);
2125}
2126
Bram Moolenaar4c137212021-04-19 16:48:48 +02002127/*
Bram Moolenaar65b0d162022-12-13 18:43:22 +00002128 * Store a value in a list, dict, blob or object variable.
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002129 * Returns OK, FAIL or NOTDONE (uncatchable error).
2130 */
2131 static int
2132execute_storeindex(isn_T *iptr, ectx_T *ectx)
2133{
Bram Moolenaarf7d1c6e2023-01-16 20:47:57 +00002134 vartype_T dest_type = iptr->isn_arg.storeindex.si_vartype;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002135 typval_T *tv;
2136 typval_T *tv_idx = STACK_TV_BOT(-2);
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002137 long lidx = 0;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002138 typval_T *tv_dest = STACK_TV_BOT(-1);
2139 int status = OK;
2140
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002141 if (tv_idx->v_type == VAR_NUMBER)
2142 lidx = (long)tv_idx->vval.v_number;
2143
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002144 // Stack contains:
2145 // -3 value to be stored
2146 // -2 index
Yegappan Lakshmanan3775f772023-09-01 22:05:45 +02002147 // -1 dict, list, blob, object or class
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002148 tv = STACK_TV_BOT(-3);
2149 SOURCING_LNUM = iptr->isn_lnum;
Gianmaria Bajod7085a02023-08-31 18:15:26 +02002150
2151 // Make sure an object has been initialized
2152 if (dest_type == VAR_OBJECT && tv_dest->vval.v_object == NULL)
2153 {
2154 emsg(_(e_using_null_object));
2155 status = FAIL;
2156 }
2157 else if (dest_type == VAR_ANY)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002158 {
2159 dest_type = tv_dest->v_type;
2160 if (dest_type == VAR_DICT)
2161 status = do_2string(tv_idx, TRUE, FALSE);
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002162 else if (dest_type == VAR_OBJECT && tv_idx->v_type == VAR_STRING)
2163 {
2164 // Need to get the member index now that the class is known.
2165 object_T *obj = tv_dest->vval.v_object;
2166 class_T *cl = obj->obj_class;
2167 char_u *member = tv_idx->vval.v_string;
2168
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02002169 int m_idx;
2170 ocmember_T *m = object_member_lookup(cl, member, 0, &m_idx);
2171 if (m != NULL)
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002172 {
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02002173 if (*member == '_')
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002174 {
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +02002175 semsg(_(e_cannot_access_private_member_str), m->ocm_name);
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02002176 status = FAIL;
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002177 }
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002178
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02002179 lidx = m_idx;
2180 }
2181 else
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002182 {
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +02002183 member_not_found_msg(cl, VAR_OBJECT, member, 0);
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002184 status = FAIL;
2185 }
2186 }
2187 else if ((dest_type == VAR_LIST || dest_type == VAR_OBJECT)
2188 && tv_idx->v_type != VAR_NUMBER)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002189 {
2190 emsg(_(e_number_expected));
2191 status = FAIL;
2192 }
2193 }
Bram Moolenaare08be092022-02-17 13:08:26 +00002194
2195 if (status == OK)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002196 {
Bram Moolenaare08be092022-02-17 13:08:26 +00002197 if (dest_type == VAR_LIST)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002198 {
Bram Moolenaare08be092022-02-17 13:08:26 +00002199 list_T *list = tv_dest->vval.v_list;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002200
Bram Moolenaare08be092022-02-17 13:08:26 +00002201 if (list == NULL)
2202 {
2203 emsg(_(e_list_not_set));
2204 return FAIL;
2205 }
2206 if (lidx < 0 && list->lv_len + lidx >= 0)
2207 // negative index is relative to the end
2208 lidx = list->lv_len + lidx;
2209 if (lidx < 0 || lidx > list->lv_len)
2210 {
2211 semsg(_(e_list_index_out_of_range_nr), lidx);
2212 return FAIL;
2213 }
2214 if (lidx < list->lv_len)
2215 {
2216 listitem_T *li = list_find(list, lidx);
2217
2218 if (error_if_locked(li->li_tv.v_lock,
Bram Moolenaar70c43d82022-01-26 21:01:15 +00002219 e_cannot_change_locked_list_item))
Bram Moolenaare08be092022-02-17 13:08:26 +00002220 return FAIL;
2221 // overwrite existing list item
2222 clear_tv(&li->li_tv);
2223 li->li_tv = *tv;
2224 }
2225 else
2226 {
2227 if (error_if_locked(list->lv_lock, e_cannot_change_locked_list))
2228 return FAIL;
2229 // append to list, only fails when out of memory
2230 if (list_append_tv(list, tv) == FAIL)
2231 return NOTDONE;
2232 clear_tv(tv);
2233 }
2234 }
2235 else if (dest_type == VAR_DICT)
2236 {
2237 char_u *key = tv_idx->vval.v_string;
2238 dict_T *dict = tv_dest->vval.v_dict;
2239 dictitem_T *di;
2240
2241 SOURCING_LNUM = iptr->isn_lnum;
2242 if (dict == NULL)
2243 {
2244 emsg(_(e_dictionary_not_set));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002245 return FAIL;
Bram Moolenaare08be092022-02-17 13:08:26 +00002246 }
2247 if (key == NULL)
2248 key = (char_u *)"";
2249 di = dict_find(dict, key, -1);
2250 if (di != NULL)
2251 {
2252 if (error_if_locked(di->di_tv.v_lock,
2253 e_cannot_change_dict_item))
2254 return FAIL;
2255 // overwrite existing value
2256 clear_tv(&di->di_tv);
2257 di->di_tv = *tv;
2258 }
2259 else
2260 {
2261 if (error_if_locked(dict->dv_lock, e_cannot_change_dict))
2262 return FAIL;
2263 // add to dict, only fails when out of memory
2264 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
2265 return NOTDONE;
2266 clear_tv(tv);
2267 }
2268 }
2269 else if (dest_type == VAR_BLOB)
2270 {
Bram Moolenaare08be092022-02-17 13:08:26 +00002271 blob_T *blob = tv_dest->vval.v_blob;
Bram Moolenaar65b0d162022-12-13 18:43:22 +00002272 varnumber_T nr;
2273 int error = FALSE;
2274 int len;
Bram Moolenaare08be092022-02-17 13:08:26 +00002275
2276 if (blob == NULL)
2277 {
2278 emsg(_(e_blob_not_set));
2279 return FAIL;
2280 }
2281 len = blob_len(blob);
2282 if (lidx < 0 && len + lidx >= 0)
2283 // negative index is relative to the end
2284 lidx = len + lidx;
2285
2286 // Can add one byte at the end.
2287 if (lidx < 0 || lidx > len)
2288 {
2289 semsg(_(e_blob_index_out_of_range_nr), lidx);
2290 return FAIL;
2291 }
2292 if (value_check_lock(blob->bv_lock, (char_u *)"blob", FALSE))
2293 return FAIL;
2294 nr = tv_get_number_chk(tv, &error);
2295 if (error)
2296 return FAIL;
2297 blob_set_append(blob, lidx, nr);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002298 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002299 else if (dest_type == VAR_CLASS || dest_type == VAR_OBJECT)
2300 {
Yegappan Lakshmanan3775f772023-09-01 22:05:45 +02002301 typval_T *otv;
Bram Moolenaarf7d1c6e2023-01-16 20:47:57 +00002302
Yegappan Lakshmanan3775f772023-09-01 22:05:45 +02002303 if (dest_type == VAR_OBJECT)
2304 {
2305 object_T *obj = tv_dest->vval.v_object;
2306
2307 otv = (typval_T *)(obj + 1);
2308 class_T *itf = iptr->isn_arg.storeindex.si_class;
2309 if (itf != NULL)
2310 // convert interface member index to class member index
Ernie Rael18143d32023-09-04 22:30:41 +02002311 lidx = object_index_from_itf_index(itf, FALSE, lidx,
2312 obj->obj_class, FALSE);
Yegappan Lakshmanan3775f772023-09-01 22:05:45 +02002313 }
2314 else
2315 {
2316 // VAR_CLASS
2317 class_T *class = tv_dest->vval.v_class;
2318 otv = class->class_members_tv;
2319 }
Bram Moolenaarf7d1c6e2023-01-16 20:47:57 +00002320
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002321 clear_tv(&otv[lidx]);
2322 otv[lidx] = *tv;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002323 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002324 else
2325 {
Bram Moolenaare08be092022-02-17 13:08:26 +00002326 status = FAIL;
2327 semsg(_(e_cannot_index_str), vartype_name(dest_type));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002328 }
2329 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002330
2331 clear_tv(tv_idx);
2332 clear_tv(tv_dest);
2333 ectx->ec_stack.ga_len -= 3;
2334 if (status == FAIL)
2335 {
2336 clear_tv(tv);
2337 return FAIL;
2338 }
2339 return OK;
2340}
2341
2342/*
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002343 * Store a value in a list or blob range.
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002344 */
2345 static int
2346execute_storerange(isn_T *iptr, ectx_T *ectx)
2347{
2348 typval_T *tv;
2349 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2350 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2351 typval_T *tv_dest = STACK_TV_BOT(-1);
2352 int status = OK;
2353
2354 // Stack contains:
2355 // -4 value to be stored
2356 // -3 first index or "none"
2357 // -2 second index or "none"
2358 // -1 destination list or blob
2359 tv = STACK_TV_BOT(-4);
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002360 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002361 if (tv_dest->v_type == VAR_LIST)
2362 {
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002363 long n1;
2364 long n2;
2365 listitem_T *li1;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002366
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002367 n1 = (long)tv_get_number_chk(tv_idx1, NULL);
2368 if (tv_idx2->v_type == VAR_SPECIAL
2369 && tv_idx2->vval.v_number == VVAL_NONE)
2370 n2 = list_len(tv_dest->vval.v_list) - 1;
2371 else
2372 n2 = (long)tv_get_number_chk(tv_idx2, NULL);
2373
Bram Moolenaar22ebd172022-04-01 15:26:58 +01002374 li1 = check_range_index_one(tv_dest->vval.v_list, &n1, TRUE, FALSE);
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002375 if (li1 == NULL)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002376 status = FAIL;
2377 else
2378 {
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002379 status = check_range_index_two(tv_dest->vval.v_list,
2380 &n1, li1, &n2, FALSE);
2381 if (status != FAIL)
2382 status = list_assign_range(
2383 tv_dest->vval.v_list,
2384 tv->vval.v_list,
2385 n1,
2386 n2,
2387 tv_idx2->v_type == VAR_SPECIAL,
2388 (char_u *)"=",
2389 (char_u *)"[unknown]");
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002390 }
2391 }
2392 else if (tv_dest->v_type == VAR_BLOB)
2393 {
2394 varnumber_T n1;
2395 varnumber_T n2;
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002396 long bloblen;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002397
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002398 n1 = tv_get_number_chk(tv_idx1, NULL);
2399 if (tv_idx2->v_type == VAR_SPECIAL
2400 && tv_idx2->vval.v_number == VVAL_NONE)
2401 n2 = blob_len(tv_dest->vval.v_blob) - 1;
2402 else
2403 n2 = tv_get_number_chk(tv_idx2, NULL);
2404 bloblen = blob_len(tv_dest->vval.v_blob);
2405
2406 if (check_blob_index(bloblen, n1, FALSE) == FAIL
2407 || check_blob_range(bloblen, n1, n2, FALSE) == FAIL)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002408 status = FAIL;
2409 else
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002410 status = blob_set_range(tv_dest->vval.v_blob, n1, n2, tv);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002411 }
2412 else
2413 {
2414 status = FAIL;
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002415 emsg(_(e_list_or_blob_required));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002416 }
2417
2418 clear_tv(tv_idx1);
2419 clear_tv(tv_idx2);
2420 clear_tv(tv_dest);
2421 ectx->ec_stack.ga_len -= 4;
2422 clear_tv(tv);
2423
2424 return status;
2425}
2426
2427/*
2428 * Unlet item in list or dict variable.
2429 */
2430 static int
2431execute_unletindex(isn_T *iptr, ectx_T *ectx)
2432{
2433 typval_T *tv_idx = STACK_TV_BOT(-2);
2434 typval_T *tv_dest = STACK_TV_BOT(-1);
2435 int status = OK;
2436
2437 // Stack contains:
2438 // -2 index
2439 // -1 dict or list
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002440 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002441 if (tv_dest->v_type == VAR_DICT)
2442 {
2443 // unlet a dict item, index must be a string
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002444 if (tv_idx->v_type != VAR_STRING && tv_idx->v_type != VAR_NUMBER)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002445 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002446 semsg(_(e_expected_str_but_got_str),
2447 vartype_name(VAR_STRING),
2448 vartype_name(tv_idx->v_type));
2449 status = FAIL;
2450 }
2451 else
2452 {
2453 dict_T *d = tv_dest->vval.v_dict;
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002454 char_u *key;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002455 dictitem_T *di = NULL;
2456
2457 if (d != NULL && value_check_lock(
2458 d->dv_lock, NULL, FALSE))
2459 status = FAIL;
2460 else
2461 {
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002462 if (tv_idx->v_type == VAR_STRING)
2463 {
2464 key = tv_idx->vval.v_string;
2465 if (key == NULL)
2466 key = (char_u *)"";
2467 }
2468 else
2469 {
2470 key = tv_get_string(tv_idx);
2471 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002472 if (d != NULL)
2473 di = dict_find(d, key, (int)STRLEN(key));
2474 if (di == NULL)
2475 {
2476 // NULL dict is equivalent to empty dict
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00002477 semsg(_(e_key_not_present_in_dictionary_str), key);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002478 status = FAIL;
2479 }
2480 else if (var_check_fixed(di->di_flags,
2481 NULL, FALSE)
2482 || var_check_ro(di->di_flags,
2483 NULL, FALSE))
2484 status = FAIL;
2485 else
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002486 dictitem_remove(d, di, "unlet");
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002487 }
2488 }
2489 }
2490 else if (tv_dest->v_type == VAR_LIST)
2491 {
2492 // unlet a List item, index must be a number
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002493 if (check_for_number(tv_idx) == FAIL)
2494 {
2495 status = FAIL;
2496 }
2497 else
2498 {
2499 list_T *l = tv_dest->vval.v_list;
2500 long n = (long)tv_idx->vval.v_number;
2501
2502 if (l != NULL && value_check_lock(
2503 l->lv_lock, NULL, FALSE))
2504 status = FAIL;
2505 else
2506 {
2507 listitem_T *li = list_find(l, n);
2508
2509 if (li == NULL)
2510 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002511 semsg(_(e_list_index_out_of_range_nr), n);
2512 status = FAIL;
2513 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002514 else
2515 listitem_remove(l, li);
2516 }
2517 }
2518 }
2519 else
2520 {
2521 status = FAIL;
2522 semsg(_(e_cannot_index_str),
2523 vartype_name(tv_dest->v_type));
2524 }
2525
2526 clear_tv(tv_idx);
2527 clear_tv(tv_dest);
2528 ectx->ec_stack.ga_len -= 2;
2529
2530 return status;
2531}
2532
2533/*
2534 * Unlet a range of items in a list variable.
2535 */
2536 static int
2537execute_unletrange(isn_T *iptr, ectx_T *ectx)
2538{
2539 // Stack contains:
2540 // -3 index1
2541 // -2 index2
2542 // -1 dict or list
2543 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2544 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2545 typval_T *tv_dest = STACK_TV_BOT(-1);
2546 int status = OK;
2547
2548 if (tv_dest->v_type == VAR_LIST)
2549 {
2550 // indexes must be a number
2551 SOURCING_LNUM = iptr->isn_lnum;
2552 if (check_for_number(tv_idx1) == FAIL
2553 || (tv_idx2->v_type != VAR_SPECIAL
2554 && check_for_number(tv_idx2) == FAIL))
2555 {
2556 status = FAIL;
2557 }
2558 else
2559 {
2560 list_T *l = tv_dest->vval.v_list;
2561 long n1 = (long)tv_idx1->vval.v_number;
2562 long n2 = tv_idx2->v_type == VAR_SPECIAL
2563 ? 0 : (long)tv_idx2->vval.v_number;
2564 listitem_T *li;
2565
2566 li = list_find_index(l, &n1);
2567 if (li == NULL)
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002568 {
2569 semsg(_(e_list_index_out_of_range_nr),
2570 (long)tv_idx1->vval.v_number);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002571 status = FAIL;
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002572 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002573 else
2574 {
2575 if (n1 < 0)
2576 n1 = list_idx_of_item(l, li);
2577 if (n2 < 0)
2578 {
2579 listitem_T *li2 = list_find(l, n2);
2580
2581 if (li2 == NULL)
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002582 {
2583 semsg(_(e_list_index_out_of_range_nr), n2);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002584 status = FAIL;
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002585 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002586 else
2587 n2 = list_idx_of_item(l, li2);
2588 }
2589 if (status != FAIL
2590 && tv_idx2->v_type != VAR_SPECIAL
2591 && n2 < n1)
2592 {
2593 semsg(_(e_list_index_out_of_range_nr), n2);
2594 status = FAIL;
2595 }
Bram Moolenaar6b8c7ba2022-03-20 17:46:06 +00002596 if (status != FAIL)
2597 list_unlet_range(l, li, n1,
2598 tv_idx2->v_type != VAR_SPECIAL, n2);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002599 }
2600 }
2601 }
2602 else
2603 {
2604 status = FAIL;
2605 SOURCING_LNUM = iptr->isn_lnum;
2606 semsg(_(e_cannot_index_str),
2607 vartype_name(tv_dest->v_type));
2608 }
2609
2610 clear_tv(tv_idx1);
2611 clear_tv(tv_idx2);
2612 clear_tv(tv_dest);
2613 ectx->ec_stack.ga_len -= 3;
2614
2615 return status;
2616}
2617
2618/*
2619 * Top of a for loop.
2620 */
2621 static int
2622execute_for(isn_T *iptr, ectx_T *ectx)
2623{
2624 typval_T *tv;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002625 int jump = FALSE;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002626 typval_T *ltv = STACK_TV_BOT(-1);
2627 typval_T *idxtv =
Bram Moolenaarcc341812022-09-19 15:54:34 +01002628 STACK_TV_VAR(iptr->isn_arg.forloop.for_loop_idx);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002629
2630 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
2631 return FAIL;
2632 if (ltv->v_type == VAR_LIST)
2633 {
2634 list_T *list = ltv->vval.v_list;
2635
2636 // push the next item from the list
2637 ++idxtv->vval.v_number;
2638 if (list == NULL
2639 || idxtv->vval.v_number >= list->lv_len)
2640 {
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002641 jump = TRUE;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002642 }
2643 else if (list->lv_first == &range_list_item)
2644 {
2645 // non-materialized range() list
2646 tv = STACK_TV_BOT(0);
2647 tv->v_type = VAR_NUMBER;
2648 tv->v_lock = 0;
2649 tv->vval.v_number = list_find_nr(
2650 list, idxtv->vval.v_number, NULL);
2651 ++ectx->ec_stack.ga_len;
2652 }
2653 else
2654 {
2655 listitem_T *li = list_find(list,
2656 idxtv->vval.v_number);
2657
2658 copy_tv(&li->li_tv, STACK_TV_BOT(0));
2659 ++ectx->ec_stack.ga_len;
2660 }
2661 }
2662 else if (ltv->v_type == VAR_STRING)
2663 {
2664 char_u *str = ltv->vval.v_string;
2665
2666 // The index is for the last byte of the previous
2667 // character.
2668 ++idxtv->vval.v_number;
2669 if (str == NULL || str[idxtv->vval.v_number] == NUL)
2670 {
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002671 jump = TRUE;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002672 }
2673 else
2674 {
2675 int clen = mb_ptr2len(str + idxtv->vval.v_number);
2676
2677 // Push the next character from the string.
2678 tv = STACK_TV_BOT(0);
2679 tv->v_type = VAR_STRING;
2680 tv->vval.v_string = vim_strnsave(
2681 str + idxtv->vval.v_number, clen);
2682 ++ectx->ec_stack.ga_len;
2683 idxtv->vval.v_number += clen - 1;
2684 }
2685 }
2686 else if (ltv->v_type == VAR_BLOB)
2687 {
2688 blob_T *blob = ltv->vval.v_blob;
2689
2690 // When we get here the first time make a copy of the
2691 // blob, so that the iteration still works when it is
2692 // changed.
2693 if (idxtv->vval.v_number == -1 && blob != NULL)
2694 {
2695 blob_copy(blob, ltv);
2696 blob_unref(blob);
2697 blob = ltv->vval.v_blob;
2698 }
2699
2700 // The index is for the previous byte.
2701 ++idxtv->vval.v_number;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002702 if (blob == NULL || idxtv->vval.v_number >= blob_len(blob))
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002703 {
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002704 jump = TRUE;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002705 }
2706 else
2707 {
2708 // Push the next byte from the blob.
2709 tv = STACK_TV_BOT(0);
2710 tv->v_type = VAR_NUMBER;
2711 tv->vval.v_number = blob_get(blob,
2712 idxtv->vval.v_number);
2713 ++ectx->ec_stack.ga_len;
2714 }
2715 }
2716 else
2717 {
2718 semsg(_(e_for_loop_on_str_not_supported),
2719 vartype_name(ltv->v_type));
2720 return FAIL;
2721 }
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002722
2723 if (jump)
2724 {
2725 // past the end of the list/string/blob, jump to "endfor"
2726 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
2727 may_restore_cmdmod(&ectx->ec_funclocal);
2728 }
2729 else
2730 {
2731 // Store the current number of funcrefs, this may be used in
2732 // ISN_LOOPEND. The variable index is always one more than the loop
2733 // variable index.
Bram Moolenaarcc341812022-09-19 15:54:34 +01002734 tv = STACK_TV_VAR(iptr->isn_arg.forloop.for_loop_idx + 1);
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002735 tv->vval.v_number = ectx->ec_funcrefs.ga_len;
2736 }
2737
2738 return OK;
2739}
2740
2741/*
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002742 * Code for handling variables declared inside a loop and used in a closure.
2743 * This is very similar to what is done with funcstack_T. The difference is
2744 * that the funcstack_T has the scope of a function, while a loopvars_T has the
2745 * scope of the block inside a loop and each loop may have its own.
2746 */
2747
2748// Double linked list of loopvars_T in use.
2749static loopvars_T *first_loopvars = NULL;
2750
2751 static void
2752add_loopvars_to_list(loopvars_T *loopvars)
2753{
2754 // Link in list of loopvarss.
2755 if (first_loopvars != NULL)
2756 first_loopvars->lvs_prev = loopvars;
2757 loopvars->lvs_next = first_loopvars;
2758 loopvars->lvs_prev = NULL;
2759 first_loopvars = loopvars;
2760}
2761
2762 static void
2763remove_loopvars_from_list(loopvars_T *loopvars)
2764{
2765 if (loopvars->lvs_prev == NULL)
2766 first_loopvars = loopvars->lvs_next;
2767 else
2768 loopvars->lvs_prev->lvs_next = loopvars->lvs_next;
2769 if (loopvars->lvs_next != NULL)
2770 loopvars->lvs_next->lvs_prev = loopvars->lvs_prev;
2771}
2772
2773/*
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002774 * End of a for or while loop: Handle any variables used by a closure.
2775 */
2776 static int
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002777execute_endloop(isn_T *iptr, ectx_T *ectx)
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002778{
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002779 endloop_T *endloop = &iptr->isn_arg.endloop;
2780 typval_T *tv_refcount = STACK_TV_VAR(endloop->end_funcref_idx);
2781 int prev_closure_count = tv_refcount->vval.v_number;
Bram Moolenaarcc341812022-09-19 15:54:34 +01002782 int depth = endloop->end_depth;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002783 garray_T *gap = &ectx->ec_funcrefs;
2784 int closure_in_use = FALSE;
2785 loopvars_T *loopvars;
2786 typval_T *stack;
2787 int idx;
2788
Bram Moolenaarcc341812022-09-19 15:54:34 +01002789 // Check if any created closure is still being referenced and loopvars have
2790 // not been saved yet for the current depth.
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002791 for (idx = prev_closure_count; idx < gap->ga_len; ++idx)
2792 {
2793 partial_T *pt = ((partial_T **)gap->ga_data)[idx];
2794
Bram Moolenaarcc341812022-09-19 15:54:34 +01002795 if (pt->pt_refcount > 1 && pt->pt_loopvars[depth] == NULL)
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002796 {
2797 int refcount = pt->pt_refcount;
2798 int i;
2799
2800 // A Reference in a variable inside the loop doesn't count, it gets
2801 // unreferenced at the end of the loop.
2802 for (i = 0; i < endloop->end_var_count; ++i)
2803 {
2804 typval_T *stv = STACK_TV_VAR(endloop->end_var_idx + i);
2805
2806 if (stv->v_type == VAR_PARTIAL && pt == stv->vval.v_partial)
2807 --refcount;
2808 }
2809 if (refcount > 1)
2810 {
2811 closure_in_use = TRUE;
2812 break;
2813 }
2814 }
2815 }
2816
2817 // If no function reference were created since the start of the loop block
2818 // or it is no longer referenced there is nothing to do.
2819 if (!closure_in_use)
2820 return OK;
2821
2822 // A closure is using variables declared inside the loop.
2823 // Move them to the called function.
2824 loopvars = ALLOC_CLEAR_ONE(loopvars_T);
2825 if (loopvars == NULL)
2826 return FAIL;
2827
2828 loopvars->lvs_ga.ga_len = endloop->end_var_count;
2829 stack = ALLOC_CLEAR_MULT(typval_T, loopvars->lvs_ga.ga_len);
2830 loopvars->lvs_ga.ga_data = stack;
2831 if (stack == NULL)
2832 {
2833 vim_free(loopvars);
2834 return FAIL;
2835 }
2836 add_loopvars_to_list(loopvars);
2837
2838 // Move the variable values.
2839 for (idx = 0; idx < endloop->end_var_count; ++idx)
2840 {
2841 typval_T *tv = STACK_TV_VAR(endloop->end_var_idx + idx);
2842
2843 *(stack + idx) = *tv;
2844 tv->v_type = VAR_UNKNOWN;
2845 }
2846
2847 for (idx = prev_closure_count; idx < gap->ga_len; ++idx)
2848 {
2849 partial_T *pt = ((partial_T **)gap->ga_data)[idx];
2850
Bram Moolenaarcc341812022-09-19 15:54:34 +01002851 if (pt->pt_refcount > 1 && pt->pt_loopvars[depth] == NULL)
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002852 {
2853 ++loopvars->lvs_refcount;
Bram Moolenaarcc341812022-09-19 15:54:34 +01002854 pt->pt_loopvars[depth] = loopvars;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002855
Bram Moolenaarcc341812022-09-19 15:54:34 +01002856 pt->pt_outer.out_loop[depth].stack = &loopvars->lvs_ga;
2857 pt->pt_outer.out_loop[depth].var_idx -=
2858 ectx->ec_frame_idx + STACK_FRAME_SIZE + endloop->end_var_idx;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002859 }
2860 }
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002861
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002862 return OK;
2863}
2864
2865/*
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002866 * Called when a partial is freed or its reference count goes down to one. The
2867 * loopvars may be the only reference to the partials in the local variables.
2868 * Go over all of them, the funcref and can be freed if all partials
2869 * referencing the loopvars have a reference count of one.
Bram Moolenaarcc341812022-09-19 15:54:34 +01002870 * Return TRUE if it was freed.
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002871 */
Bram Moolenaarcc341812022-09-19 15:54:34 +01002872 int
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002873loopvars_check_refcount(loopvars_T *loopvars)
2874{
2875 int i;
2876 garray_T *gap = &loopvars->lvs_ga;
2877 int done = 0;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002878 typval_T *stack = gap->ga_data;
2879
Bram Moolenaarcc341812022-09-19 15:54:34 +01002880 if (loopvars->lvs_refcount > loopvars->lvs_min_refcount)
2881 return FALSE;
2882 for (i = 0; i < gap->ga_len; ++i)
2883 {
2884 typval_T *tv = ((typval_T *)gap->ga_data) + i;
2885
2886 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL
2887 && tv->vval.v_partial->pt_refcount == 1)
2888 {
2889 int depth;
2890
2891 for (depth = 0; depth < MAX_LOOP_DEPTH; ++depth)
2892 if (tv->vval.v_partial->pt_loopvars[depth] == loopvars)
2893 ++done;
2894 }
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002895 }
Bram Moolenaarcc341812022-09-19 15:54:34 +01002896 if (done != loopvars->lvs_min_refcount)
2897 return FALSE;
2898
2899 // All partials referencing the loopvars have a reference count of
2900 // one, thus the loopvars is no longer of use.
2901 stack = gap->ga_data;
2902 for (i = 0; i < gap->ga_len; ++i)
2903 clear_tv(stack + i);
2904 vim_free(stack);
2905 remove_loopvars_from_list(loopvars);
2906 vim_free(loopvars);
2907 return TRUE;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002908}
2909
2910/*
2911 * For garbage collecting: set references in all variables referenced by
2912 * all loopvars.
2913 */
2914 int
2915set_ref_in_loopvars(int copyID)
2916{
2917 loopvars_T *loopvars;
2918
2919 for (loopvars = first_loopvars; loopvars != NULL;
2920 loopvars = loopvars->lvs_next)
2921 {
2922 typval_T *stack = loopvars->lvs_ga.ga_data;
2923 int i;
2924
2925 for (i = 0; i < loopvars->lvs_ga.ga_len; ++i)
2926 if (set_ref_in_item(stack + i, copyID, NULL, NULL))
2927 return TRUE; // abort
2928 }
2929 return FALSE;
2930}
2931
2932/*
Bram Moolenaar06b77222022-01-25 15:51:56 +00002933 * Load instruction for w:/b:/g:/t: variable.
2934 * "isn_type" is used instead of "iptr->isn_type".
2935 */
2936 static int
2937load_namespace_var(ectx_T *ectx, isntype_T isn_type, isn_T *iptr)
2938{
2939 dictitem_T *di = NULL;
2940 hashtab_T *ht = NULL;
2941 char namespace;
2942
2943 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
2944 return NOTDONE;
2945 switch (isn_type)
2946 {
2947 case ISN_LOADG:
2948 ht = get_globvar_ht();
2949 namespace = 'g';
2950 break;
2951 case ISN_LOADB:
2952 ht = &curbuf->b_vars->dv_hashtab;
2953 namespace = 'b';
2954 break;
2955 case ISN_LOADW:
2956 ht = &curwin->w_vars->dv_hashtab;
2957 namespace = 'w';
2958 break;
2959 case ISN_LOADT:
2960 ht = &curtab->tp_vars->dv_hashtab;
2961 namespace = 't';
2962 break;
2963 default: // Cannot reach here
2964 return NOTDONE;
2965 }
2966 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
2967
Bram Moolenaar06b77222022-01-25 15:51:56 +00002968 if (di == NULL)
2969 {
Bram Moolenaarfe732552022-02-22 19:39:13 +00002970 if (isn_type == ISN_LOADG)
2971 {
2972 ufunc_T *ufunc = find_func(iptr->isn_arg.string, TRUE);
2973
2974 // g:Something could be a function
2975 if (ufunc != NULL)
2976 {
2977 typval_T *tv = STACK_TV_BOT(0);
2978
2979 ++ectx->ec_stack.ga_len;
2980 tv->v_type = VAR_FUNC;
2981 tv->vval.v_string = alloc(STRLEN(iptr->isn_arg.string) + 3);
2982 if (tv->vval.v_string == NULL)
2983 return FAIL;
2984 STRCPY(tv->vval.v_string, "g:");
2985 STRCPY(tv->vval.v_string + 2, iptr->isn_arg.string);
2986 return OK;
2987 }
2988 }
Bram Moolenaar06b77222022-01-25 15:51:56 +00002989 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarfe732552022-02-22 19:39:13 +00002990 if (vim_strchr(iptr->isn_arg.string, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar06b77222022-01-25 15:51:56 +00002991 // no check if the item exists in the script but
2992 // isn't exported, it is too complicated
Bram Moolenaarfe732552022-02-22 19:39:13 +00002993 semsg(_(e_item_not_found_in_script_str), iptr->isn_arg.string);
Bram Moolenaar06b77222022-01-25 15:51:56 +00002994 else
2995 semsg(_(e_undefined_variable_char_str),
Bram Moolenaarfe732552022-02-22 19:39:13 +00002996 namespace, iptr->isn_arg.string);
Bram Moolenaar06b77222022-01-25 15:51:56 +00002997 return FAIL;
2998 }
2999 else
3000 {
3001 copy_tv(&di->di_tv, STACK_TV_BOT(0));
3002 ++ectx->ec_stack.ga_len;
3003 }
3004 return OK;
3005}
3006
Bram Moolenaard0200c82023-01-28 15:19:40 +00003007
3008 static void
3009object_required_error(typval_T *tv)
3010{
3011 garray_T type_list;
3012 ga_init2(&type_list, sizeof(type_T *), 10);
3013 type_T *type = typval2type(tv, get_copyID(), &type_list, TVTT_DO_MEMBER);
3014 char *tofree = NULL;
3015 char *typename = type_name(type, &tofree);
3016 semsg(_(e_object_required_found_str), typename);
3017 vim_free(tofree);
3018 clear_type_list(&type_list);
3019}
3020
Bram Moolenaar06b77222022-01-25 15:51:56 +00003021/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02003022 * Execute instructions in execution context "ectx".
3023 * Return OK or FAIL;
3024 */
3025 static int
3026exec_instructions(ectx_T *ectx)
3027{
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003028 int ret = FAIL;
3029 int save_trylevel_at_start = ectx->ec_trylevel_at_start;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02003030 int dict_stack_len_at_start = dict_stack.ga_len;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003031
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02003032 // Start execution at the first instruction.
Bram Moolenaar4c137212021-04-19 16:48:48 +02003033 ectx->ec_iidx = 0;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01003034
Bram Moolenaarff652882021-05-16 15:24:49 +02003035 // Only catch exceptions in this instruction list.
3036 ectx->ec_trylevel_at_start = trylevel;
3037
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003038 for (;;)
3039 {
Bram Moolenaara7490192021-07-22 12:26:14 +02003040 static int breakcheck_count = 0; // using "static" makes it faster
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003041 isn_T *iptr;
Bram Moolenaara7490192021-07-22 12:26:14 +02003042 typval_T *tv;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003043
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003044 if (unlikely(++breakcheck_count >= 100))
Bram Moolenaar270d0382020-05-15 21:42:53 +02003045 {
3046 line_breakcheck();
3047 breakcheck_count = 0;
3048 }
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003049 if (unlikely(got_int))
Bram Moolenaar20431c92020-03-20 18:39:46 +01003050 {
3051 // Turn CTRL-C into an exception.
3052 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +01003053 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003054 goto theend;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003055 did_throw = TRUE;
3056 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003057
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003058 if (unlikely(did_emsg && msg_list != NULL && *msg_list != NULL))
Bram Moolenaara26b9702020-04-18 19:53:28 +02003059 {
3060 // Turn an error message into an exception.
3061 did_emsg = FALSE;
3062 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003063 goto theend;
Bram Moolenaara26b9702020-04-18 19:53:28 +02003064 did_throw = TRUE;
3065 *msg_list = NULL;
Bram Moolenaar3ef2e412023-04-30 18:50:48 +01003066
3067 // This exception was not caught (yet).
3068 garray_T *trystack = &ectx->ec_trystack;
3069 if (trystack->ga_len > 0)
3070 {
3071 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
3072 + trystack->ga_len - 1;
3073 if (trycmd->tcd_frame_idx == ectx->ec_frame_idx)
3074 trycmd->tcd_caught = FALSE;
3075 }
Bram Moolenaara26b9702020-04-18 19:53:28 +02003076 }
3077
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003078 if (unlikely(did_throw))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003079 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003080 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003081 trycmd_T *trycmd = NULL;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003082 int index = trystack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003083
3084 // An exception jumps to the first catch, finally, or returns from
3085 // the current function.
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003086 while (index > 0)
3087 {
3088 trycmd = ((trycmd_T *)trystack->ga_data) + index - 1;
Bram Moolenaar3ef2e412023-04-30 18:50:48 +01003089 // 1. after :try and before :catch - jump to first :catch
3090 // 2. in :catch block - jump to :finally
3091 // 3. in :catch block and no finally - jump to :endtry
3092 if (!trycmd->tcd_in_catch || trycmd->tcd_finally_idx != 0
3093 || trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003094 break;
3095 // In the catch and finally block of this try we have to go up
3096 // one level.
3097 --index;
3098 trycmd = NULL;
3099 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003100 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003101 {
Bram Moolenaar834193a2021-06-30 20:39:15 +02003102 if (trycmd->tcd_in_catch)
3103 {
Bram Moolenaar3ef2e412023-04-30 18:50:48 +01003104 if (trycmd->tcd_finally_idx > 0)
3105 {
3106 // exception inside ":catch", jump to ":finally" once
3107 ectx->ec_iidx = trycmd->tcd_finally_idx;
3108 trycmd->tcd_finally_idx = 0;
3109 }
3110 else
3111 {
3112 // exception inside ":catch" or ":finally", jump to
3113 // ":endtry"
3114 ectx->ec_iidx = trycmd->tcd_endtry_idx;
3115 }
Bram Moolenaar834193a2021-06-30 20:39:15 +02003116 }
3117 else
Bram Moolenaar3ef2e412023-04-30 18:50:48 +01003118 {
Bram Moolenaar834193a2021-06-30 20:39:15 +02003119 // jump to first ":catch"
3120 ectx->ec_iidx = trycmd->tcd_catch_idx;
Bram Moolenaar3ef2e412023-04-30 18:50:48 +01003121 trycmd->tcd_in_catch = TRUE;
3122 }
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003123 did_throw = FALSE; // don't come back here until :endtry
3124 trycmd->tcd_did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003125 }
3126 else
3127 {
Bram Moolenaar3ef2e412023-04-30 18:50:48 +01003128 // Not inside try or need to return from current function.
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02003129 // Push a dummy return value.
Bram Moolenaar35578162021-08-02 19:10:38 +02003130 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003131 goto theend;
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02003132 tv = STACK_TV_BOT(0);
3133 tv->v_type = VAR_NUMBER;
3134 tv->vval.v_number = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003135 ++ectx->ec_stack.ga_len;
3136 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003137 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02003138 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +01003139 need_rethrow = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003140 if (handle_closure_in_use(ectx, FALSE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003141 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003142 goto done;
3143 }
3144
Bram Moolenaar4c137212021-04-19 16:48:48 +02003145 if (func_return(ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003146 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003147 }
3148 continue;
3149 }
3150
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003151 /*
3152 * Big switch on the instruction. Most compilers will be turning this
3153 * into an efficient lookup table, since the "case" values are an enum
3154 * with sequential numbers. It may look ugly, but it should be the
3155 * most efficient way.
3156 */
Bram Moolenaar4c137212021-04-19 16:48:48 +02003157 iptr = &ectx->ec_instr[ectx->ec_iidx++];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003158 switch (iptr->isn_type)
3159 {
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00003160 // Constructor, first instruction in a new() method.
Bram Moolenaar00b28d62022-12-08 15:32:33 +00003161 case ISN_CONSTRUCT:
3162 // "this" is always the local variable at index zero
3163 tv = STACK_TV_VAR(0);
3164 tv->v_type = VAR_OBJECT;
3165 tv->vval.v_object = alloc_clear(
3166 iptr->isn_arg.construct.construct_size);
3167 tv->vval.v_object->obj_class =
3168 iptr->isn_arg.construct.construct_class;
Bram Moolenaard28d7b92022-12-08 20:42:00 +00003169 ++tv->vval.v_object->obj_class->class_refcount;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00003170 tv->vval.v_object->obj_refcount = 1;
Bram Moolenaard28d7b92022-12-08 20:42:00 +00003171 object_created(tv->vval.v_object);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00003172 break;
3173
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003174 // execute Ex command line
3175 case ISN_EXEC:
Bram Moolenaaraacc9662021-08-13 19:40:51 +02003176 if (exec_command(iptr) == FAIL)
3177 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003178 break;
3179
Bram Moolenaar20677332021-06-06 17:02:53 +02003180 // execute Ex command line split at NL characters.
3181 case ISN_EXEC_SPLIT:
3182 {
3183 source_cookie_T cookie;
Bram Moolenaar518df272021-06-06 17:34:13 +02003184 char_u *line;
Bram Moolenaar20677332021-06-06 17:02:53 +02003185
3186 SOURCING_LNUM = iptr->isn_lnum;
3187 CLEAR_FIELD(cookie);
3188 cookie.sourcing_lnum = iptr->isn_lnum - 1;
3189 cookie.nextline = iptr->isn_arg.string;
Bram Moolenaar518df272021-06-06 17:34:13 +02003190 line = get_split_sourceline(0, &cookie, 0, 0);
3191 if (do_cmdline(line,
Bram Moolenaar20677332021-06-06 17:02:53 +02003192 get_split_sourceline, &cookie,
3193 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED)
3194 == FAIL
3195 || did_emsg)
Bram Moolenaar518df272021-06-06 17:34:13 +02003196 {
3197 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02003198 goto on_error;
Bram Moolenaar518df272021-06-06 17:34:13 +02003199 }
3200 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02003201 }
3202 break;
3203
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00003204 // execute Ex command line that is only a range
3205 case ISN_EXECRANGE:
3206 {
3207 exarg_T ea;
3208 char *error = NULL;
3209
3210 CLEAR_FIELD(ea);
3211 ea.cmdidx = CMD_SIZE;
3212 ea.addr_type = ADDR_LINES;
3213 ea.cmd = iptr->isn_arg.string;
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00003214 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00003215 parse_cmd_address(&ea, &error, FALSE);
Bram Moolenaar01a4dcb2021-12-04 13:15:10 +00003216 if (ea.cmd == NULL)
3217 goto on_error;
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00003218 // error is always NULL when using ADDR_LINES
3219 error = ex_range_without_command(&ea);
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00003220 if (error != NULL)
3221 {
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00003222 emsg(error);
3223 goto on_error;
3224 }
3225 }
3226 break;
3227
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02003228 // Evaluate an expression with legacy syntax, push it onto the
3229 // stack.
3230 case ISN_LEGACY_EVAL:
3231 {
3232 char_u *arg = iptr->isn_arg.string;
3233 int res;
3234 int save_flags = cmdmod.cmod_flags;
3235
Bram Moolenaar35578162021-08-02 19:10:38 +02003236 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003237 goto theend;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02003238 tv = STACK_TV_BOT(0);
3239 init_tv(tv);
3240 cmdmod.cmod_flags |= CMOD_LEGACY;
3241 res = eval0(arg, tv, NULL, &EVALARG_EVALUATE);
3242 cmdmod.cmod_flags = save_flags;
3243 if (res == FAIL)
3244 goto on_error;
3245 ++ectx->ec_stack.ga_len;
3246 }
3247 break;
3248
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003249 // push typeval VAR_INSTR with instructions to be executed
3250 case ISN_INSTR:
3251 {
Bram Moolenaar35578162021-08-02 19:10:38 +02003252 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003253 goto theend;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003254 tv = STACK_TV_BOT(0);
3255 tv->vval.v_instr = ALLOC_ONE(instr_T);
3256 if (tv->vval.v_instr == NULL)
3257 goto on_error;
3258 ++ectx->ec_stack.ga_len;
3259
3260 tv->v_type = VAR_INSTR;
3261 tv->vval.v_instr->instr_ectx = ectx;
3262 tv->vval.v_instr->instr_instr = iptr->isn_arg.instr;
3263 }
3264 break;
3265
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003266 case ISN_SOURCE:
3267 {
Bram Moolenaar89445512022-04-14 12:58:23 +01003268 int notused;
LemonBoy77142312022-04-15 20:50:46 +01003269
Bram Moolenaar89445512022-04-14 12:58:23 +01003270 SOURCING_LNUM = iptr->isn_lnum;
3271 if (may_load_script((int)iptr->isn_arg.number, &notused)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003272 == FAIL)
Bram Moolenaar89445512022-04-14 12:58:23 +01003273 goto on_error;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003274 }
3275 break;
3276
Bram Moolenaar4c137212021-04-19 16:48:48 +02003277 // execute :substitute with an expression
3278 case ISN_SUBSTITUTE:
3279 {
3280 subs_T *subs = &iptr->isn_arg.subs;
3281 source_cookie_T cookie;
3282 struct subs_expr_S *save_instr = substitute_instr;
3283 struct subs_expr_S subs_instr;
3284 int res;
3285
3286 subs_instr.subs_ectx = ectx;
3287 subs_instr.subs_instr = subs->subs_instr;
3288 subs_instr.subs_status = OK;
3289 substitute_instr = &subs_instr;
3290
3291 SOURCING_LNUM = iptr->isn_lnum;
3292 // This is very much like ISN_EXEC
3293 CLEAR_FIELD(cookie);
3294 cookie.sourcing_lnum = iptr->isn_lnum - 1;
3295 res = do_cmdline(subs->subs_cmd,
3296 getsourceline, &cookie,
3297 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
3298 substitute_instr = save_instr;
3299
3300 if (res == FAIL || did_emsg
3301 || subs_instr.subs_status == FAIL)
3302 goto on_error;
3303 }
3304 break;
3305
3306 case ISN_FINISH:
3307 goto done;
3308
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02003309 case ISN_REDIRSTART:
3310 // create a dummy entry for var_redir_str()
3311 if (alloc_redir_lval() == FAIL)
3312 goto on_error;
3313
3314 // The output is stored in growarray "redir_ga" until
3315 // redirection ends.
3316 init_redir_ga();
3317 redir_vname = 1;
3318 break;
3319
3320 case ISN_REDIREND:
3321 {
3322 char_u *res = get_clear_redir_ga();
3323
3324 // End redirection, put redirected text on the stack.
3325 clear_redir_lval();
3326 redir_vname = 0;
3327
Bram Moolenaar35578162021-08-02 19:10:38 +02003328 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02003329 {
3330 vim_free(res);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003331 goto theend;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02003332 }
3333 tv = STACK_TV_BOT(0);
3334 tv->v_type = VAR_STRING;
3335 tv->vval.v_string = res;
3336 ++ectx->ec_stack.ga_len;
3337 }
3338 break;
3339
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003340 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02003341#ifdef FEAT_QUICKFIX
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003342 force_abort = TRUE;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003343 if (trigger_cexpr_autocmd(iptr->isn_arg.number) == FAIL)
3344 goto on_error;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003345 force_abort = FALSE;
Bram Moolenaarb7c97812021-05-05 22:51:39 +02003346#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003347 break;
3348
3349 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02003350#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003351 {
3352 exarg_T ea;
3353 int res;
3354
3355 CLEAR_FIELD(ea);
3356 ea.cmdidx = iptr->isn_arg.cexpr.cexpr_ref->cer_cmdidx;
3357 ea.forceit = iptr->isn_arg.cexpr.cexpr_ref->cer_forceit;
3358 ea.cmdlinep = &iptr->isn_arg.cexpr.cexpr_ref->cer_cmdline;
3359 --ectx->ec_stack.ga_len;
3360 tv = STACK_TV_BOT(0);
Bram Moolenaarbd683e32022-07-18 17:49:03 +01003361 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003362 res = cexpr_core(&ea, tv);
3363 clear_tv(tv);
3364 if (res == FAIL)
3365 goto on_error;
3366 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02003367#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003368 break;
3369
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003370 // execute Ex command from pieces on the stack
3371 case ISN_EXECCONCAT:
3372 {
3373 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +02003374 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003375 int pass;
3376 int i;
3377 char_u *cmd = NULL;
3378 char_u *str;
3379
3380 for (pass = 1; pass <= 2; ++pass)
3381 {
3382 for (i = 0; i < count; ++i)
3383 {
3384 tv = STACK_TV_BOT(i - count);
3385 str = tv->vval.v_string;
3386 if (str != NULL && *str != NUL)
3387 {
3388 if (pass == 2)
3389 STRCPY(cmd + len, str);
3390 len += STRLEN(str);
3391 }
3392 if (pass == 2)
3393 clear_tv(tv);
3394 }
3395 if (pass == 1)
3396 {
3397 cmd = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003398 if (unlikely(cmd == NULL))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003399 goto theend;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003400 len = 0;
3401 }
3402 }
3403
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02003404 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003405 do_cmdline_cmd(cmd);
3406 vim_free(cmd);
3407 }
3408 break;
3409
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003410 // execute :echo {string} ...
3411 case ISN_ECHO:
3412 {
3413 int count = iptr->isn_arg.echo.echo_count;
3414 int atstart = TRUE;
3415 int needclr = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003416 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003417
3418 for (idx = 0; idx < count; ++idx)
3419 {
3420 tv = STACK_TV_BOT(idx - count);
3421 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
3422 &atstart, &needclr);
3423 clear_tv(tv);
3424 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01003425 if (needclr)
3426 msg_clr_eos();
Bram Moolenaar4c137212021-04-19 16:48:48 +02003427 ectx->ec_stack.ga_len -= count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003428 }
3429 break;
3430
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003431 // :execute {string} ...
3432 // :echomsg {string} ...
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01003433 // :echowindow {string} ...
Bram Moolenaar7de62622021-08-07 15:05:47 +02003434 // :echoconsole {string} ...
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003435 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01003436 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003437 case ISN_ECHOMSG:
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01003438 case ISN_ECHOWINDOW:
Bram Moolenaar7de62622021-08-07 15:05:47 +02003439 case ISN_ECHOCONSOLE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003440 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01003441 {
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01003442 int count;
Bram Moolenaarad39c092020-02-26 18:23:43 +01003443 garray_T ga;
3444 char_u buf[NUMBUFLEN];
3445 char_u *p;
3446 int len;
3447 int failed = FALSE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003448 int idx;
Bram Moolenaarad39c092020-02-26 18:23:43 +01003449
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01003450 if (iptr->isn_type == ISN_ECHOWINDOW)
3451 count = iptr->isn_arg.echowin.ewin_count;
3452 else
3453 count = iptr->isn_arg.number;
Bram Moolenaarad39c092020-02-26 18:23:43 +01003454 ga_init2(&ga, 1, 80);
3455 for (idx = 0; idx < count; ++idx)
3456 {
3457 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003458 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01003459 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003460 if (tv->v_type == VAR_CHANNEL
3461 || tv->v_type == VAR_JOB)
3462 {
3463 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar68db9962021-05-09 23:19:22 +02003464 semsg(_(e_using_invalid_value_as_string_str),
3465 vartype_name(tv->v_type));
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003466 break;
3467 }
3468 else
3469 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01003470 }
3471 else
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003472 p = tv_stringify(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01003473
3474 len = (int)STRLEN(p);
Bram Moolenaar35578162021-08-02 19:10:38 +02003475 if (GA_GROW_FAILS(&ga, len + 2))
Bram Moolenaarad39c092020-02-26 18:23:43 +01003476 failed = TRUE;
3477 else
3478 {
3479 if (ga.ga_len > 0)
3480 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
3481 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
3482 ga.ga_len += len;
3483 }
3484 clear_tv(tv);
3485 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003486 ectx->ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003487 if (failed)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003488 {
3489 ga_clear(&ga);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003490 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003491 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01003492
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003493 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003494 {
3495 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02003496 {
3497 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003498 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01003499 if (did_emsg)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003500 {
3501 ga_clear(&ga);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01003502 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003503 }
Bram Moolenaar430deb12020-08-23 16:29:11 +02003504 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003505 else
3506 {
3507 msg_sb_eol();
3508 if (iptr->isn_type == ISN_ECHOMSG)
3509 {
3510 msg_attr(ga.ga_data, echo_attr);
3511 out_flush();
3512 }
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01003513#ifdef HAS_MESSAGE_WINDOW
3514 else if (iptr->isn_type == ISN_ECHOWINDOW)
3515 {
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01003516 start_echowindow(
3517 iptr->isn_arg.echowin.ewin_time);
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01003518 msg_attr(ga.ga_data, echo_attr);
3519 end_echowindow();
3520 }
3521#endif
Bram Moolenaar7de62622021-08-07 15:05:47 +02003522 else if (iptr->isn_type == ISN_ECHOCONSOLE)
3523 {
3524 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data),
3525 TRUE);
3526 ui_write((char_u *)"\r\n", 2, TRUE);
3527 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003528 else
3529 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003530 SOURCING_LNUM = iptr->isn_lnum;
3531 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003532 }
3533 }
3534 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01003535 ga_clear(&ga);
3536 }
3537 break;
3538
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003539 // load local variable or argument
3540 case ISN_LOAD:
Bram Moolenaar35578162021-08-02 19:10:38 +02003541 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003542 goto theend;
Bram Moolenaar2ed57ac2023-04-01 22:05:38 +01003543 tv = STACK_TV_VAR(iptr->isn_arg.number);
3544 if (tv->v_type == VAR_UNKNOWN)
3545 {
3546 // missing argument or default value v:none
3547 STACK_TV_BOT(0)->v_type = VAR_SPECIAL;
3548 STACK_TV_BOT(0)->vval.v_number = VVAL_NONE;
3549 }
3550 else
3551 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003552 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003553 break;
3554
3555 // load v: variable
3556 case ISN_LOADV:
Bram Moolenaar35578162021-08-02 19:10:38 +02003557 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003558 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003559 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003560 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003561 break;
3562
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003563 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003564 case ISN_LOADSCRIPT:
3565 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01003566 scriptref_T *sref = iptr->isn_arg.script.scriptref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003567 svar_T *sv;
3568
Bram Moolenaar6db660b2021-08-01 14:08:54 +02003569 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003570 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003571 goto theend;
Bram Moolenaar859cc212022-03-28 15:22:35 +01003572 allocate_if_null(sv);
Bram Moolenaar35578162021-08-02 19:10:38 +02003573 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003574 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003575 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003576 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003577 }
3578 break;
3579
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003580 // load s: variable in old script or autoload import
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003581 case ISN_LOADS:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003582 case ISN_LOADEXPORT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003583 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003584 int sid = iptr->isn_arg.loadstore.ls_sid;
3585 hashtab_T *ht = &SCRIPT_VARS(sid);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003586 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003587 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003588
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003589 if (di == NULL)
3590 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003591 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003592 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003593 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003594 }
3595 else
3596 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003597 if (iptr->isn_type == ISN_LOADEXPORT)
3598 {
3599 int idx = get_script_item_idx(sid, name, 0,
3600 NULL, NULL);
3601 svar_T *sv;
3602
3603 if (idx >= 0)
3604 {
3605 sv = ((svar_T *)SCRIPT_ITEM(sid)
3606 ->sn_var_vals.ga_data) + idx;
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003607 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003608 {
3609 SOURCING_LNUM = iptr->isn_lnum;
3610 semsg(_(e_item_not_exported_in_script_str),
3611 name);
3612 goto on_error;
3613 }
3614 }
3615 }
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 copy_tv(&di->di_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003619 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003620 }
3621 }
3622 break;
3623
Bram Moolenaard3aac292020-04-19 14:32:17 +02003624 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003625 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02003626 case ISN_LOADB:
3627 case ISN_LOADW:
3628 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003629 {
Bram Moolenaar06b77222022-01-25 15:51:56 +00003630 int res = load_namespace_var(ectx, iptr->isn_type, iptr);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003631
Bram Moolenaar06b77222022-01-25 15:51:56 +00003632 if (res == NOTDONE)
Bram Moolenaarcbbc48f2022-01-18 12:58:28 +00003633 goto theend;
Bram Moolenaar06b77222022-01-25 15:51:56 +00003634 if (res == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003635 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003636 }
Bram Moolenaar06b77222022-01-25 15:51:56 +00003637
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003638 break;
3639
Bram Moolenaar03290b82020-12-19 16:30:44 +01003640 // load autoload variable
3641 case ISN_LOADAUTO:
3642 {
3643 char_u *name = iptr->isn_arg.string;
3644
Bram Moolenaar35578162021-08-02 19:10:38 +02003645 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003646 goto theend;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003647 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003648 if (eval_variable(name, (int)STRLEN(name), 0,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003649 STACK_TV_BOT(0), NULL, EVAL_VAR_VERBOSE) == FAIL)
Bram Moolenaar03290b82020-12-19 16:30:44 +01003650 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003651 ++ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003652 }
3653 break;
3654
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003655 // load g:/b:/w:/t: namespace
3656 case ISN_LOADGDICT:
3657 case ISN_LOADBDICT:
3658 case ISN_LOADWDICT:
3659 case ISN_LOADTDICT:
3660 {
3661 dict_T *d = NULL;
3662
3663 switch (iptr->isn_type)
3664 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02003665 case ISN_LOADGDICT: d = get_globvar_dict(); break;
3666 case ISN_LOADBDICT: d = curbuf->b_vars; break;
3667 case ISN_LOADWDICT: d = curwin->w_vars; break;
3668 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003669 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003670 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003671 }
Bram Moolenaar35578162021-08-02 19:10:38 +02003672 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003673 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003674 tv = STACK_TV_BOT(0);
3675 tv->v_type = VAR_DICT;
3676 tv->v_lock = 0;
3677 tv->vval.v_dict = d;
Bram Moolenaar1bd3cb22021-02-24 12:27:31 +01003678 ++d->dv_refcount;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003679 ++ectx->ec_stack.ga_len;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003680 }
3681 break;
3682
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003683 // load &option
3684 case ISN_LOADOPT:
3685 {
3686 typval_T optval;
3687 char_u *name = iptr->isn_arg.string;
3688
Bram Moolenaara8c17702020-04-01 21:17:24 +02003689 // This is not expected to fail, name is checked during
3690 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar35578162021-08-02 19:10:38 +02003691 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003692 goto theend;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003693 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003694 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003695 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003696 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003697 }
3698 break;
3699
3700 // load $ENV
3701 case ISN_LOADENV:
3702 {
3703 typval_T optval;
3704 char_u *name = iptr->isn_arg.string;
3705
Bram Moolenaar35578162021-08-02 19:10:38 +02003706 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003707 goto theend;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003708 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003709 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003710 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003711 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003712 }
3713 break;
3714
3715 // load @register
3716 case ISN_LOADREG:
Bram Moolenaar35578162021-08-02 19:10:38 +02003717 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003718 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003719 tv = STACK_TV_BOT(0);
3720 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003721 tv->v_lock = 0;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02003722 // This may result in NULL, which should be equivalent to an
3723 // empty string.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003724 tv->vval.v_string = get_reg_contents(
3725 iptr->isn_arg.number, GREG_EXPR_SRC);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003726 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003727 break;
3728
3729 // store local variable
3730 case ISN_STORE:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003731 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003732 tv = STACK_TV_VAR(iptr->isn_arg.number);
3733 clear_tv(tv);
3734 *tv = *STACK_TV_BOT(0);
3735 break;
3736
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003737 // store s: variable in old script or autoload import
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003738 case ISN_STORES:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003739 case ISN_STOREEXPORT:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003740 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003741 int sid = iptr->isn_arg.loadstore.ls_sid;
3742 hashtab_T *ht = &SCRIPT_VARS(sid);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003743 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003744 dictitem_T *di = find_var_in_ht(ht, 0,
3745 iptr->isn_type == ISN_STORES
3746 ? name + 2 : name, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003747
Bram Moolenaar4c137212021-04-19 16:48:48 +02003748 --ectx->ec_stack.ga_len;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003749 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003750 if (di == NULL)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003751 {
3752 if (iptr->isn_type == ISN_STOREEXPORT)
3753 {
3754 semsg(_(e_undefined_variable_str), name);
Bram Moolenaard1d26842022-03-31 10:13:47 +01003755 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003756 goto on_error;
3757 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003758 store_var(name, STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003759 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003760 else
3761 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003762 if (iptr->isn_type == ISN_STOREEXPORT)
3763 {
3764 int idx = get_script_item_idx(sid, name, 0,
3765 NULL, NULL);
3766
Bram Moolenaar06651632022-04-27 17:54:25 +01003767 // can this ever fail?
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003768 if (idx >= 0)
3769 {
3770 svar_T *sv = ((svar_T *)SCRIPT_ITEM(sid)
3771 ->sn_var_vals.ga_data) + idx;
3772
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003773 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003774 {
3775 semsg(_(e_item_not_exported_in_script_str),
3776 name);
Bram Moolenaard1d26842022-03-31 10:13:47 +01003777 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003778 goto on_error;
3779 }
3780 }
3781 }
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02003782 if (var_check_permission(di, name) == FAIL)
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003783 {
3784 clear_tv(STACK_TV_BOT(0));
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02003785 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003786 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003787 clear_tv(&di->di_tv);
3788 di->di_tv = *STACK_TV_BOT(0);
3789 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003790 }
3791 break;
3792
3793 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003794 case ISN_STORESCRIPT:
3795 {
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003796 scriptref_T *sref = iptr->isn_arg.script.scriptref;
3797 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003798
Bram Moolenaar6db660b2021-08-01 14:08:54 +02003799 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003800 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003801 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003802 --ectx->ec_stack.ga_len;
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003803
3804 // "const" and "final" are checked at compile time, locking
3805 // the value needs to be checked here.
3806 SOURCING_LNUM = iptr->isn_lnum;
3807 if (value_check_lock(sv->sv_tv->v_lock, sv->sv_name, FALSE))
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003808 {
3809 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003810 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003811 }
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003812
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003813 clear_tv(sv->sv_tv);
3814 *sv->sv_tv = *STACK_TV_BOT(0);
3815 }
3816 break;
3817
3818 // store option
3819 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003820 case ISN_STOREFUNCOPT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003821 {
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003822 char_u *opt_name = iptr->isn_arg.storeopt.so_name;
3823 int opt_flags = iptr->isn_arg.storeopt.so_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003824 long n = 0;
3825 char_u *s = NULL;
3826 char *msg;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003827 char_u numbuf[NUMBUFLEN];
3828 char_u *tofree = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003829
Bram Moolenaar4c137212021-04-19 16:48:48 +02003830 --ectx->ec_stack.ga_len;
LemonBoy32f34612023-09-02 21:52:05 +02003831 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003832 tv = STACK_TV_BOT(0);
3833 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01003834 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003835 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01003836 if (s == NULL)
3837 s = (char_u *)"";
3838 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003839 else if (iptr->isn_type == ISN_STOREFUNCOPT)
3840 {
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003841 // If the option can be set to a function reference or
3842 // a lambda and the passed value is a function
3843 // reference, then convert it to the name (string) of
3844 // the function reference.
3845 s = tv2string(tv, &tofree, numbuf, 0);
3846 if (s == NULL || *s == NUL)
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003847 {
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003848 // cannot happen?
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003849 clear_tv(tv);
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003850 vim_free(tofree);
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003851 goto on_error;
3852 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003853 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003854 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003855 // must be VAR_NUMBER, CHECKTYPE makes sure
3856 n = tv->vval.v_number;
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003857 msg = set_option_value(opt_name, n, s, opt_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02003858 clear_tv(tv);
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003859 vim_free(tofree);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003860 if (msg != NULL)
3861 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003862 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003863 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02003864 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003865 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003866 }
3867 break;
3868
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003869 // store $ENV
3870 case ISN_STOREENV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003871 --ectx->ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003872 tv = STACK_TV_BOT(0);
3873 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
3874 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003875 break;
3876
3877 // store @r
3878 case ISN_STOREREG:
3879 {
3880 int reg = iptr->isn_arg.number;
3881
Bram Moolenaar4c137212021-04-19 16:48:48 +02003882 --ectx->ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01003883 tv = STACK_TV_BOT(0);
Bram Moolenaar74f4a962021-06-17 21:03:07 +02003884 write_reg_contents(reg, tv_get_string(tv), -1, FALSE);
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01003885 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003886 }
3887 break;
3888
3889 // store v: variable
3890 case ISN_STOREV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003891 --ectx->ec_stack.ga_len;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003892 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
3893 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003894 // should not happen, type is checked when compiling
3895 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003896 break;
3897
Bram Moolenaard3aac292020-04-19 14:32:17 +02003898 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003899 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02003900 case ISN_STOREB:
3901 case ISN_STOREW:
3902 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003903 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003904 dictitem_T *di;
3905 hashtab_T *ht;
3906 char_u *name = iptr->isn_arg.string + 2;
3907
Bram Moolenaard3aac292020-04-19 14:32:17 +02003908 switch (iptr->isn_type)
3909 {
3910 case ISN_STOREG:
3911 ht = get_globvar_ht();
3912 break;
3913 case ISN_STOREB:
3914 ht = &curbuf->b_vars->dv_hashtab;
3915 break;
3916 case ISN_STOREW:
3917 ht = &curwin->w_vars->dv_hashtab;
3918 break;
3919 case ISN_STORET:
3920 ht = &curtab->tp_vars->dv_hashtab;
3921 break;
3922 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003923 goto theend;
Bram Moolenaard3aac292020-04-19 14:32:17 +02003924 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003925
Bram Moolenaar4c137212021-04-19 16:48:48 +02003926 --ectx->ec_stack.ga_len;
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003927 di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003928 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003929 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003930 else
3931 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003932 SOURCING_LNUM = iptr->isn_lnum;
3933 if (var_check_permission(di, name) == FAIL)
3934 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003935 clear_tv(&di->di_tv);
3936 di->di_tv = *STACK_TV_BOT(0);
3937 }
3938 }
3939 break;
3940
Bram Moolenaar03290b82020-12-19 16:30:44 +01003941 // store an autoload variable
3942 case ISN_STOREAUTO:
3943 SOURCING_LNUM = iptr->isn_lnum;
3944 set_var(iptr->isn_arg.string, STACK_TV_BOT(-1), TRUE);
3945 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003946 --ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003947 break;
3948
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003949 // store number in local variable
3950 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01003951 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003952 clear_tv(tv);
3953 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01003954 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003955 break;
3956
Bram Moolenaar590162c2022-12-24 21:24:06 +00003957 // Store a value in a list, dict, blob or object variable.
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01003958 case ISN_STOREINDEX:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003959 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003960 int res = execute_storeindex(iptr, ectx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003961
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003962 if (res == FAIL)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02003963 goto on_error;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003964 if (res == NOTDONE)
3965 goto theend;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003966 }
3967 break;
3968
Bram Moolenaarea5c8982022-02-17 14:42:02 +00003969 // store value in list or blob range
Bram Moolenaar68452172021-04-12 21:21:02 +02003970 case ISN_STORERANGE:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003971 if (execute_storerange(iptr, ectx) == FAIL)
3972 goto on_error;
Bram Moolenaar68452172021-04-12 21:21:02 +02003973 break;
3974
Bram Moolenaard505d172022-12-18 21:42:55 +00003975 case ISN_LOAD_CLASSMEMBER:
3976 {
3977 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
3978 goto theend;
3979 classmember_T *cm = &iptr->isn_arg.classmember;
Bram Moolenaar4e2406c2023-06-24 19:22:21 +01003980 copy_tv(cm->cm_class->class_members_tv + cm->cm_idx,
3981 STACK_TV_BOT(0));
Bram Moolenaard505d172022-12-18 21:42:55 +00003982 ++ectx->ec_stack.ga_len;
3983 }
3984 break;
3985
3986 case ISN_STORE_CLASSMEMBER:
3987 {
3988 classmember_T *cm = &iptr->isn_arg.classmember;
3989 tv = &cm->cm_class->class_members_tv[cm->cm_idx];
3990 clear_tv(tv);
3991 *tv = *STACK_TV_BOT(-1);
3992 --ectx->ec_stack.ga_len;
3993 }
3994 break;
3995
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01003996 // Load or store variable or argument from outer scope.
Bram Moolenaar0186e582021-01-10 18:33:11 +01003997 case ISN_LOADOUTER:
3998 case ISN_STOREOUTER:
3999 {
4000 int depth = iptr->isn_arg.outer.outer_depth;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004001 outer_T *outer = ectx->ec_outer_ref == NULL ? NULL
4002 : ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +01004003
4004 while (depth > 1 && outer != NULL)
4005 {
4006 outer = outer->out_up;
4007 --depth;
4008 }
4009 if (outer == NULL)
4010 {
4011 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar69c76172021-12-02 16:38:52 +00004012 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx
4013 || ectx->ec_outer_ref == NULL)
4014 // Possibly :def function called from legacy
4015 // context.
4016 emsg(_(e_closure_called_from_invalid_context));
4017 else
4018 iemsg("LOADOUTER depth more than scope levels");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004019 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01004020 }
Bram Moolenaarcc341812022-09-19 15:54:34 +01004021 if (depth < 0)
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01004022 // Variable declared in loop. May be copied if the
4023 // loop block has already ended.
Bram Moolenaarcc341812022-09-19 15:54:34 +01004024 tv = ((typval_T *)outer->out_loop[-depth - 1]
4025 .stack->ga_data)
4026 + outer->out_loop[-depth - 1].var_idx
4027 + iptr->isn_arg.outer.outer_idx;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004028 else
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01004029 // Variable declared in a function. May be copied if
4030 // the function has already returned.
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004031 tv = ((typval_T *)outer->out_stack->ga_data)
4032 + outer->out_frame_idx + STACK_FRAME_SIZE
4033 + iptr->isn_arg.outer.outer_idx;
Bram Moolenaar0186e582021-01-10 18:33:11 +01004034 if (iptr->isn_type == ISN_LOADOUTER)
4035 {
Bram Moolenaar35578162021-08-02 19:10:38 +02004036 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004037 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01004038 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02004039 ++ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01004040 }
4041 else
4042 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004043 --ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01004044 clear_tv(tv);
4045 *tv = *STACK_TV_BOT(0);
4046 }
4047 }
4048 break;
4049
Bram Moolenaar752fc692021-01-04 21:57:11 +01004050 // unlet item in list or dict variable
4051 case ISN_UNLETINDEX:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00004052 if (execute_unletindex(iptr, ectx) == FAIL)
4053 goto on_error;
Bram Moolenaar752fc692021-01-04 21:57:11 +01004054 break;
4055
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01004056 // unlet range of items in list variable
4057 case ISN_UNLETRANGE:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00004058 if (execute_unletrange(iptr, ectx) == FAIL)
4059 goto on_error;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01004060 break;
4061
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004062 // push constant
4063 case ISN_PUSHNR:
4064 case ISN_PUSHBOOL:
4065 case ISN_PUSHSPEC:
4066 case ISN_PUSHF:
4067 case ISN_PUSHS:
4068 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004069 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004070 case ISN_PUSHCHANNEL:
4071 case ISN_PUSHJOB:
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00004072 case ISN_PUSHOBJ:
4073 case ISN_PUSHCLASS:
Bram Moolenaar35578162021-08-02 19:10:38 +02004074 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004075 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004076 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02004077 tv->v_lock = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004078 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004079 switch (iptr->isn_type)
4080 {
4081 case ISN_PUSHNR:
4082 tv->v_type = VAR_NUMBER;
4083 tv->vval.v_number = iptr->isn_arg.number;
4084 break;
4085 case ISN_PUSHBOOL:
4086 tv->v_type = VAR_BOOL;
4087 tv->vval.v_number = iptr->isn_arg.number;
4088 break;
4089 case ISN_PUSHSPEC:
4090 tv->v_type = VAR_SPECIAL;
4091 tv->vval.v_number = iptr->isn_arg.number;
4092 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004093 case ISN_PUSHF:
4094 tv->v_type = VAR_FLOAT;
4095 tv->vval.v_float = iptr->isn_arg.fnumber;
4096 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004097 case ISN_PUSHBLOB:
4098 blob_copy(iptr->isn_arg.blob, tv);
4099 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004100 case ISN_PUSHFUNC:
4101 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01004102 if (iptr->isn_arg.string == NULL)
4103 tv->vval.v_string = NULL;
4104 else
4105 tv->vval.v_string =
4106 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004107 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004108 case ISN_PUSHCHANNEL:
4109#ifdef FEAT_JOB_CHANNEL
4110 tv->v_type = VAR_CHANNEL;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00004111 tv->vval.v_channel = NULL;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004112#endif
4113 break;
4114 case ISN_PUSHJOB:
4115#ifdef FEAT_JOB_CHANNEL
4116 tv->v_type = VAR_JOB;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00004117 tv->vval.v_job = NULL;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004118#endif
4119 break;
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00004120 case ISN_PUSHOBJ:
4121 tv->v_type = VAR_OBJECT;
4122 tv->vval.v_object = NULL;
4123 break;
4124 case ISN_PUSHCLASS:
4125 tv->v_type = VAR_CLASS;
Bram Moolenaar30a84472023-02-27 08:07:14 +00004126 tv->vval.v_class = iptr->isn_arg.classarg;
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00004127 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004128 default:
4129 tv->v_type = VAR_STRING;
Bram Moolenaarf8691002022-03-10 12:20:53 +00004130 tv->vval.v_string = iptr->isn_arg.string == NULL
4131 ? NULL : vim_strsave(iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004132 }
4133 break;
4134
Bram Moolenaar06b77222022-01-25 15:51:56 +00004135 case ISN_AUTOLOAD:
4136 {
4137 char_u *name = iptr->isn_arg.string;
4138
4139 (void)script_autoload(name, FALSE);
4140 if (find_func(name, TRUE))
4141 {
4142 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
4143 goto theend;
4144 tv = STACK_TV_BOT(0);
4145 tv->v_lock = 0;
4146 ++ectx->ec_stack.ga_len;
4147 tv->v_type = VAR_FUNC;
4148 tv->vval.v_string = vim_strsave(name);
4149 }
4150 else
4151 {
4152 int res = load_namespace_var(ectx, ISN_LOADG, iptr);
4153
4154 if (res == NOTDONE)
4155 goto theend;
4156 if (res == FAIL)
4157 goto on_error;
4158 }
4159 }
4160 break;
4161
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004162 case ISN_UNLET:
4163 if (do_unlet(iptr->isn_arg.unlet.ul_name,
4164 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02004165 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004166 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02004167 case ISN_UNLETENV:
LemonBoy77142312022-04-15 20:50:46 +01004168 vim_unsetenv_ext(iptr->isn_arg.unlet.ul_name);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02004169 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004170
Bram Moolenaaraacc9662021-08-13 19:40:51 +02004171 case ISN_LOCKUNLOCK:
4172 {
4173 typval_T *lval_root_save = lval_root;
4174 int res;
4175
4176 // Stack has the local variable, argument the whole :lock
4177 // or :unlock command, like ISN_EXEC.
4178 --ectx->ec_stack.ga_len;
4179 lval_root = STACK_TV_BOT(0);
4180 res = exec_command(iptr);
4181 clear_tv(lval_root);
4182 lval_root = lval_root_save;
4183 if (res == FAIL)
4184 goto on_error;
4185 }
4186 break;
4187
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02004188 case ISN_LOCKCONST:
4189 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
4190 break;
4191
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004192 // create a list from items on the stack; uses a single allocation
4193 // for the list header and the items
4194 case ISN_NEWLIST:
Bram Moolenaar4c137212021-04-19 16:48:48 +02004195 if (exe_newlist(iptr->isn_arg.number, ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004196 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004197 break;
4198
4199 // create a dict from items on the stack
4200 case ISN_NEWDICT:
4201 {
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01004202 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004203
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01004204 SOURCING_LNUM = iptr->isn_lnum;
4205 res = exe_newdict(iptr->isn_arg.number, ectx);
4206 if (res == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004207 goto theend;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01004208 if (res == MAYBE)
4209 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004210 }
4211 break;
4212
LemonBoy372bcce2022-04-25 12:43:20 +01004213 case ISN_CONCAT:
4214 if (exe_concat(iptr->isn_arg.number, ectx) == FAIL)
4215 goto theend;
4216 break;
4217
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004218 // create a partial with NULL value
4219 case ISN_NEWPARTIAL:
4220 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
4221 goto theend;
4222 ++ectx->ec_stack.ga_len;
4223 tv = STACK_TV_BOT(-1);
4224 tv->v_type = VAR_PARTIAL;
4225 tv->v_lock = 0;
4226 tv->vval.v_partial = NULL;
4227 break;
4228
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004229 // call a :def function
4230 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02004231 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01004232 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
4233 NULL,
4234 iptr->isn_arg.dfunc.cdf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02004235 ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02004236 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004237 break;
4238
Bram Moolenaard0200c82023-01-28 15:19:40 +00004239 // call a method on an interface
4240 case ISN_METHODCALL:
4241 {
Bram Moolenaar094cf9f2023-02-10 15:52:25 +00004242 cmfunc_T *mfunc = iptr->isn_arg.mfunc;
4243
Bram Moolenaard0200c82023-01-28 15:19:40 +00004244 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar094cf9f2023-02-10 15:52:25 +00004245 tv = STACK_TV_BOT(-1 - mfunc->cmf_argcount);
Bram Moolenaard0200c82023-01-28 15:19:40 +00004246 if (tv->v_type != VAR_OBJECT)
4247 {
4248 object_required_error(tv);
4249 goto on_error;
4250 }
4251 object_T *obj = tv->vval.v_object;
4252 class_T *cl = obj->obj_class;
4253
4254 // convert the interface index to the object index
Bram Moolenaard0200c82023-01-28 15:19:40 +00004255 int idx = object_index_from_itf_index(mfunc->cmf_itf,
Ernie Rael18143d32023-09-04 22:30:41 +02004256 TRUE, mfunc->cmf_idx, cl,
4257 FALSE);
Bram Moolenaard0200c82023-01-28 15:19:40 +00004258
4259 if (call_ufunc(cl->class_obj_methods[idx], NULL,
4260 mfunc->cmf_argcount, ectx, NULL, NULL) == FAIL)
4261 goto on_error;
4262 }
4263 break;
4264
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004265 // call a builtin function
4266 case ISN_BCALL:
4267 SOURCING_LNUM = iptr->isn_lnum;
4268 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
4269 iptr->isn_arg.bfunc.cbf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02004270 ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004271 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004272 break;
4273
4274 // call a funcref or partial
4275 case ISN_PCALL:
4276 {
4277 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
4278 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02004279 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004280
4281 SOURCING_LNUM = iptr->isn_lnum;
4282 if (pfunc->cpf_top)
4283 {
4284 // funcref is above the arguments
4285 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
4286 }
4287 else
4288 {
4289 // Get the funcref from the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004290 --ectx->ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02004291 partial_tv = *STACK_TV_BOT(0);
4292 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004293 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004294 r = call_partial(tv, pfunc->cpf_argcount, ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02004295 if (tv == &partial_tv)
4296 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004297 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004298 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004299 }
4300 break;
4301
Bram Moolenaarbd5da372020-03-31 23:13:10 +02004302 case ISN_PCALL_END:
4303 // PCALL finished, arguments have been consumed and replaced by
4304 // the return value. Now clear the funcref from the stack,
4305 // and move the return value in its place.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004306 --ectx->ec_stack.ga_len;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02004307 clear_tv(STACK_TV_BOT(-1));
4308 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
4309 break;
4310
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004311 // call a user defined function or funcref/partial
4312 case ISN_UCALL:
4313 {
4314 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
4315
4316 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01004317 if (call_eval_func(cufunc->cuf_name, cufunc->cuf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02004318 ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004319 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004320 }
4321 break;
4322
Bram Moolenaar1d84f762022-09-03 21:35:53 +01004323 // :defer func(arg)
4324 case ISN_DEFER:
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00004325 case ISN_DEFEROBJ:
Bram Moolenaar806a2732022-09-04 15:40:36 +01004326 if (defer_command(iptr->isn_arg.defer.defer_var_idx,
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00004327 iptr->isn_type == ISN_DEFEROBJ,
Bram Moolenaar1d84f762022-09-03 21:35:53 +01004328 iptr->isn_arg.defer.defer_argcount, ectx) == FAIL)
4329 goto on_error;
4330 break;
4331
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004332 // Return from a :def function call without a value.
4333 // Return from a constructor.
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02004334 case ISN_RETURN_VOID:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004335 case ISN_RETURN_OBJECT:
Bram Moolenaar35578162021-08-02 19:10:38 +02004336 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004337 goto theend;
Bram Moolenaar299f3032021-01-08 20:53:09 +01004338 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004339 ++ectx->ec_stack.ga_len;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004340 if (iptr->isn_type == ISN_RETURN_VOID)
4341 {
4342 tv->v_type = VAR_VOID;
4343 tv->vval.v_number = 0;
4344 tv->v_lock = 0;
4345 }
4346 else
4347 {
4348 *tv = *STACK_TV_VAR(0);
4349 ++tv->vval.v_object->obj_refcount;
4350 }
Bram Moolenaar299f3032021-01-08 20:53:09 +01004351 // FALLTHROUGH
4352
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02004353 // return from a :def function call with what is on the stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004354 case ISN_RETURN:
4355 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004356 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01004357 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01004358
4359 if (trystack->ga_len > 0)
4360 trycmd = ((trycmd_T *)trystack->ga_data)
4361 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004362 if (trycmd != NULL
Bram Moolenaar4c137212021-04-19 16:48:48 +02004363 && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004364 {
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01004365 // jump to ":finally" or ":endtry"
4366 if (trycmd->tcd_finally_idx != 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004367 ectx->ec_iidx = trycmd->tcd_finally_idx;
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01004368 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02004369 ectx->ec_iidx = trycmd->tcd_endtry_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004370 trycmd->tcd_return = TRUE;
4371 }
4372 else
Bram Moolenaard032f342020-07-18 18:13:02 +02004373 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004374 }
4375 break;
4376
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004377 // push a partial, a reference to a compiled function
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004378 case ISN_FUNCREF:
4379 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004380 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
4381 ufunc_T *ufunc;
4382 funcref_T *funcref = &iptr->isn_arg.funcref;
4383 funcref_extra_T *extra = funcref->fr_extra;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004384
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004385 if (pt == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004386 goto theend;
Bram Moolenaar35578162021-08-02 19:10:38 +02004387 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02004388 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004389 vim_free(pt);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004390 goto theend;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004391 }
Bram Moolenaar313e4722023-02-08 20:55:27 +00004392 if (extra != NULL && extra->fre_class != NULL)
4393 {
4394 tv = STACK_TV_BOT(-1);
4395 if (tv->v_type != VAR_OBJECT)
4396 {
4397 object_required_error(tv);
4398 vim_free(pt);
4399 goto on_error;
4400 }
4401 object_T *obj = tv->vval.v_object;
4402 class_T *cl = obj->obj_class;
4403
4404 // convert the interface index to the object index
4405 int idx = object_index_from_itf_index(extra->fre_class,
Ernie Rael18143d32023-09-04 22:30:41 +02004406 TRUE, extra->fre_method_idx, cl,
4407 FALSE);
Bram Moolenaar313e4722023-02-08 20:55:27 +00004408 ufunc = cl->class_obj_methods[idx];
4409 }
4410 else if (extra == NULL || extra->fre_func_name == NULL)
Bram Moolenaar38453522021-11-28 22:00:12 +00004411 {
4412 dfunc_T *pt_dfunc = ((dfunc_T *)def_functions.ga_data)
4413 + funcref->fr_dfunc_idx;
4414
4415 ufunc = pt_dfunc->df_ufunc;
4416 }
4417 else
Bram Moolenaar313e4722023-02-08 20:55:27 +00004418 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004419 ufunc = find_func(extra->fre_func_name, FALSE);
Bram Moolenaar313e4722023-02-08 20:55:27 +00004420 }
Bram Moolenaar56acd1f2022-02-18 13:24:52 +00004421 if (ufunc == NULL)
4422 {
4423 SOURCING_LNUM = iptr->isn_lnum;
4424 iemsg("ufunc unexpectedly NULL for FUNCREF");
4425 goto theend;
4426 }
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004427 if (fill_partial_and_closure(pt, ufunc,
Bram Moolenaarcc341812022-09-19 15:54:34 +01004428 extra == NULL ? NULL : &extra->fre_loopvar_info,
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004429 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004430 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004431 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004432 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004433 tv->vval.v_partial = pt;
4434 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02004435 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004436 }
4437 break;
4438
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004439 // Create a global function from a lambda.
4440 case ISN_NEWFUNC:
4441 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004442 newfuncarg_T *arg = iptr->isn_arg.newfunc.nf_arg;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004443
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004444 if (copy_lambda_to_global_func(arg->nfa_lambda,
Bram Moolenaarcc341812022-09-19 15:54:34 +01004445 arg->nfa_global, &arg->nfa_loopvar_info,
4446 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004447 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004448 }
4449 break;
4450
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004451 // List functions
4452 case ISN_DEF:
4453 if (iptr->isn_arg.string == NULL)
4454 list_functions(NULL);
4455 else
4456 {
Bram Moolenaar14336722022-01-08 16:02:59 +00004457 exarg_T ea;
4458 garray_T lines_to_free;
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004459
4460 CLEAR_FIELD(ea);
4461 ea.cmd = ea.arg = iptr->isn_arg.string;
Bram Moolenaar14336722022-01-08 16:02:59 +00004462 ga_init2(&lines_to_free, sizeof(char_u *), 50);
Bram Moolenaard4a9b7f2023-05-23 14:48:42 +01004463 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar554d0312023-01-05 19:59:18 +00004464 define_function(&ea, NULL, &lines_to_free, 0);
Bram Moolenaar14336722022-01-08 16:02:59 +00004465 ga_clear_strings(&lines_to_free);
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004466 }
4467 break;
4468
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004469 // jump if a condition is met
4470 case ISN_JUMP:
4471 {
4472 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004473 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004474 int jump = TRUE;
4475
4476 if (when != JUMP_ALWAYS)
4477 {
4478 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004479 if (when == JUMP_IF_COND_FALSE
Bram Moolenaar13106602020-10-04 16:06:05 +02004480 || when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004481 || when == JUMP_IF_COND_TRUE)
4482 {
4483 SOURCING_LNUM = iptr->isn_lnum;
4484 jump = tv_get_bool_chk(tv, &error);
4485 if (error)
4486 goto on_error;
4487 }
4488 else
4489 jump = tv2bool(tv);
Bram Moolenaarf6ced982022-04-28 12:00:49 +01004490 if (when == JUMP_IF_FALSE || when == JUMP_IF_COND_FALSE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004491 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01004492 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004493 {
4494 // drop the value from the stack
4495 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004496 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004497 }
4498 }
4499 if (jump)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004500 ectx->ec_iidx = iptr->isn_arg.jump.jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004501 }
4502 break;
4503
Bram Moolenaarb46c0832022-09-15 17:19:37 +01004504 // "while": jump to end if a condition is false
4505 case ISN_WHILE:
4506 {
4507 int error = FALSE;
4508 int jump = TRUE;
4509
4510 tv = STACK_TV_BOT(-1);
4511 SOURCING_LNUM = iptr->isn_lnum;
4512 jump = !tv_get_bool_chk(tv, &error);
4513 if (error)
4514 goto on_error;
4515 // drop the value from the stack
4516 clear_tv(tv);
4517 --ectx->ec_stack.ga_len;
4518 if (jump)
4519 ectx->ec_iidx = iptr->isn_arg.whileloop.while_end;
4520
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01004521 // Store the current funcref count, may be used by
Bram Moolenaarcc341812022-09-19 15:54:34 +01004522 // ISN_ENDLOOP later
Bram Moolenaarb46c0832022-09-15 17:19:37 +01004523 tv = STACK_TV_VAR(
4524 iptr->isn_arg.whileloop.while_funcref_idx);
4525 tv->vval.v_number = ectx->ec_funcrefs.ga_len;
4526 }
4527 break;
4528
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02004529 // Jump if an argument with a default value was already set and not
4530 // v:none.
4531 case ISN_JUMP_IF_ARG_SET:
Bram Moolenaar65b0d162022-12-13 18:43:22 +00004532 case ISN_JUMP_IF_ARG_NOT_SET:
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02004533 tv = STACK_TV_VAR(iptr->isn_arg.jumparg.jump_arg_off);
Bram Moolenaar65b0d162022-12-13 18:43:22 +00004534 int arg_set = tv->v_type != VAR_UNKNOWN
4535 && !(tv->v_type == VAR_SPECIAL
4536 && tv->vval.v_number == VVAL_NONE);
4537 if (iptr->isn_type == ISN_JUMP_IF_ARG_SET ? arg_set : !arg_set)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004538 ectx->ec_iidx = iptr->isn_arg.jumparg.jump_where;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02004539 break;
4540
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004541 // top of a for loop
4542 case ISN_FOR:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00004543 if (execute_for(iptr, ectx) == FAIL)
4544 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004545 break;
4546
Bram Moolenaarb46c0832022-09-15 17:19:37 +01004547 // end of a for or while loop
4548 case ISN_ENDLOOP:
4549 if (execute_endloop(iptr, ectx) == FAIL)
4550 goto theend;
4551 break;
4552
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004553 // start of ":try" block
4554 case ISN_TRY:
4555 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01004556 trycmd_T *trycmd = NULL;
4557
Bram Moolenaar35578162021-08-02 19:10:38 +02004558 if (GA_GROW_FAILS(&ectx->ec_trystack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004559 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004560 trycmd = ((trycmd_T *)ectx->ec_trystack.ga_data)
4561 + ectx->ec_trystack.ga_len;
4562 ++ectx->ec_trystack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004563 ++trylevel;
Bram Moolenaar8d4be892021-02-13 18:33:02 +01004564 CLEAR_POINTER(trycmd);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004565 trycmd->tcd_frame_idx = ectx->ec_frame_idx;
4566 trycmd->tcd_stack_len = ectx->ec_stack.ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01004567 trycmd->tcd_catch_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00004568 iptr->isn_arg.tryref.try_ref->try_catch;
Bram Moolenaara91a7132021-03-25 21:12:15 +01004569 trycmd->tcd_finally_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00004570 iptr->isn_arg.tryref.try_ref->try_finally;
Bram Moolenaara91a7132021-03-25 21:12:15 +01004571 trycmd->tcd_endtry_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00004572 iptr->isn_arg.tryref.try_ref->try_endtry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004573 }
4574 break;
4575
4576 case ISN_PUSHEXC:
4577 if (current_exception == NULL)
4578 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004579 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004580 iemsg("Evaluating catch while current_exception is NULL");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004581 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004582 }
Bram Moolenaar35578162021-08-02 19:10:38 +02004583 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004584 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004585 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004586 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004587 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02004588 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004589 tv->vval.v_string = vim_strsave(
4590 (char_u *)current_exception->value);
4591 break;
4592
4593 case ISN_CATCH:
4594 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004595 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar06651632022-04-27 17:54:25 +01004596 trycmd_T *trycmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004597
Bram Moolenaar4c137212021-04-19 16:48:48 +02004598 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar06651632022-04-27 17:54:25 +01004599 trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004600 + trystack->ga_len - 1;
Bram Moolenaar06651632022-04-27 17:54:25 +01004601 trycmd->tcd_caught = TRUE;
4602 trycmd->tcd_did_throw = FALSE;
4603
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004604 did_emsg = got_int = did_throw = FALSE;
Bram Moolenaar1430cee2021-01-17 19:20:32 +01004605 force_abort = need_rethrow = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004606 catch_exception(current_exception);
4607 }
4608 break;
4609
Bram Moolenaarc150c092021-02-13 15:02:46 +01004610 case ISN_TRYCONT:
4611 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004612 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004613 trycont_T *trycont = &iptr->isn_arg.trycont;
4614 int i;
4615 trycmd_T *trycmd;
4616 int iidx = trycont->tct_where;
4617
4618 if (trystack->ga_len < trycont->tct_levels)
4619 {
4620 siemsg("TRYCONT: expected %d levels, found %d",
4621 trycont->tct_levels, trystack->ga_len);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004622 goto theend;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004623 }
4624 // Make :endtry jump to any outer try block and the last
4625 // :endtry inside the loop to the loop start.
4626 for (i = trycont->tct_levels; i > 0; --i)
4627 {
4628 trycmd = ((trycmd_T *)trystack->ga_data)
4629 + trystack->ga_len - i;
Bram Moolenaar2e34c342021-03-14 12:13:33 +01004630 // Add one to tcd_cont to be able to jump to
4631 // instruction with index zero.
4632 trycmd->tcd_cont = iidx + 1;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01004633 iidx = trycmd->tcd_finally_idx == 0
4634 ? trycmd->tcd_endtry_idx : trycmd->tcd_finally_idx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004635 }
4636 // jump to :finally or :endtry of current try statement
Bram Moolenaar4c137212021-04-19 16:48:48 +02004637 ectx->ec_iidx = iidx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004638 }
4639 break;
4640
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01004641 case ISN_FINALLY:
4642 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004643 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01004644 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
4645 + trystack->ga_len - 1;
4646
4647 // Reset the index to avoid a return statement jumps here
4648 // again.
4649 trycmd->tcd_finally_idx = 0;
4650 break;
4651 }
4652
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004653 // end of ":try" block
4654 case ISN_ENDTRY:
4655 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004656 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar06651632022-04-27 17:54:25 +01004657 trycmd_T *trycmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004658
Bram Moolenaar06651632022-04-27 17:54:25 +01004659 --trystack->ga_len;
4660 --trylevel;
4661 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len;
4662 if (trycmd->tcd_did_throw)
4663 did_throw = TRUE;
4664 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004665 {
Bram Moolenaar06651632022-04-27 17:54:25 +01004666 // discard the exception
4667 if (caught_stack == current_exception)
4668 caught_stack = caught_stack->caught;
4669 discard_current_exception();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004670 }
Bram Moolenaar06651632022-04-27 17:54:25 +01004671
4672 if (trycmd->tcd_return)
4673 goto func_return;
4674
4675 while (ectx->ec_stack.ga_len > trycmd->tcd_stack_len)
4676 {
4677 --ectx->ec_stack.ga_len;
4678 clear_tv(STACK_TV_BOT(0));
4679 }
4680 if (trycmd->tcd_cont != 0)
4681 // handling :continue: jump to outer try block or
4682 // start of the loop
4683 ectx->ec_iidx = trycmd->tcd_cont - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004684 }
4685 break;
4686
4687 case ISN_THROW:
Bram Moolenaar8f81b222021-01-14 21:47:06 +01004688 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004689 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02004690
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004691 if (trystack->ga_len == 0 && trylevel == 0 && emsg_silent)
4692 {
Bram Moolenaar3ef2e412023-04-30 18:50:48 +01004693 // Throwing an exception while using "silent!" causes
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004694 // the function to abort but not display an error.
4695 tv = STACK_TV_BOT(-1);
4696 clear_tv(tv);
4697 tv->v_type = VAR_NUMBER;
4698 tv->vval.v_number = 0;
4699 goto done;
4700 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004701 --ectx->ec_stack.ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004702 tv = STACK_TV_BOT(0);
4703 if (tv->vval.v_string == NULL
4704 || *skipwhite(tv->vval.v_string) == NUL)
4705 {
4706 vim_free(tv->vval.v_string);
4707 SOURCING_LNUM = iptr->isn_lnum;
4708 emsg(_(e_throw_with_empty_string));
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004709 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004710 }
4711
4712 // Inside a "catch" we need to first discard the caught
4713 // exception.
4714 if (trystack->ga_len > 0)
4715 {
4716 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
4717 + trystack->ga_len - 1;
4718 if (trycmd->tcd_caught && current_exception != NULL)
4719 {
4720 // discard the exception
4721 if (caught_stack == current_exception)
4722 caught_stack = caught_stack->caught;
4723 discard_current_exception();
4724 trycmd->tcd_caught = FALSE;
4725 }
4726 }
4727
Bram Moolenaar90a57162022-02-12 14:23:17 +00004728 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004729 if (throw_exception(tv->vval.v_string, ET_USER, NULL)
4730 == FAIL)
4731 {
4732 vim_free(tv->vval.v_string);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004733 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004734 }
4735 did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004736 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004737 break;
4738
4739 // compare with special values
4740 case ISN_COMPAREBOOL:
4741 case ISN_COMPARESPECIAL:
4742 {
4743 typval_T *tv1 = STACK_TV_BOT(-2);
4744 typval_T *tv2 = STACK_TV_BOT(-1);
4745 varnumber_T arg1 = tv1->vval.v_number;
4746 varnumber_T arg2 = tv2->vval.v_number;
4747 int res;
4748
Bram Moolenaar06651632022-04-27 17:54:25 +01004749 if (iptr->isn_arg.op.op_type == EXPR_EQUAL)
4750 res = arg1 == arg2;
4751 else
4752 res = arg1 != arg2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004753
Bram Moolenaar4c137212021-04-19 16:48:48 +02004754 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004755 tv1->v_type = VAR_BOOL;
4756 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4757 }
4758 break;
4759
Bram Moolenaar7a222242022-03-01 19:23:24 +00004760 case ISN_COMPARENULL:
4761 {
4762 typval_T *tv1 = STACK_TV_BOT(-2);
4763 typval_T *tv2 = STACK_TV_BOT(-1);
4764 int res;
4765
4766 res = typval_compare_null(tv1, tv2);
4767 if (res == MAYBE)
4768 goto on_error;
4769 if (iptr->isn_arg.op.op_type == EXPR_NEQUAL)
4770 res = !res;
4771 clear_tv(tv1);
4772 clear_tv(tv2);
4773 --ectx->ec_stack.ga_len;
4774 tv1->v_type = VAR_BOOL;
4775 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4776 }
4777 break;
4778
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004779 // Operation with two number arguments
4780 case ISN_OPNR:
4781 case ISN_COMPARENR:
4782 {
4783 typval_T *tv1 = STACK_TV_BOT(-2);
4784 typval_T *tv2 = STACK_TV_BOT(-1);
4785 varnumber_T arg1 = tv1->vval.v_number;
4786 varnumber_T arg2 = tv2->vval.v_number;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004787 varnumber_T res = 0;
4788 int div_zero = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004789
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004790 if (iptr->isn_arg.op.op_type == EXPR_LSHIFT
4791 || iptr->isn_arg.op.op_type == EXPR_RSHIFT)
4792 {
4793 if (arg2 < 0)
4794 {
4795 SOURCING_LNUM = iptr->isn_lnum;
dundargocc57b5bc2022-11-02 13:30:51 +00004796 emsg(_(e_bitshift_ops_must_be_positive));
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004797 goto on_error;
4798 }
4799 }
4800
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004801 switch (iptr->isn_arg.op.op_type)
4802 {
4803 case EXPR_MULT: res = arg1 * arg2; break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004804 case EXPR_DIV: if (arg2 == 0)
4805 div_zero = TRUE;
4806 else
4807 res = arg1 / arg2;
4808 break;
4809 case EXPR_REM: if (arg2 == 0)
4810 div_zero = TRUE;
4811 else
4812 res = arg1 % arg2;
4813 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004814 case EXPR_SUB: res = arg1 - arg2; break;
4815 case EXPR_ADD: res = arg1 + arg2; break;
4816
4817 case EXPR_EQUAL: res = arg1 == arg2; break;
4818 case EXPR_NEQUAL: res = arg1 != arg2; break;
4819 case EXPR_GREATER: res = arg1 > arg2; break;
4820 case EXPR_GEQUAL: res = arg1 >= arg2; break;
4821 case EXPR_SMALLER: res = arg1 < arg2; break;
4822 case EXPR_SEQUAL: res = arg1 <= arg2; break;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004823 case EXPR_LSHIFT: if (arg2 > MAX_LSHIFT_BITS)
4824 res = 0;
4825 else
Bram Moolenaar68e64d22022-05-22 22:07:52 +01004826 res = (uvarnumber_T)arg1 << arg2;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004827 break;
4828 case EXPR_RSHIFT: if (arg2 > MAX_LSHIFT_BITS)
4829 res = 0;
4830 else
Bram Moolenaar338bf582022-05-22 20:16:32 +01004831 res = (uvarnumber_T)arg1 >> arg2;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004832 break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004833 default: break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004834 }
4835
Bram Moolenaar4c137212021-04-19 16:48:48 +02004836 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004837 if (iptr->isn_type == ISN_COMPARENR)
4838 {
4839 tv1->v_type = VAR_BOOL;
4840 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4841 }
4842 else
4843 tv1->vval.v_number = res;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004844 if (div_zero)
4845 {
4846 SOURCING_LNUM = iptr->isn_lnum;
4847 emsg(_(e_divide_by_zero));
4848 goto on_error;
4849 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004850 }
4851 break;
4852
4853 // Computation with two float arguments
4854 case ISN_OPFLOAT:
4855 case ISN_COMPAREFLOAT:
4856 {
4857 typval_T *tv1 = STACK_TV_BOT(-2);
4858 typval_T *tv2 = STACK_TV_BOT(-1);
4859 float_T arg1 = tv1->vval.v_float;
4860 float_T arg2 = tv2->vval.v_float;
4861 float_T res = 0;
4862 int cmp = FALSE;
4863
4864 switch (iptr->isn_arg.op.op_type)
4865 {
4866 case EXPR_MULT: res = arg1 * arg2; break;
4867 case EXPR_DIV: res = arg1 / arg2; break;
4868 case EXPR_SUB: res = arg1 - arg2; break;
4869 case EXPR_ADD: res = arg1 + arg2; break;
4870
4871 case EXPR_EQUAL: cmp = arg1 == arg2; break;
4872 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
4873 case EXPR_GREATER: cmp = arg1 > arg2; break;
4874 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
4875 case EXPR_SMALLER: cmp = arg1 < arg2; break;
4876 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
4877 default: cmp = 0; break;
4878 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004879 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004880 if (iptr->isn_type == ISN_COMPAREFLOAT)
4881 {
4882 tv1->v_type = VAR_BOOL;
4883 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
4884 }
4885 else
4886 tv1->vval.v_float = res;
4887 }
4888 break;
4889
4890 case ISN_COMPARELIST:
Bram Moolenaar265f8112021-12-19 12:33:05 +00004891 case ISN_COMPAREDICT:
4892 case ISN_COMPAREFUNC:
4893 case ISN_COMPARESTRING:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004894 case ISN_COMPAREBLOB:
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00004895 case ISN_COMPARECLASS:
4896 case ISN_COMPAREOBJECT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004897 {
4898 typval_T *tv1 = STACK_TV_BOT(-2);
4899 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar265f8112021-12-19 12:33:05 +00004900 exprtype_T exprtype = iptr->isn_arg.op.op_type;
4901 int ic = iptr->isn_arg.op.op_ic;
4902 int res = FALSE;
4903 int status = OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004904
Bram Moolenaar265f8112021-12-19 12:33:05 +00004905 SOURCING_LNUM = iptr->isn_lnum;
4906 if (iptr->isn_type == ISN_COMPARELIST)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004907 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00004908 status = typval_compare_list(tv1, tv2,
4909 exprtype, ic, &res);
4910 }
4911 else if (iptr->isn_type == ISN_COMPAREDICT)
4912 {
4913 status = typval_compare_dict(tv1, tv2,
4914 exprtype, ic, &res);
4915 }
4916 else if (iptr->isn_type == ISN_COMPAREFUNC)
4917 {
4918 status = typval_compare_func(tv1, tv2,
4919 exprtype, ic, &res);
4920 }
4921 else if (iptr->isn_type == ISN_COMPARESTRING)
4922 {
4923 status = typval_compare_string(tv1, tv2,
4924 exprtype, ic, &res);
4925 }
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00004926 else if (iptr->isn_type == ISN_COMPAREBLOB)
Bram Moolenaar265f8112021-12-19 12:33:05 +00004927 {
4928 status = typval_compare_blob(tv1, tv2, exprtype, &res);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004929 }
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00004930 else if (iptr->isn_type == ISN_COMPARECLASS)
4931 {
Bram Moolenaar03ff0c62023-01-02 20:38:01 +00004932 status = typval_compare_class(tv1, tv2,
4933 exprtype, FALSE, &res);
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00004934 }
4935 else // ISN_COMPAREOBJECT
4936 {
4937 status = typval_compare_object(tv1, tv2,
Bram Moolenaar03ff0c62023-01-02 20:38:01 +00004938 exprtype, FALSE, &res);
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00004939 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004940 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004941 clear_tv(tv1);
4942 clear_tv(tv2);
4943 tv1->v_type = VAR_BOOL;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004944 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4945 if (status == FAIL)
4946 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004947 }
4948 break;
4949
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004950 case ISN_COMPAREANY:
4951 {
4952 typval_T *tv1 = STACK_TV_BOT(-2);
4953 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar657137c2021-01-09 15:45:23 +01004954 exprtype_T exprtype = iptr->isn_arg.op.op_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004955 int ic = iptr->isn_arg.op.op_ic;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004956 int status;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004957
Bram Moolenaareb26f432020-09-14 16:50:05 +02004958 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004959 status = typval_compare(tv1, tv2, exprtype, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004960 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004961 --ectx->ec_stack.ga_len;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004962 if (status == FAIL)
4963 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004964 }
4965 break;
4966
4967 case ISN_ADDLIST:
4968 case ISN_ADDBLOB:
4969 {
4970 typval_T *tv1 = STACK_TV_BOT(-2);
4971 typval_T *tv2 = STACK_TV_BOT(-1);
4972
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004973 // add two lists or blobs
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004974 if (iptr->isn_type == ISN_ADDLIST)
Bram Moolenaar07802042021-09-09 23:01:14 +02004975 {
4976 if (iptr->isn_arg.op.op_type == EXPR_APPEND
4977 && tv1->vval.v_list != NULL)
4978 list_extend(tv1->vval.v_list, tv2->vval.v_list,
4979 NULL);
4980 else
4981 eval_addlist(tv1, tv2);
4982 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004983 else
4984 eval_addblob(tv1, tv2);
4985 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004986 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004987 }
4988 break;
4989
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004990 case ISN_LISTAPPEND:
4991 {
4992 typval_T *tv1 = STACK_TV_BOT(-2);
4993 typval_T *tv2 = STACK_TV_BOT(-1);
4994 list_T *l = tv1->vval.v_list;
4995
4996 // add an item to a list
Bram Moolenaar1f4a3452022-01-01 18:29:21 +00004997 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004998 if (l == NULL)
4999 {
Bram Moolenaar1dcae592020-10-19 19:02:42 +02005000 emsg(_(e_cannot_add_to_null_list));
5001 goto on_error;
5002 }
Bram Moolenaar1f4a3452022-01-01 18:29:21 +00005003 if (value_check_lock(l->lv_lock, NULL, FALSE))
5004 goto on_error;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02005005 if (list_append_tv(l, tv2) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005006 goto theend;
Bram Moolenaar955347c2020-10-19 23:01:46 +02005007 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005008 --ectx->ec_stack.ga_len;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02005009 }
5010 break;
5011
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02005012 case ISN_BLOBAPPEND:
5013 {
5014 typval_T *tv1 = STACK_TV_BOT(-2);
5015 typval_T *tv2 = STACK_TV_BOT(-1);
5016 blob_T *b = tv1->vval.v_blob;
5017 int error = FALSE;
5018 varnumber_T n;
5019
5020 // add a number to a blob
5021 if (b == NULL)
5022 {
5023 SOURCING_LNUM = iptr->isn_lnum;
5024 emsg(_(e_cannot_add_to_null_blob));
5025 goto on_error;
5026 }
5027 n = tv_get_number_chk(tv2, &error);
5028 if (error)
5029 goto on_error;
5030 ga_append(&b->bv_ga, (int)n);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005031 --ectx->ec_stack.ga_len;
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02005032 }
5033 break;
5034
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005035 // Computation with two arguments of unknown type
5036 case ISN_OPANY:
5037 {
5038 typval_T *tv1 = STACK_TV_BOT(-2);
5039 typval_T *tv2 = STACK_TV_BOT(-1);
5040 varnumber_T n1, n2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005041 float_T f1 = 0, f2 = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005042 int error = FALSE;
5043
5044 if (iptr->isn_arg.op.op_type == EXPR_ADD)
5045 {
5046 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
5047 {
5048 eval_addlist(tv1, tv2);
5049 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005050 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005051 break;
5052 }
5053 else if (tv1->v_type == VAR_BLOB
5054 && tv2->v_type == VAR_BLOB)
5055 {
5056 eval_addblob(tv1, tv2);
5057 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005058 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005059 break;
5060 }
5061 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005062 if (tv1->v_type == VAR_FLOAT)
5063 {
5064 f1 = tv1->vval.v_float;
5065 n1 = 0;
5066 }
5067 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005068 {
Bram Moolenaarf665e972020-12-05 19:17:16 +01005069 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005070 n1 = tv_get_number_chk(tv1, &error);
5071 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02005072 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005073 if (tv2->v_type == VAR_FLOAT)
5074 f1 = n1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005075 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005076 if (tv2->v_type == VAR_FLOAT)
5077 {
5078 f2 = tv2->vval.v_float;
5079 n2 = 0;
5080 }
5081 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005082 {
5083 n2 = tv_get_number_chk(tv2, &error);
5084 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02005085 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005086 if (tv1->v_type == VAR_FLOAT)
5087 f2 = n2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005088 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005089 // if there is a float on either side the result is a float
5090 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
5091 {
5092 switch (iptr->isn_arg.op.op_type)
5093 {
5094 case EXPR_MULT: f1 = f1 * f2; break;
5095 case EXPR_DIV: f1 = f1 / f2; break;
5096 case EXPR_SUB: f1 = f1 - f2; break;
5097 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005098 default: SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005099 emsg(_(e_cannot_use_percent_with_float));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02005100 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005101 }
5102 clear_tv(tv1);
5103 clear_tv(tv2);
5104 tv1->v_type = VAR_FLOAT;
5105 tv1->vval.v_float = f1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005106 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005107 }
5108 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005109 {
Bram Moolenaarb1f28572021-01-21 13:03:20 +01005110 int failed = FALSE;
5111
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005112 switch (iptr->isn_arg.op.op_type)
5113 {
5114 case EXPR_MULT: n1 = n1 * n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01005115 case EXPR_DIV: n1 = num_divide(n1, n2, &failed);
5116 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01005117 goto on_error;
5118 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005119 case EXPR_SUB: n1 = n1 - n2; break;
5120 case EXPR_ADD: n1 = n1 + n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01005121 default: n1 = num_modulus(n1, n2, &failed);
5122 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01005123 goto on_error;
5124 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005125 }
5126 clear_tv(tv1);
5127 clear_tv(tv2);
5128 tv1->v_type = VAR_NUMBER;
5129 tv1->vval.v_number = n1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005130 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005131 }
5132 }
5133 break;
5134
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005135 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005136 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005137 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005138 int is_slice = iptr->isn_type == ISN_STRSLICE;
5139 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005140 char_u *res;
5141
5142 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005143 // string slice: string is at stack-3, first index at
5144 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005145 if (is_slice)
5146 {
5147 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005148 n1 = tv->vval.v_number;
5149 }
5150
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005151 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005152 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005153
Bram Moolenaar4c137212021-04-19 16:48:48 +02005154 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005155 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005156 if (is_slice)
5157 // Slice: Select the characters from the string
Bram Moolenaar6601b622021-01-13 21:47:15 +01005158 res = string_slice(tv->vval.v_string, n1, n2, FALSE);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005159 else
5160 // Index: The resulting variable is a string of a
Bram Moolenaar0289a092021-03-14 18:40:19 +01005161 // single character (including composing characters).
5162 // If the index is too big or negative the result is
5163 // empty.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005164 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005165 vim_free(tv->vval.v_string);
5166 tv->vval.v_string = res;
5167 }
5168 break;
5169
5170 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02005171 case ISN_LISTSLICE:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02005172 case ISN_BLOBINDEX:
5173 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005174 {
Bram Moolenaarcfc30232021-04-11 20:26:34 +02005175 int is_slice = iptr->isn_type == ISN_LISTSLICE
5176 || iptr->isn_type == ISN_BLOBSLICE;
5177 int is_blob = iptr->isn_type == ISN_BLOBINDEX
5178 || iptr->isn_type == ISN_BLOBSLICE;
Bram Moolenaared591872020-08-15 22:14:53 +02005179 varnumber_T n1, n2;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02005180 typval_T *val_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005181
5182 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02005183 // list slice: list is at stack-3, indexes at stack-2 and
5184 // stack-1
Bram Moolenaarcfc30232021-04-11 20:26:34 +02005185 // Same for blob.
5186 val_tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005187
5188 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02005189 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005190 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02005191
5192 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005193 {
Bram Moolenaared591872020-08-15 22:14:53 +02005194 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02005195 n1 = tv->vval.v_number;
5196 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005197 }
Bram Moolenaared591872020-08-15 22:14:53 +02005198
Bram Moolenaar4c137212021-04-19 16:48:48 +02005199 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02005200 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02005201 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02005202 if (is_blob)
5203 {
5204 if (blob_slice_or_index(val_tv->vval.v_blob, is_slice,
5205 n1, n2, FALSE, tv) == FAIL)
5206 goto on_error;
5207 }
5208 else
5209 {
5210 if (list_slice_or_index(val_tv->vval.v_list, is_slice,
5211 n1, n2, FALSE, tv, TRUE) == FAIL)
5212 goto on_error;
5213 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005214 }
5215 break;
5216
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005217 case ISN_ANYINDEX:
5218 case ISN_ANYSLICE:
5219 {
5220 int is_slice = iptr->isn_type == ISN_ANYSLICE;
5221 typval_T *var1, *var2;
5222 int res;
5223
5224 // index: composite is at stack-2, index at stack-1
5225 // slice: composite is at stack-3, indexes at stack-2 and
5226 // stack-1
5227 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02005228 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005229 if (check_can_index(tv, TRUE, TRUE) == FAIL)
5230 goto on_error;
5231 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
5232 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
Bram Moolenaar6601b622021-01-13 21:47:15 +01005233 res = eval_index_inner(tv, is_slice, var1, var2,
5234 FALSE, NULL, -1, TRUE);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005235 clear_tv(var1);
5236 if (is_slice)
5237 clear_tv(var2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005238 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005239 if (res == FAIL)
5240 goto on_error;
5241 }
5242 break;
5243
Bram Moolenaar9af78762020-06-16 11:34:42 +02005244 case ISN_SLICE:
5245 {
5246 list_T *list;
5247 int count = iptr->isn_arg.number;
5248
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02005249 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02005250 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02005251 list = tv->vval.v_list;
5252
5253 // no error for short list, expect it to be checked earlier
5254 if (list != NULL && list->lv_len >= count)
5255 {
5256 list_T *newlist = list_slice(list,
5257 count, list->lv_len - 1);
5258
5259 if (newlist != NULL)
5260 {
5261 list_unref(list);
5262 tv->vval.v_list = newlist;
5263 ++newlist->lv_refcount;
5264 }
5265 }
5266 }
5267 break;
5268
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005269 case ISN_GETITEM:
5270 {
5271 listitem_T *li;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02005272 getitem_T *gi = &iptr->isn_arg.getitem;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005273
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02005274 // Get list item: list is at stack-1, push item.
5275 // List type and length is checked for when compiling.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02005276 tv = STACK_TV_BOT(-1 - gi->gi_with_op);
5277 li = list_find(tv->vval.v_list, gi->gi_index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005278
Bram Moolenaar35578162021-08-02 19:10:38 +02005279 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005280 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005281 ++ectx->ec_stack.ga_len;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005282 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
Bram Moolenaarf785aa12021-02-11 21:19:34 +01005283
5284 // Useful when used in unpack assignment. Reset at
5285 // ISN_DROP.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02005286 ectx->ec_where.wt_index = gi->gi_index + 1;
LemonBoyc5d27442023-08-19 13:02:35 +02005287 ectx->ec_where.wt_kind = WT_VARIABLE;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005288 }
5289 break;
5290
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005291 case ISN_MEMBER:
5292 {
5293 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005294 char_u *key;
5295 dictitem_T *di;
5296
5297 // dict member: dict is at stack-2, key at stack-1
5298 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02005299 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005300 dict = tv->vval.v_dict;
5301
5302 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02005303 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005304 key = tv->vval.v_string;
Bram Moolenaar086fc9a2020-10-30 18:33:02 +01005305 if (key == NULL)
5306 key = (char_u *)"";
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02005307
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005308 if ((di = dict_find(dict, key, -1)) == NULL)
5309 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005310 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00005311 semsg(_(e_key_not_present_in_dictionary_str), key);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01005312
5313 // If :silent! is used we will continue, make sure the
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005314 // stack contents makes sense and the dict stack is
5315 // updated.
Bram Moolenaar4029cab2020-12-05 18:13:27 +01005316 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005317 --ectx->ec_stack.ga_len;
Bram Moolenaar4029cab2020-12-05 18:13:27 +01005318 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005319 (void) dict_stack_save(tv);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01005320 tv->v_type = VAR_NUMBER;
5321 tv->vval.v_number = 0;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01005322 goto on_fatal_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005323 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005324 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005325 --ectx->ec_stack.ga_len;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005326 // Put the dict used on the dict stack, it might be used by
5327 // a dict function later.
Bram Moolenaar50788ef2020-07-05 16:51:26 +02005328 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005329 if (dict_stack_save(tv) == FAIL)
5330 goto on_fatal_error;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02005331 copy_tv(&di->di_tv, tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005332 }
5333 break;
5334
5335 // dict member with string key
5336 case ISN_STRINGMEMBER:
5337 {
5338 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005339 dictitem_T *di;
5340
5341 tv = STACK_TV_BOT(-1);
5342 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
5343 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005344 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005345 emsg(_(e_dictionary_required));
Bram Moolenaard032f342020-07-18 18:13:02 +02005346 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005347 }
5348 dict = tv->vval.v_dict;
5349
5350 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
5351 == NULL)
5352 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005353 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00005354 semsg(_(e_key_not_present_in_dictionary_str),
Bram Moolenaar381692b2022-02-02 20:01:27 +00005355 iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02005356 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005357 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005358 // Put the dict used on the dict stack, it might be used by
5359 // a dict function later.
5360 if (dict_stack_save(tv) == FAIL)
5361 goto on_fatal_error;
5362
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005363 copy_tv(&di->di_tv, tv);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005364 }
5365 break;
5366
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005367 case ISN_GET_OBJ_MEMBER:
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00005368 case ISN_GET_ITF_MEMBER:
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005369 {
5370 tv = STACK_TV_BOT(-1);
5371 if (tv->v_type != VAR_OBJECT)
5372 {
5373 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaard0200c82023-01-28 15:19:40 +00005374 object_required_error(tv);
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005375 goto on_error;
5376 }
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00005377
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005378 object_T *obj = tv->vval.v_object;
Bram Moolenaarc3f971f2023-03-02 17:38:33 +00005379 if (obj == NULL)
5380 {
5381 SOURCING_LNUM = iptr->isn_lnum;
5382 emsg(_(e_using_null_object));
5383 goto on_error;
5384 }
5385
Ernie Rael18143d32023-09-04 22:30:41 +02005386 int is_static = iptr->isn_arg.classmember.cm_static;
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00005387 int idx;
5388 if (iptr->isn_type == ISN_GET_OBJ_MEMBER)
Ernie Rael18143d32023-09-04 22:30:41 +02005389 idx = iptr->isn_arg.classmember.cm_idx;
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00005390 else
5391 {
5392 idx = iptr->isn_arg.classmember.cm_idx;
5393 // convert the interface index to the object index
5394 idx = object_index_from_itf_index(
Ernie Rael18143d32023-09-04 22:30:41 +02005395 iptr->isn_arg.classmember.cm_class,
5396 FALSE, idx, obj->obj_class, is_static);
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00005397 }
5398
Ernie Rael18143d32023-09-04 22:30:41 +02005399 // The members are located right after the object struct.
5400 typval_T *mtv;
5401 if (is_static)
5402 mtv = &obj->obj_class->class_members_tv[idx];
5403 else
5404 mtv = ((typval_T *)(obj + 1)) + idx;
Bram Moolenaar590162c2022-12-24 21:24:06 +00005405 copy_tv(mtv, tv);
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005406
5407 // Unreference the object after getting the member, it may
5408 // be freed.
5409 object_unref(obj);
5410 }
5411 break;
5412
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005413 case ISN_STORE_THIS:
5414 {
5415 int idx = iptr->isn_arg.number;
5416 object_T *obj = STACK_TV_VAR(0)->vval.v_object;
5417 // the members are located right after the object struct
5418 typval_T *mtv = ((typval_T *)(obj + 1)) + idx;
5419 clear_tv(mtv);
5420 *mtv = *STACK_TV_BOT(-1);
5421 --ectx->ec_stack.ga_len;
5422 }
5423 break;
5424
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005425 case ISN_CLEARDICT:
5426 dict_stack_drop();
5427 break;
5428
5429 case ISN_USEDICT:
5430 {
5431 typval_T *dict_tv = dict_stack_get_tv();
5432
5433 // Turn "dict.Func" into a partial for "Func" bound to
5434 // "dict". Don't do this when "Func" is already a partial
5435 // that was bound explicitly (pt_auto is FALSE).
5436 tv = STACK_TV_BOT(-1);
5437 if (dict_tv != NULL
5438 && dict_tv->v_type == VAR_DICT
5439 && dict_tv->vval.v_dict != NULL
5440 && (tv->v_type == VAR_FUNC
5441 || (tv->v_type == VAR_PARTIAL
5442 && (tv->vval.v_partial->pt_auto
5443 || tv->vval.v_partial->pt_dict == NULL))))
5444 dict_tv->vval.v_dict =
5445 make_partial(dict_tv->vval.v_dict, tv);
5446 dict_stack_drop();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005447 }
5448 break;
5449
5450 case ISN_NEGATENR:
5451 tv = STACK_TV_BOT(-1);
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00005452 // CHECKTYPE should have checked the variable type
Bram Moolenaarc58164c2020-03-29 18:40:30 +02005453 if (tv->v_type == VAR_FLOAT)
5454 tv->vval.v_float = -tv->vval.v_float;
5455 else
Bram Moolenaarc58164c2020-03-29 18:40:30 +02005456 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005457 break;
5458
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005459 case ISN_CHECKTYPE:
5460 {
5461 checktype_T *ct = &iptr->isn_arg.type;
Bram Moolenaarb1040dc2022-05-18 11:00:48 +01005462 int r;
LemonBoyc5d27442023-08-19 13:02:35 +02005463 where_T where = WHERE_INIT;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005464
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01005465 tv = STACK_TV_BOT((int)ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02005466 SOURCING_LNUM = iptr->isn_lnum;
LemonBoyc5d27442023-08-19 13:02:35 +02005467 if (ct->ct_arg_idx > 0)
5468 {
5469 where.wt_index = ct->ct_arg_idx;
5470 where.wt_kind = ct->ct_is_var ? WT_VARIABLE : WT_ARGUMENT;
LemonBoyc5d27442023-08-19 13:02:35 +02005471 }
LemonBoyf244b2f2023-08-19 16:02:04 +02005472 where.wt_func_name = ectx->ec_where.wt_func_name;
LemonBoyc5d27442023-08-19 13:02:35 +02005473 r = check_typval_type(ct->ct_type, tv, where);
Bram Moolenaarb1040dc2022-05-18 11:00:48 +01005474 if (r == FAIL)
5475 goto on_error;
Bram Moolenaar5e654232020-09-16 15:22:00 +02005476
5477 // number 0 is FALSE, number 1 is TRUE
5478 if (tv->v_type == VAR_NUMBER
5479 && ct->ct_type->tt_type == VAR_BOOL
5480 && (tv->vval.v_number == 0
5481 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005482 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02005483 tv->v_type = VAR_BOOL;
5484 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02005485 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005486 }
5487 }
5488 break;
5489
Bram Moolenaar9af78762020-06-16 11:34:42 +02005490 case ISN_CHECKLEN:
5491 {
5492 int min_len = iptr->isn_arg.checklen.cl_min_len;
5493 list_T *list = NULL;
5494
5495 tv = STACK_TV_BOT(-1);
5496 if (tv->v_type == VAR_LIST)
5497 list = tv->vval.v_list;
5498 if (list == NULL || list->lv_len < min_len
5499 || (list->lv_len > min_len
5500 && !iptr->isn_arg.checklen.cl_more_OK))
5501 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005502 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005503 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02005504 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02005505 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02005506 }
5507 }
5508 break;
5509
Bram Moolenaaraa210a32021-01-02 15:41:03 +01005510 case ISN_SETTYPE:
Bram Moolenaar381692b2022-02-02 20:01:27 +00005511 set_tv_type(STACK_TV_BOT(-1), iptr->isn_arg.type.ct_type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01005512 break;
5513
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005514 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005515 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005516 {
5517 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005518 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005519
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005520 if (iptr->isn_type == ISN_2BOOL)
5521 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005522 tv = STACK_TV_BOT(iptr->isn_arg.tobool.offset);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005523 n = tv2bool(tv);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005524 if (iptr->isn_arg.tobool.invert)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005525 n = !n;
5526 }
5527 else
5528 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005529 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005530 SOURCING_LNUM = iptr->isn_lnum;
5531 n = tv_get_bool_chk(tv, &error);
5532 if (error)
5533 goto on_error;
5534 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005535 clear_tv(tv);
5536 tv->v_type = VAR_BOOL;
5537 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
5538 }
5539 break;
5540
5541 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02005542 case ISN_2STRING_ANY:
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01005543 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005544 if (do_2string(STACK_TV_BOT(iptr->isn_arg.tostring.offset),
5545 iptr->isn_type == ISN_2STRING_ANY,
5546 iptr->isn_arg.tostring.tolerant) == FAIL)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01005547 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005548 break;
5549
Bram Moolenaar08597872020-12-10 19:43:40 +01005550 case ISN_RANGE:
5551 {
5552 exarg_T ea;
5553 char *errormsg;
5554
Bram Moolenaarece0b872021-01-08 20:40:45 +01005555 ea.line2 = 0;
Bram Moolenaard1510ee2021-01-04 16:15:58 +01005556 ea.addr_count = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01005557 ea.addr_type = ADDR_LINES;
5558 ea.cmd = iptr->isn_arg.string;
Bram Moolenaarece0b872021-01-08 20:40:45 +01005559 ea.skip = FALSE;
Bram Moolenaar08597872020-12-10 19:43:40 +01005560 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaarece0b872021-01-08 20:40:45 +01005561 goto on_error;
Bram Moolenaara28639e2021-01-19 22:48:09 +01005562
Bram Moolenaar35578162021-08-02 19:10:38 +02005563 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005564 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005565 ++ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01005566 tv = STACK_TV_BOT(-1);
5567 tv->v_type = VAR_NUMBER;
5568 tv->v_lock = 0;
Bram Moolenaar06651632022-04-27 17:54:25 +01005569 tv->vval.v_number = ea.line2;
Bram Moolenaar08597872020-12-10 19:43:40 +01005570 }
5571 break;
5572
Bram Moolenaarc3516f72020-09-08 22:45:35 +02005573 case ISN_PUT:
5574 {
5575 int regname = iptr->isn_arg.put.put_regname;
5576 linenr_T lnum = iptr->isn_arg.put.put_lnum;
5577 char_u *expr = NULL;
5578 int dir = FORWARD;
5579
Bram Moolenaar08597872020-12-10 19:43:40 +01005580 if (lnum < -2)
5581 {
5582 // line number was put on the stack by ISN_RANGE
5583 tv = STACK_TV_BOT(-1);
5584 curwin->w_cursor.lnum = tv->vval.v_number;
5585 if (lnum == LNUM_VARIABLE_RANGE_ABOVE)
5586 dir = BACKWARD;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005587 --ectx->ec_stack.ga_len;
Bram Moolenaar08597872020-12-10 19:43:40 +01005588 }
5589 else if (lnum == -2)
Bram Moolenaarc3516f72020-09-08 22:45:35 +02005590 // :put! above cursor
5591 dir = BACKWARD;
5592 else if (lnum >= 0)
Bram Moolenaar4e713ba2022-02-07 15:31:37 +00005593 {
5594 curwin->w_cursor.lnum = lnum;
5595 if (lnum == 0)
5596 // check_cursor() below will move to line 1
5597 dir = BACKWARD;
5598 }
Bram Moolenaara28639e2021-01-19 22:48:09 +01005599
5600 if (regname == '=')
5601 {
5602 tv = STACK_TV_BOT(-1);
5603 if (tv->v_type == VAR_STRING)
5604 expr = tv->vval.v_string;
5605 else
5606 {
5607 expr = typval2string(tv, TRUE); // allocates value
5608 clear_tv(tv);
5609 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005610 --ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01005611 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02005612 check_cursor();
5613 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
5614 vim_free(expr);
5615 }
5616 break;
5617
Bram Moolenaar02194d22020-10-24 23:08:38 +02005618 case ISN_CMDMOD:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005619 ectx->ec_funclocal.floc_save_cmdmod = cmdmod;
5620 ectx->ec_funclocal.floc_restore_cmdmod = TRUE;
5621 ectx->ec_funclocal.floc_restore_cmdmod_stacklen =
5622 ectx->ec_stack.ga_len;
Bram Moolenaar02194d22020-10-24 23:08:38 +02005623 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
5624 apply_cmdmod(&cmdmod);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02005625 break;
5626
Bram Moolenaar02194d22020-10-24 23:08:38 +02005627 case ISN_CMDMOD_REV:
5628 // filter regprog is owned by the instruction, don't free it
5629 cmdmod.cmod_filter_regmatch.regprog = NULL;
5630 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005631 cmdmod = ectx->ec_funclocal.floc_save_cmdmod;
5632 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02005633 break;
5634
Bram Moolenaar792f7862020-11-23 08:31:18 +01005635 case ISN_UNPACK:
5636 {
5637 int count = iptr->isn_arg.unpack.unp_count;
5638 int semicolon = iptr->isn_arg.unpack.unp_semicolon;
5639 list_T *l;
5640 listitem_T *li;
5641 int i;
5642
5643 // Check there is a valid list to unpack.
5644 tv = STACK_TV_BOT(-1);
5645 if (tv->v_type != VAR_LIST)
5646 {
5647 SOURCING_LNUM = iptr->isn_lnum;
5648 emsg(_(e_for_argument_must_be_sequence_of_lists));
5649 goto on_error;
5650 }
5651 l = tv->vval.v_list;
5652 if (l == NULL
5653 || l->lv_len < (semicolon ? count - 1 : count))
5654 {
5655 SOURCING_LNUM = iptr->isn_lnum;
5656 emsg(_(e_list_value_does_not_have_enough_items));
5657 goto on_error;
5658 }
5659 else if (!semicolon && l->lv_len > count)
5660 {
5661 SOURCING_LNUM = iptr->isn_lnum;
5662 emsg(_(e_list_value_has_more_items_than_targets));
5663 goto on_error;
5664 }
5665
5666 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar35578162021-08-02 19:10:38 +02005667 if (GA_GROW_FAILS(&ectx->ec_stack, count - 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005668 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005669 ectx->ec_stack.ga_len += count - 1;
Bram Moolenaar792f7862020-11-23 08:31:18 +01005670
5671 // Variable after semicolon gets a list with the remaining
5672 // items.
5673 if (semicolon)
5674 {
5675 list_T *rem_list =
5676 list_alloc_with_items(l->lv_len - count + 1);
5677
5678 if (rem_list == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005679 goto theend;
Bram Moolenaar792f7862020-11-23 08:31:18 +01005680 tv = STACK_TV_BOT(-count);
5681 tv->vval.v_list = rem_list;
5682 ++rem_list->lv_refcount;
5683 tv->v_lock = 0;
5684 li = l->lv_first;
5685 for (i = 0; i < count - 1; ++i)
5686 li = li->li_next;
5687 for (i = 0; li != NULL; ++i)
5688 {
Bram Moolenaar61efa162022-03-18 13:10:48 +00005689 typval_T tvcopy;
5690
5691 copy_tv(&li->li_tv, &tvcopy);
5692 list_set_item(rem_list, i, &tvcopy);
Bram Moolenaar792f7862020-11-23 08:31:18 +01005693 li = li->li_next;
5694 }
5695 --count;
5696 }
5697
5698 // Produce the values in reverse order, first item last.
5699 li = l->lv_first;
5700 for (i = 0; i < count; ++i)
5701 {
5702 tv = STACK_TV_BOT(-i - 1);
5703 copy_tv(&li->li_tv, tv);
5704 li = li->li_next;
5705 }
5706
5707 list_unref(l);
5708 }
5709 break;
5710
Bram Moolenaarb2049902021-01-24 12:53:53 +01005711 case ISN_PROF_START:
5712 case ISN_PROF_END:
5713 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01005714#ifdef FEAT_PROFILE
Bram Moolenaarca16c602022-09-06 18:57:08 +01005715 funccall_T cookie;
5716 ufunc_T *cur_ufunc =
Bram Moolenaarb2049902021-01-24 12:53:53 +01005717 (((dfunc_T *)def_functions.ga_data)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02005718 + ectx->ec_dfunc_idx)->df_ufunc;
Bram Moolenaarb2049902021-01-24 12:53:53 +01005719
Bram Moolenaarca16c602022-09-06 18:57:08 +01005720 cookie.fc_func = cur_ufunc;
Bram Moolenaarb2049902021-01-24 12:53:53 +01005721 if (iptr->isn_type == ISN_PROF_START)
5722 {
5723 func_line_start(&cookie, iptr->isn_lnum);
5724 // if we get here the instruction is executed
5725 func_line_exec(&cookie);
5726 }
5727 else
5728 func_line_end(&cookie);
Bram Moolenaarf002a412021-01-24 13:34:18 +01005729#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01005730 }
5731 break;
5732
Bram Moolenaare99d4222021-06-13 14:01:26 +02005733 case ISN_DEBUG:
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02005734 handle_debug(iptr, ectx);
Bram Moolenaare99d4222021-06-13 14:01:26 +02005735 break;
5736
Bram Moolenaar389df252020-07-09 21:20:47 +02005737 case ISN_SHUFFLE:
5738 {
Bram Moolenaar792f7862020-11-23 08:31:18 +01005739 typval_T tmp_tv;
5740 int item = iptr->isn_arg.shuffle.shfl_item;
5741 int up = iptr->isn_arg.shuffle.shfl_up;
Bram Moolenaar389df252020-07-09 21:20:47 +02005742
5743 tmp_tv = *STACK_TV_BOT(-item);
5744 for ( ; up > 0 && item > 1; --up)
5745 {
5746 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
5747 --item;
5748 }
5749 *STACK_TV_BOT(-item) = tmp_tv;
5750 }
5751 break;
5752
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005753 case ISN_DROP:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005754 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005755 clear_tv(STACK_TV_BOT(0));
LemonBoyc5d27442023-08-19 13:02:35 +02005756 ectx->ec_where = (where_T)WHERE_INIT;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005757 break;
5758 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02005759 continue;
5760
Bram Moolenaard032f342020-07-18 18:13:02 +02005761func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02005762 // Restore previous function. If the frame pointer is where we started
5763 // then there is none and we are done.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005764 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02005765 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02005766
Bram Moolenaar4c137212021-04-19 16:48:48 +02005767 if (func_return(ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02005768 // only fails when out of memory
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005769 goto theend;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02005770 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02005771
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02005772on_error:
Bram Moolenaaraf0df472020-12-02 20:51:22 +01005773 // Jump here for an error that does not require aborting execution.
Bram Moolenaar56602ba2020-12-05 21:22:08 +01005774 // If "emsg_silent" is set then ignore the error, unless it was set
5775 // when calling the function.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005776 if (did_emsg_cumul + did_emsg == ectx->ec_did_emsg_before
Bram Moolenaar56602ba2020-12-05 21:22:08 +01005777 && emsg_silent && did_emsg_def == 0)
Bram Moolenaarf9041332021-01-21 19:41:16 +01005778 {
5779 // If a sequence of instructions causes an error while ":silent!"
5780 // was used, restore the stack length and jump ahead to restoring
5781 // the cmdmod.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005782 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaarf9041332021-01-21 19:41:16 +01005783 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005784 while (ectx->ec_stack.ga_len
5785 > ectx->ec_funclocal.floc_restore_cmdmod_stacklen)
Bram Moolenaarf9041332021-01-21 19:41:16 +01005786 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005787 --ectx->ec_stack.ga_len;
Bram Moolenaarf9041332021-01-21 19:41:16 +01005788 clear_tv(STACK_TV_BOT(0));
5789 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005790 while (ectx->ec_instr[ectx->ec_iidx].isn_type != ISN_CMDMOD_REV)
5791 ++ectx->ec_iidx;
Bram Moolenaarf9041332021-01-21 19:41:16 +01005792 }
Bram Moolenaarcd030c42020-10-30 21:49:40 +01005793 continue;
Bram Moolenaarf9041332021-01-21 19:41:16 +01005794 }
Bram Moolenaaraf0df472020-12-02 20:51:22 +01005795on_fatal_error:
5796 // Jump here for an error that messes up the stack.
Bram Moolenaar171fb922020-10-28 16:54:47 +01005797 // If we are not inside a try-catch started here, abort execution.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005798 if (trylevel <= ectx->ec_trylevel_at_start)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005799 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005800 }
5801
5802done:
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005803 ret = OK;
5804theend:
Bram Moolenaar58779852022-09-06 18:31:14 +01005805 may_invoke_defer_funcs(ectx);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01005806
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005807 dict_stack_clear(dict_stack_len_at_start);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005808 ectx->ec_trylevel_at_start = save_trylevel_at_start;
5809 return ret;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005810}
5811
5812/*
Bram Moolenaar806a2732022-09-04 15:40:36 +01005813 * Execute the instructions from a VAR_INSTR typval and put the result in
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005814 * "rettv".
5815 * Return OK or FAIL.
5816 */
5817 int
5818exe_typval_instr(typval_T *tv, typval_T *rettv)
5819{
5820 ectx_T *ectx = tv->vval.v_instr->instr_ectx;
5821 isn_T *save_instr = ectx->ec_instr;
5822 int save_iidx = ectx->ec_iidx;
5823 int res;
5824
LemonBoyf3b48952022-05-05 13:53:03 +01005825 // Initialize rettv so that it is safe for caller to invoke clear_tv(rettv)
5826 // even when the compilation fails.
5827 rettv->v_type = VAR_UNKNOWN;
5828
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005829 ectx->ec_instr = tv->vval.v_instr->instr_instr;
5830 res = exec_instructions(ectx);
5831 if (res == OK)
5832 {
5833 *rettv = *STACK_TV_BOT(-1);
5834 --ectx->ec_stack.ga_len;
5835 }
5836
5837 ectx->ec_instr = save_instr;
5838 ectx->ec_iidx = save_iidx;
5839
5840 return res;
5841}
5842
5843/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02005844 * Execute the instructions from an ISN_SUBSTITUTE command, which are in
5845 * "substitute_instr".
5846 */
5847 char_u *
5848exe_substitute_instr(void)
5849{
5850 ectx_T *ectx = substitute_instr->subs_ectx;
5851 isn_T *save_instr = ectx->ec_instr;
5852 int save_iidx = ectx->ec_iidx;
5853 char_u *res;
5854
5855 ectx->ec_instr = substitute_instr->subs_instr;
5856 if (exec_instructions(ectx) == OK)
Bram Moolenaar90193e62021-04-04 20:49:50 +02005857 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005858 typval_T *tv = STACK_TV_BOT(-1);
5859
Bram Moolenaar27523602021-06-05 21:36:19 +02005860 res = typval2string(tv, TRUE);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005861 --ectx->ec_stack.ga_len;
5862 clear_tv(tv);
Bram Moolenaar90193e62021-04-04 20:49:50 +02005863 }
5864 else
5865 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005866 substitute_instr->subs_status = FAIL;
5867 res = vim_strsave((char_u *)"");
Bram Moolenaar90193e62021-04-04 20:49:50 +02005868 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005869
Bram Moolenaar4c137212021-04-19 16:48:48 +02005870 ectx->ec_instr = save_instr;
5871 ectx->ec_iidx = save_iidx;
5872
5873 return res;
5874}
5875
5876/*
5877 * Call a "def" function from old Vim script.
5878 * Return OK or FAIL.
5879 */
5880 int
5881call_def_function(
5882 ufunc_T *ufunc,
5883 int argc_arg, // nr of arguments
5884 typval_T *argv, // arguments
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005885 int flags, // DEF_ flags
Bram Moolenaar4c137212021-04-19 16:48:48 +02005886 partial_T *partial, // optional partial for context
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005887 object_T *object, // object, e.g. for this.Func()
Bram Moolenaar58779852022-09-06 18:31:14 +01005888 funccall_T *funccal,
Bram Moolenaar4c137212021-04-19 16:48:48 +02005889 typval_T *rettv) // return value
5890{
5891 ectx_T ectx; // execution context
5892 int argc = argc_arg;
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005893 int partial_argc = partial == NULL
5894 || (flags & DEF_USE_PT_ARGV) == 0
5895 ? 0 : partial->pt_argc;
5896 int total_argc = argc + partial_argc;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005897 typval_T *tv;
5898 int idx;
5899 int ret = FAIL;
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005900 int defcount = ufunc->uf_args.ga_len - total_argc;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005901 sctx_T save_current_sctx = current_sctx;
5902 int did_emsg_before = did_emsg_cumul + did_emsg;
5903 int save_suppress_errthrow = suppress_errthrow;
5904 msglist_T **saved_msg_list = NULL;
5905 msglist_T *private_msg_list = NULL;
5906 int save_emsg_silent_def = emsg_silent_def;
5907 int save_did_emsg_def = did_emsg_def;
5908 int orig_funcdepth;
Bram Moolenaare99d4222021-06-13 14:01:26 +02005909 int orig_nesting_level = ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005910
5911// Get pointer to item in the stack.
5912#undef STACK_TV
5913#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
5914
5915// Get pointer to item at the bottom of the stack, -1 is the bottom.
5916#undef STACK_TV_BOT
5917#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
5918
5919// Get pointer to a local variable on the stack. Negative for arguments.
5920#undef STACK_TV_VAR
5921#define STACK_TV_VAR(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_frame_idx + STACK_FRAME_SIZE + idx)
5922
5923 if (ufunc->uf_def_status == UF_NOT_COMPILED
5924 || ufunc->uf_def_status == UF_COMPILE_ERROR
Bram Moolenaar139575d2022-03-15 19:29:30 +00005925 || (func_needs_compiling(ufunc, get_compile_type(ufunc))
5926 && compile_def_function(ufunc, FALSE,
5927 get_compile_type(ufunc), NULL) == FAIL))
Bram Moolenaar4c137212021-04-19 16:48:48 +02005928 {
5929 if (did_emsg_cumul + did_emsg == did_emsg_before)
5930 semsg(_(e_function_is_not_compiled_str),
5931 printable_func_name(ufunc));
5932 return FAIL;
5933 }
5934
5935 {
5936 // Check the function was really compiled.
5937 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5938 + ufunc->uf_dfunc_idx;
Bram Moolenaarcc341812022-09-19 15:54:34 +01005939 if (dfunc->df_ufunc == NULL)
5940 {
5941 semsg(_(e_function_was_deleted_str), printable_func_name(ufunc));
5942 return FAIL;
5943 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005944 if (INSTRUCTIONS(dfunc) == NULL)
5945 {
5946 iemsg("using call_def_function() on not compiled function");
5947 return FAIL;
5948 }
5949 }
5950
5951 // If depth of calling is getting too high, don't execute the function.
5952 orig_funcdepth = funcdepth_get();
5953 if (funcdepth_increment() == FAIL)
5954 return FAIL;
5955
5956 CLEAR_FIELD(ectx);
5957 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
5958 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
Bram Moolenaar35578162021-08-02 19:10:38 +02005959 if (GA_GROW_FAILS(&ectx.ec_stack, 20))
Bram Moolenaar4c137212021-04-19 16:48:48 +02005960 {
5961 funcdepth_decrement();
5962 return FAIL;
5963 }
5964 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
5965 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
5966 ectx.ec_did_emsg_before = did_emsg_before;
Bram Moolenaare99d4222021-06-13 14:01:26 +02005967 ++ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005968
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005969 idx = total_argc - ufunc->uf_args.ga_len;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005970 if (idx > 0 && ufunc->uf_va_name == NULL)
5971 {
Matvey Tarasovd14bb1a2022-06-29 13:18:27 +01005972 semsg(NGETTEXT(e_one_argument_too_many, e_nr_arguments_too_many,
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005973 idx), idx);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005974 goto failed_early;
5975 }
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005976 idx = total_argc - ufunc->uf_args.ga_len + ufunc->uf_def_args.ga_len;
Bram Moolenaarc6d71532021-06-05 18:49:38 +02005977 if (idx < 0)
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02005978 {
Matvey Tarasovd14bb1a2022-06-29 13:18:27 +01005979 semsg(NGETTEXT(e_one_argument_too_few, e_nr_arguments_too_few,
Bram Moolenaar98aff652022-09-06 21:02:35 +01005980 -idx), -idx);
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02005981 goto failed_early;
5982 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005983
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005984 // Put values from the partial and arguments on the stack, but no more than
5985 // what the function expects. A lambda can be called with more arguments
5986 // than it uses.
5987 for (idx = 0; idx < total_argc
Bram Moolenaar4c137212021-04-19 16:48:48 +02005988 && (ufunc->uf_va_name != NULL || idx < ufunc->uf_args.ga_len);
5989 ++idx)
5990 {
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005991 int argv_idx = idx - partial_argc;
5992
5993 tv = idx < partial_argc ? partial->pt_argv + idx : argv + argv_idx;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005994 if (idx >= ufunc->uf_args.ga_len - ufunc->uf_def_args.ga_len
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005995 && tv->v_type == VAR_SPECIAL
5996 && tv->vval.v_number == VVAL_NONE)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005997 {
5998 // Use the default value.
5999 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
6000 }
6001 else
6002 {
Bram Moolenaar47bba532023-01-20 18:49:46 +00006003 int done = FALSE;
6004 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len)
6005 {
6006 type_T *expected = ufunc->uf_arg_types[idx];
6007 if (expected->tt_type == VAR_FLOAT && tv->v_type == VAR_NUMBER)
6008 {
6009 // When a float is expected and a number was given, convert
6010 // the value.
6011 STACK_TV_BOT(0)->v_type = VAR_FLOAT;
6012 STACK_TV_BOT(0)->v_lock = 0;
6013 STACK_TV_BOT(0)->vval.v_float = tv->vval.v_number;
6014 done = TRUE;
6015 }
6016 else if (check_typval_arg_type(expected, tv,
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01006017 NULL, argv_idx + 1) == FAIL)
Bram Moolenaar47bba532023-01-20 18:49:46 +00006018 goto failed_early;
6019 }
6020 if (!done)
6021 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006022 }
6023 ++ectx.ec_stack.ga_len;
6024 }
6025
6026 // Turn varargs into a list. Empty list if no args.
6027 if (ufunc->uf_va_name != NULL)
6028 {
6029 int vararg_count = argc - ufunc->uf_args.ga_len;
6030
6031 if (vararg_count < 0)
6032 vararg_count = 0;
6033 else
6034 argc -= vararg_count;
6035 if (exe_newlist(vararg_count, &ectx) == FAIL)
6036 goto failed_early;
6037
6038 // Check the type of the list items.
6039 tv = STACK_TV_BOT(-1);
6040 if (ufunc->uf_va_type != NULL
6041 && ufunc->uf_va_type != &t_list_any
6042 && ufunc->uf_va_type->tt_member != &t_any
6043 && tv->vval.v_list != NULL)
6044 {
6045 type_T *expected = ufunc->uf_va_type->tt_member;
6046 listitem_T *li = tv->vval.v_list->lv_first;
6047
6048 for (idx = 0; idx < vararg_count; ++idx)
6049 {
6050 if (check_typval_arg_type(expected, &li->li_tv,
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02006051 NULL, argc + idx + 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02006052 goto failed_early;
6053 li = li->li_next;
6054 }
6055 }
6056
6057 if (defcount > 0)
6058 // Move varargs list to below missing default arguments.
6059 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
6060 --ectx.ec_stack.ga_len;
6061 }
6062
6063 // Make space for omitted arguments, will store default value below.
6064 // Any varargs list goes after them.
6065 if (defcount > 0)
6066 for (idx = 0; idx < defcount; ++idx)
6067 {
6068 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
6069 ++ectx.ec_stack.ga_len;
6070 }
6071 if (ufunc->uf_va_name != NULL)
6072 ++ectx.ec_stack.ga_len;
6073
6074 // Frame pointer points to just after arguments.
6075 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
6076 ectx.ec_initial_frame_idx = ectx.ec_frame_idx;
6077
6078 {
6079 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6080 + ufunc->uf_dfunc_idx;
6081 ufunc_T *base_ufunc = dfunc->df_ufunc;
6082
6083 // "uf_partial" is on the ufunc that "df_ufunc" points to, as is done
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006084 // by copy_lambda_to_global_func().
Bram Moolenaar4c137212021-04-19 16:48:48 +02006085 if (partial != NULL || base_ufunc->uf_partial != NULL)
6086 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006087 ectx.ec_outer_ref = ALLOC_CLEAR_ONE(outer_ref_T);
6088 if (ectx.ec_outer_ref == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02006089 goto failed_early;
6090 if (partial != NULL)
6091 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00006092 outer_T *outer = get_pt_outer(partial);
6093
Bram Moolenaar6d313be2022-09-22 16:36:25 +01006094 if (outer->out_stack == NULL && outer->out_loop_size == 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02006095 {
Bram Moolenaar6d313be2022-09-22 16:36:25 +01006096 // no stack was set
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00006097 if (current_ectx != NULL)
6098 {
6099 if (current_ectx->ec_outer_ref != NULL
6100 && current_ectx->ec_outer_ref->or_outer != NULL)
6101 ectx.ec_outer_ref->or_outer =
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006102 current_ectx->ec_outer_ref->or_outer;
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00006103 }
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01006104 // else: should there be an error here?
Bram Moolenaar4c137212021-04-19 16:48:48 +02006105 }
6106 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006107 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00006108 ectx.ec_outer_ref->or_outer = outer;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006109 ++partial->pt_refcount;
6110 ectx.ec_outer_ref->or_partial = partial;
6111 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006112 }
6113 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006114 {
6115 ectx.ec_outer_ref->or_outer = &base_ufunc->uf_partial->pt_outer;
6116 ++base_ufunc->uf_partial->pt_refcount;
6117 ectx.ec_outer_ref->or_partial = base_ufunc->uf_partial;
6118 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006119 }
6120 }
6121
6122 // dummy frame entries
6123 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
6124 {
6125 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
6126 ++ectx.ec_stack.ga_len;
6127 }
6128
6129 {
6130 // Reserve space for local variables and any closure reference count.
6131 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6132 + ufunc->uf_dfunc_idx;
6133
Bram Moolenaar5cd64792021-12-25 18:23:24 +00006134 // Initialize variables to zero. That avoids having to generate
6135 // initializing instructions for "var nr: number", "var x: any", etc.
Bram Moolenaar4c137212021-04-19 16:48:48 +02006136 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaar5cd64792021-12-25 18:23:24 +00006137 {
6138 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
6139 STACK_TV_VAR(idx)->vval.v_number = 0;
6140 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006141 ectx.ec_stack.ga_len += dfunc->df_varcount;
Bram Moolenaarffdaca92022-12-09 21:41:48 +00006142
6143 if (object != NULL)
6144 {
6145 // the object is always the variable at index zero
6146 tv = STACK_TV_VAR(0);
6147 tv->v_type = VAR_OBJECT;
6148 tv->vval.v_object = object;
6149 }
6150
Bram Moolenaar4c137212021-04-19 16:48:48 +02006151 if (dfunc->df_has_closure)
6152 {
Bram Moolenaarcc341812022-09-19 15:54:34 +01006153 // Initialize the variable that counts how many closures were
6154 // created. This is used in handle_closure_in_use().
Bram Moolenaar4c137212021-04-19 16:48:48 +02006155 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
6156 STACK_TV_VAR(idx)->vval.v_number = 0;
6157 ++ectx.ec_stack.ga_len;
6158 }
6159
6160 ectx.ec_instr = INSTRUCTIONS(dfunc);
6161 }
6162
Bram Moolenaar58779852022-09-06 18:31:14 +01006163 // Store the execution context in funccal, used by invoke_all_defer().
6164 if (funccal != NULL)
6165 funccal->fc_ectx = &ectx;
6166
Bram Moolenaar4c137212021-04-19 16:48:48 +02006167 // Following errors are in the function, not the caller.
6168 // Commands behave like vim9script.
6169 estack_push_ufunc(ufunc, 1);
6170 current_sctx = ufunc->uf_script_ctx;
6171 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
6172
6173 // Use a specific location for storing error messages to be converted to an
6174 // exception.
6175 saved_msg_list = msg_list;
6176 msg_list = &private_msg_list;
6177
6178 // Do turn errors into exceptions.
6179 suppress_errthrow = FALSE;
6180
6181 // Do not delete the function while executing it.
6182 ++ufunc->uf_calls;
6183
6184 // When ":silent!" was used before calling then we still abort the
6185 // function. If ":silent!" is used in the function then we don't.
6186 emsg_silent_def = emsg_silent;
6187 did_emsg_def = 0;
6188
LemonBoyc5d27442023-08-19 13:02:35 +02006189 ectx.ec_where = (where_T)WHERE_INIT;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006190
Bram Moolenaar5800c792022-09-22 17:34:01 +01006191 /*
6192 * Execute the instructions until done.
6193 */
Bram Moolenaar4c137212021-04-19 16:48:48 +02006194 ret = exec_instructions(&ectx);
6195 if (ret == OK)
6196 {
6197 // function finished, get result from the stack.
6198 if (ufunc->uf_ret_type == &t_void)
6199 {
6200 rettv->v_type = VAR_VOID;
6201 }
6202 else
6203 {
6204 tv = STACK_TV_BOT(-1);
6205 *rettv = *tv;
6206 tv->v_type = VAR_UNKNOWN;
6207 }
6208 }
6209
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01006210 // When failed need to unwind the call stack.
Bram Moolenaar58779852022-09-06 18:31:14 +01006211 unwind_def_callstack(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02006212
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02006213 // Deal with any remaining closures, they may be in use somewhere.
6214 if (ectx.ec_funcrefs.ga_len > 0)
Bram Moolenaarf112f302020-12-20 17:47:52 +01006215 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02006216 handle_closure_in_use(&ectx, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00006217 ga_clear(&ectx.ec_funcrefs);
Bram Moolenaarf112f302020-12-20 17:47:52 +01006218 }
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02006219
Bram Moolenaaree8580e2020-08-28 17:19:07 +02006220 estack_pop();
6221 current_sctx = save_current_sctx;
6222
Bram Moolenaar2336c372021-12-06 15:06:54 +00006223 if (--ufunc->uf_calls <= 0 && ufunc->uf_refcount <= 0)
6224 // Function was unreferenced while being used, free it now.
6225 func_clear_free(ufunc, FALSE);
Bram Moolenaarc970e422021-03-17 15:03:04 +01006226
Bram Moolenaar352134b2020-10-17 22:04:08 +02006227 if (*msg_list != NULL && saved_msg_list != NULL)
6228 {
6229 msglist_T **plist = saved_msg_list;
6230
6231 // Append entries from the current msg_list (uncaught exceptions) to
6232 // the saved msg_list.
6233 while (*plist != NULL)
6234 plist = &(*plist)->next;
6235
6236 *plist = *msg_list;
6237 }
6238 msg_list = saved_msg_list;
6239
Bram Moolenaar4c137212021-04-19 16:48:48 +02006240 if (ectx.ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar02194d22020-10-24 23:08:38 +02006241 {
6242 cmdmod.cmod_filter_regmatch.regprog = NULL;
6243 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006244 cmdmod = ectx.ec_funclocal.floc_save_cmdmod;
Bram Moolenaar02194d22020-10-24 23:08:38 +02006245 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +01006246 emsg_silent_def = save_emsg_silent_def;
6247 did_emsg_def += save_did_emsg_def;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02006248
Bram Moolenaaree8580e2020-08-28 17:19:07 +02006249failed_early:
Bram Moolenaar0d1f55c2022-04-05 17:30:29 +01006250 // Free all arguments and local variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006251 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
Bram Moolenaar1936c762022-09-28 15:19:10 +01006252 {
6253 tv = STACK_TV(idx);
6254 if (tv->v_type != VAR_NUMBER && tv->v_type != VAR_UNKNOWN)
6255 clear_tv(tv);
6256 }
Bram Moolenaare99d4222021-06-13 14:01:26 +02006257 ex_nesting_level = orig_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02006258
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006259 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01006260 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006261 if (ectx.ec_outer_ref != NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +01006262 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006263 if (ectx.ec_outer_ref->or_outer_allocated)
6264 vim_free(ectx.ec_outer_ref->or_outer);
6265 partial_unref(ectx.ec_outer_ref->or_partial);
6266 vim_free(ectx.ec_outer_ref);
Bram Moolenaar0186e582021-01-10 18:33:11 +01006267 }
6268
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02006269 // Not sure if this is necessary.
6270 suppress_errthrow = save_suppress_errthrow;
6271
Bram Moolenaarb5841b92021-07-15 18:09:53 +02006272 if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before
6273 && !need_rethrow)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006274 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02006275 printable_func_name(ufunc));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01006276 funcdepth_restore(orig_funcdepth);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006277 return ret;
6278}
6279
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006280/*
Bram Moolenaar58779852022-09-06 18:31:14 +01006281 * Called when a def function has finished (possibly failed).
6282 * Invoke all the function returns to clean up and invoke deferred functions,
6283 * except the toplevel one.
6284 */
6285 void
6286unwind_def_callstack(ectx_T *ectx)
6287{
6288 while (ectx->ec_frame_idx != ectx->ec_initial_frame_idx)
6289 func_return(ectx);
6290}
6291
6292/*
dundargocc57b5bc2022-11-02 13:30:51 +00006293 * Invoke any deferred functions for the top function in "ectx".
Bram Moolenaar58779852022-09-06 18:31:14 +01006294 */
6295 void
6296may_invoke_defer_funcs(ectx_T *ectx)
6297{
6298 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
6299
6300 if (dfunc->df_defer_var_idx > 0)
6301 invoke_defer_funcs(ectx);
6302}
6303
6304/*
Bram Moolenaarcc341812022-09-19 15:54:34 +01006305 * Return loopvarinfo in a printable form in allocated memory.
6306 */
6307 static char_u *
6308printable_loopvarinfo(loopvarinfo_T *lvi)
6309{
6310 garray_T ga;
6311 int depth;
6312
6313 ga_init2(&ga, 1, 100);
6314 for (depth = 0; depth < lvi->lvi_depth; ++depth)
6315 {
6316 if (ga_grow(&ga, 50) == FAIL)
6317 break;
6318 if (lvi->lvi_loop[depth].var_idx == 0)
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +01006319 STRCPY((char *)ga.ga_data + ga.ga_len, " -");
Bram Moolenaarcc341812022-09-19 15:54:34 +01006320 else
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +01006321 vim_snprintf((char *)ga.ga_data + ga.ga_len, 50, " $%d-$%d",
Bram Moolenaarcc341812022-09-19 15:54:34 +01006322 lvi->lvi_loop[depth].var_idx,
6323 lvi->lvi_loop[depth].var_idx
6324 + lvi->lvi_loop[depth].var_count - 1);
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +01006325 ga.ga_len = (int)STRLEN(ga.ga_data);
Bram Moolenaarcc341812022-09-19 15:54:34 +01006326 }
6327 return ga.ga_data;
6328}
6329
6330/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02006331 * List instructions "instr" up to "instr_count" or until ISN_FINISH.
6332 * "ufunc" has the source lines, NULL for the instructions of ISN_SUBSTITUTE.
6333 * "pfx" is prefixed to every line.
6334 */
6335 static void
6336list_instructions(char *pfx, isn_T *instr, int instr_count, ufunc_T *ufunc)
6337{
6338 int line_idx = 0;
6339 int prev_current = 0;
6340 int current;
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02006341 int def_arg_idx = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006342
6343 for (current = 0; current < instr_count; ++current)
6344 {
6345 isn_T *iptr = &instr[current];
6346 char *line;
6347
6348 if (ufunc != NULL)
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02006349 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02006350 while (line_idx < iptr->isn_lnum
6351 && line_idx < ufunc->uf_lines.ga_len)
6352 {
6353 if (current > prev_current)
6354 {
6355 msg_puts("\n\n");
6356 prev_current = current;
6357 }
6358 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
6359 if (line != NULL)
6360 msg(line);
6361 }
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02006362 if (iptr->isn_type == ISN_JUMP_IF_ARG_SET)
6363 {
6364 int first_def_arg = ufunc->uf_args.ga_len
6365 - ufunc->uf_def_args.ga_len;
6366
6367 if (def_arg_idx > 0)
6368 msg_puts("\n\n");
6369 msg_start();
6370 msg_puts(" ");
6371 msg_puts(((char **)(ufunc->uf_args.ga_data))[
6372 first_def_arg + def_arg_idx]);
6373 msg_puts(" = ");
6374 msg_puts(((char **)(ufunc->uf_def_args.ga_data))[def_arg_idx++]);
6375 msg_clr_eos();
6376 msg_end();
6377 }
6378 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006379
6380 switch (iptr->isn_type)
6381 {
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006382 case ISN_CONSTRUCT:
6383 smsg("%s%4d NEW %s size %d", pfx, current,
6384 iptr->isn_arg.construct.construct_class->class_name,
6385 (int)iptr->isn_arg.construct.construct_size);
6386 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006387 case ISN_EXEC:
6388 smsg("%s%4d EXEC %s", pfx, current, iptr->isn_arg.string);
6389 break;
Bram Moolenaar20677332021-06-06 17:02:53 +02006390 case ISN_EXEC_SPLIT:
6391 smsg("%s%4d EXEC_SPLIT %s", pfx, current, iptr->isn_arg.string);
6392 break;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00006393 case ISN_EXECRANGE:
6394 smsg("%s%4d EXECRANGE %s", pfx, current, iptr->isn_arg.string);
6395 break;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02006396 case ISN_LEGACY_EVAL:
6397 smsg("%s%4d EVAL legacy %s", pfx, current,
6398 iptr->isn_arg.string);
6399 break;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02006400 case ISN_REDIRSTART:
6401 smsg("%s%4d REDIR", pfx, current);
6402 break;
6403 case ISN_REDIREND:
6404 smsg("%s%4d REDIR END%s", pfx, current,
6405 iptr->isn_arg.number ? " append" : "");
6406 break;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006407 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02006408#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006409 smsg("%s%4d CEXPR pre %s", pfx, current,
6410 cexpr_get_auname(iptr->isn_arg.number));
Bram Moolenaarb7c97812021-05-05 22:51:39 +02006411#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006412 break;
6413 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02006414#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006415 {
6416 cexprref_T *cer = iptr->isn_arg.cexpr.cexpr_ref;
6417
6418 smsg("%s%4d CEXPR core %s%s \"%s\"", pfx, current,
6419 cexpr_get_auname(cer->cer_cmdidx),
6420 cer->cer_forceit ? "!" : "",
6421 cer->cer_cmdline);
6422 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02006423#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006424 break;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006425 case ISN_INSTR:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006426 smsg("%s%4d INSTR", pfx, current);
6427 list_instructions(" ", iptr->isn_arg.instr, INT_MAX, NULL);
6428 msg(" -------------");
6429 break;
6430 case ISN_SOURCE:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006431 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006432 scriptitem_T *si = SCRIPT_ITEM(iptr->isn_arg.number);
6433
6434 smsg("%s%4d SOURCE %s", pfx, current, si->sn_name);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006435 }
6436 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006437 case ISN_SUBSTITUTE:
6438 {
6439 subs_T *subs = &iptr->isn_arg.subs;
6440
6441 smsg("%s%4d SUBSTITUTE %s", pfx, current, subs->subs_cmd);
6442 list_instructions(" ", subs->subs_instr, INT_MAX, NULL);
6443 msg(" -------------");
6444 }
6445 break;
6446 case ISN_EXECCONCAT:
6447 smsg("%s%4d EXECCONCAT %lld", pfx, current,
6448 (varnumber_T)iptr->isn_arg.number);
6449 break;
6450 case ISN_ECHO:
6451 {
6452 echo_T *echo = &iptr->isn_arg.echo;
6453
6454 smsg("%s%4d %s %d", pfx, current,
6455 echo->echo_with_white ? "ECHO" : "ECHON",
6456 echo->echo_count);
6457 }
6458 break;
6459 case ISN_EXECUTE:
6460 smsg("%s%4d EXECUTE %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02006461 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006462 break;
6463 case ISN_ECHOMSG:
6464 smsg("%s%4d ECHOMSG %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02006465 (varnumber_T)(iptr->isn_arg.number));
6466 break;
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01006467 case ISN_ECHOWINDOW:
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01006468 if (iptr->isn_arg.echowin.ewin_time > 0)
6469 smsg("%s%4d ECHOWINDOW %d (%ld sec)", pfx, current,
6470 iptr->isn_arg.echowin.ewin_count,
6471 iptr->isn_arg.echowin.ewin_time);
6472 else
6473 smsg("%s%4d ECHOWINDOW %d", pfx, current,
6474 iptr->isn_arg.echowin.ewin_count);
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01006475 break;
Bram Moolenaar7de62622021-08-07 15:05:47 +02006476 case ISN_ECHOCONSOLE:
6477 smsg("%s%4d ECHOCONSOLE %lld", pfx, current,
6478 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006479 break;
6480 case ISN_ECHOERR:
6481 smsg("%s%4d ECHOERR %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02006482 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006483 break;
6484 case ISN_LOAD:
6485 {
6486 if (iptr->isn_arg.number < 0)
6487 smsg("%s%4d LOAD arg[%lld]", pfx, current,
6488 (varnumber_T)(iptr->isn_arg.number
6489 + STACK_FRAME_SIZE));
6490 else
6491 smsg("%s%4d LOAD $%lld", pfx, current,
6492 (varnumber_T)(iptr->isn_arg.number));
6493 }
6494 break;
6495 case ISN_LOADOUTER:
6496 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006497 isn_outer_T *outer = &iptr->isn_arg.outer;
6498
6499 if (outer->outer_idx < 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02006500 smsg("%s%4d LOADOUTER level %d arg[%d]", pfx, current,
Bram Moolenaarcc341812022-09-19 15:54:34 +01006501 outer->outer_depth,
6502 outer->outer_idx + STACK_FRAME_SIZE);
6503 else if (outer->outer_depth < 0)
6504 smsg("%s%4d LOADOUTER $%d in loop level %d",
6505 pfx, current,
6506 outer->outer_idx,
6507 -outer->outer_depth);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006508 else
6509 smsg("%s%4d LOADOUTER level %d $%d", pfx, current,
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006510 outer->outer_depth,
6511 outer->outer_idx);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006512 }
6513 break;
6514 case ISN_LOADV:
6515 smsg("%s%4d LOADV v:%s", pfx, current,
6516 get_vim_var_name(iptr->isn_arg.number));
6517 break;
6518 case ISN_LOADSCRIPT:
6519 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006520 scriptref_T *sref = iptr->isn_arg.script.scriptref;
6521 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
6522 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006523
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006524 sv = get_script_svar(sref, -1);
6525 if (sv == NULL)
6526 smsg("%s%4d LOADSCRIPT [deleted] from %s",
6527 pfx, current, si->sn_name);
6528 else
6529 smsg("%s%4d LOADSCRIPT %s-%d from %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02006530 sv->sv_name,
6531 sref->sref_idx,
6532 si->sn_name);
6533 }
6534 break;
6535 case ISN_LOADS:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006536 case ISN_LOADEXPORT:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006537 {
6538 scriptitem_T *si = SCRIPT_ITEM(
6539 iptr->isn_arg.loadstore.ls_sid);
6540
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006541 smsg("%s%4d %s s:%s from %s", pfx, current,
6542 iptr->isn_type == ISN_LOADS ? "LOADS"
6543 : "LOADEXPORT",
6544 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006545 }
6546 break;
6547 case ISN_LOADAUTO:
6548 smsg("%s%4d LOADAUTO %s", pfx, current, iptr->isn_arg.string);
6549 break;
6550 case ISN_LOADG:
6551 smsg("%s%4d LOADG g:%s", pfx, current, iptr->isn_arg.string);
6552 break;
6553 case ISN_LOADB:
6554 smsg("%s%4d LOADB b:%s", pfx, current, iptr->isn_arg.string);
6555 break;
6556 case ISN_LOADW:
6557 smsg("%s%4d LOADW w:%s", pfx, current, iptr->isn_arg.string);
6558 break;
6559 case ISN_LOADT:
6560 smsg("%s%4d LOADT t:%s", pfx, current, iptr->isn_arg.string);
6561 break;
6562 case ISN_LOADGDICT:
6563 smsg("%s%4d LOAD g:", pfx, current);
6564 break;
6565 case ISN_LOADBDICT:
6566 smsg("%s%4d LOAD b:", pfx, current);
6567 break;
6568 case ISN_LOADWDICT:
6569 smsg("%s%4d LOAD w:", pfx, current);
6570 break;
6571 case ISN_LOADTDICT:
6572 smsg("%s%4d LOAD t:", pfx, current);
6573 break;
6574 case ISN_LOADOPT:
6575 smsg("%s%4d LOADOPT %s", pfx, current, iptr->isn_arg.string);
6576 break;
6577 case ISN_LOADENV:
6578 smsg("%s%4d LOADENV %s", pfx, current, iptr->isn_arg.string);
6579 break;
6580 case ISN_LOADREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006581 smsg("%s%4d LOADREG @%c", pfx, current,
6582 (int)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006583 break;
6584
6585 case ISN_STORE:
6586 if (iptr->isn_arg.number < 0)
6587 smsg("%s%4d STORE arg[%lld]", pfx, current,
6588 iptr->isn_arg.number + STACK_FRAME_SIZE);
6589 else
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006590 smsg("%s%4d STORE $%lld", pfx, current,
6591 iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006592 break;
6593 case ISN_STOREOUTER:
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006594 {
6595 isn_outer_T *outer = &iptr->isn_arg.outer;
6596
6597 if (outer->outer_depth == OUTER_LOOP_DEPTH)
6598 smsg("%s%4d STOREOUTER level 1 $%d in loop",
6599 pfx, current, outer->outer_idx);
6600 else
6601 smsg("%s%4d STOREOUTER level %d $%d", pfx, current,
6602 outer->outer_depth, outer->outer_idx);
6603 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006604 break;
6605 case ISN_STOREV:
6606 smsg("%s%4d STOREV v:%s", pfx, current,
6607 get_vim_var_name(iptr->isn_arg.number));
6608 break;
6609 case ISN_STOREAUTO:
6610 smsg("%s%4d STOREAUTO %s", pfx, current, iptr->isn_arg.string);
6611 break;
6612 case ISN_STOREG:
6613 smsg("%s%4d STOREG %s", pfx, current, iptr->isn_arg.string);
6614 break;
6615 case ISN_STOREB:
6616 smsg("%s%4d STOREB %s", pfx, current, iptr->isn_arg.string);
6617 break;
6618 case ISN_STOREW:
6619 smsg("%s%4d STOREW %s", pfx, current, iptr->isn_arg.string);
6620 break;
6621 case ISN_STORET:
6622 smsg("%s%4d STORET %s", pfx, current, iptr->isn_arg.string);
6623 break;
6624 case ISN_STORES:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006625 case ISN_STOREEXPORT:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006626 {
6627 scriptitem_T *si = SCRIPT_ITEM(
6628 iptr->isn_arg.loadstore.ls_sid);
6629
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006630 smsg("%s%4d %s %s in %s", pfx, current,
6631 iptr->isn_type == ISN_STORES
6632 ? "STORES" : "STOREEXPORT",
Bram Moolenaar4c137212021-04-19 16:48:48 +02006633 iptr->isn_arg.loadstore.ls_name, si->sn_name);
6634 }
6635 break;
6636 case ISN_STORESCRIPT:
6637 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006638 scriptref_T *sref = iptr->isn_arg.script.scriptref;
6639 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
6640 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006641
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006642 sv = get_script_svar(sref, -1);
6643 if (sv == NULL)
6644 smsg("%s%4d STORESCRIPT [deleted] in %s",
6645 pfx, current, si->sn_name);
6646 else
6647 smsg("%s%4d STORESCRIPT %s-%d in %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02006648 sv->sv_name,
6649 sref->sref_idx,
6650 si->sn_name);
6651 }
6652 break;
6653 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00006654 case ISN_STOREFUNCOPT:
6655 smsg("%s%4d %s &%s", pfx, current,
6656 iptr->isn_type == ISN_STOREOPT ? "STOREOPT" : "STOREFUNCOPT",
Bram Moolenaar4c137212021-04-19 16:48:48 +02006657 iptr->isn_arg.storeopt.so_name);
6658 break;
6659 case ISN_STOREENV:
6660 smsg("%s%4d STOREENV $%s", pfx, current, iptr->isn_arg.string);
6661 break;
6662 case ISN_STOREREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006663 smsg("%s%4d STOREREG @%c", pfx, current,
6664 (int)iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006665 break;
6666 case ISN_STORENR:
6667 smsg("%s%4d STORE %lld in $%d", pfx, current,
6668 iptr->isn_arg.storenr.stnr_val,
6669 iptr->isn_arg.storenr.stnr_idx);
6670 break;
6671
6672 case ISN_STOREINDEX:
6673 smsg("%s%4d STOREINDEX %s", pfx, current,
Bram Moolenaarf7d1c6e2023-01-16 20:47:57 +00006674 vartype_name(iptr->isn_arg.storeindex.si_vartype));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006675 break;
6676
6677 case ISN_STORERANGE:
6678 smsg("%s%4d STORERANGE", pfx, current);
6679 break;
6680
Bram Moolenaard505d172022-12-18 21:42:55 +00006681 case ISN_LOAD_CLASSMEMBER:
6682 case ISN_STORE_CLASSMEMBER:
6683 {
6684 class_T *cl = iptr->isn_arg.classmember.cm_class;
6685 int idx = iptr->isn_arg.classmember.cm_idx;
6686 ocmember_T *ocm = &cl->class_class_members[idx];
6687 smsg("%s%4d %s CLASSMEMBER %s.%s", pfx, current,
6688 iptr->isn_type == ISN_LOAD_CLASSMEMBER
6689 ? "LOAD" : "STORE",
6690 cl->class_name, ocm->ocm_name);
6691 }
6692 break;
6693
Bram Moolenaar4c137212021-04-19 16:48:48 +02006694 // constants
6695 case ISN_PUSHNR:
6696 smsg("%s%4d PUSHNR %lld", pfx, current,
6697 (varnumber_T)(iptr->isn_arg.number));
6698 break;
6699 case ISN_PUSHBOOL:
6700 case ISN_PUSHSPEC:
6701 smsg("%s%4d PUSH %s", pfx, current,
6702 get_var_special_name(iptr->isn_arg.number));
6703 break;
6704 case ISN_PUSHF:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006705 smsg("%s%4d PUSHF %g", pfx, current, iptr->isn_arg.fnumber);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006706 break;
6707 case ISN_PUSHS:
6708 smsg("%s%4d PUSHS \"%s\"", pfx, current, iptr->isn_arg.string);
6709 break;
6710 case ISN_PUSHBLOB:
6711 {
6712 char_u *r;
6713 char_u numbuf[NUMBUFLEN];
6714 char_u *tofree;
6715
6716 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
6717 smsg("%s%4d PUSHBLOB %s", pfx, current, r);
6718 vim_free(tofree);
6719 }
6720 break;
6721 case ISN_PUSHFUNC:
6722 {
6723 char *name = (char *)iptr->isn_arg.string;
6724
6725 smsg("%s%4d PUSHFUNC \"%s\"", pfx, current,
6726 name == NULL ? "[none]" : name);
6727 }
6728 break;
6729 case ISN_PUSHCHANNEL:
6730#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar397a87a2022-03-20 21:14:15 +00006731 smsg("%s%4d PUSHCHANNEL 0", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006732#endif
6733 break;
6734 case ISN_PUSHJOB:
6735#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar397a87a2022-03-20 21:14:15 +00006736 smsg("%s%4d PUSHJOB \"no process\"", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006737#endif
6738 break;
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00006739 case ISN_PUSHOBJ:
6740 smsg("%s%4d PUSHOBJ null", pfx, current);
6741 break;
6742 case ISN_PUSHCLASS:
6743 smsg("%s%4d PUSHCLASS %s", pfx, current,
Bram Moolenaar30a84472023-02-27 08:07:14 +00006744 iptr->isn_arg.classarg == NULL ? "null"
6745 : (char *)iptr->isn_arg.classarg->class_name);
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00006746 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006747 case ISN_PUSHEXC:
6748 smsg("%s%4d PUSH v:exception", pfx, current);
6749 break;
Bram Moolenaar06b77222022-01-25 15:51:56 +00006750 case ISN_AUTOLOAD:
6751 smsg("%s%4d AUTOLOAD %s", pfx, current, iptr->isn_arg.string);
6752 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006753 case ISN_UNLET:
6754 smsg("%s%4d UNLET%s %s", pfx, current,
6755 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
6756 iptr->isn_arg.unlet.ul_name);
6757 break;
6758 case ISN_UNLETENV:
6759 smsg("%s%4d UNLETENV%s $%s", pfx, current,
6760 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
6761 iptr->isn_arg.unlet.ul_name);
6762 break;
6763 case ISN_UNLETINDEX:
6764 smsg("%s%4d UNLETINDEX", pfx, current);
6765 break;
6766 case ISN_UNLETRANGE:
6767 smsg("%s%4d UNLETRANGE", pfx, current);
6768 break;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02006769 case ISN_LOCKUNLOCK:
6770 smsg("%s%4d LOCKUNLOCK %s", pfx, current, iptr->isn_arg.string);
6771 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006772 case ISN_LOCKCONST:
6773 smsg("%s%4d LOCKCONST", pfx, current);
6774 break;
6775 case ISN_NEWLIST:
6776 smsg("%s%4d NEWLIST size %lld", pfx, current,
Bram Moolenaar1936c762022-09-28 15:19:10 +01006777 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006778 break;
6779 case ISN_NEWDICT:
6780 smsg("%s%4d NEWDICT size %lld", pfx, current,
Bram Moolenaar1936c762022-09-28 15:19:10 +01006781 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006782 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00006783 case ISN_NEWPARTIAL:
6784 smsg("%s%4d NEWPARTIAL", pfx, current);
6785 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006786
6787 // function call
6788 case ISN_BCALL:
6789 {
6790 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
6791
6792 smsg("%s%4d BCALL %s(argc %d)", pfx, current,
6793 internal_func_name(cbfunc->cbf_idx),
6794 cbfunc->cbf_argcount);
6795 }
6796 break;
6797 case ISN_DCALL:
6798 {
6799 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
6800 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
6801 + cdfunc->cdf_idx;
6802
6803 smsg("%s%4d DCALL %s(argc %d)", pfx, current,
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006804 printable_func_name(df->df_ufunc),
6805 cdfunc->cdf_argcount);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006806 }
6807 break;
Bram Moolenaard0200c82023-01-28 15:19:40 +00006808 case ISN_METHODCALL:
6809 {
6810 cmfunc_T *mfunc = iptr->isn_arg.mfunc;
6811
6812 smsg("%s%4d METHODCALL %s.%s(argc %d)", pfx, current,
6813 mfunc->cmf_itf->class_name,
6814 mfunc->cmf_itf->class_obj_methods[
6815 mfunc->cmf_idx]->uf_name,
6816 mfunc->cmf_argcount);
6817 }
6818 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006819 case ISN_UCALL:
6820 {
6821 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
6822
6823 smsg("%s%4d UCALL %s(argc %d)", pfx, current,
6824 cufunc->cuf_name, cufunc->cuf_argcount);
6825 }
6826 break;
6827 case ISN_PCALL:
6828 {
6829 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
6830
6831 smsg("%s%4d PCALL%s (argc %d)", pfx, current,
6832 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
6833 }
6834 break;
6835 case ISN_PCALL_END:
6836 smsg("%s%4d PCALL end", pfx, current);
6837 break;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006838 case ISN_DEFER:
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00006839 case ISN_DEFEROBJ:
6840 smsg("%s%4d %s %d args", pfx, current,
6841 iptr->isn_type == ISN_DEFER ? "DEFER" : "DEFEROBJ",
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006842 (int)iptr->isn_arg.defer.defer_argcount);
6843 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006844 case ISN_RETURN:
6845 smsg("%s%4d RETURN", pfx, current);
6846 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02006847 case ISN_RETURN_VOID:
6848 smsg("%s%4d RETURN void", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006849 break;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006850 case ISN_RETURN_OBJECT:
6851 smsg("%s%4d RETURN object", pfx, current);
6852 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006853 case ISN_FUNCREF:
6854 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006855 funcref_T *funcref = &iptr->isn_arg.funcref;
6856 funcref_extra_T *extra = funcref->fr_extra;
6857 char_u *name;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006858
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006859 if (extra == NULL || extra->fre_func_name == NULL)
Bram Moolenaar38453522021-11-28 22:00:12 +00006860 {
6861 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
6862 + funcref->fr_dfunc_idx;
6863 name = df->df_ufunc->uf_name;
6864 }
6865 else
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006866 name = extra->fre_func_name;
Bram Moolenaar313e4722023-02-08 20:55:27 +00006867 if (extra != NULL && extra->fre_class != NULL)
6868 {
6869 smsg("%s%4d FUNCREF %s.%s", pfx, current,
6870 extra->fre_class->class_name, name);
6871 }
6872 else if (extra == NULL
6873 || extra->fre_loopvar_info.lvi_depth == 0)
6874 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006875 smsg("%s%4d FUNCREF %s", pfx, current, name);
Bram Moolenaar313e4722023-02-08 20:55:27 +00006876 }
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006877 else
Bram Moolenaarcc341812022-09-19 15:54:34 +01006878 {
6879 char_u *info = printable_loopvarinfo(
6880 &extra->fre_loopvar_info);
6881
6882 smsg("%s%4d FUNCREF %s vars %s", pfx, current,
6883 name, info);
6884 vim_free(info);
6885 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006886 }
6887 break;
6888
6889 case ISN_NEWFUNC:
6890 {
Bram Moolenaarcc341812022-09-19 15:54:34 +01006891 newfuncarg_T *arg = iptr->isn_arg.newfunc.nf_arg;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006892
Bram Moolenaarcc341812022-09-19 15:54:34 +01006893 if (arg->nfa_loopvar_info.lvi_depth == 0)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006894 smsg("%s%4d NEWFUNC %s %s", pfx, current,
6895 arg->nfa_lambda, arg->nfa_global);
6896 else
Bram Moolenaarcc341812022-09-19 15:54:34 +01006897 {
6898 char_u *info = printable_loopvarinfo(
6899 &arg->nfa_loopvar_info);
6900
6901 smsg("%s%4d NEWFUNC %s %s vars %s", pfx, current,
6902 arg->nfa_lambda, arg->nfa_global, info);
6903 vim_free(info);
6904 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006905 }
6906 break;
6907
6908 case ISN_DEF:
6909 {
6910 char_u *name = iptr->isn_arg.string;
6911
6912 smsg("%s%4d DEF %s", pfx, current,
6913 name == NULL ? (char_u *)"" : name);
6914 }
6915 break;
6916
6917 case ISN_JUMP:
6918 {
6919 char *when = "?";
6920
6921 switch (iptr->isn_arg.jump.jump_when)
6922 {
6923 case JUMP_ALWAYS:
6924 when = "JUMP";
6925 break;
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02006926 case JUMP_NEVER:
6927 iemsg("JUMP_NEVER should not be used");
6928 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006929 case JUMP_AND_KEEP_IF_TRUE:
6930 when = "JUMP_AND_KEEP_IF_TRUE";
6931 break;
6932 case JUMP_IF_FALSE:
6933 when = "JUMP_IF_FALSE";
6934 break;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01006935 case JUMP_WHILE_FALSE:
6936 when = "JUMP_WHILE_FALSE"; // unused
6937 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006938 case JUMP_IF_COND_FALSE:
6939 when = "JUMP_IF_COND_FALSE";
6940 break;
6941 case JUMP_IF_COND_TRUE:
6942 when = "JUMP_IF_COND_TRUE";
6943 break;
6944 }
6945 smsg("%s%4d %s -> %d", pfx, current, when,
6946 iptr->isn_arg.jump.jump_where);
6947 }
6948 break;
6949
6950 case ISN_JUMP_IF_ARG_SET:
6951 smsg("%s%4d JUMP_IF_ARG_SET arg[%d] -> %d", pfx, current,
6952 iptr->isn_arg.jumparg.jump_arg_off + STACK_FRAME_SIZE,
6953 iptr->isn_arg.jump.jump_where);
6954 break;
6955
Bram Moolenaar65b0d162022-12-13 18:43:22 +00006956 case ISN_JUMP_IF_ARG_NOT_SET:
6957 smsg("%s%4d JUMP_IF_ARG_NOT_SET arg[%d] -> %d", pfx, current,
6958 iptr->isn_arg.jumparg.jump_arg_off + STACK_FRAME_SIZE,
6959 iptr->isn_arg.jump.jump_where);
6960 break;
6961
Bram Moolenaar4c137212021-04-19 16:48:48 +02006962 case ISN_FOR:
6963 {
6964 forloop_T *forloop = &iptr->isn_arg.forloop;
6965
6966 smsg("%s%4d FOR $%d -> %d", pfx, current,
Bram Moolenaarcc341812022-09-19 15:54:34 +01006967 forloop->for_loop_idx, forloop->for_end);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006968 }
6969 break;
6970
Bram Moolenaarb46c0832022-09-15 17:19:37 +01006971 case ISN_ENDLOOP:
6972 {
6973 endloop_T *endloop = &iptr->isn_arg.endloop;
6974
Bram Moolenaarcc341812022-09-19 15:54:34 +01006975 smsg("%s%4d ENDLOOP ref $%d save $%d-$%d depth %d",
6976 pfx, current,
Bram Moolenaarb46c0832022-09-15 17:19:37 +01006977 endloop->end_funcref_idx,
6978 endloop->end_var_idx,
Bram Moolenaarcc341812022-09-19 15:54:34 +01006979 endloop->end_var_idx + endloop->end_var_count - 1,
6980 endloop->end_depth);
Bram Moolenaarb46c0832022-09-15 17:19:37 +01006981 }
6982 break;
6983
6984 case ISN_WHILE:
6985 {
6986 whileloop_T *whileloop = &iptr->isn_arg.whileloop;
6987
6988 smsg("%s%4d WHILE $%d -> %d", pfx, current,
6989 whileloop->while_funcref_idx,
6990 whileloop->while_end);
6991 }
6992 break;
6993
Bram Moolenaar4c137212021-04-19 16:48:48 +02006994 case ISN_TRY:
6995 {
Bram Moolenaar0d807102021-12-21 09:42:09 +00006996 try_T *try = &iptr->isn_arg.tryref;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006997
6998 if (try->try_ref->try_finally == 0)
6999 smsg("%s%4d TRY catch -> %d, endtry -> %d",
7000 pfx, current,
7001 try->try_ref->try_catch,
7002 try->try_ref->try_endtry);
7003 else
7004 smsg("%s%4d TRY catch -> %d, finally -> %d, endtry -> %d",
7005 pfx, current,
7006 try->try_ref->try_catch,
7007 try->try_ref->try_finally,
7008 try->try_ref->try_endtry);
7009 }
7010 break;
7011 case ISN_CATCH:
Bram Moolenaar4c137212021-04-19 16:48:48 +02007012 smsg("%s%4d CATCH", pfx, current);
7013 break;
7014 case ISN_TRYCONT:
7015 {
7016 trycont_T *trycont = &iptr->isn_arg.trycont;
7017
7018 smsg("%s%4d TRY-CONTINUE %d level%s -> %d", pfx, current,
7019 trycont->tct_levels,
7020 trycont->tct_levels == 1 ? "" : "s",
7021 trycont->tct_where);
7022 }
7023 break;
7024 case ISN_FINALLY:
7025 smsg("%s%4d FINALLY", pfx, current);
7026 break;
7027 case ISN_ENDTRY:
7028 smsg("%s%4d ENDTRY", pfx, current);
7029 break;
7030 case ISN_THROW:
7031 smsg("%s%4d THROW", pfx, current);
7032 break;
7033
7034 // expression operations on number
7035 case ISN_OPNR:
7036 case ISN_OPFLOAT:
7037 case ISN_OPANY:
7038 {
7039 char *what;
7040 char *ins;
7041
7042 switch (iptr->isn_arg.op.op_type)
7043 {
7044 case EXPR_MULT: what = "*"; break;
7045 case EXPR_DIV: what = "/"; break;
7046 case EXPR_REM: what = "%"; break;
7047 case EXPR_SUB: what = "-"; break;
7048 case EXPR_ADD: what = "+"; break;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01007049 case EXPR_LSHIFT: what = "<<"; break;
7050 case EXPR_RSHIFT: what = ">>"; break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007051 default: what = "???"; break;
7052 }
7053 switch (iptr->isn_type)
7054 {
7055 case ISN_OPNR: ins = "OPNR"; break;
7056 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
7057 case ISN_OPANY: ins = "OPANY"; break;
7058 default: ins = "???"; break;
7059 }
7060 smsg("%s%4d %s %s", pfx, current, ins, what);
7061 }
7062 break;
7063
7064 case ISN_COMPAREBOOL:
7065 case ISN_COMPARESPECIAL:
Bram Moolenaar7a222242022-03-01 19:23:24 +00007066 case ISN_COMPARENULL:
Bram Moolenaar4c137212021-04-19 16:48:48 +02007067 case ISN_COMPARENR:
7068 case ISN_COMPAREFLOAT:
7069 case ISN_COMPARESTRING:
7070 case ISN_COMPAREBLOB:
7071 case ISN_COMPARELIST:
7072 case ISN_COMPAREDICT:
7073 case ISN_COMPAREFUNC:
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00007074 case ISN_COMPARECLASS:
7075 case ISN_COMPAREOBJECT:
Bram Moolenaar4c137212021-04-19 16:48:48 +02007076 case ISN_COMPAREANY:
7077 {
7078 char *p;
7079 char buf[10];
7080 char *type;
7081
7082 switch (iptr->isn_arg.op.op_type)
7083 {
7084 case EXPR_EQUAL: p = "=="; break;
7085 case EXPR_NEQUAL: p = "!="; break;
7086 case EXPR_GREATER: p = ">"; break;
7087 case EXPR_GEQUAL: p = ">="; break;
7088 case EXPR_SMALLER: p = "<"; break;
7089 case EXPR_SEQUAL: p = "<="; break;
7090 case EXPR_MATCH: p = "=~"; break;
7091 case EXPR_IS: p = "is"; break;
7092 case EXPR_ISNOT: p = "isnot"; break;
7093 case EXPR_NOMATCH: p = "!~"; break;
7094 default: p = "???"; break;
7095 }
7096 STRCPY(buf, p);
7097 if (iptr->isn_arg.op.op_ic == TRUE)
7098 strcat(buf, "?");
7099 switch(iptr->isn_type)
7100 {
7101 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
7102 case ISN_COMPARESPECIAL:
7103 type = "COMPARESPECIAL"; break;
Bram Moolenaar7a222242022-03-01 19:23:24 +00007104 case ISN_COMPARENULL: type = "COMPARENULL"; break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007105 case ISN_COMPARENR: type = "COMPARENR"; break;
7106 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
7107 case ISN_COMPARESTRING:
7108 type = "COMPARESTRING"; break;
7109 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
7110 case ISN_COMPARELIST: type = "COMPARELIST"; break;
7111 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
7112 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00007113 case ISN_COMPARECLASS: type = "COMPARECLASS"; break;
7114 case ISN_COMPAREOBJECT:
7115 type = "COMPAREOBJECT"; break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007116 case ISN_COMPAREANY: type = "COMPAREANY"; break;
7117 default: type = "???"; break;
7118 }
7119
7120 smsg("%s%4d %s %s", pfx, current, type, buf);
7121 }
7122 break;
7123
7124 case ISN_ADDLIST: smsg("%s%4d ADDLIST", pfx, current); break;
7125 case ISN_ADDBLOB: smsg("%s%4d ADDBLOB", pfx, current); break;
7126
7127 // expression operations
LemonBoy372bcce2022-04-25 12:43:20 +01007128 case ISN_CONCAT:
7129 smsg("%s%4d CONCAT size %lld", pfx, current,
7130 (varnumber_T)(iptr->isn_arg.number));
7131 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007132 case ISN_STRINDEX: smsg("%s%4d STRINDEX", pfx, current); break;
7133 case ISN_STRSLICE: smsg("%s%4d STRSLICE", pfx, current); break;
7134 case ISN_BLOBINDEX: smsg("%s%4d BLOBINDEX", pfx, current); break;
7135 case ISN_BLOBSLICE: smsg("%s%4d BLOBSLICE", pfx, current); break;
7136 case ISN_LISTAPPEND: smsg("%s%4d LISTAPPEND", pfx, current); break;
7137 case ISN_BLOBAPPEND: smsg("%s%4d BLOBAPPEND", pfx, current); break;
7138 case ISN_LISTINDEX: smsg("%s%4d LISTINDEX", pfx, current); break;
7139 case ISN_LISTSLICE: smsg("%s%4d LISTSLICE", pfx, current); break;
7140 case ISN_ANYINDEX: smsg("%s%4d ANYINDEX", pfx, current); break;
7141 case ISN_ANYSLICE: smsg("%s%4d ANYSLICE", pfx, current); break;
7142 case ISN_SLICE: smsg("%s%4d SLICE %lld",
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02007143 pfx, current, iptr->isn_arg.number); break;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02007144 case ISN_GETITEM: smsg("%s%4d ITEM %lld%s", pfx, current,
7145 iptr->isn_arg.getitem.gi_index,
7146 iptr->isn_arg.getitem.gi_with_op ?
7147 " with op" : ""); break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007148 case ISN_MEMBER: smsg("%s%4d MEMBER", pfx, current); break;
7149 case ISN_STRINGMEMBER: smsg("%s%4d MEMBER %s", pfx, current,
7150 iptr->isn_arg.string); break;
Ernie Rael00df69e2023-09-05 07:38:09 +02007151 case ISN_GET_OBJ_MEMBER: smsg("%s%4d OBJ_MEMBER %d%s", pfx, current,
7152 (int)iptr->isn_arg.classmember.cm_idx,
7153 iptr->isn_arg.classmember.cm_static
7154 ? " [STATIC]" : "");
7155 break;
7156 case ISN_GET_ITF_MEMBER: smsg("%s%4d ITF_MEMBER %d on %s%s",
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00007157 pfx, current,
7158 (int)iptr->isn_arg.classmember.cm_idx,
Ernie Rael00df69e2023-09-05 07:38:09 +02007159 iptr->isn_arg.classmember.cm_class->class_name,
7160 iptr->isn_arg.classmember.cm_static
7161 ? " [STATIC]" : "");
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00007162 break;
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00007163 case ISN_STORE_THIS: smsg("%s%4d STORE_THIS %d", pfx, current,
Bram Moolenaarffdaca92022-12-09 21:41:48 +00007164 (int)iptr->isn_arg.number); break;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02007165 case ISN_CLEARDICT: smsg("%s%4d CLEARDICT", pfx, current); break;
7166 case ISN_USEDICT: smsg("%s%4d USEDICT", pfx, current); break;
7167
Bram Moolenaar4c137212021-04-19 16:48:48 +02007168 case ISN_NEGATENR: smsg("%s%4d NEGATENR", pfx, current); break;
7169
Bram Moolenaar4c137212021-04-19 16:48:48 +02007170 case ISN_CHECKTYPE:
7171 {
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01007172 checktype_T *ct = &iptr->isn_arg.type;
Bram Moolenaarc6951a72022-12-29 20:56:24 +00007173 char *tofree = NULL;
7174 char *typename;
7175
7176 if (ct->ct_type->tt_type == VAR_FLOAT
7177 && (ct->ct_type->tt_flags & TTFLAG_NUMBER_OK))
7178 typename = "float|number";
7179 else
7180 typename = type_name(ct->ct_type, &tofree);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007181
7182 if (ct->ct_arg_idx == 0)
7183 smsg("%s%4d CHECKTYPE %s stack[%d]", pfx, current,
Bram Moolenaarc6951a72022-12-29 20:56:24 +00007184 typename,
Bram Moolenaar4c137212021-04-19 16:48:48 +02007185 (int)ct->ct_off);
7186 else
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01007187 smsg("%s%4d CHECKTYPE %s stack[%d] %s %d",
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02007188 pfx, current,
Bram Moolenaarc6951a72022-12-29 20:56:24 +00007189 typename,
Bram Moolenaar4c137212021-04-19 16:48:48 +02007190 (int)ct->ct_off,
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01007191 ct->ct_is_var ? "var": "arg",
Bram Moolenaar4c137212021-04-19 16:48:48 +02007192 (int)ct->ct_arg_idx);
7193 vim_free(tofree);
7194 break;
7195 }
7196 case ISN_CHECKLEN: smsg("%s%4d CHECKLEN %s%d", pfx, current,
7197 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
7198 iptr->isn_arg.checklen.cl_min_len);
7199 break;
7200 case ISN_SETTYPE:
7201 {
7202 char *tofree;
7203
7204 smsg("%s%4d SETTYPE %s", pfx, current,
7205 type_name(iptr->isn_arg.type.ct_type, &tofree));
7206 vim_free(tofree);
7207 break;
7208 }
7209 case ISN_COND2BOOL: smsg("%s%4d COND2BOOL", pfx, current); break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007210 case ISN_2BOOL: if (iptr->isn_arg.tobool.invert)
7211 smsg("%s%4d INVERT %d (!val)", pfx, current,
7212 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007213 else
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007214 smsg("%s%4d 2BOOL %d (!!val)", pfx, current,
7215 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007216 break;
7217 case ISN_2STRING: smsg("%s%4d 2STRING stack[%lld]", pfx, current,
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007218 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02007219 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007220 case ISN_2STRING_ANY: smsg("%s%4d 2STRING_ANY stack[%lld]",
7221 pfx, current,
7222 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02007223 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007224 case ISN_RANGE: smsg("%s%4d RANGE %s", pfx, current,
7225 iptr->isn_arg.string);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007226 break;
7227 case ISN_PUT:
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01007228 if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE)
Bram Moolenaar4c137212021-04-19 16:48:48 +02007229 smsg("%s%4d PUT %c above range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007230 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007231 else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE)
7232 smsg("%s%4d PUT %c range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007233 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007234 else
7235 smsg("%s%4d PUT %c %ld", pfx, current,
7236 iptr->isn_arg.put.put_regname,
7237 (long)iptr->isn_arg.put.put_lnum);
7238 break;
7239
Bram Moolenaar4c137212021-04-19 16:48:48 +02007240 case ISN_CMDMOD:
7241 {
7242 char_u *buf;
7243 size_t len = produce_cmdmods(
7244 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
7245
7246 buf = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02007247 if (likely(buf != NULL))
Bram Moolenaar4c137212021-04-19 16:48:48 +02007248 {
7249 (void)produce_cmdmods(
7250 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
7251 smsg("%s%4d CMDMOD %s", pfx, current, buf);
7252 vim_free(buf);
7253 }
7254 break;
7255 }
7256 case ISN_CMDMOD_REV: smsg("%s%4d CMDMOD_REV", pfx, current); break;
7257
7258 case ISN_PROF_START:
Bram Moolenaare99d4222021-06-13 14:01:26 +02007259 smsg("%s%4d PROFILE START line %d", pfx, current,
7260 iptr->isn_lnum);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007261 break;
7262
7263 case ISN_PROF_END:
7264 smsg("%s%4d PROFILE END", pfx, current);
7265 break;
7266
Bram Moolenaare99d4222021-06-13 14:01:26 +02007267 case ISN_DEBUG:
Bram Moolenaar8cec9272021-06-23 20:20:53 +02007268 smsg("%s%4d DEBUG line %d-%d varcount %lld", pfx, current,
7269 iptr->isn_arg.debug.dbg_break_lnum + 1,
7270 iptr->isn_lnum,
7271 iptr->isn_arg.debug.dbg_var_names_len);
Bram Moolenaare99d4222021-06-13 14:01:26 +02007272 break;
7273
Bram Moolenaar4c137212021-04-19 16:48:48 +02007274 case ISN_UNPACK: smsg("%s%4d UNPACK %d%s", pfx, current,
7275 iptr->isn_arg.unpack.unp_count,
7276 iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : "");
7277 break;
7278 case ISN_SHUFFLE: smsg("%s%4d SHUFFLE %d up %d", pfx, current,
7279 iptr->isn_arg.shuffle.shfl_item,
7280 iptr->isn_arg.shuffle.shfl_up);
7281 break;
7282 case ISN_DROP: smsg("%s%4d DROP", pfx, current); break;
7283
7284 case ISN_FINISH: // End of list of instructions for ISN_SUBSTITUTE.
7285 return;
7286 }
7287
7288 out_flush(); // output one line at a time
7289 ui_breakcheck();
7290 if (got_int)
7291 break;
7292 }
7293}
7294
7295/*
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02007296 * Handle command line completion for the :disassemble command.
7297 */
7298 void
7299set_context_in_disassemble_cmd(expand_T *xp, char_u *arg)
7300{
7301 char_u *p;
7302
7303 // Default: expand user functions, "debug" and "profile"
7304 xp->xp_context = EXPAND_DISASSEMBLE;
7305 xp->xp_pattern = arg;
7306
7307 // first argument already typed: only user function names
7308 if (*arg != NUL && *(p = skiptowhite(arg)) != NUL)
7309 {
7310 xp->xp_context = EXPAND_USER_FUNC;
7311 xp->xp_pattern = skipwhite(p);
7312 }
7313}
7314
7315/*
7316 * Function given to ExpandGeneric() to obtain the list of :disassemble
7317 * arguments.
7318 */
7319 char_u *
7320get_disassemble_argument(expand_T *xp, int idx)
7321{
7322 if (idx == 0)
7323 return (char_u *)"debug";
7324 if (idx == 1)
7325 return (char_u *)"profile";
7326 return get_user_func_name(xp, idx - 2);
7327}
7328
7329/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01007330 * ":disassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01007331 * We don't really need this at runtime, but we do have tests that require it,
7332 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007333 */
7334 void
7335ex_disassemble(exarg_T *eap)
7336{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01007337 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01007338 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007339 dfunc_T *dfunc;
Bram Moolenaardafef512022-05-21 21:55:55 +01007340 isn_T *instr = NULL; // init to shut up gcc warning
7341 int instr_count = 0; // init to shut up gcc warning
Bram Moolenaar5a01caa2022-05-21 18:56:58 +01007342 compiletype_T compile_type = CT_NONE;
Bram Moolenaare99d4222021-06-13 14:01:26 +02007343
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01007344 ufunc = find_func_by_name(arg, &compile_type);
Bram Moolenaara26b9702020-04-18 19:53:28 +02007345 if (ufunc == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007346 return;
Bram Moolenaare99d4222021-06-13 14:01:26 +02007347 if (func_needs_compiling(ufunc, compile_type)
7348 && compile_def_function(ufunc, FALSE, compile_type, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02007349 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007350 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007351 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007352 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007353 return;
7354 }
Bram Moolenaar6db660b2021-08-01 14:08:54 +02007355 msg((char *)printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007356
7357 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
Bram Moolenaare99d4222021-06-13 14:01:26 +02007358 switch (compile_type)
7359 {
7360 case CT_PROFILE:
Bram Moolenaarf002a412021-01-24 13:34:18 +01007361#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02007362 instr = dfunc->df_instr_prof;
7363 instr_count = dfunc->df_instr_prof_count;
7364 break;
Bram Moolenaarf002a412021-01-24 13:34:18 +01007365#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02007366 // FALLTHROUGH
7367 case CT_NONE:
7368 instr = dfunc->df_instr;
7369 instr_count = dfunc->df_instr_count;
7370 break;
7371 case CT_DEBUG:
7372 instr = dfunc->df_instr_debug;
7373 instr_count = dfunc->df_instr_debug_count;
7374 break;
7375 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007376
Bram Moolenaar4c137212021-04-19 16:48:48 +02007377 list_instructions("", instr, instr_count, ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007378}
7379
7380/*
Bram Moolenaar13106602020-10-04 16:06:05 +02007381 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007382 * list, etc. Mostly like what JavaScript does, except that empty list and
7383 * empty dictionary are FALSE.
7384 */
7385 int
7386tv2bool(typval_T *tv)
7387{
7388 switch (tv->v_type)
7389 {
7390 case VAR_NUMBER:
7391 return tv->vval.v_number != 0;
7392 case VAR_FLOAT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007393 return tv->vval.v_float != 0.0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007394 case VAR_PARTIAL:
7395 return tv->vval.v_partial != NULL;
7396 case VAR_FUNC:
7397 case VAR_STRING:
7398 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
7399 case VAR_LIST:
7400 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
7401 case VAR_DICT:
7402 return tv->vval.v_dict != NULL
7403 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
7404 case VAR_BOOL:
7405 case VAR_SPECIAL:
7406 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
7407 case VAR_JOB:
7408#ifdef FEAT_JOB_CHANNEL
7409 return tv->vval.v_job != NULL;
7410#else
7411 break;
7412#endif
7413 case VAR_CHANNEL:
7414#ifdef FEAT_JOB_CHANNEL
7415 return tv->vval.v_channel != NULL;
7416#else
7417 break;
7418#endif
7419 case VAR_BLOB:
7420 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
7421 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02007422 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007423 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02007424 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00007425 case VAR_CLASS:
7426 case VAR_OBJECT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007427 break;
7428 }
7429 return FALSE;
7430}
7431
Bram Moolenaarea2d4072020-11-12 12:08:51 +01007432 void
7433emsg_using_string_as(typval_T *tv, int as_number)
7434{
7435 semsg(_(as_number ? e_using_string_as_number_str
7436 : e_using_string_as_bool_str),
7437 tv->vval.v_string == NULL
7438 ? (char_u *)"" : tv->vval.v_string);
7439}
7440
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007441/*
7442 * If "tv" is a string give an error and return FAIL.
7443 */
7444 int
7445check_not_string(typval_T *tv)
7446{
7447 if (tv->v_type == VAR_STRING)
7448 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +01007449 emsg_using_string_as(tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007450 clear_tv(tv);
7451 return FAIL;
7452 }
7453 return OK;
7454}
7455
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007456
7457#endif // FEAT_EVAL