blob: 7901f6e78359115aa05736a82fffe32505424164 [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 {
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200619 if (obj->v_type != VAR_OBJECT)
620 {
621 semsg(_(e_internal_error_str), "type in stack is not an object");
622 return FAIL;
623 }
624
Bram Moolenaar574950d2023-01-03 19:08:50 +0000625 *STACK_TV_VAR(0) = *obj;
626 obj->v_type = VAR_UNKNOWN;
627 }
628
Bram Moolenaar5800c792022-09-22 17:34:01 +0100629 partial_T *pt = pt_arg != NULL ? pt_arg : ufunc->uf_partial;
630 if (pt != NULL || (ufunc->uf_flags & FC_CLOSURE))
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100631 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200632 outer_ref_T *ref = ALLOC_CLEAR_ONE(outer_ref_T);
Bram Moolenaar0186e582021-01-10 18:33:11 +0100633
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200634 if (ref == NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +0100635 return FAIL;
636 if (pt != NULL)
637 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +0000638 ref->or_outer = get_pt_outer(pt);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200639 ++pt->pt_refcount;
640 ref->or_partial = pt;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100641 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100642 else
643 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200644 ref->or_outer = ALLOC_CLEAR_ONE(outer_T);
Dominique Pelle5a9e5842021-07-24 19:32:12 +0200645 if (unlikely(ref->or_outer == NULL))
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200646 {
647 vim_free(ref);
648 return FAIL;
649 }
650 ref->or_outer_allocated = TRUE;
651 ref->or_outer->out_stack = &ectx->ec_stack;
652 ref->or_outer->out_frame_idx = ectx->ec_frame_idx;
653 if (ectx->ec_outer_ref != NULL)
654 ref->or_outer->out_up = ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100655 }
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200656 ectx->ec_outer_ref = ref;
Bram Moolenaarab360522021-01-10 14:02:28 +0100657 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100658 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200659 ectx->ec_outer_ref = NULL;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100660
Bram Moolenaarc970e422021-03-17 15:03:04 +0100661 ++ufunc->uf_calls;
662
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100663 // Set execution state to the start of the called function.
664 ectx->ec_dfunc_idx = cdf_idx;
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100665 ectx->ec_instr = INSTRUCTIONS(dfunc);
Bram Moolenaarb2049902021-01-24 12:53:53 +0100666 entry = estack_push_ufunc(ufunc, 1);
Bram Moolenaarc620c052020-07-08 15:16:19 +0200667 if (entry != NULL)
668 {
669 // Set the script context to the script where the function was defined.
Bram Moolenaarc70fe462021-04-17 17:59:19 +0200670 // Save the current context so it can be restored on return.
671 entry->es_save_sctx = current_sctx;
672 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200673 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100674
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +0200675 // Start execution at the first instruction.
676 ectx->ec_iidx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100677
678 return OK;
679}
680
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000681// Double linked list of funcstack_T in use.
682static funcstack_T *first_funcstack = NULL;
683
684 static void
685add_funcstack_to_list(funcstack_T *funcstack)
686{
687 // Link in list of funcstacks.
688 if (first_funcstack != NULL)
689 first_funcstack->fs_prev = funcstack;
690 funcstack->fs_next = first_funcstack;
691 funcstack->fs_prev = NULL;
692 first_funcstack = funcstack;
693}
694
695 static void
696remove_funcstack_from_list(funcstack_T *funcstack)
697{
698 if (funcstack->fs_prev == NULL)
699 first_funcstack = funcstack->fs_next;
700 else
701 funcstack->fs_prev->fs_next = funcstack->fs_next;
702 if (funcstack->fs_next != NULL)
703 funcstack->fs_next->fs_prev = funcstack->fs_prev;
704}
705
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100706/*
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200707 * Used when returning from a function: Check if any closure is still
708 * referenced. If so then move the arguments and variables to a separate piece
709 * of stack to be used when the closure is called.
710 * When "free_arguments" is TRUE the arguments are to be freed.
711 * Returns FAIL when out of memory.
712 */
713 static int
714handle_closure_in_use(ectx_T *ectx, int free_arguments)
715{
716 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
717 + ectx->ec_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200718 int argcount;
719 int top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200720 int idx;
721 typval_T *tv;
722 int closure_in_use = FALSE;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200723 garray_T *gap = &ectx->ec_funcrefs;
724 varnumber_T closure_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200725
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200726 if (dfunc->df_ufunc == NULL)
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200727 return OK; // function was freed
728 if (dfunc->df_has_closure == 0)
729 return OK; // no closures
730 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + dfunc->df_varcount);
731 closure_count = tv->vval.v_number;
732 if (closure_count == 0)
733 return OK; // no funcrefs created
734
Bram Moolenaar8fa745e2022-09-16 19:04:24 +0100735 // Compute "top": the first entry in the stack used by the function.
736 // This is the first argument (after that comes the stack frame and then
737 // the local variables).
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200738 argcount = ufunc_argcount(dfunc->df_ufunc);
739 top = ectx->ec_frame_idx - argcount;
740
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200741 // Check if any created closure is still in use.
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200742 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200743 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200744 partial_T *pt;
745 int off = gap->ga_len - closure_count + idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200746
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200747 if (off < 0)
748 continue; // count is off or already done
749 pt = ((partial_T **)gap->ga_data)[off];
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200750 if (pt->pt_refcount > 1)
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200751 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200752 int refcount = pt->pt_refcount;
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200753 int i;
754
Dominique Pelleaf4a61a2021-12-27 17:21:41 +0000755 // A Reference in a local variable doesn't count, it gets
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200756 // unreferenced on return.
757 for (i = 0; i < dfunc->df_varcount; ++i)
758 {
759 typval_T *stv = STACK_TV(ectx->ec_frame_idx
760 + STACK_FRAME_SIZE + i);
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200761 if (stv->v_type == VAR_PARTIAL && pt == stv->vval.v_partial)
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200762 --refcount;
763 }
764 if (refcount > 1)
765 {
766 closure_in_use = TRUE;
767 break;
768 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200769 }
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200770 }
771
772 if (closure_in_use)
773 {
774 funcstack_T *funcstack = ALLOC_CLEAR_ONE(funcstack_T);
775 typval_T *stack;
776
777 // A closure is using the arguments and/or local variables.
778 // Move them to the called function.
779 if (funcstack == NULL)
780 return FAIL;
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000781
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200782 funcstack->fs_var_offset = argcount + STACK_FRAME_SIZE;
Bram Moolenaar1936c762022-09-28 15:19:10 +0100783 funcstack->fs_ga.ga_len = funcstack->fs_var_offset
784 + dfunc->df_varcount;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200785 stack = ALLOC_CLEAR_MULT(typval_T, funcstack->fs_ga.ga_len);
786 funcstack->fs_ga.ga_data = stack;
787 if (stack == NULL)
788 {
789 vim_free(funcstack);
790 return FAIL;
791 }
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000792 add_funcstack_to_list(funcstack);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200793
794 // Move or copy the arguments.
795 for (idx = 0; idx < argcount; ++idx)
796 {
797 tv = STACK_TV(top + idx);
798 if (free_arguments)
799 {
800 *(stack + idx) = *tv;
801 tv->v_type = VAR_UNKNOWN;
802 }
803 else
804 copy_tv(tv, stack + idx);
805 }
Bram Moolenaar8fa745e2022-09-16 19:04:24 +0100806 // Skip the stack frame.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200807 // Move the local variables.
808 for (idx = 0; idx < dfunc->df_varcount; ++idx)
809 {
810 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + idx);
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200811
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200812 // A partial created for a local function, that is also used as a
813 // local variable, has a reference count for the variable, thus
814 // will never go down to zero. When all these refcounts are one
815 // then the funcstack is unused. We need to count how many we have
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000816 // so we know when to check.
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200817 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL)
818 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200819 int i;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200820
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200821 for (i = 0; i < closure_count; ++i)
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200822 if (tv->vval.v_partial == ((partial_T **)gap->ga_data)[
823 gap->ga_len - closure_count + i])
824 ++funcstack->fs_min_refcount;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200825 }
826
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200827 *(stack + funcstack->fs_var_offset + idx) = *tv;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200828 tv->v_type = VAR_UNKNOWN;
829 }
830
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200831 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200832 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200833 partial_T *pt = ((partial_T **)gap->ga_data)[gap->ga_len
834 - closure_count + idx];
835 if (pt->pt_refcount > 1)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200836 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200837 ++funcstack->fs_refcount;
838 pt->pt_funcstack = funcstack;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100839 pt->pt_outer.out_stack = &funcstack->fs_ga;
840 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx - top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200841 }
842 }
843 }
844
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200845 for (idx = 0; idx < closure_count; ++idx)
846 partial_unref(((partial_T **)gap->ga_data)[gap->ga_len
847 - closure_count + idx]);
848 gap->ga_len -= closure_count;
849 if (gap->ga_len == 0)
850 ga_clear(gap);
851
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200852 return OK;
853}
854
855/*
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200856 * Called when a partial is freed or its reference count goes down to one. The
857 * funcstack may be the only reference to the partials in the local variables.
858 * Go over all of them, the funcref and can be freed if all partials
859 * referencing the funcstack have a reference count of one.
Bram Moolenaaracd6b992022-09-17 16:27:39 +0100860 * Returns TRUE if the funcstack is freed, the partial referencing it will then
861 * also have been freed.
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200862 */
Bram Moolenaaracd6b992022-09-17 16:27:39 +0100863 int
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200864funcstack_check_refcount(funcstack_T *funcstack)
865{
Bram Moolenaaracd6b992022-09-17 16:27:39 +0100866 int i;
867 garray_T *gap = &funcstack->fs_ga;
868 int done = 0;
869 typval_T *stack;
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200870
871 if (funcstack->fs_refcount > funcstack->fs_min_refcount)
Bram Moolenaaracd6b992022-09-17 16:27:39 +0100872 return FALSE;
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200873 for (i = funcstack->fs_var_offset; i < gap->ga_len; ++i)
874 {
875 typval_T *tv = ((typval_T *)gap->ga_data) + i;
876
877 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL
878 && tv->vval.v_partial->pt_funcstack == funcstack
879 && tv->vval.v_partial->pt_refcount == 1)
880 ++done;
881 }
Bram Moolenaaracd6b992022-09-17 16:27:39 +0100882 if (done != funcstack->fs_min_refcount)
883 return FALSE;
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200884
Bram Moolenaaracd6b992022-09-17 16:27:39 +0100885 stack = gap->ga_data;
886
887 // All partials referencing the funcstack have a reference count of
888 // one, thus the funcstack is no longer of use.
889 for (i = 0; i < gap->ga_len; ++i)
890 clear_tv(stack + i);
891 vim_free(stack);
892 remove_funcstack_from_list(funcstack);
893 vim_free(funcstack);
894
895 return TRUE;
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200896}
897
898/*
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000899 * For garbage collecting: set references in all variables referenced by
900 * all funcstacks.
901 */
902 int
903set_ref_in_funcstacks(int copyID)
904{
905 funcstack_T *funcstack;
906
907 for (funcstack = first_funcstack; funcstack != NULL;
908 funcstack = funcstack->fs_next)
909 {
910 typval_T *stack = funcstack->fs_ga.ga_data;
911 int i;
912
913 for (i = 0; i < funcstack->fs_ga.ga_len; ++i)
914 if (set_ref_in_item(stack + i, copyID, NULL, NULL))
915 return TRUE; // abort
916 }
917 return FALSE;
918}
919
Bram Moolenaar806a2732022-09-04 15:40:36 +0100920// Ugly static to avoid passing the execution context around through many
921// layers.
922static ectx_T *current_ectx = NULL;
923
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000924/*
Bram Moolenaar806a2732022-09-04 15:40:36 +0100925 * Return TRUE if currently executing a :def function.
926 * Can be used by builtin functions only.
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100927 */
Bram Moolenaar806a2732022-09-04 15:40:36 +0100928 int
929in_def_function(void)
930{
931 return current_ectx != NULL;
932}
933
934/*
Ernie Rael4c8da022023-10-11 21:35:11 +0200935 * If executing a class/object method, then fill in the lval_T.
936 * Set lr_tv to the executing item, and lr_exec_class to the executing class;
937 * use free_tv and class_unref when finished with the lval_root.
938 * For use by builtin functions.
939 *
940 * Return FAIL and do nothing if not executing in a class; otherwise OK.
941 */
942 int
943fill_exec_lval_root(lval_root_T *root)
944{
945 ectx_T *ectx = current_ectx;
946 if (ectx != NULL)
947 {
948 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
949 + current_ectx->ec_dfunc_idx;
950 ufunc_T *ufunc = dfunc->df_ufunc;
951 if (ufunc->uf_class != NULL) // executing a method?
952 {
953 typval_T *tv = alloc_tv();
954 if (tv != NULL)
955 {
956 CLEAR_POINTER(root);
957 root->lr_tv = tv;
958 copy_tv(STACK_TV_VAR(0), root->lr_tv);
959 root->lr_cl_exec = ufunc->uf_class;
960 ++root->lr_cl_exec->class_refcount;
961 return OK;
962 }
963 }
964 }
965
966 return FAIL;
967}
968
969/*
Bram Moolenaar98aff652022-09-06 21:02:35 +0100970 * Clear "current_ectx" and return the previous value. To be used when calling
971 * a user function.
972 */
973 ectx_T *
dundargocc57b5bc2022-11-02 13:30:51 +0000974clear_current_ectx(void)
Bram Moolenaar98aff652022-09-06 21:02:35 +0100975{
976 ectx_T *r = current_ectx;
977
978 current_ectx = NULL;
979 return r;
980}
981
982 void
983restore_current_ectx(ectx_T *ectx)
984{
985 if (current_ectx != NULL)
986 iemsg("Restoring current_ectx while it is not NULL");
987 current_ectx = ectx;
988}
989
990/*
Bram Moolenaar806a2732022-09-04 15:40:36 +0100991 * Add an entry for a deferred function call to the currently executing
992 * function.
993 * Return the list or NULL when failed.
994 */
995 static list_T *
996add_defer_item(int var_idx, int argcount, ectx_T *ectx)
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100997{
998 typval_T *defer_tv = STACK_TV_VAR(var_idx);
999 list_T *defer_l;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001000 list_T *l;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001001 typval_T listval;
1002
1003 if (defer_tv->v_type != VAR_LIST)
1004 {
Bram Moolenaara5348f22022-09-04 11:42:22 +01001005 // first time, allocate the list
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001006 if (rettv_list_alloc(defer_tv) == FAIL)
Bram Moolenaar806a2732022-09-04 15:40:36 +01001007 return NULL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001008 }
1009 defer_l = defer_tv->vval.v_list;
1010
1011 l = list_alloc_with_items(argcount + 1);
1012 if (l == NULL)
Bram Moolenaar806a2732022-09-04 15:40:36 +01001013 return NULL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001014 listval.v_type = VAR_LIST;
1015 listval.vval.v_list = l;
1016 listval.v_lock = 0;
Bram Moolenaara5348f22022-09-04 11:42:22 +01001017 if (list_insert_tv(defer_l, &listval, defer_l->lv_first) == FAIL)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001018 {
1019 vim_free(l);
Bram Moolenaar806a2732022-09-04 15:40:36 +01001020 return NULL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001021 }
1022
Bram Moolenaar806a2732022-09-04 15:40:36 +01001023 return l;
1024}
1025
1026/*
1027 * Handle ISN_DEFER. Stack has a function reference and "argcount" arguments.
1028 * The local variable that lists deferred functions is "var_idx".
1029 * Returns OK or FAIL.
1030 */
1031 static int
Yegappan Lakshmananf3eac692023-10-17 11:00:45 +02001032defer_command(int var_idx, int argcount, ectx_T *ectx)
Bram Moolenaar806a2732022-09-04 15:40:36 +01001033{
Yegappan Lakshmananf3eac692023-10-17 11:00:45 +02001034 list_T *l = add_defer_item(var_idx, argcount, ectx);
Bram Moolenaar806a2732022-09-04 15:40:36 +01001035 int i;
1036 typval_T *func_tv;
1037
1038 if (l == NULL)
1039 return FAIL;
1040
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001041 func_tv = STACK_TV_BOT(-argcount - 1);
Yegappan Lakshmananf3eac692023-10-17 11:00:45 +02001042 if (func_tv->v_type != VAR_PARTIAL && func_tv->v_type != VAR_FUNC)
Bram Moolenaarf5fec052022-09-11 11:49:22 +01001043 {
1044 semsg(_(e_expected_str_but_got_str),
Yegappan Lakshmananf3eac692023-10-17 11:00:45 +02001045 "function or partial",
Bram Moolenaarf5fec052022-09-11 11:49:22 +01001046 vartype_name(func_tv->v_type));
1047 return FAIL;
1048 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001049 list_set_item(l, 0, func_tv);
1050
Bram Moolenaarf5fec052022-09-11 11:49:22 +01001051 for (i = 0; i < argcount; ++i)
Yegappan Lakshmananf3eac692023-10-17 11:00:45 +02001052 list_set_item(l, i + 1, STACK_TV_BOT(-argcount + i));
1053 ectx->ec_stack.ga_len -= argcount + 1;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001054 return OK;
1055}
1056
1057/*
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001058 * Add a deferred call for "name" with arguments "argvars[argcount]".
Bram Moolenaar806a2732022-09-04 15:40:36 +01001059 * Consumes "name", also on failure.
1060 * Only to be called when in_def_function() returns TRUE.
1061 */
1062 int
1063add_defer_function(char_u *name, int argcount, typval_T *argvars)
1064{
1065 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1066 + current_ectx->ec_dfunc_idx;
1067 list_T *l;
1068 typval_T func_tv;
1069 int i;
1070
1071 if (dfunc->df_defer_var_idx == 0)
1072 {
1073 iemsg("df_defer_var_idx is zero");
Bram Moolenaar31ea6bf2022-09-05 10:47:13 +01001074 vim_free(name);
Bram Moolenaar806a2732022-09-04 15:40:36 +01001075 return FAIL;
1076 }
Bram Moolenaar806a2732022-09-04 15:40:36 +01001077
Bram Moolenaarf5fec052022-09-11 11:49:22 +01001078 l = add_defer_item(dfunc->df_defer_var_idx - 1, argcount, current_ectx);
Bram Moolenaar806a2732022-09-04 15:40:36 +01001079 if (l == NULL)
1080 {
Bram Moolenaar31ea6bf2022-09-05 10:47:13 +01001081 vim_free(name);
Bram Moolenaar806a2732022-09-04 15:40:36 +01001082 return FAIL;
1083 }
1084
Bram Moolenaar31ea6bf2022-09-05 10:47:13 +01001085 func_tv.v_type = VAR_FUNC;
1086 func_tv.v_lock = 0;
1087 func_tv.vval.v_string = name;
Bram Moolenaar806a2732022-09-04 15:40:36 +01001088 list_set_item(l, 0, &func_tv);
Bram Moolenaar31ea6bf2022-09-05 10:47:13 +01001089
Bram Moolenaar806a2732022-09-04 15:40:36 +01001090 for (i = 0; i < argcount; ++i)
1091 list_set_item(l, i + 1, argvars + i);
1092 return OK;
1093}
1094
1095/*
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001096 * Invoked when returning from a function: Invoke any deferred calls.
1097 */
1098 static void
1099invoke_defer_funcs(ectx_T *ectx)
1100{
1101 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1102 + ectx->ec_dfunc_idx;
1103 typval_T *defer_tv = STACK_TV_VAR(dfunc->df_defer_var_idx - 1);
1104 listitem_T *li;
1105
1106 if (defer_tv->v_type != VAR_LIST)
1107 return; // no function added
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00001108 FOR_ALL_LIST_ITEMS(defer_tv->vval.v_list, li)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001109 {
1110 list_T *l = li->li_tv.vval.v_list;
1111 typval_T rettv;
1112 typval_T argvars[MAX_FUNC_ARGS];
1113 int i;
1114 listitem_T *arg_li = l->lv_first;
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001115 typval_T *functv = &l->lv_first->li_tv;
Yegappan Lakshmananf3eac692023-10-17 11:00:45 +02001116 int argcount = l->lv_len - 1;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001117
zeertzjqa1f2b5d2023-04-18 21:04:53 +01001118 if (functv->vval.v_string == NULL)
1119 // already being called, can happen if function does ":qa"
1120 continue;
1121
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001122 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;
Yegappan Lakshmananf3eac692023-10-17 11:00:45 +02001135 funcexe.fe_object = functv->vval.v_partial->pt_obj;
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001136 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
Yegappan Lakshmanan06725952023-10-18 11:47:37 +02001143 // If the deferred function is called after an exception, then only the
Yegappan Lakshmananc59c1e02023-10-19 10:52:34 +02001144 // first statement in the function will be executed (because of the
1145 // exception). So save and restore the try/catch/throw exception
1146 // state.
1147 exception_state_T estate;
1148 exception_state_save(&estate);
1149 exception_state_clear();
Yegappan Lakshmanan06725952023-10-18 11:47:37 +02001150
zeertzjqa1f2b5d2023-04-18 21:04:53 +01001151 (void)call_func(name, -1, &rettv, argcount, argvars, &funcexe);
1152
Yegappan Lakshmananc59c1e02023-10-19 10:52:34 +02001153 exception_state_restore(&estate);
Yegappan Lakshmanan06725952023-10-18 11:47:37 +02001154
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001155 clear_tv(&rettv);
zeertzjqa1f2b5d2023-04-18 21:04:53 +01001156 vim_free(name);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001157 }
1158}
1159
1160/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001161 * Return from the current function.
1162 */
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001163 static int
Bram Moolenaar4c137212021-04-19 16:48:48 +02001164func_return(ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001165{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001166 int idx;
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01001167 int ret_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001168 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1169 + ectx->ec_dfunc_idx;
1170 int argcount = ufunc_argcount(dfunc->df_ufunc);
Bram Moolenaarc620c052020-07-08 15:16:19 +02001171 estack_T *entry;
Bram Moolenaar12d26532021-02-19 19:13:21 +01001172 int prev_dfunc_idx = STACK_TV(ectx->ec_frame_idx
1173 + STACK_FRAME_FUNC_OFF)->vval.v_number;
Bram Moolenaarb06b50d2021-04-26 21:39:25 +02001174 funclocal_T *floc;
1175#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01001176 dfunc_T *prev_dfunc = ((dfunc_T *)def_functions.ga_data)
1177 + prev_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001178
Bram Moolenaar12d26532021-02-19 19:13:21 +01001179 if (do_profiling == PROF_YES)
1180 {
1181 ufunc_T *caller = prev_dfunc->df_ufunc;
1182
1183 if (dfunc->df_ufunc->uf_profiling
1184 || (caller != NULL && caller->uf_profiling))
1185 {
1186 profile_may_end_func(((profinfo_T *)profile_info_ga.ga_data)
1187 + profile_info_ga.ga_len - 1, dfunc->df_ufunc, caller);
1188 --profile_info_ga.ga_len;
1189 }
1190 }
1191#endif
Bram Moolenaar919c12c2021-12-14 14:29:16 +00001192
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001193 if (dfunc->df_defer_var_idx > 0)
1194 invoke_defer_funcs(ectx);
1195
Bram Moolenaar919c12c2021-12-14 14:29:16 +00001196 // No check for uf_refcount being zero, cannot think of a way that would
1197 // happen.
Bram Moolenaarc970e422021-03-17 15:03:04 +01001198 --dfunc->df_ufunc->uf_calls;
1199
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001200 // execution context goes one level up
Bram Moolenaarc620c052020-07-08 15:16:19 +02001201 entry = estack_pop();
1202 if (entry != NULL)
Bram Moolenaarc70fe462021-04-17 17:59:19 +02001203 current_sctx = entry->es_save_sctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001204
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001205 if (handle_closure_in_use(ectx, TRUE) == FAIL)
1206 return FAIL;
1207
Bram Moolenaar574950d2023-01-03 19:08:50 +00001208 // Clear the arguments. If this was an object method also clear the
1209 // object, it is just before the arguments.
1210 int top = ectx->ec_frame_idx - argcount;
Yegappan Lakshmanan1db15142023-09-19 20:34:05 +02001211 if (IS_OBJECT_METHOD(dfunc->df_ufunc))
Bram Moolenaar574950d2023-01-03 19:08:50 +00001212 --top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001213 for (idx = top; idx < ectx->ec_frame_idx; ++idx)
1214 clear_tv(STACK_TV(idx));
1215
1216 // Clear local variables and temp values, but not the return value.
1217 for (idx = ectx->ec_frame_idx + STACK_FRAME_SIZE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001218 idx < ectx->ec_stack.ga_len - 1; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001219 clear_tv(STACK_TV(idx));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001220
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01001221 // The return value should be on top of the stack. However, when aborting
1222 // it may not be there and ec_frame_idx is the top of the stack.
1223 ret_idx = ectx->ec_stack.ga_len - 1;
Bram Moolenaar0186e582021-01-10 18:33:11 +01001224 if (ret_idx == ectx->ec_frame_idx + STACK_FRAME_IDX_OFF)
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01001225 ret_idx = 0;
1226
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001227 if (ectx->ec_outer_ref != NULL)
1228 {
1229 if (ectx->ec_outer_ref->or_outer_allocated)
1230 vim_free(ectx->ec_outer_ref->or_outer);
1231 partial_unref(ectx->ec_outer_ref->or_partial);
1232 vim_free(ectx->ec_outer_ref);
1233 }
Bram Moolenaar0186e582021-01-10 18:33:11 +01001234
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001235 // Restore the previous frame.
Bram Moolenaar12d26532021-02-19 19:13:21 +01001236 ectx->ec_dfunc_idx = prev_dfunc_idx;
Bram Moolenaar0186e582021-01-10 18:33:11 +01001237 ectx->ec_iidx = STACK_TV(ectx->ec_frame_idx
1238 + STACK_FRAME_IIDX_OFF)->vval.v_number;
Bram Moolenaar5930ddc2021-04-26 20:32:59 +02001239 ectx->ec_instr = (void *)STACK_TV(ectx->ec_frame_idx
1240 + STACK_FRAME_INSTR_OFF)->vval.v_string;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001241 ectx->ec_outer_ref = (void *)STACK_TV(ectx->ec_frame_idx
Bram Moolenaar0186e582021-01-10 18:33:11 +01001242 + STACK_FRAME_OUTER_OFF)->vval.v_string;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001243 floc = (void *)STACK_TV(ectx->ec_frame_idx
1244 + STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string;
Bram Moolenaar5366e1a2020-10-01 13:01:34 +02001245 // restoring ec_frame_idx must be last
Bram Moolenaar0186e582021-01-10 18:33:11 +01001246 ectx->ec_frame_idx = STACK_TV(ectx->ec_frame_idx
1247 + STACK_FRAME_IDX_OFF)->vval.v_number;
Bram Moolenaard386e922021-04-25 14:48:49 +02001248
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001249 if (floc == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02001250 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001251 else
1252 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02001253 ectx->ec_funclocal = *floc;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001254 vim_free(floc);
1255 }
1256
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01001257 if (ret_idx > 0)
1258 {
1259 // Reset the stack to the position before the call, with a spot for the
1260 // return value, moved there from above the frame.
1261 ectx->ec_stack.ga_len = top + 1;
1262 *STACK_TV_BOT(-1) = *STACK_TV(ret_idx);
1263 }
1264 else
1265 // Reset the stack to the position before the call.
1266 ectx->ec_stack.ga_len = top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001267
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001268 funcdepth_decrement();
Bram Moolenaare99d4222021-06-13 14:01:26 +02001269 --ex_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001270 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001271}
1272
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001273/*
1274 * Prepare arguments and rettv for calling a builtin or user function.
1275 */
1276 static int
1277call_prepare(int argcount, typval_T *argvars, ectx_T *ectx)
1278{
1279 int idx;
1280 typval_T *tv;
1281
1282 // Move arguments from bottom of the stack to argvars[] and add terminator.
1283 for (idx = 0; idx < argcount; ++idx)
1284 argvars[idx] = *STACK_TV_BOT(idx - argcount);
1285 argvars[argcount].v_type = VAR_UNKNOWN;
1286
1287 // Result replaces the arguments on the stack.
1288 if (argcount > 0)
1289 ectx->ec_stack.ga_len -= argcount - 1;
Bram Moolenaar35578162021-08-02 19:10:38 +02001290 else if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001291 return FAIL;
1292 else
1293 ++ectx->ec_stack.ga_len;
1294
1295 // Default return value is zero.
1296 tv = STACK_TV_BOT(-1);
1297 tv->v_type = VAR_NUMBER;
1298 tv->vval.v_number = 0;
Bram Moolenaar24565cf2022-03-28 18:16:52 +01001299 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001300
1301 return OK;
1302}
1303
1304/*
1305 * Call a builtin function by index.
1306 */
1307 static int
1308call_bfunc(int func_idx, int argcount, ectx_T *ectx)
1309{
1310 typval_T argvars[MAX_FUNC_ARGS];
1311 int idx;
Bram Moolenaar171fb922020-10-28 16:54:47 +01001312 int did_emsg_before = did_emsg;
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001313 ectx_T *prev_ectx = current_ectx;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001314 char *save_func_name = ectx->ec_where.wt_func_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001315
1316 if (call_prepare(argcount, argvars, ectx) == FAIL)
1317 return FAIL;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001318 ectx->ec_where.wt_func_name = internal_func_name(func_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001319
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001320 // Call the builtin function. Set "current_ectx" so that when it
1321 // recursively invokes call_def_function() a closure context can be set.
1322 current_ectx = ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001323 call_internal_func_by_idx(func_idx, argvars, STACK_TV_BOT(-1));
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001324 current_ectx = prev_ectx;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001325 ectx->ec_where.wt_func_name = save_func_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001326
1327 // Clear the arguments.
1328 for (idx = 0; idx < argcount; ++idx)
1329 clear_tv(&argvars[idx]);
Bram Moolenaar015f4262020-05-05 21:25:22 +02001330
Bram Moolenaar57f799e2020-12-12 20:42:19 +01001331 if (did_emsg > did_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +02001332 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001333 return OK;
1334}
1335
1336/*
1337 * Execute a user defined function.
Bram Moolenaarab360522021-01-10 14:02:28 +01001338 * If the function is compiled this will add a stack frame and set the
1339 * instruction pointer at the start of the function.
1340 * Otherwise the function is called here.
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001341 * If "pt" is not null use "pt->pt_outer" for ec_outer_ref->or_outer.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001342 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001343 */
1344 static int
Bram Moolenaar0186e582021-01-10 18:33:11 +01001345call_ufunc(
1346 ufunc_T *ufunc,
1347 partial_T *pt,
1348 int argcount,
1349 ectx_T *ectx,
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001350 isn_T *iptr,
1351 dict_T *selfdict)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001352{
1353 typval_T argvars[MAX_FUNC_ARGS];
1354 funcexe_T funcexe;
Bram Moolenaar0917e862023-02-18 14:42:44 +00001355 funcerror_T error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001356 int idx;
Bram Moolenaar28ee8922020-10-28 20:20:00 +01001357 int did_emsg_before = did_emsg;
Bram Moolenaar139575d2022-03-15 19:29:30 +00001358 compiletype_T compile_type = get_compile_type(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001359
Bram Moolenaare99d4222021-06-13 14:01:26 +02001360 if (func_needs_compiling(ufunc, compile_type)
1361 && compile_def_function(ufunc, FALSE, compile_type, NULL)
1362 == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02001363 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001364 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001365 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +01001366 error = check_user_func_argcount(ufunc, argcount);
Bram Moolenaar50824712020-12-20 21:10:17 +01001367 if (error != FCERR_UNKNOWN)
1368 {
1369 if (error == FCERR_TOOMANY)
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001370 semsg(_(e_too_many_arguments_for_function_str),
1371 printable_func_name(ufunc));
Bram Moolenaar50824712020-12-20 21:10:17 +01001372 else
Bram Moolenaare1242042021-12-16 20:56:57 +00001373 semsg(_(e_not_enough_arguments_for_function_str),
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001374 printable_func_name(ufunc));
Bram Moolenaar50824712020-12-20 21:10:17 +01001375 return FAIL;
1376 }
1377
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001378 // The function has been compiled, can call it quickly. For a function
1379 // that was defined later: we can call it directly next time.
1380 if (iptr != NULL)
1381 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001382 delete_instr(iptr);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001383 iptr->isn_type = ISN_DCALL;
1384 iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1385 iptr->isn_arg.dfunc.cdf_argcount = argcount;
1386 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001387 return call_dfunc(ufunc->uf_dfunc_idx, pt, argcount, ectx);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001388 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001389
1390 if (call_prepare(argcount, argvars, ectx) == FAIL)
1391 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +02001392 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00001393 funcexe.fe_evaluate = TRUE;
1394 funcexe.fe_selfdict = selfdict != NULL ? selfdict : dict_stack_get_dict();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001395
1396 // Call the user function. Result goes in last position on the stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001397 error = call_user_func_check(ufunc, argcount, argvars,
Bram Moolenaar851f86b2021-12-13 14:26:44 +00001398 STACK_TV_BOT(-1), &funcexe, funcexe.fe_selfdict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001399
1400 // Clear the arguments.
1401 for (idx = 0; idx < argcount; ++idx)
1402 clear_tv(&argvars[idx]);
1403
1404 if (error != FCERR_NONE)
1405 {
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01001406 user_func_error(error, printable_func_name(ufunc),
1407 funcexe.fe_found_var);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001408 return FAIL;
1409 }
Bram Moolenaar28ee8922020-10-28 20:20:00 +01001410 if (did_emsg > did_emsg_before)
Bram Moolenaared677f52020-08-12 16:38:10 +02001411 // Error other than from calling the function itself.
1412 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001413 return OK;
1414}
1415
1416/*
Bram Moolenaara91a7132021-03-25 21:12:15 +01001417 * If command modifiers were applied restore them.
1418 */
1419 static void
1420may_restore_cmdmod(funclocal_T *funclocal)
1421{
1422 if (funclocal->floc_restore_cmdmod)
1423 {
1424 cmdmod.cmod_filter_regmatch.regprog = NULL;
1425 undo_cmdmod(&cmdmod);
1426 cmdmod = funclocal->floc_save_cmdmod;
1427 funclocal->floc_restore_cmdmod = FALSE;
1428 }
1429}
1430
1431/*
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001432 * Return TRUE if an error was given (not caught in try/catch) or CTRL-C was
1433 * pressed.
Bram Moolenaara1773442020-08-12 15:21:22 +02001434 */
1435 static int
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001436vim9_aborting(int prev_uncaught_emsg)
Bram Moolenaara1773442020-08-12 15:21:22 +02001437{
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001438 return uncaught_emsg > prev_uncaught_emsg || got_int || did_throw;
Bram Moolenaara1773442020-08-12 15:21:22 +02001439}
1440
1441/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001442 * Execute a function by "name".
1443 * This can be a builtin function or a user function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001444 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001445 * Returns FAIL if not found without an error message.
1446 */
1447 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001448call_by_name(
1449 char_u *name,
1450 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001451 ectx_T *ectx,
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001452 isn_T *iptr,
1453 dict_T *selfdict)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001454{
1455 ufunc_T *ufunc;
1456
1457 if (builtin_function(name, -1))
1458 {
1459 int func_idx = find_internal_func(name);
1460
Bram Moolenaar1983f1a2022-02-28 20:55:02 +00001461 if (func_idx < 0) // Impossible?
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001462 return FAIL;
Bram Moolenaar389df252020-07-09 21:20:47 +02001463 if (check_internal_func(func_idx, argcount) < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001464 return FAIL;
1465 return call_bfunc(func_idx, argcount, ectx);
1466 }
1467
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00001468 ufunc = find_func(name, FALSE);
Bram Moolenaara1773442020-08-12 15:21:22 +02001469
1470 if (ufunc == NULL)
1471 {
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001472 int prev_uncaught_emsg = uncaught_emsg;
Bram Moolenaara1773442020-08-12 15:21:22 +02001473
1474 if (script_autoload(name, TRUE))
1475 // loaded a package, search for the function again
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00001476 ufunc = find_func(name, FALSE);
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001477
1478 if (vim9_aborting(prev_uncaught_emsg))
Bram Moolenaara1773442020-08-12 15:21:22 +02001479 return FAIL; // bail out if loading the script caused an error
1480 }
1481
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001482 if (ufunc != NULL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001483 {
Bram Moolenaar0d89d8a2022-12-31 14:01:24 +00001484 if (check_ufunc_arg_types(ufunc, argcount, 0, ectx) == FAIL)
1485 return FAIL;
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001486
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001487 return call_ufunc(ufunc, NULL, argcount, ectx, iptr, selfdict);
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001488 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001489
1490 return FAIL;
1491}
1492
1493 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001494call_partial(
1495 typval_T *tv,
1496 int argcount_arg,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001497 ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001498{
Bram Moolenaara90afb92020-07-15 22:38:56 +02001499 int argcount = argcount_arg;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001500 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001501 int called_emsg_before = called_emsg;
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +01001502 int res = FAIL;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001503 dict_T *selfdict = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001504
1505 if (tv->v_type == VAR_PARTIAL)
1506 {
Bram Moolenaara90afb92020-07-15 22:38:56 +02001507 partial_T *pt = tv->vval.v_partial;
1508 int i;
1509
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +02001510 if (pt->pt_obj != NULL)
1511 {
1512 // partial with an object method. Push the object before the
1513 // function arguments.
1514 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
1515 return FAIL;
1516 for (i = 1; i <= argcount; ++i)
1517 *STACK_TV_BOT(-i + 1) = *STACK_TV_BOT(-i);
1518
1519 typval_T *obj_tv = STACK_TV_BOT(-argcount);
1520 obj_tv->v_type = VAR_OBJECT;
1521 obj_tv->v_lock = 0;
1522 obj_tv->vval.v_object = pt->pt_obj;
1523 ++pt->pt_obj->obj_refcount;
1524 ++ectx->ec_stack.ga_len;
1525 }
1526
Bram Moolenaara90afb92020-07-15 22:38:56 +02001527 if (pt->pt_argc > 0)
1528 {
1529 // Make space for arguments from the partial, shift the "argcount"
1530 // arguments up.
Bram Moolenaar35578162021-08-02 19:10:38 +02001531 if (GA_GROW_FAILS(&ectx->ec_stack, pt->pt_argc))
Bram Moolenaara90afb92020-07-15 22:38:56 +02001532 return FAIL;
1533 for (i = 1; i <= argcount; ++i)
1534 *STACK_TV_BOT(-i + pt->pt_argc) = *STACK_TV_BOT(-i);
1535 ectx->ec_stack.ga_len += pt->pt_argc;
1536 argcount += pt->pt_argc;
1537
1538 // copy the arguments from the partial onto the stack
1539 for (i = 0; i < pt->pt_argc; ++i)
1540 copy_tv(&pt->pt_argv[i], STACK_TV_BOT(-argcount + i));
1541 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001542 selfdict = pt->pt_dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001543
1544 if (pt->pt_func != NULL)
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001545 return call_ufunc(pt->pt_func, pt, argcount, ectx, NULL, selfdict);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001546
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001547 name = pt->pt_name;
1548 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001549 else if (tv->v_type == VAR_FUNC)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001550 name = tv->vval.v_string;
Bram Moolenaar95006e32020-08-29 17:47:08 +02001551 if (name != NULL)
1552 {
Bram Moolenaar0917e862023-02-18 14:42:44 +00001553 char_u fname_buf[FLEN_FIXED + 1];
1554 char_u *tofree = NULL;
1555 funcerror_T error = FCERR_NONE;
1556 char_u *fname;
Bram Moolenaar95006e32020-08-29 17:47:08 +02001557
1558 // May need to translate <SNR>123_ to K_SNR.
1559 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
1560 if (error != FCERR_NONE)
1561 res = FAIL;
1562 else
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001563 res = call_by_name(fname, argcount, ectx, NULL, selfdict);
Bram Moolenaar95006e32020-08-29 17:47:08 +02001564 vim_free(tofree);
1565 }
1566
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +01001567 if (res == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001568 {
1569 if (called_emsg == called_emsg_before)
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001570 emsg_funcname(e_unknown_function_str,
Bram Moolenaar015f4262020-05-05 21:25:22 +02001571 name == NULL ? (char_u *)"[unknown]" : name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001572 return FAIL;
1573 }
1574 return OK;
1575}
1576
1577/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001578 * Check if "lock" is VAR_LOCKED or VAR_FIXED. If so give an error and return
1579 * TRUE.
1580 */
1581 static int
1582error_if_locked(int lock, char *error)
1583{
1584 if (lock & (VAR_LOCKED | VAR_FIXED))
1585 {
1586 emsg(_(error));
1587 return TRUE;
1588 }
1589 return FALSE;
1590}
1591
1592/*
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001593 * Give an error if "tv" is not a number and return FAIL.
1594 */
1595 static int
1596check_for_number(typval_T *tv)
1597{
1598 if (tv->v_type != VAR_NUMBER)
1599 {
1600 semsg(_(e_expected_str_but_got_str),
1601 vartype_name(VAR_NUMBER), vartype_name(tv->v_type));
1602 return FAIL;
1603 }
1604 return OK;
1605}
1606
1607/*
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001608 * Store "tv" in variable "name".
1609 * This is for s: and g: variables.
1610 */
1611 static void
1612store_var(char_u *name, typval_T *tv)
1613{
1614 funccal_entry_T entry;
Bram Moolenaard877a572021-04-01 19:42:48 +02001615 int flags = ASSIGN_DECL;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001616
Bram Moolenaard877a572021-04-01 19:42:48 +02001617 if (tv->v_lock)
1618 flags |= ASSIGN_CONST;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001619 save_funccal(&entry);
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001620 set_var_const(name, 0, NULL, tv, FALSE, flags, 0);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001621 restore_funccal();
1622}
1623
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001624/*
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001625 * Convert "tv" to a string.
1626 * Return FAIL if not allowed.
1627 */
1628 static int
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001629do_2string(typval_T *tv, int is_2string_any, int tolerant)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001630{
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00001631 if (tv->v_type == VAR_STRING)
1632 return OK;
1633
1634 char_u *str;
1635
1636 if (is_2string_any)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001637 {
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00001638 switch (tv->v_type)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001639 {
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00001640 case VAR_SPECIAL:
1641 case VAR_BOOL:
1642 case VAR_NUMBER:
1643 case VAR_FLOAT:
1644 case VAR_BLOB: break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001645
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00001646 case VAR_LIST:
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001647 if (tolerant)
1648 {
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001649 char_u *s, *e, *p;
1650 garray_T ga;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001651
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001652 ga_init2(&ga, sizeof(char_u *), 1);
1653
1654 // Convert to NL separated items, then
1655 // escape the items and replace the NL with
1656 // a space.
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001657 str = typval2string(tv, TRUE);
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001658 if (str == NULL)
1659 return FAIL;
1660 s = str;
1661 while ((e = vim_strchr(s, '\n')) != NULL)
1662 {
1663 *e = NUL;
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +01001664 p = vim_strsave_fnameescape(s,
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00001665 VSE_NONE);
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001666 if (p != NULL)
1667 {
1668 ga_concat(&ga, p);
1669 ga_concat(&ga, (char_u *)" ");
1670 vim_free(p);
1671 }
1672 s = e + 1;
1673 }
1674 vim_free(str);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001675 clear_tv(tv);
1676 tv->v_type = VAR_STRING;
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001677 tv->vval.v_string = ga.ga_data;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001678 return OK;
1679 }
1680 // FALLTHROUGH
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00001681 default: to_string_error(tv->v_type);
1682 return FAIL;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001683 }
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001684 }
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00001685 str = typval_tostring(tv, TRUE);
1686 clear_tv(tv);
1687 tv->v_type = VAR_STRING;
1688 tv->vval.v_string = str;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001689 return OK;
1690}
1691
1692/*
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001693 * When the value of "sv" is a null list of dict, allocate it.
1694 */
1695 static void
Bram Moolenaar859cc212022-03-28 15:22:35 +01001696allocate_if_null(svar_T *sv)
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001697{
Bram Moolenaar859cc212022-03-28 15:22:35 +01001698 typval_T *tv = sv->sv_tv;
1699
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001700 switch (tv->v_type)
1701 {
1702 case VAR_LIST:
Bram Moolenaar859cc212022-03-28 15:22:35 +01001703 if (tv->vval.v_list == NULL && sv->sv_type != &t_list_empty)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001704 (void)rettv_list_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001705 break;
1706 case VAR_DICT:
Bram Moolenaar859cc212022-03-28 15:22:35 +01001707 if (tv->vval.v_dict == NULL && sv->sv_type != &t_dict_empty)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001708 (void)rettv_dict_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001709 break;
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02001710 case VAR_BLOB:
Bram Moolenaar859cc212022-03-28 15:22:35 +01001711 if (tv->vval.v_blob == NULL && sv->sv_type != &t_blob_null)
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02001712 (void)rettv_blob_alloc(tv);
1713 break;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001714 default:
1715 break;
1716 }
1717}
Bram Moolenaard3aac292020-04-19 14:32:17 +02001718
Bram Moolenaare7525c52021-01-09 13:20:37 +01001719/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001720 * Return the character "str[index]" where "index" is the character index,
1721 * including composing characters.
1722 * If "index" is out of range NULL is returned.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001723 */
1724 char_u *
1725char_from_string(char_u *str, varnumber_T index)
1726{
1727 size_t nbyte = 0;
1728 varnumber_T nchar = index;
1729 size_t slen;
1730
1731 if (str == NULL)
1732 return NULL;
1733 slen = STRLEN(str);
1734
Bram Moolenaarff871402021-03-26 13:34:05 +01001735 // Do the same as for a list: a negative index counts from the end.
1736 // Optimization to check the first byte to be below 0x80 (and no composing
1737 // character follows) makes this a lot faster.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001738 if (index < 0)
1739 {
1740 int clen = 0;
1741
1742 for (nbyte = 0; nbyte < slen; ++clen)
Bram Moolenaarff871402021-03-26 13:34:05 +01001743 {
1744 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1745 ++nbyte;
1746 else if (enc_utf8)
1747 nbyte += utfc_ptr2len(str + nbyte);
1748 else
1749 nbyte += mb_ptr2len(str + nbyte);
1750 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001751 nchar = clen + index;
1752 if (nchar < 0)
1753 // unlike list: index out of range results in empty string
1754 return NULL;
1755 }
1756
1757 for (nbyte = 0; nchar > 0 && nbyte < slen; --nchar)
Bram Moolenaarff871402021-03-26 13:34:05 +01001758 {
1759 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1760 ++nbyte;
1761 else if (enc_utf8)
1762 nbyte += utfc_ptr2len(str + nbyte);
1763 else
1764 nbyte += mb_ptr2len(str + nbyte);
1765 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001766 if (nbyte >= slen)
1767 return NULL;
Bram Moolenaar0289a092021-03-14 18:40:19 +01001768 return vim_strnsave(str + nbyte, mb_ptr2len(str + nbyte));
Bram Moolenaare7525c52021-01-09 13:20:37 +01001769}
1770
1771/*
1772 * Get the byte index for character index "idx" in string "str" with length
Bram Moolenaar0289a092021-03-14 18:40:19 +01001773 * "str_len". Composing characters are included.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001774 * If going over the end return "str_len".
1775 * If "idx" is negative count from the end, -1 is the last character.
1776 * When going over the start return -1.
1777 */
1778 static long
1779char_idx2byte(char_u *str, size_t str_len, varnumber_T idx)
1780{
1781 varnumber_T nchar = idx;
1782 size_t nbyte = 0;
1783
1784 if (nchar >= 0)
1785 {
1786 while (nchar > 0 && nbyte < str_len)
1787 {
Bram Moolenaar0289a092021-03-14 18:40:19 +01001788 nbyte += mb_ptr2len(str + nbyte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001789 --nchar;
1790 }
1791 }
1792 else
1793 {
1794 nbyte = str_len;
1795 while (nchar < 0 && nbyte > 0)
1796 {
1797 --nbyte;
1798 nbyte -= mb_head_off(str, str + nbyte);
1799 ++nchar;
1800 }
1801 if (nchar < 0)
1802 return -1;
1803 }
1804 return (long)nbyte;
1805}
1806
1807/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001808 * Return the slice "str[first : last]" using character indexes. Composing
1809 * characters are included.
Bram Moolenaar6601b622021-01-13 21:47:15 +01001810 * "exclusive" is TRUE for slice().
Bram Moolenaare7525c52021-01-09 13:20:37 +01001811 * Return NULL when the result is empty.
1812 */
1813 char_u *
Bram Moolenaar6601b622021-01-13 21:47:15 +01001814string_slice(char_u *str, varnumber_T first, varnumber_T last, int exclusive)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001815{
1816 long start_byte, end_byte;
1817 size_t slen;
1818
1819 if (str == NULL)
1820 return NULL;
1821 slen = STRLEN(str);
1822 start_byte = char_idx2byte(str, slen, first);
1823 if (start_byte < 0)
1824 start_byte = 0; // first index very negative: use zero
Bram Moolenaar6601b622021-01-13 21:47:15 +01001825 if ((last == -1 && !exclusive) || last == VARNUM_MAX)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001826 end_byte = (long)slen;
1827 else
1828 {
1829 end_byte = char_idx2byte(str, slen, last);
Bram Moolenaar6601b622021-01-13 21:47:15 +01001830 if (!exclusive && end_byte >= 0 && end_byte < (long)slen)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001831 // end index is inclusive
Bram Moolenaar0289a092021-03-14 18:40:19 +01001832 end_byte += mb_ptr2len(str + end_byte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001833 }
1834
1835 if (start_byte >= (long)slen || end_byte <= start_byte)
1836 return NULL;
1837 return vim_strnsave(str + start_byte, end_byte - start_byte);
1838}
1839
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001840/*
1841 * Get a script variable for ISN_STORESCRIPT and ISN_LOADSCRIPT.
1842 * When "dfunc_idx" is negative don't give an error.
1843 * Returns NULL for an error.
1844 */
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001845 static svar_T *
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001846get_script_svar(scriptref_T *sref, int dfunc_idx)
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001847{
1848 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001849 dfunc_T *dfunc = dfunc_idx < 0 ? NULL
1850 : ((dfunc_T *)def_functions.ga_data) + dfunc_idx;
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001851 svar_T *sv;
1852
1853 if (sref->sref_seq != si->sn_script_seq)
1854 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001855 // The script was reloaded after the function was compiled, the
1856 // script_idx may not be valid.
1857 if (dfunc != NULL)
1858 semsg(_(e_script_variable_invalid_after_reload_in_function_str),
1859 printable_func_name(dfunc->df_ufunc));
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001860 return NULL;
1861 }
1862 sv = ((svar_T *)si->sn_var_vals.ga_data) + sref->sref_idx;
Bram Moolenaar6de22962022-09-09 21:35:36 +01001863 if (sv->sv_name == NULL)
1864 {
1865 if (dfunc != NULL)
1866 emsg(_(e_script_variable_was_deleted));
1867 return NULL;
1868 }
Bram Moolenaar60dc8272021-07-29 22:48:54 +02001869 if (!equal_type(sv->sv_type, sref->sref_type, 0))
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001870 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001871 if (dfunc != NULL)
1872 emsg(_(e_script_variable_type_changed));
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001873 return NULL;
1874 }
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001875
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01001876 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0
1877 && sref->sref_sid != current_sctx.sc_sid)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001878 {
1879 if (dfunc != NULL)
1880 semsg(_(e_item_not_exported_in_script_str), sv->sv_name);
1881 return NULL;
1882 }
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001883 return sv;
1884}
1885
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001886/*
Bram Moolenaar20677332021-06-06 17:02:53 +02001887 * Function passed to do_cmdline() for splitting a script joined by NL
1888 * characters.
1889 */
1890 static char_u *
1891get_split_sourceline(
1892 int c UNUSED,
1893 void *cookie,
1894 int indent UNUSED,
1895 getline_opt_T options UNUSED)
1896{
1897 source_cookie_T *sp = (source_cookie_T *)cookie;
1898 char_u *p;
1899 char_u *line;
1900
Bram Moolenaar20677332021-06-06 17:02:53 +02001901 p = vim_strchr(sp->nextline, '\n');
1902 if (p == NULL)
1903 {
1904 line = vim_strsave(sp->nextline);
1905 sp->nextline += STRLEN(sp->nextline);
1906 }
1907 else
1908 {
1909 line = vim_strnsave(sp->nextline, p - sp->nextline);
1910 sp->nextline = p + 1;
1911 }
1912 return line;
1913}
1914
1915/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001916 * Execute a function by "name".
1917 * This can be a builtin function, user function or a funcref.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001918 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001919 */
1920 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001921call_eval_func(
1922 char_u *name,
1923 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001924 ectx_T *ectx,
1925 isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001926{
Bram Moolenaared677f52020-08-12 16:38:10 +02001927 int called_emsg_before = called_emsg;
1928 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001929
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001930 res = call_by_name(name, argcount, ectx, iptr, NULL);
Bram Moolenaared677f52020-08-12 16:38:10 +02001931 if (res == FAIL && called_emsg == called_emsg_before)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001932 {
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001933 dictitem_T *v;
1934
1935 v = find_var(name, NULL, FALSE);
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001936 if (v == NULL || (v->di_tv.v_type != VAR_PARTIAL
1937 && v->di_tv.v_type != VAR_FUNC))
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001938 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001939 emsg_funcname(e_unknown_function_str, name);
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001940 return FAIL;
1941 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001942 return call_partial(&v->di_tv, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001943 }
Bram Moolenaared677f52020-08-12 16:38:10 +02001944 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001945}
1946
1947/*
Bram Moolenaarf112f302020-12-20 17:47:52 +01001948 * When a function reference is used, fill a partial with the information
1949 * needed, especially when it is used as a closure.
1950 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001951 int
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001952fill_partial_and_closure(
Bram Moolenaarcc341812022-09-19 15:54:34 +01001953 partial_T *pt,
1954 ufunc_T *ufunc,
1955 loopvarinfo_T *lvi,
1956 ectx_T *ectx)
Bram Moolenaarf112f302020-12-20 17:47:52 +01001957{
1958 pt->pt_func = ufunc;
1959 pt->pt_refcount = 1;
1960
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001961 if (ufunc->uf_flags & FC_CLOSURE)
Bram Moolenaarf112f302020-12-20 17:47:52 +01001962 {
1963 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1964 + ectx->ec_dfunc_idx;
1965
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001966 // The closure may need to find arguments and local variables of the
1967 // current function in the stack.
Bram Moolenaar0186e582021-01-10 18:33:11 +01001968 pt->pt_outer.out_stack = &ectx->ec_stack;
1969 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001970 if (ectx->ec_outer_ref != NULL)
1971 {
1972 // The current context already has a context, link to that one.
1973 pt->pt_outer.out_up = ectx->ec_outer_ref->or_outer;
1974 if (ectx->ec_outer_ref->or_partial != NULL)
1975 {
1976 pt->pt_outer.out_up_partial = ectx->ec_outer_ref->or_partial;
1977 ++pt->pt_outer.out_up_partial->pt_refcount;
1978 }
1979 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01001980
Bram Moolenaarcc341812022-09-19 15:54:34 +01001981 if (lvi != NULL)
1982 {
1983 int depth;
1984
1985 // The closure may need to find variables defined inside a loop,
1986 // for every nested loop. A new reference is made every time,
1987 // ISN_ENDLOOP will check if they are actually used.
1988 for (depth = 0; depth < lvi->lvi_depth; ++depth)
1989 {
1990 pt->pt_outer.out_loop[depth].stack = &ectx->ec_stack;
1991 pt->pt_outer.out_loop[depth].var_idx = ectx->ec_frame_idx
1992 + STACK_FRAME_SIZE + lvi->lvi_loop[depth].var_idx;
1993 pt->pt_outer.out_loop[depth].var_count =
1994 lvi->lvi_loop[depth].var_count;
1995 }
Bram Moolenaar6d313be2022-09-22 16:36:25 +01001996 pt->pt_outer.out_loop_size = lvi->lvi_depth;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001997 }
Bram Moolenaar6d313be2022-09-22 16:36:25 +01001998 else
1999 pt->pt_outer.out_loop_size = 0;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002000
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002001 // If the function currently executing returns and the closure is still
2002 // being referenced, we need to make a copy of the context (arguments
2003 // and local variables) so that the closure can use it later.
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02002004 // Store a reference to the partial so we can handle that.
Bram Moolenaar35578162021-08-02 19:10:38 +02002005 if (GA_GROW_FAILS(&ectx->ec_funcrefs, 1))
Bram Moolenaarf112f302020-12-20 17:47:52 +01002006 {
2007 vim_free(pt);
2008 return FAIL;
2009 }
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02002010 // Extra variable keeps the count of closures created in the current
2011 // function call.
Bram Moolenaarf112f302020-12-20 17:47:52 +01002012 ++(((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx
2013 + STACK_FRAME_SIZE + dfunc->df_varcount)->vval.v_number;
2014
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002015 ((partial_T **)ectx->ec_funcrefs.ga_data)[ectx->ec_funcrefs.ga_len]
2016 = pt;
Bram Moolenaarf112f302020-12-20 17:47:52 +01002017 ++pt->pt_refcount;
2018 ++ectx->ec_funcrefs.ga_len;
2019 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002020 ++ufunc->uf_refcount;
Bram Moolenaarf112f302020-12-20 17:47:52 +01002021 return OK;
2022}
2023
Bram Moolenaaraacc9662021-08-13 19:40:51 +02002024/*
2025 * Execute iptr->isn_arg.string as an Ex command.
2026 */
2027 static int
Ernie Raelee865f32023-09-29 19:53:55 +02002028exec_command(isn_T *iptr, char_u *cmd_string)
Bram Moolenaaraacc9662021-08-13 19:40:51 +02002029{
2030 source_cookie_T cookie;
2031
2032 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00002033 // Pass getsourceline to get an error for a missing ":end" command.
Bram Moolenaaraacc9662021-08-13 19:40:51 +02002034 CLEAR_FIELD(cookie);
2035 cookie.sourcing_lnum = iptr->isn_lnum - 1;
Ernie Raelee865f32023-09-29 19:53:55 +02002036 if (do_cmdline(cmd_string, getsourceline, &cookie,
Bram Moolenaaraacc9662021-08-13 19:40:51 +02002037 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED) == FAIL
2038 || did_emsg)
2039 return FAIL;
2040 return OK;
2041}
2042
Bram Moolenaar89445512022-04-14 12:58:23 +01002043/*
2044 * If script "sid" is not loaded yet then load it now.
2045 * Caller must make sure "sid" is a valid script ID.
2046 * "loaded" is set to TRUE if the script had to be loaded.
2047 * Returns FAIL if loading fails, OK if already loaded or loaded now.
2048 */
2049 int
2050may_load_script(int sid, int *loaded)
2051{
2052 scriptitem_T *si = SCRIPT_ITEM(sid);
2053
2054 if (si->sn_state == SN_STATE_NOT_LOADED)
2055 {
2056 *loaded = TRUE;
2057 if (do_source(si->sn_name, FALSE, DOSO_NONE, NULL) == FAIL)
2058 {
2059 semsg(_(e_cant_open_file_str), si->sn_name);
2060 return FAIL;
2061 }
2062 }
2063 return OK;
2064}
2065
Bram Moolenaarf18332f2021-05-07 17:55:55 +02002066// used for v_instr of typval of VAR_INSTR
2067struct instr_S {
2068 ectx_T *instr_ectx;
2069 isn_T *instr_instr;
2070};
2071
Bram Moolenaar4c137212021-04-19 16:48:48 +02002072// used for substitute_instr
2073typedef struct subs_expr_S {
2074 ectx_T *subs_ectx;
2075 isn_T *subs_instr;
2076 int subs_status;
2077} subs_expr_T;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002078
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002079// Set when calling do_debug().
2080static ectx_T *debug_context = NULL;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02002081static int debug_var_count;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002082
2083/*
2084 * When debugging lookup "name" and return the typeval.
2085 * When not found return NULL.
2086 */
2087 typval_T *
2088lookup_debug_var(char_u *name)
2089{
2090 int idx;
2091 dfunc_T *dfunc;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02002092 ufunc_T *ufunc;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002093 ectx_T *ectx = debug_context;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02002094 int varargs_off;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002095
2096 if (ectx == NULL)
2097 return NULL;
2098 dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
2099
2100 // Go through the local variable names, from last to first.
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02002101 for (idx = debug_var_count - 1; idx >= 0; --idx)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002102 {
Bram Moolenaare406ff82022-03-10 20:47:43 +00002103 char_u *varname = ((char_u **)dfunc->df_var_names.ga_data)[idx];
2104
2105 // the variable name may be NULL when not available in this block
2106 if (varname != NULL && STRCMP(varname, name) == 0)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002107 return STACK_TV_VAR(idx);
2108 }
2109
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02002110 // Go through argument names.
2111 ufunc = dfunc->df_ufunc;
2112 varargs_off = ufunc->uf_va_name == NULL ? 0 : 1;
2113 for (idx = 0; idx < ufunc->uf_args.ga_len; ++idx)
2114 if (STRCMP(((char_u **)(ufunc->uf_args.ga_data))[idx], name) == 0)
2115 return STACK_TV(ectx->ec_frame_idx - ufunc->uf_args.ga_len
2116 - varargs_off + idx);
2117 if (ufunc->uf_va_name != NULL && STRCMP(ufunc->uf_va_name, name) == 0)
2118 return STACK_TV(ectx->ec_frame_idx - 1);
2119
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002120 return NULL;
2121}
2122
Bram Moolenaar26a44842021-09-02 18:49:06 +02002123/*
2124 * Return TRUE if there might be a breakpoint in "ufunc", which is when a
2125 * breakpoint was set in that function or when there is any expression.
2126 */
2127 int
2128may_break_in_function(ufunc_T *ufunc)
2129{
2130 return ufunc->uf_has_breakpoint || debug_has_expr_breakpoint();
2131}
2132
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002133 static void
2134handle_debug(isn_T *iptr, ectx_T *ectx)
2135{
2136 char_u *line;
2137 ufunc_T *ufunc = (((dfunc_T *)def_functions.ga_data)
2138 + ectx->ec_dfunc_idx)->df_ufunc;
2139 isn_T *ni;
2140 int end_lnum = iptr->isn_lnum;
2141 garray_T ga;
2142 int lnum;
2143
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02002144 if (ex_nesting_level > debug_break_level)
2145 {
2146 linenr_T breakpoint;
2147
Bram Moolenaar26a44842021-09-02 18:49:06 +02002148 if (!may_break_in_function(ufunc))
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02002149 return;
2150
2151 // check for the next breakpoint if needed
2152 breakpoint = dbg_find_breakpoint(FALSE, ufunc->uf_name,
Bram Moolenaar8cec9272021-06-23 20:20:53 +02002153 iptr->isn_arg.debug.dbg_break_lnum);
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02002154 if (breakpoint <= 0 || breakpoint > iptr->isn_lnum)
2155 return;
2156 }
2157
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002158 SOURCING_LNUM = iptr->isn_lnum;
2159 debug_context = ectx;
Bram Moolenaar8cec9272021-06-23 20:20:53 +02002160 debug_var_count = iptr->isn_arg.debug.dbg_var_names_len;
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002161
2162 for (ni = iptr + 1; ni->isn_type != ISN_FINISH; ++ni)
2163 if (ni->isn_type == ISN_DEBUG
2164 || ni->isn_type == ISN_RETURN
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002165 || ni->isn_type == ISN_RETURN_OBJECT
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002166 || ni->isn_type == ISN_RETURN_VOID)
2167 {
Bram Moolenaar112bed02021-11-23 22:16:34 +00002168 end_lnum = ni->isn_lnum + (ni->isn_type == ISN_DEBUG ? 0 : 1);
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002169 break;
2170 }
2171
2172 if (end_lnum > iptr->isn_lnum)
2173 {
2174 ga_init2(&ga, sizeof(char_u *), 10);
Bram Moolenaar310091d2021-12-23 21:14:37 +00002175 for (lnum = iptr->isn_lnum; lnum < end_lnum
2176 && lnum <= ufunc->uf_lines.ga_len; ++lnum)
Bram Moolenaar59b50c32021-06-17 22:27:48 +02002177 {
Bram Moolenaar303215d2021-07-07 20:10:43 +02002178 char_u *p = ((char_u **)ufunc->uf_lines.ga_data)[lnum - 1];
Bram Moolenaar59b50c32021-06-17 22:27:48 +02002179
Bram Moolenaar303215d2021-07-07 20:10:43 +02002180 if (p == NULL)
2181 continue; // left over from continuation line
2182 p = skipwhite(p);
Bram Moolenaar59b50c32021-06-17 22:27:48 +02002183 if (*p == '#')
2184 break;
Bram Moolenaar35578162021-08-02 19:10:38 +02002185 if (GA_GROW_OK(&ga, 1))
Bram Moolenaar59b50c32021-06-17 22:27:48 +02002186 ((char_u **)(ga.ga_data))[ga.ga_len++] = p;
2187 if (STRNCMP(p, "def ", 4) == 0)
2188 break;
2189 }
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002190 line = ga_concat_strings(&ga, " ");
2191 vim_free(ga.ga_data);
2192 }
2193 else
2194 line = ((char_u **)ufunc->uf_lines.ga_data)[iptr->isn_lnum - 1];
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002195
Dominique Pelle6e969552021-06-17 13:53:41 +02002196 do_debug(line == NULL ? (char_u *)"[empty]" : line);
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002197 debug_context = NULL;
2198
2199 if (end_lnum > iptr->isn_lnum)
2200 vim_free(line);
2201}
2202
Bram Moolenaar4c137212021-04-19 16:48:48 +02002203/*
Bram Moolenaar65b0d162022-12-13 18:43:22 +00002204 * Store a value in a list, dict, blob or object variable.
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002205 * Returns OK, FAIL or NOTDONE (uncatchable error).
2206 */
2207 static int
2208execute_storeindex(isn_T *iptr, ectx_T *ectx)
2209{
Bram Moolenaarf7d1c6e2023-01-16 20:47:57 +00002210 vartype_T dest_type = iptr->isn_arg.storeindex.si_vartype;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002211 typval_T *tv;
2212 typval_T *tv_idx = STACK_TV_BOT(-2);
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002213 long lidx = 0;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002214 typval_T *tv_dest = STACK_TV_BOT(-1);
2215 int status = OK;
2216
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002217 if (tv_idx->v_type == VAR_NUMBER)
2218 lidx = (long)tv_idx->vval.v_number;
2219
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002220 // Stack contains:
2221 // -3 value to be stored
2222 // -2 index
Yegappan Lakshmanan3775f772023-09-01 22:05:45 +02002223 // -1 dict, list, blob, object or class
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002224 tv = STACK_TV_BOT(-3);
2225 SOURCING_LNUM = iptr->isn_lnum;
Gianmaria Bajod7085a02023-08-31 18:15:26 +02002226
2227 // Make sure an object has been initialized
2228 if (dest_type == VAR_OBJECT && tv_dest->vval.v_object == NULL)
2229 {
2230 emsg(_(e_using_null_object));
2231 status = FAIL;
2232 }
2233 else if (dest_type == VAR_ANY)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002234 {
2235 dest_type = tv_dest->v_type;
2236 if (dest_type == VAR_DICT)
2237 status = do_2string(tv_idx, TRUE, FALSE);
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002238 else if (dest_type == VAR_OBJECT && tv_idx->v_type == VAR_STRING)
2239 {
2240 // Need to get the member index now that the class is known.
2241 object_T *obj = tv_dest->vval.v_object;
2242 class_T *cl = obj->obj_class;
2243 char_u *member = tv_idx->vval.v_string;
2244
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02002245 int m_idx;
2246 ocmember_T *m = object_member_lookup(cl, member, 0, &m_idx);
2247 if (m != NULL)
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002248 {
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02002249 if (*member == '_')
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002250 {
Ernie Raele6c9aa52023-10-06 19:55:52 +02002251 emsg_var_cl_define(e_cannot_access_private_variable_str,
2252 m->ocm_name, 0, cl);
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02002253 status = FAIL;
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002254 }
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002255
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02002256 lidx = m_idx;
2257 }
2258 else
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002259 {
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +02002260 member_not_found_msg(cl, VAR_OBJECT, member, 0);
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002261 status = FAIL;
2262 }
2263 }
2264 else if ((dest_type == VAR_LIST || dest_type == VAR_OBJECT)
2265 && tv_idx->v_type != VAR_NUMBER)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002266 {
2267 emsg(_(e_number_expected));
2268 status = FAIL;
2269 }
2270 }
Bram Moolenaare08be092022-02-17 13:08:26 +00002271
2272 if (status == OK)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002273 {
Bram Moolenaare08be092022-02-17 13:08:26 +00002274 if (dest_type == VAR_LIST)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002275 {
Bram Moolenaare08be092022-02-17 13:08:26 +00002276 list_T *list = tv_dest->vval.v_list;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002277
Bram Moolenaare08be092022-02-17 13:08:26 +00002278 if (list == NULL)
2279 {
2280 emsg(_(e_list_not_set));
2281 return FAIL;
2282 }
2283 if (lidx < 0 && list->lv_len + lidx >= 0)
2284 // negative index is relative to the end
2285 lidx = list->lv_len + lidx;
2286 if (lidx < 0 || lidx > list->lv_len)
2287 {
2288 semsg(_(e_list_index_out_of_range_nr), lidx);
2289 return FAIL;
2290 }
2291 if (lidx < list->lv_len)
2292 {
2293 listitem_T *li = list_find(list, lidx);
2294
2295 if (error_if_locked(li->li_tv.v_lock,
Bram Moolenaar70c43d82022-01-26 21:01:15 +00002296 e_cannot_change_locked_list_item))
Bram Moolenaare08be092022-02-17 13:08:26 +00002297 return FAIL;
2298 // overwrite existing list item
2299 clear_tv(&li->li_tv);
2300 li->li_tv = *tv;
2301 }
2302 else
2303 {
2304 if (error_if_locked(list->lv_lock, e_cannot_change_locked_list))
2305 return FAIL;
2306 // append to list, only fails when out of memory
2307 if (list_append_tv(list, tv) == FAIL)
2308 return NOTDONE;
2309 clear_tv(tv);
2310 }
2311 }
2312 else if (dest_type == VAR_DICT)
2313 {
2314 char_u *key = tv_idx->vval.v_string;
2315 dict_T *dict = tv_dest->vval.v_dict;
2316 dictitem_T *di;
2317
2318 SOURCING_LNUM = iptr->isn_lnum;
2319 if (dict == NULL)
2320 {
2321 emsg(_(e_dictionary_not_set));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002322 return FAIL;
Bram Moolenaare08be092022-02-17 13:08:26 +00002323 }
2324 if (key == NULL)
2325 key = (char_u *)"";
2326 di = dict_find(dict, key, -1);
2327 if (di != NULL)
2328 {
2329 if (error_if_locked(di->di_tv.v_lock,
2330 e_cannot_change_dict_item))
2331 return FAIL;
2332 // overwrite existing value
2333 clear_tv(&di->di_tv);
2334 di->di_tv = *tv;
2335 }
2336 else
2337 {
2338 if (error_if_locked(dict->dv_lock, e_cannot_change_dict))
2339 return FAIL;
2340 // add to dict, only fails when out of memory
2341 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
2342 return NOTDONE;
2343 clear_tv(tv);
2344 }
2345 }
2346 else if (dest_type == VAR_BLOB)
2347 {
Bram Moolenaare08be092022-02-17 13:08:26 +00002348 blob_T *blob = tv_dest->vval.v_blob;
Bram Moolenaar65b0d162022-12-13 18:43:22 +00002349 varnumber_T nr;
2350 int error = FALSE;
2351 int len;
Bram Moolenaare08be092022-02-17 13:08:26 +00002352
2353 if (blob == NULL)
2354 {
2355 emsg(_(e_blob_not_set));
2356 return FAIL;
2357 }
2358 len = blob_len(blob);
2359 if (lidx < 0 && len + lidx >= 0)
2360 // negative index is relative to the end
2361 lidx = len + lidx;
2362
2363 // Can add one byte at the end.
2364 if (lidx < 0 || lidx > len)
2365 {
2366 semsg(_(e_blob_index_out_of_range_nr), lidx);
2367 return FAIL;
2368 }
2369 if (value_check_lock(blob->bv_lock, (char_u *)"blob", FALSE))
2370 return FAIL;
2371 nr = tv_get_number_chk(tv, &error);
2372 if (error)
2373 return FAIL;
2374 blob_set_append(blob, lidx, nr);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002375 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002376 else if (dest_type == VAR_CLASS || dest_type == VAR_OBJECT)
2377 {
Yegappan Lakshmanan3775f772023-09-01 22:05:45 +02002378 typval_T *otv;
Bram Moolenaarf7d1c6e2023-01-16 20:47:57 +00002379
Yegappan Lakshmanan3775f772023-09-01 22:05:45 +02002380 if (dest_type == VAR_OBJECT)
2381 {
2382 object_T *obj = tv_dest->vval.v_object;
2383
2384 otv = (typval_T *)(obj + 1);
2385 class_T *itf = iptr->isn_arg.storeindex.si_class;
2386 if (itf != NULL)
2387 // convert interface member index to class member index
Ernie Rael18143d32023-09-04 22:30:41 +02002388 lidx = object_index_from_itf_index(itf, FALSE, lidx,
Yegappan Lakshmanan5a05d372023-09-29 19:43:11 +02002389 obj->obj_class);
Yegappan Lakshmanan3775f772023-09-01 22:05:45 +02002390 }
2391 else
2392 {
2393 // VAR_CLASS
2394 class_T *class = tv_dest->vval.v_class;
2395 otv = class->class_members_tv;
2396 }
Bram Moolenaarf7d1c6e2023-01-16 20:47:57 +00002397
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002398 clear_tv(&otv[lidx]);
2399 otv[lidx] = *tv;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002400 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002401 else
2402 {
Bram Moolenaare08be092022-02-17 13:08:26 +00002403 status = FAIL;
2404 semsg(_(e_cannot_index_str), vartype_name(dest_type));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002405 }
2406 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002407
2408 clear_tv(tv_idx);
2409 clear_tv(tv_dest);
2410 ectx->ec_stack.ga_len -= 3;
2411 if (status == FAIL)
2412 {
2413 clear_tv(tv);
2414 return FAIL;
2415 }
2416 return OK;
2417}
2418
2419/*
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002420 * Store a value in a list or blob range.
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002421 */
2422 static int
2423execute_storerange(isn_T *iptr, ectx_T *ectx)
2424{
2425 typval_T *tv;
2426 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2427 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2428 typval_T *tv_dest = STACK_TV_BOT(-1);
2429 int status = OK;
2430
2431 // Stack contains:
2432 // -4 value to be stored
2433 // -3 first index or "none"
2434 // -2 second index or "none"
2435 // -1 destination list or blob
2436 tv = STACK_TV_BOT(-4);
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002437 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002438 if (tv_dest->v_type == VAR_LIST)
2439 {
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002440 long n1;
2441 long n2;
2442 listitem_T *li1;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002443
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002444 n1 = (long)tv_get_number_chk(tv_idx1, NULL);
2445 if (tv_idx2->v_type == VAR_SPECIAL
2446 && tv_idx2->vval.v_number == VVAL_NONE)
2447 n2 = list_len(tv_dest->vval.v_list) - 1;
2448 else
2449 n2 = (long)tv_get_number_chk(tv_idx2, NULL);
2450
Bram Moolenaar22ebd172022-04-01 15:26:58 +01002451 li1 = check_range_index_one(tv_dest->vval.v_list, &n1, TRUE, FALSE);
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002452 if (li1 == NULL)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002453 status = FAIL;
2454 else
2455 {
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002456 status = check_range_index_two(tv_dest->vval.v_list,
2457 &n1, li1, &n2, FALSE);
2458 if (status != FAIL)
2459 status = list_assign_range(
2460 tv_dest->vval.v_list,
2461 tv->vval.v_list,
2462 n1,
2463 n2,
2464 tv_idx2->v_type == VAR_SPECIAL,
2465 (char_u *)"=",
2466 (char_u *)"[unknown]");
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002467 }
2468 }
2469 else if (tv_dest->v_type == VAR_BLOB)
2470 {
2471 varnumber_T n1;
2472 varnumber_T n2;
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002473 long bloblen;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002474
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002475 n1 = tv_get_number_chk(tv_idx1, NULL);
2476 if (tv_idx2->v_type == VAR_SPECIAL
2477 && tv_idx2->vval.v_number == VVAL_NONE)
2478 n2 = blob_len(tv_dest->vval.v_blob) - 1;
2479 else
2480 n2 = tv_get_number_chk(tv_idx2, NULL);
2481 bloblen = blob_len(tv_dest->vval.v_blob);
2482
2483 if (check_blob_index(bloblen, n1, FALSE) == FAIL
2484 || check_blob_range(bloblen, n1, n2, FALSE) == FAIL)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002485 status = FAIL;
2486 else
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002487 status = blob_set_range(tv_dest->vval.v_blob, n1, n2, tv);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002488 }
2489 else
2490 {
2491 status = FAIL;
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002492 emsg(_(e_list_or_blob_required));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002493 }
2494
2495 clear_tv(tv_idx1);
2496 clear_tv(tv_idx2);
2497 clear_tv(tv_dest);
2498 ectx->ec_stack.ga_len -= 4;
2499 clear_tv(tv);
2500
2501 return status;
2502}
2503
2504/*
2505 * Unlet item in list or dict variable.
2506 */
2507 static int
2508execute_unletindex(isn_T *iptr, ectx_T *ectx)
2509{
2510 typval_T *tv_idx = STACK_TV_BOT(-2);
2511 typval_T *tv_dest = STACK_TV_BOT(-1);
2512 int status = OK;
2513
2514 // Stack contains:
2515 // -2 index
2516 // -1 dict or list
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002517 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002518 if (tv_dest->v_type == VAR_DICT)
2519 {
2520 // unlet a dict item, index must be a string
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002521 if (tv_idx->v_type != VAR_STRING && tv_idx->v_type != VAR_NUMBER)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002522 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002523 semsg(_(e_expected_str_but_got_str),
2524 vartype_name(VAR_STRING),
2525 vartype_name(tv_idx->v_type));
2526 status = FAIL;
2527 }
2528 else
2529 {
2530 dict_T *d = tv_dest->vval.v_dict;
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002531 char_u *key;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002532 dictitem_T *di = NULL;
2533
2534 if (d != NULL && value_check_lock(
2535 d->dv_lock, NULL, FALSE))
2536 status = FAIL;
2537 else
2538 {
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002539 if (tv_idx->v_type == VAR_STRING)
2540 {
2541 key = tv_idx->vval.v_string;
2542 if (key == NULL)
2543 key = (char_u *)"";
2544 }
2545 else
2546 {
2547 key = tv_get_string(tv_idx);
2548 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002549 if (d != NULL)
2550 di = dict_find(d, key, (int)STRLEN(key));
2551 if (di == NULL)
2552 {
2553 // NULL dict is equivalent to empty dict
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00002554 semsg(_(e_key_not_present_in_dictionary_str), key);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002555 status = FAIL;
2556 }
2557 else if (var_check_fixed(di->di_flags,
2558 NULL, FALSE)
2559 || var_check_ro(di->di_flags,
2560 NULL, FALSE))
2561 status = FAIL;
2562 else
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002563 dictitem_remove(d, di, "unlet");
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002564 }
2565 }
2566 }
2567 else if (tv_dest->v_type == VAR_LIST)
2568 {
2569 // unlet a List item, index must be a number
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002570 if (check_for_number(tv_idx) == FAIL)
2571 {
2572 status = FAIL;
2573 }
2574 else
2575 {
2576 list_T *l = tv_dest->vval.v_list;
2577 long n = (long)tv_idx->vval.v_number;
2578
2579 if (l != NULL && value_check_lock(
2580 l->lv_lock, NULL, FALSE))
2581 status = FAIL;
2582 else
2583 {
2584 listitem_T *li = list_find(l, n);
2585
2586 if (li == NULL)
2587 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002588 semsg(_(e_list_index_out_of_range_nr), n);
2589 status = FAIL;
2590 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002591 else
2592 listitem_remove(l, li);
2593 }
2594 }
2595 }
2596 else
2597 {
2598 status = FAIL;
2599 semsg(_(e_cannot_index_str),
2600 vartype_name(tv_dest->v_type));
2601 }
2602
2603 clear_tv(tv_idx);
2604 clear_tv(tv_dest);
2605 ectx->ec_stack.ga_len -= 2;
2606
2607 return status;
2608}
2609
2610/*
2611 * Unlet a range of items in a list variable.
2612 */
2613 static int
2614execute_unletrange(isn_T *iptr, ectx_T *ectx)
2615{
2616 // Stack contains:
2617 // -3 index1
2618 // -2 index2
2619 // -1 dict or list
2620 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2621 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2622 typval_T *tv_dest = STACK_TV_BOT(-1);
2623 int status = OK;
2624
2625 if (tv_dest->v_type == VAR_LIST)
2626 {
2627 // indexes must be a number
2628 SOURCING_LNUM = iptr->isn_lnum;
2629 if (check_for_number(tv_idx1) == FAIL
2630 || (tv_idx2->v_type != VAR_SPECIAL
2631 && check_for_number(tv_idx2) == FAIL))
2632 {
2633 status = FAIL;
2634 }
2635 else
2636 {
2637 list_T *l = tv_dest->vval.v_list;
2638 long n1 = (long)tv_idx1->vval.v_number;
2639 long n2 = tv_idx2->v_type == VAR_SPECIAL
2640 ? 0 : (long)tv_idx2->vval.v_number;
2641 listitem_T *li;
2642
2643 li = list_find_index(l, &n1);
2644 if (li == NULL)
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002645 {
2646 semsg(_(e_list_index_out_of_range_nr),
2647 (long)tv_idx1->vval.v_number);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002648 status = FAIL;
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002649 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002650 else
2651 {
2652 if (n1 < 0)
2653 n1 = list_idx_of_item(l, li);
2654 if (n2 < 0)
2655 {
2656 listitem_T *li2 = list_find(l, n2);
2657
2658 if (li2 == NULL)
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002659 {
2660 semsg(_(e_list_index_out_of_range_nr), n2);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002661 status = FAIL;
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002662 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002663 else
2664 n2 = list_idx_of_item(l, li2);
2665 }
2666 if (status != FAIL
2667 && tv_idx2->v_type != VAR_SPECIAL
2668 && n2 < n1)
2669 {
2670 semsg(_(e_list_index_out_of_range_nr), n2);
2671 status = FAIL;
2672 }
Bram Moolenaar6b8c7ba2022-03-20 17:46:06 +00002673 if (status != FAIL)
2674 list_unlet_range(l, li, n1,
2675 tv_idx2->v_type != VAR_SPECIAL, n2);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002676 }
2677 }
2678 }
2679 else
2680 {
2681 status = FAIL;
2682 SOURCING_LNUM = iptr->isn_lnum;
2683 semsg(_(e_cannot_index_str),
2684 vartype_name(tv_dest->v_type));
2685 }
2686
2687 clear_tv(tv_idx1);
2688 clear_tv(tv_idx2);
2689 clear_tv(tv_dest);
2690 ectx->ec_stack.ga_len -= 3;
2691
2692 return status;
2693}
2694
2695/*
2696 * Top of a for loop.
2697 */
2698 static int
2699execute_for(isn_T *iptr, ectx_T *ectx)
2700{
2701 typval_T *tv;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002702 int jump = FALSE;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002703 typval_T *ltv = STACK_TV_BOT(-1);
2704 typval_T *idxtv =
Bram Moolenaarcc341812022-09-19 15:54:34 +01002705 STACK_TV_VAR(iptr->isn_arg.forloop.for_loop_idx);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002706
2707 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
2708 return FAIL;
2709 if (ltv->v_type == VAR_LIST)
2710 {
2711 list_T *list = ltv->vval.v_list;
2712
2713 // push the next item from the list
2714 ++idxtv->vval.v_number;
2715 if (list == NULL
2716 || idxtv->vval.v_number >= list->lv_len)
2717 {
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002718 jump = TRUE;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002719 }
2720 else if (list->lv_first == &range_list_item)
2721 {
2722 // non-materialized range() list
2723 tv = STACK_TV_BOT(0);
2724 tv->v_type = VAR_NUMBER;
2725 tv->v_lock = 0;
2726 tv->vval.v_number = list_find_nr(
2727 list, idxtv->vval.v_number, NULL);
2728 ++ectx->ec_stack.ga_len;
2729 }
2730 else
2731 {
2732 listitem_T *li = list_find(list,
2733 idxtv->vval.v_number);
2734
2735 copy_tv(&li->li_tv, STACK_TV_BOT(0));
2736 ++ectx->ec_stack.ga_len;
2737 }
2738 }
2739 else if (ltv->v_type == VAR_STRING)
2740 {
2741 char_u *str = ltv->vval.v_string;
2742
2743 // The index is for the last byte of the previous
2744 // character.
2745 ++idxtv->vval.v_number;
2746 if (str == NULL || str[idxtv->vval.v_number] == NUL)
2747 {
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002748 jump = TRUE;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002749 }
2750 else
2751 {
2752 int clen = mb_ptr2len(str + idxtv->vval.v_number);
2753
2754 // Push the next character from the string.
2755 tv = STACK_TV_BOT(0);
2756 tv->v_type = VAR_STRING;
2757 tv->vval.v_string = vim_strnsave(
2758 str + idxtv->vval.v_number, clen);
2759 ++ectx->ec_stack.ga_len;
2760 idxtv->vval.v_number += clen - 1;
2761 }
2762 }
2763 else if (ltv->v_type == VAR_BLOB)
2764 {
2765 blob_T *blob = ltv->vval.v_blob;
2766
2767 // When we get here the first time make a copy of the
2768 // blob, so that the iteration still works when it is
2769 // changed.
2770 if (idxtv->vval.v_number == -1 && blob != NULL)
2771 {
2772 blob_copy(blob, ltv);
2773 blob_unref(blob);
2774 blob = ltv->vval.v_blob;
2775 }
2776
2777 // The index is for the previous byte.
2778 ++idxtv->vval.v_number;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002779 if (blob == NULL || idxtv->vval.v_number >= blob_len(blob))
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002780 {
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002781 jump = TRUE;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002782 }
2783 else
2784 {
2785 // Push the next byte from the blob.
2786 tv = STACK_TV_BOT(0);
2787 tv->v_type = VAR_NUMBER;
2788 tv->vval.v_number = blob_get(blob,
2789 idxtv->vval.v_number);
2790 ++ectx->ec_stack.ga_len;
2791 }
2792 }
2793 else
2794 {
2795 semsg(_(e_for_loop_on_str_not_supported),
2796 vartype_name(ltv->v_type));
2797 return FAIL;
2798 }
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002799
2800 if (jump)
2801 {
2802 // past the end of the list/string/blob, jump to "endfor"
2803 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
2804 may_restore_cmdmod(&ectx->ec_funclocal);
2805 }
2806 else
2807 {
2808 // Store the current number of funcrefs, this may be used in
2809 // ISN_LOOPEND. The variable index is always one more than the loop
2810 // variable index.
Bram Moolenaarcc341812022-09-19 15:54:34 +01002811 tv = STACK_TV_VAR(iptr->isn_arg.forloop.for_loop_idx + 1);
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002812 tv->vval.v_number = ectx->ec_funcrefs.ga_len;
2813 }
2814
2815 return OK;
2816}
2817
2818/*
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002819 * Code for handling variables declared inside a loop and used in a closure.
2820 * This is very similar to what is done with funcstack_T. The difference is
2821 * that the funcstack_T has the scope of a function, while a loopvars_T has the
2822 * scope of the block inside a loop and each loop may have its own.
2823 */
2824
2825// Double linked list of loopvars_T in use.
2826static loopvars_T *first_loopvars = NULL;
2827
2828 static void
2829add_loopvars_to_list(loopvars_T *loopvars)
2830{
2831 // Link in list of loopvarss.
2832 if (first_loopvars != NULL)
2833 first_loopvars->lvs_prev = loopvars;
2834 loopvars->lvs_next = first_loopvars;
2835 loopvars->lvs_prev = NULL;
2836 first_loopvars = loopvars;
2837}
2838
2839 static void
2840remove_loopvars_from_list(loopvars_T *loopvars)
2841{
2842 if (loopvars->lvs_prev == NULL)
2843 first_loopvars = loopvars->lvs_next;
2844 else
2845 loopvars->lvs_prev->lvs_next = loopvars->lvs_next;
2846 if (loopvars->lvs_next != NULL)
2847 loopvars->lvs_next->lvs_prev = loopvars->lvs_prev;
2848}
2849
2850/*
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002851 * End of a for or while loop: Handle any variables used by a closure.
2852 */
2853 static int
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002854execute_endloop(isn_T *iptr, ectx_T *ectx)
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002855{
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002856 endloop_T *endloop = &iptr->isn_arg.endloop;
2857 typval_T *tv_refcount = STACK_TV_VAR(endloop->end_funcref_idx);
2858 int prev_closure_count = tv_refcount->vval.v_number;
Bram Moolenaarcc341812022-09-19 15:54:34 +01002859 int depth = endloop->end_depth;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002860 garray_T *gap = &ectx->ec_funcrefs;
2861 int closure_in_use = FALSE;
2862 loopvars_T *loopvars;
2863 typval_T *stack;
2864 int idx;
2865
Bram Moolenaarcc341812022-09-19 15:54:34 +01002866 // Check if any created closure is still being referenced and loopvars have
2867 // not been saved yet for the current depth.
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002868 for (idx = prev_closure_count; idx < gap->ga_len; ++idx)
2869 {
2870 partial_T *pt = ((partial_T **)gap->ga_data)[idx];
2871
Bram Moolenaarcc341812022-09-19 15:54:34 +01002872 if (pt->pt_refcount > 1 && pt->pt_loopvars[depth] == NULL)
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002873 {
2874 int refcount = pt->pt_refcount;
2875 int i;
2876
2877 // A Reference in a variable inside the loop doesn't count, it gets
2878 // unreferenced at the end of the loop.
2879 for (i = 0; i < endloop->end_var_count; ++i)
2880 {
2881 typval_T *stv = STACK_TV_VAR(endloop->end_var_idx + i);
2882
2883 if (stv->v_type == VAR_PARTIAL && pt == stv->vval.v_partial)
2884 --refcount;
2885 }
2886 if (refcount > 1)
2887 {
2888 closure_in_use = TRUE;
2889 break;
2890 }
2891 }
2892 }
2893
2894 // If no function reference were created since the start of the loop block
2895 // or it is no longer referenced there is nothing to do.
2896 if (!closure_in_use)
2897 return OK;
2898
2899 // A closure is using variables declared inside the loop.
2900 // Move them to the called function.
2901 loopvars = ALLOC_CLEAR_ONE(loopvars_T);
2902 if (loopvars == NULL)
2903 return FAIL;
2904
2905 loopvars->lvs_ga.ga_len = endloop->end_var_count;
2906 stack = ALLOC_CLEAR_MULT(typval_T, loopvars->lvs_ga.ga_len);
2907 loopvars->lvs_ga.ga_data = stack;
2908 if (stack == NULL)
2909 {
2910 vim_free(loopvars);
2911 return FAIL;
2912 }
2913 add_loopvars_to_list(loopvars);
2914
2915 // Move the variable values.
2916 for (idx = 0; idx < endloop->end_var_count; ++idx)
2917 {
2918 typval_T *tv = STACK_TV_VAR(endloop->end_var_idx + idx);
2919
2920 *(stack + idx) = *tv;
2921 tv->v_type = VAR_UNKNOWN;
2922 }
2923
2924 for (idx = prev_closure_count; idx < gap->ga_len; ++idx)
2925 {
2926 partial_T *pt = ((partial_T **)gap->ga_data)[idx];
2927
Bram Moolenaarcc341812022-09-19 15:54:34 +01002928 if (pt->pt_refcount > 1 && pt->pt_loopvars[depth] == NULL)
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002929 {
2930 ++loopvars->lvs_refcount;
Bram Moolenaarcc341812022-09-19 15:54:34 +01002931 pt->pt_loopvars[depth] = loopvars;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002932
Bram Moolenaarcc341812022-09-19 15:54:34 +01002933 pt->pt_outer.out_loop[depth].stack = &loopvars->lvs_ga;
2934 pt->pt_outer.out_loop[depth].var_idx -=
2935 ectx->ec_frame_idx + STACK_FRAME_SIZE + endloop->end_var_idx;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002936 }
2937 }
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002938
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002939 return OK;
2940}
2941
2942/*
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002943 * Called when a partial is freed or its reference count goes down to one. The
2944 * loopvars may be the only reference to the partials in the local variables.
2945 * Go over all of them, the funcref and can be freed if all partials
2946 * referencing the loopvars have a reference count of one.
Bram Moolenaarcc341812022-09-19 15:54:34 +01002947 * Return TRUE if it was freed.
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002948 */
Bram Moolenaarcc341812022-09-19 15:54:34 +01002949 int
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002950loopvars_check_refcount(loopvars_T *loopvars)
2951{
2952 int i;
2953 garray_T *gap = &loopvars->lvs_ga;
2954 int done = 0;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002955 typval_T *stack = gap->ga_data;
2956
Bram Moolenaarcc341812022-09-19 15:54:34 +01002957 if (loopvars->lvs_refcount > loopvars->lvs_min_refcount)
2958 return FALSE;
2959 for (i = 0; i < gap->ga_len; ++i)
2960 {
2961 typval_T *tv = ((typval_T *)gap->ga_data) + i;
2962
2963 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL
2964 && tv->vval.v_partial->pt_refcount == 1)
2965 {
2966 int depth;
2967
2968 for (depth = 0; depth < MAX_LOOP_DEPTH; ++depth)
2969 if (tv->vval.v_partial->pt_loopvars[depth] == loopvars)
2970 ++done;
2971 }
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002972 }
Bram Moolenaarcc341812022-09-19 15:54:34 +01002973 if (done != loopvars->lvs_min_refcount)
2974 return FALSE;
2975
2976 // All partials referencing the loopvars have a reference count of
2977 // one, thus the loopvars is no longer of use.
2978 stack = gap->ga_data;
2979 for (i = 0; i < gap->ga_len; ++i)
2980 clear_tv(stack + i);
2981 vim_free(stack);
2982 remove_loopvars_from_list(loopvars);
2983 vim_free(loopvars);
2984 return TRUE;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002985}
2986
2987/*
2988 * For garbage collecting: set references in all variables referenced by
2989 * all loopvars.
2990 */
2991 int
2992set_ref_in_loopvars(int copyID)
2993{
2994 loopvars_T *loopvars;
2995
2996 for (loopvars = first_loopvars; loopvars != NULL;
2997 loopvars = loopvars->lvs_next)
2998 {
2999 typval_T *stack = loopvars->lvs_ga.ga_data;
3000 int i;
3001
3002 for (i = 0; i < loopvars->lvs_ga.ga_len; ++i)
3003 if (set_ref_in_item(stack + i, copyID, NULL, NULL))
3004 return TRUE; // abort
3005 }
3006 return FALSE;
3007}
3008
3009/*
Bram Moolenaar06b77222022-01-25 15:51:56 +00003010 * Load instruction for w:/b:/g:/t: variable.
3011 * "isn_type" is used instead of "iptr->isn_type".
3012 */
3013 static int
3014load_namespace_var(ectx_T *ectx, isntype_T isn_type, isn_T *iptr)
3015{
3016 dictitem_T *di = NULL;
3017 hashtab_T *ht = NULL;
3018 char namespace;
3019
3020 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
3021 return NOTDONE;
3022 switch (isn_type)
3023 {
3024 case ISN_LOADG:
3025 ht = get_globvar_ht();
3026 namespace = 'g';
3027 break;
3028 case ISN_LOADB:
3029 ht = &curbuf->b_vars->dv_hashtab;
3030 namespace = 'b';
3031 break;
3032 case ISN_LOADW:
3033 ht = &curwin->w_vars->dv_hashtab;
3034 namespace = 'w';
3035 break;
3036 case ISN_LOADT:
3037 ht = &curtab->tp_vars->dv_hashtab;
3038 namespace = 't';
3039 break;
3040 default: // Cannot reach here
3041 return NOTDONE;
3042 }
3043 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
3044
Bram Moolenaar06b77222022-01-25 15:51:56 +00003045 if (di == NULL)
3046 {
Bram Moolenaarfe732552022-02-22 19:39:13 +00003047 if (isn_type == ISN_LOADG)
3048 {
3049 ufunc_T *ufunc = find_func(iptr->isn_arg.string, TRUE);
3050
3051 // g:Something could be a function
3052 if (ufunc != NULL)
3053 {
3054 typval_T *tv = STACK_TV_BOT(0);
3055
3056 ++ectx->ec_stack.ga_len;
3057 tv->v_type = VAR_FUNC;
3058 tv->vval.v_string = alloc(STRLEN(iptr->isn_arg.string) + 3);
3059 if (tv->vval.v_string == NULL)
3060 return FAIL;
3061 STRCPY(tv->vval.v_string, "g:");
3062 STRCPY(tv->vval.v_string + 2, iptr->isn_arg.string);
3063 return OK;
3064 }
3065 }
Bram Moolenaar06b77222022-01-25 15:51:56 +00003066 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarfe732552022-02-22 19:39:13 +00003067 if (vim_strchr(iptr->isn_arg.string, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar06b77222022-01-25 15:51:56 +00003068 // no check if the item exists in the script but
3069 // isn't exported, it is too complicated
Bram Moolenaarfe732552022-02-22 19:39:13 +00003070 semsg(_(e_item_not_found_in_script_str), iptr->isn_arg.string);
Bram Moolenaar06b77222022-01-25 15:51:56 +00003071 else
3072 semsg(_(e_undefined_variable_char_str),
Bram Moolenaarfe732552022-02-22 19:39:13 +00003073 namespace, iptr->isn_arg.string);
Bram Moolenaar06b77222022-01-25 15:51:56 +00003074 return FAIL;
3075 }
3076 else
3077 {
3078 copy_tv(&di->di_tv, STACK_TV_BOT(0));
3079 ++ectx->ec_stack.ga_len;
3080 }
3081 return OK;
3082}
3083
Bram Moolenaard0200c82023-01-28 15:19:40 +00003084
3085 static void
3086object_required_error(typval_T *tv)
3087{
3088 garray_T type_list;
3089 ga_init2(&type_list, sizeof(type_T *), 10);
3090 type_T *type = typval2type(tv, get_copyID(), &type_list, TVTT_DO_MEMBER);
3091 char *tofree = NULL;
3092 char *typename = type_name(type, &tofree);
3093 semsg(_(e_object_required_found_str), typename);
3094 vim_free(tofree);
3095 clear_type_list(&type_list);
3096}
3097
Bram Moolenaar06b77222022-01-25 15:51:56 +00003098/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02003099 * Execute instructions in execution context "ectx".
3100 * Return OK or FAIL;
3101 */
3102 static int
3103exec_instructions(ectx_T *ectx)
3104{
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003105 int ret = FAIL;
3106 int save_trylevel_at_start = ectx->ec_trylevel_at_start;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02003107 int dict_stack_len_at_start = dict_stack.ga_len;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003108
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02003109 // Start execution at the first instruction.
Bram Moolenaar4c137212021-04-19 16:48:48 +02003110 ectx->ec_iidx = 0;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01003111
Bram Moolenaarff652882021-05-16 15:24:49 +02003112 // Only catch exceptions in this instruction list.
3113 ectx->ec_trylevel_at_start = trylevel;
3114
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003115 for (;;)
3116 {
Bram Moolenaara7490192021-07-22 12:26:14 +02003117 static int breakcheck_count = 0; // using "static" makes it faster
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003118 isn_T *iptr;
Bram Moolenaara7490192021-07-22 12:26:14 +02003119 typval_T *tv;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003120
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003121 if (unlikely(++breakcheck_count >= 100))
Bram Moolenaar270d0382020-05-15 21:42:53 +02003122 {
3123 line_breakcheck();
3124 breakcheck_count = 0;
3125 }
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003126 if (unlikely(got_int))
Bram Moolenaar20431c92020-03-20 18:39:46 +01003127 {
3128 // Turn CTRL-C into an exception.
3129 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +01003130 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003131 goto theend;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003132 did_throw = TRUE;
3133 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003134
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003135 if (unlikely(did_emsg && msg_list != NULL && *msg_list != NULL))
Bram Moolenaara26b9702020-04-18 19:53:28 +02003136 {
3137 // Turn an error message into an exception.
3138 did_emsg = FALSE;
3139 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003140 goto theend;
Bram Moolenaara26b9702020-04-18 19:53:28 +02003141 did_throw = TRUE;
3142 *msg_list = NULL;
Bram Moolenaar3ef2e412023-04-30 18:50:48 +01003143
3144 // This exception was not caught (yet).
3145 garray_T *trystack = &ectx->ec_trystack;
3146 if (trystack->ga_len > 0)
3147 {
3148 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
3149 + trystack->ga_len - 1;
3150 if (trycmd->tcd_frame_idx == ectx->ec_frame_idx)
3151 trycmd->tcd_caught = FALSE;
3152 }
Bram Moolenaara26b9702020-04-18 19:53:28 +02003153 }
3154
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003155 if (unlikely(did_throw))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003156 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003157 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003158 trycmd_T *trycmd = NULL;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003159 int index = trystack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003160
3161 // An exception jumps to the first catch, finally, or returns from
3162 // the current function.
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003163 while (index > 0)
3164 {
3165 trycmd = ((trycmd_T *)trystack->ga_data) + index - 1;
Bram Moolenaar3ef2e412023-04-30 18:50:48 +01003166 // 1. after :try and before :catch - jump to first :catch
3167 // 2. in :catch block - jump to :finally
3168 // 3. in :catch block and no finally - jump to :endtry
3169 if (!trycmd->tcd_in_catch || trycmd->tcd_finally_idx != 0
3170 || trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003171 break;
3172 // In the catch and finally block of this try we have to go up
3173 // one level.
3174 --index;
3175 trycmd = NULL;
3176 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003177 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003178 {
Bram Moolenaar834193a2021-06-30 20:39:15 +02003179 if (trycmd->tcd_in_catch)
3180 {
Bram Moolenaar3ef2e412023-04-30 18:50:48 +01003181 if (trycmd->tcd_finally_idx > 0)
3182 {
3183 // exception inside ":catch", jump to ":finally" once
3184 ectx->ec_iidx = trycmd->tcd_finally_idx;
3185 trycmd->tcd_finally_idx = 0;
3186 }
3187 else
3188 {
3189 // exception inside ":catch" or ":finally", jump to
3190 // ":endtry"
3191 ectx->ec_iidx = trycmd->tcd_endtry_idx;
3192 }
Bram Moolenaar834193a2021-06-30 20:39:15 +02003193 }
3194 else
Bram Moolenaar3ef2e412023-04-30 18:50:48 +01003195 {
Bram Moolenaar834193a2021-06-30 20:39:15 +02003196 // jump to first ":catch"
3197 ectx->ec_iidx = trycmd->tcd_catch_idx;
Bram Moolenaar3ef2e412023-04-30 18:50:48 +01003198 trycmd->tcd_in_catch = TRUE;
3199 }
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003200 did_throw = FALSE; // don't come back here until :endtry
3201 trycmd->tcd_did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003202 }
3203 else
3204 {
Bram Moolenaar3ef2e412023-04-30 18:50:48 +01003205 // Not inside try or need to return from current function.
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02003206 // Push a dummy return value.
Bram Moolenaar35578162021-08-02 19:10:38 +02003207 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003208 goto theend;
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02003209 tv = STACK_TV_BOT(0);
3210 tv->v_type = VAR_NUMBER;
3211 tv->vval.v_number = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003212 ++ectx->ec_stack.ga_len;
3213 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003214 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02003215 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +01003216 need_rethrow = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003217 if (handle_closure_in_use(ectx, FALSE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003218 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003219 goto done;
3220 }
3221
Bram Moolenaar4c137212021-04-19 16:48:48 +02003222 if (func_return(ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003223 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003224 }
3225 continue;
3226 }
3227
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003228 /*
3229 * Big switch on the instruction. Most compilers will be turning this
3230 * into an efficient lookup table, since the "case" values are an enum
3231 * with sequential numbers. It may look ugly, but it should be the
3232 * most efficient way.
3233 */
Bram Moolenaar4c137212021-04-19 16:48:48 +02003234 iptr = &ectx->ec_instr[ectx->ec_iidx++];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003235 switch (iptr->isn_type)
3236 {
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00003237 // Constructor, first instruction in a new() method.
Bram Moolenaar00b28d62022-12-08 15:32:33 +00003238 case ISN_CONSTRUCT:
3239 // "this" is always the local variable at index zero
3240 tv = STACK_TV_VAR(0);
3241 tv->v_type = VAR_OBJECT;
3242 tv->vval.v_object = alloc_clear(
3243 iptr->isn_arg.construct.construct_size);
3244 tv->vval.v_object->obj_class =
3245 iptr->isn_arg.construct.construct_class;
Bram Moolenaard28d7b92022-12-08 20:42:00 +00003246 ++tv->vval.v_object->obj_class->class_refcount;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00003247 tv->vval.v_object->obj_refcount = 1;
Bram Moolenaard28d7b92022-12-08 20:42:00 +00003248 object_created(tv->vval.v_object);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00003249 break;
3250
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003251 // execute Ex command line
3252 case ISN_EXEC:
Ernie Raelee865f32023-09-29 19:53:55 +02003253 if (exec_command(iptr, iptr->isn_arg.string) == FAIL)
Bram Moolenaaraacc9662021-08-13 19:40:51 +02003254 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003255 break;
3256
Bram Moolenaar20677332021-06-06 17:02:53 +02003257 // execute Ex command line split at NL characters.
3258 case ISN_EXEC_SPLIT:
3259 {
3260 source_cookie_T cookie;
Bram Moolenaar518df272021-06-06 17:34:13 +02003261 char_u *line;
Bram Moolenaar20677332021-06-06 17:02:53 +02003262
3263 SOURCING_LNUM = iptr->isn_lnum;
3264 CLEAR_FIELD(cookie);
3265 cookie.sourcing_lnum = iptr->isn_lnum - 1;
3266 cookie.nextline = iptr->isn_arg.string;
Bram Moolenaar518df272021-06-06 17:34:13 +02003267 line = get_split_sourceline(0, &cookie, 0, 0);
3268 if (do_cmdline(line,
Bram Moolenaar20677332021-06-06 17:02:53 +02003269 get_split_sourceline, &cookie,
3270 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED)
3271 == FAIL
3272 || did_emsg)
Bram Moolenaar518df272021-06-06 17:34:13 +02003273 {
3274 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02003275 goto on_error;
Bram Moolenaar518df272021-06-06 17:34:13 +02003276 }
3277 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02003278 }
3279 break;
3280
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00003281 // execute Ex command line that is only a range
3282 case ISN_EXECRANGE:
3283 {
3284 exarg_T ea;
3285 char *error = NULL;
3286
3287 CLEAR_FIELD(ea);
3288 ea.cmdidx = CMD_SIZE;
3289 ea.addr_type = ADDR_LINES;
3290 ea.cmd = iptr->isn_arg.string;
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00003291 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00003292 parse_cmd_address(&ea, &error, FALSE);
Bram Moolenaar01a4dcb2021-12-04 13:15:10 +00003293 if (ea.cmd == NULL)
3294 goto on_error;
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00003295 // error is always NULL when using ADDR_LINES
3296 error = ex_range_without_command(&ea);
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00003297 if (error != NULL)
3298 {
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00003299 emsg(error);
3300 goto on_error;
3301 }
3302 }
3303 break;
3304
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02003305 // Evaluate an expression with legacy syntax, push it onto the
3306 // stack.
3307 case ISN_LEGACY_EVAL:
3308 {
3309 char_u *arg = iptr->isn_arg.string;
3310 int res;
3311 int save_flags = cmdmod.cmod_flags;
3312
Bram Moolenaar35578162021-08-02 19:10:38 +02003313 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003314 goto theend;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02003315 tv = STACK_TV_BOT(0);
3316 init_tv(tv);
3317 cmdmod.cmod_flags |= CMOD_LEGACY;
3318 res = eval0(arg, tv, NULL, &EVALARG_EVALUATE);
3319 cmdmod.cmod_flags = save_flags;
3320 if (res == FAIL)
3321 goto on_error;
3322 ++ectx->ec_stack.ga_len;
3323 }
3324 break;
3325
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003326 // push typeval VAR_INSTR with instructions to be executed
3327 case ISN_INSTR:
3328 {
Bram Moolenaar35578162021-08-02 19:10:38 +02003329 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003330 goto theend;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003331 tv = STACK_TV_BOT(0);
3332 tv->vval.v_instr = ALLOC_ONE(instr_T);
3333 if (tv->vval.v_instr == NULL)
3334 goto on_error;
3335 ++ectx->ec_stack.ga_len;
3336
3337 tv->v_type = VAR_INSTR;
3338 tv->vval.v_instr->instr_ectx = ectx;
3339 tv->vval.v_instr->instr_instr = iptr->isn_arg.instr;
3340 }
3341 break;
3342
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003343 case ISN_SOURCE:
3344 {
Bram Moolenaar89445512022-04-14 12:58:23 +01003345 int notused;
LemonBoy77142312022-04-15 20:50:46 +01003346
Bram Moolenaar89445512022-04-14 12:58:23 +01003347 SOURCING_LNUM = iptr->isn_lnum;
3348 if (may_load_script((int)iptr->isn_arg.number, &notused)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003349 == FAIL)
Bram Moolenaar89445512022-04-14 12:58:23 +01003350 goto on_error;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003351 }
3352 break;
3353
Bram Moolenaar4c137212021-04-19 16:48:48 +02003354 // execute :substitute with an expression
3355 case ISN_SUBSTITUTE:
3356 {
3357 subs_T *subs = &iptr->isn_arg.subs;
3358 source_cookie_T cookie;
3359 struct subs_expr_S *save_instr = substitute_instr;
3360 struct subs_expr_S subs_instr;
3361 int res;
3362
3363 subs_instr.subs_ectx = ectx;
3364 subs_instr.subs_instr = subs->subs_instr;
3365 subs_instr.subs_status = OK;
3366 substitute_instr = &subs_instr;
3367
3368 SOURCING_LNUM = iptr->isn_lnum;
3369 // This is very much like ISN_EXEC
3370 CLEAR_FIELD(cookie);
3371 cookie.sourcing_lnum = iptr->isn_lnum - 1;
3372 res = do_cmdline(subs->subs_cmd,
3373 getsourceline, &cookie,
3374 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
3375 substitute_instr = save_instr;
3376
3377 if (res == FAIL || did_emsg
3378 || subs_instr.subs_status == FAIL)
3379 goto on_error;
3380 }
3381 break;
3382
3383 case ISN_FINISH:
3384 goto done;
3385
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02003386 case ISN_REDIRSTART:
3387 // create a dummy entry for var_redir_str()
3388 if (alloc_redir_lval() == FAIL)
3389 goto on_error;
3390
3391 // The output is stored in growarray "redir_ga" until
3392 // redirection ends.
3393 init_redir_ga();
3394 redir_vname = 1;
3395 break;
3396
3397 case ISN_REDIREND:
3398 {
3399 char_u *res = get_clear_redir_ga();
3400
3401 // End redirection, put redirected text on the stack.
3402 clear_redir_lval();
3403 redir_vname = 0;
3404
Bram Moolenaar35578162021-08-02 19:10:38 +02003405 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02003406 {
3407 vim_free(res);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003408 goto theend;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02003409 }
3410 tv = STACK_TV_BOT(0);
3411 tv->v_type = VAR_STRING;
3412 tv->vval.v_string = res;
3413 ++ectx->ec_stack.ga_len;
3414 }
3415 break;
3416
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003417 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02003418#ifdef FEAT_QUICKFIX
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003419 force_abort = TRUE;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003420 if (trigger_cexpr_autocmd(iptr->isn_arg.number) == FAIL)
3421 goto on_error;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003422 force_abort = FALSE;
Bram Moolenaarb7c97812021-05-05 22:51:39 +02003423#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003424 break;
3425
3426 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02003427#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003428 {
3429 exarg_T ea;
3430 int res;
3431
3432 CLEAR_FIELD(ea);
3433 ea.cmdidx = iptr->isn_arg.cexpr.cexpr_ref->cer_cmdidx;
3434 ea.forceit = iptr->isn_arg.cexpr.cexpr_ref->cer_forceit;
3435 ea.cmdlinep = &iptr->isn_arg.cexpr.cexpr_ref->cer_cmdline;
3436 --ectx->ec_stack.ga_len;
3437 tv = STACK_TV_BOT(0);
Bram Moolenaarbd683e32022-07-18 17:49:03 +01003438 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003439 res = cexpr_core(&ea, tv);
3440 clear_tv(tv);
3441 if (res == FAIL)
3442 goto on_error;
3443 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02003444#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003445 break;
3446
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003447 // execute Ex command from pieces on the stack
3448 case ISN_EXECCONCAT:
3449 {
3450 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +02003451 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003452 int pass;
3453 int i;
3454 char_u *cmd = NULL;
3455 char_u *str;
3456
3457 for (pass = 1; pass <= 2; ++pass)
3458 {
3459 for (i = 0; i < count; ++i)
3460 {
3461 tv = STACK_TV_BOT(i - count);
3462 str = tv->vval.v_string;
3463 if (str != NULL && *str != NUL)
3464 {
3465 if (pass == 2)
3466 STRCPY(cmd + len, str);
3467 len += STRLEN(str);
3468 }
3469 if (pass == 2)
3470 clear_tv(tv);
3471 }
3472 if (pass == 1)
3473 {
3474 cmd = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003475 if (unlikely(cmd == NULL))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003476 goto theend;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003477 len = 0;
3478 }
3479 }
3480
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02003481 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003482 do_cmdline_cmd(cmd);
3483 vim_free(cmd);
3484 }
3485 break;
3486
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003487 // execute :echo {string} ...
3488 case ISN_ECHO:
3489 {
3490 int count = iptr->isn_arg.echo.echo_count;
3491 int atstart = TRUE;
3492 int needclr = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003493 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003494
3495 for (idx = 0; idx < count; ++idx)
3496 {
3497 tv = STACK_TV_BOT(idx - count);
3498 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
3499 &atstart, &needclr);
3500 clear_tv(tv);
3501 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01003502 if (needclr)
3503 msg_clr_eos();
Bram Moolenaar4c137212021-04-19 16:48:48 +02003504 ectx->ec_stack.ga_len -= count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003505 }
3506 break;
3507
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003508 // :execute {string} ...
3509 // :echomsg {string} ...
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01003510 // :echowindow {string} ...
Bram Moolenaar7de62622021-08-07 15:05:47 +02003511 // :echoconsole {string} ...
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003512 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01003513 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003514 case ISN_ECHOMSG:
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01003515 case ISN_ECHOWINDOW:
Bram Moolenaar7de62622021-08-07 15:05:47 +02003516 case ISN_ECHOCONSOLE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003517 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01003518 {
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01003519 int count;
Bram Moolenaarad39c092020-02-26 18:23:43 +01003520 garray_T ga;
3521 char_u buf[NUMBUFLEN];
3522 char_u *p;
3523 int len;
3524 int failed = FALSE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003525 int idx;
Bram Moolenaarad39c092020-02-26 18:23:43 +01003526
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01003527 if (iptr->isn_type == ISN_ECHOWINDOW)
3528 count = iptr->isn_arg.echowin.ewin_count;
3529 else
3530 count = iptr->isn_arg.number;
Bram Moolenaarad39c092020-02-26 18:23:43 +01003531 ga_init2(&ga, 1, 80);
3532 for (idx = 0; idx < count; ++idx)
3533 {
3534 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003535 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01003536 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003537 if (tv->v_type == VAR_CHANNEL
3538 || tv->v_type == VAR_JOB)
3539 {
3540 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar68db9962021-05-09 23:19:22 +02003541 semsg(_(e_using_invalid_value_as_string_str),
3542 vartype_name(tv->v_type));
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003543 break;
3544 }
3545 else
3546 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01003547 }
3548 else
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003549 p = tv_stringify(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01003550
3551 len = (int)STRLEN(p);
Bram Moolenaar35578162021-08-02 19:10:38 +02003552 if (GA_GROW_FAILS(&ga, len + 2))
Bram Moolenaarad39c092020-02-26 18:23:43 +01003553 failed = TRUE;
3554 else
3555 {
3556 if (ga.ga_len > 0)
3557 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
3558 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
3559 ga.ga_len += len;
3560 }
3561 clear_tv(tv);
3562 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003563 ectx->ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003564 if (failed)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003565 {
3566 ga_clear(&ga);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003567 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003568 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01003569
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003570 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003571 {
3572 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02003573 {
3574 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003575 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01003576 if (did_emsg)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003577 {
3578 ga_clear(&ga);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01003579 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003580 }
Bram Moolenaar430deb12020-08-23 16:29:11 +02003581 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003582 else
3583 {
3584 msg_sb_eol();
3585 if (iptr->isn_type == ISN_ECHOMSG)
3586 {
3587 msg_attr(ga.ga_data, echo_attr);
3588 out_flush();
3589 }
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01003590#ifdef HAS_MESSAGE_WINDOW
3591 else if (iptr->isn_type == ISN_ECHOWINDOW)
3592 {
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01003593 start_echowindow(
3594 iptr->isn_arg.echowin.ewin_time);
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01003595 msg_attr(ga.ga_data, echo_attr);
3596 end_echowindow();
3597 }
3598#endif
Bram Moolenaar7de62622021-08-07 15:05:47 +02003599 else if (iptr->isn_type == ISN_ECHOCONSOLE)
3600 {
3601 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data),
3602 TRUE);
3603 ui_write((char_u *)"\r\n", 2, TRUE);
3604 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003605 else
3606 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003607 SOURCING_LNUM = iptr->isn_lnum;
3608 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003609 }
3610 }
3611 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01003612 ga_clear(&ga);
3613 }
3614 break;
3615
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003616 // load local variable or argument
3617 case ISN_LOAD:
Bram Moolenaar35578162021-08-02 19:10:38 +02003618 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003619 goto theend;
Bram Moolenaar2ed57ac2023-04-01 22:05:38 +01003620 tv = STACK_TV_VAR(iptr->isn_arg.number);
3621 if (tv->v_type == VAR_UNKNOWN)
3622 {
3623 // missing argument or default value v:none
3624 STACK_TV_BOT(0)->v_type = VAR_SPECIAL;
3625 STACK_TV_BOT(0)->vval.v_number = VVAL_NONE;
3626 }
3627 else
3628 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003629 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003630 break;
3631
3632 // load v: variable
3633 case ISN_LOADV:
Bram Moolenaar35578162021-08-02 19:10:38 +02003634 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003635 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003636 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003637 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003638 break;
3639
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003640 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003641 case ISN_LOADSCRIPT:
3642 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01003643 scriptref_T *sref = iptr->isn_arg.script.scriptref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003644 svar_T *sv;
3645
Bram Moolenaar6db660b2021-08-01 14:08:54 +02003646 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003647 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003648 goto theend;
Bram Moolenaar859cc212022-03-28 15:22:35 +01003649 allocate_if_null(sv);
Bram Moolenaar35578162021-08-02 19:10:38 +02003650 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003651 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003652 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003653 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003654 }
3655 break;
3656
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003657 // load s: variable in old script or autoload import
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003658 case ISN_LOADS:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003659 case ISN_LOADEXPORT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003660 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003661 int sid = iptr->isn_arg.loadstore.ls_sid;
3662 hashtab_T *ht = &SCRIPT_VARS(sid);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003663 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003664 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003665
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003666 if (di == NULL)
3667 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003668 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003669 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003670 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003671 }
3672 else
3673 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003674 if (iptr->isn_type == ISN_LOADEXPORT)
3675 {
3676 int idx = get_script_item_idx(sid, name, 0,
3677 NULL, NULL);
3678 svar_T *sv;
3679
3680 if (idx >= 0)
3681 {
3682 sv = ((svar_T *)SCRIPT_ITEM(sid)
3683 ->sn_var_vals.ga_data) + idx;
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003684 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003685 {
3686 SOURCING_LNUM = iptr->isn_lnum;
3687 semsg(_(e_item_not_exported_in_script_str),
3688 name);
3689 goto on_error;
3690 }
3691 }
3692 }
Bram Moolenaar35578162021-08-02 19:10:38 +02003693 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003694 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003695 copy_tv(&di->di_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003696 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003697 }
3698 }
3699 break;
3700
Bram Moolenaard3aac292020-04-19 14:32:17 +02003701 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003702 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02003703 case ISN_LOADB:
3704 case ISN_LOADW:
3705 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003706 {
Bram Moolenaar06b77222022-01-25 15:51:56 +00003707 int res = load_namespace_var(ectx, iptr->isn_type, iptr);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003708
Bram Moolenaar06b77222022-01-25 15:51:56 +00003709 if (res == NOTDONE)
Bram Moolenaarcbbc48f2022-01-18 12:58:28 +00003710 goto theend;
Bram Moolenaar06b77222022-01-25 15:51:56 +00003711 if (res == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003712 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003713 }
Bram Moolenaar06b77222022-01-25 15:51:56 +00003714
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003715 break;
3716
Bram Moolenaar03290b82020-12-19 16:30:44 +01003717 // load autoload variable
3718 case ISN_LOADAUTO:
3719 {
3720 char_u *name = iptr->isn_arg.string;
3721
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 Moolenaar03290b82020-12-19 16:30:44 +01003724 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003725 if (eval_variable(name, (int)STRLEN(name), 0,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003726 STACK_TV_BOT(0), NULL, EVAL_VAR_VERBOSE) == FAIL)
Bram Moolenaar03290b82020-12-19 16:30:44 +01003727 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003728 ++ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003729 }
3730 break;
3731
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003732 // load g:/b:/w:/t: namespace
3733 case ISN_LOADGDICT:
3734 case ISN_LOADBDICT:
3735 case ISN_LOADWDICT:
3736 case ISN_LOADTDICT:
3737 {
3738 dict_T *d = NULL;
3739
3740 switch (iptr->isn_type)
3741 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02003742 case ISN_LOADGDICT: d = get_globvar_dict(); break;
3743 case ISN_LOADBDICT: d = curbuf->b_vars; break;
3744 case ISN_LOADWDICT: d = curwin->w_vars; break;
3745 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003746 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003747 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003748 }
Bram Moolenaar35578162021-08-02 19:10:38 +02003749 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003750 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003751 tv = STACK_TV_BOT(0);
3752 tv->v_type = VAR_DICT;
3753 tv->v_lock = 0;
3754 tv->vval.v_dict = d;
Bram Moolenaar1bd3cb22021-02-24 12:27:31 +01003755 ++d->dv_refcount;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003756 ++ectx->ec_stack.ga_len;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003757 }
3758 break;
3759
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003760 // load &option
3761 case ISN_LOADOPT:
3762 {
3763 typval_T optval;
3764 char_u *name = iptr->isn_arg.string;
3765
Bram Moolenaara8c17702020-04-01 21:17:24 +02003766 // This is not expected to fail, name is checked during
3767 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar35578162021-08-02 19:10:38 +02003768 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003769 goto theend;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003770 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003771 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003772 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003773 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003774 }
3775 break;
3776
3777 // load $ENV
3778 case ISN_LOADENV:
3779 {
3780 typval_T optval;
3781 char_u *name = iptr->isn_arg.string;
3782
Bram Moolenaar35578162021-08-02 19:10:38 +02003783 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003784 goto theend;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003785 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003786 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003787 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003788 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003789 }
3790 break;
3791
3792 // load @register
3793 case ISN_LOADREG:
Bram Moolenaar35578162021-08-02 19:10:38 +02003794 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003795 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003796 tv = STACK_TV_BOT(0);
3797 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003798 tv->v_lock = 0;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02003799 // This may result in NULL, which should be equivalent to an
3800 // empty string.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003801 tv->vval.v_string = get_reg_contents(
3802 iptr->isn_arg.number, GREG_EXPR_SRC);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003803 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003804 break;
3805
3806 // store local variable
3807 case ISN_STORE:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003808 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003809 tv = STACK_TV_VAR(iptr->isn_arg.number);
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02003810 if (STACK_TV_BOT(0)->v_type == VAR_TYPEALIAS)
3811 {
3812 semsg(_(e_using_typealias_as_variable),
3813 STACK_TV_BOT(0)->vval.v_typealias->ta_name);
3814 clear_tv(STACK_TV_BOT(0));
3815 goto on_error;
3816 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003817 clear_tv(tv);
3818 *tv = *STACK_TV_BOT(0);
3819 break;
3820
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003821 // store s: variable in old script or autoload import
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003822 case ISN_STORES:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003823 case ISN_STOREEXPORT:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003824 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003825 int sid = iptr->isn_arg.loadstore.ls_sid;
3826 hashtab_T *ht = &SCRIPT_VARS(sid);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003827 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003828 dictitem_T *di = find_var_in_ht(ht, 0,
3829 iptr->isn_type == ISN_STORES
3830 ? name + 2 : name, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003831
Bram Moolenaar4c137212021-04-19 16:48:48 +02003832 --ectx->ec_stack.ga_len;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003833 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003834 if (di == NULL)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003835 {
3836 if (iptr->isn_type == ISN_STOREEXPORT)
3837 {
3838 semsg(_(e_undefined_variable_str), name);
Bram Moolenaard1d26842022-03-31 10:13:47 +01003839 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003840 goto on_error;
3841 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003842 store_var(name, STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003843 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003844 else
3845 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003846 if (iptr->isn_type == ISN_STOREEXPORT)
3847 {
3848 int idx = get_script_item_idx(sid, name, 0,
3849 NULL, NULL);
3850
Bram Moolenaar06651632022-04-27 17:54:25 +01003851 // can this ever fail?
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003852 if (idx >= 0)
3853 {
3854 svar_T *sv = ((svar_T *)SCRIPT_ITEM(sid)
3855 ->sn_var_vals.ga_data) + idx;
3856
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003857 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003858 {
3859 semsg(_(e_item_not_exported_in_script_str),
3860 name);
Bram Moolenaard1d26842022-03-31 10:13:47 +01003861 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003862 goto on_error;
3863 }
3864 }
3865 }
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02003866 if (var_check_permission(di, name) == FAIL)
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003867 {
3868 clear_tv(STACK_TV_BOT(0));
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02003869 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003870 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003871 clear_tv(&di->di_tv);
3872 di->di_tv = *STACK_TV_BOT(0);
3873 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003874 }
3875 break;
3876
3877 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003878 case ISN_STORESCRIPT:
3879 {
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003880 scriptref_T *sref = iptr->isn_arg.script.scriptref;
3881 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003882
Bram Moolenaar6db660b2021-08-01 14:08:54 +02003883 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003884 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003885 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003886 --ectx->ec_stack.ga_len;
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003887
3888 // "const" and "final" are checked at compile time, locking
3889 // the value needs to be checked here.
3890 SOURCING_LNUM = iptr->isn_lnum;
3891 if (value_check_lock(sv->sv_tv->v_lock, sv->sv_name, FALSE))
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003892 {
3893 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003894 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003895 }
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003896
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003897 clear_tv(sv->sv_tv);
3898 *sv->sv_tv = *STACK_TV_BOT(0);
3899 }
3900 break;
3901
3902 // store option
3903 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003904 case ISN_STOREFUNCOPT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003905 {
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003906 char_u *opt_name = iptr->isn_arg.storeopt.so_name;
3907 int opt_flags = iptr->isn_arg.storeopt.so_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003908 long n = 0;
3909 char_u *s = NULL;
3910 char *msg;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003911 char_u numbuf[NUMBUFLEN];
3912 char_u *tofree = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003913
Bram Moolenaar4c137212021-04-19 16:48:48 +02003914 --ectx->ec_stack.ga_len;
LemonBoy32f34612023-09-02 21:52:05 +02003915 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003916 tv = STACK_TV_BOT(0);
3917 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01003918 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003919 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01003920 if (s == NULL)
3921 s = (char_u *)"";
3922 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003923 else if (iptr->isn_type == ISN_STOREFUNCOPT)
3924 {
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003925 // If the option can be set to a function reference or
3926 // a lambda and the passed value is a function
3927 // reference, then convert it to the name (string) of
3928 // the function reference.
3929 s = tv2string(tv, &tofree, numbuf, 0);
3930 if (s == NULL || *s == NUL)
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003931 {
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003932 // cannot happen?
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003933 clear_tv(tv);
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003934 vim_free(tofree);
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003935 goto on_error;
3936 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003937 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003938 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003939 // must be VAR_NUMBER, CHECKTYPE makes sure
3940 n = tv->vval.v_number;
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003941 msg = set_option_value(opt_name, n, s, opt_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02003942 clear_tv(tv);
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003943 vim_free(tofree);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003944 if (msg != NULL)
3945 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003946 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003947 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02003948 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003949 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003950 }
3951 break;
3952
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003953 // store $ENV
3954 case ISN_STOREENV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003955 --ectx->ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003956 tv = STACK_TV_BOT(0);
3957 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
3958 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003959 break;
3960
3961 // store @r
3962 case ISN_STOREREG:
3963 {
3964 int reg = iptr->isn_arg.number;
3965
Bram Moolenaar4c137212021-04-19 16:48:48 +02003966 --ectx->ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01003967 tv = STACK_TV_BOT(0);
Bram Moolenaar74f4a962021-06-17 21:03:07 +02003968 write_reg_contents(reg, tv_get_string(tv), -1, FALSE);
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01003969 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003970 }
3971 break;
3972
3973 // store v: variable
3974 case ISN_STOREV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003975 --ectx->ec_stack.ga_len;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003976 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
3977 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003978 // should not happen, type is checked when compiling
3979 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003980 break;
3981
Bram Moolenaard3aac292020-04-19 14:32:17 +02003982 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003983 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02003984 case ISN_STOREB:
3985 case ISN_STOREW:
3986 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003987 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003988 dictitem_T *di;
3989 hashtab_T *ht;
3990 char_u *name = iptr->isn_arg.string + 2;
3991
Bram Moolenaard3aac292020-04-19 14:32:17 +02003992 switch (iptr->isn_type)
3993 {
3994 case ISN_STOREG:
3995 ht = get_globvar_ht();
3996 break;
3997 case ISN_STOREB:
3998 ht = &curbuf->b_vars->dv_hashtab;
3999 break;
4000 case ISN_STOREW:
4001 ht = &curwin->w_vars->dv_hashtab;
4002 break;
4003 case ISN_STORET:
4004 ht = &curtab->tp_vars->dv_hashtab;
4005 break;
4006 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004007 goto theend;
Bram Moolenaard3aac292020-04-19 14:32:17 +02004008 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004009
Bram Moolenaar4c137212021-04-19 16:48:48 +02004010 --ectx->ec_stack.ga_len;
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01004011 di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004012 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01004013 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004014 else
4015 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01004016 SOURCING_LNUM = iptr->isn_lnum;
4017 if (var_check_permission(di, name) == FAIL)
4018 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004019 clear_tv(&di->di_tv);
4020 di->di_tv = *STACK_TV_BOT(0);
4021 }
4022 }
4023 break;
4024
Bram Moolenaar03290b82020-12-19 16:30:44 +01004025 // store an autoload variable
4026 case ISN_STOREAUTO:
4027 SOURCING_LNUM = iptr->isn_lnum;
4028 set_var(iptr->isn_arg.string, STACK_TV_BOT(-1), TRUE);
4029 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02004030 --ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01004031 break;
4032
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004033 // store number in local variable
4034 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01004035 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004036 clear_tv(tv);
4037 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01004038 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004039 break;
4040
Bram Moolenaar590162c2022-12-24 21:24:06 +00004041 // Store a value in a list, dict, blob or object variable.
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01004042 case ISN_STOREINDEX:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004043 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00004044 int res = execute_storeindex(iptr, ectx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004045
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00004046 if (res == FAIL)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004047 goto on_error;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00004048 if (res == NOTDONE)
4049 goto theend;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004050 }
4051 break;
4052
Bram Moolenaarea5c8982022-02-17 14:42:02 +00004053 // store value in list or blob range
Bram Moolenaar68452172021-04-12 21:21:02 +02004054 case ISN_STORERANGE:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00004055 if (execute_storerange(iptr, ectx) == FAIL)
4056 goto on_error;
Bram Moolenaar68452172021-04-12 21:21:02 +02004057 break;
4058
Bram Moolenaard505d172022-12-18 21:42:55 +00004059 case ISN_LOAD_CLASSMEMBER:
4060 {
4061 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
4062 goto theend;
4063 classmember_T *cm = &iptr->isn_arg.classmember;
Bram Moolenaar4e2406c2023-06-24 19:22:21 +01004064 copy_tv(cm->cm_class->class_members_tv + cm->cm_idx,
4065 STACK_TV_BOT(0));
Bram Moolenaard505d172022-12-18 21:42:55 +00004066 ++ectx->ec_stack.ga_len;
4067 }
4068 break;
4069
4070 case ISN_STORE_CLASSMEMBER:
4071 {
4072 classmember_T *cm = &iptr->isn_arg.classmember;
4073 tv = &cm->cm_class->class_members_tv[cm->cm_idx];
4074 clear_tv(tv);
4075 *tv = *STACK_TV_BOT(-1);
4076 --ectx->ec_stack.ga_len;
4077 }
4078 break;
4079
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01004080 // Load or store variable or argument from outer scope.
Bram Moolenaar0186e582021-01-10 18:33:11 +01004081 case ISN_LOADOUTER:
4082 case ISN_STOREOUTER:
4083 {
4084 int depth = iptr->isn_arg.outer.outer_depth;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004085 outer_T *outer = ectx->ec_outer_ref == NULL ? NULL
4086 : ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +01004087
4088 while (depth > 1 && outer != NULL)
4089 {
4090 outer = outer->out_up;
4091 --depth;
4092 }
4093 if (outer == NULL)
4094 {
4095 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar69c76172021-12-02 16:38:52 +00004096 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx
4097 || ectx->ec_outer_ref == NULL)
4098 // Possibly :def function called from legacy
4099 // context.
4100 emsg(_(e_closure_called_from_invalid_context));
4101 else
4102 iemsg("LOADOUTER depth more than scope levels");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004103 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01004104 }
Bram Moolenaarcc341812022-09-19 15:54:34 +01004105 if (depth < 0)
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01004106 // Variable declared in loop. May be copied if the
4107 // loop block has already ended.
Bram Moolenaarcc341812022-09-19 15:54:34 +01004108 tv = ((typval_T *)outer->out_loop[-depth - 1]
4109 .stack->ga_data)
4110 + outer->out_loop[-depth - 1].var_idx
4111 + iptr->isn_arg.outer.outer_idx;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004112 else
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01004113 // Variable declared in a function. May be copied if
4114 // the function has already returned.
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004115 tv = ((typval_T *)outer->out_stack->ga_data)
4116 + outer->out_frame_idx + STACK_FRAME_SIZE
4117 + iptr->isn_arg.outer.outer_idx;
Bram Moolenaar0186e582021-01-10 18:33:11 +01004118 if (iptr->isn_type == ISN_LOADOUTER)
4119 {
Bram Moolenaar35578162021-08-02 19:10:38 +02004120 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004121 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01004122 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02004123 ++ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01004124 }
4125 else
4126 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004127 --ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01004128 clear_tv(tv);
4129 *tv = *STACK_TV_BOT(0);
4130 }
4131 }
4132 break;
4133
Bram Moolenaar752fc692021-01-04 21:57:11 +01004134 // unlet item in list or dict variable
4135 case ISN_UNLETINDEX:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00004136 if (execute_unletindex(iptr, ectx) == FAIL)
4137 goto on_error;
Bram Moolenaar752fc692021-01-04 21:57:11 +01004138 break;
4139
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01004140 // unlet range of items in list variable
4141 case ISN_UNLETRANGE:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00004142 if (execute_unletrange(iptr, ectx) == FAIL)
4143 goto on_error;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01004144 break;
4145
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004146 // push constant
4147 case ISN_PUSHNR:
4148 case ISN_PUSHBOOL:
4149 case ISN_PUSHSPEC:
4150 case ISN_PUSHF:
4151 case ISN_PUSHS:
4152 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004153 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004154 case ISN_PUSHCHANNEL:
4155 case ISN_PUSHJOB:
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00004156 case ISN_PUSHOBJ:
4157 case ISN_PUSHCLASS:
Bram Moolenaar35578162021-08-02 19:10:38 +02004158 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004159 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004160 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02004161 tv->v_lock = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004162 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004163 switch (iptr->isn_type)
4164 {
4165 case ISN_PUSHNR:
4166 tv->v_type = VAR_NUMBER;
4167 tv->vval.v_number = iptr->isn_arg.number;
4168 break;
4169 case ISN_PUSHBOOL:
4170 tv->v_type = VAR_BOOL;
4171 tv->vval.v_number = iptr->isn_arg.number;
4172 break;
4173 case ISN_PUSHSPEC:
4174 tv->v_type = VAR_SPECIAL;
4175 tv->vval.v_number = iptr->isn_arg.number;
4176 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004177 case ISN_PUSHF:
4178 tv->v_type = VAR_FLOAT;
4179 tv->vval.v_float = iptr->isn_arg.fnumber;
4180 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004181 case ISN_PUSHBLOB:
4182 blob_copy(iptr->isn_arg.blob, tv);
4183 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004184 case ISN_PUSHFUNC:
4185 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01004186 if (iptr->isn_arg.string == NULL)
4187 tv->vval.v_string = NULL;
4188 else
4189 tv->vval.v_string =
4190 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004191 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004192 case ISN_PUSHCHANNEL:
4193#ifdef FEAT_JOB_CHANNEL
4194 tv->v_type = VAR_CHANNEL;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00004195 tv->vval.v_channel = NULL;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004196#endif
4197 break;
4198 case ISN_PUSHJOB:
4199#ifdef FEAT_JOB_CHANNEL
4200 tv->v_type = VAR_JOB;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00004201 tv->vval.v_job = NULL;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004202#endif
4203 break;
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00004204 case ISN_PUSHOBJ:
4205 tv->v_type = VAR_OBJECT;
4206 tv->vval.v_object = NULL;
4207 break;
4208 case ISN_PUSHCLASS:
4209 tv->v_type = VAR_CLASS;
Bram Moolenaar30a84472023-02-27 08:07:14 +00004210 tv->vval.v_class = iptr->isn_arg.classarg;
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00004211 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004212 default:
4213 tv->v_type = VAR_STRING;
Bram Moolenaarf8691002022-03-10 12:20:53 +00004214 tv->vval.v_string = iptr->isn_arg.string == NULL
4215 ? NULL : vim_strsave(iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004216 }
4217 break;
4218
Bram Moolenaar06b77222022-01-25 15:51:56 +00004219 case ISN_AUTOLOAD:
4220 {
4221 char_u *name = iptr->isn_arg.string;
4222
4223 (void)script_autoload(name, FALSE);
4224 if (find_func(name, TRUE))
4225 {
4226 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
4227 goto theend;
4228 tv = STACK_TV_BOT(0);
4229 tv->v_lock = 0;
4230 ++ectx->ec_stack.ga_len;
4231 tv->v_type = VAR_FUNC;
4232 tv->vval.v_string = vim_strsave(name);
4233 }
4234 else
4235 {
4236 int res = load_namespace_var(ectx, ISN_LOADG, iptr);
4237
4238 if (res == NOTDONE)
4239 goto theend;
4240 if (res == FAIL)
4241 goto on_error;
4242 }
4243 }
4244 break;
4245
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004246 case ISN_UNLET:
4247 if (do_unlet(iptr->isn_arg.unlet.ul_name,
4248 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02004249 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004250 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02004251 case ISN_UNLETENV:
LemonBoy77142312022-04-15 20:50:46 +01004252 vim_unsetenv_ext(iptr->isn_arg.unlet.ul_name);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02004253 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004254
Bram Moolenaaraacc9662021-08-13 19:40:51 +02004255 case ISN_LOCKUNLOCK:
4256 {
Ernie Raelee865f32023-09-29 19:53:55 +02004257#ifdef LOG_LOCKVAR
4258 ch_log(NULL, "LKVAR: execute INS_LOCKUNLOCK isn_arg %s",
4259 iptr->isn_arg.string);
4260#endif
Ernie Rael4c8da022023-10-11 21:35:11 +02004261 lval_root_T *lval_root_save = lval_root;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02004262
4263 // Stack has the local variable, argument the whole :lock
4264 // or :unlock command, like ISN_EXEC.
4265 --ectx->ec_stack.ga_len;
Ernie Rael4c8da022023-10-11 21:35:11 +02004266 lval_root_T root = { .lr_tv = STACK_TV_BOT(0),
4267 .lr_cl_exec = iptr->isn_arg.lockunlock.lu_cl_exec,
4268 .lr_is_arg = iptr->isn_arg.lockunlock.lu_is_arg };
Ernie Rael64885642023-10-04 20:16:22 +02004269 lval_root = &root;
Ernie Rael4c8da022023-10-11 21:35:11 +02004270 int res = exec_command(iptr,
Ernie Rael64885642023-10-04 20:16:22 +02004271 iptr->isn_arg.lockunlock.lu_string);
4272 clear_tv(root.lr_tv);
Bram Moolenaaraacc9662021-08-13 19:40:51 +02004273 lval_root = lval_root_save;
4274 if (res == FAIL)
4275 goto on_error;
4276 }
4277 break;
4278
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02004279 case ISN_LOCKCONST:
4280 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
4281 break;
4282
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004283 // create a list from items on the stack; uses a single allocation
4284 // for the list header and the items
4285 case ISN_NEWLIST:
Bram Moolenaar4c137212021-04-19 16:48:48 +02004286 if (exe_newlist(iptr->isn_arg.number, ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004287 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004288 break;
4289
4290 // create a dict from items on the stack
4291 case ISN_NEWDICT:
4292 {
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01004293 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004294
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01004295 SOURCING_LNUM = iptr->isn_lnum;
4296 res = exe_newdict(iptr->isn_arg.number, ectx);
4297 if (res == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004298 goto theend;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01004299 if (res == MAYBE)
4300 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004301 }
4302 break;
4303
LemonBoy372bcce2022-04-25 12:43:20 +01004304 case ISN_CONCAT:
4305 if (exe_concat(iptr->isn_arg.number, ectx) == FAIL)
4306 goto theend;
4307 break;
4308
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004309 // create a partial with NULL value
4310 case ISN_NEWPARTIAL:
4311 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
4312 goto theend;
4313 ++ectx->ec_stack.ga_len;
4314 tv = STACK_TV_BOT(-1);
4315 tv->v_type = VAR_PARTIAL;
4316 tv->v_lock = 0;
4317 tv->vval.v_partial = NULL;
4318 break;
4319
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004320 // call a :def function
4321 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02004322 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01004323 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
4324 NULL,
4325 iptr->isn_arg.dfunc.cdf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02004326 ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02004327 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004328 break;
4329
Bram Moolenaard0200c82023-01-28 15:19:40 +00004330 // call a method on an interface
4331 case ISN_METHODCALL:
4332 {
Bram Moolenaar094cf9f2023-02-10 15:52:25 +00004333 cmfunc_T *mfunc = iptr->isn_arg.mfunc;
4334
Bram Moolenaard0200c82023-01-28 15:19:40 +00004335 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar094cf9f2023-02-10 15:52:25 +00004336 tv = STACK_TV_BOT(-1 - mfunc->cmf_argcount);
Bram Moolenaard0200c82023-01-28 15:19:40 +00004337 if (tv->v_type != VAR_OBJECT)
4338 {
4339 object_required_error(tv);
4340 goto on_error;
4341 }
4342 object_T *obj = tv->vval.v_object;
4343 class_T *cl = obj->obj_class;
4344
4345 // convert the interface index to the object index
Bram Moolenaard0200c82023-01-28 15:19:40 +00004346 int idx = object_index_from_itf_index(mfunc->cmf_itf,
Yegappan Lakshmanan5a05d372023-09-29 19:43:11 +02004347 TRUE, mfunc->cmf_idx, cl);
Bram Moolenaard0200c82023-01-28 15:19:40 +00004348
4349 if (call_ufunc(cl->class_obj_methods[idx], NULL,
4350 mfunc->cmf_argcount, ectx, NULL, NULL) == FAIL)
4351 goto on_error;
4352 }
4353 break;
4354
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004355 // call a builtin function
4356 case ISN_BCALL:
4357 SOURCING_LNUM = iptr->isn_lnum;
4358 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
4359 iptr->isn_arg.bfunc.cbf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02004360 ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004361 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004362 break;
4363
4364 // call a funcref or partial
4365 case ISN_PCALL:
4366 {
4367 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
4368 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02004369 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004370
4371 SOURCING_LNUM = iptr->isn_lnum;
4372 if (pfunc->cpf_top)
4373 {
4374 // funcref is above the arguments
4375 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
4376 }
4377 else
4378 {
4379 // Get the funcref from the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004380 --ectx->ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02004381 partial_tv = *STACK_TV_BOT(0);
4382 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004383 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004384 r = call_partial(tv, pfunc->cpf_argcount, ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02004385 if (tv == &partial_tv)
4386 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004387 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004388 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004389 }
4390 break;
4391
Bram Moolenaarbd5da372020-03-31 23:13:10 +02004392 case ISN_PCALL_END:
4393 // PCALL finished, arguments have been consumed and replaced by
4394 // the return value. Now clear the funcref from the stack,
4395 // and move the return value in its place.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004396 --ectx->ec_stack.ga_len;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02004397 clear_tv(STACK_TV_BOT(-1));
4398 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
4399 break;
4400
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004401 // call a user defined function or funcref/partial
4402 case ISN_UCALL:
4403 {
4404 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
4405
4406 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01004407 if (call_eval_func(cufunc->cuf_name, cufunc->cuf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02004408 ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004409 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004410 }
4411 break;
4412
Bram Moolenaar1d84f762022-09-03 21:35:53 +01004413 // :defer func(arg)
4414 case ISN_DEFER:
Bram Moolenaar806a2732022-09-04 15:40:36 +01004415 if (defer_command(iptr->isn_arg.defer.defer_var_idx,
Bram Moolenaar1d84f762022-09-03 21:35:53 +01004416 iptr->isn_arg.defer.defer_argcount, ectx) == FAIL)
4417 goto on_error;
4418 break;
4419
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004420 // Return from a :def function call without a value.
4421 // Return from a constructor.
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02004422 case ISN_RETURN_VOID:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004423 case ISN_RETURN_OBJECT:
Bram Moolenaar35578162021-08-02 19:10:38 +02004424 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004425 goto theend;
Bram Moolenaar299f3032021-01-08 20:53:09 +01004426 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004427 ++ectx->ec_stack.ga_len;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004428 if (iptr->isn_type == ISN_RETURN_VOID)
4429 {
4430 tv->v_type = VAR_VOID;
4431 tv->vval.v_number = 0;
4432 tv->v_lock = 0;
4433 }
4434 else
4435 {
4436 *tv = *STACK_TV_VAR(0);
4437 ++tv->vval.v_object->obj_refcount;
4438 }
Bram Moolenaar299f3032021-01-08 20:53:09 +01004439 // FALLTHROUGH
4440
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02004441 // return from a :def function call with what is on the stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004442 case ISN_RETURN:
4443 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004444 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01004445 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01004446
4447 if (trystack->ga_len > 0)
4448 trycmd = ((trycmd_T *)trystack->ga_data)
4449 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004450 if (trycmd != NULL
Bram Moolenaar4c137212021-04-19 16:48:48 +02004451 && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004452 {
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01004453 // jump to ":finally" or ":endtry"
4454 if (trycmd->tcd_finally_idx != 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004455 ectx->ec_iidx = trycmd->tcd_finally_idx;
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01004456 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02004457 ectx->ec_iidx = trycmd->tcd_endtry_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004458 trycmd->tcd_return = TRUE;
4459 }
4460 else
Bram Moolenaard032f342020-07-18 18:13:02 +02004461 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004462 }
4463 break;
4464
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004465 // push a partial, a reference to a compiled function
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004466 case ISN_FUNCREF:
4467 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004468 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
4469 ufunc_T *ufunc;
4470 funcref_T *funcref = &iptr->isn_arg.funcref;
4471 funcref_extra_T *extra = funcref->fr_extra;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004472
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004473 if (pt == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004474 goto theend;
Bram Moolenaar35578162021-08-02 19:10:38 +02004475 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02004476 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004477 vim_free(pt);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004478 goto theend;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004479 }
Bram Moolenaar313e4722023-02-08 20:55:27 +00004480 if (extra != NULL && extra->fre_class != NULL)
4481 {
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +02004482 class_T *cl;
4483 if (extra->fre_object_method)
Bram Moolenaar313e4722023-02-08 20:55:27 +00004484 {
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +02004485 tv = STACK_TV_BOT(-1);
4486 if (tv->v_type != VAR_OBJECT)
4487 {
4488 object_required_error(tv);
4489 vim_free(pt);
4490 goto on_error;
4491 }
Bram Moolenaar313e4722023-02-08 20:55:27 +00004492
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +02004493 object_T *obj = tv->vval.v_object;
4494 cl = obj->obj_class;
4495 // drop the value from the stack
4496 clear_tv(tv);
4497 --ectx->ec_stack.ga_len;
4498
4499 pt->pt_obj = obj;
4500 ++obj->obj_refcount;
4501 }
4502 else
4503 cl = extra->fre_class;
4504
4505 if (extra->fre_object_method)
4506 {
4507 // object method
4508 // convert the interface index to the object index
4509 int idx =
4510 object_index_from_itf_index(extra->fre_class,
4511 TRUE, extra->fre_method_idx, cl);
4512 ufunc = cl->class_obj_methods[idx];
4513 }
4514 else
4515 {
4516 // class method
4517 ufunc =
4518 cl->class_class_functions[extra->fre_method_idx];
4519 }
Bram Moolenaar313e4722023-02-08 20:55:27 +00004520 }
4521 else if (extra == NULL || extra->fre_func_name == NULL)
Bram Moolenaar38453522021-11-28 22:00:12 +00004522 {
4523 dfunc_T *pt_dfunc = ((dfunc_T *)def_functions.ga_data)
4524 + funcref->fr_dfunc_idx;
4525
4526 ufunc = pt_dfunc->df_ufunc;
4527 }
4528 else
Bram Moolenaar313e4722023-02-08 20:55:27 +00004529 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004530 ufunc = find_func(extra->fre_func_name, FALSE);
Bram Moolenaar313e4722023-02-08 20:55:27 +00004531 }
Bram Moolenaar56acd1f2022-02-18 13:24:52 +00004532 if (ufunc == NULL)
4533 {
4534 SOURCING_LNUM = iptr->isn_lnum;
4535 iemsg("ufunc unexpectedly NULL for FUNCREF");
4536 goto theend;
4537 }
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004538 if (fill_partial_and_closure(pt, ufunc,
Bram Moolenaarcc341812022-09-19 15:54:34 +01004539 extra == NULL ? NULL : &extra->fre_loopvar_info,
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004540 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004541 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004542 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004543 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004544 tv->vval.v_partial = pt;
4545 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02004546 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004547 }
4548 break;
4549
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004550 // Create a global function from a lambda.
4551 case ISN_NEWFUNC:
4552 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004553 newfuncarg_T *arg = iptr->isn_arg.newfunc.nf_arg;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004554
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004555 if (copy_lambda_to_global_func(arg->nfa_lambda,
Bram Moolenaarcc341812022-09-19 15:54:34 +01004556 arg->nfa_global, &arg->nfa_loopvar_info,
4557 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004558 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004559 }
4560 break;
4561
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004562 // List functions
4563 case ISN_DEF:
4564 if (iptr->isn_arg.string == NULL)
4565 list_functions(NULL);
4566 else
4567 {
Bram Moolenaar14336722022-01-08 16:02:59 +00004568 exarg_T ea;
4569 garray_T lines_to_free;
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004570
4571 CLEAR_FIELD(ea);
4572 ea.cmd = ea.arg = iptr->isn_arg.string;
Bram Moolenaar14336722022-01-08 16:02:59 +00004573 ga_init2(&lines_to_free, sizeof(char_u *), 50);
Bram Moolenaard4a9b7f2023-05-23 14:48:42 +01004574 SOURCING_LNUM = iptr->isn_lnum;
h-eastb895b0f2023-09-24 15:46:31 +02004575 define_function(&ea, NULL, &lines_to_free, 0, NULL, 0);
Bram Moolenaar14336722022-01-08 16:02:59 +00004576 ga_clear_strings(&lines_to_free);
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004577 }
4578 break;
4579
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004580 // jump if a condition is met
4581 case ISN_JUMP:
4582 {
4583 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004584 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004585 int jump = TRUE;
4586
4587 if (when != JUMP_ALWAYS)
4588 {
4589 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004590 if (when == JUMP_IF_COND_FALSE
Bram Moolenaar13106602020-10-04 16:06:05 +02004591 || when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004592 || when == JUMP_IF_COND_TRUE)
4593 {
4594 SOURCING_LNUM = iptr->isn_lnum;
4595 jump = tv_get_bool_chk(tv, &error);
4596 if (error)
4597 goto on_error;
4598 }
4599 else
4600 jump = tv2bool(tv);
Bram Moolenaarf6ced982022-04-28 12:00:49 +01004601 if (when == JUMP_IF_FALSE || when == JUMP_IF_COND_FALSE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004602 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01004603 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004604 {
4605 // drop the value from the stack
4606 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004607 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004608 }
4609 }
4610 if (jump)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004611 ectx->ec_iidx = iptr->isn_arg.jump.jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004612 }
4613 break;
4614
Bram Moolenaarb46c0832022-09-15 17:19:37 +01004615 // "while": jump to end if a condition is false
4616 case ISN_WHILE:
4617 {
4618 int error = FALSE;
4619 int jump = TRUE;
4620
4621 tv = STACK_TV_BOT(-1);
4622 SOURCING_LNUM = iptr->isn_lnum;
4623 jump = !tv_get_bool_chk(tv, &error);
4624 if (error)
4625 goto on_error;
4626 // drop the value from the stack
4627 clear_tv(tv);
4628 --ectx->ec_stack.ga_len;
4629 if (jump)
4630 ectx->ec_iidx = iptr->isn_arg.whileloop.while_end;
4631
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01004632 // Store the current funcref count, may be used by
Bram Moolenaarcc341812022-09-19 15:54:34 +01004633 // ISN_ENDLOOP later
Bram Moolenaarb46c0832022-09-15 17:19:37 +01004634 tv = STACK_TV_VAR(
4635 iptr->isn_arg.whileloop.while_funcref_idx);
4636 tv->vval.v_number = ectx->ec_funcrefs.ga_len;
4637 }
4638 break;
4639
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02004640 // Jump if an argument with a default value was already set and not
4641 // v:none.
4642 case ISN_JUMP_IF_ARG_SET:
Bram Moolenaar65b0d162022-12-13 18:43:22 +00004643 case ISN_JUMP_IF_ARG_NOT_SET:
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02004644 tv = STACK_TV_VAR(iptr->isn_arg.jumparg.jump_arg_off);
Bram Moolenaar65b0d162022-12-13 18:43:22 +00004645 int arg_set = tv->v_type != VAR_UNKNOWN
4646 && !(tv->v_type == VAR_SPECIAL
4647 && tv->vval.v_number == VVAL_NONE);
4648 if (iptr->isn_type == ISN_JUMP_IF_ARG_SET ? arg_set : !arg_set)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004649 ectx->ec_iidx = iptr->isn_arg.jumparg.jump_where;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02004650 break;
4651
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004652 // top of a for loop
4653 case ISN_FOR:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00004654 if (execute_for(iptr, ectx) == FAIL)
4655 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004656 break;
4657
Bram Moolenaarb46c0832022-09-15 17:19:37 +01004658 // end of a for or while loop
4659 case ISN_ENDLOOP:
4660 if (execute_endloop(iptr, ectx) == FAIL)
4661 goto theend;
4662 break;
4663
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004664 // start of ":try" block
4665 case ISN_TRY:
4666 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01004667 trycmd_T *trycmd = NULL;
4668
Bram Moolenaar35578162021-08-02 19:10:38 +02004669 if (GA_GROW_FAILS(&ectx->ec_trystack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004670 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004671 trycmd = ((trycmd_T *)ectx->ec_trystack.ga_data)
4672 + ectx->ec_trystack.ga_len;
4673 ++ectx->ec_trystack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004674 ++trylevel;
Bram Moolenaar8d4be892021-02-13 18:33:02 +01004675 CLEAR_POINTER(trycmd);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004676 trycmd->tcd_frame_idx = ectx->ec_frame_idx;
4677 trycmd->tcd_stack_len = ectx->ec_stack.ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01004678 trycmd->tcd_catch_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00004679 iptr->isn_arg.tryref.try_ref->try_catch;
Bram Moolenaara91a7132021-03-25 21:12:15 +01004680 trycmd->tcd_finally_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00004681 iptr->isn_arg.tryref.try_ref->try_finally;
Bram Moolenaara91a7132021-03-25 21:12:15 +01004682 trycmd->tcd_endtry_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00004683 iptr->isn_arg.tryref.try_ref->try_endtry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004684 }
4685 break;
4686
4687 case ISN_PUSHEXC:
4688 if (current_exception == NULL)
4689 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004690 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004691 iemsg("Evaluating catch while current_exception is NULL");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004692 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004693 }
Bram Moolenaar35578162021-08-02 19:10:38 +02004694 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004695 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004696 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004697 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004698 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02004699 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004700 tv->vval.v_string = vim_strsave(
4701 (char_u *)current_exception->value);
4702 break;
4703
4704 case ISN_CATCH:
4705 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004706 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar06651632022-04-27 17:54:25 +01004707 trycmd_T *trycmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004708
Bram Moolenaar4c137212021-04-19 16:48:48 +02004709 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar06651632022-04-27 17:54:25 +01004710 trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004711 + trystack->ga_len - 1;
Bram Moolenaar06651632022-04-27 17:54:25 +01004712 trycmd->tcd_caught = TRUE;
4713 trycmd->tcd_did_throw = FALSE;
4714
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004715 did_emsg = got_int = did_throw = FALSE;
Bram Moolenaar1430cee2021-01-17 19:20:32 +01004716 force_abort = need_rethrow = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004717 catch_exception(current_exception);
4718 }
4719 break;
4720
Bram Moolenaarc150c092021-02-13 15:02:46 +01004721 case ISN_TRYCONT:
4722 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004723 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004724 trycont_T *trycont = &iptr->isn_arg.trycont;
4725 int i;
4726 trycmd_T *trycmd;
4727 int iidx = trycont->tct_where;
4728
4729 if (trystack->ga_len < trycont->tct_levels)
4730 {
4731 siemsg("TRYCONT: expected %d levels, found %d",
4732 trycont->tct_levels, trystack->ga_len);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004733 goto theend;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004734 }
4735 // Make :endtry jump to any outer try block and the last
4736 // :endtry inside the loop to the loop start.
4737 for (i = trycont->tct_levels; i > 0; --i)
4738 {
4739 trycmd = ((trycmd_T *)trystack->ga_data)
4740 + trystack->ga_len - i;
Bram Moolenaar2e34c342021-03-14 12:13:33 +01004741 // Add one to tcd_cont to be able to jump to
4742 // instruction with index zero.
4743 trycmd->tcd_cont = iidx + 1;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01004744 iidx = trycmd->tcd_finally_idx == 0
4745 ? trycmd->tcd_endtry_idx : trycmd->tcd_finally_idx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004746 }
4747 // jump to :finally or :endtry of current try statement
Bram Moolenaar4c137212021-04-19 16:48:48 +02004748 ectx->ec_iidx = iidx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004749 }
4750 break;
4751
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01004752 case ISN_FINALLY:
4753 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004754 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01004755 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
4756 + trystack->ga_len - 1;
4757
4758 // Reset the index to avoid a return statement jumps here
4759 // again.
4760 trycmd->tcd_finally_idx = 0;
4761 break;
4762 }
4763
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004764 // end of ":try" block
4765 case ISN_ENDTRY:
4766 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004767 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar06651632022-04-27 17:54:25 +01004768 trycmd_T *trycmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004769
Bram Moolenaar06651632022-04-27 17:54:25 +01004770 --trystack->ga_len;
4771 --trylevel;
4772 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len;
4773 if (trycmd->tcd_did_throw)
4774 did_throw = TRUE;
4775 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004776 {
Bram Moolenaar06651632022-04-27 17:54:25 +01004777 // discard the exception
4778 if (caught_stack == current_exception)
4779 caught_stack = caught_stack->caught;
4780 discard_current_exception();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004781 }
Bram Moolenaar06651632022-04-27 17:54:25 +01004782
4783 if (trycmd->tcd_return)
4784 goto func_return;
4785
4786 while (ectx->ec_stack.ga_len > trycmd->tcd_stack_len)
4787 {
4788 --ectx->ec_stack.ga_len;
4789 clear_tv(STACK_TV_BOT(0));
4790 }
4791 if (trycmd->tcd_cont != 0)
4792 // handling :continue: jump to outer try block or
4793 // start of the loop
4794 ectx->ec_iidx = trycmd->tcd_cont - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004795 }
4796 break;
4797
4798 case ISN_THROW:
Bram Moolenaar8f81b222021-01-14 21:47:06 +01004799 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004800 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02004801
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004802 if (trystack->ga_len == 0 && trylevel == 0 && emsg_silent)
4803 {
Bram Moolenaar3ef2e412023-04-30 18:50:48 +01004804 // Throwing an exception while using "silent!" causes
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004805 // the function to abort but not display an error.
4806 tv = STACK_TV_BOT(-1);
4807 clear_tv(tv);
4808 tv->v_type = VAR_NUMBER;
4809 tv->vval.v_number = 0;
4810 goto done;
4811 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004812 --ectx->ec_stack.ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004813 tv = STACK_TV_BOT(0);
4814 if (tv->vval.v_string == NULL
4815 || *skipwhite(tv->vval.v_string) == NUL)
4816 {
4817 vim_free(tv->vval.v_string);
4818 SOURCING_LNUM = iptr->isn_lnum;
4819 emsg(_(e_throw_with_empty_string));
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004820 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004821 }
4822
4823 // Inside a "catch" we need to first discard the caught
4824 // exception.
4825 if (trystack->ga_len > 0)
4826 {
4827 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
4828 + trystack->ga_len - 1;
4829 if (trycmd->tcd_caught && current_exception != NULL)
4830 {
4831 // discard the exception
4832 if (caught_stack == current_exception)
4833 caught_stack = caught_stack->caught;
4834 discard_current_exception();
4835 trycmd->tcd_caught = FALSE;
4836 }
4837 }
4838
Bram Moolenaar90a57162022-02-12 14:23:17 +00004839 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004840 if (throw_exception(tv->vval.v_string, ET_USER, NULL)
4841 == FAIL)
4842 {
4843 vim_free(tv->vval.v_string);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004844 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004845 }
4846 did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004847 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004848 break;
4849
4850 // compare with special values
4851 case ISN_COMPAREBOOL:
4852 case ISN_COMPARESPECIAL:
4853 {
4854 typval_T *tv1 = STACK_TV_BOT(-2);
4855 typval_T *tv2 = STACK_TV_BOT(-1);
4856 varnumber_T arg1 = tv1->vval.v_number;
4857 varnumber_T arg2 = tv2->vval.v_number;
4858 int res;
4859
Bram Moolenaar06651632022-04-27 17:54:25 +01004860 if (iptr->isn_arg.op.op_type == EXPR_EQUAL)
4861 res = arg1 == arg2;
4862 else
4863 res = arg1 != arg2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004864
Bram Moolenaar4c137212021-04-19 16:48:48 +02004865 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004866 tv1->v_type = VAR_BOOL;
4867 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4868 }
4869 break;
4870
Bram Moolenaar7a222242022-03-01 19:23:24 +00004871 case ISN_COMPARENULL:
4872 {
4873 typval_T *tv1 = STACK_TV_BOT(-2);
4874 typval_T *tv2 = STACK_TV_BOT(-1);
4875 int res;
4876
4877 res = typval_compare_null(tv1, tv2);
4878 if (res == MAYBE)
4879 goto on_error;
4880 if (iptr->isn_arg.op.op_type == EXPR_NEQUAL)
4881 res = !res;
4882 clear_tv(tv1);
4883 clear_tv(tv2);
4884 --ectx->ec_stack.ga_len;
4885 tv1->v_type = VAR_BOOL;
4886 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4887 }
4888 break;
4889
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004890 // Operation with two number arguments
4891 case ISN_OPNR:
4892 case ISN_COMPARENR:
4893 {
4894 typval_T *tv1 = STACK_TV_BOT(-2);
4895 typval_T *tv2 = STACK_TV_BOT(-1);
4896 varnumber_T arg1 = tv1->vval.v_number;
4897 varnumber_T arg2 = tv2->vval.v_number;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004898 varnumber_T res = 0;
4899 int div_zero = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004900
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004901 if (iptr->isn_arg.op.op_type == EXPR_LSHIFT
4902 || iptr->isn_arg.op.op_type == EXPR_RSHIFT)
4903 {
4904 if (arg2 < 0)
4905 {
4906 SOURCING_LNUM = iptr->isn_lnum;
dundargocc57b5bc2022-11-02 13:30:51 +00004907 emsg(_(e_bitshift_ops_must_be_positive));
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004908 goto on_error;
4909 }
4910 }
4911
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004912 switch (iptr->isn_arg.op.op_type)
4913 {
4914 case EXPR_MULT: res = arg1 * arg2; break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004915 case EXPR_DIV: if (arg2 == 0)
4916 div_zero = TRUE;
4917 else
4918 res = arg1 / arg2;
4919 break;
4920 case EXPR_REM: if (arg2 == 0)
4921 div_zero = TRUE;
4922 else
4923 res = arg1 % arg2;
4924 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004925 case EXPR_SUB: res = arg1 - arg2; break;
4926 case EXPR_ADD: res = arg1 + arg2; break;
4927
4928 case EXPR_EQUAL: res = arg1 == arg2; break;
4929 case EXPR_NEQUAL: res = arg1 != arg2; break;
4930 case EXPR_GREATER: res = arg1 > arg2; break;
4931 case EXPR_GEQUAL: res = arg1 >= arg2; break;
4932 case EXPR_SMALLER: res = arg1 < arg2; break;
4933 case EXPR_SEQUAL: res = arg1 <= arg2; break;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004934 case EXPR_LSHIFT: if (arg2 > MAX_LSHIFT_BITS)
4935 res = 0;
4936 else
Bram Moolenaar68e64d22022-05-22 22:07:52 +01004937 res = (uvarnumber_T)arg1 << arg2;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004938 break;
4939 case EXPR_RSHIFT: if (arg2 > MAX_LSHIFT_BITS)
4940 res = 0;
4941 else
Bram Moolenaar338bf582022-05-22 20:16:32 +01004942 res = (uvarnumber_T)arg1 >> arg2;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004943 break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004944 default: break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004945 }
4946
Bram Moolenaar4c137212021-04-19 16:48:48 +02004947 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004948 if (iptr->isn_type == ISN_COMPARENR)
4949 {
4950 tv1->v_type = VAR_BOOL;
4951 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4952 }
4953 else
4954 tv1->vval.v_number = res;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004955 if (div_zero)
4956 {
4957 SOURCING_LNUM = iptr->isn_lnum;
4958 emsg(_(e_divide_by_zero));
4959 goto on_error;
4960 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004961 }
4962 break;
4963
4964 // Computation with two float arguments
4965 case ISN_OPFLOAT:
4966 case ISN_COMPAREFLOAT:
4967 {
4968 typval_T *tv1 = STACK_TV_BOT(-2);
4969 typval_T *tv2 = STACK_TV_BOT(-1);
4970 float_T arg1 = tv1->vval.v_float;
4971 float_T arg2 = tv2->vval.v_float;
4972 float_T res = 0;
4973 int cmp = FALSE;
4974
4975 switch (iptr->isn_arg.op.op_type)
4976 {
4977 case EXPR_MULT: res = arg1 * arg2; break;
4978 case EXPR_DIV: res = arg1 / arg2; break;
4979 case EXPR_SUB: res = arg1 - arg2; break;
4980 case EXPR_ADD: res = arg1 + arg2; break;
4981
4982 case EXPR_EQUAL: cmp = arg1 == arg2; break;
4983 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
4984 case EXPR_GREATER: cmp = arg1 > arg2; break;
4985 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
4986 case EXPR_SMALLER: cmp = arg1 < arg2; break;
4987 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
4988 default: cmp = 0; break;
4989 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004990 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004991 if (iptr->isn_type == ISN_COMPAREFLOAT)
4992 {
4993 tv1->v_type = VAR_BOOL;
4994 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
4995 }
4996 else
4997 tv1->vval.v_float = res;
4998 }
4999 break;
5000
5001 case ISN_COMPARELIST:
Bram Moolenaar265f8112021-12-19 12:33:05 +00005002 case ISN_COMPAREDICT:
5003 case ISN_COMPAREFUNC:
5004 case ISN_COMPARESTRING:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005005 case ISN_COMPAREBLOB:
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00005006 case ISN_COMPARECLASS:
5007 case ISN_COMPAREOBJECT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005008 {
5009 typval_T *tv1 = STACK_TV_BOT(-2);
5010 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar265f8112021-12-19 12:33:05 +00005011 exprtype_T exprtype = iptr->isn_arg.op.op_type;
5012 int ic = iptr->isn_arg.op.op_ic;
5013 int res = FALSE;
5014 int status = OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005015
Bram Moolenaar265f8112021-12-19 12:33:05 +00005016 SOURCING_LNUM = iptr->isn_lnum;
5017 if (iptr->isn_type == ISN_COMPARELIST)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005018 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00005019 status = typval_compare_list(tv1, tv2,
5020 exprtype, ic, &res);
5021 }
5022 else if (iptr->isn_type == ISN_COMPAREDICT)
5023 {
5024 status = typval_compare_dict(tv1, tv2,
5025 exprtype, ic, &res);
5026 }
5027 else if (iptr->isn_type == ISN_COMPAREFUNC)
5028 {
5029 status = typval_compare_func(tv1, tv2,
5030 exprtype, ic, &res);
5031 }
5032 else if (iptr->isn_type == ISN_COMPARESTRING)
5033 {
5034 status = typval_compare_string(tv1, tv2,
5035 exprtype, ic, &res);
5036 }
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00005037 else if (iptr->isn_type == ISN_COMPAREBLOB)
Bram Moolenaar265f8112021-12-19 12:33:05 +00005038 {
5039 status = typval_compare_blob(tv1, tv2, exprtype, &res);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005040 }
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00005041 else if (iptr->isn_type == ISN_COMPARECLASS)
5042 {
Bram Moolenaar03ff0c62023-01-02 20:38:01 +00005043 status = typval_compare_class(tv1, tv2,
5044 exprtype, FALSE, &res);
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00005045 }
5046 else // ISN_COMPAREOBJECT
5047 {
5048 status = typval_compare_object(tv1, tv2,
Bram Moolenaar03ff0c62023-01-02 20:38:01 +00005049 exprtype, FALSE, &res);
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00005050 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005051 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005052 clear_tv(tv1);
5053 clear_tv(tv2);
5054 tv1->v_type = VAR_BOOL;
Bram Moolenaar265f8112021-12-19 12:33:05 +00005055 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
5056 if (status == FAIL)
5057 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005058 }
5059 break;
5060
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005061 case ISN_COMPAREANY:
5062 {
5063 typval_T *tv1 = STACK_TV_BOT(-2);
5064 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar657137c2021-01-09 15:45:23 +01005065 exprtype_T exprtype = iptr->isn_arg.op.op_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005066 int ic = iptr->isn_arg.op.op_ic;
Bram Moolenaar265f8112021-12-19 12:33:05 +00005067 int status;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005068
Bram Moolenaareb26f432020-09-14 16:50:05 +02005069 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar265f8112021-12-19 12:33:05 +00005070 status = typval_compare(tv1, tv2, exprtype, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005071 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005072 --ectx->ec_stack.ga_len;
Bram Moolenaar265f8112021-12-19 12:33:05 +00005073 if (status == FAIL)
5074 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005075 }
5076 break;
5077
5078 case ISN_ADDLIST:
5079 case ISN_ADDBLOB:
5080 {
5081 typval_T *tv1 = STACK_TV_BOT(-2);
5082 typval_T *tv2 = STACK_TV_BOT(-1);
5083
Bram Moolenaar1dcae592020-10-19 19:02:42 +02005084 // add two lists or blobs
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005085 if (iptr->isn_type == ISN_ADDLIST)
Bram Moolenaar07802042021-09-09 23:01:14 +02005086 {
5087 if (iptr->isn_arg.op.op_type == EXPR_APPEND
5088 && tv1->vval.v_list != NULL)
5089 list_extend(tv1->vval.v_list, tv2->vval.v_list,
5090 NULL);
5091 else
5092 eval_addlist(tv1, tv2);
5093 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005094 else
5095 eval_addblob(tv1, tv2);
5096 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005097 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005098 }
5099 break;
5100
Bram Moolenaar1dcae592020-10-19 19:02:42 +02005101 case ISN_LISTAPPEND:
5102 {
5103 typval_T *tv1 = STACK_TV_BOT(-2);
5104 typval_T *tv2 = STACK_TV_BOT(-1);
5105 list_T *l = tv1->vval.v_list;
5106
5107 // add an item to a list
Bram Moolenaar1f4a3452022-01-01 18:29:21 +00005108 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02005109 if (l == NULL)
5110 {
Bram Moolenaar1dcae592020-10-19 19:02:42 +02005111 emsg(_(e_cannot_add_to_null_list));
5112 goto on_error;
5113 }
Bram Moolenaar1f4a3452022-01-01 18:29:21 +00005114 if (value_check_lock(l->lv_lock, NULL, FALSE))
5115 goto on_error;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02005116 if (list_append_tv(l, tv2) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005117 goto theend;
Bram Moolenaar955347c2020-10-19 23:01:46 +02005118 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005119 --ectx->ec_stack.ga_len;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02005120 }
5121 break;
5122
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02005123 case ISN_BLOBAPPEND:
5124 {
5125 typval_T *tv1 = STACK_TV_BOT(-2);
5126 typval_T *tv2 = STACK_TV_BOT(-1);
5127 blob_T *b = tv1->vval.v_blob;
5128 int error = FALSE;
5129 varnumber_T n;
5130
5131 // add a number to a blob
5132 if (b == NULL)
5133 {
5134 SOURCING_LNUM = iptr->isn_lnum;
5135 emsg(_(e_cannot_add_to_null_blob));
5136 goto on_error;
5137 }
5138 n = tv_get_number_chk(tv2, &error);
5139 if (error)
5140 goto on_error;
5141 ga_append(&b->bv_ga, (int)n);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005142 --ectx->ec_stack.ga_len;
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02005143 }
5144 break;
5145
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005146 // Computation with two arguments of unknown type
5147 case ISN_OPANY:
5148 {
5149 typval_T *tv1 = STACK_TV_BOT(-2);
5150 typval_T *tv2 = STACK_TV_BOT(-1);
5151 varnumber_T n1, n2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005152 float_T f1 = 0, f2 = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005153 int error = FALSE;
5154
5155 if (iptr->isn_arg.op.op_type == EXPR_ADD)
5156 {
5157 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
5158 {
5159 eval_addlist(tv1, tv2);
5160 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005161 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005162 break;
5163 }
5164 else if (tv1->v_type == VAR_BLOB
5165 && tv2->v_type == VAR_BLOB)
5166 {
5167 eval_addblob(tv1, tv2);
5168 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005169 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005170 break;
5171 }
5172 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005173 if (tv1->v_type == VAR_FLOAT)
5174 {
5175 f1 = tv1->vval.v_float;
5176 n1 = 0;
5177 }
5178 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005179 {
Bram Moolenaarf665e972020-12-05 19:17:16 +01005180 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005181 n1 = tv_get_number_chk(tv1, &error);
5182 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02005183 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005184 if (tv2->v_type == VAR_FLOAT)
5185 f1 = n1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005186 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005187 if (tv2->v_type == VAR_FLOAT)
5188 {
5189 f2 = tv2->vval.v_float;
5190 n2 = 0;
5191 }
5192 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005193 {
5194 n2 = tv_get_number_chk(tv2, &error);
5195 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02005196 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005197 if (tv1->v_type == VAR_FLOAT)
5198 f2 = n2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005199 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005200 // if there is a float on either side the result is a float
5201 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
5202 {
5203 switch (iptr->isn_arg.op.op_type)
5204 {
5205 case EXPR_MULT: f1 = f1 * f2; break;
5206 case EXPR_DIV: f1 = f1 / f2; break;
5207 case EXPR_SUB: f1 = f1 - f2; break;
5208 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005209 default: SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005210 emsg(_(e_cannot_use_percent_with_float));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02005211 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005212 }
5213 clear_tv(tv1);
5214 clear_tv(tv2);
5215 tv1->v_type = VAR_FLOAT;
5216 tv1->vval.v_float = f1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005217 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005218 }
5219 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005220 {
Bram Moolenaarb1f28572021-01-21 13:03:20 +01005221 int failed = FALSE;
5222
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005223 switch (iptr->isn_arg.op.op_type)
5224 {
5225 case EXPR_MULT: n1 = n1 * n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01005226 case EXPR_DIV: n1 = num_divide(n1, n2, &failed);
5227 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01005228 goto on_error;
5229 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005230 case EXPR_SUB: n1 = n1 - n2; break;
5231 case EXPR_ADD: n1 = n1 + n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01005232 default: n1 = num_modulus(n1, n2, &failed);
5233 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01005234 goto on_error;
5235 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005236 }
5237 clear_tv(tv1);
5238 clear_tv(tv2);
5239 tv1->v_type = VAR_NUMBER;
5240 tv1->vval.v_number = n1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005241 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005242 }
5243 }
5244 break;
5245
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005246 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005247 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005248 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005249 int is_slice = iptr->isn_type == ISN_STRSLICE;
5250 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005251 char_u *res;
5252
5253 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005254 // string slice: string is at stack-3, first index at
5255 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005256 if (is_slice)
5257 {
5258 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005259 n1 = tv->vval.v_number;
5260 }
5261
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005262 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005263 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005264
Bram Moolenaar4c137212021-04-19 16:48:48 +02005265 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005266 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005267 if (is_slice)
5268 // Slice: Select the characters from the string
Bram Moolenaar6601b622021-01-13 21:47:15 +01005269 res = string_slice(tv->vval.v_string, n1, n2, FALSE);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005270 else
5271 // Index: The resulting variable is a string of a
Bram Moolenaar0289a092021-03-14 18:40:19 +01005272 // single character (including composing characters).
5273 // If the index is too big or negative the result is
5274 // empty.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005275 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005276 vim_free(tv->vval.v_string);
5277 tv->vval.v_string = res;
5278 }
5279 break;
5280
5281 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02005282 case ISN_LISTSLICE:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02005283 case ISN_BLOBINDEX:
5284 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005285 {
Bram Moolenaarcfc30232021-04-11 20:26:34 +02005286 int is_slice = iptr->isn_type == ISN_LISTSLICE
5287 || iptr->isn_type == ISN_BLOBSLICE;
5288 int is_blob = iptr->isn_type == ISN_BLOBINDEX
5289 || iptr->isn_type == ISN_BLOBSLICE;
Bram Moolenaared591872020-08-15 22:14:53 +02005290 varnumber_T n1, n2;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02005291 typval_T *val_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005292
5293 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02005294 // list slice: list is at stack-3, indexes at stack-2 and
5295 // stack-1
Bram Moolenaarcfc30232021-04-11 20:26:34 +02005296 // Same for blob.
5297 val_tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005298
5299 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02005300 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005301 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02005302
5303 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005304 {
Bram Moolenaared591872020-08-15 22:14:53 +02005305 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02005306 n1 = tv->vval.v_number;
5307 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005308 }
Bram Moolenaared591872020-08-15 22:14:53 +02005309
Bram Moolenaar4c137212021-04-19 16:48:48 +02005310 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02005311 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02005312 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02005313 if (is_blob)
5314 {
5315 if (blob_slice_or_index(val_tv->vval.v_blob, is_slice,
5316 n1, n2, FALSE, tv) == FAIL)
5317 goto on_error;
5318 }
5319 else
5320 {
5321 if (list_slice_or_index(val_tv->vval.v_list, is_slice,
5322 n1, n2, FALSE, tv, TRUE) == FAIL)
5323 goto on_error;
5324 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005325 }
5326 break;
5327
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005328 case ISN_ANYINDEX:
5329 case ISN_ANYSLICE:
5330 {
5331 int is_slice = iptr->isn_type == ISN_ANYSLICE;
5332 typval_T *var1, *var2;
5333 int res;
5334
5335 // index: composite is at stack-2, index at stack-1
5336 // slice: composite is at stack-3, indexes at stack-2 and
5337 // stack-1
5338 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02005339 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005340 if (check_can_index(tv, TRUE, TRUE) == FAIL)
5341 goto on_error;
5342 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
5343 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
Bram Moolenaar6601b622021-01-13 21:47:15 +01005344 res = eval_index_inner(tv, is_slice, var1, var2,
5345 FALSE, NULL, -1, TRUE);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005346 clear_tv(var1);
5347 if (is_slice)
5348 clear_tv(var2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005349 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005350 if (res == FAIL)
5351 goto on_error;
5352 }
5353 break;
5354
Bram Moolenaar9af78762020-06-16 11:34:42 +02005355 case ISN_SLICE:
5356 {
5357 list_T *list;
5358 int count = iptr->isn_arg.number;
5359
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02005360 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02005361 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02005362 list = tv->vval.v_list;
5363
5364 // no error for short list, expect it to be checked earlier
5365 if (list != NULL && list->lv_len >= count)
5366 {
5367 list_T *newlist = list_slice(list,
5368 count, list->lv_len - 1);
5369
5370 if (newlist != NULL)
5371 {
5372 list_unref(list);
5373 tv->vval.v_list = newlist;
5374 ++newlist->lv_refcount;
5375 }
5376 }
5377 }
5378 break;
5379
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005380 case ISN_GETITEM:
5381 {
5382 listitem_T *li;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02005383 getitem_T *gi = &iptr->isn_arg.getitem;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005384
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02005385 // Get list item: list is at stack-1, push item.
5386 // List type and length is checked for when compiling.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02005387 tv = STACK_TV_BOT(-1 - gi->gi_with_op);
5388 li = list_find(tv->vval.v_list, gi->gi_index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005389
Bram Moolenaar35578162021-08-02 19:10:38 +02005390 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005391 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005392 ++ectx->ec_stack.ga_len;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005393 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
Bram Moolenaarf785aa12021-02-11 21:19:34 +01005394
5395 // Useful when used in unpack assignment. Reset at
5396 // ISN_DROP.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02005397 ectx->ec_where.wt_index = gi->gi_index + 1;
LemonBoyc5d27442023-08-19 13:02:35 +02005398 ectx->ec_where.wt_kind = WT_VARIABLE;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005399 }
5400 break;
5401
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005402 case ISN_MEMBER:
5403 {
5404 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005405 char_u *key;
5406 dictitem_T *di;
5407
5408 // dict member: dict is at stack-2, key at stack-1
5409 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02005410 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005411 dict = tv->vval.v_dict;
5412
5413 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02005414 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005415 key = tv->vval.v_string;
Bram Moolenaar086fc9a2020-10-30 18:33:02 +01005416 if (key == NULL)
5417 key = (char_u *)"";
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02005418
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005419 if ((di = dict_find(dict, key, -1)) == NULL)
5420 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005421 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00005422 semsg(_(e_key_not_present_in_dictionary_str), key);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01005423
5424 // If :silent! is used we will continue, make sure the
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005425 // stack contents makes sense and the dict stack is
5426 // updated.
Bram Moolenaar4029cab2020-12-05 18:13:27 +01005427 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005428 --ectx->ec_stack.ga_len;
Bram Moolenaar4029cab2020-12-05 18:13:27 +01005429 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005430 (void) dict_stack_save(tv);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01005431 tv->v_type = VAR_NUMBER;
5432 tv->vval.v_number = 0;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01005433 goto on_fatal_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005434 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005435 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005436 --ectx->ec_stack.ga_len;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005437 // Put the dict used on the dict stack, it might be used by
5438 // a dict function later.
Bram Moolenaar50788ef2020-07-05 16:51:26 +02005439 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005440 if (dict_stack_save(tv) == FAIL)
5441 goto on_fatal_error;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02005442 copy_tv(&di->di_tv, tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005443 }
5444 break;
5445
5446 // dict member with string key
5447 case ISN_STRINGMEMBER:
5448 {
5449 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005450 dictitem_T *di;
5451
5452 tv = STACK_TV_BOT(-1);
5453 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
5454 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005455 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005456 emsg(_(e_dictionary_required));
Bram Moolenaard032f342020-07-18 18:13:02 +02005457 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005458 }
5459 dict = tv->vval.v_dict;
5460
5461 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
5462 == NULL)
5463 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005464 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00005465 semsg(_(e_key_not_present_in_dictionary_str),
Bram Moolenaar381692b2022-02-02 20:01:27 +00005466 iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02005467 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005468 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005469 // Put the dict used on the dict stack, it might be used by
5470 // a dict function later.
5471 if (dict_stack_save(tv) == FAIL)
5472 goto on_fatal_error;
5473
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005474 copy_tv(&di->di_tv, tv);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005475 }
5476 break;
5477
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005478 case ISN_GET_OBJ_MEMBER:
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00005479 case ISN_GET_ITF_MEMBER:
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005480 {
5481 tv = STACK_TV_BOT(-1);
5482 if (tv->v_type != VAR_OBJECT)
5483 {
5484 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaard0200c82023-01-28 15:19:40 +00005485 object_required_error(tv);
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005486 goto on_error;
5487 }
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00005488
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005489 object_T *obj = tv->vval.v_object;
Bram Moolenaarc3f971f2023-03-02 17:38:33 +00005490 if (obj == NULL)
5491 {
5492 SOURCING_LNUM = iptr->isn_lnum;
5493 emsg(_(e_using_null_object));
5494 goto on_error;
5495 }
5496
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00005497 int idx;
5498 if (iptr->isn_type == ISN_GET_OBJ_MEMBER)
Ernie Rael18143d32023-09-04 22:30:41 +02005499 idx = iptr->isn_arg.classmember.cm_idx;
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00005500 else
5501 {
5502 idx = iptr->isn_arg.classmember.cm_idx;
5503 // convert the interface index to the object index
5504 idx = object_index_from_itf_index(
Ernie Rael18143d32023-09-04 22:30:41 +02005505 iptr->isn_arg.classmember.cm_class,
Yegappan Lakshmanan5a05d372023-09-29 19:43:11 +02005506 FALSE, idx, obj->obj_class);
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00005507 }
5508
Ernie Rael18143d32023-09-04 22:30:41 +02005509 // The members are located right after the object struct.
Yegappan Lakshmanan5a05d372023-09-29 19:43:11 +02005510 typval_T *mtv = ((typval_T *)(obj + 1)) + idx;
Bram Moolenaar590162c2022-12-24 21:24:06 +00005511 copy_tv(mtv, tv);
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005512
5513 // Unreference the object after getting the member, it may
5514 // be freed.
5515 object_unref(obj);
5516 }
5517 break;
5518
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005519 case ISN_STORE_THIS:
5520 {
5521 int idx = iptr->isn_arg.number;
5522 object_T *obj = STACK_TV_VAR(0)->vval.v_object;
5523 // the members are located right after the object struct
5524 typval_T *mtv = ((typval_T *)(obj + 1)) + idx;
5525 clear_tv(mtv);
5526 *mtv = *STACK_TV_BOT(-1);
5527 --ectx->ec_stack.ga_len;
5528 }
5529 break;
5530
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005531 case ISN_CLEARDICT:
5532 dict_stack_drop();
5533 break;
5534
5535 case ISN_USEDICT:
5536 {
5537 typval_T *dict_tv = dict_stack_get_tv();
5538
5539 // Turn "dict.Func" into a partial for "Func" bound to
5540 // "dict". Don't do this when "Func" is already a partial
5541 // that was bound explicitly (pt_auto is FALSE).
5542 tv = STACK_TV_BOT(-1);
5543 if (dict_tv != NULL
5544 && dict_tv->v_type == VAR_DICT
5545 && dict_tv->vval.v_dict != NULL
5546 && (tv->v_type == VAR_FUNC
5547 || (tv->v_type == VAR_PARTIAL
5548 && (tv->vval.v_partial->pt_auto
5549 || tv->vval.v_partial->pt_dict == NULL))))
5550 dict_tv->vval.v_dict =
5551 make_partial(dict_tv->vval.v_dict, tv);
5552 dict_stack_drop();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005553 }
5554 break;
5555
5556 case ISN_NEGATENR:
5557 tv = STACK_TV_BOT(-1);
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00005558 // CHECKTYPE should have checked the variable type
Bram Moolenaarc58164c2020-03-29 18:40:30 +02005559 if (tv->v_type == VAR_FLOAT)
5560 tv->vval.v_float = -tv->vval.v_float;
5561 else
Bram Moolenaarc58164c2020-03-29 18:40:30 +02005562 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005563 break;
5564
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005565 case ISN_CHECKTYPE:
5566 {
5567 checktype_T *ct = &iptr->isn_arg.type;
Bram Moolenaarb1040dc2022-05-18 11:00:48 +01005568 int r;
LemonBoyc5d27442023-08-19 13:02:35 +02005569 where_T where = WHERE_INIT;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005570
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01005571 tv = STACK_TV_BOT((int)ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02005572 SOURCING_LNUM = iptr->isn_lnum;
LemonBoyc5d27442023-08-19 13:02:35 +02005573 if (ct->ct_arg_idx > 0)
5574 {
5575 where.wt_index = ct->ct_arg_idx;
5576 where.wt_kind = ct->ct_is_var ? WT_VARIABLE : WT_ARGUMENT;
LemonBoyc5d27442023-08-19 13:02:35 +02005577 }
LemonBoyf244b2f2023-08-19 16:02:04 +02005578 where.wt_func_name = ectx->ec_where.wt_func_name;
LemonBoyc5d27442023-08-19 13:02:35 +02005579 r = check_typval_type(ct->ct_type, tv, where);
Bram Moolenaarb1040dc2022-05-18 11:00:48 +01005580 if (r == FAIL)
5581 goto on_error;
Bram Moolenaar5e654232020-09-16 15:22:00 +02005582
5583 // number 0 is FALSE, number 1 is TRUE
5584 if (tv->v_type == VAR_NUMBER
5585 && ct->ct_type->tt_type == VAR_BOOL
5586 && (tv->vval.v_number == 0
5587 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005588 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02005589 tv->v_type = VAR_BOOL;
5590 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02005591 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005592 }
5593 }
5594 break;
5595
Bram Moolenaar9af78762020-06-16 11:34:42 +02005596 case ISN_CHECKLEN:
5597 {
5598 int min_len = iptr->isn_arg.checklen.cl_min_len;
5599 list_T *list = NULL;
5600
5601 tv = STACK_TV_BOT(-1);
5602 if (tv->v_type == VAR_LIST)
5603 list = tv->vval.v_list;
5604 if (list == NULL || list->lv_len < min_len
5605 || (list->lv_len > min_len
5606 && !iptr->isn_arg.checklen.cl_more_OK))
5607 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005608 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005609 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02005610 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02005611 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02005612 }
5613 }
5614 break;
5615
Bram Moolenaaraa210a32021-01-02 15:41:03 +01005616 case ISN_SETTYPE:
Bram Moolenaar381692b2022-02-02 20:01:27 +00005617 set_tv_type(STACK_TV_BOT(-1), iptr->isn_arg.type.ct_type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01005618 break;
5619
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005620 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005621 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005622 {
5623 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005624 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005625
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005626 if (iptr->isn_type == ISN_2BOOL)
5627 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005628 tv = STACK_TV_BOT(iptr->isn_arg.tobool.offset);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005629 n = tv2bool(tv);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005630 if (iptr->isn_arg.tobool.invert)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005631 n = !n;
5632 }
5633 else
5634 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005635 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005636 SOURCING_LNUM = iptr->isn_lnum;
5637 n = tv_get_bool_chk(tv, &error);
5638 if (error)
5639 goto on_error;
5640 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005641 clear_tv(tv);
5642 tv->v_type = VAR_BOOL;
5643 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
5644 }
5645 break;
5646
5647 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02005648 case ISN_2STRING_ANY:
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01005649 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005650 if (do_2string(STACK_TV_BOT(iptr->isn_arg.tostring.offset),
5651 iptr->isn_type == ISN_2STRING_ANY,
5652 iptr->isn_arg.tostring.tolerant) == FAIL)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01005653 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005654 break;
5655
Bram Moolenaar08597872020-12-10 19:43:40 +01005656 case ISN_RANGE:
5657 {
5658 exarg_T ea;
5659 char *errormsg;
5660
Bram Moolenaarece0b872021-01-08 20:40:45 +01005661 ea.line2 = 0;
Bram Moolenaard1510ee2021-01-04 16:15:58 +01005662 ea.addr_count = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01005663 ea.addr_type = ADDR_LINES;
5664 ea.cmd = iptr->isn_arg.string;
Bram Moolenaarece0b872021-01-08 20:40:45 +01005665 ea.skip = FALSE;
Bram Moolenaar08597872020-12-10 19:43:40 +01005666 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaarece0b872021-01-08 20:40:45 +01005667 goto on_error;
Bram Moolenaara28639e2021-01-19 22:48:09 +01005668
Bram Moolenaar35578162021-08-02 19:10:38 +02005669 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005670 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005671 ++ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01005672 tv = STACK_TV_BOT(-1);
5673 tv->v_type = VAR_NUMBER;
5674 tv->v_lock = 0;
Bram Moolenaar06651632022-04-27 17:54:25 +01005675 tv->vval.v_number = ea.line2;
Bram Moolenaar08597872020-12-10 19:43:40 +01005676 }
5677 break;
5678
Bram Moolenaarc3516f72020-09-08 22:45:35 +02005679 case ISN_PUT:
5680 {
5681 int regname = iptr->isn_arg.put.put_regname;
5682 linenr_T lnum = iptr->isn_arg.put.put_lnum;
5683 char_u *expr = NULL;
5684 int dir = FORWARD;
5685
Bram Moolenaar08597872020-12-10 19:43:40 +01005686 if (lnum < -2)
5687 {
5688 // line number was put on the stack by ISN_RANGE
5689 tv = STACK_TV_BOT(-1);
5690 curwin->w_cursor.lnum = tv->vval.v_number;
5691 if (lnum == LNUM_VARIABLE_RANGE_ABOVE)
5692 dir = BACKWARD;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005693 --ectx->ec_stack.ga_len;
Bram Moolenaar08597872020-12-10 19:43:40 +01005694 }
5695 else if (lnum == -2)
Bram Moolenaarc3516f72020-09-08 22:45:35 +02005696 // :put! above cursor
5697 dir = BACKWARD;
5698 else if (lnum >= 0)
Bram Moolenaar4e713ba2022-02-07 15:31:37 +00005699 {
5700 curwin->w_cursor.lnum = lnum;
5701 if (lnum == 0)
5702 // check_cursor() below will move to line 1
5703 dir = BACKWARD;
5704 }
Bram Moolenaara28639e2021-01-19 22:48:09 +01005705
5706 if (regname == '=')
5707 {
5708 tv = STACK_TV_BOT(-1);
5709 if (tv->v_type == VAR_STRING)
5710 expr = tv->vval.v_string;
5711 else
5712 {
5713 expr = typval2string(tv, TRUE); // allocates value
5714 clear_tv(tv);
5715 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005716 --ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01005717 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02005718 check_cursor();
5719 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
5720 vim_free(expr);
5721 }
5722 break;
5723
Bram Moolenaar02194d22020-10-24 23:08:38 +02005724 case ISN_CMDMOD:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005725 ectx->ec_funclocal.floc_save_cmdmod = cmdmod;
5726 ectx->ec_funclocal.floc_restore_cmdmod = TRUE;
5727 ectx->ec_funclocal.floc_restore_cmdmod_stacklen =
5728 ectx->ec_stack.ga_len;
Bram Moolenaar02194d22020-10-24 23:08:38 +02005729 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
5730 apply_cmdmod(&cmdmod);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02005731 break;
5732
Bram Moolenaar02194d22020-10-24 23:08:38 +02005733 case ISN_CMDMOD_REV:
5734 // filter regprog is owned by the instruction, don't free it
5735 cmdmod.cmod_filter_regmatch.regprog = NULL;
5736 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005737 cmdmod = ectx->ec_funclocal.floc_save_cmdmod;
5738 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02005739 break;
5740
Bram Moolenaar792f7862020-11-23 08:31:18 +01005741 case ISN_UNPACK:
5742 {
5743 int count = iptr->isn_arg.unpack.unp_count;
5744 int semicolon = iptr->isn_arg.unpack.unp_semicolon;
5745 list_T *l;
5746 listitem_T *li;
5747 int i;
5748
5749 // Check there is a valid list to unpack.
5750 tv = STACK_TV_BOT(-1);
5751 if (tv->v_type != VAR_LIST)
5752 {
5753 SOURCING_LNUM = iptr->isn_lnum;
5754 emsg(_(e_for_argument_must_be_sequence_of_lists));
5755 goto on_error;
5756 }
5757 l = tv->vval.v_list;
5758 if (l == NULL
5759 || l->lv_len < (semicolon ? count - 1 : count))
5760 {
5761 SOURCING_LNUM = iptr->isn_lnum;
5762 emsg(_(e_list_value_does_not_have_enough_items));
5763 goto on_error;
5764 }
5765 else if (!semicolon && l->lv_len > count)
5766 {
5767 SOURCING_LNUM = iptr->isn_lnum;
5768 emsg(_(e_list_value_has_more_items_than_targets));
5769 goto on_error;
5770 }
5771
5772 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar35578162021-08-02 19:10:38 +02005773 if (GA_GROW_FAILS(&ectx->ec_stack, count - 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005774 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005775 ectx->ec_stack.ga_len += count - 1;
Bram Moolenaar792f7862020-11-23 08:31:18 +01005776
5777 // Variable after semicolon gets a list with the remaining
5778 // items.
5779 if (semicolon)
5780 {
5781 list_T *rem_list =
5782 list_alloc_with_items(l->lv_len - count + 1);
5783
5784 if (rem_list == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005785 goto theend;
Bram Moolenaar792f7862020-11-23 08:31:18 +01005786 tv = STACK_TV_BOT(-count);
5787 tv->vval.v_list = rem_list;
5788 ++rem_list->lv_refcount;
5789 tv->v_lock = 0;
5790 li = l->lv_first;
5791 for (i = 0; i < count - 1; ++i)
5792 li = li->li_next;
5793 for (i = 0; li != NULL; ++i)
5794 {
Bram Moolenaar61efa162022-03-18 13:10:48 +00005795 typval_T tvcopy;
5796
5797 copy_tv(&li->li_tv, &tvcopy);
5798 list_set_item(rem_list, i, &tvcopy);
Bram Moolenaar792f7862020-11-23 08:31:18 +01005799 li = li->li_next;
5800 }
5801 --count;
5802 }
5803
5804 // Produce the values in reverse order, first item last.
5805 li = l->lv_first;
5806 for (i = 0; i < count; ++i)
5807 {
5808 tv = STACK_TV_BOT(-i - 1);
5809 copy_tv(&li->li_tv, tv);
5810 li = li->li_next;
5811 }
5812
5813 list_unref(l);
5814 }
5815 break;
5816
Bram Moolenaarb2049902021-01-24 12:53:53 +01005817 case ISN_PROF_START:
5818 case ISN_PROF_END:
5819 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01005820#ifdef FEAT_PROFILE
Bram Moolenaarca16c602022-09-06 18:57:08 +01005821 funccall_T cookie;
5822 ufunc_T *cur_ufunc =
Bram Moolenaarb2049902021-01-24 12:53:53 +01005823 (((dfunc_T *)def_functions.ga_data)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02005824 + ectx->ec_dfunc_idx)->df_ufunc;
Bram Moolenaarb2049902021-01-24 12:53:53 +01005825
Bram Moolenaarca16c602022-09-06 18:57:08 +01005826 cookie.fc_func = cur_ufunc;
Bram Moolenaarb2049902021-01-24 12:53:53 +01005827 if (iptr->isn_type == ISN_PROF_START)
5828 {
5829 func_line_start(&cookie, iptr->isn_lnum);
5830 // if we get here the instruction is executed
5831 func_line_exec(&cookie);
5832 }
5833 else
5834 func_line_end(&cookie);
Bram Moolenaarf002a412021-01-24 13:34:18 +01005835#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01005836 }
5837 break;
5838
Bram Moolenaare99d4222021-06-13 14:01:26 +02005839 case ISN_DEBUG:
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02005840 handle_debug(iptr, ectx);
Bram Moolenaare99d4222021-06-13 14:01:26 +02005841 break;
5842
Bram Moolenaar389df252020-07-09 21:20:47 +02005843 case ISN_SHUFFLE:
5844 {
Bram Moolenaar792f7862020-11-23 08:31:18 +01005845 typval_T tmp_tv;
5846 int item = iptr->isn_arg.shuffle.shfl_item;
5847 int up = iptr->isn_arg.shuffle.shfl_up;
Bram Moolenaar389df252020-07-09 21:20:47 +02005848
5849 tmp_tv = *STACK_TV_BOT(-item);
5850 for ( ; up > 0 && item > 1; --up)
5851 {
5852 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
5853 --item;
5854 }
5855 *STACK_TV_BOT(-item) = tmp_tv;
5856 }
5857 break;
5858
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005859 case ISN_DROP:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005860 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005861 clear_tv(STACK_TV_BOT(0));
LemonBoyc5d27442023-08-19 13:02:35 +02005862 ectx->ec_where = (where_T)WHERE_INIT;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005863 break;
5864 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02005865 continue;
5866
Bram Moolenaard032f342020-07-18 18:13:02 +02005867func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02005868 // Restore previous function. If the frame pointer is where we started
5869 // then there is none and we are done.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005870 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02005871 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02005872
Bram Moolenaar4c137212021-04-19 16:48:48 +02005873 if (func_return(ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02005874 // only fails when out of memory
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005875 goto theend;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02005876 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02005877
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02005878on_error:
Bram Moolenaaraf0df472020-12-02 20:51:22 +01005879 // Jump here for an error that does not require aborting execution.
Bram Moolenaar56602ba2020-12-05 21:22:08 +01005880 // If "emsg_silent" is set then ignore the error, unless it was set
5881 // when calling the function.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005882 if (did_emsg_cumul + did_emsg == ectx->ec_did_emsg_before
Bram Moolenaar56602ba2020-12-05 21:22:08 +01005883 && emsg_silent && did_emsg_def == 0)
Bram Moolenaarf9041332021-01-21 19:41:16 +01005884 {
5885 // If a sequence of instructions causes an error while ":silent!"
5886 // was used, restore the stack length and jump ahead to restoring
5887 // the cmdmod.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005888 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaarf9041332021-01-21 19:41:16 +01005889 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005890 while (ectx->ec_stack.ga_len
5891 > ectx->ec_funclocal.floc_restore_cmdmod_stacklen)
Bram Moolenaarf9041332021-01-21 19:41:16 +01005892 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005893 --ectx->ec_stack.ga_len;
Bram Moolenaarf9041332021-01-21 19:41:16 +01005894 clear_tv(STACK_TV_BOT(0));
5895 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005896 while (ectx->ec_instr[ectx->ec_iidx].isn_type != ISN_CMDMOD_REV)
5897 ++ectx->ec_iidx;
Bram Moolenaarf9041332021-01-21 19:41:16 +01005898 }
Bram Moolenaarcd030c42020-10-30 21:49:40 +01005899 continue;
Bram Moolenaarf9041332021-01-21 19:41:16 +01005900 }
Bram Moolenaaraf0df472020-12-02 20:51:22 +01005901on_fatal_error:
5902 // Jump here for an error that messes up the stack.
Bram Moolenaar171fb922020-10-28 16:54:47 +01005903 // If we are not inside a try-catch started here, abort execution.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005904 if (trylevel <= ectx->ec_trylevel_at_start)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005905 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005906 }
5907
5908done:
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005909 ret = OK;
5910theend:
Bram Moolenaar58779852022-09-06 18:31:14 +01005911 may_invoke_defer_funcs(ectx);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01005912
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005913 dict_stack_clear(dict_stack_len_at_start);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005914 ectx->ec_trylevel_at_start = save_trylevel_at_start;
5915 return ret;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005916}
5917
5918/*
Bram Moolenaar806a2732022-09-04 15:40:36 +01005919 * Execute the instructions from a VAR_INSTR typval and put the result in
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005920 * "rettv".
5921 * Return OK or FAIL.
5922 */
5923 int
5924exe_typval_instr(typval_T *tv, typval_T *rettv)
5925{
5926 ectx_T *ectx = tv->vval.v_instr->instr_ectx;
5927 isn_T *save_instr = ectx->ec_instr;
5928 int save_iidx = ectx->ec_iidx;
5929 int res;
5930
LemonBoyf3b48952022-05-05 13:53:03 +01005931 // Initialize rettv so that it is safe for caller to invoke clear_tv(rettv)
5932 // even when the compilation fails.
5933 rettv->v_type = VAR_UNKNOWN;
5934
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005935 ectx->ec_instr = tv->vval.v_instr->instr_instr;
5936 res = exec_instructions(ectx);
5937 if (res == OK)
5938 {
5939 *rettv = *STACK_TV_BOT(-1);
5940 --ectx->ec_stack.ga_len;
5941 }
5942
5943 ectx->ec_instr = save_instr;
5944 ectx->ec_iidx = save_iidx;
5945
5946 return res;
5947}
5948
5949/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02005950 * Execute the instructions from an ISN_SUBSTITUTE command, which are in
5951 * "substitute_instr".
5952 */
5953 char_u *
5954exe_substitute_instr(void)
5955{
5956 ectx_T *ectx = substitute_instr->subs_ectx;
5957 isn_T *save_instr = ectx->ec_instr;
5958 int save_iidx = ectx->ec_iidx;
5959 char_u *res;
5960
5961 ectx->ec_instr = substitute_instr->subs_instr;
5962 if (exec_instructions(ectx) == OK)
Bram Moolenaar90193e62021-04-04 20:49:50 +02005963 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005964 typval_T *tv = STACK_TV_BOT(-1);
5965
Bram Moolenaar27523602021-06-05 21:36:19 +02005966 res = typval2string(tv, TRUE);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005967 --ectx->ec_stack.ga_len;
5968 clear_tv(tv);
Bram Moolenaar90193e62021-04-04 20:49:50 +02005969 }
5970 else
5971 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005972 substitute_instr->subs_status = FAIL;
5973 res = vim_strsave((char_u *)"");
Bram Moolenaar90193e62021-04-04 20:49:50 +02005974 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005975
Bram Moolenaar4c137212021-04-19 16:48:48 +02005976 ectx->ec_instr = save_instr;
5977 ectx->ec_iidx = save_iidx;
5978
5979 return res;
5980}
5981
5982/*
5983 * Call a "def" function from old Vim script.
5984 * Return OK or FAIL.
5985 */
5986 int
5987call_def_function(
5988 ufunc_T *ufunc,
5989 int argc_arg, // nr of arguments
5990 typval_T *argv, // arguments
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005991 int flags, // DEF_ flags
Bram Moolenaar4c137212021-04-19 16:48:48 +02005992 partial_T *partial, // optional partial for context
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005993 object_T *object, // object, e.g. for this.Func()
Bram Moolenaar58779852022-09-06 18:31:14 +01005994 funccall_T *funccal,
Bram Moolenaar4c137212021-04-19 16:48:48 +02005995 typval_T *rettv) // return value
5996{
5997 ectx_T ectx; // execution context
5998 int argc = argc_arg;
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005999 int partial_argc = partial == NULL
6000 || (flags & DEF_USE_PT_ARGV) == 0
6001 ? 0 : partial->pt_argc;
6002 int total_argc = argc + partial_argc;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006003 typval_T *tv;
6004 int idx;
6005 int ret = FAIL;
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01006006 int defcount = ufunc->uf_args.ga_len - total_argc;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006007 sctx_T save_current_sctx = current_sctx;
6008 int did_emsg_before = did_emsg_cumul + did_emsg;
6009 int save_suppress_errthrow = suppress_errthrow;
6010 msglist_T **saved_msg_list = NULL;
6011 msglist_T *private_msg_list = NULL;
6012 int save_emsg_silent_def = emsg_silent_def;
6013 int save_did_emsg_def = did_emsg_def;
6014 int orig_funcdepth;
Bram Moolenaare99d4222021-06-13 14:01:26 +02006015 int orig_nesting_level = ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006016
6017// Get pointer to item in the stack.
6018#undef STACK_TV
6019#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
6020
6021// Get pointer to item at the bottom of the stack, -1 is the bottom.
6022#undef STACK_TV_BOT
6023#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
6024
6025// Get pointer to a local variable on the stack. Negative for arguments.
6026#undef STACK_TV_VAR
6027#define STACK_TV_VAR(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_frame_idx + STACK_FRAME_SIZE + idx)
6028
6029 if (ufunc->uf_def_status == UF_NOT_COMPILED
6030 || ufunc->uf_def_status == UF_COMPILE_ERROR
Bram Moolenaar139575d2022-03-15 19:29:30 +00006031 || (func_needs_compiling(ufunc, get_compile_type(ufunc))
6032 && compile_def_function(ufunc, FALSE,
6033 get_compile_type(ufunc), NULL) == FAIL))
Bram Moolenaar4c137212021-04-19 16:48:48 +02006034 {
6035 if (did_emsg_cumul + did_emsg == did_emsg_before)
6036 semsg(_(e_function_is_not_compiled_str),
6037 printable_func_name(ufunc));
6038 return FAIL;
6039 }
6040
6041 {
6042 // Check the function was really compiled.
6043 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6044 + ufunc->uf_dfunc_idx;
Bram Moolenaarcc341812022-09-19 15:54:34 +01006045 if (dfunc->df_ufunc == NULL)
6046 {
6047 semsg(_(e_function_was_deleted_str), printable_func_name(ufunc));
6048 return FAIL;
6049 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006050 if (INSTRUCTIONS(dfunc) == NULL)
6051 {
6052 iemsg("using call_def_function() on not compiled function");
6053 return FAIL;
6054 }
6055 }
6056
6057 // If depth of calling is getting too high, don't execute the function.
6058 orig_funcdepth = funcdepth_get();
6059 if (funcdepth_increment() == FAIL)
6060 return FAIL;
6061
6062 CLEAR_FIELD(ectx);
6063 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
6064 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
Bram Moolenaar35578162021-08-02 19:10:38 +02006065 if (GA_GROW_FAILS(&ectx.ec_stack, 20))
Bram Moolenaar4c137212021-04-19 16:48:48 +02006066 {
6067 funcdepth_decrement();
6068 return FAIL;
6069 }
6070 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
6071 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
6072 ectx.ec_did_emsg_before = did_emsg_before;
Bram Moolenaare99d4222021-06-13 14:01:26 +02006073 ++ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006074
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01006075 idx = total_argc - ufunc->uf_args.ga_len;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006076 if (idx > 0 && ufunc->uf_va_name == NULL)
6077 {
Matvey Tarasovd14bb1a2022-06-29 13:18:27 +01006078 semsg(NGETTEXT(e_one_argument_too_many, e_nr_arguments_too_many,
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01006079 idx), idx);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006080 goto failed_early;
6081 }
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01006082 idx = total_argc - ufunc->uf_args.ga_len + ufunc->uf_def_args.ga_len;
Bram Moolenaarc6d71532021-06-05 18:49:38 +02006083 if (idx < 0)
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02006084 {
Matvey Tarasovd14bb1a2022-06-29 13:18:27 +01006085 semsg(NGETTEXT(e_one_argument_too_few, e_nr_arguments_too_few,
Bram Moolenaar98aff652022-09-06 21:02:35 +01006086 -idx), -idx);
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02006087 goto failed_early;
6088 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006089
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01006090 // Put values from the partial and arguments on the stack, but no more than
6091 // what the function expects. A lambda can be called with more arguments
6092 // than it uses.
6093 for (idx = 0; idx < total_argc
Bram Moolenaar4c137212021-04-19 16:48:48 +02006094 && (ufunc->uf_va_name != NULL || idx < ufunc->uf_args.ga_len);
6095 ++idx)
6096 {
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01006097 int argv_idx = idx - partial_argc;
6098
6099 tv = idx < partial_argc ? partial->pt_argv + idx : argv + argv_idx;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006100 if (idx >= ufunc->uf_args.ga_len - ufunc->uf_def_args.ga_len
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01006101 && tv->v_type == VAR_SPECIAL
6102 && tv->vval.v_number == VVAL_NONE)
Bram Moolenaar4c137212021-04-19 16:48:48 +02006103 {
6104 // Use the default value.
6105 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
6106 }
6107 else
6108 {
Bram Moolenaar47bba532023-01-20 18:49:46 +00006109 int done = FALSE;
6110 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len)
6111 {
6112 type_T *expected = ufunc->uf_arg_types[idx];
6113 if (expected->tt_type == VAR_FLOAT && tv->v_type == VAR_NUMBER)
6114 {
6115 // When a float is expected and a number was given, convert
6116 // the value.
6117 STACK_TV_BOT(0)->v_type = VAR_FLOAT;
6118 STACK_TV_BOT(0)->v_lock = 0;
6119 STACK_TV_BOT(0)->vval.v_float = tv->vval.v_number;
6120 done = TRUE;
6121 }
6122 else if (check_typval_arg_type(expected, tv,
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01006123 NULL, argv_idx + 1) == FAIL)
Bram Moolenaar47bba532023-01-20 18:49:46 +00006124 goto failed_early;
6125 }
6126 if (!done)
6127 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006128 }
6129 ++ectx.ec_stack.ga_len;
6130 }
6131
6132 // Turn varargs into a list. Empty list if no args.
6133 if (ufunc->uf_va_name != NULL)
6134 {
6135 int vararg_count = argc - ufunc->uf_args.ga_len;
6136
6137 if (vararg_count < 0)
6138 vararg_count = 0;
6139 else
6140 argc -= vararg_count;
6141 if (exe_newlist(vararg_count, &ectx) == FAIL)
6142 goto failed_early;
6143
6144 // Check the type of the list items.
6145 tv = STACK_TV_BOT(-1);
6146 if (ufunc->uf_va_type != NULL
6147 && ufunc->uf_va_type != &t_list_any
6148 && ufunc->uf_va_type->tt_member != &t_any
6149 && tv->vval.v_list != NULL)
6150 {
6151 type_T *expected = ufunc->uf_va_type->tt_member;
6152 listitem_T *li = tv->vval.v_list->lv_first;
6153
6154 for (idx = 0; idx < vararg_count; ++idx)
6155 {
6156 if (check_typval_arg_type(expected, &li->li_tv,
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02006157 NULL, argc + idx + 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02006158 goto failed_early;
6159 li = li->li_next;
6160 }
6161 }
6162
6163 if (defcount > 0)
6164 // Move varargs list to below missing default arguments.
6165 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
6166 --ectx.ec_stack.ga_len;
6167 }
6168
6169 // Make space for omitted arguments, will store default value below.
6170 // Any varargs list goes after them.
6171 if (defcount > 0)
6172 for (idx = 0; idx < defcount; ++idx)
6173 {
6174 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
6175 ++ectx.ec_stack.ga_len;
6176 }
6177 if (ufunc->uf_va_name != NULL)
6178 ++ectx.ec_stack.ga_len;
6179
6180 // Frame pointer points to just after arguments.
6181 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
6182 ectx.ec_initial_frame_idx = ectx.ec_frame_idx;
6183
6184 {
6185 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6186 + ufunc->uf_dfunc_idx;
6187 ufunc_T *base_ufunc = dfunc->df_ufunc;
6188
6189 // "uf_partial" is on the ufunc that "df_ufunc" points to, as is done
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006190 // by copy_lambda_to_global_func().
Bram Moolenaar4c137212021-04-19 16:48:48 +02006191 if (partial != NULL || base_ufunc->uf_partial != NULL)
6192 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006193 ectx.ec_outer_ref = ALLOC_CLEAR_ONE(outer_ref_T);
6194 if (ectx.ec_outer_ref == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02006195 goto failed_early;
6196 if (partial != NULL)
6197 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00006198 outer_T *outer = get_pt_outer(partial);
6199
Bram Moolenaar6d313be2022-09-22 16:36:25 +01006200 if (outer->out_stack == NULL && outer->out_loop_size == 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02006201 {
Bram Moolenaar6d313be2022-09-22 16:36:25 +01006202 // no stack was set
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00006203 if (current_ectx != NULL)
6204 {
6205 if (current_ectx->ec_outer_ref != NULL
6206 && current_ectx->ec_outer_ref->or_outer != NULL)
6207 ectx.ec_outer_ref->or_outer =
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006208 current_ectx->ec_outer_ref->or_outer;
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00006209 }
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01006210 // else: should there be an error here?
Bram Moolenaar4c137212021-04-19 16:48:48 +02006211 }
6212 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006213 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00006214 ectx.ec_outer_ref->or_outer = outer;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006215 ++partial->pt_refcount;
6216 ectx.ec_outer_ref->or_partial = partial;
6217 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006218 }
6219 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006220 {
6221 ectx.ec_outer_ref->or_outer = &base_ufunc->uf_partial->pt_outer;
6222 ++base_ufunc->uf_partial->pt_refcount;
6223 ectx.ec_outer_ref->or_partial = base_ufunc->uf_partial;
6224 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006225 }
6226 }
6227
6228 // dummy frame entries
6229 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
6230 {
6231 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
6232 ++ectx.ec_stack.ga_len;
6233 }
6234
6235 {
6236 // Reserve space for local variables and any closure reference count.
6237 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6238 + ufunc->uf_dfunc_idx;
6239
Bram Moolenaar5cd64792021-12-25 18:23:24 +00006240 // Initialize variables to zero. That avoids having to generate
6241 // initializing instructions for "var nr: number", "var x: any", etc.
Bram Moolenaar4c137212021-04-19 16:48:48 +02006242 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaar5cd64792021-12-25 18:23:24 +00006243 {
6244 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
6245 STACK_TV_VAR(idx)->vval.v_number = 0;
6246 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006247 ectx.ec_stack.ga_len += dfunc->df_varcount;
Bram Moolenaarffdaca92022-12-09 21:41:48 +00006248
6249 if (object != NULL)
6250 {
6251 // the object is always the variable at index zero
6252 tv = STACK_TV_VAR(0);
6253 tv->v_type = VAR_OBJECT;
6254 tv->vval.v_object = object;
6255 }
6256
Bram Moolenaar4c137212021-04-19 16:48:48 +02006257 if (dfunc->df_has_closure)
6258 {
Bram Moolenaarcc341812022-09-19 15:54:34 +01006259 // Initialize the variable that counts how many closures were
6260 // created. This is used in handle_closure_in_use().
Bram Moolenaar4c137212021-04-19 16:48:48 +02006261 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
6262 STACK_TV_VAR(idx)->vval.v_number = 0;
6263 ++ectx.ec_stack.ga_len;
6264 }
6265
6266 ectx.ec_instr = INSTRUCTIONS(dfunc);
6267 }
6268
Bram Moolenaar58779852022-09-06 18:31:14 +01006269 // Store the execution context in funccal, used by invoke_all_defer().
6270 if (funccal != NULL)
6271 funccal->fc_ectx = &ectx;
6272
Bram Moolenaar4c137212021-04-19 16:48:48 +02006273 // Following errors are in the function, not the caller.
6274 // Commands behave like vim9script.
6275 estack_push_ufunc(ufunc, 1);
6276 current_sctx = ufunc->uf_script_ctx;
6277 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
6278
6279 // Use a specific location for storing error messages to be converted to an
6280 // exception.
6281 saved_msg_list = msg_list;
6282 msg_list = &private_msg_list;
6283
6284 // Do turn errors into exceptions.
6285 suppress_errthrow = FALSE;
6286
6287 // Do not delete the function while executing it.
6288 ++ufunc->uf_calls;
6289
6290 // When ":silent!" was used before calling then we still abort the
6291 // function. If ":silent!" is used in the function then we don't.
6292 emsg_silent_def = emsg_silent;
6293 did_emsg_def = 0;
6294
LemonBoyc5d27442023-08-19 13:02:35 +02006295 ectx.ec_where = (where_T)WHERE_INIT;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006296
Bram Moolenaar5800c792022-09-22 17:34:01 +01006297 /*
6298 * Execute the instructions until done.
6299 */
Bram Moolenaar4c137212021-04-19 16:48:48 +02006300 ret = exec_instructions(&ectx);
6301 if (ret == OK)
6302 {
6303 // function finished, get result from the stack.
6304 if (ufunc->uf_ret_type == &t_void)
6305 {
6306 rettv->v_type = VAR_VOID;
6307 }
6308 else
6309 {
6310 tv = STACK_TV_BOT(-1);
6311 *rettv = *tv;
6312 tv->v_type = VAR_UNKNOWN;
6313 }
6314 }
6315
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01006316 // When failed need to unwind the call stack.
Bram Moolenaar58779852022-09-06 18:31:14 +01006317 unwind_def_callstack(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02006318
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02006319 // Deal with any remaining closures, they may be in use somewhere.
6320 if (ectx.ec_funcrefs.ga_len > 0)
Bram Moolenaarf112f302020-12-20 17:47:52 +01006321 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02006322 handle_closure_in_use(&ectx, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00006323 ga_clear(&ectx.ec_funcrefs);
Bram Moolenaarf112f302020-12-20 17:47:52 +01006324 }
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02006325
Bram Moolenaaree8580e2020-08-28 17:19:07 +02006326 estack_pop();
6327 current_sctx = save_current_sctx;
6328
Bram Moolenaar2336c372021-12-06 15:06:54 +00006329 if (--ufunc->uf_calls <= 0 && ufunc->uf_refcount <= 0)
6330 // Function was unreferenced while being used, free it now.
6331 func_clear_free(ufunc, FALSE);
Bram Moolenaarc970e422021-03-17 15:03:04 +01006332
Bram Moolenaar352134b2020-10-17 22:04:08 +02006333 if (*msg_list != NULL && saved_msg_list != NULL)
6334 {
6335 msglist_T **plist = saved_msg_list;
6336
6337 // Append entries from the current msg_list (uncaught exceptions) to
6338 // the saved msg_list.
6339 while (*plist != NULL)
6340 plist = &(*plist)->next;
6341
6342 *plist = *msg_list;
6343 }
6344 msg_list = saved_msg_list;
6345
Bram Moolenaar4c137212021-04-19 16:48:48 +02006346 if (ectx.ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar02194d22020-10-24 23:08:38 +02006347 {
6348 cmdmod.cmod_filter_regmatch.regprog = NULL;
6349 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006350 cmdmod = ectx.ec_funclocal.floc_save_cmdmod;
Bram Moolenaar02194d22020-10-24 23:08:38 +02006351 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +01006352 emsg_silent_def = save_emsg_silent_def;
6353 did_emsg_def += save_did_emsg_def;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02006354
Bram Moolenaaree8580e2020-08-28 17:19:07 +02006355failed_early:
Bram Moolenaar0d1f55c2022-04-05 17:30:29 +01006356 // Free all arguments and local variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006357 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
Bram Moolenaar1936c762022-09-28 15:19:10 +01006358 {
6359 tv = STACK_TV(idx);
6360 if (tv->v_type != VAR_NUMBER && tv->v_type != VAR_UNKNOWN)
6361 clear_tv(tv);
6362 }
Bram Moolenaare99d4222021-06-13 14:01:26 +02006363 ex_nesting_level = orig_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02006364
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006365 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01006366 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006367 if (ectx.ec_outer_ref != NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +01006368 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006369 if (ectx.ec_outer_ref->or_outer_allocated)
6370 vim_free(ectx.ec_outer_ref->or_outer);
6371 partial_unref(ectx.ec_outer_ref->or_partial);
6372 vim_free(ectx.ec_outer_ref);
Bram Moolenaar0186e582021-01-10 18:33:11 +01006373 }
6374
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02006375 // Not sure if this is necessary.
6376 suppress_errthrow = save_suppress_errthrow;
6377
Bram Moolenaarb5841b92021-07-15 18:09:53 +02006378 if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before
6379 && !need_rethrow)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006380 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02006381 printable_func_name(ufunc));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01006382 funcdepth_restore(orig_funcdepth);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006383 return ret;
6384}
6385
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006386/*
Bram Moolenaar58779852022-09-06 18:31:14 +01006387 * Called when a def function has finished (possibly failed).
6388 * Invoke all the function returns to clean up and invoke deferred functions,
6389 * except the toplevel one.
6390 */
6391 void
6392unwind_def_callstack(ectx_T *ectx)
6393{
6394 while (ectx->ec_frame_idx != ectx->ec_initial_frame_idx)
6395 func_return(ectx);
6396}
6397
6398/*
dundargocc57b5bc2022-11-02 13:30:51 +00006399 * Invoke any deferred functions for the top function in "ectx".
Bram Moolenaar58779852022-09-06 18:31:14 +01006400 */
6401 void
6402may_invoke_defer_funcs(ectx_T *ectx)
6403{
6404 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
6405
6406 if (dfunc->df_defer_var_idx > 0)
6407 invoke_defer_funcs(ectx);
6408}
6409
6410/*
Bram Moolenaarcc341812022-09-19 15:54:34 +01006411 * Return loopvarinfo in a printable form in allocated memory.
6412 */
6413 static char_u *
6414printable_loopvarinfo(loopvarinfo_T *lvi)
6415{
6416 garray_T ga;
6417 int depth;
6418
6419 ga_init2(&ga, 1, 100);
6420 for (depth = 0; depth < lvi->lvi_depth; ++depth)
6421 {
6422 if (ga_grow(&ga, 50) == FAIL)
6423 break;
6424 if (lvi->lvi_loop[depth].var_idx == 0)
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +01006425 STRCPY((char *)ga.ga_data + ga.ga_len, " -");
Bram Moolenaarcc341812022-09-19 15:54:34 +01006426 else
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +01006427 vim_snprintf((char *)ga.ga_data + ga.ga_len, 50, " $%d-$%d",
Bram Moolenaarcc341812022-09-19 15:54:34 +01006428 lvi->lvi_loop[depth].var_idx,
6429 lvi->lvi_loop[depth].var_idx
6430 + lvi->lvi_loop[depth].var_count - 1);
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +01006431 ga.ga_len = (int)STRLEN(ga.ga_data);
Bram Moolenaarcc341812022-09-19 15:54:34 +01006432 }
6433 return ga.ga_data;
6434}
6435
6436/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02006437 * List instructions "instr" up to "instr_count" or until ISN_FINISH.
6438 * "ufunc" has the source lines, NULL for the instructions of ISN_SUBSTITUTE.
6439 * "pfx" is prefixed to every line.
6440 */
6441 static void
6442list_instructions(char *pfx, isn_T *instr, int instr_count, ufunc_T *ufunc)
6443{
6444 int line_idx = 0;
6445 int prev_current = 0;
6446 int current;
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02006447 int def_arg_idx = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006448
6449 for (current = 0; current < instr_count; ++current)
6450 {
6451 isn_T *iptr = &instr[current];
6452 char *line;
6453
6454 if (ufunc != NULL)
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02006455 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02006456 while (line_idx < iptr->isn_lnum
6457 && line_idx < ufunc->uf_lines.ga_len)
6458 {
6459 if (current > prev_current)
6460 {
6461 msg_puts("\n\n");
6462 prev_current = current;
6463 }
6464 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
6465 if (line != NULL)
6466 msg(line);
6467 }
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02006468 if (iptr->isn_type == ISN_JUMP_IF_ARG_SET)
6469 {
6470 int first_def_arg = ufunc->uf_args.ga_len
6471 - ufunc->uf_def_args.ga_len;
6472
6473 if (def_arg_idx > 0)
6474 msg_puts("\n\n");
6475 msg_start();
6476 msg_puts(" ");
6477 msg_puts(((char **)(ufunc->uf_args.ga_data))[
6478 first_def_arg + def_arg_idx]);
6479 msg_puts(" = ");
6480 msg_puts(((char **)(ufunc->uf_def_args.ga_data))[def_arg_idx++]);
6481 msg_clr_eos();
6482 msg_end();
6483 }
6484 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006485
6486 switch (iptr->isn_type)
6487 {
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006488 case ISN_CONSTRUCT:
6489 smsg("%s%4d NEW %s size %d", pfx, current,
6490 iptr->isn_arg.construct.construct_class->class_name,
6491 (int)iptr->isn_arg.construct.construct_size);
6492 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006493 case ISN_EXEC:
6494 smsg("%s%4d EXEC %s", pfx, current, iptr->isn_arg.string);
6495 break;
Bram Moolenaar20677332021-06-06 17:02:53 +02006496 case ISN_EXEC_SPLIT:
6497 smsg("%s%4d EXEC_SPLIT %s", pfx, current, iptr->isn_arg.string);
6498 break;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00006499 case ISN_EXECRANGE:
6500 smsg("%s%4d EXECRANGE %s", pfx, current, iptr->isn_arg.string);
6501 break;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02006502 case ISN_LEGACY_EVAL:
6503 smsg("%s%4d EVAL legacy %s", pfx, current,
6504 iptr->isn_arg.string);
6505 break;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02006506 case ISN_REDIRSTART:
6507 smsg("%s%4d REDIR", pfx, current);
6508 break;
6509 case ISN_REDIREND:
6510 smsg("%s%4d REDIR END%s", pfx, current,
6511 iptr->isn_arg.number ? " append" : "");
6512 break;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006513 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02006514#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006515 smsg("%s%4d CEXPR pre %s", pfx, current,
6516 cexpr_get_auname(iptr->isn_arg.number));
Bram Moolenaarb7c97812021-05-05 22:51:39 +02006517#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006518 break;
6519 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02006520#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006521 {
6522 cexprref_T *cer = iptr->isn_arg.cexpr.cexpr_ref;
6523
6524 smsg("%s%4d CEXPR core %s%s \"%s\"", pfx, current,
6525 cexpr_get_auname(cer->cer_cmdidx),
6526 cer->cer_forceit ? "!" : "",
6527 cer->cer_cmdline);
6528 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02006529#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006530 break;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006531 case ISN_INSTR:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006532 smsg("%s%4d INSTR", pfx, current);
6533 list_instructions(" ", iptr->isn_arg.instr, INT_MAX, NULL);
6534 msg(" -------------");
6535 break;
6536 case ISN_SOURCE:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006537 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006538 scriptitem_T *si = SCRIPT_ITEM(iptr->isn_arg.number);
6539
6540 smsg("%s%4d SOURCE %s", pfx, current, si->sn_name);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006541 }
6542 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006543 case ISN_SUBSTITUTE:
6544 {
6545 subs_T *subs = &iptr->isn_arg.subs;
6546
6547 smsg("%s%4d SUBSTITUTE %s", pfx, current, subs->subs_cmd);
6548 list_instructions(" ", subs->subs_instr, INT_MAX, NULL);
6549 msg(" -------------");
6550 }
6551 break;
6552 case ISN_EXECCONCAT:
6553 smsg("%s%4d EXECCONCAT %lld", pfx, current,
6554 (varnumber_T)iptr->isn_arg.number);
6555 break;
6556 case ISN_ECHO:
6557 {
6558 echo_T *echo = &iptr->isn_arg.echo;
6559
6560 smsg("%s%4d %s %d", pfx, current,
6561 echo->echo_with_white ? "ECHO" : "ECHON",
6562 echo->echo_count);
6563 }
6564 break;
6565 case ISN_EXECUTE:
6566 smsg("%s%4d EXECUTE %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02006567 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006568 break;
6569 case ISN_ECHOMSG:
6570 smsg("%s%4d ECHOMSG %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02006571 (varnumber_T)(iptr->isn_arg.number));
6572 break;
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01006573 case ISN_ECHOWINDOW:
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01006574 if (iptr->isn_arg.echowin.ewin_time > 0)
6575 smsg("%s%4d ECHOWINDOW %d (%ld sec)", pfx, current,
6576 iptr->isn_arg.echowin.ewin_count,
6577 iptr->isn_arg.echowin.ewin_time);
6578 else
6579 smsg("%s%4d ECHOWINDOW %d", pfx, current,
6580 iptr->isn_arg.echowin.ewin_count);
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01006581 break;
Bram Moolenaar7de62622021-08-07 15:05:47 +02006582 case ISN_ECHOCONSOLE:
6583 smsg("%s%4d ECHOCONSOLE %lld", pfx, current,
6584 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006585 break;
6586 case ISN_ECHOERR:
6587 smsg("%s%4d ECHOERR %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02006588 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006589 break;
6590 case ISN_LOAD:
6591 {
6592 if (iptr->isn_arg.number < 0)
6593 smsg("%s%4d LOAD arg[%lld]", pfx, current,
6594 (varnumber_T)(iptr->isn_arg.number
6595 + STACK_FRAME_SIZE));
6596 else
6597 smsg("%s%4d LOAD $%lld", pfx, current,
6598 (varnumber_T)(iptr->isn_arg.number));
6599 }
6600 break;
6601 case ISN_LOADOUTER:
6602 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006603 isn_outer_T *outer = &iptr->isn_arg.outer;
6604
6605 if (outer->outer_idx < 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02006606 smsg("%s%4d LOADOUTER level %d arg[%d]", pfx, current,
Bram Moolenaarcc341812022-09-19 15:54:34 +01006607 outer->outer_depth,
6608 outer->outer_idx + STACK_FRAME_SIZE);
6609 else if (outer->outer_depth < 0)
6610 smsg("%s%4d LOADOUTER $%d in loop level %d",
6611 pfx, current,
6612 outer->outer_idx,
6613 -outer->outer_depth);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006614 else
6615 smsg("%s%4d LOADOUTER level %d $%d", pfx, current,
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006616 outer->outer_depth,
6617 outer->outer_idx);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006618 }
6619 break;
6620 case ISN_LOADV:
6621 smsg("%s%4d LOADV v:%s", pfx, current,
6622 get_vim_var_name(iptr->isn_arg.number));
6623 break;
6624 case ISN_LOADSCRIPT:
6625 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006626 scriptref_T *sref = iptr->isn_arg.script.scriptref;
6627 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
6628 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006629
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006630 sv = get_script_svar(sref, -1);
6631 if (sv == NULL)
6632 smsg("%s%4d LOADSCRIPT [deleted] from %s",
6633 pfx, current, si->sn_name);
6634 else
6635 smsg("%s%4d LOADSCRIPT %s-%d from %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02006636 sv->sv_name,
6637 sref->sref_idx,
6638 si->sn_name);
6639 }
6640 break;
6641 case ISN_LOADS:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006642 case ISN_LOADEXPORT:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006643 {
6644 scriptitem_T *si = SCRIPT_ITEM(
6645 iptr->isn_arg.loadstore.ls_sid);
6646
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006647 smsg("%s%4d %s s:%s from %s", pfx, current,
6648 iptr->isn_type == ISN_LOADS ? "LOADS"
6649 : "LOADEXPORT",
6650 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006651 }
6652 break;
6653 case ISN_LOADAUTO:
6654 smsg("%s%4d LOADAUTO %s", pfx, current, iptr->isn_arg.string);
6655 break;
6656 case ISN_LOADG:
6657 smsg("%s%4d LOADG g:%s", pfx, current, iptr->isn_arg.string);
6658 break;
6659 case ISN_LOADB:
6660 smsg("%s%4d LOADB b:%s", pfx, current, iptr->isn_arg.string);
6661 break;
6662 case ISN_LOADW:
6663 smsg("%s%4d LOADW w:%s", pfx, current, iptr->isn_arg.string);
6664 break;
6665 case ISN_LOADT:
6666 smsg("%s%4d LOADT t:%s", pfx, current, iptr->isn_arg.string);
6667 break;
6668 case ISN_LOADGDICT:
6669 smsg("%s%4d LOAD g:", pfx, current);
6670 break;
6671 case ISN_LOADBDICT:
6672 smsg("%s%4d LOAD b:", pfx, current);
6673 break;
6674 case ISN_LOADWDICT:
6675 smsg("%s%4d LOAD w:", pfx, current);
6676 break;
6677 case ISN_LOADTDICT:
6678 smsg("%s%4d LOAD t:", pfx, current);
6679 break;
6680 case ISN_LOADOPT:
6681 smsg("%s%4d LOADOPT %s", pfx, current, iptr->isn_arg.string);
6682 break;
6683 case ISN_LOADENV:
6684 smsg("%s%4d LOADENV %s", pfx, current, iptr->isn_arg.string);
6685 break;
6686 case ISN_LOADREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006687 smsg("%s%4d LOADREG @%c", pfx, current,
6688 (int)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006689 break;
6690
6691 case ISN_STORE:
6692 if (iptr->isn_arg.number < 0)
6693 smsg("%s%4d STORE arg[%lld]", pfx, current,
6694 iptr->isn_arg.number + STACK_FRAME_SIZE);
6695 else
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006696 smsg("%s%4d STORE $%lld", pfx, current,
6697 iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006698 break;
6699 case ISN_STOREOUTER:
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006700 {
6701 isn_outer_T *outer = &iptr->isn_arg.outer;
6702
6703 if (outer->outer_depth == OUTER_LOOP_DEPTH)
6704 smsg("%s%4d STOREOUTER level 1 $%d in loop",
6705 pfx, current, outer->outer_idx);
6706 else
6707 smsg("%s%4d STOREOUTER level %d $%d", pfx, current,
6708 outer->outer_depth, outer->outer_idx);
6709 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006710 break;
6711 case ISN_STOREV:
6712 smsg("%s%4d STOREV v:%s", pfx, current,
6713 get_vim_var_name(iptr->isn_arg.number));
6714 break;
6715 case ISN_STOREAUTO:
6716 smsg("%s%4d STOREAUTO %s", pfx, current, iptr->isn_arg.string);
6717 break;
6718 case ISN_STOREG:
6719 smsg("%s%4d STOREG %s", pfx, current, iptr->isn_arg.string);
6720 break;
6721 case ISN_STOREB:
6722 smsg("%s%4d STOREB %s", pfx, current, iptr->isn_arg.string);
6723 break;
6724 case ISN_STOREW:
6725 smsg("%s%4d STOREW %s", pfx, current, iptr->isn_arg.string);
6726 break;
6727 case ISN_STORET:
6728 smsg("%s%4d STORET %s", pfx, current, iptr->isn_arg.string);
6729 break;
6730 case ISN_STORES:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006731 case ISN_STOREEXPORT:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006732 {
6733 scriptitem_T *si = SCRIPT_ITEM(
6734 iptr->isn_arg.loadstore.ls_sid);
6735
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006736 smsg("%s%4d %s %s in %s", pfx, current,
6737 iptr->isn_type == ISN_STORES
6738 ? "STORES" : "STOREEXPORT",
Bram Moolenaar4c137212021-04-19 16:48:48 +02006739 iptr->isn_arg.loadstore.ls_name, si->sn_name);
6740 }
6741 break;
6742 case ISN_STORESCRIPT:
6743 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006744 scriptref_T *sref = iptr->isn_arg.script.scriptref;
6745 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
6746 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006747
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006748 sv = get_script_svar(sref, -1);
6749 if (sv == NULL)
6750 smsg("%s%4d STORESCRIPT [deleted] in %s",
6751 pfx, current, si->sn_name);
6752 else
6753 smsg("%s%4d STORESCRIPT %s-%d in %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02006754 sv->sv_name,
6755 sref->sref_idx,
6756 si->sn_name);
6757 }
6758 break;
6759 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00006760 case ISN_STOREFUNCOPT:
6761 smsg("%s%4d %s &%s", pfx, current,
6762 iptr->isn_type == ISN_STOREOPT ? "STOREOPT" : "STOREFUNCOPT",
Bram Moolenaar4c137212021-04-19 16:48:48 +02006763 iptr->isn_arg.storeopt.so_name);
6764 break;
6765 case ISN_STOREENV:
6766 smsg("%s%4d STOREENV $%s", pfx, current, iptr->isn_arg.string);
6767 break;
6768 case ISN_STOREREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006769 smsg("%s%4d STOREREG @%c", pfx, current,
6770 (int)iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006771 break;
6772 case ISN_STORENR:
6773 smsg("%s%4d STORE %lld in $%d", pfx, current,
6774 iptr->isn_arg.storenr.stnr_val,
6775 iptr->isn_arg.storenr.stnr_idx);
6776 break;
6777
6778 case ISN_STOREINDEX:
6779 smsg("%s%4d STOREINDEX %s", pfx, current,
Bram Moolenaarf7d1c6e2023-01-16 20:47:57 +00006780 vartype_name(iptr->isn_arg.storeindex.si_vartype));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006781 break;
6782
6783 case ISN_STORERANGE:
6784 smsg("%s%4d STORERANGE", pfx, current);
6785 break;
6786
Bram Moolenaard505d172022-12-18 21:42:55 +00006787 case ISN_LOAD_CLASSMEMBER:
6788 case ISN_STORE_CLASSMEMBER:
6789 {
6790 class_T *cl = iptr->isn_arg.classmember.cm_class;
6791 int idx = iptr->isn_arg.classmember.cm_idx;
6792 ocmember_T *ocm = &cl->class_class_members[idx];
6793 smsg("%s%4d %s CLASSMEMBER %s.%s", pfx, current,
6794 iptr->isn_type == ISN_LOAD_CLASSMEMBER
6795 ? "LOAD" : "STORE",
6796 cl->class_name, ocm->ocm_name);
6797 }
6798 break;
6799
Bram Moolenaar4c137212021-04-19 16:48:48 +02006800 // constants
6801 case ISN_PUSHNR:
6802 smsg("%s%4d PUSHNR %lld", pfx, current,
6803 (varnumber_T)(iptr->isn_arg.number));
6804 break;
6805 case ISN_PUSHBOOL:
6806 case ISN_PUSHSPEC:
6807 smsg("%s%4d PUSH %s", pfx, current,
6808 get_var_special_name(iptr->isn_arg.number));
6809 break;
6810 case ISN_PUSHF:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006811 smsg("%s%4d PUSHF %g", pfx, current, iptr->isn_arg.fnumber);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006812 break;
6813 case ISN_PUSHS:
6814 smsg("%s%4d PUSHS \"%s\"", pfx, current, iptr->isn_arg.string);
6815 break;
6816 case ISN_PUSHBLOB:
6817 {
6818 char_u *r;
6819 char_u numbuf[NUMBUFLEN];
6820 char_u *tofree;
6821
6822 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
6823 smsg("%s%4d PUSHBLOB %s", pfx, current, r);
6824 vim_free(tofree);
6825 }
6826 break;
6827 case ISN_PUSHFUNC:
6828 {
6829 char *name = (char *)iptr->isn_arg.string;
6830
6831 smsg("%s%4d PUSHFUNC \"%s\"", pfx, current,
6832 name == NULL ? "[none]" : name);
6833 }
6834 break;
6835 case ISN_PUSHCHANNEL:
6836#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar397a87a2022-03-20 21:14:15 +00006837 smsg("%s%4d PUSHCHANNEL 0", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006838#endif
6839 break;
6840 case ISN_PUSHJOB:
6841#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar397a87a2022-03-20 21:14:15 +00006842 smsg("%s%4d PUSHJOB \"no process\"", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006843#endif
6844 break;
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00006845 case ISN_PUSHOBJ:
6846 smsg("%s%4d PUSHOBJ null", pfx, current);
6847 break;
6848 case ISN_PUSHCLASS:
6849 smsg("%s%4d PUSHCLASS %s", pfx, current,
Bram Moolenaar30a84472023-02-27 08:07:14 +00006850 iptr->isn_arg.classarg == NULL ? "null"
6851 : (char *)iptr->isn_arg.classarg->class_name);
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00006852 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006853 case ISN_PUSHEXC:
6854 smsg("%s%4d PUSH v:exception", pfx, current);
6855 break;
Bram Moolenaar06b77222022-01-25 15:51:56 +00006856 case ISN_AUTOLOAD:
6857 smsg("%s%4d AUTOLOAD %s", pfx, current, iptr->isn_arg.string);
6858 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006859 case ISN_UNLET:
6860 smsg("%s%4d UNLET%s %s", pfx, current,
6861 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
6862 iptr->isn_arg.unlet.ul_name);
6863 break;
6864 case ISN_UNLETENV:
6865 smsg("%s%4d UNLETENV%s $%s", pfx, current,
6866 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
6867 iptr->isn_arg.unlet.ul_name);
6868 break;
6869 case ISN_UNLETINDEX:
6870 smsg("%s%4d UNLETINDEX", pfx, current);
6871 break;
6872 case ISN_UNLETRANGE:
6873 smsg("%s%4d UNLETRANGE", pfx, current);
6874 break;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02006875 case ISN_LOCKUNLOCK:
6876 smsg("%s%4d LOCKUNLOCK %s", pfx, current, iptr->isn_arg.string);
6877 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006878 case ISN_LOCKCONST:
6879 smsg("%s%4d LOCKCONST", pfx, current);
6880 break;
6881 case ISN_NEWLIST:
6882 smsg("%s%4d NEWLIST size %lld", pfx, current,
Bram Moolenaar1936c762022-09-28 15:19:10 +01006883 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006884 break;
6885 case ISN_NEWDICT:
6886 smsg("%s%4d NEWDICT size %lld", pfx, current,
Bram Moolenaar1936c762022-09-28 15:19:10 +01006887 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006888 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00006889 case ISN_NEWPARTIAL:
6890 smsg("%s%4d NEWPARTIAL", pfx, current);
6891 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006892
6893 // function call
6894 case ISN_BCALL:
6895 {
6896 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
6897
6898 smsg("%s%4d BCALL %s(argc %d)", pfx, current,
6899 internal_func_name(cbfunc->cbf_idx),
6900 cbfunc->cbf_argcount);
6901 }
6902 break;
6903 case ISN_DCALL:
6904 {
6905 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
6906 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
6907 + cdfunc->cdf_idx;
6908
6909 smsg("%s%4d DCALL %s(argc %d)", pfx, current,
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006910 printable_func_name(df->df_ufunc),
6911 cdfunc->cdf_argcount);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006912 }
6913 break;
Bram Moolenaard0200c82023-01-28 15:19:40 +00006914 case ISN_METHODCALL:
6915 {
6916 cmfunc_T *mfunc = iptr->isn_arg.mfunc;
6917
6918 smsg("%s%4d METHODCALL %s.%s(argc %d)", pfx, current,
6919 mfunc->cmf_itf->class_name,
6920 mfunc->cmf_itf->class_obj_methods[
6921 mfunc->cmf_idx]->uf_name,
6922 mfunc->cmf_argcount);
6923 }
6924 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006925 case ISN_UCALL:
6926 {
6927 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
6928
6929 smsg("%s%4d UCALL %s(argc %d)", pfx, current,
6930 cufunc->cuf_name, cufunc->cuf_argcount);
6931 }
6932 break;
6933 case ISN_PCALL:
6934 {
6935 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
6936
6937 smsg("%s%4d PCALL%s (argc %d)", pfx, current,
6938 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
6939 }
6940 break;
6941 case ISN_PCALL_END:
6942 smsg("%s%4d PCALL end", pfx, current);
6943 break;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006944 case ISN_DEFER:
Yegappan Lakshmananf3eac692023-10-17 11:00:45 +02006945 smsg("%s%4d DEFER %d args", pfx, current,
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006946 (int)iptr->isn_arg.defer.defer_argcount);
6947 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006948 case ISN_RETURN:
6949 smsg("%s%4d RETURN", pfx, current);
6950 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02006951 case ISN_RETURN_VOID:
6952 smsg("%s%4d RETURN void", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006953 break;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006954 case ISN_RETURN_OBJECT:
6955 smsg("%s%4d RETURN object", pfx, current);
6956 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006957 case ISN_FUNCREF:
6958 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006959 funcref_T *funcref = &iptr->isn_arg.funcref;
6960 funcref_extra_T *extra = funcref->fr_extra;
6961 char_u *name;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006962
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006963 if (extra == NULL || extra->fre_func_name == NULL)
Bram Moolenaar38453522021-11-28 22:00:12 +00006964 {
6965 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
6966 + funcref->fr_dfunc_idx;
6967 name = df->df_ufunc->uf_name;
6968 }
6969 else
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006970 name = extra->fre_func_name;
Bram Moolenaar313e4722023-02-08 20:55:27 +00006971 if (extra != NULL && extra->fre_class != NULL)
6972 {
6973 smsg("%s%4d FUNCREF %s.%s", pfx, current,
6974 extra->fre_class->class_name, name);
6975 }
6976 else if (extra == NULL
6977 || extra->fre_loopvar_info.lvi_depth == 0)
6978 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006979 smsg("%s%4d FUNCREF %s", pfx, current, name);
Bram Moolenaar313e4722023-02-08 20:55:27 +00006980 }
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006981 else
Bram Moolenaarcc341812022-09-19 15:54:34 +01006982 {
6983 char_u *info = printable_loopvarinfo(
6984 &extra->fre_loopvar_info);
6985
6986 smsg("%s%4d FUNCREF %s vars %s", pfx, current,
6987 name, info);
6988 vim_free(info);
6989 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006990 }
6991 break;
6992
6993 case ISN_NEWFUNC:
6994 {
Bram Moolenaarcc341812022-09-19 15:54:34 +01006995 newfuncarg_T *arg = iptr->isn_arg.newfunc.nf_arg;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006996
Bram Moolenaarcc341812022-09-19 15:54:34 +01006997 if (arg->nfa_loopvar_info.lvi_depth == 0)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006998 smsg("%s%4d NEWFUNC %s %s", pfx, current,
6999 arg->nfa_lambda, arg->nfa_global);
7000 else
Bram Moolenaarcc341812022-09-19 15:54:34 +01007001 {
7002 char_u *info = printable_loopvarinfo(
7003 &arg->nfa_loopvar_info);
7004
7005 smsg("%s%4d NEWFUNC %s %s vars %s", pfx, current,
7006 arg->nfa_lambda, arg->nfa_global, info);
7007 vim_free(info);
7008 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02007009 }
7010 break;
7011
7012 case ISN_DEF:
7013 {
7014 char_u *name = iptr->isn_arg.string;
7015
7016 smsg("%s%4d DEF %s", pfx, current,
7017 name == NULL ? (char_u *)"" : name);
7018 }
7019 break;
7020
7021 case ISN_JUMP:
7022 {
7023 char *when = "?";
7024
7025 switch (iptr->isn_arg.jump.jump_when)
7026 {
7027 case JUMP_ALWAYS:
7028 when = "JUMP";
7029 break;
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02007030 case JUMP_NEVER:
7031 iemsg("JUMP_NEVER should not be used");
7032 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007033 case JUMP_AND_KEEP_IF_TRUE:
7034 when = "JUMP_AND_KEEP_IF_TRUE";
7035 break;
7036 case JUMP_IF_FALSE:
7037 when = "JUMP_IF_FALSE";
7038 break;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01007039 case JUMP_WHILE_FALSE:
7040 when = "JUMP_WHILE_FALSE"; // unused
7041 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007042 case JUMP_IF_COND_FALSE:
7043 when = "JUMP_IF_COND_FALSE";
7044 break;
7045 case JUMP_IF_COND_TRUE:
7046 when = "JUMP_IF_COND_TRUE";
7047 break;
7048 }
7049 smsg("%s%4d %s -> %d", pfx, current, when,
7050 iptr->isn_arg.jump.jump_where);
7051 }
7052 break;
7053
7054 case ISN_JUMP_IF_ARG_SET:
7055 smsg("%s%4d JUMP_IF_ARG_SET arg[%d] -> %d", pfx, current,
7056 iptr->isn_arg.jumparg.jump_arg_off + STACK_FRAME_SIZE,
7057 iptr->isn_arg.jump.jump_where);
7058 break;
7059
Bram Moolenaar65b0d162022-12-13 18:43:22 +00007060 case ISN_JUMP_IF_ARG_NOT_SET:
7061 smsg("%s%4d JUMP_IF_ARG_NOT_SET arg[%d] -> %d", pfx, current,
7062 iptr->isn_arg.jumparg.jump_arg_off + STACK_FRAME_SIZE,
7063 iptr->isn_arg.jump.jump_where);
7064 break;
7065
Bram Moolenaar4c137212021-04-19 16:48:48 +02007066 case ISN_FOR:
7067 {
7068 forloop_T *forloop = &iptr->isn_arg.forloop;
7069
7070 smsg("%s%4d FOR $%d -> %d", pfx, current,
Bram Moolenaarcc341812022-09-19 15:54:34 +01007071 forloop->for_loop_idx, forloop->for_end);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007072 }
7073 break;
7074
Bram Moolenaarb46c0832022-09-15 17:19:37 +01007075 case ISN_ENDLOOP:
7076 {
7077 endloop_T *endloop = &iptr->isn_arg.endloop;
7078
Bram Moolenaarcc341812022-09-19 15:54:34 +01007079 smsg("%s%4d ENDLOOP ref $%d save $%d-$%d depth %d",
7080 pfx, current,
Bram Moolenaarb46c0832022-09-15 17:19:37 +01007081 endloop->end_funcref_idx,
7082 endloop->end_var_idx,
Bram Moolenaarcc341812022-09-19 15:54:34 +01007083 endloop->end_var_idx + endloop->end_var_count - 1,
7084 endloop->end_depth);
Bram Moolenaarb46c0832022-09-15 17:19:37 +01007085 }
7086 break;
7087
7088 case ISN_WHILE:
7089 {
7090 whileloop_T *whileloop = &iptr->isn_arg.whileloop;
7091
7092 smsg("%s%4d WHILE $%d -> %d", pfx, current,
7093 whileloop->while_funcref_idx,
7094 whileloop->while_end);
7095 }
7096 break;
7097
Bram Moolenaar4c137212021-04-19 16:48:48 +02007098 case ISN_TRY:
7099 {
Bram Moolenaar0d807102021-12-21 09:42:09 +00007100 try_T *try = &iptr->isn_arg.tryref;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007101
7102 if (try->try_ref->try_finally == 0)
7103 smsg("%s%4d TRY catch -> %d, endtry -> %d",
7104 pfx, current,
7105 try->try_ref->try_catch,
7106 try->try_ref->try_endtry);
7107 else
7108 smsg("%s%4d TRY catch -> %d, finally -> %d, endtry -> %d",
7109 pfx, current,
7110 try->try_ref->try_catch,
7111 try->try_ref->try_finally,
7112 try->try_ref->try_endtry);
7113 }
7114 break;
7115 case ISN_CATCH:
Bram Moolenaar4c137212021-04-19 16:48:48 +02007116 smsg("%s%4d CATCH", pfx, current);
7117 break;
7118 case ISN_TRYCONT:
7119 {
7120 trycont_T *trycont = &iptr->isn_arg.trycont;
7121
7122 smsg("%s%4d TRY-CONTINUE %d level%s -> %d", pfx, current,
7123 trycont->tct_levels,
7124 trycont->tct_levels == 1 ? "" : "s",
7125 trycont->tct_where);
7126 }
7127 break;
7128 case ISN_FINALLY:
7129 smsg("%s%4d FINALLY", pfx, current);
7130 break;
7131 case ISN_ENDTRY:
7132 smsg("%s%4d ENDTRY", pfx, current);
7133 break;
7134 case ISN_THROW:
7135 smsg("%s%4d THROW", pfx, current);
7136 break;
7137
7138 // expression operations on number
7139 case ISN_OPNR:
7140 case ISN_OPFLOAT:
7141 case ISN_OPANY:
7142 {
7143 char *what;
7144 char *ins;
7145
7146 switch (iptr->isn_arg.op.op_type)
7147 {
7148 case EXPR_MULT: what = "*"; break;
7149 case EXPR_DIV: what = "/"; break;
7150 case EXPR_REM: what = "%"; break;
7151 case EXPR_SUB: what = "-"; break;
7152 case EXPR_ADD: what = "+"; break;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01007153 case EXPR_LSHIFT: what = "<<"; break;
7154 case EXPR_RSHIFT: what = ">>"; break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007155 default: what = "???"; break;
7156 }
7157 switch (iptr->isn_type)
7158 {
7159 case ISN_OPNR: ins = "OPNR"; break;
7160 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
7161 case ISN_OPANY: ins = "OPANY"; break;
7162 default: ins = "???"; break;
7163 }
7164 smsg("%s%4d %s %s", pfx, current, ins, what);
7165 }
7166 break;
7167
7168 case ISN_COMPAREBOOL:
7169 case ISN_COMPARESPECIAL:
Bram Moolenaar7a222242022-03-01 19:23:24 +00007170 case ISN_COMPARENULL:
Bram Moolenaar4c137212021-04-19 16:48:48 +02007171 case ISN_COMPARENR:
7172 case ISN_COMPAREFLOAT:
7173 case ISN_COMPARESTRING:
7174 case ISN_COMPAREBLOB:
7175 case ISN_COMPARELIST:
7176 case ISN_COMPAREDICT:
7177 case ISN_COMPAREFUNC:
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00007178 case ISN_COMPARECLASS:
7179 case ISN_COMPAREOBJECT:
Bram Moolenaar4c137212021-04-19 16:48:48 +02007180 case ISN_COMPAREANY:
7181 {
7182 char *p;
7183 char buf[10];
7184 char *type;
7185
7186 switch (iptr->isn_arg.op.op_type)
7187 {
7188 case EXPR_EQUAL: p = "=="; break;
7189 case EXPR_NEQUAL: p = "!="; break;
7190 case EXPR_GREATER: p = ">"; break;
7191 case EXPR_GEQUAL: p = ">="; break;
7192 case EXPR_SMALLER: p = "<"; break;
7193 case EXPR_SEQUAL: p = "<="; break;
7194 case EXPR_MATCH: p = "=~"; break;
7195 case EXPR_IS: p = "is"; break;
7196 case EXPR_ISNOT: p = "isnot"; break;
7197 case EXPR_NOMATCH: p = "!~"; break;
7198 default: p = "???"; break;
7199 }
7200 STRCPY(buf, p);
7201 if (iptr->isn_arg.op.op_ic == TRUE)
7202 strcat(buf, "?");
7203 switch(iptr->isn_type)
7204 {
7205 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
7206 case ISN_COMPARESPECIAL:
7207 type = "COMPARESPECIAL"; break;
Bram Moolenaar7a222242022-03-01 19:23:24 +00007208 case ISN_COMPARENULL: type = "COMPARENULL"; break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007209 case ISN_COMPARENR: type = "COMPARENR"; break;
7210 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
7211 case ISN_COMPARESTRING:
7212 type = "COMPARESTRING"; break;
7213 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
7214 case ISN_COMPARELIST: type = "COMPARELIST"; break;
7215 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
7216 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00007217 case ISN_COMPARECLASS: type = "COMPARECLASS"; break;
7218 case ISN_COMPAREOBJECT:
7219 type = "COMPAREOBJECT"; break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007220 case ISN_COMPAREANY: type = "COMPAREANY"; break;
7221 default: type = "???"; break;
7222 }
7223
7224 smsg("%s%4d %s %s", pfx, current, type, buf);
7225 }
7226 break;
7227
7228 case ISN_ADDLIST: smsg("%s%4d ADDLIST", pfx, current); break;
7229 case ISN_ADDBLOB: smsg("%s%4d ADDBLOB", pfx, current); break;
7230
7231 // expression operations
LemonBoy372bcce2022-04-25 12:43:20 +01007232 case ISN_CONCAT:
7233 smsg("%s%4d CONCAT size %lld", pfx, current,
7234 (varnumber_T)(iptr->isn_arg.number));
7235 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007236 case ISN_STRINDEX: smsg("%s%4d STRINDEX", pfx, current); break;
7237 case ISN_STRSLICE: smsg("%s%4d STRSLICE", pfx, current); break;
7238 case ISN_BLOBINDEX: smsg("%s%4d BLOBINDEX", pfx, current); break;
7239 case ISN_BLOBSLICE: smsg("%s%4d BLOBSLICE", pfx, current); break;
7240 case ISN_LISTAPPEND: smsg("%s%4d LISTAPPEND", pfx, current); break;
7241 case ISN_BLOBAPPEND: smsg("%s%4d BLOBAPPEND", pfx, current); break;
7242 case ISN_LISTINDEX: smsg("%s%4d LISTINDEX", pfx, current); break;
7243 case ISN_LISTSLICE: smsg("%s%4d LISTSLICE", pfx, current); break;
7244 case ISN_ANYINDEX: smsg("%s%4d ANYINDEX", pfx, current); break;
7245 case ISN_ANYSLICE: smsg("%s%4d ANYSLICE", pfx, current); break;
7246 case ISN_SLICE: smsg("%s%4d SLICE %lld",
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02007247 pfx, current, iptr->isn_arg.number); break;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02007248 case ISN_GETITEM: smsg("%s%4d ITEM %lld%s", pfx, current,
7249 iptr->isn_arg.getitem.gi_index,
7250 iptr->isn_arg.getitem.gi_with_op ?
7251 " with op" : ""); break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007252 case ISN_MEMBER: smsg("%s%4d MEMBER", pfx, current); break;
7253 case ISN_STRINGMEMBER: smsg("%s%4d MEMBER %s", pfx, current,
7254 iptr->isn_arg.string); break;
Yegappan Lakshmanan5a05d372023-09-29 19:43:11 +02007255 case ISN_GET_OBJ_MEMBER: smsg("%s%4d OBJ_MEMBER %d", pfx, current,
7256 (int)iptr->isn_arg.classmember.cm_idx);
Ernie Rael00df69e2023-09-05 07:38:09 +02007257 break;
Yegappan Lakshmanan5a05d372023-09-29 19:43:11 +02007258 case ISN_GET_ITF_MEMBER: smsg("%s%4d ITF_MEMBER %d on %s",
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00007259 pfx, current,
7260 (int)iptr->isn_arg.classmember.cm_idx,
Yegappan Lakshmanan5a05d372023-09-29 19:43:11 +02007261 iptr->isn_arg.classmember.cm_class->class_name);
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00007262 break;
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00007263 case ISN_STORE_THIS: smsg("%s%4d STORE_THIS %d", pfx, current,
Bram Moolenaarffdaca92022-12-09 21:41:48 +00007264 (int)iptr->isn_arg.number); break;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02007265 case ISN_CLEARDICT: smsg("%s%4d CLEARDICT", pfx, current); break;
7266 case ISN_USEDICT: smsg("%s%4d USEDICT", pfx, current); break;
7267
Bram Moolenaar4c137212021-04-19 16:48:48 +02007268 case ISN_NEGATENR: smsg("%s%4d NEGATENR", pfx, current); break;
7269
Bram Moolenaar4c137212021-04-19 16:48:48 +02007270 case ISN_CHECKTYPE:
7271 {
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01007272 checktype_T *ct = &iptr->isn_arg.type;
Bram Moolenaarc6951a72022-12-29 20:56:24 +00007273 char *tofree = NULL;
7274 char *typename;
7275
7276 if (ct->ct_type->tt_type == VAR_FLOAT
7277 && (ct->ct_type->tt_flags & TTFLAG_NUMBER_OK))
7278 typename = "float|number";
7279 else
7280 typename = type_name(ct->ct_type, &tofree);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007281
7282 if (ct->ct_arg_idx == 0)
7283 smsg("%s%4d CHECKTYPE %s stack[%d]", pfx, current,
Bram Moolenaarc6951a72022-12-29 20:56:24 +00007284 typename,
Bram Moolenaar4c137212021-04-19 16:48:48 +02007285 (int)ct->ct_off);
7286 else
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01007287 smsg("%s%4d CHECKTYPE %s stack[%d] %s %d",
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02007288 pfx, current,
Bram Moolenaarc6951a72022-12-29 20:56:24 +00007289 typename,
Bram Moolenaar4c137212021-04-19 16:48:48 +02007290 (int)ct->ct_off,
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01007291 ct->ct_is_var ? "var": "arg",
Bram Moolenaar4c137212021-04-19 16:48:48 +02007292 (int)ct->ct_arg_idx);
7293 vim_free(tofree);
7294 break;
7295 }
7296 case ISN_CHECKLEN: smsg("%s%4d CHECKLEN %s%d", pfx, current,
7297 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
7298 iptr->isn_arg.checklen.cl_min_len);
7299 break;
7300 case ISN_SETTYPE:
7301 {
7302 char *tofree;
7303
7304 smsg("%s%4d SETTYPE %s", pfx, current,
7305 type_name(iptr->isn_arg.type.ct_type, &tofree));
7306 vim_free(tofree);
7307 break;
7308 }
7309 case ISN_COND2BOOL: smsg("%s%4d COND2BOOL", pfx, current); break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007310 case ISN_2BOOL: if (iptr->isn_arg.tobool.invert)
7311 smsg("%s%4d INVERT %d (!val)", pfx, current,
7312 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007313 else
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007314 smsg("%s%4d 2BOOL %d (!!val)", pfx, current,
7315 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007316 break;
7317 case ISN_2STRING: smsg("%s%4d 2STRING stack[%lld]", pfx, current,
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007318 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02007319 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007320 case ISN_2STRING_ANY: smsg("%s%4d 2STRING_ANY stack[%lld]",
7321 pfx, current,
7322 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02007323 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007324 case ISN_RANGE: smsg("%s%4d RANGE %s", pfx, current,
7325 iptr->isn_arg.string);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007326 break;
7327 case ISN_PUT:
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01007328 if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE)
Bram Moolenaar4c137212021-04-19 16:48:48 +02007329 smsg("%s%4d PUT %c above range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007330 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007331 else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE)
7332 smsg("%s%4d PUT %c range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007333 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007334 else
7335 smsg("%s%4d PUT %c %ld", pfx, current,
7336 iptr->isn_arg.put.put_regname,
7337 (long)iptr->isn_arg.put.put_lnum);
7338 break;
7339
Bram Moolenaar4c137212021-04-19 16:48:48 +02007340 case ISN_CMDMOD:
7341 {
7342 char_u *buf;
7343 size_t len = produce_cmdmods(
7344 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
7345
7346 buf = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02007347 if (likely(buf != NULL))
Bram Moolenaar4c137212021-04-19 16:48:48 +02007348 {
7349 (void)produce_cmdmods(
7350 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
7351 smsg("%s%4d CMDMOD %s", pfx, current, buf);
7352 vim_free(buf);
7353 }
7354 break;
7355 }
7356 case ISN_CMDMOD_REV: smsg("%s%4d CMDMOD_REV", pfx, current); break;
7357
7358 case ISN_PROF_START:
Bram Moolenaare99d4222021-06-13 14:01:26 +02007359 smsg("%s%4d PROFILE START line %d", pfx, current,
7360 iptr->isn_lnum);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007361 break;
7362
7363 case ISN_PROF_END:
7364 smsg("%s%4d PROFILE END", pfx, current);
7365 break;
7366
Bram Moolenaare99d4222021-06-13 14:01:26 +02007367 case ISN_DEBUG:
Bram Moolenaar8cec9272021-06-23 20:20:53 +02007368 smsg("%s%4d DEBUG line %d-%d varcount %lld", pfx, current,
7369 iptr->isn_arg.debug.dbg_break_lnum + 1,
7370 iptr->isn_lnum,
7371 iptr->isn_arg.debug.dbg_var_names_len);
Bram Moolenaare99d4222021-06-13 14:01:26 +02007372 break;
7373
Bram Moolenaar4c137212021-04-19 16:48:48 +02007374 case ISN_UNPACK: smsg("%s%4d UNPACK %d%s", pfx, current,
7375 iptr->isn_arg.unpack.unp_count,
7376 iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : "");
7377 break;
7378 case ISN_SHUFFLE: smsg("%s%4d SHUFFLE %d up %d", pfx, current,
7379 iptr->isn_arg.shuffle.shfl_item,
7380 iptr->isn_arg.shuffle.shfl_up);
7381 break;
7382 case ISN_DROP: smsg("%s%4d DROP", pfx, current); break;
7383
7384 case ISN_FINISH: // End of list of instructions for ISN_SUBSTITUTE.
7385 return;
7386 }
7387
7388 out_flush(); // output one line at a time
7389 ui_breakcheck();
7390 if (got_int)
7391 break;
7392 }
7393}
7394
7395/*
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02007396 * Handle command line completion for the :disassemble command.
7397 */
7398 void
7399set_context_in_disassemble_cmd(expand_T *xp, char_u *arg)
7400{
7401 char_u *p;
7402
7403 // Default: expand user functions, "debug" and "profile"
7404 xp->xp_context = EXPAND_DISASSEMBLE;
7405 xp->xp_pattern = arg;
7406
7407 // first argument already typed: only user function names
7408 if (*arg != NUL && *(p = skiptowhite(arg)) != NUL)
7409 {
7410 xp->xp_context = EXPAND_USER_FUNC;
7411 xp->xp_pattern = skipwhite(p);
7412 }
7413}
7414
7415/*
7416 * Function given to ExpandGeneric() to obtain the list of :disassemble
7417 * arguments.
7418 */
7419 char_u *
7420get_disassemble_argument(expand_T *xp, int idx)
7421{
7422 if (idx == 0)
7423 return (char_u *)"debug";
7424 if (idx == 1)
7425 return (char_u *)"profile";
7426 return get_user_func_name(xp, idx - 2);
7427}
7428
7429/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01007430 * ":disassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01007431 * We don't really need this at runtime, but we do have tests that require it,
7432 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007433 */
7434 void
7435ex_disassemble(exarg_T *eap)
7436{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01007437 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01007438 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007439 dfunc_T *dfunc;
Bram Moolenaardafef512022-05-21 21:55:55 +01007440 isn_T *instr = NULL; // init to shut up gcc warning
7441 int instr_count = 0; // init to shut up gcc warning
Bram Moolenaar5a01caa2022-05-21 18:56:58 +01007442 compiletype_T compile_type = CT_NONE;
Bram Moolenaare99d4222021-06-13 14:01:26 +02007443
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01007444 ufunc = find_func_by_name(arg, &compile_type);
Bram Moolenaara26b9702020-04-18 19:53:28 +02007445 if (ufunc == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007446 return;
Bram Moolenaare99d4222021-06-13 14:01:26 +02007447 if (func_needs_compiling(ufunc, compile_type)
7448 && compile_def_function(ufunc, FALSE, compile_type, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02007449 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007450 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007451 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007452 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007453 return;
7454 }
Bram Moolenaar6db660b2021-08-01 14:08:54 +02007455 msg((char *)printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007456
7457 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
Bram Moolenaare99d4222021-06-13 14:01:26 +02007458 switch (compile_type)
7459 {
7460 case CT_PROFILE:
Bram Moolenaarf002a412021-01-24 13:34:18 +01007461#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02007462 instr = dfunc->df_instr_prof;
7463 instr_count = dfunc->df_instr_prof_count;
7464 break;
Bram Moolenaarf002a412021-01-24 13:34:18 +01007465#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02007466 // FALLTHROUGH
7467 case CT_NONE:
7468 instr = dfunc->df_instr;
7469 instr_count = dfunc->df_instr_count;
7470 break;
7471 case CT_DEBUG:
7472 instr = dfunc->df_instr_debug;
7473 instr_count = dfunc->df_instr_debug_count;
7474 break;
7475 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007476
Bram Moolenaar4c137212021-04-19 16:48:48 +02007477 list_instructions("", instr, instr_count, ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007478}
7479
7480/*
Bram Moolenaar13106602020-10-04 16:06:05 +02007481 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007482 * list, etc. Mostly like what JavaScript does, except that empty list and
7483 * empty dictionary are FALSE.
7484 */
7485 int
7486tv2bool(typval_T *tv)
7487{
7488 switch (tv->v_type)
7489 {
7490 case VAR_NUMBER:
7491 return tv->vval.v_number != 0;
7492 case VAR_FLOAT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007493 return tv->vval.v_float != 0.0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007494 case VAR_PARTIAL:
7495 return tv->vval.v_partial != NULL;
7496 case VAR_FUNC:
7497 case VAR_STRING:
7498 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
7499 case VAR_LIST:
7500 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
7501 case VAR_DICT:
7502 return tv->vval.v_dict != NULL
7503 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
7504 case VAR_BOOL:
7505 case VAR_SPECIAL:
7506 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
7507 case VAR_JOB:
7508#ifdef FEAT_JOB_CHANNEL
7509 return tv->vval.v_job != NULL;
7510#else
7511 break;
7512#endif
7513 case VAR_CHANNEL:
7514#ifdef FEAT_JOB_CHANNEL
7515 return tv->vval.v_channel != NULL;
7516#else
7517 break;
7518#endif
7519 case VAR_BLOB:
7520 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
7521 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02007522 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007523 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02007524 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00007525 case VAR_CLASS:
7526 case VAR_OBJECT:
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02007527 case VAR_TYPEALIAS:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007528 break;
7529 }
7530 return FALSE;
7531}
7532
Bram Moolenaarea2d4072020-11-12 12:08:51 +01007533 void
7534emsg_using_string_as(typval_T *tv, int as_number)
7535{
7536 semsg(_(as_number ? e_using_string_as_number_str
7537 : e_using_string_as_bool_str),
7538 tv->vval.v_string == NULL
7539 ? (char_u *)"" : tv->vval.v_string);
7540}
7541
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007542/*
7543 * If "tv" is a string give an error and return FAIL.
7544 */
7545 int
7546check_not_string(typval_T *tv)
7547{
7548 if (tv->v_type == VAR_STRING)
7549 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +01007550 emsg_using_string_as(tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007551 clear_tv(tv);
7552 return FAIL;
7553 }
7554 return OK;
7555}
7556
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007557
7558#endif // FEAT_EVAL