blob: 826282241ba785ea7697f872c54cab30d2bd58c3 [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
Yegappan Lakshmanan1087b8c2023-10-07 22:03:18 +0200562 // The object pointer is in the execution typval stack. The GA_GROW call
563 // above may have reallocated the execution typval stack. So the object
564 // pointer may not be valid anymore. Get the object pointer again from the
565 // execution stack.
566 obj = STACK_TV_BOT(0) - argcount - vararg_count - 1;
567
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100568 // If depth of calling is getting too high, don't execute the function.
569 if (funcdepth_increment() == FAIL)
570 return FAIL;
Bram Moolenaare99d4222021-06-13 14:01:26 +0200571 ++ex_nesting_level;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100572
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100573 // Only make a copy of funclocal if it contains something to restore.
Bram Moolenaar4c137212021-04-19 16:48:48 +0200574 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100575 {
576 floc = ALLOC_ONE(funclocal_T);
577 if (floc == NULL)
578 return FAIL;
Bram Moolenaar4c137212021-04-19 16:48:48 +0200579 *floc = ectx->ec_funclocal;
580 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100581 }
582
Bram Moolenaarfe270812020-04-11 22:31:27 +0200583 // Move the vararg-list to below the missing optional arguments.
584 if (vararg_count > 0 && arg_to_add > 0)
585 *STACK_TV_BOT(arg_to_add - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100586
587 // Reserve space for omitted optional arguments, filled in soon.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200588 for (idx = 0; idx < arg_to_add; ++idx)
Bram Moolenaarfe270812020-04-11 22:31:27 +0200589 STACK_TV_BOT(idx - vararg_count)->v_type = VAR_UNKNOWN;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200590 ectx->ec_stack.ga_len += arg_to_add;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100591
592 // Store current execution state in stack frame for ISN_RETURN.
Bram Moolenaar0186e582021-01-10 18:33:11 +0100593 STACK_TV_BOT(STACK_FRAME_FUNC_OFF)->vval.v_number = ectx->ec_dfunc_idx;
594 STACK_TV_BOT(STACK_FRAME_IIDX_OFF)->vval.v_number = ectx->ec_iidx;
Bram Moolenaar5930ddc2021-04-26 20:32:59 +0200595 STACK_TV_BOT(STACK_FRAME_INSTR_OFF)->vval.v_string = (void *)ectx->ec_instr;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200596 STACK_TV_BOT(STACK_FRAME_OUTER_OFF)->vval.v_string =
597 (void *)ectx->ec_outer_ref;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100598 STACK_TV_BOT(STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string = (void *)floc;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100599 STACK_TV_BOT(STACK_FRAME_IDX_OFF)->vval.v_number = ectx->ec_frame_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200600 ectx->ec_frame_idx = ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100601
Bram Moolenaar5800c792022-09-22 17:34:01 +0100602 // Initialize all local variables to number zero. Also initialize the
603 // variable that counts how many closures were created. This is used in
604 // handle_closure_in_use().
605 int initcount = dfunc->df_varcount + (dfunc->df_has_closure ? 1 : 0);
606 for (idx = 0; idx < initcount; ++idx)
Bram Moolenaar5cd64792021-12-25 18:23:24 +0000607 {
608 typval_T *tv = STACK_TV_BOT(STACK_FRAME_SIZE + idx);
609
610 tv->v_type = VAR_NUMBER;
611 tv->vval.v_number = 0;
612 }
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200613 ectx->ec_stack.ga_len += STACK_FRAME_SIZE + varcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100614
Bram Moolenaar574950d2023-01-03 19:08:50 +0000615 // For an object method move the object from just before the arguments to
616 // the first local variable.
Yegappan Lakshmanan1db15142023-09-19 20:34:05 +0200617 if (IS_OBJECT_METHOD(ufunc))
Bram Moolenaar574950d2023-01-03 19:08:50 +0000618 {
619 *STACK_TV_VAR(0) = *obj;
620 obj->v_type = VAR_UNKNOWN;
621 }
622
Bram Moolenaar5800c792022-09-22 17:34:01 +0100623 partial_T *pt = pt_arg != NULL ? pt_arg : ufunc->uf_partial;
624 if (pt != NULL || (ufunc->uf_flags & FC_CLOSURE))
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100625 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200626 outer_ref_T *ref = ALLOC_CLEAR_ONE(outer_ref_T);
Bram Moolenaar0186e582021-01-10 18:33:11 +0100627
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200628 if (ref == NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +0100629 return FAIL;
630 if (pt != NULL)
631 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +0000632 ref->or_outer = get_pt_outer(pt);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200633 ++pt->pt_refcount;
634 ref->or_partial = pt;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100635 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100636 else
637 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200638 ref->or_outer = ALLOC_CLEAR_ONE(outer_T);
Dominique Pelle5a9e5842021-07-24 19:32:12 +0200639 if (unlikely(ref->or_outer == NULL))
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200640 {
641 vim_free(ref);
642 return FAIL;
643 }
644 ref->or_outer_allocated = TRUE;
645 ref->or_outer->out_stack = &ectx->ec_stack;
646 ref->or_outer->out_frame_idx = ectx->ec_frame_idx;
647 if (ectx->ec_outer_ref != NULL)
648 ref->or_outer->out_up = ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100649 }
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200650 ectx->ec_outer_ref = ref;
Bram Moolenaarab360522021-01-10 14:02:28 +0100651 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100652 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200653 ectx->ec_outer_ref = NULL;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100654
Bram Moolenaarc970e422021-03-17 15:03:04 +0100655 ++ufunc->uf_calls;
656
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100657 // Set execution state to the start of the called function.
658 ectx->ec_dfunc_idx = cdf_idx;
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100659 ectx->ec_instr = INSTRUCTIONS(dfunc);
Bram Moolenaarb2049902021-01-24 12:53:53 +0100660 entry = estack_push_ufunc(ufunc, 1);
Bram Moolenaarc620c052020-07-08 15:16:19 +0200661 if (entry != NULL)
662 {
663 // Set the script context to the script where the function was defined.
Bram Moolenaarc70fe462021-04-17 17:59:19 +0200664 // Save the current context so it can be restored on return.
665 entry->es_save_sctx = current_sctx;
666 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200667 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100668
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +0200669 // Start execution at the first instruction.
670 ectx->ec_iidx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100671
672 return OK;
673}
674
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000675// Double linked list of funcstack_T in use.
676static funcstack_T *first_funcstack = NULL;
677
678 static void
679add_funcstack_to_list(funcstack_T *funcstack)
680{
681 // Link in list of funcstacks.
682 if (first_funcstack != NULL)
683 first_funcstack->fs_prev = funcstack;
684 funcstack->fs_next = first_funcstack;
685 funcstack->fs_prev = NULL;
686 first_funcstack = funcstack;
687}
688
689 static void
690remove_funcstack_from_list(funcstack_T *funcstack)
691{
692 if (funcstack->fs_prev == NULL)
693 first_funcstack = funcstack->fs_next;
694 else
695 funcstack->fs_prev->fs_next = funcstack->fs_next;
696 if (funcstack->fs_next != NULL)
697 funcstack->fs_next->fs_prev = funcstack->fs_prev;
698}
699
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100700/*
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200701 * Used when returning from a function: Check if any closure is still
702 * referenced. If so then move the arguments and variables to a separate piece
703 * of stack to be used when the closure is called.
704 * When "free_arguments" is TRUE the arguments are to be freed.
705 * Returns FAIL when out of memory.
706 */
707 static int
708handle_closure_in_use(ectx_T *ectx, int free_arguments)
709{
710 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
711 + ectx->ec_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200712 int argcount;
713 int top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200714 int idx;
715 typval_T *tv;
716 int closure_in_use = FALSE;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200717 garray_T *gap = &ectx->ec_funcrefs;
718 varnumber_T closure_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200719
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200720 if (dfunc->df_ufunc == NULL)
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200721 return OK; // function was freed
722 if (dfunc->df_has_closure == 0)
723 return OK; // no closures
724 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + dfunc->df_varcount);
725 closure_count = tv->vval.v_number;
726 if (closure_count == 0)
727 return OK; // no funcrefs created
728
Bram Moolenaar8fa745e2022-09-16 19:04:24 +0100729 // Compute "top": the first entry in the stack used by the function.
730 // This is the first argument (after that comes the stack frame and then
731 // the local variables).
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200732 argcount = ufunc_argcount(dfunc->df_ufunc);
733 top = ectx->ec_frame_idx - argcount;
734
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200735 // Check if any created closure is still in use.
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200736 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200737 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200738 partial_T *pt;
739 int off = gap->ga_len - closure_count + idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200740
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200741 if (off < 0)
742 continue; // count is off or already done
743 pt = ((partial_T **)gap->ga_data)[off];
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200744 if (pt->pt_refcount > 1)
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200745 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200746 int refcount = pt->pt_refcount;
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200747 int i;
748
Dominique Pelleaf4a61a2021-12-27 17:21:41 +0000749 // A Reference in a local variable doesn't count, it gets
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200750 // unreferenced on return.
751 for (i = 0; i < dfunc->df_varcount; ++i)
752 {
753 typval_T *stv = STACK_TV(ectx->ec_frame_idx
754 + STACK_FRAME_SIZE + i);
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200755 if (stv->v_type == VAR_PARTIAL && pt == stv->vval.v_partial)
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200756 --refcount;
757 }
758 if (refcount > 1)
759 {
760 closure_in_use = TRUE;
761 break;
762 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200763 }
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200764 }
765
766 if (closure_in_use)
767 {
768 funcstack_T *funcstack = ALLOC_CLEAR_ONE(funcstack_T);
769 typval_T *stack;
770
771 // A closure is using the arguments and/or local variables.
772 // Move them to the called function.
773 if (funcstack == NULL)
774 return FAIL;
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000775
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200776 funcstack->fs_var_offset = argcount + STACK_FRAME_SIZE;
Bram Moolenaar1936c762022-09-28 15:19:10 +0100777 funcstack->fs_ga.ga_len = funcstack->fs_var_offset
778 + dfunc->df_varcount;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200779 stack = ALLOC_CLEAR_MULT(typval_T, funcstack->fs_ga.ga_len);
780 funcstack->fs_ga.ga_data = stack;
781 if (stack == NULL)
782 {
783 vim_free(funcstack);
784 return FAIL;
785 }
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000786 add_funcstack_to_list(funcstack);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200787
788 // Move or copy the arguments.
789 for (idx = 0; idx < argcount; ++idx)
790 {
791 tv = STACK_TV(top + idx);
792 if (free_arguments)
793 {
794 *(stack + idx) = *tv;
795 tv->v_type = VAR_UNKNOWN;
796 }
797 else
798 copy_tv(tv, stack + idx);
799 }
Bram Moolenaar8fa745e2022-09-16 19:04:24 +0100800 // Skip the stack frame.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200801 // Move the local variables.
802 for (idx = 0; idx < dfunc->df_varcount; ++idx)
803 {
804 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + idx);
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200805
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200806 // A partial created for a local function, that is also used as a
807 // local variable, has a reference count for the variable, thus
808 // will never go down to zero. When all these refcounts are one
809 // then the funcstack is unused. We need to count how many we have
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000810 // so we know when to check.
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200811 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL)
812 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200813 int i;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200814
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200815 for (i = 0; i < closure_count; ++i)
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200816 if (tv->vval.v_partial == ((partial_T **)gap->ga_data)[
817 gap->ga_len - closure_count + i])
818 ++funcstack->fs_min_refcount;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200819 }
820
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200821 *(stack + funcstack->fs_var_offset + idx) = *tv;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200822 tv->v_type = VAR_UNKNOWN;
823 }
824
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200825 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200826 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200827 partial_T *pt = ((partial_T **)gap->ga_data)[gap->ga_len
828 - closure_count + idx];
829 if (pt->pt_refcount > 1)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200830 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200831 ++funcstack->fs_refcount;
832 pt->pt_funcstack = funcstack;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100833 pt->pt_outer.out_stack = &funcstack->fs_ga;
834 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx - top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200835 }
836 }
837 }
838
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200839 for (idx = 0; idx < closure_count; ++idx)
840 partial_unref(((partial_T **)gap->ga_data)[gap->ga_len
841 - closure_count + idx]);
842 gap->ga_len -= closure_count;
843 if (gap->ga_len == 0)
844 ga_clear(gap);
845
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200846 return OK;
847}
848
849/*
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200850 * Called when a partial is freed or its reference count goes down to one. The
851 * funcstack may be the only reference to the partials in the local variables.
852 * Go over all of them, the funcref and can be freed if all partials
853 * referencing the funcstack have a reference count of one.
Bram Moolenaaracd6b992022-09-17 16:27:39 +0100854 * Returns TRUE if the funcstack is freed, the partial referencing it will then
855 * also have been freed.
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200856 */
Bram Moolenaaracd6b992022-09-17 16:27:39 +0100857 int
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200858funcstack_check_refcount(funcstack_T *funcstack)
859{
Bram Moolenaaracd6b992022-09-17 16:27:39 +0100860 int i;
861 garray_T *gap = &funcstack->fs_ga;
862 int done = 0;
863 typval_T *stack;
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200864
865 if (funcstack->fs_refcount > funcstack->fs_min_refcount)
Bram Moolenaaracd6b992022-09-17 16:27:39 +0100866 return FALSE;
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200867 for (i = funcstack->fs_var_offset; i < gap->ga_len; ++i)
868 {
869 typval_T *tv = ((typval_T *)gap->ga_data) + i;
870
871 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL
872 && tv->vval.v_partial->pt_funcstack == funcstack
873 && tv->vval.v_partial->pt_refcount == 1)
874 ++done;
875 }
Bram Moolenaaracd6b992022-09-17 16:27:39 +0100876 if (done != funcstack->fs_min_refcount)
877 return FALSE;
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200878
Bram Moolenaaracd6b992022-09-17 16:27:39 +0100879 stack = gap->ga_data;
880
881 // All partials referencing the funcstack have a reference count of
882 // one, thus the funcstack is no longer of use.
883 for (i = 0; i < gap->ga_len; ++i)
884 clear_tv(stack + i);
885 vim_free(stack);
886 remove_funcstack_from_list(funcstack);
887 vim_free(funcstack);
888
889 return TRUE;
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200890}
891
892/*
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000893 * For garbage collecting: set references in all variables referenced by
894 * all funcstacks.
895 */
896 int
897set_ref_in_funcstacks(int copyID)
898{
899 funcstack_T *funcstack;
900
901 for (funcstack = first_funcstack; funcstack != NULL;
902 funcstack = funcstack->fs_next)
903 {
904 typval_T *stack = funcstack->fs_ga.ga_data;
905 int i;
906
907 for (i = 0; i < funcstack->fs_ga.ga_len; ++i)
908 if (set_ref_in_item(stack + i, copyID, NULL, NULL))
909 return TRUE; // abort
910 }
911 return FALSE;
912}
913
Bram Moolenaar806a2732022-09-04 15:40:36 +0100914// Ugly static to avoid passing the execution context around through many
915// layers.
916static ectx_T *current_ectx = NULL;
917
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000918/*
Bram Moolenaar806a2732022-09-04 15:40:36 +0100919 * Return TRUE if currently executing a :def function.
920 * Can be used by builtin functions only.
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100921 */
Bram Moolenaar806a2732022-09-04 15:40:36 +0100922 int
923in_def_function(void)
924{
925 return current_ectx != NULL;
926}
927
928/*
Bram Moolenaar98aff652022-09-06 21:02:35 +0100929 * Clear "current_ectx" and return the previous value. To be used when calling
930 * a user function.
931 */
932 ectx_T *
dundargocc57b5bc2022-11-02 13:30:51 +0000933clear_current_ectx(void)
Bram Moolenaar98aff652022-09-06 21:02:35 +0100934{
935 ectx_T *r = current_ectx;
936
937 current_ectx = NULL;
938 return r;
939}
940
941 void
942restore_current_ectx(ectx_T *ectx)
943{
944 if (current_ectx != NULL)
945 iemsg("Restoring current_ectx while it is not NULL");
946 current_ectx = ectx;
947}
948
949/*
Bram Moolenaar806a2732022-09-04 15:40:36 +0100950 * Add an entry for a deferred function call to the currently executing
951 * function.
952 * Return the list or NULL when failed.
953 */
954 static list_T *
955add_defer_item(int var_idx, int argcount, ectx_T *ectx)
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100956{
957 typval_T *defer_tv = STACK_TV_VAR(var_idx);
958 list_T *defer_l;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100959 list_T *l;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100960 typval_T listval;
961
962 if (defer_tv->v_type != VAR_LIST)
963 {
Bram Moolenaara5348f22022-09-04 11:42:22 +0100964 // first time, allocate the list
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100965 if (rettv_list_alloc(defer_tv) == FAIL)
Bram Moolenaar806a2732022-09-04 15:40:36 +0100966 return NULL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100967 }
968 defer_l = defer_tv->vval.v_list;
969
970 l = list_alloc_with_items(argcount + 1);
971 if (l == NULL)
Bram Moolenaar806a2732022-09-04 15:40:36 +0100972 return NULL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100973 listval.v_type = VAR_LIST;
974 listval.vval.v_list = l;
975 listval.v_lock = 0;
Bram Moolenaara5348f22022-09-04 11:42:22 +0100976 if (list_insert_tv(defer_l, &listval, defer_l->lv_first) == FAIL)
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100977 {
978 vim_free(l);
Bram Moolenaar806a2732022-09-04 15:40:36 +0100979 return NULL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100980 }
981
Bram Moolenaar806a2732022-09-04 15:40:36 +0100982 return l;
983}
984
985/*
986 * Handle ISN_DEFER. Stack has a function reference and "argcount" arguments.
987 * The local variable that lists deferred functions is "var_idx".
988 * Returns OK or FAIL.
989 */
990 static int
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +0000991defer_command(int var_idx, int has_obj, int argcount, ectx_T *ectx)
Bram Moolenaar806a2732022-09-04 15:40:36 +0100992{
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +0000993 int obj_off = has_obj ? 1 : 0;
994 list_T *l = add_defer_item(var_idx, argcount + obj_off, ectx);
Bram Moolenaar806a2732022-09-04 15:40:36 +0100995 int i;
996 typval_T *func_tv;
997
998 if (l == NULL)
999 return FAIL;
1000
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001001 func_tv = STACK_TV_BOT(-argcount - 1);
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001002 if (has_obj ? func_tv->v_type != VAR_PARTIAL : func_tv->v_type != VAR_FUNC)
Bram Moolenaarf5fec052022-09-11 11:49:22 +01001003 {
1004 semsg(_(e_expected_str_but_got_str),
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001005 has_obj ? "partial" : "function",
Bram Moolenaarf5fec052022-09-11 11:49:22 +01001006 vartype_name(func_tv->v_type));
1007 return FAIL;
1008 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001009 list_set_item(l, 0, func_tv);
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001010 if (has_obj)
1011 list_set_item(l, 1, STACK_TV_BOT(-argcount - 2));
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001012
Bram Moolenaarf5fec052022-09-11 11:49:22 +01001013 for (i = 0; i < argcount; ++i)
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001014 list_set_item(l, i + 1 + obj_off, STACK_TV_BOT(-argcount + i));
1015 ectx->ec_stack.ga_len -= argcount + 1 + obj_off;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001016 return OK;
1017}
1018
1019/*
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001020 * Add a deferred call for "name" with arguments "argvars[argcount]".
Bram Moolenaar806a2732022-09-04 15:40:36 +01001021 * Consumes "name", also on failure.
1022 * Only to be called when in_def_function() returns TRUE.
1023 */
1024 int
1025add_defer_function(char_u *name, int argcount, typval_T *argvars)
1026{
1027 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1028 + current_ectx->ec_dfunc_idx;
1029 list_T *l;
1030 typval_T func_tv;
1031 int i;
1032
1033 if (dfunc->df_defer_var_idx == 0)
1034 {
1035 iemsg("df_defer_var_idx is zero");
Bram Moolenaar31ea6bf2022-09-05 10:47:13 +01001036 vim_free(name);
Bram Moolenaar806a2732022-09-04 15:40:36 +01001037 return FAIL;
1038 }
Bram Moolenaar806a2732022-09-04 15:40:36 +01001039
Bram Moolenaarf5fec052022-09-11 11:49:22 +01001040 l = add_defer_item(dfunc->df_defer_var_idx - 1, argcount, current_ectx);
Bram Moolenaar806a2732022-09-04 15:40:36 +01001041 if (l == NULL)
1042 {
Bram Moolenaar31ea6bf2022-09-05 10:47:13 +01001043 vim_free(name);
Bram Moolenaar806a2732022-09-04 15:40:36 +01001044 return FAIL;
1045 }
1046
Bram Moolenaar31ea6bf2022-09-05 10:47:13 +01001047 func_tv.v_type = VAR_FUNC;
1048 func_tv.v_lock = 0;
1049 func_tv.vval.v_string = name;
Bram Moolenaar806a2732022-09-04 15:40:36 +01001050 list_set_item(l, 0, &func_tv);
Bram Moolenaar31ea6bf2022-09-05 10:47:13 +01001051
Bram Moolenaar806a2732022-09-04 15:40:36 +01001052 for (i = 0; i < argcount; ++i)
1053 list_set_item(l, i + 1, argvars + i);
1054 return OK;
1055}
1056
1057/*
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001058 * Invoked when returning from a function: Invoke any deferred calls.
1059 */
1060 static void
1061invoke_defer_funcs(ectx_T *ectx)
1062{
1063 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1064 + ectx->ec_dfunc_idx;
1065 typval_T *defer_tv = STACK_TV_VAR(dfunc->df_defer_var_idx - 1);
1066 listitem_T *li;
1067
1068 if (defer_tv->v_type != VAR_LIST)
1069 return; // no function added
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00001070 FOR_ALL_LIST_ITEMS(defer_tv->vval.v_list, li)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001071 {
1072 list_T *l = li->li_tv.vval.v_list;
1073 typval_T rettv;
1074 typval_T argvars[MAX_FUNC_ARGS];
1075 int i;
1076 listitem_T *arg_li = l->lv_first;
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001077 typval_T *functv = &l->lv_first->li_tv;
1078 int obj_off = functv->v_type == VAR_PARTIAL ? 1 : 0;
1079 int argcount = l->lv_len - 1 - obj_off;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001080
zeertzjqa1f2b5d2023-04-18 21:04:53 +01001081 if (functv->vval.v_string == NULL)
1082 // already being called, can happen if function does ":qa"
1083 continue;
1084
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001085 if (obj_off == 1)
1086 arg_li = arg_li->li_next; // second list item is the object
1087 for (i = 0; i < argcount; ++i)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001088 {
1089 arg_li = arg_li->li_next;
1090 argvars[i] = arg_li->li_tv;
1091 }
1092
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001093 funcexe_T funcexe;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001094 CLEAR_FIELD(funcexe);
1095 funcexe.fe_evaluate = TRUE;
1096 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001097 if (functv->v_type == VAR_PARTIAL)
1098 {
1099 funcexe.fe_partial = functv->vval.v_partial;
1100 funcexe.fe_object = l->lv_first->li_next->li_tv.vval.v_object;
1101 if (funcexe.fe_object != NULL)
1102 ++funcexe.fe_object->obj_refcount;
1103 }
zeertzjqa1f2b5d2023-04-18 21:04:53 +01001104
1105 char_u *name = functv->vval.v_string;
1106 functv->vval.v_string = NULL;
1107
1108 (void)call_func(name, -1, &rettv, argcount, argvars, &funcexe);
1109
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001110 clear_tv(&rettv);
zeertzjqa1f2b5d2023-04-18 21:04:53 +01001111 vim_free(name);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001112 }
1113}
1114
1115/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001116 * Return from the current function.
1117 */
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001118 static int
Bram Moolenaar4c137212021-04-19 16:48:48 +02001119func_return(ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001120{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001121 int idx;
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01001122 int ret_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001123 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1124 + ectx->ec_dfunc_idx;
1125 int argcount = ufunc_argcount(dfunc->df_ufunc);
Bram Moolenaarc620c052020-07-08 15:16:19 +02001126 estack_T *entry;
Bram Moolenaar12d26532021-02-19 19:13:21 +01001127 int prev_dfunc_idx = STACK_TV(ectx->ec_frame_idx
1128 + STACK_FRAME_FUNC_OFF)->vval.v_number;
Bram Moolenaarb06b50d2021-04-26 21:39:25 +02001129 funclocal_T *floc;
1130#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01001131 dfunc_T *prev_dfunc = ((dfunc_T *)def_functions.ga_data)
1132 + prev_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001133
Bram Moolenaar12d26532021-02-19 19:13:21 +01001134 if (do_profiling == PROF_YES)
1135 {
1136 ufunc_T *caller = prev_dfunc->df_ufunc;
1137
1138 if (dfunc->df_ufunc->uf_profiling
1139 || (caller != NULL && caller->uf_profiling))
1140 {
1141 profile_may_end_func(((profinfo_T *)profile_info_ga.ga_data)
1142 + profile_info_ga.ga_len - 1, dfunc->df_ufunc, caller);
1143 --profile_info_ga.ga_len;
1144 }
1145 }
1146#endif
Bram Moolenaar919c12c2021-12-14 14:29:16 +00001147
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001148 if (dfunc->df_defer_var_idx > 0)
1149 invoke_defer_funcs(ectx);
1150
Bram Moolenaar919c12c2021-12-14 14:29:16 +00001151 // No check for uf_refcount being zero, cannot think of a way that would
1152 // happen.
Bram Moolenaarc970e422021-03-17 15:03:04 +01001153 --dfunc->df_ufunc->uf_calls;
1154
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001155 // execution context goes one level up
Bram Moolenaarc620c052020-07-08 15:16:19 +02001156 entry = estack_pop();
1157 if (entry != NULL)
Bram Moolenaarc70fe462021-04-17 17:59:19 +02001158 current_sctx = entry->es_save_sctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001159
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001160 if (handle_closure_in_use(ectx, TRUE) == FAIL)
1161 return FAIL;
1162
Bram Moolenaar574950d2023-01-03 19:08:50 +00001163 // Clear the arguments. If this was an object method also clear the
1164 // object, it is just before the arguments.
1165 int top = ectx->ec_frame_idx - argcount;
Yegappan Lakshmanan1db15142023-09-19 20:34:05 +02001166 if (IS_OBJECT_METHOD(dfunc->df_ufunc))
Bram Moolenaar574950d2023-01-03 19:08:50 +00001167 --top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001168 for (idx = top; idx < ectx->ec_frame_idx; ++idx)
1169 clear_tv(STACK_TV(idx));
1170
1171 // Clear local variables and temp values, but not the return value.
1172 for (idx = ectx->ec_frame_idx + STACK_FRAME_SIZE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001173 idx < ectx->ec_stack.ga_len - 1; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001174 clear_tv(STACK_TV(idx));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001175
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01001176 // The return value should be on top of the stack. However, when aborting
1177 // it may not be there and ec_frame_idx is the top of the stack.
1178 ret_idx = ectx->ec_stack.ga_len - 1;
Bram Moolenaar0186e582021-01-10 18:33:11 +01001179 if (ret_idx == ectx->ec_frame_idx + STACK_FRAME_IDX_OFF)
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01001180 ret_idx = 0;
1181
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001182 if (ectx->ec_outer_ref != NULL)
1183 {
1184 if (ectx->ec_outer_ref->or_outer_allocated)
1185 vim_free(ectx->ec_outer_ref->or_outer);
1186 partial_unref(ectx->ec_outer_ref->or_partial);
1187 vim_free(ectx->ec_outer_ref);
1188 }
Bram Moolenaar0186e582021-01-10 18:33:11 +01001189
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001190 // Restore the previous frame.
Bram Moolenaar12d26532021-02-19 19:13:21 +01001191 ectx->ec_dfunc_idx = prev_dfunc_idx;
Bram Moolenaar0186e582021-01-10 18:33:11 +01001192 ectx->ec_iidx = STACK_TV(ectx->ec_frame_idx
1193 + STACK_FRAME_IIDX_OFF)->vval.v_number;
Bram Moolenaar5930ddc2021-04-26 20:32:59 +02001194 ectx->ec_instr = (void *)STACK_TV(ectx->ec_frame_idx
1195 + STACK_FRAME_INSTR_OFF)->vval.v_string;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001196 ectx->ec_outer_ref = (void *)STACK_TV(ectx->ec_frame_idx
Bram Moolenaar0186e582021-01-10 18:33:11 +01001197 + STACK_FRAME_OUTER_OFF)->vval.v_string;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001198 floc = (void *)STACK_TV(ectx->ec_frame_idx
1199 + STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string;
Bram Moolenaar5366e1a2020-10-01 13:01:34 +02001200 // restoring ec_frame_idx must be last
Bram Moolenaar0186e582021-01-10 18:33:11 +01001201 ectx->ec_frame_idx = STACK_TV(ectx->ec_frame_idx
1202 + STACK_FRAME_IDX_OFF)->vval.v_number;
Bram Moolenaard386e922021-04-25 14:48:49 +02001203
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001204 if (floc == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02001205 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001206 else
1207 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02001208 ectx->ec_funclocal = *floc;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001209 vim_free(floc);
1210 }
1211
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01001212 if (ret_idx > 0)
1213 {
1214 // Reset the stack to the position before the call, with a spot for the
1215 // return value, moved there from above the frame.
1216 ectx->ec_stack.ga_len = top + 1;
1217 *STACK_TV_BOT(-1) = *STACK_TV(ret_idx);
1218 }
1219 else
1220 // Reset the stack to the position before the call.
1221 ectx->ec_stack.ga_len = top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001222
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001223 funcdepth_decrement();
Bram Moolenaare99d4222021-06-13 14:01:26 +02001224 --ex_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001225 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001226}
1227
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001228/*
1229 * Prepare arguments and rettv for calling a builtin or user function.
1230 */
1231 static int
1232call_prepare(int argcount, typval_T *argvars, ectx_T *ectx)
1233{
1234 int idx;
1235 typval_T *tv;
1236
1237 // Move arguments from bottom of the stack to argvars[] and add terminator.
1238 for (idx = 0; idx < argcount; ++idx)
1239 argvars[idx] = *STACK_TV_BOT(idx - argcount);
1240 argvars[argcount].v_type = VAR_UNKNOWN;
1241
1242 // Result replaces the arguments on the stack.
1243 if (argcount > 0)
1244 ectx->ec_stack.ga_len -= argcount - 1;
Bram Moolenaar35578162021-08-02 19:10:38 +02001245 else if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001246 return FAIL;
1247 else
1248 ++ectx->ec_stack.ga_len;
1249
1250 // Default return value is zero.
1251 tv = STACK_TV_BOT(-1);
1252 tv->v_type = VAR_NUMBER;
1253 tv->vval.v_number = 0;
Bram Moolenaar24565cf2022-03-28 18:16:52 +01001254 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001255
1256 return OK;
1257}
1258
1259/*
1260 * Call a builtin function by index.
1261 */
1262 static int
1263call_bfunc(int func_idx, int argcount, ectx_T *ectx)
1264{
1265 typval_T argvars[MAX_FUNC_ARGS];
1266 int idx;
Bram Moolenaar171fb922020-10-28 16:54:47 +01001267 int did_emsg_before = did_emsg;
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001268 ectx_T *prev_ectx = current_ectx;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001269 char *save_func_name = ectx->ec_where.wt_func_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001270
1271 if (call_prepare(argcount, argvars, ectx) == FAIL)
1272 return FAIL;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001273 ectx->ec_where.wt_func_name = internal_func_name(func_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001274
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001275 // Call the builtin function. Set "current_ectx" so that when it
1276 // recursively invokes call_def_function() a closure context can be set.
1277 current_ectx = ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001278 call_internal_func_by_idx(func_idx, argvars, STACK_TV_BOT(-1));
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001279 current_ectx = prev_ectx;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001280 ectx->ec_where.wt_func_name = save_func_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001281
1282 // Clear the arguments.
1283 for (idx = 0; idx < argcount; ++idx)
1284 clear_tv(&argvars[idx]);
Bram Moolenaar015f4262020-05-05 21:25:22 +02001285
Bram Moolenaar57f799e2020-12-12 20:42:19 +01001286 if (did_emsg > did_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +02001287 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001288 return OK;
1289}
1290
1291/*
1292 * Execute a user defined function.
Bram Moolenaarab360522021-01-10 14:02:28 +01001293 * If the function is compiled this will add a stack frame and set the
1294 * instruction pointer at the start of the function.
1295 * Otherwise the function is called here.
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001296 * If "pt" is not null use "pt->pt_outer" for ec_outer_ref->or_outer.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001297 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001298 */
1299 static int
Bram Moolenaar0186e582021-01-10 18:33:11 +01001300call_ufunc(
1301 ufunc_T *ufunc,
1302 partial_T *pt,
1303 int argcount,
1304 ectx_T *ectx,
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001305 isn_T *iptr,
1306 dict_T *selfdict)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001307{
1308 typval_T argvars[MAX_FUNC_ARGS];
1309 funcexe_T funcexe;
Bram Moolenaar0917e862023-02-18 14:42:44 +00001310 funcerror_T error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001311 int idx;
Bram Moolenaar28ee8922020-10-28 20:20:00 +01001312 int did_emsg_before = did_emsg;
Bram Moolenaar139575d2022-03-15 19:29:30 +00001313 compiletype_T compile_type = get_compile_type(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001314
Bram Moolenaare99d4222021-06-13 14:01:26 +02001315 if (func_needs_compiling(ufunc, compile_type)
1316 && compile_def_function(ufunc, FALSE, compile_type, NULL)
1317 == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02001318 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001319 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001320 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +01001321 error = check_user_func_argcount(ufunc, argcount);
Bram Moolenaar50824712020-12-20 21:10:17 +01001322 if (error != FCERR_UNKNOWN)
1323 {
1324 if (error == FCERR_TOOMANY)
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001325 semsg(_(e_too_many_arguments_for_function_str),
1326 printable_func_name(ufunc));
Bram Moolenaar50824712020-12-20 21:10:17 +01001327 else
Bram Moolenaare1242042021-12-16 20:56:57 +00001328 semsg(_(e_not_enough_arguments_for_function_str),
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001329 printable_func_name(ufunc));
Bram Moolenaar50824712020-12-20 21:10:17 +01001330 return FAIL;
1331 }
1332
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001333 // The function has been compiled, can call it quickly. For a function
1334 // that was defined later: we can call it directly next time.
1335 if (iptr != NULL)
1336 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001337 delete_instr(iptr);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001338 iptr->isn_type = ISN_DCALL;
1339 iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1340 iptr->isn_arg.dfunc.cdf_argcount = argcount;
1341 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001342 return call_dfunc(ufunc->uf_dfunc_idx, pt, argcount, ectx);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001343 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001344
1345 if (call_prepare(argcount, argvars, ectx) == FAIL)
1346 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +02001347 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00001348 funcexe.fe_evaluate = TRUE;
1349 funcexe.fe_selfdict = selfdict != NULL ? selfdict : dict_stack_get_dict();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001350
1351 // Call the user function. Result goes in last position on the stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001352 error = call_user_func_check(ufunc, argcount, argvars,
Bram Moolenaar851f86b2021-12-13 14:26:44 +00001353 STACK_TV_BOT(-1), &funcexe, funcexe.fe_selfdict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001354
1355 // Clear the arguments.
1356 for (idx = 0; idx < argcount; ++idx)
1357 clear_tv(&argvars[idx]);
1358
1359 if (error != FCERR_NONE)
1360 {
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01001361 user_func_error(error, printable_func_name(ufunc),
1362 funcexe.fe_found_var);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001363 return FAIL;
1364 }
Bram Moolenaar28ee8922020-10-28 20:20:00 +01001365 if (did_emsg > did_emsg_before)
Bram Moolenaared677f52020-08-12 16:38:10 +02001366 // Error other than from calling the function itself.
1367 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001368 return OK;
1369}
1370
1371/*
Bram Moolenaara91a7132021-03-25 21:12:15 +01001372 * If command modifiers were applied restore them.
1373 */
1374 static void
1375may_restore_cmdmod(funclocal_T *funclocal)
1376{
1377 if (funclocal->floc_restore_cmdmod)
1378 {
1379 cmdmod.cmod_filter_regmatch.regprog = NULL;
1380 undo_cmdmod(&cmdmod);
1381 cmdmod = funclocal->floc_save_cmdmod;
1382 funclocal->floc_restore_cmdmod = FALSE;
1383 }
1384}
1385
1386/*
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001387 * Return TRUE if an error was given (not caught in try/catch) or CTRL-C was
1388 * pressed.
Bram Moolenaara1773442020-08-12 15:21:22 +02001389 */
1390 static int
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001391vim9_aborting(int prev_uncaught_emsg)
Bram Moolenaara1773442020-08-12 15:21:22 +02001392{
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001393 return uncaught_emsg > prev_uncaught_emsg || got_int || did_throw;
Bram Moolenaara1773442020-08-12 15:21:22 +02001394}
1395
1396/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001397 * Execute a function by "name".
1398 * This can be a builtin function or a user function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001399 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001400 * Returns FAIL if not found without an error message.
1401 */
1402 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001403call_by_name(
1404 char_u *name,
1405 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001406 ectx_T *ectx,
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001407 isn_T *iptr,
1408 dict_T *selfdict)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001409{
1410 ufunc_T *ufunc;
1411
1412 if (builtin_function(name, -1))
1413 {
1414 int func_idx = find_internal_func(name);
1415
Bram Moolenaar1983f1a2022-02-28 20:55:02 +00001416 if (func_idx < 0) // Impossible?
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001417 return FAIL;
Bram Moolenaar389df252020-07-09 21:20:47 +02001418 if (check_internal_func(func_idx, argcount) < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001419 return FAIL;
1420 return call_bfunc(func_idx, argcount, ectx);
1421 }
1422
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00001423 ufunc = find_func(name, FALSE);
Bram Moolenaara1773442020-08-12 15:21:22 +02001424
1425 if (ufunc == NULL)
1426 {
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001427 int prev_uncaught_emsg = uncaught_emsg;
Bram Moolenaara1773442020-08-12 15:21:22 +02001428
1429 if (script_autoload(name, TRUE))
1430 // loaded a package, search for the function again
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00001431 ufunc = find_func(name, FALSE);
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001432
1433 if (vim9_aborting(prev_uncaught_emsg))
Bram Moolenaara1773442020-08-12 15:21:22 +02001434 return FAIL; // bail out if loading the script caused an error
1435 }
1436
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001437 if (ufunc != NULL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001438 {
Bram Moolenaar0d89d8a2022-12-31 14:01:24 +00001439 if (check_ufunc_arg_types(ufunc, argcount, 0, ectx) == FAIL)
1440 return FAIL;
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001441
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001442 return call_ufunc(ufunc, NULL, argcount, ectx, iptr, selfdict);
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001443 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001444
1445 return FAIL;
1446}
1447
1448 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001449call_partial(
1450 typval_T *tv,
1451 int argcount_arg,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001452 ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001453{
Bram Moolenaara90afb92020-07-15 22:38:56 +02001454 int argcount = argcount_arg;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001455 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001456 int called_emsg_before = called_emsg;
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +01001457 int res = FAIL;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001458 dict_T *selfdict = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001459
1460 if (tv->v_type == VAR_PARTIAL)
1461 {
Bram Moolenaara90afb92020-07-15 22:38:56 +02001462 partial_T *pt = tv->vval.v_partial;
1463 int i;
1464
1465 if (pt->pt_argc > 0)
1466 {
1467 // Make space for arguments from the partial, shift the "argcount"
1468 // arguments up.
Bram Moolenaar35578162021-08-02 19:10:38 +02001469 if (GA_GROW_FAILS(&ectx->ec_stack, pt->pt_argc))
Bram Moolenaara90afb92020-07-15 22:38:56 +02001470 return FAIL;
1471 for (i = 1; i <= argcount; ++i)
1472 *STACK_TV_BOT(-i + pt->pt_argc) = *STACK_TV_BOT(-i);
1473 ectx->ec_stack.ga_len += pt->pt_argc;
1474 argcount += pt->pt_argc;
1475
1476 // copy the arguments from the partial onto the stack
1477 for (i = 0; i < pt->pt_argc; ++i)
1478 copy_tv(&pt->pt_argv[i], STACK_TV_BOT(-argcount + i));
1479 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001480 selfdict = pt->pt_dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001481
1482 if (pt->pt_func != NULL)
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001483 return call_ufunc(pt->pt_func, pt, argcount, ectx, NULL, selfdict);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001484
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001485 name = pt->pt_name;
1486 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001487 else if (tv->v_type == VAR_FUNC)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001488 name = tv->vval.v_string;
Bram Moolenaar95006e32020-08-29 17:47:08 +02001489 if (name != NULL)
1490 {
Bram Moolenaar0917e862023-02-18 14:42:44 +00001491 char_u fname_buf[FLEN_FIXED + 1];
1492 char_u *tofree = NULL;
1493 funcerror_T error = FCERR_NONE;
1494 char_u *fname;
Bram Moolenaar95006e32020-08-29 17:47:08 +02001495
1496 // May need to translate <SNR>123_ to K_SNR.
1497 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
1498 if (error != FCERR_NONE)
1499 res = FAIL;
1500 else
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001501 res = call_by_name(fname, argcount, ectx, NULL, selfdict);
Bram Moolenaar95006e32020-08-29 17:47:08 +02001502 vim_free(tofree);
1503 }
1504
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +01001505 if (res == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001506 {
1507 if (called_emsg == called_emsg_before)
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001508 emsg_funcname(e_unknown_function_str,
Bram Moolenaar015f4262020-05-05 21:25:22 +02001509 name == NULL ? (char_u *)"[unknown]" : name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001510 return FAIL;
1511 }
1512 return OK;
1513}
1514
1515/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001516 * Check if "lock" is VAR_LOCKED or VAR_FIXED. If so give an error and return
1517 * TRUE.
1518 */
1519 static int
1520error_if_locked(int lock, char *error)
1521{
1522 if (lock & (VAR_LOCKED | VAR_FIXED))
1523 {
1524 emsg(_(error));
1525 return TRUE;
1526 }
1527 return FALSE;
1528}
1529
1530/*
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001531 * Give an error if "tv" is not a number and return FAIL.
1532 */
1533 static int
1534check_for_number(typval_T *tv)
1535{
1536 if (tv->v_type != VAR_NUMBER)
1537 {
1538 semsg(_(e_expected_str_but_got_str),
1539 vartype_name(VAR_NUMBER), vartype_name(tv->v_type));
1540 return FAIL;
1541 }
1542 return OK;
1543}
1544
1545/*
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001546 * Store "tv" in variable "name".
1547 * This is for s: and g: variables.
1548 */
1549 static void
1550store_var(char_u *name, typval_T *tv)
1551{
1552 funccal_entry_T entry;
Bram Moolenaard877a572021-04-01 19:42:48 +02001553 int flags = ASSIGN_DECL;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001554
Bram Moolenaard877a572021-04-01 19:42:48 +02001555 if (tv->v_lock)
1556 flags |= ASSIGN_CONST;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001557 save_funccal(&entry);
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001558 set_var_const(name, 0, NULL, tv, FALSE, flags, 0);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001559 restore_funccal();
1560}
1561
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001562/*
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001563 * Convert "tv" to a string.
1564 * Return FAIL if not allowed.
1565 */
1566 static int
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001567do_2string(typval_T *tv, int is_2string_any, int tolerant)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001568{
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00001569 if (tv->v_type == VAR_STRING)
1570 return OK;
1571
1572 char_u *str;
1573
1574 if (is_2string_any)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001575 {
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00001576 switch (tv->v_type)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001577 {
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00001578 case VAR_SPECIAL:
1579 case VAR_BOOL:
1580 case VAR_NUMBER:
1581 case VAR_FLOAT:
1582 case VAR_BLOB: break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001583
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00001584 case VAR_LIST:
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001585 if (tolerant)
1586 {
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001587 char_u *s, *e, *p;
1588 garray_T ga;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001589
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001590 ga_init2(&ga, sizeof(char_u *), 1);
1591
1592 // Convert to NL separated items, then
1593 // escape the items and replace the NL with
1594 // a space.
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001595 str = typval2string(tv, TRUE);
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001596 if (str == NULL)
1597 return FAIL;
1598 s = str;
1599 while ((e = vim_strchr(s, '\n')) != NULL)
1600 {
1601 *e = NUL;
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +01001602 p = vim_strsave_fnameescape(s,
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00001603 VSE_NONE);
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001604 if (p != NULL)
1605 {
1606 ga_concat(&ga, p);
1607 ga_concat(&ga, (char_u *)" ");
1608 vim_free(p);
1609 }
1610 s = e + 1;
1611 }
1612 vim_free(str);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001613 clear_tv(tv);
1614 tv->v_type = VAR_STRING;
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001615 tv->vval.v_string = ga.ga_data;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001616 return OK;
1617 }
1618 // FALLTHROUGH
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00001619 default: to_string_error(tv->v_type);
1620 return FAIL;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001621 }
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001622 }
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00001623 str = typval_tostring(tv, TRUE);
1624 clear_tv(tv);
1625 tv->v_type = VAR_STRING;
1626 tv->vval.v_string = str;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001627 return OK;
1628}
1629
1630/*
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001631 * When the value of "sv" is a null list of dict, allocate it.
1632 */
1633 static void
Bram Moolenaar859cc212022-03-28 15:22:35 +01001634allocate_if_null(svar_T *sv)
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001635{
Bram Moolenaar859cc212022-03-28 15:22:35 +01001636 typval_T *tv = sv->sv_tv;
1637
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001638 switch (tv->v_type)
1639 {
1640 case VAR_LIST:
Bram Moolenaar859cc212022-03-28 15:22:35 +01001641 if (tv->vval.v_list == NULL && sv->sv_type != &t_list_empty)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001642 (void)rettv_list_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001643 break;
1644 case VAR_DICT:
Bram Moolenaar859cc212022-03-28 15:22:35 +01001645 if (tv->vval.v_dict == NULL && sv->sv_type != &t_dict_empty)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001646 (void)rettv_dict_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001647 break;
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02001648 case VAR_BLOB:
Bram Moolenaar859cc212022-03-28 15:22:35 +01001649 if (tv->vval.v_blob == NULL && sv->sv_type != &t_blob_null)
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02001650 (void)rettv_blob_alloc(tv);
1651 break;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001652 default:
1653 break;
1654 }
1655}
Bram Moolenaard3aac292020-04-19 14:32:17 +02001656
Bram Moolenaare7525c52021-01-09 13:20:37 +01001657/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001658 * Return the character "str[index]" where "index" is the character index,
1659 * including composing characters.
1660 * If "index" is out of range NULL is returned.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001661 */
1662 char_u *
1663char_from_string(char_u *str, varnumber_T index)
1664{
1665 size_t nbyte = 0;
1666 varnumber_T nchar = index;
1667 size_t slen;
1668
1669 if (str == NULL)
1670 return NULL;
1671 slen = STRLEN(str);
1672
Bram Moolenaarff871402021-03-26 13:34:05 +01001673 // Do the same as for a list: a negative index counts from the end.
1674 // Optimization to check the first byte to be below 0x80 (and no composing
1675 // character follows) makes this a lot faster.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001676 if (index < 0)
1677 {
1678 int clen = 0;
1679
1680 for (nbyte = 0; nbyte < slen; ++clen)
Bram Moolenaarff871402021-03-26 13:34:05 +01001681 {
1682 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1683 ++nbyte;
1684 else if (enc_utf8)
1685 nbyte += utfc_ptr2len(str + nbyte);
1686 else
1687 nbyte += mb_ptr2len(str + nbyte);
1688 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001689 nchar = clen + index;
1690 if (nchar < 0)
1691 // unlike list: index out of range results in empty string
1692 return NULL;
1693 }
1694
1695 for (nbyte = 0; nchar > 0 && nbyte < slen; --nchar)
Bram Moolenaarff871402021-03-26 13:34:05 +01001696 {
1697 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1698 ++nbyte;
1699 else if (enc_utf8)
1700 nbyte += utfc_ptr2len(str + nbyte);
1701 else
1702 nbyte += mb_ptr2len(str + nbyte);
1703 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001704 if (nbyte >= slen)
1705 return NULL;
Bram Moolenaar0289a092021-03-14 18:40:19 +01001706 return vim_strnsave(str + nbyte, mb_ptr2len(str + nbyte));
Bram Moolenaare7525c52021-01-09 13:20:37 +01001707}
1708
1709/*
1710 * Get the byte index for character index "idx" in string "str" with length
Bram Moolenaar0289a092021-03-14 18:40:19 +01001711 * "str_len". Composing characters are included.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001712 * If going over the end return "str_len".
1713 * If "idx" is negative count from the end, -1 is the last character.
1714 * When going over the start return -1.
1715 */
1716 static long
1717char_idx2byte(char_u *str, size_t str_len, varnumber_T idx)
1718{
1719 varnumber_T nchar = idx;
1720 size_t nbyte = 0;
1721
1722 if (nchar >= 0)
1723 {
1724 while (nchar > 0 && nbyte < str_len)
1725 {
Bram Moolenaar0289a092021-03-14 18:40:19 +01001726 nbyte += mb_ptr2len(str + nbyte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001727 --nchar;
1728 }
1729 }
1730 else
1731 {
1732 nbyte = str_len;
1733 while (nchar < 0 && nbyte > 0)
1734 {
1735 --nbyte;
1736 nbyte -= mb_head_off(str, str + nbyte);
1737 ++nchar;
1738 }
1739 if (nchar < 0)
1740 return -1;
1741 }
1742 return (long)nbyte;
1743}
1744
1745/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001746 * Return the slice "str[first : last]" using character indexes. Composing
1747 * characters are included.
Bram Moolenaar6601b622021-01-13 21:47:15 +01001748 * "exclusive" is TRUE for slice().
Bram Moolenaare7525c52021-01-09 13:20:37 +01001749 * Return NULL when the result is empty.
1750 */
1751 char_u *
Bram Moolenaar6601b622021-01-13 21:47:15 +01001752string_slice(char_u *str, varnumber_T first, varnumber_T last, int exclusive)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001753{
1754 long start_byte, end_byte;
1755 size_t slen;
1756
1757 if (str == NULL)
1758 return NULL;
1759 slen = STRLEN(str);
1760 start_byte = char_idx2byte(str, slen, first);
1761 if (start_byte < 0)
1762 start_byte = 0; // first index very negative: use zero
Bram Moolenaar6601b622021-01-13 21:47:15 +01001763 if ((last == -1 && !exclusive) || last == VARNUM_MAX)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001764 end_byte = (long)slen;
1765 else
1766 {
1767 end_byte = char_idx2byte(str, slen, last);
Bram Moolenaar6601b622021-01-13 21:47:15 +01001768 if (!exclusive && end_byte >= 0 && end_byte < (long)slen)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001769 // end index is inclusive
Bram Moolenaar0289a092021-03-14 18:40:19 +01001770 end_byte += mb_ptr2len(str + end_byte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001771 }
1772
1773 if (start_byte >= (long)slen || end_byte <= start_byte)
1774 return NULL;
1775 return vim_strnsave(str + start_byte, end_byte - start_byte);
1776}
1777
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001778/*
1779 * Get a script variable for ISN_STORESCRIPT and ISN_LOADSCRIPT.
1780 * When "dfunc_idx" is negative don't give an error.
1781 * Returns NULL for an error.
1782 */
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001783 static svar_T *
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001784get_script_svar(scriptref_T *sref, int dfunc_idx)
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001785{
1786 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001787 dfunc_T *dfunc = dfunc_idx < 0 ? NULL
1788 : ((dfunc_T *)def_functions.ga_data) + dfunc_idx;
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001789 svar_T *sv;
1790
1791 if (sref->sref_seq != si->sn_script_seq)
1792 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001793 // The script was reloaded after the function was compiled, the
1794 // script_idx may not be valid.
1795 if (dfunc != NULL)
1796 semsg(_(e_script_variable_invalid_after_reload_in_function_str),
1797 printable_func_name(dfunc->df_ufunc));
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001798 return NULL;
1799 }
1800 sv = ((svar_T *)si->sn_var_vals.ga_data) + sref->sref_idx;
Bram Moolenaar6de22962022-09-09 21:35:36 +01001801 if (sv->sv_name == NULL)
1802 {
1803 if (dfunc != NULL)
1804 emsg(_(e_script_variable_was_deleted));
1805 return NULL;
1806 }
Bram Moolenaar60dc8272021-07-29 22:48:54 +02001807 if (!equal_type(sv->sv_type, sref->sref_type, 0))
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001808 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001809 if (dfunc != NULL)
1810 emsg(_(e_script_variable_type_changed));
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001811 return NULL;
1812 }
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001813
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01001814 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0
1815 && sref->sref_sid != current_sctx.sc_sid)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001816 {
1817 if (dfunc != NULL)
1818 semsg(_(e_item_not_exported_in_script_str), sv->sv_name);
1819 return NULL;
1820 }
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001821 return sv;
1822}
1823
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001824/*
Bram Moolenaar20677332021-06-06 17:02:53 +02001825 * Function passed to do_cmdline() for splitting a script joined by NL
1826 * characters.
1827 */
1828 static char_u *
1829get_split_sourceline(
1830 int c UNUSED,
1831 void *cookie,
1832 int indent UNUSED,
1833 getline_opt_T options UNUSED)
1834{
1835 source_cookie_T *sp = (source_cookie_T *)cookie;
1836 char_u *p;
1837 char_u *line;
1838
Bram Moolenaar20677332021-06-06 17:02:53 +02001839 p = vim_strchr(sp->nextline, '\n');
1840 if (p == NULL)
1841 {
1842 line = vim_strsave(sp->nextline);
1843 sp->nextline += STRLEN(sp->nextline);
1844 }
1845 else
1846 {
1847 line = vim_strnsave(sp->nextline, p - sp->nextline);
1848 sp->nextline = p + 1;
1849 }
1850 return line;
1851}
1852
1853/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001854 * Execute a function by "name".
1855 * This can be a builtin function, user function or a funcref.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001856 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001857 */
1858 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001859call_eval_func(
1860 char_u *name,
1861 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001862 ectx_T *ectx,
1863 isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001864{
Bram Moolenaared677f52020-08-12 16:38:10 +02001865 int called_emsg_before = called_emsg;
1866 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001867
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001868 res = call_by_name(name, argcount, ectx, iptr, NULL);
Bram Moolenaared677f52020-08-12 16:38:10 +02001869 if (res == FAIL && called_emsg == called_emsg_before)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001870 {
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001871 dictitem_T *v;
1872
1873 v = find_var(name, NULL, FALSE);
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001874 if (v == NULL || (v->di_tv.v_type != VAR_PARTIAL
1875 && v->di_tv.v_type != VAR_FUNC))
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001876 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001877 emsg_funcname(e_unknown_function_str, name);
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001878 return FAIL;
1879 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001880 return call_partial(&v->di_tv, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001881 }
Bram Moolenaared677f52020-08-12 16:38:10 +02001882 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001883}
1884
1885/*
Bram Moolenaarf112f302020-12-20 17:47:52 +01001886 * When a function reference is used, fill a partial with the information
1887 * needed, especially when it is used as a closure.
1888 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001889 int
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001890fill_partial_and_closure(
Bram Moolenaarcc341812022-09-19 15:54:34 +01001891 partial_T *pt,
1892 ufunc_T *ufunc,
1893 loopvarinfo_T *lvi,
1894 ectx_T *ectx)
Bram Moolenaarf112f302020-12-20 17:47:52 +01001895{
1896 pt->pt_func = ufunc;
1897 pt->pt_refcount = 1;
1898
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001899 if (ufunc->uf_flags & FC_CLOSURE)
Bram Moolenaarf112f302020-12-20 17:47:52 +01001900 {
1901 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1902 + ectx->ec_dfunc_idx;
1903
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001904 // The closure may need to find arguments and local variables of the
1905 // current function in the stack.
Bram Moolenaar0186e582021-01-10 18:33:11 +01001906 pt->pt_outer.out_stack = &ectx->ec_stack;
1907 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001908 if (ectx->ec_outer_ref != NULL)
1909 {
1910 // The current context already has a context, link to that one.
1911 pt->pt_outer.out_up = ectx->ec_outer_ref->or_outer;
1912 if (ectx->ec_outer_ref->or_partial != NULL)
1913 {
1914 pt->pt_outer.out_up_partial = ectx->ec_outer_ref->or_partial;
1915 ++pt->pt_outer.out_up_partial->pt_refcount;
1916 }
1917 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01001918
Bram Moolenaarcc341812022-09-19 15:54:34 +01001919 if (lvi != NULL)
1920 {
1921 int depth;
1922
1923 // The closure may need to find variables defined inside a loop,
1924 // for every nested loop. A new reference is made every time,
1925 // ISN_ENDLOOP will check if they are actually used.
1926 for (depth = 0; depth < lvi->lvi_depth; ++depth)
1927 {
1928 pt->pt_outer.out_loop[depth].stack = &ectx->ec_stack;
1929 pt->pt_outer.out_loop[depth].var_idx = ectx->ec_frame_idx
1930 + STACK_FRAME_SIZE + lvi->lvi_loop[depth].var_idx;
1931 pt->pt_outer.out_loop[depth].var_count =
1932 lvi->lvi_loop[depth].var_count;
1933 }
Bram Moolenaar6d313be2022-09-22 16:36:25 +01001934 pt->pt_outer.out_loop_size = lvi->lvi_depth;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001935 }
Bram Moolenaar6d313be2022-09-22 16:36:25 +01001936 else
1937 pt->pt_outer.out_loop_size = 0;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001938
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001939 // If the function currently executing returns and the closure is still
1940 // being referenced, we need to make a copy of the context (arguments
1941 // and local variables) so that the closure can use it later.
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001942 // Store a reference to the partial so we can handle that.
Bram Moolenaar35578162021-08-02 19:10:38 +02001943 if (GA_GROW_FAILS(&ectx->ec_funcrefs, 1))
Bram Moolenaarf112f302020-12-20 17:47:52 +01001944 {
1945 vim_free(pt);
1946 return FAIL;
1947 }
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001948 // Extra variable keeps the count of closures created in the current
1949 // function call.
Bram Moolenaarf112f302020-12-20 17:47:52 +01001950 ++(((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx
1951 + STACK_FRAME_SIZE + dfunc->df_varcount)->vval.v_number;
1952
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001953 ((partial_T **)ectx->ec_funcrefs.ga_data)[ectx->ec_funcrefs.ga_len]
1954 = pt;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001955 ++pt->pt_refcount;
1956 ++ectx->ec_funcrefs.ga_len;
1957 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001958 ++ufunc->uf_refcount;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001959 return OK;
1960}
1961
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001962/*
1963 * Execute iptr->isn_arg.string as an Ex command.
1964 */
1965 static int
Ernie Raelee865f32023-09-29 19:53:55 +02001966exec_command(isn_T *iptr, char_u *cmd_string)
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001967{
1968 source_cookie_T cookie;
1969
1970 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00001971 // Pass getsourceline to get an error for a missing ":end" command.
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001972 CLEAR_FIELD(cookie);
1973 cookie.sourcing_lnum = iptr->isn_lnum - 1;
Ernie Raelee865f32023-09-29 19:53:55 +02001974 if (do_cmdline(cmd_string, getsourceline, &cookie,
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001975 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED) == FAIL
1976 || did_emsg)
1977 return FAIL;
1978 return OK;
1979}
1980
Bram Moolenaar89445512022-04-14 12:58:23 +01001981/*
1982 * If script "sid" is not loaded yet then load it now.
1983 * Caller must make sure "sid" is a valid script ID.
1984 * "loaded" is set to TRUE if the script had to be loaded.
1985 * Returns FAIL if loading fails, OK if already loaded or loaded now.
1986 */
1987 int
1988may_load_script(int sid, int *loaded)
1989{
1990 scriptitem_T *si = SCRIPT_ITEM(sid);
1991
1992 if (si->sn_state == SN_STATE_NOT_LOADED)
1993 {
1994 *loaded = TRUE;
1995 if (do_source(si->sn_name, FALSE, DOSO_NONE, NULL) == FAIL)
1996 {
1997 semsg(_(e_cant_open_file_str), si->sn_name);
1998 return FAIL;
1999 }
2000 }
2001 return OK;
2002}
2003
Bram Moolenaarf18332f2021-05-07 17:55:55 +02002004// used for v_instr of typval of VAR_INSTR
2005struct instr_S {
2006 ectx_T *instr_ectx;
2007 isn_T *instr_instr;
2008};
2009
Bram Moolenaar4c137212021-04-19 16:48:48 +02002010// used for substitute_instr
2011typedef struct subs_expr_S {
2012 ectx_T *subs_ectx;
2013 isn_T *subs_instr;
2014 int subs_status;
2015} subs_expr_T;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002016
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002017// Set when calling do_debug().
2018static ectx_T *debug_context = NULL;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02002019static int debug_var_count;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002020
2021/*
2022 * When debugging lookup "name" and return the typeval.
2023 * When not found return NULL.
2024 */
2025 typval_T *
2026lookup_debug_var(char_u *name)
2027{
2028 int idx;
2029 dfunc_T *dfunc;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02002030 ufunc_T *ufunc;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002031 ectx_T *ectx = debug_context;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02002032 int varargs_off;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002033
2034 if (ectx == NULL)
2035 return NULL;
2036 dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
2037
2038 // Go through the local variable names, from last to first.
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02002039 for (idx = debug_var_count - 1; idx >= 0; --idx)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002040 {
Bram Moolenaare406ff82022-03-10 20:47:43 +00002041 char_u *varname = ((char_u **)dfunc->df_var_names.ga_data)[idx];
2042
2043 // the variable name may be NULL when not available in this block
2044 if (varname != NULL && STRCMP(varname, name) == 0)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002045 return STACK_TV_VAR(idx);
2046 }
2047
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02002048 // Go through argument names.
2049 ufunc = dfunc->df_ufunc;
2050 varargs_off = ufunc->uf_va_name == NULL ? 0 : 1;
2051 for (idx = 0; idx < ufunc->uf_args.ga_len; ++idx)
2052 if (STRCMP(((char_u **)(ufunc->uf_args.ga_data))[idx], name) == 0)
2053 return STACK_TV(ectx->ec_frame_idx - ufunc->uf_args.ga_len
2054 - varargs_off + idx);
2055 if (ufunc->uf_va_name != NULL && STRCMP(ufunc->uf_va_name, name) == 0)
2056 return STACK_TV(ectx->ec_frame_idx - 1);
2057
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002058 return NULL;
2059}
2060
Bram Moolenaar26a44842021-09-02 18:49:06 +02002061/*
2062 * Return TRUE if there might be a breakpoint in "ufunc", which is when a
2063 * breakpoint was set in that function or when there is any expression.
2064 */
2065 int
2066may_break_in_function(ufunc_T *ufunc)
2067{
2068 return ufunc->uf_has_breakpoint || debug_has_expr_breakpoint();
2069}
2070
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002071 static void
2072handle_debug(isn_T *iptr, ectx_T *ectx)
2073{
2074 char_u *line;
2075 ufunc_T *ufunc = (((dfunc_T *)def_functions.ga_data)
2076 + ectx->ec_dfunc_idx)->df_ufunc;
2077 isn_T *ni;
2078 int end_lnum = iptr->isn_lnum;
2079 garray_T ga;
2080 int lnum;
2081
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02002082 if (ex_nesting_level > debug_break_level)
2083 {
2084 linenr_T breakpoint;
2085
Bram Moolenaar26a44842021-09-02 18:49:06 +02002086 if (!may_break_in_function(ufunc))
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02002087 return;
2088
2089 // check for the next breakpoint if needed
2090 breakpoint = dbg_find_breakpoint(FALSE, ufunc->uf_name,
Bram Moolenaar8cec9272021-06-23 20:20:53 +02002091 iptr->isn_arg.debug.dbg_break_lnum);
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02002092 if (breakpoint <= 0 || breakpoint > iptr->isn_lnum)
2093 return;
2094 }
2095
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002096 SOURCING_LNUM = iptr->isn_lnum;
2097 debug_context = ectx;
Bram Moolenaar8cec9272021-06-23 20:20:53 +02002098 debug_var_count = iptr->isn_arg.debug.dbg_var_names_len;
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002099
2100 for (ni = iptr + 1; ni->isn_type != ISN_FINISH; ++ni)
2101 if (ni->isn_type == ISN_DEBUG
2102 || ni->isn_type == ISN_RETURN
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002103 || ni->isn_type == ISN_RETURN_OBJECT
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002104 || ni->isn_type == ISN_RETURN_VOID)
2105 {
Bram Moolenaar112bed02021-11-23 22:16:34 +00002106 end_lnum = ni->isn_lnum + (ni->isn_type == ISN_DEBUG ? 0 : 1);
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002107 break;
2108 }
2109
2110 if (end_lnum > iptr->isn_lnum)
2111 {
2112 ga_init2(&ga, sizeof(char_u *), 10);
Bram Moolenaar310091d2021-12-23 21:14:37 +00002113 for (lnum = iptr->isn_lnum; lnum < end_lnum
2114 && lnum <= ufunc->uf_lines.ga_len; ++lnum)
Bram Moolenaar59b50c32021-06-17 22:27:48 +02002115 {
Bram Moolenaar303215d2021-07-07 20:10:43 +02002116 char_u *p = ((char_u **)ufunc->uf_lines.ga_data)[lnum - 1];
Bram Moolenaar59b50c32021-06-17 22:27:48 +02002117
Bram Moolenaar303215d2021-07-07 20:10:43 +02002118 if (p == NULL)
2119 continue; // left over from continuation line
2120 p = skipwhite(p);
Bram Moolenaar59b50c32021-06-17 22:27:48 +02002121 if (*p == '#')
2122 break;
Bram Moolenaar35578162021-08-02 19:10:38 +02002123 if (GA_GROW_OK(&ga, 1))
Bram Moolenaar59b50c32021-06-17 22:27:48 +02002124 ((char_u **)(ga.ga_data))[ga.ga_len++] = p;
2125 if (STRNCMP(p, "def ", 4) == 0)
2126 break;
2127 }
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002128 line = ga_concat_strings(&ga, " ");
2129 vim_free(ga.ga_data);
2130 }
2131 else
2132 line = ((char_u **)ufunc->uf_lines.ga_data)[iptr->isn_lnum - 1];
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002133
Dominique Pelle6e969552021-06-17 13:53:41 +02002134 do_debug(line == NULL ? (char_u *)"[empty]" : line);
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002135 debug_context = NULL;
2136
2137 if (end_lnum > iptr->isn_lnum)
2138 vim_free(line);
2139}
2140
Bram Moolenaar4c137212021-04-19 16:48:48 +02002141/*
Bram Moolenaar65b0d162022-12-13 18:43:22 +00002142 * Store a value in a list, dict, blob or object variable.
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002143 * Returns OK, FAIL or NOTDONE (uncatchable error).
2144 */
2145 static int
2146execute_storeindex(isn_T *iptr, ectx_T *ectx)
2147{
Bram Moolenaarf7d1c6e2023-01-16 20:47:57 +00002148 vartype_T dest_type = iptr->isn_arg.storeindex.si_vartype;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002149 typval_T *tv;
2150 typval_T *tv_idx = STACK_TV_BOT(-2);
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002151 long lidx = 0;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002152 typval_T *tv_dest = STACK_TV_BOT(-1);
2153 int status = OK;
2154
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002155 if (tv_idx->v_type == VAR_NUMBER)
2156 lidx = (long)tv_idx->vval.v_number;
2157
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002158 // Stack contains:
2159 // -3 value to be stored
2160 // -2 index
Yegappan Lakshmanan3775f772023-09-01 22:05:45 +02002161 // -1 dict, list, blob, object or class
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002162 tv = STACK_TV_BOT(-3);
2163 SOURCING_LNUM = iptr->isn_lnum;
Gianmaria Bajod7085a02023-08-31 18:15:26 +02002164
2165 // Make sure an object has been initialized
2166 if (dest_type == VAR_OBJECT && tv_dest->vval.v_object == NULL)
2167 {
2168 emsg(_(e_using_null_object));
2169 status = FAIL;
2170 }
2171 else if (dest_type == VAR_ANY)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002172 {
2173 dest_type = tv_dest->v_type;
2174 if (dest_type == VAR_DICT)
2175 status = do_2string(tv_idx, TRUE, FALSE);
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002176 else if (dest_type == VAR_OBJECT && tv_idx->v_type == VAR_STRING)
2177 {
2178 // Need to get the member index now that the class is known.
2179 object_T *obj = tv_dest->vval.v_object;
2180 class_T *cl = obj->obj_class;
2181 char_u *member = tv_idx->vval.v_string;
2182
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02002183 int m_idx;
2184 ocmember_T *m = object_member_lookup(cl, member, 0, &m_idx);
2185 if (m != NULL)
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002186 {
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02002187 if (*member == '_')
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002188 {
Ernie Raele6c9aa52023-10-06 19:55:52 +02002189 emsg_var_cl_define(e_cannot_access_private_variable_str,
2190 m->ocm_name, 0, cl);
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02002191 status = FAIL;
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002192 }
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002193
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02002194 lidx = m_idx;
2195 }
2196 else
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002197 {
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +02002198 member_not_found_msg(cl, VAR_OBJECT, member, 0);
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002199 status = FAIL;
2200 }
2201 }
2202 else if ((dest_type == VAR_LIST || dest_type == VAR_OBJECT)
2203 && tv_idx->v_type != VAR_NUMBER)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002204 {
2205 emsg(_(e_number_expected));
2206 status = FAIL;
2207 }
2208 }
Bram Moolenaare08be092022-02-17 13:08:26 +00002209
2210 if (status == OK)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002211 {
Bram Moolenaare08be092022-02-17 13:08:26 +00002212 if (dest_type == VAR_LIST)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002213 {
Bram Moolenaare08be092022-02-17 13:08:26 +00002214 list_T *list = tv_dest->vval.v_list;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002215
Bram Moolenaare08be092022-02-17 13:08:26 +00002216 if (list == NULL)
2217 {
2218 emsg(_(e_list_not_set));
2219 return FAIL;
2220 }
2221 if (lidx < 0 && list->lv_len + lidx >= 0)
2222 // negative index is relative to the end
2223 lidx = list->lv_len + lidx;
2224 if (lidx < 0 || lidx > list->lv_len)
2225 {
2226 semsg(_(e_list_index_out_of_range_nr), lidx);
2227 return FAIL;
2228 }
2229 if (lidx < list->lv_len)
2230 {
2231 listitem_T *li = list_find(list, lidx);
2232
2233 if (error_if_locked(li->li_tv.v_lock,
Bram Moolenaar70c43d82022-01-26 21:01:15 +00002234 e_cannot_change_locked_list_item))
Bram Moolenaare08be092022-02-17 13:08:26 +00002235 return FAIL;
2236 // overwrite existing list item
2237 clear_tv(&li->li_tv);
2238 li->li_tv = *tv;
2239 }
2240 else
2241 {
2242 if (error_if_locked(list->lv_lock, e_cannot_change_locked_list))
2243 return FAIL;
2244 // append to list, only fails when out of memory
2245 if (list_append_tv(list, tv) == FAIL)
2246 return NOTDONE;
2247 clear_tv(tv);
2248 }
2249 }
2250 else if (dest_type == VAR_DICT)
2251 {
2252 char_u *key = tv_idx->vval.v_string;
2253 dict_T *dict = tv_dest->vval.v_dict;
2254 dictitem_T *di;
2255
2256 SOURCING_LNUM = iptr->isn_lnum;
2257 if (dict == NULL)
2258 {
2259 emsg(_(e_dictionary_not_set));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002260 return FAIL;
Bram Moolenaare08be092022-02-17 13:08:26 +00002261 }
2262 if (key == NULL)
2263 key = (char_u *)"";
2264 di = dict_find(dict, key, -1);
2265 if (di != NULL)
2266 {
2267 if (error_if_locked(di->di_tv.v_lock,
2268 e_cannot_change_dict_item))
2269 return FAIL;
2270 // overwrite existing value
2271 clear_tv(&di->di_tv);
2272 di->di_tv = *tv;
2273 }
2274 else
2275 {
2276 if (error_if_locked(dict->dv_lock, e_cannot_change_dict))
2277 return FAIL;
2278 // add to dict, only fails when out of memory
2279 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
2280 return NOTDONE;
2281 clear_tv(tv);
2282 }
2283 }
2284 else if (dest_type == VAR_BLOB)
2285 {
Bram Moolenaare08be092022-02-17 13:08:26 +00002286 blob_T *blob = tv_dest->vval.v_blob;
Bram Moolenaar65b0d162022-12-13 18:43:22 +00002287 varnumber_T nr;
2288 int error = FALSE;
2289 int len;
Bram Moolenaare08be092022-02-17 13:08:26 +00002290
2291 if (blob == NULL)
2292 {
2293 emsg(_(e_blob_not_set));
2294 return FAIL;
2295 }
2296 len = blob_len(blob);
2297 if (lidx < 0 && len + lidx >= 0)
2298 // negative index is relative to the end
2299 lidx = len + lidx;
2300
2301 // Can add one byte at the end.
2302 if (lidx < 0 || lidx > len)
2303 {
2304 semsg(_(e_blob_index_out_of_range_nr), lidx);
2305 return FAIL;
2306 }
2307 if (value_check_lock(blob->bv_lock, (char_u *)"blob", FALSE))
2308 return FAIL;
2309 nr = tv_get_number_chk(tv, &error);
2310 if (error)
2311 return FAIL;
2312 blob_set_append(blob, lidx, nr);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002313 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002314 else if (dest_type == VAR_CLASS || dest_type == VAR_OBJECT)
2315 {
Yegappan Lakshmanan3775f772023-09-01 22:05:45 +02002316 typval_T *otv;
Bram Moolenaarf7d1c6e2023-01-16 20:47:57 +00002317
Yegappan Lakshmanan3775f772023-09-01 22:05:45 +02002318 if (dest_type == VAR_OBJECT)
2319 {
2320 object_T *obj = tv_dest->vval.v_object;
2321
2322 otv = (typval_T *)(obj + 1);
2323 class_T *itf = iptr->isn_arg.storeindex.si_class;
2324 if (itf != NULL)
2325 // convert interface member index to class member index
Ernie Rael18143d32023-09-04 22:30:41 +02002326 lidx = object_index_from_itf_index(itf, FALSE, lidx,
Yegappan Lakshmanan5a05d372023-09-29 19:43:11 +02002327 obj->obj_class);
Yegappan Lakshmanan3775f772023-09-01 22:05:45 +02002328 }
2329 else
2330 {
2331 // VAR_CLASS
2332 class_T *class = tv_dest->vval.v_class;
2333 otv = class->class_members_tv;
2334 }
Bram Moolenaarf7d1c6e2023-01-16 20:47:57 +00002335
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002336 clear_tv(&otv[lidx]);
2337 otv[lidx] = *tv;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002338 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002339 else
2340 {
Bram Moolenaare08be092022-02-17 13:08:26 +00002341 status = FAIL;
2342 semsg(_(e_cannot_index_str), vartype_name(dest_type));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002343 }
2344 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002345
2346 clear_tv(tv_idx);
2347 clear_tv(tv_dest);
2348 ectx->ec_stack.ga_len -= 3;
2349 if (status == FAIL)
2350 {
2351 clear_tv(tv);
2352 return FAIL;
2353 }
2354 return OK;
2355}
2356
2357/*
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002358 * Store a value in a list or blob range.
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002359 */
2360 static int
2361execute_storerange(isn_T *iptr, ectx_T *ectx)
2362{
2363 typval_T *tv;
2364 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2365 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2366 typval_T *tv_dest = STACK_TV_BOT(-1);
2367 int status = OK;
2368
2369 // Stack contains:
2370 // -4 value to be stored
2371 // -3 first index or "none"
2372 // -2 second index or "none"
2373 // -1 destination list or blob
2374 tv = STACK_TV_BOT(-4);
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002375 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002376 if (tv_dest->v_type == VAR_LIST)
2377 {
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002378 long n1;
2379 long n2;
2380 listitem_T *li1;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002381
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002382 n1 = (long)tv_get_number_chk(tv_idx1, NULL);
2383 if (tv_idx2->v_type == VAR_SPECIAL
2384 && tv_idx2->vval.v_number == VVAL_NONE)
2385 n2 = list_len(tv_dest->vval.v_list) - 1;
2386 else
2387 n2 = (long)tv_get_number_chk(tv_idx2, NULL);
2388
Bram Moolenaar22ebd172022-04-01 15:26:58 +01002389 li1 = check_range_index_one(tv_dest->vval.v_list, &n1, TRUE, FALSE);
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002390 if (li1 == NULL)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002391 status = FAIL;
2392 else
2393 {
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002394 status = check_range_index_two(tv_dest->vval.v_list,
2395 &n1, li1, &n2, FALSE);
2396 if (status != FAIL)
2397 status = list_assign_range(
2398 tv_dest->vval.v_list,
2399 tv->vval.v_list,
2400 n1,
2401 n2,
2402 tv_idx2->v_type == VAR_SPECIAL,
2403 (char_u *)"=",
2404 (char_u *)"[unknown]");
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002405 }
2406 }
2407 else if (tv_dest->v_type == VAR_BLOB)
2408 {
2409 varnumber_T n1;
2410 varnumber_T n2;
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002411 long bloblen;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002412
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002413 n1 = tv_get_number_chk(tv_idx1, NULL);
2414 if (tv_idx2->v_type == VAR_SPECIAL
2415 && tv_idx2->vval.v_number == VVAL_NONE)
2416 n2 = blob_len(tv_dest->vval.v_blob) - 1;
2417 else
2418 n2 = tv_get_number_chk(tv_idx2, NULL);
2419 bloblen = blob_len(tv_dest->vval.v_blob);
2420
2421 if (check_blob_index(bloblen, n1, FALSE) == FAIL
2422 || check_blob_range(bloblen, n1, n2, FALSE) == FAIL)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002423 status = FAIL;
2424 else
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002425 status = blob_set_range(tv_dest->vval.v_blob, n1, n2, tv);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002426 }
2427 else
2428 {
2429 status = FAIL;
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002430 emsg(_(e_list_or_blob_required));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002431 }
2432
2433 clear_tv(tv_idx1);
2434 clear_tv(tv_idx2);
2435 clear_tv(tv_dest);
2436 ectx->ec_stack.ga_len -= 4;
2437 clear_tv(tv);
2438
2439 return status;
2440}
2441
2442/*
2443 * Unlet item in list or dict variable.
2444 */
2445 static int
2446execute_unletindex(isn_T *iptr, ectx_T *ectx)
2447{
2448 typval_T *tv_idx = STACK_TV_BOT(-2);
2449 typval_T *tv_dest = STACK_TV_BOT(-1);
2450 int status = OK;
2451
2452 // Stack contains:
2453 // -2 index
2454 // -1 dict or list
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002455 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002456 if (tv_dest->v_type == VAR_DICT)
2457 {
2458 // unlet a dict item, index must be a string
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002459 if (tv_idx->v_type != VAR_STRING && tv_idx->v_type != VAR_NUMBER)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002460 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002461 semsg(_(e_expected_str_but_got_str),
2462 vartype_name(VAR_STRING),
2463 vartype_name(tv_idx->v_type));
2464 status = FAIL;
2465 }
2466 else
2467 {
2468 dict_T *d = tv_dest->vval.v_dict;
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002469 char_u *key;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002470 dictitem_T *di = NULL;
2471
2472 if (d != NULL && value_check_lock(
2473 d->dv_lock, NULL, FALSE))
2474 status = FAIL;
2475 else
2476 {
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002477 if (tv_idx->v_type == VAR_STRING)
2478 {
2479 key = tv_idx->vval.v_string;
2480 if (key == NULL)
2481 key = (char_u *)"";
2482 }
2483 else
2484 {
2485 key = tv_get_string(tv_idx);
2486 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002487 if (d != NULL)
2488 di = dict_find(d, key, (int)STRLEN(key));
2489 if (di == NULL)
2490 {
2491 // NULL dict is equivalent to empty dict
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00002492 semsg(_(e_key_not_present_in_dictionary_str), key);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002493 status = FAIL;
2494 }
2495 else if (var_check_fixed(di->di_flags,
2496 NULL, FALSE)
2497 || var_check_ro(di->di_flags,
2498 NULL, FALSE))
2499 status = FAIL;
2500 else
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002501 dictitem_remove(d, di, "unlet");
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002502 }
2503 }
2504 }
2505 else if (tv_dest->v_type == VAR_LIST)
2506 {
2507 // unlet a List item, index must be a number
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002508 if (check_for_number(tv_idx) == FAIL)
2509 {
2510 status = FAIL;
2511 }
2512 else
2513 {
2514 list_T *l = tv_dest->vval.v_list;
2515 long n = (long)tv_idx->vval.v_number;
2516
2517 if (l != NULL && value_check_lock(
2518 l->lv_lock, NULL, FALSE))
2519 status = FAIL;
2520 else
2521 {
2522 listitem_T *li = list_find(l, n);
2523
2524 if (li == NULL)
2525 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002526 semsg(_(e_list_index_out_of_range_nr), n);
2527 status = FAIL;
2528 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002529 else
2530 listitem_remove(l, li);
2531 }
2532 }
2533 }
2534 else
2535 {
2536 status = FAIL;
2537 semsg(_(e_cannot_index_str),
2538 vartype_name(tv_dest->v_type));
2539 }
2540
2541 clear_tv(tv_idx);
2542 clear_tv(tv_dest);
2543 ectx->ec_stack.ga_len -= 2;
2544
2545 return status;
2546}
2547
2548/*
2549 * Unlet a range of items in a list variable.
2550 */
2551 static int
2552execute_unletrange(isn_T *iptr, ectx_T *ectx)
2553{
2554 // Stack contains:
2555 // -3 index1
2556 // -2 index2
2557 // -1 dict or list
2558 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2559 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2560 typval_T *tv_dest = STACK_TV_BOT(-1);
2561 int status = OK;
2562
2563 if (tv_dest->v_type == VAR_LIST)
2564 {
2565 // indexes must be a number
2566 SOURCING_LNUM = iptr->isn_lnum;
2567 if (check_for_number(tv_idx1) == FAIL
2568 || (tv_idx2->v_type != VAR_SPECIAL
2569 && check_for_number(tv_idx2) == FAIL))
2570 {
2571 status = FAIL;
2572 }
2573 else
2574 {
2575 list_T *l = tv_dest->vval.v_list;
2576 long n1 = (long)tv_idx1->vval.v_number;
2577 long n2 = tv_idx2->v_type == VAR_SPECIAL
2578 ? 0 : (long)tv_idx2->vval.v_number;
2579 listitem_T *li;
2580
2581 li = list_find_index(l, &n1);
2582 if (li == NULL)
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002583 {
2584 semsg(_(e_list_index_out_of_range_nr),
2585 (long)tv_idx1->vval.v_number);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002586 status = FAIL;
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002587 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002588 else
2589 {
2590 if (n1 < 0)
2591 n1 = list_idx_of_item(l, li);
2592 if (n2 < 0)
2593 {
2594 listitem_T *li2 = list_find(l, n2);
2595
2596 if (li2 == NULL)
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002597 {
2598 semsg(_(e_list_index_out_of_range_nr), n2);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002599 status = FAIL;
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002600 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002601 else
2602 n2 = list_idx_of_item(l, li2);
2603 }
2604 if (status != FAIL
2605 && tv_idx2->v_type != VAR_SPECIAL
2606 && n2 < n1)
2607 {
2608 semsg(_(e_list_index_out_of_range_nr), n2);
2609 status = FAIL;
2610 }
Bram Moolenaar6b8c7ba2022-03-20 17:46:06 +00002611 if (status != FAIL)
2612 list_unlet_range(l, li, n1,
2613 tv_idx2->v_type != VAR_SPECIAL, n2);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002614 }
2615 }
2616 }
2617 else
2618 {
2619 status = FAIL;
2620 SOURCING_LNUM = iptr->isn_lnum;
2621 semsg(_(e_cannot_index_str),
2622 vartype_name(tv_dest->v_type));
2623 }
2624
2625 clear_tv(tv_idx1);
2626 clear_tv(tv_idx2);
2627 clear_tv(tv_dest);
2628 ectx->ec_stack.ga_len -= 3;
2629
2630 return status;
2631}
2632
2633/*
2634 * Top of a for loop.
2635 */
2636 static int
2637execute_for(isn_T *iptr, ectx_T *ectx)
2638{
2639 typval_T *tv;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002640 int jump = FALSE;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002641 typval_T *ltv = STACK_TV_BOT(-1);
2642 typval_T *idxtv =
Bram Moolenaarcc341812022-09-19 15:54:34 +01002643 STACK_TV_VAR(iptr->isn_arg.forloop.for_loop_idx);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002644
2645 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
2646 return FAIL;
2647 if (ltv->v_type == VAR_LIST)
2648 {
2649 list_T *list = ltv->vval.v_list;
2650
2651 // push the next item from the list
2652 ++idxtv->vval.v_number;
2653 if (list == NULL
2654 || idxtv->vval.v_number >= list->lv_len)
2655 {
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002656 jump = TRUE;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002657 }
2658 else if (list->lv_first == &range_list_item)
2659 {
2660 // non-materialized range() list
2661 tv = STACK_TV_BOT(0);
2662 tv->v_type = VAR_NUMBER;
2663 tv->v_lock = 0;
2664 tv->vval.v_number = list_find_nr(
2665 list, idxtv->vval.v_number, NULL);
2666 ++ectx->ec_stack.ga_len;
2667 }
2668 else
2669 {
2670 listitem_T *li = list_find(list,
2671 idxtv->vval.v_number);
2672
2673 copy_tv(&li->li_tv, STACK_TV_BOT(0));
2674 ++ectx->ec_stack.ga_len;
2675 }
2676 }
2677 else if (ltv->v_type == VAR_STRING)
2678 {
2679 char_u *str = ltv->vval.v_string;
2680
2681 // The index is for the last byte of the previous
2682 // character.
2683 ++idxtv->vval.v_number;
2684 if (str == NULL || str[idxtv->vval.v_number] == NUL)
2685 {
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002686 jump = TRUE;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002687 }
2688 else
2689 {
2690 int clen = mb_ptr2len(str + idxtv->vval.v_number);
2691
2692 // Push the next character from the string.
2693 tv = STACK_TV_BOT(0);
2694 tv->v_type = VAR_STRING;
2695 tv->vval.v_string = vim_strnsave(
2696 str + idxtv->vval.v_number, clen);
2697 ++ectx->ec_stack.ga_len;
2698 idxtv->vval.v_number += clen - 1;
2699 }
2700 }
2701 else if (ltv->v_type == VAR_BLOB)
2702 {
2703 blob_T *blob = ltv->vval.v_blob;
2704
2705 // When we get here the first time make a copy of the
2706 // blob, so that the iteration still works when it is
2707 // changed.
2708 if (idxtv->vval.v_number == -1 && blob != NULL)
2709 {
2710 blob_copy(blob, ltv);
2711 blob_unref(blob);
2712 blob = ltv->vval.v_blob;
2713 }
2714
2715 // The index is for the previous byte.
2716 ++idxtv->vval.v_number;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002717 if (blob == NULL || idxtv->vval.v_number >= blob_len(blob))
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002718 {
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002719 jump = TRUE;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002720 }
2721 else
2722 {
2723 // Push the next byte from the blob.
2724 tv = STACK_TV_BOT(0);
2725 tv->v_type = VAR_NUMBER;
2726 tv->vval.v_number = blob_get(blob,
2727 idxtv->vval.v_number);
2728 ++ectx->ec_stack.ga_len;
2729 }
2730 }
2731 else
2732 {
2733 semsg(_(e_for_loop_on_str_not_supported),
2734 vartype_name(ltv->v_type));
2735 return FAIL;
2736 }
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002737
2738 if (jump)
2739 {
2740 // past the end of the list/string/blob, jump to "endfor"
2741 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
2742 may_restore_cmdmod(&ectx->ec_funclocal);
2743 }
2744 else
2745 {
2746 // Store the current number of funcrefs, this may be used in
2747 // ISN_LOOPEND. The variable index is always one more than the loop
2748 // variable index.
Bram Moolenaarcc341812022-09-19 15:54:34 +01002749 tv = STACK_TV_VAR(iptr->isn_arg.forloop.for_loop_idx + 1);
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002750 tv->vval.v_number = ectx->ec_funcrefs.ga_len;
2751 }
2752
2753 return OK;
2754}
2755
2756/*
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002757 * Code for handling variables declared inside a loop and used in a closure.
2758 * This is very similar to what is done with funcstack_T. The difference is
2759 * that the funcstack_T has the scope of a function, while a loopvars_T has the
2760 * scope of the block inside a loop and each loop may have its own.
2761 */
2762
2763// Double linked list of loopvars_T in use.
2764static loopvars_T *first_loopvars = NULL;
2765
2766 static void
2767add_loopvars_to_list(loopvars_T *loopvars)
2768{
2769 // Link in list of loopvarss.
2770 if (first_loopvars != NULL)
2771 first_loopvars->lvs_prev = loopvars;
2772 loopvars->lvs_next = first_loopvars;
2773 loopvars->lvs_prev = NULL;
2774 first_loopvars = loopvars;
2775}
2776
2777 static void
2778remove_loopvars_from_list(loopvars_T *loopvars)
2779{
2780 if (loopvars->lvs_prev == NULL)
2781 first_loopvars = loopvars->lvs_next;
2782 else
2783 loopvars->lvs_prev->lvs_next = loopvars->lvs_next;
2784 if (loopvars->lvs_next != NULL)
2785 loopvars->lvs_next->lvs_prev = loopvars->lvs_prev;
2786}
2787
2788/*
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002789 * End of a for or while loop: Handle any variables used by a closure.
2790 */
2791 static int
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002792execute_endloop(isn_T *iptr, ectx_T *ectx)
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002793{
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002794 endloop_T *endloop = &iptr->isn_arg.endloop;
2795 typval_T *tv_refcount = STACK_TV_VAR(endloop->end_funcref_idx);
2796 int prev_closure_count = tv_refcount->vval.v_number;
Bram Moolenaarcc341812022-09-19 15:54:34 +01002797 int depth = endloop->end_depth;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002798 garray_T *gap = &ectx->ec_funcrefs;
2799 int closure_in_use = FALSE;
2800 loopvars_T *loopvars;
2801 typval_T *stack;
2802 int idx;
2803
Bram Moolenaarcc341812022-09-19 15:54:34 +01002804 // Check if any created closure is still being referenced and loopvars have
2805 // not been saved yet for the current depth.
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002806 for (idx = prev_closure_count; idx < gap->ga_len; ++idx)
2807 {
2808 partial_T *pt = ((partial_T **)gap->ga_data)[idx];
2809
Bram Moolenaarcc341812022-09-19 15:54:34 +01002810 if (pt->pt_refcount > 1 && pt->pt_loopvars[depth] == NULL)
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002811 {
2812 int refcount = pt->pt_refcount;
2813 int i;
2814
2815 // A Reference in a variable inside the loop doesn't count, it gets
2816 // unreferenced at the end of the loop.
2817 for (i = 0; i < endloop->end_var_count; ++i)
2818 {
2819 typval_T *stv = STACK_TV_VAR(endloop->end_var_idx + i);
2820
2821 if (stv->v_type == VAR_PARTIAL && pt == stv->vval.v_partial)
2822 --refcount;
2823 }
2824 if (refcount > 1)
2825 {
2826 closure_in_use = TRUE;
2827 break;
2828 }
2829 }
2830 }
2831
2832 // If no function reference were created since the start of the loop block
2833 // or it is no longer referenced there is nothing to do.
2834 if (!closure_in_use)
2835 return OK;
2836
2837 // A closure is using variables declared inside the loop.
2838 // Move them to the called function.
2839 loopvars = ALLOC_CLEAR_ONE(loopvars_T);
2840 if (loopvars == NULL)
2841 return FAIL;
2842
2843 loopvars->lvs_ga.ga_len = endloop->end_var_count;
2844 stack = ALLOC_CLEAR_MULT(typval_T, loopvars->lvs_ga.ga_len);
2845 loopvars->lvs_ga.ga_data = stack;
2846 if (stack == NULL)
2847 {
2848 vim_free(loopvars);
2849 return FAIL;
2850 }
2851 add_loopvars_to_list(loopvars);
2852
2853 // Move the variable values.
2854 for (idx = 0; idx < endloop->end_var_count; ++idx)
2855 {
2856 typval_T *tv = STACK_TV_VAR(endloop->end_var_idx + idx);
2857
2858 *(stack + idx) = *tv;
2859 tv->v_type = VAR_UNKNOWN;
2860 }
2861
2862 for (idx = prev_closure_count; idx < gap->ga_len; ++idx)
2863 {
2864 partial_T *pt = ((partial_T **)gap->ga_data)[idx];
2865
Bram Moolenaarcc341812022-09-19 15:54:34 +01002866 if (pt->pt_refcount > 1 && pt->pt_loopvars[depth] == NULL)
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002867 {
2868 ++loopvars->lvs_refcount;
Bram Moolenaarcc341812022-09-19 15:54:34 +01002869 pt->pt_loopvars[depth] = loopvars;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002870
Bram Moolenaarcc341812022-09-19 15:54:34 +01002871 pt->pt_outer.out_loop[depth].stack = &loopvars->lvs_ga;
2872 pt->pt_outer.out_loop[depth].var_idx -=
2873 ectx->ec_frame_idx + STACK_FRAME_SIZE + endloop->end_var_idx;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002874 }
2875 }
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002876
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002877 return OK;
2878}
2879
2880/*
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002881 * Called when a partial is freed or its reference count goes down to one. The
2882 * loopvars may be the only reference to the partials in the local variables.
2883 * Go over all of them, the funcref and can be freed if all partials
2884 * referencing the loopvars have a reference count of one.
Bram Moolenaarcc341812022-09-19 15:54:34 +01002885 * Return TRUE if it was freed.
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002886 */
Bram Moolenaarcc341812022-09-19 15:54:34 +01002887 int
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002888loopvars_check_refcount(loopvars_T *loopvars)
2889{
2890 int i;
2891 garray_T *gap = &loopvars->lvs_ga;
2892 int done = 0;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002893 typval_T *stack = gap->ga_data;
2894
Bram Moolenaarcc341812022-09-19 15:54:34 +01002895 if (loopvars->lvs_refcount > loopvars->lvs_min_refcount)
2896 return FALSE;
2897 for (i = 0; i < gap->ga_len; ++i)
2898 {
2899 typval_T *tv = ((typval_T *)gap->ga_data) + i;
2900
2901 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL
2902 && tv->vval.v_partial->pt_refcount == 1)
2903 {
2904 int depth;
2905
2906 for (depth = 0; depth < MAX_LOOP_DEPTH; ++depth)
2907 if (tv->vval.v_partial->pt_loopvars[depth] == loopvars)
2908 ++done;
2909 }
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002910 }
Bram Moolenaarcc341812022-09-19 15:54:34 +01002911 if (done != loopvars->lvs_min_refcount)
2912 return FALSE;
2913
2914 // All partials referencing the loopvars have a reference count of
2915 // one, thus the loopvars is no longer of use.
2916 stack = gap->ga_data;
2917 for (i = 0; i < gap->ga_len; ++i)
2918 clear_tv(stack + i);
2919 vim_free(stack);
2920 remove_loopvars_from_list(loopvars);
2921 vim_free(loopvars);
2922 return TRUE;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002923}
2924
2925/*
2926 * For garbage collecting: set references in all variables referenced by
2927 * all loopvars.
2928 */
2929 int
2930set_ref_in_loopvars(int copyID)
2931{
2932 loopvars_T *loopvars;
2933
2934 for (loopvars = first_loopvars; loopvars != NULL;
2935 loopvars = loopvars->lvs_next)
2936 {
2937 typval_T *stack = loopvars->lvs_ga.ga_data;
2938 int i;
2939
2940 for (i = 0; i < loopvars->lvs_ga.ga_len; ++i)
2941 if (set_ref_in_item(stack + i, copyID, NULL, NULL))
2942 return TRUE; // abort
2943 }
2944 return FALSE;
2945}
2946
2947/*
Bram Moolenaar06b77222022-01-25 15:51:56 +00002948 * Load instruction for w:/b:/g:/t: variable.
2949 * "isn_type" is used instead of "iptr->isn_type".
2950 */
2951 static int
2952load_namespace_var(ectx_T *ectx, isntype_T isn_type, isn_T *iptr)
2953{
2954 dictitem_T *di = NULL;
2955 hashtab_T *ht = NULL;
2956 char namespace;
2957
2958 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
2959 return NOTDONE;
2960 switch (isn_type)
2961 {
2962 case ISN_LOADG:
2963 ht = get_globvar_ht();
2964 namespace = 'g';
2965 break;
2966 case ISN_LOADB:
2967 ht = &curbuf->b_vars->dv_hashtab;
2968 namespace = 'b';
2969 break;
2970 case ISN_LOADW:
2971 ht = &curwin->w_vars->dv_hashtab;
2972 namespace = 'w';
2973 break;
2974 case ISN_LOADT:
2975 ht = &curtab->tp_vars->dv_hashtab;
2976 namespace = 't';
2977 break;
2978 default: // Cannot reach here
2979 return NOTDONE;
2980 }
2981 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
2982
Bram Moolenaar06b77222022-01-25 15:51:56 +00002983 if (di == NULL)
2984 {
Bram Moolenaarfe732552022-02-22 19:39:13 +00002985 if (isn_type == ISN_LOADG)
2986 {
2987 ufunc_T *ufunc = find_func(iptr->isn_arg.string, TRUE);
2988
2989 // g:Something could be a function
2990 if (ufunc != NULL)
2991 {
2992 typval_T *tv = STACK_TV_BOT(0);
2993
2994 ++ectx->ec_stack.ga_len;
2995 tv->v_type = VAR_FUNC;
2996 tv->vval.v_string = alloc(STRLEN(iptr->isn_arg.string) + 3);
2997 if (tv->vval.v_string == NULL)
2998 return FAIL;
2999 STRCPY(tv->vval.v_string, "g:");
3000 STRCPY(tv->vval.v_string + 2, iptr->isn_arg.string);
3001 return OK;
3002 }
3003 }
Bram Moolenaar06b77222022-01-25 15:51:56 +00003004 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarfe732552022-02-22 19:39:13 +00003005 if (vim_strchr(iptr->isn_arg.string, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar06b77222022-01-25 15:51:56 +00003006 // no check if the item exists in the script but
3007 // isn't exported, it is too complicated
Bram Moolenaarfe732552022-02-22 19:39:13 +00003008 semsg(_(e_item_not_found_in_script_str), iptr->isn_arg.string);
Bram Moolenaar06b77222022-01-25 15:51:56 +00003009 else
3010 semsg(_(e_undefined_variable_char_str),
Bram Moolenaarfe732552022-02-22 19:39:13 +00003011 namespace, iptr->isn_arg.string);
Bram Moolenaar06b77222022-01-25 15:51:56 +00003012 return FAIL;
3013 }
3014 else
3015 {
3016 copy_tv(&di->di_tv, STACK_TV_BOT(0));
3017 ++ectx->ec_stack.ga_len;
3018 }
3019 return OK;
3020}
3021
Bram Moolenaard0200c82023-01-28 15:19:40 +00003022
3023 static void
3024object_required_error(typval_T *tv)
3025{
3026 garray_T type_list;
3027 ga_init2(&type_list, sizeof(type_T *), 10);
3028 type_T *type = typval2type(tv, get_copyID(), &type_list, TVTT_DO_MEMBER);
3029 char *tofree = NULL;
3030 char *typename = type_name(type, &tofree);
3031 semsg(_(e_object_required_found_str), typename);
3032 vim_free(tofree);
3033 clear_type_list(&type_list);
3034}
3035
Bram Moolenaar06b77222022-01-25 15:51:56 +00003036/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02003037 * Execute instructions in execution context "ectx".
3038 * Return OK or FAIL;
3039 */
3040 static int
3041exec_instructions(ectx_T *ectx)
3042{
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003043 int ret = FAIL;
3044 int save_trylevel_at_start = ectx->ec_trylevel_at_start;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02003045 int dict_stack_len_at_start = dict_stack.ga_len;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003046
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02003047 // Start execution at the first instruction.
Bram Moolenaar4c137212021-04-19 16:48:48 +02003048 ectx->ec_iidx = 0;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01003049
Bram Moolenaarff652882021-05-16 15:24:49 +02003050 // Only catch exceptions in this instruction list.
3051 ectx->ec_trylevel_at_start = trylevel;
3052
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003053 for (;;)
3054 {
Bram Moolenaara7490192021-07-22 12:26:14 +02003055 static int breakcheck_count = 0; // using "static" makes it faster
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003056 isn_T *iptr;
Bram Moolenaara7490192021-07-22 12:26:14 +02003057 typval_T *tv;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003058
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003059 if (unlikely(++breakcheck_count >= 100))
Bram Moolenaar270d0382020-05-15 21:42:53 +02003060 {
3061 line_breakcheck();
3062 breakcheck_count = 0;
3063 }
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003064 if (unlikely(got_int))
Bram Moolenaar20431c92020-03-20 18:39:46 +01003065 {
3066 // Turn CTRL-C into an exception.
3067 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +01003068 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003069 goto theend;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003070 did_throw = TRUE;
3071 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003072
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003073 if (unlikely(did_emsg && msg_list != NULL && *msg_list != NULL))
Bram Moolenaara26b9702020-04-18 19:53:28 +02003074 {
3075 // Turn an error message into an exception.
3076 did_emsg = FALSE;
3077 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003078 goto theend;
Bram Moolenaara26b9702020-04-18 19:53:28 +02003079 did_throw = TRUE;
3080 *msg_list = NULL;
Bram Moolenaar3ef2e412023-04-30 18:50:48 +01003081
3082 // This exception was not caught (yet).
3083 garray_T *trystack = &ectx->ec_trystack;
3084 if (trystack->ga_len > 0)
3085 {
3086 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
3087 + trystack->ga_len - 1;
3088 if (trycmd->tcd_frame_idx == ectx->ec_frame_idx)
3089 trycmd->tcd_caught = FALSE;
3090 }
Bram Moolenaara26b9702020-04-18 19:53:28 +02003091 }
3092
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003093 if (unlikely(did_throw))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003094 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003095 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003096 trycmd_T *trycmd = NULL;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003097 int index = trystack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003098
3099 // An exception jumps to the first catch, finally, or returns from
3100 // the current function.
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003101 while (index > 0)
3102 {
3103 trycmd = ((trycmd_T *)trystack->ga_data) + index - 1;
Bram Moolenaar3ef2e412023-04-30 18:50:48 +01003104 // 1. after :try and before :catch - jump to first :catch
3105 // 2. in :catch block - jump to :finally
3106 // 3. in :catch block and no finally - jump to :endtry
3107 if (!trycmd->tcd_in_catch || trycmd->tcd_finally_idx != 0
3108 || trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003109 break;
3110 // In the catch and finally block of this try we have to go up
3111 // one level.
3112 --index;
3113 trycmd = NULL;
3114 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003115 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003116 {
Bram Moolenaar834193a2021-06-30 20:39:15 +02003117 if (trycmd->tcd_in_catch)
3118 {
Bram Moolenaar3ef2e412023-04-30 18:50:48 +01003119 if (trycmd->tcd_finally_idx > 0)
3120 {
3121 // exception inside ":catch", jump to ":finally" once
3122 ectx->ec_iidx = trycmd->tcd_finally_idx;
3123 trycmd->tcd_finally_idx = 0;
3124 }
3125 else
3126 {
3127 // exception inside ":catch" or ":finally", jump to
3128 // ":endtry"
3129 ectx->ec_iidx = trycmd->tcd_endtry_idx;
3130 }
Bram Moolenaar834193a2021-06-30 20:39:15 +02003131 }
3132 else
Bram Moolenaar3ef2e412023-04-30 18:50:48 +01003133 {
Bram Moolenaar834193a2021-06-30 20:39:15 +02003134 // jump to first ":catch"
3135 ectx->ec_iidx = trycmd->tcd_catch_idx;
Bram Moolenaar3ef2e412023-04-30 18:50:48 +01003136 trycmd->tcd_in_catch = TRUE;
3137 }
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003138 did_throw = FALSE; // don't come back here until :endtry
3139 trycmd->tcd_did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003140 }
3141 else
3142 {
Bram Moolenaar3ef2e412023-04-30 18:50:48 +01003143 // Not inside try or need to return from current function.
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02003144 // Push a dummy return value.
Bram Moolenaar35578162021-08-02 19:10:38 +02003145 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003146 goto theend;
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02003147 tv = STACK_TV_BOT(0);
3148 tv->v_type = VAR_NUMBER;
3149 tv->vval.v_number = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003150 ++ectx->ec_stack.ga_len;
3151 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003152 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02003153 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +01003154 need_rethrow = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003155 if (handle_closure_in_use(ectx, FALSE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003156 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003157 goto done;
3158 }
3159
Bram Moolenaar4c137212021-04-19 16:48:48 +02003160 if (func_return(ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003161 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003162 }
3163 continue;
3164 }
3165
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003166 /*
3167 * Big switch on the instruction. Most compilers will be turning this
3168 * into an efficient lookup table, since the "case" values are an enum
3169 * with sequential numbers. It may look ugly, but it should be the
3170 * most efficient way.
3171 */
Bram Moolenaar4c137212021-04-19 16:48:48 +02003172 iptr = &ectx->ec_instr[ectx->ec_iidx++];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003173 switch (iptr->isn_type)
3174 {
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00003175 // Constructor, first instruction in a new() method.
Bram Moolenaar00b28d62022-12-08 15:32:33 +00003176 case ISN_CONSTRUCT:
3177 // "this" is always the local variable at index zero
3178 tv = STACK_TV_VAR(0);
3179 tv->v_type = VAR_OBJECT;
3180 tv->vval.v_object = alloc_clear(
3181 iptr->isn_arg.construct.construct_size);
3182 tv->vval.v_object->obj_class =
3183 iptr->isn_arg.construct.construct_class;
Bram Moolenaard28d7b92022-12-08 20:42:00 +00003184 ++tv->vval.v_object->obj_class->class_refcount;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00003185 tv->vval.v_object->obj_refcount = 1;
Bram Moolenaard28d7b92022-12-08 20:42:00 +00003186 object_created(tv->vval.v_object);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00003187 break;
3188
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003189 // execute Ex command line
3190 case ISN_EXEC:
Ernie Raelee865f32023-09-29 19:53:55 +02003191 if (exec_command(iptr, iptr->isn_arg.string) == FAIL)
Bram Moolenaaraacc9662021-08-13 19:40:51 +02003192 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003193 break;
3194
Bram Moolenaar20677332021-06-06 17:02:53 +02003195 // execute Ex command line split at NL characters.
3196 case ISN_EXEC_SPLIT:
3197 {
3198 source_cookie_T cookie;
Bram Moolenaar518df272021-06-06 17:34:13 +02003199 char_u *line;
Bram Moolenaar20677332021-06-06 17:02:53 +02003200
3201 SOURCING_LNUM = iptr->isn_lnum;
3202 CLEAR_FIELD(cookie);
3203 cookie.sourcing_lnum = iptr->isn_lnum - 1;
3204 cookie.nextline = iptr->isn_arg.string;
Bram Moolenaar518df272021-06-06 17:34:13 +02003205 line = get_split_sourceline(0, &cookie, 0, 0);
3206 if (do_cmdline(line,
Bram Moolenaar20677332021-06-06 17:02:53 +02003207 get_split_sourceline, &cookie,
3208 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED)
3209 == FAIL
3210 || did_emsg)
Bram Moolenaar518df272021-06-06 17:34:13 +02003211 {
3212 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02003213 goto on_error;
Bram Moolenaar518df272021-06-06 17:34:13 +02003214 }
3215 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02003216 }
3217 break;
3218
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00003219 // execute Ex command line that is only a range
3220 case ISN_EXECRANGE:
3221 {
3222 exarg_T ea;
3223 char *error = NULL;
3224
3225 CLEAR_FIELD(ea);
3226 ea.cmdidx = CMD_SIZE;
3227 ea.addr_type = ADDR_LINES;
3228 ea.cmd = iptr->isn_arg.string;
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00003229 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00003230 parse_cmd_address(&ea, &error, FALSE);
Bram Moolenaar01a4dcb2021-12-04 13:15:10 +00003231 if (ea.cmd == NULL)
3232 goto on_error;
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00003233 // error is always NULL when using ADDR_LINES
3234 error = ex_range_without_command(&ea);
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00003235 if (error != NULL)
3236 {
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00003237 emsg(error);
3238 goto on_error;
3239 }
3240 }
3241 break;
3242
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02003243 // Evaluate an expression with legacy syntax, push it onto the
3244 // stack.
3245 case ISN_LEGACY_EVAL:
3246 {
3247 char_u *arg = iptr->isn_arg.string;
3248 int res;
3249 int save_flags = cmdmod.cmod_flags;
3250
Bram Moolenaar35578162021-08-02 19:10:38 +02003251 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003252 goto theend;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02003253 tv = STACK_TV_BOT(0);
3254 init_tv(tv);
3255 cmdmod.cmod_flags |= CMOD_LEGACY;
3256 res = eval0(arg, tv, NULL, &EVALARG_EVALUATE);
3257 cmdmod.cmod_flags = save_flags;
3258 if (res == FAIL)
3259 goto on_error;
3260 ++ectx->ec_stack.ga_len;
3261 }
3262 break;
3263
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003264 // push typeval VAR_INSTR with instructions to be executed
3265 case ISN_INSTR:
3266 {
Bram Moolenaar35578162021-08-02 19:10:38 +02003267 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003268 goto theend;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003269 tv = STACK_TV_BOT(0);
3270 tv->vval.v_instr = ALLOC_ONE(instr_T);
3271 if (tv->vval.v_instr == NULL)
3272 goto on_error;
3273 ++ectx->ec_stack.ga_len;
3274
3275 tv->v_type = VAR_INSTR;
3276 tv->vval.v_instr->instr_ectx = ectx;
3277 tv->vval.v_instr->instr_instr = iptr->isn_arg.instr;
3278 }
3279 break;
3280
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003281 case ISN_SOURCE:
3282 {
Bram Moolenaar89445512022-04-14 12:58:23 +01003283 int notused;
LemonBoy77142312022-04-15 20:50:46 +01003284
Bram Moolenaar89445512022-04-14 12:58:23 +01003285 SOURCING_LNUM = iptr->isn_lnum;
3286 if (may_load_script((int)iptr->isn_arg.number, &notused)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003287 == FAIL)
Bram Moolenaar89445512022-04-14 12:58:23 +01003288 goto on_error;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003289 }
3290 break;
3291
Bram Moolenaar4c137212021-04-19 16:48:48 +02003292 // execute :substitute with an expression
3293 case ISN_SUBSTITUTE:
3294 {
3295 subs_T *subs = &iptr->isn_arg.subs;
3296 source_cookie_T cookie;
3297 struct subs_expr_S *save_instr = substitute_instr;
3298 struct subs_expr_S subs_instr;
3299 int res;
3300
3301 subs_instr.subs_ectx = ectx;
3302 subs_instr.subs_instr = subs->subs_instr;
3303 subs_instr.subs_status = OK;
3304 substitute_instr = &subs_instr;
3305
3306 SOURCING_LNUM = iptr->isn_lnum;
3307 // This is very much like ISN_EXEC
3308 CLEAR_FIELD(cookie);
3309 cookie.sourcing_lnum = iptr->isn_lnum - 1;
3310 res = do_cmdline(subs->subs_cmd,
3311 getsourceline, &cookie,
3312 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
3313 substitute_instr = save_instr;
3314
3315 if (res == FAIL || did_emsg
3316 || subs_instr.subs_status == FAIL)
3317 goto on_error;
3318 }
3319 break;
3320
3321 case ISN_FINISH:
3322 goto done;
3323
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02003324 case ISN_REDIRSTART:
3325 // create a dummy entry for var_redir_str()
3326 if (alloc_redir_lval() == FAIL)
3327 goto on_error;
3328
3329 // The output is stored in growarray "redir_ga" until
3330 // redirection ends.
3331 init_redir_ga();
3332 redir_vname = 1;
3333 break;
3334
3335 case ISN_REDIREND:
3336 {
3337 char_u *res = get_clear_redir_ga();
3338
3339 // End redirection, put redirected text on the stack.
3340 clear_redir_lval();
3341 redir_vname = 0;
3342
Bram Moolenaar35578162021-08-02 19:10:38 +02003343 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02003344 {
3345 vim_free(res);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003346 goto theend;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02003347 }
3348 tv = STACK_TV_BOT(0);
3349 tv->v_type = VAR_STRING;
3350 tv->vval.v_string = res;
3351 ++ectx->ec_stack.ga_len;
3352 }
3353 break;
3354
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003355 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02003356#ifdef FEAT_QUICKFIX
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003357 force_abort = TRUE;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003358 if (trigger_cexpr_autocmd(iptr->isn_arg.number) == FAIL)
3359 goto on_error;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003360 force_abort = FALSE;
Bram Moolenaarb7c97812021-05-05 22:51:39 +02003361#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003362 break;
3363
3364 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02003365#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003366 {
3367 exarg_T ea;
3368 int res;
3369
3370 CLEAR_FIELD(ea);
3371 ea.cmdidx = iptr->isn_arg.cexpr.cexpr_ref->cer_cmdidx;
3372 ea.forceit = iptr->isn_arg.cexpr.cexpr_ref->cer_forceit;
3373 ea.cmdlinep = &iptr->isn_arg.cexpr.cexpr_ref->cer_cmdline;
3374 --ectx->ec_stack.ga_len;
3375 tv = STACK_TV_BOT(0);
Bram Moolenaarbd683e32022-07-18 17:49:03 +01003376 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003377 res = cexpr_core(&ea, tv);
3378 clear_tv(tv);
3379 if (res == FAIL)
3380 goto on_error;
3381 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02003382#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003383 break;
3384
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003385 // execute Ex command from pieces on the stack
3386 case ISN_EXECCONCAT:
3387 {
3388 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +02003389 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003390 int pass;
3391 int i;
3392 char_u *cmd = NULL;
3393 char_u *str;
3394
3395 for (pass = 1; pass <= 2; ++pass)
3396 {
3397 for (i = 0; i < count; ++i)
3398 {
3399 tv = STACK_TV_BOT(i - count);
3400 str = tv->vval.v_string;
3401 if (str != NULL && *str != NUL)
3402 {
3403 if (pass == 2)
3404 STRCPY(cmd + len, str);
3405 len += STRLEN(str);
3406 }
3407 if (pass == 2)
3408 clear_tv(tv);
3409 }
3410 if (pass == 1)
3411 {
3412 cmd = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003413 if (unlikely(cmd == NULL))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003414 goto theend;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003415 len = 0;
3416 }
3417 }
3418
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02003419 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003420 do_cmdline_cmd(cmd);
3421 vim_free(cmd);
3422 }
3423 break;
3424
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003425 // execute :echo {string} ...
3426 case ISN_ECHO:
3427 {
3428 int count = iptr->isn_arg.echo.echo_count;
3429 int atstart = TRUE;
3430 int needclr = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003431 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003432
3433 for (idx = 0; idx < count; ++idx)
3434 {
3435 tv = STACK_TV_BOT(idx - count);
3436 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
3437 &atstart, &needclr);
3438 clear_tv(tv);
3439 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01003440 if (needclr)
3441 msg_clr_eos();
Bram Moolenaar4c137212021-04-19 16:48:48 +02003442 ectx->ec_stack.ga_len -= count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003443 }
3444 break;
3445
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003446 // :execute {string} ...
3447 // :echomsg {string} ...
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01003448 // :echowindow {string} ...
Bram Moolenaar7de62622021-08-07 15:05:47 +02003449 // :echoconsole {string} ...
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003450 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01003451 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003452 case ISN_ECHOMSG:
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01003453 case ISN_ECHOWINDOW:
Bram Moolenaar7de62622021-08-07 15:05:47 +02003454 case ISN_ECHOCONSOLE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003455 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01003456 {
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01003457 int count;
Bram Moolenaarad39c092020-02-26 18:23:43 +01003458 garray_T ga;
3459 char_u buf[NUMBUFLEN];
3460 char_u *p;
3461 int len;
3462 int failed = FALSE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003463 int idx;
Bram Moolenaarad39c092020-02-26 18:23:43 +01003464
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01003465 if (iptr->isn_type == ISN_ECHOWINDOW)
3466 count = iptr->isn_arg.echowin.ewin_count;
3467 else
3468 count = iptr->isn_arg.number;
Bram Moolenaarad39c092020-02-26 18:23:43 +01003469 ga_init2(&ga, 1, 80);
3470 for (idx = 0; idx < count; ++idx)
3471 {
3472 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003473 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01003474 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003475 if (tv->v_type == VAR_CHANNEL
3476 || tv->v_type == VAR_JOB)
3477 {
3478 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar68db9962021-05-09 23:19:22 +02003479 semsg(_(e_using_invalid_value_as_string_str),
3480 vartype_name(tv->v_type));
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003481 break;
3482 }
3483 else
3484 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01003485 }
3486 else
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003487 p = tv_stringify(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01003488
3489 len = (int)STRLEN(p);
Bram Moolenaar35578162021-08-02 19:10:38 +02003490 if (GA_GROW_FAILS(&ga, len + 2))
Bram Moolenaarad39c092020-02-26 18:23:43 +01003491 failed = TRUE;
3492 else
3493 {
3494 if (ga.ga_len > 0)
3495 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
3496 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
3497 ga.ga_len += len;
3498 }
3499 clear_tv(tv);
3500 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003501 ectx->ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003502 if (failed)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003503 {
3504 ga_clear(&ga);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003505 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003506 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01003507
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003508 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003509 {
3510 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02003511 {
3512 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003513 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01003514 if (did_emsg)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003515 {
3516 ga_clear(&ga);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01003517 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003518 }
Bram Moolenaar430deb12020-08-23 16:29:11 +02003519 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003520 else
3521 {
3522 msg_sb_eol();
3523 if (iptr->isn_type == ISN_ECHOMSG)
3524 {
3525 msg_attr(ga.ga_data, echo_attr);
3526 out_flush();
3527 }
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01003528#ifdef HAS_MESSAGE_WINDOW
3529 else if (iptr->isn_type == ISN_ECHOWINDOW)
3530 {
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01003531 start_echowindow(
3532 iptr->isn_arg.echowin.ewin_time);
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01003533 msg_attr(ga.ga_data, echo_attr);
3534 end_echowindow();
3535 }
3536#endif
Bram Moolenaar7de62622021-08-07 15:05:47 +02003537 else if (iptr->isn_type == ISN_ECHOCONSOLE)
3538 {
3539 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data),
3540 TRUE);
3541 ui_write((char_u *)"\r\n", 2, TRUE);
3542 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003543 else
3544 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003545 SOURCING_LNUM = iptr->isn_lnum;
3546 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003547 }
3548 }
3549 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01003550 ga_clear(&ga);
3551 }
3552 break;
3553
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003554 // load local variable or argument
3555 case ISN_LOAD:
Bram Moolenaar35578162021-08-02 19:10:38 +02003556 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003557 goto theend;
Bram Moolenaar2ed57ac2023-04-01 22:05:38 +01003558 tv = STACK_TV_VAR(iptr->isn_arg.number);
3559 if (tv->v_type == VAR_UNKNOWN)
3560 {
3561 // missing argument or default value v:none
3562 STACK_TV_BOT(0)->v_type = VAR_SPECIAL;
3563 STACK_TV_BOT(0)->vval.v_number = VVAL_NONE;
3564 }
3565 else
3566 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003567 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003568 break;
3569
3570 // load v: variable
3571 case ISN_LOADV:
Bram Moolenaar35578162021-08-02 19:10:38 +02003572 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003573 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003574 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003575 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003576 break;
3577
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003578 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003579 case ISN_LOADSCRIPT:
3580 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01003581 scriptref_T *sref = iptr->isn_arg.script.scriptref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003582 svar_T *sv;
3583
Bram Moolenaar6db660b2021-08-01 14:08:54 +02003584 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003585 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003586 goto theend;
Bram Moolenaar859cc212022-03-28 15:22:35 +01003587 allocate_if_null(sv);
Bram Moolenaar35578162021-08-02 19:10:38 +02003588 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003589 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003590 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003591 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003592 }
3593 break;
3594
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003595 // load s: variable in old script or autoload import
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003596 case ISN_LOADS:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003597 case ISN_LOADEXPORT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003598 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003599 int sid = iptr->isn_arg.loadstore.ls_sid;
3600 hashtab_T *ht = &SCRIPT_VARS(sid);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003601 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003602 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003603
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003604 if (di == NULL)
3605 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003606 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003607 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003608 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003609 }
3610 else
3611 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003612 if (iptr->isn_type == ISN_LOADEXPORT)
3613 {
3614 int idx = get_script_item_idx(sid, name, 0,
3615 NULL, NULL);
3616 svar_T *sv;
3617
3618 if (idx >= 0)
3619 {
3620 sv = ((svar_T *)SCRIPT_ITEM(sid)
3621 ->sn_var_vals.ga_data) + idx;
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003622 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003623 {
3624 SOURCING_LNUM = iptr->isn_lnum;
3625 semsg(_(e_item_not_exported_in_script_str),
3626 name);
3627 goto on_error;
3628 }
3629 }
3630 }
Bram Moolenaar35578162021-08-02 19:10:38 +02003631 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003632 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003633 copy_tv(&di->di_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003634 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003635 }
3636 }
3637 break;
3638
Bram Moolenaard3aac292020-04-19 14:32:17 +02003639 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003640 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02003641 case ISN_LOADB:
3642 case ISN_LOADW:
3643 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003644 {
Bram Moolenaar06b77222022-01-25 15:51:56 +00003645 int res = load_namespace_var(ectx, iptr->isn_type, iptr);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003646
Bram Moolenaar06b77222022-01-25 15:51:56 +00003647 if (res == NOTDONE)
Bram Moolenaarcbbc48f2022-01-18 12:58:28 +00003648 goto theend;
Bram Moolenaar06b77222022-01-25 15:51:56 +00003649 if (res == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003650 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003651 }
Bram Moolenaar06b77222022-01-25 15:51:56 +00003652
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003653 break;
3654
Bram Moolenaar03290b82020-12-19 16:30:44 +01003655 // load autoload variable
3656 case ISN_LOADAUTO:
3657 {
3658 char_u *name = iptr->isn_arg.string;
3659
Bram Moolenaar35578162021-08-02 19:10:38 +02003660 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003661 goto theend;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003662 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003663 if (eval_variable(name, (int)STRLEN(name), 0,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003664 STACK_TV_BOT(0), NULL, EVAL_VAR_VERBOSE) == FAIL)
Bram Moolenaar03290b82020-12-19 16:30:44 +01003665 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003666 ++ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003667 }
3668 break;
3669
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003670 // load g:/b:/w:/t: namespace
3671 case ISN_LOADGDICT:
3672 case ISN_LOADBDICT:
3673 case ISN_LOADWDICT:
3674 case ISN_LOADTDICT:
3675 {
3676 dict_T *d = NULL;
3677
3678 switch (iptr->isn_type)
3679 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02003680 case ISN_LOADGDICT: d = get_globvar_dict(); break;
3681 case ISN_LOADBDICT: d = curbuf->b_vars; break;
3682 case ISN_LOADWDICT: d = curwin->w_vars; break;
3683 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003684 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003685 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003686 }
Bram Moolenaar35578162021-08-02 19:10:38 +02003687 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003688 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003689 tv = STACK_TV_BOT(0);
3690 tv->v_type = VAR_DICT;
3691 tv->v_lock = 0;
3692 tv->vval.v_dict = d;
Bram Moolenaar1bd3cb22021-02-24 12:27:31 +01003693 ++d->dv_refcount;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003694 ++ectx->ec_stack.ga_len;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003695 }
3696 break;
3697
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003698 // load &option
3699 case ISN_LOADOPT:
3700 {
3701 typval_T optval;
3702 char_u *name = iptr->isn_arg.string;
3703
Bram Moolenaara8c17702020-04-01 21:17:24 +02003704 // This is not expected to fail, name is checked during
3705 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar35578162021-08-02 19:10:38 +02003706 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003707 goto theend;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003708 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003709 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003710 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003711 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003712 }
3713 break;
3714
3715 // load $ENV
3716 case ISN_LOADENV:
3717 {
3718 typval_T optval;
3719 char_u *name = iptr->isn_arg.string;
3720
Bram Moolenaar35578162021-08-02 19:10:38 +02003721 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003722 goto theend;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003723 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003724 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003725 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003726 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003727 }
3728 break;
3729
3730 // load @register
3731 case ISN_LOADREG:
Bram Moolenaar35578162021-08-02 19:10:38 +02003732 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003733 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003734 tv = STACK_TV_BOT(0);
3735 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003736 tv->v_lock = 0;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02003737 // This may result in NULL, which should be equivalent to an
3738 // empty string.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003739 tv->vval.v_string = get_reg_contents(
3740 iptr->isn_arg.number, GREG_EXPR_SRC);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003741 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003742 break;
3743
3744 // store local variable
3745 case ISN_STORE:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003746 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003747 tv = STACK_TV_VAR(iptr->isn_arg.number);
3748 clear_tv(tv);
3749 *tv = *STACK_TV_BOT(0);
3750 break;
3751
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003752 // store s: variable in old script or autoload import
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003753 case ISN_STORES:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003754 case ISN_STOREEXPORT:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003755 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003756 int sid = iptr->isn_arg.loadstore.ls_sid;
3757 hashtab_T *ht = &SCRIPT_VARS(sid);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003758 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003759 dictitem_T *di = find_var_in_ht(ht, 0,
3760 iptr->isn_type == ISN_STORES
3761 ? name + 2 : name, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003762
Bram Moolenaar4c137212021-04-19 16:48:48 +02003763 --ectx->ec_stack.ga_len;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003764 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003765 if (di == NULL)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003766 {
3767 if (iptr->isn_type == ISN_STOREEXPORT)
3768 {
3769 semsg(_(e_undefined_variable_str), name);
Bram Moolenaard1d26842022-03-31 10:13:47 +01003770 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003771 goto on_error;
3772 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003773 store_var(name, STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003774 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003775 else
3776 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003777 if (iptr->isn_type == ISN_STOREEXPORT)
3778 {
3779 int idx = get_script_item_idx(sid, name, 0,
3780 NULL, NULL);
3781
Bram Moolenaar06651632022-04-27 17:54:25 +01003782 // can this ever fail?
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003783 if (idx >= 0)
3784 {
3785 svar_T *sv = ((svar_T *)SCRIPT_ITEM(sid)
3786 ->sn_var_vals.ga_data) + idx;
3787
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003788 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003789 {
3790 semsg(_(e_item_not_exported_in_script_str),
3791 name);
Bram Moolenaard1d26842022-03-31 10:13:47 +01003792 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003793 goto on_error;
3794 }
3795 }
3796 }
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02003797 if (var_check_permission(di, name) == FAIL)
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003798 {
3799 clear_tv(STACK_TV_BOT(0));
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02003800 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003801 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003802 clear_tv(&di->di_tv);
3803 di->di_tv = *STACK_TV_BOT(0);
3804 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003805 }
3806 break;
3807
3808 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003809 case ISN_STORESCRIPT:
3810 {
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003811 scriptref_T *sref = iptr->isn_arg.script.scriptref;
3812 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003813
Bram Moolenaar6db660b2021-08-01 14:08:54 +02003814 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003815 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003816 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003817 --ectx->ec_stack.ga_len;
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003818
3819 // "const" and "final" are checked at compile time, locking
3820 // the value needs to be checked here.
3821 SOURCING_LNUM = iptr->isn_lnum;
3822 if (value_check_lock(sv->sv_tv->v_lock, sv->sv_name, FALSE))
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003823 {
3824 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003825 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003826 }
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003827
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003828 clear_tv(sv->sv_tv);
3829 *sv->sv_tv = *STACK_TV_BOT(0);
3830 }
3831 break;
3832
3833 // store option
3834 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003835 case ISN_STOREFUNCOPT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003836 {
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003837 char_u *opt_name = iptr->isn_arg.storeopt.so_name;
3838 int opt_flags = iptr->isn_arg.storeopt.so_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003839 long n = 0;
3840 char_u *s = NULL;
3841 char *msg;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003842 char_u numbuf[NUMBUFLEN];
3843 char_u *tofree = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003844
Bram Moolenaar4c137212021-04-19 16:48:48 +02003845 --ectx->ec_stack.ga_len;
LemonBoy32f34612023-09-02 21:52:05 +02003846 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003847 tv = STACK_TV_BOT(0);
3848 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01003849 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003850 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01003851 if (s == NULL)
3852 s = (char_u *)"";
3853 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003854 else if (iptr->isn_type == ISN_STOREFUNCOPT)
3855 {
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003856 // If the option can be set to a function reference or
3857 // a lambda and the passed value is a function
3858 // reference, then convert it to the name (string) of
3859 // the function reference.
3860 s = tv2string(tv, &tofree, numbuf, 0);
3861 if (s == NULL || *s == NUL)
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003862 {
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003863 // cannot happen?
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003864 clear_tv(tv);
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003865 vim_free(tofree);
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003866 goto on_error;
3867 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003868 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003869 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003870 // must be VAR_NUMBER, CHECKTYPE makes sure
3871 n = tv->vval.v_number;
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003872 msg = set_option_value(opt_name, n, s, opt_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02003873 clear_tv(tv);
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003874 vim_free(tofree);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003875 if (msg != NULL)
3876 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003877 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003878 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02003879 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003880 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003881 }
3882 break;
3883
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003884 // store $ENV
3885 case ISN_STOREENV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003886 --ectx->ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003887 tv = STACK_TV_BOT(0);
3888 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
3889 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003890 break;
3891
3892 // store @r
3893 case ISN_STOREREG:
3894 {
3895 int reg = iptr->isn_arg.number;
3896
Bram Moolenaar4c137212021-04-19 16:48:48 +02003897 --ectx->ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01003898 tv = STACK_TV_BOT(0);
Bram Moolenaar74f4a962021-06-17 21:03:07 +02003899 write_reg_contents(reg, tv_get_string(tv), -1, FALSE);
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01003900 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003901 }
3902 break;
3903
3904 // store v: variable
3905 case ISN_STOREV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003906 --ectx->ec_stack.ga_len;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003907 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
3908 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003909 // should not happen, type is checked when compiling
3910 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003911 break;
3912
Bram Moolenaard3aac292020-04-19 14:32:17 +02003913 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003914 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02003915 case ISN_STOREB:
3916 case ISN_STOREW:
3917 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003918 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003919 dictitem_T *di;
3920 hashtab_T *ht;
3921 char_u *name = iptr->isn_arg.string + 2;
3922
Bram Moolenaard3aac292020-04-19 14:32:17 +02003923 switch (iptr->isn_type)
3924 {
3925 case ISN_STOREG:
3926 ht = get_globvar_ht();
3927 break;
3928 case ISN_STOREB:
3929 ht = &curbuf->b_vars->dv_hashtab;
3930 break;
3931 case ISN_STOREW:
3932 ht = &curwin->w_vars->dv_hashtab;
3933 break;
3934 case ISN_STORET:
3935 ht = &curtab->tp_vars->dv_hashtab;
3936 break;
3937 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003938 goto theend;
Bram Moolenaard3aac292020-04-19 14:32:17 +02003939 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003940
Bram Moolenaar4c137212021-04-19 16:48:48 +02003941 --ectx->ec_stack.ga_len;
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003942 di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003943 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003944 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003945 else
3946 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003947 SOURCING_LNUM = iptr->isn_lnum;
3948 if (var_check_permission(di, name) == FAIL)
3949 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003950 clear_tv(&di->di_tv);
3951 di->di_tv = *STACK_TV_BOT(0);
3952 }
3953 }
3954 break;
3955
Bram Moolenaar03290b82020-12-19 16:30:44 +01003956 // store an autoload variable
3957 case ISN_STOREAUTO:
3958 SOURCING_LNUM = iptr->isn_lnum;
3959 set_var(iptr->isn_arg.string, STACK_TV_BOT(-1), TRUE);
3960 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003961 --ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003962 break;
3963
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003964 // store number in local variable
3965 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01003966 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003967 clear_tv(tv);
3968 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01003969 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003970 break;
3971
Bram Moolenaar590162c2022-12-24 21:24:06 +00003972 // Store a value in a list, dict, blob or object variable.
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01003973 case ISN_STOREINDEX:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003974 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003975 int res = execute_storeindex(iptr, ectx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003976
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003977 if (res == FAIL)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02003978 goto on_error;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003979 if (res == NOTDONE)
3980 goto theend;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003981 }
3982 break;
3983
Bram Moolenaarea5c8982022-02-17 14:42:02 +00003984 // store value in list or blob range
Bram Moolenaar68452172021-04-12 21:21:02 +02003985 case ISN_STORERANGE:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003986 if (execute_storerange(iptr, ectx) == FAIL)
3987 goto on_error;
Bram Moolenaar68452172021-04-12 21:21:02 +02003988 break;
3989
Bram Moolenaard505d172022-12-18 21:42:55 +00003990 case ISN_LOAD_CLASSMEMBER:
3991 {
3992 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
3993 goto theend;
3994 classmember_T *cm = &iptr->isn_arg.classmember;
Bram Moolenaar4e2406c2023-06-24 19:22:21 +01003995 copy_tv(cm->cm_class->class_members_tv + cm->cm_idx,
3996 STACK_TV_BOT(0));
Bram Moolenaard505d172022-12-18 21:42:55 +00003997 ++ectx->ec_stack.ga_len;
3998 }
3999 break;
4000
4001 case ISN_STORE_CLASSMEMBER:
4002 {
4003 classmember_T *cm = &iptr->isn_arg.classmember;
4004 tv = &cm->cm_class->class_members_tv[cm->cm_idx];
4005 clear_tv(tv);
4006 *tv = *STACK_TV_BOT(-1);
4007 --ectx->ec_stack.ga_len;
4008 }
4009 break;
4010
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01004011 // Load or store variable or argument from outer scope.
Bram Moolenaar0186e582021-01-10 18:33:11 +01004012 case ISN_LOADOUTER:
4013 case ISN_STOREOUTER:
4014 {
4015 int depth = iptr->isn_arg.outer.outer_depth;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004016 outer_T *outer = ectx->ec_outer_ref == NULL ? NULL
4017 : ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +01004018
4019 while (depth > 1 && outer != NULL)
4020 {
4021 outer = outer->out_up;
4022 --depth;
4023 }
4024 if (outer == NULL)
4025 {
4026 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar69c76172021-12-02 16:38:52 +00004027 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx
4028 || ectx->ec_outer_ref == NULL)
4029 // Possibly :def function called from legacy
4030 // context.
4031 emsg(_(e_closure_called_from_invalid_context));
4032 else
4033 iemsg("LOADOUTER depth more than scope levels");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004034 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01004035 }
Bram Moolenaarcc341812022-09-19 15:54:34 +01004036 if (depth < 0)
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01004037 // Variable declared in loop. May be copied if the
4038 // loop block has already ended.
Bram Moolenaarcc341812022-09-19 15:54:34 +01004039 tv = ((typval_T *)outer->out_loop[-depth - 1]
4040 .stack->ga_data)
4041 + outer->out_loop[-depth - 1].var_idx
4042 + iptr->isn_arg.outer.outer_idx;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004043 else
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01004044 // Variable declared in a function. May be copied if
4045 // the function has already returned.
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004046 tv = ((typval_T *)outer->out_stack->ga_data)
4047 + outer->out_frame_idx + STACK_FRAME_SIZE
4048 + iptr->isn_arg.outer.outer_idx;
Bram Moolenaar0186e582021-01-10 18:33:11 +01004049 if (iptr->isn_type == ISN_LOADOUTER)
4050 {
Bram Moolenaar35578162021-08-02 19:10:38 +02004051 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004052 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01004053 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02004054 ++ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01004055 }
4056 else
4057 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004058 --ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01004059 clear_tv(tv);
4060 *tv = *STACK_TV_BOT(0);
4061 }
4062 }
4063 break;
4064
Bram Moolenaar752fc692021-01-04 21:57:11 +01004065 // unlet item in list or dict variable
4066 case ISN_UNLETINDEX:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00004067 if (execute_unletindex(iptr, ectx) == FAIL)
4068 goto on_error;
Bram Moolenaar752fc692021-01-04 21:57:11 +01004069 break;
4070
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01004071 // unlet range of items in list variable
4072 case ISN_UNLETRANGE:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00004073 if (execute_unletrange(iptr, ectx) == FAIL)
4074 goto on_error;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01004075 break;
4076
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004077 // push constant
4078 case ISN_PUSHNR:
4079 case ISN_PUSHBOOL:
4080 case ISN_PUSHSPEC:
4081 case ISN_PUSHF:
4082 case ISN_PUSHS:
4083 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004084 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004085 case ISN_PUSHCHANNEL:
4086 case ISN_PUSHJOB:
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00004087 case ISN_PUSHOBJ:
4088 case ISN_PUSHCLASS:
Bram Moolenaar35578162021-08-02 19:10:38 +02004089 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004090 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004091 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02004092 tv->v_lock = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004093 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004094 switch (iptr->isn_type)
4095 {
4096 case ISN_PUSHNR:
4097 tv->v_type = VAR_NUMBER;
4098 tv->vval.v_number = iptr->isn_arg.number;
4099 break;
4100 case ISN_PUSHBOOL:
4101 tv->v_type = VAR_BOOL;
4102 tv->vval.v_number = iptr->isn_arg.number;
4103 break;
4104 case ISN_PUSHSPEC:
4105 tv->v_type = VAR_SPECIAL;
4106 tv->vval.v_number = iptr->isn_arg.number;
4107 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004108 case ISN_PUSHF:
4109 tv->v_type = VAR_FLOAT;
4110 tv->vval.v_float = iptr->isn_arg.fnumber;
4111 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004112 case ISN_PUSHBLOB:
4113 blob_copy(iptr->isn_arg.blob, tv);
4114 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004115 case ISN_PUSHFUNC:
4116 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01004117 if (iptr->isn_arg.string == NULL)
4118 tv->vval.v_string = NULL;
4119 else
4120 tv->vval.v_string =
4121 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004122 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004123 case ISN_PUSHCHANNEL:
4124#ifdef FEAT_JOB_CHANNEL
4125 tv->v_type = VAR_CHANNEL;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00004126 tv->vval.v_channel = NULL;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004127#endif
4128 break;
4129 case ISN_PUSHJOB:
4130#ifdef FEAT_JOB_CHANNEL
4131 tv->v_type = VAR_JOB;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00004132 tv->vval.v_job = NULL;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004133#endif
4134 break;
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00004135 case ISN_PUSHOBJ:
4136 tv->v_type = VAR_OBJECT;
4137 tv->vval.v_object = NULL;
4138 break;
4139 case ISN_PUSHCLASS:
4140 tv->v_type = VAR_CLASS;
Bram Moolenaar30a84472023-02-27 08:07:14 +00004141 tv->vval.v_class = iptr->isn_arg.classarg;
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00004142 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004143 default:
4144 tv->v_type = VAR_STRING;
Bram Moolenaarf8691002022-03-10 12:20:53 +00004145 tv->vval.v_string = iptr->isn_arg.string == NULL
4146 ? NULL : vim_strsave(iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004147 }
4148 break;
4149
Bram Moolenaar06b77222022-01-25 15:51:56 +00004150 case ISN_AUTOLOAD:
4151 {
4152 char_u *name = iptr->isn_arg.string;
4153
4154 (void)script_autoload(name, FALSE);
4155 if (find_func(name, TRUE))
4156 {
4157 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
4158 goto theend;
4159 tv = STACK_TV_BOT(0);
4160 tv->v_lock = 0;
4161 ++ectx->ec_stack.ga_len;
4162 tv->v_type = VAR_FUNC;
4163 tv->vval.v_string = vim_strsave(name);
4164 }
4165 else
4166 {
4167 int res = load_namespace_var(ectx, ISN_LOADG, iptr);
4168
4169 if (res == NOTDONE)
4170 goto theend;
4171 if (res == FAIL)
4172 goto on_error;
4173 }
4174 }
4175 break;
4176
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004177 case ISN_UNLET:
4178 if (do_unlet(iptr->isn_arg.unlet.ul_name,
4179 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02004180 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004181 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02004182 case ISN_UNLETENV:
LemonBoy77142312022-04-15 20:50:46 +01004183 vim_unsetenv_ext(iptr->isn_arg.unlet.ul_name);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02004184 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004185
Bram Moolenaaraacc9662021-08-13 19:40:51 +02004186 case ISN_LOCKUNLOCK:
4187 {
Ernie Rael64885642023-10-04 20:16:22 +02004188 lval_root_T *lval_root_save = lval_root;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02004189 int res;
Ernie Raelee865f32023-09-29 19:53:55 +02004190#ifdef LOG_LOCKVAR
4191 ch_log(NULL, "LKVAR: execute INS_LOCKUNLOCK isn_arg %s",
4192 iptr->isn_arg.string);
4193#endif
Bram Moolenaaraacc9662021-08-13 19:40:51 +02004194
4195 // Stack has the local variable, argument the whole :lock
4196 // or :unlock command, like ISN_EXEC.
4197 --ectx->ec_stack.ga_len;
Ernie Rael64885642023-10-04 20:16:22 +02004198 lval_root_T root = { STACK_TV_BOT(0),
4199 iptr->isn_arg.lockunlock.lu_cl_exec,
4200 iptr->isn_arg.lockunlock.lu_is_arg };
4201 lval_root = &root;
4202 res = exec_command(iptr,
4203 iptr->isn_arg.lockunlock.lu_string);
4204 clear_tv(root.lr_tv);
Bram Moolenaaraacc9662021-08-13 19:40:51 +02004205 lval_root = lval_root_save;
4206 if (res == FAIL)
4207 goto on_error;
4208 }
4209 break;
4210
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02004211 case ISN_LOCKCONST:
4212 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
4213 break;
4214
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004215 // create a list from items on the stack; uses a single allocation
4216 // for the list header and the items
4217 case ISN_NEWLIST:
Bram Moolenaar4c137212021-04-19 16:48:48 +02004218 if (exe_newlist(iptr->isn_arg.number, ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004219 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004220 break;
4221
4222 // create a dict from items on the stack
4223 case ISN_NEWDICT:
4224 {
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01004225 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004226
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01004227 SOURCING_LNUM = iptr->isn_lnum;
4228 res = exe_newdict(iptr->isn_arg.number, ectx);
4229 if (res == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004230 goto theend;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01004231 if (res == MAYBE)
4232 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004233 }
4234 break;
4235
LemonBoy372bcce2022-04-25 12:43:20 +01004236 case ISN_CONCAT:
4237 if (exe_concat(iptr->isn_arg.number, ectx) == FAIL)
4238 goto theend;
4239 break;
4240
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004241 // create a partial with NULL value
4242 case ISN_NEWPARTIAL:
4243 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
4244 goto theend;
4245 ++ectx->ec_stack.ga_len;
4246 tv = STACK_TV_BOT(-1);
4247 tv->v_type = VAR_PARTIAL;
4248 tv->v_lock = 0;
4249 tv->vval.v_partial = NULL;
4250 break;
4251
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004252 // call a :def function
4253 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02004254 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01004255 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
4256 NULL,
4257 iptr->isn_arg.dfunc.cdf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02004258 ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02004259 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004260 break;
4261
Bram Moolenaard0200c82023-01-28 15:19:40 +00004262 // call a method on an interface
4263 case ISN_METHODCALL:
4264 {
Bram Moolenaar094cf9f2023-02-10 15:52:25 +00004265 cmfunc_T *mfunc = iptr->isn_arg.mfunc;
4266
Bram Moolenaard0200c82023-01-28 15:19:40 +00004267 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar094cf9f2023-02-10 15:52:25 +00004268 tv = STACK_TV_BOT(-1 - mfunc->cmf_argcount);
Bram Moolenaard0200c82023-01-28 15:19:40 +00004269 if (tv->v_type != VAR_OBJECT)
4270 {
4271 object_required_error(tv);
4272 goto on_error;
4273 }
4274 object_T *obj = tv->vval.v_object;
4275 class_T *cl = obj->obj_class;
4276
4277 // convert the interface index to the object index
Bram Moolenaard0200c82023-01-28 15:19:40 +00004278 int idx = object_index_from_itf_index(mfunc->cmf_itf,
Yegappan Lakshmanan5a05d372023-09-29 19:43:11 +02004279 TRUE, mfunc->cmf_idx, cl);
Bram Moolenaard0200c82023-01-28 15:19:40 +00004280
4281 if (call_ufunc(cl->class_obj_methods[idx], NULL,
4282 mfunc->cmf_argcount, ectx, NULL, NULL) == FAIL)
4283 goto on_error;
4284 }
4285 break;
4286
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004287 // call a builtin function
4288 case ISN_BCALL:
4289 SOURCING_LNUM = iptr->isn_lnum;
4290 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
4291 iptr->isn_arg.bfunc.cbf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02004292 ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004293 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004294 break;
4295
4296 // call a funcref or partial
4297 case ISN_PCALL:
4298 {
4299 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
4300 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02004301 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004302
4303 SOURCING_LNUM = iptr->isn_lnum;
4304 if (pfunc->cpf_top)
4305 {
4306 // funcref is above the arguments
4307 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
4308 }
4309 else
4310 {
4311 // Get the funcref from the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004312 --ectx->ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02004313 partial_tv = *STACK_TV_BOT(0);
4314 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004315 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004316 r = call_partial(tv, pfunc->cpf_argcount, ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02004317 if (tv == &partial_tv)
4318 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004319 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004320 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004321 }
4322 break;
4323
Bram Moolenaarbd5da372020-03-31 23:13:10 +02004324 case ISN_PCALL_END:
4325 // PCALL finished, arguments have been consumed and replaced by
4326 // the return value. Now clear the funcref from the stack,
4327 // and move the return value in its place.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004328 --ectx->ec_stack.ga_len;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02004329 clear_tv(STACK_TV_BOT(-1));
4330 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
4331 break;
4332
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004333 // call a user defined function or funcref/partial
4334 case ISN_UCALL:
4335 {
4336 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
4337
4338 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01004339 if (call_eval_func(cufunc->cuf_name, cufunc->cuf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02004340 ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004341 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004342 }
4343 break;
4344
Bram Moolenaar1d84f762022-09-03 21:35:53 +01004345 // :defer func(arg)
4346 case ISN_DEFER:
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00004347 case ISN_DEFEROBJ:
Bram Moolenaar806a2732022-09-04 15:40:36 +01004348 if (defer_command(iptr->isn_arg.defer.defer_var_idx,
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00004349 iptr->isn_type == ISN_DEFEROBJ,
Bram Moolenaar1d84f762022-09-03 21:35:53 +01004350 iptr->isn_arg.defer.defer_argcount, ectx) == FAIL)
4351 goto on_error;
4352 break;
4353
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004354 // Return from a :def function call without a value.
4355 // Return from a constructor.
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02004356 case ISN_RETURN_VOID:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004357 case ISN_RETURN_OBJECT:
Bram Moolenaar35578162021-08-02 19:10:38 +02004358 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004359 goto theend;
Bram Moolenaar299f3032021-01-08 20:53:09 +01004360 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004361 ++ectx->ec_stack.ga_len;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004362 if (iptr->isn_type == ISN_RETURN_VOID)
4363 {
4364 tv->v_type = VAR_VOID;
4365 tv->vval.v_number = 0;
4366 tv->v_lock = 0;
4367 }
4368 else
4369 {
4370 *tv = *STACK_TV_VAR(0);
4371 ++tv->vval.v_object->obj_refcount;
4372 }
Bram Moolenaar299f3032021-01-08 20:53:09 +01004373 // FALLTHROUGH
4374
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02004375 // return from a :def function call with what is on the stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004376 case ISN_RETURN:
4377 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004378 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01004379 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01004380
4381 if (trystack->ga_len > 0)
4382 trycmd = ((trycmd_T *)trystack->ga_data)
4383 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004384 if (trycmd != NULL
Bram Moolenaar4c137212021-04-19 16:48:48 +02004385 && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004386 {
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01004387 // jump to ":finally" or ":endtry"
4388 if (trycmd->tcd_finally_idx != 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004389 ectx->ec_iidx = trycmd->tcd_finally_idx;
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01004390 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02004391 ectx->ec_iidx = trycmd->tcd_endtry_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004392 trycmd->tcd_return = TRUE;
4393 }
4394 else
Bram Moolenaard032f342020-07-18 18:13:02 +02004395 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004396 }
4397 break;
4398
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004399 // push a partial, a reference to a compiled function
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004400 case ISN_FUNCREF:
4401 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004402 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
4403 ufunc_T *ufunc;
4404 funcref_T *funcref = &iptr->isn_arg.funcref;
4405 funcref_extra_T *extra = funcref->fr_extra;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004406
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004407 if (pt == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004408 goto theend;
Bram Moolenaar35578162021-08-02 19:10:38 +02004409 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02004410 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004411 vim_free(pt);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004412 goto theend;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004413 }
Bram Moolenaar313e4722023-02-08 20:55:27 +00004414 if (extra != NULL && extra->fre_class != NULL)
4415 {
4416 tv = STACK_TV_BOT(-1);
4417 if (tv->v_type != VAR_OBJECT)
4418 {
4419 object_required_error(tv);
4420 vim_free(pt);
4421 goto on_error;
4422 }
4423 object_T *obj = tv->vval.v_object;
4424 class_T *cl = obj->obj_class;
4425
4426 // convert the interface index to the object index
4427 int idx = object_index_from_itf_index(extra->fre_class,
Yegappan Lakshmanan5a05d372023-09-29 19:43:11 +02004428 TRUE, extra->fre_method_idx, cl);
Bram Moolenaar313e4722023-02-08 20:55:27 +00004429 ufunc = cl->class_obj_methods[idx];
4430 }
4431 else if (extra == NULL || extra->fre_func_name == NULL)
Bram Moolenaar38453522021-11-28 22:00:12 +00004432 {
4433 dfunc_T *pt_dfunc = ((dfunc_T *)def_functions.ga_data)
4434 + funcref->fr_dfunc_idx;
4435
4436 ufunc = pt_dfunc->df_ufunc;
4437 }
4438 else
Bram Moolenaar313e4722023-02-08 20:55:27 +00004439 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004440 ufunc = find_func(extra->fre_func_name, FALSE);
Bram Moolenaar313e4722023-02-08 20:55:27 +00004441 }
Bram Moolenaar56acd1f2022-02-18 13:24:52 +00004442 if (ufunc == NULL)
4443 {
4444 SOURCING_LNUM = iptr->isn_lnum;
4445 iemsg("ufunc unexpectedly NULL for FUNCREF");
4446 goto theend;
4447 }
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004448 if (fill_partial_and_closure(pt, ufunc,
Bram Moolenaarcc341812022-09-19 15:54:34 +01004449 extra == NULL ? NULL : &extra->fre_loopvar_info,
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004450 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004451 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004452 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004453 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004454 tv->vval.v_partial = pt;
4455 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02004456 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004457 }
4458 break;
4459
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004460 // Create a global function from a lambda.
4461 case ISN_NEWFUNC:
4462 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004463 newfuncarg_T *arg = iptr->isn_arg.newfunc.nf_arg;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004464
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004465 if (copy_lambda_to_global_func(arg->nfa_lambda,
Bram Moolenaarcc341812022-09-19 15:54:34 +01004466 arg->nfa_global, &arg->nfa_loopvar_info,
4467 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004468 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004469 }
4470 break;
4471
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004472 // List functions
4473 case ISN_DEF:
4474 if (iptr->isn_arg.string == NULL)
4475 list_functions(NULL);
4476 else
4477 {
Bram Moolenaar14336722022-01-08 16:02:59 +00004478 exarg_T ea;
4479 garray_T lines_to_free;
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004480
4481 CLEAR_FIELD(ea);
4482 ea.cmd = ea.arg = iptr->isn_arg.string;
Bram Moolenaar14336722022-01-08 16:02:59 +00004483 ga_init2(&lines_to_free, sizeof(char_u *), 50);
Bram Moolenaard4a9b7f2023-05-23 14:48:42 +01004484 SOURCING_LNUM = iptr->isn_lnum;
h-eastb895b0f2023-09-24 15:46:31 +02004485 define_function(&ea, NULL, &lines_to_free, 0, NULL, 0);
Bram Moolenaar14336722022-01-08 16:02:59 +00004486 ga_clear_strings(&lines_to_free);
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004487 }
4488 break;
4489
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004490 // jump if a condition is met
4491 case ISN_JUMP:
4492 {
4493 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004494 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004495 int jump = TRUE;
4496
4497 if (when != JUMP_ALWAYS)
4498 {
4499 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004500 if (when == JUMP_IF_COND_FALSE
Bram Moolenaar13106602020-10-04 16:06:05 +02004501 || when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004502 || when == JUMP_IF_COND_TRUE)
4503 {
4504 SOURCING_LNUM = iptr->isn_lnum;
4505 jump = tv_get_bool_chk(tv, &error);
4506 if (error)
4507 goto on_error;
4508 }
4509 else
4510 jump = tv2bool(tv);
Bram Moolenaarf6ced982022-04-28 12:00:49 +01004511 if (when == JUMP_IF_FALSE || when == JUMP_IF_COND_FALSE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004512 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01004513 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004514 {
4515 // drop the value from the stack
4516 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004517 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004518 }
4519 }
4520 if (jump)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004521 ectx->ec_iidx = iptr->isn_arg.jump.jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004522 }
4523 break;
4524
Bram Moolenaarb46c0832022-09-15 17:19:37 +01004525 // "while": jump to end if a condition is false
4526 case ISN_WHILE:
4527 {
4528 int error = FALSE;
4529 int jump = TRUE;
4530
4531 tv = STACK_TV_BOT(-1);
4532 SOURCING_LNUM = iptr->isn_lnum;
4533 jump = !tv_get_bool_chk(tv, &error);
4534 if (error)
4535 goto on_error;
4536 // drop the value from the stack
4537 clear_tv(tv);
4538 --ectx->ec_stack.ga_len;
4539 if (jump)
4540 ectx->ec_iidx = iptr->isn_arg.whileloop.while_end;
4541
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01004542 // Store the current funcref count, may be used by
Bram Moolenaarcc341812022-09-19 15:54:34 +01004543 // ISN_ENDLOOP later
Bram Moolenaarb46c0832022-09-15 17:19:37 +01004544 tv = STACK_TV_VAR(
4545 iptr->isn_arg.whileloop.while_funcref_idx);
4546 tv->vval.v_number = ectx->ec_funcrefs.ga_len;
4547 }
4548 break;
4549
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02004550 // Jump if an argument with a default value was already set and not
4551 // v:none.
4552 case ISN_JUMP_IF_ARG_SET:
Bram Moolenaar65b0d162022-12-13 18:43:22 +00004553 case ISN_JUMP_IF_ARG_NOT_SET:
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02004554 tv = STACK_TV_VAR(iptr->isn_arg.jumparg.jump_arg_off);
Bram Moolenaar65b0d162022-12-13 18:43:22 +00004555 int arg_set = tv->v_type != VAR_UNKNOWN
4556 && !(tv->v_type == VAR_SPECIAL
4557 && tv->vval.v_number == VVAL_NONE);
4558 if (iptr->isn_type == ISN_JUMP_IF_ARG_SET ? arg_set : !arg_set)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004559 ectx->ec_iidx = iptr->isn_arg.jumparg.jump_where;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02004560 break;
4561
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004562 // top of a for loop
4563 case ISN_FOR:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00004564 if (execute_for(iptr, ectx) == FAIL)
4565 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004566 break;
4567
Bram Moolenaarb46c0832022-09-15 17:19:37 +01004568 // end of a for or while loop
4569 case ISN_ENDLOOP:
4570 if (execute_endloop(iptr, ectx) == FAIL)
4571 goto theend;
4572 break;
4573
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004574 // start of ":try" block
4575 case ISN_TRY:
4576 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01004577 trycmd_T *trycmd = NULL;
4578
Bram Moolenaar35578162021-08-02 19:10:38 +02004579 if (GA_GROW_FAILS(&ectx->ec_trystack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004580 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004581 trycmd = ((trycmd_T *)ectx->ec_trystack.ga_data)
4582 + ectx->ec_trystack.ga_len;
4583 ++ectx->ec_trystack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004584 ++trylevel;
Bram Moolenaar8d4be892021-02-13 18:33:02 +01004585 CLEAR_POINTER(trycmd);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004586 trycmd->tcd_frame_idx = ectx->ec_frame_idx;
4587 trycmd->tcd_stack_len = ectx->ec_stack.ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01004588 trycmd->tcd_catch_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00004589 iptr->isn_arg.tryref.try_ref->try_catch;
Bram Moolenaara91a7132021-03-25 21:12:15 +01004590 trycmd->tcd_finally_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00004591 iptr->isn_arg.tryref.try_ref->try_finally;
Bram Moolenaara91a7132021-03-25 21:12:15 +01004592 trycmd->tcd_endtry_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00004593 iptr->isn_arg.tryref.try_ref->try_endtry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004594 }
4595 break;
4596
4597 case ISN_PUSHEXC:
4598 if (current_exception == NULL)
4599 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004600 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004601 iemsg("Evaluating catch while current_exception is NULL");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004602 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004603 }
Bram Moolenaar35578162021-08-02 19:10:38 +02004604 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004605 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004606 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004607 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004608 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02004609 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004610 tv->vval.v_string = vim_strsave(
4611 (char_u *)current_exception->value);
4612 break;
4613
4614 case ISN_CATCH:
4615 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004616 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar06651632022-04-27 17:54:25 +01004617 trycmd_T *trycmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004618
Bram Moolenaar4c137212021-04-19 16:48:48 +02004619 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar06651632022-04-27 17:54:25 +01004620 trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004621 + trystack->ga_len - 1;
Bram Moolenaar06651632022-04-27 17:54:25 +01004622 trycmd->tcd_caught = TRUE;
4623 trycmd->tcd_did_throw = FALSE;
4624
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004625 did_emsg = got_int = did_throw = FALSE;
Bram Moolenaar1430cee2021-01-17 19:20:32 +01004626 force_abort = need_rethrow = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004627 catch_exception(current_exception);
4628 }
4629 break;
4630
Bram Moolenaarc150c092021-02-13 15:02:46 +01004631 case ISN_TRYCONT:
4632 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004633 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004634 trycont_T *trycont = &iptr->isn_arg.trycont;
4635 int i;
4636 trycmd_T *trycmd;
4637 int iidx = trycont->tct_where;
4638
4639 if (trystack->ga_len < trycont->tct_levels)
4640 {
4641 siemsg("TRYCONT: expected %d levels, found %d",
4642 trycont->tct_levels, trystack->ga_len);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004643 goto theend;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004644 }
4645 // Make :endtry jump to any outer try block and the last
4646 // :endtry inside the loop to the loop start.
4647 for (i = trycont->tct_levels; i > 0; --i)
4648 {
4649 trycmd = ((trycmd_T *)trystack->ga_data)
4650 + trystack->ga_len - i;
Bram Moolenaar2e34c342021-03-14 12:13:33 +01004651 // Add one to tcd_cont to be able to jump to
4652 // instruction with index zero.
4653 trycmd->tcd_cont = iidx + 1;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01004654 iidx = trycmd->tcd_finally_idx == 0
4655 ? trycmd->tcd_endtry_idx : trycmd->tcd_finally_idx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004656 }
4657 // jump to :finally or :endtry of current try statement
Bram Moolenaar4c137212021-04-19 16:48:48 +02004658 ectx->ec_iidx = iidx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004659 }
4660 break;
4661
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01004662 case ISN_FINALLY:
4663 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004664 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01004665 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
4666 + trystack->ga_len - 1;
4667
4668 // Reset the index to avoid a return statement jumps here
4669 // again.
4670 trycmd->tcd_finally_idx = 0;
4671 break;
4672 }
4673
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004674 // end of ":try" block
4675 case ISN_ENDTRY:
4676 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004677 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar06651632022-04-27 17:54:25 +01004678 trycmd_T *trycmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004679
Bram Moolenaar06651632022-04-27 17:54:25 +01004680 --trystack->ga_len;
4681 --trylevel;
4682 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len;
4683 if (trycmd->tcd_did_throw)
4684 did_throw = TRUE;
4685 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004686 {
Bram Moolenaar06651632022-04-27 17:54:25 +01004687 // discard the exception
4688 if (caught_stack == current_exception)
4689 caught_stack = caught_stack->caught;
4690 discard_current_exception();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004691 }
Bram Moolenaar06651632022-04-27 17:54:25 +01004692
4693 if (trycmd->tcd_return)
4694 goto func_return;
4695
4696 while (ectx->ec_stack.ga_len > trycmd->tcd_stack_len)
4697 {
4698 --ectx->ec_stack.ga_len;
4699 clear_tv(STACK_TV_BOT(0));
4700 }
4701 if (trycmd->tcd_cont != 0)
4702 // handling :continue: jump to outer try block or
4703 // start of the loop
4704 ectx->ec_iidx = trycmd->tcd_cont - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004705 }
4706 break;
4707
4708 case ISN_THROW:
Bram Moolenaar8f81b222021-01-14 21:47:06 +01004709 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004710 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02004711
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004712 if (trystack->ga_len == 0 && trylevel == 0 && emsg_silent)
4713 {
Bram Moolenaar3ef2e412023-04-30 18:50:48 +01004714 // Throwing an exception while using "silent!" causes
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004715 // the function to abort but not display an error.
4716 tv = STACK_TV_BOT(-1);
4717 clear_tv(tv);
4718 tv->v_type = VAR_NUMBER;
4719 tv->vval.v_number = 0;
4720 goto done;
4721 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004722 --ectx->ec_stack.ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004723 tv = STACK_TV_BOT(0);
4724 if (tv->vval.v_string == NULL
4725 || *skipwhite(tv->vval.v_string) == NUL)
4726 {
4727 vim_free(tv->vval.v_string);
4728 SOURCING_LNUM = iptr->isn_lnum;
4729 emsg(_(e_throw_with_empty_string));
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004730 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004731 }
4732
4733 // Inside a "catch" we need to first discard the caught
4734 // exception.
4735 if (trystack->ga_len > 0)
4736 {
4737 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
4738 + trystack->ga_len - 1;
4739 if (trycmd->tcd_caught && current_exception != NULL)
4740 {
4741 // discard the exception
4742 if (caught_stack == current_exception)
4743 caught_stack = caught_stack->caught;
4744 discard_current_exception();
4745 trycmd->tcd_caught = FALSE;
4746 }
4747 }
4748
Bram Moolenaar90a57162022-02-12 14:23:17 +00004749 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004750 if (throw_exception(tv->vval.v_string, ET_USER, NULL)
4751 == FAIL)
4752 {
4753 vim_free(tv->vval.v_string);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004754 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004755 }
4756 did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004757 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004758 break;
4759
4760 // compare with special values
4761 case ISN_COMPAREBOOL:
4762 case ISN_COMPARESPECIAL:
4763 {
4764 typval_T *tv1 = STACK_TV_BOT(-2);
4765 typval_T *tv2 = STACK_TV_BOT(-1);
4766 varnumber_T arg1 = tv1->vval.v_number;
4767 varnumber_T arg2 = tv2->vval.v_number;
4768 int res;
4769
Bram Moolenaar06651632022-04-27 17:54:25 +01004770 if (iptr->isn_arg.op.op_type == EXPR_EQUAL)
4771 res = arg1 == arg2;
4772 else
4773 res = arg1 != arg2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004774
Bram Moolenaar4c137212021-04-19 16:48:48 +02004775 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004776 tv1->v_type = VAR_BOOL;
4777 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4778 }
4779 break;
4780
Bram Moolenaar7a222242022-03-01 19:23:24 +00004781 case ISN_COMPARENULL:
4782 {
4783 typval_T *tv1 = STACK_TV_BOT(-2);
4784 typval_T *tv2 = STACK_TV_BOT(-1);
4785 int res;
4786
4787 res = typval_compare_null(tv1, tv2);
4788 if (res == MAYBE)
4789 goto on_error;
4790 if (iptr->isn_arg.op.op_type == EXPR_NEQUAL)
4791 res = !res;
4792 clear_tv(tv1);
4793 clear_tv(tv2);
4794 --ectx->ec_stack.ga_len;
4795 tv1->v_type = VAR_BOOL;
4796 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4797 }
4798 break;
4799
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004800 // Operation with two number arguments
4801 case ISN_OPNR:
4802 case ISN_COMPARENR:
4803 {
4804 typval_T *tv1 = STACK_TV_BOT(-2);
4805 typval_T *tv2 = STACK_TV_BOT(-1);
4806 varnumber_T arg1 = tv1->vval.v_number;
4807 varnumber_T arg2 = tv2->vval.v_number;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004808 varnumber_T res = 0;
4809 int div_zero = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004810
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004811 if (iptr->isn_arg.op.op_type == EXPR_LSHIFT
4812 || iptr->isn_arg.op.op_type == EXPR_RSHIFT)
4813 {
4814 if (arg2 < 0)
4815 {
4816 SOURCING_LNUM = iptr->isn_lnum;
dundargocc57b5bc2022-11-02 13:30:51 +00004817 emsg(_(e_bitshift_ops_must_be_positive));
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004818 goto on_error;
4819 }
4820 }
4821
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004822 switch (iptr->isn_arg.op.op_type)
4823 {
4824 case EXPR_MULT: res = arg1 * arg2; break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004825 case EXPR_DIV: if (arg2 == 0)
4826 div_zero = TRUE;
4827 else
4828 res = arg1 / arg2;
4829 break;
4830 case EXPR_REM: if (arg2 == 0)
4831 div_zero = TRUE;
4832 else
4833 res = arg1 % arg2;
4834 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004835 case EXPR_SUB: res = arg1 - arg2; break;
4836 case EXPR_ADD: res = arg1 + arg2; break;
4837
4838 case EXPR_EQUAL: res = arg1 == arg2; break;
4839 case EXPR_NEQUAL: res = arg1 != arg2; break;
4840 case EXPR_GREATER: res = arg1 > arg2; break;
4841 case EXPR_GEQUAL: res = arg1 >= arg2; break;
4842 case EXPR_SMALLER: res = arg1 < arg2; break;
4843 case EXPR_SEQUAL: res = arg1 <= arg2; break;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004844 case EXPR_LSHIFT: if (arg2 > MAX_LSHIFT_BITS)
4845 res = 0;
4846 else
Bram Moolenaar68e64d22022-05-22 22:07:52 +01004847 res = (uvarnumber_T)arg1 << arg2;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004848 break;
4849 case EXPR_RSHIFT: if (arg2 > MAX_LSHIFT_BITS)
4850 res = 0;
4851 else
Bram Moolenaar338bf582022-05-22 20:16:32 +01004852 res = (uvarnumber_T)arg1 >> arg2;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004853 break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004854 default: break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004855 }
4856
Bram Moolenaar4c137212021-04-19 16:48:48 +02004857 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004858 if (iptr->isn_type == ISN_COMPARENR)
4859 {
4860 tv1->v_type = VAR_BOOL;
4861 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4862 }
4863 else
4864 tv1->vval.v_number = res;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004865 if (div_zero)
4866 {
4867 SOURCING_LNUM = iptr->isn_lnum;
4868 emsg(_(e_divide_by_zero));
4869 goto on_error;
4870 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004871 }
4872 break;
4873
4874 // Computation with two float arguments
4875 case ISN_OPFLOAT:
4876 case ISN_COMPAREFLOAT:
4877 {
4878 typval_T *tv1 = STACK_TV_BOT(-2);
4879 typval_T *tv2 = STACK_TV_BOT(-1);
4880 float_T arg1 = tv1->vval.v_float;
4881 float_T arg2 = tv2->vval.v_float;
4882 float_T res = 0;
4883 int cmp = FALSE;
4884
4885 switch (iptr->isn_arg.op.op_type)
4886 {
4887 case EXPR_MULT: res = arg1 * arg2; break;
4888 case EXPR_DIV: res = arg1 / arg2; break;
4889 case EXPR_SUB: res = arg1 - arg2; break;
4890 case EXPR_ADD: res = arg1 + arg2; break;
4891
4892 case EXPR_EQUAL: cmp = arg1 == arg2; break;
4893 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
4894 case EXPR_GREATER: cmp = arg1 > arg2; break;
4895 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
4896 case EXPR_SMALLER: cmp = arg1 < arg2; break;
4897 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
4898 default: cmp = 0; break;
4899 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004900 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004901 if (iptr->isn_type == ISN_COMPAREFLOAT)
4902 {
4903 tv1->v_type = VAR_BOOL;
4904 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
4905 }
4906 else
4907 tv1->vval.v_float = res;
4908 }
4909 break;
4910
4911 case ISN_COMPARELIST:
Bram Moolenaar265f8112021-12-19 12:33:05 +00004912 case ISN_COMPAREDICT:
4913 case ISN_COMPAREFUNC:
4914 case ISN_COMPARESTRING:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004915 case ISN_COMPAREBLOB:
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00004916 case ISN_COMPARECLASS:
4917 case ISN_COMPAREOBJECT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004918 {
4919 typval_T *tv1 = STACK_TV_BOT(-2);
4920 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar265f8112021-12-19 12:33:05 +00004921 exprtype_T exprtype = iptr->isn_arg.op.op_type;
4922 int ic = iptr->isn_arg.op.op_ic;
4923 int res = FALSE;
4924 int status = OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004925
Bram Moolenaar265f8112021-12-19 12:33:05 +00004926 SOURCING_LNUM = iptr->isn_lnum;
4927 if (iptr->isn_type == ISN_COMPARELIST)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004928 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00004929 status = typval_compare_list(tv1, tv2,
4930 exprtype, ic, &res);
4931 }
4932 else if (iptr->isn_type == ISN_COMPAREDICT)
4933 {
4934 status = typval_compare_dict(tv1, tv2,
4935 exprtype, ic, &res);
4936 }
4937 else if (iptr->isn_type == ISN_COMPAREFUNC)
4938 {
4939 status = typval_compare_func(tv1, tv2,
4940 exprtype, ic, &res);
4941 }
4942 else if (iptr->isn_type == ISN_COMPARESTRING)
4943 {
4944 status = typval_compare_string(tv1, tv2,
4945 exprtype, ic, &res);
4946 }
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00004947 else if (iptr->isn_type == ISN_COMPAREBLOB)
Bram Moolenaar265f8112021-12-19 12:33:05 +00004948 {
4949 status = typval_compare_blob(tv1, tv2, exprtype, &res);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004950 }
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00004951 else if (iptr->isn_type == ISN_COMPARECLASS)
4952 {
Bram Moolenaar03ff0c62023-01-02 20:38:01 +00004953 status = typval_compare_class(tv1, tv2,
4954 exprtype, FALSE, &res);
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00004955 }
4956 else // ISN_COMPAREOBJECT
4957 {
4958 status = typval_compare_object(tv1, tv2,
Bram Moolenaar03ff0c62023-01-02 20:38:01 +00004959 exprtype, FALSE, &res);
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00004960 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004961 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004962 clear_tv(tv1);
4963 clear_tv(tv2);
4964 tv1->v_type = VAR_BOOL;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004965 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4966 if (status == FAIL)
4967 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004968 }
4969 break;
4970
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004971 case ISN_COMPAREANY:
4972 {
4973 typval_T *tv1 = STACK_TV_BOT(-2);
4974 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar657137c2021-01-09 15:45:23 +01004975 exprtype_T exprtype = iptr->isn_arg.op.op_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004976 int ic = iptr->isn_arg.op.op_ic;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004977 int status;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004978
Bram Moolenaareb26f432020-09-14 16:50:05 +02004979 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004980 status = typval_compare(tv1, tv2, exprtype, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004981 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004982 --ectx->ec_stack.ga_len;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004983 if (status == FAIL)
4984 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004985 }
4986 break;
4987
4988 case ISN_ADDLIST:
4989 case ISN_ADDBLOB:
4990 {
4991 typval_T *tv1 = STACK_TV_BOT(-2);
4992 typval_T *tv2 = STACK_TV_BOT(-1);
4993
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004994 // add two lists or blobs
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004995 if (iptr->isn_type == ISN_ADDLIST)
Bram Moolenaar07802042021-09-09 23:01:14 +02004996 {
4997 if (iptr->isn_arg.op.op_type == EXPR_APPEND
4998 && tv1->vval.v_list != NULL)
4999 list_extend(tv1->vval.v_list, tv2->vval.v_list,
5000 NULL);
5001 else
5002 eval_addlist(tv1, tv2);
5003 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005004 else
5005 eval_addblob(tv1, tv2);
5006 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005007 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005008 }
5009 break;
5010
Bram Moolenaar1dcae592020-10-19 19:02:42 +02005011 case ISN_LISTAPPEND:
5012 {
5013 typval_T *tv1 = STACK_TV_BOT(-2);
5014 typval_T *tv2 = STACK_TV_BOT(-1);
5015 list_T *l = tv1->vval.v_list;
5016
5017 // add an item to a list
Bram Moolenaar1f4a3452022-01-01 18:29:21 +00005018 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02005019 if (l == NULL)
5020 {
Bram Moolenaar1dcae592020-10-19 19:02:42 +02005021 emsg(_(e_cannot_add_to_null_list));
5022 goto on_error;
5023 }
Bram Moolenaar1f4a3452022-01-01 18:29:21 +00005024 if (value_check_lock(l->lv_lock, NULL, FALSE))
5025 goto on_error;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02005026 if (list_append_tv(l, tv2) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005027 goto theend;
Bram Moolenaar955347c2020-10-19 23:01:46 +02005028 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005029 --ectx->ec_stack.ga_len;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02005030 }
5031 break;
5032
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02005033 case ISN_BLOBAPPEND:
5034 {
5035 typval_T *tv1 = STACK_TV_BOT(-2);
5036 typval_T *tv2 = STACK_TV_BOT(-1);
5037 blob_T *b = tv1->vval.v_blob;
5038 int error = FALSE;
5039 varnumber_T n;
5040
5041 // add a number to a blob
5042 if (b == NULL)
5043 {
5044 SOURCING_LNUM = iptr->isn_lnum;
5045 emsg(_(e_cannot_add_to_null_blob));
5046 goto on_error;
5047 }
5048 n = tv_get_number_chk(tv2, &error);
5049 if (error)
5050 goto on_error;
5051 ga_append(&b->bv_ga, (int)n);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005052 --ectx->ec_stack.ga_len;
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02005053 }
5054 break;
5055
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005056 // Computation with two arguments of unknown type
5057 case ISN_OPANY:
5058 {
5059 typval_T *tv1 = STACK_TV_BOT(-2);
5060 typval_T *tv2 = STACK_TV_BOT(-1);
5061 varnumber_T n1, n2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005062 float_T f1 = 0, f2 = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005063 int error = FALSE;
5064
5065 if (iptr->isn_arg.op.op_type == EXPR_ADD)
5066 {
5067 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
5068 {
5069 eval_addlist(tv1, tv2);
5070 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005071 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005072 break;
5073 }
5074 else if (tv1->v_type == VAR_BLOB
5075 && tv2->v_type == VAR_BLOB)
5076 {
5077 eval_addblob(tv1, tv2);
5078 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005079 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005080 break;
5081 }
5082 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005083 if (tv1->v_type == VAR_FLOAT)
5084 {
5085 f1 = tv1->vval.v_float;
5086 n1 = 0;
5087 }
5088 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005089 {
Bram Moolenaarf665e972020-12-05 19:17:16 +01005090 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005091 n1 = tv_get_number_chk(tv1, &error);
5092 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02005093 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005094 if (tv2->v_type == VAR_FLOAT)
5095 f1 = n1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005096 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005097 if (tv2->v_type == VAR_FLOAT)
5098 {
5099 f2 = tv2->vval.v_float;
5100 n2 = 0;
5101 }
5102 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005103 {
5104 n2 = tv_get_number_chk(tv2, &error);
5105 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02005106 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005107 if (tv1->v_type == VAR_FLOAT)
5108 f2 = n2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005109 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005110 // if there is a float on either side the result is a float
5111 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
5112 {
5113 switch (iptr->isn_arg.op.op_type)
5114 {
5115 case EXPR_MULT: f1 = f1 * f2; break;
5116 case EXPR_DIV: f1 = f1 / f2; break;
5117 case EXPR_SUB: f1 = f1 - f2; break;
5118 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005119 default: SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005120 emsg(_(e_cannot_use_percent_with_float));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02005121 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005122 }
5123 clear_tv(tv1);
5124 clear_tv(tv2);
5125 tv1->v_type = VAR_FLOAT;
5126 tv1->vval.v_float = f1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005127 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005128 }
5129 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005130 {
Bram Moolenaarb1f28572021-01-21 13:03:20 +01005131 int failed = FALSE;
5132
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005133 switch (iptr->isn_arg.op.op_type)
5134 {
5135 case EXPR_MULT: n1 = n1 * n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01005136 case EXPR_DIV: n1 = num_divide(n1, n2, &failed);
5137 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01005138 goto on_error;
5139 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005140 case EXPR_SUB: n1 = n1 - n2; break;
5141 case EXPR_ADD: n1 = n1 + n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01005142 default: n1 = num_modulus(n1, n2, &failed);
5143 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01005144 goto on_error;
5145 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005146 }
5147 clear_tv(tv1);
5148 clear_tv(tv2);
5149 tv1->v_type = VAR_NUMBER;
5150 tv1->vval.v_number = n1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005151 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005152 }
5153 }
5154 break;
5155
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005156 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005157 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005158 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005159 int is_slice = iptr->isn_type == ISN_STRSLICE;
5160 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005161 char_u *res;
5162
5163 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005164 // string slice: string is at stack-3, first index at
5165 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005166 if (is_slice)
5167 {
5168 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005169 n1 = tv->vval.v_number;
5170 }
5171
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005172 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005173 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005174
Bram Moolenaar4c137212021-04-19 16:48:48 +02005175 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005176 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005177 if (is_slice)
5178 // Slice: Select the characters from the string
Bram Moolenaar6601b622021-01-13 21:47:15 +01005179 res = string_slice(tv->vval.v_string, n1, n2, FALSE);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005180 else
5181 // Index: The resulting variable is a string of a
Bram Moolenaar0289a092021-03-14 18:40:19 +01005182 // single character (including composing characters).
5183 // If the index is too big or negative the result is
5184 // empty.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005185 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005186 vim_free(tv->vval.v_string);
5187 tv->vval.v_string = res;
5188 }
5189 break;
5190
5191 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02005192 case ISN_LISTSLICE:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02005193 case ISN_BLOBINDEX:
5194 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005195 {
Bram Moolenaarcfc30232021-04-11 20:26:34 +02005196 int is_slice = iptr->isn_type == ISN_LISTSLICE
5197 || iptr->isn_type == ISN_BLOBSLICE;
5198 int is_blob = iptr->isn_type == ISN_BLOBINDEX
5199 || iptr->isn_type == ISN_BLOBSLICE;
Bram Moolenaared591872020-08-15 22:14:53 +02005200 varnumber_T n1, n2;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02005201 typval_T *val_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005202
5203 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02005204 // list slice: list is at stack-3, indexes at stack-2 and
5205 // stack-1
Bram Moolenaarcfc30232021-04-11 20:26:34 +02005206 // Same for blob.
5207 val_tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005208
5209 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02005210 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005211 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02005212
5213 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005214 {
Bram Moolenaared591872020-08-15 22:14:53 +02005215 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02005216 n1 = tv->vval.v_number;
5217 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005218 }
Bram Moolenaared591872020-08-15 22:14:53 +02005219
Bram Moolenaar4c137212021-04-19 16:48:48 +02005220 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02005221 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02005222 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02005223 if (is_blob)
5224 {
5225 if (blob_slice_or_index(val_tv->vval.v_blob, is_slice,
5226 n1, n2, FALSE, tv) == FAIL)
5227 goto on_error;
5228 }
5229 else
5230 {
5231 if (list_slice_or_index(val_tv->vval.v_list, is_slice,
5232 n1, n2, FALSE, tv, TRUE) == FAIL)
5233 goto on_error;
5234 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005235 }
5236 break;
5237
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005238 case ISN_ANYINDEX:
5239 case ISN_ANYSLICE:
5240 {
5241 int is_slice = iptr->isn_type == ISN_ANYSLICE;
5242 typval_T *var1, *var2;
5243 int res;
5244
5245 // index: composite is at stack-2, index at stack-1
5246 // slice: composite is at stack-3, indexes at stack-2 and
5247 // stack-1
5248 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02005249 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005250 if (check_can_index(tv, TRUE, TRUE) == FAIL)
5251 goto on_error;
5252 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
5253 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
Bram Moolenaar6601b622021-01-13 21:47:15 +01005254 res = eval_index_inner(tv, is_slice, var1, var2,
5255 FALSE, NULL, -1, TRUE);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005256 clear_tv(var1);
5257 if (is_slice)
5258 clear_tv(var2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005259 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005260 if (res == FAIL)
5261 goto on_error;
5262 }
5263 break;
5264
Bram Moolenaar9af78762020-06-16 11:34:42 +02005265 case ISN_SLICE:
5266 {
5267 list_T *list;
5268 int count = iptr->isn_arg.number;
5269
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02005270 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02005271 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02005272 list = tv->vval.v_list;
5273
5274 // no error for short list, expect it to be checked earlier
5275 if (list != NULL && list->lv_len >= count)
5276 {
5277 list_T *newlist = list_slice(list,
5278 count, list->lv_len - 1);
5279
5280 if (newlist != NULL)
5281 {
5282 list_unref(list);
5283 tv->vval.v_list = newlist;
5284 ++newlist->lv_refcount;
5285 }
5286 }
5287 }
5288 break;
5289
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005290 case ISN_GETITEM:
5291 {
5292 listitem_T *li;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02005293 getitem_T *gi = &iptr->isn_arg.getitem;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005294
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02005295 // Get list item: list is at stack-1, push item.
5296 // List type and length is checked for when compiling.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02005297 tv = STACK_TV_BOT(-1 - gi->gi_with_op);
5298 li = list_find(tv->vval.v_list, gi->gi_index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005299
Bram Moolenaar35578162021-08-02 19:10:38 +02005300 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005301 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005302 ++ectx->ec_stack.ga_len;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005303 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
Bram Moolenaarf785aa12021-02-11 21:19:34 +01005304
5305 // Useful when used in unpack assignment. Reset at
5306 // ISN_DROP.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02005307 ectx->ec_where.wt_index = gi->gi_index + 1;
LemonBoyc5d27442023-08-19 13:02:35 +02005308 ectx->ec_where.wt_kind = WT_VARIABLE;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005309 }
5310 break;
5311
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005312 case ISN_MEMBER:
5313 {
5314 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005315 char_u *key;
5316 dictitem_T *di;
5317
5318 // dict member: dict is at stack-2, key at stack-1
5319 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02005320 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005321 dict = tv->vval.v_dict;
5322
5323 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02005324 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005325 key = tv->vval.v_string;
Bram Moolenaar086fc9a2020-10-30 18:33:02 +01005326 if (key == NULL)
5327 key = (char_u *)"";
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02005328
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005329 if ((di = dict_find(dict, key, -1)) == NULL)
5330 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005331 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00005332 semsg(_(e_key_not_present_in_dictionary_str), key);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01005333
5334 // If :silent! is used we will continue, make sure the
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005335 // stack contents makes sense and the dict stack is
5336 // updated.
Bram Moolenaar4029cab2020-12-05 18:13:27 +01005337 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005338 --ectx->ec_stack.ga_len;
Bram Moolenaar4029cab2020-12-05 18:13:27 +01005339 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005340 (void) dict_stack_save(tv);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01005341 tv->v_type = VAR_NUMBER;
5342 tv->vval.v_number = 0;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01005343 goto on_fatal_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005344 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005345 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005346 --ectx->ec_stack.ga_len;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005347 // Put the dict used on the dict stack, it might be used by
5348 // a dict function later.
Bram Moolenaar50788ef2020-07-05 16:51:26 +02005349 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005350 if (dict_stack_save(tv) == FAIL)
5351 goto on_fatal_error;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02005352 copy_tv(&di->di_tv, tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005353 }
5354 break;
5355
5356 // dict member with string key
5357 case ISN_STRINGMEMBER:
5358 {
5359 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005360 dictitem_T *di;
5361
5362 tv = STACK_TV_BOT(-1);
5363 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
5364 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005365 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005366 emsg(_(e_dictionary_required));
Bram Moolenaard032f342020-07-18 18:13:02 +02005367 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005368 }
5369 dict = tv->vval.v_dict;
5370
5371 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
5372 == NULL)
5373 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005374 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00005375 semsg(_(e_key_not_present_in_dictionary_str),
Bram Moolenaar381692b2022-02-02 20:01:27 +00005376 iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02005377 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005378 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005379 // Put the dict used on the dict stack, it might be used by
5380 // a dict function later.
5381 if (dict_stack_save(tv) == FAIL)
5382 goto on_fatal_error;
5383
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005384 copy_tv(&di->di_tv, tv);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005385 }
5386 break;
5387
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005388 case ISN_GET_OBJ_MEMBER:
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00005389 case ISN_GET_ITF_MEMBER:
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005390 {
5391 tv = STACK_TV_BOT(-1);
5392 if (tv->v_type != VAR_OBJECT)
5393 {
5394 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaard0200c82023-01-28 15:19:40 +00005395 object_required_error(tv);
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005396 goto on_error;
5397 }
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00005398
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005399 object_T *obj = tv->vval.v_object;
Bram Moolenaarc3f971f2023-03-02 17:38:33 +00005400 if (obj == NULL)
5401 {
5402 SOURCING_LNUM = iptr->isn_lnum;
5403 emsg(_(e_using_null_object));
5404 goto on_error;
5405 }
5406
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00005407 int idx;
5408 if (iptr->isn_type == ISN_GET_OBJ_MEMBER)
Ernie Rael18143d32023-09-04 22:30:41 +02005409 idx = iptr->isn_arg.classmember.cm_idx;
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00005410 else
5411 {
5412 idx = iptr->isn_arg.classmember.cm_idx;
5413 // convert the interface index to the object index
5414 idx = object_index_from_itf_index(
Ernie Rael18143d32023-09-04 22:30:41 +02005415 iptr->isn_arg.classmember.cm_class,
Yegappan Lakshmanan5a05d372023-09-29 19:43:11 +02005416 FALSE, idx, obj->obj_class);
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00005417 }
5418
Ernie Rael18143d32023-09-04 22:30:41 +02005419 // The members are located right after the object struct.
Yegappan Lakshmanan5a05d372023-09-29 19:43:11 +02005420 typval_T *mtv = ((typval_T *)(obj + 1)) + idx;
Bram Moolenaar590162c2022-12-24 21:24:06 +00005421 copy_tv(mtv, tv);
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005422
5423 // Unreference the object after getting the member, it may
5424 // be freed.
5425 object_unref(obj);
5426 }
5427 break;
5428
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005429 case ISN_STORE_THIS:
5430 {
5431 int idx = iptr->isn_arg.number;
5432 object_T *obj = STACK_TV_VAR(0)->vval.v_object;
5433 // the members are located right after the object struct
5434 typval_T *mtv = ((typval_T *)(obj + 1)) + idx;
5435 clear_tv(mtv);
5436 *mtv = *STACK_TV_BOT(-1);
5437 --ectx->ec_stack.ga_len;
5438 }
5439 break;
5440
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005441 case ISN_CLEARDICT:
5442 dict_stack_drop();
5443 break;
5444
5445 case ISN_USEDICT:
5446 {
5447 typval_T *dict_tv = dict_stack_get_tv();
5448
5449 // Turn "dict.Func" into a partial for "Func" bound to
5450 // "dict". Don't do this when "Func" is already a partial
5451 // that was bound explicitly (pt_auto is FALSE).
5452 tv = STACK_TV_BOT(-1);
5453 if (dict_tv != NULL
5454 && dict_tv->v_type == VAR_DICT
5455 && dict_tv->vval.v_dict != NULL
5456 && (tv->v_type == VAR_FUNC
5457 || (tv->v_type == VAR_PARTIAL
5458 && (tv->vval.v_partial->pt_auto
5459 || tv->vval.v_partial->pt_dict == NULL))))
5460 dict_tv->vval.v_dict =
5461 make_partial(dict_tv->vval.v_dict, tv);
5462 dict_stack_drop();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005463 }
5464 break;
5465
5466 case ISN_NEGATENR:
5467 tv = STACK_TV_BOT(-1);
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00005468 // CHECKTYPE should have checked the variable type
Bram Moolenaarc58164c2020-03-29 18:40:30 +02005469 if (tv->v_type == VAR_FLOAT)
5470 tv->vval.v_float = -tv->vval.v_float;
5471 else
Bram Moolenaarc58164c2020-03-29 18:40:30 +02005472 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005473 break;
5474
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005475 case ISN_CHECKTYPE:
5476 {
5477 checktype_T *ct = &iptr->isn_arg.type;
Bram Moolenaarb1040dc2022-05-18 11:00:48 +01005478 int r;
LemonBoyc5d27442023-08-19 13:02:35 +02005479 where_T where = WHERE_INIT;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005480
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01005481 tv = STACK_TV_BOT((int)ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02005482 SOURCING_LNUM = iptr->isn_lnum;
LemonBoyc5d27442023-08-19 13:02:35 +02005483 if (ct->ct_arg_idx > 0)
5484 {
5485 where.wt_index = ct->ct_arg_idx;
5486 where.wt_kind = ct->ct_is_var ? WT_VARIABLE : WT_ARGUMENT;
LemonBoyc5d27442023-08-19 13:02:35 +02005487 }
LemonBoyf244b2f2023-08-19 16:02:04 +02005488 where.wt_func_name = ectx->ec_where.wt_func_name;
LemonBoyc5d27442023-08-19 13:02:35 +02005489 r = check_typval_type(ct->ct_type, tv, where);
Bram Moolenaarb1040dc2022-05-18 11:00:48 +01005490 if (r == FAIL)
5491 goto on_error;
Bram Moolenaar5e654232020-09-16 15:22:00 +02005492
5493 // number 0 is FALSE, number 1 is TRUE
5494 if (tv->v_type == VAR_NUMBER
5495 && ct->ct_type->tt_type == VAR_BOOL
5496 && (tv->vval.v_number == 0
5497 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005498 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02005499 tv->v_type = VAR_BOOL;
5500 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02005501 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005502 }
5503 }
5504 break;
5505
Bram Moolenaar9af78762020-06-16 11:34:42 +02005506 case ISN_CHECKLEN:
5507 {
5508 int min_len = iptr->isn_arg.checklen.cl_min_len;
5509 list_T *list = NULL;
5510
5511 tv = STACK_TV_BOT(-1);
5512 if (tv->v_type == VAR_LIST)
5513 list = tv->vval.v_list;
5514 if (list == NULL || list->lv_len < min_len
5515 || (list->lv_len > min_len
5516 && !iptr->isn_arg.checklen.cl_more_OK))
5517 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005518 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005519 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02005520 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02005521 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02005522 }
5523 }
5524 break;
5525
Bram Moolenaaraa210a32021-01-02 15:41:03 +01005526 case ISN_SETTYPE:
Bram Moolenaar381692b2022-02-02 20:01:27 +00005527 set_tv_type(STACK_TV_BOT(-1), iptr->isn_arg.type.ct_type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01005528 break;
5529
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005530 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005531 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005532 {
5533 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005534 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005535
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005536 if (iptr->isn_type == ISN_2BOOL)
5537 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005538 tv = STACK_TV_BOT(iptr->isn_arg.tobool.offset);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005539 n = tv2bool(tv);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005540 if (iptr->isn_arg.tobool.invert)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005541 n = !n;
5542 }
5543 else
5544 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005545 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005546 SOURCING_LNUM = iptr->isn_lnum;
5547 n = tv_get_bool_chk(tv, &error);
5548 if (error)
5549 goto on_error;
5550 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005551 clear_tv(tv);
5552 tv->v_type = VAR_BOOL;
5553 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
5554 }
5555 break;
5556
5557 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02005558 case ISN_2STRING_ANY:
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01005559 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005560 if (do_2string(STACK_TV_BOT(iptr->isn_arg.tostring.offset),
5561 iptr->isn_type == ISN_2STRING_ANY,
5562 iptr->isn_arg.tostring.tolerant) == FAIL)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01005563 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005564 break;
5565
Bram Moolenaar08597872020-12-10 19:43:40 +01005566 case ISN_RANGE:
5567 {
5568 exarg_T ea;
5569 char *errormsg;
5570
Bram Moolenaarece0b872021-01-08 20:40:45 +01005571 ea.line2 = 0;
Bram Moolenaard1510ee2021-01-04 16:15:58 +01005572 ea.addr_count = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01005573 ea.addr_type = ADDR_LINES;
5574 ea.cmd = iptr->isn_arg.string;
Bram Moolenaarece0b872021-01-08 20:40:45 +01005575 ea.skip = FALSE;
Bram Moolenaar08597872020-12-10 19:43:40 +01005576 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaarece0b872021-01-08 20:40:45 +01005577 goto on_error;
Bram Moolenaara28639e2021-01-19 22:48:09 +01005578
Bram Moolenaar35578162021-08-02 19:10:38 +02005579 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005580 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005581 ++ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01005582 tv = STACK_TV_BOT(-1);
5583 tv->v_type = VAR_NUMBER;
5584 tv->v_lock = 0;
Bram Moolenaar06651632022-04-27 17:54:25 +01005585 tv->vval.v_number = ea.line2;
Bram Moolenaar08597872020-12-10 19:43:40 +01005586 }
5587 break;
5588
Bram Moolenaarc3516f72020-09-08 22:45:35 +02005589 case ISN_PUT:
5590 {
5591 int regname = iptr->isn_arg.put.put_regname;
5592 linenr_T lnum = iptr->isn_arg.put.put_lnum;
5593 char_u *expr = NULL;
5594 int dir = FORWARD;
5595
Bram Moolenaar08597872020-12-10 19:43:40 +01005596 if (lnum < -2)
5597 {
5598 // line number was put on the stack by ISN_RANGE
5599 tv = STACK_TV_BOT(-1);
5600 curwin->w_cursor.lnum = tv->vval.v_number;
5601 if (lnum == LNUM_VARIABLE_RANGE_ABOVE)
5602 dir = BACKWARD;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005603 --ectx->ec_stack.ga_len;
Bram Moolenaar08597872020-12-10 19:43:40 +01005604 }
5605 else if (lnum == -2)
Bram Moolenaarc3516f72020-09-08 22:45:35 +02005606 // :put! above cursor
5607 dir = BACKWARD;
5608 else if (lnum >= 0)
Bram Moolenaar4e713ba2022-02-07 15:31:37 +00005609 {
5610 curwin->w_cursor.lnum = lnum;
5611 if (lnum == 0)
5612 // check_cursor() below will move to line 1
5613 dir = BACKWARD;
5614 }
Bram Moolenaara28639e2021-01-19 22:48:09 +01005615
5616 if (regname == '=')
5617 {
5618 tv = STACK_TV_BOT(-1);
5619 if (tv->v_type == VAR_STRING)
5620 expr = tv->vval.v_string;
5621 else
5622 {
5623 expr = typval2string(tv, TRUE); // allocates value
5624 clear_tv(tv);
5625 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005626 --ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01005627 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02005628 check_cursor();
5629 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
5630 vim_free(expr);
5631 }
5632 break;
5633
Bram Moolenaar02194d22020-10-24 23:08:38 +02005634 case ISN_CMDMOD:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005635 ectx->ec_funclocal.floc_save_cmdmod = cmdmod;
5636 ectx->ec_funclocal.floc_restore_cmdmod = TRUE;
5637 ectx->ec_funclocal.floc_restore_cmdmod_stacklen =
5638 ectx->ec_stack.ga_len;
Bram Moolenaar02194d22020-10-24 23:08:38 +02005639 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
5640 apply_cmdmod(&cmdmod);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02005641 break;
5642
Bram Moolenaar02194d22020-10-24 23:08:38 +02005643 case ISN_CMDMOD_REV:
5644 // filter regprog is owned by the instruction, don't free it
5645 cmdmod.cmod_filter_regmatch.regprog = NULL;
5646 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005647 cmdmod = ectx->ec_funclocal.floc_save_cmdmod;
5648 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02005649 break;
5650
Bram Moolenaar792f7862020-11-23 08:31:18 +01005651 case ISN_UNPACK:
5652 {
5653 int count = iptr->isn_arg.unpack.unp_count;
5654 int semicolon = iptr->isn_arg.unpack.unp_semicolon;
5655 list_T *l;
5656 listitem_T *li;
5657 int i;
5658
5659 // Check there is a valid list to unpack.
5660 tv = STACK_TV_BOT(-1);
5661 if (tv->v_type != VAR_LIST)
5662 {
5663 SOURCING_LNUM = iptr->isn_lnum;
5664 emsg(_(e_for_argument_must_be_sequence_of_lists));
5665 goto on_error;
5666 }
5667 l = tv->vval.v_list;
5668 if (l == NULL
5669 || l->lv_len < (semicolon ? count - 1 : count))
5670 {
5671 SOURCING_LNUM = iptr->isn_lnum;
5672 emsg(_(e_list_value_does_not_have_enough_items));
5673 goto on_error;
5674 }
5675 else if (!semicolon && l->lv_len > count)
5676 {
5677 SOURCING_LNUM = iptr->isn_lnum;
5678 emsg(_(e_list_value_has_more_items_than_targets));
5679 goto on_error;
5680 }
5681
5682 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar35578162021-08-02 19:10:38 +02005683 if (GA_GROW_FAILS(&ectx->ec_stack, count - 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005684 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005685 ectx->ec_stack.ga_len += count - 1;
Bram Moolenaar792f7862020-11-23 08:31:18 +01005686
5687 // Variable after semicolon gets a list with the remaining
5688 // items.
5689 if (semicolon)
5690 {
5691 list_T *rem_list =
5692 list_alloc_with_items(l->lv_len - count + 1);
5693
5694 if (rem_list == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005695 goto theend;
Bram Moolenaar792f7862020-11-23 08:31:18 +01005696 tv = STACK_TV_BOT(-count);
5697 tv->vval.v_list = rem_list;
5698 ++rem_list->lv_refcount;
5699 tv->v_lock = 0;
5700 li = l->lv_first;
5701 for (i = 0; i < count - 1; ++i)
5702 li = li->li_next;
5703 for (i = 0; li != NULL; ++i)
5704 {
Bram Moolenaar61efa162022-03-18 13:10:48 +00005705 typval_T tvcopy;
5706
5707 copy_tv(&li->li_tv, &tvcopy);
5708 list_set_item(rem_list, i, &tvcopy);
Bram Moolenaar792f7862020-11-23 08:31:18 +01005709 li = li->li_next;
5710 }
5711 --count;
5712 }
5713
5714 // Produce the values in reverse order, first item last.
5715 li = l->lv_first;
5716 for (i = 0; i < count; ++i)
5717 {
5718 tv = STACK_TV_BOT(-i - 1);
5719 copy_tv(&li->li_tv, tv);
5720 li = li->li_next;
5721 }
5722
5723 list_unref(l);
5724 }
5725 break;
5726
Bram Moolenaarb2049902021-01-24 12:53:53 +01005727 case ISN_PROF_START:
5728 case ISN_PROF_END:
5729 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01005730#ifdef FEAT_PROFILE
Bram Moolenaarca16c602022-09-06 18:57:08 +01005731 funccall_T cookie;
5732 ufunc_T *cur_ufunc =
Bram Moolenaarb2049902021-01-24 12:53:53 +01005733 (((dfunc_T *)def_functions.ga_data)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02005734 + ectx->ec_dfunc_idx)->df_ufunc;
Bram Moolenaarb2049902021-01-24 12:53:53 +01005735
Bram Moolenaarca16c602022-09-06 18:57:08 +01005736 cookie.fc_func = cur_ufunc;
Bram Moolenaarb2049902021-01-24 12:53:53 +01005737 if (iptr->isn_type == ISN_PROF_START)
5738 {
5739 func_line_start(&cookie, iptr->isn_lnum);
5740 // if we get here the instruction is executed
5741 func_line_exec(&cookie);
5742 }
5743 else
5744 func_line_end(&cookie);
Bram Moolenaarf002a412021-01-24 13:34:18 +01005745#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01005746 }
5747 break;
5748
Bram Moolenaare99d4222021-06-13 14:01:26 +02005749 case ISN_DEBUG:
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02005750 handle_debug(iptr, ectx);
Bram Moolenaare99d4222021-06-13 14:01:26 +02005751 break;
5752
Bram Moolenaar389df252020-07-09 21:20:47 +02005753 case ISN_SHUFFLE:
5754 {
Bram Moolenaar792f7862020-11-23 08:31:18 +01005755 typval_T tmp_tv;
5756 int item = iptr->isn_arg.shuffle.shfl_item;
5757 int up = iptr->isn_arg.shuffle.shfl_up;
Bram Moolenaar389df252020-07-09 21:20:47 +02005758
5759 tmp_tv = *STACK_TV_BOT(-item);
5760 for ( ; up > 0 && item > 1; --up)
5761 {
5762 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
5763 --item;
5764 }
5765 *STACK_TV_BOT(-item) = tmp_tv;
5766 }
5767 break;
5768
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005769 case ISN_DROP:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005770 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005771 clear_tv(STACK_TV_BOT(0));
LemonBoyc5d27442023-08-19 13:02:35 +02005772 ectx->ec_where = (where_T)WHERE_INIT;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005773 break;
5774 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02005775 continue;
5776
Bram Moolenaard032f342020-07-18 18:13:02 +02005777func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02005778 // Restore previous function. If the frame pointer is where we started
5779 // then there is none and we are done.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005780 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02005781 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02005782
Bram Moolenaar4c137212021-04-19 16:48:48 +02005783 if (func_return(ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02005784 // only fails when out of memory
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005785 goto theend;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02005786 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02005787
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02005788on_error:
Bram Moolenaaraf0df472020-12-02 20:51:22 +01005789 // Jump here for an error that does not require aborting execution.
Bram Moolenaar56602ba2020-12-05 21:22:08 +01005790 // If "emsg_silent" is set then ignore the error, unless it was set
5791 // when calling the function.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005792 if (did_emsg_cumul + did_emsg == ectx->ec_did_emsg_before
Bram Moolenaar56602ba2020-12-05 21:22:08 +01005793 && emsg_silent && did_emsg_def == 0)
Bram Moolenaarf9041332021-01-21 19:41:16 +01005794 {
5795 // If a sequence of instructions causes an error while ":silent!"
5796 // was used, restore the stack length and jump ahead to restoring
5797 // the cmdmod.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005798 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaarf9041332021-01-21 19:41:16 +01005799 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005800 while (ectx->ec_stack.ga_len
5801 > ectx->ec_funclocal.floc_restore_cmdmod_stacklen)
Bram Moolenaarf9041332021-01-21 19:41:16 +01005802 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005803 --ectx->ec_stack.ga_len;
Bram Moolenaarf9041332021-01-21 19:41:16 +01005804 clear_tv(STACK_TV_BOT(0));
5805 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005806 while (ectx->ec_instr[ectx->ec_iidx].isn_type != ISN_CMDMOD_REV)
5807 ++ectx->ec_iidx;
Bram Moolenaarf9041332021-01-21 19:41:16 +01005808 }
Bram Moolenaarcd030c42020-10-30 21:49:40 +01005809 continue;
Bram Moolenaarf9041332021-01-21 19:41:16 +01005810 }
Bram Moolenaaraf0df472020-12-02 20:51:22 +01005811on_fatal_error:
5812 // Jump here for an error that messes up the stack.
Bram Moolenaar171fb922020-10-28 16:54:47 +01005813 // If we are not inside a try-catch started here, abort execution.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005814 if (trylevel <= ectx->ec_trylevel_at_start)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005815 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005816 }
5817
5818done:
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005819 ret = OK;
5820theend:
Bram Moolenaar58779852022-09-06 18:31:14 +01005821 may_invoke_defer_funcs(ectx);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01005822
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005823 dict_stack_clear(dict_stack_len_at_start);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005824 ectx->ec_trylevel_at_start = save_trylevel_at_start;
5825 return ret;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005826}
5827
5828/*
Bram Moolenaar806a2732022-09-04 15:40:36 +01005829 * Execute the instructions from a VAR_INSTR typval and put the result in
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005830 * "rettv".
5831 * Return OK or FAIL.
5832 */
5833 int
5834exe_typval_instr(typval_T *tv, typval_T *rettv)
5835{
5836 ectx_T *ectx = tv->vval.v_instr->instr_ectx;
5837 isn_T *save_instr = ectx->ec_instr;
5838 int save_iidx = ectx->ec_iidx;
5839 int res;
5840
LemonBoyf3b48952022-05-05 13:53:03 +01005841 // Initialize rettv so that it is safe for caller to invoke clear_tv(rettv)
5842 // even when the compilation fails.
5843 rettv->v_type = VAR_UNKNOWN;
5844
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005845 ectx->ec_instr = tv->vval.v_instr->instr_instr;
5846 res = exec_instructions(ectx);
5847 if (res == OK)
5848 {
5849 *rettv = *STACK_TV_BOT(-1);
5850 --ectx->ec_stack.ga_len;
5851 }
5852
5853 ectx->ec_instr = save_instr;
5854 ectx->ec_iidx = save_iidx;
5855
5856 return res;
5857}
5858
5859/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02005860 * Execute the instructions from an ISN_SUBSTITUTE command, which are in
5861 * "substitute_instr".
5862 */
5863 char_u *
5864exe_substitute_instr(void)
5865{
5866 ectx_T *ectx = substitute_instr->subs_ectx;
5867 isn_T *save_instr = ectx->ec_instr;
5868 int save_iidx = ectx->ec_iidx;
5869 char_u *res;
5870
5871 ectx->ec_instr = substitute_instr->subs_instr;
5872 if (exec_instructions(ectx) == OK)
Bram Moolenaar90193e62021-04-04 20:49:50 +02005873 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005874 typval_T *tv = STACK_TV_BOT(-1);
5875
Bram Moolenaar27523602021-06-05 21:36:19 +02005876 res = typval2string(tv, TRUE);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005877 --ectx->ec_stack.ga_len;
5878 clear_tv(tv);
Bram Moolenaar90193e62021-04-04 20:49:50 +02005879 }
5880 else
5881 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005882 substitute_instr->subs_status = FAIL;
5883 res = vim_strsave((char_u *)"");
Bram Moolenaar90193e62021-04-04 20:49:50 +02005884 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005885
Bram Moolenaar4c137212021-04-19 16:48:48 +02005886 ectx->ec_instr = save_instr;
5887 ectx->ec_iidx = save_iidx;
5888
5889 return res;
5890}
5891
5892/*
5893 * Call a "def" function from old Vim script.
5894 * Return OK or FAIL.
5895 */
5896 int
5897call_def_function(
5898 ufunc_T *ufunc,
5899 int argc_arg, // nr of arguments
5900 typval_T *argv, // arguments
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005901 int flags, // DEF_ flags
Bram Moolenaar4c137212021-04-19 16:48:48 +02005902 partial_T *partial, // optional partial for context
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005903 object_T *object, // object, e.g. for this.Func()
Bram Moolenaar58779852022-09-06 18:31:14 +01005904 funccall_T *funccal,
Bram Moolenaar4c137212021-04-19 16:48:48 +02005905 typval_T *rettv) // return value
5906{
5907 ectx_T ectx; // execution context
5908 int argc = argc_arg;
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005909 int partial_argc = partial == NULL
5910 || (flags & DEF_USE_PT_ARGV) == 0
5911 ? 0 : partial->pt_argc;
5912 int total_argc = argc + partial_argc;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005913 typval_T *tv;
5914 int idx;
5915 int ret = FAIL;
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005916 int defcount = ufunc->uf_args.ga_len - total_argc;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005917 sctx_T save_current_sctx = current_sctx;
5918 int did_emsg_before = did_emsg_cumul + did_emsg;
5919 int save_suppress_errthrow = suppress_errthrow;
5920 msglist_T **saved_msg_list = NULL;
5921 msglist_T *private_msg_list = NULL;
5922 int save_emsg_silent_def = emsg_silent_def;
5923 int save_did_emsg_def = did_emsg_def;
5924 int orig_funcdepth;
Bram Moolenaare99d4222021-06-13 14:01:26 +02005925 int orig_nesting_level = ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005926
5927// Get pointer to item in the stack.
5928#undef STACK_TV
5929#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
5930
5931// Get pointer to item at the bottom of the stack, -1 is the bottom.
5932#undef STACK_TV_BOT
5933#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
5934
5935// Get pointer to a local variable on the stack. Negative for arguments.
5936#undef STACK_TV_VAR
5937#define STACK_TV_VAR(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_frame_idx + STACK_FRAME_SIZE + idx)
5938
5939 if (ufunc->uf_def_status == UF_NOT_COMPILED
5940 || ufunc->uf_def_status == UF_COMPILE_ERROR
Bram Moolenaar139575d2022-03-15 19:29:30 +00005941 || (func_needs_compiling(ufunc, get_compile_type(ufunc))
5942 && compile_def_function(ufunc, FALSE,
5943 get_compile_type(ufunc), NULL) == FAIL))
Bram Moolenaar4c137212021-04-19 16:48:48 +02005944 {
5945 if (did_emsg_cumul + did_emsg == did_emsg_before)
5946 semsg(_(e_function_is_not_compiled_str),
5947 printable_func_name(ufunc));
5948 return FAIL;
5949 }
5950
5951 {
5952 // Check the function was really compiled.
5953 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5954 + ufunc->uf_dfunc_idx;
Bram Moolenaarcc341812022-09-19 15:54:34 +01005955 if (dfunc->df_ufunc == NULL)
5956 {
5957 semsg(_(e_function_was_deleted_str), printable_func_name(ufunc));
5958 return FAIL;
5959 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005960 if (INSTRUCTIONS(dfunc) == NULL)
5961 {
5962 iemsg("using call_def_function() on not compiled function");
5963 return FAIL;
5964 }
5965 }
5966
5967 // If depth of calling is getting too high, don't execute the function.
5968 orig_funcdepth = funcdepth_get();
5969 if (funcdepth_increment() == FAIL)
5970 return FAIL;
5971
5972 CLEAR_FIELD(ectx);
5973 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
5974 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
Bram Moolenaar35578162021-08-02 19:10:38 +02005975 if (GA_GROW_FAILS(&ectx.ec_stack, 20))
Bram Moolenaar4c137212021-04-19 16:48:48 +02005976 {
5977 funcdepth_decrement();
5978 return FAIL;
5979 }
5980 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
5981 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
5982 ectx.ec_did_emsg_before = did_emsg_before;
Bram Moolenaare99d4222021-06-13 14:01:26 +02005983 ++ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005984
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005985 idx = total_argc - ufunc->uf_args.ga_len;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005986 if (idx > 0 && ufunc->uf_va_name == NULL)
5987 {
Matvey Tarasovd14bb1a2022-06-29 13:18:27 +01005988 semsg(NGETTEXT(e_one_argument_too_many, e_nr_arguments_too_many,
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005989 idx), idx);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005990 goto failed_early;
5991 }
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005992 idx = total_argc - ufunc->uf_args.ga_len + ufunc->uf_def_args.ga_len;
Bram Moolenaarc6d71532021-06-05 18:49:38 +02005993 if (idx < 0)
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02005994 {
Matvey Tarasovd14bb1a2022-06-29 13:18:27 +01005995 semsg(NGETTEXT(e_one_argument_too_few, e_nr_arguments_too_few,
Bram Moolenaar98aff652022-09-06 21:02:35 +01005996 -idx), -idx);
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02005997 goto failed_early;
5998 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005999
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01006000 // Put values from the partial and arguments on the stack, but no more than
6001 // what the function expects. A lambda can be called with more arguments
6002 // than it uses.
6003 for (idx = 0; idx < total_argc
Bram Moolenaar4c137212021-04-19 16:48:48 +02006004 && (ufunc->uf_va_name != NULL || idx < ufunc->uf_args.ga_len);
6005 ++idx)
6006 {
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01006007 int argv_idx = idx - partial_argc;
6008
6009 tv = idx < partial_argc ? partial->pt_argv + idx : argv + argv_idx;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006010 if (idx >= ufunc->uf_args.ga_len - ufunc->uf_def_args.ga_len
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01006011 && tv->v_type == VAR_SPECIAL
6012 && tv->vval.v_number == VVAL_NONE)
Bram Moolenaar4c137212021-04-19 16:48:48 +02006013 {
6014 // Use the default value.
6015 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
6016 }
6017 else
6018 {
Bram Moolenaar47bba532023-01-20 18:49:46 +00006019 int done = FALSE;
6020 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len)
6021 {
6022 type_T *expected = ufunc->uf_arg_types[idx];
6023 if (expected->tt_type == VAR_FLOAT && tv->v_type == VAR_NUMBER)
6024 {
6025 // When a float is expected and a number was given, convert
6026 // the value.
6027 STACK_TV_BOT(0)->v_type = VAR_FLOAT;
6028 STACK_TV_BOT(0)->v_lock = 0;
6029 STACK_TV_BOT(0)->vval.v_float = tv->vval.v_number;
6030 done = TRUE;
6031 }
6032 else if (check_typval_arg_type(expected, tv,
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01006033 NULL, argv_idx + 1) == FAIL)
Bram Moolenaar47bba532023-01-20 18:49:46 +00006034 goto failed_early;
6035 }
6036 if (!done)
6037 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006038 }
6039 ++ectx.ec_stack.ga_len;
6040 }
6041
6042 // Turn varargs into a list. Empty list if no args.
6043 if (ufunc->uf_va_name != NULL)
6044 {
6045 int vararg_count = argc - ufunc->uf_args.ga_len;
6046
6047 if (vararg_count < 0)
6048 vararg_count = 0;
6049 else
6050 argc -= vararg_count;
6051 if (exe_newlist(vararg_count, &ectx) == FAIL)
6052 goto failed_early;
6053
6054 // Check the type of the list items.
6055 tv = STACK_TV_BOT(-1);
6056 if (ufunc->uf_va_type != NULL
6057 && ufunc->uf_va_type != &t_list_any
6058 && ufunc->uf_va_type->tt_member != &t_any
6059 && tv->vval.v_list != NULL)
6060 {
6061 type_T *expected = ufunc->uf_va_type->tt_member;
6062 listitem_T *li = tv->vval.v_list->lv_first;
6063
6064 for (idx = 0; idx < vararg_count; ++idx)
6065 {
6066 if (check_typval_arg_type(expected, &li->li_tv,
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02006067 NULL, argc + idx + 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02006068 goto failed_early;
6069 li = li->li_next;
6070 }
6071 }
6072
6073 if (defcount > 0)
6074 // Move varargs list to below missing default arguments.
6075 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
6076 --ectx.ec_stack.ga_len;
6077 }
6078
6079 // Make space for omitted arguments, will store default value below.
6080 // Any varargs list goes after them.
6081 if (defcount > 0)
6082 for (idx = 0; idx < defcount; ++idx)
6083 {
6084 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
6085 ++ectx.ec_stack.ga_len;
6086 }
6087 if (ufunc->uf_va_name != NULL)
6088 ++ectx.ec_stack.ga_len;
6089
6090 // Frame pointer points to just after arguments.
6091 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
6092 ectx.ec_initial_frame_idx = ectx.ec_frame_idx;
6093
6094 {
6095 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6096 + ufunc->uf_dfunc_idx;
6097 ufunc_T *base_ufunc = dfunc->df_ufunc;
6098
6099 // "uf_partial" is on the ufunc that "df_ufunc" points to, as is done
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006100 // by copy_lambda_to_global_func().
Bram Moolenaar4c137212021-04-19 16:48:48 +02006101 if (partial != NULL || base_ufunc->uf_partial != NULL)
6102 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006103 ectx.ec_outer_ref = ALLOC_CLEAR_ONE(outer_ref_T);
6104 if (ectx.ec_outer_ref == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02006105 goto failed_early;
6106 if (partial != NULL)
6107 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00006108 outer_T *outer = get_pt_outer(partial);
6109
Bram Moolenaar6d313be2022-09-22 16:36:25 +01006110 if (outer->out_stack == NULL && outer->out_loop_size == 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02006111 {
Bram Moolenaar6d313be2022-09-22 16:36:25 +01006112 // no stack was set
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00006113 if (current_ectx != NULL)
6114 {
6115 if (current_ectx->ec_outer_ref != NULL
6116 && current_ectx->ec_outer_ref->or_outer != NULL)
6117 ectx.ec_outer_ref->or_outer =
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006118 current_ectx->ec_outer_ref->or_outer;
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00006119 }
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01006120 // else: should there be an error here?
Bram Moolenaar4c137212021-04-19 16:48:48 +02006121 }
6122 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006123 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00006124 ectx.ec_outer_ref->or_outer = outer;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006125 ++partial->pt_refcount;
6126 ectx.ec_outer_ref->or_partial = partial;
6127 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006128 }
6129 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006130 {
6131 ectx.ec_outer_ref->or_outer = &base_ufunc->uf_partial->pt_outer;
6132 ++base_ufunc->uf_partial->pt_refcount;
6133 ectx.ec_outer_ref->or_partial = base_ufunc->uf_partial;
6134 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006135 }
6136 }
6137
6138 // dummy frame entries
6139 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
6140 {
6141 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
6142 ++ectx.ec_stack.ga_len;
6143 }
6144
6145 {
6146 // Reserve space for local variables and any closure reference count.
6147 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6148 + ufunc->uf_dfunc_idx;
6149
Bram Moolenaar5cd64792021-12-25 18:23:24 +00006150 // Initialize variables to zero. That avoids having to generate
6151 // initializing instructions for "var nr: number", "var x: any", etc.
Bram Moolenaar4c137212021-04-19 16:48:48 +02006152 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaar5cd64792021-12-25 18:23:24 +00006153 {
6154 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
6155 STACK_TV_VAR(idx)->vval.v_number = 0;
6156 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006157 ectx.ec_stack.ga_len += dfunc->df_varcount;
Bram Moolenaarffdaca92022-12-09 21:41:48 +00006158
6159 if (object != NULL)
6160 {
6161 // the object is always the variable at index zero
6162 tv = STACK_TV_VAR(0);
6163 tv->v_type = VAR_OBJECT;
6164 tv->vval.v_object = object;
6165 }
6166
Bram Moolenaar4c137212021-04-19 16:48:48 +02006167 if (dfunc->df_has_closure)
6168 {
Bram Moolenaarcc341812022-09-19 15:54:34 +01006169 // Initialize the variable that counts how many closures were
6170 // created. This is used in handle_closure_in_use().
Bram Moolenaar4c137212021-04-19 16:48:48 +02006171 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
6172 STACK_TV_VAR(idx)->vval.v_number = 0;
6173 ++ectx.ec_stack.ga_len;
6174 }
6175
6176 ectx.ec_instr = INSTRUCTIONS(dfunc);
6177 }
6178
Bram Moolenaar58779852022-09-06 18:31:14 +01006179 // Store the execution context in funccal, used by invoke_all_defer().
6180 if (funccal != NULL)
6181 funccal->fc_ectx = &ectx;
6182
Bram Moolenaar4c137212021-04-19 16:48:48 +02006183 // Following errors are in the function, not the caller.
6184 // Commands behave like vim9script.
6185 estack_push_ufunc(ufunc, 1);
6186 current_sctx = ufunc->uf_script_ctx;
6187 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
6188
6189 // Use a specific location for storing error messages to be converted to an
6190 // exception.
6191 saved_msg_list = msg_list;
6192 msg_list = &private_msg_list;
6193
6194 // Do turn errors into exceptions.
6195 suppress_errthrow = FALSE;
6196
6197 // Do not delete the function while executing it.
6198 ++ufunc->uf_calls;
6199
6200 // When ":silent!" was used before calling then we still abort the
6201 // function. If ":silent!" is used in the function then we don't.
6202 emsg_silent_def = emsg_silent;
6203 did_emsg_def = 0;
6204
LemonBoyc5d27442023-08-19 13:02:35 +02006205 ectx.ec_where = (where_T)WHERE_INIT;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006206
Bram Moolenaar5800c792022-09-22 17:34:01 +01006207 /*
6208 * Execute the instructions until done.
6209 */
Bram Moolenaar4c137212021-04-19 16:48:48 +02006210 ret = exec_instructions(&ectx);
6211 if (ret == OK)
6212 {
6213 // function finished, get result from the stack.
6214 if (ufunc->uf_ret_type == &t_void)
6215 {
6216 rettv->v_type = VAR_VOID;
6217 }
6218 else
6219 {
6220 tv = STACK_TV_BOT(-1);
6221 *rettv = *tv;
6222 tv->v_type = VAR_UNKNOWN;
6223 }
6224 }
6225
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01006226 // When failed need to unwind the call stack.
Bram Moolenaar58779852022-09-06 18:31:14 +01006227 unwind_def_callstack(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02006228
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02006229 // Deal with any remaining closures, they may be in use somewhere.
6230 if (ectx.ec_funcrefs.ga_len > 0)
Bram Moolenaarf112f302020-12-20 17:47:52 +01006231 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02006232 handle_closure_in_use(&ectx, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00006233 ga_clear(&ectx.ec_funcrefs);
Bram Moolenaarf112f302020-12-20 17:47:52 +01006234 }
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02006235
Bram Moolenaaree8580e2020-08-28 17:19:07 +02006236 estack_pop();
6237 current_sctx = save_current_sctx;
6238
Bram Moolenaar2336c372021-12-06 15:06:54 +00006239 if (--ufunc->uf_calls <= 0 && ufunc->uf_refcount <= 0)
6240 // Function was unreferenced while being used, free it now.
6241 func_clear_free(ufunc, FALSE);
Bram Moolenaarc970e422021-03-17 15:03:04 +01006242
Bram Moolenaar352134b2020-10-17 22:04:08 +02006243 if (*msg_list != NULL && saved_msg_list != NULL)
6244 {
6245 msglist_T **plist = saved_msg_list;
6246
6247 // Append entries from the current msg_list (uncaught exceptions) to
6248 // the saved msg_list.
6249 while (*plist != NULL)
6250 plist = &(*plist)->next;
6251
6252 *plist = *msg_list;
6253 }
6254 msg_list = saved_msg_list;
6255
Bram Moolenaar4c137212021-04-19 16:48:48 +02006256 if (ectx.ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar02194d22020-10-24 23:08:38 +02006257 {
6258 cmdmod.cmod_filter_regmatch.regprog = NULL;
6259 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006260 cmdmod = ectx.ec_funclocal.floc_save_cmdmod;
Bram Moolenaar02194d22020-10-24 23:08:38 +02006261 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +01006262 emsg_silent_def = save_emsg_silent_def;
6263 did_emsg_def += save_did_emsg_def;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02006264
Bram Moolenaaree8580e2020-08-28 17:19:07 +02006265failed_early:
Bram Moolenaar0d1f55c2022-04-05 17:30:29 +01006266 // Free all arguments and local variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006267 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
Bram Moolenaar1936c762022-09-28 15:19:10 +01006268 {
6269 tv = STACK_TV(idx);
6270 if (tv->v_type != VAR_NUMBER && tv->v_type != VAR_UNKNOWN)
6271 clear_tv(tv);
6272 }
Bram Moolenaare99d4222021-06-13 14:01:26 +02006273 ex_nesting_level = orig_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02006274
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006275 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01006276 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006277 if (ectx.ec_outer_ref != NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +01006278 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006279 if (ectx.ec_outer_ref->or_outer_allocated)
6280 vim_free(ectx.ec_outer_ref->or_outer);
6281 partial_unref(ectx.ec_outer_ref->or_partial);
6282 vim_free(ectx.ec_outer_ref);
Bram Moolenaar0186e582021-01-10 18:33:11 +01006283 }
6284
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02006285 // Not sure if this is necessary.
6286 suppress_errthrow = save_suppress_errthrow;
6287
Bram Moolenaarb5841b92021-07-15 18:09:53 +02006288 if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before
6289 && !need_rethrow)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006290 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02006291 printable_func_name(ufunc));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01006292 funcdepth_restore(orig_funcdepth);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006293 return ret;
6294}
6295
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006296/*
Bram Moolenaar58779852022-09-06 18:31:14 +01006297 * Called when a def function has finished (possibly failed).
6298 * Invoke all the function returns to clean up and invoke deferred functions,
6299 * except the toplevel one.
6300 */
6301 void
6302unwind_def_callstack(ectx_T *ectx)
6303{
6304 while (ectx->ec_frame_idx != ectx->ec_initial_frame_idx)
6305 func_return(ectx);
6306}
6307
6308/*
dundargocc57b5bc2022-11-02 13:30:51 +00006309 * Invoke any deferred functions for the top function in "ectx".
Bram Moolenaar58779852022-09-06 18:31:14 +01006310 */
6311 void
6312may_invoke_defer_funcs(ectx_T *ectx)
6313{
6314 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
6315
6316 if (dfunc->df_defer_var_idx > 0)
6317 invoke_defer_funcs(ectx);
6318}
6319
6320/*
Bram Moolenaarcc341812022-09-19 15:54:34 +01006321 * Return loopvarinfo in a printable form in allocated memory.
6322 */
6323 static char_u *
6324printable_loopvarinfo(loopvarinfo_T *lvi)
6325{
6326 garray_T ga;
6327 int depth;
6328
6329 ga_init2(&ga, 1, 100);
6330 for (depth = 0; depth < lvi->lvi_depth; ++depth)
6331 {
6332 if (ga_grow(&ga, 50) == FAIL)
6333 break;
6334 if (lvi->lvi_loop[depth].var_idx == 0)
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +01006335 STRCPY((char *)ga.ga_data + ga.ga_len, " -");
Bram Moolenaarcc341812022-09-19 15:54:34 +01006336 else
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +01006337 vim_snprintf((char *)ga.ga_data + ga.ga_len, 50, " $%d-$%d",
Bram Moolenaarcc341812022-09-19 15:54:34 +01006338 lvi->lvi_loop[depth].var_idx,
6339 lvi->lvi_loop[depth].var_idx
6340 + lvi->lvi_loop[depth].var_count - 1);
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +01006341 ga.ga_len = (int)STRLEN(ga.ga_data);
Bram Moolenaarcc341812022-09-19 15:54:34 +01006342 }
6343 return ga.ga_data;
6344}
6345
6346/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02006347 * List instructions "instr" up to "instr_count" or until ISN_FINISH.
6348 * "ufunc" has the source lines, NULL for the instructions of ISN_SUBSTITUTE.
6349 * "pfx" is prefixed to every line.
6350 */
6351 static void
6352list_instructions(char *pfx, isn_T *instr, int instr_count, ufunc_T *ufunc)
6353{
6354 int line_idx = 0;
6355 int prev_current = 0;
6356 int current;
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02006357 int def_arg_idx = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006358
6359 for (current = 0; current < instr_count; ++current)
6360 {
6361 isn_T *iptr = &instr[current];
6362 char *line;
6363
6364 if (ufunc != NULL)
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02006365 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02006366 while (line_idx < iptr->isn_lnum
6367 && line_idx < ufunc->uf_lines.ga_len)
6368 {
6369 if (current > prev_current)
6370 {
6371 msg_puts("\n\n");
6372 prev_current = current;
6373 }
6374 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
6375 if (line != NULL)
6376 msg(line);
6377 }
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02006378 if (iptr->isn_type == ISN_JUMP_IF_ARG_SET)
6379 {
6380 int first_def_arg = ufunc->uf_args.ga_len
6381 - ufunc->uf_def_args.ga_len;
6382
6383 if (def_arg_idx > 0)
6384 msg_puts("\n\n");
6385 msg_start();
6386 msg_puts(" ");
6387 msg_puts(((char **)(ufunc->uf_args.ga_data))[
6388 first_def_arg + def_arg_idx]);
6389 msg_puts(" = ");
6390 msg_puts(((char **)(ufunc->uf_def_args.ga_data))[def_arg_idx++]);
6391 msg_clr_eos();
6392 msg_end();
6393 }
6394 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006395
6396 switch (iptr->isn_type)
6397 {
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006398 case ISN_CONSTRUCT:
6399 smsg("%s%4d NEW %s size %d", pfx, current,
6400 iptr->isn_arg.construct.construct_class->class_name,
6401 (int)iptr->isn_arg.construct.construct_size);
6402 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006403 case ISN_EXEC:
6404 smsg("%s%4d EXEC %s", pfx, current, iptr->isn_arg.string);
6405 break;
Bram Moolenaar20677332021-06-06 17:02:53 +02006406 case ISN_EXEC_SPLIT:
6407 smsg("%s%4d EXEC_SPLIT %s", pfx, current, iptr->isn_arg.string);
6408 break;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00006409 case ISN_EXECRANGE:
6410 smsg("%s%4d EXECRANGE %s", pfx, current, iptr->isn_arg.string);
6411 break;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02006412 case ISN_LEGACY_EVAL:
6413 smsg("%s%4d EVAL legacy %s", pfx, current,
6414 iptr->isn_arg.string);
6415 break;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02006416 case ISN_REDIRSTART:
6417 smsg("%s%4d REDIR", pfx, current);
6418 break;
6419 case ISN_REDIREND:
6420 smsg("%s%4d REDIR END%s", pfx, current,
6421 iptr->isn_arg.number ? " append" : "");
6422 break;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006423 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02006424#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006425 smsg("%s%4d CEXPR pre %s", pfx, current,
6426 cexpr_get_auname(iptr->isn_arg.number));
Bram Moolenaarb7c97812021-05-05 22:51:39 +02006427#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006428 break;
6429 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02006430#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006431 {
6432 cexprref_T *cer = iptr->isn_arg.cexpr.cexpr_ref;
6433
6434 smsg("%s%4d CEXPR core %s%s \"%s\"", pfx, current,
6435 cexpr_get_auname(cer->cer_cmdidx),
6436 cer->cer_forceit ? "!" : "",
6437 cer->cer_cmdline);
6438 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02006439#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006440 break;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006441 case ISN_INSTR:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006442 smsg("%s%4d INSTR", pfx, current);
6443 list_instructions(" ", iptr->isn_arg.instr, INT_MAX, NULL);
6444 msg(" -------------");
6445 break;
6446 case ISN_SOURCE:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006447 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006448 scriptitem_T *si = SCRIPT_ITEM(iptr->isn_arg.number);
6449
6450 smsg("%s%4d SOURCE %s", pfx, current, si->sn_name);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006451 }
6452 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006453 case ISN_SUBSTITUTE:
6454 {
6455 subs_T *subs = &iptr->isn_arg.subs;
6456
6457 smsg("%s%4d SUBSTITUTE %s", pfx, current, subs->subs_cmd);
6458 list_instructions(" ", subs->subs_instr, INT_MAX, NULL);
6459 msg(" -------------");
6460 }
6461 break;
6462 case ISN_EXECCONCAT:
6463 smsg("%s%4d EXECCONCAT %lld", pfx, current,
6464 (varnumber_T)iptr->isn_arg.number);
6465 break;
6466 case ISN_ECHO:
6467 {
6468 echo_T *echo = &iptr->isn_arg.echo;
6469
6470 smsg("%s%4d %s %d", pfx, current,
6471 echo->echo_with_white ? "ECHO" : "ECHON",
6472 echo->echo_count);
6473 }
6474 break;
6475 case ISN_EXECUTE:
6476 smsg("%s%4d EXECUTE %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02006477 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006478 break;
6479 case ISN_ECHOMSG:
6480 smsg("%s%4d ECHOMSG %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02006481 (varnumber_T)(iptr->isn_arg.number));
6482 break;
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01006483 case ISN_ECHOWINDOW:
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01006484 if (iptr->isn_arg.echowin.ewin_time > 0)
6485 smsg("%s%4d ECHOWINDOW %d (%ld sec)", pfx, current,
6486 iptr->isn_arg.echowin.ewin_count,
6487 iptr->isn_arg.echowin.ewin_time);
6488 else
6489 smsg("%s%4d ECHOWINDOW %d", pfx, current,
6490 iptr->isn_arg.echowin.ewin_count);
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01006491 break;
Bram Moolenaar7de62622021-08-07 15:05:47 +02006492 case ISN_ECHOCONSOLE:
6493 smsg("%s%4d ECHOCONSOLE %lld", pfx, current,
6494 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006495 break;
6496 case ISN_ECHOERR:
6497 smsg("%s%4d ECHOERR %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02006498 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006499 break;
6500 case ISN_LOAD:
6501 {
6502 if (iptr->isn_arg.number < 0)
6503 smsg("%s%4d LOAD arg[%lld]", pfx, current,
6504 (varnumber_T)(iptr->isn_arg.number
6505 + STACK_FRAME_SIZE));
6506 else
6507 smsg("%s%4d LOAD $%lld", pfx, current,
6508 (varnumber_T)(iptr->isn_arg.number));
6509 }
6510 break;
6511 case ISN_LOADOUTER:
6512 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006513 isn_outer_T *outer = &iptr->isn_arg.outer;
6514
6515 if (outer->outer_idx < 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02006516 smsg("%s%4d LOADOUTER level %d arg[%d]", pfx, current,
Bram Moolenaarcc341812022-09-19 15:54:34 +01006517 outer->outer_depth,
6518 outer->outer_idx + STACK_FRAME_SIZE);
6519 else if (outer->outer_depth < 0)
6520 smsg("%s%4d LOADOUTER $%d in loop level %d",
6521 pfx, current,
6522 outer->outer_idx,
6523 -outer->outer_depth);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006524 else
6525 smsg("%s%4d LOADOUTER level %d $%d", pfx, current,
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006526 outer->outer_depth,
6527 outer->outer_idx);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006528 }
6529 break;
6530 case ISN_LOADV:
6531 smsg("%s%4d LOADV v:%s", pfx, current,
6532 get_vim_var_name(iptr->isn_arg.number));
6533 break;
6534 case ISN_LOADSCRIPT:
6535 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006536 scriptref_T *sref = iptr->isn_arg.script.scriptref;
6537 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
6538 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006539
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006540 sv = get_script_svar(sref, -1);
6541 if (sv == NULL)
6542 smsg("%s%4d LOADSCRIPT [deleted] from %s",
6543 pfx, current, si->sn_name);
6544 else
6545 smsg("%s%4d LOADSCRIPT %s-%d from %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02006546 sv->sv_name,
6547 sref->sref_idx,
6548 si->sn_name);
6549 }
6550 break;
6551 case ISN_LOADS:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006552 case ISN_LOADEXPORT:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006553 {
6554 scriptitem_T *si = SCRIPT_ITEM(
6555 iptr->isn_arg.loadstore.ls_sid);
6556
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006557 smsg("%s%4d %s s:%s from %s", pfx, current,
6558 iptr->isn_type == ISN_LOADS ? "LOADS"
6559 : "LOADEXPORT",
6560 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006561 }
6562 break;
6563 case ISN_LOADAUTO:
6564 smsg("%s%4d LOADAUTO %s", pfx, current, iptr->isn_arg.string);
6565 break;
6566 case ISN_LOADG:
6567 smsg("%s%4d LOADG g:%s", pfx, current, iptr->isn_arg.string);
6568 break;
6569 case ISN_LOADB:
6570 smsg("%s%4d LOADB b:%s", pfx, current, iptr->isn_arg.string);
6571 break;
6572 case ISN_LOADW:
6573 smsg("%s%4d LOADW w:%s", pfx, current, iptr->isn_arg.string);
6574 break;
6575 case ISN_LOADT:
6576 smsg("%s%4d LOADT t:%s", pfx, current, iptr->isn_arg.string);
6577 break;
6578 case ISN_LOADGDICT:
6579 smsg("%s%4d LOAD g:", pfx, current);
6580 break;
6581 case ISN_LOADBDICT:
6582 smsg("%s%4d LOAD b:", pfx, current);
6583 break;
6584 case ISN_LOADWDICT:
6585 smsg("%s%4d LOAD w:", pfx, current);
6586 break;
6587 case ISN_LOADTDICT:
6588 smsg("%s%4d LOAD t:", pfx, current);
6589 break;
6590 case ISN_LOADOPT:
6591 smsg("%s%4d LOADOPT %s", pfx, current, iptr->isn_arg.string);
6592 break;
6593 case ISN_LOADENV:
6594 smsg("%s%4d LOADENV %s", pfx, current, iptr->isn_arg.string);
6595 break;
6596 case ISN_LOADREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006597 smsg("%s%4d LOADREG @%c", pfx, current,
6598 (int)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006599 break;
6600
6601 case ISN_STORE:
6602 if (iptr->isn_arg.number < 0)
6603 smsg("%s%4d STORE arg[%lld]", pfx, current,
6604 iptr->isn_arg.number + STACK_FRAME_SIZE);
6605 else
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006606 smsg("%s%4d STORE $%lld", pfx, current,
6607 iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006608 break;
6609 case ISN_STOREOUTER:
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006610 {
6611 isn_outer_T *outer = &iptr->isn_arg.outer;
6612
6613 if (outer->outer_depth == OUTER_LOOP_DEPTH)
6614 smsg("%s%4d STOREOUTER level 1 $%d in loop",
6615 pfx, current, outer->outer_idx);
6616 else
6617 smsg("%s%4d STOREOUTER level %d $%d", pfx, current,
6618 outer->outer_depth, outer->outer_idx);
6619 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006620 break;
6621 case ISN_STOREV:
6622 smsg("%s%4d STOREV v:%s", pfx, current,
6623 get_vim_var_name(iptr->isn_arg.number));
6624 break;
6625 case ISN_STOREAUTO:
6626 smsg("%s%4d STOREAUTO %s", pfx, current, iptr->isn_arg.string);
6627 break;
6628 case ISN_STOREG:
6629 smsg("%s%4d STOREG %s", pfx, current, iptr->isn_arg.string);
6630 break;
6631 case ISN_STOREB:
6632 smsg("%s%4d STOREB %s", pfx, current, iptr->isn_arg.string);
6633 break;
6634 case ISN_STOREW:
6635 smsg("%s%4d STOREW %s", pfx, current, iptr->isn_arg.string);
6636 break;
6637 case ISN_STORET:
6638 smsg("%s%4d STORET %s", pfx, current, iptr->isn_arg.string);
6639 break;
6640 case ISN_STORES:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006641 case ISN_STOREEXPORT:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006642 {
6643 scriptitem_T *si = SCRIPT_ITEM(
6644 iptr->isn_arg.loadstore.ls_sid);
6645
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006646 smsg("%s%4d %s %s in %s", pfx, current,
6647 iptr->isn_type == ISN_STORES
6648 ? "STORES" : "STOREEXPORT",
Bram Moolenaar4c137212021-04-19 16:48:48 +02006649 iptr->isn_arg.loadstore.ls_name, si->sn_name);
6650 }
6651 break;
6652 case ISN_STORESCRIPT:
6653 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006654 scriptref_T *sref = iptr->isn_arg.script.scriptref;
6655 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
6656 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006657
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006658 sv = get_script_svar(sref, -1);
6659 if (sv == NULL)
6660 smsg("%s%4d STORESCRIPT [deleted] in %s",
6661 pfx, current, si->sn_name);
6662 else
6663 smsg("%s%4d STORESCRIPT %s-%d in %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02006664 sv->sv_name,
6665 sref->sref_idx,
6666 si->sn_name);
6667 }
6668 break;
6669 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00006670 case ISN_STOREFUNCOPT:
6671 smsg("%s%4d %s &%s", pfx, current,
6672 iptr->isn_type == ISN_STOREOPT ? "STOREOPT" : "STOREFUNCOPT",
Bram Moolenaar4c137212021-04-19 16:48:48 +02006673 iptr->isn_arg.storeopt.so_name);
6674 break;
6675 case ISN_STOREENV:
6676 smsg("%s%4d STOREENV $%s", pfx, current, iptr->isn_arg.string);
6677 break;
6678 case ISN_STOREREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006679 smsg("%s%4d STOREREG @%c", pfx, current,
6680 (int)iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006681 break;
6682 case ISN_STORENR:
6683 smsg("%s%4d STORE %lld in $%d", pfx, current,
6684 iptr->isn_arg.storenr.stnr_val,
6685 iptr->isn_arg.storenr.stnr_idx);
6686 break;
6687
6688 case ISN_STOREINDEX:
6689 smsg("%s%4d STOREINDEX %s", pfx, current,
Bram Moolenaarf7d1c6e2023-01-16 20:47:57 +00006690 vartype_name(iptr->isn_arg.storeindex.si_vartype));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006691 break;
6692
6693 case ISN_STORERANGE:
6694 smsg("%s%4d STORERANGE", pfx, current);
6695 break;
6696
Bram Moolenaard505d172022-12-18 21:42:55 +00006697 case ISN_LOAD_CLASSMEMBER:
6698 case ISN_STORE_CLASSMEMBER:
6699 {
6700 class_T *cl = iptr->isn_arg.classmember.cm_class;
6701 int idx = iptr->isn_arg.classmember.cm_idx;
6702 ocmember_T *ocm = &cl->class_class_members[idx];
6703 smsg("%s%4d %s CLASSMEMBER %s.%s", pfx, current,
6704 iptr->isn_type == ISN_LOAD_CLASSMEMBER
6705 ? "LOAD" : "STORE",
6706 cl->class_name, ocm->ocm_name);
6707 }
6708 break;
6709
Bram Moolenaar4c137212021-04-19 16:48:48 +02006710 // constants
6711 case ISN_PUSHNR:
6712 smsg("%s%4d PUSHNR %lld", pfx, current,
6713 (varnumber_T)(iptr->isn_arg.number));
6714 break;
6715 case ISN_PUSHBOOL:
6716 case ISN_PUSHSPEC:
6717 smsg("%s%4d PUSH %s", pfx, current,
6718 get_var_special_name(iptr->isn_arg.number));
6719 break;
6720 case ISN_PUSHF:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006721 smsg("%s%4d PUSHF %g", pfx, current, iptr->isn_arg.fnumber);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006722 break;
6723 case ISN_PUSHS:
6724 smsg("%s%4d PUSHS \"%s\"", pfx, current, iptr->isn_arg.string);
6725 break;
6726 case ISN_PUSHBLOB:
6727 {
6728 char_u *r;
6729 char_u numbuf[NUMBUFLEN];
6730 char_u *tofree;
6731
6732 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
6733 smsg("%s%4d PUSHBLOB %s", pfx, current, r);
6734 vim_free(tofree);
6735 }
6736 break;
6737 case ISN_PUSHFUNC:
6738 {
6739 char *name = (char *)iptr->isn_arg.string;
6740
6741 smsg("%s%4d PUSHFUNC \"%s\"", pfx, current,
6742 name == NULL ? "[none]" : name);
6743 }
6744 break;
6745 case ISN_PUSHCHANNEL:
6746#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar397a87a2022-03-20 21:14:15 +00006747 smsg("%s%4d PUSHCHANNEL 0", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006748#endif
6749 break;
6750 case ISN_PUSHJOB:
6751#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar397a87a2022-03-20 21:14:15 +00006752 smsg("%s%4d PUSHJOB \"no process\"", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006753#endif
6754 break;
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00006755 case ISN_PUSHOBJ:
6756 smsg("%s%4d PUSHOBJ null", pfx, current);
6757 break;
6758 case ISN_PUSHCLASS:
6759 smsg("%s%4d PUSHCLASS %s", pfx, current,
Bram Moolenaar30a84472023-02-27 08:07:14 +00006760 iptr->isn_arg.classarg == NULL ? "null"
6761 : (char *)iptr->isn_arg.classarg->class_name);
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00006762 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006763 case ISN_PUSHEXC:
6764 smsg("%s%4d PUSH v:exception", pfx, current);
6765 break;
Bram Moolenaar06b77222022-01-25 15:51:56 +00006766 case ISN_AUTOLOAD:
6767 smsg("%s%4d AUTOLOAD %s", pfx, current, iptr->isn_arg.string);
6768 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006769 case ISN_UNLET:
6770 smsg("%s%4d UNLET%s %s", pfx, current,
6771 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
6772 iptr->isn_arg.unlet.ul_name);
6773 break;
6774 case ISN_UNLETENV:
6775 smsg("%s%4d UNLETENV%s $%s", pfx, current,
6776 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
6777 iptr->isn_arg.unlet.ul_name);
6778 break;
6779 case ISN_UNLETINDEX:
6780 smsg("%s%4d UNLETINDEX", pfx, current);
6781 break;
6782 case ISN_UNLETRANGE:
6783 smsg("%s%4d UNLETRANGE", pfx, current);
6784 break;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02006785 case ISN_LOCKUNLOCK:
6786 smsg("%s%4d LOCKUNLOCK %s", pfx, current, iptr->isn_arg.string);
6787 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006788 case ISN_LOCKCONST:
6789 smsg("%s%4d LOCKCONST", pfx, current);
6790 break;
6791 case ISN_NEWLIST:
6792 smsg("%s%4d NEWLIST size %lld", pfx, current,
Bram Moolenaar1936c762022-09-28 15:19:10 +01006793 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006794 break;
6795 case ISN_NEWDICT:
6796 smsg("%s%4d NEWDICT size %lld", pfx, current,
Bram Moolenaar1936c762022-09-28 15:19:10 +01006797 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006798 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00006799 case ISN_NEWPARTIAL:
6800 smsg("%s%4d NEWPARTIAL", pfx, current);
6801 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006802
6803 // function call
6804 case ISN_BCALL:
6805 {
6806 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
6807
6808 smsg("%s%4d BCALL %s(argc %d)", pfx, current,
6809 internal_func_name(cbfunc->cbf_idx),
6810 cbfunc->cbf_argcount);
6811 }
6812 break;
6813 case ISN_DCALL:
6814 {
6815 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
6816 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
6817 + cdfunc->cdf_idx;
6818
6819 smsg("%s%4d DCALL %s(argc %d)", pfx, current,
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006820 printable_func_name(df->df_ufunc),
6821 cdfunc->cdf_argcount);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006822 }
6823 break;
Bram Moolenaard0200c82023-01-28 15:19:40 +00006824 case ISN_METHODCALL:
6825 {
6826 cmfunc_T *mfunc = iptr->isn_arg.mfunc;
6827
6828 smsg("%s%4d METHODCALL %s.%s(argc %d)", pfx, current,
6829 mfunc->cmf_itf->class_name,
6830 mfunc->cmf_itf->class_obj_methods[
6831 mfunc->cmf_idx]->uf_name,
6832 mfunc->cmf_argcount);
6833 }
6834 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006835 case ISN_UCALL:
6836 {
6837 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
6838
6839 smsg("%s%4d UCALL %s(argc %d)", pfx, current,
6840 cufunc->cuf_name, cufunc->cuf_argcount);
6841 }
6842 break;
6843 case ISN_PCALL:
6844 {
6845 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
6846
6847 smsg("%s%4d PCALL%s (argc %d)", pfx, current,
6848 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
6849 }
6850 break;
6851 case ISN_PCALL_END:
6852 smsg("%s%4d PCALL end", pfx, current);
6853 break;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006854 case ISN_DEFER:
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00006855 case ISN_DEFEROBJ:
6856 smsg("%s%4d %s %d args", pfx, current,
6857 iptr->isn_type == ISN_DEFER ? "DEFER" : "DEFEROBJ",
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006858 (int)iptr->isn_arg.defer.defer_argcount);
6859 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006860 case ISN_RETURN:
6861 smsg("%s%4d RETURN", pfx, current);
6862 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02006863 case ISN_RETURN_VOID:
6864 smsg("%s%4d RETURN void", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006865 break;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006866 case ISN_RETURN_OBJECT:
6867 smsg("%s%4d RETURN object", pfx, current);
6868 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006869 case ISN_FUNCREF:
6870 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006871 funcref_T *funcref = &iptr->isn_arg.funcref;
6872 funcref_extra_T *extra = funcref->fr_extra;
6873 char_u *name;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006874
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006875 if (extra == NULL || extra->fre_func_name == NULL)
Bram Moolenaar38453522021-11-28 22:00:12 +00006876 {
6877 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
6878 + funcref->fr_dfunc_idx;
6879 name = df->df_ufunc->uf_name;
6880 }
6881 else
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006882 name = extra->fre_func_name;
Bram Moolenaar313e4722023-02-08 20:55:27 +00006883 if (extra != NULL && extra->fre_class != NULL)
6884 {
6885 smsg("%s%4d FUNCREF %s.%s", pfx, current,
6886 extra->fre_class->class_name, name);
6887 }
6888 else if (extra == NULL
6889 || extra->fre_loopvar_info.lvi_depth == 0)
6890 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006891 smsg("%s%4d FUNCREF %s", pfx, current, name);
Bram Moolenaar313e4722023-02-08 20:55:27 +00006892 }
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006893 else
Bram Moolenaarcc341812022-09-19 15:54:34 +01006894 {
6895 char_u *info = printable_loopvarinfo(
6896 &extra->fre_loopvar_info);
6897
6898 smsg("%s%4d FUNCREF %s vars %s", pfx, current,
6899 name, info);
6900 vim_free(info);
6901 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006902 }
6903 break;
6904
6905 case ISN_NEWFUNC:
6906 {
Bram Moolenaarcc341812022-09-19 15:54:34 +01006907 newfuncarg_T *arg = iptr->isn_arg.newfunc.nf_arg;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006908
Bram Moolenaarcc341812022-09-19 15:54:34 +01006909 if (arg->nfa_loopvar_info.lvi_depth == 0)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006910 smsg("%s%4d NEWFUNC %s %s", pfx, current,
6911 arg->nfa_lambda, arg->nfa_global);
6912 else
Bram Moolenaarcc341812022-09-19 15:54:34 +01006913 {
6914 char_u *info = printable_loopvarinfo(
6915 &arg->nfa_loopvar_info);
6916
6917 smsg("%s%4d NEWFUNC %s %s vars %s", pfx, current,
6918 arg->nfa_lambda, arg->nfa_global, info);
6919 vim_free(info);
6920 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006921 }
6922 break;
6923
6924 case ISN_DEF:
6925 {
6926 char_u *name = iptr->isn_arg.string;
6927
6928 smsg("%s%4d DEF %s", pfx, current,
6929 name == NULL ? (char_u *)"" : name);
6930 }
6931 break;
6932
6933 case ISN_JUMP:
6934 {
6935 char *when = "?";
6936
6937 switch (iptr->isn_arg.jump.jump_when)
6938 {
6939 case JUMP_ALWAYS:
6940 when = "JUMP";
6941 break;
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02006942 case JUMP_NEVER:
6943 iemsg("JUMP_NEVER should not be used");
6944 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006945 case JUMP_AND_KEEP_IF_TRUE:
6946 when = "JUMP_AND_KEEP_IF_TRUE";
6947 break;
6948 case JUMP_IF_FALSE:
6949 when = "JUMP_IF_FALSE";
6950 break;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01006951 case JUMP_WHILE_FALSE:
6952 when = "JUMP_WHILE_FALSE"; // unused
6953 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006954 case JUMP_IF_COND_FALSE:
6955 when = "JUMP_IF_COND_FALSE";
6956 break;
6957 case JUMP_IF_COND_TRUE:
6958 when = "JUMP_IF_COND_TRUE";
6959 break;
6960 }
6961 smsg("%s%4d %s -> %d", pfx, current, when,
6962 iptr->isn_arg.jump.jump_where);
6963 }
6964 break;
6965
6966 case ISN_JUMP_IF_ARG_SET:
6967 smsg("%s%4d JUMP_IF_ARG_SET arg[%d] -> %d", pfx, current,
6968 iptr->isn_arg.jumparg.jump_arg_off + STACK_FRAME_SIZE,
6969 iptr->isn_arg.jump.jump_where);
6970 break;
6971
Bram Moolenaar65b0d162022-12-13 18:43:22 +00006972 case ISN_JUMP_IF_ARG_NOT_SET:
6973 smsg("%s%4d JUMP_IF_ARG_NOT_SET arg[%d] -> %d", pfx, current,
6974 iptr->isn_arg.jumparg.jump_arg_off + STACK_FRAME_SIZE,
6975 iptr->isn_arg.jump.jump_where);
6976 break;
6977
Bram Moolenaar4c137212021-04-19 16:48:48 +02006978 case ISN_FOR:
6979 {
6980 forloop_T *forloop = &iptr->isn_arg.forloop;
6981
6982 smsg("%s%4d FOR $%d -> %d", pfx, current,
Bram Moolenaarcc341812022-09-19 15:54:34 +01006983 forloop->for_loop_idx, forloop->for_end);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006984 }
6985 break;
6986
Bram Moolenaarb46c0832022-09-15 17:19:37 +01006987 case ISN_ENDLOOP:
6988 {
6989 endloop_T *endloop = &iptr->isn_arg.endloop;
6990
Bram Moolenaarcc341812022-09-19 15:54:34 +01006991 smsg("%s%4d ENDLOOP ref $%d save $%d-$%d depth %d",
6992 pfx, current,
Bram Moolenaarb46c0832022-09-15 17:19:37 +01006993 endloop->end_funcref_idx,
6994 endloop->end_var_idx,
Bram Moolenaarcc341812022-09-19 15:54:34 +01006995 endloop->end_var_idx + endloop->end_var_count - 1,
6996 endloop->end_depth);
Bram Moolenaarb46c0832022-09-15 17:19:37 +01006997 }
6998 break;
6999
7000 case ISN_WHILE:
7001 {
7002 whileloop_T *whileloop = &iptr->isn_arg.whileloop;
7003
7004 smsg("%s%4d WHILE $%d -> %d", pfx, current,
7005 whileloop->while_funcref_idx,
7006 whileloop->while_end);
7007 }
7008 break;
7009
Bram Moolenaar4c137212021-04-19 16:48:48 +02007010 case ISN_TRY:
7011 {
Bram Moolenaar0d807102021-12-21 09:42:09 +00007012 try_T *try = &iptr->isn_arg.tryref;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007013
7014 if (try->try_ref->try_finally == 0)
7015 smsg("%s%4d TRY catch -> %d, endtry -> %d",
7016 pfx, current,
7017 try->try_ref->try_catch,
7018 try->try_ref->try_endtry);
7019 else
7020 smsg("%s%4d TRY catch -> %d, finally -> %d, endtry -> %d",
7021 pfx, current,
7022 try->try_ref->try_catch,
7023 try->try_ref->try_finally,
7024 try->try_ref->try_endtry);
7025 }
7026 break;
7027 case ISN_CATCH:
Bram Moolenaar4c137212021-04-19 16:48:48 +02007028 smsg("%s%4d CATCH", pfx, current);
7029 break;
7030 case ISN_TRYCONT:
7031 {
7032 trycont_T *trycont = &iptr->isn_arg.trycont;
7033
7034 smsg("%s%4d TRY-CONTINUE %d level%s -> %d", pfx, current,
7035 trycont->tct_levels,
7036 trycont->tct_levels == 1 ? "" : "s",
7037 trycont->tct_where);
7038 }
7039 break;
7040 case ISN_FINALLY:
7041 smsg("%s%4d FINALLY", pfx, current);
7042 break;
7043 case ISN_ENDTRY:
7044 smsg("%s%4d ENDTRY", pfx, current);
7045 break;
7046 case ISN_THROW:
7047 smsg("%s%4d THROW", pfx, current);
7048 break;
7049
7050 // expression operations on number
7051 case ISN_OPNR:
7052 case ISN_OPFLOAT:
7053 case ISN_OPANY:
7054 {
7055 char *what;
7056 char *ins;
7057
7058 switch (iptr->isn_arg.op.op_type)
7059 {
7060 case EXPR_MULT: what = "*"; break;
7061 case EXPR_DIV: what = "/"; break;
7062 case EXPR_REM: what = "%"; break;
7063 case EXPR_SUB: what = "-"; break;
7064 case EXPR_ADD: what = "+"; break;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01007065 case EXPR_LSHIFT: what = "<<"; break;
7066 case EXPR_RSHIFT: what = ">>"; break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007067 default: what = "???"; break;
7068 }
7069 switch (iptr->isn_type)
7070 {
7071 case ISN_OPNR: ins = "OPNR"; break;
7072 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
7073 case ISN_OPANY: ins = "OPANY"; break;
7074 default: ins = "???"; break;
7075 }
7076 smsg("%s%4d %s %s", pfx, current, ins, what);
7077 }
7078 break;
7079
7080 case ISN_COMPAREBOOL:
7081 case ISN_COMPARESPECIAL:
Bram Moolenaar7a222242022-03-01 19:23:24 +00007082 case ISN_COMPARENULL:
Bram Moolenaar4c137212021-04-19 16:48:48 +02007083 case ISN_COMPARENR:
7084 case ISN_COMPAREFLOAT:
7085 case ISN_COMPARESTRING:
7086 case ISN_COMPAREBLOB:
7087 case ISN_COMPARELIST:
7088 case ISN_COMPAREDICT:
7089 case ISN_COMPAREFUNC:
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00007090 case ISN_COMPARECLASS:
7091 case ISN_COMPAREOBJECT:
Bram Moolenaar4c137212021-04-19 16:48:48 +02007092 case ISN_COMPAREANY:
7093 {
7094 char *p;
7095 char buf[10];
7096 char *type;
7097
7098 switch (iptr->isn_arg.op.op_type)
7099 {
7100 case EXPR_EQUAL: p = "=="; break;
7101 case EXPR_NEQUAL: p = "!="; break;
7102 case EXPR_GREATER: p = ">"; break;
7103 case EXPR_GEQUAL: p = ">="; break;
7104 case EXPR_SMALLER: p = "<"; break;
7105 case EXPR_SEQUAL: p = "<="; break;
7106 case EXPR_MATCH: p = "=~"; break;
7107 case EXPR_IS: p = "is"; break;
7108 case EXPR_ISNOT: p = "isnot"; break;
7109 case EXPR_NOMATCH: p = "!~"; break;
7110 default: p = "???"; break;
7111 }
7112 STRCPY(buf, p);
7113 if (iptr->isn_arg.op.op_ic == TRUE)
7114 strcat(buf, "?");
7115 switch(iptr->isn_type)
7116 {
7117 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
7118 case ISN_COMPARESPECIAL:
7119 type = "COMPARESPECIAL"; break;
Bram Moolenaar7a222242022-03-01 19:23:24 +00007120 case ISN_COMPARENULL: type = "COMPARENULL"; break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007121 case ISN_COMPARENR: type = "COMPARENR"; break;
7122 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
7123 case ISN_COMPARESTRING:
7124 type = "COMPARESTRING"; break;
7125 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
7126 case ISN_COMPARELIST: type = "COMPARELIST"; break;
7127 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
7128 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00007129 case ISN_COMPARECLASS: type = "COMPARECLASS"; break;
7130 case ISN_COMPAREOBJECT:
7131 type = "COMPAREOBJECT"; break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007132 case ISN_COMPAREANY: type = "COMPAREANY"; break;
7133 default: type = "???"; break;
7134 }
7135
7136 smsg("%s%4d %s %s", pfx, current, type, buf);
7137 }
7138 break;
7139
7140 case ISN_ADDLIST: smsg("%s%4d ADDLIST", pfx, current); break;
7141 case ISN_ADDBLOB: smsg("%s%4d ADDBLOB", pfx, current); break;
7142
7143 // expression operations
LemonBoy372bcce2022-04-25 12:43:20 +01007144 case ISN_CONCAT:
7145 smsg("%s%4d CONCAT size %lld", pfx, current,
7146 (varnumber_T)(iptr->isn_arg.number));
7147 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007148 case ISN_STRINDEX: smsg("%s%4d STRINDEX", pfx, current); break;
7149 case ISN_STRSLICE: smsg("%s%4d STRSLICE", pfx, current); break;
7150 case ISN_BLOBINDEX: smsg("%s%4d BLOBINDEX", pfx, current); break;
7151 case ISN_BLOBSLICE: smsg("%s%4d BLOBSLICE", pfx, current); break;
7152 case ISN_LISTAPPEND: smsg("%s%4d LISTAPPEND", pfx, current); break;
7153 case ISN_BLOBAPPEND: smsg("%s%4d BLOBAPPEND", pfx, current); break;
7154 case ISN_LISTINDEX: smsg("%s%4d LISTINDEX", pfx, current); break;
7155 case ISN_LISTSLICE: smsg("%s%4d LISTSLICE", pfx, current); break;
7156 case ISN_ANYINDEX: smsg("%s%4d ANYINDEX", pfx, current); break;
7157 case ISN_ANYSLICE: smsg("%s%4d ANYSLICE", pfx, current); break;
7158 case ISN_SLICE: smsg("%s%4d SLICE %lld",
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02007159 pfx, current, iptr->isn_arg.number); break;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02007160 case ISN_GETITEM: smsg("%s%4d ITEM %lld%s", pfx, current,
7161 iptr->isn_arg.getitem.gi_index,
7162 iptr->isn_arg.getitem.gi_with_op ?
7163 " with op" : ""); break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007164 case ISN_MEMBER: smsg("%s%4d MEMBER", pfx, current); break;
7165 case ISN_STRINGMEMBER: smsg("%s%4d MEMBER %s", pfx, current,
7166 iptr->isn_arg.string); break;
Yegappan Lakshmanan5a05d372023-09-29 19:43:11 +02007167 case ISN_GET_OBJ_MEMBER: smsg("%s%4d OBJ_MEMBER %d", pfx, current,
7168 (int)iptr->isn_arg.classmember.cm_idx);
Ernie Rael00df69e2023-09-05 07:38:09 +02007169 break;
Yegappan Lakshmanan5a05d372023-09-29 19:43:11 +02007170 case ISN_GET_ITF_MEMBER: smsg("%s%4d ITF_MEMBER %d on %s",
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00007171 pfx, current,
7172 (int)iptr->isn_arg.classmember.cm_idx,
Yegappan Lakshmanan5a05d372023-09-29 19:43:11 +02007173 iptr->isn_arg.classmember.cm_class->class_name);
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00007174 break;
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00007175 case ISN_STORE_THIS: smsg("%s%4d STORE_THIS %d", pfx, current,
Bram Moolenaarffdaca92022-12-09 21:41:48 +00007176 (int)iptr->isn_arg.number); break;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02007177 case ISN_CLEARDICT: smsg("%s%4d CLEARDICT", pfx, current); break;
7178 case ISN_USEDICT: smsg("%s%4d USEDICT", pfx, current); break;
7179
Bram Moolenaar4c137212021-04-19 16:48:48 +02007180 case ISN_NEGATENR: smsg("%s%4d NEGATENR", pfx, current); break;
7181
Bram Moolenaar4c137212021-04-19 16:48:48 +02007182 case ISN_CHECKTYPE:
7183 {
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01007184 checktype_T *ct = &iptr->isn_arg.type;
Bram Moolenaarc6951a72022-12-29 20:56:24 +00007185 char *tofree = NULL;
7186 char *typename;
7187
7188 if (ct->ct_type->tt_type == VAR_FLOAT
7189 && (ct->ct_type->tt_flags & TTFLAG_NUMBER_OK))
7190 typename = "float|number";
7191 else
7192 typename = type_name(ct->ct_type, &tofree);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007193
7194 if (ct->ct_arg_idx == 0)
7195 smsg("%s%4d CHECKTYPE %s stack[%d]", pfx, current,
Bram Moolenaarc6951a72022-12-29 20:56:24 +00007196 typename,
Bram Moolenaar4c137212021-04-19 16:48:48 +02007197 (int)ct->ct_off);
7198 else
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01007199 smsg("%s%4d CHECKTYPE %s stack[%d] %s %d",
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02007200 pfx, current,
Bram Moolenaarc6951a72022-12-29 20:56:24 +00007201 typename,
Bram Moolenaar4c137212021-04-19 16:48:48 +02007202 (int)ct->ct_off,
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01007203 ct->ct_is_var ? "var": "arg",
Bram Moolenaar4c137212021-04-19 16:48:48 +02007204 (int)ct->ct_arg_idx);
7205 vim_free(tofree);
7206 break;
7207 }
7208 case ISN_CHECKLEN: smsg("%s%4d CHECKLEN %s%d", pfx, current,
7209 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
7210 iptr->isn_arg.checklen.cl_min_len);
7211 break;
7212 case ISN_SETTYPE:
7213 {
7214 char *tofree;
7215
7216 smsg("%s%4d SETTYPE %s", pfx, current,
7217 type_name(iptr->isn_arg.type.ct_type, &tofree));
7218 vim_free(tofree);
7219 break;
7220 }
7221 case ISN_COND2BOOL: smsg("%s%4d COND2BOOL", pfx, current); break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007222 case ISN_2BOOL: if (iptr->isn_arg.tobool.invert)
7223 smsg("%s%4d INVERT %d (!val)", pfx, current,
7224 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007225 else
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007226 smsg("%s%4d 2BOOL %d (!!val)", pfx, current,
7227 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007228 break;
7229 case ISN_2STRING: smsg("%s%4d 2STRING stack[%lld]", pfx, current,
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007230 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02007231 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007232 case ISN_2STRING_ANY: smsg("%s%4d 2STRING_ANY stack[%lld]",
7233 pfx, current,
7234 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02007235 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007236 case ISN_RANGE: smsg("%s%4d RANGE %s", pfx, current,
7237 iptr->isn_arg.string);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007238 break;
7239 case ISN_PUT:
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01007240 if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE)
Bram Moolenaar4c137212021-04-19 16:48:48 +02007241 smsg("%s%4d PUT %c above 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 if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE)
7244 smsg("%s%4d PUT %c range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007245 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007246 else
7247 smsg("%s%4d PUT %c %ld", pfx, current,
7248 iptr->isn_arg.put.put_regname,
7249 (long)iptr->isn_arg.put.put_lnum);
7250 break;
7251
Bram Moolenaar4c137212021-04-19 16:48:48 +02007252 case ISN_CMDMOD:
7253 {
7254 char_u *buf;
7255 size_t len = produce_cmdmods(
7256 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
7257
7258 buf = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02007259 if (likely(buf != NULL))
Bram Moolenaar4c137212021-04-19 16:48:48 +02007260 {
7261 (void)produce_cmdmods(
7262 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
7263 smsg("%s%4d CMDMOD %s", pfx, current, buf);
7264 vim_free(buf);
7265 }
7266 break;
7267 }
7268 case ISN_CMDMOD_REV: smsg("%s%4d CMDMOD_REV", pfx, current); break;
7269
7270 case ISN_PROF_START:
Bram Moolenaare99d4222021-06-13 14:01:26 +02007271 smsg("%s%4d PROFILE START line %d", pfx, current,
7272 iptr->isn_lnum);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007273 break;
7274
7275 case ISN_PROF_END:
7276 smsg("%s%4d PROFILE END", pfx, current);
7277 break;
7278
Bram Moolenaare99d4222021-06-13 14:01:26 +02007279 case ISN_DEBUG:
Bram Moolenaar8cec9272021-06-23 20:20:53 +02007280 smsg("%s%4d DEBUG line %d-%d varcount %lld", pfx, current,
7281 iptr->isn_arg.debug.dbg_break_lnum + 1,
7282 iptr->isn_lnum,
7283 iptr->isn_arg.debug.dbg_var_names_len);
Bram Moolenaare99d4222021-06-13 14:01:26 +02007284 break;
7285
Bram Moolenaar4c137212021-04-19 16:48:48 +02007286 case ISN_UNPACK: smsg("%s%4d UNPACK %d%s", pfx, current,
7287 iptr->isn_arg.unpack.unp_count,
7288 iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : "");
7289 break;
7290 case ISN_SHUFFLE: smsg("%s%4d SHUFFLE %d up %d", pfx, current,
7291 iptr->isn_arg.shuffle.shfl_item,
7292 iptr->isn_arg.shuffle.shfl_up);
7293 break;
7294 case ISN_DROP: smsg("%s%4d DROP", pfx, current); break;
7295
7296 case ISN_FINISH: // End of list of instructions for ISN_SUBSTITUTE.
7297 return;
7298 }
7299
7300 out_flush(); // output one line at a time
7301 ui_breakcheck();
7302 if (got_int)
7303 break;
7304 }
7305}
7306
7307/*
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02007308 * Handle command line completion for the :disassemble command.
7309 */
7310 void
7311set_context_in_disassemble_cmd(expand_T *xp, char_u *arg)
7312{
7313 char_u *p;
7314
7315 // Default: expand user functions, "debug" and "profile"
7316 xp->xp_context = EXPAND_DISASSEMBLE;
7317 xp->xp_pattern = arg;
7318
7319 // first argument already typed: only user function names
7320 if (*arg != NUL && *(p = skiptowhite(arg)) != NUL)
7321 {
7322 xp->xp_context = EXPAND_USER_FUNC;
7323 xp->xp_pattern = skipwhite(p);
7324 }
7325}
7326
7327/*
7328 * Function given to ExpandGeneric() to obtain the list of :disassemble
7329 * arguments.
7330 */
7331 char_u *
7332get_disassemble_argument(expand_T *xp, int idx)
7333{
7334 if (idx == 0)
7335 return (char_u *)"debug";
7336 if (idx == 1)
7337 return (char_u *)"profile";
7338 return get_user_func_name(xp, idx - 2);
7339}
7340
7341/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01007342 * ":disassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01007343 * We don't really need this at runtime, but we do have tests that require it,
7344 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007345 */
7346 void
7347ex_disassemble(exarg_T *eap)
7348{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01007349 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01007350 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007351 dfunc_T *dfunc;
Bram Moolenaardafef512022-05-21 21:55:55 +01007352 isn_T *instr = NULL; // init to shut up gcc warning
7353 int instr_count = 0; // init to shut up gcc warning
Bram Moolenaar5a01caa2022-05-21 18:56:58 +01007354 compiletype_T compile_type = CT_NONE;
Bram Moolenaare99d4222021-06-13 14:01:26 +02007355
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01007356 ufunc = find_func_by_name(arg, &compile_type);
Bram Moolenaara26b9702020-04-18 19:53:28 +02007357 if (ufunc == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007358 return;
Bram Moolenaare99d4222021-06-13 14:01:26 +02007359 if (func_needs_compiling(ufunc, compile_type)
7360 && compile_def_function(ufunc, FALSE, compile_type, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02007361 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007362 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007363 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007364 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007365 return;
7366 }
Bram Moolenaar6db660b2021-08-01 14:08:54 +02007367 msg((char *)printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007368
7369 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
Bram Moolenaare99d4222021-06-13 14:01:26 +02007370 switch (compile_type)
7371 {
7372 case CT_PROFILE:
Bram Moolenaarf002a412021-01-24 13:34:18 +01007373#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02007374 instr = dfunc->df_instr_prof;
7375 instr_count = dfunc->df_instr_prof_count;
7376 break;
Bram Moolenaarf002a412021-01-24 13:34:18 +01007377#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02007378 // FALLTHROUGH
7379 case CT_NONE:
7380 instr = dfunc->df_instr;
7381 instr_count = dfunc->df_instr_count;
7382 break;
7383 case CT_DEBUG:
7384 instr = dfunc->df_instr_debug;
7385 instr_count = dfunc->df_instr_debug_count;
7386 break;
7387 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007388
Bram Moolenaar4c137212021-04-19 16:48:48 +02007389 list_instructions("", instr, instr_count, ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007390}
7391
7392/*
Bram Moolenaar13106602020-10-04 16:06:05 +02007393 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007394 * list, etc. Mostly like what JavaScript does, except that empty list and
7395 * empty dictionary are FALSE.
7396 */
7397 int
7398tv2bool(typval_T *tv)
7399{
7400 switch (tv->v_type)
7401 {
7402 case VAR_NUMBER:
7403 return tv->vval.v_number != 0;
7404 case VAR_FLOAT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007405 return tv->vval.v_float != 0.0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007406 case VAR_PARTIAL:
7407 return tv->vval.v_partial != NULL;
7408 case VAR_FUNC:
7409 case VAR_STRING:
7410 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
7411 case VAR_LIST:
7412 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
7413 case VAR_DICT:
7414 return tv->vval.v_dict != NULL
7415 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
7416 case VAR_BOOL:
7417 case VAR_SPECIAL:
7418 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
7419 case VAR_JOB:
7420#ifdef FEAT_JOB_CHANNEL
7421 return tv->vval.v_job != NULL;
7422#else
7423 break;
7424#endif
7425 case VAR_CHANNEL:
7426#ifdef FEAT_JOB_CHANNEL
7427 return tv->vval.v_channel != NULL;
7428#else
7429 break;
7430#endif
7431 case VAR_BLOB:
7432 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
7433 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02007434 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007435 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02007436 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00007437 case VAR_CLASS:
7438 case VAR_OBJECT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007439 break;
7440 }
7441 return FALSE;
7442}
7443
Bram Moolenaarea2d4072020-11-12 12:08:51 +01007444 void
7445emsg_using_string_as(typval_T *tv, int as_number)
7446{
7447 semsg(_(as_number ? e_using_string_as_number_str
7448 : e_using_string_as_bool_str),
7449 tv->vval.v_string == NULL
7450 ? (char_u *)"" : tv->vval.v_string);
7451}
7452
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007453/*
7454 * If "tv" is a string give an error and return FAIL.
7455 */
7456 int
7457check_not_string(typval_T *tv)
7458{
7459 if (tv->v_type == VAR_STRING)
7460 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +01007461 emsg_using_string_as(tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007462 clear_tv(tv);
7463 return FAIL;
7464 }
7465 return OK;
7466}
7467
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007468
7469#endif // FEAT_EVAL