blob: d005deb4d7283d4fb5cf42b1ed2095fcd77d65d8 [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
Yegappan Lakshmananc1946262023-09-25 21:00:46 +0200538 if (IS_OBJECT_METHOD(ufunc) && !IS_CONSTRUCTOR_METHOD(ufunc)
539 && obj->v_type == VAR_OBJECT && obj->vval.v_object == NULL)
540 {
541 // If this is not a constructor method, then a valid object is
542 // needed.
543 emsg(_(e_using_null_object));
544 return FAIL;
545 }
546
Bram Moolenaar0d89d8a2022-12-31 14:01:24 +0000547 // Check the argument types.
548 if (check_ufunc_arg_types(ufunc, argcount, vararg_count, ectx) == FAIL)
549 return FAIL;
550
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200551 // Reserve space for:
552 // - missing arguments
553 // - stack frame
554 // - local variables
555 // - if needed: a counter for number of closures created in
556 // ectx->ec_funcrefs.
557 varcount = dfunc->df_varcount + dfunc->df_has_closure;
Bram Moolenaarb46c0832022-09-15 17:19:37 +0100558 if (GA_GROW_FAILS(&ectx->ec_stack,
559 arg_to_add + STACK_FRAME_SIZE + varcount))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100560 return FAIL;
561
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100562 // If depth of calling is getting too high, don't execute the function.
563 if (funcdepth_increment() == FAIL)
564 return FAIL;
Bram Moolenaare99d4222021-06-13 14:01:26 +0200565 ++ex_nesting_level;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100566
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100567 // Only make a copy of funclocal if it contains something to restore.
Bram Moolenaar4c137212021-04-19 16:48:48 +0200568 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100569 {
570 floc = ALLOC_ONE(funclocal_T);
571 if (floc == NULL)
572 return FAIL;
Bram Moolenaar4c137212021-04-19 16:48:48 +0200573 *floc = ectx->ec_funclocal;
574 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100575 }
576
Bram Moolenaarfe270812020-04-11 22:31:27 +0200577 // Move the vararg-list to below the missing optional arguments.
578 if (vararg_count > 0 && arg_to_add > 0)
579 *STACK_TV_BOT(arg_to_add - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100580
581 // Reserve space for omitted optional arguments, filled in soon.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200582 for (idx = 0; idx < arg_to_add; ++idx)
Bram Moolenaarfe270812020-04-11 22:31:27 +0200583 STACK_TV_BOT(idx - vararg_count)->v_type = VAR_UNKNOWN;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200584 ectx->ec_stack.ga_len += arg_to_add;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100585
586 // Store current execution state in stack frame for ISN_RETURN.
Bram Moolenaar0186e582021-01-10 18:33:11 +0100587 STACK_TV_BOT(STACK_FRAME_FUNC_OFF)->vval.v_number = ectx->ec_dfunc_idx;
588 STACK_TV_BOT(STACK_FRAME_IIDX_OFF)->vval.v_number = ectx->ec_iidx;
Bram Moolenaar5930ddc2021-04-26 20:32:59 +0200589 STACK_TV_BOT(STACK_FRAME_INSTR_OFF)->vval.v_string = (void *)ectx->ec_instr;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200590 STACK_TV_BOT(STACK_FRAME_OUTER_OFF)->vval.v_string =
591 (void *)ectx->ec_outer_ref;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100592 STACK_TV_BOT(STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string = (void *)floc;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100593 STACK_TV_BOT(STACK_FRAME_IDX_OFF)->vval.v_number = ectx->ec_frame_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200594 ectx->ec_frame_idx = ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100595
Bram Moolenaar5800c792022-09-22 17:34:01 +0100596 // Initialize all local variables to number zero. Also initialize the
597 // variable that counts how many closures were created. This is used in
598 // handle_closure_in_use().
599 int initcount = dfunc->df_varcount + (dfunc->df_has_closure ? 1 : 0);
600 for (idx = 0; idx < initcount; ++idx)
Bram Moolenaar5cd64792021-12-25 18:23:24 +0000601 {
602 typval_T *tv = STACK_TV_BOT(STACK_FRAME_SIZE + idx);
603
604 tv->v_type = VAR_NUMBER;
605 tv->vval.v_number = 0;
606 }
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200607 ectx->ec_stack.ga_len += STACK_FRAME_SIZE + varcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100608
Bram Moolenaar574950d2023-01-03 19:08:50 +0000609 // For an object method move the object from just before the arguments to
610 // the first local variable.
Yegappan Lakshmanan1db15142023-09-19 20:34:05 +0200611 if (IS_OBJECT_METHOD(ufunc))
Bram Moolenaar574950d2023-01-03 19:08:50 +0000612 {
613 *STACK_TV_VAR(0) = *obj;
614 obj->v_type = VAR_UNKNOWN;
615 }
616
Bram Moolenaar5800c792022-09-22 17:34:01 +0100617 partial_T *pt = pt_arg != NULL ? pt_arg : ufunc->uf_partial;
618 if (pt != NULL || (ufunc->uf_flags & FC_CLOSURE))
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100619 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200620 outer_ref_T *ref = ALLOC_CLEAR_ONE(outer_ref_T);
Bram Moolenaar0186e582021-01-10 18:33:11 +0100621
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200622 if (ref == NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +0100623 return FAIL;
624 if (pt != NULL)
625 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +0000626 ref->or_outer = get_pt_outer(pt);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200627 ++pt->pt_refcount;
628 ref->or_partial = pt;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100629 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100630 else
631 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200632 ref->or_outer = ALLOC_CLEAR_ONE(outer_T);
Dominique Pelle5a9e5842021-07-24 19:32:12 +0200633 if (unlikely(ref->or_outer == NULL))
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200634 {
635 vim_free(ref);
636 return FAIL;
637 }
638 ref->or_outer_allocated = TRUE;
639 ref->or_outer->out_stack = &ectx->ec_stack;
640 ref->or_outer->out_frame_idx = ectx->ec_frame_idx;
641 if (ectx->ec_outer_ref != NULL)
642 ref->or_outer->out_up = ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100643 }
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200644 ectx->ec_outer_ref = ref;
Bram Moolenaarab360522021-01-10 14:02:28 +0100645 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100646 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200647 ectx->ec_outer_ref = NULL;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100648
Bram Moolenaarc970e422021-03-17 15:03:04 +0100649 ++ufunc->uf_calls;
650
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100651 // Set execution state to the start of the called function.
652 ectx->ec_dfunc_idx = cdf_idx;
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100653 ectx->ec_instr = INSTRUCTIONS(dfunc);
Bram Moolenaarb2049902021-01-24 12:53:53 +0100654 entry = estack_push_ufunc(ufunc, 1);
Bram Moolenaarc620c052020-07-08 15:16:19 +0200655 if (entry != NULL)
656 {
657 // Set the script context to the script where the function was defined.
Bram Moolenaarc70fe462021-04-17 17:59:19 +0200658 // Save the current context so it can be restored on return.
659 entry->es_save_sctx = current_sctx;
660 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200661 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100662
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +0200663 // Start execution at the first instruction.
664 ectx->ec_iidx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100665
666 return OK;
667}
668
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000669// Double linked list of funcstack_T in use.
670static funcstack_T *first_funcstack = NULL;
671
672 static void
673add_funcstack_to_list(funcstack_T *funcstack)
674{
675 // Link in list of funcstacks.
676 if (first_funcstack != NULL)
677 first_funcstack->fs_prev = funcstack;
678 funcstack->fs_next = first_funcstack;
679 funcstack->fs_prev = NULL;
680 first_funcstack = funcstack;
681}
682
683 static void
684remove_funcstack_from_list(funcstack_T *funcstack)
685{
686 if (funcstack->fs_prev == NULL)
687 first_funcstack = funcstack->fs_next;
688 else
689 funcstack->fs_prev->fs_next = funcstack->fs_next;
690 if (funcstack->fs_next != NULL)
691 funcstack->fs_next->fs_prev = funcstack->fs_prev;
692}
693
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100694/*
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200695 * Used when returning from a function: Check if any closure is still
696 * referenced. If so then move the arguments and variables to a separate piece
697 * of stack to be used when the closure is called.
698 * When "free_arguments" is TRUE the arguments are to be freed.
699 * Returns FAIL when out of memory.
700 */
701 static int
702handle_closure_in_use(ectx_T *ectx, int free_arguments)
703{
704 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
705 + ectx->ec_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200706 int argcount;
707 int top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200708 int idx;
709 typval_T *tv;
710 int closure_in_use = FALSE;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200711 garray_T *gap = &ectx->ec_funcrefs;
712 varnumber_T closure_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200713
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200714 if (dfunc->df_ufunc == NULL)
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200715 return OK; // function was freed
716 if (dfunc->df_has_closure == 0)
717 return OK; // no closures
718 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + dfunc->df_varcount);
719 closure_count = tv->vval.v_number;
720 if (closure_count == 0)
721 return OK; // no funcrefs created
722
Bram Moolenaar8fa745e2022-09-16 19:04:24 +0100723 // Compute "top": the first entry in the stack used by the function.
724 // This is the first argument (after that comes the stack frame and then
725 // the local variables).
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200726 argcount = ufunc_argcount(dfunc->df_ufunc);
727 top = ectx->ec_frame_idx - argcount;
728
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200729 // Check if any created closure is still in use.
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200730 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200731 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200732 partial_T *pt;
733 int off = gap->ga_len - closure_count + idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200734
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200735 if (off < 0)
736 continue; // count is off or already done
737 pt = ((partial_T **)gap->ga_data)[off];
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200738 if (pt->pt_refcount > 1)
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200739 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200740 int refcount = pt->pt_refcount;
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200741 int i;
742
Dominique Pelleaf4a61a2021-12-27 17:21:41 +0000743 // A Reference in a local variable doesn't count, it gets
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200744 // unreferenced on return.
745 for (i = 0; i < dfunc->df_varcount; ++i)
746 {
747 typval_T *stv = STACK_TV(ectx->ec_frame_idx
748 + STACK_FRAME_SIZE + i);
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200749 if (stv->v_type == VAR_PARTIAL && pt == stv->vval.v_partial)
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200750 --refcount;
751 }
752 if (refcount > 1)
753 {
754 closure_in_use = TRUE;
755 break;
756 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200757 }
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200758 }
759
760 if (closure_in_use)
761 {
762 funcstack_T *funcstack = ALLOC_CLEAR_ONE(funcstack_T);
763 typval_T *stack;
764
765 // A closure is using the arguments and/or local variables.
766 // Move them to the called function.
767 if (funcstack == NULL)
768 return FAIL;
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000769
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200770 funcstack->fs_var_offset = argcount + STACK_FRAME_SIZE;
Bram Moolenaar1936c762022-09-28 15:19:10 +0100771 funcstack->fs_ga.ga_len = funcstack->fs_var_offset
772 + dfunc->df_varcount;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200773 stack = ALLOC_CLEAR_MULT(typval_T, funcstack->fs_ga.ga_len);
774 funcstack->fs_ga.ga_data = stack;
775 if (stack == NULL)
776 {
777 vim_free(funcstack);
778 return FAIL;
779 }
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000780 add_funcstack_to_list(funcstack);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200781
782 // Move or copy the arguments.
783 for (idx = 0; idx < argcount; ++idx)
784 {
785 tv = STACK_TV(top + idx);
786 if (free_arguments)
787 {
788 *(stack + idx) = *tv;
789 tv->v_type = VAR_UNKNOWN;
790 }
791 else
792 copy_tv(tv, stack + idx);
793 }
Bram Moolenaar8fa745e2022-09-16 19:04:24 +0100794 // Skip the stack frame.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200795 // Move the local variables.
796 for (idx = 0; idx < dfunc->df_varcount; ++idx)
797 {
798 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + idx);
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200799
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200800 // A partial created for a local function, that is also used as a
801 // local variable, has a reference count for the variable, thus
802 // will never go down to zero. When all these refcounts are one
803 // then the funcstack is unused. We need to count how many we have
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000804 // so we know when to check.
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200805 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL)
806 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200807 int i;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200808
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200809 for (i = 0; i < closure_count; ++i)
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200810 if (tv->vval.v_partial == ((partial_T **)gap->ga_data)[
811 gap->ga_len - closure_count + i])
812 ++funcstack->fs_min_refcount;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200813 }
814
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200815 *(stack + funcstack->fs_var_offset + idx) = *tv;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200816 tv->v_type = VAR_UNKNOWN;
817 }
818
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200819 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200820 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200821 partial_T *pt = ((partial_T **)gap->ga_data)[gap->ga_len
822 - closure_count + idx];
823 if (pt->pt_refcount > 1)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200824 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200825 ++funcstack->fs_refcount;
826 pt->pt_funcstack = funcstack;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100827 pt->pt_outer.out_stack = &funcstack->fs_ga;
828 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx - top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200829 }
830 }
831 }
832
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200833 for (idx = 0; idx < closure_count; ++idx)
834 partial_unref(((partial_T **)gap->ga_data)[gap->ga_len
835 - closure_count + idx]);
836 gap->ga_len -= closure_count;
837 if (gap->ga_len == 0)
838 ga_clear(gap);
839
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200840 return OK;
841}
842
843/*
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200844 * Called when a partial is freed or its reference count goes down to one. The
845 * funcstack may be the only reference to the partials in the local variables.
846 * Go over all of them, the funcref and can be freed if all partials
847 * referencing the funcstack have a reference count of one.
Bram Moolenaaracd6b992022-09-17 16:27:39 +0100848 * Returns TRUE if the funcstack is freed, the partial referencing it will then
849 * also have been freed.
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200850 */
Bram Moolenaaracd6b992022-09-17 16:27:39 +0100851 int
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200852funcstack_check_refcount(funcstack_T *funcstack)
853{
Bram Moolenaaracd6b992022-09-17 16:27:39 +0100854 int i;
855 garray_T *gap = &funcstack->fs_ga;
856 int done = 0;
857 typval_T *stack;
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200858
859 if (funcstack->fs_refcount > funcstack->fs_min_refcount)
Bram Moolenaaracd6b992022-09-17 16:27:39 +0100860 return FALSE;
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200861 for (i = funcstack->fs_var_offset; i < gap->ga_len; ++i)
862 {
863 typval_T *tv = ((typval_T *)gap->ga_data) + i;
864
865 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL
866 && tv->vval.v_partial->pt_funcstack == funcstack
867 && tv->vval.v_partial->pt_refcount == 1)
868 ++done;
869 }
Bram Moolenaaracd6b992022-09-17 16:27:39 +0100870 if (done != funcstack->fs_min_refcount)
871 return FALSE;
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200872
Bram Moolenaaracd6b992022-09-17 16:27:39 +0100873 stack = gap->ga_data;
874
875 // All partials referencing the funcstack have a reference count of
876 // one, thus the funcstack is no longer of use.
877 for (i = 0; i < gap->ga_len; ++i)
878 clear_tv(stack + i);
879 vim_free(stack);
880 remove_funcstack_from_list(funcstack);
881 vim_free(funcstack);
882
883 return TRUE;
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200884}
885
886/*
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000887 * For garbage collecting: set references in all variables referenced by
888 * all funcstacks.
889 */
890 int
891set_ref_in_funcstacks(int copyID)
892{
893 funcstack_T *funcstack;
894
895 for (funcstack = first_funcstack; funcstack != NULL;
896 funcstack = funcstack->fs_next)
897 {
898 typval_T *stack = funcstack->fs_ga.ga_data;
899 int i;
900
901 for (i = 0; i < funcstack->fs_ga.ga_len; ++i)
902 if (set_ref_in_item(stack + i, copyID, NULL, NULL))
903 return TRUE; // abort
904 }
905 return FALSE;
906}
907
Bram Moolenaar806a2732022-09-04 15:40:36 +0100908// Ugly static to avoid passing the execution context around through many
909// layers.
910static ectx_T *current_ectx = NULL;
911
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000912/*
Bram Moolenaar806a2732022-09-04 15:40:36 +0100913 * Return TRUE if currently executing a :def function.
914 * Can be used by builtin functions only.
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100915 */
Bram Moolenaar806a2732022-09-04 15:40:36 +0100916 int
917in_def_function(void)
918{
919 return current_ectx != NULL;
920}
921
922/*
Bram Moolenaar98aff652022-09-06 21:02:35 +0100923 * Clear "current_ectx" and return the previous value. To be used when calling
924 * a user function.
925 */
926 ectx_T *
dundargocc57b5bc2022-11-02 13:30:51 +0000927clear_current_ectx(void)
Bram Moolenaar98aff652022-09-06 21:02:35 +0100928{
929 ectx_T *r = current_ectx;
930
931 current_ectx = NULL;
932 return r;
933}
934
935 void
936restore_current_ectx(ectx_T *ectx)
937{
938 if (current_ectx != NULL)
939 iemsg("Restoring current_ectx while it is not NULL");
940 current_ectx = ectx;
941}
942
943/*
Bram Moolenaar806a2732022-09-04 15:40:36 +0100944 * Add an entry for a deferred function call to the currently executing
945 * function.
946 * Return the list or NULL when failed.
947 */
948 static list_T *
949add_defer_item(int var_idx, int argcount, ectx_T *ectx)
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100950{
951 typval_T *defer_tv = STACK_TV_VAR(var_idx);
952 list_T *defer_l;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100953 list_T *l;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100954 typval_T listval;
955
956 if (defer_tv->v_type != VAR_LIST)
957 {
Bram Moolenaara5348f22022-09-04 11:42:22 +0100958 // first time, allocate the list
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100959 if (rettv_list_alloc(defer_tv) == FAIL)
Bram Moolenaar806a2732022-09-04 15:40:36 +0100960 return NULL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100961 }
962 defer_l = defer_tv->vval.v_list;
963
964 l = list_alloc_with_items(argcount + 1);
965 if (l == NULL)
Bram Moolenaar806a2732022-09-04 15:40:36 +0100966 return NULL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100967 listval.v_type = VAR_LIST;
968 listval.vval.v_list = l;
969 listval.v_lock = 0;
Bram Moolenaara5348f22022-09-04 11:42:22 +0100970 if (list_insert_tv(defer_l, &listval, defer_l->lv_first) == FAIL)
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100971 {
972 vim_free(l);
Bram Moolenaar806a2732022-09-04 15:40:36 +0100973 return NULL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100974 }
975
Bram Moolenaar806a2732022-09-04 15:40:36 +0100976 return l;
977}
978
979/*
980 * Handle ISN_DEFER. Stack has a function reference and "argcount" arguments.
981 * The local variable that lists deferred functions is "var_idx".
982 * Returns OK or FAIL.
983 */
984 static int
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +0000985defer_command(int var_idx, int has_obj, int argcount, ectx_T *ectx)
Bram Moolenaar806a2732022-09-04 15:40:36 +0100986{
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +0000987 int obj_off = has_obj ? 1 : 0;
988 list_T *l = add_defer_item(var_idx, argcount + obj_off, ectx);
Bram Moolenaar806a2732022-09-04 15:40:36 +0100989 int i;
990 typval_T *func_tv;
991
992 if (l == NULL)
993 return FAIL;
994
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100995 func_tv = STACK_TV_BOT(-argcount - 1);
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +0000996 if (has_obj ? func_tv->v_type != VAR_PARTIAL : func_tv->v_type != VAR_FUNC)
Bram Moolenaarf5fec052022-09-11 11:49:22 +0100997 {
998 semsg(_(e_expected_str_but_got_str),
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +0000999 has_obj ? "partial" : "function",
Bram Moolenaarf5fec052022-09-11 11:49:22 +01001000 vartype_name(func_tv->v_type));
1001 return FAIL;
1002 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001003 list_set_item(l, 0, func_tv);
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001004 if (has_obj)
1005 list_set_item(l, 1, STACK_TV_BOT(-argcount - 2));
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001006
Bram Moolenaarf5fec052022-09-11 11:49:22 +01001007 for (i = 0; i < argcount; ++i)
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001008 list_set_item(l, i + 1 + obj_off, STACK_TV_BOT(-argcount + i));
1009 ectx->ec_stack.ga_len -= argcount + 1 + obj_off;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001010 return OK;
1011}
1012
1013/*
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001014 * Add a deferred call for "name" with arguments "argvars[argcount]".
Bram Moolenaar806a2732022-09-04 15:40:36 +01001015 * Consumes "name", also on failure.
1016 * Only to be called when in_def_function() returns TRUE.
1017 */
1018 int
1019add_defer_function(char_u *name, int argcount, typval_T *argvars)
1020{
1021 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1022 + current_ectx->ec_dfunc_idx;
1023 list_T *l;
1024 typval_T func_tv;
1025 int i;
1026
1027 if (dfunc->df_defer_var_idx == 0)
1028 {
1029 iemsg("df_defer_var_idx is zero");
Bram Moolenaar31ea6bf2022-09-05 10:47:13 +01001030 vim_free(name);
Bram Moolenaar806a2732022-09-04 15:40:36 +01001031 return FAIL;
1032 }
Bram Moolenaar806a2732022-09-04 15:40:36 +01001033
Bram Moolenaarf5fec052022-09-11 11:49:22 +01001034 l = add_defer_item(dfunc->df_defer_var_idx - 1, argcount, current_ectx);
Bram Moolenaar806a2732022-09-04 15:40:36 +01001035 if (l == NULL)
1036 {
Bram Moolenaar31ea6bf2022-09-05 10:47:13 +01001037 vim_free(name);
Bram Moolenaar806a2732022-09-04 15:40:36 +01001038 return FAIL;
1039 }
1040
Bram Moolenaar31ea6bf2022-09-05 10:47:13 +01001041 func_tv.v_type = VAR_FUNC;
1042 func_tv.v_lock = 0;
1043 func_tv.vval.v_string = name;
Bram Moolenaar806a2732022-09-04 15:40:36 +01001044 list_set_item(l, 0, &func_tv);
Bram Moolenaar31ea6bf2022-09-05 10:47:13 +01001045
Bram Moolenaar806a2732022-09-04 15:40:36 +01001046 for (i = 0; i < argcount; ++i)
1047 list_set_item(l, i + 1, argvars + i);
1048 return OK;
1049}
1050
1051/*
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001052 * Invoked when returning from a function: Invoke any deferred calls.
1053 */
1054 static void
1055invoke_defer_funcs(ectx_T *ectx)
1056{
1057 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1058 + ectx->ec_dfunc_idx;
1059 typval_T *defer_tv = STACK_TV_VAR(dfunc->df_defer_var_idx - 1);
1060 listitem_T *li;
1061
1062 if (defer_tv->v_type != VAR_LIST)
1063 return; // no function added
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00001064 FOR_ALL_LIST_ITEMS(defer_tv->vval.v_list, li)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001065 {
1066 list_T *l = li->li_tv.vval.v_list;
1067 typval_T rettv;
1068 typval_T argvars[MAX_FUNC_ARGS];
1069 int i;
1070 listitem_T *arg_li = l->lv_first;
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001071 typval_T *functv = &l->lv_first->li_tv;
1072 int obj_off = functv->v_type == VAR_PARTIAL ? 1 : 0;
1073 int argcount = l->lv_len - 1 - obj_off;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001074
zeertzjqa1f2b5d2023-04-18 21:04:53 +01001075 if (functv->vval.v_string == NULL)
1076 // already being called, can happen if function does ":qa"
1077 continue;
1078
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001079 if (obj_off == 1)
1080 arg_li = arg_li->li_next; // second list item is the object
1081 for (i = 0; i < argcount; ++i)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001082 {
1083 arg_li = arg_li->li_next;
1084 argvars[i] = arg_li->li_tv;
1085 }
1086
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001087 funcexe_T funcexe;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001088 CLEAR_FIELD(funcexe);
1089 funcexe.fe_evaluate = TRUE;
1090 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001091 if (functv->v_type == VAR_PARTIAL)
1092 {
1093 funcexe.fe_partial = functv->vval.v_partial;
1094 funcexe.fe_object = l->lv_first->li_next->li_tv.vval.v_object;
1095 if (funcexe.fe_object != NULL)
1096 ++funcexe.fe_object->obj_refcount;
1097 }
zeertzjqa1f2b5d2023-04-18 21:04:53 +01001098
1099 char_u *name = functv->vval.v_string;
1100 functv->vval.v_string = NULL;
1101
1102 (void)call_func(name, -1, &rettv, argcount, argvars, &funcexe);
1103
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001104 clear_tv(&rettv);
zeertzjqa1f2b5d2023-04-18 21:04:53 +01001105 vim_free(name);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001106 }
1107}
1108
1109/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001110 * Return from the current function.
1111 */
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001112 static int
Bram Moolenaar4c137212021-04-19 16:48:48 +02001113func_return(ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001114{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001115 int idx;
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01001116 int ret_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001117 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1118 + ectx->ec_dfunc_idx;
1119 int argcount = ufunc_argcount(dfunc->df_ufunc);
Bram Moolenaarc620c052020-07-08 15:16:19 +02001120 estack_T *entry;
Bram Moolenaar12d26532021-02-19 19:13:21 +01001121 int prev_dfunc_idx = STACK_TV(ectx->ec_frame_idx
1122 + STACK_FRAME_FUNC_OFF)->vval.v_number;
Bram Moolenaarb06b50d2021-04-26 21:39:25 +02001123 funclocal_T *floc;
1124#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01001125 dfunc_T *prev_dfunc = ((dfunc_T *)def_functions.ga_data)
1126 + prev_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001127
Bram Moolenaar12d26532021-02-19 19:13:21 +01001128 if (do_profiling == PROF_YES)
1129 {
1130 ufunc_T *caller = prev_dfunc->df_ufunc;
1131
1132 if (dfunc->df_ufunc->uf_profiling
1133 || (caller != NULL && caller->uf_profiling))
1134 {
1135 profile_may_end_func(((profinfo_T *)profile_info_ga.ga_data)
1136 + profile_info_ga.ga_len - 1, dfunc->df_ufunc, caller);
1137 --profile_info_ga.ga_len;
1138 }
1139 }
1140#endif
Bram Moolenaar919c12c2021-12-14 14:29:16 +00001141
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001142 if (dfunc->df_defer_var_idx > 0)
1143 invoke_defer_funcs(ectx);
1144
Bram Moolenaar919c12c2021-12-14 14:29:16 +00001145 // No check for uf_refcount being zero, cannot think of a way that would
1146 // happen.
Bram Moolenaarc970e422021-03-17 15:03:04 +01001147 --dfunc->df_ufunc->uf_calls;
1148
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001149 // execution context goes one level up
Bram Moolenaarc620c052020-07-08 15:16:19 +02001150 entry = estack_pop();
1151 if (entry != NULL)
Bram Moolenaarc70fe462021-04-17 17:59:19 +02001152 current_sctx = entry->es_save_sctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001153
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001154 if (handle_closure_in_use(ectx, TRUE) == FAIL)
1155 return FAIL;
1156
Bram Moolenaar574950d2023-01-03 19:08:50 +00001157 // Clear the arguments. If this was an object method also clear the
1158 // object, it is just before the arguments.
1159 int top = ectx->ec_frame_idx - argcount;
Yegappan Lakshmanan1db15142023-09-19 20:34:05 +02001160 if (IS_OBJECT_METHOD(dfunc->df_ufunc))
Bram Moolenaar574950d2023-01-03 19:08:50 +00001161 --top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001162 for (idx = top; idx < ectx->ec_frame_idx; ++idx)
1163 clear_tv(STACK_TV(idx));
1164
1165 // Clear local variables and temp values, but not the return value.
1166 for (idx = ectx->ec_frame_idx + STACK_FRAME_SIZE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001167 idx < ectx->ec_stack.ga_len - 1; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001168 clear_tv(STACK_TV(idx));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001169
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01001170 // The return value should be on top of the stack. However, when aborting
1171 // it may not be there and ec_frame_idx is the top of the stack.
1172 ret_idx = ectx->ec_stack.ga_len - 1;
Bram Moolenaar0186e582021-01-10 18:33:11 +01001173 if (ret_idx == ectx->ec_frame_idx + STACK_FRAME_IDX_OFF)
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01001174 ret_idx = 0;
1175
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001176 if (ectx->ec_outer_ref != NULL)
1177 {
1178 if (ectx->ec_outer_ref->or_outer_allocated)
1179 vim_free(ectx->ec_outer_ref->or_outer);
1180 partial_unref(ectx->ec_outer_ref->or_partial);
1181 vim_free(ectx->ec_outer_ref);
1182 }
Bram Moolenaar0186e582021-01-10 18:33:11 +01001183
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001184 // Restore the previous frame.
Bram Moolenaar12d26532021-02-19 19:13:21 +01001185 ectx->ec_dfunc_idx = prev_dfunc_idx;
Bram Moolenaar0186e582021-01-10 18:33:11 +01001186 ectx->ec_iidx = STACK_TV(ectx->ec_frame_idx
1187 + STACK_FRAME_IIDX_OFF)->vval.v_number;
Bram Moolenaar5930ddc2021-04-26 20:32:59 +02001188 ectx->ec_instr = (void *)STACK_TV(ectx->ec_frame_idx
1189 + STACK_FRAME_INSTR_OFF)->vval.v_string;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001190 ectx->ec_outer_ref = (void *)STACK_TV(ectx->ec_frame_idx
Bram Moolenaar0186e582021-01-10 18:33:11 +01001191 + STACK_FRAME_OUTER_OFF)->vval.v_string;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001192 floc = (void *)STACK_TV(ectx->ec_frame_idx
1193 + STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string;
Bram Moolenaar5366e1a2020-10-01 13:01:34 +02001194 // restoring ec_frame_idx must be last
Bram Moolenaar0186e582021-01-10 18:33:11 +01001195 ectx->ec_frame_idx = STACK_TV(ectx->ec_frame_idx
1196 + STACK_FRAME_IDX_OFF)->vval.v_number;
Bram Moolenaard386e922021-04-25 14:48:49 +02001197
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001198 if (floc == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02001199 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001200 else
1201 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02001202 ectx->ec_funclocal = *floc;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001203 vim_free(floc);
1204 }
1205
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01001206 if (ret_idx > 0)
1207 {
1208 // Reset the stack to the position before the call, with a spot for the
1209 // return value, moved there from above the frame.
1210 ectx->ec_stack.ga_len = top + 1;
1211 *STACK_TV_BOT(-1) = *STACK_TV(ret_idx);
1212 }
1213 else
1214 // Reset the stack to the position before the call.
1215 ectx->ec_stack.ga_len = top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001216
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001217 funcdepth_decrement();
Bram Moolenaare99d4222021-06-13 14:01:26 +02001218 --ex_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001219 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001220}
1221
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001222/*
1223 * Prepare arguments and rettv for calling a builtin or user function.
1224 */
1225 static int
1226call_prepare(int argcount, typval_T *argvars, ectx_T *ectx)
1227{
1228 int idx;
1229 typval_T *tv;
1230
1231 // Move arguments from bottom of the stack to argvars[] and add terminator.
1232 for (idx = 0; idx < argcount; ++idx)
1233 argvars[idx] = *STACK_TV_BOT(idx - argcount);
1234 argvars[argcount].v_type = VAR_UNKNOWN;
1235
1236 // Result replaces the arguments on the stack.
1237 if (argcount > 0)
1238 ectx->ec_stack.ga_len -= argcount - 1;
Bram Moolenaar35578162021-08-02 19:10:38 +02001239 else if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001240 return FAIL;
1241 else
1242 ++ectx->ec_stack.ga_len;
1243
1244 // Default return value is zero.
1245 tv = STACK_TV_BOT(-1);
1246 tv->v_type = VAR_NUMBER;
1247 tv->vval.v_number = 0;
Bram Moolenaar24565cf2022-03-28 18:16:52 +01001248 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001249
1250 return OK;
1251}
1252
1253/*
1254 * Call a builtin function by index.
1255 */
1256 static int
1257call_bfunc(int func_idx, int argcount, ectx_T *ectx)
1258{
1259 typval_T argvars[MAX_FUNC_ARGS];
1260 int idx;
Bram Moolenaar171fb922020-10-28 16:54:47 +01001261 int did_emsg_before = did_emsg;
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001262 ectx_T *prev_ectx = current_ectx;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001263 char *save_func_name = ectx->ec_where.wt_func_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001264
1265 if (call_prepare(argcount, argvars, ectx) == FAIL)
1266 return FAIL;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001267 ectx->ec_where.wt_func_name = internal_func_name(func_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001268
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001269 // Call the builtin function. Set "current_ectx" so that when it
1270 // recursively invokes call_def_function() a closure context can be set.
1271 current_ectx = ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001272 call_internal_func_by_idx(func_idx, argvars, STACK_TV_BOT(-1));
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001273 current_ectx = prev_ectx;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001274 ectx->ec_where.wt_func_name = save_func_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001275
1276 // Clear the arguments.
1277 for (idx = 0; idx < argcount; ++idx)
1278 clear_tv(&argvars[idx]);
Bram Moolenaar015f4262020-05-05 21:25:22 +02001279
Bram Moolenaar57f799e2020-12-12 20:42:19 +01001280 if (did_emsg > did_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +02001281 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001282 return OK;
1283}
1284
1285/*
1286 * Execute a user defined function.
Bram Moolenaarab360522021-01-10 14:02:28 +01001287 * If the function is compiled this will add a stack frame and set the
1288 * instruction pointer at the start of the function.
1289 * Otherwise the function is called here.
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001290 * If "pt" is not null use "pt->pt_outer" for ec_outer_ref->or_outer.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001291 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001292 */
1293 static int
Bram Moolenaar0186e582021-01-10 18:33:11 +01001294call_ufunc(
1295 ufunc_T *ufunc,
1296 partial_T *pt,
1297 int argcount,
1298 ectx_T *ectx,
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001299 isn_T *iptr,
1300 dict_T *selfdict)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001301{
1302 typval_T argvars[MAX_FUNC_ARGS];
1303 funcexe_T funcexe;
Bram Moolenaar0917e862023-02-18 14:42:44 +00001304 funcerror_T error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001305 int idx;
Bram Moolenaar28ee8922020-10-28 20:20:00 +01001306 int did_emsg_before = did_emsg;
Bram Moolenaar139575d2022-03-15 19:29:30 +00001307 compiletype_T compile_type = get_compile_type(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001308
Bram Moolenaare99d4222021-06-13 14:01:26 +02001309 if (func_needs_compiling(ufunc, compile_type)
1310 && compile_def_function(ufunc, FALSE, compile_type, NULL)
1311 == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02001312 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001313 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001314 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +01001315 error = check_user_func_argcount(ufunc, argcount);
Bram Moolenaar50824712020-12-20 21:10:17 +01001316 if (error != FCERR_UNKNOWN)
1317 {
1318 if (error == FCERR_TOOMANY)
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001319 semsg(_(e_too_many_arguments_for_function_str),
1320 printable_func_name(ufunc));
Bram Moolenaar50824712020-12-20 21:10:17 +01001321 else
Bram Moolenaare1242042021-12-16 20:56:57 +00001322 semsg(_(e_not_enough_arguments_for_function_str),
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001323 printable_func_name(ufunc));
Bram Moolenaar50824712020-12-20 21:10:17 +01001324 return FAIL;
1325 }
1326
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001327 // The function has been compiled, can call it quickly. For a function
1328 // that was defined later: we can call it directly next time.
1329 if (iptr != NULL)
1330 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001331 delete_instr(iptr);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001332 iptr->isn_type = ISN_DCALL;
1333 iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1334 iptr->isn_arg.dfunc.cdf_argcount = argcount;
1335 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001336 return call_dfunc(ufunc->uf_dfunc_idx, pt, argcount, ectx);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001337 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001338
1339 if (call_prepare(argcount, argvars, ectx) == FAIL)
1340 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +02001341 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00001342 funcexe.fe_evaluate = TRUE;
1343 funcexe.fe_selfdict = selfdict != NULL ? selfdict : dict_stack_get_dict();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001344
1345 // Call the user function. Result goes in last position on the stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001346 error = call_user_func_check(ufunc, argcount, argvars,
Bram Moolenaar851f86b2021-12-13 14:26:44 +00001347 STACK_TV_BOT(-1), &funcexe, funcexe.fe_selfdict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001348
1349 // Clear the arguments.
1350 for (idx = 0; idx < argcount; ++idx)
1351 clear_tv(&argvars[idx]);
1352
1353 if (error != FCERR_NONE)
1354 {
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01001355 user_func_error(error, printable_func_name(ufunc),
1356 funcexe.fe_found_var);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001357 return FAIL;
1358 }
Bram Moolenaar28ee8922020-10-28 20:20:00 +01001359 if (did_emsg > did_emsg_before)
Bram Moolenaared677f52020-08-12 16:38:10 +02001360 // Error other than from calling the function itself.
1361 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001362 return OK;
1363}
1364
1365/*
Bram Moolenaara91a7132021-03-25 21:12:15 +01001366 * If command modifiers were applied restore them.
1367 */
1368 static void
1369may_restore_cmdmod(funclocal_T *funclocal)
1370{
1371 if (funclocal->floc_restore_cmdmod)
1372 {
1373 cmdmod.cmod_filter_regmatch.regprog = NULL;
1374 undo_cmdmod(&cmdmod);
1375 cmdmod = funclocal->floc_save_cmdmod;
1376 funclocal->floc_restore_cmdmod = FALSE;
1377 }
1378}
1379
1380/*
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001381 * Return TRUE if an error was given (not caught in try/catch) or CTRL-C was
1382 * pressed.
Bram Moolenaara1773442020-08-12 15:21:22 +02001383 */
1384 static int
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001385vim9_aborting(int prev_uncaught_emsg)
Bram Moolenaara1773442020-08-12 15:21:22 +02001386{
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001387 return uncaught_emsg > prev_uncaught_emsg || got_int || did_throw;
Bram Moolenaara1773442020-08-12 15:21:22 +02001388}
1389
1390/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001391 * Execute a function by "name".
1392 * This can be a builtin function or a user function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001393 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001394 * Returns FAIL if not found without an error message.
1395 */
1396 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001397call_by_name(
1398 char_u *name,
1399 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001400 ectx_T *ectx,
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001401 isn_T *iptr,
1402 dict_T *selfdict)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001403{
1404 ufunc_T *ufunc;
1405
1406 if (builtin_function(name, -1))
1407 {
1408 int func_idx = find_internal_func(name);
1409
Bram Moolenaar1983f1a2022-02-28 20:55:02 +00001410 if (func_idx < 0) // Impossible?
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001411 return FAIL;
Bram Moolenaar389df252020-07-09 21:20:47 +02001412 if (check_internal_func(func_idx, argcount) < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001413 return FAIL;
1414 return call_bfunc(func_idx, argcount, ectx);
1415 }
1416
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00001417 ufunc = find_func(name, FALSE);
Bram Moolenaara1773442020-08-12 15:21:22 +02001418
1419 if (ufunc == NULL)
1420 {
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001421 int prev_uncaught_emsg = uncaught_emsg;
Bram Moolenaara1773442020-08-12 15:21:22 +02001422
1423 if (script_autoload(name, TRUE))
1424 // loaded a package, search for the function again
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00001425 ufunc = find_func(name, FALSE);
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001426
1427 if (vim9_aborting(prev_uncaught_emsg))
Bram Moolenaara1773442020-08-12 15:21:22 +02001428 return FAIL; // bail out if loading the script caused an error
1429 }
1430
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001431 if (ufunc != NULL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001432 {
Bram Moolenaar0d89d8a2022-12-31 14:01:24 +00001433 if (check_ufunc_arg_types(ufunc, argcount, 0, ectx) == FAIL)
1434 return FAIL;
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001435
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001436 return call_ufunc(ufunc, NULL, argcount, ectx, iptr, selfdict);
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001437 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001438
1439 return FAIL;
1440}
1441
1442 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001443call_partial(
1444 typval_T *tv,
1445 int argcount_arg,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001446 ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001447{
Bram Moolenaara90afb92020-07-15 22:38:56 +02001448 int argcount = argcount_arg;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001449 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001450 int called_emsg_before = called_emsg;
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +01001451 int res = FAIL;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001452 dict_T *selfdict = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001453
1454 if (tv->v_type == VAR_PARTIAL)
1455 {
Bram Moolenaara90afb92020-07-15 22:38:56 +02001456 partial_T *pt = tv->vval.v_partial;
1457 int i;
1458
1459 if (pt->pt_argc > 0)
1460 {
1461 // Make space for arguments from the partial, shift the "argcount"
1462 // arguments up.
Bram Moolenaar35578162021-08-02 19:10:38 +02001463 if (GA_GROW_FAILS(&ectx->ec_stack, pt->pt_argc))
Bram Moolenaara90afb92020-07-15 22:38:56 +02001464 return FAIL;
1465 for (i = 1; i <= argcount; ++i)
1466 *STACK_TV_BOT(-i + pt->pt_argc) = *STACK_TV_BOT(-i);
1467 ectx->ec_stack.ga_len += pt->pt_argc;
1468 argcount += pt->pt_argc;
1469
1470 // copy the arguments from the partial onto the stack
1471 for (i = 0; i < pt->pt_argc; ++i)
1472 copy_tv(&pt->pt_argv[i], STACK_TV_BOT(-argcount + i));
1473 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001474 selfdict = pt->pt_dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001475
1476 if (pt->pt_func != NULL)
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001477 return call_ufunc(pt->pt_func, pt, argcount, ectx, NULL, selfdict);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001478
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001479 name = pt->pt_name;
1480 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001481 else if (tv->v_type == VAR_FUNC)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001482 name = tv->vval.v_string;
Bram Moolenaar95006e32020-08-29 17:47:08 +02001483 if (name != NULL)
1484 {
Bram Moolenaar0917e862023-02-18 14:42:44 +00001485 char_u fname_buf[FLEN_FIXED + 1];
1486 char_u *tofree = NULL;
1487 funcerror_T error = FCERR_NONE;
1488 char_u *fname;
Bram Moolenaar95006e32020-08-29 17:47:08 +02001489
1490 // May need to translate <SNR>123_ to K_SNR.
1491 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
1492 if (error != FCERR_NONE)
1493 res = FAIL;
1494 else
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001495 res = call_by_name(fname, argcount, ectx, NULL, selfdict);
Bram Moolenaar95006e32020-08-29 17:47:08 +02001496 vim_free(tofree);
1497 }
1498
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +01001499 if (res == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001500 {
1501 if (called_emsg == called_emsg_before)
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001502 emsg_funcname(e_unknown_function_str,
Bram Moolenaar015f4262020-05-05 21:25:22 +02001503 name == NULL ? (char_u *)"[unknown]" : name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001504 return FAIL;
1505 }
1506 return OK;
1507}
1508
1509/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001510 * Check if "lock" is VAR_LOCKED or VAR_FIXED. If so give an error and return
1511 * TRUE.
1512 */
1513 static int
1514error_if_locked(int lock, char *error)
1515{
1516 if (lock & (VAR_LOCKED | VAR_FIXED))
1517 {
1518 emsg(_(error));
1519 return TRUE;
1520 }
1521 return FALSE;
1522}
1523
1524/*
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001525 * Give an error if "tv" is not a number and return FAIL.
1526 */
1527 static int
1528check_for_number(typval_T *tv)
1529{
1530 if (tv->v_type != VAR_NUMBER)
1531 {
1532 semsg(_(e_expected_str_but_got_str),
1533 vartype_name(VAR_NUMBER), vartype_name(tv->v_type));
1534 return FAIL;
1535 }
1536 return OK;
1537}
1538
1539/*
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001540 * Store "tv" in variable "name".
1541 * This is for s: and g: variables.
1542 */
1543 static void
1544store_var(char_u *name, typval_T *tv)
1545{
1546 funccal_entry_T entry;
Bram Moolenaard877a572021-04-01 19:42:48 +02001547 int flags = ASSIGN_DECL;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001548
Bram Moolenaard877a572021-04-01 19:42:48 +02001549 if (tv->v_lock)
1550 flags |= ASSIGN_CONST;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001551 save_funccal(&entry);
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001552 set_var_const(name, 0, NULL, tv, FALSE, flags, 0);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001553 restore_funccal();
1554}
1555
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001556/*
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001557 * Convert "tv" to a string.
1558 * Return FAIL if not allowed.
1559 */
1560 static int
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001561do_2string(typval_T *tv, int is_2string_any, int tolerant)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001562{
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00001563 if (tv->v_type == VAR_STRING)
1564 return OK;
1565
1566 char_u *str;
1567
1568 if (is_2string_any)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001569 {
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00001570 switch (tv->v_type)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001571 {
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00001572 case VAR_SPECIAL:
1573 case VAR_BOOL:
1574 case VAR_NUMBER:
1575 case VAR_FLOAT:
1576 case VAR_BLOB: break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001577
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00001578 case VAR_LIST:
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001579 if (tolerant)
1580 {
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001581 char_u *s, *e, *p;
1582 garray_T ga;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001583
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001584 ga_init2(&ga, sizeof(char_u *), 1);
1585
1586 // Convert to NL separated items, then
1587 // escape the items and replace the NL with
1588 // a space.
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001589 str = typval2string(tv, TRUE);
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001590 if (str == NULL)
1591 return FAIL;
1592 s = str;
1593 while ((e = vim_strchr(s, '\n')) != NULL)
1594 {
1595 *e = NUL;
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +01001596 p = vim_strsave_fnameescape(s,
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00001597 VSE_NONE);
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001598 if (p != NULL)
1599 {
1600 ga_concat(&ga, p);
1601 ga_concat(&ga, (char_u *)" ");
1602 vim_free(p);
1603 }
1604 s = e + 1;
1605 }
1606 vim_free(str);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001607 clear_tv(tv);
1608 tv->v_type = VAR_STRING;
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001609 tv->vval.v_string = ga.ga_data;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001610 return OK;
1611 }
1612 // FALLTHROUGH
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00001613 default: to_string_error(tv->v_type);
1614 return FAIL;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001615 }
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001616 }
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00001617 str = typval_tostring(tv, TRUE);
1618 clear_tv(tv);
1619 tv->v_type = VAR_STRING;
1620 tv->vval.v_string = str;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001621 return OK;
1622}
1623
1624/*
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001625 * When the value of "sv" is a null list of dict, allocate it.
1626 */
1627 static void
Bram Moolenaar859cc212022-03-28 15:22:35 +01001628allocate_if_null(svar_T *sv)
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001629{
Bram Moolenaar859cc212022-03-28 15:22:35 +01001630 typval_T *tv = sv->sv_tv;
1631
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001632 switch (tv->v_type)
1633 {
1634 case VAR_LIST:
Bram Moolenaar859cc212022-03-28 15:22:35 +01001635 if (tv->vval.v_list == NULL && sv->sv_type != &t_list_empty)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001636 (void)rettv_list_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001637 break;
1638 case VAR_DICT:
Bram Moolenaar859cc212022-03-28 15:22:35 +01001639 if (tv->vval.v_dict == NULL && sv->sv_type != &t_dict_empty)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001640 (void)rettv_dict_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001641 break;
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02001642 case VAR_BLOB:
Bram Moolenaar859cc212022-03-28 15:22:35 +01001643 if (tv->vval.v_blob == NULL && sv->sv_type != &t_blob_null)
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02001644 (void)rettv_blob_alloc(tv);
1645 break;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001646 default:
1647 break;
1648 }
1649}
Bram Moolenaard3aac292020-04-19 14:32:17 +02001650
Bram Moolenaare7525c52021-01-09 13:20:37 +01001651/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001652 * Return the character "str[index]" where "index" is the character index,
1653 * including composing characters.
1654 * If "index" is out of range NULL is returned.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001655 */
1656 char_u *
1657char_from_string(char_u *str, varnumber_T index)
1658{
1659 size_t nbyte = 0;
1660 varnumber_T nchar = index;
1661 size_t slen;
1662
1663 if (str == NULL)
1664 return NULL;
1665 slen = STRLEN(str);
1666
Bram Moolenaarff871402021-03-26 13:34:05 +01001667 // Do the same as for a list: a negative index counts from the end.
1668 // Optimization to check the first byte to be below 0x80 (and no composing
1669 // character follows) makes this a lot faster.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001670 if (index < 0)
1671 {
1672 int clen = 0;
1673
1674 for (nbyte = 0; nbyte < slen; ++clen)
Bram Moolenaarff871402021-03-26 13:34:05 +01001675 {
1676 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1677 ++nbyte;
1678 else if (enc_utf8)
1679 nbyte += utfc_ptr2len(str + nbyte);
1680 else
1681 nbyte += mb_ptr2len(str + nbyte);
1682 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001683 nchar = clen + index;
1684 if (nchar < 0)
1685 // unlike list: index out of range results in empty string
1686 return NULL;
1687 }
1688
1689 for (nbyte = 0; nchar > 0 && nbyte < slen; --nchar)
Bram Moolenaarff871402021-03-26 13:34:05 +01001690 {
1691 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1692 ++nbyte;
1693 else if (enc_utf8)
1694 nbyte += utfc_ptr2len(str + nbyte);
1695 else
1696 nbyte += mb_ptr2len(str + nbyte);
1697 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001698 if (nbyte >= slen)
1699 return NULL;
Bram Moolenaar0289a092021-03-14 18:40:19 +01001700 return vim_strnsave(str + nbyte, mb_ptr2len(str + nbyte));
Bram Moolenaare7525c52021-01-09 13:20:37 +01001701}
1702
1703/*
1704 * Get the byte index for character index "idx" in string "str" with length
Bram Moolenaar0289a092021-03-14 18:40:19 +01001705 * "str_len". Composing characters are included.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001706 * If going over the end return "str_len".
1707 * If "idx" is negative count from the end, -1 is the last character.
1708 * When going over the start return -1.
1709 */
1710 static long
1711char_idx2byte(char_u *str, size_t str_len, varnumber_T idx)
1712{
1713 varnumber_T nchar = idx;
1714 size_t nbyte = 0;
1715
1716 if (nchar >= 0)
1717 {
1718 while (nchar > 0 && nbyte < str_len)
1719 {
Bram Moolenaar0289a092021-03-14 18:40:19 +01001720 nbyte += mb_ptr2len(str + nbyte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001721 --nchar;
1722 }
1723 }
1724 else
1725 {
1726 nbyte = str_len;
1727 while (nchar < 0 && nbyte > 0)
1728 {
1729 --nbyte;
1730 nbyte -= mb_head_off(str, str + nbyte);
1731 ++nchar;
1732 }
1733 if (nchar < 0)
1734 return -1;
1735 }
1736 return (long)nbyte;
1737}
1738
1739/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001740 * Return the slice "str[first : last]" using character indexes. Composing
1741 * characters are included.
Bram Moolenaar6601b622021-01-13 21:47:15 +01001742 * "exclusive" is TRUE for slice().
Bram Moolenaare7525c52021-01-09 13:20:37 +01001743 * Return NULL when the result is empty.
1744 */
1745 char_u *
Bram Moolenaar6601b622021-01-13 21:47:15 +01001746string_slice(char_u *str, varnumber_T first, varnumber_T last, int exclusive)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001747{
1748 long start_byte, end_byte;
1749 size_t slen;
1750
1751 if (str == NULL)
1752 return NULL;
1753 slen = STRLEN(str);
1754 start_byte = char_idx2byte(str, slen, first);
1755 if (start_byte < 0)
1756 start_byte = 0; // first index very negative: use zero
Bram Moolenaar6601b622021-01-13 21:47:15 +01001757 if ((last == -1 && !exclusive) || last == VARNUM_MAX)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001758 end_byte = (long)slen;
1759 else
1760 {
1761 end_byte = char_idx2byte(str, slen, last);
Bram Moolenaar6601b622021-01-13 21:47:15 +01001762 if (!exclusive && end_byte >= 0 && end_byte < (long)slen)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001763 // end index is inclusive
Bram Moolenaar0289a092021-03-14 18:40:19 +01001764 end_byte += mb_ptr2len(str + end_byte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001765 }
1766
1767 if (start_byte >= (long)slen || end_byte <= start_byte)
1768 return NULL;
1769 return vim_strnsave(str + start_byte, end_byte - start_byte);
1770}
1771
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001772/*
1773 * Get a script variable for ISN_STORESCRIPT and ISN_LOADSCRIPT.
1774 * When "dfunc_idx" is negative don't give an error.
1775 * Returns NULL for an error.
1776 */
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001777 static svar_T *
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001778get_script_svar(scriptref_T *sref, int dfunc_idx)
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001779{
1780 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001781 dfunc_T *dfunc = dfunc_idx < 0 ? NULL
1782 : ((dfunc_T *)def_functions.ga_data) + dfunc_idx;
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001783 svar_T *sv;
1784
1785 if (sref->sref_seq != si->sn_script_seq)
1786 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001787 // The script was reloaded after the function was compiled, the
1788 // script_idx may not be valid.
1789 if (dfunc != NULL)
1790 semsg(_(e_script_variable_invalid_after_reload_in_function_str),
1791 printable_func_name(dfunc->df_ufunc));
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001792 return NULL;
1793 }
1794 sv = ((svar_T *)si->sn_var_vals.ga_data) + sref->sref_idx;
Bram Moolenaar6de22962022-09-09 21:35:36 +01001795 if (sv->sv_name == NULL)
1796 {
1797 if (dfunc != NULL)
1798 emsg(_(e_script_variable_was_deleted));
1799 return NULL;
1800 }
Bram Moolenaar60dc8272021-07-29 22:48:54 +02001801 if (!equal_type(sv->sv_type, sref->sref_type, 0))
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001802 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001803 if (dfunc != NULL)
1804 emsg(_(e_script_variable_type_changed));
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001805 return NULL;
1806 }
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001807
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01001808 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0
1809 && sref->sref_sid != current_sctx.sc_sid)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001810 {
1811 if (dfunc != NULL)
1812 semsg(_(e_item_not_exported_in_script_str), sv->sv_name);
1813 return NULL;
1814 }
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001815 return sv;
1816}
1817
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001818/*
Bram Moolenaar20677332021-06-06 17:02:53 +02001819 * Function passed to do_cmdline() for splitting a script joined by NL
1820 * characters.
1821 */
1822 static char_u *
1823get_split_sourceline(
1824 int c UNUSED,
1825 void *cookie,
1826 int indent UNUSED,
1827 getline_opt_T options UNUSED)
1828{
1829 source_cookie_T *sp = (source_cookie_T *)cookie;
1830 char_u *p;
1831 char_u *line;
1832
Bram Moolenaar20677332021-06-06 17:02:53 +02001833 p = vim_strchr(sp->nextline, '\n');
1834 if (p == NULL)
1835 {
1836 line = vim_strsave(sp->nextline);
1837 sp->nextline += STRLEN(sp->nextline);
1838 }
1839 else
1840 {
1841 line = vim_strnsave(sp->nextline, p - sp->nextline);
1842 sp->nextline = p + 1;
1843 }
1844 return line;
1845}
1846
1847/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001848 * Execute a function by "name".
1849 * This can be a builtin function, user function or a funcref.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001850 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001851 */
1852 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001853call_eval_func(
1854 char_u *name,
1855 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001856 ectx_T *ectx,
1857 isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001858{
Bram Moolenaared677f52020-08-12 16:38:10 +02001859 int called_emsg_before = called_emsg;
1860 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001861
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001862 res = call_by_name(name, argcount, ectx, iptr, NULL);
Bram Moolenaared677f52020-08-12 16:38:10 +02001863 if (res == FAIL && called_emsg == called_emsg_before)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001864 {
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001865 dictitem_T *v;
1866
1867 v = find_var(name, NULL, FALSE);
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001868 if (v == NULL || (v->di_tv.v_type != VAR_PARTIAL
1869 && v->di_tv.v_type != VAR_FUNC))
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001870 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001871 emsg_funcname(e_unknown_function_str, name);
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001872 return FAIL;
1873 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001874 return call_partial(&v->di_tv, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001875 }
Bram Moolenaared677f52020-08-12 16:38:10 +02001876 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001877}
1878
1879/*
Bram Moolenaarf112f302020-12-20 17:47:52 +01001880 * When a function reference is used, fill a partial with the information
1881 * needed, especially when it is used as a closure.
1882 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001883 int
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001884fill_partial_and_closure(
Bram Moolenaarcc341812022-09-19 15:54:34 +01001885 partial_T *pt,
1886 ufunc_T *ufunc,
1887 loopvarinfo_T *lvi,
1888 ectx_T *ectx)
Bram Moolenaarf112f302020-12-20 17:47:52 +01001889{
1890 pt->pt_func = ufunc;
1891 pt->pt_refcount = 1;
1892
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001893 if (ufunc->uf_flags & FC_CLOSURE)
Bram Moolenaarf112f302020-12-20 17:47:52 +01001894 {
1895 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1896 + ectx->ec_dfunc_idx;
1897
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001898 // The closure may need to find arguments and local variables of the
1899 // current function in the stack.
Bram Moolenaar0186e582021-01-10 18:33:11 +01001900 pt->pt_outer.out_stack = &ectx->ec_stack;
1901 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001902 if (ectx->ec_outer_ref != NULL)
1903 {
1904 // The current context already has a context, link to that one.
1905 pt->pt_outer.out_up = ectx->ec_outer_ref->or_outer;
1906 if (ectx->ec_outer_ref->or_partial != NULL)
1907 {
1908 pt->pt_outer.out_up_partial = ectx->ec_outer_ref->or_partial;
1909 ++pt->pt_outer.out_up_partial->pt_refcount;
1910 }
1911 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01001912
Bram Moolenaarcc341812022-09-19 15:54:34 +01001913 if (lvi != NULL)
1914 {
1915 int depth;
1916
1917 // The closure may need to find variables defined inside a loop,
1918 // for every nested loop. A new reference is made every time,
1919 // ISN_ENDLOOP will check if they are actually used.
1920 for (depth = 0; depth < lvi->lvi_depth; ++depth)
1921 {
1922 pt->pt_outer.out_loop[depth].stack = &ectx->ec_stack;
1923 pt->pt_outer.out_loop[depth].var_idx = ectx->ec_frame_idx
1924 + STACK_FRAME_SIZE + lvi->lvi_loop[depth].var_idx;
1925 pt->pt_outer.out_loop[depth].var_count =
1926 lvi->lvi_loop[depth].var_count;
1927 }
Bram Moolenaar6d313be2022-09-22 16:36:25 +01001928 pt->pt_outer.out_loop_size = lvi->lvi_depth;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001929 }
Bram Moolenaar6d313be2022-09-22 16:36:25 +01001930 else
1931 pt->pt_outer.out_loop_size = 0;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001932
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001933 // If the function currently executing returns and the closure is still
1934 // being referenced, we need to make a copy of the context (arguments
1935 // and local variables) so that the closure can use it later.
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001936 // Store a reference to the partial so we can handle that.
Bram Moolenaar35578162021-08-02 19:10:38 +02001937 if (GA_GROW_FAILS(&ectx->ec_funcrefs, 1))
Bram Moolenaarf112f302020-12-20 17:47:52 +01001938 {
1939 vim_free(pt);
1940 return FAIL;
1941 }
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001942 // Extra variable keeps the count of closures created in the current
1943 // function call.
Bram Moolenaarf112f302020-12-20 17:47:52 +01001944 ++(((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx
1945 + STACK_FRAME_SIZE + dfunc->df_varcount)->vval.v_number;
1946
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001947 ((partial_T **)ectx->ec_funcrefs.ga_data)[ectx->ec_funcrefs.ga_len]
1948 = pt;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001949 ++pt->pt_refcount;
1950 ++ectx->ec_funcrefs.ga_len;
1951 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001952 ++ufunc->uf_refcount;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001953 return OK;
1954}
1955
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001956/*
1957 * Execute iptr->isn_arg.string as an Ex command.
1958 */
1959 static int
1960exec_command(isn_T *iptr)
1961{
1962 source_cookie_T cookie;
1963
1964 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00001965 // Pass getsourceline to get an error for a missing ":end" command.
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001966 CLEAR_FIELD(cookie);
1967 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1968 if (do_cmdline(iptr->isn_arg.string,
1969 getsourceline, &cookie,
1970 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED) == FAIL
1971 || did_emsg)
1972 return FAIL;
1973 return OK;
1974}
1975
Bram Moolenaar89445512022-04-14 12:58:23 +01001976/*
1977 * If script "sid" is not loaded yet then load it now.
1978 * Caller must make sure "sid" is a valid script ID.
1979 * "loaded" is set to TRUE if the script had to be loaded.
1980 * Returns FAIL if loading fails, OK if already loaded or loaded now.
1981 */
1982 int
1983may_load_script(int sid, int *loaded)
1984{
1985 scriptitem_T *si = SCRIPT_ITEM(sid);
1986
1987 if (si->sn_state == SN_STATE_NOT_LOADED)
1988 {
1989 *loaded = TRUE;
1990 if (do_source(si->sn_name, FALSE, DOSO_NONE, NULL) == FAIL)
1991 {
1992 semsg(_(e_cant_open_file_str), si->sn_name);
1993 return FAIL;
1994 }
1995 }
1996 return OK;
1997}
1998
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001999// used for v_instr of typval of VAR_INSTR
2000struct instr_S {
2001 ectx_T *instr_ectx;
2002 isn_T *instr_instr;
2003};
2004
Bram Moolenaar4c137212021-04-19 16:48:48 +02002005// used for substitute_instr
2006typedef struct subs_expr_S {
2007 ectx_T *subs_ectx;
2008 isn_T *subs_instr;
2009 int subs_status;
2010} subs_expr_T;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002011
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002012// Set when calling do_debug().
2013static ectx_T *debug_context = NULL;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02002014static int debug_var_count;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002015
2016/*
2017 * When debugging lookup "name" and return the typeval.
2018 * When not found return NULL.
2019 */
2020 typval_T *
2021lookup_debug_var(char_u *name)
2022{
2023 int idx;
2024 dfunc_T *dfunc;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02002025 ufunc_T *ufunc;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002026 ectx_T *ectx = debug_context;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02002027 int varargs_off;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002028
2029 if (ectx == NULL)
2030 return NULL;
2031 dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
2032
2033 // Go through the local variable names, from last to first.
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02002034 for (idx = debug_var_count - 1; idx >= 0; --idx)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002035 {
Bram Moolenaare406ff82022-03-10 20:47:43 +00002036 char_u *varname = ((char_u **)dfunc->df_var_names.ga_data)[idx];
2037
2038 // the variable name may be NULL when not available in this block
2039 if (varname != NULL && STRCMP(varname, name) == 0)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002040 return STACK_TV_VAR(idx);
2041 }
2042
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02002043 // Go through argument names.
2044 ufunc = dfunc->df_ufunc;
2045 varargs_off = ufunc->uf_va_name == NULL ? 0 : 1;
2046 for (idx = 0; idx < ufunc->uf_args.ga_len; ++idx)
2047 if (STRCMP(((char_u **)(ufunc->uf_args.ga_data))[idx], name) == 0)
2048 return STACK_TV(ectx->ec_frame_idx - ufunc->uf_args.ga_len
2049 - varargs_off + idx);
2050 if (ufunc->uf_va_name != NULL && STRCMP(ufunc->uf_va_name, name) == 0)
2051 return STACK_TV(ectx->ec_frame_idx - 1);
2052
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002053 return NULL;
2054}
2055
Bram Moolenaar26a44842021-09-02 18:49:06 +02002056/*
2057 * Return TRUE if there might be a breakpoint in "ufunc", which is when a
2058 * breakpoint was set in that function or when there is any expression.
2059 */
2060 int
2061may_break_in_function(ufunc_T *ufunc)
2062{
2063 return ufunc->uf_has_breakpoint || debug_has_expr_breakpoint();
2064}
2065
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002066 static void
2067handle_debug(isn_T *iptr, ectx_T *ectx)
2068{
2069 char_u *line;
2070 ufunc_T *ufunc = (((dfunc_T *)def_functions.ga_data)
2071 + ectx->ec_dfunc_idx)->df_ufunc;
2072 isn_T *ni;
2073 int end_lnum = iptr->isn_lnum;
2074 garray_T ga;
2075 int lnum;
2076
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02002077 if (ex_nesting_level > debug_break_level)
2078 {
2079 linenr_T breakpoint;
2080
Bram Moolenaar26a44842021-09-02 18:49:06 +02002081 if (!may_break_in_function(ufunc))
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02002082 return;
2083
2084 // check for the next breakpoint if needed
2085 breakpoint = dbg_find_breakpoint(FALSE, ufunc->uf_name,
Bram Moolenaar8cec9272021-06-23 20:20:53 +02002086 iptr->isn_arg.debug.dbg_break_lnum);
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02002087 if (breakpoint <= 0 || breakpoint > iptr->isn_lnum)
2088 return;
2089 }
2090
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002091 SOURCING_LNUM = iptr->isn_lnum;
2092 debug_context = ectx;
Bram Moolenaar8cec9272021-06-23 20:20:53 +02002093 debug_var_count = iptr->isn_arg.debug.dbg_var_names_len;
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002094
2095 for (ni = iptr + 1; ni->isn_type != ISN_FINISH; ++ni)
2096 if (ni->isn_type == ISN_DEBUG
2097 || ni->isn_type == ISN_RETURN
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002098 || ni->isn_type == ISN_RETURN_OBJECT
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002099 || ni->isn_type == ISN_RETURN_VOID)
2100 {
Bram Moolenaar112bed02021-11-23 22:16:34 +00002101 end_lnum = ni->isn_lnum + (ni->isn_type == ISN_DEBUG ? 0 : 1);
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002102 break;
2103 }
2104
2105 if (end_lnum > iptr->isn_lnum)
2106 {
2107 ga_init2(&ga, sizeof(char_u *), 10);
Bram Moolenaar310091d2021-12-23 21:14:37 +00002108 for (lnum = iptr->isn_lnum; lnum < end_lnum
2109 && lnum <= ufunc->uf_lines.ga_len; ++lnum)
Bram Moolenaar59b50c32021-06-17 22:27:48 +02002110 {
Bram Moolenaar303215d2021-07-07 20:10:43 +02002111 char_u *p = ((char_u **)ufunc->uf_lines.ga_data)[lnum - 1];
Bram Moolenaar59b50c32021-06-17 22:27:48 +02002112
Bram Moolenaar303215d2021-07-07 20:10:43 +02002113 if (p == NULL)
2114 continue; // left over from continuation line
2115 p = skipwhite(p);
Bram Moolenaar59b50c32021-06-17 22:27:48 +02002116 if (*p == '#')
2117 break;
Bram Moolenaar35578162021-08-02 19:10:38 +02002118 if (GA_GROW_OK(&ga, 1))
Bram Moolenaar59b50c32021-06-17 22:27:48 +02002119 ((char_u **)(ga.ga_data))[ga.ga_len++] = p;
2120 if (STRNCMP(p, "def ", 4) == 0)
2121 break;
2122 }
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002123 line = ga_concat_strings(&ga, " ");
2124 vim_free(ga.ga_data);
2125 }
2126 else
2127 line = ((char_u **)ufunc->uf_lines.ga_data)[iptr->isn_lnum - 1];
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002128
Dominique Pelle6e969552021-06-17 13:53:41 +02002129 do_debug(line == NULL ? (char_u *)"[empty]" : line);
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002130 debug_context = NULL;
2131
2132 if (end_lnum > iptr->isn_lnum)
2133 vim_free(line);
2134}
2135
Bram Moolenaar4c137212021-04-19 16:48:48 +02002136/*
Bram Moolenaar65b0d162022-12-13 18:43:22 +00002137 * Store a value in a list, dict, blob or object variable.
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002138 * Returns OK, FAIL or NOTDONE (uncatchable error).
2139 */
2140 static int
2141execute_storeindex(isn_T *iptr, ectx_T *ectx)
2142{
Bram Moolenaarf7d1c6e2023-01-16 20:47:57 +00002143 vartype_T dest_type = iptr->isn_arg.storeindex.si_vartype;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002144 typval_T *tv;
2145 typval_T *tv_idx = STACK_TV_BOT(-2);
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002146 long lidx = 0;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002147 typval_T *tv_dest = STACK_TV_BOT(-1);
2148 int status = OK;
2149
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002150 if (tv_idx->v_type == VAR_NUMBER)
2151 lidx = (long)tv_idx->vval.v_number;
2152
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002153 // Stack contains:
2154 // -3 value to be stored
2155 // -2 index
Yegappan Lakshmanan3775f772023-09-01 22:05:45 +02002156 // -1 dict, list, blob, object or class
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002157 tv = STACK_TV_BOT(-3);
2158 SOURCING_LNUM = iptr->isn_lnum;
Gianmaria Bajod7085a02023-08-31 18:15:26 +02002159
2160 // Make sure an object has been initialized
2161 if (dest_type == VAR_OBJECT && tv_dest->vval.v_object == NULL)
2162 {
2163 emsg(_(e_using_null_object));
2164 status = FAIL;
2165 }
2166 else if (dest_type == VAR_ANY)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002167 {
2168 dest_type = tv_dest->v_type;
2169 if (dest_type == VAR_DICT)
2170 status = do_2string(tv_idx, TRUE, FALSE);
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002171 else if (dest_type == VAR_OBJECT && tv_idx->v_type == VAR_STRING)
2172 {
2173 // Need to get the member index now that the class is known.
2174 object_T *obj = tv_dest->vval.v_object;
2175 class_T *cl = obj->obj_class;
2176 char_u *member = tv_idx->vval.v_string;
2177
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02002178 int m_idx;
2179 ocmember_T *m = object_member_lookup(cl, member, 0, &m_idx);
2180 if (m != NULL)
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002181 {
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02002182 if (*member == '_')
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002183 {
RestorerZ7fe8f432023-09-24 23:21:24 +02002184 semsg(_(e_cannot_access_private_variable_str), m->ocm_name);
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02002185 status = FAIL;
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002186 }
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002187
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02002188 lidx = m_idx;
2189 }
2190 else
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002191 {
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +02002192 member_not_found_msg(cl, VAR_OBJECT, member, 0);
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002193 status = FAIL;
2194 }
2195 }
2196 else if ((dest_type == VAR_LIST || dest_type == VAR_OBJECT)
2197 && tv_idx->v_type != VAR_NUMBER)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002198 {
2199 emsg(_(e_number_expected));
2200 status = FAIL;
2201 }
2202 }
Bram Moolenaare08be092022-02-17 13:08:26 +00002203
2204 if (status == OK)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002205 {
Bram Moolenaare08be092022-02-17 13:08:26 +00002206 if (dest_type == VAR_LIST)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002207 {
Bram Moolenaare08be092022-02-17 13:08:26 +00002208 list_T *list = tv_dest->vval.v_list;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002209
Bram Moolenaare08be092022-02-17 13:08:26 +00002210 if (list == NULL)
2211 {
2212 emsg(_(e_list_not_set));
2213 return FAIL;
2214 }
2215 if (lidx < 0 && list->lv_len + lidx >= 0)
2216 // negative index is relative to the end
2217 lidx = list->lv_len + lidx;
2218 if (lidx < 0 || lidx > list->lv_len)
2219 {
2220 semsg(_(e_list_index_out_of_range_nr), lidx);
2221 return FAIL;
2222 }
2223 if (lidx < list->lv_len)
2224 {
2225 listitem_T *li = list_find(list, lidx);
2226
2227 if (error_if_locked(li->li_tv.v_lock,
Bram Moolenaar70c43d82022-01-26 21:01:15 +00002228 e_cannot_change_locked_list_item))
Bram Moolenaare08be092022-02-17 13:08:26 +00002229 return FAIL;
2230 // overwrite existing list item
2231 clear_tv(&li->li_tv);
2232 li->li_tv = *tv;
2233 }
2234 else
2235 {
2236 if (error_if_locked(list->lv_lock, e_cannot_change_locked_list))
2237 return FAIL;
2238 // append to list, only fails when out of memory
2239 if (list_append_tv(list, tv) == FAIL)
2240 return NOTDONE;
2241 clear_tv(tv);
2242 }
2243 }
2244 else if (dest_type == VAR_DICT)
2245 {
2246 char_u *key = tv_idx->vval.v_string;
2247 dict_T *dict = tv_dest->vval.v_dict;
2248 dictitem_T *di;
2249
2250 SOURCING_LNUM = iptr->isn_lnum;
2251 if (dict == NULL)
2252 {
2253 emsg(_(e_dictionary_not_set));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002254 return FAIL;
Bram Moolenaare08be092022-02-17 13:08:26 +00002255 }
2256 if (key == NULL)
2257 key = (char_u *)"";
2258 di = dict_find(dict, key, -1);
2259 if (di != NULL)
2260 {
2261 if (error_if_locked(di->di_tv.v_lock,
2262 e_cannot_change_dict_item))
2263 return FAIL;
2264 // overwrite existing value
2265 clear_tv(&di->di_tv);
2266 di->di_tv = *tv;
2267 }
2268 else
2269 {
2270 if (error_if_locked(dict->dv_lock, e_cannot_change_dict))
2271 return FAIL;
2272 // add to dict, only fails when out of memory
2273 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
2274 return NOTDONE;
2275 clear_tv(tv);
2276 }
2277 }
2278 else if (dest_type == VAR_BLOB)
2279 {
Bram Moolenaare08be092022-02-17 13:08:26 +00002280 blob_T *blob = tv_dest->vval.v_blob;
Bram Moolenaar65b0d162022-12-13 18:43:22 +00002281 varnumber_T nr;
2282 int error = FALSE;
2283 int len;
Bram Moolenaare08be092022-02-17 13:08:26 +00002284
2285 if (blob == NULL)
2286 {
2287 emsg(_(e_blob_not_set));
2288 return FAIL;
2289 }
2290 len = blob_len(blob);
2291 if (lidx < 0 && len + lidx >= 0)
2292 // negative index is relative to the end
2293 lidx = len + lidx;
2294
2295 // Can add one byte at the end.
2296 if (lidx < 0 || lidx > len)
2297 {
2298 semsg(_(e_blob_index_out_of_range_nr), lidx);
2299 return FAIL;
2300 }
2301 if (value_check_lock(blob->bv_lock, (char_u *)"blob", FALSE))
2302 return FAIL;
2303 nr = tv_get_number_chk(tv, &error);
2304 if (error)
2305 return FAIL;
2306 blob_set_append(blob, lidx, nr);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002307 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002308 else if (dest_type == VAR_CLASS || dest_type == VAR_OBJECT)
2309 {
Yegappan Lakshmanan3775f772023-09-01 22:05:45 +02002310 typval_T *otv;
Bram Moolenaarf7d1c6e2023-01-16 20:47:57 +00002311
Yegappan Lakshmanan3775f772023-09-01 22:05:45 +02002312 if (dest_type == VAR_OBJECT)
2313 {
2314 object_T *obj = tv_dest->vval.v_object;
2315
2316 otv = (typval_T *)(obj + 1);
2317 class_T *itf = iptr->isn_arg.storeindex.si_class;
2318 if (itf != NULL)
2319 // convert interface member index to class member index
Ernie Rael18143d32023-09-04 22:30:41 +02002320 lidx = object_index_from_itf_index(itf, FALSE, lidx,
Yegappan Lakshmanan5a05d372023-09-29 19:43:11 +02002321 obj->obj_class);
Yegappan Lakshmanan3775f772023-09-01 22:05:45 +02002322 }
2323 else
2324 {
2325 // VAR_CLASS
2326 class_T *class = tv_dest->vval.v_class;
2327 otv = class->class_members_tv;
2328 }
Bram Moolenaarf7d1c6e2023-01-16 20:47:57 +00002329
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002330 clear_tv(&otv[lidx]);
2331 otv[lidx] = *tv;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002332 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002333 else
2334 {
Bram Moolenaare08be092022-02-17 13:08:26 +00002335 status = FAIL;
2336 semsg(_(e_cannot_index_str), vartype_name(dest_type));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002337 }
2338 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002339
2340 clear_tv(tv_idx);
2341 clear_tv(tv_dest);
2342 ectx->ec_stack.ga_len -= 3;
2343 if (status == FAIL)
2344 {
2345 clear_tv(tv);
2346 return FAIL;
2347 }
2348 return OK;
2349}
2350
2351/*
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002352 * Store a value in a list or blob range.
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002353 */
2354 static int
2355execute_storerange(isn_T *iptr, ectx_T *ectx)
2356{
2357 typval_T *tv;
2358 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2359 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2360 typval_T *tv_dest = STACK_TV_BOT(-1);
2361 int status = OK;
2362
2363 // Stack contains:
2364 // -4 value to be stored
2365 // -3 first index or "none"
2366 // -2 second index or "none"
2367 // -1 destination list or blob
2368 tv = STACK_TV_BOT(-4);
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002369 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002370 if (tv_dest->v_type == VAR_LIST)
2371 {
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002372 long n1;
2373 long n2;
2374 listitem_T *li1;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002375
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002376 n1 = (long)tv_get_number_chk(tv_idx1, NULL);
2377 if (tv_idx2->v_type == VAR_SPECIAL
2378 && tv_idx2->vval.v_number == VVAL_NONE)
2379 n2 = list_len(tv_dest->vval.v_list) - 1;
2380 else
2381 n2 = (long)tv_get_number_chk(tv_idx2, NULL);
2382
Bram Moolenaar22ebd172022-04-01 15:26:58 +01002383 li1 = check_range_index_one(tv_dest->vval.v_list, &n1, TRUE, FALSE);
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002384 if (li1 == NULL)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002385 status = FAIL;
2386 else
2387 {
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002388 status = check_range_index_two(tv_dest->vval.v_list,
2389 &n1, li1, &n2, FALSE);
2390 if (status != FAIL)
2391 status = list_assign_range(
2392 tv_dest->vval.v_list,
2393 tv->vval.v_list,
2394 n1,
2395 n2,
2396 tv_idx2->v_type == VAR_SPECIAL,
2397 (char_u *)"=",
2398 (char_u *)"[unknown]");
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002399 }
2400 }
2401 else if (tv_dest->v_type == VAR_BLOB)
2402 {
2403 varnumber_T n1;
2404 varnumber_T n2;
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002405 long bloblen;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002406
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002407 n1 = tv_get_number_chk(tv_idx1, NULL);
2408 if (tv_idx2->v_type == VAR_SPECIAL
2409 && tv_idx2->vval.v_number == VVAL_NONE)
2410 n2 = blob_len(tv_dest->vval.v_blob) - 1;
2411 else
2412 n2 = tv_get_number_chk(tv_idx2, NULL);
2413 bloblen = blob_len(tv_dest->vval.v_blob);
2414
2415 if (check_blob_index(bloblen, n1, FALSE) == FAIL
2416 || check_blob_range(bloblen, n1, n2, FALSE) == FAIL)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002417 status = FAIL;
2418 else
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002419 status = blob_set_range(tv_dest->vval.v_blob, n1, n2, tv);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002420 }
2421 else
2422 {
2423 status = FAIL;
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002424 emsg(_(e_list_or_blob_required));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002425 }
2426
2427 clear_tv(tv_idx1);
2428 clear_tv(tv_idx2);
2429 clear_tv(tv_dest);
2430 ectx->ec_stack.ga_len -= 4;
2431 clear_tv(tv);
2432
2433 return status;
2434}
2435
2436/*
2437 * Unlet item in list or dict variable.
2438 */
2439 static int
2440execute_unletindex(isn_T *iptr, ectx_T *ectx)
2441{
2442 typval_T *tv_idx = STACK_TV_BOT(-2);
2443 typval_T *tv_dest = STACK_TV_BOT(-1);
2444 int status = OK;
2445
2446 // Stack contains:
2447 // -2 index
2448 // -1 dict or list
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002449 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002450 if (tv_dest->v_type == VAR_DICT)
2451 {
2452 // unlet a dict item, index must be a string
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002453 if (tv_idx->v_type != VAR_STRING && tv_idx->v_type != VAR_NUMBER)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002454 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002455 semsg(_(e_expected_str_but_got_str),
2456 vartype_name(VAR_STRING),
2457 vartype_name(tv_idx->v_type));
2458 status = FAIL;
2459 }
2460 else
2461 {
2462 dict_T *d = tv_dest->vval.v_dict;
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002463 char_u *key;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002464 dictitem_T *di = NULL;
2465
2466 if (d != NULL && value_check_lock(
2467 d->dv_lock, NULL, FALSE))
2468 status = FAIL;
2469 else
2470 {
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002471 if (tv_idx->v_type == VAR_STRING)
2472 {
2473 key = tv_idx->vval.v_string;
2474 if (key == NULL)
2475 key = (char_u *)"";
2476 }
2477 else
2478 {
2479 key = tv_get_string(tv_idx);
2480 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002481 if (d != NULL)
2482 di = dict_find(d, key, (int)STRLEN(key));
2483 if (di == NULL)
2484 {
2485 // NULL dict is equivalent to empty dict
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00002486 semsg(_(e_key_not_present_in_dictionary_str), key);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002487 status = FAIL;
2488 }
2489 else if (var_check_fixed(di->di_flags,
2490 NULL, FALSE)
2491 || var_check_ro(di->di_flags,
2492 NULL, FALSE))
2493 status = FAIL;
2494 else
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002495 dictitem_remove(d, di, "unlet");
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002496 }
2497 }
2498 }
2499 else if (tv_dest->v_type == VAR_LIST)
2500 {
2501 // unlet a List item, index must be a number
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002502 if (check_for_number(tv_idx) == FAIL)
2503 {
2504 status = FAIL;
2505 }
2506 else
2507 {
2508 list_T *l = tv_dest->vval.v_list;
2509 long n = (long)tv_idx->vval.v_number;
2510
2511 if (l != NULL && value_check_lock(
2512 l->lv_lock, NULL, FALSE))
2513 status = FAIL;
2514 else
2515 {
2516 listitem_T *li = list_find(l, n);
2517
2518 if (li == NULL)
2519 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002520 semsg(_(e_list_index_out_of_range_nr), n);
2521 status = FAIL;
2522 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002523 else
2524 listitem_remove(l, li);
2525 }
2526 }
2527 }
2528 else
2529 {
2530 status = FAIL;
2531 semsg(_(e_cannot_index_str),
2532 vartype_name(tv_dest->v_type));
2533 }
2534
2535 clear_tv(tv_idx);
2536 clear_tv(tv_dest);
2537 ectx->ec_stack.ga_len -= 2;
2538
2539 return status;
2540}
2541
2542/*
2543 * Unlet a range of items in a list variable.
2544 */
2545 static int
2546execute_unletrange(isn_T *iptr, ectx_T *ectx)
2547{
2548 // Stack contains:
2549 // -3 index1
2550 // -2 index2
2551 // -1 dict or list
2552 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2553 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2554 typval_T *tv_dest = STACK_TV_BOT(-1);
2555 int status = OK;
2556
2557 if (tv_dest->v_type == VAR_LIST)
2558 {
2559 // indexes must be a number
2560 SOURCING_LNUM = iptr->isn_lnum;
2561 if (check_for_number(tv_idx1) == FAIL
2562 || (tv_idx2->v_type != VAR_SPECIAL
2563 && check_for_number(tv_idx2) == FAIL))
2564 {
2565 status = FAIL;
2566 }
2567 else
2568 {
2569 list_T *l = tv_dest->vval.v_list;
2570 long n1 = (long)tv_idx1->vval.v_number;
2571 long n2 = tv_idx2->v_type == VAR_SPECIAL
2572 ? 0 : (long)tv_idx2->vval.v_number;
2573 listitem_T *li;
2574
2575 li = list_find_index(l, &n1);
2576 if (li == NULL)
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002577 {
2578 semsg(_(e_list_index_out_of_range_nr),
2579 (long)tv_idx1->vval.v_number);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002580 status = FAIL;
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002581 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002582 else
2583 {
2584 if (n1 < 0)
2585 n1 = list_idx_of_item(l, li);
2586 if (n2 < 0)
2587 {
2588 listitem_T *li2 = list_find(l, n2);
2589
2590 if (li2 == NULL)
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002591 {
2592 semsg(_(e_list_index_out_of_range_nr), n2);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002593 status = FAIL;
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002594 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002595 else
2596 n2 = list_idx_of_item(l, li2);
2597 }
2598 if (status != FAIL
2599 && tv_idx2->v_type != VAR_SPECIAL
2600 && n2 < n1)
2601 {
2602 semsg(_(e_list_index_out_of_range_nr), n2);
2603 status = FAIL;
2604 }
Bram Moolenaar6b8c7ba2022-03-20 17:46:06 +00002605 if (status != FAIL)
2606 list_unlet_range(l, li, n1,
2607 tv_idx2->v_type != VAR_SPECIAL, n2);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002608 }
2609 }
2610 }
2611 else
2612 {
2613 status = FAIL;
2614 SOURCING_LNUM = iptr->isn_lnum;
2615 semsg(_(e_cannot_index_str),
2616 vartype_name(tv_dest->v_type));
2617 }
2618
2619 clear_tv(tv_idx1);
2620 clear_tv(tv_idx2);
2621 clear_tv(tv_dest);
2622 ectx->ec_stack.ga_len -= 3;
2623
2624 return status;
2625}
2626
2627/*
2628 * Top of a for loop.
2629 */
2630 static int
2631execute_for(isn_T *iptr, ectx_T *ectx)
2632{
2633 typval_T *tv;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002634 int jump = FALSE;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002635 typval_T *ltv = STACK_TV_BOT(-1);
2636 typval_T *idxtv =
Bram Moolenaarcc341812022-09-19 15:54:34 +01002637 STACK_TV_VAR(iptr->isn_arg.forloop.for_loop_idx);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002638
2639 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
2640 return FAIL;
2641 if (ltv->v_type == VAR_LIST)
2642 {
2643 list_T *list = ltv->vval.v_list;
2644
2645 // push the next item from the list
2646 ++idxtv->vval.v_number;
2647 if (list == NULL
2648 || idxtv->vval.v_number >= list->lv_len)
2649 {
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002650 jump = TRUE;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002651 }
2652 else if (list->lv_first == &range_list_item)
2653 {
2654 // non-materialized range() list
2655 tv = STACK_TV_BOT(0);
2656 tv->v_type = VAR_NUMBER;
2657 tv->v_lock = 0;
2658 tv->vval.v_number = list_find_nr(
2659 list, idxtv->vval.v_number, NULL);
2660 ++ectx->ec_stack.ga_len;
2661 }
2662 else
2663 {
2664 listitem_T *li = list_find(list,
2665 idxtv->vval.v_number);
2666
2667 copy_tv(&li->li_tv, STACK_TV_BOT(0));
2668 ++ectx->ec_stack.ga_len;
2669 }
2670 }
2671 else if (ltv->v_type == VAR_STRING)
2672 {
2673 char_u *str = ltv->vval.v_string;
2674
2675 // The index is for the last byte of the previous
2676 // character.
2677 ++idxtv->vval.v_number;
2678 if (str == NULL || str[idxtv->vval.v_number] == NUL)
2679 {
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002680 jump = TRUE;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002681 }
2682 else
2683 {
2684 int clen = mb_ptr2len(str + idxtv->vval.v_number);
2685
2686 // Push the next character from the string.
2687 tv = STACK_TV_BOT(0);
2688 tv->v_type = VAR_STRING;
2689 tv->vval.v_string = vim_strnsave(
2690 str + idxtv->vval.v_number, clen);
2691 ++ectx->ec_stack.ga_len;
2692 idxtv->vval.v_number += clen - 1;
2693 }
2694 }
2695 else if (ltv->v_type == VAR_BLOB)
2696 {
2697 blob_T *blob = ltv->vval.v_blob;
2698
2699 // When we get here the first time make a copy of the
2700 // blob, so that the iteration still works when it is
2701 // changed.
2702 if (idxtv->vval.v_number == -1 && blob != NULL)
2703 {
2704 blob_copy(blob, ltv);
2705 blob_unref(blob);
2706 blob = ltv->vval.v_blob;
2707 }
2708
2709 // The index is for the previous byte.
2710 ++idxtv->vval.v_number;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002711 if (blob == NULL || idxtv->vval.v_number >= blob_len(blob))
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002712 {
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002713 jump = TRUE;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002714 }
2715 else
2716 {
2717 // Push the next byte from the blob.
2718 tv = STACK_TV_BOT(0);
2719 tv->v_type = VAR_NUMBER;
2720 tv->vval.v_number = blob_get(blob,
2721 idxtv->vval.v_number);
2722 ++ectx->ec_stack.ga_len;
2723 }
2724 }
2725 else
2726 {
2727 semsg(_(e_for_loop_on_str_not_supported),
2728 vartype_name(ltv->v_type));
2729 return FAIL;
2730 }
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002731
2732 if (jump)
2733 {
2734 // past the end of the list/string/blob, jump to "endfor"
2735 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
2736 may_restore_cmdmod(&ectx->ec_funclocal);
2737 }
2738 else
2739 {
2740 // Store the current number of funcrefs, this may be used in
2741 // ISN_LOOPEND. The variable index is always one more than the loop
2742 // variable index.
Bram Moolenaarcc341812022-09-19 15:54:34 +01002743 tv = STACK_TV_VAR(iptr->isn_arg.forloop.for_loop_idx + 1);
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002744 tv->vval.v_number = ectx->ec_funcrefs.ga_len;
2745 }
2746
2747 return OK;
2748}
2749
2750/*
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002751 * Code for handling variables declared inside a loop and used in a closure.
2752 * This is very similar to what is done with funcstack_T. The difference is
2753 * that the funcstack_T has the scope of a function, while a loopvars_T has the
2754 * scope of the block inside a loop and each loop may have its own.
2755 */
2756
2757// Double linked list of loopvars_T in use.
2758static loopvars_T *first_loopvars = NULL;
2759
2760 static void
2761add_loopvars_to_list(loopvars_T *loopvars)
2762{
2763 // Link in list of loopvarss.
2764 if (first_loopvars != NULL)
2765 first_loopvars->lvs_prev = loopvars;
2766 loopvars->lvs_next = first_loopvars;
2767 loopvars->lvs_prev = NULL;
2768 first_loopvars = loopvars;
2769}
2770
2771 static void
2772remove_loopvars_from_list(loopvars_T *loopvars)
2773{
2774 if (loopvars->lvs_prev == NULL)
2775 first_loopvars = loopvars->lvs_next;
2776 else
2777 loopvars->lvs_prev->lvs_next = loopvars->lvs_next;
2778 if (loopvars->lvs_next != NULL)
2779 loopvars->lvs_next->lvs_prev = loopvars->lvs_prev;
2780}
2781
2782/*
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002783 * End of a for or while loop: Handle any variables used by a closure.
2784 */
2785 static int
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002786execute_endloop(isn_T *iptr, ectx_T *ectx)
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002787{
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002788 endloop_T *endloop = &iptr->isn_arg.endloop;
2789 typval_T *tv_refcount = STACK_TV_VAR(endloop->end_funcref_idx);
2790 int prev_closure_count = tv_refcount->vval.v_number;
Bram Moolenaarcc341812022-09-19 15:54:34 +01002791 int depth = endloop->end_depth;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002792 garray_T *gap = &ectx->ec_funcrefs;
2793 int closure_in_use = FALSE;
2794 loopvars_T *loopvars;
2795 typval_T *stack;
2796 int idx;
2797
Bram Moolenaarcc341812022-09-19 15:54:34 +01002798 // Check if any created closure is still being referenced and loopvars have
2799 // not been saved yet for the current depth.
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002800 for (idx = prev_closure_count; idx < gap->ga_len; ++idx)
2801 {
2802 partial_T *pt = ((partial_T **)gap->ga_data)[idx];
2803
Bram Moolenaarcc341812022-09-19 15:54:34 +01002804 if (pt->pt_refcount > 1 && pt->pt_loopvars[depth] == NULL)
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002805 {
2806 int refcount = pt->pt_refcount;
2807 int i;
2808
2809 // A Reference in a variable inside the loop doesn't count, it gets
2810 // unreferenced at the end of the loop.
2811 for (i = 0; i < endloop->end_var_count; ++i)
2812 {
2813 typval_T *stv = STACK_TV_VAR(endloop->end_var_idx + i);
2814
2815 if (stv->v_type == VAR_PARTIAL && pt == stv->vval.v_partial)
2816 --refcount;
2817 }
2818 if (refcount > 1)
2819 {
2820 closure_in_use = TRUE;
2821 break;
2822 }
2823 }
2824 }
2825
2826 // If no function reference were created since the start of the loop block
2827 // or it is no longer referenced there is nothing to do.
2828 if (!closure_in_use)
2829 return OK;
2830
2831 // A closure is using variables declared inside the loop.
2832 // Move them to the called function.
2833 loopvars = ALLOC_CLEAR_ONE(loopvars_T);
2834 if (loopvars == NULL)
2835 return FAIL;
2836
2837 loopvars->lvs_ga.ga_len = endloop->end_var_count;
2838 stack = ALLOC_CLEAR_MULT(typval_T, loopvars->lvs_ga.ga_len);
2839 loopvars->lvs_ga.ga_data = stack;
2840 if (stack == NULL)
2841 {
2842 vim_free(loopvars);
2843 return FAIL;
2844 }
2845 add_loopvars_to_list(loopvars);
2846
2847 // Move the variable values.
2848 for (idx = 0; idx < endloop->end_var_count; ++idx)
2849 {
2850 typval_T *tv = STACK_TV_VAR(endloop->end_var_idx + idx);
2851
2852 *(stack + idx) = *tv;
2853 tv->v_type = VAR_UNKNOWN;
2854 }
2855
2856 for (idx = prev_closure_count; idx < gap->ga_len; ++idx)
2857 {
2858 partial_T *pt = ((partial_T **)gap->ga_data)[idx];
2859
Bram Moolenaarcc341812022-09-19 15:54:34 +01002860 if (pt->pt_refcount > 1 && pt->pt_loopvars[depth] == NULL)
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002861 {
2862 ++loopvars->lvs_refcount;
Bram Moolenaarcc341812022-09-19 15:54:34 +01002863 pt->pt_loopvars[depth] = loopvars;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002864
Bram Moolenaarcc341812022-09-19 15:54:34 +01002865 pt->pt_outer.out_loop[depth].stack = &loopvars->lvs_ga;
2866 pt->pt_outer.out_loop[depth].var_idx -=
2867 ectx->ec_frame_idx + STACK_FRAME_SIZE + endloop->end_var_idx;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002868 }
2869 }
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002870
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002871 return OK;
2872}
2873
2874/*
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002875 * Called when a partial is freed or its reference count goes down to one. The
2876 * loopvars may be the only reference to the partials in the local variables.
2877 * Go over all of them, the funcref and can be freed if all partials
2878 * referencing the loopvars have a reference count of one.
Bram Moolenaarcc341812022-09-19 15:54:34 +01002879 * Return TRUE if it was freed.
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002880 */
Bram Moolenaarcc341812022-09-19 15:54:34 +01002881 int
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002882loopvars_check_refcount(loopvars_T *loopvars)
2883{
2884 int i;
2885 garray_T *gap = &loopvars->lvs_ga;
2886 int done = 0;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002887 typval_T *stack = gap->ga_data;
2888
Bram Moolenaarcc341812022-09-19 15:54:34 +01002889 if (loopvars->lvs_refcount > loopvars->lvs_min_refcount)
2890 return FALSE;
2891 for (i = 0; i < gap->ga_len; ++i)
2892 {
2893 typval_T *tv = ((typval_T *)gap->ga_data) + i;
2894
2895 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL
2896 && tv->vval.v_partial->pt_refcount == 1)
2897 {
2898 int depth;
2899
2900 for (depth = 0; depth < MAX_LOOP_DEPTH; ++depth)
2901 if (tv->vval.v_partial->pt_loopvars[depth] == loopvars)
2902 ++done;
2903 }
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002904 }
Bram Moolenaarcc341812022-09-19 15:54:34 +01002905 if (done != loopvars->lvs_min_refcount)
2906 return FALSE;
2907
2908 // All partials referencing the loopvars have a reference count of
2909 // one, thus the loopvars is no longer of use.
2910 stack = gap->ga_data;
2911 for (i = 0; i < gap->ga_len; ++i)
2912 clear_tv(stack + i);
2913 vim_free(stack);
2914 remove_loopvars_from_list(loopvars);
2915 vim_free(loopvars);
2916 return TRUE;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002917}
2918
2919/*
2920 * For garbage collecting: set references in all variables referenced by
2921 * all loopvars.
2922 */
2923 int
2924set_ref_in_loopvars(int copyID)
2925{
2926 loopvars_T *loopvars;
2927
2928 for (loopvars = first_loopvars; loopvars != NULL;
2929 loopvars = loopvars->lvs_next)
2930 {
2931 typval_T *stack = loopvars->lvs_ga.ga_data;
2932 int i;
2933
2934 for (i = 0; i < loopvars->lvs_ga.ga_len; ++i)
2935 if (set_ref_in_item(stack + i, copyID, NULL, NULL))
2936 return TRUE; // abort
2937 }
2938 return FALSE;
2939}
2940
2941/*
Bram Moolenaar06b77222022-01-25 15:51:56 +00002942 * Load instruction for w:/b:/g:/t: variable.
2943 * "isn_type" is used instead of "iptr->isn_type".
2944 */
2945 static int
2946load_namespace_var(ectx_T *ectx, isntype_T isn_type, isn_T *iptr)
2947{
2948 dictitem_T *di = NULL;
2949 hashtab_T *ht = NULL;
2950 char namespace;
2951
2952 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
2953 return NOTDONE;
2954 switch (isn_type)
2955 {
2956 case ISN_LOADG:
2957 ht = get_globvar_ht();
2958 namespace = 'g';
2959 break;
2960 case ISN_LOADB:
2961 ht = &curbuf->b_vars->dv_hashtab;
2962 namespace = 'b';
2963 break;
2964 case ISN_LOADW:
2965 ht = &curwin->w_vars->dv_hashtab;
2966 namespace = 'w';
2967 break;
2968 case ISN_LOADT:
2969 ht = &curtab->tp_vars->dv_hashtab;
2970 namespace = 't';
2971 break;
2972 default: // Cannot reach here
2973 return NOTDONE;
2974 }
2975 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
2976
Bram Moolenaar06b77222022-01-25 15:51:56 +00002977 if (di == NULL)
2978 {
Bram Moolenaarfe732552022-02-22 19:39:13 +00002979 if (isn_type == ISN_LOADG)
2980 {
2981 ufunc_T *ufunc = find_func(iptr->isn_arg.string, TRUE);
2982
2983 // g:Something could be a function
2984 if (ufunc != NULL)
2985 {
2986 typval_T *tv = STACK_TV_BOT(0);
2987
2988 ++ectx->ec_stack.ga_len;
2989 tv->v_type = VAR_FUNC;
2990 tv->vval.v_string = alloc(STRLEN(iptr->isn_arg.string) + 3);
2991 if (tv->vval.v_string == NULL)
2992 return FAIL;
2993 STRCPY(tv->vval.v_string, "g:");
2994 STRCPY(tv->vval.v_string + 2, iptr->isn_arg.string);
2995 return OK;
2996 }
2997 }
Bram Moolenaar06b77222022-01-25 15:51:56 +00002998 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarfe732552022-02-22 19:39:13 +00002999 if (vim_strchr(iptr->isn_arg.string, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar06b77222022-01-25 15:51:56 +00003000 // no check if the item exists in the script but
3001 // isn't exported, it is too complicated
Bram Moolenaarfe732552022-02-22 19:39:13 +00003002 semsg(_(e_item_not_found_in_script_str), iptr->isn_arg.string);
Bram Moolenaar06b77222022-01-25 15:51:56 +00003003 else
3004 semsg(_(e_undefined_variable_char_str),
Bram Moolenaarfe732552022-02-22 19:39:13 +00003005 namespace, iptr->isn_arg.string);
Bram Moolenaar06b77222022-01-25 15:51:56 +00003006 return FAIL;
3007 }
3008 else
3009 {
3010 copy_tv(&di->di_tv, STACK_TV_BOT(0));
3011 ++ectx->ec_stack.ga_len;
3012 }
3013 return OK;
3014}
3015
Bram Moolenaard0200c82023-01-28 15:19:40 +00003016
3017 static void
3018object_required_error(typval_T *tv)
3019{
3020 garray_T type_list;
3021 ga_init2(&type_list, sizeof(type_T *), 10);
3022 type_T *type = typval2type(tv, get_copyID(), &type_list, TVTT_DO_MEMBER);
3023 char *tofree = NULL;
3024 char *typename = type_name(type, &tofree);
3025 semsg(_(e_object_required_found_str), typename);
3026 vim_free(tofree);
3027 clear_type_list(&type_list);
3028}
3029
Bram Moolenaar06b77222022-01-25 15:51:56 +00003030/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02003031 * Execute instructions in execution context "ectx".
3032 * Return OK or FAIL;
3033 */
3034 static int
3035exec_instructions(ectx_T *ectx)
3036{
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003037 int ret = FAIL;
3038 int save_trylevel_at_start = ectx->ec_trylevel_at_start;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02003039 int dict_stack_len_at_start = dict_stack.ga_len;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003040
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02003041 // Start execution at the first instruction.
Bram Moolenaar4c137212021-04-19 16:48:48 +02003042 ectx->ec_iidx = 0;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01003043
Bram Moolenaarff652882021-05-16 15:24:49 +02003044 // Only catch exceptions in this instruction list.
3045 ectx->ec_trylevel_at_start = trylevel;
3046
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003047 for (;;)
3048 {
Bram Moolenaara7490192021-07-22 12:26:14 +02003049 static int breakcheck_count = 0; // using "static" makes it faster
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003050 isn_T *iptr;
Bram Moolenaara7490192021-07-22 12:26:14 +02003051 typval_T *tv;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003052
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003053 if (unlikely(++breakcheck_count >= 100))
Bram Moolenaar270d0382020-05-15 21:42:53 +02003054 {
3055 line_breakcheck();
3056 breakcheck_count = 0;
3057 }
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003058 if (unlikely(got_int))
Bram Moolenaar20431c92020-03-20 18:39:46 +01003059 {
3060 // Turn CTRL-C into an exception.
3061 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +01003062 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003063 goto theend;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003064 did_throw = TRUE;
3065 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003066
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003067 if (unlikely(did_emsg && msg_list != NULL && *msg_list != NULL))
Bram Moolenaara26b9702020-04-18 19:53:28 +02003068 {
3069 // Turn an error message into an exception.
3070 did_emsg = FALSE;
3071 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003072 goto theend;
Bram Moolenaara26b9702020-04-18 19:53:28 +02003073 did_throw = TRUE;
3074 *msg_list = NULL;
Bram Moolenaar3ef2e412023-04-30 18:50:48 +01003075
3076 // This exception was not caught (yet).
3077 garray_T *trystack = &ectx->ec_trystack;
3078 if (trystack->ga_len > 0)
3079 {
3080 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
3081 + trystack->ga_len - 1;
3082 if (trycmd->tcd_frame_idx == ectx->ec_frame_idx)
3083 trycmd->tcd_caught = FALSE;
3084 }
Bram Moolenaara26b9702020-04-18 19:53:28 +02003085 }
3086
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003087 if (unlikely(did_throw))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003088 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003089 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003090 trycmd_T *trycmd = NULL;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003091 int index = trystack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003092
3093 // An exception jumps to the first catch, finally, or returns from
3094 // the current function.
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003095 while (index > 0)
3096 {
3097 trycmd = ((trycmd_T *)trystack->ga_data) + index - 1;
Bram Moolenaar3ef2e412023-04-30 18:50:48 +01003098 // 1. after :try and before :catch - jump to first :catch
3099 // 2. in :catch block - jump to :finally
3100 // 3. in :catch block and no finally - jump to :endtry
3101 if (!trycmd->tcd_in_catch || trycmd->tcd_finally_idx != 0
3102 || trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003103 break;
3104 // In the catch and finally block of this try we have to go up
3105 // one level.
3106 --index;
3107 trycmd = NULL;
3108 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003109 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003110 {
Bram Moolenaar834193a2021-06-30 20:39:15 +02003111 if (trycmd->tcd_in_catch)
3112 {
Bram Moolenaar3ef2e412023-04-30 18:50:48 +01003113 if (trycmd->tcd_finally_idx > 0)
3114 {
3115 // exception inside ":catch", jump to ":finally" once
3116 ectx->ec_iidx = trycmd->tcd_finally_idx;
3117 trycmd->tcd_finally_idx = 0;
3118 }
3119 else
3120 {
3121 // exception inside ":catch" or ":finally", jump to
3122 // ":endtry"
3123 ectx->ec_iidx = trycmd->tcd_endtry_idx;
3124 }
Bram Moolenaar834193a2021-06-30 20:39:15 +02003125 }
3126 else
Bram Moolenaar3ef2e412023-04-30 18:50:48 +01003127 {
Bram Moolenaar834193a2021-06-30 20:39:15 +02003128 // jump to first ":catch"
3129 ectx->ec_iidx = trycmd->tcd_catch_idx;
Bram Moolenaar3ef2e412023-04-30 18:50:48 +01003130 trycmd->tcd_in_catch = TRUE;
3131 }
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003132 did_throw = FALSE; // don't come back here until :endtry
3133 trycmd->tcd_did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003134 }
3135 else
3136 {
Bram Moolenaar3ef2e412023-04-30 18:50:48 +01003137 // Not inside try or need to return from current function.
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02003138 // Push a dummy return value.
Bram Moolenaar35578162021-08-02 19:10:38 +02003139 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003140 goto theend;
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02003141 tv = STACK_TV_BOT(0);
3142 tv->v_type = VAR_NUMBER;
3143 tv->vval.v_number = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003144 ++ectx->ec_stack.ga_len;
3145 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003146 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02003147 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +01003148 need_rethrow = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003149 if (handle_closure_in_use(ectx, FALSE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003150 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003151 goto done;
3152 }
3153
Bram Moolenaar4c137212021-04-19 16:48:48 +02003154 if (func_return(ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003155 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003156 }
3157 continue;
3158 }
3159
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003160 /*
3161 * Big switch on the instruction. Most compilers will be turning this
3162 * into an efficient lookup table, since the "case" values are an enum
3163 * with sequential numbers. It may look ugly, but it should be the
3164 * most efficient way.
3165 */
Bram Moolenaar4c137212021-04-19 16:48:48 +02003166 iptr = &ectx->ec_instr[ectx->ec_iidx++];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003167 switch (iptr->isn_type)
3168 {
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00003169 // Constructor, first instruction in a new() method.
Bram Moolenaar00b28d62022-12-08 15:32:33 +00003170 case ISN_CONSTRUCT:
3171 // "this" is always the local variable at index zero
3172 tv = STACK_TV_VAR(0);
3173 tv->v_type = VAR_OBJECT;
3174 tv->vval.v_object = alloc_clear(
3175 iptr->isn_arg.construct.construct_size);
3176 tv->vval.v_object->obj_class =
3177 iptr->isn_arg.construct.construct_class;
Bram Moolenaard28d7b92022-12-08 20:42:00 +00003178 ++tv->vval.v_object->obj_class->class_refcount;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00003179 tv->vval.v_object->obj_refcount = 1;
Bram Moolenaard28d7b92022-12-08 20:42:00 +00003180 object_created(tv->vval.v_object);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00003181 break;
3182
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003183 // execute Ex command line
3184 case ISN_EXEC:
Bram Moolenaaraacc9662021-08-13 19:40:51 +02003185 if (exec_command(iptr) == FAIL)
3186 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003187 break;
3188
Bram Moolenaar20677332021-06-06 17:02:53 +02003189 // execute Ex command line split at NL characters.
3190 case ISN_EXEC_SPLIT:
3191 {
3192 source_cookie_T cookie;
Bram Moolenaar518df272021-06-06 17:34:13 +02003193 char_u *line;
Bram Moolenaar20677332021-06-06 17:02:53 +02003194
3195 SOURCING_LNUM = iptr->isn_lnum;
3196 CLEAR_FIELD(cookie);
3197 cookie.sourcing_lnum = iptr->isn_lnum - 1;
3198 cookie.nextline = iptr->isn_arg.string;
Bram Moolenaar518df272021-06-06 17:34:13 +02003199 line = get_split_sourceline(0, &cookie, 0, 0);
3200 if (do_cmdline(line,
Bram Moolenaar20677332021-06-06 17:02:53 +02003201 get_split_sourceline, &cookie,
3202 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED)
3203 == FAIL
3204 || did_emsg)
Bram Moolenaar518df272021-06-06 17:34:13 +02003205 {
3206 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02003207 goto on_error;
Bram Moolenaar518df272021-06-06 17:34:13 +02003208 }
3209 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02003210 }
3211 break;
3212
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00003213 // execute Ex command line that is only a range
3214 case ISN_EXECRANGE:
3215 {
3216 exarg_T ea;
3217 char *error = NULL;
3218
3219 CLEAR_FIELD(ea);
3220 ea.cmdidx = CMD_SIZE;
3221 ea.addr_type = ADDR_LINES;
3222 ea.cmd = iptr->isn_arg.string;
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00003223 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00003224 parse_cmd_address(&ea, &error, FALSE);
Bram Moolenaar01a4dcb2021-12-04 13:15:10 +00003225 if (ea.cmd == NULL)
3226 goto on_error;
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00003227 // error is always NULL when using ADDR_LINES
3228 error = ex_range_without_command(&ea);
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00003229 if (error != NULL)
3230 {
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00003231 emsg(error);
3232 goto on_error;
3233 }
3234 }
3235 break;
3236
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02003237 // Evaluate an expression with legacy syntax, push it onto the
3238 // stack.
3239 case ISN_LEGACY_EVAL:
3240 {
3241 char_u *arg = iptr->isn_arg.string;
3242 int res;
3243 int save_flags = cmdmod.cmod_flags;
3244
Bram Moolenaar35578162021-08-02 19:10:38 +02003245 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003246 goto theend;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02003247 tv = STACK_TV_BOT(0);
3248 init_tv(tv);
3249 cmdmod.cmod_flags |= CMOD_LEGACY;
3250 res = eval0(arg, tv, NULL, &EVALARG_EVALUATE);
3251 cmdmod.cmod_flags = save_flags;
3252 if (res == FAIL)
3253 goto on_error;
3254 ++ectx->ec_stack.ga_len;
3255 }
3256 break;
3257
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003258 // push typeval VAR_INSTR with instructions to be executed
3259 case ISN_INSTR:
3260 {
Bram Moolenaar35578162021-08-02 19:10:38 +02003261 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003262 goto theend;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003263 tv = STACK_TV_BOT(0);
3264 tv->vval.v_instr = ALLOC_ONE(instr_T);
3265 if (tv->vval.v_instr == NULL)
3266 goto on_error;
3267 ++ectx->ec_stack.ga_len;
3268
3269 tv->v_type = VAR_INSTR;
3270 tv->vval.v_instr->instr_ectx = ectx;
3271 tv->vval.v_instr->instr_instr = iptr->isn_arg.instr;
3272 }
3273 break;
3274
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003275 case ISN_SOURCE:
3276 {
Bram Moolenaar89445512022-04-14 12:58:23 +01003277 int notused;
LemonBoy77142312022-04-15 20:50:46 +01003278
Bram Moolenaar89445512022-04-14 12:58:23 +01003279 SOURCING_LNUM = iptr->isn_lnum;
3280 if (may_load_script((int)iptr->isn_arg.number, &notused)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003281 == FAIL)
Bram Moolenaar89445512022-04-14 12:58:23 +01003282 goto on_error;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003283 }
3284 break;
3285
Bram Moolenaar4c137212021-04-19 16:48:48 +02003286 // execute :substitute with an expression
3287 case ISN_SUBSTITUTE:
3288 {
3289 subs_T *subs = &iptr->isn_arg.subs;
3290 source_cookie_T cookie;
3291 struct subs_expr_S *save_instr = substitute_instr;
3292 struct subs_expr_S subs_instr;
3293 int res;
3294
3295 subs_instr.subs_ectx = ectx;
3296 subs_instr.subs_instr = subs->subs_instr;
3297 subs_instr.subs_status = OK;
3298 substitute_instr = &subs_instr;
3299
3300 SOURCING_LNUM = iptr->isn_lnum;
3301 // This is very much like ISN_EXEC
3302 CLEAR_FIELD(cookie);
3303 cookie.sourcing_lnum = iptr->isn_lnum - 1;
3304 res = do_cmdline(subs->subs_cmd,
3305 getsourceline, &cookie,
3306 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
3307 substitute_instr = save_instr;
3308
3309 if (res == FAIL || did_emsg
3310 || subs_instr.subs_status == FAIL)
3311 goto on_error;
3312 }
3313 break;
3314
3315 case ISN_FINISH:
3316 goto done;
3317
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02003318 case ISN_REDIRSTART:
3319 // create a dummy entry for var_redir_str()
3320 if (alloc_redir_lval() == FAIL)
3321 goto on_error;
3322
3323 // The output is stored in growarray "redir_ga" until
3324 // redirection ends.
3325 init_redir_ga();
3326 redir_vname = 1;
3327 break;
3328
3329 case ISN_REDIREND:
3330 {
3331 char_u *res = get_clear_redir_ga();
3332
3333 // End redirection, put redirected text on the stack.
3334 clear_redir_lval();
3335 redir_vname = 0;
3336
Bram Moolenaar35578162021-08-02 19:10:38 +02003337 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02003338 {
3339 vim_free(res);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003340 goto theend;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02003341 }
3342 tv = STACK_TV_BOT(0);
3343 tv->v_type = VAR_STRING;
3344 tv->vval.v_string = res;
3345 ++ectx->ec_stack.ga_len;
3346 }
3347 break;
3348
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003349 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02003350#ifdef FEAT_QUICKFIX
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003351 force_abort = TRUE;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003352 if (trigger_cexpr_autocmd(iptr->isn_arg.number) == FAIL)
3353 goto on_error;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003354 force_abort = FALSE;
Bram Moolenaarb7c97812021-05-05 22:51:39 +02003355#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003356 break;
3357
3358 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02003359#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003360 {
3361 exarg_T ea;
3362 int res;
3363
3364 CLEAR_FIELD(ea);
3365 ea.cmdidx = iptr->isn_arg.cexpr.cexpr_ref->cer_cmdidx;
3366 ea.forceit = iptr->isn_arg.cexpr.cexpr_ref->cer_forceit;
3367 ea.cmdlinep = &iptr->isn_arg.cexpr.cexpr_ref->cer_cmdline;
3368 --ectx->ec_stack.ga_len;
3369 tv = STACK_TV_BOT(0);
Bram Moolenaarbd683e32022-07-18 17:49:03 +01003370 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003371 res = cexpr_core(&ea, tv);
3372 clear_tv(tv);
3373 if (res == FAIL)
3374 goto on_error;
3375 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02003376#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003377 break;
3378
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003379 // execute Ex command from pieces on the stack
3380 case ISN_EXECCONCAT:
3381 {
3382 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +02003383 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003384 int pass;
3385 int i;
3386 char_u *cmd = NULL;
3387 char_u *str;
3388
3389 for (pass = 1; pass <= 2; ++pass)
3390 {
3391 for (i = 0; i < count; ++i)
3392 {
3393 tv = STACK_TV_BOT(i - count);
3394 str = tv->vval.v_string;
3395 if (str != NULL && *str != NUL)
3396 {
3397 if (pass == 2)
3398 STRCPY(cmd + len, str);
3399 len += STRLEN(str);
3400 }
3401 if (pass == 2)
3402 clear_tv(tv);
3403 }
3404 if (pass == 1)
3405 {
3406 cmd = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003407 if (unlikely(cmd == NULL))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003408 goto theend;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003409 len = 0;
3410 }
3411 }
3412
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02003413 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003414 do_cmdline_cmd(cmd);
3415 vim_free(cmd);
3416 }
3417 break;
3418
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003419 // execute :echo {string} ...
3420 case ISN_ECHO:
3421 {
3422 int count = iptr->isn_arg.echo.echo_count;
3423 int atstart = TRUE;
3424 int needclr = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003425 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003426
3427 for (idx = 0; idx < count; ++idx)
3428 {
3429 tv = STACK_TV_BOT(idx - count);
3430 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
3431 &atstart, &needclr);
3432 clear_tv(tv);
3433 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01003434 if (needclr)
3435 msg_clr_eos();
Bram Moolenaar4c137212021-04-19 16:48:48 +02003436 ectx->ec_stack.ga_len -= count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003437 }
3438 break;
3439
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003440 // :execute {string} ...
3441 // :echomsg {string} ...
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01003442 // :echowindow {string} ...
Bram Moolenaar7de62622021-08-07 15:05:47 +02003443 // :echoconsole {string} ...
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003444 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01003445 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003446 case ISN_ECHOMSG:
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01003447 case ISN_ECHOWINDOW:
Bram Moolenaar7de62622021-08-07 15:05:47 +02003448 case ISN_ECHOCONSOLE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003449 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01003450 {
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01003451 int count;
Bram Moolenaarad39c092020-02-26 18:23:43 +01003452 garray_T ga;
3453 char_u buf[NUMBUFLEN];
3454 char_u *p;
3455 int len;
3456 int failed = FALSE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003457 int idx;
Bram Moolenaarad39c092020-02-26 18:23:43 +01003458
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01003459 if (iptr->isn_type == ISN_ECHOWINDOW)
3460 count = iptr->isn_arg.echowin.ewin_count;
3461 else
3462 count = iptr->isn_arg.number;
Bram Moolenaarad39c092020-02-26 18:23:43 +01003463 ga_init2(&ga, 1, 80);
3464 for (idx = 0; idx < count; ++idx)
3465 {
3466 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003467 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01003468 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003469 if (tv->v_type == VAR_CHANNEL
3470 || tv->v_type == VAR_JOB)
3471 {
3472 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar68db9962021-05-09 23:19:22 +02003473 semsg(_(e_using_invalid_value_as_string_str),
3474 vartype_name(tv->v_type));
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003475 break;
3476 }
3477 else
3478 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01003479 }
3480 else
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003481 p = tv_stringify(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01003482
3483 len = (int)STRLEN(p);
Bram Moolenaar35578162021-08-02 19:10:38 +02003484 if (GA_GROW_FAILS(&ga, len + 2))
Bram Moolenaarad39c092020-02-26 18:23:43 +01003485 failed = TRUE;
3486 else
3487 {
3488 if (ga.ga_len > 0)
3489 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
3490 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
3491 ga.ga_len += len;
3492 }
3493 clear_tv(tv);
3494 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003495 ectx->ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003496 if (failed)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003497 {
3498 ga_clear(&ga);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003499 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003500 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01003501
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003502 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003503 {
3504 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02003505 {
3506 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003507 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01003508 if (did_emsg)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003509 {
3510 ga_clear(&ga);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01003511 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003512 }
Bram Moolenaar430deb12020-08-23 16:29:11 +02003513 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003514 else
3515 {
3516 msg_sb_eol();
3517 if (iptr->isn_type == ISN_ECHOMSG)
3518 {
3519 msg_attr(ga.ga_data, echo_attr);
3520 out_flush();
3521 }
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01003522#ifdef HAS_MESSAGE_WINDOW
3523 else if (iptr->isn_type == ISN_ECHOWINDOW)
3524 {
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01003525 start_echowindow(
3526 iptr->isn_arg.echowin.ewin_time);
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01003527 msg_attr(ga.ga_data, echo_attr);
3528 end_echowindow();
3529 }
3530#endif
Bram Moolenaar7de62622021-08-07 15:05:47 +02003531 else if (iptr->isn_type == ISN_ECHOCONSOLE)
3532 {
3533 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data),
3534 TRUE);
3535 ui_write((char_u *)"\r\n", 2, TRUE);
3536 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003537 else
3538 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003539 SOURCING_LNUM = iptr->isn_lnum;
3540 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003541 }
3542 }
3543 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01003544 ga_clear(&ga);
3545 }
3546 break;
3547
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003548 // load local variable or argument
3549 case ISN_LOAD:
Bram Moolenaar35578162021-08-02 19:10:38 +02003550 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003551 goto theend;
Bram Moolenaar2ed57ac2023-04-01 22:05:38 +01003552 tv = STACK_TV_VAR(iptr->isn_arg.number);
3553 if (tv->v_type == VAR_UNKNOWN)
3554 {
3555 // missing argument or default value v:none
3556 STACK_TV_BOT(0)->v_type = VAR_SPECIAL;
3557 STACK_TV_BOT(0)->vval.v_number = VVAL_NONE;
3558 }
3559 else
3560 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003561 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003562 break;
3563
3564 // load v: variable
3565 case ISN_LOADV:
Bram Moolenaar35578162021-08-02 19:10:38 +02003566 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003567 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003568 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003569 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003570 break;
3571
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003572 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003573 case ISN_LOADSCRIPT:
3574 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01003575 scriptref_T *sref = iptr->isn_arg.script.scriptref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003576 svar_T *sv;
3577
Bram Moolenaar6db660b2021-08-01 14:08:54 +02003578 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003579 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003580 goto theend;
Bram Moolenaar859cc212022-03-28 15:22:35 +01003581 allocate_if_null(sv);
Bram Moolenaar35578162021-08-02 19:10:38 +02003582 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003583 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003584 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003585 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003586 }
3587 break;
3588
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003589 // load s: variable in old script or autoload import
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003590 case ISN_LOADS:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003591 case ISN_LOADEXPORT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003592 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003593 int sid = iptr->isn_arg.loadstore.ls_sid;
3594 hashtab_T *ht = &SCRIPT_VARS(sid);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003595 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003596 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003597
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003598 if (di == NULL)
3599 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003600 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003601 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003602 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003603 }
3604 else
3605 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003606 if (iptr->isn_type == ISN_LOADEXPORT)
3607 {
3608 int idx = get_script_item_idx(sid, name, 0,
3609 NULL, NULL);
3610 svar_T *sv;
3611
3612 if (idx >= 0)
3613 {
3614 sv = ((svar_T *)SCRIPT_ITEM(sid)
3615 ->sn_var_vals.ga_data) + idx;
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003616 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003617 {
3618 SOURCING_LNUM = iptr->isn_lnum;
3619 semsg(_(e_item_not_exported_in_script_str),
3620 name);
3621 goto on_error;
3622 }
3623 }
3624 }
Bram Moolenaar35578162021-08-02 19:10:38 +02003625 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003626 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003627 copy_tv(&di->di_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003628 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003629 }
3630 }
3631 break;
3632
Bram Moolenaard3aac292020-04-19 14:32:17 +02003633 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003634 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02003635 case ISN_LOADB:
3636 case ISN_LOADW:
3637 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003638 {
Bram Moolenaar06b77222022-01-25 15:51:56 +00003639 int res = load_namespace_var(ectx, iptr->isn_type, iptr);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003640
Bram Moolenaar06b77222022-01-25 15:51:56 +00003641 if (res == NOTDONE)
Bram Moolenaarcbbc48f2022-01-18 12:58:28 +00003642 goto theend;
Bram Moolenaar06b77222022-01-25 15:51:56 +00003643 if (res == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003644 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003645 }
Bram Moolenaar06b77222022-01-25 15:51:56 +00003646
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003647 break;
3648
Bram Moolenaar03290b82020-12-19 16:30:44 +01003649 // load autoload variable
3650 case ISN_LOADAUTO:
3651 {
3652 char_u *name = iptr->isn_arg.string;
3653
Bram Moolenaar35578162021-08-02 19:10:38 +02003654 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003655 goto theend;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003656 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003657 if (eval_variable(name, (int)STRLEN(name), 0,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003658 STACK_TV_BOT(0), NULL, EVAL_VAR_VERBOSE) == FAIL)
Bram Moolenaar03290b82020-12-19 16:30:44 +01003659 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003660 ++ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003661 }
3662 break;
3663
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003664 // load g:/b:/w:/t: namespace
3665 case ISN_LOADGDICT:
3666 case ISN_LOADBDICT:
3667 case ISN_LOADWDICT:
3668 case ISN_LOADTDICT:
3669 {
3670 dict_T *d = NULL;
3671
3672 switch (iptr->isn_type)
3673 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02003674 case ISN_LOADGDICT: d = get_globvar_dict(); break;
3675 case ISN_LOADBDICT: d = curbuf->b_vars; break;
3676 case ISN_LOADWDICT: d = curwin->w_vars; break;
3677 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003678 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003679 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003680 }
Bram Moolenaar35578162021-08-02 19:10:38 +02003681 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003682 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003683 tv = STACK_TV_BOT(0);
3684 tv->v_type = VAR_DICT;
3685 tv->v_lock = 0;
3686 tv->vval.v_dict = d;
Bram Moolenaar1bd3cb22021-02-24 12:27:31 +01003687 ++d->dv_refcount;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003688 ++ectx->ec_stack.ga_len;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003689 }
3690 break;
3691
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003692 // load &option
3693 case ISN_LOADOPT:
3694 {
3695 typval_T optval;
3696 char_u *name = iptr->isn_arg.string;
3697
Bram Moolenaara8c17702020-04-01 21:17:24 +02003698 // This is not expected to fail, name is checked during
3699 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar35578162021-08-02 19:10:38 +02003700 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003701 goto theend;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003702 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003703 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003704 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003705 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003706 }
3707 break;
3708
3709 // load $ENV
3710 case ISN_LOADENV:
3711 {
3712 typval_T optval;
3713 char_u *name = iptr->isn_arg.string;
3714
Bram Moolenaar35578162021-08-02 19:10:38 +02003715 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003716 goto theend;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003717 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003718 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003719 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003720 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003721 }
3722 break;
3723
3724 // load @register
3725 case ISN_LOADREG:
Bram Moolenaar35578162021-08-02 19:10:38 +02003726 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003727 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003728 tv = STACK_TV_BOT(0);
3729 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003730 tv->v_lock = 0;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02003731 // This may result in NULL, which should be equivalent to an
3732 // empty string.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003733 tv->vval.v_string = get_reg_contents(
3734 iptr->isn_arg.number, GREG_EXPR_SRC);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003735 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003736 break;
3737
3738 // store local variable
3739 case ISN_STORE:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003740 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003741 tv = STACK_TV_VAR(iptr->isn_arg.number);
3742 clear_tv(tv);
3743 *tv = *STACK_TV_BOT(0);
3744 break;
3745
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003746 // store s: variable in old script or autoload import
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003747 case ISN_STORES:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003748 case ISN_STOREEXPORT:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003749 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003750 int sid = iptr->isn_arg.loadstore.ls_sid;
3751 hashtab_T *ht = &SCRIPT_VARS(sid);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003752 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003753 dictitem_T *di = find_var_in_ht(ht, 0,
3754 iptr->isn_type == ISN_STORES
3755 ? name + 2 : name, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003756
Bram Moolenaar4c137212021-04-19 16:48:48 +02003757 --ectx->ec_stack.ga_len;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003758 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003759 if (di == NULL)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003760 {
3761 if (iptr->isn_type == ISN_STOREEXPORT)
3762 {
3763 semsg(_(e_undefined_variable_str), name);
Bram Moolenaard1d26842022-03-31 10:13:47 +01003764 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003765 goto on_error;
3766 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003767 store_var(name, STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003768 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003769 else
3770 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003771 if (iptr->isn_type == ISN_STOREEXPORT)
3772 {
3773 int idx = get_script_item_idx(sid, name, 0,
3774 NULL, NULL);
3775
Bram Moolenaar06651632022-04-27 17:54:25 +01003776 // can this ever fail?
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003777 if (idx >= 0)
3778 {
3779 svar_T *sv = ((svar_T *)SCRIPT_ITEM(sid)
3780 ->sn_var_vals.ga_data) + idx;
3781
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003782 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003783 {
3784 semsg(_(e_item_not_exported_in_script_str),
3785 name);
Bram Moolenaard1d26842022-03-31 10:13:47 +01003786 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003787 goto on_error;
3788 }
3789 }
3790 }
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02003791 if (var_check_permission(di, name) == FAIL)
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003792 {
3793 clear_tv(STACK_TV_BOT(0));
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02003794 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003795 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003796 clear_tv(&di->di_tv);
3797 di->di_tv = *STACK_TV_BOT(0);
3798 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003799 }
3800 break;
3801
3802 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003803 case ISN_STORESCRIPT:
3804 {
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003805 scriptref_T *sref = iptr->isn_arg.script.scriptref;
3806 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003807
Bram Moolenaar6db660b2021-08-01 14:08:54 +02003808 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003809 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003810 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003811 --ectx->ec_stack.ga_len;
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003812
3813 // "const" and "final" are checked at compile time, locking
3814 // the value needs to be checked here.
3815 SOURCING_LNUM = iptr->isn_lnum;
3816 if (value_check_lock(sv->sv_tv->v_lock, sv->sv_name, FALSE))
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003817 {
3818 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003819 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003820 }
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003821
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003822 clear_tv(sv->sv_tv);
3823 *sv->sv_tv = *STACK_TV_BOT(0);
3824 }
3825 break;
3826
3827 // store option
3828 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003829 case ISN_STOREFUNCOPT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003830 {
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003831 char_u *opt_name = iptr->isn_arg.storeopt.so_name;
3832 int opt_flags = iptr->isn_arg.storeopt.so_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003833 long n = 0;
3834 char_u *s = NULL;
3835 char *msg;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003836 char_u numbuf[NUMBUFLEN];
3837 char_u *tofree = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003838
Bram Moolenaar4c137212021-04-19 16:48:48 +02003839 --ectx->ec_stack.ga_len;
LemonBoy32f34612023-09-02 21:52:05 +02003840 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003841 tv = STACK_TV_BOT(0);
3842 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01003843 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003844 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01003845 if (s == NULL)
3846 s = (char_u *)"";
3847 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003848 else if (iptr->isn_type == ISN_STOREFUNCOPT)
3849 {
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003850 // If the option can be set to a function reference or
3851 // a lambda and the passed value is a function
3852 // reference, then convert it to the name (string) of
3853 // the function reference.
3854 s = tv2string(tv, &tofree, numbuf, 0);
3855 if (s == NULL || *s == NUL)
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003856 {
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003857 // cannot happen?
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003858 clear_tv(tv);
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003859 vim_free(tofree);
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003860 goto on_error;
3861 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003862 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003863 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003864 // must be VAR_NUMBER, CHECKTYPE makes sure
3865 n = tv->vval.v_number;
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003866 msg = set_option_value(opt_name, n, s, opt_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02003867 clear_tv(tv);
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003868 vim_free(tofree);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003869 if (msg != NULL)
3870 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003871 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003872 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02003873 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003874 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003875 }
3876 break;
3877
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003878 // store $ENV
3879 case ISN_STOREENV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003880 --ectx->ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003881 tv = STACK_TV_BOT(0);
3882 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
3883 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003884 break;
3885
3886 // store @r
3887 case ISN_STOREREG:
3888 {
3889 int reg = iptr->isn_arg.number;
3890
Bram Moolenaar4c137212021-04-19 16:48:48 +02003891 --ectx->ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01003892 tv = STACK_TV_BOT(0);
Bram Moolenaar74f4a962021-06-17 21:03:07 +02003893 write_reg_contents(reg, tv_get_string(tv), -1, FALSE);
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01003894 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003895 }
3896 break;
3897
3898 // store v: variable
3899 case ISN_STOREV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003900 --ectx->ec_stack.ga_len;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003901 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
3902 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003903 // should not happen, type is checked when compiling
3904 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003905 break;
3906
Bram Moolenaard3aac292020-04-19 14:32:17 +02003907 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003908 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02003909 case ISN_STOREB:
3910 case ISN_STOREW:
3911 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003912 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003913 dictitem_T *di;
3914 hashtab_T *ht;
3915 char_u *name = iptr->isn_arg.string + 2;
3916
Bram Moolenaard3aac292020-04-19 14:32:17 +02003917 switch (iptr->isn_type)
3918 {
3919 case ISN_STOREG:
3920 ht = get_globvar_ht();
3921 break;
3922 case ISN_STOREB:
3923 ht = &curbuf->b_vars->dv_hashtab;
3924 break;
3925 case ISN_STOREW:
3926 ht = &curwin->w_vars->dv_hashtab;
3927 break;
3928 case ISN_STORET:
3929 ht = &curtab->tp_vars->dv_hashtab;
3930 break;
3931 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003932 goto theend;
Bram Moolenaard3aac292020-04-19 14:32:17 +02003933 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003934
Bram Moolenaar4c137212021-04-19 16:48:48 +02003935 --ectx->ec_stack.ga_len;
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003936 di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003937 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003938 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003939 else
3940 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003941 SOURCING_LNUM = iptr->isn_lnum;
3942 if (var_check_permission(di, name) == FAIL)
3943 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003944 clear_tv(&di->di_tv);
3945 di->di_tv = *STACK_TV_BOT(0);
3946 }
3947 }
3948 break;
3949
Bram Moolenaar03290b82020-12-19 16:30:44 +01003950 // store an autoload variable
3951 case ISN_STOREAUTO:
3952 SOURCING_LNUM = iptr->isn_lnum;
3953 set_var(iptr->isn_arg.string, STACK_TV_BOT(-1), TRUE);
3954 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003955 --ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003956 break;
3957
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003958 // store number in local variable
3959 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01003960 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003961 clear_tv(tv);
3962 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01003963 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003964 break;
3965
Bram Moolenaar590162c2022-12-24 21:24:06 +00003966 // Store a value in a list, dict, blob or object variable.
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01003967 case ISN_STOREINDEX:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003968 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003969 int res = execute_storeindex(iptr, ectx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003970
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003971 if (res == FAIL)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02003972 goto on_error;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003973 if (res == NOTDONE)
3974 goto theend;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003975 }
3976 break;
3977
Bram Moolenaarea5c8982022-02-17 14:42:02 +00003978 // store value in list or blob range
Bram Moolenaar68452172021-04-12 21:21:02 +02003979 case ISN_STORERANGE:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003980 if (execute_storerange(iptr, ectx) == FAIL)
3981 goto on_error;
Bram Moolenaar68452172021-04-12 21:21:02 +02003982 break;
3983
Bram Moolenaard505d172022-12-18 21:42:55 +00003984 case ISN_LOAD_CLASSMEMBER:
3985 {
3986 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
3987 goto theend;
3988 classmember_T *cm = &iptr->isn_arg.classmember;
Bram Moolenaar4e2406c2023-06-24 19:22:21 +01003989 copy_tv(cm->cm_class->class_members_tv + cm->cm_idx,
3990 STACK_TV_BOT(0));
Bram Moolenaard505d172022-12-18 21:42:55 +00003991 ++ectx->ec_stack.ga_len;
3992 }
3993 break;
3994
3995 case ISN_STORE_CLASSMEMBER:
3996 {
3997 classmember_T *cm = &iptr->isn_arg.classmember;
3998 tv = &cm->cm_class->class_members_tv[cm->cm_idx];
3999 clear_tv(tv);
4000 *tv = *STACK_TV_BOT(-1);
4001 --ectx->ec_stack.ga_len;
4002 }
4003 break;
4004
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01004005 // Load or store variable or argument from outer scope.
Bram Moolenaar0186e582021-01-10 18:33:11 +01004006 case ISN_LOADOUTER:
4007 case ISN_STOREOUTER:
4008 {
4009 int depth = iptr->isn_arg.outer.outer_depth;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004010 outer_T *outer = ectx->ec_outer_ref == NULL ? NULL
4011 : ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +01004012
4013 while (depth > 1 && outer != NULL)
4014 {
4015 outer = outer->out_up;
4016 --depth;
4017 }
4018 if (outer == NULL)
4019 {
4020 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar69c76172021-12-02 16:38:52 +00004021 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx
4022 || ectx->ec_outer_ref == NULL)
4023 // Possibly :def function called from legacy
4024 // context.
4025 emsg(_(e_closure_called_from_invalid_context));
4026 else
4027 iemsg("LOADOUTER depth more than scope levels");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004028 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01004029 }
Bram Moolenaarcc341812022-09-19 15:54:34 +01004030 if (depth < 0)
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01004031 // Variable declared in loop. May be copied if the
4032 // loop block has already ended.
Bram Moolenaarcc341812022-09-19 15:54:34 +01004033 tv = ((typval_T *)outer->out_loop[-depth - 1]
4034 .stack->ga_data)
4035 + outer->out_loop[-depth - 1].var_idx
4036 + iptr->isn_arg.outer.outer_idx;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004037 else
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01004038 // Variable declared in a function. May be copied if
4039 // the function has already returned.
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004040 tv = ((typval_T *)outer->out_stack->ga_data)
4041 + outer->out_frame_idx + STACK_FRAME_SIZE
4042 + iptr->isn_arg.outer.outer_idx;
Bram Moolenaar0186e582021-01-10 18:33:11 +01004043 if (iptr->isn_type == ISN_LOADOUTER)
4044 {
Bram Moolenaar35578162021-08-02 19:10:38 +02004045 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004046 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01004047 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02004048 ++ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01004049 }
4050 else
4051 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004052 --ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01004053 clear_tv(tv);
4054 *tv = *STACK_TV_BOT(0);
4055 }
4056 }
4057 break;
4058
Bram Moolenaar752fc692021-01-04 21:57:11 +01004059 // unlet item in list or dict variable
4060 case ISN_UNLETINDEX:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00004061 if (execute_unletindex(iptr, ectx) == FAIL)
4062 goto on_error;
Bram Moolenaar752fc692021-01-04 21:57:11 +01004063 break;
4064
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01004065 // unlet range of items in list variable
4066 case ISN_UNLETRANGE:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00004067 if (execute_unletrange(iptr, ectx) == FAIL)
4068 goto on_error;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01004069 break;
4070
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004071 // push constant
4072 case ISN_PUSHNR:
4073 case ISN_PUSHBOOL:
4074 case ISN_PUSHSPEC:
4075 case ISN_PUSHF:
4076 case ISN_PUSHS:
4077 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004078 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004079 case ISN_PUSHCHANNEL:
4080 case ISN_PUSHJOB:
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00004081 case ISN_PUSHOBJ:
4082 case ISN_PUSHCLASS:
Bram Moolenaar35578162021-08-02 19:10:38 +02004083 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004084 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004085 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02004086 tv->v_lock = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004087 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004088 switch (iptr->isn_type)
4089 {
4090 case ISN_PUSHNR:
4091 tv->v_type = VAR_NUMBER;
4092 tv->vval.v_number = iptr->isn_arg.number;
4093 break;
4094 case ISN_PUSHBOOL:
4095 tv->v_type = VAR_BOOL;
4096 tv->vval.v_number = iptr->isn_arg.number;
4097 break;
4098 case ISN_PUSHSPEC:
4099 tv->v_type = VAR_SPECIAL;
4100 tv->vval.v_number = iptr->isn_arg.number;
4101 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004102 case ISN_PUSHF:
4103 tv->v_type = VAR_FLOAT;
4104 tv->vval.v_float = iptr->isn_arg.fnumber;
4105 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004106 case ISN_PUSHBLOB:
4107 blob_copy(iptr->isn_arg.blob, tv);
4108 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004109 case ISN_PUSHFUNC:
4110 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01004111 if (iptr->isn_arg.string == NULL)
4112 tv->vval.v_string = NULL;
4113 else
4114 tv->vval.v_string =
4115 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004116 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004117 case ISN_PUSHCHANNEL:
4118#ifdef FEAT_JOB_CHANNEL
4119 tv->v_type = VAR_CHANNEL;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00004120 tv->vval.v_channel = NULL;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004121#endif
4122 break;
4123 case ISN_PUSHJOB:
4124#ifdef FEAT_JOB_CHANNEL
4125 tv->v_type = VAR_JOB;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00004126 tv->vval.v_job = NULL;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004127#endif
4128 break;
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00004129 case ISN_PUSHOBJ:
4130 tv->v_type = VAR_OBJECT;
4131 tv->vval.v_object = NULL;
4132 break;
4133 case ISN_PUSHCLASS:
4134 tv->v_type = VAR_CLASS;
Bram Moolenaar30a84472023-02-27 08:07:14 +00004135 tv->vval.v_class = iptr->isn_arg.classarg;
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00004136 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004137 default:
4138 tv->v_type = VAR_STRING;
Bram Moolenaarf8691002022-03-10 12:20:53 +00004139 tv->vval.v_string = iptr->isn_arg.string == NULL
4140 ? NULL : vim_strsave(iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004141 }
4142 break;
4143
Bram Moolenaar06b77222022-01-25 15:51:56 +00004144 case ISN_AUTOLOAD:
4145 {
4146 char_u *name = iptr->isn_arg.string;
4147
4148 (void)script_autoload(name, FALSE);
4149 if (find_func(name, TRUE))
4150 {
4151 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
4152 goto theend;
4153 tv = STACK_TV_BOT(0);
4154 tv->v_lock = 0;
4155 ++ectx->ec_stack.ga_len;
4156 tv->v_type = VAR_FUNC;
4157 tv->vval.v_string = vim_strsave(name);
4158 }
4159 else
4160 {
4161 int res = load_namespace_var(ectx, ISN_LOADG, iptr);
4162
4163 if (res == NOTDONE)
4164 goto theend;
4165 if (res == FAIL)
4166 goto on_error;
4167 }
4168 }
4169 break;
4170
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004171 case ISN_UNLET:
4172 if (do_unlet(iptr->isn_arg.unlet.ul_name,
4173 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02004174 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004175 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02004176 case ISN_UNLETENV:
LemonBoy77142312022-04-15 20:50:46 +01004177 vim_unsetenv_ext(iptr->isn_arg.unlet.ul_name);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02004178 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004179
Bram Moolenaaraacc9662021-08-13 19:40:51 +02004180 case ISN_LOCKUNLOCK:
4181 {
4182 typval_T *lval_root_save = lval_root;
4183 int res;
4184
4185 // Stack has the local variable, argument the whole :lock
4186 // or :unlock command, like ISN_EXEC.
4187 --ectx->ec_stack.ga_len;
4188 lval_root = STACK_TV_BOT(0);
4189 res = exec_command(iptr);
4190 clear_tv(lval_root);
4191 lval_root = lval_root_save;
4192 if (res == FAIL)
4193 goto on_error;
4194 }
4195 break;
4196
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02004197 case ISN_LOCKCONST:
4198 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
4199 break;
4200
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004201 // create a list from items on the stack; uses a single allocation
4202 // for the list header and the items
4203 case ISN_NEWLIST:
Bram Moolenaar4c137212021-04-19 16:48:48 +02004204 if (exe_newlist(iptr->isn_arg.number, ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004205 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004206 break;
4207
4208 // create a dict from items on the stack
4209 case ISN_NEWDICT:
4210 {
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01004211 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004212
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01004213 SOURCING_LNUM = iptr->isn_lnum;
4214 res = exe_newdict(iptr->isn_arg.number, ectx);
4215 if (res == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004216 goto theend;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01004217 if (res == MAYBE)
4218 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004219 }
4220 break;
4221
LemonBoy372bcce2022-04-25 12:43:20 +01004222 case ISN_CONCAT:
4223 if (exe_concat(iptr->isn_arg.number, ectx) == FAIL)
4224 goto theend;
4225 break;
4226
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004227 // create a partial with NULL value
4228 case ISN_NEWPARTIAL:
4229 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
4230 goto theend;
4231 ++ectx->ec_stack.ga_len;
4232 tv = STACK_TV_BOT(-1);
4233 tv->v_type = VAR_PARTIAL;
4234 tv->v_lock = 0;
4235 tv->vval.v_partial = NULL;
4236 break;
4237
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004238 // call a :def function
4239 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02004240 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01004241 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
4242 NULL,
4243 iptr->isn_arg.dfunc.cdf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02004244 ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02004245 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004246 break;
4247
Bram Moolenaard0200c82023-01-28 15:19:40 +00004248 // call a method on an interface
4249 case ISN_METHODCALL:
4250 {
Bram Moolenaar094cf9f2023-02-10 15:52:25 +00004251 cmfunc_T *mfunc = iptr->isn_arg.mfunc;
4252
Bram Moolenaard0200c82023-01-28 15:19:40 +00004253 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar094cf9f2023-02-10 15:52:25 +00004254 tv = STACK_TV_BOT(-1 - mfunc->cmf_argcount);
Bram Moolenaard0200c82023-01-28 15:19:40 +00004255 if (tv->v_type != VAR_OBJECT)
4256 {
4257 object_required_error(tv);
4258 goto on_error;
4259 }
4260 object_T *obj = tv->vval.v_object;
4261 class_T *cl = obj->obj_class;
4262
4263 // convert the interface index to the object index
Bram Moolenaard0200c82023-01-28 15:19:40 +00004264 int idx = object_index_from_itf_index(mfunc->cmf_itf,
Yegappan Lakshmanan5a05d372023-09-29 19:43:11 +02004265 TRUE, mfunc->cmf_idx, cl);
Bram Moolenaard0200c82023-01-28 15:19:40 +00004266
4267 if (call_ufunc(cl->class_obj_methods[idx], NULL,
4268 mfunc->cmf_argcount, ectx, NULL, NULL) == FAIL)
4269 goto on_error;
4270 }
4271 break;
4272
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004273 // call a builtin function
4274 case ISN_BCALL:
4275 SOURCING_LNUM = iptr->isn_lnum;
4276 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
4277 iptr->isn_arg.bfunc.cbf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02004278 ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004279 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004280 break;
4281
4282 // call a funcref or partial
4283 case ISN_PCALL:
4284 {
4285 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
4286 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02004287 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004288
4289 SOURCING_LNUM = iptr->isn_lnum;
4290 if (pfunc->cpf_top)
4291 {
4292 // funcref is above the arguments
4293 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
4294 }
4295 else
4296 {
4297 // Get the funcref from the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004298 --ectx->ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02004299 partial_tv = *STACK_TV_BOT(0);
4300 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004301 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004302 r = call_partial(tv, pfunc->cpf_argcount, ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02004303 if (tv == &partial_tv)
4304 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004305 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004306 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004307 }
4308 break;
4309
Bram Moolenaarbd5da372020-03-31 23:13:10 +02004310 case ISN_PCALL_END:
4311 // PCALL finished, arguments have been consumed and replaced by
4312 // the return value. Now clear the funcref from the stack,
4313 // and move the return value in its place.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004314 --ectx->ec_stack.ga_len;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02004315 clear_tv(STACK_TV_BOT(-1));
4316 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
4317 break;
4318
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004319 // call a user defined function or funcref/partial
4320 case ISN_UCALL:
4321 {
4322 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
4323
4324 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01004325 if (call_eval_func(cufunc->cuf_name, cufunc->cuf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02004326 ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004327 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004328 }
4329 break;
4330
Bram Moolenaar1d84f762022-09-03 21:35:53 +01004331 // :defer func(arg)
4332 case ISN_DEFER:
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00004333 case ISN_DEFEROBJ:
Bram Moolenaar806a2732022-09-04 15:40:36 +01004334 if (defer_command(iptr->isn_arg.defer.defer_var_idx,
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00004335 iptr->isn_type == ISN_DEFEROBJ,
Bram Moolenaar1d84f762022-09-03 21:35:53 +01004336 iptr->isn_arg.defer.defer_argcount, ectx) == FAIL)
4337 goto on_error;
4338 break;
4339
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004340 // Return from a :def function call without a value.
4341 // Return from a constructor.
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02004342 case ISN_RETURN_VOID:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004343 case ISN_RETURN_OBJECT:
Bram Moolenaar35578162021-08-02 19:10:38 +02004344 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004345 goto theend;
Bram Moolenaar299f3032021-01-08 20:53:09 +01004346 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004347 ++ectx->ec_stack.ga_len;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004348 if (iptr->isn_type == ISN_RETURN_VOID)
4349 {
4350 tv->v_type = VAR_VOID;
4351 tv->vval.v_number = 0;
4352 tv->v_lock = 0;
4353 }
4354 else
4355 {
4356 *tv = *STACK_TV_VAR(0);
4357 ++tv->vval.v_object->obj_refcount;
4358 }
Bram Moolenaar299f3032021-01-08 20:53:09 +01004359 // FALLTHROUGH
4360
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02004361 // return from a :def function call with what is on the stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004362 case ISN_RETURN:
4363 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004364 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01004365 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01004366
4367 if (trystack->ga_len > 0)
4368 trycmd = ((trycmd_T *)trystack->ga_data)
4369 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004370 if (trycmd != NULL
Bram Moolenaar4c137212021-04-19 16:48:48 +02004371 && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004372 {
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01004373 // jump to ":finally" or ":endtry"
4374 if (trycmd->tcd_finally_idx != 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004375 ectx->ec_iidx = trycmd->tcd_finally_idx;
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01004376 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02004377 ectx->ec_iidx = trycmd->tcd_endtry_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004378 trycmd->tcd_return = TRUE;
4379 }
4380 else
Bram Moolenaard032f342020-07-18 18:13:02 +02004381 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004382 }
4383 break;
4384
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004385 // push a partial, a reference to a compiled function
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004386 case ISN_FUNCREF:
4387 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004388 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
4389 ufunc_T *ufunc;
4390 funcref_T *funcref = &iptr->isn_arg.funcref;
4391 funcref_extra_T *extra = funcref->fr_extra;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004392
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004393 if (pt == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004394 goto theend;
Bram Moolenaar35578162021-08-02 19:10:38 +02004395 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02004396 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004397 vim_free(pt);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004398 goto theend;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004399 }
Bram Moolenaar313e4722023-02-08 20:55:27 +00004400 if (extra != NULL && extra->fre_class != NULL)
4401 {
4402 tv = STACK_TV_BOT(-1);
4403 if (tv->v_type != VAR_OBJECT)
4404 {
4405 object_required_error(tv);
4406 vim_free(pt);
4407 goto on_error;
4408 }
4409 object_T *obj = tv->vval.v_object;
4410 class_T *cl = obj->obj_class;
4411
4412 // convert the interface index to the object index
4413 int idx = object_index_from_itf_index(extra->fre_class,
Yegappan Lakshmanan5a05d372023-09-29 19:43:11 +02004414 TRUE, extra->fre_method_idx, cl);
Bram Moolenaar313e4722023-02-08 20:55:27 +00004415 ufunc = cl->class_obj_methods[idx];
4416 }
4417 else if (extra == NULL || extra->fre_func_name == NULL)
Bram Moolenaar38453522021-11-28 22:00:12 +00004418 {
4419 dfunc_T *pt_dfunc = ((dfunc_T *)def_functions.ga_data)
4420 + funcref->fr_dfunc_idx;
4421
4422 ufunc = pt_dfunc->df_ufunc;
4423 }
4424 else
Bram Moolenaar313e4722023-02-08 20:55:27 +00004425 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004426 ufunc = find_func(extra->fre_func_name, FALSE);
Bram Moolenaar313e4722023-02-08 20:55:27 +00004427 }
Bram Moolenaar56acd1f2022-02-18 13:24:52 +00004428 if (ufunc == NULL)
4429 {
4430 SOURCING_LNUM = iptr->isn_lnum;
4431 iemsg("ufunc unexpectedly NULL for FUNCREF");
4432 goto theend;
4433 }
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004434 if (fill_partial_and_closure(pt, ufunc,
Bram Moolenaarcc341812022-09-19 15:54:34 +01004435 extra == NULL ? NULL : &extra->fre_loopvar_info,
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004436 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004437 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004438 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004439 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004440 tv->vval.v_partial = pt;
4441 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02004442 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004443 }
4444 break;
4445
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004446 // Create a global function from a lambda.
4447 case ISN_NEWFUNC:
4448 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004449 newfuncarg_T *arg = iptr->isn_arg.newfunc.nf_arg;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004450
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004451 if (copy_lambda_to_global_func(arg->nfa_lambda,
Bram Moolenaarcc341812022-09-19 15:54:34 +01004452 arg->nfa_global, &arg->nfa_loopvar_info,
4453 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004454 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004455 }
4456 break;
4457
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004458 // List functions
4459 case ISN_DEF:
4460 if (iptr->isn_arg.string == NULL)
4461 list_functions(NULL);
4462 else
4463 {
Bram Moolenaar14336722022-01-08 16:02:59 +00004464 exarg_T ea;
4465 garray_T lines_to_free;
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004466
4467 CLEAR_FIELD(ea);
4468 ea.cmd = ea.arg = iptr->isn_arg.string;
Bram Moolenaar14336722022-01-08 16:02:59 +00004469 ga_init2(&lines_to_free, sizeof(char_u *), 50);
Bram Moolenaard4a9b7f2023-05-23 14:48:42 +01004470 SOURCING_LNUM = iptr->isn_lnum;
h-eastb895b0f2023-09-24 15:46:31 +02004471 define_function(&ea, NULL, &lines_to_free, 0, NULL, 0);
Bram Moolenaar14336722022-01-08 16:02:59 +00004472 ga_clear_strings(&lines_to_free);
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004473 }
4474 break;
4475
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004476 // jump if a condition is met
4477 case ISN_JUMP:
4478 {
4479 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004480 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004481 int jump = TRUE;
4482
4483 if (when != JUMP_ALWAYS)
4484 {
4485 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004486 if (when == JUMP_IF_COND_FALSE
Bram Moolenaar13106602020-10-04 16:06:05 +02004487 || when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004488 || when == JUMP_IF_COND_TRUE)
4489 {
4490 SOURCING_LNUM = iptr->isn_lnum;
4491 jump = tv_get_bool_chk(tv, &error);
4492 if (error)
4493 goto on_error;
4494 }
4495 else
4496 jump = tv2bool(tv);
Bram Moolenaarf6ced982022-04-28 12:00:49 +01004497 if (when == JUMP_IF_FALSE || when == JUMP_IF_COND_FALSE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004498 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01004499 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004500 {
4501 // drop the value from the stack
4502 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004503 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004504 }
4505 }
4506 if (jump)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004507 ectx->ec_iidx = iptr->isn_arg.jump.jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004508 }
4509 break;
4510
Bram Moolenaarb46c0832022-09-15 17:19:37 +01004511 // "while": jump to end if a condition is false
4512 case ISN_WHILE:
4513 {
4514 int error = FALSE;
4515 int jump = TRUE;
4516
4517 tv = STACK_TV_BOT(-1);
4518 SOURCING_LNUM = iptr->isn_lnum;
4519 jump = !tv_get_bool_chk(tv, &error);
4520 if (error)
4521 goto on_error;
4522 // drop the value from the stack
4523 clear_tv(tv);
4524 --ectx->ec_stack.ga_len;
4525 if (jump)
4526 ectx->ec_iidx = iptr->isn_arg.whileloop.while_end;
4527
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01004528 // Store the current funcref count, may be used by
Bram Moolenaarcc341812022-09-19 15:54:34 +01004529 // ISN_ENDLOOP later
Bram Moolenaarb46c0832022-09-15 17:19:37 +01004530 tv = STACK_TV_VAR(
4531 iptr->isn_arg.whileloop.while_funcref_idx);
4532 tv->vval.v_number = ectx->ec_funcrefs.ga_len;
4533 }
4534 break;
4535
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02004536 // Jump if an argument with a default value was already set and not
4537 // v:none.
4538 case ISN_JUMP_IF_ARG_SET:
Bram Moolenaar65b0d162022-12-13 18:43:22 +00004539 case ISN_JUMP_IF_ARG_NOT_SET:
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02004540 tv = STACK_TV_VAR(iptr->isn_arg.jumparg.jump_arg_off);
Bram Moolenaar65b0d162022-12-13 18:43:22 +00004541 int arg_set = tv->v_type != VAR_UNKNOWN
4542 && !(tv->v_type == VAR_SPECIAL
4543 && tv->vval.v_number == VVAL_NONE);
4544 if (iptr->isn_type == ISN_JUMP_IF_ARG_SET ? arg_set : !arg_set)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004545 ectx->ec_iidx = iptr->isn_arg.jumparg.jump_where;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02004546 break;
4547
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004548 // top of a for loop
4549 case ISN_FOR:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00004550 if (execute_for(iptr, ectx) == FAIL)
4551 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004552 break;
4553
Bram Moolenaarb46c0832022-09-15 17:19:37 +01004554 // end of a for or while loop
4555 case ISN_ENDLOOP:
4556 if (execute_endloop(iptr, ectx) == FAIL)
4557 goto theend;
4558 break;
4559
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004560 // start of ":try" block
4561 case ISN_TRY:
4562 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01004563 trycmd_T *trycmd = NULL;
4564
Bram Moolenaar35578162021-08-02 19:10:38 +02004565 if (GA_GROW_FAILS(&ectx->ec_trystack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004566 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004567 trycmd = ((trycmd_T *)ectx->ec_trystack.ga_data)
4568 + ectx->ec_trystack.ga_len;
4569 ++ectx->ec_trystack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004570 ++trylevel;
Bram Moolenaar8d4be892021-02-13 18:33:02 +01004571 CLEAR_POINTER(trycmd);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004572 trycmd->tcd_frame_idx = ectx->ec_frame_idx;
4573 trycmd->tcd_stack_len = ectx->ec_stack.ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01004574 trycmd->tcd_catch_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00004575 iptr->isn_arg.tryref.try_ref->try_catch;
Bram Moolenaara91a7132021-03-25 21:12:15 +01004576 trycmd->tcd_finally_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00004577 iptr->isn_arg.tryref.try_ref->try_finally;
Bram Moolenaara91a7132021-03-25 21:12:15 +01004578 trycmd->tcd_endtry_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00004579 iptr->isn_arg.tryref.try_ref->try_endtry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004580 }
4581 break;
4582
4583 case ISN_PUSHEXC:
4584 if (current_exception == NULL)
4585 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004586 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004587 iemsg("Evaluating catch while current_exception is NULL");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004588 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004589 }
Bram Moolenaar35578162021-08-02 19:10:38 +02004590 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004591 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004592 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004593 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004594 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02004595 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004596 tv->vval.v_string = vim_strsave(
4597 (char_u *)current_exception->value);
4598 break;
4599
4600 case ISN_CATCH:
4601 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004602 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar06651632022-04-27 17:54:25 +01004603 trycmd_T *trycmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004604
Bram Moolenaar4c137212021-04-19 16:48:48 +02004605 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar06651632022-04-27 17:54:25 +01004606 trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004607 + trystack->ga_len - 1;
Bram Moolenaar06651632022-04-27 17:54:25 +01004608 trycmd->tcd_caught = TRUE;
4609 trycmd->tcd_did_throw = FALSE;
4610
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004611 did_emsg = got_int = did_throw = FALSE;
Bram Moolenaar1430cee2021-01-17 19:20:32 +01004612 force_abort = need_rethrow = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004613 catch_exception(current_exception);
4614 }
4615 break;
4616
Bram Moolenaarc150c092021-02-13 15:02:46 +01004617 case ISN_TRYCONT:
4618 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004619 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004620 trycont_T *trycont = &iptr->isn_arg.trycont;
4621 int i;
4622 trycmd_T *trycmd;
4623 int iidx = trycont->tct_where;
4624
4625 if (trystack->ga_len < trycont->tct_levels)
4626 {
4627 siemsg("TRYCONT: expected %d levels, found %d",
4628 trycont->tct_levels, trystack->ga_len);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004629 goto theend;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004630 }
4631 // Make :endtry jump to any outer try block and the last
4632 // :endtry inside the loop to the loop start.
4633 for (i = trycont->tct_levels; i > 0; --i)
4634 {
4635 trycmd = ((trycmd_T *)trystack->ga_data)
4636 + trystack->ga_len - i;
Bram Moolenaar2e34c342021-03-14 12:13:33 +01004637 // Add one to tcd_cont to be able to jump to
4638 // instruction with index zero.
4639 trycmd->tcd_cont = iidx + 1;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01004640 iidx = trycmd->tcd_finally_idx == 0
4641 ? trycmd->tcd_endtry_idx : trycmd->tcd_finally_idx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004642 }
4643 // jump to :finally or :endtry of current try statement
Bram Moolenaar4c137212021-04-19 16:48:48 +02004644 ectx->ec_iidx = iidx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004645 }
4646 break;
4647
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01004648 case ISN_FINALLY:
4649 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004650 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01004651 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
4652 + trystack->ga_len - 1;
4653
4654 // Reset the index to avoid a return statement jumps here
4655 // again.
4656 trycmd->tcd_finally_idx = 0;
4657 break;
4658 }
4659
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004660 // end of ":try" block
4661 case ISN_ENDTRY:
4662 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004663 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar06651632022-04-27 17:54:25 +01004664 trycmd_T *trycmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004665
Bram Moolenaar06651632022-04-27 17:54:25 +01004666 --trystack->ga_len;
4667 --trylevel;
4668 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len;
4669 if (trycmd->tcd_did_throw)
4670 did_throw = TRUE;
4671 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004672 {
Bram Moolenaar06651632022-04-27 17:54:25 +01004673 // discard the exception
4674 if (caught_stack == current_exception)
4675 caught_stack = caught_stack->caught;
4676 discard_current_exception();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004677 }
Bram Moolenaar06651632022-04-27 17:54:25 +01004678
4679 if (trycmd->tcd_return)
4680 goto func_return;
4681
4682 while (ectx->ec_stack.ga_len > trycmd->tcd_stack_len)
4683 {
4684 --ectx->ec_stack.ga_len;
4685 clear_tv(STACK_TV_BOT(0));
4686 }
4687 if (trycmd->tcd_cont != 0)
4688 // handling :continue: jump to outer try block or
4689 // start of the loop
4690 ectx->ec_iidx = trycmd->tcd_cont - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004691 }
4692 break;
4693
4694 case ISN_THROW:
Bram Moolenaar8f81b222021-01-14 21:47:06 +01004695 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004696 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02004697
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004698 if (trystack->ga_len == 0 && trylevel == 0 && emsg_silent)
4699 {
Bram Moolenaar3ef2e412023-04-30 18:50:48 +01004700 // Throwing an exception while using "silent!" causes
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004701 // the function to abort but not display an error.
4702 tv = STACK_TV_BOT(-1);
4703 clear_tv(tv);
4704 tv->v_type = VAR_NUMBER;
4705 tv->vval.v_number = 0;
4706 goto done;
4707 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004708 --ectx->ec_stack.ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004709 tv = STACK_TV_BOT(0);
4710 if (tv->vval.v_string == NULL
4711 || *skipwhite(tv->vval.v_string) == NUL)
4712 {
4713 vim_free(tv->vval.v_string);
4714 SOURCING_LNUM = iptr->isn_lnum;
4715 emsg(_(e_throw_with_empty_string));
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004716 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004717 }
4718
4719 // Inside a "catch" we need to first discard the caught
4720 // exception.
4721 if (trystack->ga_len > 0)
4722 {
4723 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
4724 + trystack->ga_len - 1;
4725 if (trycmd->tcd_caught && current_exception != NULL)
4726 {
4727 // discard the exception
4728 if (caught_stack == current_exception)
4729 caught_stack = caught_stack->caught;
4730 discard_current_exception();
4731 trycmd->tcd_caught = FALSE;
4732 }
4733 }
4734
Bram Moolenaar90a57162022-02-12 14:23:17 +00004735 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004736 if (throw_exception(tv->vval.v_string, ET_USER, NULL)
4737 == FAIL)
4738 {
4739 vim_free(tv->vval.v_string);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004740 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004741 }
4742 did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004743 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004744 break;
4745
4746 // compare with special values
4747 case ISN_COMPAREBOOL:
4748 case ISN_COMPARESPECIAL:
4749 {
4750 typval_T *tv1 = STACK_TV_BOT(-2);
4751 typval_T *tv2 = STACK_TV_BOT(-1);
4752 varnumber_T arg1 = tv1->vval.v_number;
4753 varnumber_T arg2 = tv2->vval.v_number;
4754 int res;
4755
Bram Moolenaar06651632022-04-27 17:54:25 +01004756 if (iptr->isn_arg.op.op_type == EXPR_EQUAL)
4757 res = arg1 == arg2;
4758 else
4759 res = arg1 != arg2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004760
Bram Moolenaar4c137212021-04-19 16:48:48 +02004761 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004762 tv1->v_type = VAR_BOOL;
4763 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4764 }
4765 break;
4766
Bram Moolenaar7a222242022-03-01 19:23:24 +00004767 case ISN_COMPARENULL:
4768 {
4769 typval_T *tv1 = STACK_TV_BOT(-2);
4770 typval_T *tv2 = STACK_TV_BOT(-1);
4771 int res;
4772
4773 res = typval_compare_null(tv1, tv2);
4774 if (res == MAYBE)
4775 goto on_error;
4776 if (iptr->isn_arg.op.op_type == EXPR_NEQUAL)
4777 res = !res;
4778 clear_tv(tv1);
4779 clear_tv(tv2);
4780 --ectx->ec_stack.ga_len;
4781 tv1->v_type = VAR_BOOL;
4782 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4783 }
4784 break;
4785
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004786 // Operation with two number arguments
4787 case ISN_OPNR:
4788 case ISN_COMPARENR:
4789 {
4790 typval_T *tv1 = STACK_TV_BOT(-2);
4791 typval_T *tv2 = STACK_TV_BOT(-1);
4792 varnumber_T arg1 = tv1->vval.v_number;
4793 varnumber_T arg2 = tv2->vval.v_number;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004794 varnumber_T res = 0;
4795 int div_zero = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004796
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004797 if (iptr->isn_arg.op.op_type == EXPR_LSHIFT
4798 || iptr->isn_arg.op.op_type == EXPR_RSHIFT)
4799 {
4800 if (arg2 < 0)
4801 {
4802 SOURCING_LNUM = iptr->isn_lnum;
dundargocc57b5bc2022-11-02 13:30:51 +00004803 emsg(_(e_bitshift_ops_must_be_positive));
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004804 goto on_error;
4805 }
4806 }
4807
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004808 switch (iptr->isn_arg.op.op_type)
4809 {
4810 case EXPR_MULT: res = arg1 * arg2; break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004811 case EXPR_DIV: if (arg2 == 0)
4812 div_zero = TRUE;
4813 else
4814 res = arg1 / arg2;
4815 break;
4816 case EXPR_REM: if (arg2 == 0)
4817 div_zero = TRUE;
4818 else
4819 res = arg1 % arg2;
4820 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004821 case EXPR_SUB: res = arg1 - arg2; break;
4822 case EXPR_ADD: res = arg1 + arg2; break;
4823
4824 case EXPR_EQUAL: res = arg1 == arg2; break;
4825 case EXPR_NEQUAL: res = arg1 != arg2; break;
4826 case EXPR_GREATER: res = arg1 > arg2; break;
4827 case EXPR_GEQUAL: res = arg1 >= arg2; break;
4828 case EXPR_SMALLER: res = arg1 < arg2; break;
4829 case EXPR_SEQUAL: res = arg1 <= arg2; break;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004830 case EXPR_LSHIFT: if (arg2 > MAX_LSHIFT_BITS)
4831 res = 0;
4832 else
Bram Moolenaar68e64d22022-05-22 22:07:52 +01004833 res = (uvarnumber_T)arg1 << arg2;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004834 break;
4835 case EXPR_RSHIFT: if (arg2 > MAX_LSHIFT_BITS)
4836 res = 0;
4837 else
Bram Moolenaar338bf582022-05-22 20:16:32 +01004838 res = (uvarnumber_T)arg1 >> arg2;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004839 break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004840 default: break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004841 }
4842
Bram Moolenaar4c137212021-04-19 16:48:48 +02004843 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004844 if (iptr->isn_type == ISN_COMPARENR)
4845 {
4846 tv1->v_type = VAR_BOOL;
4847 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4848 }
4849 else
4850 tv1->vval.v_number = res;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004851 if (div_zero)
4852 {
4853 SOURCING_LNUM = iptr->isn_lnum;
4854 emsg(_(e_divide_by_zero));
4855 goto on_error;
4856 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004857 }
4858 break;
4859
4860 // Computation with two float arguments
4861 case ISN_OPFLOAT:
4862 case ISN_COMPAREFLOAT:
4863 {
4864 typval_T *tv1 = STACK_TV_BOT(-2);
4865 typval_T *tv2 = STACK_TV_BOT(-1);
4866 float_T arg1 = tv1->vval.v_float;
4867 float_T arg2 = tv2->vval.v_float;
4868 float_T res = 0;
4869 int cmp = FALSE;
4870
4871 switch (iptr->isn_arg.op.op_type)
4872 {
4873 case EXPR_MULT: res = arg1 * arg2; break;
4874 case EXPR_DIV: res = arg1 / arg2; break;
4875 case EXPR_SUB: res = arg1 - arg2; break;
4876 case EXPR_ADD: res = arg1 + arg2; break;
4877
4878 case EXPR_EQUAL: cmp = arg1 == arg2; break;
4879 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
4880 case EXPR_GREATER: cmp = arg1 > arg2; break;
4881 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
4882 case EXPR_SMALLER: cmp = arg1 < arg2; break;
4883 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
4884 default: cmp = 0; break;
4885 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004886 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004887 if (iptr->isn_type == ISN_COMPAREFLOAT)
4888 {
4889 tv1->v_type = VAR_BOOL;
4890 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
4891 }
4892 else
4893 tv1->vval.v_float = res;
4894 }
4895 break;
4896
4897 case ISN_COMPARELIST:
Bram Moolenaar265f8112021-12-19 12:33:05 +00004898 case ISN_COMPAREDICT:
4899 case ISN_COMPAREFUNC:
4900 case ISN_COMPARESTRING:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004901 case ISN_COMPAREBLOB:
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00004902 case ISN_COMPARECLASS:
4903 case ISN_COMPAREOBJECT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004904 {
4905 typval_T *tv1 = STACK_TV_BOT(-2);
4906 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar265f8112021-12-19 12:33:05 +00004907 exprtype_T exprtype = iptr->isn_arg.op.op_type;
4908 int ic = iptr->isn_arg.op.op_ic;
4909 int res = FALSE;
4910 int status = OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004911
Bram Moolenaar265f8112021-12-19 12:33:05 +00004912 SOURCING_LNUM = iptr->isn_lnum;
4913 if (iptr->isn_type == ISN_COMPARELIST)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004914 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00004915 status = typval_compare_list(tv1, tv2,
4916 exprtype, ic, &res);
4917 }
4918 else if (iptr->isn_type == ISN_COMPAREDICT)
4919 {
4920 status = typval_compare_dict(tv1, tv2,
4921 exprtype, ic, &res);
4922 }
4923 else if (iptr->isn_type == ISN_COMPAREFUNC)
4924 {
4925 status = typval_compare_func(tv1, tv2,
4926 exprtype, ic, &res);
4927 }
4928 else if (iptr->isn_type == ISN_COMPARESTRING)
4929 {
4930 status = typval_compare_string(tv1, tv2,
4931 exprtype, ic, &res);
4932 }
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00004933 else if (iptr->isn_type == ISN_COMPAREBLOB)
Bram Moolenaar265f8112021-12-19 12:33:05 +00004934 {
4935 status = typval_compare_blob(tv1, tv2, exprtype, &res);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004936 }
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00004937 else if (iptr->isn_type == ISN_COMPARECLASS)
4938 {
Bram Moolenaar03ff0c62023-01-02 20:38:01 +00004939 status = typval_compare_class(tv1, tv2,
4940 exprtype, FALSE, &res);
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00004941 }
4942 else // ISN_COMPAREOBJECT
4943 {
4944 status = typval_compare_object(tv1, tv2,
Bram Moolenaar03ff0c62023-01-02 20:38:01 +00004945 exprtype, FALSE, &res);
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00004946 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004947 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004948 clear_tv(tv1);
4949 clear_tv(tv2);
4950 tv1->v_type = VAR_BOOL;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004951 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4952 if (status == FAIL)
4953 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004954 }
4955 break;
4956
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004957 case ISN_COMPAREANY:
4958 {
4959 typval_T *tv1 = STACK_TV_BOT(-2);
4960 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar657137c2021-01-09 15:45:23 +01004961 exprtype_T exprtype = iptr->isn_arg.op.op_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004962 int ic = iptr->isn_arg.op.op_ic;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004963 int status;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004964
Bram Moolenaareb26f432020-09-14 16:50:05 +02004965 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004966 status = typval_compare(tv1, tv2, exprtype, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004967 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004968 --ectx->ec_stack.ga_len;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004969 if (status == FAIL)
4970 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004971 }
4972 break;
4973
4974 case ISN_ADDLIST:
4975 case ISN_ADDBLOB:
4976 {
4977 typval_T *tv1 = STACK_TV_BOT(-2);
4978 typval_T *tv2 = STACK_TV_BOT(-1);
4979
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004980 // add two lists or blobs
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004981 if (iptr->isn_type == ISN_ADDLIST)
Bram Moolenaar07802042021-09-09 23:01:14 +02004982 {
4983 if (iptr->isn_arg.op.op_type == EXPR_APPEND
4984 && tv1->vval.v_list != NULL)
4985 list_extend(tv1->vval.v_list, tv2->vval.v_list,
4986 NULL);
4987 else
4988 eval_addlist(tv1, tv2);
4989 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004990 else
4991 eval_addblob(tv1, tv2);
4992 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004993 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004994 }
4995 break;
4996
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004997 case ISN_LISTAPPEND:
4998 {
4999 typval_T *tv1 = STACK_TV_BOT(-2);
5000 typval_T *tv2 = STACK_TV_BOT(-1);
5001 list_T *l = tv1->vval.v_list;
5002
5003 // add an item to a list
Bram Moolenaar1f4a3452022-01-01 18:29:21 +00005004 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02005005 if (l == NULL)
5006 {
Bram Moolenaar1dcae592020-10-19 19:02:42 +02005007 emsg(_(e_cannot_add_to_null_list));
5008 goto on_error;
5009 }
Bram Moolenaar1f4a3452022-01-01 18:29:21 +00005010 if (value_check_lock(l->lv_lock, NULL, FALSE))
5011 goto on_error;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02005012 if (list_append_tv(l, tv2) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005013 goto theend;
Bram Moolenaar955347c2020-10-19 23:01:46 +02005014 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005015 --ectx->ec_stack.ga_len;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02005016 }
5017 break;
5018
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02005019 case ISN_BLOBAPPEND:
5020 {
5021 typval_T *tv1 = STACK_TV_BOT(-2);
5022 typval_T *tv2 = STACK_TV_BOT(-1);
5023 blob_T *b = tv1->vval.v_blob;
5024 int error = FALSE;
5025 varnumber_T n;
5026
5027 // add a number to a blob
5028 if (b == NULL)
5029 {
5030 SOURCING_LNUM = iptr->isn_lnum;
5031 emsg(_(e_cannot_add_to_null_blob));
5032 goto on_error;
5033 }
5034 n = tv_get_number_chk(tv2, &error);
5035 if (error)
5036 goto on_error;
5037 ga_append(&b->bv_ga, (int)n);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005038 --ectx->ec_stack.ga_len;
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02005039 }
5040 break;
5041
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005042 // Computation with two arguments of unknown type
5043 case ISN_OPANY:
5044 {
5045 typval_T *tv1 = STACK_TV_BOT(-2);
5046 typval_T *tv2 = STACK_TV_BOT(-1);
5047 varnumber_T n1, n2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005048 float_T f1 = 0, f2 = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005049 int error = FALSE;
5050
5051 if (iptr->isn_arg.op.op_type == EXPR_ADD)
5052 {
5053 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
5054 {
5055 eval_addlist(tv1, tv2);
5056 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005057 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005058 break;
5059 }
5060 else if (tv1->v_type == VAR_BLOB
5061 && tv2->v_type == VAR_BLOB)
5062 {
5063 eval_addblob(tv1, tv2);
5064 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005065 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005066 break;
5067 }
5068 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005069 if (tv1->v_type == VAR_FLOAT)
5070 {
5071 f1 = tv1->vval.v_float;
5072 n1 = 0;
5073 }
5074 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005075 {
Bram Moolenaarf665e972020-12-05 19:17:16 +01005076 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005077 n1 = tv_get_number_chk(tv1, &error);
5078 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02005079 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005080 if (tv2->v_type == VAR_FLOAT)
5081 f1 = n1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005082 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005083 if (tv2->v_type == VAR_FLOAT)
5084 {
5085 f2 = tv2->vval.v_float;
5086 n2 = 0;
5087 }
5088 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005089 {
5090 n2 = tv_get_number_chk(tv2, &error);
5091 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02005092 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005093 if (tv1->v_type == VAR_FLOAT)
5094 f2 = n2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005095 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005096 // if there is a float on either side the result is a float
5097 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
5098 {
5099 switch (iptr->isn_arg.op.op_type)
5100 {
5101 case EXPR_MULT: f1 = f1 * f2; break;
5102 case EXPR_DIV: f1 = f1 / f2; break;
5103 case EXPR_SUB: f1 = f1 - f2; break;
5104 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005105 default: SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005106 emsg(_(e_cannot_use_percent_with_float));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02005107 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005108 }
5109 clear_tv(tv1);
5110 clear_tv(tv2);
5111 tv1->v_type = VAR_FLOAT;
5112 tv1->vval.v_float = f1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005113 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005114 }
5115 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005116 {
Bram Moolenaarb1f28572021-01-21 13:03:20 +01005117 int failed = FALSE;
5118
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005119 switch (iptr->isn_arg.op.op_type)
5120 {
5121 case EXPR_MULT: n1 = n1 * n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01005122 case EXPR_DIV: n1 = num_divide(n1, n2, &failed);
5123 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01005124 goto on_error;
5125 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005126 case EXPR_SUB: n1 = n1 - n2; break;
5127 case EXPR_ADD: n1 = n1 + n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01005128 default: n1 = num_modulus(n1, n2, &failed);
5129 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01005130 goto on_error;
5131 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005132 }
5133 clear_tv(tv1);
5134 clear_tv(tv2);
5135 tv1->v_type = VAR_NUMBER;
5136 tv1->vval.v_number = n1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005137 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005138 }
5139 }
5140 break;
5141
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005142 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005143 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005144 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005145 int is_slice = iptr->isn_type == ISN_STRSLICE;
5146 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005147 char_u *res;
5148
5149 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005150 // string slice: string is at stack-3, first index at
5151 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005152 if (is_slice)
5153 {
5154 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005155 n1 = tv->vval.v_number;
5156 }
5157
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005158 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005159 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005160
Bram Moolenaar4c137212021-04-19 16:48:48 +02005161 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005162 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005163 if (is_slice)
5164 // Slice: Select the characters from the string
Bram Moolenaar6601b622021-01-13 21:47:15 +01005165 res = string_slice(tv->vval.v_string, n1, n2, FALSE);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005166 else
5167 // Index: The resulting variable is a string of a
Bram Moolenaar0289a092021-03-14 18:40:19 +01005168 // single character (including composing characters).
5169 // If the index is too big or negative the result is
5170 // empty.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005171 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005172 vim_free(tv->vval.v_string);
5173 tv->vval.v_string = res;
5174 }
5175 break;
5176
5177 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02005178 case ISN_LISTSLICE:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02005179 case ISN_BLOBINDEX:
5180 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005181 {
Bram Moolenaarcfc30232021-04-11 20:26:34 +02005182 int is_slice = iptr->isn_type == ISN_LISTSLICE
5183 || iptr->isn_type == ISN_BLOBSLICE;
5184 int is_blob = iptr->isn_type == ISN_BLOBINDEX
5185 || iptr->isn_type == ISN_BLOBSLICE;
Bram Moolenaared591872020-08-15 22:14:53 +02005186 varnumber_T n1, n2;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02005187 typval_T *val_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005188
5189 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02005190 // list slice: list is at stack-3, indexes at stack-2 and
5191 // stack-1
Bram Moolenaarcfc30232021-04-11 20:26:34 +02005192 // Same for blob.
5193 val_tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005194
5195 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02005196 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005197 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02005198
5199 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005200 {
Bram Moolenaared591872020-08-15 22:14:53 +02005201 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02005202 n1 = tv->vval.v_number;
5203 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005204 }
Bram Moolenaared591872020-08-15 22:14:53 +02005205
Bram Moolenaar4c137212021-04-19 16:48:48 +02005206 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02005207 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02005208 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02005209 if (is_blob)
5210 {
5211 if (blob_slice_or_index(val_tv->vval.v_blob, is_slice,
5212 n1, n2, FALSE, tv) == FAIL)
5213 goto on_error;
5214 }
5215 else
5216 {
5217 if (list_slice_or_index(val_tv->vval.v_list, is_slice,
5218 n1, n2, FALSE, tv, TRUE) == FAIL)
5219 goto on_error;
5220 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005221 }
5222 break;
5223
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005224 case ISN_ANYINDEX:
5225 case ISN_ANYSLICE:
5226 {
5227 int is_slice = iptr->isn_type == ISN_ANYSLICE;
5228 typval_T *var1, *var2;
5229 int res;
5230
5231 // index: composite is at stack-2, index at stack-1
5232 // slice: composite is at stack-3, indexes at stack-2 and
5233 // stack-1
5234 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02005235 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005236 if (check_can_index(tv, TRUE, TRUE) == FAIL)
5237 goto on_error;
5238 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
5239 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
Bram Moolenaar6601b622021-01-13 21:47:15 +01005240 res = eval_index_inner(tv, is_slice, var1, var2,
5241 FALSE, NULL, -1, TRUE);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005242 clear_tv(var1);
5243 if (is_slice)
5244 clear_tv(var2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005245 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005246 if (res == FAIL)
5247 goto on_error;
5248 }
5249 break;
5250
Bram Moolenaar9af78762020-06-16 11:34:42 +02005251 case ISN_SLICE:
5252 {
5253 list_T *list;
5254 int count = iptr->isn_arg.number;
5255
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02005256 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02005257 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02005258 list = tv->vval.v_list;
5259
5260 // no error for short list, expect it to be checked earlier
5261 if (list != NULL && list->lv_len >= count)
5262 {
5263 list_T *newlist = list_slice(list,
5264 count, list->lv_len - 1);
5265
5266 if (newlist != NULL)
5267 {
5268 list_unref(list);
5269 tv->vval.v_list = newlist;
5270 ++newlist->lv_refcount;
5271 }
5272 }
5273 }
5274 break;
5275
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005276 case ISN_GETITEM:
5277 {
5278 listitem_T *li;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02005279 getitem_T *gi = &iptr->isn_arg.getitem;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005280
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02005281 // Get list item: list is at stack-1, push item.
5282 // List type and length is checked for when compiling.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02005283 tv = STACK_TV_BOT(-1 - gi->gi_with_op);
5284 li = list_find(tv->vval.v_list, gi->gi_index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005285
Bram Moolenaar35578162021-08-02 19:10:38 +02005286 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005287 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005288 ++ectx->ec_stack.ga_len;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005289 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
Bram Moolenaarf785aa12021-02-11 21:19:34 +01005290
5291 // Useful when used in unpack assignment. Reset at
5292 // ISN_DROP.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02005293 ectx->ec_where.wt_index = gi->gi_index + 1;
LemonBoyc5d27442023-08-19 13:02:35 +02005294 ectx->ec_where.wt_kind = WT_VARIABLE;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005295 }
5296 break;
5297
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005298 case ISN_MEMBER:
5299 {
5300 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005301 char_u *key;
5302 dictitem_T *di;
5303
5304 // dict member: dict is at stack-2, key at stack-1
5305 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02005306 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005307 dict = tv->vval.v_dict;
5308
5309 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02005310 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005311 key = tv->vval.v_string;
Bram Moolenaar086fc9a2020-10-30 18:33:02 +01005312 if (key == NULL)
5313 key = (char_u *)"";
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02005314
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005315 if ((di = dict_find(dict, key, -1)) == NULL)
5316 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005317 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00005318 semsg(_(e_key_not_present_in_dictionary_str), key);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01005319
5320 // If :silent! is used we will continue, make sure the
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005321 // stack contents makes sense and the dict stack is
5322 // updated.
Bram Moolenaar4029cab2020-12-05 18:13:27 +01005323 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005324 --ectx->ec_stack.ga_len;
Bram Moolenaar4029cab2020-12-05 18:13:27 +01005325 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005326 (void) dict_stack_save(tv);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01005327 tv->v_type = VAR_NUMBER;
5328 tv->vval.v_number = 0;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01005329 goto on_fatal_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005330 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005331 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005332 --ectx->ec_stack.ga_len;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005333 // Put the dict used on the dict stack, it might be used by
5334 // a dict function later.
Bram Moolenaar50788ef2020-07-05 16:51:26 +02005335 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005336 if (dict_stack_save(tv) == FAIL)
5337 goto on_fatal_error;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02005338 copy_tv(&di->di_tv, tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005339 }
5340 break;
5341
5342 // dict member with string key
5343 case ISN_STRINGMEMBER:
5344 {
5345 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005346 dictitem_T *di;
5347
5348 tv = STACK_TV_BOT(-1);
5349 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
5350 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005351 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005352 emsg(_(e_dictionary_required));
Bram Moolenaard032f342020-07-18 18:13:02 +02005353 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005354 }
5355 dict = tv->vval.v_dict;
5356
5357 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
5358 == NULL)
5359 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005360 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00005361 semsg(_(e_key_not_present_in_dictionary_str),
Bram Moolenaar381692b2022-02-02 20:01:27 +00005362 iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02005363 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005364 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005365 // Put the dict used on the dict stack, it might be used by
5366 // a dict function later.
5367 if (dict_stack_save(tv) == FAIL)
5368 goto on_fatal_error;
5369
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005370 copy_tv(&di->di_tv, tv);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005371 }
5372 break;
5373
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005374 case ISN_GET_OBJ_MEMBER:
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00005375 case ISN_GET_ITF_MEMBER:
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005376 {
5377 tv = STACK_TV_BOT(-1);
5378 if (tv->v_type != VAR_OBJECT)
5379 {
5380 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaard0200c82023-01-28 15:19:40 +00005381 object_required_error(tv);
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005382 goto on_error;
5383 }
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00005384
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005385 object_T *obj = tv->vval.v_object;
Bram Moolenaarc3f971f2023-03-02 17:38:33 +00005386 if (obj == NULL)
5387 {
5388 SOURCING_LNUM = iptr->isn_lnum;
5389 emsg(_(e_using_null_object));
5390 goto on_error;
5391 }
5392
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00005393 int idx;
5394 if (iptr->isn_type == ISN_GET_OBJ_MEMBER)
Ernie Rael18143d32023-09-04 22:30:41 +02005395 idx = iptr->isn_arg.classmember.cm_idx;
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00005396 else
5397 {
5398 idx = iptr->isn_arg.classmember.cm_idx;
5399 // convert the interface index to the object index
5400 idx = object_index_from_itf_index(
Ernie Rael18143d32023-09-04 22:30:41 +02005401 iptr->isn_arg.classmember.cm_class,
Yegappan Lakshmanan5a05d372023-09-29 19:43:11 +02005402 FALSE, idx, obj->obj_class);
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00005403 }
5404
Ernie Rael18143d32023-09-04 22:30:41 +02005405 // The members are located right after the object struct.
Yegappan Lakshmanan5a05d372023-09-29 19:43:11 +02005406 typval_T *mtv = ((typval_T *)(obj + 1)) + idx;
Bram Moolenaar590162c2022-12-24 21:24:06 +00005407 copy_tv(mtv, tv);
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005408
5409 // Unreference the object after getting the member, it may
5410 // be freed.
5411 object_unref(obj);
5412 }
5413 break;
5414
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005415 case ISN_STORE_THIS:
5416 {
5417 int idx = iptr->isn_arg.number;
5418 object_T *obj = STACK_TV_VAR(0)->vval.v_object;
5419 // the members are located right after the object struct
5420 typval_T *mtv = ((typval_T *)(obj + 1)) + idx;
5421 clear_tv(mtv);
5422 *mtv = *STACK_TV_BOT(-1);
5423 --ectx->ec_stack.ga_len;
5424 }
5425 break;
5426
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005427 case ISN_CLEARDICT:
5428 dict_stack_drop();
5429 break;
5430
5431 case ISN_USEDICT:
5432 {
5433 typval_T *dict_tv = dict_stack_get_tv();
5434
5435 // Turn "dict.Func" into a partial for "Func" bound to
5436 // "dict". Don't do this when "Func" is already a partial
5437 // that was bound explicitly (pt_auto is FALSE).
5438 tv = STACK_TV_BOT(-1);
5439 if (dict_tv != NULL
5440 && dict_tv->v_type == VAR_DICT
5441 && dict_tv->vval.v_dict != NULL
5442 && (tv->v_type == VAR_FUNC
5443 || (tv->v_type == VAR_PARTIAL
5444 && (tv->vval.v_partial->pt_auto
5445 || tv->vval.v_partial->pt_dict == NULL))))
5446 dict_tv->vval.v_dict =
5447 make_partial(dict_tv->vval.v_dict, tv);
5448 dict_stack_drop();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005449 }
5450 break;
5451
5452 case ISN_NEGATENR:
5453 tv = STACK_TV_BOT(-1);
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00005454 // CHECKTYPE should have checked the variable type
Bram Moolenaarc58164c2020-03-29 18:40:30 +02005455 if (tv->v_type == VAR_FLOAT)
5456 tv->vval.v_float = -tv->vval.v_float;
5457 else
Bram Moolenaarc58164c2020-03-29 18:40:30 +02005458 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005459 break;
5460
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005461 case ISN_CHECKTYPE:
5462 {
5463 checktype_T *ct = &iptr->isn_arg.type;
Bram Moolenaarb1040dc2022-05-18 11:00:48 +01005464 int r;
LemonBoyc5d27442023-08-19 13:02:35 +02005465 where_T where = WHERE_INIT;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005466
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01005467 tv = STACK_TV_BOT((int)ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02005468 SOURCING_LNUM = iptr->isn_lnum;
LemonBoyc5d27442023-08-19 13:02:35 +02005469 if (ct->ct_arg_idx > 0)
5470 {
5471 where.wt_index = ct->ct_arg_idx;
5472 where.wt_kind = ct->ct_is_var ? WT_VARIABLE : WT_ARGUMENT;
LemonBoyc5d27442023-08-19 13:02:35 +02005473 }
LemonBoyf244b2f2023-08-19 16:02:04 +02005474 where.wt_func_name = ectx->ec_where.wt_func_name;
LemonBoyc5d27442023-08-19 13:02:35 +02005475 r = check_typval_type(ct->ct_type, tv, where);
Bram Moolenaarb1040dc2022-05-18 11:00:48 +01005476 if (r == FAIL)
5477 goto on_error;
Bram Moolenaar5e654232020-09-16 15:22:00 +02005478
5479 // number 0 is FALSE, number 1 is TRUE
5480 if (tv->v_type == VAR_NUMBER
5481 && ct->ct_type->tt_type == VAR_BOOL
5482 && (tv->vval.v_number == 0
5483 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005484 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02005485 tv->v_type = VAR_BOOL;
5486 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02005487 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005488 }
5489 }
5490 break;
5491
Bram Moolenaar9af78762020-06-16 11:34:42 +02005492 case ISN_CHECKLEN:
5493 {
5494 int min_len = iptr->isn_arg.checklen.cl_min_len;
5495 list_T *list = NULL;
5496
5497 tv = STACK_TV_BOT(-1);
5498 if (tv->v_type == VAR_LIST)
5499 list = tv->vval.v_list;
5500 if (list == NULL || list->lv_len < min_len
5501 || (list->lv_len > min_len
5502 && !iptr->isn_arg.checklen.cl_more_OK))
5503 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005504 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005505 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02005506 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02005507 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02005508 }
5509 }
5510 break;
5511
Bram Moolenaaraa210a32021-01-02 15:41:03 +01005512 case ISN_SETTYPE:
Bram Moolenaar381692b2022-02-02 20:01:27 +00005513 set_tv_type(STACK_TV_BOT(-1), iptr->isn_arg.type.ct_type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01005514 break;
5515
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005516 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005517 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005518 {
5519 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005520 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005521
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005522 if (iptr->isn_type == ISN_2BOOL)
5523 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005524 tv = STACK_TV_BOT(iptr->isn_arg.tobool.offset);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005525 n = tv2bool(tv);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005526 if (iptr->isn_arg.tobool.invert)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005527 n = !n;
5528 }
5529 else
5530 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005531 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005532 SOURCING_LNUM = iptr->isn_lnum;
5533 n = tv_get_bool_chk(tv, &error);
5534 if (error)
5535 goto on_error;
5536 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005537 clear_tv(tv);
5538 tv->v_type = VAR_BOOL;
5539 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
5540 }
5541 break;
5542
5543 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02005544 case ISN_2STRING_ANY:
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01005545 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005546 if (do_2string(STACK_TV_BOT(iptr->isn_arg.tostring.offset),
5547 iptr->isn_type == ISN_2STRING_ANY,
5548 iptr->isn_arg.tostring.tolerant) == FAIL)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01005549 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005550 break;
5551
Bram Moolenaar08597872020-12-10 19:43:40 +01005552 case ISN_RANGE:
5553 {
5554 exarg_T ea;
5555 char *errormsg;
5556
Bram Moolenaarece0b872021-01-08 20:40:45 +01005557 ea.line2 = 0;
Bram Moolenaard1510ee2021-01-04 16:15:58 +01005558 ea.addr_count = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01005559 ea.addr_type = ADDR_LINES;
5560 ea.cmd = iptr->isn_arg.string;
Bram Moolenaarece0b872021-01-08 20:40:45 +01005561 ea.skip = FALSE;
Bram Moolenaar08597872020-12-10 19:43:40 +01005562 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaarece0b872021-01-08 20:40:45 +01005563 goto on_error;
Bram Moolenaara28639e2021-01-19 22:48:09 +01005564
Bram Moolenaar35578162021-08-02 19:10:38 +02005565 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005566 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005567 ++ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01005568 tv = STACK_TV_BOT(-1);
5569 tv->v_type = VAR_NUMBER;
5570 tv->v_lock = 0;
Bram Moolenaar06651632022-04-27 17:54:25 +01005571 tv->vval.v_number = ea.line2;
Bram Moolenaar08597872020-12-10 19:43:40 +01005572 }
5573 break;
5574
Bram Moolenaarc3516f72020-09-08 22:45:35 +02005575 case ISN_PUT:
5576 {
5577 int regname = iptr->isn_arg.put.put_regname;
5578 linenr_T lnum = iptr->isn_arg.put.put_lnum;
5579 char_u *expr = NULL;
5580 int dir = FORWARD;
5581
Bram Moolenaar08597872020-12-10 19:43:40 +01005582 if (lnum < -2)
5583 {
5584 // line number was put on the stack by ISN_RANGE
5585 tv = STACK_TV_BOT(-1);
5586 curwin->w_cursor.lnum = tv->vval.v_number;
5587 if (lnum == LNUM_VARIABLE_RANGE_ABOVE)
5588 dir = BACKWARD;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005589 --ectx->ec_stack.ga_len;
Bram Moolenaar08597872020-12-10 19:43:40 +01005590 }
5591 else if (lnum == -2)
Bram Moolenaarc3516f72020-09-08 22:45:35 +02005592 // :put! above cursor
5593 dir = BACKWARD;
5594 else if (lnum >= 0)
Bram Moolenaar4e713ba2022-02-07 15:31:37 +00005595 {
5596 curwin->w_cursor.lnum = lnum;
5597 if (lnum == 0)
5598 // check_cursor() below will move to line 1
5599 dir = BACKWARD;
5600 }
Bram Moolenaara28639e2021-01-19 22:48:09 +01005601
5602 if (regname == '=')
5603 {
5604 tv = STACK_TV_BOT(-1);
5605 if (tv->v_type == VAR_STRING)
5606 expr = tv->vval.v_string;
5607 else
5608 {
5609 expr = typval2string(tv, TRUE); // allocates value
5610 clear_tv(tv);
5611 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005612 --ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01005613 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02005614 check_cursor();
5615 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
5616 vim_free(expr);
5617 }
5618 break;
5619
Bram Moolenaar02194d22020-10-24 23:08:38 +02005620 case ISN_CMDMOD:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005621 ectx->ec_funclocal.floc_save_cmdmod = cmdmod;
5622 ectx->ec_funclocal.floc_restore_cmdmod = TRUE;
5623 ectx->ec_funclocal.floc_restore_cmdmod_stacklen =
5624 ectx->ec_stack.ga_len;
Bram Moolenaar02194d22020-10-24 23:08:38 +02005625 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
5626 apply_cmdmod(&cmdmod);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02005627 break;
5628
Bram Moolenaar02194d22020-10-24 23:08:38 +02005629 case ISN_CMDMOD_REV:
5630 // filter regprog is owned by the instruction, don't free it
5631 cmdmod.cmod_filter_regmatch.regprog = NULL;
5632 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005633 cmdmod = ectx->ec_funclocal.floc_save_cmdmod;
5634 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02005635 break;
5636
Bram Moolenaar792f7862020-11-23 08:31:18 +01005637 case ISN_UNPACK:
5638 {
5639 int count = iptr->isn_arg.unpack.unp_count;
5640 int semicolon = iptr->isn_arg.unpack.unp_semicolon;
5641 list_T *l;
5642 listitem_T *li;
5643 int i;
5644
5645 // Check there is a valid list to unpack.
5646 tv = STACK_TV_BOT(-1);
5647 if (tv->v_type != VAR_LIST)
5648 {
5649 SOURCING_LNUM = iptr->isn_lnum;
5650 emsg(_(e_for_argument_must_be_sequence_of_lists));
5651 goto on_error;
5652 }
5653 l = tv->vval.v_list;
5654 if (l == NULL
5655 || l->lv_len < (semicolon ? count - 1 : count))
5656 {
5657 SOURCING_LNUM = iptr->isn_lnum;
5658 emsg(_(e_list_value_does_not_have_enough_items));
5659 goto on_error;
5660 }
5661 else if (!semicolon && l->lv_len > count)
5662 {
5663 SOURCING_LNUM = iptr->isn_lnum;
5664 emsg(_(e_list_value_has_more_items_than_targets));
5665 goto on_error;
5666 }
5667
5668 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar35578162021-08-02 19:10:38 +02005669 if (GA_GROW_FAILS(&ectx->ec_stack, count - 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005670 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005671 ectx->ec_stack.ga_len += count - 1;
Bram Moolenaar792f7862020-11-23 08:31:18 +01005672
5673 // Variable after semicolon gets a list with the remaining
5674 // items.
5675 if (semicolon)
5676 {
5677 list_T *rem_list =
5678 list_alloc_with_items(l->lv_len - count + 1);
5679
5680 if (rem_list == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005681 goto theend;
Bram Moolenaar792f7862020-11-23 08:31:18 +01005682 tv = STACK_TV_BOT(-count);
5683 tv->vval.v_list = rem_list;
5684 ++rem_list->lv_refcount;
5685 tv->v_lock = 0;
5686 li = l->lv_first;
5687 for (i = 0; i < count - 1; ++i)
5688 li = li->li_next;
5689 for (i = 0; li != NULL; ++i)
5690 {
Bram Moolenaar61efa162022-03-18 13:10:48 +00005691 typval_T tvcopy;
5692
5693 copy_tv(&li->li_tv, &tvcopy);
5694 list_set_item(rem_list, i, &tvcopy);
Bram Moolenaar792f7862020-11-23 08:31:18 +01005695 li = li->li_next;
5696 }
5697 --count;
5698 }
5699
5700 // Produce the values in reverse order, first item last.
5701 li = l->lv_first;
5702 for (i = 0; i < count; ++i)
5703 {
5704 tv = STACK_TV_BOT(-i - 1);
5705 copy_tv(&li->li_tv, tv);
5706 li = li->li_next;
5707 }
5708
5709 list_unref(l);
5710 }
5711 break;
5712
Bram Moolenaarb2049902021-01-24 12:53:53 +01005713 case ISN_PROF_START:
5714 case ISN_PROF_END:
5715 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01005716#ifdef FEAT_PROFILE
Bram Moolenaarca16c602022-09-06 18:57:08 +01005717 funccall_T cookie;
5718 ufunc_T *cur_ufunc =
Bram Moolenaarb2049902021-01-24 12:53:53 +01005719 (((dfunc_T *)def_functions.ga_data)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02005720 + ectx->ec_dfunc_idx)->df_ufunc;
Bram Moolenaarb2049902021-01-24 12:53:53 +01005721
Bram Moolenaarca16c602022-09-06 18:57:08 +01005722 cookie.fc_func = cur_ufunc;
Bram Moolenaarb2049902021-01-24 12:53:53 +01005723 if (iptr->isn_type == ISN_PROF_START)
5724 {
5725 func_line_start(&cookie, iptr->isn_lnum);
5726 // if we get here the instruction is executed
5727 func_line_exec(&cookie);
5728 }
5729 else
5730 func_line_end(&cookie);
Bram Moolenaarf002a412021-01-24 13:34:18 +01005731#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01005732 }
5733 break;
5734
Bram Moolenaare99d4222021-06-13 14:01:26 +02005735 case ISN_DEBUG:
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02005736 handle_debug(iptr, ectx);
Bram Moolenaare99d4222021-06-13 14:01:26 +02005737 break;
5738
Bram Moolenaar389df252020-07-09 21:20:47 +02005739 case ISN_SHUFFLE:
5740 {
Bram Moolenaar792f7862020-11-23 08:31:18 +01005741 typval_T tmp_tv;
5742 int item = iptr->isn_arg.shuffle.shfl_item;
5743 int up = iptr->isn_arg.shuffle.shfl_up;
Bram Moolenaar389df252020-07-09 21:20:47 +02005744
5745 tmp_tv = *STACK_TV_BOT(-item);
5746 for ( ; up > 0 && item > 1; --up)
5747 {
5748 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
5749 --item;
5750 }
5751 *STACK_TV_BOT(-item) = tmp_tv;
5752 }
5753 break;
5754
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005755 case ISN_DROP:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005756 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005757 clear_tv(STACK_TV_BOT(0));
LemonBoyc5d27442023-08-19 13:02:35 +02005758 ectx->ec_where = (where_T)WHERE_INIT;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005759 break;
5760 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02005761 continue;
5762
Bram Moolenaard032f342020-07-18 18:13:02 +02005763func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02005764 // Restore previous function. If the frame pointer is where we started
5765 // then there is none and we are done.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005766 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02005767 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02005768
Bram Moolenaar4c137212021-04-19 16:48:48 +02005769 if (func_return(ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02005770 // only fails when out of memory
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005771 goto theend;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02005772 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02005773
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02005774on_error:
Bram Moolenaaraf0df472020-12-02 20:51:22 +01005775 // Jump here for an error that does not require aborting execution.
Bram Moolenaar56602ba2020-12-05 21:22:08 +01005776 // If "emsg_silent" is set then ignore the error, unless it was set
5777 // when calling the function.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005778 if (did_emsg_cumul + did_emsg == ectx->ec_did_emsg_before
Bram Moolenaar56602ba2020-12-05 21:22:08 +01005779 && emsg_silent && did_emsg_def == 0)
Bram Moolenaarf9041332021-01-21 19:41:16 +01005780 {
5781 // If a sequence of instructions causes an error while ":silent!"
5782 // was used, restore the stack length and jump ahead to restoring
5783 // the cmdmod.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005784 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaarf9041332021-01-21 19:41:16 +01005785 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005786 while (ectx->ec_stack.ga_len
5787 > ectx->ec_funclocal.floc_restore_cmdmod_stacklen)
Bram Moolenaarf9041332021-01-21 19:41:16 +01005788 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005789 --ectx->ec_stack.ga_len;
Bram Moolenaarf9041332021-01-21 19:41:16 +01005790 clear_tv(STACK_TV_BOT(0));
5791 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005792 while (ectx->ec_instr[ectx->ec_iidx].isn_type != ISN_CMDMOD_REV)
5793 ++ectx->ec_iidx;
Bram Moolenaarf9041332021-01-21 19:41:16 +01005794 }
Bram Moolenaarcd030c42020-10-30 21:49:40 +01005795 continue;
Bram Moolenaarf9041332021-01-21 19:41:16 +01005796 }
Bram Moolenaaraf0df472020-12-02 20:51:22 +01005797on_fatal_error:
5798 // Jump here for an error that messes up the stack.
Bram Moolenaar171fb922020-10-28 16:54:47 +01005799 // If we are not inside a try-catch started here, abort execution.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005800 if (trylevel <= ectx->ec_trylevel_at_start)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005801 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005802 }
5803
5804done:
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005805 ret = OK;
5806theend:
Bram Moolenaar58779852022-09-06 18:31:14 +01005807 may_invoke_defer_funcs(ectx);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01005808
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005809 dict_stack_clear(dict_stack_len_at_start);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005810 ectx->ec_trylevel_at_start = save_trylevel_at_start;
5811 return ret;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005812}
5813
5814/*
Bram Moolenaar806a2732022-09-04 15:40:36 +01005815 * Execute the instructions from a VAR_INSTR typval and put the result in
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005816 * "rettv".
5817 * Return OK or FAIL.
5818 */
5819 int
5820exe_typval_instr(typval_T *tv, typval_T *rettv)
5821{
5822 ectx_T *ectx = tv->vval.v_instr->instr_ectx;
5823 isn_T *save_instr = ectx->ec_instr;
5824 int save_iidx = ectx->ec_iidx;
5825 int res;
5826
LemonBoyf3b48952022-05-05 13:53:03 +01005827 // Initialize rettv so that it is safe for caller to invoke clear_tv(rettv)
5828 // even when the compilation fails.
5829 rettv->v_type = VAR_UNKNOWN;
5830
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005831 ectx->ec_instr = tv->vval.v_instr->instr_instr;
5832 res = exec_instructions(ectx);
5833 if (res == OK)
5834 {
5835 *rettv = *STACK_TV_BOT(-1);
5836 --ectx->ec_stack.ga_len;
5837 }
5838
5839 ectx->ec_instr = save_instr;
5840 ectx->ec_iidx = save_iidx;
5841
5842 return res;
5843}
5844
5845/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02005846 * Execute the instructions from an ISN_SUBSTITUTE command, which are in
5847 * "substitute_instr".
5848 */
5849 char_u *
5850exe_substitute_instr(void)
5851{
5852 ectx_T *ectx = substitute_instr->subs_ectx;
5853 isn_T *save_instr = ectx->ec_instr;
5854 int save_iidx = ectx->ec_iidx;
5855 char_u *res;
5856
5857 ectx->ec_instr = substitute_instr->subs_instr;
5858 if (exec_instructions(ectx) == OK)
Bram Moolenaar90193e62021-04-04 20:49:50 +02005859 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005860 typval_T *tv = STACK_TV_BOT(-1);
5861
Bram Moolenaar27523602021-06-05 21:36:19 +02005862 res = typval2string(tv, TRUE);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005863 --ectx->ec_stack.ga_len;
5864 clear_tv(tv);
Bram Moolenaar90193e62021-04-04 20:49:50 +02005865 }
5866 else
5867 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005868 substitute_instr->subs_status = FAIL;
5869 res = vim_strsave((char_u *)"");
Bram Moolenaar90193e62021-04-04 20:49:50 +02005870 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005871
Bram Moolenaar4c137212021-04-19 16:48:48 +02005872 ectx->ec_instr = save_instr;
5873 ectx->ec_iidx = save_iidx;
5874
5875 return res;
5876}
5877
5878/*
5879 * Call a "def" function from old Vim script.
5880 * Return OK or FAIL.
5881 */
5882 int
5883call_def_function(
5884 ufunc_T *ufunc,
5885 int argc_arg, // nr of arguments
5886 typval_T *argv, // arguments
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005887 int flags, // DEF_ flags
Bram Moolenaar4c137212021-04-19 16:48:48 +02005888 partial_T *partial, // optional partial for context
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005889 object_T *object, // object, e.g. for this.Func()
Bram Moolenaar58779852022-09-06 18:31:14 +01005890 funccall_T *funccal,
Bram Moolenaar4c137212021-04-19 16:48:48 +02005891 typval_T *rettv) // return value
5892{
5893 ectx_T ectx; // execution context
5894 int argc = argc_arg;
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005895 int partial_argc = partial == NULL
5896 || (flags & DEF_USE_PT_ARGV) == 0
5897 ? 0 : partial->pt_argc;
5898 int total_argc = argc + partial_argc;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005899 typval_T *tv;
5900 int idx;
5901 int ret = FAIL;
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005902 int defcount = ufunc->uf_args.ga_len - total_argc;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005903 sctx_T save_current_sctx = current_sctx;
5904 int did_emsg_before = did_emsg_cumul + did_emsg;
5905 int save_suppress_errthrow = suppress_errthrow;
5906 msglist_T **saved_msg_list = NULL;
5907 msglist_T *private_msg_list = NULL;
5908 int save_emsg_silent_def = emsg_silent_def;
5909 int save_did_emsg_def = did_emsg_def;
5910 int orig_funcdepth;
Bram Moolenaare99d4222021-06-13 14:01:26 +02005911 int orig_nesting_level = ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005912
5913// Get pointer to item in the stack.
5914#undef STACK_TV
5915#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
5916
5917// Get pointer to item at the bottom of the stack, -1 is the bottom.
5918#undef STACK_TV_BOT
5919#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
5920
5921// Get pointer to a local variable on the stack. Negative for arguments.
5922#undef STACK_TV_VAR
5923#define STACK_TV_VAR(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_frame_idx + STACK_FRAME_SIZE + idx)
5924
5925 if (ufunc->uf_def_status == UF_NOT_COMPILED
5926 || ufunc->uf_def_status == UF_COMPILE_ERROR
Bram Moolenaar139575d2022-03-15 19:29:30 +00005927 || (func_needs_compiling(ufunc, get_compile_type(ufunc))
5928 && compile_def_function(ufunc, FALSE,
5929 get_compile_type(ufunc), NULL) == FAIL))
Bram Moolenaar4c137212021-04-19 16:48:48 +02005930 {
5931 if (did_emsg_cumul + did_emsg == did_emsg_before)
5932 semsg(_(e_function_is_not_compiled_str),
5933 printable_func_name(ufunc));
5934 return FAIL;
5935 }
5936
5937 {
5938 // Check the function was really compiled.
5939 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5940 + ufunc->uf_dfunc_idx;
Bram Moolenaarcc341812022-09-19 15:54:34 +01005941 if (dfunc->df_ufunc == NULL)
5942 {
5943 semsg(_(e_function_was_deleted_str), printable_func_name(ufunc));
5944 return FAIL;
5945 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005946 if (INSTRUCTIONS(dfunc) == NULL)
5947 {
5948 iemsg("using call_def_function() on not compiled function");
5949 return FAIL;
5950 }
5951 }
5952
5953 // If depth of calling is getting too high, don't execute the function.
5954 orig_funcdepth = funcdepth_get();
5955 if (funcdepth_increment() == FAIL)
5956 return FAIL;
5957
5958 CLEAR_FIELD(ectx);
5959 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
5960 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
Bram Moolenaar35578162021-08-02 19:10:38 +02005961 if (GA_GROW_FAILS(&ectx.ec_stack, 20))
Bram Moolenaar4c137212021-04-19 16:48:48 +02005962 {
5963 funcdepth_decrement();
5964 return FAIL;
5965 }
5966 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
5967 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
5968 ectx.ec_did_emsg_before = did_emsg_before;
Bram Moolenaare99d4222021-06-13 14:01:26 +02005969 ++ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005970
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005971 idx = total_argc - ufunc->uf_args.ga_len;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005972 if (idx > 0 && ufunc->uf_va_name == NULL)
5973 {
Matvey Tarasovd14bb1a2022-06-29 13:18:27 +01005974 semsg(NGETTEXT(e_one_argument_too_many, e_nr_arguments_too_many,
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005975 idx), idx);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005976 goto failed_early;
5977 }
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005978 idx = total_argc - ufunc->uf_args.ga_len + ufunc->uf_def_args.ga_len;
Bram Moolenaarc6d71532021-06-05 18:49:38 +02005979 if (idx < 0)
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02005980 {
Matvey Tarasovd14bb1a2022-06-29 13:18:27 +01005981 semsg(NGETTEXT(e_one_argument_too_few, e_nr_arguments_too_few,
Bram Moolenaar98aff652022-09-06 21:02:35 +01005982 -idx), -idx);
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02005983 goto failed_early;
5984 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005985
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005986 // Put values from the partial and arguments on the stack, but no more than
5987 // what the function expects. A lambda can be called with more arguments
5988 // than it uses.
5989 for (idx = 0; idx < total_argc
Bram Moolenaar4c137212021-04-19 16:48:48 +02005990 && (ufunc->uf_va_name != NULL || idx < ufunc->uf_args.ga_len);
5991 ++idx)
5992 {
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005993 int argv_idx = idx - partial_argc;
5994
5995 tv = idx < partial_argc ? partial->pt_argv + idx : argv + argv_idx;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005996 if (idx >= ufunc->uf_args.ga_len - ufunc->uf_def_args.ga_len
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005997 && tv->v_type == VAR_SPECIAL
5998 && tv->vval.v_number == VVAL_NONE)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005999 {
6000 // Use the default value.
6001 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
6002 }
6003 else
6004 {
Bram Moolenaar47bba532023-01-20 18:49:46 +00006005 int done = FALSE;
6006 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len)
6007 {
6008 type_T *expected = ufunc->uf_arg_types[idx];
6009 if (expected->tt_type == VAR_FLOAT && tv->v_type == VAR_NUMBER)
6010 {
6011 // When a float is expected and a number was given, convert
6012 // the value.
6013 STACK_TV_BOT(0)->v_type = VAR_FLOAT;
6014 STACK_TV_BOT(0)->v_lock = 0;
6015 STACK_TV_BOT(0)->vval.v_float = tv->vval.v_number;
6016 done = TRUE;
6017 }
6018 else if (check_typval_arg_type(expected, tv,
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01006019 NULL, argv_idx + 1) == FAIL)
Bram Moolenaar47bba532023-01-20 18:49:46 +00006020 goto failed_early;
6021 }
6022 if (!done)
6023 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006024 }
6025 ++ectx.ec_stack.ga_len;
6026 }
6027
6028 // Turn varargs into a list. Empty list if no args.
6029 if (ufunc->uf_va_name != NULL)
6030 {
6031 int vararg_count = argc - ufunc->uf_args.ga_len;
6032
6033 if (vararg_count < 0)
6034 vararg_count = 0;
6035 else
6036 argc -= vararg_count;
6037 if (exe_newlist(vararg_count, &ectx) == FAIL)
6038 goto failed_early;
6039
6040 // Check the type of the list items.
6041 tv = STACK_TV_BOT(-1);
6042 if (ufunc->uf_va_type != NULL
6043 && ufunc->uf_va_type != &t_list_any
6044 && ufunc->uf_va_type->tt_member != &t_any
6045 && tv->vval.v_list != NULL)
6046 {
6047 type_T *expected = ufunc->uf_va_type->tt_member;
6048 listitem_T *li = tv->vval.v_list->lv_first;
6049
6050 for (idx = 0; idx < vararg_count; ++idx)
6051 {
6052 if (check_typval_arg_type(expected, &li->li_tv,
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02006053 NULL, argc + idx + 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02006054 goto failed_early;
6055 li = li->li_next;
6056 }
6057 }
6058
6059 if (defcount > 0)
6060 // Move varargs list to below missing default arguments.
6061 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
6062 --ectx.ec_stack.ga_len;
6063 }
6064
6065 // Make space for omitted arguments, will store default value below.
6066 // Any varargs list goes after them.
6067 if (defcount > 0)
6068 for (idx = 0; idx < defcount; ++idx)
6069 {
6070 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
6071 ++ectx.ec_stack.ga_len;
6072 }
6073 if (ufunc->uf_va_name != NULL)
6074 ++ectx.ec_stack.ga_len;
6075
6076 // Frame pointer points to just after arguments.
6077 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
6078 ectx.ec_initial_frame_idx = ectx.ec_frame_idx;
6079
6080 {
6081 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6082 + ufunc->uf_dfunc_idx;
6083 ufunc_T *base_ufunc = dfunc->df_ufunc;
6084
6085 // "uf_partial" is on the ufunc that "df_ufunc" points to, as is done
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006086 // by copy_lambda_to_global_func().
Bram Moolenaar4c137212021-04-19 16:48:48 +02006087 if (partial != NULL || base_ufunc->uf_partial != NULL)
6088 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006089 ectx.ec_outer_ref = ALLOC_CLEAR_ONE(outer_ref_T);
6090 if (ectx.ec_outer_ref == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02006091 goto failed_early;
6092 if (partial != NULL)
6093 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00006094 outer_T *outer = get_pt_outer(partial);
6095
Bram Moolenaar6d313be2022-09-22 16:36:25 +01006096 if (outer->out_stack == NULL && outer->out_loop_size == 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02006097 {
Bram Moolenaar6d313be2022-09-22 16:36:25 +01006098 // no stack was set
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00006099 if (current_ectx != NULL)
6100 {
6101 if (current_ectx->ec_outer_ref != NULL
6102 && current_ectx->ec_outer_ref->or_outer != NULL)
6103 ectx.ec_outer_ref->or_outer =
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006104 current_ectx->ec_outer_ref->or_outer;
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00006105 }
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01006106 // else: should there be an error here?
Bram Moolenaar4c137212021-04-19 16:48:48 +02006107 }
6108 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006109 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00006110 ectx.ec_outer_ref->or_outer = outer;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006111 ++partial->pt_refcount;
6112 ectx.ec_outer_ref->or_partial = partial;
6113 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006114 }
6115 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006116 {
6117 ectx.ec_outer_ref->or_outer = &base_ufunc->uf_partial->pt_outer;
6118 ++base_ufunc->uf_partial->pt_refcount;
6119 ectx.ec_outer_ref->or_partial = base_ufunc->uf_partial;
6120 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006121 }
6122 }
6123
6124 // dummy frame entries
6125 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
6126 {
6127 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
6128 ++ectx.ec_stack.ga_len;
6129 }
6130
6131 {
6132 // Reserve space for local variables and any closure reference count.
6133 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6134 + ufunc->uf_dfunc_idx;
6135
Bram Moolenaar5cd64792021-12-25 18:23:24 +00006136 // Initialize variables to zero. That avoids having to generate
6137 // initializing instructions for "var nr: number", "var x: any", etc.
Bram Moolenaar4c137212021-04-19 16:48:48 +02006138 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaar5cd64792021-12-25 18:23:24 +00006139 {
6140 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
6141 STACK_TV_VAR(idx)->vval.v_number = 0;
6142 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006143 ectx.ec_stack.ga_len += dfunc->df_varcount;
Bram Moolenaarffdaca92022-12-09 21:41:48 +00006144
6145 if (object != NULL)
6146 {
6147 // the object is always the variable at index zero
6148 tv = STACK_TV_VAR(0);
6149 tv->v_type = VAR_OBJECT;
6150 tv->vval.v_object = object;
6151 }
6152
Bram Moolenaar4c137212021-04-19 16:48:48 +02006153 if (dfunc->df_has_closure)
6154 {
Bram Moolenaarcc341812022-09-19 15:54:34 +01006155 // Initialize the variable that counts how many closures were
6156 // created. This is used in handle_closure_in_use().
Bram Moolenaar4c137212021-04-19 16:48:48 +02006157 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
6158 STACK_TV_VAR(idx)->vval.v_number = 0;
6159 ++ectx.ec_stack.ga_len;
6160 }
6161
6162 ectx.ec_instr = INSTRUCTIONS(dfunc);
6163 }
6164
Bram Moolenaar58779852022-09-06 18:31:14 +01006165 // Store the execution context in funccal, used by invoke_all_defer().
6166 if (funccal != NULL)
6167 funccal->fc_ectx = &ectx;
6168
Bram Moolenaar4c137212021-04-19 16:48:48 +02006169 // Following errors are in the function, not the caller.
6170 // Commands behave like vim9script.
6171 estack_push_ufunc(ufunc, 1);
6172 current_sctx = ufunc->uf_script_ctx;
6173 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
6174
6175 // Use a specific location for storing error messages to be converted to an
6176 // exception.
6177 saved_msg_list = msg_list;
6178 msg_list = &private_msg_list;
6179
6180 // Do turn errors into exceptions.
6181 suppress_errthrow = FALSE;
6182
6183 // Do not delete the function while executing it.
6184 ++ufunc->uf_calls;
6185
6186 // When ":silent!" was used before calling then we still abort the
6187 // function. If ":silent!" is used in the function then we don't.
6188 emsg_silent_def = emsg_silent;
6189 did_emsg_def = 0;
6190
LemonBoyc5d27442023-08-19 13:02:35 +02006191 ectx.ec_where = (where_T)WHERE_INIT;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006192
Bram Moolenaar5800c792022-09-22 17:34:01 +01006193 /*
6194 * Execute the instructions until done.
6195 */
Bram Moolenaar4c137212021-04-19 16:48:48 +02006196 ret = exec_instructions(&ectx);
6197 if (ret == OK)
6198 {
6199 // function finished, get result from the stack.
6200 if (ufunc->uf_ret_type == &t_void)
6201 {
6202 rettv->v_type = VAR_VOID;
6203 }
6204 else
6205 {
6206 tv = STACK_TV_BOT(-1);
6207 *rettv = *tv;
6208 tv->v_type = VAR_UNKNOWN;
6209 }
6210 }
6211
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01006212 // When failed need to unwind the call stack.
Bram Moolenaar58779852022-09-06 18:31:14 +01006213 unwind_def_callstack(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02006214
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02006215 // Deal with any remaining closures, they may be in use somewhere.
6216 if (ectx.ec_funcrefs.ga_len > 0)
Bram Moolenaarf112f302020-12-20 17:47:52 +01006217 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02006218 handle_closure_in_use(&ectx, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00006219 ga_clear(&ectx.ec_funcrefs);
Bram Moolenaarf112f302020-12-20 17:47:52 +01006220 }
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02006221
Bram Moolenaaree8580e2020-08-28 17:19:07 +02006222 estack_pop();
6223 current_sctx = save_current_sctx;
6224
Bram Moolenaar2336c372021-12-06 15:06:54 +00006225 if (--ufunc->uf_calls <= 0 && ufunc->uf_refcount <= 0)
6226 // Function was unreferenced while being used, free it now.
6227 func_clear_free(ufunc, FALSE);
Bram Moolenaarc970e422021-03-17 15:03:04 +01006228
Bram Moolenaar352134b2020-10-17 22:04:08 +02006229 if (*msg_list != NULL && saved_msg_list != NULL)
6230 {
6231 msglist_T **plist = saved_msg_list;
6232
6233 // Append entries from the current msg_list (uncaught exceptions) to
6234 // the saved msg_list.
6235 while (*plist != NULL)
6236 plist = &(*plist)->next;
6237
6238 *plist = *msg_list;
6239 }
6240 msg_list = saved_msg_list;
6241
Bram Moolenaar4c137212021-04-19 16:48:48 +02006242 if (ectx.ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar02194d22020-10-24 23:08:38 +02006243 {
6244 cmdmod.cmod_filter_regmatch.regprog = NULL;
6245 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006246 cmdmod = ectx.ec_funclocal.floc_save_cmdmod;
Bram Moolenaar02194d22020-10-24 23:08:38 +02006247 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +01006248 emsg_silent_def = save_emsg_silent_def;
6249 did_emsg_def += save_did_emsg_def;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02006250
Bram Moolenaaree8580e2020-08-28 17:19:07 +02006251failed_early:
Bram Moolenaar0d1f55c2022-04-05 17:30:29 +01006252 // Free all arguments and local variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006253 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
Bram Moolenaar1936c762022-09-28 15:19:10 +01006254 {
6255 tv = STACK_TV(idx);
6256 if (tv->v_type != VAR_NUMBER && tv->v_type != VAR_UNKNOWN)
6257 clear_tv(tv);
6258 }
Bram Moolenaare99d4222021-06-13 14:01:26 +02006259 ex_nesting_level = orig_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02006260
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006261 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01006262 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006263 if (ectx.ec_outer_ref != NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +01006264 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006265 if (ectx.ec_outer_ref->or_outer_allocated)
6266 vim_free(ectx.ec_outer_ref->or_outer);
6267 partial_unref(ectx.ec_outer_ref->or_partial);
6268 vim_free(ectx.ec_outer_ref);
Bram Moolenaar0186e582021-01-10 18:33:11 +01006269 }
6270
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02006271 // Not sure if this is necessary.
6272 suppress_errthrow = save_suppress_errthrow;
6273
Bram Moolenaarb5841b92021-07-15 18:09:53 +02006274 if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before
6275 && !need_rethrow)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006276 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02006277 printable_func_name(ufunc));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01006278 funcdepth_restore(orig_funcdepth);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006279 return ret;
6280}
6281
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006282/*
Bram Moolenaar58779852022-09-06 18:31:14 +01006283 * Called when a def function has finished (possibly failed).
6284 * Invoke all the function returns to clean up and invoke deferred functions,
6285 * except the toplevel one.
6286 */
6287 void
6288unwind_def_callstack(ectx_T *ectx)
6289{
6290 while (ectx->ec_frame_idx != ectx->ec_initial_frame_idx)
6291 func_return(ectx);
6292}
6293
6294/*
dundargocc57b5bc2022-11-02 13:30:51 +00006295 * Invoke any deferred functions for the top function in "ectx".
Bram Moolenaar58779852022-09-06 18:31:14 +01006296 */
6297 void
6298may_invoke_defer_funcs(ectx_T *ectx)
6299{
6300 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
6301
6302 if (dfunc->df_defer_var_idx > 0)
6303 invoke_defer_funcs(ectx);
6304}
6305
6306/*
Bram Moolenaarcc341812022-09-19 15:54:34 +01006307 * Return loopvarinfo in a printable form in allocated memory.
6308 */
6309 static char_u *
6310printable_loopvarinfo(loopvarinfo_T *lvi)
6311{
6312 garray_T ga;
6313 int depth;
6314
6315 ga_init2(&ga, 1, 100);
6316 for (depth = 0; depth < lvi->lvi_depth; ++depth)
6317 {
6318 if (ga_grow(&ga, 50) == FAIL)
6319 break;
6320 if (lvi->lvi_loop[depth].var_idx == 0)
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +01006321 STRCPY((char *)ga.ga_data + ga.ga_len, " -");
Bram Moolenaarcc341812022-09-19 15:54:34 +01006322 else
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +01006323 vim_snprintf((char *)ga.ga_data + ga.ga_len, 50, " $%d-$%d",
Bram Moolenaarcc341812022-09-19 15:54:34 +01006324 lvi->lvi_loop[depth].var_idx,
6325 lvi->lvi_loop[depth].var_idx
6326 + lvi->lvi_loop[depth].var_count - 1);
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +01006327 ga.ga_len = (int)STRLEN(ga.ga_data);
Bram Moolenaarcc341812022-09-19 15:54:34 +01006328 }
6329 return ga.ga_data;
6330}
6331
6332/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02006333 * List instructions "instr" up to "instr_count" or until ISN_FINISH.
6334 * "ufunc" has the source lines, NULL for the instructions of ISN_SUBSTITUTE.
6335 * "pfx" is prefixed to every line.
6336 */
6337 static void
6338list_instructions(char *pfx, isn_T *instr, int instr_count, ufunc_T *ufunc)
6339{
6340 int line_idx = 0;
6341 int prev_current = 0;
6342 int current;
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02006343 int def_arg_idx = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006344
6345 for (current = 0; current < instr_count; ++current)
6346 {
6347 isn_T *iptr = &instr[current];
6348 char *line;
6349
6350 if (ufunc != NULL)
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02006351 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02006352 while (line_idx < iptr->isn_lnum
6353 && line_idx < ufunc->uf_lines.ga_len)
6354 {
6355 if (current > prev_current)
6356 {
6357 msg_puts("\n\n");
6358 prev_current = current;
6359 }
6360 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
6361 if (line != NULL)
6362 msg(line);
6363 }
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02006364 if (iptr->isn_type == ISN_JUMP_IF_ARG_SET)
6365 {
6366 int first_def_arg = ufunc->uf_args.ga_len
6367 - ufunc->uf_def_args.ga_len;
6368
6369 if (def_arg_idx > 0)
6370 msg_puts("\n\n");
6371 msg_start();
6372 msg_puts(" ");
6373 msg_puts(((char **)(ufunc->uf_args.ga_data))[
6374 first_def_arg + def_arg_idx]);
6375 msg_puts(" = ");
6376 msg_puts(((char **)(ufunc->uf_def_args.ga_data))[def_arg_idx++]);
6377 msg_clr_eos();
6378 msg_end();
6379 }
6380 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006381
6382 switch (iptr->isn_type)
6383 {
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006384 case ISN_CONSTRUCT:
6385 smsg("%s%4d NEW %s size %d", pfx, current,
6386 iptr->isn_arg.construct.construct_class->class_name,
6387 (int)iptr->isn_arg.construct.construct_size);
6388 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006389 case ISN_EXEC:
6390 smsg("%s%4d EXEC %s", pfx, current, iptr->isn_arg.string);
6391 break;
Bram Moolenaar20677332021-06-06 17:02:53 +02006392 case ISN_EXEC_SPLIT:
6393 smsg("%s%4d EXEC_SPLIT %s", pfx, current, iptr->isn_arg.string);
6394 break;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00006395 case ISN_EXECRANGE:
6396 smsg("%s%4d EXECRANGE %s", pfx, current, iptr->isn_arg.string);
6397 break;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02006398 case ISN_LEGACY_EVAL:
6399 smsg("%s%4d EVAL legacy %s", pfx, current,
6400 iptr->isn_arg.string);
6401 break;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02006402 case ISN_REDIRSTART:
6403 smsg("%s%4d REDIR", pfx, current);
6404 break;
6405 case ISN_REDIREND:
6406 smsg("%s%4d REDIR END%s", pfx, current,
6407 iptr->isn_arg.number ? " append" : "");
6408 break;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006409 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02006410#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006411 smsg("%s%4d CEXPR pre %s", pfx, current,
6412 cexpr_get_auname(iptr->isn_arg.number));
Bram Moolenaarb7c97812021-05-05 22:51:39 +02006413#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006414 break;
6415 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02006416#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006417 {
6418 cexprref_T *cer = iptr->isn_arg.cexpr.cexpr_ref;
6419
6420 smsg("%s%4d CEXPR core %s%s \"%s\"", pfx, current,
6421 cexpr_get_auname(cer->cer_cmdidx),
6422 cer->cer_forceit ? "!" : "",
6423 cer->cer_cmdline);
6424 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02006425#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006426 break;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006427 case ISN_INSTR:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006428 smsg("%s%4d INSTR", pfx, current);
6429 list_instructions(" ", iptr->isn_arg.instr, INT_MAX, NULL);
6430 msg(" -------------");
6431 break;
6432 case ISN_SOURCE:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006433 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006434 scriptitem_T *si = SCRIPT_ITEM(iptr->isn_arg.number);
6435
6436 smsg("%s%4d SOURCE %s", pfx, current, si->sn_name);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006437 }
6438 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006439 case ISN_SUBSTITUTE:
6440 {
6441 subs_T *subs = &iptr->isn_arg.subs;
6442
6443 smsg("%s%4d SUBSTITUTE %s", pfx, current, subs->subs_cmd);
6444 list_instructions(" ", subs->subs_instr, INT_MAX, NULL);
6445 msg(" -------------");
6446 }
6447 break;
6448 case ISN_EXECCONCAT:
6449 smsg("%s%4d EXECCONCAT %lld", pfx, current,
6450 (varnumber_T)iptr->isn_arg.number);
6451 break;
6452 case ISN_ECHO:
6453 {
6454 echo_T *echo = &iptr->isn_arg.echo;
6455
6456 smsg("%s%4d %s %d", pfx, current,
6457 echo->echo_with_white ? "ECHO" : "ECHON",
6458 echo->echo_count);
6459 }
6460 break;
6461 case ISN_EXECUTE:
6462 smsg("%s%4d EXECUTE %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02006463 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006464 break;
6465 case ISN_ECHOMSG:
6466 smsg("%s%4d ECHOMSG %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02006467 (varnumber_T)(iptr->isn_arg.number));
6468 break;
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01006469 case ISN_ECHOWINDOW:
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01006470 if (iptr->isn_arg.echowin.ewin_time > 0)
6471 smsg("%s%4d ECHOWINDOW %d (%ld sec)", pfx, current,
6472 iptr->isn_arg.echowin.ewin_count,
6473 iptr->isn_arg.echowin.ewin_time);
6474 else
6475 smsg("%s%4d ECHOWINDOW %d", pfx, current,
6476 iptr->isn_arg.echowin.ewin_count);
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01006477 break;
Bram Moolenaar7de62622021-08-07 15:05:47 +02006478 case ISN_ECHOCONSOLE:
6479 smsg("%s%4d ECHOCONSOLE %lld", pfx, current,
6480 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006481 break;
6482 case ISN_ECHOERR:
6483 smsg("%s%4d ECHOERR %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02006484 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006485 break;
6486 case ISN_LOAD:
6487 {
6488 if (iptr->isn_arg.number < 0)
6489 smsg("%s%4d LOAD arg[%lld]", pfx, current,
6490 (varnumber_T)(iptr->isn_arg.number
6491 + STACK_FRAME_SIZE));
6492 else
6493 smsg("%s%4d LOAD $%lld", pfx, current,
6494 (varnumber_T)(iptr->isn_arg.number));
6495 }
6496 break;
6497 case ISN_LOADOUTER:
6498 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006499 isn_outer_T *outer = &iptr->isn_arg.outer;
6500
6501 if (outer->outer_idx < 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02006502 smsg("%s%4d LOADOUTER level %d arg[%d]", pfx, current,
Bram Moolenaarcc341812022-09-19 15:54:34 +01006503 outer->outer_depth,
6504 outer->outer_idx + STACK_FRAME_SIZE);
6505 else if (outer->outer_depth < 0)
6506 smsg("%s%4d LOADOUTER $%d in loop level %d",
6507 pfx, current,
6508 outer->outer_idx,
6509 -outer->outer_depth);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006510 else
6511 smsg("%s%4d LOADOUTER level %d $%d", pfx, current,
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006512 outer->outer_depth,
6513 outer->outer_idx);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006514 }
6515 break;
6516 case ISN_LOADV:
6517 smsg("%s%4d LOADV v:%s", pfx, current,
6518 get_vim_var_name(iptr->isn_arg.number));
6519 break;
6520 case ISN_LOADSCRIPT:
6521 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006522 scriptref_T *sref = iptr->isn_arg.script.scriptref;
6523 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
6524 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006525
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006526 sv = get_script_svar(sref, -1);
6527 if (sv == NULL)
6528 smsg("%s%4d LOADSCRIPT [deleted] from %s",
6529 pfx, current, si->sn_name);
6530 else
6531 smsg("%s%4d LOADSCRIPT %s-%d from %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02006532 sv->sv_name,
6533 sref->sref_idx,
6534 si->sn_name);
6535 }
6536 break;
6537 case ISN_LOADS:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006538 case ISN_LOADEXPORT:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006539 {
6540 scriptitem_T *si = SCRIPT_ITEM(
6541 iptr->isn_arg.loadstore.ls_sid);
6542
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006543 smsg("%s%4d %s s:%s from %s", pfx, current,
6544 iptr->isn_type == ISN_LOADS ? "LOADS"
6545 : "LOADEXPORT",
6546 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006547 }
6548 break;
6549 case ISN_LOADAUTO:
6550 smsg("%s%4d LOADAUTO %s", pfx, current, iptr->isn_arg.string);
6551 break;
6552 case ISN_LOADG:
6553 smsg("%s%4d LOADG g:%s", pfx, current, iptr->isn_arg.string);
6554 break;
6555 case ISN_LOADB:
6556 smsg("%s%4d LOADB b:%s", pfx, current, iptr->isn_arg.string);
6557 break;
6558 case ISN_LOADW:
6559 smsg("%s%4d LOADW w:%s", pfx, current, iptr->isn_arg.string);
6560 break;
6561 case ISN_LOADT:
6562 smsg("%s%4d LOADT t:%s", pfx, current, iptr->isn_arg.string);
6563 break;
6564 case ISN_LOADGDICT:
6565 smsg("%s%4d LOAD g:", pfx, current);
6566 break;
6567 case ISN_LOADBDICT:
6568 smsg("%s%4d LOAD b:", pfx, current);
6569 break;
6570 case ISN_LOADWDICT:
6571 smsg("%s%4d LOAD w:", pfx, current);
6572 break;
6573 case ISN_LOADTDICT:
6574 smsg("%s%4d LOAD t:", pfx, current);
6575 break;
6576 case ISN_LOADOPT:
6577 smsg("%s%4d LOADOPT %s", pfx, current, iptr->isn_arg.string);
6578 break;
6579 case ISN_LOADENV:
6580 smsg("%s%4d LOADENV %s", pfx, current, iptr->isn_arg.string);
6581 break;
6582 case ISN_LOADREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006583 smsg("%s%4d LOADREG @%c", pfx, current,
6584 (int)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006585 break;
6586
6587 case ISN_STORE:
6588 if (iptr->isn_arg.number < 0)
6589 smsg("%s%4d STORE arg[%lld]", pfx, current,
6590 iptr->isn_arg.number + STACK_FRAME_SIZE);
6591 else
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006592 smsg("%s%4d STORE $%lld", pfx, current,
6593 iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006594 break;
6595 case ISN_STOREOUTER:
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006596 {
6597 isn_outer_T *outer = &iptr->isn_arg.outer;
6598
6599 if (outer->outer_depth == OUTER_LOOP_DEPTH)
6600 smsg("%s%4d STOREOUTER level 1 $%d in loop",
6601 pfx, current, outer->outer_idx);
6602 else
6603 smsg("%s%4d STOREOUTER level %d $%d", pfx, current,
6604 outer->outer_depth, outer->outer_idx);
6605 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006606 break;
6607 case ISN_STOREV:
6608 smsg("%s%4d STOREV v:%s", pfx, current,
6609 get_vim_var_name(iptr->isn_arg.number));
6610 break;
6611 case ISN_STOREAUTO:
6612 smsg("%s%4d STOREAUTO %s", pfx, current, iptr->isn_arg.string);
6613 break;
6614 case ISN_STOREG:
6615 smsg("%s%4d STOREG %s", pfx, current, iptr->isn_arg.string);
6616 break;
6617 case ISN_STOREB:
6618 smsg("%s%4d STOREB %s", pfx, current, iptr->isn_arg.string);
6619 break;
6620 case ISN_STOREW:
6621 smsg("%s%4d STOREW %s", pfx, current, iptr->isn_arg.string);
6622 break;
6623 case ISN_STORET:
6624 smsg("%s%4d STORET %s", pfx, current, iptr->isn_arg.string);
6625 break;
6626 case ISN_STORES:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006627 case ISN_STOREEXPORT:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006628 {
6629 scriptitem_T *si = SCRIPT_ITEM(
6630 iptr->isn_arg.loadstore.ls_sid);
6631
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006632 smsg("%s%4d %s %s in %s", pfx, current,
6633 iptr->isn_type == ISN_STORES
6634 ? "STORES" : "STOREEXPORT",
Bram Moolenaar4c137212021-04-19 16:48:48 +02006635 iptr->isn_arg.loadstore.ls_name, si->sn_name);
6636 }
6637 break;
6638 case ISN_STORESCRIPT:
6639 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006640 scriptref_T *sref = iptr->isn_arg.script.scriptref;
6641 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
6642 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006643
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006644 sv = get_script_svar(sref, -1);
6645 if (sv == NULL)
6646 smsg("%s%4d STORESCRIPT [deleted] in %s",
6647 pfx, current, si->sn_name);
6648 else
6649 smsg("%s%4d STORESCRIPT %s-%d in %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02006650 sv->sv_name,
6651 sref->sref_idx,
6652 si->sn_name);
6653 }
6654 break;
6655 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00006656 case ISN_STOREFUNCOPT:
6657 smsg("%s%4d %s &%s", pfx, current,
6658 iptr->isn_type == ISN_STOREOPT ? "STOREOPT" : "STOREFUNCOPT",
Bram Moolenaar4c137212021-04-19 16:48:48 +02006659 iptr->isn_arg.storeopt.so_name);
6660 break;
6661 case ISN_STOREENV:
6662 smsg("%s%4d STOREENV $%s", pfx, current, iptr->isn_arg.string);
6663 break;
6664 case ISN_STOREREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006665 smsg("%s%4d STOREREG @%c", pfx, current,
6666 (int)iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006667 break;
6668 case ISN_STORENR:
6669 smsg("%s%4d STORE %lld in $%d", pfx, current,
6670 iptr->isn_arg.storenr.stnr_val,
6671 iptr->isn_arg.storenr.stnr_idx);
6672 break;
6673
6674 case ISN_STOREINDEX:
6675 smsg("%s%4d STOREINDEX %s", pfx, current,
Bram Moolenaarf7d1c6e2023-01-16 20:47:57 +00006676 vartype_name(iptr->isn_arg.storeindex.si_vartype));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006677 break;
6678
6679 case ISN_STORERANGE:
6680 smsg("%s%4d STORERANGE", pfx, current);
6681 break;
6682
Bram Moolenaard505d172022-12-18 21:42:55 +00006683 case ISN_LOAD_CLASSMEMBER:
6684 case ISN_STORE_CLASSMEMBER:
6685 {
6686 class_T *cl = iptr->isn_arg.classmember.cm_class;
6687 int idx = iptr->isn_arg.classmember.cm_idx;
6688 ocmember_T *ocm = &cl->class_class_members[idx];
6689 smsg("%s%4d %s CLASSMEMBER %s.%s", pfx, current,
6690 iptr->isn_type == ISN_LOAD_CLASSMEMBER
6691 ? "LOAD" : "STORE",
6692 cl->class_name, ocm->ocm_name);
6693 }
6694 break;
6695
Bram Moolenaar4c137212021-04-19 16:48:48 +02006696 // constants
6697 case ISN_PUSHNR:
6698 smsg("%s%4d PUSHNR %lld", pfx, current,
6699 (varnumber_T)(iptr->isn_arg.number));
6700 break;
6701 case ISN_PUSHBOOL:
6702 case ISN_PUSHSPEC:
6703 smsg("%s%4d PUSH %s", pfx, current,
6704 get_var_special_name(iptr->isn_arg.number));
6705 break;
6706 case ISN_PUSHF:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006707 smsg("%s%4d PUSHF %g", pfx, current, iptr->isn_arg.fnumber);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006708 break;
6709 case ISN_PUSHS:
6710 smsg("%s%4d PUSHS \"%s\"", pfx, current, iptr->isn_arg.string);
6711 break;
6712 case ISN_PUSHBLOB:
6713 {
6714 char_u *r;
6715 char_u numbuf[NUMBUFLEN];
6716 char_u *tofree;
6717
6718 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
6719 smsg("%s%4d PUSHBLOB %s", pfx, current, r);
6720 vim_free(tofree);
6721 }
6722 break;
6723 case ISN_PUSHFUNC:
6724 {
6725 char *name = (char *)iptr->isn_arg.string;
6726
6727 smsg("%s%4d PUSHFUNC \"%s\"", pfx, current,
6728 name == NULL ? "[none]" : name);
6729 }
6730 break;
6731 case ISN_PUSHCHANNEL:
6732#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar397a87a2022-03-20 21:14:15 +00006733 smsg("%s%4d PUSHCHANNEL 0", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006734#endif
6735 break;
6736 case ISN_PUSHJOB:
6737#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar397a87a2022-03-20 21:14:15 +00006738 smsg("%s%4d PUSHJOB \"no process\"", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006739#endif
6740 break;
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00006741 case ISN_PUSHOBJ:
6742 smsg("%s%4d PUSHOBJ null", pfx, current);
6743 break;
6744 case ISN_PUSHCLASS:
6745 smsg("%s%4d PUSHCLASS %s", pfx, current,
Bram Moolenaar30a84472023-02-27 08:07:14 +00006746 iptr->isn_arg.classarg == NULL ? "null"
6747 : (char *)iptr->isn_arg.classarg->class_name);
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00006748 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006749 case ISN_PUSHEXC:
6750 smsg("%s%4d PUSH v:exception", pfx, current);
6751 break;
Bram Moolenaar06b77222022-01-25 15:51:56 +00006752 case ISN_AUTOLOAD:
6753 smsg("%s%4d AUTOLOAD %s", pfx, current, iptr->isn_arg.string);
6754 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006755 case ISN_UNLET:
6756 smsg("%s%4d UNLET%s %s", pfx, current,
6757 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
6758 iptr->isn_arg.unlet.ul_name);
6759 break;
6760 case ISN_UNLETENV:
6761 smsg("%s%4d UNLETENV%s $%s", pfx, current,
6762 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
6763 iptr->isn_arg.unlet.ul_name);
6764 break;
6765 case ISN_UNLETINDEX:
6766 smsg("%s%4d UNLETINDEX", pfx, current);
6767 break;
6768 case ISN_UNLETRANGE:
6769 smsg("%s%4d UNLETRANGE", pfx, current);
6770 break;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02006771 case ISN_LOCKUNLOCK:
6772 smsg("%s%4d LOCKUNLOCK %s", pfx, current, iptr->isn_arg.string);
6773 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006774 case ISN_LOCKCONST:
6775 smsg("%s%4d LOCKCONST", pfx, current);
6776 break;
6777 case ISN_NEWLIST:
6778 smsg("%s%4d NEWLIST size %lld", pfx, current,
Bram Moolenaar1936c762022-09-28 15:19:10 +01006779 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006780 break;
6781 case ISN_NEWDICT:
6782 smsg("%s%4d NEWDICT size %lld", pfx, current,
Bram Moolenaar1936c762022-09-28 15:19:10 +01006783 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006784 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00006785 case ISN_NEWPARTIAL:
6786 smsg("%s%4d NEWPARTIAL", pfx, current);
6787 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006788
6789 // function call
6790 case ISN_BCALL:
6791 {
6792 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
6793
6794 smsg("%s%4d BCALL %s(argc %d)", pfx, current,
6795 internal_func_name(cbfunc->cbf_idx),
6796 cbfunc->cbf_argcount);
6797 }
6798 break;
6799 case ISN_DCALL:
6800 {
6801 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
6802 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
6803 + cdfunc->cdf_idx;
6804
6805 smsg("%s%4d DCALL %s(argc %d)", pfx, current,
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006806 printable_func_name(df->df_ufunc),
6807 cdfunc->cdf_argcount);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006808 }
6809 break;
Bram Moolenaard0200c82023-01-28 15:19:40 +00006810 case ISN_METHODCALL:
6811 {
6812 cmfunc_T *mfunc = iptr->isn_arg.mfunc;
6813
6814 smsg("%s%4d METHODCALL %s.%s(argc %d)", pfx, current,
6815 mfunc->cmf_itf->class_name,
6816 mfunc->cmf_itf->class_obj_methods[
6817 mfunc->cmf_idx]->uf_name,
6818 mfunc->cmf_argcount);
6819 }
6820 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006821 case ISN_UCALL:
6822 {
6823 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
6824
6825 smsg("%s%4d UCALL %s(argc %d)", pfx, current,
6826 cufunc->cuf_name, cufunc->cuf_argcount);
6827 }
6828 break;
6829 case ISN_PCALL:
6830 {
6831 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
6832
6833 smsg("%s%4d PCALL%s (argc %d)", pfx, current,
6834 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
6835 }
6836 break;
6837 case ISN_PCALL_END:
6838 smsg("%s%4d PCALL end", pfx, current);
6839 break;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006840 case ISN_DEFER:
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00006841 case ISN_DEFEROBJ:
6842 smsg("%s%4d %s %d args", pfx, current,
6843 iptr->isn_type == ISN_DEFER ? "DEFER" : "DEFEROBJ",
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006844 (int)iptr->isn_arg.defer.defer_argcount);
6845 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006846 case ISN_RETURN:
6847 smsg("%s%4d RETURN", pfx, current);
6848 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02006849 case ISN_RETURN_VOID:
6850 smsg("%s%4d RETURN void", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006851 break;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006852 case ISN_RETURN_OBJECT:
6853 smsg("%s%4d RETURN object", pfx, current);
6854 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006855 case ISN_FUNCREF:
6856 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006857 funcref_T *funcref = &iptr->isn_arg.funcref;
6858 funcref_extra_T *extra = funcref->fr_extra;
6859 char_u *name;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006860
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006861 if (extra == NULL || extra->fre_func_name == NULL)
Bram Moolenaar38453522021-11-28 22:00:12 +00006862 {
6863 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
6864 + funcref->fr_dfunc_idx;
6865 name = df->df_ufunc->uf_name;
6866 }
6867 else
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006868 name = extra->fre_func_name;
Bram Moolenaar313e4722023-02-08 20:55:27 +00006869 if (extra != NULL && extra->fre_class != NULL)
6870 {
6871 smsg("%s%4d FUNCREF %s.%s", pfx, current,
6872 extra->fre_class->class_name, name);
6873 }
6874 else if (extra == NULL
6875 || extra->fre_loopvar_info.lvi_depth == 0)
6876 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006877 smsg("%s%4d FUNCREF %s", pfx, current, name);
Bram Moolenaar313e4722023-02-08 20:55:27 +00006878 }
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006879 else
Bram Moolenaarcc341812022-09-19 15:54:34 +01006880 {
6881 char_u *info = printable_loopvarinfo(
6882 &extra->fre_loopvar_info);
6883
6884 smsg("%s%4d FUNCREF %s vars %s", pfx, current,
6885 name, info);
6886 vim_free(info);
6887 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006888 }
6889 break;
6890
6891 case ISN_NEWFUNC:
6892 {
Bram Moolenaarcc341812022-09-19 15:54:34 +01006893 newfuncarg_T *arg = iptr->isn_arg.newfunc.nf_arg;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006894
Bram Moolenaarcc341812022-09-19 15:54:34 +01006895 if (arg->nfa_loopvar_info.lvi_depth == 0)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006896 smsg("%s%4d NEWFUNC %s %s", pfx, current,
6897 arg->nfa_lambda, arg->nfa_global);
6898 else
Bram Moolenaarcc341812022-09-19 15:54:34 +01006899 {
6900 char_u *info = printable_loopvarinfo(
6901 &arg->nfa_loopvar_info);
6902
6903 smsg("%s%4d NEWFUNC %s %s vars %s", pfx, current,
6904 arg->nfa_lambda, arg->nfa_global, info);
6905 vim_free(info);
6906 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006907 }
6908 break;
6909
6910 case ISN_DEF:
6911 {
6912 char_u *name = iptr->isn_arg.string;
6913
6914 smsg("%s%4d DEF %s", pfx, current,
6915 name == NULL ? (char_u *)"" : name);
6916 }
6917 break;
6918
6919 case ISN_JUMP:
6920 {
6921 char *when = "?";
6922
6923 switch (iptr->isn_arg.jump.jump_when)
6924 {
6925 case JUMP_ALWAYS:
6926 when = "JUMP";
6927 break;
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02006928 case JUMP_NEVER:
6929 iemsg("JUMP_NEVER should not be used");
6930 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006931 case JUMP_AND_KEEP_IF_TRUE:
6932 when = "JUMP_AND_KEEP_IF_TRUE";
6933 break;
6934 case JUMP_IF_FALSE:
6935 when = "JUMP_IF_FALSE";
6936 break;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01006937 case JUMP_WHILE_FALSE:
6938 when = "JUMP_WHILE_FALSE"; // unused
6939 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006940 case JUMP_IF_COND_FALSE:
6941 when = "JUMP_IF_COND_FALSE";
6942 break;
6943 case JUMP_IF_COND_TRUE:
6944 when = "JUMP_IF_COND_TRUE";
6945 break;
6946 }
6947 smsg("%s%4d %s -> %d", pfx, current, when,
6948 iptr->isn_arg.jump.jump_where);
6949 }
6950 break;
6951
6952 case ISN_JUMP_IF_ARG_SET:
6953 smsg("%s%4d JUMP_IF_ARG_SET arg[%d] -> %d", pfx, current,
6954 iptr->isn_arg.jumparg.jump_arg_off + STACK_FRAME_SIZE,
6955 iptr->isn_arg.jump.jump_where);
6956 break;
6957
Bram Moolenaar65b0d162022-12-13 18:43:22 +00006958 case ISN_JUMP_IF_ARG_NOT_SET:
6959 smsg("%s%4d JUMP_IF_ARG_NOT_SET arg[%d] -> %d", pfx, current,
6960 iptr->isn_arg.jumparg.jump_arg_off + STACK_FRAME_SIZE,
6961 iptr->isn_arg.jump.jump_where);
6962 break;
6963
Bram Moolenaar4c137212021-04-19 16:48:48 +02006964 case ISN_FOR:
6965 {
6966 forloop_T *forloop = &iptr->isn_arg.forloop;
6967
6968 smsg("%s%4d FOR $%d -> %d", pfx, current,
Bram Moolenaarcc341812022-09-19 15:54:34 +01006969 forloop->for_loop_idx, forloop->for_end);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006970 }
6971 break;
6972
Bram Moolenaarb46c0832022-09-15 17:19:37 +01006973 case ISN_ENDLOOP:
6974 {
6975 endloop_T *endloop = &iptr->isn_arg.endloop;
6976
Bram Moolenaarcc341812022-09-19 15:54:34 +01006977 smsg("%s%4d ENDLOOP ref $%d save $%d-$%d depth %d",
6978 pfx, current,
Bram Moolenaarb46c0832022-09-15 17:19:37 +01006979 endloop->end_funcref_idx,
6980 endloop->end_var_idx,
Bram Moolenaarcc341812022-09-19 15:54:34 +01006981 endloop->end_var_idx + endloop->end_var_count - 1,
6982 endloop->end_depth);
Bram Moolenaarb46c0832022-09-15 17:19:37 +01006983 }
6984 break;
6985
6986 case ISN_WHILE:
6987 {
6988 whileloop_T *whileloop = &iptr->isn_arg.whileloop;
6989
6990 smsg("%s%4d WHILE $%d -> %d", pfx, current,
6991 whileloop->while_funcref_idx,
6992 whileloop->while_end);
6993 }
6994 break;
6995
Bram Moolenaar4c137212021-04-19 16:48:48 +02006996 case ISN_TRY:
6997 {
Bram Moolenaar0d807102021-12-21 09:42:09 +00006998 try_T *try = &iptr->isn_arg.tryref;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006999
7000 if (try->try_ref->try_finally == 0)
7001 smsg("%s%4d TRY catch -> %d, endtry -> %d",
7002 pfx, current,
7003 try->try_ref->try_catch,
7004 try->try_ref->try_endtry);
7005 else
7006 smsg("%s%4d TRY catch -> %d, finally -> %d, endtry -> %d",
7007 pfx, current,
7008 try->try_ref->try_catch,
7009 try->try_ref->try_finally,
7010 try->try_ref->try_endtry);
7011 }
7012 break;
7013 case ISN_CATCH:
Bram Moolenaar4c137212021-04-19 16:48:48 +02007014 smsg("%s%4d CATCH", pfx, current);
7015 break;
7016 case ISN_TRYCONT:
7017 {
7018 trycont_T *trycont = &iptr->isn_arg.trycont;
7019
7020 smsg("%s%4d TRY-CONTINUE %d level%s -> %d", pfx, current,
7021 trycont->tct_levels,
7022 trycont->tct_levels == 1 ? "" : "s",
7023 trycont->tct_where);
7024 }
7025 break;
7026 case ISN_FINALLY:
7027 smsg("%s%4d FINALLY", pfx, current);
7028 break;
7029 case ISN_ENDTRY:
7030 smsg("%s%4d ENDTRY", pfx, current);
7031 break;
7032 case ISN_THROW:
7033 smsg("%s%4d THROW", pfx, current);
7034 break;
7035
7036 // expression operations on number
7037 case ISN_OPNR:
7038 case ISN_OPFLOAT:
7039 case ISN_OPANY:
7040 {
7041 char *what;
7042 char *ins;
7043
7044 switch (iptr->isn_arg.op.op_type)
7045 {
7046 case EXPR_MULT: what = "*"; break;
7047 case EXPR_DIV: what = "/"; break;
7048 case EXPR_REM: what = "%"; break;
7049 case EXPR_SUB: what = "-"; break;
7050 case EXPR_ADD: what = "+"; break;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01007051 case EXPR_LSHIFT: what = "<<"; break;
7052 case EXPR_RSHIFT: what = ">>"; break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007053 default: what = "???"; break;
7054 }
7055 switch (iptr->isn_type)
7056 {
7057 case ISN_OPNR: ins = "OPNR"; break;
7058 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
7059 case ISN_OPANY: ins = "OPANY"; break;
7060 default: ins = "???"; break;
7061 }
7062 smsg("%s%4d %s %s", pfx, current, ins, what);
7063 }
7064 break;
7065
7066 case ISN_COMPAREBOOL:
7067 case ISN_COMPARESPECIAL:
Bram Moolenaar7a222242022-03-01 19:23:24 +00007068 case ISN_COMPARENULL:
Bram Moolenaar4c137212021-04-19 16:48:48 +02007069 case ISN_COMPARENR:
7070 case ISN_COMPAREFLOAT:
7071 case ISN_COMPARESTRING:
7072 case ISN_COMPAREBLOB:
7073 case ISN_COMPARELIST:
7074 case ISN_COMPAREDICT:
7075 case ISN_COMPAREFUNC:
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00007076 case ISN_COMPARECLASS:
7077 case ISN_COMPAREOBJECT:
Bram Moolenaar4c137212021-04-19 16:48:48 +02007078 case ISN_COMPAREANY:
7079 {
7080 char *p;
7081 char buf[10];
7082 char *type;
7083
7084 switch (iptr->isn_arg.op.op_type)
7085 {
7086 case EXPR_EQUAL: p = "=="; break;
7087 case EXPR_NEQUAL: p = "!="; break;
7088 case EXPR_GREATER: p = ">"; break;
7089 case EXPR_GEQUAL: p = ">="; break;
7090 case EXPR_SMALLER: p = "<"; break;
7091 case EXPR_SEQUAL: p = "<="; break;
7092 case EXPR_MATCH: p = "=~"; break;
7093 case EXPR_IS: p = "is"; break;
7094 case EXPR_ISNOT: p = "isnot"; break;
7095 case EXPR_NOMATCH: p = "!~"; break;
7096 default: p = "???"; break;
7097 }
7098 STRCPY(buf, p);
7099 if (iptr->isn_arg.op.op_ic == TRUE)
7100 strcat(buf, "?");
7101 switch(iptr->isn_type)
7102 {
7103 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
7104 case ISN_COMPARESPECIAL:
7105 type = "COMPARESPECIAL"; break;
Bram Moolenaar7a222242022-03-01 19:23:24 +00007106 case ISN_COMPARENULL: type = "COMPARENULL"; break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007107 case ISN_COMPARENR: type = "COMPARENR"; break;
7108 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
7109 case ISN_COMPARESTRING:
7110 type = "COMPARESTRING"; break;
7111 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
7112 case ISN_COMPARELIST: type = "COMPARELIST"; break;
7113 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
7114 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00007115 case ISN_COMPARECLASS: type = "COMPARECLASS"; break;
7116 case ISN_COMPAREOBJECT:
7117 type = "COMPAREOBJECT"; break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007118 case ISN_COMPAREANY: type = "COMPAREANY"; break;
7119 default: type = "???"; break;
7120 }
7121
7122 smsg("%s%4d %s %s", pfx, current, type, buf);
7123 }
7124 break;
7125
7126 case ISN_ADDLIST: smsg("%s%4d ADDLIST", pfx, current); break;
7127 case ISN_ADDBLOB: smsg("%s%4d ADDBLOB", pfx, current); break;
7128
7129 // expression operations
LemonBoy372bcce2022-04-25 12:43:20 +01007130 case ISN_CONCAT:
7131 smsg("%s%4d CONCAT size %lld", pfx, current,
7132 (varnumber_T)(iptr->isn_arg.number));
7133 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007134 case ISN_STRINDEX: smsg("%s%4d STRINDEX", pfx, current); break;
7135 case ISN_STRSLICE: smsg("%s%4d STRSLICE", pfx, current); break;
7136 case ISN_BLOBINDEX: smsg("%s%4d BLOBINDEX", pfx, current); break;
7137 case ISN_BLOBSLICE: smsg("%s%4d BLOBSLICE", pfx, current); break;
7138 case ISN_LISTAPPEND: smsg("%s%4d LISTAPPEND", pfx, current); break;
7139 case ISN_BLOBAPPEND: smsg("%s%4d BLOBAPPEND", pfx, current); break;
7140 case ISN_LISTINDEX: smsg("%s%4d LISTINDEX", pfx, current); break;
7141 case ISN_LISTSLICE: smsg("%s%4d LISTSLICE", pfx, current); break;
7142 case ISN_ANYINDEX: smsg("%s%4d ANYINDEX", pfx, current); break;
7143 case ISN_ANYSLICE: smsg("%s%4d ANYSLICE", pfx, current); break;
7144 case ISN_SLICE: smsg("%s%4d SLICE %lld",
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02007145 pfx, current, iptr->isn_arg.number); break;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02007146 case ISN_GETITEM: smsg("%s%4d ITEM %lld%s", pfx, current,
7147 iptr->isn_arg.getitem.gi_index,
7148 iptr->isn_arg.getitem.gi_with_op ?
7149 " with op" : ""); break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007150 case ISN_MEMBER: smsg("%s%4d MEMBER", pfx, current); break;
7151 case ISN_STRINGMEMBER: smsg("%s%4d MEMBER %s", pfx, current,
7152 iptr->isn_arg.string); break;
Yegappan Lakshmanan5a05d372023-09-29 19:43:11 +02007153 case ISN_GET_OBJ_MEMBER: smsg("%s%4d OBJ_MEMBER %d", pfx, current,
7154 (int)iptr->isn_arg.classmember.cm_idx);
Ernie Rael00df69e2023-09-05 07:38:09 +02007155 break;
Yegappan Lakshmanan5a05d372023-09-29 19:43:11 +02007156 case ISN_GET_ITF_MEMBER: smsg("%s%4d ITF_MEMBER %d on %s",
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00007157 pfx, current,
7158 (int)iptr->isn_arg.classmember.cm_idx,
Yegappan Lakshmanan5a05d372023-09-29 19:43:11 +02007159 iptr->isn_arg.classmember.cm_class->class_name);
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00007160 break;
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00007161 case ISN_STORE_THIS: smsg("%s%4d STORE_THIS %d", pfx, current,
Bram Moolenaarffdaca92022-12-09 21:41:48 +00007162 (int)iptr->isn_arg.number); break;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02007163 case ISN_CLEARDICT: smsg("%s%4d CLEARDICT", pfx, current); break;
7164 case ISN_USEDICT: smsg("%s%4d USEDICT", pfx, current); break;
7165
Bram Moolenaar4c137212021-04-19 16:48:48 +02007166 case ISN_NEGATENR: smsg("%s%4d NEGATENR", pfx, current); break;
7167
Bram Moolenaar4c137212021-04-19 16:48:48 +02007168 case ISN_CHECKTYPE:
7169 {
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01007170 checktype_T *ct = &iptr->isn_arg.type;
Bram Moolenaarc6951a72022-12-29 20:56:24 +00007171 char *tofree = NULL;
7172 char *typename;
7173
7174 if (ct->ct_type->tt_type == VAR_FLOAT
7175 && (ct->ct_type->tt_flags & TTFLAG_NUMBER_OK))
7176 typename = "float|number";
7177 else
7178 typename = type_name(ct->ct_type, &tofree);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007179
7180 if (ct->ct_arg_idx == 0)
7181 smsg("%s%4d CHECKTYPE %s stack[%d]", pfx, current,
Bram Moolenaarc6951a72022-12-29 20:56:24 +00007182 typename,
Bram Moolenaar4c137212021-04-19 16:48:48 +02007183 (int)ct->ct_off);
7184 else
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01007185 smsg("%s%4d CHECKTYPE %s stack[%d] %s %d",
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02007186 pfx, current,
Bram Moolenaarc6951a72022-12-29 20:56:24 +00007187 typename,
Bram Moolenaar4c137212021-04-19 16:48:48 +02007188 (int)ct->ct_off,
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01007189 ct->ct_is_var ? "var": "arg",
Bram Moolenaar4c137212021-04-19 16:48:48 +02007190 (int)ct->ct_arg_idx);
7191 vim_free(tofree);
7192 break;
7193 }
7194 case ISN_CHECKLEN: smsg("%s%4d CHECKLEN %s%d", pfx, current,
7195 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
7196 iptr->isn_arg.checklen.cl_min_len);
7197 break;
7198 case ISN_SETTYPE:
7199 {
7200 char *tofree;
7201
7202 smsg("%s%4d SETTYPE %s", pfx, current,
7203 type_name(iptr->isn_arg.type.ct_type, &tofree));
7204 vim_free(tofree);
7205 break;
7206 }
7207 case ISN_COND2BOOL: smsg("%s%4d COND2BOOL", pfx, current); break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007208 case ISN_2BOOL: if (iptr->isn_arg.tobool.invert)
7209 smsg("%s%4d INVERT %d (!val)", pfx, current,
7210 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007211 else
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007212 smsg("%s%4d 2BOOL %d (!!val)", pfx, current,
7213 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007214 break;
7215 case ISN_2STRING: smsg("%s%4d 2STRING stack[%lld]", pfx, current,
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007216 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02007217 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007218 case ISN_2STRING_ANY: smsg("%s%4d 2STRING_ANY stack[%lld]",
7219 pfx, current,
7220 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02007221 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007222 case ISN_RANGE: smsg("%s%4d RANGE %s", pfx, current,
7223 iptr->isn_arg.string);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007224 break;
7225 case ISN_PUT:
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01007226 if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE)
Bram Moolenaar4c137212021-04-19 16:48:48 +02007227 smsg("%s%4d PUT %c above range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007228 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007229 else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE)
7230 smsg("%s%4d PUT %c range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007231 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007232 else
7233 smsg("%s%4d PUT %c %ld", pfx, current,
7234 iptr->isn_arg.put.put_regname,
7235 (long)iptr->isn_arg.put.put_lnum);
7236 break;
7237
Bram Moolenaar4c137212021-04-19 16:48:48 +02007238 case ISN_CMDMOD:
7239 {
7240 char_u *buf;
7241 size_t len = produce_cmdmods(
7242 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
7243
7244 buf = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02007245 if (likely(buf != NULL))
Bram Moolenaar4c137212021-04-19 16:48:48 +02007246 {
7247 (void)produce_cmdmods(
7248 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
7249 smsg("%s%4d CMDMOD %s", pfx, current, buf);
7250 vim_free(buf);
7251 }
7252 break;
7253 }
7254 case ISN_CMDMOD_REV: smsg("%s%4d CMDMOD_REV", pfx, current); break;
7255
7256 case ISN_PROF_START:
Bram Moolenaare99d4222021-06-13 14:01:26 +02007257 smsg("%s%4d PROFILE START line %d", pfx, current,
7258 iptr->isn_lnum);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007259 break;
7260
7261 case ISN_PROF_END:
7262 smsg("%s%4d PROFILE END", pfx, current);
7263 break;
7264
Bram Moolenaare99d4222021-06-13 14:01:26 +02007265 case ISN_DEBUG:
Bram Moolenaar8cec9272021-06-23 20:20:53 +02007266 smsg("%s%4d DEBUG line %d-%d varcount %lld", pfx, current,
7267 iptr->isn_arg.debug.dbg_break_lnum + 1,
7268 iptr->isn_lnum,
7269 iptr->isn_arg.debug.dbg_var_names_len);
Bram Moolenaare99d4222021-06-13 14:01:26 +02007270 break;
7271
Bram Moolenaar4c137212021-04-19 16:48:48 +02007272 case ISN_UNPACK: smsg("%s%4d UNPACK %d%s", pfx, current,
7273 iptr->isn_arg.unpack.unp_count,
7274 iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : "");
7275 break;
7276 case ISN_SHUFFLE: smsg("%s%4d SHUFFLE %d up %d", pfx, current,
7277 iptr->isn_arg.shuffle.shfl_item,
7278 iptr->isn_arg.shuffle.shfl_up);
7279 break;
7280 case ISN_DROP: smsg("%s%4d DROP", pfx, current); break;
7281
7282 case ISN_FINISH: // End of list of instructions for ISN_SUBSTITUTE.
7283 return;
7284 }
7285
7286 out_flush(); // output one line at a time
7287 ui_breakcheck();
7288 if (got_int)
7289 break;
7290 }
7291}
7292
7293/*
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02007294 * Handle command line completion for the :disassemble command.
7295 */
7296 void
7297set_context_in_disassemble_cmd(expand_T *xp, char_u *arg)
7298{
7299 char_u *p;
7300
7301 // Default: expand user functions, "debug" and "profile"
7302 xp->xp_context = EXPAND_DISASSEMBLE;
7303 xp->xp_pattern = arg;
7304
7305 // first argument already typed: only user function names
7306 if (*arg != NUL && *(p = skiptowhite(arg)) != NUL)
7307 {
7308 xp->xp_context = EXPAND_USER_FUNC;
7309 xp->xp_pattern = skipwhite(p);
7310 }
7311}
7312
7313/*
7314 * Function given to ExpandGeneric() to obtain the list of :disassemble
7315 * arguments.
7316 */
7317 char_u *
7318get_disassemble_argument(expand_T *xp, int idx)
7319{
7320 if (idx == 0)
7321 return (char_u *)"debug";
7322 if (idx == 1)
7323 return (char_u *)"profile";
7324 return get_user_func_name(xp, idx - 2);
7325}
7326
7327/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01007328 * ":disassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01007329 * We don't really need this at runtime, but we do have tests that require it,
7330 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007331 */
7332 void
7333ex_disassemble(exarg_T *eap)
7334{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01007335 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01007336 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007337 dfunc_T *dfunc;
Bram Moolenaardafef512022-05-21 21:55:55 +01007338 isn_T *instr = NULL; // init to shut up gcc warning
7339 int instr_count = 0; // init to shut up gcc warning
Bram Moolenaar5a01caa2022-05-21 18:56:58 +01007340 compiletype_T compile_type = CT_NONE;
Bram Moolenaare99d4222021-06-13 14:01:26 +02007341
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01007342 ufunc = find_func_by_name(arg, &compile_type);
Bram Moolenaara26b9702020-04-18 19:53:28 +02007343 if (ufunc == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007344 return;
Bram Moolenaare99d4222021-06-13 14:01:26 +02007345 if (func_needs_compiling(ufunc, compile_type)
7346 && compile_def_function(ufunc, FALSE, compile_type, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02007347 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007348 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007349 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007350 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007351 return;
7352 }
Bram Moolenaar6db660b2021-08-01 14:08:54 +02007353 msg((char *)printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007354
7355 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
Bram Moolenaare99d4222021-06-13 14:01:26 +02007356 switch (compile_type)
7357 {
7358 case CT_PROFILE:
Bram Moolenaarf002a412021-01-24 13:34:18 +01007359#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02007360 instr = dfunc->df_instr_prof;
7361 instr_count = dfunc->df_instr_prof_count;
7362 break;
Bram Moolenaarf002a412021-01-24 13:34:18 +01007363#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02007364 // FALLTHROUGH
7365 case CT_NONE:
7366 instr = dfunc->df_instr;
7367 instr_count = dfunc->df_instr_count;
7368 break;
7369 case CT_DEBUG:
7370 instr = dfunc->df_instr_debug;
7371 instr_count = dfunc->df_instr_debug_count;
7372 break;
7373 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007374
Bram Moolenaar4c137212021-04-19 16:48:48 +02007375 list_instructions("", instr, instr_count, ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007376}
7377
7378/*
Bram Moolenaar13106602020-10-04 16:06:05 +02007379 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007380 * list, etc. Mostly like what JavaScript does, except that empty list and
7381 * empty dictionary are FALSE.
7382 */
7383 int
7384tv2bool(typval_T *tv)
7385{
7386 switch (tv->v_type)
7387 {
7388 case VAR_NUMBER:
7389 return tv->vval.v_number != 0;
7390 case VAR_FLOAT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007391 return tv->vval.v_float != 0.0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007392 case VAR_PARTIAL:
7393 return tv->vval.v_partial != NULL;
7394 case VAR_FUNC:
7395 case VAR_STRING:
7396 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
7397 case VAR_LIST:
7398 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
7399 case VAR_DICT:
7400 return tv->vval.v_dict != NULL
7401 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
7402 case VAR_BOOL:
7403 case VAR_SPECIAL:
7404 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
7405 case VAR_JOB:
7406#ifdef FEAT_JOB_CHANNEL
7407 return tv->vval.v_job != NULL;
7408#else
7409 break;
7410#endif
7411 case VAR_CHANNEL:
7412#ifdef FEAT_JOB_CHANNEL
7413 return tv->vval.v_channel != NULL;
7414#else
7415 break;
7416#endif
7417 case VAR_BLOB:
7418 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
7419 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02007420 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007421 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02007422 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00007423 case VAR_CLASS:
7424 case VAR_OBJECT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007425 break;
7426 }
7427 return FALSE;
7428}
7429
Bram Moolenaarea2d4072020-11-12 12:08:51 +01007430 void
7431emsg_using_string_as(typval_T *tv, int as_number)
7432{
7433 semsg(_(as_number ? e_using_string_as_number_str
7434 : e_using_string_as_bool_str),
7435 tv->vval.v_string == NULL
7436 ? (char_u *)"" : tv->vval.v_string);
7437}
7438
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007439/*
7440 * If "tv" is a string give an error and return FAIL.
7441 */
7442 int
7443check_not_string(typval_T *tv)
7444{
7445 if (tv->v_type == VAR_STRING)
7446 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +01007447 emsg_using_string_as(tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007448 clear_tv(tv);
7449 return FAIL;
7450 }
7451 return OK;
7452}
7453
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007454
7455#endif // FEAT_EVAL