blob: 0700eb78094b301ce0b9e828ffeea25ecc0392e0 [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,
2321 obj->obj_class, FALSE);
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,
Ernie Rael18143d32023-09-04 22:30:41 +02004265 TRUE, mfunc->cmf_idx, cl,
4266 FALSE);
Bram Moolenaard0200c82023-01-28 15:19:40 +00004267
4268 if (call_ufunc(cl->class_obj_methods[idx], NULL,
4269 mfunc->cmf_argcount, ectx, NULL, NULL) == FAIL)
4270 goto on_error;
4271 }
4272 break;
4273
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004274 // call a builtin function
4275 case ISN_BCALL:
4276 SOURCING_LNUM = iptr->isn_lnum;
4277 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
4278 iptr->isn_arg.bfunc.cbf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02004279 ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004280 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004281 break;
4282
4283 // call a funcref or partial
4284 case ISN_PCALL:
4285 {
4286 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
4287 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02004288 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004289
4290 SOURCING_LNUM = iptr->isn_lnum;
4291 if (pfunc->cpf_top)
4292 {
4293 // funcref is above the arguments
4294 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
4295 }
4296 else
4297 {
4298 // Get the funcref from the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004299 --ectx->ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02004300 partial_tv = *STACK_TV_BOT(0);
4301 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004302 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004303 r = call_partial(tv, pfunc->cpf_argcount, ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02004304 if (tv == &partial_tv)
4305 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004306 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004307 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004308 }
4309 break;
4310
Bram Moolenaarbd5da372020-03-31 23:13:10 +02004311 case ISN_PCALL_END:
4312 // PCALL finished, arguments have been consumed and replaced by
4313 // the return value. Now clear the funcref from the stack,
4314 // and move the return value in its place.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004315 --ectx->ec_stack.ga_len;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02004316 clear_tv(STACK_TV_BOT(-1));
4317 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
4318 break;
4319
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004320 // call a user defined function or funcref/partial
4321 case ISN_UCALL:
4322 {
4323 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
4324
4325 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01004326 if (call_eval_func(cufunc->cuf_name, cufunc->cuf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02004327 ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004328 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004329 }
4330 break;
4331
Bram Moolenaar1d84f762022-09-03 21:35:53 +01004332 // :defer func(arg)
4333 case ISN_DEFER:
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00004334 case ISN_DEFEROBJ:
Bram Moolenaar806a2732022-09-04 15:40:36 +01004335 if (defer_command(iptr->isn_arg.defer.defer_var_idx,
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00004336 iptr->isn_type == ISN_DEFEROBJ,
Bram Moolenaar1d84f762022-09-03 21:35:53 +01004337 iptr->isn_arg.defer.defer_argcount, ectx) == FAIL)
4338 goto on_error;
4339 break;
4340
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004341 // Return from a :def function call without a value.
4342 // Return from a constructor.
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02004343 case ISN_RETURN_VOID:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004344 case ISN_RETURN_OBJECT:
Bram Moolenaar35578162021-08-02 19:10:38 +02004345 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004346 goto theend;
Bram Moolenaar299f3032021-01-08 20:53:09 +01004347 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004348 ++ectx->ec_stack.ga_len;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004349 if (iptr->isn_type == ISN_RETURN_VOID)
4350 {
4351 tv->v_type = VAR_VOID;
4352 tv->vval.v_number = 0;
4353 tv->v_lock = 0;
4354 }
4355 else
4356 {
4357 *tv = *STACK_TV_VAR(0);
4358 ++tv->vval.v_object->obj_refcount;
4359 }
Bram Moolenaar299f3032021-01-08 20:53:09 +01004360 // FALLTHROUGH
4361
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02004362 // return from a :def function call with what is on the stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004363 case ISN_RETURN:
4364 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004365 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01004366 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01004367
4368 if (trystack->ga_len > 0)
4369 trycmd = ((trycmd_T *)trystack->ga_data)
4370 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004371 if (trycmd != NULL
Bram Moolenaar4c137212021-04-19 16:48:48 +02004372 && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004373 {
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01004374 // jump to ":finally" or ":endtry"
4375 if (trycmd->tcd_finally_idx != 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004376 ectx->ec_iidx = trycmd->tcd_finally_idx;
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01004377 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02004378 ectx->ec_iidx = trycmd->tcd_endtry_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004379 trycmd->tcd_return = TRUE;
4380 }
4381 else
Bram Moolenaard032f342020-07-18 18:13:02 +02004382 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004383 }
4384 break;
4385
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004386 // push a partial, a reference to a compiled function
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004387 case ISN_FUNCREF:
4388 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004389 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
4390 ufunc_T *ufunc;
4391 funcref_T *funcref = &iptr->isn_arg.funcref;
4392 funcref_extra_T *extra = funcref->fr_extra;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004393
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004394 if (pt == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004395 goto theend;
Bram Moolenaar35578162021-08-02 19:10:38 +02004396 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02004397 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004398 vim_free(pt);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004399 goto theend;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004400 }
Bram Moolenaar313e4722023-02-08 20:55:27 +00004401 if (extra != NULL && extra->fre_class != NULL)
4402 {
4403 tv = STACK_TV_BOT(-1);
4404 if (tv->v_type != VAR_OBJECT)
4405 {
4406 object_required_error(tv);
4407 vim_free(pt);
4408 goto on_error;
4409 }
4410 object_T *obj = tv->vval.v_object;
4411 class_T *cl = obj->obj_class;
4412
4413 // convert the interface index to the object index
4414 int idx = object_index_from_itf_index(extra->fre_class,
Ernie Rael18143d32023-09-04 22:30:41 +02004415 TRUE, extra->fre_method_idx, cl,
4416 FALSE);
Bram Moolenaar313e4722023-02-08 20:55:27 +00004417 ufunc = cl->class_obj_methods[idx];
4418 }
4419 else if (extra == NULL || extra->fre_func_name == NULL)
Bram Moolenaar38453522021-11-28 22:00:12 +00004420 {
4421 dfunc_T *pt_dfunc = ((dfunc_T *)def_functions.ga_data)
4422 + funcref->fr_dfunc_idx;
4423
4424 ufunc = pt_dfunc->df_ufunc;
4425 }
4426 else
Bram Moolenaar313e4722023-02-08 20:55:27 +00004427 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004428 ufunc = find_func(extra->fre_func_name, FALSE);
Bram Moolenaar313e4722023-02-08 20:55:27 +00004429 }
Bram Moolenaar56acd1f2022-02-18 13:24:52 +00004430 if (ufunc == NULL)
4431 {
4432 SOURCING_LNUM = iptr->isn_lnum;
4433 iemsg("ufunc unexpectedly NULL for FUNCREF");
4434 goto theend;
4435 }
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004436 if (fill_partial_and_closure(pt, ufunc,
Bram Moolenaarcc341812022-09-19 15:54:34 +01004437 extra == NULL ? NULL : &extra->fre_loopvar_info,
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004438 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004439 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004440 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004441 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004442 tv->vval.v_partial = pt;
4443 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02004444 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004445 }
4446 break;
4447
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004448 // Create a global function from a lambda.
4449 case ISN_NEWFUNC:
4450 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004451 newfuncarg_T *arg = iptr->isn_arg.newfunc.nf_arg;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004452
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004453 if (copy_lambda_to_global_func(arg->nfa_lambda,
Bram Moolenaarcc341812022-09-19 15:54:34 +01004454 arg->nfa_global, &arg->nfa_loopvar_info,
4455 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004456 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004457 }
4458 break;
4459
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004460 // List functions
4461 case ISN_DEF:
4462 if (iptr->isn_arg.string == NULL)
4463 list_functions(NULL);
4464 else
4465 {
Bram Moolenaar14336722022-01-08 16:02:59 +00004466 exarg_T ea;
4467 garray_T lines_to_free;
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004468
4469 CLEAR_FIELD(ea);
4470 ea.cmd = ea.arg = iptr->isn_arg.string;
Bram Moolenaar14336722022-01-08 16:02:59 +00004471 ga_init2(&lines_to_free, sizeof(char_u *), 50);
Bram Moolenaard4a9b7f2023-05-23 14:48:42 +01004472 SOURCING_LNUM = iptr->isn_lnum;
h-eastb895b0f2023-09-24 15:46:31 +02004473 define_function(&ea, NULL, &lines_to_free, 0, NULL, 0);
Bram Moolenaar14336722022-01-08 16:02:59 +00004474 ga_clear_strings(&lines_to_free);
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004475 }
4476 break;
4477
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004478 // jump if a condition is met
4479 case ISN_JUMP:
4480 {
4481 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004482 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004483 int jump = TRUE;
4484
4485 if (when != JUMP_ALWAYS)
4486 {
4487 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004488 if (when == JUMP_IF_COND_FALSE
Bram Moolenaar13106602020-10-04 16:06:05 +02004489 || when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004490 || when == JUMP_IF_COND_TRUE)
4491 {
4492 SOURCING_LNUM = iptr->isn_lnum;
4493 jump = tv_get_bool_chk(tv, &error);
4494 if (error)
4495 goto on_error;
4496 }
4497 else
4498 jump = tv2bool(tv);
Bram Moolenaarf6ced982022-04-28 12:00:49 +01004499 if (when == JUMP_IF_FALSE || when == JUMP_IF_COND_FALSE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004500 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01004501 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004502 {
4503 // drop the value from the stack
4504 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004505 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004506 }
4507 }
4508 if (jump)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004509 ectx->ec_iidx = iptr->isn_arg.jump.jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004510 }
4511 break;
4512
Bram Moolenaarb46c0832022-09-15 17:19:37 +01004513 // "while": jump to end if a condition is false
4514 case ISN_WHILE:
4515 {
4516 int error = FALSE;
4517 int jump = TRUE;
4518
4519 tv = STACK_TV_BOT(-1);
4520 SOURCING_LNUM = iptr->isn_lnum;
4521 jump = !tv_get_bool_chk(tv, &error);
4522 if (error)
4523 goto on_error;
4524 // drop the value from the stack
4525 clear_tv(tv);
4526 --ectx->ec_stack.ga_len;
4527 if (jump)
4528 ectx->ec_iidx = iptr->isn_arg.whileloop.while_end;
4529
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01004530 // Store the current funcref count, may be used by
Bram Moolenaarcc341812022-09-19 15:54:34 +01004531 // ISN_ENDLOOP later
Bram Moolenaarb46c0832022-09-15 17:19:37 +01004532 tv = STACK_TV_VAR(
4533 iptr->isn_arg.whileloop.while_funcref_idx);
4534 tv->vval.v_number = ectx->ec_funcrefs.ga_len;
4535 }
4536 break;
4537
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02004538 // Jump if an argument with a default value was already set and not
4539 // v:none.
4540 case ISN_JUMP_IF_ARG_SET:
Bram Moolenaar65b0d162022-12-13 18:43:22 +00004541 case ISN_JUMP_IF_ARG_NOT_SET:
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02004542 tv = STACK_TV_VAR(iptr->isn_arg.jumparg.jump_arg_off);
Bram Moolenaar65b0d162022-12-13 18:43:22 +00004543 int arg_set = tv->v_type != VAR_UNKNOWN
4544 && !(tv->v_type == VAR_SPECIAL
4545 && tv->vval.v_number == VVAL_NONE);
4546 if (iptr->isn_type == ISN_JUMP_IF_ARG_SET ? arg_set : !arg_set)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004547 ectx->ec_iidx = iptr->isn_arg.jumparg.jump_where;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02004548 break;
4549
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004550 // top of a for loop
4551 case ISN_FOR:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00004552 if (execute_for(iptr, ectx) == FAIL)
4553 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004554 break;
4555
Bram Moolenaarb46c0832022-09-15 17:19:37 +01004556 // end of a for or while loop
4557 case ISN_ENDLOOP:
4558 if (execute_endloop(iptr, ectx) == FAIL)
4559 goto theend;
4560 break;
4561
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004562 // start of ":try" block
4563 case ISN_TRY:
4564 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01004565 trycmd_T *trycmd = NULL;
4566
Bram Moolenaar35578162021-08-02 19:10:38 +02004567 if (GA_GROW_FAILS(&ectx->ec_trystack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004568 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004569 trycmd = ((trycmd_T *)ectx->ec_trystack.ga_data)
4570 + ectx->ec_trystack.ga_len;
4571 ++ectx->ec_trystack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004572 ++trylevel;
Bram Moolenaar8d4be892021-02-13 18:33:02 +01004573 CLEAR_POINTER(trycmd);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004574 trycmd->tcd_frame_idx = ectx->ec_frame_idx;
4575 trycmd->tcd_stack_len = ectx->ec_stack.ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01004576 trycmd->tcd_catch_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00004577 iptr->isn_arg.tryref.try_ref->try_catch;
Bram Moolenaara91a7132021-03-25 21:12:15 +01004578 trycmd->tcd_finally_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00004579 iptr->isn_arg.tryref.try_ref->try_finally;
Bram Moolenaara91a7132021-03-25 21:12:15 +01004580 trycmd->tcd_endtry_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00004581 iptr->isn_arg.tryref.try_ref->try_endtry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004582 }
4583 break;
4584
4585 case ISN_PUSHEXC:
4586 if (current_exception == NULL)
4587 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004588 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004589 iemsg("Evaluating catch while current_exception is NULL");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004590 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004591 }
Bram Moolenaar35578162021-08-02 19:10:38 +02004592 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004593 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004594 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004595 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004596 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02004597 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004598 tv->vval.v_string = vim_strsave(
4599 (char_u *)current_exception->value);
4600 break;
4601
4602 case ISN_CATCH:
4603 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004604 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar06651632022-04-27 17:54:25 +01004605 trycmd_T *trycmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004606
Bram Moolenaar4c137212021-04-19 16:48:48 +02004607 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar06651632022-04-27 17:54:25 +01004608 trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004609 + trystack->ga_len - 1;
Bram Moolenaar06651632022-04-27 17:54:25 +01004610 trycmd->tcd_caught = TRUE;
4611 trycmd->tcd_did_throw = FALSE;
4612
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004613 did_emsg = got_int = did_throw = FALSE;
Bram Moolenaar1430cee2021-01-17 19:20:32 +01004614 force_abort = need_rethrow = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004615 catch_exception(current_exception);
4616 }
4617 break;
4618
Bram Moolenaarc150c092021-02-13 15:02:46 +01004619 case ISN_TRYCONT:
4620 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004621 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004622 trycont_T *trycont = &iptr->isn_arg.trycont;
4623 int i;
4624 trycmd_T *trycmd;
4625 int iidx = trycont->tct_where;
4626
4627 if (trystack->ga_len < trycont->tct_levels)
4628 {
4629 siemsg("TRYCONT: expected %d levels, found %d",
4630 trycont->tct_levels, trystack->ga_len);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004631 goto theend;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004632 }
4633 // Make :endtry jump to any outer try block and the last
4634 // :endtry inside the loop to the loop start.
4635 for (i = trycont->tct_levels; i > 0; --i)
4636 {
4637 trycmd = ((trycmd_T *)trystack->ga_data)
4638 + trystack->ga_len - i;
Bram Moolenaar2e34c342021-03-14 12:13:33 +01004639 // Add one to tcd_cont to be able to jump to
4640 // instruction with index zero.
4641 trycmd->tcd_cont = iidx + 1;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01004642 iidx = trycmd->tcd_finally_idx == 0
4643 ? trycmd->tcd_endtry_idx : trycmd->tcd_finally_idx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004644 }
4645 // jump to :finally or :endtry of current try statement
Bram Moolenaar4c137212021-04-19 16:48:48 +02004646 ectx->ec_iidx = iidx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004647 }
4648 break;
4649
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01004650 case ISN_FINALLY:
4651 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004652 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01004653 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
4654 + trystack->ga_len - 1;
4655
4656 // Reset the index to avoid a return statement jumps here
4657 // again.
4658 trycmd->tcd_finally_idx = 0;
4659 break;
4660 }
4661
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004662 // end of ":try" block
4663 case ISN_ENDTRY:
4664 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004665 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar06651632022-04-27 17:54:25 +01004666 trycmd_T *trycmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004667
Bram Moolenaar06651632022-04-27 17:54:25 +01004668 --trystack->ga_len;
4669 --trylevel;
4670 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len;
4671 if (trycmd->tcd_did_throw)
4672 did_throw = TRUE;
4673 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004674 {
Bram Moolenaar06651632022-04-27 17:54:25 +01004675 // discard the exception
4676 if (caught_stack == current_exception)
4677 caught_stack = caught_stack->caught;
4678 discard_current_exception();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004679 }
Bram Moolenaar06651632022-04-27 17:54:25 +01004680
4681 if (trycmd->tcd_return)
4682 goto func_return;
4683
4684 while (ectx->ec_stack.ga_len > trycmd->tcd_stack_len)
4685 {
4686 --ectx->ec_stack.ga_len;
4687 clear_tv(STACK_TV_BOT(0));
4688 }
4689 if (trycmd->tcd_cont != 0)
4690 // handling :continue: jump to outer try block or
4691 // start of the loop
4692 ectx->ec_iidx = trycmd->tcd_cont - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004693 }
4694 break;
4695
4696 case ISN_THROW:
Bram Moolenaar8f81b222021-01-14 21:47:06 +01004697 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004698 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02004699
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004700 if (trystack->ga_len == 0 && trylevel == 0 && emsg_silent)
4701 {
Bram Moolenaar3ef2e412023-04-30 18:50:48 +01004702 // Throwing an exception while using "silent!" causes
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004703 // the function to abort but not display an error.
4704 tv = STACK_TV_BOT(-1);
4705 clear_tv(tv);
4706 tv->v_type = VAR_NUMBER;
4707 tv->vval.v_number = 0;
4708 goto done;
4709 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004710 --ectx->ec_stack.ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004711 tv = STACK_TV_BOT(0);
4712 if (tv->vval.v_string == NULL
4713 || *skipwhite(tv->vval.v_string) == NUL)
4714 {
4715 vim_free(tv->vval.v_string);
4716 SOURCING_LNUM = iptr->isn_lnum;
4717 emsg(_(e_throw_with_empty_string));
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004718 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004719 }
4720
4721 // Inside a "catch" we need to first discard the caught
4722 // exception.
4723 if (trystack->ga_len > 0)
4724 {
4725 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
4726 + trystack->ga_len - 1;
4727 if (trycmd->tcd_caught && current_exception != NULL)
4728 {
4729 // discard the exception
4730 if (caught_stack == current_exception)
4731 caught_stack = caught_stack->caught;
4732 discard_current_exception();
4733 trycmd->tcd_caught = FALSE;
4734 }
4735 }
4736
Bram Moolenaar90a57162022-02-12 14:23:17 +00004737 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004738 if (throw_exception(tv->vval.v_string, ET_USER, NULL)
4739 == FAIL)
4740 {
4741 vim_free(tv->vval.v_string);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004742 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004743 }
4744 did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004745 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004746 break;
4747
4748 // compare with special values
4749 case ISN_COMPAREBOOL:
4750 case ISN_COMPARESPECIAL:
4751 {
4752 typval_T *tv1 = STACK_TV_BOT(-2);
4753 typval_T *tv2 = STACK_TV_BOT(-1);
4754 varnumber_T arg1 = tv1->vval.v_number;
4755 varnumber_T arg2 = tv2->vval.v_number;
4756 int res;
4757
Bram Moolenaar06651632022-04-27 17:54:25 +01004758 if (iptr->isn_arg.op.op_type == EXPR_EQUAL)
4759 res = arg1 == arg2;
4760 else
4761 res = arg1 != arg2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004762
Bram Moolenaar4c137212021-04-19 16:48:48 +02004763 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004764 tv1->v_type = VAR_BOOL;
4765 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4766 }
4767 break;
4768
Bram Moolenaar7a222242022-03-01 19:23:24 +00004769 case ISN_COMPARENULL:
4770 {
4771 typval_T *tv1 = STACK_TV_BOT(-2);
4772 typval_T *tv2 = STACK_TV_BOT(-1);
4773 int res;
4774
4775 res = typval_compare_null(tv1, tv2);
4776 if (res == MAYBE)
4777 goto on_error;
4778 if (iptr->isn_arg.op.op_type == EXPR_NEQUAL)
4779 res = !res;
4780 clear_tv(tv1);
4781 clear_tv(tv2);
4782 --ectx->ec_stack.ga_len;
4783 tv1->v_type = VAR_BOOL;
4784 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4785 }
4786 break;
4787
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004788 // Operation with two number arguments
4789 case ISN_OPNR:
4790 case ISN_COMPARENR:
4791 {
4792 typval_T *tv1 = STACK_TV_BOT(-2);
4793 typval_T *tv2 = STACK_TV_BOT(-1);
4794 varnumber_T arg1 = tv1->vval.v_number;
4795 varnumber_T arg2 = tv2->vval.v_number;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004796 varnumber_T res = 0;
4797 int div_zero = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004798
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004799 if (iptr->isn_arg.op.op_type == EXPR_LSHIFT
4800 || iptr->isn_arg.op.op_type == EXPR_RSHIFT)
4801 {
4802 if (arg2 < 0)
4803 {
4804 SOURCING_LNUM = iptr->isn_lnum;
dundargocc57b5bc2022-11-02 13:30:51 +00004805 emsg(_(e_bitshift_ops_must_be_positive));
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004806 goto on_error;
4807 }
4808 }
4809
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004810 switch (iptr->isn_arg.op.op_type)
4811 {
4812 case EXPR_MULT: res = arg1 * arg2; break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004813 case EXPR_DIV: if (arg2 == 0)
4814 div_zero = TRUE;
4815 else
4816 res = arg1 / arg2;
4817 break;
4818 case EXPR_REM: if (arg2 == 0)
4819 div_zero = TRUE;
4820 else
4821 res = arg1 % arg2;
4822 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004823 case EXPR_SUB: res = arg1 - arg2; break;
4824 case EXPR_ADD: res = arg1 + arg2; break;
4825
4826 case EXPR_EQUAL: res = arg1 == arg2; break;
4827 case EXPR_NEQUAL: res = arg1 != arg2; break;
4828 case EXPR_GREATER: res = arg1 > arg2; break;
4829 case EXPR_GEQUAL: res = arg1 >= arg2; break;
4830 case EXPR_SMALLER: res = arg1 < arg2; break;
4831 case EXPR_SEQUAL: res = arg1 <= arg2; break;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004832 case EXPR_LSHIFT: if (arg2 > MAX_LSHIFT_BITS)
4833 res = 0;
4834 else
Bram Moolenaar68e64d22022-05-22 22:07:52 +01004835 res = (uvarnumber_T)arg1 << arg2;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004836 break;
4837 case EXPR_RSHIFT: if (arg2 > MAX_LSHIFT_BITS)
4838 res = 0;
4839 else
Bram Moolenaar338bf582022-05-22 20:16:32 +01004840 res = (uvarnumber_T)arg1 >> arg2;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004841 break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004842 default: break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004843 }
4844
Bram Moolenaar4c137212021-04-19 16:48:48 +02004845 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004846 if (iptr->isn_type == ISN_COMPARENR)
4847 {
4848 tv1->v_type = VAR_BOOL;
4849 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4850 }
4851 else
4852 tv1->vval.v_number = res;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004853 if (div_zero)
4854 {
4855 SOURCING_LNUM = iptr->isn_lnum;
4856 emsg(_(e_divide_by_zero));
4857 goto on_error;
4858 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004859 }
4860 break;
4861
4862 // Computation with two float arguments
4863 case ISN_OPFLOAT:
4864 case ISN_COMPAREFLOAT:
4865 {
4866 typval_T *tv1 = STACK_TV_BOT(-2);
4867 typval_T *tv2 = STACK_TV_BOT(-1);
4868 float_T arg1 = tv1->vval.v_float;
4869 float_T arg2 = tv2->vval.v_float;
4870 float_T res = 0;
4871 int cmp = FALSE;
4872
4873 switch (iptr->isn_arg.op.op_type)
4874 {
4875 case EXPR_MULT: res = arg1 * arg2; break;
4876 case EXPR_DIV: res = arg1 / arg2; break;
4877 case EXPR_SUB: res = arg1 - arg2; break;
4878 case EXPR_ADD: res = arg1 + arg2; break;
4879
4880 case EXPR_EQUAL: cmp = arg1 == arg2; break;
4881 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
4882 case EXPR_GREATER: cmp = arg1 > arg2; break;
4883 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
4884 case EXPR_SMALLER: cmp = arg1 < arg2; break;
4885 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
4886 default: cmp = 0; break;
4887 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004888 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004889 if (iptr->isn_type == ISN_COMPAREFLOAT)
4890 {
4891 tv1->v_type = VAR_BOOL;
4892 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
4893 }
4894 else
4895 tv1->vval.v_float = res;
4896 }
4897 break;
4898
4899 case ISN_COMPARELIST:
Bram Moolenaar265f8112021-12-19 12:33:05 +00004900 case ISN_COMPAREDICT:
4901 case ISN_COMPAREFUNC:
4902 case ISN_COMPARESTRING:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004903 case ISN_COMPAREBLOB:
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00004904 case ISN_COMPARECLASS:
4905 case ISN_COMPAREOBJECT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004906 {
4907 typval_T *tv1 = STACK_TV_BOT(-2);
4908 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar265f8112021-12-19 12:33:05 +00004909 exprtype_T exprtype = iptr->isn_arg.op.op_type;
4910 int ic = iptr->isn_arg.op.op_ic;
4911 int res = FALSE;
4912 int status = OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004913
Bram Moolenaar265f8112021-12-19 12:33:05 +00004914 SOURCING_LNUM = iptr->isn_lnum;
4915 if (iptr->isn_type == ISN_COMPARELIST)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004916 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00004917 status = typval_compare_list(tv1, tv2,
4918 exprtype, ic, &res);
4919 }
4920 else if (iptr->isn_type == ISN_COMPAREDICT)
4921 {
4922 status = typval_compare_dict(tv1, tv2,
4923 exprtype, ic, &res);
4924 }
4925 else if (iptr->isn_type == ISN_COMPAREFUNC)
4926 {
4927 status = typval_compare_func(tv1, tv2,
4928 exprtype, ic, &res);
4929 }
4930 else if (iptr->isn_type == ISN_COMPARESTRING)
4931 {
4932 status = typval_compare_string(tv1, tv2,
4933 exprtype, ic, &res);
4934 }
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00004935 else if (iptr->isn_type == ISN_COMPAREBLOB)
Bram Moolenaar265f8112021-12-19 12:33:05 +00004936 {
4937 status = typval_compare_blob(tv1, tv2, exprtype, &res);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004938 }
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00004939 else if (iptr->isn_type == ISN_COMPARECLASS)
4940 {
Bram Moolenaar03ff0c62023-01-02 20:38:01 +00004941 status = typval_compare_class(tv1, tv2,
4942 exprtype, FALSE, &res);
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00004943 }
4944 else // ISN_COMPAREOBJECT
4945 {
4946 status = typval_compare_object(tv1, tv2,
Bram Moolenaar03ff0c62023-01-02 20:38:01 +00004947 exprtype, FALSE, &res);
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00004948 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004949 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004950 clear_tv(tv1);
4951 clear_tv(tv2);
4952 tv1->v_type = VAR_BOOL;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004953 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4954 if (status == FAIL)
4955 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004956 }
4957 break;
4958
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004959 case ISN_COMPAREANY:
4960 {
4961 typval_T *tv1 = STACK_TV_BOT(-2);
4962 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar657137c2021-01-09 15:45:23 +01004963 exprtype_T exprtype = iptr->isn_arg.op.op_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004964 int ic = iptr->isn_arg.op.op_ic;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004965 int status;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004966
Bram Moolenaareb26f432020-09-14 16:50:05 +02004967 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004968 status = typval_compare(tv1, tv2, exprtype, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004969 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004970 --ectx->ec_stack.ga_len;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004971 if (status == FAIL)
4972 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004973 }
4974 break;
4975
4976 case ISN_ADDLIST:
4977 case ISN_ADDBLOB:
4978 {
4979 typval_T *tv1 = STACK_TV_BOT(-2);
4980 typval_T *tv2 = STACK_TV_BOT(-1);
4981
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004982 // add two lists or blobs
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004983 if (iptr->isn_type == ISN_ADDLIST)
Bram Moolenaar07802042021-09-09 23:01:14 +02004984 {
4985 if (iptr->isn_arg.op.op_type == EXPR_APPEND
4986 && tv1->vval.v_list != NULL)
4987 list_extend(tv1->vval.v_list, tv2->vval.v_list,
4988 NULL);
4989 else
4990 eval_addlist(tv1, tv2);
4991 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004992 else
4993 eval_addblob(tv1, tv2);
4994 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004995 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004996 }
4997 break;
4998
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004999 case ISN_LISTAPPEND:
5000 {
5001 typval_T *tv1 = STACK_TV_BOT(-2);
5002 typval_T *tv2 = STACK_TV_BOT(-1);
5003 list_T *l = tv1->vval.v_list;
5004
5005 // add an item to a list
Bram Moolenaar1f4a3452022-01-01 18:29:21 +00005006 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02005007 if (l == NULL)
5008 {
Bram Moolenaar1dcae592020-10-19 19:02:42 +02005009 emsg(_(e_cannot_add_to_null_list));
5010 goto on_error;
5011 }
Bram Moolenaar1f4a3452022-01-01 18:29:21 +00005012 if (value_check_lock(l->lv_lock, NULL, FALSE))
5013 goto on_error;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02005014 if (list_append_tv(l, tv2) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005015 goto theend;
Bram Moolenaar955347c2020-10-19 23:01:46 +02005016 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005017 --ectx->ec_stack.ga_len;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02005018 }
5019 break;
5020
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02005021 case ISN_BLOBAPPEND:
5022 {
5023 typval_T *tv1 = STACK_TV_BOT(-2);
5024 typval_T *tv2 = STACK_TV_BOT(-1);
5025 blob_T *b = tv1->vval.v_blob;
5026 int error = FALSE;
5027 varnumber_T n;
5028
5029 // add a number to a blob
5030 if (b == NULL)
5031 {
5032 SOURCING_LNUM = iptr->isn_lnum;
5033 emsg(_(e_cannot_add_to_null_blob));
5034 goto on_error;
5035 }
5036 n = tv_get_number_chk(tv2, &error);
5037 if (error)
5038 goto on_error;
5039 ga_append(&b->bv_ga, (int)n);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005040 --ectx->ec_stack.ga_len;
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02005041 }
5042 break;
5043
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005044 // Computation with two arguments of unknown type
5045 case ISN_OPANY:
5046 {
5047 typval_T *tv1 = STACK_TV_BOT(-2);
5048 typval_T *tv2 = STACK_TV_BOT(-1);
5049 varnumber_T n1, n2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005050 float_T f1 = 0, f2 = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005051 int error = FALSE;
5052
5053 if (iptr->isn_arg.op.op_type == EXPR_ADD)
5054 {
5055 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
5056 {
5057 eval_addlist(tv1, tv2);
5058 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005059 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005060 break;
5061 }
5062 else if (tv1->v_type == VAR_BLOB
5063 && tv2->v_type == VAR_BLOB)
5064 {
5065 eval_addblob(tv1, tv2);
5066 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005067 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005068 break;
5069 }
5070 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005071 if (tv1->v_type == VAR_FLOAT)
5072 {
5073 f1 = tv1->vval.v_float;
5074 n1 = 0;
5075 }
5076 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005077 {
Bram Moolenaarf665e972020-12-05 19:17:16 +01005078 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005079 n1 = tv_get_number_chk(tv1, &error);
5080 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02005081 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005082 if (tv2->v_type == VAR_FLOAT)
5083 f1 = n1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005084 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005085 if (tv2->v_type == VAR_FLOAT)
5086 {
5087 f2 = tv2->vval.v_float;
5088 n2 = 0;
5089 }
5090 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005091 {
5092 n2 = tv_get_number_chk(tv2, &error);
5093 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02005094 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005095 if (tv1->v_type == VAR_FLOAT)
5096 f2 = n2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005097 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005098 // if there is a float on either side the result is a float
5099 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
5100 {
5101 switch (iptr->isn_arg.op.op_type)
5102 {
5103 case EXPR_MULT: f1 = f1 * f2; break;
5104 case EXPR_DIV: f1 = f1 / f2; break;
5105 case EXPR_SUB: f1 = f1 - f2; break;
5106 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005107 default: SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005108 emsg(_(e_cannot_use_percent_with_float));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02005109 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005110 }
5111 clear_tv(tv1);
5112 clear_tv(tv2);
5113 tv1->v_type = VAR_FLOAT;
5114 tv1->vval.v_float = f1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005115 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005116 }
5117 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005118 {
Bram Moolenaarb1f28572021-01-21 13:03:20 +01005119 int failed = FALSE;
5120
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005121 switch (iptr->isn_arg.op.op_type)
5122 {
5123 case EXPR_MULT: n1 = n1 * n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01005124 case EXPR_DIV: n1 = num_divide(n1, n2, &failed);
5125 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01005126 goto on_error;
5127 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005128 case EXPR_SUB: n1 = n1 - n2; break;
5129 case EXPR_ADD: n1 = n1 + n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01005130 default: n1 = num_modulus(n1, n2, &failed);
5131 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01005132 goto on_error;
5133 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005134 }
5135 clear_tv(tv1);
5136 clear_tv(tv2);
5137 tv1->v_type = VAR_NUMBER;
5138 tv1->vval.v_number = n1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005139 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005140 }
5141 }
5142 break;
5143
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005144 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005145 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005146 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005147 int is_slice = iptr->isn_type == ISN_STRSLICE;
5148 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005149 char_u *res;
5150
5151 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005152 // string slice: string is at stack-3, first index at
5153 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005154 if (is_slice)
5155 {
5156 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005157 n1 = tv->vval.v_number;
5158 }
5159
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005160 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005161 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005162
Bram Moolenaar4c137212021-04-19 16:48:48 +02005163 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005164 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005165 if (is_slice)
5166 // Slice: Select the characters from the string
Bram Moolenaar6601b622021-01-13 21:47:15 +01005167 res = string_slice(tv->vval.v_string, n1, n2, FALSE);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005168 else
5169 // Index: The resulting variable is a string of a
Bram Moolenaar0289a092021-03-14 18:40:19 +01005170 // single character (including composing characters).
5171 // If the index is too big or negative the result is
5172 // empty.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005173 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005174 vim_free(tv->vval.v_string);
5175 tv->vval.v_string = res;
5176 }
5177 break;
5178
5179 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02005180 case ISN_LISTSLICE:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02005181 case ISN_BLOBINDEX:
5182 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005183 {
Bram Moolenaarcfc30232021-04-11 20:26:34 +02005184 int is_slice = iptr->isn_type == ISN_LISTSLICE
5185 || iptr->isn_type == ISN_BLOBSLICE;
5186 int is_blob = iptr->isn_type == ISN_BLOBINDEX
5187 || iptr->isn_type == ISN_BLOBSLICE;
Bram Moolenaared591872020-08-15 22:14:53 +02005188 varnumber_T n1, n2;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02005189 typval_T *val_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005190
5191 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02005192 // list slice: list is at stack-3, indexes at stack-2 and
5193 // stack-1
Bram Moolenaarcfc30232021-04-11 20:26:34 +02005194 // Same for blob.
5195 val_tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005196
5197 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02005198 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005199 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02005200
5201 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005202 {
Bram Moolenaared591872020-08-15 22:14:53 +02005203 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02005204 n1 = tv->vval.v_number;
5205 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005206 }
Bram Moolenaared591872020-08-15 22:14:53 +02005207
Bram Moolenaar4c137212021-04-19 16:48:48 +02005208 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02005209 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02005210 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02005211 if (is_blob)
5212 {
5213 if (blob_slice_or_index(val_tv->vval.v_blob, is_slice,
5214 n1, n2, FALSE, tv) == FAIL)
5215 goto on_error;
5216 }
5217 else
5218 {
5219 if (list_slice_or_index(val_tv->vval.v_list, is_slice,
5220 n1, n2, FALSE, tv, TRUE) == FAIL)
5221 goto on_error;
5222 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005223 }
5224 break;
5225
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005226 case ISN_ANYINDEX:
5227 case ISN_ANYSLICE:
5228 {
5229 int is_slice = iptr->isn_type == ISN_ANYSLICE;
5230 typval_T *var1, *var2;
5231 int res;
5232
5233 // index: composite is at stack-2, index at stack-1
5234 // slice: composite is at stack-3, indexes at stack-2 and
5235 // stack-1
5236 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02005237 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005238 if (check_can_index(tv, TRUE, TRUE) == FAIL)
5239 goto on_error;
5240 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
5241 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
Bram Moolenaar6601b622021-01-13 21:47:15 +01005242 res = eval_index_inner(tv, is_slice, var1, var2,
5243 FALSE, NULL, -1, TRUE);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005244 clear_tv(var1);
5245 if (is_slice)
5246 clear_tv(var2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005247 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005248 if (res == FAIL)
5249 goto on_error;
5250 }
5251 break;
5252
Bram Moolenaar9af78762020-06-16 11:34:42 +02005253 case ISN_SLICE:
5254 {
5255 list_T *list;
5256 int count = iptr->isn_arg.number;
5257
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02005258 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02005259 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02005260 list = tv->vval.v_list;
5261
5262 // no error for short list, expect it to be checked earlier
5263 if (list != NULL && list->lv_len >= count)
5264 {
5265 list_T *newlist = list_slice(list,
5266 count, list->lv_len - 1);
5267
5268 if (newlist != NULL)
5269 {
5270 list_unref(list);
5271 tv->vval.v_list = newlist;
5272 ++newlist->lv_refcount;
5273 }
5274 }
5275 }
5276 break;
5277
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005278 case ISN_GETITEM:
5279 {
5280 listitem_T *li;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02005281 getitem_T *gi = &iptr->isn_arg.getitem;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005282
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02005283 // Get list item: list is at stack-1, push item.
5284 // List type and length is checked for when compiling.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02005285 tv = STACK_TV_BOT(-1 - gi->gi_with_op);
5286 li = list_find(tv->vval.v_list, gi->gi_index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005287
Bram Moolenaar35578162021-08-02 19:10:38 +02005288 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005289 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005290 ++ectx->ec_stack.ga_len;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005291 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
Bram Moolenaarf785aa12021-02-11 21:19:34 +01005292
5293 // Useful when used in unpack assignment. Reset at
5294 // ISN_DROP.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02005295 ectx->ec_where.wt_index = gi->gi_index + 1;
LemonBoyc5d27442023-08-19 13:02:35 +02005296 ectx->ec_where.wt_kind = WT_VARIABLE;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005297 }
5298 break;
5299
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005300 case ISN_MEMBER:
5301 {
5302 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005303 char_u *key;
5304 dictitem_T *di;
5305
5306 // dict member: dict is at stack-2, key at stack-1
5307 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02005308 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005309 dict = tv->vval.v_dict;
5310
5311 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02005312 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005313 key = tv->vval.v_string;
Bram Moolenaar086fc9a2020-10-30 18:33:02 +01005314 if (key == NULL)
5315 key = (char_u *)"";
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02005316
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005317 if ((di = dict_find(dict, key, -1)) == NULL)
5318 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005319 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00005320 semsg(_(e_key_not_present_in_dictionary_str), key);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01005321
5322 // If :silent! is used we will continue, make sure the
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005323 // stack contents makes sense and the dict stack is
5324 // updated.
Bram Moolenaar4029cab2020-12-05 18:13:27 +01005325 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005326 --ectx->ec_stack.ga_len;
Bram Moolenaar4029cab2020-12-05 18:13:27 +01005327 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005328 (void) dict_stack_save(tv);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01005329 tv->v_type = VAR_NUMBER;
5330 tv->vval.v_number = 0;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01005331 goto on_fatal_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005332 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005333 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005334 --ectx->ec_stack.ga_len;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005335 // Put the dict used on the dict stack, it might be used by
5336 // a dict function later.
Bram Moolenaar50788ef2020-07-05 16:51:26 +02005337 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005338 if (dict_stack_save(tv) == FAIL)
5339 goto on_fatal_error;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02005340 copy_tv(&di->di_tv, tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005341 }
5342 break;
5343
5344 // dict member with string key
5345 case ISN_STRINGMEMBER:
5346 {
5347 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005348 dictitem_T *di;
5349
5350 tv = STACK_TV_BOT(-1);
5351 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
5352 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005353 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005354 emsg(_(e_dictionary_required));
Bram Moolenaard032f342020-07-18 18:13:02 +02005355 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005356 }
5357 dict = tv->vval.v_dict;
5358
5359 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
5360 == NULL)
5361 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005362 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00005363 semsg(_(e_key_not_present_in_dictionary_str),
Bram Moolenaar381692b2022-02-02 20:01:27 +00005364 iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02005365 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005366 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005367 // Put the dict used on the dict stack, it might be used by
5368 // a dict function later.
5369 if (dict_stack_save(tv) == FAIL)
5370 goto on_fatal_error;
5371
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005372 copy_tv(&di->di_tv, tv);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005373 }
5374 break;
5375
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005376 case ISN_GET_OBJ_MEMBER:
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00005377 case ISN_GET_ITF_MEMBER:
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005378 {
5379 tv = STACK_TV_BOT(-1);
5380 if (tv->v_type != VAR_OBJECT)
5381 {
5382 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaard0200c82023-01-28 15:19:40 +00005383 object_required_error(tv);
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005384 goto on_error;
5385 }
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00005386
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005387 object_T *obj = tv->vval.v_object;
Bram Moolenaarc3f971f2023-03-02 17:38:33 +00005388 if (obj == NULL)
5389 {
5390 SOURCING_LNUM = iptr->isn_lnum;
5391 emsg(_(e_using_null_object));
5392 goto on_error;
5393 }
5394
Ernie Rael18143d32023-09-04 22:30:41 +02005395 int is_static = iptr->isn_arg.classmember.cm_static;
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00005396 int idx;
5397 if (iptr->isn_type == ISN_GET_OBJ_MEMBER)
Ernie Rael18143d32023-09-04 22:30:41 +02005398 idx = iptr->isn_arg.classmember.cm_idx;
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00005399 else
5400 {
5401 idx = iptr->isn_arg.classmember.cm_idx;
5402 // convert the interface index to the object index
5403 idx = object_index_from_itf_index(
Ernie Rael18143d32023-09-04 22:30:41 +02005404 iptr->isn_arg.classmember.cm_class,
5405 FALSE, idx, obj->obj_class, is_static);
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00005406 }
5407
Ernie Rael18143d32023-09-04 22:30:41 +02005408 // The members are located right after the object struct.
5409 typval_T *mtv;
5410 if (is_static)
5411 mtv = &obj->obj_class->class_members_tv[idx];
5412 else
5413 mtv = ((typval_T *)(obj + 1)) + idx;
Bram Moolenaar590162c2022-12-24 21:24:06 +00005414 copy_tv(mtv, tv);
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005415
5416 // Unreference the object after getting the member, it may
5417 // be freed.
5418 object_unref(obj);
5419 }
5420 break;
5421
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005422 case ISN_STORE_THIS:
5423 {
5424 int idx = iptr->isn_arg.number;
5425 object_T *obj = STACK_TV_VAR(0)->vval.v_object;
5426 // the members are located right after the object struct
5427 typval_T *mtv = ((typval_T *)(obj + 1)) + idx;
5428 clear_tv(mtv);
5429 *mtv = *STACK_TV_BOT(-1);
5430 --ectx->ec_stack.ga_len;
5431 }
5432 break;
5433
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005434 case ISN_CLEARDICT:
5435 dict_stack_drop();
5436 break;
5437
5438 case ISN_USEDICT:
5439 {
5440 typval_T *dict_tv = dict_stack_get_tv();
5441
5442 // Turn "dict.Func" into a partial for "Func" bound to
5443 // "dict". Don't do this when "Func" is already a partial
5444 // that was bound explicitly (pt_auto is FALSE).
5445 tv = STACK_TV_BOT(-1);
5446 if (dict_tv != NULL
5447 && dict_tv->v_type == VAR_DICT
5448 && dict_tv->vval.v_dict != NULL
5449 && (tv->v_type == VAR_FUNC
5450 || (tv->v_type == VAR_PARTIAL
5451 && (tv->vval.v_partial->pt_auto
5452 || tv->vval.v_partial->pt_dict == NULL))))
5453 dict_tv->vval.v_dict =
5454 make_partial(dict_tv->vval.v_dict, tv);
5455 dict_stack_drop();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005456 }
5457 break;
5458
5459 case ISN_NEGATENR:
5460 tv = STACK_TV_BOT(-1);
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00005461 // CHECKTYPE should have checked the variable type
Bram Moolenaarc58164c2020-03-29 18:40:30 +02005462 if (tv->v_type == VAR_FLOAT)
5463 tv->vval.v_float = -tv->vval.v_float;
5464 else
Bram Moolenaarc58164c2020-03-29 18:40:30 +02005465 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005466 break;
5467
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005468 case ISN_CHECKTYPE:
5469 {
5470 checktype_T *ct = &iptr->isn_arg.type;
Bram Moolenaarb1040dc2022-05-18 11:00:48 +01005471 int r;
LemonBoyc5d27442023-08-19 13:02:35 +02005472 where_T where = WHERE_INIT;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005473
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01005474 tv = STACK_TV_BOT((int)ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02005475 SOURCING_LNUM = iptr->isn_lnum;
LemonBoyc5d27442023-08-19 13:02:35 +02005476 if (ct->ct_arg_idx > 0)
5477 {
5478 where.wt_index = ct->ct_arg_idx;
5479 where.wt_kind = ct->ct_is_var ? WT_VARIABLE : WT_ARGUMENT;
LemonBoyc5d27442023-08-19 13:02:35 +02005480 }
LemonBoyf244b2f2023-08-19 16:02:04 +02005481 where.wt_func_name = ectx->ec_where.wt_func_name;
LemonBoyc5d27442023-08-19 13:02:35 +02005482 r = check_typval_type(ct->ct_type, tv, where);
Bram Moolenaarb1040dc2022-05-18 11:00:48 +01005483 if (r == FAIL)
5484 goto on_error;
Bram Moolenaar5e654232020-09-16 15:22:00 +02005485
5486 // number 0 is FALSE, number 1 is TRUE
5487 if (tv->v_type == VAR_NUMBER
5488 && ct->ct_type->tt_type == VAR_BOOL
5489 && (tv->vval.v_number == 0
5490 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005491 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02005492 tv->v_type = VAR_BOOL;
5493 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02005494 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005495 }
5496 }
5497 break;
5498
Bram Moolenaar9af78762020-06-16 11:34:42 +02005499 case ISN_CHECKLEN:
5500 {
5501 int min_len = iptr->isn_arg.checklen.cl_min_len;
5502 list_T *list = NULL;
5503
5504 tv = STACK_TV_BOT(-1);
5505 if (tv->v_type == VAR_LIST)
5506 list = tv->vval.v_list;
5507 if (list == NULL || list->lv_len < min_len
5508 || (list->lv_len > min_len
5509 && !iptr->isn_arg.checklen.cl_more_OK))
5510 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005511 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005512 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02005513 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02005514 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02005515 }
5516 }
5517 break;
5518
Bram Moolenaaraa210a32021-01-02 15:41:03 +01005519 case ISN_SETTYPE:
Bram Moolenaar381692b2022-02-02 20:01:27 +00005520 set_tv_type(STACK_TV_BOT(-1), iptr->isn_arg.type.ct_type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01005521 break;
5522
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005523 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005524 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005525 {
5526 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005527 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005528
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005529 if (iptr->isn_type == ISN_2BOOL)
5530 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005531 tv = STACK_TV_BOT(iptr->isn_arg.tobool.offset);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005532 n = tv2bool(tv);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005533 if (iptr->isn_arg.tobool.invert)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005534 n = !n;
5535 }
5536 else
5537 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005538 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005539 SOURCING_LNUM = iptr->isn_lnum;
5540 n = tv_get_bool_chk(tv, &error);
5541 if (error)
5542 goto on_error;
5543 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005544 clear_tv(tv);
5545 tv->v_type = VAR_BOOL;
5546 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
5547 }
5548 break;
5549
5550 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02005551 case ISN_2STRING_ANY:
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01005552 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005553 if (do_2string(STACK_TV_BOT(iptr->isn_arg.tostring.offset),
5554 iptr->isn_type == ISN_2STRING_ANY,
5555 iptr->isn_arg.tostring.tolerant) == FAIL)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01005556 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005557 break;
5558
Bram Moolenaar08597872020-12-10 19:43:40 +01005559 case ISN_RANGE:
5560 {
5561 exarg_T ea;
5562 char *errormsg;
5563
Bram Moolenaarece0b872021-01-08 20:40:45 +01005564 ea.line2 = 0;
Bram Moolenaard1510ee2021-01-04 16:15:58 +01005565 ea.addr_count = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01005566 ea.addr_type = ADDR_LINES;
5567 ea.cmd = iptr->isn_arg.string;
Bram Moolenaarece0b872021-01-08 20:40:45 +01005568 ea.skip = FALSE;
Bram Moolenaar08597872020-12-10 19:43:40 +01005569 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaarece0b872021-01-08 20:40:45 +01005570 goto on_error;
Bram Moolenaara28639e2021-01-19 22:48:09 +01005571
Bram Moolenaar35578162021-08-02 19:10:38 +02005572 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005573 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005574 ++ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01005575 tv = STACK_TV_BOT(-1);
5576 tv->v_type = VAR_NUMBER;
5577 tv->v_lock = 0;
Bram Moolenaar06651632022-04-27 17:54:25 +01005578 tv->vval.v_number = ea.line2;
Bram Moolenaar08597872020-12-10 19:43:40 +01005579 }
5580 break;
5581
Bram Moolenaarc3516f72020-09-08 22:45:35 +02005582 case ISN_PUT:
5583 {
5584 int regname = iptr->isn_arg.put.put_regname;
5585 linenr_T lnum = iptr->isn_arg.put.put_lnum;
5586 char_u *expr = NULL;
5587 int dir = FORWARD;
5588
Bram Moolenaar08597872020-12-10 19:43:40 +01005589 if (lnum < -2)
5590 {
5591 // line number was put on the stack by ISN_RANGE
5592 tv = STACK_TV_BOT(-1);
5593 curwin->w_cursor.lnum = tv->vval.v_number;
5594 if (lnum == LNUM_VARIABLE_RANGE_ABOVE)
5595 dir = BACKWARD;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005596 --ectx->ec_stack.ga_len;
Bram Moolenaar08597872020-12-10 19:43:40 +01005597 }
5598 else if (lnum == -2)
Bram Moolenaarc3516f72020-09-08 22:45:35 +02005599 // :put! above cursor
5600 dir = BACKWARD;
5601 else if (lnum >= 0)
Bram Moolenaar4e713ba2022-02-07 15:31:37 +00005602 {
5603 curwin->w_cursor.lnum = lnum;
5604 if (lnum == 0)
5605 // check_cursor() below will move to line 1
5606 dir = BACKWARD;
5607 }
Bram Moolenaara28639e2021-01-19 22:48:09 +01005608
5609 if (regname == '=')
5610 {
5611 tv = STACK_TV_BOT(-1);
5612 if (tv->v_type == VAR_STRING)
5613 expr = tv->vval.v_string;
5614 else
5615 {
5616 expr = typval2string(tv, TRUE); // allocates value
5617 clear_tv(tv);
5618 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005619 --ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01005620 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02005621 check_cursor();
5622 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
5623 vim_free(expr);
5624 }
5625 break;
5626
Bram Moolenaar02194d22020-10-24 23:08:38 +02005627 case ISN_CMDMOD:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005628 ectx->ec_funclocal.floc_save_cmdmod = cmdmod;
5629 ectx->ec_funclocal.floc_restore_cmdmod = TRUE;
5630 ectx->ec_funclocal.floc_restore_cmdmod_stacklen =
5631 ectx->ec_stack.ga_len;
Bram Moolenaar02194d22020-10-24 23:08:38 +02005632 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
5633 apply_cmdmod(&cmdmod);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02005634 break;
5635
Bram Moolenaar02194d22020-10-24 23:08:38 +02005636 case ISN_CMDMOD_REV:
5637 // filter regprog is owned by the instruction, don't free it
5638 cmdmod.cmod_filter_regmatch.regprog = NULL;
5639 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005640 cmdmod = ectx->ec_funclocal.floc_save_cmdmod;
5641 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02005642 break;
5643
Bram Moolenaar792f7862020-11-23 08:31:18 +01005644 case ISN_UNPACK:
5645 {
5646 int count = iptr->isn_arg.unpack.unp_count;
5647 int semicolon = iptr->isn_arg.unpack.unp_semicolon;
5648 list_T *l;
5649 listitem_T *li;
5650 int i;
5651
5652 // Check there is a valid list to unpack.
5653 tv = STACK_TV_BOT(-1);
5654 if (tv->v_type != VAR_LIST)
5655 {
5656 SOURCING_LNUM = iptr->isn_lnum;
5657 emsg(_(e_for_argument_must_be_sequence_of_lists));
5658 goto on_error;
5659 }
5660 l = tv->vval.v_list;
5661 if (l == NULL
5662 || l->lv_len < (semicolon ? count - 1 : count))
5663 {
5664 SOURCING_LNUM = iptr->isn_lnum;
5665 emsg(_(e_list_value_does_not_have_enough_items));
5666 goto on_error;
5667 }
5668 else if (!semicolon && l->lv_len > count)
5669 {
5670 SOURCING_LNUM = iptr->isn_lnum;
5671 emsg(_(e_list_value_has_more_items_than_targets));
5672 goto on_error;
5673 }
5674
5675 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar35578162021-08-02 19:10:38 +02005676 if (GA_GROW_FAILS(&ectx->ec_stack, count - 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005677 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005678 ectx->ec_stack.ga_len += count - 1;
Bram Moolenaar792f7862020-11-23 08:31:18 +01005679
5680 // Variable after semicolon gets a list with the remaining
5681 // items.
5682 if (semicolon)
5683 {
5684 list_T *rem_list =
5685 list_alloc_with_items(l->lv_len - count + 1);
5686
5687 if (rem_list == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005688 goto theend;
Bram Moolenaar792f7862020-11-23 08:31:18 +01005689 tv = STACK_TV_BOT(-count);
5690 tv->vval.v_list = rem_list;
5691 ++rem_list->lv_refcount;
5692 tv->v_lock = 0;
5693 li = l->lv_first;
5694 for (i = 0; i < count - 1; ++i)
5695 li = li->li_next;
5696 for (i = 0; li != NULL; ++i)
5697 {
Bram Moolenaar61efa162022-03-18 13:10:48 +00005698 typval_T tvcopy;
5699
5700 copy_tv(&li->li_tv, &tvcopy);
5701 list_set_item(rem_list, i, &tvcopy);
Bram Moolenaar792f7862020-11-23 08:31:18 +01005702 li = li->li_next;
5703 }
5704 --count;
5705 }
5706
5707 // Produce the values in reverse order, first item last.
5708 li = l->lv_first;
5709 for (i = 0; i < count; ++i)
5710 {
5711 tv = STACK_TV_BOT(-i - 1);
5712 copy_tv(&li->li_tv, tv);
5713 li = li->li_next;
5714 }
5715
5716 list_unref(l);
5717 }
5718 break;
5719
Bram Moolenaarb2049902021-01-24 12:53:53 +01005720 case ISN_PROF_START:
5721 case ISN_PROF_END:
5722 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01005723#ifdef FEAT_PROFILE
Bram Moolenaarca16c602022-09-06 18:57:08 +01005724 funccall_T cookie;
5725 ufunc_T *cur_ufunc =
Bram Moolenaarb2049902021-01-24 12:53:53 +01005726 (((dfunc_T *)def_functions.ga_data)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02005727 + ectx->ec_dfunc_idx)->df_ufunc;
Bram Moolenaarb2049902021-01-24 12:53:53 +01005728
Bram Moolenaarca16c602022-09-06 18:57:08 +01005729 cookie.fc_func = cur_ufunc;
Bram Moolenaarb2049902021-01-24 12:53:53 +01005730 if (iptr->isn_type == ISN_PROF_START)
5731 {
5732 func_line_start(&cookie, iptr->isn_lnum);
5733 // if we get here the instruction is executed
5734 func_line_exec(&cookie);
5735 }
5736 else
5737 func_line_end(&cookie);
Bram Moolenaarf002a412021-01-24 13:34:18 +01005738#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01005739 }
5740 break;
5741
Bram Moolenaare99d4222021-06-13 14:01:26 +02005742 case ISN_DEBUG:
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02005743 handle_debug(iptr, ectx);
Bram Moolenaare99d4222021-06-13 14:01:26 +02005744 break;
5745
Bram Moolenaar389df252020-07-09 21:20:47 +02005746 case ISN_SHUFFLE:
5747 {
Bram Moolenaar792f7862020-11-23 08:31:18 +01005748 typval_T tmp_tv;
5749 int item = iptr->isn_arg.shuffle.shfl_item;
5750 int up = iptr->isn_arg.shuffle.shfl_up;
Bram Moolenaar389df252020-07-09 21:20:47 +02005751
5752 tmp_tv = *STACK_TV_BOT(-item);
5753 for ( ; up > 0 && item > 1; --up)
5754 {
5755 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
5756 --item;
5757 }
5758 *STACK_TV_BOT(-item) = tmp_tv;
5759 }
5760 break;
5761
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005762 case ISN_DROP:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005763 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005764 clear_tv(STACK_TV_BOT(0));
LemonBoyc5d27442023-08-19 13:02:35 +02005765 ectx->ec_where = (where_T)WHERE_INIT;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005766 break;
5767 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02005768 continue;
5769
Bram Moolenaard032f342020-07-18 18:13:02 +02005770func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02005771 // Restore previous function. If the frame pointer is where we started
5772 // then there is none and we are done.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005773 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02005774 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02005775
Bram Moolenaar4c137212021-04-19 16:48:48 +02005776 if (func_return(ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02005777 // only fails when out of memory
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005778 goto theend;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02005779 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02005780
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02005781on_error:
Bram Moolenaaraf0df472020-12-02 20:51:22 +01005782 // Jump here for an error that does not require aborting execution.
Bram Moolenaar56602ba2020-12-05 21:22:08 +01005783 // If "emsg_silent" is set then ignore the error, unless it was set
5784 // when calling the function.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005785 if (did_emsg_cumul + did_emsg == ectx->ec_did_emsg_before
Bram Moolenaar56602ba2020-12-05 21:22:08 +01005786 && emsg_silent && did_emsg_def == 0)
Bram Moolenaarf9041332021-01-21 19:41:16 +01005787 {
5788 // If a sequence of instructions causes an error while ":silent!"
5789 // was used, restore the stack length and jump ahead to restoring
5790 // the cmdmod.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005791 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaarf9041332021-01-21 19:41:16 +01005792 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005793 while (ectx->ec_stack.ga_len
5794 > ectx->ec_funclocal.floc_restore_cmdmod_stacklen)
Bram Moolenaarf9041332021-01-21 19:41:16 +01005795 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005796 --ectx->ec_stack.ga_len;
Bram Moolenaarf9041332021-01-21 19:41:16 +01005797 clear_tv(STACK_TV_BOT(0));
5798 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005799 while (ectx->ec_instr[ectx->ec_iidx].isn_type != ISN_CMDMOD_REV)
5800 ++ectx->ec_iidx;
Bram Moolenaarf9041332021-01-21 19:41:16 +01005801 }
Bram Moolenaarcd030c42020-10-30 21:49:40 +01005802 continue;
Bram Moolenaarf9041332021-01-21 19:41:16 +01005803 }
Bram Moolenaaraf0df472020-12-02 20:51:22 +01005804on_fatal_error:
5805 // Jump here for an error that messes up the stack.
Bram Moolenaar171fb922020-10-28 16:54:47 +01005806 // If we are not inside a try-catch started here, abort execution.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005807 if (trylevel <= ectx->ec_trylevel_at_start)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005808 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005809 }
5810
5811done:
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005812 ret = OK;
5813theend:
Bram Moolenaar58779852022-09-06 18:31:14 +01005814 may_invoke_defer_funcs(ectx);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01005815
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005816 dict_stack_clear(dict_stack_len_at_start);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005817 ectx->ec_trylevel_at_start = save_trylevel_at_start;
5818 return ret;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005819}
5820
5821/*
Bram Moolenaar806a2732022-09-04 15:40:36 +01005822 * Execute the instructions from a VAR_INSTR typval and put the result in
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005823 * "rettv".
5824 * Return OK or FAIL.
5825 */
5826 int
5827exe_typval_instr(typval_T *tv, typval_T *rettv)
5828{
5829 ectx_T *ectx = tv->vval.v_instr->instr_ectx;
5830 isn_T *save_instr = ectx->ec_instr;
5831 int save_iidx = ectx->ec_iidx;
5832 int res;
5833
LemonBoyf3b48952022-05-05 13:53:03 +01005834 // Initialize rettv so that it is safe for caller to invoke clear_tv(rettv)
5835 // even when the compilation fails.
5836 rettv->v_type = VAR_UNKNOWN;
5837
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005838 ectx->ec_instr = tv->vval.v_instr->instr_instr;
5839 res = exec_instructions(ectx);
5840 if (res == OK)
5841 {
5842 *rettv = *STACK_TV_BOT(-1);
5843 --ectx->ec_stack.ga_len;
5844 }
5845
5846 ectx->ec_instr = save_instr;
5847 ectx->ec_iidx = save_iidx;
5848
5849 return res;
5850}
5851
5852/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02005853 * Execute the instructions from an ISN_SUBSTITUTE command, which are in
5854 * "substitute_instr".
5855 */
5856 char_u *
5857exe_substitute_instr(void)
5858{
5859 ectx_T *ectx = substitute_instr->subs_ectx;
5860 isn_T *save_instr = ectx->ec_instr;
5861 int save_iidx = ectx->ec_iidx;
5862 char_u *res;
5863
5864 ectx->ec_instr = substitute_instr->subs_instr;
5865 if (exec_instructions(ectx) == OK)
Bram Moolenaar90193e62021-04-04 20:49:50 +02005866 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005867 typval_T *tv = STACK_TV_BOT(-1);
5868
Bram Moolenaar27523602021-06-05 21:36:19 +02005869 res = typval2string(tv, TRUE);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005870 --ectx->ec_stack.ga_len;
5871 clear_tv(tv);
Bram Moolenaar90193e62021-04-04 20:49:50 +02005872 }
5873 else
5874 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005875 substitute_instr->subs_status = FAIL;
5876 res = vim_strsave((char_u *)"");
Bram Moolenaar90193e62021-04-04 20:49:50 +02005877 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005878
Bram Moolenaar4c137212021-04-19 16:48:48 +02005879 ectx->ec_instr = save_instr;
5880 ectx->ec_iidx = save_iidx;
5881
5882 return res;
5883}
5884
5885/*
5886 * Call a "def" function from old Vim script.
5887 * Return OK or FAIL.
5888 */
5889 int
5890call_def_function(
5891 ufunc_T *ufunc,
5892 int argc_arg, // nr of arguments
5893 typval_T *argv, // arguments
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005894 int flags, // DEF_ flags
Bram Moolenaar4c137212021-04-19 16:48:48 +02005895 partial_T *partial, // optional partial for context
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005896 object_T *object, // object, e.g. for this.Func()
Bram Moolenaar58779852022-09-06 18:31:14 +01005897 funccall_T *funccal,
Bram Moolenaar4c137212021-04-19 16:48:48 +02005898 typval_T *rettv) // return value
5899{
5900 ectx_T ectx; // execution context
5901 int argc = argc_arg;
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005902 int partial_argc = partial == NULL
5903 || (flags & DEF_USE_PT_ARGV) == 0
5904 ? 0 : partial->pt_argc;
5905 int total_argc = argc + partial_argc;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005906 typval_T *tv;
5907 int idx;
5908 int ret = FAIL;
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005909 int defcount = ufunc->uf_args.ga_len - total_argc;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005910 sctx_T save_current_sctx = current_sctx;
5911 int did_emsg_before = did_emsg_cumul + did_emsg;
5912 int save_suppress_errthrow = suppress_errthrow;
5913 msglist_T **saved_msg_list = NULL;
5914 msglist_T *private_msg_list = NULL;
5915 int save_emsg_silent_def = emsg_silent_def;
5916 int save_did_emsg_def = did_emsg_def;
5917 int orig_funcdepth;
Bram Moolenaare99d4222021-06-13 14:01:26 +02005918 int orig_nesting_level = ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005919
5920// Get pointer to item in the stack.
5921#undef STACK_TV
5922#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
5923
5924// Get pointer to item at the bottom of the stack, -1 is the bottom.
5925#undef STACK_TV_BOT
5926#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
5927
5928// Get pointer to a local variable on the stack. Negative for arguments.
5929#undef STACK_TV_VAR
5930#define STACK_TV_VAR(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_frame_idx + STACK_FRAME_SIZE + idx)
5931
5932 if (ufunc->uf_def_status == UF_NOT_COMPILED
5933 || ufunc->uf_def_status == UF_COMPILE_ERROR
Bram Moolenaar139575d2022-03-15 19:29:30 +00005934 || (func_needs_compiling(ufunc, get_compile_type(ufunc))
5935 && compile_def_function(ufunc, FALSE,
5936 get_compile_type(ufunc), NULL) == FAIL))
Bram Moolenaar4c137212021-04-19 16:48:48 +02005937 {
5938 if (did_emsg_cumul + did_emsg == did_emsg_before)
5939 semsg(_(e_function_is_not_compiled_str),
5940 printable_func_name(ufunc));
5941 return FAIL;
5942 }
5943
5944 {
5945 // Check the function was really compiled.
5946 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5947 + ufunc->uf_dfunc_idx;
Bram Moolenaarcc341812022-09-19 15:54:34 +01005948 if (dfunc->df_ufunc == NULL)
5949 {
5950 semsg(_(e_function_was_deleted_str), printable_func_name(ufunc));
5951 return FAIL;
5952 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005953 if (INSTRUCTIONS(dfunc) == NULL)
5954 {
5955 iemsg("using call_def_function() on not compiled function");
5956 return FAIL;
5957 }
5958 }
5959
5960 // If depth of calling is getting too high, don't execute the function.
5961 orig_funcdepth = funcdepth_get();
5962 if (funcdepth_increment() == FAIL)
5963 return FAIL;
5964
5965 CLEAR_FIELD(ectx);
5966 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
5967 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
Bram Moolenaar35578162021-08-02 19:10:38 +02005968 if (GA_GROW_FAILS(&ectx.ec_stack, 20))
Bram Moolenaar4c137212021-04-19 16:48:48 +02005969 {
5970 funcdepth_decrement();
5971 return FAIL;
5972 }
5973 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
5974 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
5975 ectx.ec_did_emsg_before = did_emsg_before;
Bram Moolenaare99d4222021-06-13 14:01:26 +02005976 ++ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005977
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005978 idx = total_argc - ufunc->uf_args.ga_len;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005979 if (idx > 0 && ufunc->uf_va_name == NULL)
5980 {
Matvey Tarasovd14bb1a2022-06-29 13:18:27 +01005981 semsg(NGETTEXT(e_one_argument_too_many, e_nr_arguments_too_many,
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005982 idx), idx);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005983 goto failed_early;
5984 }
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005985 idx = total_argc - ufunc->uf_args.ga_len + ufunc->uf_def_args.ga_len;
Bram Moolenaarc6d71532021-06-05 18:49:38 +02005986 if (idx < 0)
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02005987 {
Matvey Tarasovd14bb1a2022-06-29 13:18:27 +01005988 semsg(NGETTEXT(e_one_argument_too_few, e_nr_arguments_too_few,
Bram Moolenaar98aff652022-09-06 21:02:35 +01005989 -idx), -idx);
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02005990 goto failed_early;
5991 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005992
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005993 // Put values from the partial and arguments on the stack, but no more than
5994 // what the function expects. A lambda can be called with more arguments
5995 // than it uses.
5996 for (idx = 0; idx < total_argc
Bram Moolenaar4c137212021-04-19 16:48:48 +02005997 && (ufunc->uf_va_name != NULL || idx < ufunc->uf_args.ga_len);
5998 ++idx)
5999 {
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01006000 int argv_idx = idx - partial_argc;
6001
6002 tv = idx < partial_argc ? partial->pt_argv + idx : argv + argv_idx;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006003 if (idx >= ufunc->uf_args.ga_len - ufunc->uf_def_args.ga_len
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01006004 && tv->v_type == VAR_SPECIAL
6005 && tv->vval.v_number == VVAL_NONE)
Bram Moolenaar4c137212021-04-19 16:48:48 +02006006 {
6007 // Use the default value.
6008 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
6009 }
6010 else
6011 {
Bram Moolenaar47bba532023-01-20 18:49:46 +00006012 int done = FALSE;
6013 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len)
6014 {
6015 type_T *expected = ufunc->uf_arg_types[idx];
6016 if (expected->tt_type == VAR_FLOAT && tv->v_type == VAR_NUMBER)
6017 {
6018 // When a float is expected and a number was given, convert
6019 // the value.
6020 STACK_TV_BOT(0)->v_type = VAR_FLOAT;
6021 STACK_TV_BOT(0)->v_lock = 0;
6022 STACK_TV_BOT(0)->vval.v_float = tv->vval.v_number;
6023 done = TRUE;
6024 }
6025 else if (check_typval_arg_type(expected, tv,
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01006026 NULL, argv_idx + 1) == FAIL)
Bram Moolenaar47bba532023-01-20 18:49:46 +00006027 goto failed_early;
6028 }
6029 if (!done)
6030 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006031 }
6032 ++ectx.ec_stack.ga_len;
6033 }
6034
6035 // Turn varargs into a list. Empty list if no args.
6036 if (ufunc->uf_va_name != NULL)
6037 {
6038 int vararg_count = argc - ufunc->uf_args.ga_len;
6039
6040 if (vararg_count < 0)
6041 vararg_count = 0;
6042 else
6043 argc -= vararg_count;
6044 if (exe_newlist(vararg_count, &ectx) == FAIL)
6045 goto failed_early;
6046
6047 // Check the type of the list items.
6048 tv = STACK_TV_BOT(-1);
6049 if (ufunc->uf_va_type != NULL
6050 && ufunc->uf_va_type != &t_list_any
6051 && ufunc->uf_va_type->tt_member != &t_any
6052 && tv->vval.v_list != NULL)
6053 {
6054 type_T *expected = ufunc->uf_va_type->tt_member;
6055 listitem_T *li = tv->vval.v_list->lv_first;
6056
6057 for (idx = 0; idx < vararg_count; ++idx)
6058 {
6059 if (check_typval_arg_type(expected, &li->li_tv,
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02006060 NULL, argc + idx + 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02006061 goto failed_early;
6062 li = li->li_next;
6063 }
6064 }
6065
6066 if (defcount > 0)
6067 // Move varargs list to below missing default arguments.
6068 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
6069 --ectx.ec_stack.ga_len;
6070 }
6071
6072 // Make space for omitted arguments, will store default value below.
6073 // Any varargs list goes after them.
6074 if (defcount > 0)
6075 for (idx = 0; idx < defcount; ++idx)
6076 {
6077 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
6078 ++ectx.ec_stack.ga_len;
6079 }
6080 if (ufunc->uf_va_name != NULL)
6081 ++ectx.ec_stack.ga_len;
6082
6083 // Frame pointer points to just after arguments.
6084 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
6085 ectx.ec_initial_frame_idx = ectx.ec_frame_idx;
6086
6087 {
6088 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6089 + ufunc->uf_dfunc_idx;
6090 ufunc_T *base_ufunc = dfunc->df_ufunc;
6091
6092 // "uf_partial" is on the ufunc that "df_ufunc" points to, as is done
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006093 // by copy_lambda_to_global_func().
Bram Moolenaar4c137212021-04-19 16:48:48 +02006094 if (partial != NULL || base_ufunc->uf_partial != NULL)
6095 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006096 ectx.ec_outer_ref = ALLOC_CLEAR_ONE(outer_ref_T);
6097 if (ectx.ec_outer_ref == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02006098 goto failed_early;
6099 if (partial != NULL)
6100 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00006101 outer_T *outer = get_pt_outer(partial);
6102
Bram Moolenaar6d313be2022-09-22 16:36:25 +01006103 if (outer->out_stack == NULL && outer->out_loop_size == 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02006104 {
Bram Moolenaar6d313be2022-09-22 16:36:25 +01006105 // no stack was set
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00006106 if (current_ectx != NULL)
6107 {
6108 if (current_ectx->ec_outer_ref != NULL
6109 && current_ectx->ec_outer_ref->or_outer != NULL)
6110 ectx.ec_outer_ref->or_outer =
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006111 current_ectx->ec_outer_ref->or_outer;
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00006112 }
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01006113 // else: should there be an error here?
Bram Moolenaar4c137212021-04-19 16:48:48 +02006114 }
6115 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006116 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00006117 ectx.ec_outer_ref->or_outer = outer;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006118 ++partial->pt_refcount;
6119 ectx.ec_outer_ref->or_partial = partial;
6120 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006121 }
6122 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006123 {
6124 ectx.ec_outer_ref->or_outer = &base_ufunc->uf_partial->pt_outer;
6125 ++base_ufunc->uf_partial->pt_refcount;
6126 ectx.ec_outer_ref->or_partial = base_ufunc->uf_partial;
6127 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006128 }
6129 }
6130
6131 // dummy frame entries
6132 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
6133 {
6134 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
6135 ++ectx.ec_stack.ga_len;
6136 }
6137
6138 {
6139 // Reserve space for local variables and any closure reference count.
6140 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6141 + ufunc->uf_dfunc_idx;
6142
Bram Moolenaar5cd64792021-12-25 18:23:24 +00006143 // Initialize variables to zero. That avoids having to generate
6144 // initializing instructions for "var nr: number", "var x: any", etc.
Bram Moolenaar4c137212021-04-19 16:48:48 +02006145 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaar5cd64792021-12-25 18:23:24 +00006146 {
6147 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
6148 STACK_TV_VAR(idx)->vval.v_number = 0;
6149 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006150 ectx.ec_stack.ga_len += dfunc->df_varcount;
Bram Moolenaarffdaca92022-12-09 21:41:48 +00006151
6152 if (object != NULL)
6153 {
6154 // the object is always the variable at index zero
6155 tv = STACK_TV_VAR(0);
6156 tv->v_type = VAR_OBJECT;
6157 tv->vval.v_object = object;
6158 }
6159
Bram Moolenaar4c137212021-04-19 16:48:48 +02006160 if (dfunc->df_has_closure)
6161 {
Bram Moolenaarcc341812022-09-19 15:54:34 +01006162 // Initialize the variable that counts how many closures were
6163 // created. This is used in handle_closure_in_use().
Bram Moolenaar4c137212021-04-19 16:48:48 +02006164 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
6165 STACK_TV_VAR(idx)->vval.v_number = 0;
6166 ++ectx.ec_stack.ga_len;
6167 }
6168
6169 ectx.ec_instr = INSTRUCTIONS(dfunc);
6170 }
6171
Bram Moolenaar58779852022-09-06 18:31:14 +01006172 // Store the execution context in funccal, used by invoke_all_defer().
6173 if (funccal != NULL)
6174 funccal->fc_ectx = &ectx;
6175
Bram Moolenaar4c137212021-04-19 16:48:48 +02006176 // Following errors are in the function, not the caller.
6177 // Commands behave like vim9script.
6178 estack_push_ufunc(ufunc, 1);
6179 current_sctx = ufunc->uf_script_ctx;
6180 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
6181
6182 // Use a specific location for storing error messages to be converted to an
6183 // exception.
6184 saved_msg_list = msg_list;
6185 msg_list = &private_msg_list;
6186
6187 // Do turn errors into exceptions.
6188 suppress_errthrow = FALSE;
6189
6190 // Do not delete the function while executing it.
6191 ++ufunc->uf_calls;
6192
6193 // When ":silent!" was used before calling then we still abort the
6194 // function. If ":silent!" is used in the function then we don't.
6195 emsg_silent_def = emsg_silent;
6196 did_emsg_def = 0;
6197
LemonBoyc5d27442023-08-19 13:02:35 +02006198 ectx.ec_where = (where_T)WHERE_INIT;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006199
Bram Moolenaar5800c792022-09-22 17:34:01 +01006200 /*
6201 * Execute the instructions until done.
6202 */
Bram Moolenaar4c137212021-04-19 16:48:48 +02006203 ret = exec_instructions(&ectx);
6204 if (ret == OK)
6205 {
6206 // function finished, get result from the stack.
6207 if (ufunc->uf_ret_type == &t_void)
6208 {
6209 rettv->v_type = VAR_VOID;
6210 }
6211 else
6212 {
6213 tv = STACK_TV_BOT(-1);
6214 *rettv = *tv;
6215 tv->v_type = VAR_UNKNOWN;
6216 }
6217 }
6218
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01006219 // When failed need to unwind the call stack.
Bram Moolenaar58779852022-09-06 18:31:14 +01006220 unwind_def_callstack(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02006221
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02006222 // Deal with any remaining closures, they may be in use somewhere.
6223 if (ectx.ec_funcrefs.ga_len > 0)
Bram Moolenaarf112f302020-12-20 17:47:52 +01006224 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02006225 handle_closure_in_use(&ectx, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00006226 ga_clear(&ectx.ec_funcrefs);
Bram Moolenaarf112f302020-12-20 17:47:52 +01006227 }
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02006228
Bram Moolenaaree8580e2020-08-28 17:19:07 +02006229 estack_pop();
6230 current_sctx = save_current_sctx;
6231
Bram Moolenaar2336c372021-12-06 15:06:54 +00006232 if (--ufunc->uf_calls <= 0 && ufunc->uf_refcount <= 0)
6233 // Function was unreferenced while being used, free it now.
6234 func_clear_free(ufunc, FALSE);
Bram Moolenaarc970e422021-03-17 15:03:04 +01006235
Bram Moolenaar352134b2020-10-17 22:04:08 +02006236 if (*msg_list != NULL && saved_msg_list != NULL)
6237 {
6238 msglist_T **plist = saved_msg_list;
6239
6240 // Append entries from the current msg_list (uncaught exceptions) to
6241 // the saved msg_list.
6242 while (*plist != NULL)
6243 plist = &(*plist)->next;
6244
6245 *plist = *msg_list;
6246 }
6247 msg_list = saved_msg_list;
6248
Bram Moolenaar4c137212021-04-19 16:48:48 +02006249 if (ectx.ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar02194d22020-10-24 23:08:38 +02006250 {
6251 cmdmod.cmod_filter_regmatch.regprog = NULL;
6252 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006253 cmdmod = ectx.ec_funclocal.floc_save_cmdmod;
Bram Moolenaar02194d22020-10-24 23:08:38 +02006254 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +01006255 emsg_silent_def = save_emsg_silent_def;
6256 did_emsg_def += save_did_emsg_def;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02006257
Bram Moolenaaree8580e2020-08-28 17:19:07 +02006258failed_early:
Bram Moolenaar0d1f55c2022-04-05 17:30:29 +01006259 // Free all arguments and local variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006260 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
Bram Moolenaar1936c762022-09-28 15:19:10 +01006261 {
6262 tv = STACK_TV(idx);
6263 if (tv->v_type != VAR_NUMBER && tv->v_type != VAR_UNKNOWN)
6264 clear_tv(tv);
6265 }
Bram Moolenaare99d4222021-06-13 14:01:26 +02006266 ex_nesting_level = orig_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02006267
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006268 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01006269 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006270 if (ectx.ec_outer_ref != NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +01006271 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006272 if (ectx.ec_outer_ref->or_outer_allocated)
6273 vim_free(ectx.ec_outer_ref->or_outer);
6274 partial_unref(ectx.ec_outer_ref->or_partial);
6275 vim_free(ectx.ec_outer_ref);
Bram Moolenaar0186e582021-01-10 18:33:11 +01006276 }
6277
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02006278 // Not sure if this is necessary.
6279 suppress_errthrow = save_suppress_errthrow;
6280
Bram Moolenaarb5841b92021-07-15 18:09:53 +02006281 if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before
6282 && !need_rethrow)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006283 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02006284 printable_func_name(ufunc));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01006285 funcdepth_restore(orig_funcdepth);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006286 return ret;
6287}
6288
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006289/*
Bram Moolenaar58779852022-09-06 18:31:14 +01006290 * Called when a def function has finished (possibly failed).
6291 * Invoke all the function returns to clean up and invoke deferred functions,
6292 * except the toplevel one.
6293 */
6294 void
6295unwind_def_callstack(ectx_T *ectx)
6296{
6297 while (ectx->ec_frame_idx != ectx->ec_initial_frame_idx)
6298 func_return(ectx);
6299}
6300
6301/*
dundargocc57b5bc2022-11-02 13:30:51 +00006302 * Invoke any deferred functions for the top function in "ectx".
Bram Moolenaar58779852022-09-06 18:31:14 +01006303 */
6304 void
6305may_invoke_defer_funcs(ectx_T *ectx)
6306{
6307 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
6308
6309 if (dfunc->df_defer_var_idx > 0)
6310 invoke_defer_funcs(ectx);
6311}
6312
6313/*
Bram Moolenaarcc341812022-09-19 15:54:34 +01006314 * Return loopvarinfo in a printable form in allocated memory.
6315 */
6316 static char_u *
6317printable_loopvarinfo(loopvarinfo_T *lvi)
6318{
6319 garray_T ga;
6320 int depth;
6321
6322 ga_init2(&ga, 1, 100);
6323 for (depth = 0; depth < lvi->lvi_depth; ++depth)
6324 {
6325 if (ga_grow(&ga, 50) == FAIL)
6326 break;
6327 if (lvi->lvi_loop[depth].var_idx == 0)
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +01006328 STRCPY((char *)ga.ga_data + ga.ga_len, " -");
Bram Moolenaarcc341812022-09-19 15:54:34 +01006329 else
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +01006330 vim_snprintf((char *)ga.ga_data + ga.ga_len, 50, " $%d-$%d",
Bram Moolenaarcc341812022-09-19 15:54:34 +01006331 lvi->lvi_loop[depth].var_idx,
6332 lvi->lvi_loop[depth].var_idx
6333 + lvi->lvi_loop[depth].var_count - 1);
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +01006334 ga.ga_len = (int)STRLEN(ga.ga_data);
Bram Moolenaarcc341812022-09-19 15:54:34 +01006335 }
6336 return ga.ga_data;
6337}
6338
6339/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02006340 * List instructions "instr" up to "instr_count" or until ISN_FINISH.
6341 * "ufunc" has the source lines, NULL for the instructions of ISN_SUBSTITUTE.
6342 * "pfx" is prefixed to every line.
6343 */
6344 static void
6345list_instructions(char *pfx, isn_T *instr, int instr_count, ufunc_T *ufunc)
6346{
6347 int line_idx = 0;
6348 int prev_current = 0;
6349 int current;
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02006350 int def_arg_idx = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006351
6352 for (current = 0; current < instr_count; ++current)
6353 {
6354 isn_T *iptr = &instr[current];
6355 char *line;
6356
6357 if (ufunc != NULL)
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02006358 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02006359 while (line_idx < iptr->isn_lnum
6360 && line_idx < ufunc->uf_lines.ga_len)
6361 {
6362 if (current > prev_current)
6363 {
6364 msg_puts("\n\n");
6365 prev_current = current;
6366 }
6367 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
6368 if (line != NULL)
6369 msg(line);
6370 }
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02006371 if (iptr->isn_type == ISN_JUMP_IF_ARG_SET)
6372 {
6373 int first_def_arg = ufunc->uf_args.ga_len
6374 - ufunc->uf_def_args.ga_len;
6375
6376 if (def_arg_idx > 0)
6377 msg_puts("\n\n");
6378 msg_start();
6379 msg_puts(" ");
6380 msg_puts(((char **)(ufunc->uf_args.ga_data))[
6381 first_def_arg + def_arg_idx]);
6382 msg_puts(" = ");
6383 msg_puts(((char **)(ufunc->uf_def_args.ga_data))[def_arg_idx++]);
6384 msg_clr_eos();
6385 msg_end();
6386 }
6387 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006388
6389 switch (iptr->isn_type)
6390 {
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006391 case ISN_CONSTRUCT:
6392 smsg("%s%4d NEW %s size %d", pfx, current,
6393 iptr->isn_arg.construct.construct_class->class_name,
6394 (int)iptr->isn_arg.construct.construct_size);
6395 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006396 case ISN_EXEC:
6397 smsg("%s%4d EXEC %s", pfx, current, iptr->isn_arg.string);
6398 break;
Bram Moolenaar20677332021-06-06 17:02:53 +02006399 case ISN_EXEC_SPLIT:
6400 smsg("%s%4d EXEC_SPLIT %s", pfx, current, iptr->isn_arg.string);
6401 break;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00006402 case ISN_EXECRANGE:
6403 smsg("%s%4d EXECRANGE %s", pfx, current, iptr->isn_arg.string);
6404 break;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02006405 case ISN_LEGACY_EVAL:
6406 smsg("%s%4d EVAL legacy %s", pfx, current,
6407 iptr->isn_arg.string);
6408 break;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02006409 case ISN_REDIRSTART:
6410 smsg("%s%4d REDIR", pfx, current);
6411 break;
6412 case ISN_REDIREND:
6413 smsg("%s%4d REDIR END%s", pfx, current,
6414 iptr->isn_arg.number ? " append" : "");
6415 break;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006416 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02006417#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006418 smsg("%s%4d CEXPR pre %s", pfx, current,
6419 cexpr_get_auname(iptr->isn_arg.number));
Bram Moolenaarb7c97812021-05-05 22:51:39 +02006420#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006421 break;
6422 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02006423#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006424 {
6425 cexprref_T *cer = iptr->isn_arg.cexpr.cexpr_ref;
6426
6427 smsg("%s%4d CEXPR core %s%s \"%s\"", pfx, current,
6428 cexpr_get_auname(cer->cer_cmdidx),
6429 cer->cer_forceit ? "!" : "",
6430 cer->cer_cmdline);
6431 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02006432#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006433 break;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006434 case ISN_INSTR:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006435 smsg("%s%4d INSTR", pfx, current);
6436 list_instructions(" ", iptr->isn_arg.instr, INT_MAX, NULL);
6437 msg(" -------------");
6438 break;
6439 case ISN_SOURCE:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006440 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006441 scriptitem_T *si = SCRIPT_ITEM(iptr->isn_arg.number);
6442
6443 smsg("%s%4d SOURCE %s", pfx, current, si->sn_name);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006444 }
6445 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006446 case ISN_SUBSTITUTE:
6447 {
6448 subs_T *subs = &iptr->isn_arg.subs;
6449
6450 smsg("%s%4d SUBSTITUTE %s", pfx, current, subs->subs_cmd);
6451 list_instructions(" ", subs->subs_instr, INT_MAX, NULL);
6452 msg(" -------------");
6453 }
6454 break;
6455 case ISN_EXECCONCAT:
6456 smsg("%s%4d EXECCONCAT %lld", pfx, current,
6457 (varnumber_T)iptr->isn_arg.number);
6458 break;
6459 case ISN_ECHO:
6460 {
6461 echo_T *echo = &iptr->isn_arg.echo;
6462
6463 smsg("%s%4d %s %d", pfx, current,
6464 echo->echo_with_white ? "ECHO" : "ECHON",
6465 echo->echo_count);
6466 }
6467 break;
6468 case ISN_EXECUTE:
6469 smsg("%s%4d EXECUTE %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02006470 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006471 break;
6472 case ISN_ECHOMSG:
6473 smsg("%s%4d ECHOMSG %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02006474 (varnumber_T)(iptr->isn_arg.number));
6475 break;
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01006476 case ISN_ECHOWINDOW:
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01006477 if (iptr->isn_arg.echowin.ewin_time > 0)
6478 smsg("%s%4d ECHOWINDOW %d (%ld sec)", pfx, current,
6479 iptr->isn_arg.echowin.ewin_count,
6480 iptr->isn_arg.echowin.ewin_time);
6481 else
6482 smsg("%s%4d ECHOWINDOW %d", pfx, current,
6483 iptr->isn_arg.echowin.ewin_count);
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01006484 break;
Bram Moolenaar7de62622021-08-07 15:05:47 +02006485 case ISN_ECHOCONSOLE:
6486 smsg("%s%4d ECHOCONSOLE %lld", pfx, current,
6487 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006488 break;
6489 case ISN_ECHOERR:
6490 smsg("%s%4d ECHOERR %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02006491 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006492 break;
6493 case ISN_LOAD:
6494 {
6495 if (iptr->isn_arg.number < 0)
6496 smsg("%s%4d LOAD arg[%lld]", pfx, current,
6497 (varnumber_T)(iptr->isn_arg.number
6498 + STACK_FRAME_SIZE));
6499 else
6500 smsg("%s%4d LOAD $%lld", pfx, current,
6501 (varnumber_T)(iptr->isn_arg.number));
6502 }
6503 break;
6504 case ISN_LOADOUTER:
6505 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006506 isn_outer_T *outer = &iptr->isn_arg.outer;
6507
6508 if (outer->outer_idx < 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02006509 smsg("%s%4d LOADOUTER level %d arg[%d]", pfx, current,
Bram Moolenaarcc341812022-09-19 15:54:34 +01006510 outer->outer_depth,
6511 outer->outer_idx + STACK_FRAME_SIZE);
6512 else if (outer->outer_depth < 0)
6513 smsg("%s%4d LOADOUTER $%d in loop level %d",
6514 pfx, current,
6515 outer->outer_idx,
6516 -outer->outer_depth);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006517 else
6518 smsg("%s%4d LOADOUTER level %d $%d", pfx, current,
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006519 outer->outer_depth,
6520 outer->outer_idx);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006521 }
6522 break;
6523 case ISN_LOADV:
6524 smsg("%s%4d LOADV v:%s", pfx, current,
6525 get_vim_var_name(iptr->isn_arg.number));
6526 break;
6527 case ISN_LOADSCRIPT:
6528 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006529 scriptref_T *sref = iptr->isn_arg.script.scriptref;
6530 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
6531 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006532
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006533 sv = get_script_svar(sref, -1);
6534 if (sv == NULL)
6535 smsg("%s%4d LOADSCRIPT [deleted] from %s",
6536 pfx, current, si->sn_name);
6537 else
6538 smsg("%s%4d LOADSCRIPT %s-%d from %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02006539 sv->sv_name,
6540 sref->sref_idx,
6541 si->sn_name);
6542 }
6543 break;
6544 case ISN_LOADS:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006545 case ISN_LOADEXPORT:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006546 {
6547 scriptitem_T *si = SCRIPT_ITEM(
6548 iptr->isn_arg.loadstore.ls_sid);
6549
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006550 smsg("%s%4d %s s:%s from %s", pfx, current,
6551 iptr->isn_type == ISN_LOADS ? "LOADS"
6552 : "LOADEXPORT",
6553 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006554 }
6555 break;
6556 case ISN_LOADAUTO:
6557 smsg("%s%4d LOADAUTO %s", pfx, current, iptr->isn_arg.string);
6558 break;
6559 case ISN_LOADG:
6560 smsg("%s%4d LOADG g:%s", pfx, current, iptr->isn_arg.string);
6561 break;
6562 case ISN_LOADB:
6563 smsg("%s%4d LOADB b:%s", pfx, current, iptr->isn_arg.string);
6564 break;
6565 case ISN_LOADW:
6566 smsg("%s%4d LOADW w:%s", pfx, current, iptr->isn_arg.string);
6567 break;
6568 case ISN_LOADT:
6569 smsg("%s%4d LOADT t:%s", pfx, current, iptr->isn_arg.string);
6570 break;
6571 case ISN_LOADGDICT:
6572 smsg("%s%4d LOAD g:", pfx, current);
6573 break;
6574 case ISN_LOADBDICT:
6575 smsg("%s%4d LOAD b:", pfx, current);
6576 break;
6577 case ISN_LOADWDICT:
6578 smsg("%s%4d LOAD w:", pfx, current);
6579 break;
6580 case ISN_LOADTDICT:
6581 smsg("%s%4d LOAD t:", pfx, current);
6582 break;
6583 case ISN_LOADOPT:
6584 smsg("%s%4d LOADOPT %s", pfx, current, iptr->isn_arg.string);
6585 break;
6586 case ISN_LOADENV:
6587 smsg("%s%4d LOADENV %s", pfx, current, iptr->isn_arg.string);
6588 break;
6589 case ISN_LOADREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006590 smsg("%s%4d LOADREG @%c", pfx, current,
6591 (int)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006592 break;
6593
6594 case ISN_STORE:
6595 if (iptr->isn_arg.number < 0)
6596 smsg("%s%4d STORE arg[%lld]", pfx, current,
6597 iptr->isn_arg.number + STACK_FRAME_SIZE);
6598 else
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006599 smsg("%s%4d STORE $%lld", pfx, current,
6600 iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006601 break;
6602 case ISN_STOREOUTER:
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006603 {
6604 isn_outer_T *outer = &iptr->isn_arg.outer;
6605
6606 if (outer->outer_depth == OUTER_LOOP_DEPTH)
6607 smsg("%s%4d STOREOUTER level 1 $%d in loop",
6608 pfx, current, outer->outer_idx);
6609 else
6610 smsg("%s%4d STOREOUTER level %d $%d", pfx, current,
6611 outer->outer_depth, outer->outer_idx);
6612 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006613 break;
6614 case ISN_STOREV:
6615 smsg("%s%4d STOREV v:%s", pfx, current,
6616 get_vim_var_name(iptr->isn_arg.number));
6617 break;
6618 case ISN_STOREAUTO:
6619 smsg("%s%4d STOREAUTO %s", pfx, current, iptr->isn_arg.string);
6620 break;
6621 case ISN_STOREG:
6622 smsg("%s%4d STOREG %s", pfx, current, iptr->isn_arg.string);
6623 break;
6624 case ISN_STOREB:
6625 smsg("%s%4d STOREB %s", pfx, current, iptr->isn_arg.string);
6626 break;
6627 case ISN_STOREW:
6628 smsg("%s%4d STOREW %s", pfx, current, iptr->isn_arg.string);
6629 break;
6630 case ISN_STORET:
6631 smsg("%s%4d STORET %s", pfx, current, iptr->isn_arg.string);
6632 break;
6633 case ISN_STORES:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006634 case ISN_STOREEXPORT:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006635 {
6636 scriptitem_T *si = SCRIPT_ITEM(
6637 iptr->isn_arg.loadstore.ls_sid);
6638
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006639 smsg("%s%4d %s %s in %s", pfx, current,
6640 iptr->isn_type == ISN_STORES
6641 ? "STORES" : "STOREEXPORT",
Bram Moolenaar4c137212021-04-19 16:48:48 +02006642 iptr->isn_arg.loadstore.ls_name, si->sn_name);
6643 }
6644 break;
6645 case ISN_STORESCRIPT:
6646 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006647 scriptref_T *sref = iptr->isn_arg.script.scriptref;
6648 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
6649 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006650
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006651 sv = get_script_svar(sref, -1);
6652 if (sv == NULL)
6653 smsg("%s%4d STORESCRIPT [deleted] in %s",
6654 pfx, current, si->sn_name);
6655 else
6656 smsg("%s%4d STORESCRIPT %s-%d in %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02006657 sv->sv_name,
6658 sref->sref_idx,
6659 si->sn_name);
6660 }
6661 break;
6662 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00006663 case ISN_STOREFUNCOPT:
6664 smsg("%s%4d %s &%s", pfx, current,
6665 iptr->isn_type == ISN_STOREOPT ? "STOREOPT" : "STOREFUNCOPT",
Bram Moolenaar4c137212021-04-19 16:48:48 +02006666 iptr->isn_arg.storeopt.so_name);
6667 break;
6668 case ISN_STOREENV:
6669 smsg("%s%4d STOREENV $%s", pfx, current, iptr->isn_arg.string);
6670 break;
6671 case ISN_STOREREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006672 smsg("%s%4d STOREREG @%c", pfx, current,
6673 (int)iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006674 break;
6675 case ISN_STORENR:
6676 smsg("%s%4d STORE %lld in $%d", pfx, current,
6677 iptr->isn_arg.storenr.stnr_val,
6678 iptr->isn_arg.storenr.stnr_idx);
6679 break;
6680
6681 case ISN_STOREINDEX:
6682 smsg("%s%4d STOREINDEX %s", pfx, current,
Bram Moolenaarf7d1c6e2023-01-16 20:47:57 +00006683 vartype_name(iptr->isn_arg.storeindex.si_vartype));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006684 break;
6685
6686 case ISN_STORERANGE:
6687 smsg("%s%4d STORERANGE", pfx, current);
6688 break;
6689
Bram Moolenaard505d172022-12-18 21:42:55 +00006690 case ISN_LOAD_CLASSMEMBER:
6691 case ISN_STORE_CLASSMEMBER:
6692 {
6693 class_T *cl = iptr->isn_arg.classmember.cm_class;
6694 int idx = iptr->isn_arg.classmember.cm_idx;
6695 ocmember_T *ocm = &cl->class_class_members[idx];
6696 smsg("%s%4d %s CLASSMEMBER %s.%s", pfx, current,
6697 iptr->isn_type == ISN_LOAD_CLASSMEMBER
6698 ? "LOAD" : "STORE",
6699 cl->class_name, ocm->ocm_name);
6700 }
6701 break;
6702
Bram Moolenaar4c137212021-04-19 16:48:48 +02006703 // constants
6704 case ISN_PUSHNR:
6705 smsg("%s%4d PUSHNR %lld", pfx, current,
6706 (varnumber_T)(iptr->isn_arg.number));
6707 break;
6708 case ISN_PUSHBOOL:
6709 case ISN_PUSHSPEC:
6710 smsg("%s%4d PUSH %s", pfx, current,
6711 get_var_special_name(iptr->isn_arg.number));
6712 break;
6713 case ISN_PUSHF:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006714 smsg("%s%4d PUSHF %g", pfx, current, iptr->isn_arg.fnumber);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006715 break;
6716 case ISN_PUSHS:
6717 smsg("%s%4d PUSHS \"%s\"", pfx, current, iptr->isn_arg.string);
6718 break;
6719 case ISN_PUSHBLOB:
6720 {
6721 char_u *r;
6722 char_u numbuf[NUMBUFLEN];
6723 char_u *tofree;
6724
6725 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
6726 smsg("%s%4d PUSHBLOB %s", pfx, current, r);
6727 vim_free(tofree);
6728 }
6729 break;
6730 case ISN_PUSHFUNC:
6731 {
6732 char *name = (char *)iptr->isn_arg.string;
6733
6734 smsg("%s%4d PUSHFUNC \"%s\"", pfx, current,
6735 name == NULL ? "[none]" : name);
6736 }
6737 break;
6738 case ISN_PUSHCHANNEL:
6739#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar397a87a2022-03-20 21:14:15 +00006740 smsg("%s%4d PUSHCHANNEL 0", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006741#endif
6742 break;
6743 case ISN_PUSHJOB:
6744#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar397a87a2022-03-20 21:14:15 +00006745 smsg("%s%4d PUSHJOB \"no process\"", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006746#endif
6747 break;
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00006748 case ISN_PUSHOBJ:
6749 smsg("%s%4d PUSHOBJ null", pfx, current);
6750 break;
6751 case ISN_PUSHCLASS:
6752 smsg("%s%4d PUSHCLASS %s", pfx, current,
Bram Moolenaar30a84472023-02-27 08:07:14 +00006753 iptr->isn_arg.classarg == NULL ? "null"
6754 : (char *)iptr->isn_arg.classarg->class_name);
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00006755 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006756 case ISN_PUSHEXC:
6757 smsg("%s%4d PUSH v:exception", pfx, current);
6758 break;
Bram Moolenaar06b77222022-01-25 15:51:56 +00006759 case ISN_AUTOLOAD:
6760 smsg("%s%4d AUTOLOAD %s", pfx, current, iptr->isn_arg.string);
6761 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006762 case ISN_UNLET:
6763 smsg("%s%4d UNLET%s %s", pfx, current,
6764 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
6765 iptr->isn_arg.unlet.ul_name);
6766 break;
6767 case ISN_UNLETENV:
6768 smsg("%s%4d UNLETENV%s $%s", pfx, current,
6769 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
6770 iptr->isn_arg.unlet.ul_name);
6771 break;
6772 case ISN_UNLETINDEX:
6773 smsg("%s%4d UNLETINDEX", pfx, current);
6774 break;
6775 case ISN_UNLETRANGE:
6776 smsg("%s%4d UNLETRANGE", pfx, current);
6777 break;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02006778 case ISN_LOCKUNLOCK:
6779 smsg("%s%4d LOCKUNLOCK %s", pfx, current, iptr->isn_arg.string);
6780 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006781 case ISN_LOCKCONST:
6782 smsg("%s%4d LOCKCONST", pfx, current);
6783 break;
6784 case ISN_NEWLIST:
6785 smsg("%s%4d NEWLIST size %lld", pfx, current,
Bram Moolenaar1936c762022-09-28 15:19:10 +01006786 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006787 break;
6788 case ISN_NEWDICT:
6789 smsg("%s%4d NEWDICT size %lld", pfx, current,
Bram Moolenaar1936c762022-09-28 15:19:10 +01006790 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006791 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00006792 case ISN_NEWPARTIAL:
6793 smsg("%s%4d NEWPARTIAL", pfx, current);
6794 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006795
6796 // function call
6797 case ISN_BCALL:
6798 {
6799 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
6800
6801 smsg("%s%4d BCALL %s(argc %d)", pfx, current,
6802 internal_func_name(cbfunc->cbf_idx),
6803 cbfunc->cbf_argcount);
6804 }
6805 break;
6806 case ISN_DCALL:
6807 {
6808 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
6809 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
6810 + cdfunc->cdf_idx;
6811
6812 smsg("%s%4d DCALL %s(argc %d)", pfx, current,
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006813 printable_func_name(df->df_ufunc),
6814 cdfunc->cdf_argcount);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006815 }
6816 break;
Bram Moolenaard0200c82023-01-28 15:19:40 +00006817 case ISN_METHODCALL:
6818 {
6819 cmfunc_T *mfunc = iptr->isn_arg.mfunc;
6820
6821 smsg("%s%4d METHODCALL %s.%s(argc %d)", pfx, current,
6822 mfunc->cmf_itf->class_name,
6823 mfunc->cmf_itf->class_obj_methods[
6824 mfunc->cmf_idx]->uf_name,
6825 mfunc->cmf_argcount);
6826 }
6827 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006828 case ISN_UCALL:
6829 {
6830 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
6831
6832 smsg("%s%4d UCALL %s(argc %d)", pfx, current,
6833 cufunc->cuf_name, cufunc->cuf_argcount);
6834 }
6835 break;
6836 case ISN_PCALL:
6837 {
6838 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
6839
6840 smsg("%s%4d PCALL%s (argc %d)", pfx, current,
6841 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
6842 }
6843 break;
6844 case ISN_PCALL_END:
6845 smsg("%s%4d PCALL end", pfx, current);
6846 break;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006847 case ISN_DEFER:
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00006848 case ISN_DEFEROBJ:
6849 smsg("%s%4d %s %d args", pfx, current,
6850 iptr->isn_type == ISN_DEFER ? "DEFER" : "DEFEROBJ",
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006851 (int)iptr->isn_arg.defer.defer_argcount);
6852 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006853 case ISN_RETURN:
6854 smsg("%s%4d RETURN", pfx, current);
6855 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02006856 case ISN_RETURN_VOID:
6857 smsg("%s%4d RETURN void", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006858 break;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006859 case ISN_RETURN_OBJECT:
6860 smsg("%s%4d RETURN object", pfx, current);
6861 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006862 case ISN_FUNCREF:
6863 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006864 funcref_T *funcref = &iptr->isn_arg.funcref;
6865 funcref_extra_T *extra = funcref->fr_extra;
6866 char_u *name;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006867
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006868 if (extra == NULL || extra->fre_func_name == NULL)
Bram Moolenaar38453522021-11-28 22:00:12 +00006869 {
6870 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
6871 + funcref->fr_dfunc_idx;
6872 name = df->df_ufunc->uf_name;
6873 }
6874 else
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006875 name = extra->fre_func_name;
Bram Moolenaar313e4722023-02-08 20:55:27 +00006876 if (extra != NULL && extra->fre_class != NULL)
6877 {
6878 smsg("%s%4d FUNCREF %s.%s", pfx, current,
6879 extra->fre_class->class_name, name);
6880 }
6881 else if (extra == NULL
6882 || extra->fre_loopvar_info.lvi_depth == 0)
6883 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006884 smsg("%s%4d FUNCREF %s", pfx, current, name);
Bram Moolenaar313e4722023-02-08 20:55:27 +00006885 }
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006886 else
Bram Moolenaarcc341812022-09-19 15:54:34 +01006887 {
6888 char_u *info = printable_loopvarinfo(
6889 &extra->fre_loopvar_info);
6890
6891 smsg("%s%4d FUNCREF %s vars %s", pfx, current,
6892 name, info);
6893 vim_free(info);
6894 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006895 }
6896 break;
6897
6898 case ISN_NEWFUNC:
6899 {
Bram Moolenaarcc341812022-09-19 15:54:34 +01006900 newfuncarg_T *arg = iptr->isn_arg.newfunc.nf_arg;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006901
Bram Moolenaarcc341812022-09-19 15:54:34 +01006902 if (arg->nfa_loopvar_info.lvi_depth == 0)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006903 smsg("%s%4d NEWFUNC %s %s", pfx, current,
6904 arg->nfa_lambda, arg->nfa_global);
6905 else
Bram Moolenaarcc341812022-09-19 15:54:34 +01006906 {
6907 char_u *info = printable_loopvarinfo(
6908 &arg->nfa_loopvar_info);
6909
6910 smsg("%s%4d NEWFUNC %s %s vars %s", pfx, current,
6911 arg->nfa_lambda, arg->nfa_global, info);
6912 vim_free(info);
6913 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006914 }
6915 break;
6916
6917 case ISN_DEF:
6918 {
6919 char_u *name = iptr->isn_arg.string;
6920
6921 smsg("%s%4d DEF %s", pfx, current,
6922 name == NULL ? (char_u *)"" : name);
6923 }
6924 break;
6925
6926 case ISN_JUMP:
6927 {
6928 char *when = "?";
6929
6930 switch (iptr->isn_arg.jump.jump_when)
6931 {
6932 case JUMP_ALWAYS:
6933 when = "JUMP";
6934 break;
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02006935 case JUMP_NEVER:
6936 iemsg("JUMP_NEVER should not be used");
6937 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006938 case JUMP_AND_KEEP_IF_TRUE:
6939 when = "JUMP_AND_KEEP_IF_TRUE";
6940 break;
6941 case JUMP_IF_FALSE:
6942 when = "JUMP_IF_FALSE";
6943 break;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01006944 case JUMP_WHILE_FALSE:
6945 when = "JUMP_WHILE_FALSE"; // unused
6946 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006947 case JUMP_IF_COND_FALSE:
6948 when = "JUMP_IF_COND_FALSE";
6949 break;
6950 case JUMP_IF_COND_TRUE:
6951 when = "JUMP_IF_COND_TRUE";
6952 break;
6953 }
6954 smsg("%s%4d %s -> %d", pfx, current, when,
6955 iptr->isn_arg.jump.jump_where);
6956 }
6957 break;
6958
6959 case ISN_JUMP_IF_ARG_SET:
6960 smsg("%s%4d JUMP_IF_ARG_SET arg[%d] -> %d", pfx, current,
6961 iptr->isn_arg.jumparg.jump_arg_off + STACK_FRAME_SIZE,
6962 iptr->isn_arg.jump.jump_where);
6963 break;
6964
Bram Moolenaar65b0d162022-12-13 18:43:22 +00006965 case ISN_JUMP_IF_ARG_NOT_SET:
6966 smsg("%s%4d JUMP_IF_ARG_NOT_SET arg[%d] -> %d", pfx, current,
6967 iptr->isn_arg.jumparg.jump_arg_off + STACK_FRAME_SIZE,
6968 iptr->isn_arg.jump.jump_where);
6969 break;
6970
Bram Moolenaar4c137212021-04-19 16:48:48 +02006971 case ISN_FOR:
6972 {
6973 forloop_T *forloop = &iptr->isn_arg.forloop;
6974
6975 smsg("%s%4d FOR $%d -> %d", pfx, current,
Bram Moolenaarcc341812022-09-19 15:54:34 +01006976 forloop->for_loop_idx, forloop->for_end);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006977 }
6978 break;
6979
Bram Moolenaarb46c0832022-09-15 17:19:37 +01006980 case ISN_ENDLOOP:
6981 {
6982 endloop_T *endloop = &iptr->isn_arg.endloop;
6983
Bram Moolenaarcc341812022-09-19 15:54:34 +01006984 smsg("%s%4d ENDLOOP ref $%d save $%d-$%d depth %d",
6985 pfx, current,
Bram Moolenaarb46c0832022-09-15 17:19:37 +01006986 endloop->end_funcref_idx,
6987 endloop->end_var_idx,
Bram Moolenaarcc341812022-09-19 15:54:34 +01006988 endloop->end_var_idx + endloop->end_var_count - 1,
6989 endloop->end_depth);
Bram Moolenaarb46c0832022-09-15 17:19:37 +01006990 }
6991 break;
6992
6993 case ISN_WHILE:
6994 {
6995 whileloop_T *whileloop = &iptr->isn_arg.whileloop;
6996
6997 smsg("%s%4d WHILE $%d -> %d", pfx, current,
6998 whileloop->while_funcref_idx,
6999 whileloop->while_end);
7000 }
7001 break;
7002
Bram Moolenaar4c137212021-04-19 16:48:48 +02007003 case ISN_TRY:
7004 {
Bram Moolenaar0d807102021-12-21 09:42:09 +00007005 try_T *try = &iptr->isn_arg.tryref;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007006
7007 if (try->try_ref->try_finally == 0)
7008 smsg("%s%4d TRY catch -> %d, endtry -> %d",
7009 pfx, current,
7010 try->try_ref->try_catch,
7011 try->try_ref->try_endtry);
7012 else
7013 smsg("%s%4d TRY catch -> %d, finally -> %d, endtry -> %d",
7014 pfx, current,
7015 try->try_ref->try_catch,
7016 try->try_ref->try_finally,
7017 try->try_ref->try_endtry);
7018 }
7019 break;
7020 case ISN_CATCH:
Bram Moolenaar4c137212021-04-19 16:48:48 +02007021 smsg("%s%4d CATCH", pfx, current);
7022 break;
7023 case ISN_TRYCONT:
7024 {
7025 trycont_T *trycont = &iptr->isn_arg.trycont;
7026
7027 smsg("%s%4d TRY-CONTINUE %d level%s -> %d", pfx, current,
7028 trycont->tct_levels,
7029 trycont->tct_levels == 1 ? "" : "s",
7030 trycont->tct_where);
7031 }
7032 break;
7033 case ISN_FINALLY:
7034 smsg("%s%4d FINALLY", pfx, current);
7035 break;
7036 case ISN_ENDTRY:
7037 smsg("%s%4d ENDTRY", pfx, current);
7038 break;
7039 case ISN_THROW:
7040 smsg("%s%4d THROW", pfx, current);
7041 break;
7042
7043 // expression operations on number
7044 case ISN_OPNR:
7045 case ISN_OPFLOAT:
7046 case ISN_OPANY:
7047 {
7048 char *what;
7049 char *ins;
7050
7051 switch (iptr->isn_arg.op.op_type)
7052 {
7053 case EXPR_MULT: what = "*"; break;
7054 case EXPR_DIV: what = "/"; break;
7055 case EXPR_REM: what = "%"; break;
7056 case EXPR_SUB: what = "-"; break;
7057 case EXPR_ADD: what = "+"; break;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01007058 case EXPR_LSHIFT: what = "<<"; break;
7059 case EXPR_RSHIFT: what = ">>"; break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007060 default: what = "???"; break;
7061 }
7062 switch (iptr->isn_type)
7063 {
7064 case ISN_OPNR: ins = "OPNR"; break;
7065 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
7066 case ISN_OPANY: ins = "OPANY"; break;
7067 default: ins = "???"; break;
7068 }
7069 smsg("%s%4d %s %s", pfx, current, ins, what);
7070 }
7071 break;
7072
7073 case ISN_COMPAREBOOL:
7074 case ISN_COMPARESPECIAL:
Bram Moolenaar7a222242022-03-01 19:23:24 +00007075 case ISN_COMPARENULL:
Bram Moolenaar4c137212021-04-19 16:48:48 +02007076 case ISN_COMPARENR:
7077 case ISN_COMPAREFLOAT:
7078 case ISN_COMPARESTRING:
7079 case ISN_COMPAREBLOB:
7080 case ISN_COMPARELIST:
7081 case ISN_COMPAREDICT:
7082 case ISN_COMPAREFUNC:
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00007083 case ISN_COMPARECLASS:
7084 case ISN_COMPAREOBJECT:
Bram Moolenaar4c137212021-04-19 16:48:48 +02007085 case ISN_COMPAREANY:
7086 {
7087 char *p;
7088 char buf[10];
7089 char *type;
7090
7091 switch (iptr->isn_arg.op.op_type)
7092 {
7093 case EXPR_EQUAL: p = "=="; break;
7094 case EXPR_NEQUAL: p = "!="; break;
7095 case EXPR_GREATER: p = ">"; break;
7096 case EXPR_GEQUAL: p = ">="; break;
7097 case EXPR_SMALLER: p = "<"; break;
7098 case EXPR_SEQUAL: p = "<="; break;
7099 case EXPR_MATCH: p = "=~"; break;
7100 case EXPR_IS: p = "is"; break;
7101 case EXPR_ISNOT: p = "isnot"; break;
7102 case EXPR_NOMATCH: p = "!~"; break;
7103 default: p = "???"; break;
7104 }
7105 STRCPY(buf, p);
7106 if (iptr->isn_arg.op.op_ic == TRUE)
7107 strcat(buf, "?");
7108 switch(iptr->isn_type)
7109 {
7110 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
7111 case ISN_COMPARESPECIAL:
7112 type = "COMPARESPECIAL"; break;
Bram Moolenaar7a222242022-03-01 19:23:24 +00007113 case ISN_COMPARENULL: type = "COMPARENULL"; break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007114 case ISN_COMPARENR: type = "COMPARENR"; break;
7115 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
7116 case ISN_COMPARESTRING:
7117 type = "COMPARESTRING"; break;
7118 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
7119 case ISN_COMPARELIST: type = "COMPARELIST"; break;
7120 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
7121 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00007122 case ISN_COMPARECLASS: type = "COMPARECLASS"; break;
7123 case ISN_COMPAREOBJECT:
7124 type = "COMPAREOBJECT"; break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007125 case ISN_COMPAREANY: type = "COMPAREANY"; break;
7126 default: type = "???"; break;
7127 }
7128
7129 smsg("%s%4d %s %s", pfx, current, type, buf);
7130 }
7131 break;
7132
7133 case ISN_ADDLIST: smsg("%s%4d ADDLIST", pfx, current); break;
7134 case ISN_ADDBLOB: smsg("%s%4d ADDBLOB", pfx, current); break;
7135
7136 // expression operations
LemonBoy372bcce2022-04-25 12:43:20 +01007137 case ISN_CONCAT:
7138 smsg("%s%4d CONCAT size %lld", pfx, current,
7139 (varnumber_T)(iptr->isn_arg.number));
7140 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007141 case ISN_STRINDEX: smsg("%s%4d STRINDEX", pfx, current); break;
7142 case ISN_STRSLICE: smsg("%s%4d STRSLICE", pfx, current); break;
7143 case ISN_BLOBINDEX: smsg("%s%4d BLOBINDEX", pfx, current); break;
7144 case ISN_BLOBSLICE: smsg("%s%4d BLOBSLICE", pfx, current); break;
7145 case ISN_LISTAPPEND: smsg("%s%4d LISTAPPEND", pfx, current); break;
7146 case ISN_BLOBAPPEND: smsg("%s%4d BLOBAPPEND", pfx, current); break;
7147 case ISN_LISTINDEX: smsg("%s%4d LISTINDEX", pfx, current); break;
7148 case ISN_LISTSLICE: smsg("%s%4d LISTSLICE", pfx, current); break;
7149 case ISN_ANYINDEX: smsg("%s%4d ANYINDEX", pfx, current); break;
7150 case ISN_ANYSLICE: smsg("%s%4d ANYSLICE", pfx, current); break;
7151 case ISN_SLICE: smsg("%s%4d SLICE %lld",
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02007152 pfx, current, iptr->isn_arg.number); break;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02007153 case ISN_GETITEM: smsg("%s%4d ITEM %lld%s", pfx, current,
7154 iptr->isn_arg.getitem.gi_index,
7155 iptr->isn_arg.getitem.gi_with_op ?
7156 " with op" : ""); break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007157 case ISN_MEMBER: smsg("%s%4d MEMBER", pfx, current); break;
7158 case ISN_STRINGMEMBER: smsg("%s%4d MEMBER %s", pfx, current,
7159 iptr->isn_arg.string); break;
Ernie Rael00df69e2023-09-05 07:38:09 +02007160 case ISN_GET_OBJ_MEMBER: smsg("%s%4d OBJ_MEMBER %d%s", pfx, current,
7161 (int)iptr->isn_arg.classmember.cm_idx,
7162 iptr->isn_arg.classmember.cm_static
7163 ? " [STATIC]" : "");
7164 break;
7165 case ISN_GET_ITF_MEMBER: smsg("%s%4d ITF_MEMBER %d on %s%s",
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00007166 pfx, current,
7167 (int)iptr->isn_arg.classmember.cm_idx,
Ernie Rael00df69e2023-09-05 07:38:09 +02007168 iptr->isn_arg.classmember.cm_class->class_name,
7169 iptr->isn_arg.classmember.cm_static
7170 ? " [STATIC]" : "");
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00007171 break;
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00007172 case ISN_STORE_THIS: smsg("%s%4d STORE_THIS %d", pfx, current,
Bram Moolenaarffdaca92022-12-09 21:41:48 +00007173 (int)iptr->isn_arg.number); break;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02007174 case ISN_CLEARDICT: smsg("%s%4d CLEARDICT", pfx, current); break;
7175 case ISN_USEDICT: smsg("%s%4d USEDICT", pfx, current); break;
7176
Bram Moolenaar4c137212021-04-19 16:48:48 +02007177 case ISN_NEGATENR: smsg("%s%4d NEGATENR", pfx, current); break;
7178
Bram Moolenaar4c137212021-04-19 16:48:48 +02007179 case ISN_CHECKTYPE:
7180 {
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01007181 checktype_T *ct = &iptr->isn_arg.type;
Bram Moolenaarc6951a72022-12-29 20:56:24 +00007182 char *tofree = NULL;
7183 char *typename;
7184
7185 if (ct->ct_type->tt_type == VAR_FLOAT
7186 && (ct->ct_type->tt_flags & TTFLAG_NUMBER_OK))
7187 typename = "float|number";
7188 else
7189 typename = type_name(ct->ct_type, &tofree);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007190
7191 if (ct->ct_arg_idx == 0)
7192 smsg("%s%4d CHECKTYPE %s stack[%d]", pfx, current,
Bram Moolenaarc6951a72022-12-29 20:56:24 +00007193 typename,
Bram Moolenaar4c137212021-04-19 16:48:48 +02007194 (int)ct->ct_off);
7195 else
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01007196 smsg("%s%4d CHECKTYPE %s stack[%d] %s %d",
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02007197 pfx, current,
Bram Moolenaarc6951a72022-12-29 20:56:24 +00007198 typename,
Bram Moolenaar4c137212021-04-19 16:48:48 +02007199 (int)ct->ct_off,
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01007200 ct->ct_is_var ? "var": "arg",
Bram Moolenaar4c137212021-04-19 16:48:48 +02007201 (int)ct->ct_arg_idx);
7202 vim_free(tofree);
7203 break;
7204 }
7205 case ISN_CHECKLEN: smsg("%s%4d CHECKLEN %s%d", pfx, current,
7206 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
7207 iptr->isn_arg.checklen.cl_min_len);
7208 break;
7209 case ISN_SETTYPE:
7210 {
7211 char *tofree;
7212
7213 smsg("%s%4d SETTYPE %s", pfx, current,
7214 type_name(iptr->isn_arg.type.ct_type, &tofree));
7215 vim_free(tofree);
7216 break;
7217 }
7218 case ISN_COND2BOOL: smsg("%s%4d COND2BOOL", pfx, current); break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007219 case ISN_2BOOL: if (iptr->isn_arg.tobool.invert)
7220 smsg("%s%4d INVERT %d (!val)", pfx, current,
7221 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007222 else
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007223 smsg("%s%4d 2BOOL %d (!!val)", pfx, current,
7224 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007225 break;
7226 case ISN_2STRING: smsg("%s%4d 2STRING stack[%lld]", pfx, current,
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007227 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02007228 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007229 case ISN_2STRING_ANY: smsg("%s%4d 2STRING_ANY stack[%lld]",
7230 pfx, current,
7231 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02007232 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007233 case ISN_RANGE: smsg("%s%4d RANGE %s", pfx, current,
7234 iptr->isn_arg.string);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007235 break;
7236 case ISN_PUT:
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01007237 if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE)
Bram Moolenaar4c137212021-04-19 16:48:48 +02007238 smsg("%s%4d PUT %c above range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007239 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007240 else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE)
7241 smsg("%s%4d PUT %c range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007242 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007243 else
7244 smsg("%s%4d PUT %c %ld", pfx, current,
7245 iptr->isn_arg.put.put_regname,
7246 (long)iptr->isn_arg.put.put_lnum);
7247 break;
7248
Bram Moolenaar4c137212021-04-19 16:48:48 +02007249 case ISN_CMDMOD:
7250 {
7251 char_u *buf;
7252 size_t len = produce_cmdmods(
7253 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
7254
7255 buf = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02007256 if (likely(buf != NULL))
Bram Moolenaar4c137212021-04-19 16:48:48 +02007257 {
7258 (void)produce_cmdmods(
7259 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
7260 smsg("%s%4d CMDMOD %s", pfx, current, buf);
7261 vim_free(buf);
7262 }
7263 break;
7264 }
7265 case ISN_CMDMOD_REV: smsg("%s%4d CMDMOD_REV", pfx, current); break;
7266
7267 case ISN_PROF_START:
Bram Moolenaare99d4222021-06-13 14:01:26 +02007268 smsg("%s%4d PROFILE START line %d", pfx, current,
7269 iptr->isn_lnum);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007270 break;
7271
7272 case ISN_PROF_END:
7273 smsg("%s%4d PROFILE END", pfx, current);
7274 break;
7275
Bram Moolenaare99d4222021-06-13 14:01:26 +02007276 case ISN_DEBUG:
Bram Moolenaar8cec9272021-06-23 20:20:53 +02007277 smsg("%s%4d DEBUG line %d-%d varcount %lld", pfx, current,
7278 iptr->isn_arg.debug.dbg_break_lnum + 1,
7279 iptr->isn_lnum,
7280 iptr->isn_arg.debug.dbg_var_names_len);
Bram Moolenaare99d4222021-06-13 14:01:26 +02007281 break;
7282
Bram Moolenaar4c137212021-04-19 16:48:48 +02007283 case ISN_UNPACK: smsg("%s%4d UNPACK %d%s", pfx, current,
7284 iptr->isn_arg.unpack.unp_count,
7285 iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : "");
7286 break;
7287 case ISN_SHUFFLE: smsg("%s%4d SHUFFLE %d up %d", pfx, current,
7288 iptr->isn_arg.shuffle.shfl_item,
7289 iptr->isn_arg.shuffle.shfl_up);
7290 break;
7291 case ISN_DROP: smsg("%s%4d DROP", pfx, current); break;
7292
7293 case ISN_FINISH: // End of list of instructions for ISN_SUBSTITUTE.
7294 return;
7295 }
7296
7297 out_flush(); // output one line at a time
7298 ui_breakcheck();
7299 if (got_int)
7300 break;
7301 }
7302}
7303
7304/*
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02007305 * Handle command line completion for the :disassemble command.
7306 */
7307 void
7308set_context_in_disassemble_cmd(expand_T *xp, char_u *arg)
7309{
7310 char_u *p;
7311
7312 // Default: expand user functions, "debug" and "profile"
7313 xp->xp_context = EXPAND_DISASSEMBLE;
7314 xp->xp_pattern = arg;
7315
7316 // first argument already typed: only user function names
7317 if (*arg != NUL && *(p = skiptowhite(arg)) != NUL)
7318 {
7319 xp->xp_context = EXPAND_USER_FUNC;
7320 xp->xp_pattern = skipwhite(p);
7321 }
7322}
7323
7324/*
7325 * Function given to ExpandGeneric() to obtain the list of :disassemble
7326 * arguments.
7327 */
7328 char_u *
7329get_disassemble_argument(expand_T *xp, int idx)
7330{
7331 if (idx == 0)
7332 return (char_u *)"debug";
7333 if (idx == 1)
7334 return (char_u *)"profile";
7335 return get_user_func_name(xp, idx - 2);
7336}
7337
7338/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01007339 * ":disassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01007340 * We don't really need this at runtime, but we do have tests that require it,
7341 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007342 */
7343 void
7344ex_disassemble(exarg_T *eap)
7345{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01007346 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01007347 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007348 dfunc_T *dfunc;
Bram Moolenaardafef512022-05-21 21:55:55 +01007349 isn_T *instr = NULL; // init to shut up gcc warning
7350 int instr_count = 0; // init to shut up gcc warning
Bram Moolenaar5a01caa2022-05-21 18:56:58 +01007351 compiletype_T compile_type = CT_NONE;
Bram Moolenaare99d4222021-06-13 14:01:26 +02007352
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01007353 ufunc = find_func_by_name(arg, &compile_type);
Bram Moolenaara26b9702020-04-18 19:53:28 +02007354 if (ufunc == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007355 return;
Bram Moolenaare99d4222021-06-13 14:01:26 +02007356 if (func_needs_compiling(ufunc, compile_type)
7357 && compile_def_function(ufunc, FALSE, compile_type, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02007358 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007359 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007360 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007361 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007362 return;
7363 }
Bram Moolenaar6db660b2021-08-01 14:08:54 +02007364 msg((char *)printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007365
7366 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
Bram Moolenaare99d4222021-06-13 14:01:26 +02007367 switch (compile_type)
7368 {
7369 case CT_PROFILE:
Bram Moolenaarf002a412021-01-24 13:34:18 +01007370#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02007371 instr = dfunc->df_instr_prof;
7372 instr_count = dfunc->df_instr_prof_count;
7373 break;
Bram Moolenaarf002a412021-01-24 13:34:18 +01007374#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02007375 // FALLTHROUGH
7376 case CT_NONE:
7377 instr = dfunc->df_instr;
7378 instr_count = dfunc->df_instr_count;
7379 break;
7380 case CT_DEBUG:
7381 instr = dfunc->df_instr_debug;
7382 instr_count = dfunc->df_instr_debug_count;
7383 break;
7384 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007385
Bram Moolenaar4c137212021-04-19 16:48:48 +02007386 list_instructions("", instr, instr_count, ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007387}
7388
7389/*
Bram Moolenaar13106602020-10-04 16:06:05 +02007390 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007391 * list, etc. Mostly like what JavaScript does, except that empty list and
7392 * empty dictionary are FALSE.
7393 */
7394 int
7395tv2bool(typval_T *tv)
7396{
7397 switch (tv->v_type)
7398 {
7399 case VAR_NUMBER:
7400 return tv->vval.v_number != 0;
7401 case VAR_FLOAT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007402 return tv->vval.v_float != 0.0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007403 case VAR_PARTIAL:
7404 return tv->vval.v_partial != NULL;
7405 case VAR_FUNC:
7406 case VAR_STRING:
7407 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
7408 case VAR_LIST:
7409 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
7410 case VAR_DICT:
7411 return tv->vval.v_dict != NULL
7412 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
7413 case VAR_BOOL:
7414 case VAR_SPECIAL:
7415 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
7416 case VAR_JOB:
7417#ifdef FEAT_JOB_CHANNEL
7418 return tv->vval.v_job != NULL;
7419#else
7420 break;
7421#endif
7422 case VAR_CHANNEL:
7423#ifdef FEAT_JOB_CHANNEL
7424 return tv->vval.v_channel != NULL;
7425#else
7426 break;
7427#endif
7428 case VAR_BLOB:
7429 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
7430 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02007431 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007432 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02007433 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00007434 case VAR_CLASS:
7435 case VAR_OBJECT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007436 break;
7437 }
7438 return FALSE;
7439}
7440
Bram Moolenaarea2d4072020-11-12 12:08:51 +01007441 void
7442emsg_using_string_as(typval_T *tv, int as_number)
7443{
7444 semsg(_(as_number ? e_using_string_as_number_str
7445 : e_using_string_as_bool_str),
7446 tv->vval.v_string == NULL
7447 ? (char_u *)"" : tv->vval.v_string);
7448}
7449
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007450/*
7451 * If "tv" is a string give an error and return FAIL.
7452 */
7453 int
7454check_not_string(typval_T *tv)
7455{
7456 if (tv->v_type == VAR_STRING)
7457 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +01007458 emsg_using_string_as(tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007459 clear_tv(tv);
7460 return FAIL;
7461 }
7462 return OK;
7463}
7464
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007465
7466#endif // FEAT_EVAL