blob: a89bcd19b4a516c3f5b0eef0aeff217eb486fa86 [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/*
Ernie Rael4c8da022023-10-11 21:35:11 +0200929 * If executing a class/object method, then fill in the lval_T.
930 * Set lr_tv to the executing item, and lr_exec_class to the executing class;
931 * use free_tv and class_unref when finished with the lval_root.
932 * For use by builtin functions.
933 *
934 * Return FAIL and do nothing if not executing in a class; otherwise OK.
935 */
936 int
937fill_exec_lval_root(lval_root_T *root)
938{
939 ectx_T *ectx = current_ectx;
940 if (ectx != NULL)
941 {
942 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
943 + current_ectx->ec_dfunc_idx;
944 ufunc_T *ufunc = dfunc->df_ufunc;
945 if (ufunc->uf_class != NULL) // executing a method?
946 {
947 typval_T *tv = alloc_tv();
948 if (tv != NULL)
949 {
950 CLEAR_POINTER(root);
951 root->lr_tv = tv;
952 copy_tv(STACK_TV_VAR(0), root->lr_tv);
953 root->lr_cl_exec = ufunc->uf_class;
954 ++root->lr_cl_exec->class_refcount;
955 return OK;
956 }
957 }
958 }
959
960 return FAIL;
961}
962
963/*
Bram Moolenaar98aff652022-09-06 21:02:35 +0100964 * Clear "current_ectx" and return the previous value. To be used when calling
965 * a user function.
966 */
967 ectx_T *
dundargocc57b5bc2022-11-02 13:30:51 +0000968clear_current_ectx(void)
Bram Moolenaar98aff652022-09-06 21:02:35 +0100969{
970 ectx_T *r = current_ectx;
971
972 current_ectx = NULL;
973 return r;
974}
975
976 void
977restore_current_ectx(ectx_T *ectx)
978{
979 if (current_ectx != NULL)
980 iemsg("Restoring current_ectx while it is not NULL");
981 current_ectx = ectx;
982}
983
984/*
Bram Moolenaar806a2732022-09-04 15:40:36 +0100985 * Add an entry for a deferred function call to the currently executing
986 * function.
987 * Return the list or NULL when failed.
988 */
989 static list_T *
990add_defer_item(int var_idx, int argcount, ectx_T *ectx)
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100991{
992 typval_T *defer_tv = STACK_TV_VAR(var_idx);
993 list_T *defer_l;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100994 list_T *l;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100995 typval_T listval;
996
997 if (defer_tv->v_type != VAR_LIST)
998 {
Bram Moolenaara5348f22022-09-04 11:42:22 +0100999 // first time, allocate the list
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001000 if (rettv_list_alloc(defer_tv) == FAIL)
Bram Moolenaar806a2732022-09-04 15:40:36 +01001001 return NULL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001002 }
1003 defer_l = defer_tv->vval.v_list;
1004
1005 l = list_alloc_with_items(argcount + 1);
1006 if (l == NULL)
Bram Moolenaar806a2732022-09-04 15:40:36 +01001007 return NULL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001008 listval.v_type = VAR_LIST;
1009 listval.vval.v_list = l;
1010 listval.v_lock = 0;
Bram Moolenaara5348f22022-09-04 11:42:22 +01001011 if (list_insert_tv(defer_l, &listval, defer_l->lv_first) == FAIL)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001012 {
1013 vim_free(l);
Bram Moolenaar806a2732022-09-04 15:40:36 +01001014 return NULL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001015 }
1016
Bram Moolenaar806a2732022-09-04 15:40:36 +01001017 return l;
1018}
1019
1020/*
1021 * Handle ISN_DEFER. Stack has a function reference and "argcount" arguments.
1022 * The local variable that lists deferred functions is "var_idx".
1023 * Returns OK or FAIL.
1024 */
1025 static int
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001026defer_command(int var_idx, int has_obj, int argcount, ectx_T *ectx)
Bram Moolenaar806a2732022-09-04 15:40:36 +01001027{
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001028 int obj_off = has_obj ? 1 : 0;
1029 list_T *l = add_defer_item(var_idx, argcount + obj_off, ectx);
Bram Moolenaar806a2732022-09-04 15:40:36 +01001030 int i;
1031 typval_T *func_tv;
1032
1033 if (l == NULL)
1034 return FAIL;
1035
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001036 func_tv = STACK_TV_BOT(-argcount - 1);
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001037 if (has_obj ? func_tv->v_type != VAR_PARTIAL : func_tv->v_type != VAR_FUNC)
Bram Moolenaarf5fec052022-09-11 11:49:22 +01001038 {
1039 semsg(_(e_expected_str_but_got_str),
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001040 has_obj ? "partial" : "function",
Bram Moolenaarf5fec052022-09-11 11:49:22 +01001041 vartype_name(func_tv->v_type));
1042 return FAIL;
1043 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001044 list_set_item(l, 0, func_tv);
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001045 if (has_obj)
1046 list_set_item(l, 1, STACK_TV_BOT(-argcount - 2));
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001047
Bram Moolenaarf5fec052022-09-11 11:49:22 +01001048 for (i = 0; i < argcount; ++i)
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001049 list_set_item(l, i + 1 + obj_off, STACK_TV_BOT(-argcount + i));
1050 ectx->ec_stack.ga_len -= argcount + 1 + obj_off;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001051 return OK;
1052}
1053
1054/*
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001055 * Add a deferred call for "name" with arguments "argvars[argcount]".
Bram Moolenaar806a2732022-09-04 15:40:36 +01001056 * Consumes "name", also on failure.
1057 * Only to be called when in_def_function() returns TRUE.
1058 */
1059 int
1060add_defer_function(char_u *name, int argcount, typval_T *argvars)
1061{
1062 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1063 + current_ectx->ec_dfunc_idx;
1064 list_T *l;
1065 typval_T func_tv;
1066 int i;
1067
1068 if (dfunc->df_defer_var_idx == 0)
1069 {
1070 iemsg("df_defer_var_idx is zero");
Bram Moolenaar31ea6bf2022-09-05 10:47:13 +01001071 vim_free(name);
Bram Moolenaar806a2732022-09-04 15:40:36 +01001072 return FAIL;
1073 }
Bram Moolenaar806a2732022-09-04 15:40:36 +01001074
Bram Moolenaarf5fec052022-09-11 11:49:22 +01001075 l = add_defer_item(dfunc->df_defer_var_idx - 1, argcount, current_ectx);
Bram Moolenaar806a2732022-09-04 15:40:36 +01001076 if (l == NULL)
1077 {
Bram Moolenaar31ea6bf2022-09-05 10:47:13 +01001078 vim_free(name);
Bram Moolenaar806a2732022-09-04 15:40:36 +01001079 return FAIL;
1080 }
1081
Bram Moolenaar31ea6bf2022-09-05 10:47:13 +01001082 func_tv.v_type = VAR_FUNC;
1083 func_tv.v_lock = 0;
1084 func_tv.vval.v_string = name;
Bram Moolenaar806a2732022-09-04 15:40:36 +01001085 list_set_item(l, 0, &func_tv);
Bram Moolenaar31ea6bf2022-09-05 10:47:13 +01001086
Bram Moolenaar806a2732022-09-04 15:40:36 +01001087 for (i = 0; i < argcount; ++i)
1088 list_set_item(l, i + 1, argvars + i);
1089 return OK;
1090}
1091
1092/*
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001093 * Invoked when returning from a function: Invoke any deferred calls.
1094 */
1095 static void
1096invoke_defer_funcs(ectx_T *ectx)
1097{
1098 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1099 + ectx->ec_dfunc_idx;
1100 typval_T *defer_tv = STACK_TV_VAR(dfunc->df_defer_var_idx - 1);
1101 listitem_T *li;
1102
1103 if (defer_tv->v_type != VAR_LIST)
1104 return; // no function added
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00001105 FOR_ALL_LIST_ITEMS(defer_tv->vval.v_list, li)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001106 {
1107 list_T *l = li->li_tv.vval.v_list;
1108 typval_T rettv;
1109 typval_T argvars[MAX_FUNC_ARGS];
1110 int i;
1111 listitem_T *arg_li = l->lv_first;
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001112 typval_T *functv = &l->lv_first->li_tv;
1113 int obj_off = functv->v_type == VAR_PARTIAL ? 1 : 0;
1114 int argcount = l->lv_len - 1 - obj_off;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001115
zeertzjqa1f2b5d2023-04-18 21:04:53 +01001116 if (functv->vval.v_string == NULL)
1117 // already being called, can happen if function does ":qa"
1118 continue;
1119
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001120 if (obj_off == 1)
1121 arg_li = arg_li->li_next; // second list item is the object
1122 for (i = 0; i < argcount; ++i)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001123 {
1124 arg_li = arg_li->li_next;
1125 argvars[i] = arg_li->li_tv;
1126 }
1127
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001128 funcexe_T funcexe;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001129 CLEAR_FIELD(funcexe);
1130 funcexe.fe_evaluate = TRUE;
1131 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001132 if (functv->v_type == VAR_PARTIAL)
1133 {
1134 funcexe.fe_partial = functv->vval.v_partial;
1135 funcexe.fe_object = l->lv_first->li_next->li_tv.vval.v_object;
1136 if (funcexe.fe_object != NULL)
1137 ++funcexe.fe_object->obj_refcount;
1138 }
zeertzjqa1f2b5d2023-04-18 21:04:53 +01001139
1140 char_u *name = functv->vval.v_string;
1141 functv->vval.v_string = NULL;
1142
1143 (void)call_func(name, -1, &rettv, argcount, argvars, &funcexe);
1144
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001145 clear_tv(&rettv);
zeertzjqa1f2b5d2023-04-18 21:04:53 +01001146 vim_free(name);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001147 }
1148}
1149
1150/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001151 * Return from the current function.
1152 */
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001153 static int
Bram Moolenaar4c137212021-04-19 16:48:48 +02001154func_return(ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001155{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001156 int idx;
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01001157 int ret_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001158 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1159 + ectx->ec_dfunc_idx;
1160 int argcount = ufunc_argcount(dfunc->df_ufunc);
Bram Moolenaarc620c052020-07-08 15:16:19 +02001161 estack_T *entry;
Bram Moolenaar12d26532021-02-19 19:13:21 +01001162 int prev_dfunc_idx = STACK_TV(ectx->ec_frame_idx
1163 + STACK_FRAME_FUNC_OFF)->vval.v_number;
Bram Moolenaarb06b50d2021-04-26 21:39:25 +02001164 funclocal_T *floc;
1165#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01001166 dfunc_T *prev_dfunc = ((dfunc_T *)def_functions.ga_data)
1167 + prev_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001168
Bram Moolenaar12d26532021-02-19 19:13:21 +01001169 if (do_profiling == PROF_YES)
1170 {
1171 ufunc_T *caller = prev_dfunc->df_ufunc;
1172
1173 if (dfunc->df_ufunc->uf_profiling
1174 || (caller != NULL && caller->uf_profiling))
1175 {
1176 profile_may_end_func(((profinfo_T *)profile_info_ga.ga_data)
1177 + profile_info_ga.ga_len - 1, dfunc->df_ufunc, caller);
1178 --profile_info_ga.ga_len;
1179 }
1180 }
1181#endif
Bram Moolenaar919c12c2021-12-14 14:29:16 +00001182
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001183 if (dfunc->df_defer_var_idx > 0)
1184 invoke_defer_funcs(ectx);
1185
Bram Moolenaar919c12c2021-12-14 14:29:16 +00001186 // No check for uf_refcount being zero, cannot think of a way that would
1187 // happen.
Bram Moolenaarc970e422021-03-17 15:03:04 +01001188 --dfunc->df_ufunc->uf_calls;
1189
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001190 // execution context goes one level up
Bram Moolenaarc620c052020-07-08 15:16:19 +02001191 entry = estack_pop();
1192 if (entry != NULL)
Bram Moolenaarc70fe462021-04-17 17:59:19 +02001193 current_sctx = entry->es_save_sctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001194
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001195 if (handle_closure_in_use(ectx, TRUE) == FAIL)
1196 return FAIL;
1197
Bram Moolenaar574950d2023-01-03 19:08:50 +00001198 // Clear the arguments. If this was an object method also clear the
1199 // object, it is just before the arguments.
1200 int top = ectx->ec_frame_idx - argcount;
Yegappan Lakshmanan1db15142023-09-19 20:34:05 +02001201 if (IS_OBJECT_METHOD(dfunc->df_ufunc))
Bram Moolenaar574950d2023-01-03 19:08:50 +00001202 --top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001203 for (idx = top; idx < ectx->ec_frame_idx; ++idx)
1204 clear_tv(STACK_TV(idx));
1205
1206 // Clear local variables and temp values, but not the return value.
1207 for (idx = ectx->ec_frame_idx + STACK_FRAME_SIZE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001208 idx < ectx->ec_stack.ga_len - 1; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001209 clear_tv(STACK_TV(idx));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001210
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01001211 // The return value should be on top of the stack. However, when aborting
1212 // it may not be there and ec_frame_idx is the top of the stack.
1213 ret_idx = ectx->ec_stack.ga_len - 1;
Bram Moolenaar0186e582021-01-10 18:33:11 +01001214 if (ret_idx == ectx->ec_frame_idx + STACK_FRAME_IDX_OFF)
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01001215 ret_idx = 0;
1216
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001217 if (ectx->ec_outer_ref != NULL)
1218 {
1219 if (ectx->ec_outer_ref->or_outer_allocated)
1220 vim_free(ectx->ec_outer_ref->or_outer);
1221 partial_unref(ectx->ec_outer_ref->or_partial);
1222 vim_free(ectx->ec_outer_ref);
1223 }
Bram Moolenaar0186e582021-01-10 18:33:11 +01001224
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001225 // Restore the previous frame.
Bram Moolenaar12d26532021-02-19 19:13:21 +01001226 ectx->ec_dfunc_idx = prev_dfunc_idx;
Bram Moolenaar0186e582021-01-10 18:33:11 +01001227 ectx->ec_iidx = STACK_TV(ectx->ec_frame_idx
1228 + STACK_FRAME_IIDX_OFF)->vval.v_number;
Bram Moolenaar5930ddc2021-04-26 20:32:59 +02001229 ectx->ec_instr = (void *)STACK_TV(ectx->ec_frame_idx
1230 + STACK_FRAME_INSTR_OFF)->vval.v_string;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001231 ectx->ec_outer_ref = (void *)STACK_TV(ectx->ec_frame_idx
Bram Moolenaar0186e582021-01-10 18:33:11 +01001232 + STACK_FRAME_OUTER_OFF)->vval.v_string;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001233 floc = (void *)STACK_TV(ectx->ec_frame_idx
1234 + STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string;
Bram Moolenaar5366e1a2020-10-01 13:01:34 +02001235 // restoring ec_frame_idx must be last
Bram Moolenaar0186e582021-01-10 18:33:11 +01001236 ectx->ec_frame_idx = STACK_TV(ectx->ec_frame_idx
1237 + STACK_FRAME_IDX_OFF)->vval.v_number;
Bram Moolenaard386e922021-04-25 14:48:49 +02001238
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001239 if (floc == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02001240 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001241 else
1242 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02001243 ectx->ec_funclocal = *floc;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001244 vim_free(floc);
1245 }
1246
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01001247 if (ret_idx > 0)
1248 {
1249 // Reset the stack to the position before the call, with a spot for the
1250 // return value, moved there from above the frame.
1251 ectx->ec_stack.ga_len = top + 1;
1252 *STACK_TV_BOT(-1) = *STACK_TV(ret_idx);
1253 }
1254 else
1255 // Reset the stack to the position before the call.
1256 ectx->ec_stack.ga_len = top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001257
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001258 funcdepth_decrement();
Bram Moolenaare99d4222021-06-13 14:01:26 +02001259 --ex_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001260 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001261}
1262
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001263/*
1264 * Prepare arguments and rettv for calling a builtin or user function.
1265 */
1266 static int
1267call_prepare(int argcount, typval_T *argvars, ectx_T *ectx)
1268{
1269 int idx;
1270 typval_T *tv;
1271
1272 // Move arguments from bottom of the stack to argvars[] and add terminator.
1273 for (idx = 0; idx < argcount; ++idx)
1274 argvars[idx] = *STACK_TV_BOT(idx - argcount);
1275 argvars[argcount].v_type = VAR_UNKNOWN;
1276
1277 // Result replaces the arguments on the stack.
1278 if (argcount > 0)
1279 ectx->ec_stack.ga_len -= argcount - 1;
Bram Moolenaar35578162021-08-02 19:10:38 +02001280 else if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001281 return FAIL;
1282 else
1283 ++ectx->ec_stack.ga_len;
1284
1285 // Default return value is zero.
1286 tv = STACK_TV_BOT(-1);
1287 tv->v_type = VAR_NUMBER;
1288 tv->vval.v_number = 0;
Bram Moolenaar24565cf2022-03-28 18:16:52 +01001289 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001290
1291 return OK;
1292}
1293
1294/*
1295 * Call a builtin function by index.
1296 */
1297 static int
1298call_bfunc(int func_idx, int argcount, ectx_T *ectx)
1299{
1300 typval_T argvars[MAX_FUNC_ARGS];
1301 int idx;
Bram Moolenaar171fb922020-10-28 16:54:47 +01001302 int did_emsg_before = did_emsg;
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001303 ectx_T *prev_ectx = current_ectx;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001304 char *save_func_name = ectx->ec_where.wt_func_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001305
1306 if (call_prepare(argcount, argvars, ectx) == FAIL)
1307 return FAIL;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001308 ectx->ec_where.wt_func_name = internal_func_name(func_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001309
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001310 // Call the builtin function. Set "current_ectx" so that when it
1311 // recursively invokes call_def_function() a closure context can be set.
1312 current_ectx = ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001313 call_internal_func_by_idx(func_idx, argvars, STACK_TV_BOT(-1));
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001314 current_ectx = prev_ectx;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001315 ectx->ec_where.wt_func_name = save_func_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001316
1317 // Clear the arguments.
1318 for (idx = 0; idx < argcount; ++idx)
1319 clear_tv(&argvars[idx]);
Bram Moolenaar015f4262020-05-05 21:25:22 +02001320
Bram Moolenaar57f799e2020-12-12 20:42:19 +01001321 if (did_emsg > did_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +02001322 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001323 return OK;
1324}
1325
1326/*
1327 * Execute a user defined function.
Bram Moolenaarab360522021-01-10 14:02:28 +01001328 * If the function is compiled this will add a stack frame and set the
1329 * instruction pointer at the start of the function.
1330 * Otherwise the function is called here.
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001331 * If "pt" is not null use "pt->pt_outer" for ec_outer_ref->or_outer.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001332 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001333 */
1334 static int
Bram Moolenaar0186e582021-01-10 18:33:11 +01001335call_ufunc(
1336 ufunc_T *ufunc,
1337 partial_T *pt,
1338 int argcount,
1339 ectx_T *ectx,
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001340 isn_T *iptr,
1341 dict_T *selfdict)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001342{
1343 typval_T argvars[MAX_FUNC_ARGS];
1344 funcexe_T funcexe;
Bram Moolenaar0917e862023-02-18 14:42:44 +00001345 funcerror_T error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001346 int idx;
Bram Moolenaar28ee8922020-10-28 20:20:00 +01001347 int did_emsg_before = did_emsg;
Bram Moolenaar139575d2022-03-15 19:29:30 +00001348 compiletype_T compile_type = get_compile_type(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001349
Bram Moolenaare99d4222021-06-13 14:01:26 +02001350 if (func_needs_compiling(ufunc, compile_type)
1351 && compile_def_function(ufunc, FALSE, compile_type, NULL)
1352 == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02001353 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001354 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001355 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +01001356 error = check_user_func_argcount(ufunc, argcount);
Bram Moolenaar50824712020-12-20 21:10:17 +01001357 if (error != FCERR_UNKNOWN)
1358 {
1359 if (error == FCERR_TOOMANY)
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001360 semsg(_(e_too_many_arguments_for_function_str),
1361 printable_func_name(ufunc));
Bram Moolenaar50824712020-12-20 21:10:17 +01001362 else
Bram Moolenaare1242042021-12-16 20:56:57 +00001363 semsg(_(e_not_enough_arguments_for_function_str),
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001364 printable_func_name(ufunc));
Bram Moolenaar50824712020-12-20 21:10:17 +01001365 return FAIL;
1366 }
1367
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001368 // The function has been compiled, can call it quickly. For a function
1369 // that was defined later: we can call it directly next time.
1370 if (iptr != NULL)
1371 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001372 delete_instr(iptr);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001373 iptr->isn_type = ISN_DCALL;
1374 iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1375 iptr->isn_arg.dfunc.cdf_argcount = argcount;
1376 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001377 return call_dfunc(ufunc->uf_dfunc_idx, pt, argcount, ectx);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001378 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001379
1380 if (call_prepare(argcount, argvars, ectx) == FAIL)
1381 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +02001382 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00001383 funcexe.fe_evaluate = TRUE;
1384 funcexe.fe_selfdict = selfdict != NULL ? selfdict : dict_stack_get_dict();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001385
1386 // Call the user function. Result goes in last position on the stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001387 error = call_user_func_check(ufunc, argcount, argvars,
Bram Moolenaar851f86b2021-12-13 14:26:44 +00001388 STACK_TV_BOT(-1), &funcexe, funcexe.fe_selfdict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001389
1390 // Clear the arguments.
1391 for (idx = 0; idx < argcount; ++idx)
1392 clear_tv(&argvars[idx]);
1393
1394 if (error != FCERR_NONE)
1395 {
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01001396 user_func_error(error, printable_func_name(ufunc),
1397 funcexe.fe_found_var);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001398 return FAIL;
1399 }
Bram Moolenaar28ee8922020-10-28 20:20:00 +01001400 if (did_emsg > did_emsg_before)
Bram Moolenaared677f52020-08-12 16:38:10 +02001401 // Error other than from calling the function itself.
1402 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001403 return OK;
1404}
1405
1406/*
Bram Moolenaara91a7132021-03-25 21:12:15 +01001407 * If command modifiers were applied restore them.
1408 */
1409 static void
1410may_restore_cmdmod(funclocal_T *funclocal)
1411{
1412 if (funclocal->floc_restore_cmdmod)
1413 {
1414 cmdmod.cmod_filter_regmatch.regprog = NULL;
1415 undo_cmdmod(&cmdmod);
1416 cmdmod = funclocal->floc_save_cmdmod;
1417 funclocal->floc_restore_cmdmod = FALSE;
1418 }
1419}
1420
1421/*
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001422 * Return TRUE if an error was given (not caught in try/catch) or CTRL-C was
1423 * pressed.
Bram Moolenaara1773442020-08-12 15:21:22 +02001424 */
1425 static int
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001426vim9_aborting(int prev_uncaught_emsg)
Bram Moolenaara1773442020-08-12 15:21:22 +02001427{
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001428 return uncaught_emsg > prev_uncaught_emsg || got_int || did_throw;
Bram Moolenaara1773442020-08-12 15:21:22 +02001429}
1430
1431/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001432 * Execute a function by "name".
1433 * This can be a builtin function or a user function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001434 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001435 * Returns FAIL if not found without an error message.
1436 */
1437 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001438call_by_name(
1439 char_u *name,
1440 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001441 ectx_T *ectx,
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001442 isn_T *iptr,
1443 dict_T *selfdict)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001444{
1445 ufunc_T *ufunc;
1446
1447 if (builtin_function(name, -1))
1448 {
1449 int func_idx = find_internal_func(name);
1450
Bram Moolenaar1983f1a2022-02-28 20:55:02 +00001451 if (func_idx < 0) // Impossible?
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001452 return FAIL;
Bram Moolenaar389df252020-07-09 21:20:47 +02001453 if (check_internal_func(func_idx, argcount) < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001454 return FAIL;
1455 return call_bfunc(func_idx, argcount, ectx);
1456 }
1457
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00001458 ufunc = find_func(name, FALSE);
Bram Moolenaara1773442020-08-12 15:21:22 +02001459
1460 if (ufunc == NULL)
1461 {
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001462 int prev_uncaught_emsg = uncaught_emsg;
Bram Moolenaara1773442020-08-12 15:21:22 +02001463
1464 if (script_autoload(name, TRUE))
1465 // loaded a package, search for the function again
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00001466 ufunc = find_func(name, FALSE);
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001467
1468 if (vim9_aborting(prev_uncaught_emsg))
Bram Moolenaara1773442020-08-12 15:21:22 +02001469 return FAIL; // bail out if loading the script caused an error
1470 }
1471
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001472 if (ufunc != NULL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001473 {
Bram Moolenaar0d89d8a2022-12-31 14:01:24 +00001474 if (check_ufunc_arg_types(ufunc, argcount, 0, ectx) == FAIL)
1475 return FAIL;
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001476
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001477 return call_ufunc(ufunc, NULL, argcount, ectx, iptr, selfdict);
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001478 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001479
1480 return FAIL;
1481}
1482
1483 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001484call_partial(
1485 typval_T *tv,
1486 int argcount_arg,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001487 ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001488{
Bram Moolenaara90afb92020-07-15 22:38:56 +02001489 int argcount = argcount_arg;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001490 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001491 int called_emsg_before = called_emsg;
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +01001492 int res = FAIL;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001493 dict_T *selfdict = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001494
1495 if (tv->v_type == VAR_PARTIAL)
1496 {
Bram Moolenaara90afb92020-07-15 22:38:56 +02001497 partial_T *pt = tv->vval.v_partial;
1498 int i;
1499
1500 if (pt->pt_argc > 0)
1501 {
1502 // Make space for arguments from the partial, shift the "argcount"
1503 // arguments up.
Bram Moolenaar35578162021-08-02 19:10:38 +02001504 if (GA_GROW_FAILS(&ectx->ec_stack, pt->pt_argc))
Bram Moolenaara90afb92020-07-15 22:38:56 +02001505 return FAIL;
1506 for (i = 1; i <= argcount; ++i)
1507 *STACK_TV_BOT(-i + pt->pt_argc) = *STACK_TV_BOT(-i);
1508 ectx->ec_stack.ga_len += pt->pt_argc;
1509 argcount += pt->pt_argc;
1510
1511 // copy the arguments from the partial onto the stack
1512 for (i = 0; i < pt->pt_argc; ++i)
1513 copy_tv(&pt->pt_argv[i], STACK_TV_BOT(-argcount + i));
1514 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001515 selfdict = pt->pt_dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001516
1517 if (pt->pt_func != NULL)
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001518 return call_ufunc(pt->pt_func, pt, argcount, ectx, NULL, selfdict);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001519
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001520 name = pt->pt_name;
1521 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001522 else if (tv->v_type == VAR_FUNC)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001523 name = tv->vval.v_string;
Bram Moolenaar95006e32020-08-29 17:47:08 +02001524 if (name != NULL)
1525 {
Bram Moolenaar0917e862023-02-18 14:42:44 +00001526 char_u fname_buf[FLEN_FIXED + 1];
1527 char_u *tofree = NULL;
1528 funcerror_T error = FCERR_NONE;
1529 char_u *fname;
Bram Moolenaar95006e32020-08-29 17:47:08 +02001530
1531 // May need to translate <SNR>123_ to K_SNR.
1532 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
1533 if (error != FCERR_NONE)
1534 res = FAIL;
1535 else
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001536 res = call_by_name(fname, argcount, ectx, NULL, selfdict);
Bram Moolenaar95006e32020-08-29 17:47:08 +02001537 vim_free(tofree);
1538 }
1539
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +01001540 if (res == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001541 {
1542 if (called_emsg == called_emsg_before)
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001543 emsg_funcname(e_unknown_function_str,
Bram Moolenaar015f4262020-05-05 21:25:22 +02001544 name == NULL ? (char_u *)"[unknown]" : name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001545 return FAIL;
1546 }
1547 return OK;
1548}
1549
1550/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001551 * Check if "lock" is VAR_LOCKED or VAR_FIXED. If so give an error and return
1552 * TRUE.
1553 */
1554 static int
1555error_if_locked(int lock, char *error)
1556{
1557 if (lock & (VAR_LOCKED | VAR_FIXED))
1558 {
1559 emsg(_(error));
1560 return TRUE;
1561 }
1562 return FALSE;
1563}
1564
1565/*
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001566 * Give an error if "tv" is not a number and return FAIL.
1567 */
1568 static int
1569check_for_number(typval_T *tv)
1570{
1571 if (tv->v_type != VAR_NUMBER)
1572 {
1573 semsg(_(e_expected_str_but_got_str),
1574 vartype_name(VAR_NUMBER), vartype_name(tv->v_type));
1575 return FAIL;
1576 }
1577 return OK;
1578}
1579
1580/*
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001581 * Store "tv" in variable "name".
1582 * This is for s: and g: variables.
1583 */
1584 static void
1585store_var(char_u *name, typval_T *tv)
1586{
1587 funccal_entry_T entry;
Bram Moolenaard877a572021-04-01 19:42:48 +02001588 int flags = ASSIGN_DECL;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001589
Bram Moolenaard877a572021-04-01 19:42:48 +02001590 if (tv->v_lock)
1591 flags |= ASSIGN_CONST;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001592 save_funccal(&entry);
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001593 set_var_const(name, 0, NULL, tv, FALSE, flags, 0);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001594 restore_funccal();
1595}
1596
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001597/*
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001598 * Convert "tv" to a string.
1599 * Return FAIL if not allowed.
1600 */
1601 static int
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001602do_2string(typval_T *tv, int is_2string_any, int tolerant)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001603{
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00001604 if (tv->v_type == VAR_STRING)
1605 return OK;
1606
1607 char_u *str;
1608
1609 if (is_2string_any)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001610 {
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00001611 switch (tv->v_type)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001612 {
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00001613 case VAR_SPECIAL:
1614 case VAR_BOOL:
1615 case VAR_NUMBER:
1616 case VAR_FLOAT:
1617 case VAR_BLOB: break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001618
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00001619 case VAR_LIST:
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001620 if (tolerant)
1621 {
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001622 char_u *s, *e, *p;
1623 garray_T ga;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001624
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001625 ga_init2(&ga, sizeof(char_u *), 1);
1626
1627 // Convert to NL separated items, then
1628 // escape the items and replace the NL with
1629 // a space.
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001630 str = typval2string(tv, TRUE);
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001631 if (str == NULL)
1632 return FAIL;
1633 s = str;
1634 while ((e = vim_strchr(s, '\n')) != NULL)
1635 {
1636 *e = NUL;
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +01001637 p = vim_strsave_fnameescape(s,
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00001638 VSE_NONE);
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001639 if (p != NULL)
1640 {
1641 ga_concat(&ga, p);
1642 ga_concat(&ga, (char_u *)" ");
1643 vim_free(p);
1644 }
1645 s = e + 1;
1646 }
1647 vim_free(str);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001648 clear_tv(tv);
1649 tv->v_type = VAR_STRING;
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001650 tv->vval.v_string = ga.ga_data;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001651 return OK;
1652 }
1653 // FALLTHROUGH
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00001654 default: to_string_error(tv->v_type);
1655 return FAIL;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001656 }
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001657 }
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00001658 str = typval_tostring(tv, TRUE);
1659 clear_tv(tv);
1660 tv->v_type = VAR_STRING;
1661 tv->vval.v_string = str;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001662 return OK;
1663}
1664
1665/*
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001666 * When the value of "sv" is a null list of dict, allocate it.
1667 */
1668 static void
Bram Moolenaar859cc212022-03-28 15:22:35 +01001669allocate_if_null(svar_T *sv)
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001670{
Bram Moolenaar859cc212022-03-28 15:22:35 +01001671 typval_T *tv = sv->sv_tv;
1672
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001673 switch (tv->v_type)
1674 {
1675 case VAR_LIST:
Bram Moolenaar859cc212022-03-28 15:22:35 +01001676 if (tv->vval.v_list == NULL && sv->sv_type != &t_list_empty)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001677 (void)rettv_list_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001678 break;
1679 case VAR_DICT:
Bram Moolenaar859cc212022-03-28 15:22:35 +01001680 if (tv->vval.v_dict == NULL && sv->sv_type != &t_dict_empty)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001681 (void)rettv_dict_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001682 break;
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02001683 case VAR_BLOB:
Bram Moolenaar859cc212022-03-28 15:22:35 +01001684 if (tv->vval.v_blob == NULL && sv->sv_type != &t_blob_null)
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02001685 (void)rettv_blob_alloc(tv);
1686 break;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001687 default:
1688 break;
1689 }
1690}
Bram Moolenaard3aac292020-04-19 14:32:17 +02001691
Bram Moolenaare7525c52021-01-09 13:20:37 +01001692/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001693 * Return the character "str[index]" where "index" is the character index,
1694 * including composing characters.
1695 * If "index" is out of range NULL is returned.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001696 */
1697 char_u *
1698char_from_string(char_u *str, varnumber_T index)
1699{
1700 size_t nbyte = 0;
1701 varnumber_T nchar = index;
1702 size_t slen;
1703
1704 if (str == NULL)
1705 return NULL;
1706 slen = STRLEN(str);
1707
Bram Moolenaarff871402021-03-26 13:34:05 +01001708 // Do the same as for a list: a negative index counts from the end.
1709 // Optimization to check the first byte to be below 0x80 (and no composing
1710 // character follows) makes this a lot faster.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001711 if (index < 0)
1712 {
1713 int clen = 0;
1714
1715 for (nbyte = 0; nbyte < slen; ++clen)
Bram Moolenaarff871402021-03-26 13:34:05 +01001716 {
1717 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1718 ++nbyte;
1719 else if (enc_utf8)
1720 nbyte += utfc_ptr2len(str + nbyte);
1721 else
1722 nbyte += mb_ptr2len(str + nbyte);
1723 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001724 nchar = clen + index;
1725 if (nchar < 0)
1726 // unlike list: index out of range results in empty string
1727 return NULL;
1728 }
1729
1730 for (nbyte = 0; nchar > 0 && nbyte < slen; --nchar)
Bram Moolenaarff871402021-03-26 13:34:05 +01001731 {
1732 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1733 ++nbyte;
1734 else if (enc_utf8)
1735 nbyte += utfc_ptr2len(str + nbyte);
1736 else
1737 nbyte += mb_ptr2len(str + nbyte);
1738 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001739 if (nbyte >= slen)
1740 return NULL;
Bram Moolenaar0289a092021-03-14 18:40:19 +01001741 return vim_strnsave(str + nbyte, mb_ptr2len(str + nbyte));
Bram Moolenaare7525c52021-01-09 13:20:37 +01001742}
1743
1744/*
1745 * Get the byte index for character index "idx" in string "str" with length
Bram Moolenaar0289a092021-03-14 18:40:19 +01001746 * "str_len". Composing characters are included.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001747 * If going over the end return "str_len".
1748 * If "idx" is negative count from the end, -1 is the last character.
1749 * When going over the start return -1.
1750 */
1751 static long
1752char_idx2byte(char_u *str, size_t str_len, varnumber_T idx)
1753{
1754 varnumber_T nchar = idx;
1755 size_t nbyte = 0;
1756
1757 if (nchar >= 0)
1758 {
1759 while (nchar > 0 && nbyte < str_len)
1760 {
Bram Moolenaar0289a092021-03-14 18:40:19 +01001761 nbyte += mb_ptr2len(str + nbyte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001762 --nchar;
1763 }
1764 }
1765 else
1766 {
1767 nbyte = str_len;
1768 while (nchar < 0 && nbyte > 0)
1769 {
1770 --nbyte;
1771 nbyte -= mb_head_off(str, str + nbyte);
1772 ++nchar;
1773 }
1774 if (nchar < 0)
1775 return -1;
1776 }
1777 return (long)nbyte;
1778}
1779
1780/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001781 * Return the slice "str[first : last]" using character indexes. Composing
1782 * characters are included.
Bram Moolenaar6601b622021-01-13 21:47:15 +01001783 * "exclusive" is TRUE for slice().
Bram Moolenaare7525c52021-01-09 13:20:37 +01001784 * Return NULL when the result is empty.
1785 */
1786 char_u *
Bram Moolenaar6601b622021-01-13 21:47:15 +01001787string_slice(char_u *str, varnumber_T first, varnumber_T last, int exclusive)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001788{
1789 long start_byte, end_byte;
1790 size_t slen;
1791
1792 if (str == NULL)
1793 return NULL;
1794 slen = STRLEN(str);
1795 start_byte = char_idx2byte(str, slen, first);
1796 if (start_byte < 0)
1797 start_byte = 0; // first index very negative: use zero
Bram Moolenaar6601b622021-01-13 21:47:15 +01001798 if ((last == -1 && !exclusive) || last == VARNUM_MAX)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001799 end_byte = (long)slen;
1800 else
1801 {
1802 end_byte = char_idx2byte(str, slen, last);
Bram Moolenaar6601b622021-01-13 21:47:15 +01001803 if (!exclusive && end_byte >= 0 && end_byte < (long)slen)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001804 // end index is inclusive
Bram Moolenaar0289a092021-03-14 18:40:19 +01001805 end_byte += mb_ptr2len(str + end_byte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001806 }
1807
1808 if (start_byte >= (long)slen || end_byte <= start_byte)
1809 return NULL;
1810 return vim_strnsave(str + start_byte, end_byte - start_byte);
1811}
1812
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001813/*
1814 * Get a script variable for ISN_STORESCRIPT and ISN_LOADSCRIPT.
1815 * When "dfunc_idx" is negative don't give an error.
1816 * Returns NULL for an error.
1817 */
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001818 static svar_T *
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001819get_script_svar(scriptref_T *sref, int dfunc_idx)
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001820{
1821 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001822 dfunc_T *dfunc = dfunc_idx < 0 ? NULL
1823 : ((dfunc_T *)def_functions.ga_data) + dfunc_idx;
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001824 svar_T *sv;
1825
1826 if (sref->sref_seq != si->sn_script_seq)
1827 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001828 // The script was reloaded after the function was compiled, the
1829 // script_idx may not be valid.
1830 if (dfunc != NULL)
1831 semsg(_(e_script_variable_invalid_after_reload_in_function_str),
1832 printable_func_name(dfunc->df_ufunc));
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001833 return NULL;
1834 }
1835 sv = ((svar_T *)si->sn_var_vals.ga_data) + sref->sref_idx;
Bram Moolenaar6de22962022-09-09 21:35:36 +01001836 if (sv->sv_name == NULL)
1837 {
1838 if (dfunc != NULL)
1839 emsg(_(e_script_variable_was_deleted));
1840 return NULL;
1841 }
Bram Moolenaar60dc8272021-07-29 22:48:54 +02001842 if (!equal_type(sv->sv_type, sref->sref_type, 0))
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001843 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001844 if (dfunc != NULL)
1845 emsg(_(e_script_variable_type_changed));
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001846 return NULL;
1847 }
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001848
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01001849 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0
1850 && sref->sref_sid != current_sctx.sc_sid)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001851 {
1852 if (dfunc != NULL)
1853 semsg(_(e_item_not_exported_in_script_str), sv->sv_name);
1854 return NULL;
1855 }
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001856 return sv;
1857}
1858
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001859/*
Bram Moolenaar20677332021-06-06 17:02:53 +02001860 * Function passed to do_cmdline() for splitting a script joined by NL
1861 * characters.
1862 */
1863 static char_u *
1864get_split_sourceline(
1865 int c UNUSED,
1866 void *cookie,
1867 int indent UNUSED,
1868 getline_opt_T options UNUSED)
1869{
1870 source_cookie_T *sp = (source_cookie_T *)cookie;
1871 char_u *p;
1872 char_u *line;
1873
Bram Moolenaar20677332021-06-06 17:02:53 +02001874 p = vim_strchr(sp->nextline, '\n');
1875 if (p == NULL)
1876 {
1877 line = vim_strsave(sp->nextline);
1878 sp->nextline += STRLEN(sp->nextline);
1879 }
1880 else
1881 {
1882 line = vim_strnsave(sp->nextline, p - sp->nextline);
1883 sp->nextline = p + 1;
1884 }
1885 return line;
1886}
1887
1888/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001889 * Execute a function by "name".
1890 * This can be a builtin function, user function or a funcref.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001891 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001892 */
1893 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001894call_eval_func(
1895 char_u *name,
1896 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001897 ectx_T *ectx,
1898 isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001899{
Bram Moolenaared677f52020-08-12 16:38:10 +02001900 int called_emsg_before = called_emsg;
1901 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001902
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001903 res = call_by_name(name, argcount, ectx, iptr, NULL);
Bram Moolenaared677f52020-08-12 16:38:10 +02001904 if (res == FAIL && called_emsg == called_emsg_before)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001905 {
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001906 dictitem_T *v;
1907
1908 v = find_var(name, NULL, FALSE);
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001909 if (v == NULL || (v->di_tv.v_type != VAR_PARTIAL
1910 && v->di_tv.v_type != VAR_FUNC))
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001911 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001912 emsg_funcname(e_unknown_function_str, name);
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001913 return FAIL;
1914 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001915 return call_partial(&v->di_tv, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001916 }
Bram Moolenaared677f52020-08-12 16:38:10 +02001917 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001918}
1919
1920/*
Bram Moolenaarf112f302020-12-20 17:47:52 +01001921 * When a function reference is used, fill a partial with the information
1922 * needed, especially when it is used as a closure.
1923 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001924 int
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001925fill_partial_and_closure(
Bram Moolenaarcc341812022-09-19 15:54:34 +01001926 partial_T *pt,
1927 ufunc_T *ufunc,
1928 loopvarinfo_T *lvi,
1929 ectx_T *ectx)
Bram Moolenaarf112f302020-12-20 17:47:52 +01001930{
1931 pt->pt_func = ufunc;
1932 pt->pt_refcount = 1;
1933
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001934 if (ufunc->uf_flags & FC_CLOSURE)
Bram Moolenaarf112f302020-12-20 17:47:52 +01001935 {
1936 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1937 + ectx->ec_dfunc_idx;
1938
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001939 // The closure may need to find arguments and local variables of the
1940 // current function in the stack.
Bram Moolenaar0186e582021-01-10 18:33:11 +01001941 pt->pt_outer.out_stack = &ectx->ec_stack;
1942 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001943 if (ectx->ec_outer_ref != NULL)
1944 {
1945 // The current context already has a context, link to that one.
1946 pt->pt_outer.out_up = ectx->ec_outer_ref->or_outer;
1947 if (ectx->ec_outer_ref->or_partial != NULL)
1948 {
1949 pt->pt_outer.out_up_partial = ectx->ec_outer_ref->or_partial;
1950 ++pt->pt_outer.out_up_partial->pt_refcount;
1951 }
1952 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01001953
Bram Moolenaarcc341812022-09-19 15:54:34 +01001954 if (lvi != NULL)
1955 {
1956 int depth;
1957
1958 // The closure may need to find variables defined inside a loop,
1959 // for every nested loop. A new reference is made every time,
1960 // ISN_ENDLOOP will check if they are actually used.
1961 for (depth = 0; depth < lvi->lvi_depth; ++depth)
1962 {
1963 pt->pt_outer.out_loop[depth].stack = &ectx->ec_stack;
1964 pt->pt_outer.out_loop[depth].var_idx = ectx->ec_frame_idx
1965 + STACK_FRAME_SIZE + lvi->lvi_loop[depth].var_idx;
1966 pt->pt_outer.out_loop[depth].var_count =
1967 lvi->lvi_loop[depth].var_count;
1968 }
Bram Moolenaar6d313be2022-09-22 16:36:25 +01001969 pt->pt_outer.out_loop_size = lvi->lvi_depth;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001970 }
Bram Moolenaar6d313be2022-09-22 16:36:25 +01001971 else
1972 pt->pt_outer.out_loop_size = 0;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001973
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001974 // If the function currently executing returns and the closure is still
1975 // being referenced, we need to make a copy of the context (arguments
1976 // and local variables) so that the closure can use it later.
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001977 // Store a reference to the partial so we can handle that.
Bram Moolenaar35578162021-08-02 19:10:38 +02001978 if (GA_GROW_FAILS(&ectx->ec_funcrefs, 1))
Bram Moolenaarf112f302020-12-20 17:47:52 +01001979 {
1980 vim_free(pt);
1981 return FAIL;
1982 }
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001983 // Extra variable keeps the count of closures created in the current
1984 // function call.
Bram Moolenaarf112f302020-12-20 17:47:52 +01001985 ++(((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx
1986 + STACK_FRAME_SIZE + dfunc->df_varcount)->vval.v_number;
1987
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001988 ((partial_T **)ectx->ec_funcrefs.ga_data)[ectx->ec_funcrefs.ga_len]
1989 = pt;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001990 ++pt->pt_refcount;
1991 ++ectx->ec_funcrefs.ga_len;
1992 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001993 ++ufunc->uf_refcount;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001994 return OK;
1995}
1996
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001997/*
1998 * Execute iptr->isn_arg.string as an Ex command.
1999 */
2000 static int
Ernie Raelee865f32023-09-29 19:53:55 +02002001exec_command(isn_T *iptr, char_u *cmd_string)
Bram Moolenaaraacc9662021-08-13 19:40:51 +02002002{
2003 source_cookie_T cookie;
2004
2005 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00002006 // Pass getsourceline to get an error for a missing ":end" command.
Bram Moolenaaraacc9662021-08-13 19:40:51 +02002007 CLEAR_FIELD(cookie);
2008 cookie.sourcing_lnum = iptr->isn_lnum - 1;
Ernie Raelee865f32023-09-29 19:53:55 +02002009 if (do_cmdline(cmd_string, getsourceline, &cookie,
Bram Moolenaaraacc9662021-08-13 19:40:51 +02002010 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED) == FAIL
2011 || did_emsg)
2012 return FAIL;
2013 return OK;
2014}
2015
Bram Moolenaar89445512022-04-14 12:58:23 +01002016/*
2017 * If script "sid" is not loaded yet then load it now.
2018 * Caller must make sure "sid" is a valid script ID.
2019 * "loaded" is set to TRUE if the script had to be loaded.
2020 * Returns FAIL if loading fails, OK if already loaded or loaded now.
2021 */
2022 int
2023may_load_script(int sid, int *loaded)
2024{
2025 scriptitem_T *si = SCRIPT_ITEM(sid);
2026
2027 if (si->sn_state == SN_STATE_NOT_LOADED)
2028 {
2029 *loaded = TRUE;
2030 if (do_source(si->sn_name, FALSE, DOSO_NONE, NULL) == FAIL)
2031 {
2032 semsg(_(e_cant_open_file_str), si->sn_name);
2033 return FAIL;
2034 }
2035 }
2036 return OK;
2037}
2038
Bram Moolenaarf18332f2021-05-07 17:55:55 +02002039// used for v_instr of typval of VAR_INSTR
2040struct instr_S {
2041 ectx_T *instr_ectx;
2042 isn_T *instr_instr;
2043};
2044
Bram Moolenaar4c137212021-04-19 16:48:48 +02002045// used for substitute_instr
2046typedef struct subs_expr_S {
2047 ectx_T *subs_ectx;
2048 isn_T *subs_instr;
2049 int subs_status;
2050} subs_expr_T;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002051
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002052// Set when calling do_debug().
2053static ectx_T *debug_context = NULL;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02002054static int debug_var_count;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002055
2056/*
2057 * When debugging lookup "name" and return the typeval.
2058 * When not found return NULL.
2059 */
2060 typval_T *
2061lookup_debug_var(char_u *name)
2062{
2063 int idx;
2064 dfunc_T *dfunc;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02002065 ufunc_T *ufunc;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002066 ectx_T *ectx = debug_context;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02002067 int varargs_off;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002068
2069 if (ectx == NULL)
2070 return NULL;
2071 dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
2072
2073 // Go through the local variable names, from last to first.
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02002074 for (idx = debug_var_count - 1; idx >= 0; --idx)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002075 {
Bram Moolenaare406ff82022-03-10 20:47:43 +00002076 char_u *varname = ((char_u **)dfunc->df_var_names.ga_data)[idx];
2077
2078 // the variable name may be NULL when not available in this block
2079 if (varname != NULL && STRCMP(varname, name) == 0)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002080 return STACK_TV_VAR(idx);
2081 }
2082
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02002083 // Go through argument names.
2084 ufunc = dfunc->df_ufunc;
2085 varargs_off = ufunc->uf_va_name == NULL ? 0 : 1;
2086 for (idx = 0; idx < ufunc->uf_args.ga_len; ++idx)
2087 if (STRCMP(((char_u **)(ufunc->uf_args.ga_data))[idx], name) == 0)
2088 return STACK_TV(ectx->ec_frame_idx - ufunc->uf_args.ga_len
2089 - varargs_off + idx);
2090 if (ufunc->uf_va_name != NULL && STRCMP(ufunc->uf_va_name, name) == 0)
2091 return STACK_TV(ectx->ec_frame_idx - 1);
2092
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002093 return NULL;
2094}
2095
Bram Moolenaar26a44842021-09-02 18:49:06 +02002096/*
2097 * Return TRUE if there might be a breakpoint in "ufunc", which is when a
2098 * breakpoint was set in that function or when there is any expression.
2099 */
2100 int
2101may_break_in_function(ufunc_T *ufunc)
2102{
2103 return ufunc->uf_has_breakpoint || debug_has_expr_breakpoint();
2104}
2105
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002106 static void
2107handle_debug(isn_T *iptr, ectx_T *ectx)
2108{
2109 char_u *line;
2110 ufunc_T *ufunc = (((dfunc_T *)def_functions.ga_data)
2111 + ectx->ec_dfunc_idx)->df_ufunc;
2112 isn_T *ni;
2113 int end_lnum = iptr->isn_lnum;
2114 garray_T ga;
2115 int lnum;
2116
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02002117 if (ex_nesting_level > debug_break_level)
2118 {
2119 linenr_T breakpoint;
2120
Bram Moolenaar26a44842021-09-02 18:49:06 +02002121 if (!may_break_in_function(ufunc))
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02002122 return;
2123
2124 // check for the next breakpoint if needed
2125 breakpoint = dbg_find_breakpoint(FALSE, ufunc->uf_name,
Bram Moolenaar8cec9272021-06-23 20:20:53 +02002126 iptr->isn_arg.debug.dbg_break_lnum);
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02002127 if (breakpoint <= 0 || breakpoint > iptr->isn_lnum)
2128 return;
2129 }
2130
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002131 SOURCING_LNUM = iptr->isn_lnum;
2132 debug_context = ectx;
Bram Moolenaar8cec9272021-06-23 20:20:53 +02002133 debug_var_count = iptr->isn_arg.debug.dbg_var_names_len;
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002134
2135 for (ni = iptr + 1; ni->isn_type != ISN_FINISH; ++ni)
2136 if (ni->isn_type == ISN_DEBUG
2137 || ni->isn_type == ISN_RETURN
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002138 || ni->isn_type == ISN_RETURN_OBJECT
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002139 || ni->isn_type == ISN_RETURN_VOID)
2140 {
Bram Moolenaar112bed02021-11-23 22:16:34 +00002141 end_lnum = ni->isn_lnum + (ni->isn_type == ISN_DEBUG ? 0 : 1);
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002142 break;
2143 }
2144
2145 if (end_lnum > iptr->isn_lnum)
2146 {
2147 ga_init2(&ga, sizeof(char_u *), 10);
Bram Moolenaar310091d2021-12-23 21:14:37 +00002148 for (lnum = iptr->isn_lnum; lnum < end_lnum
2149 && lnum <= ufunc->uf_lines.ga_len; ++lnum)
Bram Moolenaar59b50c32021-06-17 22:27:48 +02002150 {
Bram Moolenaar303215d2021-07-07 20:10:43 +02002151 char_u *p = ((char_u **)ufunc->uf_lines.ga_data)[lnum - 1];
Bram Moolenaar59b50c32021-06-17 22:27:48 +02002152
Bram Moolenaar303215d2021-07-07 20:10:43 +02002153 if (p == NULL)
2154 continue; // left over from continuation line
2155 p = skipwhite(p);
Bram Moolenaar59b50c32021-06-17 22:27:48 +02002156 if (*p == '#')
2157 break;
Bram Moolenaar35578162021-08-02 19:10:38 +02002158 if (GA_GROW_OK(&ga, 1))
Bram Moolenaar59b50c32021-06-17 22:27:48 +02002159 ((char_u **)(ga.ga_data))[ga.ga_len++] = p;
2160 if (STRNCMP(p, "def ", 4) == 0)
2161 break;
2162 }
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002163 line = ga_concat_strings(&ga, " ");
2164 vim_free(ga.ga_data);
2165 }
2166 else
2167 line = ((char_u **)ufunc->uf_lines.ga_data)[iptr->isn_lnum - 1];
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002168
Dominique Pelle6e969552021-06-17 13:53:41 +02002169 do_debug(line == NULL ? (char_u *)"[empty]" : line);
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002170 debug_context = NULL;
2171
2172 if (end_lnum > iptr->isn_lnum)
2173 vim_free(line);
2174}
2175
Bram Moolenaar4c137212021-04-19 16:48:48 +02002176/*
Bram Moolenaar65b0d162022-12-13 18:43:22 +00002177 * Store a value in a list, dict, blob or object variable.
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002178 * Returns OK, FAIL or NOTDONE (uncatchable error).
2179 */
2180 static int
2181execute_storeindex(isn_T *iptr, ectx_T *ectx)
2182{
Bram Moolenaarf7d1c6e2023-01-16 20:47:57 +00002183 vartype_T dest_type = iptr->isn_arg.storeindex.si_vartype;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002184 typval_T *tv;
2185 typval_T *tv_idx = STACK_TV_BOT(-2);
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002186 long lidx = 0;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002187 typval_T *tv_dest = STACK_TV_BOT(-1);
2188 int status = OK;
2189
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002190 if (tv_idx->v_type == VAR_NUMBER)
2191 lidx = (long)tv_idx->vval.v_number;
2192
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002193 // Stack contains:
2194 // -3 value to be stored
2195 // -2 index
Yegappan Lakshmanan3775f772023-09-01 22:05:45 +02002196 // -1 dict, list, blob, object or class
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002197 tv = STACK_TV_BOT(-3);
2198 SOURCING_LNUM = iptr->isn_lnum;
Gianmaria Bajod7085a02023-08-31 18:15:26 +02002199
2200 // Make sure an object has been initialized
2201 if (dest_type == VAR_OBJECT && tv_dest->vval.v_object == NULL)
2202 {
2203 emsg(_(e_using_null_object));
2204 status = FAIL;
2205 }
2206 else if (dest_type == VAR_ANY)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002207 {
2208 dest_type = tv_dest->v_type;
2209 if (dest_type == VAR_DICT)
2210 status = do_2string(tv_idx, TRUE, FALSE);
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002211 else if (dest_type == VAR_OBJECT && tv_idx->v_type == VAR_STRING)
2212 {
2213 // Need to get the member index now that the class is known.
2214 object_T *obj = tv_dest->vval.v_object;
2215 class_T *cl = obj->obj_class;
2216 char_u *member = tv_idx->vval.v_string;
2217
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02002218 int m_idx;
2219 ocmember_T *m = object_member_lookup(cl, member, 0, &m_idx);
2220 if (m != NULL)
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002221 {
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02002222 if (*member == '_')
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002223 {
Ernie Raele6c9aa52023-10-06 19:55:52 +02002224 emsg_var_cl_define(e_cannot_access_private_variable_str,
2225 m->ocm_name, 0, cl);
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02002226 status = FAIL;
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002227 }
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002228
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02002229 lidx = m_idx;
2230 }
2231 else
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002232 {
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +02002233 member_not_found_msg(cl, VAR_OBJECT, member, 0);
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002234 status = FAIL;
2235 }
2236 }
2237 else if ((dest_type == VAR_LIST || dest_type == VAR_OBJECT)
2238 && tv_idx->v_type != VAR_NUMBER)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002239 {
2240 emsg(_(e_number_expected));
2241 status = FAIL;
2242 }
2243 }
Bram Moolenaare08be092022-02-17 13:08:26 +00002244
2245 if (status == OK)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002246 {
Bram Moolenaare08be092022-02-17 13:08:26 +00002247 if (dest_type == VAR_LIST)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002248 {
Bram Moolenaare08be092022-02-17 13:08:26 +00002249 list_T *list = tv_dest->vval.v_list;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002250
Bram Moolenaare08be092022-02-17 13:08:26 +00002251 if (list == NULL)
2252 {
2253 emsg(_(e_list_not_set));
2254 return FAIL;
2255 }
2256 if (lidx < 0 && list->lv_len + lidx >= 0)
2257 // negative index is relative to the end
2258 lidx = list->lv_len + lidx;
2259 if (lidx < 0 || lidx > list->lv_len)
2260 {
2261 semsg(_(e_list_index_out_of_range_nr), lidx);
2262 return FAIL;
2263 }
2264 if (lidx < list->lv_len)
2265 {
2266 listitem_T *li = list_find(list, lidx);
2267
2268 if (error_if_locked(li->li_tv.v_lock,
Bram Moolenaar70c43d82022-01-26 21:01:15 +00002269 e_cannot_change_locked_list_item))
Bram Moolenaare08be092022-02-17 13:08:26 +00002270 return FAIL;
2271 // overwrite existing list item
2272 clear_tv(&li->li_tv);
2273 li->li_tv = *tv;
2274 }
2275 else
2276 {
2277 if (error_if_locked(list->lv_lock, e_cannot_change_locked_list))
2278 return FAIL;
2279 // append to list, only fails when out of memory
2280 if (list_append_tv(list, tv) == FAIL)
2281 return NOTDONE;
2282 clear_tv(tv);
2283 }
2284 }
2285 else if (dest_type == VAR_DICT)
2286 {
2287 char_u *key = tv_idx->vval.v_string;
2288 dict_T *dict = tv_dest->vval.v_dict;
2289 dictitem_T *di;
2290
2291 SOURCING_LNUM = iptr->isn_lnum;
2292 if (dict == NULL)
2293 {
2294 emsg(_(e_dictionary_not_set));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002295 return FAIL;
Bram Moolenaare08be092022-02-17 13:08:26 +00002296 }
2297 if (key == NULL)
2298 key = (char_u *)"";
2299 di = dict_find(dict, key, -1);
2300 if (di != NULL)
2301 {
2302 if (error_if_locked(di->di_tv.v_lock,
2303 e_cannot_change_dict_item))
2304 return FAIL;
2305 // overwrite existing value
2306 clear_tv(&di->di_tv);
2307 di->di_tv = *tv;
2308 }
2309 else
2310 {
2311 if (error_if_locked(dict->dv_lock, e_cannot_change_dict))
2312 return FAIL;
2313 // add to dict, only fails when out of memory
2314 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
2315 return NOTDONE;
2316 clear_tv(tv);
2317 }
2318 }
2319 else if (dest_type == VAR_BLOB)
2320 {
Bram Moolenaare08be092022-02-17 13:08:26 +00002321 blob_T *blob = tv_dest->vval.v_blob;
Bram Moolenaar65b0d162022-12-13 18:43:22 +00002322 varnumber_T nr;
2323 int error = FALSE;
2324 int len;
Bram Moolenaare08be092022-02-17 13:08:26 +00002325
2326 if (blob == NULL)
2327 {
2328 emsg(_(e_blob_not_set));
2329 return FAIL;
2330 }
2331 len = blob_len(blob);
2332 if (lidx < 0 && len + lidx >= 0)
2333 // negative index is relative to the end
2334 lidx = len + lidx;
2335
2336 // Can add one byte at the end.
2337 if (lidx < 0 || lidx > len)
2338 {
2339 semsg(_(e_blob_index_out_of_range_nr), lidx);
2340 return FAIL;
2341 }
2342 if (value_check_lock(blob->bv_lock, (char_u *)"blob", FALSE))
2343 return FAIL;
2344 nr = tv_get_number_chk(tv, &error);
2345 if (error)
2346 return FAIL;
2347 blob_set_append(blob, lidx, nr);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002348 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002349 else if (dest_type == VAR_CLASS || dest_type == VAR_OBJECT)
2350 {
Yegappan Lakshmanan3775f772023-09-01 22:05:45 +02002351 typval_T *otv;
Bram Moolenaarf7d1c6e2023-01-16 20:47:57 +00002352
Yegappan Lakshmanan3775f772023-09-01 22:05:45 +02002353 if (dest_type == VAR_OBJECT)
2354 {
2355 object_T *obj = tv_dest->vval.v_object;
2356
2357 otv = (typval_T *)(obj + 1);
2358 class_T *itf = iptr->isn_arg.storeindex.si_class;
2359 if (itf != NULL)
2360 // convert interface member index to class member index
Ernie Rael18143d32023-09-04 22:30:41 +02002361 lidx = object_index_from_itf_index(itf, FALSE, lidx,
Yegappan Lakshmanan5a05d372023-09-29 19:43:11 +02002362 obj->obj_class);
Yegappan Lakshmanan3775f772023-09-01 22:05:45 +02002363 }
2364 else
2365 {
2366 // VAR_CLASS
2367 class_T *class = tv_dest->vval.v_class;
2368 otv = class->class_members_tv;
2369 }
Bram Moolenaarf7d1c6e2023-01-16 20:47:57 +00002370
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002371 clear_tv(&otv[lidx]);
2372 otv[lidx] = *tv;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002373 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002374 else
2375 {
Bram Moolenaare08be092022-02-17 13:08:26 +00002376 status = FAIL;
2377 semsg(_(e_cannot_index_str), vartype_name(dest_type));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002378 }
2379 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002380
2381 clear_tv(tv_idx);
2382 clear_tv(tv_dest);
2383 ectx->ec_stack.ga_len -= 3;
2384 if (status == FAIL)
2385 {
2386 clear_tv(tv);
2387 return FAIL;
2388 }
2389 return OK;
2390}
2391
2392/*
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002393 * Store a value in a list or blob range.
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002394 */
2395 static int
2396execute_storerange(isn_T *iptr, ectx_T *ectx)
2397{
2398 typval_T *tv;
2399 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2400 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2401 typval_T *tv_dest = STACK_TV_BOT(-1);
2402 int status = OK;
2403
2404 // Stack contains:
2405 // -4 value to be stored
2406 // -3 first index or "none"
2407 // -2 second index or "none"
2408 // -1 destination list or blob
2409 tv = STACK_TV_BOT(-4);
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002410 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002411 if (tv_dest->v_type == VAR_LIST)
2412 {
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002413 long n1;
2414 long n2;
2415 listitem_T *li1;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002416
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002417 n1 = (long)tv_get_number_chk(tv_idx1, NULL);
2418 if (tv_idx2->v_type == VAR_SPECIAL
2419 && tv_idx2->vval.v_number == VVAL_NONE)
2420 n2 = list_len(tv_dest->vval.v_list) - 1;
2421 else
2422 n2 = (long)tv_get_number_chk(tv_idx2, NULL);
2423
Bram Moolenaar22ebd172022-04-01 15:26:58 +01002424 li1 = check_range_index_one(tv_dest->vval.v_list, &n1, TRUE, FALSE);
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002425 if (li1 == NULL)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002426 status = FAIL;
2427 else
2428 {
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002429 status = check_range_index_two(tv_dest->vval.v_list,
2430 &n1, li1, &n2, FALSE);
2431 if (status != FAIL)
2432 status = list_assign_range(
2433 tv_dest->vval.v_list,
2434 tv->vval.v_list,
2435 n1,
2436 n2,
2437 tv_idx2->v_type == VAR_SPECIAL,
2438 (char_u *)"=",
2439 (char_u *)"[unknown]");
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002440 }
2441 }
2442 else if (tv_dest->v_type == VAR_BLOB)
2443 {
2444 varnumber_T n1;
2445 varnumber_T n2;
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002446 long bloblen;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002447
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002448 n1 = tv_get_number_chk(tv_idx1, NULL);
2449 if (tv_idx2->v_type == VAR_SPECIAL
2450 && tv_idx2->vval.v_number == VVAL_NONE)
2451 n2 = blob_len(tv_dest->vval.v_blob) - 1;
2452 else
2453 n2 = tv_get_number_chk(tv_idx2, NULL);
2454 bloblen = blob_len(tv_dest->vval.v_blob);
2455
2456 if (check_blob_index(bloblen, n1, FALSE) == FAIL
2457 || check_blob_range(bloblen, n1, n2, FALSE) == FAIL)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002458 status = FAIL;
2459 else
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002460 status = blob_set_range(tv_dest->vval.v_blob, n1, n2, tv);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002461 }
2462 else
2463 {
2464 status = FAIL;
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002465 emsg(_(e_list_or_blob_required));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002466 }
2467
2468 clear_tv(tv_idx1);
2469 clear_tv(tv_idx2);
2470 clear_tv(tv_dest);
2471 ectx->ec_stack.ga_len -= 4;
2472 clear_tv(tv);
2473
2474 return status;
2475}
2476
2477/*
2478 * Unlet item in list or dict variable.
2479 */
2480 static int
2481execute_unletindex(isn_T *iptr, ectx_T *ectx)
2482{
2483 typval_T *tv_idx = STACK_TV_BOT(-2);
2484 typval_T *tv_dest = STACK_TV_BOT(-1);
2485 int status = OK;
2486
2487 // Stack contains:
2488 // -2 index
2489 // -1 dict or list
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002490 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002491 if (tv_dest->v_type == VAR_DICT)
2492 {
2493 // unlet a dict item, index must be a string
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002494 if (tv_idx->v_type != VAR_STRING && tv_idx->v_type != VAR_NUMBER)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002495 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002496 semsg(_(e_expected_str_but_got_str),
2497 vartype_name(VAR_STRING),
2498 vartype_name(tv_idx->v_type));
2499 status = FAIL;
2500 }
2501 else
2502 {
2503 dict_T *d = tv_dest->vval.v_dict;
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002504 char_u *key;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002505 dictitem_T *di = NULL;
2506
2507 if (d != NULL && value_check_lock(
2508 d->dv_lock, NULL, FALSE))
2509 status = FAIL;
2510 else
2511 {
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002512 if (tv_idx->v_type == VAR_STRING)
2513 {
2514 key = tv_idx->vval.v_string;
2515 if (key == NULL)
2516 key = (char_u *)"";
2517 }
2518 else
2519 {
2520 key = tv_get_string(tv_idx);
2521 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002522 if (d != NULL)
2523 di = dict_find(d, key, (int)STRLEN(key));
2524 if (di == NULL)
2525 {
2526 // NULL dict is equivalent to empty dict
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00002527 semsg(_(e_key_not_present_in_dictionary_str), key);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002528 status = FAIL;
2529 }
2530 else if (var_check_fixed(di->di_flags,
2531 NULL, FALSE)
2532 || var_check_ro(di->di_flags,
2533 NULL, FALSE))
2534 status = FAIL;
2535 else
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002536 dictitem_remove(d, di, "unlet");
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002537 }
2538 }
2539 }
2540 else if (tv_dest->v_type == VAR_LIST)
2541 {
2542 // unlet a List item, index must be a number
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002543 if (check_for_number(tv_idx) == FAIL)
2544 {
2545 status = FAIL;
2546 }
2547 else
2548 {
2549 list_T *l = tv_dest->vval.v_list;
2550 long n = (long)tv_idx->vval.v_number;
2551
2552 if (l != NULL && value_check_lock(
2553 l->lv_lock, NULL, FALSE))
2554 status = FAIL;
2555 else
2556 {
2557 listitem_T *li = list_find(l, n);
2558
2559 if (li == NULL)
2560 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002561 semsg(_(e_list_index_out_of_range_nr), n);
2562 status = FAIL;
2563 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002564 else
2565 listitem_remove(l, li);
2566 }
2567 }
2568 }
2569 else
2570 {
2571 status = FAIL;
2572 semsg(_(e_cannot_index_str),
2573 vartype_name(tv_dest->v_type));
2574 }
2575
2576 clear_tv(tv_idx);
2577 clear_tv(tv_dest);
2578 ectx->ec_stack.ga_len -= 2;
2579
2580 return status;
2581}
2582
2583/*
2584 * Unlet a range of items in a list variable.
2585 */
2586 static int
2587execute_unletrange(isn_T *iptr, ectx_T *ectx)
2588{
2589 // Stack contains:
2590 // -3 index1
2591 // -2 index2
2592 // -1 dict or list
2593 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2594 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2595 typval_T *tv_dest = STACK_TV_BOT(-1);
2596 int status = OK;
2597
2598 if (tv_dest->v_type == VAR_LIST)
2599 {
2600 // indexes must be a number
2601 SOURCING_LNUM = iptr->isn_lnum;
2602 if (check_for_number(tv_idx1) == FAIL
2603 || (tv_idx2->v_type != VAR_SPECIAL
2604 && check_for_number(tv_idx2) == FAIL))
2605 {
2606 status = FAIL;
2607 }
2608 else
2609 {
2610 list_T *l = tv_dest->vval.v_list;
2611 long n1 = (long)tv_idx1->vval.v_number;
2612 long n2 = tv_idx2->v_type == VAR_SPECIAL
2613 ? 0 : (long)tv_idx2->vval.v_number;
2614 listitem_T *li;
2615
2616 li = list_find_index(l, &n1);
2617 if (li == NULL)
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002618 {
2619 semsg(_(e_list_index_out_of_range_nr),
2620 (long)tv_idx1->vval.v_number);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002621 status = FAIL;
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002622 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002623 else
2624 {
2625 if (n1 < 0)
2626 n1 = list_idx_of_item(l, li);
2627 if (n2 < 0)
2628 {
2629 listitem_T *li2 = list_find(l, n2);
2630
2631 if (li2 == NULL)
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002632 {
2633 semsg(_(e_list_index_out_of_range_nr), n2);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002634 status = FAIL;
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002635 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002636 else
2637 n2 = list_idx_of_item(l, li2);
2638 }
2639 if (status != FAIL
2640 && tv_idx2->v_type != VAR_SPECIAL
2641 && n2 < n1)
2642 {
2643 semsg(_(e_list_index_out_of_range_nr), n2);
2644 status = FAIL;
2645 }
Bram Moolenaar6b8c7ba2022-03-20 17:46:06 +00002646 if (status != FAIL)
2647 list_unlet_range(l, li, n1,
2648 tv_idx2->v_type != VAR_SPECIAL, n2);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002649 }
2650 }
2651 }
2652 else
2653 {
2654 status = FAIL;
2655 SOURCING_LNUM = iptr->isn_lnum;
2656 semsg(_(e_cannot_index_str),
2657 vartype_name(tv_dest->v_type));
2658 }
2659
2660 clear_tv(tv_idx1);
2661 clear_tv(tv_idx2);
2662 clear_tv(tv_dest);
2663 ectx->ec_stack.ga_len -= 3;
2664
2665 return status;
2666}
2667
2668/*
2669 * Top of a for loop.
2670 */
2671 static int
2672execute_for(isn_T *iptr, ectx_T *ectx)
2673{
2674 typval_T *tv;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002675 int jump = FALSE;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002676 typval_T *ltv = STACK_TV_BOT(-1);
2677 typval_T *idxtv =
Bram Moolenaarcc341812022-09-19 15:54:34 +01002678 STACK_TV_VAR(iptr->isn_arg.forloop.for_loop_idx);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002679
2680 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
2681 return FAIL;
2682 if (ltv->v_type == VAR_LIST)
2683 {
2684 list_T *list = ltv->vval.v_list;
2685
2686 // push the next item from the list
2687 ++idxtv->vval.v_number;
2688 if (list == NULL
2689 || idxtv->vval.v_number >= list->lv_len)
2690 {
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002691 jump = TRUE;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002692 }
2693 else if (list->lv_first == &range_list_item)
2694 {
2695 // non-materialized range() list
2696 tv = STACK_TV_BOT(0);
2697 tv->v_type = VAR_NUMBER;
2698 tv->v_lock = 0;
2699 tv->vval.v_number = list_find_nr(
2700 list, idxtv->vval.v_number, NULL);
2701 ++ectx->ec_stack.ga_len;
2702 }
2703 else
2704 {
2705 listitem_T *li = list_find(list,
2706 idxtv->vval.v_number);
2707
2708 copy_tv(&li->li_tv, STACK_TV_BOT(0));
2709 ++ectx->ec_stack.ga_len;
2710 }
2711 }
2712 else if (ltv->v_type == VAR_STRING)
2713 {
2714 char_u *str = ltv->vval.v_string;
2715
2716 // The index is for the last byte of the previous
2717 // character.
2718 ++idxtv->vval.v_number;
2719 if (str == NULL || str[idxtv->vval.v_number] == NUL)
2720 {
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002721 jump = TRUE;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002722 }
2723 else
2724 {
2725 int clen = mb_ptr2len(str + idxtv->vval.v_number);
2726
2727 // Push the next character from the string.
2728 tv = STACK_TV_BOT(0);
2729 tv->v_type = VAR_STRING;
2730 tv->vval.v_string = vim_strnsave(
2731 str + idxtv->vval.v_number, clen);
2732 ++ectx->ec_stack.ga_len;
2733 idxtv->vval.v_number += clen - 1;
2734 }
2735 }
2736 else if (ltv->v_type == VAR_BLOB)
2737 {
2738 blob_T *blob = ltv->vval.v_blob;
2739
2740 // When we get here the first time make a copy of the
2741 // blob, so that the iteration still works when it is
2742 // changed.
2743 if (idxtv->vval.v_number == -1 && blob != NULL)
2744 {
2745 blob_copy(blob, ltv);
2746 blob_unref(blob);
2747 blob = ltv->vval.v_blob;
2748 }
2749
2750 // The index is for the previous byte.
2751 ++idxtv->vval.v_number;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002752 if (blob == NULL || idxtv->vval.v_number >= blob_len(blob))
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002753 {
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002754 jump = TRUE;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002755 }
2756 else
2757 {
2758 // Push the next byte from the blob.
2759 tv = STACK_TV_BOT(0);
2760 tv->v_type = VAR_NUMBER;
2761 tv->vval.v_number = blob_get(blob,
2762 idxtv->vval.v_number);
2763 ++ectx->ec_stack.ga_len;
2764 }
2765 }
2766 else
2767 {
2768 semsg(_(e_for_loop_on_str_not_supported),
2769 vartype_name(ltv->v_type));
2770 return FAIL;
2771 }
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002772
2773 if (jump)
2774 {
2775 // past the end of the list/string/blob, jump to "endfor"
2776 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
2777 may_restore_cmdmod(&ectx->ec_funclocal);
2778 }
2779 else
2780 {
2781 // Store the current number of funcrefs, this may be used in
2782 // ISN_LOOPEND. The variable index is always one more than the loop
2783 // variable index.
Bram Moolenaarcc341812022-09-19 15:54:34 +01002784 tv = STACK_TV_VAR(iptr->isn_arg.forloop.for_loop_idx + 1);
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002785 tv->vval.v_number = ectx->ec_funcrefs.ga_len;
2786 }
2787
2788 return OK;
2789}
2790
2791/*
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002792 * Code for handling variables declared inside a loop and used in a closure.
2793 * This is very similar to what is done with funcstack_T. The difference is
2794 * that the funcstack_T has the scope of a function, while a loopvars_T has the
2795 * scope of the block inside a loop and each loop may have its own.
2796 */
2797
2798// Double linked list of loopvars_T in use.
2799static loopvars_T *first_loopvars = NULL;
2800
2801 static void
2802add_loopvars_to_list(loopvars_T *loopvars)
2803{
2804 // Link in list of loopvarss.
2805 if (first_loopvars != NULL)
2806 first_loopvars->lvs_prev = loopvars;
2807 loopvars->lvs_next = first_loopvars;
2808 loopvars->lvs_prev = NULL;
2809 first_loopvars = loopvars;
2810}
2811
2812 static void
2813remove_loopvars_from_list(loopvars_T *loopvars)
2814{
2815 if (loopvars->lvs_prev == NULL)
2816 first_loopvars = loopvars->lvs_next;
2817 else
2818 loopvars->lvs_prev->lvs_next = loopvars->lvs_next;
2819 if (loopvars->lvs_next != NULL)
2820 loopvars->lvs_next->lvs_prev = loopvars->lvs_prev;
2821}
2822
2823/*
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002824 * End of a for or while loop: Handle any variables used by a closure.
2825 */
2826 static int
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002827execute_endloop(isn_T *iptr, ectx_T *ectx)
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002828{
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002829 endloop_T *endloop = &iptr->isn_arg.endloop;
2830 typval_T *tv_refcount = STACK_TV_VAR(endloop->end_funcref_idx);
2831 int prev_closure_count = tv_refcount->vval.v_number;
Bram Moolenaarcc341812022-09-19 15:54:34 +01002832 int depth = endloop->end_depth;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002833 garray_T *gap = &ectx->ec_funcrefs;
2834 int closure_in_use = FALSE;
2835 loopvars_T *loopvars;
2836 typval_T *stack;
2837 int idx;
2838
Bram Moolenaarcc341812022-09-19 15:54:34 +01002839 // Check if any created closure is still being referenced and loopvars have
2840 // not been saved yet for the current depth.
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002841 for (idx = prev_closure_count; idx < gap->ga_len; ++idx)
2842 {
2843 partial_T *pt = ((partial_T **)gap->ga_data)[idx];
2844
Bram Moolenaarcc341812022-09-19 15:54:34 +01002845 if (pt->pt_refcount > 1 && pt->pt_loopvars[depth] == NULL)
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002846 {
2847 int refcount = pt->pt_refcount;
2848 int i;
2849
2850 // A Reference in a variable inside the loop doesn't count, it gets
2851 // unreferenced at the end of the loop.
2852 for (i = 0; i < endloop->end_var_count; ++i)
2853 {
2854 typval_T *stv = STACK_TV_VAR(endloop->end_var_idx + i);
2855
2856 if (stv->v_type == VAR_PARTIAL && pt == stv->vval.v_partial)
2857 --refcount;
2858 }
2859 if (refcount > 1)
2860 {
2861 closure_in_use = TRUE;
2862 break;
2863 }
2864 }
2865 }
2866
2867 // If no function reference were created since the start of the loop block
2868 // or it is no longer referenced there is nothing to do.
2869 if (!closure_in_use)
2870 return OK;
2871
2872 // A closure is using variables declared inside the loop.
2873 // Move them to the called function.
2874 loopvars = ALLOC_CLEAR_ONE(loopvars_T);
2875 if (loopvars == NULL)
2876 return FAIL;
2877
2878 loopvars->lvs_ga.ga_len = endloop->end_var_count;
2879 stack = ALLOC_CLEAR_MULT(typval_T, loopvars->lvs_ga.ga_len);
2880 loopvars->lvs_ga.ga_data = stack;
2881 if (stack == NULL)
2882 {
2883 vim_free(loopvars);
2884 return FAIL;
2885 }
2886 add_loopvars_to_list(loopvars);
2887
2888 // Move the variable values.
2889 for (idx = 0; idx < endloop->end_var_count; ++idx)
2890 {
2891 typval_T *tv = STACK_TV_VAR(endloop->end_var_idx + idx);
2892
2893 *(stack + idx) = *tv;
2894 tv->v_type = VAR_UNKNOWN;
2895 }
2896
2897 for (idx = prev_closure_count; idx < gap->ga_len; ++idx)
2898 {
2899 partial_T *pt = ((partial_T **)gap->ga_data)[idx];
2900
Bram Moolenaarcc341812022-09-19 15:54:34 +01002901 if (pt->pt_refcount > 1 && pt->pt_loopvars[depth] == NULL)
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002902 {
2903 ++loopvars->lvs_refcount;
Bram Moolenaarcc341812022-09-19 15:54:34 +01002904 pt->pt_loopvars[depth] = loopvars;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002905
Bram Moolenaarcc341812022-09-19 15:54:34 +01002906 pt->pt_outer.out_loop[depth].stack = &loopvars->lvs_ga;
2907 pt->pt_outer.out_loop[depth].var_idx -=
2908 ectx->ec_frame_idx + STACK_FRAME_SIZE + endloop->end_var_idx;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002909 }
2910 }
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002911
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002912 return OK;
2913}
2914
2915/*
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002916 * Called when a partial is freed or its reference count goes down to one. The
2917 * loopvars may be the only reference to the partials in the local variables.
2918 * Go over all of them, the funcref and can be freed if all partials
2919 * referencing the loopvars have a reference count of one.
Bram Moolenaarcc341812022-09-19 15:54:34 +01002920 * Return TRUE if it was freed.
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002921 */
Bram Moolenaarcc341812022-09-19 15:54:34 +01002922 int
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002923loopvars_check_refcount(loopvars_T *loopvars)
2924{
2925 int i;
2926 garray_T *gap = &loopvars->lvs_ga;
2927 int done = 0;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002928 typval_T *stack = gap->ga_data;
2929
Bram Moolenaarcc341812022-09-19 15:54:34 +01002930 if (loopvars->lvs_refcount > loopvars->lvs_min_refcount)
2931 return FALSE;
2932 for (i = 0; i < gap->ga_len; ++i)
2933 {
2934 typval_T *tv = ((typval_T *)gap->ga_data) + i;
2935
2936 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL
2937 && tv->vval.v_partial->pt_refcount == 1)
2938 {
2939 int depth;
2940
2941 for (depth = 0; depth < MAX_LOOP_DEPTH; ++depth)
2942 if (tv->vval.v_partial->pt_loopvars[depth] == loopvars)
2943 ++done;
2944 }
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002945 }
Bram Moolenaarcc341812022-09-19 15:54:34 +01002946 if (done != loopvars->lvs_min_refcount)
2947 return FALSE;
2948
2949 // All partials referencing the loopvars have a reference count of
2950 // one, thus the loopvars is no longer of use.
2951 stack = gap->ga_data;
2952 for (i = 0; i < gap->ga_len; ++i)
2953 clear_tv(stack + i);
2954 vim_free(stack);
2955 remove_loopvars_from_list(loopvars);
2956 vim_free(loopvars);
2957 return TRUE;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002958}
2959
2960/*
2961 * For garbage collecting: set references in all variables referenced by
2962 * all loopvars.
2963 */
2964 int
2965set_ref_in_loopvars(int copyID)
2966{
2967 loopvars_T *loopvars;
2968
2969 for (loopvars = first_loopvars; loopvars != NULL;
2970 loopvars = loopvars->lvs_next)
2971 {
2972 typval_T *stack = loopvars->lvs_ga.ga_data;
2973 int i;
2974
2975 for (i = 0; i < loopvars->lvs_ga.ga_len; ++i)
2976 if (set_ref_in_item(stack + i, copyID, NULL, NULL))
2977 return TRUE; // abort
2978 }
2979 return FALSE;
2980}
2981
2982/*
Bram Moolenaar06b77222022-01-25 15:51:56 +00002983 * Load instruction for w:/b:/g:/t: variable.
2984 * "isn_type" is used instead of "iptr->isn_type".
2985 */
2986 static int
2987load_namespace_var(ectx_T *ectx, isntype_T isn_type, isn_T *iptr)
2988{
2989 dictitem_T *di = NULL;
2990 hashtab_T *ht = NULL;
2991 char namespace;
2992
2993 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
2994 return NOTDONE;
2995 switch (isn_type)
2996 {
2997 case ISN_LOADG:
2998 ht = get_globvar_ht();
2999 namespace = 'g';
3000 break;
3001 case ISN_LOADB:
3002 ht = &curbuf->b_vars->dv_hashtab;
3003 namespace = 'b';
3004 break;
3005 case ISN_LOADW:
3006 ht = &curwin->w_vars->dv_hashtab;
3007 namespace = 'w';
3008 break;
3009 case ISN_LOADT:
3010 ht = &curtab->tp_vars->dv_hashtab;
3011 namespace = 't';
3012 break;
3013 default: // Cannot reach here
3014 return NOTDONE;
3015 }
3016 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
3017
Bram Moolenaar06b77222022-01-25 15:51:56 +00003018 if (di == NULL)
3019 {
Bram Moolenaarfe732552022-02-22 19:39:13 +00003020 if (isn_type == ISN_LOADG)
3021 {
3022 ufunc_T *ufunc = find_func(iptr->isn_arg.string, TRUE);
3023
3024 // g:Something could be a function
3025 if (ufunc != NULL)
3026 {
3027 typval_T *tv = STACK_TV_BOT(0);
3028
3029 ++ectx->ec_stack.ga_len;
3030 tv->v_type = VAR_FUNC;
3031 tv->vval.v_string = alloc(STRLEN(iptr->isn_arg.string) + 3);
3032 if (tv->vval.v_string == NULL)
3033 return FAIL;
3034 STRCPY(tv->vval.v_string, "g:");
3035 STRCPY(tv->vval.v_string + 2, iptr->isn_arg.string);
3036 return OK;
3037 }
3038 }
Bram Moolenaar06b77222022-01-25 15:51:56 +00003039 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarfe732552022-02-22 19:39:13 +00003040 if (vim_strchr(iptr->isn_arg.string, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar06b77222022-01-25 15:51:56 +00003041 // no check if the item exists in the script but
3042 // isn't exported, it is too complicated
Bram Moolenaarfe732552022-02-22 19:39:13 +00003043 semsg(_(e_item_not_found_in_script_str), iptr->isn_arg.string);
Bram Moolenaar06b77222022-01-25 15:51:56 +00003044 else
3045 semsg(_(e_undefined_variable_char_str),
Bram Moolenaarfe732552022-02-22 19:39:13 +00003046 namespace, iptr->isn_arg.string);
Bram Moolenaar06b77222022-01-25 15:51:56 +00003047 return FAIL;
3048 }
3049 else
3050 {
3051 copy_tv(&di->di_tv, STACK_TV_BOT(0));
3052 ++ectx->ec_stack.ga_len;
3053 }
3054 return OK;
3055}
3056
Bram Moolenaard0200c82023-01-28 15:19:40 +00003057
3058 static void
3059object_required_error(typval_T *tv)
3060{
3061 garray_T type_list;
3062 ga_init2(&type_list, sizeof(type_T *), 10);
3063 type_T *type = typval2type(tv, get_copyID(), &type_list, TVTT_DO_MEMBER);
3064 char *tofree = NULL;
3065 char *typename = type_name(type, &tofree);
3066 semsg(_(e_object_required_found_str), typename);
3067 vim_free(tofree);
3068 clear_type_list(&type_list);
3069}
3070
Bram Moolenaar06b77222022-01-25 15:51:56 +00003071/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02003072 * Execute instructions in execution context "ectx".
3073 * Return OK or FAIL;
3074 */
3075 static int
3076exec_instructions(ectx_T *ectx)
3077{
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003078 int ret = FAIL;
3079 int save_trylevel_at_start = ectx->ec_trylevel_at_start;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02003080 int dict_stack_len_at_start = dict_stack.ga_len;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003081
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02003082 // Start execution at the first instruction.
Bram Moolenaar4c137212021-04-19 16:48:48 +02003083 ectx->ec_iidx = 0;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01003084
Bram Moolenaarff652882021-05-16 15:24:49 +02003085 // Only catch exceptions in this instruction list.
3086 ectx->ec_trylevel_at_start = trylevel;
3087
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003088 for (;;)
3089 {
Bram Moolenaara7490192021-07-22 12:26:14 +02003090 static int breakcheck_count = 0; // using "static" makes it faster
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003091 isn_T *iptr;
Bram Moolenaara7490192021-07-22 12:26:14 +02003092 typval_T *tv;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003093
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003094 if (unlikely(++breakcheck_count >= 100))
Bram Moolenaar270d0382020-05-15 21:42:53 +02003095 {
3096 line_breakcheck();
3097 breakcheck_count = 0;
3098 }
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003099 if (unlikely(got_int))
Bram Moolenaar20431c92020-03-20 18:39:46 +01003100 {
3101 // Turn CTRL-C into an exception.
3102 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +01003103 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003104 goto theend;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003105 did_throw = TRUE;
3106 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003107
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003108 if (unlikely(did_emsg && msg_list != NULL && *msg_list != NULL))
Bram Moolenaara26b9702020-04-18 19:53:28 +02003109 {
3110 // Turn an error message into an exception.
3111 did_emsg = FALSE;
3112 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003113 goto theend;
Bram Moolenaara26b9702020-04-18 19:53:28 +02003114 did_throw = TRUE;
3115 *msg_list = NULL;
Bram Moolenaar3ef2e412023-04-30 18:50:48 +01003116
3117 // This exception was not caught (yet).
3118 garray_T *trystack = &ectx->ec_trystack;
3119 if (trystack->ga_len > 0)
3120 {
3121 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
3122 + trystack->ga_len - 1;
3123 if (trycmd->tcd_frame_idx == ectx->ec_frame_idx)
3124 trycmd->tcd_caught = FALSE;
3125 }
Bram Moolenaara26b9702020-04-18 19:53:28 +02003126 }
3127
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003128 if (unlikely(did_throw))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003129 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003130 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003131 trycmd_T *trycmd = NULL;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003132 int index = trystack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003133
3134 // An exception jumps to the first catch, finally, or returns from
3135 // the current function.
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003136 while (index > 0)
3137 {
3138 trycmd = ((trycmd_T *)trystack->ga_data) + index - 1;
Bram Moolenaar3ef2e412023-04-30 18:50:48 +01003139 // 1. after :try and before :catch - jump to first :catch
3140 // 2. in :catch block - jump to :finally
3141 // 3. in :catch block and no finally - jump to :endtry
3142 if (!trycmd->tcd_in_catch || trycmd->tcd_finally_idx != 0
3143 || trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003144 break;
3145 // In the catch and finally block of this try we have to go up
3146 // one level.
3147 --index;
3148 trycmd = NULL;
3149 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003150 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003151 {
Bram Moolenaar834193a2021-06-30 20:39:15 +02003152 if (trycmd->tcd_in_catch)
3153 {
Bram Moolenaar3ef2e412023-04-30 18:50:48 +01003154 if (trycmd->tcd_finally_idx > 0)
3155 {
3156 // exception inside ":catch", jump to ":finally" once
3157 ectx->ec_iidx = trycmd->tcd_finally_idx;
3158 trycmd->tcd_finally_idx = 0;
3159 }
3160 else
3161 {
3162 // exception inside ":catch" or ":finally", jump to
3163 // ":endtry"
3164 ectx->ec_iidx = trycmd->tcd_endtry_idx;
3165 }
Bram Moolenaar834193a2021-06-30 20:39:15 +02003166 }
3167 else
Bram Moolenaar3ef2e412023-04-30 18:50:48 +01003168 {
Bram Moolenaar834193a2021-06-30 20:39:15 +02003169 // jump to first ":catch"
3170 ectx->ec_iidx = trycmd->tcd_catch_idx;
Bram Moolenaar3ef2e412023-04-30 18:50:48 +01003171 trycmd->tcd_in_catch = TRUE;
3172 }
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003173 did_throw = FALSE; // don't come back here until :endtry
3174 trycmd->tcd_did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003175 }
3176 else
3177 {
Bram Moolenaar3ef2e412023-04-30 18:50:48 +01003178 // Not inside try or need to return from current function.
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02003179 // Push a dummy return value.
Bram Moolenaar35578162021-08-02 19:10:38 +02003180 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003181 goto theend;
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02003182 tv = STACK_TV_BOT(0);
3183 tv->v_type = VAR_NUMBER;
3184 tv->vval.v_number = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003185 ++ectx->ec_stack.ga_len;
3186 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003187 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02003188 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +01003189 need_rethrow = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003190 if (handle_closure_in_use(ectx, FALSE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003191 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003192 goto done;
3193 }
3194
Bram Moolenaar4c137212021-04-19 16:48:48 +02003195 if (func_return(ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003196 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003197 }
3198 continue;
3199 }
3200
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003201 /*
3202 * Big switch on the instruction. Most compilers will be turning this
3203 * into an efficient lookup table, since the "case" values are an enum
3204 * with sequential numbers. It may look ugly, but it should be the
3205 * most efficient way.
3206 */
Bram Moolenaar4c137212021-04-19 16:48:48 +02003207 iptr = &ectx->ec_instr[ectx->ec_iidx++];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003208 switch (iptr->isn_type)
3209 {
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00003210 // Constructor, first instruction in a new() method.
Bram Moolenaar00b28d62022-12-08 15:32:33 +00003211 case ISN_CONSTRUCT:
3212 // "this" is always the local variable at index zero
3213 tv = STACK_TV_VAR(0);
3214 tv->v_type = VAR_OBJECT;
3215 tv->vval.v_object = alloc_clear(
3216 iptr->isn_arg.construct.construct_size);
3217 tv->vval.v_object->obj_class =
3218 iptr->isn_arg.construct.construct_class;
Bram Moolenaard28d7b92022-12-08 20:42:00 +00003219 ++tv->vval.v_object->obj_class->class_refcount;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00003220 tv->vval.v_object->obj_refcount = 1;
Bram Moolenaard28d7b92022-12-08 20:42:00 +00003221 object_created(tv->vval.v_object);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00003222 break;
3223
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003224 // execute Ex command line
3225 case ISN_EXEC:
Ernie Raelee865f32023-09-29 19:53:55 +02003226 if (exec_command(iptr, iptr->isn_arg.string) == FAIL)
Bram Moolenaaraacc9662021-08-13 19:40:51 +02003227 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003228 break;
3229
Bram Moolenaar20677332021-06-06 17:02:53 +02003230 // execute Ex command line split at NL characters.
3231 case ISN_EXEC_SPLIT:
3232 {
3233 source_cookie_T cookie;
Bram Moolenaar518df272021-06-06 17:34:13 +02003234 char_u *line;
Bram Moolenaar20677332021-06-06 17:02:53 +02003235
3236 SOURCING_LNUM = iptr->isn_lnum;
3237 CLEAR_FIELD(cookie);
3238 cookie.sourcing_lnum = iptr->isn_lnum - 1;
3239 cookie.nextline = iptr->isn_arg.string;
Bram Moolenaar518df272021-06-06 17:34:13 +02003240 line = get_split_sourceline(0, &cookie, 0, 0);
3241 if (do_cmdline(line,
Bram Moolenaar20677332021-06-06 17:02:53 +02003242 get_split_sourceline, &cookie,
3243 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED)
3244 == FAIL
3245 || did_emsg)
Bram Moolenaar518df272021-06-06 17:34:13 +02003246 {
3247 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02003248 goto on_error;
Bram Moolenaar518df272021-06-06 17:34:13 +02003249 }
3250 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02003251 }
3252 break;
3253
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00003254 // execute Ex command line that is only a range
3255 case ISN_EXECRANGE:
3256 {
3257 exarg_T ea;
3258 char *error = NULL;
3259
3260 CLEAR_FIELD(ea);
3261 ea.cmdidx = CMD_SIZE;
3262 ea.addr_type = ADDR_LINES;
3263 ea.cmd = iptr->isn_arg.string;
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00003264 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00003265 parse_cmd_address(&ea, &error, FALSE);
Bram Moolenaar01a4dcb2021-12-04 13:15:10 +00003266 if (ea.cmd == NULL)
3267 goto on_error;
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00003268 // error is always NULL when using ADDR_LINES
3269 error = ex_range_without_command(&ea);
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00003270 if (error != NULL)
3271 {
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00003272 emsg(error);
3273 goto on_error;
3274 }
3275 }
3276 break;
3277
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02003278 // Evaluate an expression with legacy syntax, push it onto the
3279 // stack.
3280 case ISN_LEGACY_EVAL:
3281 {
3282 char_u *arg = iptr->isn_arg.string;
3283 int res;
3284 int save_flags = cmdmod.cmod_flags;
3285
Bram Moolenaar35578162021-08-02 19:10:38 +02003286 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003287 goto theend;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02003288 tv = STACK_TV_BOT(0);
3289 init_tv(tv);
3290 cmdmod.cmod_flags |= CMOD_LEGACY;
3291 res = eval0(arg, tv, NULL, &EVALARG_EVALUATE);
3292 cmdmod.cmod_flags = save_flags;
3293 if (res == FAIL)
3294 goto on_error;
3295 ++ectx->ec_stack.ga_len;
3296 }
3297 break;
3298
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003299 // push typeval VAR_INSTR with instructions to be executed
3300 case ISN_INSTR:
3301 {
Bram Moolenaar35578162021-08-02 19:10:38 +02003302 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003303 goto theend;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003304 tv = STACK_TV_BOT(0);
3305 tv->vval.v_instr = ALLOC_ONE(instr_T);
3306 if (tv->vval.v_instr == NULL)
3307 goto on_error;
3308 ++ectx->ec_stack.ga_len;
3309
3310 tv->v_type = VAR_INSTR;
3311 tv->vval.v_instr->instr_ectx = ectx;
3312 tv->vval.v_instr->instr_instr = iptr->isn_arg.instr;
3313 }
3314 break;
3315
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003316 case ISN_SOURCE:
3317 {
Bram Moolenaar89445512022-04-14 12:58:23 +01003318 int notused;
LemonBoy77142312022-04-15 20:50:46 +01003319
Bram Moolenaar89445512022-04-14 12:58:23 +01003320 SOURCING_LNUM = iptr->isn_lnum;
3321 if (may_load_script((int)iptr->isn_arg.number, &notused)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003322 == FAIL)
Bram Moolenaar89445512022-04-14 12:58:23 +01003323 goto on_error;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003324 }
3325 break;
3326
Bram Moolenaar4c137212021-04-19 16:48:48 +02003327 // execute :substitute with an expression
3328 case ISN_SUBSTITUTE:
3329 {
3330 subs_T *subs = &iptr->isn_arg.subs;
3331 source_cookie_T cookie;
3332 struct subs_expr_S *save_instr = substitute_instr;
3333 struct subs_expr_S subs_instr;
3334 int res;
3335
3336 subs_instr.subs_ectx = ectx;
3337 subs_instr.subs_instr = subs->subs_instr;
3338 subs_instr.subs_status = OK;
3339 substitute_instr = &subs_instr;
3340
3341 SOURCING_LNUM = iptr->isn_lnum;
3342 // This is very much like ISN_EXEC
3343 CLEAR_FIELD(cookie);
3344 cookie.sourcing_lnum = iptr->isn_lnum - 1;
3345 res = do_cmdline(subs->subs_cmd,
3346 getsourceline, &cookie,
3347 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
3348 substitute_instr = save_instr;
3349
3350 if (res == FAIL || did_emsg
3351 || subs_instr.subs_status == FAIL)
3352 goto on_error;
3353 }
3354 break;
3355
3356 case ISN_FINISH:
3357 goto done;
3358
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02003359 case ISN_REDIRSTART:
3360 // create a dummy entry for var_redir_str()
3361 if (alloc_redir_lval() == FAIL)
3362 goto on_error;
3363
3364 // The output is stored in growarray "redir_ga" until
3365 // redirection ends.
3366 init_redir_ga();
3367 redir_vname = 1;
3368 break;
3369
3370 case ISN_REDIREND:
3371 {
3372 char_u *res = get_clear_redir_ga();
3373
3374 // End redirection, put redirected text on the stack.
3375 clear_redir_lval();
3376 redir_vname = 0;
3377
Bram Moolenaar35578162021-08-02 19:10:38 +02003378 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02003379 {
3380 vim_free(res);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003381 goto theend;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02003382 }
3383 tv = STACK_TV_BOT(0);
3384 tv->v_type = VAR_STRING;
3385 tv->vval.v_string = res;
3386 ++ectx->ec_stack.ga_len;
3387 }
3388 break;
3389
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003390 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02003391#ifdef FEAT_QUICKFIX
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003392 force_abort = TRUE;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003393 if (trigger_cexpr_autocmd(iptr->isn_arg.number) == FAIL)
3394 goto on_error;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003395 force_abort = FALSE;
Bram Moolenaarb7c97812021-05-05 22:51:39 +02003396#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003397 break;
3398
3399 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02003400#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003401 {
3402 exarg_T ea;
3403 int res;
3404
3405 CLEAR_FIELD(ea);
3406 ea.cmdidx = iptr->isn_arg.cexpr.cexpr_ref->cer_cmdidx;
3407 ea.forceit = iptr->isn_arg.cexpr.cexpr_ref->cer_forceit;
3408 ea.cmdlinep = &iptr->isn_arg.cexpr.cexpr_ref->cer_cmdline;
3409 --ectx->ec_stack.ga_len;
3410 tv = STACK_TV_BOT(0);
Bram Moolenaarbd683e32022-07-18 17:49:03 +01003411 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003412 res = cexpr_core(&ea, tv);
3413 clear_tv(tv);
3414 if (res == FAIL)
3415 goto on_error;
3416 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02003417#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003418 break;
3419
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003420 // execute Ex command from pieces on the stack
3421 case ISN_EXECCONCAT:
3422 {
3423 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +02003424 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003425 int pass;
3426 int i;
3427 char_u *cmd = NULL;
3428 char_u *str;
3429
3430 for (pass = 1; pass <= 2; ++pass)
3431 {
3432 for (i = 0; i < count; ++i)
3433 {
3434 tv = STACK_TV_BOT(i - count);
3435 str = tv->vval.v_string;
3436 if (str != NULL && *str != NUL)
3437 {
3438 if (pass == 2)
3439 STRCPY(cmd + len, str);
3440 len += STRLEN(str);
3441 }
3442 if (pass == 2)
3443 clear_tv(tv);
3444 }
3445 if (pass == 1)
3446 {
3447 cmd = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003448 if (unlikely(cmd == NULL))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003449 goto theend;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003450 len = 0;
3451 }
3452 }
3453
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02003454 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003455 do_cmdline_cmd(cmd);
3456 vim_free(cmd);
3457 }
3458 break;
3459
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003460 // execute :echo {string} ...
3461 case ISN_ECHO:
3462 {
3463 int count = iptr->isn_arg.echo.echo_count;
3464 int atstart = TRUE;
3465 int needclr = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003466 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003467
3468 for (idx = 0; idx < count; ++idx)
3469 {
3470 tv = STACK_TV_BOT(idx - count);
3471 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
3472 &atstart, &needclr);
3473 clear_tv(tv);
3474 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01003475 if (needclr)
3476 msg_clr_eos();
Bram Moolenaar4c137212021-04-19 16:48:48 +02003477 ectx->ec_stack.ga_len -= count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003478 }
3479 break;
3480
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003481 // :execute {string} ...
3482 // :echomsg {string} ...
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01003483 // :echowindow {string} ...
Bram Moolenaar7de62622021-08-07 15:05:47 +02003484 // :echoconsole {string} ...
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003485 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01003486 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003487 case ISN_ECHOMSG:
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01003488 case ISN_ECHOWINDOW:
Bram Moolenaar7de62622021-08-07 15:05:47 +02003489 case ISN_ECHOCONSOLE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003490 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01003491 {
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01003492 int count;
Bram Moolenaarad39c092020-02-26 18:23:43 +01003493 garray_T ga;
3494 char_u buf[NUMBUFLEN];
3495 char_u *p;
3496 int len;
3497 int failed = FALSE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003498 int idx;
Bram Moolenaarad39c092020-02-26 18:23:43 +01003499
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01003500 if (iptr->isn_type == ISN_ECHOWINDOW)
3501 count = iptr->isn_arg.echowin.ewin_count;
3502 else
3503 count = iptr->isn_arg.number;
Bram Moolenaarad39c092020-02-26 18:23:43 +01003504 ga_init2(&ga, 1, 80);
3505 for (idx = 0; idx < count; ++idx)
3506 {
3507 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003508 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01003509 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003510 if (tv->v_type == VAR_CHANNEL
3511 || tv->v_type == VAR_JOB)
3512 {
3513 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar68db9962021-05-09 23:19:22 +02003514 semsg(_(e_using_invalid_value_as_string_str),
3515 vartype_name(tv->v_type));
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003516 break;
3517 }
3518 else
3519 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01003520 }
3521 else
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003522 p = tv_stringify(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01003523
3524 len = (int)STRLEN(p);
Bram Moolenaar35578162021-08-02 19:10:38 +02003525 if (GA_GROW_FAILS(&ga, len + 2))
Bram Moolenaarad39c092020-02-26 18:23:43 +01003526 failed = TRUE;
3527 else
3528 {
3529 if (ga.ga_len > 0)
3530 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
3531 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
3532 ga.ga_len += len;
3533 }
3534 clear_tv(tv);
3535 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003536 ectx->ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003537 if (failed)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003538 {
3539 ga_clear(&ga);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003540 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003541 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01003542
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003543 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003544 {
3545 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02003546 {
3547 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003548 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01003549 if (did_emsg)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003550 {
3551 ga_clear(&ga);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01003552 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003553 }
Bram Moolenaar430deb12020-08-23 16:29:11 +02003554 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003555 else
3556 {
3557 msg_sb_eol();
3558 if (iptr->isn_type == ISN_ECHOMSG)
3559 {
3560 msg_attr(ga.ga_data, echo_attr);
3561 out_flush();
3562 }
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01003563#ifdef HAS_MESSAGE_WINDOW
3564 else if (iptr->isn_type == ISN_ECHOWINDOW)
3565 {
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01003566 start_echowindow(
3567 iptr->isn_arg.echowin.ewin_time);
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01003568 msg_attr(ga.ga_data, echo_attr);
3569 end_echowindow();
3570 }
3571#endif
Bram Moolenaar7de62622021-08-07 15:05:47 +02003572 else if (iptr->isn_type == ISN_ECHOCONSOLE)
3573 {
3574 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data),
3575 TRUE);
3576 ui_write((char_u *)"\r\n", 2, TRUE);
3577 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003578 else
3579 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003580 SOURCING_LNUM = iptr->isn_lnum;
3581 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003582 }
3583 }
3584 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01003585 ga_clear(&ga);
3586 }
3587 break;
3588
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003589 // load local variable or argument
3590 case ISN_LOAD:
Bram Moolenaar35578162021-08-02 19:10:38 +02003591 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003592 goto theend;
Bram Moolenaar2ed57ac2023-04-01 22:05:38 +01003593 tv = STACK_TV_VAR(iptr->isn_arg.number);
3594 if (tv->v_type == VAR_UNKNOWN)
3595 {
3596 // missing argument or default value v:none
3597 STACK_TV_BOT(0)->v_type = VAR_SPECIAL;
3598 STACK_TV_BOT(0)->vval.v_number = VVAL_NONE;
3599 }
3600 else
3601 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003602 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003603 break;
3604
3605 // load v: variable
3606 case ISN_LOADV:
Bram Moolenaar35578162021-08-02 19:10:38 +02003607 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003608 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003609 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003610 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003611 break;
3612
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003613 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003614 case ISN_LOADSCRIPT:
3615 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01003616 scriptref_T *sref = iptr->isn_arg.script.scriptref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003617 svar_T *sv;
3618
Bram Moolenaar6db660b2021-08-01 14:08:54 +02003619 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003620 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003621 goto theend;
Bram Moolenaar859cc212022-03-28 15:22:35 +01003622 allocate_if_null(sv);
Bram Moolenaar35578162021-08-02 19:10:38 +02003623 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003624 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003625 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003626 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003627 }
3628 break;
3629
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003630 // load s: variable in old script or autoload import
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003631 case ISN_LOADS:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003632 case ISN_LOADEXPORT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003633 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003634 int sid = iptr->isn_arg.loadstore.ls_sid;
3635 hashtab_T *ht = &SCRIPT_VARS(sid);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003636 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003637 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003638
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003639 if (di == NULL)
3640 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003641 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003642 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003643 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003644 }
3645 else
3646 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003647 if (iptr->isn_type == ISN_LOADEXPORT)
3648 {
3649 int idx = get_script_item_idx(sid, name, 0,
3650 NULL, NULL);
3651 svar_T *sv;
3652
3653 if (idx >= 0)
3654 {
3655 sv = ((svar_T *)SCRIPT_ITEM(sid)
3656 ->sn_var_vals.ga_data) + idx;
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003657 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003658 {
3659 SOURCING_LNUM = iptr->isn_lnum;
3660 semsg(_(e_item_not_exported_in_script_str),
3661 name);
3662 goto on_error;
3663 }
3664 }
3665 }
Bram Moolenaar35578162021-08-02 19:10:38 +02003666 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003667 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003668 copy_tv(&di->di_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003669 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003670 }
3671 }
3672 break;
3673
Bram Moolenaard3aac292020-04-19 14:32:17 +02003674 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003675 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02003676 case ISN_LOADB:
3677 case ISN_LOADW:
3678 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003679 {
Bram Moolenaar06b77222022-01-25 15:51:56 +00003680 int res = load_namespace_var(ectx, iptr->isn_type, iptr);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003681
Bram Moolenaar06b77222022-01-25 15:51:56 +00003682 if (res == NOTDONE)
Bram Moolenaarcbbc48f2022-01-18 12:58:28 +00003683 goto theend;
Bram Moolenaar06b77222022-01-25 15:51:56 +00003684 if (res == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003685 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003686 }
Bram Moolenaar06b77222022-01-25 15:51:56 +00003687
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003688 break;
3689
Bram Moolenaar03290b82020-12-19 16:30:44 +01003690 // load autoload variable
3691 case ISN_LOADAUTO:
3692 {
3693 char_u *name = iptr->isn_arg.string;
3694
Bram Moolenaar35578162021-08-02 19:10:38 +02003695 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003696 goto theend;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003697 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003698 if (eval_variable(name, (int)STRLEN(name), 0,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003699 STACK_TV_BOT(0), NULL, EVAL_VAR_VERBOSE) == FAIL)
Bram Moolenaar03290b82020-12-19 16:30:44 +01003700 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003701 ++ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003702 }
3703 break;
3704
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003705 // load g:/b:/w:/t: namespace
3706 case ISN_LOADGDICT:
3707 case ISN_LOADBDICT:
3708 case ISN_LOADWDICT:
3709 case ISN_LOADTDICT:
3710 {
3711 dict_T *d = NULL;
3712
3713 switch (iptr->isn_type)
3714 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02003715 case ISN_LOADGDICT: d = get_globvar_dict(); break;
3716 case ISN_LOADBDICT: d = curbuf->b_vars; break;
3717 case ISN_LOADWDICT: d = curwin->w_vars; break;
3718 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003719 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003720 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003721 }
Bram Moolenaar35578162021-08-02 19:10:38 +02003722 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003723 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003724 tv = STACK_TV_BOT(0);
3725 tv->v_type = VAR_DICT;
3726 tv->v_lock = 0;
3727 tv->vval.v_dict = d;
Bram Moolenaar1bd3cb22021-02-24 12:27:31 +01003728 ++d->dv_refcount;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003729 ++ectx->ec_stack.ga_len;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003730 }
3731 break;
3732
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003733 // load &option
3734 case ISN_LOADOPT:
3735 {
3736 typval_T optval;
3737 char_u *name = iptr->isn_arg.string;
3738
Bram Moolenaara8c17702020-04-01 21:17:24 +02003739 // This is not expected to fail, name is checked during
3740 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar35578162021-08-02 19:10:38 +02003741 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003742 goto theend;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003743 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003744 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003745 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003746 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003747 }
3748 break;
3749
3750 // load $ENV
3751 case ISN_LOADENV:
3752 {
3753 typval_T optval;
3754 char_u *name = iptr->isn_arg.string;
3755
Bram Moolenaar35578162021-08-02 19:10:38 +02003756 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003757 goto theend;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003758 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003759 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003760 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003761 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003762 }
3763 break;
3764
3765 // load @register
3766 case ISN_LOADREG:
Bram Moolenaar35578162021-08-02 19:10:38 +02003767 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003768 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003769 tv = STACK_TV_BOT(0);
3770 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003771 tv->v_lock = 0;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02003772 // This may result in NULL, which should be equivalent to an
3773 // empty string.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003774 tv->vval.v_string = get_reg_contents(
3775 iptr->isn_arg.number, GREG_EXPR_SRC);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003776 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003777 break;
3778
3779 // store local variable
3780 case ISN_STORE:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003781 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003782 tv = STACK_TV_VAR(iptr->isn_arg.number);
3783 clear_tv(tv);
3784 *tv = *STACK_TV_BOT(0);
3785 break;
3786
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003787 // store s: variable in old script or autoload import
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003788 case ISN_STORES:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003789 case ISN_STOREEXPORT:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003790 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003791 int sid = iptr->isn_arg.loadstore.ls_sid;
3792 hashtab_T *ht = &SCRIPT_VARS(sid);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003793 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003794 dictitem_T *di = find_var_in_ht(ht, 0,
3795 iptr->isn_type == ISN_STORES
3796 ? name + 2 : name, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003797
Bram Moolenaar4c137212021-04-19 16:48:48 +02003798 --ectx->ec_stack.ga_len;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003799 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003800 if (di == NULL)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003801 {
3802 if (iptr->isn_type == ISN_STOREEXPORT)
3803 {
3804 semsg(_(e_undefined_variable_str), name);
Bram Moolenaard1d26842022-03-31 10:13:47 +01003805 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003806 goto on_error;
3807 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003808 store_var(name, STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003809 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003810 else
3811 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003812 if (iptr->isn_type == ISN_STOREEXPORT)
3813 {
3814 int idx = get_script_item_idx(sid, name, 0,
3815 NULL, NULL);
3816
Bram Moolenaar06651632022-04-27 17:54:25 +01003817 // can this ever fail?
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003818 if (idx >= 0)
3819 {
3820 svar_T *sv = ((svar_T *)SCRIPT_ITEM(sid)
3821 ->sn_var_vals.ga_data) + idx;
3822
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003823 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003824 {
3825 semsg(_(e_item_not_exported_in_script_str),
3826 name);
Bram Moolenaard1d26842022-03-31 10:13:47 +01003827 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003828 goto on_error;
3829 }
3830 }
3831 }
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02003832 if (var_check_permission(di, name) == FAIL)
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003833 {
3834 clear_tv(STACK_TV_BOT(0));
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02003835 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003836 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003837 clear_tv(&di->di_tv);
3838 di->di_tv = *STACK_TV_BOT(0);
3839 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003840 }
3841 break;
3842
3843 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003844 case ISN_STORESCRIPT:
3845 {
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003846 scriptref_T *sref = iptr->isn_arg.script.scriptref;
3847 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003848
Bram Moolenaar6db660b2021-08-01 14:08:54 +02003849 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003850 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003851 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003852 --ectx->ec_stack.ga_len;
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003853
3854 // "const" and "final" are checked at compile time, locking
3855 // the value needs to be checked here.
3856 SOURCING_LNUM = iptr->isn_lnum;
3857 if (value_check_lock(sv->sv_tv->v_lock, sv->sv_name, FALSE))
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003858 {
3859 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003860 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003861 }
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003862
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003863 clear_tv(sv->sv_tv);
3864 *sv->sv_tv = *STACK_TV_BOT(0);
3865 }
3866 break;
3867
3868 // store option
3869 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003870 case ISN_STOREFUNCOPT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003871 {
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003872 char_u *opt_name = iptr->isn_arg.storeopt.so_name;
3873 int opt_flags = iptr->isn_arg.storeopt.so_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003874 long n = 0;
3875 char_u *s = NULL;
3876 char *msg;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003877 char_u numbuf[NUMBUFLEN];
3878 char_u *tofree = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003879
Bram Moolenaar4c137212021-04-19 16:48:48 +02003880 --ectx->ec_stack.ga_len;
LemonBoy32f34612023-09-02 21:52:05 +02003881 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003882 tv = STACK_TV_BOT(0);
3883 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01003884 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003885 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01003886 if (s == NULL)
3887 s = (char_u *)"";
3888 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003889 else if (iptr->isn_type == ISN_STOREFUNCOPT)
3890 {
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003891 // If the option can be set to a function reference or
3892 // a lambda and the passed value is a function
3893 // reference, then convert it to the name (string) of
3894 // the function reference.
3895 s = tv2string(tv, &tofree, numbuf, 0);
3896 if (s == NULL || *s == NUL)
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003897 {
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003898 // cannot happen?
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003899 clear_tv(tv);
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003900 vim_free(tofree);
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003901 goto on_error;
3902 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003903 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003904 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003905 // must be VAR_NUMBER, CHECKTYPE makes sure
3906 n = tv->vval.v_number;
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003907 msg = set_option_value(opt_name, n, s, opt_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02003908 clear_tv(tv);
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003909 vim_free(tofree);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003910 if (msg != NULL)
3911 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003912 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003913 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02003914 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003915 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003916 }
3917 break;
3918
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003919 // store $ENV
3920 case ISN_STOREENV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003921 --ectx->ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003922 tv = STACK_TV_BOT(0);
3923 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
3924 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003925 break;
3926
3927 // store @r
3928 case ISN_STOREREG:
3929 {
3930 int reg = iptr->isn_arg.number;
3931
Bram Moolenaar4c137212021-04-19 16:48:48 +02003932 --ectx->ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01003933 tv = STACK_TV_BOT(0);
Bram Moolenaar74f4a962021-06-17 21:03:07 +02003934 write_reg_contents(reg, tv_get_string(tv), -1, FALSE);
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01003935 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003936 }
3937 break;
3938
3939 // store v: variable
3940 case ISN_STOREV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003941 --ectx->ec_stack.ga_len;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003942 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
3943 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003944 // should not happen, type is checked when compiling
3945 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003946 break;
3947
Bram Moolenaard3aac292020-04-19 14:32:17 +02003948 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003949 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02003950 case ISN_STOREB:
3951 case ISN_STOREW:
3952 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003953 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003954 dictitem_T *di;
3955 hashtab_T *ht;
3956 char_u *name = iptr->isn_arg.string + 2;
3957
Bram Moolenaard3aac292020-04-19 14:32:17 +02003958 switch (iptr->isn_type)
3959 {
3960 case ISN_STOREG:
3961 ht = get_globvar_ht();
3962 break;
3963 case ISN_STOREB:
3964 ht = &curbuf->b_vars->dv_hashtab;
3965 break;
3966 case ISN_STOREW:
3967 ht = &curwin->w_vars->dv_hashtab;
3968 break;
3969 case ISN_STORET:
3970 ht = &curtab->tp_vars->dv_hashtab;
3971 break;
3972 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003973 goto theend;
Bram Moolenaard3aac292020-04-19 14:32:17 +02003974 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003975
Bram Moolenaar4c137212021-04-19 16:48:48 +02003976 --ectx->ec_stack.ga_len;
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003977 di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003978 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003979 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003980 else
3981 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003982 SOURCING_LNUM = iptr->isn_lnum;
3983 if (var_check_permission(di, name) == FAIL)
3984 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003985 clear_tv(&di->di_tv);
3986 di->di_tv = *STACK_TV_BOT(0);
3987 }
3988 }
3989 break;
3990
Bram Moolenaar03290b82020-12-19 16:30:44 +01003991 // store an autoload variable
3992 case ISN_STOREAUTO:
3993 SOURCING_LNUM = iptr->isn_lnum;
3994 set_var(iptr->isn_arg.string, STACK_TV_BOT(-1), TRUE);
3995 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003996 --ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003997 break;
3998
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003999 // store number in local variable
4000 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01004001 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004002 clear_tv(tv);
4003 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01004004 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004005 break;
4006
Bram Moolenaar590162c2022-12-24 21:24:06 +00004007 // Store a value in a list, dict, blob or object variable.
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01004008 case ISN_STOREINDEX:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004009 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00004010 int res = execute_storeindex(iptr, ectx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004011
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00004012 if (res == FAIL)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004013 goto on_error;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00004014 if (res == NOTDONE)
4015 goto theend;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004016 }
4017 break;
4018
Bram Moolenaarea5c8982022-02-17 14:42:02 +00004019 // store value in list or blob range
Bram Moolenaar68452172021-04-12 21:21:02 +02004020 case ISN_STORERANGE:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00004021 if (execute_storerange(iptr, ectx) == FAIL)
4022 goto on_error;
Bram Moolenaar68452172021-04-12 21:21:02 +02004023 break;
4024
Bram Moolenaard505d172022-12-18 21:42:55 +00004025 case ISN_LOAD_CLASSMEMBER:
4026 {
4027 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
4028 goto theend;
4029 classmember_T *cm = &iptr->isn_arg.classmember;
Bram Moolenaar4e2406c2023-06-24 19:22:21 +01004030 copy_tv(cm->cm_class->class_members_tv + cm->cm_idx,
4031 STACK_TV_BOT(0));
Bram Moolenaard505d172022-12-18 21:42:55 +00004032 ++ectx->ec_stack.ga_len;
4033 }
4034 break;
4035
4036 case ISN_STORE_CLASSMEMBER:
4037 {
4038 classmember_T *cm = &iptr->isn_arg.classmember;
4039 tv = &cm->cm_class->class_members_tv[cm->cm_idx];
4040 clear_tv(tv);
4041 *tv = *STACK_TV_BOT(-1);
4042 --ectx->ec_stack.ga_len;
4043 }
4044 break;
4045
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01004046 // Load or store variable or argument from outer scope.
Bram Moolenaar0186e582021-01-10 18:33:11 +01004047 case ISN_LOADOUTER:
4048 case ISN_STOREOUTER:
4049 {
4050 int depth = iptr->isn_arg.outer.outer_depth;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004051 outer_T *outer = ectx->ec_outer_ref == NULL ? NULL
4052 : ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +01004053
4054 while (depth > 1 && outer != NULL)
4055 {
4056 outer = outer->out_up;
4057 --depth;
4058 }
4059 if (outer == NULL)
4060 {
4061 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar69c76172021-12-02 16:38:52 +00004062 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx
4063 || ectx->ec_outer_ref == NULL)
4064 // Possibly :def function called from legacy
4065 // context.
4066 emsg(_(e_closure_called_from_invalid_context));
4067 else
4068 iemsg("LOADOUTER depth more than scope levels");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004069 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01004070 }
Bram Moolenaarcc341812022-09-19 15:54:34 +01004071 if (depth < 0)
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01004072 // Variable declared in loop. May be copied if the
4073 // loop block has already ended.
Bram Moolenaarcc341812022-09-19 15:54:34 +01004074 tv = ((typval_T *)outer->out_loop[-depth - 1]
4075 .stack->ga_data)
4076 + outer->out_loop[-depth - 1].var_idx
4077 + iptr->isn_arg.outer.outer_idx;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004078 else
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01004079 // Variable declared in a function. May be copied if
4080 // the function has already returned.
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004081 tv = ((typval_T *)outer->out_stack->ga_data)
4082 + outer->out_frame_idx + STACK_FRAME_SIZE
4083 + iptr->isn_arg.outer.outer_idx;
Bram Moolenaar0186e582021-01-10 18:33:11 +01004084 if (iptr->isn_type == ISN_LOADOUTER)
4085 {
Bram Moolenaar35578162021-08-02 19:10:38 +02004086 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004087 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01004088 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02004089 ++ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01004090 }
4091 else
4092 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004093 --ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01004094 clear_tv(tv);
4095 *tv = *STACK_TV_BOT(0);
4096 }
4097 }
4098 break;
4099
Bram Moolenaar752fc692021-01-04 21:57:11 +01004100 // unlet item in list or dict variable
4101 case ISN_UNLETINDEX:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00004102 if (execute_unletindex(iptr, ectx) == FAIL)
4103 goto on_error;
Bram Moolenaar752fc692021-01-04 21:57:11 +01004104 break;
4105
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01004106 // unlet range of items in list variable
4107 case ISN_UNLETRANGE:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00004108 if (execute_unletrange(iptr, ectx) == FAIL)
4109 goto on_error;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01004110 break;
4111
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004112 // push constant
4113 case ISN_PUSHNR:
4114 case ISN_PUSHBOOL:
4115 case ISN_PUSHSPEC:
4116 case ISN_PUSHF:
4117 case ISN_PUSHS:
4118 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004119 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004120 case ISN_PUSHCHANNEL:
4121 case ISN_PUSHJOB:
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00004122 case ISN_PUSHOBJ:
4123 case ISN_PUSHCLASS:
Bram Moolenaar35578162021-08-02 19:10:38 +02004124 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004125 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004126 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02004127 tv->v_lock = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004128 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004129 switch (iptr->isn_type)
4130 {
4131 case ISN_PUSHNR:
4132 tv->v_type = VAR_NUMBER;
4133 tv->vval.v_number = iptr->isn_arg.number;
4134 break;
4135 case ISN_PUSHBOOL:
4136 tv->v_type = VAR_BOOL;
4137 tv->vval.v_number = iptr->isn_arg.number;
4138 break;
4139 case ISN_PUSHSPEC:
4140 tv->v_type = VAR_SPECIAL;
4141 tv->vval.v_number = iptr->isn_arg.number;
4142 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004143 case ISN_PUSHF:
4144 tv->v_type = VAR_FLOAT;
4145 tv->vval.v_float = iptr->isn_arg.fnumber;
4146 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004147 case ISN_PUSHBLOB:
4148 blob_copy(iptr->isn_arg.blob, tv);
4149 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004150 case ISN_PUSHFUNC:
4151 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01004152 if (iptr->isn_arg.string == NULL)
4153 tv->vval.v_string = NULL;
4154 else
4155 tv->vval.v_string =
4156 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004157 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004158 case ISN_PUSHCHANNEL:
4159#ifdef FEAT_JOB_CHANNEL
4160 tv->v_type = VAR_CHANNEL;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00004161 tv->vval.v_channel = NULL;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004162#endif
4163 break;
4164 case ISN_PUSHJOB:
4165#ifdef FEAT_JOB_CHANNEL
4166 tv->v_type = VAR_JOB;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00004167 tv->vval.v_job = NULL;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004168#endif
4169 break;
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00004170 case ISN_PUSHOBJ:
4171 tv->v_type = VAR_OBJECT;
4172 tv->vval.v_object = NULL;
4173 break;
4174 case ISN_PUSHCLASS:
4175 tv->v_type = VAR_CLASS;
Bram Moolenaar30a84472023-02-27 08:07:14 +00004176 tv->vval.v_class = iptr->isn_arg.classarg;
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00004177 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004178 default:
4179 tv->v_type = VAR_STRING;
Bram Moolenaarf8691002022-03-10 12:20:53 +00004180 tv->vval.v_string = iptr->isn_arg.string == NULL
4181 ? NULL : vim_strsave(iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004182 }
4183 break;
4184
Bram Moolenaar06b77222022-01-25 15:51:56 +00004185 case ISN_AUTOLOAD:
4186 {
4187 char_u *name = iptr->isn_arg.string;
4188
4189 (void)script_autoload(name, FALSE);
4190 if (find_func(name, TRUE))
4191 {
4192 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
4193 goto theend;
4194 tv = STACK_TV_BOT(0);
4195 tv->v_lock = 0;
4196 ++ectx->ec_stack.ga_len;
4197 tv->v_type = VAR_FUNC;
4198 tv->vval.v_string = vim_strsave(name);
4199 }
4200 else
4201 {
4202 int res = load_namespace_var(ectx, ISN_LOADG, iptr);
4203
4204 if (res == NOTDONE)
4205 goto theend;
4206 if (res == FAIL)
4207 goto on_error;
4208 }
4209 }
4210 break;
4211
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004212 case ISN_UNLET:
4213 if (do_unlet(iptr->isn_arg.unlet.ul_name,
4214 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02004215 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004216 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02004217 case ISN_UNLETENV:
LemonBoy77142312022-04-15 20:50:46 +01004218 vim_unsetenv_ext(iptr->isn_arg.unlet.ul_name);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02004219 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004220
Bram Moolenaaraacc9662021-08-13 19:40:51 +02004221 case ISN_LOCKUNLOCK:
4222 {
Ernie Raelee865f32023-09-29 19:53:55 +02004223#ifdef LOG_LOCKVAR
4224 ch_log(NULL, "LKVAR: execute INS_LOCKUNLOCK isn_arg %s",
4225 iptr->isn_arg.string);
4226#endif
Ernie Rael4c8da022023-10-11 21:35:11 +02004227 lval_root_T *lval_root_save = lval_root;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02004228
4229 // Stack has the local variable, argument the whole :lock
4230 // or :unlock command, like ISN_EXEC.
4231 --ectx->ec_stack.ga_len;
Ernie Rael4c8da022023-10-11 21:35:11 +02004232 lval_root_T root = { .lr_tv = STACK_TV_BOT(0),
4233 .lr_cl_exec = iptr->isn_arg.lockunlock.lu_cl_exec,
4234 .lr_is_arg = iptr->isn_arg.lockunlock.lu_is_arg };
Ernie Rael64885642023-10-04 20:16:22 +02004235 lval_root = &root;
Ernie Rael4c8da022023-10-11 21:35:11 +02004236 int res = exec_command(iptr,
Ernie Rael64885642023-10-04 20:16:22 +02004237 iptr->isn_arg.lockunlock.lu_string);
4238 clear_tv(root.lr_tv);
Bram Moolenaaraacc9662021-08-13 19:40:51 +02004239 lval_root = lval_root_save;
4240 if (res == FAIL)
4241 goto on_error;
4242 }
4243 break;
4244
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02004245 case ISN_LOCKCONST:
4246 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
4247 break;
4248
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004249 // create a list from items on the stack; uses a single allocation
4250 // for the list header and the items
4251 case ISN_NEWLIST:
Bram Moolenaar4c137212021-04-19 16:48:48 +02004252 if (exe_newlist(iptr->isn_arg.number, ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004253 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004254 break;
4255
4256 // create a dict from items on the stack
4257 case ISN_NEWDICT:
4258 {
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01004259 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004260
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01004261 SOURCING_LNUM = iptr->isn_lnum;
4262 res = exe_newdict(iptr->isn_arg.number, ectx);
4263 if (res == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004264 goto theend;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01004265 if (res == MAYBE)
4266 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004267 }
4268 break;
4269
LemonBoy372bcce2022-04-25 12:43:20 +01004270 case ISN_CONCAT:
4271 if (exe_concat(iptr->isn_arg.number, ectx) == FAIL)
4272 goto theend;
4273 break;
4274
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004275 // create a partial with NULL value
4276 case ISN_NEWPARTIAL:
4277 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
4278 goto theend;
4279 ++ectx->ec_stack.ga_len;
4280 tv = STACK_TV_BOT(-1);
4281 tv->v_type = VAR_PARTIAL;
4282 tv->v_lock = 0;
4283 tv->vval.v_partial = NULL;
4284 break;
4285
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004286 // call a :def function
4287 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02004288 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01004289 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
4290 NULL,
4291 iptr->isn_arg.dfunc.cdf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02004292 ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02004293 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004294 break;
4295
Bram Moolenaard0200c82023-01-28 15:19:40 +00004296 // call a method on an interface
4297 case ISN_METHODCALL:
4298 {
Bram Moolenaar094cf9f2023-02-10 15:52:25 +00004299 cmfunc_T *mfunc = iptr->isn_arg.mfunc;
4300
Bram Moolenaard0200c82023-01-28 15:19:40 +00004301 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar094cf9f2023-02-10 15:52:25 +00004302 tv = STACK_TV_BOT(-1 - mfunc->cmf_argcount);
Bram Moolenaard0200c82023-01-28 15:19:40 +00004303 if (tv->v_type != VAR_OBJECT)
4304 {
4305 object_required_error(tv);
4306 goto on_error;
4307 }
4308 object_T *obj = tv->vval.v_object;
4309 class_T *cl = obj->obj_class;
4310
4311 // convert the interface index to the object index
Bram Moolenaard0200c82023-01-28 15:19:40 +00004312 int idx = object_index_from_itf_index(mfunc->cmf_itf,
Yegappan Lakshmanan5a05d372023-09-29 19:43:11 +02004313 TRUE, mfunc->cmf_idx, cl);
Bram Moolenaard0200c82023-01-28 15:19:40 +00004314
4315 if (call_ufunc(cl->class_obj_methods[idx], NULL,
4316 mfunc->cmf_argcount, ectx, NULL, NULL) == FAIL)
4317 goto on_error;
4318 }
4319 break;
4320
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004321 // call a builtin function
4322 case ISN_BCALL:
4323 SOURCING_LNUM = iptr->isn_lnum;
4324 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
4325 iptr->isn_arg.bfunc.cbf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02004326 ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004327 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004328 break;
4329
4330 // call a funcref or partial
4331 case ISN_PCALL:
4332 {
4333 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
4334 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02004335 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004336
4337 SOURCING_LNUM = iptr->isn_lnum;
4338 if (pfunc->cpf_top)
4339 {
4340 // funcref is above the arguments
4341 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
4342 }
4343 else
4344 {
4345 // Get the funcref from the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004346 --ectx->ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02004347 partial_tv = *STACK_TV_BOT(0);
4348 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004349 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004350 r = call_partial(tv, pfunc->cpf_argcount, ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02004351 if (tv == &partial_tv)
4352 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004353 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004354 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004355 }
4356 break;
4357
Bram Moolenaarbd5da372020-03-31 23:13:10 +02004358 case ISN_PCALL_END:
4359 // PCALL finished, arguments have been consumed and replaced by
4360 // the return value. Now clear the funcref from the stack,
4361 // and move the return value in its place.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004362 --ectx->ec_stack.ga_len;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02004363 clear_tv(STACK_TV_BOT(-1));
4364 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
4365 break;
4366
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004367 // call a user defined function or funcref/partial
4368 case ISN_UCALL:
4369 {
4370 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
4371
4372 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01004373 if (call_eval_func(cufunc->cuf_name, cufunc->cuf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02004374 ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004375 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004376 }
4377 break;
4378
Bram Moolenaar1d84f762022-09-03 21:35:53 +01004379 // :defer func(arg)
4380 case ISN_DEFER:
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00004381 case ISN_DEFEROBJ:
Bram Moolenaar806a2732022-09-04 15:40:36 +01004382 if (defer_command(iptr->isn_arg.defer.defer_var_idx,
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00004383 iptr->isn_type == ISN_DEFEROBJ,
Bram Moolenaar1d84f762022-09-03 21:35:53 +01004384 iptr->isn_arg.defer.defer_argcount, ectx) == FAIL)
4385 goto on_error;
4386 break;
4387
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004388 // Return from a :def function call without a value.
4389 // Return from a constructor.
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02004390 case ISN_RETURN_VOID:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004391 case ISN_RETURN_OBJECT:
Bram Moolenaar35578162021-08-02 19:10:38 +02004392 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004393 goto theend;
Bram Moolenaar299f3032021-01-08 20:53:09 +01004394 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004395 ++ectx->ec_stack.ga_len;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004396 if (iptr->isn_type == ISN_RETURN_VOID)
4397 {
4398 tv->v_type = VAR_VOID;
4399 tv->vval.v_number = 0;
4400 tv->v_lock = 0;
4401 }
4402 else
4403 {
4404 *tv = *STACK_TV_VAR(0);
4405 ++tv->vval.v_object->obj_refcount;
4406 }
Bram Moolenaar299f3032021-01-08 20:53:09 +01004407 // FALLTHROUGH
4408
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02004409 // return from a :def function call with what is on the stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004410 case ISN_RETURN:
4411 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004412 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01004413 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01004414
4415 if (trystack->ga_len > 0)
4416 trycmd = ((trycmd_T *)trystack->ga_data)
4417 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004418 if (trycmd != NULL
Bram Moolenaar4c137212021-04-19 16:48:48 +02004419 && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004420 {
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01004421 // jump to ":finally" or ":endtry"
4422 if (trycmd->tcd_finally_idx != 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004423 ectx->ec_iidx = trycmd->tcd_finally_idx;
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01004424 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02004425 ectx->ec_iidx = trycmd->tcd_endtry_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004426 trycmd->tcd_return = TRUE;
4427 }
4428 else
Bram Moolenaard032f342020-07-18 18:13:02 +02004429 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004430 }
4431 break;
4432
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004433 // push a partial, a reference to a compiled function
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004434 case ISN_FUNCREF:
4435 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004436 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
4437 ufunc_T *ufunc;
4438 funcref_T *funcref = &iptr->isn_arg.funcref;
4439 funcref_extra_T *extra = funcref->fr_extra;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004440
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004441 if (pt == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004442 goto theend;
Bram Moolenaar35578162021-08-02 19:10:38 +02004443 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02004444 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004445 vim_free(pt);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004446 goto theend;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004447 }
Bram Moolenaar313e4722023-02-08 20:55:27 +00004448 if (extra != NULL && extra->fre_class != NULL)
4449 {
4450 tv = STACK_TV_BOT(-1);
4451 if (tv->v_type != VAR_OBJECT)
4452 {
4453 object_required_error(tv);
4454 vim_free(pt);
4455 goto on_error;
4456 }
4457 object_T *obj = tv->vval.v_object;
4458 class_T *cl = obj->obj_class;
4459
4460 // convert the interface index to the object index
4461 int idx = object_index_from_itf_index(extra->fre_class,
Yegappan Lakshmanan5a05d372023-09-29 19:43:11 +02004462 TRUE, extra->fre_method_idx, cl);
Bram Moolenaar313e4722023-02-08 20:55:27 +00004463 ufunc = cl->class_obj_methods[idx];
4464 }
4465 else if (extra == NULL || extra->fre_func_name == NULL)
Bram Moolenaar38453522021-11-28 22:00:12 +00004466 {
4467 dfunc_T *pt_dfunc = ((dfunc_T *)def_functions.ga_data)
4468 + funcref->fr_dfunc_idx;
4469
4470 ufunc = pt_dfunc->df_ufunc;
4471 }
4472 else
Bram Moolenaar313e4722023-02-08 20:55:27 +00004473 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004474 ufunc = find_func(extra->fre_func_name, FALSE);
Bram Moolenaar313e4722023-02-08 20:55:27 +00004475 }
Bram Moolenaar56acd1f2022-02-18 13:24:52 +00004476 if (ufunc == NULL)
4477 {
4478 SOURCING_LNUM = iptr->isn_lnum;
4479 iemsg("ufunc unexpectedly NULL for FUNCREF");
4480 goto theend;
4481 }
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004482 if (fill_partial_and_closure(pt, ufunc,
Bram Moolenaarcc341812022-09-19 15:54:34 +01004483 extra == NULL ? NULL : &extra->fre_loopvar_info,
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004484 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004485 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004486 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004487 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004488 tv->vval.v_partial = pt;
4489 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02004490 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004491 }
4492 break;
4493
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004494 // Create a global function from a lambda.
4495 case ISN_NEWFUNC:
4496 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004497 newfuncarg_T *arg = iptr->isn_arg.newfunc.nf_arg;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004498
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004499 if (copy_lambda_to_global_func(arg->nfa_lambda,
Bram Moolenaarcc341812022-09-19 15:54:34 +01004500 arg->nfa_global, &arg->nfa_loopvar_info,
4501 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004502 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004503 }
4504 break;
4505
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004506 // List functions
4507 case ISN_DEF:
4508 if (iptr->isn_arg.string == NULL)
4509 list_functions(NULL);
4510 else
4511 {
Bram Moolenaar14336722022-01-08 16:02:59 +00004512 exarg_T ea;
4513 garray_T lines_to_free;
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004514
4515 CLEAR_FIELD(ea);
4516 ea.cmd = ea.arg = iptr->isn_arg.string;
Bram Moolenaar14336722022-01-08 16:02:59 +00004517 ga_init2(&lines_to_free, sizeof(char_u *), 50);
Bram Moolenaard4a9b7f2023-05-23 14:48:42 +01004518 SOURCING_LNUM = iptr->isn_lnum;
h-eastb895b0f2023-09-24 15:46:31 +02004519 define_function(&ea, NULL, &lines_to_free, 0, NULL, 0);
Bram Moolenaar14336722022-01-08 16:02:59 +00004520 ga_clear_strings(&lines_to_free);
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004521 }
4522 break;
4523
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004524 // jump if a condition is met
4525 case ISN_JUMP:
4526 {
4527 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004528 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004529 int jump = TRUE;
4530
4531 if (when != JUMP_ALWAYS)
4532 {
4533 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004534 if (when == JUMP_IF_COND_FALSE
Bram Moolenaar13106602020-10-04 16:06:05 +02004535 || when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004536 || when == JUMP_IF_COND_TRUE)
4537 {
4538 SOURCING_LNUM = iptr->isn_lnum;
4539 jump = tv_get_bool_chk(tv, &error);
4540 if (error)
4541 goto on_error;
4542 }
4543 else
4544 jump = tv2bool(tv);
Bram Moolenaarf6ced982022-04-28 12:00:49 +01004545 if (when == JUMP_IF_FALSE || when == JUMP_IF_COND_FALSE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004546 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01004547 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004548 {
4549 // drop the value from the stack
4550 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004551 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004552 }
4553 }
4554 if (jump)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004555 ectx->ec_iidx = iptr->isn_arg.jump.jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004556 }
4557 break;
4558
Bram Moolenaarb46c0832022-09-15 17:19:37 +01004559 // "while": jump to end if a condition is false
4560 case ISN_WHILE:
4561 {
4562 int error = FALSE;
4563 int jump = TRUE;
4564
4565 tv = STACK_TV_BOT(-1);
4566 SOURCING_LNUM = iptr->isn_lnum;
4567 jump = !tv_get_bool_chk(tv, &error);
4568 if (error)
4569 goto on_error;
4570 // drop the value from the stack
4571 clear_tv(tv);
4572 --ectx->ec_stack.ga_len;
4573 if (jump)
4574 ectx->ec_iidx = iptr->isn_arg.whileloop.while_end;
4575
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01004576 // Store the current funcref count, may be used by
Bram Moolenaarcc341812022-09-19 15:54:34 +01004577 // ISN_ENDLOOP later
Bram Moolenaarb46c0832022-09-15 17:19:37 +01004578 tv = STACK_TV_VAR(
4579 iptr->isn_arg.whileloop.while_funcref_idx);
4580 tv->vval.v_number = ectx->ec_funcrefs.ga_len;
4581 }
4582 break;
4583
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02004584 // Jump if an argument with a default value was already set and not
4585 // v:none.
4586 case ISN_JUMP_IF_ARG_SET:
Bram Moolenaar65b0d162022-12-13 18:43:22 +00004587 case ISN_JUMP_IF_ARG_NOT_SET:
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02004588 tv = STACK_TV_VAR(iptr->isn_arg.jumparg.jump_arg_off);
Bram Moolenaar65b0d162022-12-13 18:43:22 +00004589 int arg_set = tv->v_type != VAR_UNKNOWN
4590 && !(tv->v_type == VAR_SPECIAL
4591 && tv->vval.v_number == VVAL_NONE);
4592 if (iptr->isn_type == ISN_JUMP_IF_ARG_SET ? arg_set : !arg_set)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004593 ectx->ec_iidx = iptr->isn_arg.jumparg.jump_where;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02004594 break;
4595
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004596 // top of a for loop
4597 case ISN_FOR:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00004598 if (execute_for(iptr, ectx) == FAIL)
4599 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004600 break;
4601
Bram Moolenaarb46c0832022-09-15 17:19:37 +01004602 // end of a for or while loop
4603 case ISN_ENDLOOP:
4604 if (execute_endloop(iptr, ectx) == FAIL)
4605 goto theend;
4606 break;
4607
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004608 // start of ":try" block
4609 case ISN_TRY:
4610 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01004611 trycmd_T *trycmd = NULL;
4612
Bram Moolenaar35578162021-08-02 19:10:38 +02004613 if (GA_GROW_FAILS(&ectx->ec_trystack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004614 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004615 trycmd = ((trycmd_T *)ectx->ec_trystack.ga_data)
4616 + ectx->ec_trystack.ga_len;
4617 ++ectx->ec_trystack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004618 ++trylevel;
Bram Moolenaar8d4be892021-02-13 18:33:02 +01004619 CLEAR_POINTER(trycmd);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004620 trycmd->tcd_frame_idx = ectx->ec_frame_idx;
4621 trycmd->tcd_stack_len = ectx->ec_stack.ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01004622 trycmd->tcd_catch_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00004623 iptr->isn_arg.tryref.try_ref->try_catch;
Bram Moolenaara91a7132021-03-25 21:12:15 +01004624 trycmd->tcd_finally_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00004625 iptr->isn_arg.tryref.try_ref->try_finally;
Bram Moolenaara91a7132021-03-25 21:12:15 +01004626 trycmd->tcd_endtry_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00004627 iptr->isn_arg.tryref.try_ref->try_endtry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004628 }
4629 break;
4630
4631 case ISN_PUSHEXC:
4632 if (current_exception == NULL)
4633 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004634 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004635 iemsg("Evaluating catch while current_exception is NULL");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004636 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004637 }
Bram Moolenaar35578162021-08-02 19:10:38 +02004638 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004639 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004640 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004641 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004642 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02004643 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004644 tv->vval.v_string = vim_strsave(
4645 (char_u *)current_exception->value);
4646 break;
4647
4648 case ISN_CATCH:
4649 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004650 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar06651632022-04-27 17:54:25 +01004651 trycmd_T *trycmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004652
Bram Moolenaar4c137212021-04-19 16:48:48 +02004653 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar06651632022-04-27 17:54:25 +01004654 trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004655 + trystack->ga_len - 1;
Bram Moolenaar06651632022-04-27 17:54:25 +01004656 trycmd->tcd_caught = TRUE;
4657 trycmd->tcd_did_throw = FALSE;
4658
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004659 did_emsg = got_int = did_throw = FALSE;
Bram Moolenaar1430cee2021-01-17 19:20:32 +01004660 force_abort = need_rethrow = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004661 catch_exception(current_exception);
4662 }
4663 break;
4664
Bram Moolenaarc150c092021-02-13 15:02:46 +01004665 case ISN_TRYCONT:
4666 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004667 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004668 trycont_T *trycont = &iptr->isn_arg.trycont;
4669 int i;
4670 trycmd_T *trycmd;
4671 int iidx = trycont->tct_where;
4672
4673 if (trystack->ga_len < trycont->tct_levels)
4674 {
4675 siemsg("TRYCONT: expected %d levels, found %d",
4676 trycont->tct_levels, trystack->ga_len);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004677 goto theend;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004678 }
4679 // Make :endtry jump to any outer try block and the last
4680 // :endtry inside the loop to the loop start.
4681 for (i = trycont->tct_levels; i > 0; --i)
4682 {
4683 trycmd = ((trycmd_T *)trystack->ga_data)
4684 + trystack->ga_len - i;
Bram Moolenaar2e34c342021-03-14 12:13:33 +01004685 // Add one to tcd_cont to be able to jump to
4686 // instruction with index zero.
4687 trycmd->tcd_cont = iidx + 1;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01004688 iidx = trycmd->tcd_finally_idx == 0
4689 ? trycmd->tcd_endtry_idx : trycmd->tcd_finally_idx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004690 }
4691 // jump to :finally or :endtry of current try statement
Bram Moolenaar4c137212021-04-19 16:48:48 +02004692 ectx->ec_iidx = iidx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004693 }
4694 break;
4695
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01004696 case ISN_FINALLY:
4697 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004698 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01004699 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
4700 + trystack->ga_len - 1;
4701
4702 // Reset the index to avoid a return statement jumps here
4703 // again.
4704 trycmd->tcd_finally_idx = 0;
4705 break;
4706 }
4707
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004708 // end of ":try" block
4709 case ISN_ENDTRY:
4710 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004711 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar06651632022-04-27 17:54:25 +01004712 trycmd_T *trycmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004713
Bram Moolenaar06651632022-04-27 17:54:25 +01004714 --trystack->ga_len;
4715 --trylevel;
4716 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len;
4717 if (trycmd->tcd_did_throw)
4718 did_throw = TRUE;
4719 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004720 {
Bram Moolenaar06651632022-04-27 17:54:25 +01004721 // discard the exception
4722 if (caught_stack == current_exception)
4723 caught_stack = caught_stack->caught;
4724 discard_current_exception();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004725 }
Bram Moolenaar06651632022-04-27 17:54:25 +01004726
4727 if (trycmd->tcd_return)
4728 goto func_return;
4729
4730 while (ectx->ec_stack.ga_len > trycmd->tcd_stack_len)
4731 {
4732 --ectx->ec_stack.ga_len;
4733 clear_tv(STACK_TV_BOT(0));
4734 }
4735 if (trycmd->tcd_cont != 0)
4736 // handling :continue: jump to outer try block or
4737 // start of the loop
4738 ectx->ec_iidx = trycmd->tcd_cont - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004739 }
4740 break;
4741
4742 case ISN_THROW:
Bram Moolenaar8f81b222021-01-14 21:47:06 +01004743 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004744 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02004745
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004746 if (trystack->ga_len == 0 && trylevel == 0 && emsg_silent)
4747 {
Bram Moolenaar3ef2e412023-04-30 18:50:48 +01004748 // Throwing an exception while using "silent!" causes
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004749 // the function to abort but not display an error.
4750 tv = STACK_TV_BOT(-1);
4751 clear_tv(tv);
4752 tv->v_type = VAR_NUMBER;
4753 tv->vval.v_number = 0;
4754 goto done;
4755 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004756 --ectx->ec_stack.ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004757 tv = STACK_TV_BOT(0);
4758 if (tv->vval.v_string == NULL
4759 || *skipwhite(tv->vval.v_string) == NUL)
4760 {
4761 vim_free(tv->vval.v_string);
4762 SOURCING_LNUM = iptr->isn_lnum;
4763 emsg(_(e_throw_with_empty_string));
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004764 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004765 }
4766
4767 // Inside a "catch" we need to first discard the caught
4768 // exception.
4769 if (trystack->ga_len > 0)
4770 {
4771 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
4772 + trystack->ga_len - 1;
4773 if (trycmd->tcd_caught && current_exception != NULL)
4774 {
4775 // discard the exception
4776 if (caught_stack == current_exception)
4777 caught_stack = caught_stack->caught;
4778 discard_current_exception();
4779 trycmd->tcd_caught = FALSE;
4780 }
4781 }
4782
Bram Moolenaar90a57162022-02-12 14:23:17 +00004783 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004784 if (throw_exception(tv->vval.v_string, ET_USER, NULL)
4785 == FAIL)
4786 {
4787 vim_free(tv->vval.v_string);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004788 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004789 }
4790 did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004791 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004792 break;
4793
4794 // compare with special values
4795 case ISN_COMPAREBOOL:
4796 case ISN_COMPARESPECIAL:
4797 {
4798 typval_T *tv1 = STACK_TV_BOT(-2);
4799 typval_T *tv2 = STACK_TV_BOT(-1);
4800 varnumber_T arg1 = tv1->vval.v_number;
4801 varnumber_T arg2 = tv2->vval.v_number;
4802 int res;
4803
Bram Moolenaar06651632022-04-27 17:54:25 +01004804 if (iptr->isn_arg.op.op_type == EXPR_EQUAL)
4805 res = arg1 == arg2;
4806 else
4807 res = arg1 != arg2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004808
Bram Moolenaar4c137212021-04-19 16:48:48 +02004809 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004810 tv1->v_type = VAR_BOOL;
4811 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4812 }
4813 break;
4814
Bram Moolenaar7a222242022-03-01 19:23:24 +00004815 case ISN_COMPARENULL:
4816 {
4817 typval_T *tv1 = STACK_TV_BOT(-2);
4818 typval_T *tv2 = STACK_TV_BOT(-1);
4819 int res;
4820
4821 res = typval_compare_null(tv1, tv2);
4822 if (res == MAYBE)
4823 goto on_error;
4824 if (iptr->isn_arg.op.op_type == EXPR_NEQUAL)
4825 res = !res;
4826 clear_tv(tv1);
4827 clear_tv(tv2);
4828 --ectx->ec_stack.ga_len;
4829 tv1->v_type = VAR_BOOL;
4830 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4831 }
4832 break;
4833
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004834 // Operation with two number arguments
4835 case ISN_OPNR:
4836 case ISN_COMPARENR:
4837 {
4838 typval_T *tv1 = STACK_TV_BOT(-2);
4839 typval_T *tv2 = STACK_TV_BOT(-1);
4840 varnumber_T arg1 = tv1->vval.v_number;
4841 varnumber_T arg2 = tv2->vval.v_number;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004842 varnumber_T res = 0;
4843 int div_zero = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004844
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004845 if (iptr->isn_arg.op.op_type == EXPR_LSHIFT
4846 || iptr->isn_arg.op.op_type == EXPR_RSHIFT)
4847 {
4848 if (arg2 < 0)
4849 {
4850 SOURCING_LNUM = iptr->isn_lnum;
dundargocc57b5bc2022-11-02 13:30:51 +00004851 emsg(_(e_bitshift_ops_must_be_positive));
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004852 goto on_error;
4853 }
4854 }
4855
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004856 switch (iptr->isn_arg.op.op_type)
4857 {
4858 case EXPR_MULT: res = arg1 * arg2; break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004859 case EXPR_DIV: if (arg2 == 0)
4860 div_zero = TRUE;
4861 else
4862 res = arg1 / arg2;
4863 break;
4864 case EXPR_REM: if (arg2 == 0)
4865 div_zero = TRUE;
4866 else
4867 res = arg1 % arg2;
4868 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004869 case EXPR_SUB: res = arg1 - arg2; break;
4870 case EXPR_ADD: res = arg1 + arg2; break;
4871
4872 case EXPR_EQUAL: res = arg1 == arg2; break;
4873 case EXPR_NEQUAL: res = arg1 != arg2; break;
4874 case EXPR_GREATER: res = arg1 > arg2; break;
4875 case EXPR_GEQUAL: res = arg1 >= arg2; break;
4876 case EXPR_SMALLER: res = arg1 < arg2; break;
4877 case EXPR_SEQUAL: res = arg1 <= arg2; break;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004878 case EXPR_LSHIFT: if (arg2 > MAX_LSHIFT_BITS)
4879 res = 0;
4880 else
Bram Moolenaar68e64d22022-05-22 22:07:52 +01004881 res = (uvarnumber_T)arg1 << arg2;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004882 break;
4883 case EXPR_RSHIFT: if (arg2 > MAX_LSHIFT_BITS)
4884 res = 0;
4885 else
Bram Moolenaar338bf582022-05-22 20:16:32 +01004886 res = (uvarnumber_T)arg1 >> arg2;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004887 break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004888 default: break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004889 }
4890
Bram Moolenaar4c137212021-04-19 16:48:48 +02004891 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004892 if (iptr->isn_type == ISN_COMPARENR)
4893 {
4894 tv1->v_type = VAR_BOOL;
4895 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4896 }
4897 else
4898 tv1->vval.v_number = res;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004899 if (div_zero)
4900 {
4901 SOURCING_LNUM = iptr->isn_lnum;
4902 emsg(_(e_divide_by_zero));
4903 goto on_error;
4904 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004905 }
4906 break;
4907
4908 // Computation with two float arguments
4909 case ISN_OPFLOAT:
4910 case ISN_COMPAREFLOAT:
4911 {
4912 typval_T *tv1 = STACK_TV_BOT(-2);
4913 typval_T *tv2 = STACK_TV_BOT(-1);
4914 float_T arg1 = tv1->vval.v_float;
4915 float_T arg2 = tv2->vval.v_float;
4916 float_T res = 0;
4917 int cmp = FALSE;
4918
4919 switch (iptr->isn_arg.op.op_type)
4920 {
4921 case EXPR_MULT: res = arg1 * arg2; break;
4922 case EXPR_DIV: res = arg1 / arg2; break;
4923 case EXPR_SUB: res = arg1 - arg2; break;
4924 case EXPR_ADD: res = arg1 + arg2; break;
4925
4926 case EXPR_EQUAL: cmp = arg1 == arg2; break;
4927 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
4928 case EXPR_GREATER: cmp = arg1 > arg2; break;
4929 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
4930 case EXPR_SMALLER: cmp = arg1 < arg2; break;
4931 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
4932 default: cmp = 0; break;
4933 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004934 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004935 if (iptr->isn_type == ISN_COMPAREFLOAT)
4936 {
4937 tv1->v_type = VAR_BOOL;
4938 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
4939 }
4940 else
4941 tv1->vval.v_float = res;
4942 }
4943 break;
4944
4945 case ISN_COMPARELIST:
Bram Moolenaar265f8112021-12-19 12:33:05 +00004946 case ISN_COMPAREDICT:
4947 case ISN_COMPAREFUNC:
4948 case ISN_COMPARESTRING:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004949 case ISN_COMPAREBLOB:
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00004950 case ISN_COMPARECLASS:
4951 case ISN_COMPAREOBJECT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004952 {
4953 typval_T *tv1 = STACK_TV_BOT(-2);
4954 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar265f8112021-12-19 12:33:05 +00004955 exprtype_T exprtype = iptr->isn_arg.op.op_type;
4956 int ic = iptr->isn_arg.op.op_ic;
4957 int res = FALSE;
4958 int status = OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004959
Bram Moolenaar265f8112021-12-19 12:33:05 +00004960 SOURCING_LNUM = iptr->isn_lnum;
4961 if (iptr->isn_type == ISN_COMPARELIST)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004962 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00004963 status = typval_compare_list(tv1, tv2,
4964 exprtype, ic, &res);
4965 }
4966 else if (iptr->isn_type == ISN_COMPAREDICT)
4967 {
4968 status = typval_compare_dict(tv1, tv2,
4969 exprtype, ic, &res);
4970 }
4971 else if (iptr->isn_type == ISN_COMPAREFUNC)
4972 {
4973 status = typval_compare_func(tv1, tv2,
4974 exprtype, ic, &res);
4975 }
4976 else if (iptr->isn_type == ISN_COMPARESTRING)
4977 {
4978 status = typval_compare_string(tv1, tv2,
4979 exprtype, ic, &res);
4980 }
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00004981 else if (iptr->isn_type == ISN_COMPAREBLOB)
Bram Moolenaar265f8112021-12-19 12:33:05 +00004982 {
4983 status = typval_compare_blob(tv1, tv2, exprtype, &res);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004984 }
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00004985 else if (iptr->isn_type == ISN_COMPARECLASS)
4986 {
Bram Moolenaar03ff0c62023-01-02 20:38:01 +00004987 status = typval_compare_class(tv1, tv2,
4988 exprtype, FALSE, &res);
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00004989 }
4990 else // ISN_COMPAREOBJECT
4991 {
4992 status = typval_compare_object(tv1, tv2,
Bram Moolenaar03ff0c62023-01-02 20:38:01 +00004993 exprtype, FALSE, &res);
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00004994 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004995 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004996 clear_tv(tv1);
4997 clear_tv(tv2);
4998 tv1->v_type = VAR_BOOL;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004999 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
5000 if (status == FAIL)
5001 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005002 }
5003 break;
5004
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005005 case ISN_COMPAREANY:
5006 {
5007 typval_T *tv1 = STACK_TV_BOT(-2);
5008 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar657137c2021-01-09 15:45:23 +01005009 exprtype_T exprtype = iptr->isn_arg.op.op_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005010 int ic = iptr->isn_arg.op.op_ic;
Bram Moolenaar265f8112021-12-19 12:33:05 +00005011 int status;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005012
Bram Moolenaareb26f432020-09-14 16:50:05 +02005013 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar265f8112021-12-19 12:33:05 +00005014 status = typval_compare(tv1, tv2, exprtype, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005015 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005016 --ectx->ec_stack.ga_len;
Bram Moolenaar265f8112021-12-19 12:33:05 +00005017 if (status == FAIL)
5018 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005019 }
5020 break;
5021
5022 case ISN_ADDLIST:
5023 case ISN_ADDBLOB:
5024 {
5025 typval_T *tv1 = STACK_TV_BOT(-2);
5026 typval_T *tv2 = STACK_TV_BOT(-1);
5027
Bram Moolenaar1dcae592020-10-19 19:02:42 +02005028 // add two lists or blobs
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005029 if (iptr->isn_type == ISN_ADDLIST)
Bram Moolenaar07802042021-09-09 23:01:14 +02005030 {
5031 if (iptr->isn_arg.op.op_type == EXPR_APPEND
5032 && tv1->vval.v_list != NULL)
5033 list_extend(tv1->vval.v_list, tv2->vval.v_list,
5034 NULL);
5035 else
5036 eval_addlist(tv1, tv2);
5037 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005038 else
5039 eval_addblob(tv1, tv2);
5040 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005041 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005042 }
5043 break;
5044
Bram Moolenaar1dcae592020-10-19 19:02:42 +02005045 case ISN_LISTAPPEND:
5046 {
5047 typval_T *tv1 = STACK_TV_BOT(-2);
5048 typval_T *tv2 = STACK_TV_BOT(-1);
5049 list_T *l = tv1->vval.v_list;
5050
5051 // add an item to a list
Bram Moolenaar1f4a3452022-01-01 18:29:21 +00005052 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02005053 if (l == NULL)
5054 {
Bram Moolenaar1dcae592020-10-19 19:02:42 +02005055 emsg(_(e_cannot_add_to_null_list));
5056 goto on_error;
5057 }
Bram Moolenaar1f4a3452022-01-01 18:29:21 +00005058 if (value_check_lock(l->lv_lock, NULL, FALSE))
5059 goto on_error;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02005060 if (list_append_tv(l, tv2) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005061 goto theend;
Bram Moolenaar955347c2020-10-19 23:01:46 +02005062 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005063 --ectx->ec_stack.ga_len;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02005064 }
5065 break;
5066
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02005067 case ISN_BLOBAPPEND:
5068 {
5069 typval_T *tv1 = STACK_TV_BOT(-2);
5070 typval_T *tv2 = STACK_TV_BOT(-1);
5071 blob_T *b = tv1->vval.v_blob;
5072 int error = FALSE;
5073 varnumber_T n;
5074
5075 // add a number to a blob
5076 if (b == NULL)
5077 {
5078 SOURCING_LNUM = iptr->isn_lnum;
5079 emsg(_(e_cannot_add_to_null_blob));
5080 goto on_error;
5081 }
5082 n = tv_get_number_chk(tv2, &error);
5083 if (error)
5084 goto on_error;
5085 ga_append(&b->bv_ga, (int)n);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005086 --ectx->ec_stack.ga_len;
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02005087 }
5088 break;
5089
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005090 // Computation with two arguments of unknown type
5091 case ISN_OPANY:
5092 {
5093 typval_T *tv1 = STACK_TV_BOT(-2);
5094 typval_T *tv2 = STACK_TV_BOT(-1);
5095 varnumber_T n1, n2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005096 float_T f1 = 0, f2 = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005097 int error = FALSE;
5098
5099 if (iptr->isn_arg.op.op_type == EXPR_ADD)
5100 {
5101 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
5102 {
5103 eval_addlist(tv1, tv2);
5104 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005105 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005106 break;
5107 }
5108 else if (tv1->v_type == VAR_BLOB
5109 && tv2->v_type == VAR_BLOB)
5110 {
5111 eval_addblob(tv1, tv2);
5112 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005113 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005114 break;
5115 }
5116 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005117 if (tv1->v_type == VAR_FLOAT)
5118 {
5119 f1 = tv1->vval.v_float;
5120 n1 = 0;
5121 }
5122 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005123 {
Bram Moolenaarf665e972020-12-05 19:17:16 +01005124 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005125 n1 = tv_get_number_chk(tv1, &error);
5126 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02005127 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005128 if (tv2->v_type == VAR_FLOAT)
5129 f1 = n1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005130 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005131 if (tv2->v_type == VAR_FLOAT)
5132 {
5133 f2 = tv2->vval.v_float;
5134 n2 = 0;
5135 }
5136 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005137 {
5138 n2 = tv_get_number_chk(tv2, &error);
5139 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02005140 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005141 if (tv1->v_type == VAR_FLOAT)
5142 f2 = n2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005143 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005144 // if there is a float on either side the result is a float
5145 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
5146 {
5147 switch (iptr->isn_arg.op.op_type)
5148 {
5149 case EXPR_MULT: f1 = f1 * f2; break;
5150 case EXPR_DIV: f1 = f1 / f2; break;
5151 case EXPR_SUB: f1 = f1 - f2; break;
5152 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005153 default: SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005154 emsg(_(e_cannot_use_percent_with_float));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02005155 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005156 }
5157 clear_tv(tv1);
5158 clear_tv(tv2);
5159 tv1->v_type = VAR_FLOAT;
5160 tv1->vval.v_float = f1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005161 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005162 }
5163 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005164 {
Bram Moolenaarb1f28572021-01-21 13:03:20 +01005165 int failed = FALSE;
5166
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005167 switch (iptr->isn_arg.op.op_type)
5168 {
5169 case EXPR_MULT: n1 = n1 * n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01005170 case EXPR_DIV: n1 = num_divide(n1, n2, &failed);
5171 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01005172 goto on_error;
5173 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005174 case EXPR_SUB: n1 = n1 - n2; break;
5175 case EXPR_ADD: n1 = n1 + n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01005176 default: n1 = num_modulus(n1, n2, &failed);
5177 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01005178 goto on_error;
5179 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005180 }
5181 clear_tv(tv1);
5182 clear_tv(tv2);
5183 tv1->v_type = VAR_NUMBER;
5184 tv1->vval.v_number = n1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005185 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005186 }
5187 }
5188 break;
5189
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005190 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005191 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005192 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005193 int is_slice = iptr->isn_type == ISN_STRSLICE;
5194 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005195 char_u *res;
5196
5197 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005198 // string slice: string is at stack-3, first index at
5199 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005200 if (is_slice)
5201 {
5202 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005203 n1 = tv->vval.v_number;
5204 }
5205
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005206 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005207 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005208
Bram Moolenaar4c137212021-04-19 16:48:48 +02005209 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005210 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005211 if (is_slice)
5212 // Slice: Select the characters from the string
Bram Moolenaar6601b622021-01-13 21:47:15 +01005213 res = string_slice(tv->vval.v_string, n1, n2, FALSE);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005214 else
5215 // Index: The resulting variable is a string of a
Bram Moolenaar0289a092021-03-14 18:40:19 +01005216 // single character (including composing characters).
5217 // If the index is too big or negative the result is
5218 // empty.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005219 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005220 vim_free(tv->vval.v_string);
5221 tv->vval.v_string = res;
5222 }
5223 break;
5224
5225 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02005226 case ISN_LISTSLICE:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02005227 case ISN_BLOBINDEX:
5228 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005229 {
Bram Moolenaarcfc30232021-04-11 20:26:34 +02005230 int is_slice = iptr->isn_type == ISN_LISTSLICE
5231 || iptr->isn_type == ISN_BLOBSLICE;
5232 int is_blob = iptr->isn_type == ISN_BLOBINDEX
5233 || iptr->isn_type == ISN_BLOBSLICE;
Bram Moolenaared591872020-08-15 22:14:53 +02005234 varnumber_T n1, n2;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02005235 typval_T *val_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005236
5237 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02005238 // list slice: list is at stack-3, indexes at stack-2 and
5239 // stack-1
Bram Moolenaarcfc30232021-04-11 20:26:34 +02005240 // Same for blob.
5241 val_tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005242
5243 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02005244 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005245 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02005246
5247 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005248 {
Bram Moolenaared591872020-08-15 22:14:53 +02005249 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02005250 n1 = tv->vval.v_number;
5251 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005252 }
Bram Moolenaared591872020-08-15 22:14:53 +02005253
Bram Moolenaar4c137212021-04-19 16:48:48 +02005254 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02005255 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02005256 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02005257 if (is_blob)
5258 {
5259 if (blob_slice_or_index(val_tv->vval.v_blob, is_slice,
5260 n1, n2, FALSE, tv) == FAIL)
5261 goto on_error;
5262 }
5263 else
5264 {
5265 if (list_slice_or_index(val_tv->vval.v_list, is_slice,
5266 n1, n2, FALSE, tv, TRUE) == FAIL)
5267 goto on_error;
5268 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005269 }
5270 break;
5271
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005272 case ISN_ANYINDEX:
5273 case ISN_ANYSLICE:
5274 {
5275 int is_slice = iptr->isn_type == ISN_ANYSLICE;
5276 typval_T *var1, *var2;
5277 int res;
5278
5279 // index: composite is at stack-2, index at stack-1
5280 // slice: composite is at stack-3, indexes at stack-2 and
5281 // stack-1
5282 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02005283 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005284 if (check_can_index(tv, TRUE, TRUE) == FAIL)
5285 goto on_error;
5286 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
5287 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
Bram Moolenaar6601b622021-01-13 21:47:15 +01005288 res = eval_index_inner(tv, is_slice, var1, var2,
5289 FALSE, NULL, -1, TRUE);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005290 clear_tv(var1);
5291 if (is_slice)
5292 clear_tv(var2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005293 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005294 if (res == FAIL)
5295 goto on_error;
5296 }
5297 break;
5298
Bram Moolenaar9af78762020-06-16 11:34:42 +02005299 case ISN_SLICE:
5300 {
5301 list_T *list;
5302 int count = iptr->isn_arg.number;
5303
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02005304 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02005305 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02005306 list = tv->vval.v_list;
5307
5308 // no error for short list, expect it to be checked earlier
5309 if (list != NULL && list->lv_len >= count)
5310 {
5311 list_T *newlist = list_slice(list,
5312 count, list->lv_len - 1);
5313
5314 if (newlist != NULL)
5315 {
5316 list_unref(list);
5317 tv->vval.v_list = newlist;
5318 ++newlist->lv_refcount;
5319 }
5320 }
5321 }
5322 break;
5323
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005324 case ISN_GETITEM:
5325 {
5326 listitem_T *li;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02005327 getitem_T *gi = &iptr->isn_arg.getitem;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005328
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02005329 // Get list item: list is at stack-1, push item.
5330 // List type and length is checked for when compiling.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02005331 tv = STACK_TV_BOT(-1 - gi->gi_with_op);
5332 li = list_find(tv->vval.v_list, gi->gi_index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005333
Bram Moolenaar35578162021-08-02 19:10:38 +02005334 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005335 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005336 ++ectx->ec_stack.ga_len;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005337 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
Bram Moolenaarf785aa12021-02-11 21:19:34 +01005338
5339 // Useful when used in unpack assignment. Reset at
5340 // ISN_DROP.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02005341 ectx->ec_where.wt_index = gi->gi_index + 1;
LemonBoyc5d27442023-08-19 13:02:35 +02005342 ectx->ec_where.wt_kind = WT_VARIABLE;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005343 }
5344 break;
5345
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005346 case ISN_MEMBER:
5347 {
5348 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005349 char_u *key;
5350 dictitem_T *di;
5351
5352 // dict member: dict is at stack-2, key at stack-1
5353 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02005354 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005355 dict = tv->vval.v_dict;
5356
5357 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02005358 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005359 key = tv->vval.v_string;
Bram Moolenaar086fc9a2020-10-30 18:33:02 +01005360 if (key == NULL)
5361 key = (char_u *)"";
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02005362
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005363 if ((di = dict_find(dict, key, -1)) == NULL)
5364 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005365 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00005366 semsg(_(e_key_not_present_in_dictionary_str), key);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01005367
5368 // If :silent! is used we will continue, make sure the
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005369 // stack contents makes sense and the dict stack is
5370 // updated.
Bram Moolenaar4029cab2020-12-05 18:13:27 +01005371 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005372 --ectx->ec_stack.ga_len;
Bram Moolenaar4029cab2020-12-05 18:13:27 +01005373 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005374 (void) dict_stack_save(tv);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01005375 tv->v_type = VAR_NUMBER;
5376 tv->vval.v_number = 0;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01005377 goto on_fatal_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005378 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005379 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005380 --ectx->ec_stack.ga_len;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005381 // Put the dict used on the dict stack, it might be used by
5382 // a dict function later.
Bram Moolenaar50788ef2020-07-05 16:51:26 +02005383 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005384 if (dict_stack_save(tv) == FAIL)
5385 goto on_fatal_error;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02005386 copy_tv(&di->di_tv, tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005387 }
5388 break;
5389
5390 // dict member with string key
5391 case ISN_STRINGMEMBER:
5392 {
5393 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005394 dictitem_T *di;
5395
5396 tv = STACK_TV_BOT(-1);
5397 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
5398 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005399 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005400 emsg(_(e_dictionary_required));
Bram Moolenaard032f342020-07-18 18:13:02 +02005401 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005402 }
5403 dict = tv->vval.v_dict;
5404
5405 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
5406 == NULL)
5407 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005408 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00005409 semsg(_(e_key_not_present_in_dictionary_str),
Bram Moolenaar381692b2022-02-02 20:01:27 +00005410 iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02005411 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005412 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005413 // Put the dict used on the dict stack, it might be used by
5414 // a dict function later.
5415 if (dict_stack_save(tv) == FAIL)
5416 goto on_fatal_error;
5417
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005418 copy_tv(&di->di_tv, tv);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005419 }
5420 break;
5421
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005422 case ISN_GET_OBJ_MEMBER:
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00005423 case ISN_GET_ITF_MEMBER:
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005424 {
5425 tv = STACK_TV_BOT(-1);
5426 if (tv->v_type != VAR_OBJECT)
5427 {
5428 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaard0200c82023-01-28 15:19:40 +00005429 object_required_error(tv);
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005430 goto on_error;
5431 }
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00005432
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005433 object_T *obj = tv->vval.v_object;
Bram Moolenaarc3f971f2023-03-02 17:38:33 +00005434 if (obj == NULL)
5435 {
5436 SOURCING_LNUM = iptr->isn_lnum;
5437 emsg(_(e_using_null_object));
5438 goto on_error;
5439 }
5440
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00005441 int idx;
5442 if (iptr->isn_type == ISN_GET_OBJ_MEMBER)
Ernie Rael18143d32023-09-04 22:30:41 +02005443 idx = iptr->isn_arg.classmember.cm_idx;
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00005444 else
5445 {
5446 idx = iptr->isn_arg.classmember.cm_idx;
5447 // convert the interface index to the object index
5448 idx = object_index_from_itf_index(
Ernie Rael18143d32023-09-04 22:30:41 +02005449 iptr->isn_arg.classmember.cm_class,
Yegappan Lakshmanan5a05d372023-09-29 19:43:11 +02005450 FALSE, idx, obj->obj_class);
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00005451 }
5452
Ernie Rael18143d32023-09-04 22:30:41 +02005453 // The members are located right after the object struct.
Yegappan Lakshmanan5a05d372023-09-29 19:43:11 +02005454 typval_T *mtv = ((typval_T *)(obj + 1)) + idx;
Bram Moolenaar590162c2022-12-24 21:24:06 +00005455 copy_tv(mtv, tv);
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005456
5457 // Unreference the object after getting the member, it may
5458 // be freed.
5459 object_unref(obj);
5460 }
5461 break;
5462
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005463 case ISN_STORE_THIS:
5464 {
5465 int idx = iptr->isn_arg.number;
5466 object_T *obj = STACK_TV_VAR(0)->vval.v_object;
5467 // the members are located right after the object struct
5468 typval_T *mtv = ((typval_T *)(obj + 1)) + idx;
5469 clear_tv(mtv);
5470 *mtv = *STACK_TV_BOT(-1);
5471 --ectx->ec_stack.ga_len;
5472 }
5473 break;
5474
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005475 case ISN_CLEARDICT:
5476 dict_stack_drop();
5477 break;
5478
5479 case ISN_USEDICT:
5480 {
5481 typval_T *dict_tv = dict_stack_get_tv();
5482
5483 // Turn "dict.Func" into a partial for "Func" bound to
5484 // "dict". Don't do this when "Func" is already a partial
5485 // that was bound explicitly (pt_auto is FALSE).
5486 tv = STACK_TV_BOT(-1);
5487 if (dict_tv != NULL
5488 && dict_tv->v_type == VAR_DICT
5489 && dict_tv->vval.v_dict != NULL
5490 && (tv->v_type == VAR_FUNC
5491 || (tv->v_type == VAR_PARTIAL
5492 && (tv->vval.v_partial->pt_auto
5493 || tv->vval.v_partial->pt_dict == NULL))))
5494 dict_tv->vval.v_dict =
5495 make_partial(dict_tv->vval.v_dict, tv);
5496 dict_stack_drop();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005497 }
5498 break;
5499
5500 case ISN_NEGATENR:
5501 tv = STACK_TV_BOT(-1);
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00005502 // CHECKTYPE should have checked the variable type
Bram Moolenaarc58164c2020-03-29 18:40:30 +02005503 if (tv->v_type == VAR_FLOAT)
5504 tv->vval.v_float = -tv->vval.v_float;
5505 else
Bram Moolenaarc58164c2020-03-29 18:40:30 +02005506 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005507 break;
5508
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005509 case ISN_CHECKTYPE:
5510 {
5511 checktype_T *ct = &iptr->isn_arg.type;
Bram Moolenaarb1040dc2022-05-18 11:00:48 +01005512 int r;
LemonBoyc5d27442023-08-19 13:02:35 +02005513 where_T where = WHERE_INIT;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005514
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01005515 tv = STACK_TV_BOT((int)ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02005516 SOURCING_LNUM = iptr->isn_lnum;
LemonBoyc5d27442023-08-19 13:02:35 +02005517 if (ct->ct_arg_idx > 0)
5518 {
5519 where.wt_index = ct->ct_arg_idx;
5520 where.wt_kind = ct->ct_is_var ? WT_VARIABLE : WT_ARGUMENT;
LemonBoyc5d27442023-08-19 13:02:35 +02005521 }
LemonBoyf244b2f2023-08-19 16:02:04 +02005522 where.wt_func_name = ectx->ec_where.wt_func_name;
LemonBoyc5d27442023-08-19 13:02:35 +02005523 r = check_typval_type(ct->ct_type, tv, where);
Bram Moolenaarb1040dc2022-05-18 11:00:48 +01005524 if (r == FAIL)
5525 goto on_error;
Bram Moolenaar5e654232020-09-16 15:22:00 +02005526
5527 // number 0 is FALSE, number 1 is TRUE
5528 if (tv->v_type == VAR_NUMBER
5529 && ct->ct_type->tt_type == VAR_BOOL
5530 && (tv->vval.v_number == 0
5531 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005532 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02005533 tv->v_type = VAR_BOOL;
5534 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02005535 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005536 }
5537 }
5538 break;
5539
Bram Moolenaar9af78762020-06-16 11:34:42 +02005540 case ISN_CHECKLEN:
5541 {
5542 int min_len = iptr->isn_arg.checklen.cl_min_len;
5543 list_T *list = NULL;
5544
5545 tv = STACK_TV_BOT(-1);
5546 if (tv->v_type == VAR_LIST)
5547 list = tv->vval.v_list;
5548 if (list == NULL || list->lv_len < min_len
5549 || (list->lv_len > min_len
5550 && !iptr->isn_arg.checklen.cl_more_OK))
5551 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005552 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005553 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02005554 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02005555 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02005556 }
5557 }
5558 break;
5559
Bram Moolenaaraa210a32021-01-02 15:41:03 +01005560 case ISN_SETTYPE:
Bram Moolenaar381692b2022-02-02 20:01:27 +00005561 set_tv_type(STACK_TV_BOT(-1), iptr->isn_arg.type.ct_type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01005562 break;
5563
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005564 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005565 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005566 {
5567 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005568 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005569
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005570 if (iptr->isn_type == ISN_2BOOL)
5571 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005572 tv = STACK_TV_BOT(iptr->isn_arg.tobool.offset);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005573 n = tv2bool(tv);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005574 if (iptr->isn_arg.tobool.invert)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005575 n = !n;
5576 }
5577 else
5578 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005579 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005580 SOURCING_LNUM = iptr->isn_lnum;
5581 n = tv_get_bool_chk(tv, &error);
5582 if (error)
5583 goto on_error;
5584 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005585 clear_tv(tv);
5586 tv->v_type = VAR_BOOL;
5587 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
5588 }
5589 break;
5590
5591 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02005592 case ISN_2STRING_ANY:
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01005593 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005594 if (do_2string(STACK_TV_BOT(iptr->isn_arg.tostring.offset),
5595 iptr->isn_type == ISN_2STRING_ANY,
5596 iptr->isn_arg.tostring.tolerant) == FAIL)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01005597 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005598 break;
5599
Bram Moolenaar08597872020-12-10 19:43:40 +01005600 case ISN_RANGE:
5601 {
5602 exarg_T ea;
5603 char *errormsg;
5604
Bram Moolenaarece0b872021-01-08 20:40:45 +01005605 ea.line2 = 0;
Bram Moolenaard1510ee2021-01-04 16:15:58 +01005606 ea.addr_count = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01005607 ea.addr_type = ADDR_LINES;
5608 ea.cmd = iptr->isn_arg.string;
Bram Moolenaarece0b872021-01-08 20:40:45 +01005609 ea.skip = FALSE;
Bram Moolenaar08597872020-12-10 19:43:40 +01005610 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaarece0b872021-01-08 20:40:45 +01005611 goto on_error;
Bram Moolenaara28639e2021-01-19 22:48:09 +01005612
Bram Moolenaar35578162021-08-02 19:10:38 +02005613 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005614 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005615 ++ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01005616 tv = STACK_TV_BOT(-1);
5617 tv->v_type = VAR_NUMBER;
5618 tv->v_lock = 0;
Bram Moolenaar06651632022-04-27 17:54:25 +01005619 tv->vval.v_number = ea.line2;
Bram Moolenaar08597872020-12-10 19:43:40 +01005620 }
5621 break;
5622
Bram Moolenaarc3516f72020-09-08 22:45:35 +02005623 case ISN_PUT:
5624 {
5625 int regname = iptr->isn_arg.put.put_regname;
5626 linenr_T lnum = iptr->isn_arg.put.put_lnum;
5627 char_u *expr = NULL;
5628 int dir = FORWARD;
5629
Bram Moolenaar08597872020-12-10 19:43:40 +01005630 if (lnum < -2)
5631 {
5632 // line number was put on the stack by ISN_RANGE
5633 tv = STACK_TV_BOT(-1);
5634 curwin->w_cursor.lnum = tv->vval.v_number;
5635 if (lnum == LNUM_VARIABLE_RANGE_ABOVE)
5636 dir = BACKWARD;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005637 --ectx->ec_stack.ga_len;
Bram Moolenaar08597872020-12-10 19:43:40 +01005638 }
5639 else if (lnum == -2)
Bram Moolenaarc3516f72020-09-08 22:45:35 +02005640 // :put! above cursor
5641 dir = BACKWARD;
5642 else if (lnum >= 0)
Bram Moolenaar4e713ba2022-02-07 15:31:37 +00005643 {
5644 curwin->w_cursor.lnum = lnum;
5645 if (lnum == 0)
5646 // check_cursor() below will move to line 1
5647 dir = BACKWARD;
5648 }
Bram Moolenaara28639e2021-01-19 22:48:09 +01005649
5650 if (regname == '=')
5651 {
5652 tv = STACK_TV_BOT(-1);
5653 if (tv->v_type == VAR_STRING)
5654 expr = tv->vval.v_string;
5655 else
5656 {
5657 expr = typval2string(tv, TRUE); // allocates value
5658 clear_tv(tv);
5659 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005660 --ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01005661 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02005662 check_cursor();
5663 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
5664 vim_free(expr);
5665 }
5666 break;
5667
Bram Moolenaar02194d22020-10-24 23:08:38 +02005668 case ISN_CMDMOD:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005669 ectx->ec_funclocal.floc_save_cmdmod = cmdmod;
5670 ectx->ec_funclocal.floc_restore_cmdmod = TRUE;
5671 ectx->ec_funclocal.floc_restore_cmdmod_stacklen =
5672 ectx->ec_stack.ga_len;
Bram Moolenaar02194d22020-10-24 23:08:38 +02005673 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
5674 apply_cmdmod(&cmdmod);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02005675 break;
5676
Bram Moolenaar02194d22020-10-24 23:08:38 +02005677 case ISN_CMDMOD_REV:
5678 // filter regprog is owned by the instruction, don't free it
5679 cmdmod.cmod_filter_regmatch.regprog = NULL;
5680 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005681 cmdmod = ectx->ec_funclocal.floc_save_cmdmod;
5682 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02005683 break;
5684
Bram Moolenaar792f7862020-11-23 08:31:18 +01005685 case ISN_UNPACK:
5686 {
5687 int count = iptr->isn_arg.unpack.unp_count;
5688 int semicolon = iptr->isn_arg.unpack.unp_semicolon;
5689 list_T *l;
5690 listitem_T *li;
5691 int i;
5692
5693 // Check there is a valid list to unpack.
5694 tv = STACK_TV_BOT(-1);
5695 if (tv->v_type != VAR_LIST)
5696 {
5697 SOURCING_LNUM = iptr->isn_lnum;
5698 emsg(_(e_for_argument_must_be_sequence_of_lists));
5699 goto on_error;
5700 }
5701 l = tv->vval.v_list;
5702 if (l == NULL
5703 || l->lv_len < (semicolon ? count - 1 : count))
5704 {
5705 SOURCING_LNUM = iptr->isn_lnum;
5706 emsg(_(e_list_value_does_not_have_enough_items));
5707 goto on_error;
5708 }
5709 else if (!semicolon && l->lv_len > count)
5710 {
5711 SOURCING_LNUM = iptr->isn_lnum;
5712 emsg(_(e_list_value_has_more_items_than_targets));
5713 goto on_error;
5714 }
5715
5716 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar35578162021-08-02 19:10:38 +02005717 if (GA_GROW_FAILS(&ectx->ec_stack, count - 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005718 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005719 ectx->ec_stack.ga_len += count - 1;
Bram Moolenaar792f7862020-11-23 08:31:18 +01005720
5721 // Variable after semicolon gets a list with the remaining
5722 // items.
5723 if (semicolon)
5724 {
5725 list_T *rem_list =
5726 list_alloc_with_items(l->lv_len - count + 1);
5727
5728 if (rem_list == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005729 goto theend;
Bram Moolenaar792f7862020-11-23 08:31:18 +01005730 tv = STACK_TV_BOT(-count);
5731 tv->vval.v_list = rem_list;
5732 ++rem_list->lv_refcount;
5733 tv->v_lock = 0;
5734 li = l->lv_first;
5735 for (i = 0; i < count - 1; ++i)
5736 li = li->li_next;
5737 for (i = 0; li != NULL; ++i)
5738 {
Bram Moolenaar61efa162022-03-18 13:10:48 +00005739 typval_T tvcopy;
5740
5741 copy_tv(&li->li_tv, &tvcopy);
5742 list_set_item(rem_list, i, &tvcopy);
Bram Moolenaar792f7862020-11-23 08:31:18 +01005743 li = li->li_next;
5744 }
5745 --count;
5746 }
5747
5748 // Produce the values in reverse order, first item last.
5749 li = l->lv_first;
5750 for (i = 0; i < count; ++i)
5751 {
5752 tv = STACK_TV_BOT(-i - 1);
5753 copy_tv(&li->li_tv, tv);
5754 li = li->li_next;
5755 }
5756
5757 list_unref(l);
5758 }
5759 break;
5760
Bram Moolenaarb2049902021-01-24 12:53:53 +01005761 case ISN_PROF_START:
5762 case ISN_PROF_END:
5763 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01005764#ifdef FEAT_PROFILE
Bram Moolenaarca16c602022-09-06 18:57:08 +01005765 funccall_T cookie;
5766 ufunc_T *cur_ufunc =
Bram Moolenaarb2049902021-01-24 12:53:53 +01005767 (((dfunc_T *)def_functions.ga_data)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02005768 + ectx->ec_dfunc_idx)->df_ufunc;
Bram Moolenaarb2049902021-01-24 12:53:53 +01005769
Bram Moolenaarca16c602022-09-06 18:57:08 +01005770 cookie.fc_func = cur_ufunc;
Bram Moolenaarb2049902021-01-24 12:53:53 +01005771 if (iptr->isn_type == ISN_PROF_START)
5772 {
5773 func_line_start(&cookie, iptr->isn_lnum);
5774 // if we get here the instruction is executed
5775 func_line_exec(&cookie);
5776 }
5777 else
5778 func_line_end(&cookie);
Bram Moolenaarf002a412021-01-24 13:34:18 +01005779#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01005780 }
5781 break;
5782
Bram Moolenaare99d4222021-06-13 14:01:26 +02005783 case ISN_DEBUG:
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02005784 handle_debug(iptr, ectx);
Bram Moolenaare99d4222021-06-13 14:01:26 +02005785 break;
5786
Bram Moolenaar389df252020-07-09 21:20:47 +02005787 case ISN_SHUFFLE:
5788 {
Bram Moolenaar792f7862020-11-23 08:31:18 +01005789 typval_T tmp_tv;
5790 int item = iptr->isn_arg.shuffle.shfl_item;
5791 int up = iptr->isn_arg.shuffle.shfl_up;
Bram Moolenaar389df252020-07-09 21:20:47 +02005792
5793 tmp_tv = *STACK_TV_BOT(-item);
5794 for ( ; up > 0 && item > 1; --up)
5795 {
5796 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
5797 --item;
5798 }
5799 *STACK_TV_BOT(-item) = tmp_tv;
5800 }
5801 break;
5802
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005803 case ISN_DROP:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005804 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005805 clear_tv(STACK_TV_BOT(0));
LemonBoyc5d27442023-08-19 13:02:35 +02005806 ectx->ec_where = (where_T)WHERE_INIT;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005807 break;
5808 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02005809 continue;
5810
Bram Moolenaard032f342020-07-18 18:13:02 +02005811func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02005812 // Restore previous function. If the frame pointer is where we started
5813 // then there is none and we are done.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005814 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02005815 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02005816
Bram Moolenaar4c137212021-04-19 16:48:48 +02005817 if (func_return(ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02005818 // only fails when out of memory
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005819 goto theend;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02005820 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02005821
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02005822on_error:
Bram Moolenaaraf0df472020-12-02 20:51:22 +01005823 // Jump here for an error that does not require aborting execution.
Bram Moolenaar56602ba2020-12-05 21:22:08 +01005824 // If "emsg_silent" is set then ignore the error, unless it was set
5825 // when calling the function.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005826 if (did_emsg_cumul + did_emsg == ectx->ec_did_emsg_before
Bram Moolenaar56602ba2020-12-05 21:22:08 +01005827 && emsg_silent && did_emsg_def == 0)
Bram Moolenaarf9041332021-01-21 19:41:16 +01005828 {
5829 // If a sequence of instructions causes an error while ":silent!"
5830 // was used, restore the stack length and jump ahead to restoring
5831 // the cmdmod.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005832 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaarf9041332021-01-21 19:41:16 +01005833 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005834 while (ectx->ec_stack.ga_len
5835 > ectx->ec_funclocal.floc_restore_cmdmod_stacklen)
Bram Moolenaarf9041332021-01-21 19:41:16 +01005836 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005837 --ectx->ec_stack.ga_len;
Bram Moolenaarf9041332021-01-21 19:41:16 +01005838 clear_tv(STACK_TV_BOT(0));
5839 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005840 while (ectx->ec_instr[ectx->ec_iidx].isn_type != ISN_CMDMOD_REV)
5841 ++ectx->ec_iidx;
Bram Moolenaarf9041332021-01-21 19:41:16 +01005842 }
Bram Moolenaarcd030c42020-10-30 21:49:40 +01005843 continue;
Bram Moolenaarf9041332021-01-21 19:41:16 +01005844 }
Bram Moolenaaraf0df472020-12-02 20:51:22 +01005845on_fatal_error:
5846 // Jump here for an error that messes up the stack.
Bram Moolenaar171fb922020-10-28 16:54:47 +01005847 // If we are not inside a try-catch started here, abort execution.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005848 if (trylevel <= ectx->ec_trylevel_at_start)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005849 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005850 }
5851
5852done:
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005853 ret = OK;
5854theend:
Bram Moolenaar58779852022-09-06 18:31:14 +01005855 may_invoke_defer_funcs(ectx);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01005856
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005857 dict_stack_clear(dict_stack_len_at_start);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005858 ectx->ec_trylevel_at_start = save_trylevel_at_start;
5859 return ret;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005860}
5861
5862/*
Bram Moolenaar806a2732022-09-04 15:40:36 +01005863 * Execute the instructions from a VAR_INSTR typval and put the result in
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005864 * "rettv".
5865 * Return OK or FAIL.
5866 */
5867 int
5868exe_typval_instr(typval_T *tv, typval_T *rettv)
5869{
5870 ectx_T *ectx = tv->vval.v_instr->instr_ectx;
5871 isn_T *save_instr = ectx->ec_instr;
5872 int save_iidx = ectx->ec_iidx;
5873 int res;
5874
LemonBoyf3b48952022-05-05 13:53:03 +01005875 // Initialize rettv so that it is safe for caller to invoke clear_tv(rettv)
5876 // even when the compilation fails.
5877 rettv->v_type = VAR_UNKNOWN;
5878
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005879 ectx->ec_instr = tv->vval.v_instr->instr_instr;
5880 res = exec_instructions(ectx);
5881 if (res == OK)
5882 {
5883 *rettv = *STACK_TV_BOT(-1);
5884 --ectx->ec_stack.ga_len;
5885 }
5886
5887 ectx->ec_instr = save_instr;
5888 ectx->ec_iidx = save_iidx;
5889
5890 return res;
5891}
5892
5893/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02005894 * Execute the instructions from an ISN_SUBSTITUTE command, which are in
5895 * "substitute_instr".
5896 */
5897 char_u *
5898exe_substitute_instr(void)
5899{
5900 ectx_T *ectx = substitute_instr->subs_ectx;
5901 isn_T *save_instr = ectx->ec_instr;
5902 int save_iidx = ectx->ec_iidx;
5903 char_u *res;
5904
5905 ectx->ec_instr = substitute_instr->subs_instr;
5906 if (exec_instructions(ectx) == OK)
Bram Moolenaar90193e62021-04-04 20:49:50 +02005907 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005908 typval_T *tv = STACK_TV_BOT(-1);
5909
Bram Moolenaar27523602021-06-05 21:36:19 +02005910 res = typval2string(tv, TRUE);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005911 --ectx->ec_stack.ga_len;
5912 clear_tv(tv);
Bram Moolenaar90193e62021-04-04 20:49:50 +02005913 }
5914 else
5915 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005916 substitute_instr->subs_status = FAIL;
5917 res = vim_strsave((char_u *)"");
Bram Moolenaar90193e62021-04-04 20:49:50 +02005918 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005919
Bram Moolenaar4c137212021-04-19 16:48:48 +02005920 ectx->ec_instr = save_instr;
5921 ectx->ec_iidx = save_iidx;
5922
5923 return res;
5924}
5925
5926/*
5927 * Call a "def" function from old Vim script.
5928 * Return OK or FAIL.
5929 */
5930 int
5931call_def_function(
5932 ufunc_T *ufunc,
5933 int argc_arg, // nr of arguments
5934 typval_T *argv, // arguments
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005935 int flags, // DEF_ flags
Bram Moolenaar4c137212021-04-19 16:48:48 +02005936 partial_T *partial, // optional partial for context
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005937 object_T *object, // object, e.g. for this.Func()
Bram Moolenaar58779852022-09-06 18:31:14 +01005938 funccall_T *funccal,
Bram Moolenaar4c137212021-04-19 16:48:48 +02005939 typval_T *rettv) // return value
5940{
5941 ectx_T ectx; // execution context
5942 int argc = argc_arg;
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005943 int partial_argc = partial == NULL
5944 || (flags & DEF_USE_PT_ARGV) == 0
5945 ? 0 : partial->pt_argc;
5946 int total_argc = argc + partial_argc;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005947 typval_T *tv;
5948 int idx;
5949 int ret = FAIL;
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005950 int defcount = ufunc->uf_args.ga_len - total_argc;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005951 sctx_T save_current_sctx = current_sctx;
5952 int did_emsg_before = did_emsg_cumul + did_emsg;
5953 int save_suppress_errthrow = suppress_errthrow;
5954 msglist_T **saved_msg_list = NULL;
5955 msglist_T *private_msg_list = NULL;
5956 int save_emsg_silent_def = emsg_silent_def;
5957 int save_did_emsg_def = did_emsg_def;
5958 int orig_funcdepth;
Bram Moolenaare99d4222021-06-13 14:01:26 +02005959 int orig_nesting_level = ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005960
5961// Get pointer to item in the stack.
5962#undef STACK_TV
5963#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
5964
5965// Get pointer to item at the bottom of the stack, -1 is the bottom.
5966#undef STACK_TV_BOT
5967#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
5968
5969// Get pointer to a local variable on the stack. Negative for arguments.
5970#undef STACK_TV_VAR
5971#define STACK_TV_VAR(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_frame_idx + STACK_FRAME_SIZE + idx)
5972
5973 if (ufunc->uf_def_status == UF_NOT_COMPILED
5974 || ufunc->uf_def_status == UF_COMPILE_ERROR
Bram Moolenaar139575d2022-03-15 19:29:30 +00005975 || (func_needs_compiling(ufunc, get_compile_type(ufunc))
5976 && compile_def_function(ufunc, FALSE,
5977 get_compile_type(ufunc), NULL) == FAIL))
Bram Moolenaar4c137212021-04-19 16:48:48 +02005978 {
5979 if (did_emsg_cumul + did_emsg == did_emsg_before)
5980 semsg(_(e_function_is_not_compiled_str),
5981 printable_func_name(ufunc));
5982 return FAIL;
5983 }
5984
5985 {
5986 // Check the function was really compiled.
5987 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5988 + ufunc->uf_dfunc_idx;
Bram Moolenaarcc341812022-09-19 15:54:34 +01005989 if (dfunc->df_ufunc == NULL)
5990 {
5991 semsg(_(e_function_was_deleted_str), printable_func_name(ufunc));
5992 return FAIL;
5993 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005994 if (INSTRUCTIONS(dfunc) == NULL)
5995 {
5996 iemsg("using call_def_function() on not compiled function");
5997 return FAIL;
5998 }
5999 }
6000
6001 // If depth of calling is getting too high, don't execute the function.
6002 orig_funcdepth = funcdepth_get();
6003 if (funcdepth_increment() == FAIL)
6004 return FAIL;
6005
6006 CLEAR_FIELD(ectx);
6007 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
6008 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
Bram Moolenaar35578162021-08-02 19:10:38 +02006009 if (GA_GROW_FAILS(&ectx.ec_stack, 20))
Bram Moolenaar4c137212021-04-19 16:48:48 +02006010 {
6011 funcdepth_decrement();
6012 return FAIL;
6013 }
6014 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
6015 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
6016 ectx.ec_did_emsg_before = did_emsg_before;
Bram Moolenaare99d4222021-06-13 14:01:26 +02006017 ++ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006018
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01006019 idx = total_argc - ufunc->uf_args.ga_len;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006020 if (idx > 0 && ufunc->uf_va_name == NULL)
6021 {
Matvey Tarasovd14bb1a2022-06-29 13:18:27 +01006022 semsg(NGETTEXT(e_one_argument_too_many, e_nr_arguments_too_many,
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01006023 idx), idx);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006024 goto failed_early;
6025 }
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01006026 idx = total_argc - ufunc->uf_args.ga_len + ufunc->uf_def_args.ga_len;
Bram Moolenaarc6d71532021-06-05 18:49:38 +02006027 if (idx < 0)
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02006028 {
Matvey Tarasovd14bb1a2022-06-29 13:18:27 +01006029 semsg(NGETTEXT(e_one_argument_too_few, e_nr_arguments_too_few,
Bram Moolenaar98aff652022-09-06 21:02:35 +01006030 -idx), -idx);
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02006031 goto failed_early;
6032 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006033
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01006034 // Put values from the partial and arguments on the stack, but no more than
6035 // what the function expects. A lambda can be called with more arguments
6036 // than it uses.
6037 for (idx = 0; idx < total_argc
Bram Moolenaar4c137212021-04-19 16:48:48 +02006038 && (ufunc->uf_va_name != NULL || idx < ufunc->uf_args.ga_len);
6039 ++idx)
6040 {
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01006041 int argv_idx = idx - partial_argc;
6042
6043 tv = idx < partial_argc ? partial->pt_argv + idx : argv + argv_idx;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006044 if (idx >= ufunc->uf_args.ga_len - ufunc->uf_def_args.ga_len
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01006045 && tv->v_type == VAR_SPECIAL
6046 && tv->vval.v_number == VVAL_NONE)
Bram Moolenaar4c137212021-04-19 16:48:48 +02006047 {
6048 // Use the default value.
6049 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
6050 }
6051 else
6052 {
Bram Moolenaar47bba532023-01-20 18:49:46 +00006053 int done = FALSE;
6054 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len)
6055 {
6056 type_T *expected = ufunc->uf_arg_types[idx];
6057 if (expected->tt_type == VAR_FLOAT && tv->v_type == VAR_NUMBER)
6058 {
6059 // When a float is expected and a number was given, convert
6060 // the value.
6061 STACK_TV_BOT(0)->v_type = VAR_FLOAT;
6062 STACK_TV_BOT(0)->v_lock = 0;
6063 STACK_TV_BOT(0)->vval.v_float = tv->vval.v_number;
6064 done = TRUE;
6065 }
6066 else if (check_typval_arg_type(expected, tv,
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01006067 NULL, argv_idx + 1) == FAIL)
Bram Moolenaar47bba532023-01-20 18:49:46 +00006068 goto failed_early;
6069 }
6070 if (!done)
6071 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006072 }
6073 ++ectx.ec_stack.ga_len;
6074 }
6075
6076 // Turn varargs into a list. Empty list if no args.
6077 if (ufunc->uf_va_name != NULL)
6078 {
6079 int vararg_count = argc - ufunc->uf_args.ga_len;
6080
6081 if (vararg_count < 0)
6082 vararg_count = 0;
6083 else
6084 argc -= vararg_count;
6085 if (exe_newlist(vararg_count, &ectx) == FAIL)
6086 goto failed_early;
6087
6088 // Check the type of the list items.
6089 tv = STACK_TV_BOT(-1);
6090 if (ufunc->uf_va_type != NULL
6091 && ufunc->uf_va_type != &t_list_any
6092 && ufunc->uf_va_type->tt_member != &t_any
6093 && tv->vval.v_list != NULL)
6094 {
6095 type_T *expected = ufunc->uf_va_type->tt_member;
6096 listitem_T *li = tv->vval.v_list->lv_first;
6097
6098 for (idx = 0; idx < vararg_count; ++idx)
6099 {
6100 if (check_typval_arg_type(expected, &li->li_tv,
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02006101 NULL, argc + idx + 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02006102 goto failed_early;
6103 li = li->li_next;
6104 }
6105 }
6106
6107 if (defcount > 0)
6108 // Move varargs list to below missing default arguments.
6109 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
6110 --ectx.ec_stack.ga_len;
6111 }
6112
6113 // Make space for omitted arguments, will store default value below.
6114 // Any varargs list goes after them.
6115 if (defcount > 0)
6116 for (idx = 0; idx < defcount; ++idx)
6117 {
6118 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
6119 ++ectx.ec_stack.ga_len;
6120 }
6121 if (ufunc->uf_va_name != NULL)
6122 ++ectx.ec_stack.ga_len;
6123
6124 // Frame pointer points to just after arguments.
6125 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
6126 ectx.ec_initial_frame_idx = ectx.ec_frame_idx;
6127
6128 {
6129 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6130 + ufunc->uf_dfunc_idx;
6131 ufunc_T *base_ufunc = dfunc->df_ufunc;
6132
6133 // "uf_partial" is on the ufunc that "df_ufunc" points to, as is done
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006134 // by copy_lambda_to_global_func().
Bram Moolenaar4c137212021-04-19 16:48:48 +02006135 if (partial != NULL || base_ufunc->uf_partial != NULL)
6136 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006137 ectx.ec_outer_ref = ALLOC_CLEAR_ONE(outer_ref_T);
6138 if (ectx.ec_outer_ref == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02006139 goto failed_early;
6140 if (partial != NULL)
6141 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00006142 outer_T *outer = get_pt_outer(partial);
6143
Bram Moolenaar6d313be2022-09-22 16:36:25 +01006144 if (outer->out_stack == NULL && outer->out_loop_size == 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02006145 {
Bram Moolenaar6d313be2022-09-22 16:36:25 +01006146 // no stack was set
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00006147 if (current_ectx != NULL)
6148 {
6149 if (current_ectx->ec_outer_ref != NULL
6150 && current_ectx->ec_outer_ref->or_outer != NULL)
6151 ectx.ec_outer_ref->or_outer =
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006152 current_ectx->ec_outer_ref->or_outer;
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00006153 }
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01006154 // else: should there be an error here?
Bram Moolenaar4c137212021-04-19 16:48:48 +02006155 }
6156 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006157 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00006158 ectx.ec_outer_ref->or_outer = outer;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006159 ++partial->pt_refcount;
6160 ectx.ec_outer_ref->or_partial = partial;
6161 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006162 }
6163 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006164 {
6165 ectx.ec_outer_ref->or_outer = &base_ufunc->uf_partial->pt_outer;
6166 ++base_ufunc->uf_partial->pt_refcount;
6167 ectx.ec_outer_ref->or_partial = base_ufunc->uf_partial;
6168 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006169 }
6170 }
6171
6172 // dummy frame entries
6173 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
6174 {
6175 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
6176 ++ectx.ec_stack.ga_len;
6177 }
6178
6179 {
6180 // Reserve space for local variables and any closure reference count.
6181 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6182 + ufunc->uf_dfunc_idx;
6183
Bram Moolenaar5cd64792021-12-25 18:23:24 +00006184 // Initialize variables to zero. That avoids having to generate
6185 // initializing instructions for "var nr: number", "var x: any", etc.
Bram Moolenaar4c137212021-04-19 16:48:48 +02006186 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaar5cd64792021-12-25 18:23:24 +00006187 {
6188 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
6189 STACK_TV_VAR(idx)->vval.v_number = 0;
6190 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006191 ectx.ec_stack.ga_len += dfunc->df_varcount;
Bram Moolenaarffdaca92022-12-09 21:41:48 +00006192
6193 if (object != NULL)
6194 {
6195 // the object is always the variable at index zero
6196 tv = STACK_TV_VAR(0);
6197 tv->v_type = VAR_OBJECT;
6198 tv->vval.v_object = object;
6199 }
6200
Bram Moolenaar4c137212021-04-19 16:48:48 +02006201 if (dfunc->df_has_closure)
6202 {
Bram Moolenaarcc341812022-09-19 15:54:34 +01006203 // Initialize the variable that counts how many closures were
6204 // created. This is used in handle_closure_in_use().
Bram Moolenaar4c137212021-04-19 16:48:48 +02006205 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
6206 STACK_TV_VAR(idx)->vval.v_number = 0;
6207 ++ectx.ec_stack.ga_len;
6208 }
6209
6210 ectx.ec_instr = INSTRUCTIONS(dfunc);
6211 }
6212
Bram Moolenaar58779852022-09-06 18:31:14 +01006213 // Store the execution context in funccal, used by invoke_all_defer().
6214 if (funccal != NULL)
6215 funccal->fc_ectx = &ectx;
6216
Bram Moolenaar4c137212021-04-19 16:48:48 +02006217 // Following errors are in the function, not the caller.
6218 // Commands behave like vim9script.
6219 estack_push_ufunc(ufunc, 1);
6220 current_sctx = ufunc->uf_script_ctx;
6221 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
6222
6223 // Use a specific location for storing error messages to be converted to an
6224 // exception.
6225 saved_msg_list = msg_list;
6226 msg_list = &private_msg_list;
6227
6228 // Do turn errors into exceptions.
6229 suppress_errthrow = FALSE;
6230
6231 // Do not delete the function while executing it.
6232 ++ufunc->uf_calls;
6233
6234 // When ":silent!" was used before calling then we still abort the
6235 // function. If ":silent!" is used in the function then we don't.
6236 emsg_silent_def = emsg_silent;
6237 did_emsg_def = 0;
6238
LemonBoyc5d27442023-08-19 13:02:35 +02006239 ectx.ec_where = (where_T)WHERE_INIT;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006240
Bram Moolenaar5800c792022-09-22 17:34:01 +01006241 /*
6242 * Execute the instructions until done.
6243 */
Bram Moolenaar4c137212021-04-19 16:48:48 +02006244 ret = exec_instructions(&ectx);
6245 if (ret == OK)
6246 {
6247 // function finished, get result from the stack.
6248 if (ufunc->uf_ret_type == &t_void)
6249 {
6250 rettv->v_type = VAR_VOID;
6251 }
6252 else
6253 {
6254 tv = STACK_TV_BOT(-1);
6255 *rettv = *tv;
6256 tv->v_type = VAR_UNKNOWN;
6257 }
6258 }
6259
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01006260 // When failed need to unwind the call stack.
Bram Moolenaar58779852022-09-06 18:31:14 +01006261 unwind_def_callstack(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02006262
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02006263 // Deal with any remaining closures, they may be in use somewhere.
6264 if (ectx.ec_funcrefs.ga_len > 0)
Bram Moolenaarf112f302020-12-20 17:47:52 +01006265 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02006266 handle_closure_in_use(&ectx, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00006267 ga_clear(&ectx.ec_funcrefs);
Bram Moolenaarf112f302020-12-20 17:47:52 +01006268 }
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02006269
Bram Moolenaaree8580e2020-08-28 17:19:07 +02006270 estack_pop();
6271 current_sctx = save_current_sctx;
6272
Bram Moolenaar2336c372021-12-06 15:06:54 +00006273 if (--ufunc->uf_calls <= 0 && ufunc->uf_refcount <= 0)
6274 // Function was unreferenced while being used, free it now.
6275 func_clear_free(ufunc, FALSE);
Bram Moolenaarc970e422021-03-17 15:03:04 +01006276
Bram Moolenaar352134b2020-10-17 22:04:08 +02006277 if (*msg_list != NULL && saved_msg_list != NULL)
6278 {
6279 msglist_T **plist = saved_msg_list;
6280
6281 // Append entries from the current msg_list (uncaught exceptions) to
6282 // the saved msg_list.
6283 while (*plist != NULL)
6284 plist = &(*plist)->next;
6285
6286 *plist = *msg_list;
6287 }
6288 msg_list = saved_msg_list;
6289
Bram Moolenaar4c137212021-04-19 16:48:48 +02006290 if (ectx.ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar02194d22020-10-24 23:08:38 +02006291 {
6292 cmdmod.cmod_filter_regmatch.regprog = NULL;
6293 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006294 cmdmod = ectx.ec_funclocal.floc_save_cmdmod;
Bram Moolenaar02194d22020-10-24 23:08:38 +02006295 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +01006296 emsg_silent_def = save_emsg_silent_def;
6297 did_emsg_def += save_did_emsg_def;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02006298
Bram Moolenaaree8580e2020-08-28 17:19:07 +02006299failed_early:
Bram Moolenaar0d1f55c2022-04-05 17:30:29 +01006300 // Free all arguments and local variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006301 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
Bram Moolenaar1936c762022-09-28 15:19:10 +01006302 {
6303 tv = STACK_TV(idx);
6304 if (tv->v_type != VAR_NUMBER && tv->v_type != VAR_UNKNOWN)
6305 clear_tv(tv);
6306 }
Bram Moolenaare99d4222021-06-13 14:01:26 +02006307 ex_nesting_level = orig_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02006308
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006309 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01006310 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006311 if (ectx.ec_outer_ref != NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +01006312 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006313 if (ectx.ec_outer_ref->or_outer_allocated)
6314 vim_free(ectx.ec_outer_ref->or_outer);
6315 partial_unref(ectx.ec_outer_ref->or_partial);
6316 vim_free(ectx.ec_outer_ref);
Bram Moolenaar0186e582021-01-10 18:33:11 +01006317 }
6318
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02006319 // Not sure if this is necessary.
6320 suppress_errthrow = save_suppress_errthrow;
6321
Bram Moolenaarb5841b92021-07-15 18:09:53 +02006322 if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before
6323 && !need_rethrow)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006324 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02006325 printable_func_name(ufunc));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01006326 funcdepth_restore(orig_funcdepth);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006327 return ret;
6328}
6329
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006330/*
Bram Moolenaar58779852022-09-06 18:31:14 +01006331 * Called when a def function has finished (possibly failed).
6332 * Invoke all the function returns to clean up and invoke deferred functions,
6333 * except the toplevel one.
6334 */
6335 void
6336unwind_def_callstack(ectx_T *ectx)
6337{
6338 while (ectx->ec_frame_idx != ectx->ec_initial_frame_idx)
6339 func_return(ectx);
6340}
6341
6342/*
dundargocc57b5bc2022-11-02 13:30:51 +00006343 * Invoke any deferred functions for the top function in "ectx".
Bram Moolenaar58779852022-09-06 18:31:14 +01006344 */
6345 void
6346may_invoke_defer_funcs(ectx_T *ectx)
6347{
6348 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
6349
6350 if (dfunc->df_defer_var_idx > 0)
6351 invoke_defer_funcs(ectx);
6352}
6353
6354/*
Bram Moolenaarcc341812022-09-19 15:54:34 +01006355 * Return loopvarinfo in a printable form in allocated memory.
6356 */
6357 static char_u *
6358printable_loopvarinfo(loopvarinfo_T *lvi)
6359{
6360 garray_T ga;
6361 int depth;
6362
6363 ga_init2(&ga, 1, 100);
6364 for (depth = 0; depth < lvi->lvi_depth; ++depth)
6365 {
6366 if (ga_grow(&ga, 50) == FAIL)
6367 break;
6368 if (lvi->lvi_loop[depth].var_idx == 0)
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +01006369 STRCPY((char *)ga.ga_data + ga.ga_len, " -");
Bram Moolenaarcc341812022-09-19 15:54:34 +01006370 else
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +01006371 vim_snprintf((char *)ga.ga_data + ga.ga_len, 50, " $%d-$%d",
Bram Moolenaarcc341812022-09-19 15:54:34 +01006372 lvi->lvi_loop[depth].var_idx,
6373 lvi->lvi_loop[depth].var_idx
6374 + lvi->lvi_loop[depth].var_count - 1);
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +01006375 ga.ga_len = (int)STRLEN(ga.ga_data);
Bram Moolenaarcc341812022-09-19 15:54:34 +01006376 }
6377 return ga.ga_data;
6378}
6379
6380/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02006381 * List instructions "instr" up to "instr_count" or until ISN_FINISH.
6382 * "ufunc" has the source lines, NULL for the instructions of ISN_SUBSTITUTE.
6383 * "pfx" is prefixed to every line.
6384 */
6385 static void
6386list_instructions(char *pfx, isn_T *instr, int instr_count, ufunc_T *ufunc)
6387{
6388 int line_idx = 0;
6389 int prev_current = 0;
6390 int current;
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02006391 int def_arg_idx = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006392
6393 for (current = 0; current < instr_count; ++current)
6394 {
6395 isn_T *iptr = &instr[current];
6396 char *line;
6397
6398 if (ufunc != NULL)
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02006399 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02006400 while (line_idx < iptr->isn_lnum
6401 && line_idx < ufunc->uf_lines.ga_len)
6402 {
6403 if (current > prev_current)
6404 {
6405 msg_puts("\n\n");
6406 prev_current = current;
6407 }
6408 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
6409 if (line != NULL)
6410 msg(line);
6411 }
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02006412 if (iptr->isn_type == ISN_JUMP_IF_ARG_SET)
6413 {
6414 int first_def_arg = ufunc->uf_args.ga_len
6415 - ufunc->uf_def_args.ga_len;
6416
6417 if (def_arg_idx > 0)
6418 msg_puts("\n\n");
6419 msg_start();
6420 msg_puts(" ");
6421 msg_puts(((char **)(ufunc->uf_args.ga_data))[
6422 first_def_arg + def_arg_idx]);
6423 msg_puts(" = ");
6424 msg_puts(((char **)(ufunc->uf_def_args.ga_data))[def_arg_idx++]);
6425 msg_clr_eos();
6426 msg_end();
6427 }
6428 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006429
6430 switch (iptr->isn_type)
6431 {
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006432 case ISN_CONSTRUCT:
6433 smsg("%s%4d NEW %s size %d", pfx, current,
6434 iptr->isn_arg.construct.construct_class->class_name,
6435 (int)iptr->isn_arg.construct.construct_size);
6436 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006437 case ISN_EXEC:
6438 smsg("%s%4d EXEC %s", pfx, current, iptr->isn_arg.string);
6439 break;
Bram Moolenaar20677332021-06-06 17:02:53 +02006440 case ISN_EXEC_SPLIT:
6441 smsg("%s%4d EXEC_SPLIT %s", pfx, current, iptr->isn_arg.string);
6442 break;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00006443 case ISN_EXECRANGE:
6444 smsg("%s%4d EXECRANGE %s", pfx, current, iptr->isn_arg.string);
6445 break;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02006446 case ISN_LEGACY_EVAL:
6447 smsg("%s%4d EVAL legacy %s", pfx, current,
6448 iptr->isn_arg.string);
6449 break;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02006450 case ISN_REDIRSTART:
6451 smsg("%s%4d REDIR", pfx, current);
6452 break;
6453 case ISN_REDIREND:
6454 smsg("%s%4d REDIR END%s", pfx, current,
6455 iptr->isn_arg.number ? " append" : "");
6456 break;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006457 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02006458#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006459 smsg("%s%4d CEXPR pre %s", pfx, current,
6460 cexpr_get_auname(iptr->isn_arg.number));
Bram Moolenaarb7c97812021-05-05 22:51:39 +02006461#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006462 break;
6463 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02006464#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006465 {
6466 cexprref_T *cer = iptr->isn_arg.cexpr.cexpr_ref;
6467
6468 smsg("%s%4d CEXPR core %s%s \"%s\"", pfx, current,
6469 cexpr_get_auname(cer->cer_cmdidx),
6470 cer->cer_forceit ? "!" : "",
6471 cer->cer_cmdline);
6472 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02006473#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006474 break;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006475 case ISN_INSTR:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006476 smsg("%s%4d INSTR", pfx, current);
6477 list_instructions(" ", iptr->isn_arg.instr, INT_MAX, NULL);
6478 msg(" -------------");
6479 break;
6480 case ISN_SOURCE:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006481 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006482 scriptitem_T *si = SCRIPT_ITEM(iptr->isn_arg.number);
6483
6484 smsg("%s%4d SOURCE %s", pfx, current, si->sn_name);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006485 }
6486 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006487 case ISN_SUBSTITUTE:
6488 {
6489 subs_T *subs = &iptr->isn_arg.subs;
6490
6491 smsg("%s%4d SUBSTITUTE %s", pfx, current, subs->subs_cmd);
6492 list_instructions(" ", subs->subs_instr, INT_MAX, NULL);
6493 msg(" -------------");
6494 }
6495 break;
6496 case ISN_EXECCONCAT:
6497 smsg("%s%4d EXECCONCAT %lld", pfx, current,
6498 (varnumber_T)iptr->isn_arg.number);
6499 break;
6500 case ISN_ECHO:
6501 {
6502 echo_T *echo = &iptr->isn_arg.echo;
6503
6504 smsg("%s%4d %s %d", pfx, current,
6505 echo->echo_with_white ? "ECHO" : "ECHON",
6506 echo->echo_count);
6507 }
6508 break;
6509 case ISN_EXECUTE:
6510 smsg("%s%4d EXECUTE %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02006511 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006512 break;
6513 case ISN_ECHOMSG:
6514 smsg("%s%4d ECHOMSG %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02006515 (varnumber_T)(iptr->isn_arg.number));
6516 break;
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01006517 case ISN_ECHOWINDOW:
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01006518 if (iptr->isn_arg.echowin.ewin_time > 0)
6519 smsg("%s%4d ECHOWINDOW %d (%ld sec)", pfx, current,
6520 iptr->isn_arg.echowin.ewin_count,
6521 iptr->isn_arg.echowin.ewin_time);
6522 else
6523 smsg("%s%4d ECHOWINDOW %d", pfx, current,
6524 iptr->isn_arg.echowin.ewin_count);
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01006525 break;
Bram Moolenaar7de62622021-08-07 15:05:47 +02006526 case ISN_ECHOCONSOLE:
6527 smsg("%s%4d ECHOCONSOLE %lld", pfx, current,
6528 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006529 break;
6530 case ISN_ECHOERR:
6531 smsg("%s%4d ECHOERR %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02006532 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006533 break;
6534 case ISN_LOAD:
6535 {
6536 if (iptr->isn_arg.number < 0)
6537 smsg("%s%4d LOAD arg[%lld]", pfx, current,
6538 (varnumber_T)(iptr->isn_arg.number
6539 + STACK_FRAME_SIZE));
6540 else
6541 smsg("%s%4d LOAD $%lld", pfx, current,
6542 (varnumber_T)(iptr->isn_arg.number));
6543 }
6544 break;
6545 case ISN_LOADOUTER:
6546 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006547 isn_outer_T *outer = &iptr->isn_arg.outer;
6548
6549 if (outer->outer_idx < 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02006550 smsg("%s%4d LOADOUTER level %d arg[%d]", pfx, current,
Bram Moolenaarcc341812022-09-19 15:54:34 +01006551 outer->outer_depth,
6552 outer->outer_idx + STACK_FRAME_SIZE);
6553 else if (outer->outer_depth < 0)
6554 smsg("%s%4d LOADOUTER $%d in loop level %d",
6555 pfx, current,
6556 outer->outer_idx,
6557 -outer->outer_depth);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006558 else
6559 smsg("%s%4d LOADOUTER level %d $%d", pfx, current,
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006560 outer->outer_depth,
6561 outer->outer_idx);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006562 }
6563 break;
6564 case ISN_LOADV:
6565 smsg("%s%4d LOADV v:%s", pfx, current,
6566 get_vim_var_name(iptr->isn_arg.number));
6567 break;
6568 case ISN_LOADSCRIPT:
6569 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006570 scriptref_T *sref = iptr->isn_arg.script.scriptref;
6571 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
6572 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006573
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006574 sv = get_script_svar(sref, -1);
6575 if (sv == NULL)
6576 smsg("%s%4d LOADSCRIPT [deleted] from %s",
6577 pfx, current, si->sn_name);
6578 else
6579 smsg("%s%4d LOADSCRIPT %s-%d from %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02006580 sv->sv_name,
6581 sref->sref_idx,
6582 si->sn_name);
6583 }
6584 break;
6585 case ISN_LOADS:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006586 case ISN_LOADEXPORT:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006587 {
6588 scriptitem_T *si = SCRIPT_ITEM(
6589 iptr->isn_arg.loadstore.ls_sid);
6590
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006591 smsg("%s%4d %s s:%s from %s", pfx, current,
6592 iptr->isn_type == ISN_LOADS ? "LOADS"
6593 : "LOADEXPORT",
6594 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006595 }
6596 break;
6597 case ISN_LOADAUTO:
6598 smsg("%s%4d LOADAUTO %s", pfx, current, iptr->isn_arg.string);
6599 break;
6600 case ISN_LOADG:
6601 smsg("%s%4d LOADG g:%s", pfx, current, iptr->isn_arg.string);
6602 break;
6603 case ISN_LOADB:
6604 smsg("%s%4d LOADB b:%s", pfx, current, iptr->isn_arg.string);
6605 break;
6606 case ISN_LOADW:
6607 smsg("%s%4d LOADW w:%s", pfx, current, iptr->isn_arg.string);
6608 break;
6609 case ISN_LOADT:
6610 smsg("%s%4d LOADT t:%s", pfx, current, iptr->isn_arg.string);
6611 break;
6612 case ISN_LOADGDICT:
6613 smsg("%s%4d LOAD g:", pfx, current);
6614 break;
6615 case ISN_LOADBDICT:
6616 smsg("%s%4d LOAD b:", pfx, current);
6617 break;
6618 case ISN_LOADWDICT:
6619 smsg("%s%4d LOAD w:", pfx, current);
6620 break;
6621 case ISN_LOADTDICT:
6622 smsg("%s%4d LOAD t:", pfx, current);
6623 break;
6624 case ISN_LOADOPT:
6625 smsg("%s%4d LOADOPT %s", pfx, current, iptr->isn_arg.string);
6626 break;
6627 case ISN_LOADENV:
6628 smsg("%s%4d LOADENV %s", pfx, current, iptr->isn_arg.string);
6629 break;
6630 case ISN_LOADREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006631 smsg("%s%4d LOADREG @%c", pfx, current,
6632 (int)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006633 break;
6634
6635 case ISN_STORE:
6636 if (iptr->isn_arg.number < 0)
6637 smsg("%s%4d STORE arg[%lld]", pfx, current,
6638 iptr->isn_arg.number + STACK_FRAME_SIZE);
6639 else
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006640 smsg("%s%4d STORE $%lld", pfx, current,
6641 iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006642 break;
6643 case ISN_STOREOUTER:
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006644 {
6645 isn_outer_T *outer = &iptr->isn_arg.outer;
6646
6647 if (outer->outer_depth == OUTER_LOOP_DEPTH)
6648 smsg("%s%4d STOREOUTER level 1 $%d in loop",
6649 pfx, current, outer->outer_idx);
6650 else
6651 smsg("%s%4d STOREOUTER level %d $%d", pfx, current,
6652 outer->outer_depth, outer->outer_idx);
6653 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006654 break;
6655 case ISN_STOREV:
6656 smsg("%s%4d STOREV v:%s", pfx, current,
6657 get_vim_var_name(iptr->isn_arg.number));
6658 break;
6659 case ISN_STOREAUTO:
6660 smsg("%s%4d STOREAUTO %s", pfx, current, iptr->isn_arg.string);
6661 break;
6662 case ISN_STOREG:
6663 smsg("%s%4d STOREG %s", pfx, current, iptr->isn_arg.string);
6664 break;
6665 case ISN_STOREB:
6666 smsg("%s%4d STOREB %s", pfx, current, iptr->isn_arg.string);
6667 break;
6668 case ISN_STOREW:
6669 smsg("%s%4d STOREW %s", pfx, current, iptr->isn_arg.string);
6670 break;
6671 case ISN_STORET:
6672 smsg("%s%4d STORET %s", pfx, current, iptr->isn_arg.string);
6673 break;
6674 case ISN_STORES:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006675 case ISN_STOREEXPORT:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006676 {
6677 scriptitem_T *si = SCRIPT_ITEM(
6678 iptr->isn_arg.loadstore.ls_sid);
6679
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006680 smsg("%s%4d %s %s in %s", pfx, current,
6681 iptr->isn_type == ISN_STORES
6682 ? "STORES" : "STOREEXPORT",
Bram Moolenaar4c137212021-04-19 16:48:48 +02006683 iptr->isn_arg.loadstore.ls_name, si->sn_name);
6684 }
6685 break;
6686 case ISN_STORESCRIPT:
6687 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006688 scriptref_T *sref = iptr->isn_arg.script.scriptref;
6689 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
6690 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006691
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006692 sv = get_script_svar(sref, -1);
6693 if (sv == NULL)
6694 smsg("%s%4d STORESCRIPT [deleted] in %s",
6695 pfx, current, si->sn_name);
6696 else
6697 smsg("%s%4d STORESCRIPT %s-%d in %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02006698 sv->sv_name,
6699 sref->sref_idx,
6700 si->sn_name);
6701 }
6702 break;
6703 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00006704 case ISN_STOREFUNCOPT:
6705 smsg("%s%4d %s &%s", pfx, current,
6706 iptr->isn_type == ISN_STOREOPT ? "STOREOPT" : "STOREFUNCOPT",
Bram Moolenaar4c137212021-04-19 16:48:48 +02006707 iptr->isn_arg.storeopt.so_name);
6708 break;
6709 case ISN_STOREENV:
6710 smsg("%s%4d STOREENV $%s", pfx, current, iptr->isn_arg.string);
6711 break;
6712 case ISN_STOREREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006713 smsg("%s%4d STOREREG @%c", pfx, current,
6714 (int)iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006715 break;
6716 case ISN_STORENR:
6717 smsg("%s%4d STORE %lld in $%d", pfx, current,
6718 iptr->isn_arg.storenr.stnr_val,
6719 iptr->isn_arg.storenr.stnr_idx);
6720 break;
6721
6722 case ISN_STOREINDEX:
6723 smsg("%s%4d STOREINDEX %s", pfx, current,
Bram Moolenaarf7d1c6e2023-01-16 20:47:57 +00006724 vartype_name(iptr->isn_arg.storeindex.si_vartype));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006725 break;
6726
6727 case ISN_STORERANGE:
6728 smsg("%s%4d STORERANGE", pfx, current);
6729 break;
6730
Bram Moolenaard505d172022-12-18 21:42:55 +00006731 case ISN_LOAD_CLASSMEMBER:
6732 case ISN_STORE_CLASSMEMBER:
6733 {
6734 class_T *cl = iptr->isn_arg.classmember.cm_class;
6735 int idx = iptr->isn_arg.classmember.cm_idx;
6736 ocmember_T *ocm = &cl->class_class_members[idx];
6737 smsg("%s%4d %s CLASSMEMBER %s.%s", pfx, current,
6738 iptr->isn_type == ISN_LOAD_CLASSMEMBER
6739 ? "LOAD" : "STORE",
6740 cl->class_name, ocm->ocm_name);
6741 }
6742 break;
6743
Bram Moolenaar4c137212021-04-19 16:48:48 +02006744 // constants
6745 case ISN_PUSHNR:
6746 smsg("%s%4d PUSHNR %lld", pfx, current,
6747 (varnumber_T)(iptr->isn_arg.number));
6748 break;
6749 case ISN_PUSHBOOL:
6750 case ISN_PUSHSPEC:
6751 smsg("%s%4d PUSH %s", pfx, current,
6752 get_var_special_name(iptr->isn_arg.number));
6753 break;
6754 case ISN_PUSHF:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006755 smsg("%s%4d PUSHF %g", pfx, current, iptr->isn_arg.fnumber);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006756 break;
6757 case ISN_PUSHS:
6758 smsg("%s%4d PUSHS \"%s\"", pfx, current, iptr->isn_arg.string);
6759 break;
6760 case ISN_PUSHBLOB:
6761 {
6762 char_u *r;
6763 char_u numbuf[NUMBUFLEN];
6764 char_u *tofree;
6765
6766 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
6767 smsg("%s%4d PUSHBLOB %s", pfx, current, r);
6768 vim_free(tofree);
6769 }
6770 break;
6771 case ISN_PUSHFUNC:
6772 {
6773 char *name = (char *)iptr->isn_arg.string;
6774
6775 smsg("%s%4d PUSHFUNC \"%s\"", pfx, current,
6776 name == NULL ? "[none]" : name);
6777 }
6778 break;
6779 case ISN_PUSHCHANNEL:
6780#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar397a87a2022-03-20 21:14:15 +00006781 smsg("%s%4d PUSHCHANNEL 0", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006782#endif
6783 break;
6784 case ISN_PUSHJOB:
6785#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar397a87a2022-03-20 21:14:15 +00006786 smsg("%s%4d PUSHJOB \"no process\"", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006787#endif
6788 break;
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00006789 case ISN_PUSHOBJ:
6790 smsg("%s%4d PUSHOBJ null", pfx, current);
6791 break;
6792 case ISN_PUSHCLASS:
6793 smsg("%s%4d PUSHCLASS %s", pfx, current,
Bram Moolenaar30a84472023-02-27 08:07:14 +00006794 iptr->isn_arg.classarg == NULL ? "null"
6795 : (char *)iptr->isn_arg.classarg->class_name);
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00006796 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006797 case ISN_PUSHEXC:
6798 smsg("%s%4d PUSH v:exception", pfx, current);
6799 break;
Bram Moolenaar06b77222022-01-25 15:51:56 +00006800 case ISN_AUTOLOAD:
6801 smsg("%s%4d AUTOLOAD %s", pfx, current, iptr->isn_arg.string);
6802 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006803 case ISN_UNLET:
6804 smsg("%s%4d UNLET%s %s", pfx, current,
6805 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
6806 iptr->isn_arg.unlet.ul_name);
6807 break;
6808 case ISN_UNLETENV:
6809 smsg("%s%4d UNLETENV%s $%s", pfx, current,
6810 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
6811 iptr->isn_arg.unlet.ul_name);
6812 break;
6813 case ISN_UNLETINDEX:
6814 smsg("%s%4d UNLETINDEX", pfx, current);
6815 break;
6816 case ISN_UNLETRANGE:
6817 smsg("%s%4d UNLETRANGE", pfx, current);
6818 break;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02006819 case ISN_LOCKUNLOCK:
6820 smsg("%s%4d LOCKUNLOCK %s", pfx, current, iptr->isn_arg.string);
6821 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006822 case ISN_LOCKCONST:
6823 smsg("%s%4d LOCKCONST", pfx, current);
6824 break;
6825 case ISN_NEWLIST:
6826 smsg("%s%4d NEWLIST size %lld", pfx, current,
Bram Moolenaar1936c762022-09-28 15:19:10 +01006827 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006828 break;
6829 case ISN_NEWDICT:
6830 smsg("%s%4d NEWDICT size %lld", pfx, current,
Bram Moolenaar1936c762022-09-28 15:19:10 +01006831 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006832 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00006833 case ISN_NEWPARTIAL:
6834 smsg("%s%4d NEWPARTIAL", pfx, current);
6835 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006836
6837 // function call
6838 case ISN_BCALL:
6839 {
6840 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
6841
6842 smsg("%s%4d BCALL %s(argc %d)", pfx, current,
6843 internal_func_name(cbfunc->cbf_idx),
6844 cbfunc->cbf_argcount);
6845 }
6846 break;
6847 case ISN_DCALL:
6848 {
6849 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
6850 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
6851 + cdfunc->cdf_idx;
6852
6853 smsg("%s%4d DCALL %s(argc %d)", pfx, current,
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006854 printable_func_name(df->df_ufunc),
6855 cdfunc->cdf_argcount);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006856 }
6857 break;
Bram Moolenaard0200c82023-01-28 15:19:40 +00006858 case ISN_METHODCALL:
6859 {
6860 cmfunc_T *mfunc = iptr->isn_arg.mfunc;
6861
6862 smsg("%s%4d METHODCALL %s.%s(argc %d)", pfx, current,
6863 mfunc->cmf_itf->class_name,
6864 mfunc->cmf_itf->class_obj_methods[
6865 mfunc->cmf_idx]->uf_name,
6866 mfunc->cmf_argcount);
6867 }
6868 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006869 case ISN_UCALL:
6870 {
6871 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
6872
6873 smsg("%s%4d UCALL %s(argc %d)", pfx, current,
6874 cufunc->cuf_name, cufunc->cuf_argcount);
6875 }
6876 break;
6877 case ISN_PCALL:
6878 {
6879 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
6880
6881 smsg("%s%4d PCALL%s (argc %d)", pfx, current,
6882 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
6883 }
6884 break;
6885 case ISN_PCALL_END:
6886 smsg("%s%4d PCALL end", pfx, current);
6887 break;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006888 case ISN_DEFER:
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00006889 case ISN_DEFEROBJ:
6890 smsg("%s%4d %s %d args", pfx, current,
6891 iptr->isn_type == ISN_DEFER ? "DEFER" : "DEFEROBJ",
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006892 (int)iptr->isn_arg.defer.defer_argcount);
6893 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006894 case ISN_RETURN:
6895 smsg("%s%4d RETURN", pfx, current);
6896 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02006897 case ISN_RETURN_VOID:
6898 smsg("%s%4d RETURN void", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006899 break;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006900 case ISN_RETURN_OBJECT:
6901 smsg("%s%4d RETURN object", pfx, current);
6902 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006903 case ISN_FUNCREF:
6904 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006905 funcref_T *funcref = &iptr->isn_arg.funcref;
6906 funcref_extra_T *extra = funcref->fr_extra;
6907 char_u *name;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006908
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006909 if (extra == NULL || extra->fre_func_name == NULL)
Bram Moolenaar38453522021-11-28 22:00:12 +00006910 {
6911 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
6912 + funcref->fr_dfunc_idx;
6913 name = df->df_ufunc->uf_name;
6914 }
6915 else
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006916 name = extra->fre_func_name;
Bram Moolenaar313e4722023-02-08 20:55:27 +00006917 if (extra != NULL && extra->fre_class != NULL)
6918 {
6919 smsg("%s%4d FUNCREF %s.%s", pfx, current,
6920 extra->fre_class->class_name, name);
6921 }
6922 else if (extra == NULL
6923 || extra->fre_loopvar_info.lvi_depth == 0)
6924 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006925 smsg("%s%4d FUNCREF %s", pfx, current, name);
Bram Moolenaar313e4722023-02-08 20:55:27 +00006926 }
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006927 else
Bram Moolenaarcc341812022-09-19 15:54:34 +01006928 {
6929 char_u *info = printable_loopvarinfo(
6930 &extra->fre_loopvar_info);
6931
6932 smsg("%s%4d FUNCREF %s vars %s", pfx, current,
6933 name, info);
6934 vim_free(info);
6935 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006936 }
6937 break;
6938
6939 case ISN_NEWFUNC:
6940 {
Bram Moolenaarcc341812022-09-19 15:54:34 +01006941 newfuncarg_T *arg = iptr->isn_arg.newfunc.nf_arg;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006942
Bram Moolenaarcc341812022-09-19 15:54:34 +01006943 if (arg->nfa_loopvar_info.lvi_depth == 0)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006944 smsg("%s%4d NEWFUNC %s %s", pfx, current,
6945 arg->nfa_lambda, arg->nfa_global);
6946 else
Bram Moolenaarcc341812022-09-19 15:54:34 +01006947 {
6948 char_u *info = printable_loopvarinfo(
6949 &arg->nfa_loopvar_info);
6950
6951 smsg("%s%4d NEWFUNC %s %s vars %s", pfx, current,
6952 arg->nfa_lambda, arg->nfa_global, info);
6953 vim_free(info);
6954 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006955 }
6956 break;
6957
6958 case ISN_DEF:
6959 {
6960 char_u *name = iptr->isn_arg.string;
6961
6962 smsg("%s%4d DEF %s", pfx, current,
6963 name == NULL ? (char_u *)"" : name);
6964 }
6965 break;
6966
6967 case ISN_JUMP:
6968 {
6969 char *when = "?";
6970
6971 switch (iptr->isn_arg.jump.jump_when)
6972 {
6973 case JUMP_ALWAYS:
6974 when = "JUMP";
6975 break;
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02006976 case JUMP_NEVER:
6977 iemsg("JUMP_NEVER should not be used");
6978 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006979 case JUMP_AND_KEEP_IF_TRUE:
6980 when = "JUMP_AND_KEEP_IF_TRUE";
6981 break;
6982 case JUMP_IF_FALSE:
6983 when = "JUMP_IF_FALSE";
6984 break;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01006985 case JUMP_WHILE_FALSE:
6986 when = "JUMP_WHILE_FALSE"; // unused
6987 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006988 case JUMP_IF_COND_FALSE:
6989 when = "JUMP_IF_COND_FALSE";
6990 break;
6991 case JUMP_IF_COND_TRUE:
6992 when = "JUMP_IF_COND_TRUE";
6993 break;
6994 }
6995 smsg("%s%4d %s -> %d", pfx, current, when,
6996 iptr->isn_arg.jump.jump_where);
6997 }
6998 break;
6999
7000 case ISN_JUMP_IF_ARG_SET:
7001 smsg("%s%4d JUMP_IF_ARG_SET arg[%d] -> %d", pfx, current,
7002 iptr->isn_arg.jumparg.jump_arg_off + STACK_FRAME_SIZE,
7003 iptr->isn_arg.jump.jump_where);
7004 break;
7005
Bram Moolenaar65b0d162022-12-13 18:43:22 +00007006 case ISN_JUMP_IF_ARG_NOT_SET:
7007 smsg("%s%4d JUMP_IF_ARG_NOT_SET arg[%d] -> %d", pfx, current,
7008 iptr->isn_arg.jumparg.jump_arg_off + STACK_FRAME_SIZE,
7009 iptr->isn_arg.jump.jump_where);
7010 break;
7011
Bram Moolenaar4c137212021-04-19 16:48:48 +02007012 case ISN_FOR:
7013 {
7014 forloop_T *forloop = &iptr->isn_arg.forloop;
7015
7016 smsg("%s%4d FOR $%d -> %d", pfx, current,
Bram Moolenaarcc341812022-09-19 15:54:34 +01007017 forloop->for_loop_idx, forloop->for_end);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007018 }
7019 break;
7020
Bram Moolenaarb46c0832022-09-15 17:19:37 +01007021 case ISN_ENDLOOP:
7022 {
7023 endloop_T *endloop = &iptr->isn_arg.endloop;
7024
Bram Moolenaarcc341812022-09-19 15:54:34 +01007025 smsg("%s%4d ENDLOOP ref $%d save $%d-$%d depth %d",
7026 pfx, current,
Bram Moolenaarb46c0832022-09-15 17:19:37 +01007027 endloop->end_funcref_idx,
7028 endloop->end_var_idx,
Bram Moolenaarcc341812022-09-19 15:54:34 +01007029 endloop->end_var_idx + endloop->end_var_count - 1,
7030 endloop->end_depth);
Bram Moolenaarb46c0832022-09-15 17:19:37 +01007031 }
7032 break;
7033
7034 case ISN_WHILE:
7035 {
7036 whileloop_T *whileloop = &iptr->isn_arg.whileloop;
7037
7038 smsg("%s%4d WHILE $%d -> %d", pfx, current,
7039 whileloop->while_funcref_idx,
7040 whileloop->while_end);
7041 }
7042 break;
7043
Bram Moolenaar4c137212021-04-19 16:48:48 +02007044 case ISN_TRY:
7045 {
Bram Moolenaar0d807102021-12-21 09:42:09 +00007046 try_T *try = &iptr->isn_arg.tryref;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007047
7048 if (try->try_ref->try_finally == 0)
7049 smsg("%s%4d TRY catch -> %d, endtry -> %d",
7050 pfx, current,
7051 try->try_ref->try_catch,
7052 try->try_ref->try_endtry);
7053 else
7054 smsg("%s%4d TRY catch -> %d, finally -> %d, endtry -> %d",
7055 pfx, current,
7056 try->try_ref->try_catch,
7057 try->try_ref->try_finally,
7058 try->try_ref->try_endtry);
7059 }
7060 break;
7061 case ISN_CATCH:
Bram Moolenaar4c137212021-04-19 16:48:48 +02007062 smsg("%s%4d CATCH", pfx, current);
7063 break;
7064 case ISN_TRYCONT:
7065 {
7066 trycont_T *trycont = &iptr->isn_arg.trycont;
7067
7068 smsg("%s%4d TRY-CONTINUE %d level%s -> %d", pfx, current,
7069 trycont->tct_levels,
7070 trycont->tct_levels == 1 ? "" : "s",
7071 trycont->tct_where);
7072 }
7073 break;
7074 case ISN_FINALLY:
7075 smsg("%s%4d FINALLY", pfx, current);
7076 break;
7077 case ISN_ENDTRY:
7078 smsg("%s%4d ENDTRY", pfx, current);
7079 break;
7080 case ISN_THROW:
7081 smsg("%s%4d THROW", pfx, current);
7082 break;
7083
7084 // expression operations on number
7085 case ISN_OPNR:
7086 case ISN_OPFLOAT:
7087 case ISN_OPANY:
7088 {
7089 char *what;
7090 char *ins;
7091
7092 switch (iptr->isn_arg.op.op_type)
7093 {
7094 case EXPR_MULT: what = "*"; break;
7095 case EXPR_DIV: what = "/"; break;
7096 case EXPR_REM: what = "%"; break;
7097 case EXPR_SUB: what = "-"; break;
7098 case EXPR_ADD: what = "+"; break;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01007099 case EXPR_LSHIFT: what = "<<"; break;
7100 case EXPR_RSHIFT: what = ">>"; break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007101 default: what = "???"; break;
7102 }
7103 switch (iptr->isn_type)
7104 {
7105 case ISN_OPNR: ins = "OPNR"; break;
7106 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
7107 case ISN_OPANY: ins = "OPANY"; break;
7108 default: ins = "???"; break;
7109 }
7110 smsg("%s%4d %s %s", pfx, current, ins, what);
7111 }
7112 break;
7113
7114 case ISN_COMPAREBOOL:
7115 case ISN_COMPARESPECIAL:
Bram Moolenaar7a222242022-03-01 19:23:24 +00007116 case ISN_COMPARENULL:
Bram Moolenaar4c137212021-04-19 16:48:48 +02007117 case ISN_COMPARENR:
7118 case ISN_COMPAREFLOAT:
7119 case ISN_COMPARESTRING:
7120 case ISN_COMPAREBLOB:
7121 case ISN_COMPARELIST:
7122 case ISN_COMPAREDICT:
7123 case ISN_COMPAREFUNC:
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00007124 case ISN_COMPARECLASS:
7125 case ISN_COMPAREOBJECT:
Bram Moolenaar4c137212021-04-19 16:48:48 +02007126 case ISN_COMPAREANY:
7127 {
7128 char *p;
7129 char buf[10];
7130 char *type;
7131
7132 switch (iptr->isn_arg.op.op_type)
7133 {
7134 case EXPR_EQUAL: p = "=="; break;
7135 case EXPR_NEQUAL: p = "!="; break;
7136 case EXPR_GREATER: p = ">"; break;
7137 case EXPR_GEQUAL: p = ">="; break;
7138 case EXPR_SMALLER: p = "<"; break;
7139 case EXPR_SEQUAL: p = "<="; break;
7140 case EXPR_MATCH: p = "=~"; break;
7141 case EXPR_IS: p = "is"; break;
7142 case EXPR_ISNOT: p = "isnot"; break;
7143 case EXPR_NOMATCH: p = "!~"; break;
7144 default: p = "???"; break;
7145 }
7146 STRCPY(buf, p);
7147 if (iptr->isn_arg.op.op_ic == TRUE)
7148 strcat(buf, "?");
7149 switch(iptr->isn_type)
7150 {
7151 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
7152 case ISN_COMPARESPECIAL:
7153 type = "COMPARESPECIAL"; break;
Bram Moolenaar7a222242022-03-01 19:23:24 +00007154 case ISN_COMPARENULL: type = "COMPARENULL"; break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007155 case ISN_COMPARENR: type = "COMPARENR"; break;
7156 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
7157 case ISN_COMPARESTRING:
7158 type = "COMPARESTRING"; break;
7159 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
7160 case ISN_COMPARELIST: type = "COMPARELIST"; break;
7161 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
7162 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00007163 case ISN_COMPARECLASS: type = "COMPARECLASS"; break;
7164 case ISN_COMPAREOBJECT:
7165 type = "COMPAREOBJECT"; break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007166 case ISN_COMPAREANY: type = "COMPAREANY"; break;
7167 default: type = "???"; break;
7168 }
7169
7170 smsg("%s%4d %s %s", pfx, current, type, buf);
7171 }
7172 break;
7173
7174 case ISN_ADDLIST: smsg("%s%4d ADDLIST", pfx, current); break;
7175 case ISN_ADDBLOB: smsg("%s%4d ADDBLOB", pfx, current); break;
7176
7177 // expression operations
LemonBoy372bcce2022-04-25 12:43:20 +01007178 case ISN_CONCAT:
7179 smsg("%s%4d CONCAT size %lld", pfx, current,
7180 (varnumber_T)(iptr->isn_arg.number));
7181 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007182 case ISN_STRINDEX: smsg("%s%4d STRINDEX", pfx, current); break;
7183 case ISN_STRSLICE: smsg("%s%4d STRSLICE", pfx, current); break;
7184 case ISN_BLOBINDEX: smsg("%s%4d BLOBINDEX", pfx, current); break;
7185 case ISN_BLOBSLICE: smsg("%s%4d BLOBSLICE", pfx, current); break;
7186 case ISN_LISTAPPEND: smsg("%s%4d LISTAPPEND", pfx, current); break;
7187 case ISN_BLOBAPPEND: smsg("%s%4d BLOBAPPEND", pfx, current); break;
7188 case ISN_LISTINDEX: smsg("%s%4d LISTINDEX", pfx, current); break;
7189 case ISN_LISTSLICE: smsg("%s%4d LISTSLICE", pfx, current); break;
7190 case ISN_ANYINDEX: smsg("%s%4d ANYINDEX", pfx, current); break;
7191 case ISN_ANYSLICE: smsg("%s%4d ANYSLICE", pfx, current); break;
7192 case ISN_SLICE: smsg("%s%4d SLICE %lld",
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02007193 pfx, current, iptr->isn_arg.number); break;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02007194 case ISN_GETITEM: smsg("%s%4d ITEM %lld%s", pfx, current,
7195 iptr->isn_arg.getitem.gi_index,
7196 iptr->isn_arg.getitem.gi_with_op ?
7197 " with op" : ""); break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007198 case ISN_MEMBER: smsg("%s%4d MEMBER", pfx, current); break;
7199 case ISN_STRINGMEMBER: smsg("%s%4d MEMBER %s", pfx, current,
7200 iptr->isn_arg.string); break;
Yegappan Lakshmanan5a05d372023-09-29 19:43:11 +02007201 case ISN_GET_OBJ_MEMBER: smsg("%s%4d OBJ_MEMBER %d", pfx, current,
7202 (int)iptr->isn_arg.classmember.cm_idx);
Ernie Rael00df69e2023-09-05 07:38:09 +02007203 break;
Yegappan Lakshmanan5a05d372023-09-29 19:43:11 +02007204 case ISN_GET_ITF_MEMBER: smsg("%s%4d ITF_MEMBER %d on %s",
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00007205 pfx, current,
7206 (int)iptr->isn_arg.classmember.cm_idx,
Yegappan Lakshmanan5a05d372023-09-29 19:43:11 +02007207 iptr->isn_arg.classmember.cm_class->class_name);
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00007208 break;
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00007209 case ISN_STORE_THIS: smsg("%s%4d STORE_THIS %d", pfx, current,
Bram Moolenaarffdaca92022-12-09 21:41:48 +00007210 (int)iptr->isn_arg.number); break;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02007211 case ISN_CLEARDICT: smsg("%s%4d CLEARDICT", pfx, current); break;
7212 case ISN_USEDICT: smsg("%s%4d USEDICT", pfx, current); break;
7213
Bram Moolenaar4c137212021-04-19 16:48:48 +02007214 case ISN_NEGATENR: smsg("%s%4d NEGATENR", pfx, current); break;
7215
Bram Moolenaar4c137212021-04-19 16:48:48 +02007216 case ISN_CHECKTYPE:
7217 {
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01007218 checktype_T *ct = &iptr->isn_arg.type;
Bram Moolenaarc6951a72022-12-29 20:56:24 +00007219 char *tofree = NULL;
7220 char *typename;
7221
7222 if (ct->ct_type->tt_type == VAR_FLOAT
7223 && (ct->ct_type->tt_flags & TTFLAG_NUMBER_OK))
7224 typename = "float|number";
7225 else
7226 typename = type_name(ct->ct_type, &tofree);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007227
7228 if (ct->ct_arg_idx == 0)
7229 smsg("%s%4d CHECKTYPE %s stack[%d]", pfx, current,
Bram Moolenaarc6951a72022-12-29 20:56:24 +00007230 typename,
Bram Moolenaar4c137212021-04-19 16:48:48 +02007231 (int)ct->ct_off);
7232 else
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01007233 smsg("%s%4d CHECKTYPE %s stack[%d] %s %d",
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02007234 pfx, current,
Bram Moolenaarc6951a72022-12-29 20:56:24 +00007235 typename,
Bram Moolenaar4c137212021-04-19 16:48:48 +02007236 (int)ct->ct_off,
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01007237 ct->ct_is_var ? "var": "arg",
Bram Moolenaar4c137212021-04-19 16:48:48 +02007238 (int)ct->ct_arg_idx);
7239 vim_free(tofree);
7240 break;
7241 }
7242 case ISN_CHECKLEN: smsg("%s%4d CHECKLEN %s%d", pfx, current,
7243 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
7244 iptr->isn_arg.checklen.cl_min_len);
7245 break;
7246 case ISN_SETTYPE:
7247 {
7248 char *tofree;
7249
7250 smsg("%s%4d SETTYPE %s", pfx, current,
7251 type_name(iptr->isn_arg.type.ct_type, &tofree));
7252 vim_free(tofree);
7253 break;
7254 }
7255 case ISN_COND2BOOL: smsg("%s%4d COND2BOOL", pfx, current); break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007256 case ISN_2BOOL: if (iptr->isn_arg.tobool.invert)
7257 smsg("%s%4d INVERT %d (!val)", pfx, current,
7258 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007259 else
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007260 smsg("%s%4d 2BOOL %d (!!val)", pfx, current,
7261 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007262 break;
7263 case ISN_2STRING: smsg("%s%4d 2STRING stack[%lld]", pfx, current,
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007264 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02007265 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007266 case ISN_2STRING_ANY: smsg("%s%4d 2STRING_ANY stack[%lld]",
7267 pfx, current,
7268 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02007269 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007270 case ISN_RANGE: smsg("%s%4d RANGE %s", pfx, current,
7271 iptr->isn_arg.string);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007272 break;
7273 case ISN_PUT:
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01007274 if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE)
Bram Moolenaar4c137212021-04-19 16:48:48 +02007275 smsg("%s%4d PUT %c above range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007276 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007277 else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE)
7278 smsg("%s%4d PUT %c range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007279 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007280 else
7281 smsg("%s%4d PUT %c %ld", pfx, current,
7282 iptr->isn_arg.put.put_regname,
7283 (long)iptr->isn_arg.put.put_lnum);
7284 break;
7285
Bram Moolenaar4c137212021-04-19 16:48:48 +02007286 case ISN_CMDMOD:
7287 {
7288 char_u *buf;
7289 size_t len = produce_cmdmods(
7290 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
7291
7292 buf = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02007293 if (likely(buf != NULL))
Bram Moolenaar4c137212021-04-19 16:48:48 +02007294 {
7295 (void)produce_cmdmods(
7296 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
7297 smsg("%s%4d CMDMOD %s", pfx, current, buf);
7298 vim_free(buf);
7299 }
7300 break;
7301 }
7302 case ISN_CMDMOD_REV: smsg("%s%4d CMDMOD_REV", pfx, current); break;
7303
7304 case ISN_PROF_START:
Bram Moolenaare99d4222021-06-13 14:01:26 +02007305 smsg("%s%4d PROFILE START line %d", pfx, current,
7306 iptr->isn_lnum);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007307 break;
7308
7309 case ISN_PROF_END:
7310 smsg("%s%4d PROFILE END", pfx, current);
7311 break;
7312
Bram Moolenaare99d4222021-06-13 14:01:26 +02007313 case ISN_DEBUG:
Bram Moolenaar8cec9272021-06-23 20:20:53 +02007314 smsg("%s%4d DEBUG line %d-%d varcount %lld", pfx, current,
7315 iptr->isn_arg.debug.dbg_break_lnum + 1,
7316 iptr->isn_lnum,
7317 iptr->isn_arg.debug.dbg_var_names_len);
Bram Moolenaare99d4222021-06-13 14:01:26 +02007318 break;
7319
Bram Moolenaar4c137212021-04-19 16:48:48 +02007320 case ISN_UNPACK: smsg("%s%4d UNPACK %d%s", pfx, current,
7321 iptr->isn_arg.unpack.unp_count,
7322 iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : "");
7323 break;
7324 case ISN_SHUFFLE: smsg("%s%4d SHUFFLE %d up %d", pfx, current,
7325 iptr->isn_arg.shuffle.shfl_item,
7326 iptr->isn_arg.shuffle.shfl_up);
7327 break;
7328 case ISN_DROP: smsg("%s%4d DROP", pfx, current); break;
7329
7330 case ISN_FINISH: // End of list of instructions for ISN_SUBSTITUTE.
7331 return;
7332 }
7333
7334 out_flush(); // output one line at a time
7335 ui_breakcheck();
7336 if (got_int)
7337 break;
7338 }
7339}
7340
7341/*
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02007342 * Handle command line completion for the :disassemble command.
7343 */
7344 void
7345set_context_in_disassemble_cmd(expand_T *xp, char_u *arg)
7346{
7347 char_u *p;
7348
7349 // Default: expand user functions, "debug" and "profile"
7350 xp->xp_context = EXPAND_DISASSEMBLE;
7351 xp->xp_pattern = arg;
7352
7353 // first argument already typed: only user function names
7354 if (*arg != NUL && *(p = skiptowhite(arg)) != NUL)
7355 {
7356 xp->xp_context = EXPAND_USER_FUNC;
7357 xp->xp_pattern = skipwhite(p);
7358 }
7359}
7360
7361/*
7362 * Function given to ExpandGeneric() to obtain the list of :disassemble
7363 * arguments.
7364 */
7365 char_u *
7366get_disassemble_argument(expand_T *xp, int idx)
7367{
7368 if (idx == 0)
7369 return (char_u *)"debug";
7370 if (idx == 1)
7371 return (char_u *)"profile";
7372 return get_user_func_name(xp, idx - 2);
7373}
7374
7375/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01007376 * ":disassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01007377 * We don't really need this at runtime, but we do have tests that require it,
7378 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007379 */
7380 void
7381ex_disassemble(exarg_T *eap)
7382{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01007383 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01007384 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007385 dfunc_T *dfunc;
Bram Moolenaardafef512022-05-21 21:55:55 +01007386 isn_T *instr = NULL; // init to shut up gcc warning
7387 int instr_count = 0; // init to shut up gcc warning
Bram Moolenaar5a01caa2022-05-21 18:56:58 +01007388 compiletype_T compile_type = CT_NONE;
Bram Moolenaare99d4222021-06-13 14:01:26 +02007389
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01007390 ufunc = find_func_by_name(arg, &compile_type);
Bram Moolenaara26b9702020-04-18 19:53:28 +02007391 if (ufunc == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007392 return;
Bram Moolenaare99d4222021-06-13 14:01:26 +02007393 if (func_needs_compiling(ufunc, compile_type)
7394 && compile_def_function(ufunc, FALSE, compile_type, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02007395 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007396 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007397 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007398 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007399 return;
7400 }
Bram Moolenaar6db660b2021-08-01 14:08:54 +02007401 msg((char *)printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007402
7403 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
Bram Moolenaare99d4222021-06-13 14:01:26 +02007404 switch (compile_type)
7405 {
7406 case CT_PROFILE:
Bram Moolenaarf002a412021-01-24 13:34:18 +01007407#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02007408 instr = dfunc->df_instr_prof;
7409 instr_count = dfunc->df_instr_prof_count;
7410 break;
Bram Moolenaarf002a412021-01-24 13:34:18 +01007411#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02007412 // FALLTHROUGH
7413 case CT_NONE:
7414 instr = dfunc->df_instr;
7415 instr_count = dfunc->df_instr_count;
7416 break;
7417 case CT_DEBUG:
7418 instr = dfunc->df_instr_debug;
7419 instr_count = dfunc->df_instr_debug_count;
7420 break;
7421 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007422
Bram Moolenaar4c137212021-04-19 16:48:48 +02007423 list_instructions("", instr, instr_count, ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007424}
7425
7426/*
Bram Moolenaar13106602020-10-04 16:06:05 +02007427 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007428 * list, etc. Mostly like what JavaScript does, except that empty list and
7429 * empty dictionary are FALSE.
7430 */
7431 int
7432tv2bool(typval_T *tv)
7433{
7434 switch (tv->v_type)
7435 {
7436 case VAR_NUMBER:
7437 return tv->vval.v_number != 0;
7438 case VAR_FLOAT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007439 return tv->vval.v_float != 0.0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007440 case VAR_PARTIAL:
7441 return tv->vval.v_partial != NULL;
7442 case VAR_FUNC:
7443 case VAR_STRING:
7444 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
7445 case VAR_LIST:
7446 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
7447 case VAR_DICT:
7448 return tv->vval.v_dict != NULL
7449 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
7450 case VAR_BOOL:
7451 case VAR_SPECIAL:
7452 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
7453 case VAR_JOB:
7454#ifdef FEAT_JOB_CHANNEL
7455 return tv->vval.v_job != NULL;
7456#else
7457 break;
7458#endif
7459 case VAR_CHANNEL:
7460#ifdef FEAT_JOB_CHANNEL
7461 return tv->vval.v_channel != NULL;
7462#else
7463 break;
7464#endif
7465 case VAR_BLOB:
7466 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
7467 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02007468 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007469 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02007470 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00007471 case VAR_CLASS:
7472 case VAR_OBJECT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007473 break;
7474 }
7475 return FALSE;
7476}
7477
Bram Moolenaarea2d4072020-11-12 12:08:51 +01007478 void
7479emsg_using_string_as(typval_T *tv, int as_number)
7480{
7481 semsg(_(as_number ? e_using_string_as_number_str
7482 : e_using_string_as_bool_str),
7483 tv->vval.v_string == NULL
7484 ? (char_u *)"" : tv->vval.v_string);
7485}
7486
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007487/*
7488 * If "tv" is a string give an error and return FAIL.
7489 */
7490 int
7491check_not_string(typval_T *tv)
7492{
7493 if (tv->v_type == VAR_STRING)
7494 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +01007495 emsg_using_string_as(tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007496 clear_tv(tv);
7497 return FAIL;
7498 }
7499 return OK;
7500}
7501
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007502
7503#endif // FEAT_EVAL