blob: 1fdff84da7112b6b48732e6b8005041197863be8 [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
1143 (void)call_func(name, -1, &rettv, argcount, argvars, &funcexe);
1144
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001145 clear_tv(&rettv);
zeertzjqa1f2b5d2023-04-18 21:04:53 +01001146 vim_free(name);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001147 }
1148}
1149
1150/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001151 * Return from the current function.
1152 */
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001153 static int
Bram Moolenaar4c137212021-04-19 16:48:48 +02001154func_return(ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001155{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001156 int idx;
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01001157 int ret_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001158 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1159 + ectx->ec_dfunc_idx;
1160 int argcount = ufunc_argcount(dfunc->df_ufunc);
Bram Moolenaarc620c052020-07-08 15:16:19 +02001161 estack_T *entry;
Bram Moolenaar12d26532021-02-19 19:13:21 +01001162 int prev_dfunc_idx = STACK_TV(ectx->ec_frame_idx
1163 + STACK_FRAME_FUNC_OFF)->vval.v_number;
Bram Moolenaarb06b50d2021-04-26 21:39:25 +02001164 funclocal_T *floc;
1165#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01001166 dfunc_T *prev_dfunc = ((dfunc_T *)def_functions.ga_data)
1167 + prev_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001168
Bram Moolenaar12d26532021-02-19 19:13:21 +01001169 if (do_profiling == PROF_YES)
1170 {
1171 ufunc_T *caller = prev_dfunc->df_ufunc;
1172
1173 if (dfunc->df_ufunc->uf_profiling
1174 || (caller != NULL && caller->uf_profiling))
1175 {
1176 profile_may_end_func(((profinfo_T *)profile_info_ga.ga_data)
1177 + profile_info_ga.ga_len - 1, dfunc->df_ufunc, caller);
1178 --profile_info_ga.ga_len;
1179 }
1180 }
1181#endif
Bram Moolenaar919c12c2021-12-14 14:29:16 +00001182
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001183 if (dfunc->df_defer_var_idx > 0)
1184 invoke_defer_funcs(ectx);
1185
Bram Moolenaar919c12c2021-12-14 14:29:16 +00001186 // No check for uf_refcount being zero, cannot think of a way that would
1187 // happen.
Bram Moolenaarc970e422021-03-17 15:03:04 +01001188 --dfunc->df_ufunc->uf_calls;
1189
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001190 // execution context goes one level up
Bram Moolenaarc620c052020-07-08 15:16:19 +02001191 entry = estack_pop();
1192 if (entry != NULL)
Bram Moolenaarc70fe462021-04-17 17:59:19 +02001193 current_sctx = entry->es_save_sctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001194
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001195 if (handle_closure_in_use(ectx, TRUE) == FAIL)
1196 return FAIL;
1197
Bram Moolenaar574950d2023-01-03 19:08:50 +00001198 // Clear the arguments. If this was an object method also clear the
1199 // object, it is just before the arguments.
1200 int top = ectx->ec_frame_idx - argcount;
Yegappan Lakshmanan1db15142023-09-19 20:34:05 +02001201 if (IS_OBJECT_METHOD(dfunc->df_ufunc))
Bram Moolenaar574950d2023-01-03 19:08:50 +00001202 --top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001203 for (idx = top; idx < ectx->ec_frame_idx; ++idx)
1204 clear_tv(STACK_TV(idx));
1205
1206 // Clear local variables and temp values, but not the return value.
1207 for (idx = ectx->ec_frame_idx + STACK_FRAME_SIZE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001208 idx < ectx->ec_stack.ga_len - 1; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001209 clear_tv(STACK_TV(idx));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001210
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01001211 // The return value should be on top of the stack. However, when aborting
1212 // it may not be there and ec_frame_idx is the top of the stack.
1213 ret_idx = ectx->ec_stack.ga_len - 1;
Bram Moolenaar0186e582021-01-10 18:33:11 +01001214 if (ret_idx == ectx->ec_frame_idx + STACK_FRAME_IDX_OFF)
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01001215 ret_idx = 0;
1216
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001217 if (ectx->ec_outer_ref != NULL)
1218 {
1219 if (ectx->ec_outer_ref->or_outer_allocated)
1220 vim_free(ectx->ec_outer_ref->or_outer);
1221 partial_unref(ectx->ec_outer_ref->or_partial);
1222 vim_free(ectx->ec_outer_ref);
1223 }
Bram Moolenaar0186e582021-01-10 18:33:11 +01001224
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001225 // Restore the previous frame.
Bram Moolenaar12d26532021-02-19 19:13:21 +01001226 ectx->ec_dfunc_idx = prev_dfunc_idx;
Bram Moolenaar0186e582021-01-10 18:33:11 +01001227 ectx->ec_iidx = STACK_TV(ectx->ec_frame_idx
1228 + STACK_FRAME_IIDX_OFF)->vval.v_number;
Bram Moolenaar5930ddc2021-04-26 20:32:59 +02001229 ectx->ec_instr = (void *)STACK_TV(ectx->ec_frame_idx
1230 + STACK_FRAME_INSTR_OFF)->vval.v_string;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001231 ectx->ec_outer_ref = (void *)STACK_TV(ectx->ec_frame_idx
Bram Moolenaar0186e582021-01-10 18:33:11 +01001232 + STACK_FRAME_OUTER_OFF)->vval.v_string;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001233 floc = (void *)STACK_TV(ectx->ec_frame_idx
1234 + STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string;
Bram Moolenaar5366e1a2020-10-01 13:01:34 +02001235 // restoring ec_frame_idx must be last
Bram Moolenaar0186e582021-01-10 18:33:11 +01001236 ectx->ec_frame_idx = STACK_TV(ectx->ec_frame_idx
1237 + STACK_FRAME_IDX_OFF)->vval.v_number;
Bram Moolenaard386e922021-04-25 14:48:49 +02001238
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001239 if (floc == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02001240 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001241 else
1242 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02001243 ectx->ec_funclocal = *floc;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001244 vim_free(floc);
1245 }
1246
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01001247 if (ret_idx > 0)
1248 {
1249 // Reset the stack to the position before the call, with a spot for the
1250 // return value, moved there from above the frame.
1251 ectx->ec_stack.ga_len = top + 1;
1252 *STACK_TV_BOT(-1) = *STACK_TV(ret_idx);
1253 }
1254 else
1255 // Reset the stack to the position before the call.
1256 ectx->ec_stack.ga_len = top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001257
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001258 funcdepth_decrement();
Bram Moolenaare99d4222021-06-13 14:01:26 +02001259 --ex_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001260 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001261}
1262
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001263/*
1264 * Prepare arguments and rettv for calling a builtin or user function.
1265 */
1266 static int
1267call_prepare(int argcount, typval_T *argvars, ectx_T *ectx)
1268{
1269 int idx;
1270 typval_T *tv;
1271
1272 // Move arguments from bottom of the stack to argvars[] and add terminator.
1273 for (idx = 0; idx < argcount; ++idx)
1274 argvars[idx] = *STACK_TV_BOT(idx - argcount);
1275 argvars[argcount].v_type = VAR_UNKNOWN;
1276
1277 // Result replaces the arguments on the stack.
1278 if (argcount > 0)
1279 ectx->ec_stack.ga_len -= argcount - 1;
Bram Moolenaar35578162021-08-02 19:10:38 +02001280 else if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001281 return FAIL;
1282 else
1283 ++ectx->ec_stack.ga_len;
1284
1285 // Default return value is zero.
1286 tv = STACK_TV_BOT(-1);
1287 tv->v_type = VAR_NUMBER;
1288 tv->vval.v_number = 0;
Bram Moolenaar24565cf2022-03-28 18:16:52 +01001289 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001290
1291 return OK;
1292}
1293
1294/*
1295 * Call a builtin function by index.
1296 */
1297 static int
1298call_bfunc(int func_idx, int argcount, ectx_T *ectx)
1299{
1300 typval_T argvars[MAX_FUNC_ARGS];
1301 int idx;
Bram Moolenaar171fb922020-10-28 16:54:47 +01001302 int did_emsg_before = did_emsg;
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001303 ectx_T *prev_ectx = current_ectx;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001304 char *save_func_name = ectx->ec_where.wt_func_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001305
1306 if (call_prepare(argcount, argvars, ectx) == FAIL)
1307 return FAIL;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001308 ectx->ec_where.wt_func_name = internal_func_name(func_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001309
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001310 // Call the builtin function. Set "current_ectx" so that when it
1311 // recursively invokes call_def_function() a closure context can be set.
1312 current_ectx = ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001313 call_internal_func_by_idx(func_idx, argvars, STACK_TV_BOT(-1));
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001314 current_ectx = prev_ectx;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001315 ectx->ec_where.wt_func_name = save_func_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001316
1317 // Clear the arguments.
1318 for (idx = 0; idx < argcount; ++idx)
1319 clear_tv(&argvars[idx]);
Bram Moolenaar015f4262020-05-05 21:25:22 +02001320
Bram Moolenaar57f799e2020-12-12 20:42:19 +01001321 if (did_emsg > did_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +02001322 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001323 return OK;
1324}
1325
1326/*
1327 * Execute a user defined function.
Bram Moolenaarab360522021-01-10 14:02:28 +01001328 * If the function is compiled this will add a stack frame and set the
1329 * instruction pointer at the start of the function.
1330 * Otherwise the function is called here.
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001331 * If "pt" is not null use "pt->pt_outer" for ec_outer_ref->or_outer.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001332 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001333 */
1334 static int
Bram Moolenaar0186e582021-01-10 18:33:11 +01001335call_ufunc(
1336 ufunc_T *ufunc,
1337 partial_T *pt,
1338 int argcount,
1339 ectx_T *ectx,
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001340 isn_T *iptr,
1341 dict_T *selfdict)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001342{
1343 typval_T argvars[MAX_FUNC_ARGS];
1344 funcexe_T funcexe;
Bram Moolenaar0917e862023-02-18 14:42:44 +00001345 funcerror_T error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001346 int idx;
Bram Moolenaar28ee8922020-10-28 20:20:00 +01001347 int did_emsg_before = did_emsg;
Bram Moolenaar139575d2022-03-15 19:29:30 +00001348 compiletype_T compile_type = get_compile_type(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001349
Bram Moolenaare99d4222021-06-13 14:01:26 +02001350 if (func_needs_compiling(ufunc, compile_type)
1351 && compile_def_function(ufunc, FALSE, compile_type, NULL)
1352 == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02001353 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001354 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001355 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +01001356 error = check_user_func_argcount(ufunc, argcount);
Bram Moolenaar50824712020-12-20 21:10:17 +01001357 if (error != FCERR_UNKNOWN)
1358 {
1359 if (error == FCERR_TOOMANY)
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001360 semsg(_(e_too_many_arguments_for_function_str),
1361 printable_func_name(ufunc));
Bram Moolenaar50824712020-12-20 21:10:17 +01001362 else
Bram Moolenaare1242042021-12-16 20:56:57 +00001363 semsg(_(e_not_enough_arguments_for_function_str),
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001364 printable_func_name(ufunc));
Bram Moolenaar50824712020-12-20 21:10:17 +01001365 return FAIL;
1366 }
1367
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001368 // The function has been compiled, can call it quickly. For a function
1369 // that was defined later: we can call it directly next time.
1370 if (iptr != NULL)
1371 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001372 delete_instr(iptr);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001373 iptr->isn_type = ISN_DCALL;
1374 iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1375 iptr->isn_arg.dfunc.cdf_argcount = argcount;
1376 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001377 return call_dfunc(ufunc->uf_dfunc_idx, pt, argcount, ectx);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001378 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001379
1380 if (call_prepare(argcount, argvars, ectx) == FAIL)
1381 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +02001382 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00001383 funcexe.fe_evaluate = TRUE;
1384 funcexe.fe_selfdict = selfdict != NULL ? selfdict : dict_stack_get_dict();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001385
1386 // Call the user function. Result goes in last position on the stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001387 error = call_user_func_check(ufunc, argcount, argvars,
Bram Moolenaar851f86b2021-12-13 14:26:44 +00001388 STACK_TV_BOT(-1), &funcexe, funcexe.fe_selfdict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001389
1390 // Clear the arguments.
1391 for (idx = 0; idx < argcount; ++idx)
1392 clear_tv(&argvars[idx]);
1393
1394 if (error != FCERR_NONE)
1395 {
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01001396 user_func_error(error, printable_func_name(ufunc),
1397 funcexe.fe_found_var);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001398 return FAIL;
1399 }
Bram Moolenaar28ee8922020-10-28 20:20:00 +01001400 if (did_emsg > did_emsg_before)
Bram Moolenaared677f52020-08-12 16:38:10 +02001401 // Error other than from calling the function itself.
1402 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001403 return OK;
1404}
1405
1406/*
Bram Moolenaara91a7132021-03-25 21:12:15 +01001407 * If command modifiers were applied restore them.
1408 */
1409 static void
1410may_restore_cmdmod(funclocal_T *funclocal)
1411{
1412 if (funclocal->floc_restore_cmdmod)
1413 {
1414 cmdmod.cmod_filter_regmatch.regprog = NULL;
1415 undo_cmdmod(&cmdmod);
1416 cmdmod = funclocal->floc_save_cmdmod;
1417 funclocal->floc_restore_cmdmod = FALSE;
1418 }
1419}
1420
1421/*
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001422 * Return TRUE if an error was given (not caught in try/catch) or CTRL-C was
1423 * pressed.
Bram Moolenaara1773442020-08-12 15:21:22 +02001424 */
1425 static int
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001426vim9_aborting(int prev_uncaught_emsg)
Bram Moolenaara1773442020-08-12 15:21:22 +02001427{
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001428 return uncaught_emsg > prev_uncaught_emsg || got_int || did_throw;
Bram Moolenaara1773442020-08-12 15:21:22 +02001429}
1430
1431/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001432 * Execute a function by "name".
1433 * This can be a builtin function or a user function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001434 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001435 * Returns FAIL if not found without an error message.
1436 */
1437 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001438call_by_name(
1439 char_u *name,
1440 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001441 ectx_T *ectx,
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001442 isn_T *iptr,
1443 dict_T *selfdict)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001444{
1445 ufunc_T *ufunc;
1446
1447 if (builtin_function(name, -1))
1448 {
1449 int func_idx = find_internal_func(name);
1450
Bram Moolenaar1983f1a2022-02-28 20:55:02 +00001451 if (func_idx < 0) // Impossible?
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001452 return FAIL;
Bram Moolenaar389df252020-07-09 21:20:47 +02001453 if (check_internal_func(func_idx, argcount) < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001454 return FAIL;
1455 return call_bfunc(func_idx, argcount, ectx);
1456 }
1457
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00001458 ufunc = find_func(name, FALSE);
Bram Moolenaara1773442020-08-12 15:21:22 +02001459
1460 if (ufunc == NULL)
1461 {
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001462 int prev_uncaught_emsg = uncaught_emsg;
Bram Moolenaara1773442020-08-12 15:21:22 +02001463
1464 if (script_autoload(name, TRUE))
1465 // loaded a package, search for the function again
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00001466 ufunc = find_func(name, FALSE);
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001467
1468 if (vim9_aborting(prev_uncaught_emsg))
Bram Moolenaara1773442020-08-12 15:21:22 +02001469 return FAIL; // bail out if loading the script caused an error
1470 }
1471
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001472 if (ufunc != NULL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001473 {
Bram Moolenaar0d89d8a2022-12-31 14:01:24 +00001474 if (check_ufunc_arg_types(ufunc, argcount, 0, ectx) == FAIL)
1475 return FAIL;
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001476
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001477 return call_ufunc(ufunc, NULL, argcount, ectx, iptr, selfdict);
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001478 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001479
1480 return FAIL;
1481}
1482
1483 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001484call_partial(
1485 typval_T *tv,
1486 int argcount_arg,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001487 ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001488{
Bram Moolenaara90afb92020-07-15 22:38:56 +02001489 int argcount = argcount_arg;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001490 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001491 int called_emsg_before = called_emsg;
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +01001492 int res = FAIL;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001493 dict_T *selfdict = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001494
1495 if (tv->v_type == VAR_PARTIAL)
1496 {
Bram Moolenaara90afb92020-07-15 22:38:56 +02001497 partial_T *pt = tv->vval.v_partial;
1498 int i;
1499
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +02001500 if (pt->pt_obj != NULL)
1501 {
1502 // partial with an object method. Push the object before the
1503 // function arguments.
1504 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
1505 return FAIL;
1506 for (i = 1; i <= argcount; ++i)
1507 *STACK_TV_BOT(-i + 1) = *STACK_TV_BOT(-i);
1508
1509 typval_T *obj_tv = STACK_TV_BOT(-argcount);
1510 obj_tv->v_type = VAR_OBJECT;
1511 obj_tv->v_lock = 0;
1512 obj_tv->vval.v_object = pt->pt_obj;
1513 ++pt->pt_obj->obj_refcount;
1514 ++ectx->ec_stack.ga_len;
1515 }
1516
Bram Moolenaara90afb92020-07-15 22:38:56 +02001517 if (pt->pt_argc > 0)
1518 {
1519 // Make space for arguments from the partial, shift the "argcount"
1520 // arguments up.
Bram Moolenaar35578162021-08-02 19:10:38 +02001521 if (GA_GROW_FAILS(&ectx->ec_stack, pt->pt_argc))
Bram Moolenaara90afb92020-07-15 22:38:56 +02001522 return FAIL;
1523 for (i = 1; i <= argcount; ++i)
1524 *STACK_TV_BOT(-i + pt->pt_argc) = *STACK_TV_BOT(-i);
1525 ectx->ec_stack.ga_len += pt->pt_argc;
1526 argcount += pt->pt_argc;
1527
1528 // copy the arguments from the partial onto the stack
1529 for (i = 0; i < pt->pt_argc; ++i)
1530 copy_tv(&pt->pt_argv[i], STACK_TV_BOT(-argcount + i));
1531 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001532 selfdict = pt->pt_dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001533
1534 if (pt->pt_func != NULL)
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001535 return call_ufunc(pt->pt_func, pt, argcount, ectx, NULL, selfdict);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001536
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001537 name = pt->pt_name;
1538 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001539 else if (tv->v_type == VAR_FUNC)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001540 name = tv->vval.v_string;
Bram Moolenaar95006e32020-08-29 17:47:08 +02001541 if (name != NULL)
1542 {
Bram Moolenaar0917e862023-02-18 14:42:44 +00001543 char_u fname_buf[FLEN_FIXED + 1];
1544 char_u *tofree = NULL;
1545 funcerror_T error = FCERR_NONE;
1546 char_u *fname;
Bram Moolenaar95006e32020-08-29 17:47:08 +02001547
1548 // May need to translate <SNR>123_ to K_SNR.
1549 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
1550 if (error != FCERR_NONE)
1551 res = FAIL;
1552 else
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001553 res = call_by_name(fname, argcount, ectx, NULL, selfdict);
Bram Moolenaar95006e32020-08-29 17:47:08 +02001554 vim_free(tofree);
1555 }
1556
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +01001557 if (res == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001558 {
1559 if (called_emsg == called_emsg_before)
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001560 emsg_funcname(e_unknown_function_str,
Bram Moolenaar015f4262020-05-05 21:25:22 +02001561 name == NULL ? (char_u *)"[unknown]" : name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001562 return FAIL;
1563 }
1564 return OK;
1565}
1566
1567/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001568 * Check if "lock" is VAR_LOCKED or VAR_FIXED. If so give an error and return
1569 * TRUE.
1570 */
1571 static int
1572error_if_locked(int lock, char *error)
1573{
1574 if (lock & (VAR_LOCKED | VAR_FIXED))
1575 {
1576 emsg(_(error));
1577 return TRUE;
1578 }
1579 return FALSE;
1580}
1581
1582/*
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001583 * Give an error if "tv" is not a number and return FAIL.
1584 */
1585 static int
1586check_for_number(typval_T *tv)
1587{
1588 if (tv->v_type != VAR_NUMBER)
1589 {
1590 semsg(_(e_expected_str_but_got_str),
1591 vartype_name(VAR_NUMBER), vartype_name(tv->v_type));
1592 return FAIL;
1593 }
1594 return OK;
1595}
1596
1597/*
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001598 * Store "tv" in variable "name".
1599 * This is for s: and g: variables.
1600 */
1601 static void
1602store_var(char_u *name, typval_T *tv)
1603{
1604 funccal_entry_T entry;
Bram Moolenaard877a572021-04-01 19:42:48 +02001605 int flags = ASSIGN_DECL;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001606
Bram Moolenaard877a572021-04-01 19:42:48 +02001607 if (tv->v_lock)
1608 flags |= ASSIGN_CONST;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001609 save_funccal(&entry);
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001610 set_var_const(name, 0, NULL, tv, FALSE, flags, 0);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001611 restore_funccal();
1612}
1613
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001614/*
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001615 * Convert "tv" to a string.
1616 * Return FAIL if not allowed.
1617 */
1618 static int
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001619do_2string(typval_T *tv, int is_2string_any, int tolerant)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001620{
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00001621 if (tv->v_type == VAR_STRING)
1622 return OK;
1623
1624 char_u *str;
1625
1626 if (is_2string_any)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001627 {
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00001628 switch (tv->v_type)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001629 {
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00001630 case VAR_SPECIAL:
1631 case VAR_BOOL:
1632 case VAR_NUMBER:
1633 case VAR_FLOAT:
1634 case VAR_BLOB: break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001635
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00001636 case VAR_LIST:
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001637 if (tolerant)
1638 {
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001639 char_u *s, *e, *p;
1640 garray_T ga;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001641
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001642 ga_init2(&ga, sizeof(char_u *), 1);
1643
1644 // Convert to NL separated items, then
1645 // escape the items and replace the NL with
1646 // a space.
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001647 str = typval2string(tv, TRUE);
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001648 if (str == NULL)
1649 return FAIL;
1650 s = str;
1651 while ((e = vim_strchr(s, '\n')) != NULL)
1652 {
1653 *e = NUL;
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +01001654 p = vim_strsave_fnameescape(s,
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00001655 VSE_NONE);
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001656 if (p != NULL)
1657 {
1658 ga_concat(&ga, p);
1659 ga_concat(&ga, (char_u *)" ");
1660 vim_free(p);
1661 }
1662 s = e + 1;
1663 }
1664 vim_free(str);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001665 clear_tv(tv);
1666 tv->v_type = VAR_STRING;
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001667 tv->vval.v_string = ga.ga_data;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001668 return OK;
1669 }
1670 // FALLTHROUGH
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00001671 default: to_string_error(tv->v_type);
1672 return FAIL;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001673 }
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001674 }
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00001675 str = typval_tostring(tv, TRUE);
1676 clear_tv(tv);
1677 tv->v_type = VAR_STRING;
1678 tv->vval.v_string = str;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001679 return OK;
1680}
1681
1682/*
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001683 * When the value of "sv" is a null list of dict, allocate it.
1684 */
1685 static void
Bram Moolenaar859cc212022-03-28 15:22:35 +01001686allocate_if_null(svar_T *sv)
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001687{
Bram Moolenaar859cc212022-03-28 15:22:35 +01001688 typval_T *tv = sv->sv_tv;
1689
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001690 switch (tv->v_type)
1691 {
1692 case VAR_LIST:
Bram Moolenaar859cc212022-03-28 15:22:35 +01001693 if (tv->vval.v_list == NULL && sv->sv_type != &t_list_empty)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001694 (void)rettv_list_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001695 break;
1696 case VAR_DICT:
Bram Moolenaar859cc212022-03-28 15:22:35 +01001697 if (tv->vval.v_dict == NULL && sv->sv_type != &t_dict_empty)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001698 (void)rettv_dict_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001699 break;
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02001700 case VAR_BLOB:
Bram Moolenaar859cc212022-03-28 15:22:35 +01001701 if (tv->vval.v_blob == NULL && sv->sv_type != &t_blob_null)
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02001702 (void)rettv_blob_alloc(tv);
1703 break;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001704 default:
1705 break;
1706 }
1707}
Bram Moolenaard3aac292020-04-19 14:32:17 +02001708
Bram Moolenaare7525c52021-01-09 13:20:37 +01001709/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001710 * Return the character "str[index]" where "index" is the character index,
1711 * including composing characters.
1712 * If "index" is out of range NULL is returned.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001713 */
1714 char_u *
1715char_from_string(char_u *str, varnumber_T index)
1716{
1717 size_t nbyte = 0;
1718 varnumber_T nchar = index;
1719 size_t slen;
1720
1721 if (str == NULL)
1722 return NULL;
1723 slen = STRLEN(str);
1724
Bram Moolenaarff871402021-03-26 13:34:05 +01001725 // Do the same as for a list: a negative index counts from the end.
1726 // Optimization to check the first byte to be below 0x80 (and no composing
1727 // character follows) makes this a lot faster.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001728 if (index < 0)
1729 {
1730 int clen = 0;
1731
1732 for (nbyte = 0; nbyte < slen; ++clen)
Bram Moolenaarff871402021-03-26 13:34:05 +01001733 {
1734 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1735 ++nbyte;
1736 else if (enc_utf8)
1737 nbyte += utfc_ptr2len(str + nbyte);
1738 else
1739 nbyte += mb_ptr2len(str + nbyte);
1740 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001741 nchar = clen + index;
1742 if (nchar < 0)
1743 // unlike list: index out of range results in empty string
1744 return NULL;
1745 }
1746
1747 for (nbyte = 0; nchar > 0 && nbyte < slen; --nchar)
Bram Moolenaarff871402021-03-26 13:34:05 +01001748 {
1749 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1750 ++nbyte;
1751 else if (enc_utf8)
1752 nbyte += utfc_ptr2len(str + nbyte);
1753 else
1754 nbyte += mb_ptr2len(str + nbyte);
1755 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001756 if (nbyte >= slen)
1757 return NULL;
Bram Moolenaar0289a092021-03-14 18:40:19 +01001758 return vim_strnsave(str + nbyte, mb_ptr2len(str + nbyte));
Bram Moolenaare7525c52021-01-09 13:20:37 +01001759}
1760
1761/*
1762 * Get the byte index for character index "idx" in string "str" with length
Bram Moolenaar0289a092021-03-14 18:40:19 +01001763 * "str_len". Composing characters are included.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001764 * If going over the end return "str_len".
1765 * If "idx" is negative count from the end, -1 is the last character.
1766 * When going over the start return -1.
1767 */
1768 static long
1769char_idx2byte(char_u *str, size_t str_len, varnumber_T idx)
1770{
1771 varnumber_T nchar = idx;
1772 size_t nbyte = 0;
1773
1774 if (nchar >= 0)
1775 {
1776 while (nchar > 0 && nbyte < str_len)
1777 {
Bram Moolenaar0289a092021-03-14 18:40:19 +01001778 nbyte += mb_ptr2len(str + nbyte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001779 --nchar;
1780 }
1781 }
1782 else
1783 {
1784 nbyte = str_len;
1785 while (nchar < 0 && nbyte > 0)
1786 {
1787 --nbyte;
1788 nbyte -= mb_head_off(str, str + nbyte);
1789 ++nchar;
1790 }
1791 if (nchar < 0)
1792 return -1;
1793 }
1794 return (long)nbyte;
1795}
1796
1797/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001798 * Return the slice "str[first : last]" using character indexes. Composing
1799 * characters are included.
Bram Moolenaar6601b622021-01-13 21:47:15 +01001800 * "exclusive" is TRUE for slice().
Bram Moolenaare7525c52021-01-09 13:20:37 +01001801 * Return NULL when the result is empty.
1802 */
1803 char_u *
Bram Moolenaar6601b622021-01-13 21:47:15 +01001804string_slice(char_u *str, varnumber_T first, varnumber_T last, int exclusive)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001805{
1806 long start_byte, end_byte;
1807 size_t slen;
1808
1809 if (str == NULL)
1810 return NULL;
1811 slen = STRLEN(str);
1812 start_byte = char_idx2byte(str, slen, first);
1813 if (start_byte < 0)
1814 start_byte = 0; // first index very negative: use zero
Bram Moolenaar6601b622021-01-13 21:47:15 +01001815 if ((last == -1 && !exclusive) || last == VARNUM_MAX)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001816 end_byte = (long)slen;
1817 else
1818 {
1819 end_byte = char_idx2byte(str, slen, last);
Bram Moolenaar6601b622021-01-13 21:47:15 +01001820 if (!exclusive && end_byte >= 0 && end_byte < (long)slen)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001821 // end index is inclusive
Bram Moolenaar0289a092021-03-14 18:40:19 +01001822 end_byte += mb_ptr2len(str + end_byte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001823 }
1824
1825 if (start_byte >= (long)slen || end_byte <= start_byte)
1826 return NULL;
1827 return vim_strnsave(str + start_byte, end_byte - start_byte);
1828}
1829
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001830/*
1831 * Get a script variable for ISN_STORESCRIPT and ISN_LOADSCRIPT.
1832 * When "dfunc_idx" is negative don't give an error.
1833 * Returns NULL for an error.
1834 */
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001835 static svar_T *
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001836get_script_svar(scriptref_T *sref, int dfunc_idx)
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001837{
1838 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001839 dfunc_T *dfunc = dfunc_idx < 0 ? NULL
1840 : ((dfunc_T *)def_functions.ga_data) + dfunc_idx;
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001841 svar_T *sv;
1842
1843 if (sref->sref_seq != si->sn_script_seq)
1844 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001845 // The script was reloaded after the function was compiled, the
1846 // script_idx may not be valid.
1847 if (dfunc != NULL)
1848 semsg(_(e_script_variable_invalid_after_reload_in_function_str),
1849 printable_func_name(dfunc->df_ufunc));
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001850 return NULL;
1851 }
1852 sv = ((svar_T *)si->sn_var_vals.ga_data) + sref->sref_idx;
Bram Moolenaar6de22962022-09-09 21:35:36 +01001853 if (sv->sv_name == NULL)
1854 {
1855 if (dfunc != NULL)
1856 emsg(_(e_script_variable_was_deleted));
1857 return NULL;
1858 }
Bram Moolenaar60dc8272021-07-29 22:48:54 +02001859 if (!equal_type(sv->sv_type, sref->sref_type, 0))
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001860 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001861 if (dfunc != NULL)
1862 emsg(_(e_script_variable_type_changed));
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001863 return NULL;
1864 }
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001865
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01001866 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0
1867 && sref->sref_sid != current_sctx.sc_sid)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001868 {
1869 if (dfunc != NULL)
1870 semsg(_(e_item_not_exported_in_script_str), sv->sv_name);
1871 return NULL;
1872 }
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001873 return sv;
1874}
1875
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001876/*
Bram Moolenaar20677332021-06-06 17:02:53 +02001877 * Function passed to do_cmdline() for splitting a script joined by NL
1878 * characters.
1879 */
1880 static char_u *
1881get_split_sourceline(
1882 int c UNUSED,
1883 void *cookie,
1884 int indent UNUSED,
1885 getline_opt_T options UNUSED)
1886{
1887 source_cookie_T *sp = (source_cookie_T *)cookie;
1888 char_u *p;
1889 char_u *line;
1890
Bram Moolenaar20677332021-06-06 17:02:53 +02001891 p = vim_strchr(sp->nextline, '\n');
1892 if (p == NULL)
1893 {
1894 line = vim_strsave(sp->nextline);
1895 sp->nextline += STRLEN(sp->nextline);
1896 }
1897 else
1898 {
1899 line = vim_strnsave(sp->nextline, p - sp->nextline);
1900 sp->nextline = p + 1;
1901 }
1902 return line;
1903}
1904
1905/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001906 * Execute a function by "name".
1907 * This can be a builtin function, user function or a funcref.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001908 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001909 */
1910 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001911call_eval_func(
1912 char_u *name,
1913 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001914 ectx_T *ectx,
1915 isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001916{
Bram Moolenaared677f52020-08-12 16:38:10 +02001917 int called_emsg_before = called_emsg;
1918 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001919
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001920 res = call_by_name(name, argcount, ectx, iptr, NULL);
Bram Moolenaared677f52020-08-12 16:38:10 +02001921 if (res == FAIL && called_emsg == called_emsg_before)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001922 {
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001923 dictitem_T *v;
1924
1925 v = find_var(name, NULL, FALSE);
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001926 if (v == NULL || (v->di_tv.v_type != VAR_PARTIAL
1927 && v->di_tv.v_type != VAR_FUNC))
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001928 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001929 emsg_funcname(e_unknown_function_str, name);
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001930 return FAIL;
1931 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001932 return call_partial(&v->di_tv, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001933 }
Bram Moolenaared677f52020-08-12 16:38:10 +02001934 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001935}
1936
1937/*
Bram Moolenaarf112f302020-12-20 17:47:52 +01001938 * When a function reference is used, fill a partial with the information
1939 * needed, especially when it is used as a closure.
1940 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001941 int
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001942fill_partial_and_closure(
Bram Moolenaarcc341812022-09-19 15:54:34 +01001943 partial_T *pt,
1944 ufunc_T *ufunc,
1945 loopvarinfo_T *lvi,
1946 ectx_T *ectx)
Bram Moolenaarf112f302020-12-20 17:47:52 +01001947{
1948 pt->pt_func = ufunc;
1949 pt->pt_refcount = 1;
1950
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001951 if (ufunc->uf_flags & FC_CLOSURE)
Bram Moolenaarf112f302020-12-20 17:47:52 +01001952 {
1953 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1954 + ectx->ec_dfunc_idx;
1955
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001956 // The closure may need to find arguments and local variables of the
1957 // current function in the stack.
Bram Moolenaar0186e582021-01-10 18:33:11 +01001958 pt->pt_outer.out_stack = &ectx->ec_stack;
1959 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001960 if (ectx->ec_outer_ref != NULL)
1961 {
1962 // The current context already has a context, link to that one.
1963 pt->pt_outer.out_up = ectx->ec_outer_ref->or_outer;
1964 if (ectx->ec_outer_ref->or_partial != NULL)
1965 {
1966 pt->pt_outer.out_up_partial = ectx->ec_outer_ref->or_partial;
1967 ++pt->pt_outer.out_up_partial->pt_refcount;
1968 }
1969 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01001970
Bram Moolenaarcc341812022-09-19 15:54:34 +01001971 if (lvi != NULL)
1972 {
1973 int depth;
1974
1975 // The closure may need to find variables defined inside a loop,
1976 // for every nested loop. A new reference is made every time,
1977 // ISN_ENDLOOP will check if they are actually used.
1978 for (depth = 0; depth < lvi->lvi_depth; ++depth)
1979 {
1980 pt->pt_outer.out_loop[depth].stack = &ectx->ec_stack;
1981 pt->pt_outer.out_loop[depth].var_idx = ectx->ec_frame_idx
1982 + STACK_FRAME_SIZE + lvi->lvi_loop[depth].var_idx;
1983 pt->pt_outer.out_loop[depth].var_count =
1984 lvi->lvi_loop[depth].var_count;
1985 }
Bram Moolenaar6d313be2022-09-22 16:36:25 +01001986 pt->pt_outer.out_loop_size = lvi->lvi_depth;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001987 }
Bram Moolenaar6d313be2022-09-22 16:36:25 +01001988 else
1989 pt->pt_outer.out_loop_size = 0;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001990
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001991 // If the function currently executing returns and the closure is still
1992 // being referenced, we need to make a copy of the context (arguments
1993 // and local variables) so that the closure can use it later.
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001994 // Store a reference to the partial so we can handle that.
Bram Moolenaar35578162021-08-02 19:10:38 +02001995 if (GA_GROW_FAILS(&ectx->ec_funcrefs, 1))
Bram Moolenaarf112f302020-12-20 17:47:52 +01001996 {
1997 vim_free(pt);
1998 return FAIL;
1999 }
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02002000 // Extra variable keeps the count of closures created in the current
2001 // function call.
Bram Moolenaarf112f302020-12-20 17:47:52 +01002002 ++(((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx
2003 + STACK_FRAME_SIZE + dfunc->df_varcount)->vval.v_number;
2004
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002005 ((partial_T **)ectx->ec_funcrefs.ga_data)[ectx->ec_funcrefs.ga_len]
2006 = pt;
Bram Moolenaarf112f302020-12-20 17:47:52 +01002007 ++pt->pt_refcount;
2008 ++ectx->ec_funcrefs.ga_len;
2009 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002010 ++ufunc->uf_refcount;
Bram Moolenaarf112f302020-12-20 17:47:52 +01002011 return OK;
2012}
2013
Bram Moolenaaraacc9662021-08-13 19:40:51 +02002014/*
2015 * Execute iptr->isn_arg.string as an Ex command.
2016 */
2017 static int
Ernie Raelee865f32023-09-29 19:53:55 +02002018exec_command(isn_T *iptr, char_u *cmd_string)
Bram Moolenaaraacc9662021-08-13 19:40:51 +02002019{
2020 source_cookie_T cookie;
2021
2022 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00002023 // Pass getsourceline to get an error for a missing ":end" command.
Bram Moolenaaraacc9662021-08-13 19:40:51 +02002024 CLEAR_FIELD(cookie);
2025 cookie.sourcing_lnum = iptr->isn_lnum - 1;
Ernie Raelee865f32023-09-29 19:53:55 +02002026 if (do_cmdline(cmd_string, getsourceline, &cookie,
Bram Moolenaaraacc9662021-08-13 19:40:51 +02002027 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED) == FAIL
2028 || did_emsg)
2029 return FAIL;
2030 return OK;
2031}
2032
Bram Moolenaar89445512022-04-14 12:58:23 +01002033/*
2034 * If script "sid" is not loaded yet then load it now.
2035 * Caller must make sure "sid" is a valid script ID.
2036 * "loaded" is set to TRUE if the script had to be loaded.
2037 * Returns FAIL if loading fails, OK if already loaded or loaded now.
2038 */
2039 int
2040may_load_script(int sid, int *loaded)
2041{
2042 scriptitem_T *si = SCRIPT_ITEM(sid);
2043
2044 if (si->sn_state == SN_STATE_NOT_LOADED)
2045 {
2046 *loaded = TRUE;
2047 if (do_source(si->sn_name, FALSE, DOSO_NONE, NULL) == FAIL)
2048 {
2049 semsg(_(e_cant_open_file_str), si->sn_name);
2050 return FAIL;
2051 }
2052 }
2053 return OK;
2054}
2055
Bram Moolenaarf18332f2021-05-07 17:55:55 +02002056// used for v_instr of typval of VAR_INSTR
2057struct instr_S {
2058 ectx_T *instr_ectx;
2059 isn_T *instr_instr;
2060};
2061
Bram Moolenaar4c137212021-04-19 16:48:48 +02002062// used for substitute_instr
2063typedef struct subs_expr_S {
2064 ectx_T *subs_ectx;
2065 isn_T *subs_instr;
2066 int subs_status;
2067} subs_expr_T;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002068
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002069// Set when calling do_debug().
2070static ectx_T *debug_context = NULL;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02002071static int debug_var_count;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002072
2073/*
2074 * When debugging lookup "name" and return the typeval.
2075 * When not found return NULL.
2076 */
2077 typval_T *
2078lookup_debug_var(char_u *name)
2079{
2080 int idx;
2081 dfunc_T *dfunc;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02002082 ufunc_T *ufunc;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002083 ectx_T *ectx = debug_context;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02002084 int varargs_off;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002085
2086 if (ectx == NULL)
2087 return NULL;
2088 dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
2089
2090 // Go through the local variable names, from last to first.
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02002091 for (idx = debug_var_count - 1; idx >= 0; --idx)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002092 {
Bram Moolenaare406ff82022-03-10 20:47:43 +00002093 char_u *varname = ((char_u **)dfunc->df_var_names.ga_data)[idx];
2094
2095 // the variable name may be NULL when not available in this block
2096 if (varname != NULL && STRCMP(varname, name) == 0)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002097 return STACK_TV_VAR(idx);
2098 }
2099
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02002100 // Go through argument names.
2101 ufunc = dfunc->df_ufunc;
2102 varargs_off = ufunc->uf_va_name == NULL ? 0 : 1;
2103 for (idx = 0; idx < ufunc->uf_args.ga_len; ++idx)
2104 if (STRCMP(((char_u **)(ufunc->uf_args.ga_data))[idx], name) == 0)
2105 return STACK_TV(ectx->ec_frame_idx - ufunc->uf_args.ga_len
2106 - varargs_off + idx);
2107 if (ufunc->uf_va_name != NULL && STRCMP(ufunc->uf_va_name, name) == 0)
2108 return STACK_TV(ectx->ec_frame_idx - 1);
2109
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002110 return NULL;
2111}
2112
Bram Moolenaar26a44842021-09-02 18:49:06 +02002113/*
2114 * Return TRUE if there might be a breakpoint in "ufunc", which is when a
2115 * breakpoint was set in that function or when there is any expression.
2116 */
2117 int
2118may_break_in_function(ufunc_T *ufunc)
2119{
2120 return ufunc->uf_has_breakpoint || debug_has_expr_breakpoint();
2121}
2122
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002123 static void
2124handle_debug(isn_T *iptr, ectx_T *ectx)
2125{
2126 char_u *line;
2127 ufunc_T *ufunc = (((dfunc_T *)def_functions.ga_data)
2128 + ectx->ec_dfunc_idx)->df_ufunc;
2129 isn_T *ni;
2130 int end_lnum = iptr->isn_lnum;
2131 garray_T ga;
2132 int lnum;
2133
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02002134 if (ex_nesting_level > debug_break_level)
2135 {
2136 linenr_T breakpoint;
2137
Bram Moolenaar26a44842021-09-02 18:49:06 +02002138 if (!may_break_in_function(ufunc))
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02002139 return;
2140
2141 // check for the next breakpoint if needed
2142 breakpoint = dbg_find_breakpoint(FALSE, ufunc->uf_name,
Bram Moolenaar8cec9272021-06-23 20:20:53 +02002143 iptr->isn_arg.debug.dbg_break_lnum);
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02002144 if (breakpoint <= 0 || breakpoint > iptr->isn_lnum)
2145 return;
2146 }
2147
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002148 SOURCING_LNUM = iptr->isn_lnum;
2149 debug_context = ectx;
Bram Moolenaar8cec9272021-06-23 20:20:53 +02002150 debug_var_count = iptr->isn_arg.debug.dbg_var_names_len;
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002151
2152 for (ni = iptr + 1; ni->isn_type != ISN_FINISH; ++ni)
2153 if (ni->isn_type == ISN_DEBUG
2154 || ni->isn_type == ISN_RETURN
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002155 || ni->isn_type == ISN_RETURN_OBJECT
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002156 || ni->isn_type == ISN_RETURN_VOID)
2157 {
Bram Moolenaar112bed02021-11-23 22:16:34 +00002158 end_lnum = ni->isn_lnum + (ni->isn_type == ISN_DEBUG ? 0 : 1);
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002159 break;
2160 }
2161
2162 if (end_lnum > iptr->isn_lnum)
2163 {
2164 ga_init2(&ga, sizeof(char_u *), 10);
Bram Moolenaar310091d2021-12-23 21:14:37 +00002165 for (lnum = iptr->isn_lnum; lnum < end_lnum
2166 && lnum <= ufunc->uf_lines.ga_len; ++lnum)
Bram Moolenaar59b50c32021-06-17 22:27:48 +02002167 {
Bram Moolenaar303215d2021-07-07 20:10:43 +02002168 char_u *p = ((char_u **)ufunc->uf_lines.ga_data)[lnum - 1];
Bram Moolenaar59b50c32021-06-17 22:27:48 +02002169
Bram Moolenaar303215d2021-07-07 20:10:43 +02002170 if (p == NULL)
2171 continue; // left over from continuation line
2172 p = skipwhite(p);
Bram Moolenaar59b50c32021-06-17 22:27:48 +02002173 if (*p == '#')
2174 break;
Bram Moolenaar35578162021-08-02 19:10:38 +02002175 if (GA_GROW_OK(&ga, 1))
Bram Moolenaar59b50c32021-06-17 22:27:48 +02002176 ((char_u **)(ga.ga_data))[ga.ga_len++] = p;
2177 if (STRNCMP(p, "def ", 4) == 0)
2178 break;
2179 }
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002180 line = ga_concat_strings(&ga, " ");
2181 vim_free(ga.ga_data);
2182 }
2183 else
2184 line = ((char_u **)ufunc->uf_lines.ga_data)[iptr->isn_lnum - 1];
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002185
Dominique Pelle6e969552021-06-17 13:53:41 +02002186 do_debug(line == NULL ? (char_u *)"[empty]" : line);
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002187 debug_context = NULL;
2188
2189 if (end_lnum > iptr->isn_lnum)
2190 vim_free(line);
2191}
2192
Bram Moolenaar4c137212021-04-19 16:48:48 +02002193/*
Bram Moolenaar65b0d162022-12-13 18:43:22 +00002194 * Store a value in a list, dict, blob or object variable.
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002195 * Returns OK, FAIL or NOTDONE (uncatchable error).
2196 */
2197 static int
2198execute_storeindex(isn_T *iptr, ectx_T *ectx)
2199{
Bram Moolenaarf7d1c6e2023-01-16 20:47:57 +00002200 vartype_T dest_type = iptr->isn_arg.storeindex.si_vartype;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002201 typval_T *tv;
2202 typval_T *tv_idx = STACK_TV_BOT(-2);
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002203 long lidx = 0;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002204 typval_T *tv_dest = STACK_TV_BOT(-1);
2205 int status = OK;
2206
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002207 if (tv_idx->v_type == VAR_NUMBER)
2208 lidx = (long)tv_idx->vval.v_number;
2209
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002210 // Stack contains:
2211 // -3 value to be stored
2212 // -2 index
Yegappan Lakshmanan3775f772023-09-01 22:05:45 +02002213 // -1 dict, list, blob, object or class
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002214 tv = STACK_TV_BOT(-3);
2215 SOURCING_LNUM = iptr->isn_lnum;
Gianmaria Bajod7085a02023-08-31 18:15:26 +02002216
2217 // Make sure an object has been initialized
2218 if (dest_type == VAR_OBJECT && tv_dest->vval.v_object == NULL)
2219 {
2220 emsg(_(e_using_null_object));
2221 status = FAIL;
2222 }
2223 else if (dest_type == VAR_ANY)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002224 {
2225 dest_type = tv_dest->v_type;
2226 if (dest_type == VAR_DICT)
2227 status = do_2string(tv_idx, TRUE, FALSE);
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002228 else if (dest_type == VAR_OBJECT && tv_idx->v_type == VAR_STRING)
2229 {
2230 // Need to get the member index now that the class is known.
2231 object_T *obj = tv_dest->vval.v_object;
2232 class_T *cl = obj->obj_class;
2233 char_u *member = tv_idx->vval.v_string;
2234
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02002235 int m_idx;
2236 ocmember_T *m = object_member_lookup(cl, member, 0, &m_idx);
2237 if (m != NULL)
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002238 {
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02002239 if (*member == '_')
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002240 {
Ernie Raele6c9aa52023-10-06 19:55:52 +02002241 emsg_var_cl_define(e_cannot_access_private_variable_str,
2242 m->ocm_name, 0, cl);
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02002243 status = FAIL;
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002244 }
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002245
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02002246 lidx = m_idx;
2247 }
2248 else
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002249 {
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +02002250 member_not_found_msg(cl, VAR_OBJECT, member, 0);
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002251 status = FAIL;
2252 }
2253 }
2254 else if ((dest_type == VAR_LIST || dest_type == VAR_OBJECT)
2255 && tv_idx->v_type != VAR_NUMBER)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002256 {
2257 emsg(_(e_number_expected));
2258 status = FAIL;
2259 }
2260 }
Bram Moolenaare08be092022-02-17 13:08:26 +00002261
2262 if (status == OK)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002263 {
Bram Moolenaare08be092022-02-17 13:08:26 +00002264 if (dest_type == VAR_LIST)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002265 {
Bram Moolenaare08be092022-02-17 13:08:26 +00002266 list_T *list = tv_dest->vval.v_list;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002267
Bram Moolenaare08be092022-02-17 13:08:26 +00002268 if (list == NULL)
2269 {
2270 emsg(_(e_list_not_set));
2271 return FAIL;
2272 }
2273 if (lidx < 0 && list->lv_len + lidx >= 0)
2274 // negative index is relative to the end
2275 lidx = list->lv_len + lidx;
2276 if (lidx < 0 || lidx > list->lv_len)
2277 {
2278 semsg(_(e_list_index_out_of_range_nr), lidx);
2279 return FAIL;
2280 }
2281 if (lidx < list->lv_len)
2282 {
2283 listitem_T *li = list_find(list, lidx);
2284
2285 if (error_if_locked(li->li_tv.v_lock,
Bram Moolenaar70c43d82022-01-26 21:01:15 +00002286 e_cannot_change_locked_list_item))
Bram Moolenaare08be092022-02-17 13:08:26 +00002287 return FAIL;
2288 // overwrite existing list item
2289 clear_tv(&li->li_tv);
2290 li->li_tv = *tv;
2291 }
2292 else
2293 {
2294 if (error_if_locked(list->lv_lock, e_cannot_change_locked_list))
2295 return FAIL;
2296 // append to list, only fails when out of memory
2297 if (list_append_tv(list, tv) == FAIL)
2298 return NOTDONE;
2299 clear_tv(tv);
2300 }
2301 }
2302 else if (dest_type == VAR_DICT)
2303 {
2304 char_u *key = tv_idx->vval.v_string;
2305 dict_T *dict = tv_dest->vval.v_dict;
2306 dictitem_T *di;
2307
2308 SOURCING_LNUM = iptr->isn_lnum;
2309 if (dict == NULL)
2310 {
2311 emsg(_(e_dictionary_not_set));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002312 return FAIL;
Bram Moolenaare08be092022-02-17 13:08:26 +00002313 }
2314 if (key == NULL)
2315 key = (char_u *)"";
2316 di = dict_find(dict, key, -1);
2317 if (di != NULL)
2318 {
2319 if (error_if_locked(di->di_tv.v_lock,
2320 e_cannot_change_dict_item))
2321 return FAIL;
2322 // overwrite existing value
2323 clear_tv(&di->di_tv);
2324 di->di_tv = *tv;
2325 }
2326 else
2327 {
2328 if (error_if_locked(dict->dv_lock, e_cannot_change_dict))
2329 return FAIL;
2330 // add to dict, only fails when out of memory
2331 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
2332 return NOTDONE;
2333 clear_tv(tv);
2334 }
2335 }
2336 else if (dest_type == VAR_BLOB)
2337 {
Bram Moolenaare08be092022-02-17 13:08:26 +00002338 blob_T *blob = tv_dest->vval.v_blob;
Bram Moolenaar65b0d162022-12-13 18:43:22 +00002339 varnumber_T nr;
2340 int error = FALSE;
2341 int len;
Bram Moolenaare08be092022-02-17 13:08:26 +00002342
2343 if (blob == NULL)
2344 {
2345 emsg(_(e_blob_not_set));
2346 return FAIL;
2347 }
2348 len = blob_len(blob);
2349 if (lidx < 0 && len + lidx >= 0)
2350 // negative index is relative to the end
2351 lidx = len + lidx;
2352
2353 // Can add one byte at the end.
2354 if (lidx < 0 || lidx > len)
2355 {
2356 semsg(_(e_blob_index_out_of_range_nr), lidx);
2357 return FAIL;
2358 }
2359 if (value_check_lock(blob->bv_lock, (char_u *)"blob", FALSE))
2360 return FAIL;
2361 nr = tv_get_number_chk(tv, &error);
2362 if (error)
2363 return FAIL;
2364 blob_set_append(blob, lidx, nr);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002365 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002366 else if (dest_type == VAR_CLASS || dest_type == VAR_OBJECT)
2367 {
Yegappan Lakshmanan3775f772023-09-01 22:05:45 +02002368 typval_T *otv;
Bram Moolenaarf7d1c6e2023-01-16 20:47:57 +00002369
Yegappan Lakshmanan3775f772023-09-01 22:05:45 +02002370 if (dest_type == VAR_OBJECT)
2371 {
2372 object_T *obj = tv_dest->vval.v_object;
2373
2374 otv = (typval_T *)(obj + 1);
2375 class_T *itf = iptr->isn_arg.storeindex.si_class;
2376 if (itf != NULL)
2377 // convert interface member index to class member index
Ernie Rael18143d32023-09-04 22:30:41 +02002378 lidx = object_index_from_itf_index(itf, FALSE, lidx,
Yegappan Lakshmanan5a05d372023-09-29 19:43:11 +02002379 obj->obj_class);
Yegappan Lakshmanan3775f772023-09-01 22:05:45 +02002380 }
2381 else
2382 {
2383 // VAR_CLASS
2384 class_T *class = tv_dest->vval.v_class;
2385 otv = class->class_members_tv;
2386 }
Bram Moolenaarf7d1c6e2023-01-16 20:47:57 +00002387
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002388 clear_tv(&otv[lidx]);
2389 otv[lidx] = *tv;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002390 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002391 else
2392 {
Bram Moolenaare08be092022-02-17 13:08:26 +00002393 status = FAIL;
2394 semsg(_(e_cannot_index_str), vartype_name(dest_type));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002395 }
2396 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002397
2398 clear_tv(tv_idx);
2399 clear_tv(tv_dest);
2400 ectx->ec_stack.ga_len -= 3;
2401 if (status == FAIL)
2402 {
2403 clear_tv(tv);
2404 return FAIL;
2405 }
2406 return OK;
2407}
2408
2409/*
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002410 * Store a value in a list or blob range.
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002411 */
2412 static int
2413execute_storerange(isn_T *iptr, ectx_T *ectx)
2414{
2415 typval_T *tv;
2416 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2417 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2418 typval_T *tv_dest = STACK_TV_BOT(-1);
2419 int status = OK;
2420
2421 // Stack contains:
2422 // -4 value to be stored
2423 // -3 first index or "none"
2424 // -2 second index or "none"
2425 // -1 destination list or blob
2426 tv = STACK_TV_BOT(-4);
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002427 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002428 if (tv_dest->v_type == VAR_LIST)
2429 {
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002430 long n1;
2431 long n2;
2432 listitem_T *li1;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002433
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002434 n1 = (long)tv_get_number_chk(tv_idx1, NULL);
2435 if (tv_idx2->v_type == VAR_SPECIAL
2436 && tv_idx2->vval.v_number == VVAL_NONE)
2437 n2 = list_len(tv_dest->vval.v_list) - 1;
2438 else
2439 n2 = (long)tv_get_number_chk(tv_idx2, NULL);
2440
Bram Moolenaar22ebd172022-04-01 15:26:58 +01002441 li1 = check_range_index_one(tv_dest->vval.v_list, &n1, TRUE, FALSE);
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002442 if (li1 == NULL)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002443 status = FAIL;
2444 else
2445 {
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002446 status = check_range_index_two(tv_dest->vval.v_list,
2447 &n1, li1, &n2, FALSE);
2448 if (status != FAIL)
2449 status = list_assign_range(
2450 tv_dest->vval.v_list,
2451 tv->vval.v_list,
2452 n1,
2453 n2,
2454 tv_idx2->v_type == VAR_SPECIAL,
2455 (char_u *)"=",
2456 (char_u *)"[unknown]");
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002457 }
2458 }
2459 else if (tv_dest->v_type == VAR_BLOB)
2460 {
2461 varnumber_T n1;
2462 varnumber_T n2;
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002463 long bloblen;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002464
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002465 n1 = tv_get_number_chk(tv_idx1, NULL);
2466 if (tv_idx2->v_type == VAR_SPECIAL
2467 && tv_idx2->vval.v_number == VVAL_NONE)
2468 n2 = blob_len(tv_dest->vval.v_blob) - 1;
2469 else
2470 n2 = tv_get_number_chk(tv_idx2, NULL);
2471 bloblen = blob_len(tv_dest->vval.v_blob);
2472
2473 if (check_blob_index(bloblen, n1, FALSE) == FAIL
2474 || check_blob_range(bloblen, n1, n2, FALSE) == FAIL)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002475 status = FAIL;
2476 else
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002477 status = blob_set_range(tv_dest->vval.v_blob, n1, n2, tv);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002478 }
2479 else
2480 {
2481 status = FAIL;
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002482 emsg(_(e_list_or_blob_required));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002483 }
2484
2485 clear_tv(tv_idx1);
2486 clear_tv(tv_idx2);
2487 clear_tv(tv_dest);
2488 ectx->ec_stack.ga_len -= 4;
2489 clear_tv(tv);
2490
2491 return status;
2492}
2493
2494/*
2495 * Unlet item in list or dict variable.
2496 */
2497 static int
2498execute_unletindex(isn_T *iptr, ectx_T *ectx)
2499{
2500 typval_T *tv_idx = STACK_TV_BOT(-2);
2501 typval_T *tv_dest = STACK_TV_BOT(-1);
2502 int status = OK;
2503
2504 // Stack contains:
2505 // -2 index
2506 // -1 dict or list
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002507 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002508 if (tv_dest->v_type == VAR_DICT)
2509 {
2510 // unlet a dict item, index must be a string
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002511 if (tv_idx->v_type != VAR_STRING && tv_idx->v_type != VAR_NUMBER)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002512 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002513 semsg(_(e_expected_str_but_got_str),
2514 vartype_name(VAR_STRING),
2515 vartype_name(tv_idx->v_type));
2516 status = FAIL;
2517 }
2518 else
2519 {
2520 dict_T *d = tv_dest->vval.v_dict;
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002521 char_u *key;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002522 dictitem_T *di = NULL;
2523
2524 if (d != NULL && value_check_lock(
2525 d->dv_lock, NULL, FALSE))
2526 status = FAIL;
2527 else
2528 {
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002529 if (tv_idx->v_type == VAR_STRING)
2530 {
2531 key = tv_idx->vval.v_string;
2532 if (key == NULL)
2533 key = (char_u *)"";
2534 }
2535 else
2536 {
2537 key = tv_get_string(tv_idx);
2538 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002539 if (d != NULL)
2540 di = dict_find(d, key, (int)STRLEN(key));
2541 if (di == NULL)
2542 {
2543 // NULL dict is equivalent to empty dict
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00002544 semsg(_(e_key_not_present_in_dictionary_str), key);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002545 status = FAIL;
2546 }
2547 else if (var_check_fixed(di->di_flags,
2548 NULL, FALSE)
2549 || var_check_ro(di->di_flags,
2550 NULL, FALSE))
2551 status = FAIL;
2552 else
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002553 dictitem_remove(d, di, "unlet");
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002554 }
2555 }
2556 }
2557 else if (tv_dest->v_type == VAR_LIST)
2558 {
2559 // unlet a List item, index must be a number
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002560 if (check_for_number(tv_idx) == FAIL)
2561 {
2562 status = FAIL;
2563 }
2564 else
2565 {
2566 list_T *l = tv_dest->vval.v_list;
2567 long n = (long)tv_idx->vval.v_number;
2568
2569 if (l != NULL && value_check_lock(
2570 l->lv_lock, NULL, FALSE))
2571 status = FAIL;
2572 else
2573 {
2574 listitem_T *li = list_find(l, n);
2575
2576 if (li == NULL)
2577 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002578 semsg(_(e_list_index_out_of_range_nr), n);
2579 status = FAIL;
2580 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002581 else
2582 listitem_remove(l, li);
2583 }
2584 }
2585 }
2586 else
2587 {
2588 status = FAIL;
2589 semsg(_(e_cannot_index_str),
2590 vartype_name(tv_dest->v_type));
2591 }
2592
2593 clear_tv(tv_idx);
2594 clear_tv(tv_dest);
2595 ectx->ec_stack.ga_len -= 2;
2596
2597 return status;
2598}
2599
2600/*
2601 * Unlet a range of items in a list variable.
2602 */
2603 static int
2604execute_unletrange(isn_T *iptr, ectx_T *ectx)
2605{
2606 // Stack contains:
2607 // -3 index1
2608 // -2 index2
2609 // -1 dict or list
2610 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2611 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2612 typval_T *tv_dest = STACK_TV_BOT(-1);
2613 int status = OK;
2614
2615 if (tv_dest->v_type == VAR_LIST)
2616 {
2617 // indexes must be a number
2618 SOURCING_LNUM = iptr->isn_lnum;
2619 if (check_for_number(tv_idx1) == FAIL
2620 || (tv_idx2->v_type != VAR_SPECIAL
2621 && check_for_number(tv_idx2) == FAIL))
2622 {
2623 status = FAIL;
2624 }
2625 else
2626 {
2627 list_T *l = tv_dest->vval.v_list;
2628 long n1 = (long)tv_idx1->vval.v_number;
2629 long n2 = tv_idx2->v_type == VAR_SPECIAL
2630 ? 0 : (long)tv_idx2->vval.v_number;
2631 listitem_T *li;
2632
2633 li = list_find_index(l, &n1);
2634 if (li == NULL)
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002635 {
2636 semsg(_(e_list_index_out_of_range_nr),
2637 (long)tv_idx1->vval.v_number);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002638 status = FAIL;
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002639 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002640 else
2641 {
2642 if (n1 < 0)
2643 n1 = list_idx_of_item(l, li);
2644 if (n2 < 0)
2645 {
2646 listitem_T *li2 = list_find(l, n2);
2647
2648 if (li2 == NULL)
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002649 {
2650 semsg(_(e_list_index_out_of_range_nr), n2);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002651 status = FAIL;
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002652 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002653 else
2654 n2 = list_idx_of_item(l, li2);
2655 }
2656 if (status != FAIL
2657 && tv_idx2->v_type != VAR_SPECIAL
2658 && n2 < n1)
2659 {
2660 semsg(_(e_list_index_out_of_range_nr), n2);
2661 status = FAIL;
2662 }
Bram Moolenaar6b8c7ba2022-03-20 17:46:06 +00002663 if (status != FAIL)
2664 list_unlet_range(l, li, n1,
2665 tv_idx2->v_type != VAR_SPECIAL, n2);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002666 }
2667 }
2668 }
2669 else
2670 {
2671 status = FAIL;
2672 SOURCING_LNUM = iptr->isn_lnum;
2673 semsg(_(e_cannot_index_str),
2674 vartype_name(tv_dest->v_type));
2675 }
2676
2677 clear_tv(tv_idx1);
2678 clear_tv(tv_idx2);
2679 clear_tv(tv_dest);
2680 ectx->ec_stack.ga_len -= 3;
2681
2682 return status;
2683}
2684
2685/*
2686 * Top of a for loop.
2687 */
2688 static int
2689execute_for(isn_T *iptr, ectx_T *ectx)
2690{
2691 typval_T *tv;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002692 int jump = FALSE;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002693 typval_T *ltv = STACK_TV_BOT(-1);
2694 typval_T *idxtv =
Bram Moolenaarcc341812022-09-19 15:54:34 +01002695 STACK_TV_VAR(iptr->isn_arg.forloop.for_loop_idx);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002696
2697 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
2698 return FAIL;
2699 if (ltv->v_type == VAR_LIST)
2700 {
2701 list_T *list = ltv->vval.v_list;
2702
2703 // push the next item from the list
2704 ++idxtv->vval.v_number;
2705 if (list == NULL
2706 || idxtv->vval.v_number >= list->lv_len)
2707 {
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002708 jump = TRUE;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002709 }
2710 else if (list->lv_first == &range_list_item)
2711 {
2712 // non-materialized range() list
2713 tv = STACK_TV_BOT(0);
2714 tv->v_type = VAR_NUMBER;
2715 tv->v_lock = 0;
2716 tv->vval.v_number = list_find_nr(
2717 list, idxtv->vval.v_number, NULL);
2718 ++ectx->ec_stack.ga_len;
2719 }
2720 else
2721 {
2722 listitem_T *li = list_find(list,
2723 idxtv->vval.v_number);
2724
2725 copy_tv(&li->li_tv, STACK_TV_BOT(0));
2726 ++ectx->ec_stack.ga_len;
2727 }
2728 }
2729 else if (ltv->v_type == VAR_STRING)
2730 {
2731 char_u *str = ltv->vval.v_string;
2732
2733 // The index is for the last byte of the previous
2734 // character.
2735 ++idxtv->vval.v_number;
2736 if (str == NULL || str[idxtv->vval.v_number] == NUL)
2737 {
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002738 jump = TRUE;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002739 }
2740 else
2741 {
2742 int clen = mb_ptr2len(str + idxtv->vval.v_number);
2743
2744 // Push the next character from the string.
2745 tv = STACK_TV_BOT(0);
2746 tv->v_type = VAR_STRING;
2747 tv->vval.v_string = vim_strnsave(
2748 str + idxtv->vval.v_number, clen);
2749 ++ectx->ec_stack.ga_len;
2750 idxtv->vval.v_number += clen - 1;
2751 }
2752 }
2753 else if (ltv->v_type == VAR_BLOB)
2754 {
2755 blob_T *blob = ltv->vval.v_blob;
2756
2757 // When we get here the first time make a copy of the
2758 // blob, so that the iteration still works when it is
2759 // changed.
2760 if (idxtv->vval.v_number == -1 && blob != NULL)
2761 {
2762 blob_copy(blob, ltv);
2763 blob_unref(blob);
2764 blob = ltv->vval.v_blob;
2765 }
2766
2767 // The index is for the previous byte.
2768 ++idxtv->vval.v_number;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002769 if (blob == NULL || idxtv->vval.v_number >= blob_len(blob))
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002770 {
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002771 jump = TRUE;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002772 }
2773 else
2774 {
2775 // Push the next byte from the blob.
2776 tv = STACK_TV_BOT(0);
2777 tv->v_type = VAR_NUMBER;
2778 tv->vval.v_number = blob_get(blob,
2779 idxtv->vval.v_number);
2780 ++ectx->ec_stack.ga_len;
2781 }
2782 }
2783 else
2784 {
2785 semsg(_(e_for_loop_on_str_not_supported),
2786 vartype_name(ltv->v_type));
2787 return FAIL;
2788 }
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002789
2790 if (jump)
2791 {
2792 // past the end of the list/string/blob, jump to "endfor"
2793 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
2794 may_restore_cmdmod(&ectx->ec_funclocal);
2795 }
2796 else
2797 {
2798 // Store the current number of funcrefs, this may be used in
2799 // ISN_LOOPEND. The variable index is always one more than the loop
2800 // variable index.
Bram Moolenaarcc341812022-09-19 15:54:34 +01002801 tv = STACK_TV_VAR(iptr->isn_arg.forloop.for_loop_idx + 1);
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002802 tv->vval.v_number = ectx->ec_funcrefs.ga_len;
2803 }
2804
2805 return OK;
2806}
2807
2808/*
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002809 * Code for handling variables declared inside a loop and used in a closure.
2810 * This is very similar to what is done with funcstack_T. The difference is
2811 * that the funcstack_T has the scope of a function, while a loopvars_T has the
2812 * scope of the block inside a loop and each loop may have its own.
2813 */
2814
2815// Double linked list of loopvars_T in use.
2816static loopvars_T *first_loopvars = NULL;
2817
2818 static void
2819add_loopvars_to_list(loopvars_T *loopvars)
2820{
2821 // Link in list of loopvarss.
2822 if (first_loopvars != NULL)
2823 first_loopvars->lvs_prev = loopvars;
2824 loopvars->lvs_next = first_loopvars;
2825 loopvars->lvs_prev = NULL;
2826 first_loopvars = loopvars;
2827}
2828
2829 static void
2830remove_loopvars_from_list(loopvars_T *loopvars)
2831{
2832 if (loopvars->lvs_prev == NULL)
2833 first_loopvars = loopvars->lvs_next;
2834 else
2835 loopvars->lvs_prev->lvs_next = loopvars->lvs_next;
2836 if (loopvars->lvs_next != NULL)
2837 loopvars->lvs_next->lvs_prev = loopvars->lvs_prev;
2838}
2839
2840/*
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002841 * End of a for or while loop: Handle any variables used by a closure.
2842 */
2843 static int
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002844execute_endloop(isn_T *iptr, ectx_T *ectx)
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002845{
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002846 endloop_T *endloop = &iptr->isn_arg.endloop;
2847 typval_T *tv_refcount = STACK_TV_VAR(endloop->end_funcref_idx);
2848 int prev_closure_count = tv_refcount->vval.v_number;
Bram Moolenaarcc341812022-09-19 15:54:34 +01002849 int depth = endloop->end_depth;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002850 garray_T *gap = &ectx->ec_funcrefs;
2851 int closure_in_use = FALSE;
2852 loopvars_T *loopvars;
2853 typval_T *stack;
2854 int idx;
2855
Bram Moolenaarcc341812022-09-19 15:54:34 +01002856 // Check if any created closure is still being referenced and loopvars have
2857 // not been saved yet for the current depth.
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002858 for (idx = prev_closure_count; idx < gap->ga_len; ++idx)
2859 {
2860 partial_T *pt = ((partial_T **)gap->ga_data)[idx];
2861
Bram Moolenaarcc341812022-09-19 15:54:34 +01002862 if (pt->pt_refcount > 1 && pt->pt_loopvars[depth] == NULL)
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002863 {
2864 int refcount = pt->pt_refcount;
2865 int i;
2866
2867 // A Reference in a variable inside the loop doesn't count, it gets
2868 // unreferenced at the end of the loop.
2869 for (i = 0; i < endloop->end_var_count; ++i)
2870 {
2871 typval_T *stv = STACK_TV_VAR(endloop->end_var_idx + i);
2872
2873 if (stv->v_type == VAR_PARTIAL && pt == stv->vval.v_partial)
2874 --refcount;
2875 }
2876 if (refcount > 1)
2877 {
2878 closure_in_use = TRUE;
2879 break;
2880 }
2881 }
2882 }
2883
2884 // If no function reference were created since the start of the loop block
2885 // or it is no longer referenced there is nothing to do.
2886 if (!closure_in_use)
2887 return OK;
2888
2889 // A closure is using variables declared inside the loop.
2890 // Move them to the called function.
2891 loopvars = ALLOC_CLEAR_ONE(loopvars_T);
2892 if (loopvars == NULL)
2893 return FAIL;
2894
2895 loopvars->lvs_ga.ga_len = endloop->end_var_count;
2896 stack = ALLOC_CLEAR_MULT(typval_T, loopvars->lvs_ga.ga_len);
2897 loopvars->lvs_ga.ga_data = stack;
2898 if (stack == NULL)
2899 {
2900 vim_free(loopvars);
2901 return FAIL;
2902 }
2903 add_loopvars_to_list(loopvars);
2904
2905 // Move the variable values.
2906 for (idx = 0; idx < endloop->end_var_count; ++idx)
2907 {
2908 typval_T *tv = STACK_TV_VAR(endloop->end_var_idx + idx);
2909
2910 *(stack + idx) = *tv;
2911 tv->v_type = VAR_UNKNOWN;
2912 }
2913
2914 for (idx = prev_closure_count; idx < gap->ga_len; ++idx)
2915 {
2916 partial_T *pt = ((partial_T **)gap->ga_data)[idx];
2917
Bram Moolenaarcc341812022-09-19 15:54:34 +01002918 if (pt->pt_refcount > 1 && pt->pt_loopvars[depth] == NULL)
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002919 {
2920 ++loopvars->lvs_refcount;
Bram Moolenaarcc341812022-09-19 15:54:34 +01002921 pt->pt_loopvars[depth] = loopvars;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002922
Bram Moolenaarcc341812022-09-19 15:54:34 +01002923 pt->pt_outer.out_loop[depth].stack = &loopvars->lvs_ga;
2924 pt->pt_outer.out_loop[depth].var_idx -=
2925 ectx->ec_frame_idx + STACK_FRAME_SIZE + endloop->end_var_idx;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002926 }
2927 }
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002928
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002929 return OK;
2930}
2931
2932/*
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002933 * Called when a partial is freed or its reference count goes down to one. The
2934 * loopvars may be the only reference to the partials in the local variables.
2935 * Go over all of them, the funcref and can be freed if all partials
2936 * referencing the loopvars have a reference count of one.
Bram Moolenaarcc341812022-09-19 15:54:34 +01002937 * Return TRUE if it was freed.
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002938 */
Bram Moolenaarcc341812022-09-19 15:54:34 +01002939 int
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002940loopvars_check_refcount(loopvars_T *loopvars)
2941{
2942 int i;
2943 garray_T *gap = &loopvars->lvs_ga;
2944 int done = 0;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002945 typval_T *stack = gap->ga_data;
2946
Bram Moolenaarcc341812022-09-19 15:54:34 +01002947 if (loopvars->lvs_refcount > loopvars->lvs_min_refcount)
2948 return FALSE;
2949 for (i = 0; i < gap->ga_len; ++i)
2950 {
2951 typval_T *tv = ((typval_T *)gap->ga_data) + i;
2952
2953 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL
2954 && tv->vval.v_partial->pt_refcount == 1)
2955 {
2956 int depth;
2957
2958 for (depth = 0; depth < MAX_LOOP_DEPTH; ++depth)
2959 if (tv->vval.v_partial->pt_loopvars[depth] == loopvars)
2960 ++done;
2961 }
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002962 }
Bram Moolenaarcc341812022-09-19 15:54:34 +01002963 if (done != loopvars->lvs_min_refcount)
2964 return FALSE;
2965
2966 // All partials referencing the loopvars have a reference count of
2967 // one, thus the loopvars is no longer of use.
2968 stack = gap->ga_data;
2969 for (i = 0; i < gap->ga_len; ++i)
2970 clear_tv(stack + i);
2971 vim_free(stack);
2972 remove_loopvars_from_list(loopvars);
2973 vim_free(loopvars);
2974 return TRUE;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002975}
2976
2977/*
2978 * For garbage collecting: set references in all variables referenced by
2979 * all loopvars.
2980 */
2981 int
2982set_ref_in_loopvars(int copyID)
2983{
2984 loopvars_T *loopvars;
2985
2986 for (loopvars = first_loopvars; loopvars != NULL;
2987 loopvars = loopvars->lvs_next)
2988 {
2989 typval_T *stack = loopvars->lvs_ga.ga_data;
2990 int i;
2991
2992 for (i = 0; i < loopvars->lvs_ga.ga_len; ++i)
2993 if (set_ref_in_item(stack + i, copyID, NULL, NULL))
2994 return TRUE; // abort
2995 }
2996 return FALSE;
2997}
2998
2999/*
Bram Moolenaar06b77222022-01-25 15:51:56 +00003000 * Load instruction for w:/b:/g:/t: variable.
3001 * "isn_type" is used instead of "iptr->isn_type".
3002 */
3003 static int
3004load_namespace_var(ectx_T *ectx, isntype_T isn_type, isn_T *iptr)
3005{
3006 dictitem_T *di = NULL;
3007 hashtab_T *ht = NULL;
3008 char namespace;
3009
3010 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
3011 return NOTDONE;
3012 switch (isn_type)
3013 {
3014 case ISN_LOADG:
3015 ht = get_globvar_ht();
3016 namespace = 'g';
3017 break;
3018 case ISN_LOADB:
3019 ht = &curbuf->b_vars->dv_hashtab;
3020 namespace = 'b';
3021 break;
3022 case ISN_LOADW:
3023 ht = &curwin->w_vars->dv_hashtab;
3024 namespace = 'w';
3025 break;
3026 case ISN_LOADT:
3027 ht = &curtab->tp_vars->dv_hashtab;
3028 namespace = 't';
3029 break;
3030 default: // Cannot reach here
3031 return NOTDONE;
3032 }
3033 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
3034
Bram Moolenaar06b77222022-01-25 15:51:56 +00003035 if (di == NULL)
3036 {
Bram Moolenaarfe732552022-02-22 19:39:13 +00003037 if (isn_type == ISN_LOADG)
3038 {
3039 ufunc_T *ufunc = find_func(iptr->isn_arg.string, TRUE);
3040
3041 // g:Something could be a function
3042 if (ufunc != NULL)
3043 {
3044 typval_T *tv = STACK_TV_BOT(0);
3045
3046 ++ectx->ec_stack.ga_len;
3047 tv->v_type = VAR_FUNC;
3048 tv->vval.v_string = alloc(STRLEN(iptr->isn_arg.string) + 3);
3049 if (tv->vval.v_string == NULL)
3050 return FAIL;
3051 STRCPY(tv->vval.v_string, "g:");
3052 STRCPY(tv->vval.v_string + 2, iptr->isn_arg.string);
3053 return OK;
3054 }
3055 }
Bram Moolenaar06b77222022-01-25 15:51:56 +00003056 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarfe732552022-02-22 19:39:13 +00003057 if (vim_strchr(iptr->isn_arg.string, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar06b77222022-01-25 15:51:56 +00003058 // no check if the item exists in the script but
3059 // isn't exported, it is too complicated
Bram Moolenaarfe732552022-02-22 19:39:13 +00003060 semsg(_(e_item_not_found_in_script_str), iptr->isn_arg.string);
Bram Moolenaar06b77222022-01-25 15:51:56 +00003061 else
3062 semsg(_(e_undefined_variable_char_str),
Bram Moolenaarfe732552022-02-22 19:39:13 +00003063 namespace, iptr->isn_arg.string);
Bram Moolenaar06b77222022-01-25 15:51:56 +00003064 return FAIL;
3065 }
3066 else
3067 {
3068 copy_tv(&di->di_tv, STACK_TV_BOT(0));
3069 ++ectx->ec_stack.ga_len;
3070 }
3071 return OK;
3072}
3073
Bram Moolenaard0200c82023-01-28 15:19:40 +00003074
3075 static void
3076object_required_error(typval_T *tv)
3077{
3078 garray_T type_list;
3079 ga_init2(&type_list, sizeof(type_T *), 10);
3080 type_T *type = typval2type(tv, get_copyID(), &type_list, TVTT_DO_MEMBER);
3081 char *tofree = NULL;
3082 char *typename = type_name(type, &tofree);
3083 semsg(_(e_object_required_found_str), typename);
3084 vim_free(tofree);
3085 clear_type_list(&type_list);
3086}
3087
Bram Moolenaar06b77222022-01-25 15:51:56 +00003088/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02003089 * Execute instructions in execution context "ectx".
3090 * Return OK or FAIL;
3091 */
3092 static int
3093exec_instructions(ectx_T *ectx)
3094{
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003095 int ret = FAIL;
3096 int save_trylevel_at_start = ectx->ec_trylevel_at_start;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02003097 int dict_stack_len_at_start = dict_stack.ga_len;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003098
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02003099 // Start execution at the first instruction.
Bram Moolenaar4c137212021-04-19 16:48:48 +02003100 ectx->ec_iidx = 0;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01003101
Bram Moolenaarff652882021-05-16 15:24:49 +02003102 // Only catch exceptions in this instruction list.
3103 ectx->ec_trylevel_at_start = trylevel;
3104
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003105 for (;;)
3106 {
Bram Moolenaara7490192021-07-22 12:26:14 +02003107 static int breakcheck_count = 0; // using "static" makes it faster
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003108 isn_T *iptr;
Bram Moolenaara7490192021-07-22 12:26:14 +02003109 typval_T *tv;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003110
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003111 if (unlikely(++breakcheck_count >= 100))
Bram Moolenaar270d0382020-05-15 21:42:53 +02003112 {
3113 line_breakcheck();
3114 breakcheck_count = 0;
3115 }
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003116 if (unlikely(got_int))
Bram Moolenaar20431c92020-03-20 18:39:46 +01003117 {
3118 // Turn CTRL-C into an exception.
3119 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +01003120 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003121 goto theend;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003122 did_throw = TRUE;
3123 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003124
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003125 if (unlikely(did_emsg && msg_list != NULL && *msg_list != NULL))
Bram Moolenaara26b9702020-04-18 19:53:28 +02003126 {
3127 // Turn an error message into an exception.
3128 did_emsg = FALSE;
3129 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003130 goto theend;
Bram Moolenaara26b9702020-04-18 19:53:28 +02003131 did_throw = TRUE;
3132 *msg_list = NULL;
Bram Moolenaar3ef2e412023-04-30 18:50:48 +01003133
3134 // This exception was not caught (yet).
3135 garray_T *trystack = &ectx->ec_trystack;
3136 if (trystack->ga_len > 0)
3137 {
3138 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
3139 + trystack->ga_len - 1;
3140 if (trycmd->tcd_frame_idx == ectx->ec_frame_idx)
3141 trycmd->tcd_caught = FALSE;
3142 }
Bram Moolenaara26b9702020-04-18 19:53:28 +02003143 }
3144
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003145 if (unlikely(did_throw))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003146 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003147 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003148 trycmd_T *trycmd = NULL;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003149 int index = trystack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003150
3151 // An exception jumps to the first catch, finally, or returns from
3152 // the current function.
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003153 while (index > 0)
3154 {
3155 trycmd = ((trycmd_T *)trystack->ga_data) + index - 1;
Bram Moolenaar3ef2e412023-04-30 18:50:48 +01003156 // 1. after :try and before :catch - jump to first :catch
3157 // 2. in :catch block - jump to :finally
3158 // 3. in :catch block and no finally - jump to :endtry
3159 if (!trycmd->tcd_in_catch || trycmd->tcd_finally_idx != 0
3160 || trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003161 break;
3162 // In the catch and finally block of this try we have to go up
3163 // one level.
3164 --index;
3165 trycmd = NULL;
3166 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003167 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003168 {
Bram Moolenaar834193a2021-06-30 20:39:15 +02003169 if (trycmd->tcd_in_catch)
3170 {
Bram Moolenaar3ef2e412023-04-30 18:50:48 +01003171 if (trycmd->tcd_finally_idx > 0)
3172 {
3173 // exception inside ":catch", jump to ":finally" once
3174 ectx->ec_iidx = trycmd->tcd_finally_idx;
3175 trycmd->tcd_finally_idx = 0;
3176 }
3177 else
3178 {
3179 // exception inside ":catch" or ":finally", jump to
3180 // ":endtry"
3181 ectx->ec_iidx = trycmd->tcd_endtry_idx;
3182 }
Bram Moolenaar834193a2021-06-30 20:39:15 +02003183 }
3184 else
Bram Moolenaar3ef2e412023-04-30 18:50:48 +01003185 {
Bram Moolenaar834193a2021-06-30 20:39:15 +02003186 // jump to first ":catch"
3187 ectx->ec_iidx = trycmd->tcd_catch_idx;
Bram Moolenaar3ef2e412023-04-30 18:50:48 +01003188 trycmd->tcd_in_catch = TRUE;
3189 }
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003190 did_throw = FALSE; // don't come back here until :endtry
3191 trycmd->tcd_did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003192 }
3193 else
3194 {
Bram Moolenaar3ef2e412023-04-30 18:50:48 +01003195 // Not inside try or need to return from current function.
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02003196 // Push a dummy return value.
Bram Moolenaar35578162021-08-02 19:10:38 +02003197 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003198 goto theend;
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02003199 tv = STACK_TV_BOT(0);
3200 tv->v_type = VAR_NUMBER;
3201 tv->vval.v_number = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003202 ++ectx->ec_stack.ga_len;
3203 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003204 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02003205 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +01003206 need_rethrow = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003207 if (handle_closure_in_use(ectx, FALSE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003208 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003209 goto done;
3210 }
3211
Bram Moolenaar4c137212021-04-19 16:48:48 +02003212 if (func_return(ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003213 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003214 }
3215 continue;
3216 }
3217
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003218 /*
3219 * Big switch on the instruction. Most compilers will be turning this
3220 * into an efficient lookup table, since the "case" values are an enum
3221 * with sequential numbers. It may look ugly, but it should be the
3222 * most efficient way.
3223 */
Bram Moolenaar4c137212021-04-19 16:48:48 +02003224 iptr = &ectx->ec_instr[ectx->ec_iidx++];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003225 switch (iptr->isn_type)
3226 {
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00003227 // Constructor, first instruction in a new() method.
Bram Moolenaar00b28d62022-12-08 15:32:33 +00003228 case ISN_CONSTRUCT:
3229 // "this" is always the local variable at index zero
3230 tv = STACK_TV_VAR(0);
3231 tv->v_type = VAR_OBJECT;
3232 tv->vval.v_object = alloc_clear(
3233 iptr->isn_arg.construct.construct_size);
3234 tv->vval.v_object->obj_class =
3235 iptr->isn_arg.construct.construct_class;
Bram Moolenaard28d7b92022-12-08 20:42:00 +00003236 ++tv->vval.v_object->obj_class->class_refcount;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00003237 tv->vval.v_object->obj_refcount = 1;
Bram Moolenaard28d7b92022-12-08 20:42:00 +00003238 object_created(tv->vval.v_object);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00003239 break;
3240
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003241 // execute Ex command line
3242 case ISN_EXEC:
Ernie Raelee865f32023-09-29 19:53:55 +02003243 if (exec_command(iptr, iptr->isn_arg.string) == FAIL)
Bram Moolenaaraacc9662021-08-13 19:40:51 +02003244 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003245 break;
3246
Bram Moolenaar20677332021-06-06 17:02:53 +02003247 // execute Ex command line split at NL characters.
3248 case ISN_EXEC_SPLIT:
3249 {
3250 source_cookie_T cookie;
Bram Moolenaar518df272021-06-06 17:34:13 +02003251 char_u *line;
Bram Moolenaar20677332021-06-06 17:02:53 +02003252
3253 SOURCING_LNUM = iptr->isn_lnum;
3254 CLEAR_FIELD(cookie);
3255 cookie.sourcing_lnum = iptr->isn_lnum - 1;
3256 cookie.nextline = iptr->isn_arg.string;
Bram Moolenaar518df272021-06-06 17:34:13 +02003257 line = get_split_sourceline(0, &cookie, 0, 0);
3258 if (do_cmdline(line,
Bram Moolenaar20677332021-06-06 17:02:53 +02003259 get_split_sourceline, &cookie,
3260 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED)
3261 == FAIL
3262 || did_emsg)
Bram Moolenaar518df272021-06-06 17:34:13 +02003263 {
3264 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02003265 goto on_error;
Bram Moolenaar518df272021-06-06 17:34:13 +02003266 }
3267 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02003268 }
3269 break;
3270
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00003271 // execute Ex command line that is only a range
3272 case ISN_EXECRANGE:
3273 {
3274 exarg_T ea;
3275 char *error = NULL;
3276
3277 CLEAR_FIELD(ea);
3278 ea.cmdidx = CMD_SIZE;
3279 ea.addr_type = ADDR_LINES;
3280 ea.cmd = iptr->isn_arg.string;
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00003281 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00003282 parse_cmd_address(&ea, &error, FALSE);
Bram Moolenaar01a4dcb2021-12-04 13:15:10 +00003283 if (ea.cmd == NULL)
3284 goto on_error;
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00003285 // error is always NULL when using ADDR_LINES
3286 error = ex_range_without_command(&ea);
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00003287 if (error != NULL)
3288 {
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00003289 emsg(error);
3290 goto on_error;
3291 }
3292 }
3293 break;
3294
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02003295 // Evaluate an expression with legacy syntax, push it onto the
3296 // stack.
3297 case ISN_LEGACY_EVAL:
3298 {
3299 char_u *arg = iptr->isn_arg.string;
3300 int res;
3301 int save_flags = cmdmod.cmod_flags;
3302
Bram Moolenaar35578162021-08-02 19:10:38 +02003303 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003304 goto theend;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02003305 tv = STACK_TV_BOT(0);
3306 init_tv(tv);
3307 cmdmod.cmod_flags |= CMOD_LEGACY;
3308 res = eval0(arg, tv, NULL, &EVALARG_EVALUATE);
3309 cmdmod.cmod_flags = save_flags;
3310 if (res == FAIL)
3311 goto on_error;
3312 ++ectx->ec_stack.ga_len;
3313 }
3314 break;
3315
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003316 // push typeval VAR_INSTR with instructions to be executed
3317 case ISN_INSTR:
3318 {
Bram Moolenaar35578162021-08-02 19:10:38 +02003319 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003320 goto theend;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003321 tv = STACK_TV_BOT(0);
3322 tv->vval.v_instr = ALLOC_ONE(instr_T);
3323 if (tv->vval.v_instr == NULL)
3324 goto on_error;
3325 ++ectx->ec_stack.ga_len;
3326
3327 tv->v_type = VAR_INSTR;
3328 tv->vval.v_instr->instr_ectx = ectx;
3329 tv->vval.v_instr->instr_instr = iptr->isn_arg.instr;
3330 }
3331 break;
3332
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003333 case ISN_SOURCE:
3334 {
Bram Moolenaar89445512022-04-14 12:58:23 +01003335 int notused;
LemonBoy77142312022-04-15 20:50:46 +01003336
Bram Moolenaar89445512022-04-14 12:58:23 +01003337 SOURCING_LNUM = iptr->isn_lnum;
3338 if (may_load_script((int)iptr->isn_arg.number, &notused)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003339 == FAIL)
Bram Moolenaar89445512022-04-14 12:58:23 +01003340 goto on_error;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003341 }
3342 break;
3343
Bram Moolenaar4c137212021-04-19 16:48:48 +02003344 // execute :substitute with an expression
3345 case ISN_SUBSTITUTE:
3346 {
3347 subs_T *subs = &iptr->isn_arg.subs;
3348 source_cookie_T cookie;
3349 struct subs_expr_S *save_instr = substitute_instr;
3350 struct subs_expr_S subs_instr;
3351 int res;
3352
3353 subs_instr.subs_ectx = ectx;
3354 subs_instr.subs_instr = subs->subs_instr;
3355 subs_instr.subs_status = OK;
3356 substitute_instr = &subs_instr;
3357
3358 SOURCING_LNUM = iptr->isn_lnum;
3359 // This is very much like ISN_EXEC
3360 CLEAR_FIELD(cookie);
3361 cookie.sourcing_lnum = iptr->isn_lnum - 1;
3362 res = do_cmdline(subs->subs_cmd,
3363 getsourceline, &cookie,
3364 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
3365 substitute_instr = save_instr;
3366
3367 if (res == FAIL || did_emsg
3368 || subs_instr.subs_status == FAIL)
3369 goto on_error;
3370 }
3371 break;
3372
3373 case ISN_FINISH:
3374 goto done;
3375
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02003376 case ISN_REDIRSTART:
3377 // create a dummy entry for var_redir_str()
3378 if (alloc_redir_lval() == FAIL)
3379 goto on_error;
3380
3381 // The output is stored in growarray "redir_ga" until
3382 // redirection ends.
3383 init_redir_ga();
3384 redir_vname = 1;
3385 break;
3386
3387 case ISN_REDIREND:
3388 {
3389 char_u *res = get_clear_redir_ga();
3390
3391 // End redirection, put redirected text on the stack.
3392 clear_redir_lval();
3393 redir_vname = 0;
3394
Bram Moolenaar35578162021-08-02 19:10:38 +02003395 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02003396 {
3397 vim_free(res);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003398 goto theend;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02003399 }
3400 tv = STACK_TV_BOT(0);
3401 tv->v_type = VAR_STRING;
3402 tv->vval.v_string = res;
3403 ++ectx->ec_stack.ga_len;
3404 }
3405 break;
3406
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003407 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02003408#ifdef FEAT_QUICKFIX
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003409 force_abort = TRUE;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003410 if (trigger_cexpr_autocmd(iptr->isn_arg.number) == FAIL)
3411 goto on_error;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003412 force_abort = FALSE;
Bram Moolenaarb7c97812021-05-05 22:51:39 +02003413#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003414 break;
3415
3416 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02003417#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003418 {
3419 exarg_T ea;
3420 int res;
3421
3422 CLEAR_FIELD(ea);
3423 ea.cmdidx = iptr->isn_arg.cexpr.cexpr_ref->cer_cmdidx;
3424 ea.forceit = iptr->isn_arg.cexpr.cexpr_ref->cer_forceit;
3425 ea.cmdlinep = &iptr->isn_arg.cexpr.cexpr_ref->cer_cmdline;
3426 --ectx->ec_stack.ga_len;
3427 tv = STACK_TV_BOT(0);
Bram Moolenaarbd683e32022-07-18 17:49:03 +01003428 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003429 res = cexpr_core(&ea, tv);
3430 clear_tv(tv);
3431 if (res == FAIL)
3432 goto on_error;
3433 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02003434#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003435 break;
3436
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003437 // execute Ex command from pieces on the stack
3438 case ISN_EXECCONCAT:
3439 {
3440 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +02003441 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003442 int pass;
3443 int i;
3444 char_u *cmd = NULL;
3445 char_u *str;
3446
3447 for (pass = 1; pass <= 2; ++pass)
3448 {
3449 for (i = 0; i < count; ++i)
3450 {
3451 tv = STACK_TV_BOT(i - count);
3452 str = tv->vval.v_string;
3453 if (str != NULL && *str != NUL)
3454 {
3455 if (pass == 2)
3456 STRCPY(cmd + len, str);
3457 len += STRLEN(str);
3458 }
3459 if (pass == 2)
3460 clear_tv(tv);
3461 }
3462 if (pass == 1)
3463 {
3464 cmd = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003465 if (unlikely(cmd == NULL))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003466 goto theend;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003467 len = 0;
3468 }
3469 }
3470
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02003471 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003472 do_cmdline_cmd(cmd);
3473 vim_free(cmd);
3474 }
3475 break;
3476
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003477 // execute :echo {string} ...
3478 case ISN_ECHO:
3479 {
3480 int count = iptr->isn_arg.echo.echo_count;
3481 int atstart = TRUE;
3482 int needclr = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003483 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003484
3485 for (idx = 0; idx < count; ++idx)
3486 {
3487 tv = STACK_TV_BOT(idx - count);
3488 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
3489 &atstart, &needclr);
3490 clear_tv(tv);
3491 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01003492 if (needclr)
3493 msg_clr_eos();
Bram Moolenaar4c137212021-04-19 16:48:48 +02003494 ectx->ec_stack.ga_len -= count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003495 }
3496 break;
3497
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003498 // :execute {string} ...
3499 // :echomsg {string} ...
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01003500 // :echowindow {string} ...
Bram Moolenaar7de62622021-08-07 15:05:47 +02003501 // :echoconsole {string} ...
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003502 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01003503 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003504 case ISN_ECHOMSG:
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01003505 case ISN_ECHOWINDOW:
Bram Moolenaar7de62622021-08-07 15:05:47 +02003506 case ISN_ECHOCONSOLE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003507 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01003508 {
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01003509 int count;
Bram Moolenaarad39c092020-02-26 18:23:43 +01003510 garray_T ga;
3511 char_u buf[NUMBUFLEN];
3512 char_u *p;
3513 int len;
3514 int failed = FALSE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003515 int idx;
Bram Moolenaarad39c092020-02-26 18:23:43 +01003516
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01003517 if (iptr->isn_type == ISN_ECHOWINDOW)
3518 count = iptr->isn_arg.echowin.ewin_count;
3519 else
3520 count = iptr->isn_arg.number;
Bram Moolenaarad39c092020-02-26 18:23:43 +01003521 ga_init2(&ga, 1, 80);
3522 for (idx = 0; idx < count; ++idx)
3523 {
3524 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003525 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01003526 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003527 if (tv->v_type == VAR_CHANNEL
3528 || tv->v_type == VAR_JOB)
3529 {
3530 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar68db9962021-05-09 23:19:22 +02003531 semsg(_(e_using_invalid_value_as_string_str),
3532 vartype_name(tv->v_type));
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003533 break;
3534 }
3535 else
3536 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01003537 }
3538 else
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003539 p = tv_stringify(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01003540
3541 len = (int)STRLEN(p);
Bram Moolenaar35578162021-08-02 19:10:38 +02003542 if (GA_GROW_FAILS(&ga, len + 2))
Bram Moolenaarad39c092020-02-26 18:23:43 +01003543 failed = TRUE;
3544 else
3545 {
3546 if (ga.ga_len > 0)
3547 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
3548 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
3549 ga.ga_len += len;
3550 }
3551 clear_tv(tv);
3552 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003553 ectx->ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003554 if (failed)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003555 {
3556 ga_clear(&ga);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003557 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003558 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01003559
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003560 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003561 {
3562 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02003563 {
3564 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003565 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01003566 if (did_emsg)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003567 {
3568 ga_clear(&ga);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01003569 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003570 }
Bram Moolenaar430deb12020-08-23 16:29:11 +02003571 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003572 else
3573 {
3574 msg_sb_eol();
3575 if (iptr->isn_type == ISN_ECHOMSG)
3576 {
3577 msg_attr(ga.ga_data, echo_attr);
3578 out_flush();
3579 }
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01003580#ifdef HAS_MESSAGE_WINDOW
3581 else if (iptr->isn_type == ISN_ECHOWINDOW)
3582 {
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01003583 start_echowindow(
3584 iptr->isn_arg.echowin.ewin_time);
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01003585 msg_attr(ga.ga_data, echo_attr);
3586 end_echowindow();
3587 }
3588#endif
Bram Moolenaar7de62622021-08-07 15:05:47 +02003589 else if (iptr->isn_type == ISN_ECHOCONSOLE)
3590 {
3591 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data),
3592 TRUE);
3593 ui_write((char_u *)"\r\n", 2, TRUE);
3594 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003595 else
3596 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003597 SOURCING_LNUM = iptr->isn_lnum;
3598 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003599 }
3600 }
3601 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01003602 ga_clear(&ga);
3603 }
3604 break;
3605
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003606 // load local variable or argument
3607 case ISN_LOAD:
Bram Moolenaar35578162021-08-02 19:10:38 +02003608 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003609 goto theend;
Bram Moolenaar2ed57ac2023-04-01 22:05:38 +01003610 tv = STACK_TV_VAR(iptr->isn_arg.number);
3611 if (tv->v_type == VAR_UNKNOWN)
3612 {
3613 // missing argument or default value v:none
3614 STACK_TV_BOT(0)->v_type = VAR_SPECIAL;
3615 STACK_TV_BOT(0)->vval.v_number = VVAL_NONE;
3616 }
3617 else
3618 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003619 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003620 break;
3621
3622 // load v: variable
3623 case ISN_LOADV:
Bram Moolenaar35578162021-08-02 19:10:38 +02003624 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003625 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003626 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003627 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003628 break;
3629
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003630 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003631 case ISN_LOADSCRIPT:
3632 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01003633 scriptref_T *sref = iptr->isn_arg.script.scriptref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003634 svar_T *sv;
3635
Bram Moolenaar6db660b2021-08-01 14:08:54 +02003636 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003637 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003638 goto theend;
Bram Moolenaar859cc212022-03-28 15:22:35 +01003639 allocate_if_null(sv);
Bram Moolenaar35578162021-08-02 19:10:38 +02003640 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003641 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003642 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003643 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003644 }
3645 break;
3646
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003647 // load s: variable in old script or autoload import
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003648 case ISN_LOADS:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003649 case ISN_LOADEXPORT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003650 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003651 int sid = iptr->isn_arg.loadstore.ls_sid;
3652 hashtab_T *ht = &SCRIPT_VARS(sid);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003653 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003654 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003655
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003656 if (di == NULL)
3657 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003658 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003659 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003660 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003661 }
3662 else
3663 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003664 if (iptr->isn_type == ISN_LOADEXPORT)
3665 {
3666 int idx = get_script_item_idx(sid, name, 0,
3667 NULL, NULL);
3668 svar_T *sv;
3669
3670 if (idx >= 0)
3671 {
3672 sv = ((svar_T *)SCRIPT_ITEM(sid)
3673 ->sn_var_vals.ga_data) + idx;
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003674 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003675 {
3676 SOURCING_LNUM = iptr->isn_lnum;
3677 semsg(_(e_item_not_exported_in_script_str),
3678 name);
3679 goto on_error;
3680 }
3681 }
3682 }
Bram Moolenaar35578162021-08-02 19:10:38 +02003683 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003684 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003685 copy_tv(&di->di_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003686 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003687 }
3688 }
3689 break;
3690
Bram Moolenaard3aac292020-04-19 14:32:17 +02003691 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003692 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02003693 case ISN_LOADB:
3694 case ISN_LOADW:
3695 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003696 {
Bram Moolenaar06b77222022-01-25 15:51:56 +00003697 int res = load_namespace_var(ectx, iptr->isn_type, iptr);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003698
Bram Moolenaar06b77222022-01-25 15:51:56 +00003699 if (res == NOTDONE)
Bram Moolenaarcbbc48f2022-01-18 12:58:28 +00003700 goto theend;
Bram Moolenaar06b77222022-01-25 15:51:56 +00003701 if (res == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003702 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003703 }
Bram Moolenaar06b77222022-01-25 15:51:56 +00003704
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003705 break;
3706
Bram Moolenaar03290b82020-12-19 16:30:44 +01003707 // load autoload variable
3708 case ISN_LOADAUTO:
3709 {
3710 char_u *name = iptr->isn_arg.string;
3711
Bram Moolenaar35578162021-08-02 19:10:38 +02003712 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003713 goto theend;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003714 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003715 if (eval_variable(name, (int)STRLEN(name), 0,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003716 STACK_TV_BOT(0), NULL, EVAL_VAR_VERBOSE) == FAIL)
Bram Moolenaar03290b82020-12-19 16:30:44 +01003717 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003718 ++ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003719 }
3720 break;
3721
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003722 // load g:/b:/w:/t: namespace
3723 case ISN_LOADGDICT:
3724 case ISN_LOADBDICT:
3725 case ISN_LOADWDICT:
3726 case ISN_LOADTDICT:
3727 {
3728 dict_T *d = NULL;
3729
3730 switch (iptr->isn_type)
3731 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02003732 case ISN_LOADGDICT: d = get_globvar_dict(); break;
3733 case ISN_LOADBDICT: d = curbuf->b_vars; break;
3734 case ISN_LOADWDICT: d = curwin->w_vars; break;
3735 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003736 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003737 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003738 }
Bram Moolenaar35578162021-08-02 19:10:38 +02003739 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003740 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003741 tv = STACK_TV_BOT(0);
3742 tv->v_type = VAR_DICT;
3743 tv->v_lock = 0;
3744 tv->vval.v_dict = d;
Bram Moolenaar1bd3cb22021-02-24 12:27:31 +01003745 ++d->dv_refcount;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003746 ++ectx->ec_stack.ga_len;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003747 }
3748 break;
3749
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003750 // load &option
3751 case ISN_LOADOPT:
3752 {
3753 typval_T optval;
3754 char_u *name = iptr->isn_arg.string;
3755
Bram Moolenaara8c17702020-04-01 21:17:24 +02003756 // This is not expected to fail, name is checked during
3757 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar35578162021-08-02 19:10:38 +02003758 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003759 goto theend;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003760 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003761 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003762 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003763 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003764 }
3765 break;
3766
3767 // load $ENV
3768 case ISN_LOADENV:
3769 {
3770 typval_T optval;
3771 char_u *name = iptr->isn_arg.string;
3772
Bram Moolenaar35578162021-08-02 19:10:38 +02003773 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003774 goto theend;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003775 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003776 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003777 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003778 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003779 }
3780 break;
3781
3782 // load @register
3783 case ISN_LOADREG:
Bram Moolenaar35578162021-08-02 19:10:38 +02003784 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003785 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003786 tv = STACK_TV_BOT(0);
3787 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003788 tv->v_lock = 0;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02003789 // This may result in NULL, which should be equivalent to an
3790 // empty string.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003791 tv->vval.v_string = get_reg_contents(
3792 iptr->isn_arg.number, GREG_EXPR_SRC);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003793 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003794 break;
3795
3796 // store local variable
3797 case ISN_STORE:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003798 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003799 tv = STACK_TV_VAR(iptr->isn_arg.number);
3800 clear_tv(tv);
3801 *tv = *STACK_TV_BOT(0);
3802 break;
3803
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003804 // store s: variable in old script or autoload import
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003805 case ISN_STORES:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003806 case ISN_STOREEXPORT:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003807 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003808 int sid = iptr->isn_arg.loadstore.ls_sid;
3809 hashtab_T *ht = &SCRIPT_VARS(sid);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003810 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003811 dictitem_T *di = find_var_in_ht(ht, 0,
3812 iptr->isn_type == ISN_STORES
3813 ? name + 2 : name, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003814
Bram Moolenaar4c137212021-04-19 16:48:48 +02003815 --ectx->ec_stack.ga_len;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003816 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003817 if (di == NULL)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003818 {
3819 if (iptr->isn_type == ISN_STOREEXPORT)
3820 {
3821 semsg(_(e_undefined_variable_str), name);
Bram Moolenaard1d26842022-03-31 10:13:47 +01003822 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003823 goto on_error;
3824 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003825 store_var(name, STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003826 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003827 else
3828 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003829 if (iptr->isn_type == ISN_STOREEXPORT)
3830 {
3831 int idx = get_script_item_idx(sid, name, 0,
3832 NULL, NULL);
3833
Bram Moolenaar06651632022-04-27 17:54:25 +01003834 // can this ever fail?
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003835 if (idx >= 0)
3836 {
3837 svar_T *sv = ((svar_T *)SCRIPT_ITEM(sid)
3838 ->sn_var_vals.ga_data) + idx;
3839
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003840 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003841 {
3842 semsg(_(e_item_not_exported_in_script_str),
3843 name);
Bram Moolenaard1d26842022-03-31 10:13:47 +01003844 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003845 goto on_error;
3846 }
3847 }
3848 }
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02003849 if (var_check_permission(di, name) == FAIL)
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003850 {
3851 clear_tv(STACK_TV_BOT(0));
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02003852 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003853 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003854 clear_tv(&di->di_tv);
3855 di->di_tv = *STACK_TV_BOT(0);
3856 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003857 }
3858 break;
3859
3860 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003861 case ISN_STORESCRIPT:
3862 {
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003863 scriptref_T *sref = iptr->isn_arg.script.scriptref;
3864 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003865
Bram Moolenaar6db660b2021-08-01 14:08:54 +02003866 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003867 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003868 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003869 --ectx->ec_stack.ga_len;
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003870
3871 // "const" and "final" are checked at compile time, locking
3872 // the value needs to be checked here.
3873 SOURCING_LNUM = iptr->isn_lnum;
3874 if (value_check_lock(sv->sv_tv->v_lock, sv->sv_name, FALSE))
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003875 {
3876 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003877 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003878 }
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003879
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003880 clear_tv(sv->sv_tv);
3881 *sv->sv_tv = *STACK_TV_BOT(0);
3882 }
3883 break;
3884
3885 // store option
3886 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003887 case ISN_STOREFUNCOPT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003888 {
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003889 char_u *opt_name = iptr->isn_arg.storeopt.so_name;
3890 int opt_flags = iptr->isn_arg.storeopt.so_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003891 long n = 0;
3892 char_u *s = NULL;
3893 char *msg;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003894 char_u numbuf[NUMBUFLEN];
3895 char_u *tofree = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003896
Bram Moolenaar4c137212021-04-19 16:48:48 +02003897 --ectx->ec_stack.ga_len;
LemonBoy32f34612023-09-02 21:52:05 +02003898 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003899 tv = STACK_TV_BOT(0);
3900 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01003901 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003902 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01003903 if (s == NULL)
3904 s = (char_u *)"";
3905 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003906 else if (iptr->isn_type == ISN_STOREFUNCOPT)
3907 {
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003908 // If the option can be set to a function reference or
3909 // a lambda and the passed value is a function
3910 // reference, then convert it to the name (string) of
3911 // the function reference.
3912 s = tv2string(tv, &tofree, numbuf, 0);
3913 if (s == NULL || *s == NUL)
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003914 {
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003915 // cannot happen?
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003916 clear_tv(tv);
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003917 vim_free(tofree);
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003918 goto on_error;
3919 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003920 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003921 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003922 // must be VAR_NUMBER, CHECKTYPE makes sure
3923 n = tv->vval.v_number;
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003924 msg = set_option_value(opt_name, n, s, opt_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02003925 clear_tv(tv);
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003926 vim_free(tofree);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003927 if (msg != NULL)
3928 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003929 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003930 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02003931 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003932 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003933 }
3934 break;
3935
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003936 // store $ENV
3937 case ISN_STOREENV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003938 --ectx->ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003939 tv = STACK_TV_BOT(0);
3940 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
3941 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003942 break;
3943
3944 // store @r
3945 case ISN_STOREREG:
3946 {
3947 int reg = iptr->isn_arg.number;
3948
Bram Moolenaar4c137212021-04-19 16:48:48 +02003949 --ectx->ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01003950 tv = STACK_TV_BOT(0);
Bram Moolenaar74f4a962021-06-17 21:03:07 +02003951 write_reg_contents(reg, tv_get_string(tv), -1, FALSE);
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01003952 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003953 }
3954 break;
3955
3956 // store v: variable
3957 case ISN_STOREV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003958 --ectx->ec_stack.ga_len;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003959 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
3960 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003961 // should not happen, type is checked when compiling
3962 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003963 break;
3964
Bram Moolenaard3aac292020-04-19 14:32:17 +02003965 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003966 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02003967 case ISN_STOREB:
3968 case ISN_STOREW:
3969 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003970 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003971 dictitem_T *di;
3972 hashtab_T *ht;
3973 char_u *name = iptr->isn_arg.string + 2;
3974
Bram Moolenaard3aac292020-04-19 14:32:17 +02003975 switch (iptr->isn_type)
3976 {
3977 case ISN_STOREG:
3978 ht = get_globvar_ht();
3979 break;
3980 case ISN_STOREB:
3981 ht = &curbuf->b_vars->dv_hashtab;
3982 break;
3983 case ISN_STOREW:
3984 ht = &curwin->w_vars->dv_hashtab;
3985 break;
3986 case ISN_STORET:
3987 ht = &curtab->tp_vars->dv_hashtab;
3988 break;
3989 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003990 goto theend;
Bram Moolenaard3aac292020-04-19 14:32:17 +02003991 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003992
Bram Moolenaar4c137212021-04-19 16:48:48 +02003993 --ectx->ec_stack.ga_len;
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003994 di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003995 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003996 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003997 else
3998 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003999 SOURCING_LNUM = iptr->isn_lnum;
4000 if (var_check_permission(di, name) == FAIL)
4001 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004002 clear_tv(&di->di_tv);
4003 di->di_tv = *STACK_TV_BOT(0);
4004 }
4005 }
4006 break;
4007
Bram Moolenaar03290b82020-12-19 16:30:44 +01004008 // store an autoload variable
4009 case ISN_STOREAUTO:
4010 SOURCING_LNUM = iptr->isn_lnum;
4011 set_var(iptr->isn_arg.string, STACK_TV_BOT(-1), TRUE);
4012 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02004013 --ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01004014 break;
4015
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004016 // store number in local variable
4017 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01004018 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004019 clear_tv(tv);
4020 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01004021 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004022 break;
4023
Bram Moolenaar590162c2022-12-24 21:24:06 +00004024 // Store a value in a list, dict, blob or object variable.
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01004025 case ISN_STOREINDEX:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004026 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00004027 int res = execute_storeindex(iptr, ectx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004028
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00004029 if (res == FAIL)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004030 goto on_error;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00004031 if (res == NOTDONE)
4032 goto theend;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004033 }
4034 break;
4035
Bram Moolenaarea5c8982022-02-17 14:42:02 +00004036 // store value in list or blob range
Bram Moolenaar68452172021-04-12 21:21:02 +02004037 case ISN_STORERANGE:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00004038 if (execute_storerange(iptr, ectx) == FAIL)
4039 goto on_error;
Bram Moolenaar68452172021-04-12 21:21:02 +02004040 break;
4041
Bram Moolenaard505d172022-12-18 21:42:55 +00004042 case ISN_LOAD_CLASSMEMBER:
4043 {
4044 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
4045 goto theend;
4046 classmember_T *cm = &iptr->isn_arg.classmember;
Bram Moolenaar4e2406c2023-06-24 19:22:21 +01004047 copy_tv(cm->cm_class->class_members_tv + cm->cm_idx,
4048 STACK_TV_BOT(0));
Bram Moolenaard505d172022-12-18 21:42:55 +00004049 ++ectx->ec_stack.ga_len;
4050 }
4051 break;
4052
4053 case ISN_STORE_CLASSMEMBER:
4054 {
4055 classmember_T *cm = &iptr->isn_arg.classmember;
4056 tv = &cm->cm_class->class_members_tv[cm->cm_idx];
4057 clear_tv(tv);
4058 *tv = *STACK_TV_BOT(-1);
4059 --ectx->ec_stack.ga_len;
4060 }
4061 break;
4062
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01004063 // Load or store variable or argument from outer scope.
Bram Moolenaar0186e582021-01-10 18:33:11 +01004064 case ISN_LOADOUTER:
4065 case ISN_STOREOUTER:
4066 {
4067 int depth = iptr->isn_arg.outer.outer_depth;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004068 outer_T *outer = ectx->ec_outer_ref == NULL ? NULL
4069 : ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +01004070
4071 while (depth > 1 && outer != NULL)
4072 {
4073 outer = outer->out_up;
4074 --depth;
4075 }
4076 if (outer == NULL)
4077 {
4078 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar69c76172021-12-02 16:38:52 +00004079 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx
4080 || ectx->ec_outer_ref == NULL)
4081 // Possibly :def function called from legacy
4082 // context.
4083 emsg(_(e_closure_called_from_invalid_context));
4084 else
4085 iemsg("LOADOUTER depth more than scope levels");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004086 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01004087 }
Bram Moolenaarcc341812022-09-19 15:54:34 +01004088 if (depth < 0)
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01004089 // Variable declared in loop. May be copied if the
4090 // loop block has already ended.
Bram Moolenaarcc341812022-09-19 15:54:34 +01004091 tv = ((typval_T *)outer->out_loop[-depth - 1]
4092 .stack->ga_data)
4093 + outer->out_loop[-depth - 1].var_idx
4094 + iptr->isn_arg.outer.outer_idx;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004095 else
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01004096 // Variable declared in a function. May be copied if
4097 // the function has already returned.
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004098 tv = ((typval_T *)outer->out_stack->ga_data)
4099 + outer->out_frame_idx + STACK_FRAME_SIZE
4100 + iptr->isn_arg.outer.outer_idx;
Bram Moolenaar0186e582021-01-10 18:33:11 +01004101 if (iptr->isn_type == ISN_LOADOUTER)
4102 {
Bram Moolenaar35578162021-08-02 19:10:38 +02004103 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004104 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01004105 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02004106 ++ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01004107 }
4108 else
4109 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004110 --ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01004111 clear_tv(tv);
4112 *tv = *STACK_TV_BOT(0);
4113 }
4114 }
4115 break;
4116
Bram Moolenaar752fc692021-01-04 21:57:11 +01004117 // unlet item in list or dict variable
4118 case ISN_UNLETINDEX:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00004119 if (execute_unletindex(iptr, ectx) == FAIL)
4120 goto on_error;
Bram Moolenaar752fc692021-01-04 21:57:11 +01004121 break;
4122
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01004123 // unlet range of items in list variable
4124 case ISN_UNLETRANGE:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00004125 if (execute_unletrange(iptr, ectx) == FAIL)
4126 goto on_error;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01004127 break;
4128
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004129 // push constant
4130 case ISN_PUSHNR:
4131 case ISN_PUSHBOOL:
4132 case ISN_PUSHSPEC:
4133 case ISN_PUSHF:
4134 case ISN_PUSHS:
4135 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004136 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004137 case ISN_PUSHCHANNEL:
4138 case ISN_PUSHJOB:
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00004139 case ISN_PUSHOBJ:
4140 case ISN_PUSHCLASS:
Bram Moolenaar35578162021-08-02 19:10:38 +02004141 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004142 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004143 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02004144 tv->v_lock = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004145 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004146 switch (iptr->isn_type)
4147 {
4148 case ISN_PUSHNR:
4149 tv->v_type = VAR_NUMBER;
4150 tv->vval.v_number = iptr->isn_arg.number;
4151 break;
4152 case ISN_PUSHBOOL:
4153 tv->v_type = VAR_BOOL;
4154 tv->vval.v_number = iptr->isn_arg.number;
4155 break;
4156 case ISN_PUSHSPEC:
4157 tv->v_type = VAR_SPECIAL;
4158 tv->vval.v_number = iptr->isn_arg.number;
4159 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004160 case ISN_PUSHF:
4161 tv->v_type = VAR_FLOAT;
4162 tv->vval.v_float = iptr->isn_arg.fnumber;
4163 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004164 case ISN_PUSHBLOB:
4165 blob_copy(iptr->isn_arg.blob, tv);
4166 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004167 case ISN_PUSHFUNC:
4168 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01004169 if (iptr->isn_arg.string == NULL)
4170 tv->vval.v_string = NULL;
4171 else
4172 tv->vval.v_string =
4173 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004174 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004175 case ISN_PUSHCHANNEL:
4176#ifdef FEAT_JOB_CHANNEL
4177 tv->v_type = VAR_CHANNEL;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00004178 tv->vval.v_channel = NULL;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004179#endif
4180 break;
4181 case ISN_PUSHJOB:
4182#ifdef FEAT_JOB_CHANNEL
4183 tv->v_type = VAR_JOB;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00004184 tv->vval.v_job = NULL;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004185#endif
4186 break;
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00004187 case ISN_PUSHOBJ:
4188 tv->v_type = VAR_OBJECT;
4189 tv->vval.v_object = NULL;
4190 break;
4191 case ISN_PUSHCLASS:
4192 tv->v_type = VAR_CLASS;
Bram Moolenaar30a84472023-02-27 08:07:14 +00004193 tv->vval.v_class = iptr->isn_arg.classarg;
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00004194 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004195 default:
4196 tv->v_type = VAR_STRING;
Bram Moolenaarf8691002022-03-10 12:20:53 +00004197 tv->vval.v_string = iptr->isn_arg.string == NULL
4198 ? NULL : vim_strsave(iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004199 }
4200 break;
4201
Bram Moolenaar06b77222022-01-25 15:51:56 +00004202 case ISN_AUTOLOAD:
4203 {
4204 char_u *name = iptr->isn_arg.string;
4205
4206 (void)script_autoload(name, FALSE);
4207 if (find_func(name, TRUE))
4208 {
4209 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
4210 goto theend;
4211 tv = STACK_TV_BOT(0);
4212 tv->v_lock = 0;
4213 ++ectx->ec_stack.ga_len;
4214 tv->v_type = VAR_FUNC;
4215 tv->vval.v_string = vim_strsave(name);
4216 }
4217 else
4218 {
4219 int res = load_namespace_var(ectx, ISN_LOADG, iptr);
4220
4221 if (res == NOTDONE)
4222 goto theend;
4223 if (res == FAIL)
4224 goto on_error;
4225 }
4226 }
4227 break;
4228
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004229 case ISN_UNLET:
4230 if (do_unlet(iptr->isn_arg.unlet.ul_name,
4231 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02004232 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004233 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02004234 case ISN_UNLETENV:
LemonBoy77142312022-04-15 20:50:46 +01004235 vim_unsetenv_ext(iptr->isn_arg.unlet.ul_name);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02004236 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004237
Bram Moolenaaraacc9662021-08-13 19:40:51 +02004238 case ISN_LOCKUNLOCK:
4239 {
Ernie Raelee865f32023-09-29 19:53:55 +02004240#ifdef LOG_LOCKVAR
4241 ch_log(NULL, "LKVAR: execute INS_LOCKUNLOCK isn_arg %s",
4242 iptr->isn_arg.string);
4243#endif
Ernie Rael4c8da022023-10-11 21:35:11 +02004244 lval_root_T *lval_root_save = lval_root;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02004245
4246 // Stack has the local variable, argument the whole :lock
4247 // or :unlock command, like ISN_EXEC.
4248 --ectx->ec_stack.ga_len;
Ernie Rael4c8da022023-10-11 21:35:11 +02004249 lval_root_T root = { .lr_tv = STACK_TV_BOT(0),
4250 .lr_cl_exec = iptr->isn_arg.lockunlock.lu_cl_exec,
4251 .lr_is_arg = iptr->isn_arg.lockunlock.lu_is_arg };
Ernie Rael64885642023-10-04 20:16:22 +02004252 lval_root = &root;
Ernie Rael4c8da022023-10-11 21:35:11 +02004253 int res = exec_command(iptr,
Ernie Rael64885642023-10-04 20:16:22 +02004254 iptr->isn_arg.lockunlock.lu_string);
4255 clear_tv(root.lr_tv);
Bram Moolenaaraacc9662021-08-13 19:40:51 +02004256 lval_root = lval_root_save;
4257 if (res == FAIL)
4258 goto on_error;
4259 }
4260 break;
4261
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02004262 case ISN_LOCKCONST:
4263 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
4264 break;
4265
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004266 // create a list from items on the stack; uses a single allocation
4267 // for the list header and the items
4268 case ISN_NEWLIST:
Bram Moolenaar4c137212021-04-19 16:48:48 +02004269 if (exe_newlist(iptr->isn_arg.number, ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004270 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004271 break;
4272
4273 // create a dict from items on the stack
4274 case ISN_NEWDICT:
4275 {
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01004276 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004277
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01004278 SOURCING_LNUM = iptr->isn_lnum;
4279 res = exe_newdict(iptr->isn_arg.number, ectx);
4280 if (res == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004281 goto theend;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01004282 if (res == MAYBE)
4283 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004284 }
4285 break;
4286
LemonBoy372bcce2022-04-25 12:43:20 +01004287 case ISN_CONCAT:
4288 if (exe_concat(iptr->isn_arg.number, ectx) == FAIL)
4289 goto theend;
4290 break;
4291
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004292 // create a partial with NULL value
4293 case ISN_NEWPARTIAL:
4294 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
4295 goto theend;
4296 ++ectx->ec_stack.ga_len;
4297 tv = STACK_TV_BOT(-1);
4298 tv->v_type = VAR_PARTIAL;
4299 tv->v_lock = 0;
4300 tv->vval.v_partial = NULL;
4301 break;
4302
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004303 // call a :def function
4304 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02004305 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01004306 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
4307 NULL,
4308 iptr->isn_arg.dfunc.cdf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02004309 ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02004310 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004311 break;
4312
Bram Moolenaard0200c82023-01-28 15:19:40 +00004313 // call a method on an interface
4314 case ISN_METHODCALL:
4315 {
Bram Moolenaar094cf9f2023-02-10 15:52:25 +00004316 cmfunc_T *mfunc = iptr->isn_arg.mfunc;
4317
Bram Moolenaard0200c82023-01-28 15:19:40 +00004318 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar094cf9f2023-02-10 15:52:25 +00004319 tv = STACK_TV_BOT(-1 - mfunc->cmf_argcount);
Bram Moolenaard0200c82023-01-28 15:19:40 +00004320 if (tv->v_type != VAR_OBJECT)
4321 {
4322 object_required_error(tv);
4323 goto on_error;
4324 }
4325 object_T *obj = tv->vval.v_object;
4326 class_T *cl = obj->obj_class;
4327
4328 // convert the interface index to the object index
Bram Moolenaard0200c82023-01-28 15:19:40 +00004329 int idx = object_index_from_itf_index(mfunc->cmf_itf,
Yegappan Lakshmanan5a05d372023-09-29 19:43:11 +02004330 TRUE, mfunc->cmf_idx, cl);
Bram Moolenaard0200c82023-01-28 15:19:40 +00004331
4332 if (call_ufunc(cl->class_obj_methods[idx], NULL,
4333 mfunc->cmf_argcount, ectx, NULL, NULL) == FAIL)
4334 goto on_error;
4335 }
4336 break;
4337
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004338 // call a builtin function
4339 case ISN_BCALL:
4340 SOURCING_LNUM = iptr->isn_lnum;
4341 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
4342 iptr->isn_arg.bfunc.cbf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02004343 ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004344 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004345 break;
4346
4347 // call a funcref or partial
4348 case ISN_PCALL:
4349 {
4350 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
4351 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02004352 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004353
4354 SOURCING_LNUM = iptr->isn_lnum;
4355 if (pfunc->cpf_top)
4356 {
4357 // funcref is above the arguments
4358 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
4359 }
4360 else
4361 {
4362 // Get the funcref from the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004363 --ectx->ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02004364 partial_tv = *STACK_TV_BOT(0);
4365 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004366 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004367 r = call_partial(tv, pfunc->cpf_argcount, ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02004368 if (tv == &partial_tv)
4369 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004370 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004371 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004372 }
4373 break;
4374
Bram Moolenaarbd5da372020-03-31 23:13:10 +02004375 case ISN_PCALL_END:
4376 // PCALL finished, arguments have been consumed and replaced by
4377 // the return value. Now clear the funcref from the stack,
4378 // and move the return value in its place.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004379 --ectx->ec_stack.ga_len;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02004380 clear_tv(STACK_TV_BOT(-1));
4381 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
4382 break;
4383
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004384 // call a user defined function or funcref/partial
4385 case ISN_UCALL:
4386 {
4387 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
4388
4389 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01004390 if (call_eval_func(cufunc->cuf_name, cufunc->cuf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02004391 ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004392 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004393 }
4394 break;
4395
Bram Moolenaar1d84f762022-09-03 21:35:53 +01004396 // :defer func(arg)
4397 case ISN_DEFER:
Bram Moolenaar806a2732022-09-04 15:40:36 +01004398 if (defer_command(iptr->isn_arg.defer.defer_var_idx,
Bram Moolenaar1d84f762022-09-03 21:35:53 +01004399 iptr->isn_arg.defer.defer_argcount, ectx) == FAIL)
4400 goto on_error;
4401 break;
4402
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004403 // Return from a :def function call without a value.
4404 // Return from a constructor.
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02004405 case ISN_RETURN_VOID:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004406 case ISN_RETURN_OBJECT:
Bram Moolenaar35578162021-08-02 19:10:38 +02004407 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004408 goto theend;
Bram Moolenaar299f3032021-01-08 20:53:09 +01004409 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004410 ++ectx->ec_stack.ga_len;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004411 if (iptr->isn_type == ISN_RETURN_VOID)
4412 {
4413 tv->v_type = VAR_VOID;
4414 tv->vval.v_number = 0;
4415 tv->v_lock = 0;
4416 }
4417 else
4418 {
4419 *tv = *STACK_TV_VAR(0);
4420 ++tv->vval.v_object->obj_refcount;
4421 }
Bram Moolenaar299f3032021-01-08 20:53:09 +01004422 // FALLTHROUGH
4423
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02004424 // return from a :def function call with what is on the stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004425 case ISN_RETURN:
4426 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004427 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01004428 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01004429
4430 if (trystack->ga_len > 0)
4431 trycmd = ((trycmd_T *)trystack->ga_data)
4432 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004433 if (trycmd != NULL
Bram Moolenaar4c137212021-04-19 16:48:48 +02004434 && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004435 {
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01004436 // jump to ":finally" or ":endtry"
4437 if (trycmd->tcd_finally_idx != 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004438 ectx->ec_iidx = trycmd->tcd_finally_idx;
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01004439 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02004440 ectx->ec_iidx = trycmd->tcd_endtry_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004441 trycmd->tcd_return = TRUE;
4442 }
4443 else
Bram Moolenaard032f342020-07-18 18:13:02 +02004444 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004445 }
4446 break;
4447
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004448 // push a partial, a reference to a compiled function
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004449 case ISN_FUNCREF:
4450 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004451 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
4452 ufunc_T *ufunc;
4453 funcref_T *funcref = &iptr->isn_arg.funcref;
4454 funcref_extra_T *extra = funcref->fr_extra;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004455
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004456 if (pt == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004457 goto theend;
Bram Moolenaar35578162021-08-02 19:10:38 +02004458 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02004459 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004460 vim_free(pt);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004461 goto theend;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004462 }
Bram Moolenaar313e4722023-02-08 20:55:27 +00004463 if (extra != NULL && extra->fre_class != NULL)
4464 {
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +02004465 class_T *cl;
4466 if (extra->fre_object_method)
Bram Moolenaar313e4722023-02-08 20:55:27 +00004467 {
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +02004468 tv = STACK_TV_BOT(-1);
4469 if (tv->v_type != VAR_OBJECT)
4470 {
4471 object_required_error(tv);
4472 vim_free(pt);
4473 goto on_error;
4474 }
Bram Moolenaar313e4722023-02-08 20:55:27 +00004475
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +02004476 object_T *obj = tv->vval.v_object;
4477 cl = obj->obj_class;
4478 // drop the value from the stack
4479 clear_tv(tv);
4480 --ectx->ec_stack.ga_len;
4481
4482 pt->pt_obj = obj;
4483 ++obj->obj_refcount;
4484 }
4485 else
4486 cl = extra->fre_class;
4487
4488 if (extra->fre_object_method)
4489 {
4490 // object method
4491 // convert the interface index to the object index
4492 int idx =
4493 object_index_from_itf_index(extra->fre_class,
4494 TRUE, extra->fre_method_idx, cl);
4495 ufunc = cl->class_obj_methods[idx];
4496 }
4497 else
4498 {
4499 // class method
4500 ufunc =
4501 cl->class_class_functions[extra->fre_method_idx];
4502 }
Bram Moolenaar313e4722023-02-08 20:55:27 +00004503 }
4504 else if (extra == NULL || extra->fre_func_name == NULL)
Bram Moolenaar38453522021-11-28 22:00:12 +00004505 {
4506 dfunc_T *pt_dfunc = ((dfunc_T *)def_functions.ga_data)
4507 + funcref->fr_dfunc_idx;
4508
4509 ufunc = pt_dfunc->df_ufunc;
4510 }
4511 else
Bram Moolenaar313e4722023-02-08 20:55:27 +00004512 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004513 ufunc = find_func(extra->fre_func_name, FALSE);
Bram Moolenaar313e4722023-02-08 20:55:27 +00004514 }
Bram Moolenaar56acd1f2022-02-18 13:24:52 +00004515 if (ufunc == NULL)
4516 {
4517 SOURCING_LNUM = iptr->isn_lnum;
4518 iemsg("ufunc unexpectedly NULL for FUNCREF");
4519 goto theend;
4520 }
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004521 if (fill_partial_and_closure(pt, ufunc,
Bram Moolenaarcc341812022-09-19 15:54:34 +01004522 extra == NULL ? NULL : &extra->fre_loopvar_info,
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004523 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004524 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004525 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004526 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004527 tv->vval.v_partial = pt;
4528 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02004529 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004530 }
4531 break;
4532
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004533 // Create a global function from a lambda.
4534 case ISN_NEWFUNC:
4535 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004536 newfuncarg_T *arg = iptr->isn_arg.newfunc.nf_arg;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004537
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004538 if (copy_lambda_to_global_func(arg->nfa_lambda,
Bram Moolenaarcc341812022-09-19 15:54:34 +01004539 arg->nfa_global, &arg->nfa_loopvar_info,
4540 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004541 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004542 }
4543 break;
4544
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004545 // List functions
4546 case ISN_DEF:
4547 if (iptr->isn_arg.string == NULL)
4548 list_functions(NULL);
4549 else
4550 {
Bram Moolenaar14336722022-01-08 16:02:59 +00004551 exarg_T ea;
4552 garray_T lines_to_free;
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004553
4554 CLEAR_FIELD(ea);
4555 ea.cmd = ea.arg = iptr->isn_arg.string;
Bram Moolenaar14336722022-01-08 16:02:59 +00004556 ga_init2(&lines_to_free, sizeof(char_u *), 50);
Bram Moolenaard4a9b7f2023-05-23 14:48:42 +01004557 SOURCING_LNUM = iptr->isn_lnum;
h-eastb895b0f2023-09-24 15:46:31 +02004558 define_function(&ea, NULL, &lines_to_free, 0, NULL, 0);
Bram Moolenaar14336722022-01-08 16:02:59 +00004559 ga_clear_strings(&lines_to_free);
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004560 }
4561 break;
4562
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004563 // jump if a condition is met
4564 case ISN_JUMP:
4565 {
4566 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004567 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004568 int jump = TRUE;
4569
4570 if (when != JUMP_ALWAYS)
4571 {
4572 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004573 if (when == JUMP_IF_COND_FALSE
Bram Moolenaar13106602020-10-04 16:06:05 +02004574 || when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004575 || when == JUMP_IF_COND_TRUE)
4576 {
4577 SOURCING_LNUM = iptr->isn_lnum;
4578 jump = tv_get_bool_chk(tv, &error);
4579 if (error)
4580 goto on_error;
4581 }
4582 else
4583 jump = tv2bool(tv);
Bram Moolenaarf6ced982022-04-28 12:00:49 +01004584 if (when == JUMP_IF_FALSE || when == JUMP_IF_COND_FALSE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004585 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01004586 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004587 {
4588 // drop the value from the stack
4589 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004590 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004591 }
4592 }
4593 if (jump)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004594 ectx->ec_iidx = iptr->isn_arg.jump.jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004595 }
4596 break;
4597
Bram Moolenaarb46c0832022-09-15 17:19:37 +01004598 // "while": jump to end if a condition is false
4599 case ISN_WHILE:
4600 {
4601 int error = FALSE;
4602 int jump = TRUE;
4603
4604 tv = STACK_TV_BOT(-1);
4605 SOURCING_LNUM = iptr->isn_lnum;
4606 jump = !tv_get_bool_chk(tv, &error);
4607 if (error)
4608 goto on_error;
4609 // drop the value from the stack
4610 clear_tv(tv);
4611 --ectx->ec_stack.ga_len;
4612 if (jump)
4613 ectx->ec_iidx = iptr->isn_arg.whileloop.while_end;
4614
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01004615 // Store the current funcref count, may be used by
Bram Moolenaarcc341812022-09-19 15:54:34 +01004616 // ISN_ENDLOOP later
Bram Moolenaarb46c0832022-09-15 17:19:37 +01004617 tv = STACK_TV_VAR(
4618 iptr->isn_arg.whileloop.while_funcref_idx);
4619 tv->vval.v_number = ectx->ec_funcrefs.ga_len;
4620 }
4621 break;
4622
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02004623 // Jump if an argument with a default value was already set and not
4624 // v:none.
4625 case ISN_JUMP_IF_ARG_SET:
Bram Moolenaar65b0d162022-12-13 18:43:22 +00004626 case ISN_JUMP_IF_ARG_NOT_SET:
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02004627 tv = STACK_TV_VAR(iptr->isn_arg.jumparg.jump_arg_off);
Bram Moolenaar65b0d162022-12-13 18:43:22 +00004628 int arg_set = tv->v_type != VAR_UNKNOWN
4629 && !(tv->v_type == VAR_SPECIAL
4630 && tv->vval.v_number == VVAL_NONE);
4631 if (iptr->isn_type == ISN_JUMP_IF_ARG_SET ? arg_set : !arg_set)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004632 ectx->ec_iidx = iptr->isn_arg.jumparg.jump_where;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02004633 break;
4634
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004635 // top of a for loop
4636 case ISN_FOR:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00004637 if (execute_for(iptr, ectx) == FAIL)
4638 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004639 break;
4640
Bram Moolenaarb46c0832022-09-15 17:19:37 +01004641 // end of a for or while loop
4642 case ISN_ENDLOOP:
4643 if (execute_endloop(iptr, ectx) == FAIL)
4644 goto theend;
4645 break;
4646
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004647 // start of ":try" block
4648 case ISN_TRY:
4649 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01004650 trycmd_T *trycmd = NULL;
4651
Bram Moolenaar35578162021-08-02 19:10:38 +02004652 if (GA_GROW_FAILS(&ectx->ec_trystack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004653 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004654 trycmd = ((trycmd_T *)ectx->ec_trystack.ga_data)
4655 + ectx->ec_trystack.ga_len;
4656 ++ectx->ec_trystack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004657 ++trylevel;
Bram Moolenaar8d4be892021-02-13 18:33:02 +01004658 CLEAR_POINTER(trycmd);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004659 trycmd->tcd_frame_idx = ectx->ec_frame_idx;
4660 trycmd->tcd_stack_len = ectx->ec_stack.ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01004661 trycmd->tcd_catch_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00004662 iptr->isn_arg.tryref.try_ref->try_catch;
Bram Moolenaara91a7132021-03-25 21:12:15 +01004663 trycmd->tcd_finally_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00004664 iptr->isn_arg.tryref.try_ref->try_finally;
Bram Moolenaara91a7132021-03-25 21:12:15 +01004665 trycmd->tcd_endtry_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00004666 iptr->isn_arg.tryref.try_ref->try_endtry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004667 }
4668 break;
4669
4670 case ISN_PUSHEXC:
4671 if (current_exception == NULL)
4672 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004673 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004674 iemsg("Evaluating catch while current_exception is NULL");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004675 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004676 }
Bram Moolenaar35578162021-08-02 19:10:38 +02004677 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004678 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004679 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004680 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004681 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02004682 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004683 tv->vval.v_string = vim_strsave(
4684 (char_u *)current_exception->value);
4685 break;
4686
4687 case ISN_CATCH:
4688 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004689 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar06651632022-04-27 17:54:25 +01004690 trycmd_T *trycmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004691
Bram Moolenaar4c137212021-04-19 16:48:48 +02004692 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar06651632022-04-27 17:54:25 +01004693 trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004694 + trystack->ga_len - 1;
Bram Moolenaar06651632022-04-27 17:54:25 +01004695 trycmd->tcd_caught = TRUE;
4696 trycmd->tcd_did_throw = FALSE;
4697
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004698 did_emsg = got_int = did_throw = FALSE;
Bram Moolenaar1430cee2021-01-17 19:20:32 +01004699 force_abort = need_rethrow = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004700 catch_exception(current_exception);
4701 }
4702 break;
4703
Bram Moolenaarc150c092021-02-13 15:02:46 +01004704 case ISN_TRYCONT:
4705 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004706 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004707 trycont_T *trycont = &iptr->isn_arg.trycont;
4708 int i;
4709 trycmd_T *trycmd;
4710 int iidx = trycont->tct_where;
4711
4712 if (trystack->ga_len < trycont->tct_levels)
4713 {
4714 siemsg("TRYCONT: expected %d levels, found %d",
4715 trycont->tct_levels, trystack->ga_len);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004716 goto theend;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004717 }
4718 // Make :endtry jump to any outer try block and the last
4719 // :endtry inside the loop to the loop start.
4720 for (i = trycont->tct_levels; i > 0; --i)
4721 {
4722 trycmd = ((trycmd_T *)trystack->ga_data)
4723 + trystack->ga_len - i;
Bram Moolenaar2e34c342021-03-14 12:13:33 +01004724 // Add one to tcd_cont to be able to jump to
4725 // instruction with index zero.
4726 trycmd->tcd_cont = iidx + 1;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01004727 iidx = trycmd->tcd_finally_idx == 0
4728 ? trycmd->tcd_endtry_idx : trycmd->tcd_finally_idx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004729 }
4730 // jump to :finally or :endtry of current try statement
Bram Moolenaar4c137212021-04-19 16:48:48 +02004731 ectx->ec_iidx = iidx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004732 }
4733 break;
4734
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01004735 case ISN_FINALLY:
4736 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004737 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01004738 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
4739 + trystack->ga_len - 1;
4740
4741 // Reset the index to avoid a return statement jumps here
4742 // again.
4743 trycmd->tcd_finally_idx = 0;
4744 break;
4745 }
4746
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004747 // end of ":try" block
4748 case ISN_ENDTRY:
4749 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004750 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar06651632022-04-27 17:54:25 +01004751 trycmd_T *trycmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004752
Bram Moolenaar06651632022-04-27 17:54:25 +01004753 --trystack->ga_len;
4754 --trylevel;
4755 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len;
4756 if (trycmd->tcd_did_throw)
4757 did_throw = TRUE;
4758 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004759 {
Bram Moolenaar06651632022-04-27 17:54:25 +01004760 // discard the exception
4761 if (caught_stack == current_exception)
4762 caught_stack = caught_stack->caught;
4763 discard_current_exception();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004764 }
Bram Moolenaar06651632022-04-27 17:54:25 +01004765
4766 if (trycmd->tcd_return)
4767 goto func_return;
4768
4769 while (ectx->ec_stack.ga_len > trycmd->tcd_stack_len)
4770 {
4771 --ectx->ec_stack.ga_len;
4772 clear_tv(STACK_TV_BOT(0));
4773 }
4774 if (trycmd->tcd_cont != 0)
4775 // handling :continue: jump to outer try block or
4776 // start of the loop
4777 ectx->ec_iidx = trycmd->tcd_cont - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004778 }
4779 break;
4780
4781 case ISN_THROW:
Bram Moolenaar8f81b222021-01-14 21:47:06 +01004782 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004783 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02004784
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004785 if (trystack->ga_len == 0 && trylevel == 0 && emsg_silent)
4786 {
Bram Moolenaar3ef2e412023-04-30 18:50:48 +01004787 // Throwing an exception while using "silent!" causes
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004788 // the function to abort but not display an error.
4789 tv = STACK_TV_BOT(-1);
4790 clear_tv(tv);
4791 tv->v_type = VAR_NUMBER;
4792 tv->vval.v_number = 0;
4793 goto done;
4794 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004795 --ectx->ec_stack.ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004796 tv = STACK_TV_BOT(0);
4797 if (tv->vval.v_string == NULL
4798 || *skipwhite(tv->vval.v_string) == NUL)
4799 {
4800 vim_free(tv->vval.v_string);
4801 SOURCING_LNUM = iptr->isn_lnum;
4802 emsg(_(e_throw_with_empty_string));
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004803 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004804 }
4805
4806 // Inside a "catch" we need to first discard the caught
4807 // exception.
4808 if (trystack->ga_len > 0)
4809 {
4810 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
4811 + trystack->ga_len - 1;
4812 if (trycmd->tcd_caught && current_exception != NULL)
4813 {
4814 // discard the exception
4815 if (caught_stack == current_exception)
4816 caught_stack = caught_stack->caught;
4817 discard_current_exception();
4818 trycmd->tcd_caught = FALSE;
4819 }
4820 }
4821
Bram Moolenaar90a57162022-02-12 14:23:17 +00004822 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004823 if (throw_exception(tv->vval.v_string, ET_USER, NULL)
4824 == FAIL)
4825 {
4826 vim_free(tv->vval.v_string);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004827 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004828 }
4829 did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004830 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004831 break;
4832
4833 // compare with special values
4834 case ISN_COMPAREBOOL:
4835 case ISN_COMPARESPECIAL:
4836 {
4837 typval_T *tv1 = STACK_TV_BOT(-2);
4838 typval_T *tv2 = STACK_TV_BOT(-1);
4839 varnumber_T arg1 = tv1->vval.v_number;
4840 varnumber_T arg2 = tv2->vval.v_number;
4841 int res;
4842
Bram Moolenaar06651632022-04-27 17:54:25 +01004843 if (iptr->isn_arg.op.op_type == EXPR_EQUAL)
4844 res = arg1 == arg2;
4845 else
4846 res = arg1 != arg2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004847
Bram Moolenaar4c137212021-04-19 16:48:48 +02004848 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004849 tv1->v_type = VAR_BOOL;
4850 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4851 }
4852 break;
4853
Bram Moolenaar7a222242022-03-01 19:23:24 +00004854 case ISN_COMPARENULL:
4855 {
4856 typval_T *tv1 = STACK_TV_BOT(-2);
4857 typval_T *tv2 = STACK_TV_BOT(-1);
4858 int res;
4859
4860 res = typval_compare_null(tv1, tv2);
4861 if (res == MAYBE)
4862 goto on_error;
4863 if (iptr->isn_arg.op.op_type == EXPR_NEQUAL)
4864 res = !res;
4865 clear_tv(tv1);
4866 clear_tv(tv2);
4867 --ectx->ec_stack.ga_len;
4868 tv1->v_type = VAR_BOOL;
4869 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4870 }
4871 break;
4872
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004873 // Operation with two number arguments
4874 case ISN_OPNR:
4875 case ISN_COMPARENR:
4876 {
4877 typval_T *tv1 = STACK_TV_BOT(-2);
4878 typval_T *tv2 = STACK_TV_BOT(-1);
4879 varnumber_T arg1 = tv1->vval.v_number;
4880 varnumber_T arg2 = tv2->vval.v_number;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004881 varnumber_T res = 0;
4882 int div_zero = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004883
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004884 if (iptr->isn_arg.op.op_type == EXPR_LSHIFT
4885 || iptr->isn_arg.op.op_type == EXPR_RSHIFT)
4886 {
4887 if (arg2 < 0)
4888 {
4889 SOURCING_LNUM = iptr->isn_lnum;
dundargocc57b5bc2022-11-02 13:30:51 +00004890 emsg(_(e_bitshift_ops_must_be_positive));
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004891 goto on_error;
4892 }
4893 }
4894
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004895 switch (iptr->isn_arg.op.op_type)
4896 {
4897 case EXPR_MULT: res = arg1 * arg2; break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004898 case EXPR_DIV: if (arg2 == 0)
4899 div_zero = TRUE;
4900 else
4901 res = arg1 / arg2;
4902 break;
4903 case EXPR_REM: if (arg2 == 0)
4904 div_zero = TRUE;
4905 else
4906 res = arg1 % arg2;
4907 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004908 case EXPR_SUB: res = arg1 - arg2; break;
4909 case EXPR_ADD: res = arg1 + arg2; break;
4910
4911 case EXPR_EQUAL: res = arg1 == arg2; break;
4912 case EXPR_NEQUAL: res = arg1 != arg2; break;
4913 case EXPR_GREATER: res = arg1 > arg2; break;
4914 case EXPR_GEQUAL: res = arg1 >= arg2; break;
4915 case EXPR_SMALLER: res = arg1 < arg2; break;
4916 case EXPR_SEQUAL: res = arg1 <= arg2; break;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004917 case EXPR_LSHIFT: if (arg2 > MAX_LSHIFT_BITS)
4918 res = 0;
4919 else
Bram Moolenaar68e64d22022-05-22 22:07:52 +01004920 res = (uvarnumber_T)arg1 << arg2;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004921 break;
4922 case EXPR_RSHIFT: if (arg2 > MAX_LSHIFT_BITS)
4923 res = 0;
4924 else
Bram Moolenaar338bf582022-05-22 20:16:32 +01004925 res = (uvarnumber_T)arg1 >> arg2;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004926 break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004927 default: break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004928 }
4929
Bram Moolenaar4c137212021-04-19 16:48:48 +02004930 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004931 if (iptr->isn_type == ISN_COMPARENR)
4932 {
4933 tv1->v_type = VAR_BOOL;
4934 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4935 }
4936 else
4937 tv1->vval.v_number = res;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004938 if (div_zero)
4939 {
4940 SOURCING_LNUM = iptr->isn_lnum;
4941 emsg(_(e_divide_by_zero));
4942 goto on_error;
4943 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004944 }
4945 break;
4946
4947 // Computation with two float arguments
4948 case ISN_OPFLOAT:
4949 case ISN_COMPAREFLOAT:
4950 {
4951 typval_T *tv1 = STACK_TV_BOT(-2);
4952 typval_T *tv2 = STACK_TV_BOT(-1);
4953 float_T arg1 = tv1->vval.v_float;
4954 float_T arg2 = tv2->vval.v_float;
4955 float_T res = 0;
4956 int cmp = FALSE;
4957
4958 switch (iptr->isn_arg.op.op_type)
4959 {
4960 case EXPR_MULT: res = arg1 * arg2; break;
4961 case EXPR_DIV: res = arg1 / arg2; break;
4962 case EXPR_SUB: res = arg1 - arg2; break;
4963 case EXPR_ADD: res = arg1 + arg2; break;
4964
4965 case EXPR_EQUAL: cmp = arg1 == arg2; break;
4966 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
4967 case EXPR_GREATER: cmp = arg1 > arg2; break;
4968 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
4969 case EXPR_SMALLER: cmp = arg1 < arg2; break;
4970 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
4971 default: cmp = 0; break;
4972 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004973 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004974 if (iptr->isn_type == ISN_COMPAREFLOAT)
4975 {
4976 tv1->v_type = VAR_BOOL;
4977 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
4978 }
4979 else
4980 tv1->vval.v_float = res;
4981 }
4982 break;
4983
4984 case ISN_COMPARELIST:
Bram Moolenaar265f8112021-12-19 12:33:05 +00004985 case ISN_COMPAREDICT:
4986 case ISN_COMPAREFUNC:
4987 case ISN_COMPARESTRING:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004988 case ISN_COMPAREBLOB:
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00004989 case ISN_COMPARECLASS:
4990 case ISN_COMPAREOBJECT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004991 {
4992 typval_T *tv1 = STACK_TV_BOT(-2);
4993 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar265f8112021-12-19 12:33:05 +00004994 exprtype_T exprtype = iptr->isn_arg.op.op_type;
4995 int ic = iptr->isn_arg.op.op_ic;
4996 int res = FALSE;
4997 int status = OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004998
Bram Moolenaar265f8112021-12-19 12:33:05 +00004999 SOURCING_LNUM = iptr->isn_lnum;
5000 if (iptr->isn_type == ISN_COMPARELIST)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005001 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00005002 status = typval_compare_list(tv1, tv2,
5003 exprtype, ic, &res);
5004 }
5005 else if (iptr->isn_type == ISN_COMPAREDICT)
5006 {
5007 status = typval_compare_dict(tv1, tv2,
5008 exprtype, ic, &res);
5009 }
5010 else if (iptr->isn_type == ISN_COMPAREFUNC)
5011 {
5012 status = typval_compare_func(tv1, tv2,
5013 exprtype, ic, &res);
5014 }
5015 else if (iptr->isn_type == ISN_COMPARESTRING)
5016 {
5017 status = typval_compare_string(tv1, tv2,
5018 exprtype, ic, &res);
5019 }
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00005020 else if (iptr->isn_type == ISN_COMPAREBLOB)
Bram Moolenaar265f8112021-12-19 12:33:05 +00005021 {
5022 status = typval_compare_blob(tv1, tv2, exprtype, &res);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005023 }
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00005024 else if (iptr->isn_type == ISN_COMPARECLASS)
5025 {
Bram Moolenaar03ff0c62023-01-02 20:38:01 +00005026 status = typval_compare_class(tv1, tv2,
5027 exprtype, FALSE, &res);
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00005028 }
5029 else // ISN_COMPAREOBJECT
5030 {
5031 status = typval_compare_object(tv1, tv2,
Bram Moolenaar03ff0c62023-01-02 20:38:01 +00005032 exprtype, FALSE, &res);
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00005033 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005034 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005035 clear_tv(tv1);
5036 clear_tv(tv2);
5037 tv1->v_type = VAR_BOOL;
Bram Moolenaar265f8112021-12-19 12:33:05 +00005038 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
5039 if (status == FAIL)
5040 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005041 }
5042 break;
5043
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005044 case ISN_COMPAREANY:
5045 {
5046 typval_T *tv1 = STACK_TV_BOT(-2);
5047 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar657137c2021-01-09 15:45:23 +01005048 exprtype_T exprtype = iptr->isn_arg.op.op_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005049 int ic = iptr->isn_arg.op.op_ic;
Bram Moolenaar265f8112021-12-19 12:33:05 +00005050 int status;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005051
Bram Moolenaareb26f432020-09-14 16:50:05 +02005052 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar265f8112021-12-19 12:33:05 +00005053 status = typval_compare(tv1, tv2, exprtype, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005054 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005055 --ectx->ec_stack.ga_len;
Bram Moolenaar265f8112021-12-19 12:33:05 +00005056 if (status == FAIL)
5057 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005058 }
5059 break;
5060
5061 case ISN_ADDLIST:
5062 case ISN_ADDBLOB:
5063 {
5064 typval_T *tv1 = STACK_TV_BOT(-2);
5065 typval_T *tv2 = STACK_TV_BOT(-1);
5066
Bram Moolenaar1dcae592020-10-19 19:02:42 +02005067 // add two lists or blobs
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005068 if (iptr->isn_type == ISN_ADDLIST)
Bram Moolenaar07802042021-09-09 23:01:14 +02005069 {
5070 if (iptr->isn_arg.op.op_type == EXPR_APPEND
5071 && tv1->vval.v_list != NULL)
5072 list_extend(tv1->vval.v_list, tv2->vval.v_list,
5073 NULL);
5074 else
5075 eval_addlist(tv1, tv2);
5076 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005077 else
5078 eval_addblob(tv1, tv2);
5079 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005080 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005081 }
5082 break;
5083
Bram Moolenaar1dcae592020-10-19 19:02:42 +02005084 case ISN_LISTAPPEND:
5085 {
5086 typval_T *tv1 = STACK_TV_BOT(-2);
5087 typval_T *tv2 = STACK_TV_BOT(-1);
5088 list_T *l = tv1->vval.v_list;
5089
5090 // add an item to a list
Bram Moolenaar1f4a3452022-01-01 18:29:21 +00005091 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02005092 if (l == NULL)
5093 {
Bram Moolenaar1dcae592020-10-19 19:02:42 +02005094 emsg(_(e_cannot_add_to_null_list));
5095 goto on_error;
5096 }
Bram Moolenaar1f4a3452022-01-01 18:29:21 +00005097 if (value_check_lock(l->lv_lock, NULL, FALSE))
5098 goto on_error;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02005099 if (list_append_tv(l, tv2) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005100 goto theend;
Bram Moolenaar955347c2020-10-19 23:01:46 +02005101 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005102 --ectx->ec_stack.ga_len;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02005103 }
5104 break;
5105
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02005106 case ISN_BLOBAPPEND:
5107 {
5108 typval_T *tv1 = STACK_TV_BOT(-2);
5109 typval_T *tv2 = STACK_TV_BOT(-1);
5110 blob_T *b = tv1->vval.v_blob;
5111 int error = FALSE;
5112 varnumber_T n;
5113
5114 // add a number to a blob
5115 if (b == NULL)
5116 {
5117 SOURCING_LNUM = iptr->isn_lnum;
5118 emsg(_(e_cannot_add_to_null_blob));
5119 goto on_error;
5120 }
5121 n = tv_get_number_chk(tv2, &error);
5122 if (error)
5123 goto on_error;
5124 ga_append(&b->bv_ga, (int)n);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005125 --ectx->ec_stack.ga_len;
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02005126 }
5127 break;
5128
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005129 // Computation with two arguments of unknown type
5130 case ISN_OPANY:
5131 {
5132 typval_T *tv1 = STACK_TV_BOT(-2);
5133 typval_T *tv2 = STACK_TV_BOT(-1);
5134 varnumber_T n1, n2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005135 float_T f1 = 0, f2 = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005136 int error = FALSE;
5137
5138 if (iptr->isn_arg.op.op_type == EXPR_ADD)
5139 {
5140 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
5141 {
5142 eval_addlist(tv1, tv2);
5143 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005144 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005145 break;
5146 }
5147 else if (tv1->v_type == VAR_BLOB
5148 && tv2->v_type == VAR_BLOB)
5149 {
5150 eval_addblob(tv1, tv2);
5151 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005152 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005153 break;
5154 }
5155 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005156 if (tv1->v_type == VAR_FLOAT)
5157 {
5158 f1 = tv1->vval.v_float;
5159 n1 = 0;
5160 }
5161 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005162 {
Bram Moolenaarf665e972020-12-05 19:17:16 +01005163 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005164 n1 = tv_get_number_chk(tv1, &error);
5165 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02005166 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005167 if (tv2->v_type == VAR_FLOAT)
5168 f1 = n1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005169 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005170 if (tv2->v_type == VAR_FLOAT)
5171 {
5172 f2 = tv2->vval.v_float;
5173 n2 = 0;
5174 }
5175 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005176 {
5177 n2 = tv_get_number_chk(tv2, &error);
5178 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02005179 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005180 if (tv1->v_type == VAR_FLOAT)
5181 f2 = n2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005182 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005183 // if there is a float on either side the result is a float
5184 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
5185 {
5186 switch (iptr->isn_arg.op.op_type)
5187 {
5188 case EXPR_MULT: f1 = f1 * f2; break;
5189 case EXPR_DIV: f1 = f1 / f2; break;
5190 case EXPR_SUB: f1 = f1 - f2; break;
5191 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005192 default: SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005193 emsg(_(e_cannot_use_percent_with_float));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02005194 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005195 }
5196 clear_tv(tv1);
5197 clear_tv(tv2);
5198 tv1->v_type = VAR_FLOAT;
5199 tv1->vval.v_float = f1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005200 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005201 }
5202 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005203 {
Bram Moolenaarb1f28572021-01-21 13:03:20 +01005204 int failed = FALSE;
5205
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005206 switch (iptr->isn_arg.op.op_type)
5207 {
5208 case EXPR_MULT: n1 = n1 * n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01005209 case EXPR_DIV: n1 = num_divide(n1, n2, &failed);
5210 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01005211 goto on_error;
5212 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005213 case EXPR_SUB: n1 = n1 - n2; break;
5214 case EXPR_ADD: n1 = n1 + n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01005215 default: n1 = num_modulus(n1, n2, &failed);
5216 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01005217 goto on_error;
5218 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005219 }
5220 clear_tv(tv1);
5221 clear_tv(tv2);
5222 tv1->v_type = VAR_NUMBER;
5223 tv1->vval.v_number = n1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005224 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005225 }
5226 }
5227 break;
5228
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005229 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005230 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005231 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005232 int is_slice = iptr->isn_type == ISN_STRSLICE;
5233 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005234 char_u *res;
5235
5236 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005237 // string slice: string is at stack-3, first index at
5238 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005239 if (is_slice)
5240 {
5241 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005242 n1 = tv->vval.v_number;
5243 }
5244
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005245 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005246 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005247
Bram Moolenaar4c137212021-04-19 16:48:48 +02005248 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005249 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005250 if (is_slice)
5251 // Slice: Select the characters from the string
Bram Moolenaar6601b622021-01-13 21:47:15 +01005252 res = string_slice(tv->vval.v_string, n1, n2, FALSE);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005253 else
5254 // Index: The resulting variable is a string of a
Bram Moolenaar0289a092021-03-14 18:40:19 +01005255 // single character (including composing characters).
5256 // If the index is too big or negative the result is
5257 // empty.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005258 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005259 vim_free(tv->vval.v_string);
5260 tv->vval.v_string = res;
5261 }
5262 break;
5263
5264 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02005265 case ISN_LISTSLICE:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02005266 case ISN_BLOBINDEX:
5267 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005268 {
Bram Moolenaarcfc30232021-04-11 20:26:34 +02005269 int is_slice = iptr->isn_type == ISN_LISTSLICE
5270 || iptr->isn_type == ISN_BLOBSLICE;
5271 int is_blob = iptr->isn_type == ISN_BLOBINDEX
5272 || iptr->isn_type == ISN_BLOBSLICE;
Bram Moolenaared591872020-08-15 22:14:53 +02005273 varnumber_T n1, n2;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02005274 typval_T *val_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005275
5276 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02005277 // list slice: list is at stack-3, indexes at stack-2 and
5278 // stack-1
Bram Moolenaarcfc30232021-04-11 20:26:34 +02005279 // Same for blob.
5280 val_tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005281
5282 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02005283 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005284 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02005285
5286 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005287 {
Bram Moolenaared591872020-08-15 22:14:53 +02005288 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02005289 n1 = tv->vval.v_number;
5290 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005291 }
Bram Moolenaared591872020-08-15 22:14:53 +02005292
Bram Moolenaar4c137212021-04-19 16:48:48 +02005293 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02005294 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02005295 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02005296 if (is_blob)
5297 {
5298 if (blob_slice_or_index(val_tv->vval.v_blob, is_slice,
5299 n1, n2, FALSE, tv) == FAIL)
5300 goto on_error;
5301 }
5302 else
5303 {
5304 if (list_slice_or_index(val_tv->vval.v_list, is_slice,
5305 n1, n2, FALSE, tv, TRUE) == FAIL)
5306 goto on_error;
5307 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005308 }
5309 break;
5310
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005311 case ISN_ANYINDEX:
5312 case ISN_ANYSLICE:
5313 {
5314 int is_slice = iptr->isn_type == ISN_ANYSLICE;
5315 typval_T *var1, *var2;
5316 int res;
5317
5318 // index: composite is at stack-2, index at stack-1
5319 // slice: composite is at stack-3, indexes at stack-2 and
5320 // stack-1
5321 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02005322 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005323 if (check_can_index(tv, TRUE, TRUE) == FAIL)
5324 goto on_error;
5325 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
5326 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
Bram Moolenaar6601b622021-01-13 21:47:15 +01005327 res = eval_index_inner(tv, is_slice, var1, var2,
5328 FALSE, NULL, -1, TRUE);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005329 clear_tv(var1);
5330 if (is_slice)
5331 clear_tv(var2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005332 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005333 if (res == FAIL)
5334 goto on_error;
5335 }
5336 break;
5337
Bram Moolenaar9af78762020-06-16 11:34:42 +02005338 case ISN_SLICE:
5339 {
5340 list_T *list;
5341 int count = iptr->isn_arg.number;
5342
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02005343 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02005344 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02005345 list = tv->vval.v_list;
5346
5347 // no error for short list, expect it to be checked earlier
5348 if (list != NULL && list->lv_len >= count)
5349 {
5350 list_T *newlist = list_slice(list,
5351 count, list->lv_len - 1);
5352
5353 if (newlist != NULL)
5354 {
5355 list_unref(list);
5356 tv->vval.v_list = newlist;
5357 ++newlist->lv_refcount;
5358 }
5359 }
5360 }
5361 break;
5362
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005363 case ISN_GETITEM:
5364 {
5365 listitem_T *li;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02005366 getitem_T *gi = &iptr->isn_arg.getitem;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005367
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02005368 // Get list item: list is at stack-1, push item.
5369 // List type and length is checked for when compiling.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02005370 tv = STACK_TV_BOT(-1 - gi->gi_with_op);
5371 li = list_find(tv->vval.v_list, gi->gi_index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005372
Bram Moolenaar35578162021-08-02 19:10:38 +02005373 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005374 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005375 ++ectx->ec_stack.ga_len;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005376 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
Bram Moolenaarf785aa12021-02-11 21:19:34 +01005377
5378 // Useful when used in unpack assignment. Reset at
5379 // ISN_DROP.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02005380 ectx->ec_where.wt_index = gi->gi_index + 1;
LemonBoyc5d27442023-08-19 13:02:35 +02005381 ectx->ec_where.wt_kind = WT_VARIABLE;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005382 }
5383 break;
5384
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005385 case ISN_MEMBER:
5386 {
5387 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005388 char_u *key;
5389 dictitem_T *di;
5390
5391 // dict member: dict is at stack-2, key at stack-1
5392 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02005393 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005394 dict = tv->vval.v_dict;
5395
5396 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02005397 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005398 key = tv->vval.v_string;
Bram Moolenaar086fc9a2020-10-30 18:33:02 +01005399 if (key == NULL)
5400 key = (char_u *)"";
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02005401
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005402 if ((di = dict_find(dict, key, -1)) == NULL)
5403 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005404 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00005405 semsg(_(e_key_not_present_in_dictionary_str), key);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01005406
5407 // If :silent! is used we will continue, make sure the
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005408 // stack contents makes sense and the dict stack is
5409 // updated.
Bram Moolenaar4029cab2020-12-05 18:13:27 +01005410 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005411 --ectx->ec_stack.ga_len;
Bram Moolenaar4029cab2020-12-05 18:13:27 +01005412 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005413 (void) dict_stack_save(tv);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01005414 tv->v_type = VAR_NUMBER;
5415 tv->vval.v_number = 0;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01005416 goto on_fatal_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005417 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005418 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005419 --ectx->ec_stack.ga_len;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005420 // Put the dict used on the dict stack, it might be used by
5421 // a dict function later.
Bram Moolenaar50788ef2020-07-05 16:51:26 +02005422 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005423 if (dict_stack_save(tv) == FAIL)
5424 goto on_fatal_error;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02005425 copy_tv(&di->di_tv, tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005426 }
5427 break;
5428
5429 // dict member with string key
5430 case ISN_STRINGMEMBER:
5431 {
5432 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005433 dictitem_T *di;
5434
5435 tv = STACK_TV_BOT(-1);
5436 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
5437 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005438 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005439 emsg(_(e_dictionary_required));
Bram Moolenaard032f342020-07-18 18:13:02 +02005440 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005441 }
5442 dict = tv->vval.v_dict;
5443
5444 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
5445 == NULL)
5446 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005447 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00005448 semsg(_(e_key_not_present_in_dictionary_str),
Bram Moolenaar381692b2022-02-02 20:01:27 +00005449 iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02005450 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005451 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005452 // Put the dict used on the dict stack, it might be used by
5453 // a dict function later.
5454 if (dict_stack_save(tv) == FAIL)
5455 goto on_fatal_error;
5456
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005457 copy_tv(&di->di_tv, tv);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005458 }
5459 break;
5460
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005461 case ISN_GET_OBJ_MEMBER:
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00005462 case ISN_GET_ITF_MEMBER:
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005463 {
5464 tv = STACK_TV_BOT(-1);
5465 if (tv->v_type != VAR_OBJECT)
5466 {
5467 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaard0200c82023-01-28 15:19:40 +00005468 object_required_error(tv);
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005469 goto on_error;
5470 }
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00005471
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005472 object_T *obj = tv->vval.v_object;
Bram Moolenaarc3f971f2023-03-02 17:38:33 +00005473 if (obj == NULL)
5474 {
5475 SOURCING_LNUM = iptr->isn_lnum;
5476 emsg(_(e_using_null_object));
5477 goto on_error;
5478 }
5479
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00005480 int idx;
5481 if (iptr->isn_type == ISN_GET_OBJ_MEMBER)
Ernie Rael18143d32023-09-04 22:30:41 +02005482 idx = iptr->isn_arg.classmember.cm_idx;
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00005483 else
5484 {
5485 idx = iptr->isn_arg.classmember.cm_idx;
5486 // convert the interface index to the object index
5487 idx = object_index_from_itf_index(
Ernie Rael18143d32023-09-04 22:30:41 +02005488 iptr->isn_arg.classmember.cm_class,
Yegappan Lakshmanan5a05d372023-09-29 19:43:11 +02005489 FALSE, idx, obj->obj_class);
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00005490 }
5491
Ernie Rael18143d32023-09-04 22:30:41 +02005492 // The members are located right after the object struct.
Yegappan Lakshmanan5a05d372023-09-29 19:43:11 +02005493 typval_T *mtv = ((typval_T *)(obj + 1)) + idx;
Bram Moolenaar590162c2022-12-24 21:24:06 +00005494 copy_tv(mtv, tv);
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005495
5496 // Unreference the object after getting the member, it may
5497 // be freed.
5498 object_unref(obj);
5499 }
5500 break;
5501
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005502 case ISN_STORE_THIS:
5503 {
5504 int idx = iptr->isn_arg.number;
5505 object_T *obj = STACK_TV_VAR(0)->vval.v_object;
5506 // the members are located right after the object struct
5507 typval_T *mtv = ((typval_T *)(obj + 1)) + idx;
5508 clear_tv(mtv);
5509 *mtv = *STACK_TV_BOT(-1);
5510 --ectx->ec_stack.ga_len;
5511 }
5512 break;
5513
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005514 case ISN_CLEARDICT:
5515 dict_stack_drop();
5516 break;
5517
5518 case ISN_USEDICT:
5519 {
5520 typval_T *dict_tv = dict_stack_get_tv();
5521
5522 // Turn "dict.Func" into a partial for "Func" bound to
5523 // "dict". Don't do this when "Func" is already a partial
5524 // that was bound explicitly (pt_auto is FALSE).
5525 tv = STACK_TV_BOT(-1);
5526 if (dict_tv != NULL
5527 && dict_tv->v_type == VAR_DICT
5528 && dict_tv->vval.v_dict != NULL
5529 && (tv->v_type == VAR_FUNC
5530 || (tv->v_type == VAR_PARTIAL
5531 && (tv->vval.v_partial->pt_auto
5532 || tv->vval.v_partial->pt_dict == NULL))))
5533 dict_tv->vval.v_dict =
5534 make_partial(dict_tv->vval.v_dict, tv);
5535 dict_stack_drop();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005536 }
5537 break;
5538
5539 case ISN_NEGATENR:
5540 tv = STACK_TV_BOT(-1);
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00005541 // CHECKTYPE should have checked the variable type
Bram Moolenaarc58164c2020-03-29 18:40:30 +02005542 if (tv->v_type == VAR_FLOAT)
5543 tv->vval.v_float = -tv->vval.v_float;
5544 else
Bram Moolenaarc58164c2020-03-29 18:40:30 +02005545 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005546 break;
5547
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005548 case ISN_CHECKTYPE:
5549 {
5550 checktype_T *ct = &iptr->isn_arg.type;
Bram Moolenaarb1040dc2022-05-18 11:00:48 +01005551 int r;
LemonBoyc5d27442023-08-19 13:02:35 +02005552 where_T where = WHERE_INIT;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005553
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01005554 tv = STACK_TV_BOT((int)ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02005555 SOURCING_LNUM = iptr->isn_lnum;
LemonBoyc5d27442023-08-19 13:02:35 +02005556 if (ct->ct_arg_idx > 0)
5557 {
5558 where.wt_index = ct->ct_arg_idx;
5559 where.wt_kind = ct->ct_is_var ? WT_VARIABLE : WT_ARGUMENT;
LemonBoyc5d27442023-08-19 13:02:35 +02005560 }
LemonBoyf244b2f2023-08-19 16:02:04 +02005561 where.wt_func_name = ectx->ec_where.wt_func_name;
LemonBoyc5d27442023-08-19 13:02:35 +02005562 r = check_typval_type(ct->ct_type, tv, where);
Bram Moolenaarb1040dc2022-05-18 11:00:48 +01005563 if (r == FAIL)
5564 goto on_error;
Bram Moolenaar5e654232020-09-16 15:22:00 +02005565
5566 // number 0 is FALSE, number 1 is TRUE
5567 if (tv->v_type == VAR_NUMBER
5568 && ct->ct_type->tt_type == VAR_BOOL
5569 && (tv->vval.v_number == 0
5570 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005571 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02005572 tv->v_type = VAR_BOOL;
5573 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02005574 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005575 }
5576 }
5577 break;
5578
Bram Moolenaar9af78762020-06-16 11:34:42 +02005579 case ISN_CHECKLEN:
5580 {
5581 int min_len = iptr->isn_arg.checklen.cl_min_len;
5582 list_T *list = NULL;
5583
5584 tv = STACK_TV_BOT(-1);
5585 if (tv->v_type == VAR_LIST)
5586 list = tv->vval.v_list;
5587 if (list == NULL || list->lv_len < min_len
5588 || (list->lv_len > min_len
5589 && !iptr->isn_arg.checklen.cl_more_OK))
5590 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005591 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005592 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02005593 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02005594 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02005595 }
5596 }
5597 break;
5598
Bram Moolenaaraa210a32021-01-02 15:41:03 +01005599 case ISN_SETTYPE:
Bram Moolenaar381692b2022-02-02 20:01:27 +00005600 set_tv_type(STACK_TV_BOT(-1), iptr->isn_arg.type.ct_type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01005601 break;
5602
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005603 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005604 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005605 {
5606 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005607 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005608
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005609 if (iptr->isn_type == ISN_2BOOL)
5610 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005611 tv = STACK_TV_BOT(iptr->isn_arg.tobool.offset);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005612 n = tv2bool(tv);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005613 if (iptr->isn_arg.tobool.invert)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005614 n = !n;
5615 }
5616 else
5617 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005618 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005619 SOURCING_LNUM = iptr->isn_lnum;
5620 n = tv_get_bool_chk(tv, &error);
5621 if (error)
5622 goto on_error;
5623 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005624 clear_tv(tv);
5625 tv->v_type = VAR_BOOL;
5626 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
5627 }
5628 break;
5629
5630 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02005631 case ISN_2STRING_ANY:
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01005632 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005633 if (do_2string(STACK_TV_BOT(iptr->isn_arg.tostring.offset),
5634 iptr->isn_type == ISN_2STRING_ANY,
5635 iptr->isn_arg.tostring.tolerant) == FAIL)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01005636 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005637 break;
5638
Bram Moolenaar08597872020-12-10 19:43:40 +01005639 case ISN_RANGE:
5640 {
5641 exarg_T ea;
5642 char *errormsg;
5643
Bram Moolenaarece0b872021-01-08 20:40:45 +01005644 ea.line2 = 0;
Bram Moolenaard1510ee2021-01-04 16:15:58 +01005645 ea.addr_count = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01005646 ea.addr_type = ADDR_LINES;
5647 ea.cmd = iptr->isn_arg.string;
Bram Moolenaarece0b872021-01-08 20:40:45 +01005648 ea.skip = FALSE;
Bram Moolenaar08597872020-12-10 19:43:40 +01005649 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaarece0b872021-01-08 20:40:45 +01005650 goto on_error;
Bram Moolenaara28639e2021-01-19 22:48:09 +01005651
Bram Moolenaar35578162021-08-02 19:10:38 +02005652 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005653 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005654 ++ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01005655 tv = STACK_TV_BOT(-1);
5656 tv->v_type = VAR_NUMBER;
5657 tv->v_lock = 0;
Bram Moolenaar06651632022-04-27 17:54:25 +01005658 tv->vval.v_number = ea.line2;
Bram Moolenaar08597872020-12-10 19:43:40 +01005659 }
5660 break;
5661
Bram Moolenaarc3516f72020-09-08 22:45:35 +02005662 case ISN_PUT:
5663 {
5664 int regname = iptr->isn_arg.put.put_regname;
5665 linenr_T lnum = iptr->isn_arg.put.put_lnum;
5666 char_u *expr = NULL;
5667 int dir = FORWARD;
5668
Bram Moolenaar08597872020-12-10 19:43:40 +01005669 if (lnum < -2)
5670 {
5671 // line number was put on the stack by ISN_RANGE
5672 tv = STACK_TV_BOT(-1);
5673 curwin->w_cursor.lnum = tv->vval.v_number;
5674 if (lnum == LNUM_VARIABLE_RANGE_ABOVE)
5675 dir = BACKWARD;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005676 --ectx->ec_stack.ga_len;
Bram Moolenaar08597872020-12-10 19:43:40 +01005677 }
5678 else if (lnum == -2)
Bram Moolenaarc3516f72020-09-08 22:45:35 +02005679 // :put! above cursor
5680 dir = BACKWARD;
5681 else if (lnum >= 0)
Bram Moolenaar4e713ba2022-02-07 15:31:37 +00005682 {
5683 curwin->w_cursor.lnum = lnum;
5684 if (lnum == 0)
5685 // check_cursor() below will move to line 1
5686 dir = BACKWARD;
5687 }
Bram Moolenaara28639e2021-01-19 22:48:09 +01005688
5689 if (regname == '=')
5690 {
5691 tv = STACK_TV_BOT(-1);
5692 if (tv->v_type == VAR_STRING)
5693 expr = tv->vval.v_string;
5694 else
5695 {
5696 expr = typval2string(tv, TRUE); // allocates value
5697 clear_tv(tv);
5698 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005699 --ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01005700 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02005701 check_cursor();
5702 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
5703 vim_free(expr);
5704 }
5705 break;
5706
Bram Moolenaar02194d22020-10-24 23:08:38 +02005707 case ISN_CMDMOD:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005708 ectx->ec_funclocal.floc_save_cmdmod = cmdmod;
5709 ectx->ec_funclocal.floc_restore_cmdmod = TRUE;
5710 ectx->ec_funclocal.floc_restore_cmdmod_stacklen =
5711 ectx->ec_stack.ga_len;
Bram Moolenaar02194d22020-10-24 23:08:38 +02005712 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
5713 apply_cmdmod(&cmdmod);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02005714 break;
5715
Bram Moolenaar02194d22020-10-24 23:08:38 +02005716 case ISN_CMDMOD_REV:
5717 // filter regprog is owned by the instruction, don't free it
5718 cmdmod.cmod_filter_regmatch.regprog = NULL;
5719 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005720 cmdmod = ectx->ec_funclocal.floc_save_cmdmod;
5721 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02005722 break;
5723
Bram Moolenaar792f7862020-11-23 08:31:18 +01005724 case ISN_UNPACK:
5725 {
5726 int count = iptr->isn_arg.unpack.unp_count;
5727 int semicolon = iptr->isn_arg.unpack.unp_semicolon;
5728 list_T *l;
5729 listitem_T *li;
5730 int i;
5731
5732 // Check there is a valid list to unpack.
5733 tv = STACK_TV_BOT(-1);
5734 if (tv->v_type != VAR_LIST)
5735 {
5736 SOURCING_LNUM = iptr->isn_lnum;
5737 emsg(_(e_for_argument_must_be_sequence_of_lists));
5738 goto on_error;
5739 }
5740 l = tv->vval.v_list;
5741 if (l == NULL
5742 || l->lv_len < (semicolon ? count - 1 : count))
5743 {
5744 SOURCING_LNUM = iptr->isn_lnum;
5745 emsg(_(e_list_value_does_not_have_enough_items));
5746 goto on_error;
5747 }
5748 else if (!semicolon && l->lv_len > count)
5749 {
5750 SOURCING_LNUM = iptr->isn_lnum;
5751 emsg(_(e_list_value_has_more_items_than_targets));
5752 goto on_error;
5753 }
5754
5755 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar35578162021-08-02 19:10:38 +02005756 if (GA_GROW_FAILS(&ectx->ec_stack, count - 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005757 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005758 ectx->ec_stack.ga_len += count - 1;
Bram Moolenaar792f7862020-11-23 08:31:18 +01005759
5760 // Variable after semicolon gets a list with the remaining
5761 // items.
5762 if (semicolon)
5763 {
5764 list_T *rem_list =
5765 list_alloc_with_items(l->lv_len - count + 1);
5766
5767 if (rem_list == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005768 goto theend;
Bram Moolenaar792f7862020-11-23 08:31:18 +01005769 tv = STACK_TV_BOT(-count);
5770 tv->vval.v_list = rem_list;
5771 ++rem_list->lv_refcount;
5772 tv->v_lock = 0;
5773 li = l->lv_first;
5774 for (i = 0; i < count - 1; ++i)
5775 li = li->li_next;
5776 for (i = 0; li != NULL; ++i)
5777 {
Bram Moolenaar61efa162022-03-18 13:10:48 +00005778 typval_T tvcopy;
5779
5780 copy_tv(&li->li_tv, &tvcopy);
5781 list_set_item(rem_list, i, &tvcopy);
Bram Moolenaar792f7862020-11-23 08:31:18 +01005782 li = li->li_next;
5783 }
5784 --count;
5785 }
5786
5787 // Produce the values in reverse order, first item last.
5788 li = l->lv_first;
5789 for (i = 0; i < count; ++i)
5790 {
5791 tv = STACK_TV_BOT(-i - 1);
5792 copy_tv(&li->li_tv, tv);
5793 li = li->li_next;
5794 }
5795
5796 list_unref(l);
5797 }
5798 break;
5799
Bram Moolenaarb2049902021-01-24 12:53:53 +01005800 case ISN_PROF_START:
5801 case ISN_PROF_END:
5802 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01005803#ifdef FEAT_PROFILE
Bram Moolenaarca16c602022-09-06 18:57:08 +01005804 funccall_T cookie;
5805 ufunc_T *cur_ufunc =
Bram Moolenaarb2049902021-01-24 12:53:53 +01005806 (((dfunc_T *)def_functions.ga_data)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02005807 + ectx->ec_dfunc_idx)->df_ufunc;
Bram Moolenaarb2049902021-01-24 12:53:53 +01005808
Bram Moolenaarca16c602022-09-06 18:57:08 +01005809 cookie.fc_func = cur_ufunc;
Bram Moolenaarb2049902021-01-24 12:53:53 +01005810 if (iptr->isn_type == ISN_PROF_START)
5811 {
5812 func_line_start(&cookie, iptr->isn_lnum);
5813 // if we get here the instruction is executed
5814 func_line_exec(&cookie);
5815 }
5816 else
5817 func_line_end(&cookie);
Bram Moolenaarf002a412021-01-24 13:34:18 +01005818#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01005819 }
5820 break;
5821
Bram Moolenaare99d4222021-06-13 14:01:26 +02005822 case ISN_DEBUG:
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02005823 handle_debug(iptr, ectx);
Bram Moolenaare99d4222021-06-13 14:01:26 +02005824 break;
5825
Bram Moolenaar389df252020-07-09 21:20:47 +02005826 case ISN_SHUFFLE:
5827 {
Bram Moolenaar792f7862020-11-23 08:31:18 +01005828 typval_T tmp_tv;
5829 int item = iptr->isn_arg.shuffle.shfl_item;
5830 int up = iptr->isn_arg.shuffle.shfl_up;
Bram Moolenaar389df252020-07-09 21:20:47 +02005831
5832 tmp_tv = *STACK_TV_BOT(-item);
5833 for ( ; up > 0 && item > 1; --up)
5834 {
5835 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
5836 --item;
5837 }
5838 *STACK_TV_BOT(-item) = tmp_tv;
5839 }
5840 break;
5841
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005842 case ISN_DROP:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005843 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005844 clear_tv(STACK_TV_BOT(0));
LemonBoyc5d27442023-08-19 13:02:35 +02005845 ectx->ec_where = (where_T)WHERE_INIT;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005846 break;
5847 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02005848 continue;
5849
Bram Moolenaard032f342020-07-18 18:13:02 +02005850func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02005851 // Restore previous function. If the frame pointer is where we started
5852 // then there is none and we are done.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005853 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02005854 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02005855
Bram Moolenaar4c137212021-04-19 16:48:48 +02005856 if (func_return(ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02005857 // only fails when out of memory
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005858 goto theend;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02005859 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02005860
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02005861on_error:
Bram Moolenaaraf0df472020-12-02 20:51:22 +01005862 // Jump here for an error that does not require aborting execution.
Bram Moolenaar56602ba2020-12-05 21:22:08 +01005863 // If "emsg_silent" is set then ignore the error, unless it was set
5864 // when calling the function.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005865 if (did_emsg_cumul + did_emsg == ectx->ec_did_emsg_before
Bram Moolenaar56602ba2020-12-05 21:22:08 +01005866 && emsg_silent && did_emsg_def == 0)
Bram Moolenaarf9041332021-01-21 19:41:16 +01005867 {
5868 // If a sequence of instructions causes an error while ":silent!"
5869 // was used, restore the stack length and jump ahead to restoring
5870 // the cmdmod.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005871 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaarf9041332021-01-21 19:41:16 +01005872 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005873 while (ectx->ec_stack.ga_len
5874 > ectx->ec_funclocal.floc_restore_cmdmod_stacklen)
Bram Moolenaarf9041332021-01-21 19:41:16 +01005875 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005876 --ectx->ec_stack.ga_len;
Bram Moolenaarf9041332021-01-21 19:41:16 +01005877 clear_tv(STACK_TV_BOT(0));
5878 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005879 while (ectx->ec_instr[ectx->ec_iidx].isn_type != ISN_CMDMOD_REV)
5880 ++ectx->ec_iidx;
Bram Moolenaarf9041332021-01-21 19:41:16 +01005881 }
Bram Moolenaarcd030c42020-10-30 21:49:40 +01005882 continue;
Bram Moolenaarf9041332021-01-21 19:41:16 +01005883 }
Bram Moolenaaraf0df472020-12-02 20:51:22 +01005884on_fatal_error:
5885 // Jump here for an error that messes up the stack.
Bram Moolenaar171fb922020-10-28 16:54:47 +01005886 // If we are not inside a try-catch started here, abort execution.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005887 if (trylevel <= ectx->ec_trylevel_at_start)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005888 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005889 }
5890
5891done:
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005892 ret = OK;
5893theend:
Bram Moolenaar58779852022-09-06 18:31:14 +01005894 may_invoke_defer_funcs(ectx);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01005895
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005896 dict_stack_clear(dict_stack_len_at_start);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005897 ectx->ec_trylevel_at_start = save_trylevel_at_start;
5898 return ret;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005899}
5900
5901/*
Bram Moolenaar806a2732022-09-04 15:40:36 +01005902 * Execute the instructions from a VAR_INSTR typval and put the result in
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005903 * "rettv".
5904 * Return OK or FAIL.
5905 */
5906 int
5907exe_typval_instr(typval_T *tv, typval_T *rettv)
5908{
5909 ectx_T *ectx = tv->vval.v_instr->instr_ectx;
5910 isn_T *save_instr = ectx->ec_instr;
5911 int save_iidx = ectx->ec_iidx;
5912 int res;
5913
LemonBoyf3b48952022-05-05 13:53:03 +01005914 // Initialize rettv so that it is safe for caller to invoke clear_tv(rettv)
5915 // even when the compilation fails.
5916 rettv->v_type = VAR_UNKNOWN;
5917
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005918 ectx->ec_instr = tv->vval.v_instr->instr_instr;
5919 res = exec_instructions(ectx);
5920 if (res == OK)
5921 {
5922 *rettv = *STACK_TV_BOT(-1);
5923 --ectx->ec_stack.ga_len;
5924 }
5925
5926 ectx->ec_instr = save_instr;
5927 ectx->ec_iidx = save_iidx;
5928
5929 return res;
5930}
5931
5932/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02005933 * Execute the instructions from an ISN_SUBSTITUTE command, which are in
5934 * "substitute_instr".
5935 */
5936 char_u *
5937exe_substitute_instr(void)
5938{
5939 ectx_T *ectx = substitute_instr->subs_ectx;
5940 isn_T *save_instr = ectx->ec_instr;
5941 int save_iidx = ectx->ec_iidx;
5942 char_u *res;
5943
5944 ectx->ec_instr = substitute_instr->subs_instr;
5945 if (exec_instructions(ectx) == OK)
Bram Moolenaar90193e62021-04-04 20:49:50 +02005946 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005947 typval_T *tv = STACK_TV_BOT(-1);
5948
Bram Moolenaar27523602021-06-05 21:36:19 +02005949 res = typval2string(tv, TRUE);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005950 --ectx->ec_stack.ga_len;
5951 clear_tv(tv);
Bram Moolenaar90193e62021-04-04 20:49:50 +02005952 }
5953 else
5954 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005955 substitute_instr->subs_status = FAIL;
5956 res = vim_strsave((char_u *)"");
Bram Moolenaar90193e62021-04-04 20:49:50 +02005957 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005958
Bram Moolenaar4c137212021-04-19 16:48:48 +02005959 ectx->ec_instr = save_instr;
5960 ectx->ec_iidx = save_iidx;
5961
5962 return res;
5963}
5964
5965/*
5966 * Call a "def" function from old Vim script.
5967 * Return OK or FAIL.
5968 */
5969 int
5970call_def_function(
5971 ufunc_T *ufunc,
5972 int argc_arg, // nr of arguments
5973 typval_T *argv, // arguments
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005974 int flags, // DEF_ flags
Bram Moolenaar4c137212021-04-19 16:48:48 +02005975 partial_T *partial, // optional partial for context
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005976 object_T *object, // object, e.g. for this.Func()
Bram Moolenaar58779852022-09-06 18:31:14 +01005977 funccall_T *funccal,
Bram Moolenaar4c137212021-04-19 16:48:48 +02005978 typval_T *rettv) // return value
5979{
5980 ectx_T ectx; // execution context
5981 int argc = argc_arg;
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005982 int partial_argc = partial == NULL
5983 || (flags & DEF_USE_PT_ARGV) == 0
5984 ? 0 : partial->pt_argc;
5985 int total_argc = argc + partial_argc;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005986 typval_T *tv;
5987 int idx;
5988 int ret = FAIL;
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005989 int defcount = ufunc->uf_args.ga_len - total_argc;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005990 sctx_T save_current_sctx = current_sctx;
5991 int did_emsg_before = did_emsg_cumul + did_emsg;
5992 int save_suppress_errthrow = suppress_errthrow;
5993 msglist_T **saved_msg_list = NULL;
5994 msglist_T *private_msg_list = NULL;
5995 int save_emsg_silent_def = emsg_silent_def;
5996 int save_did_emsg_def = did_emsg_def;
5997 int orig_funcdepth;
Bram Moolenaare99d4222021-06-13 14:01:26 +02005998 int orig_nesting_level = ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005999
6000// Get pointer to item in the stack.
6001#undef STACK_TV
6002#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
6003
6004// Get pointer to item at the bottom of the stack, -1 is the bottom.
6005#undef STACK_TV_BOT
6006#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
6007
6008// Get pointer to a local variable on the stack. Negative for arguments.
6009#undef STACK_TV_VAR
6010#define STACK_TV_VAR(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_frame_idx + STACK_FRAME_SIZE + idx)
6011
6012 if (ufunc->uf_def_status == UF_NOT_COMPILED
6013 || ufunc->uf_def_status == UF_COMPILE_ERROR
Bram Moolenaar139575d2022-03-15 19:29:30 +00006014 || (func_needs_compiling(ufunc, get_compile_type(ufunc))
6015 && compile_def_function(ufunc, FALSE,
6016 get_compile_type(ufunc), NULL) == FAIL))
Bram Moolenaar4c137212021-04-19 16:48:48 +02006017 {
6018 if (did_emsg_cumul + did_emsg == did_emsg_before)
6019 semsg(_(e_function_is_not_compiled_str),
6020 printable_func_name(ufunc));
6021 return FAIL;
6022 }
6023
6024 {
6025 // Check the function was really compiled.
6026 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6027 + ufunc->uf_dfunc_idx;
Bram Moolenaarcc341812022-09-19 15:54:34 +01006028 if (dfunc->df_ufunc == NULL)
6029 {
6030 semsg(_(e_function_was_deleted_str), printable_func_name(ufunc));
6031 return FAIL;
6032 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006033 if (INSTRUCTIONS(dfunc) == NULL)
6034 {
6035 iemsg("using call_def_function() on not compiled function");
6036 return FAIL;
6037 }
6038 }
6039
6040 // If depth of calling is getting too high, don't execute the function.
6041 orig_funcdepth = funcdepth_get();
6042 if (funcdepth_increment() == FAIL)
6043 return FAIL;
6044
6045 CLEAR_FIELD(ectx);
6046 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
6047 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
Bram Moolenaar35578162021-08-02 19:10:38 +02006048 if (GA_GROW_FAILS(&ectx.ec_stack, 20))
Bram Moolenaar4c137212021-04-19 16:48:48 +02006049 {
6050 funcdepth_decrement();
6051 return FAIL;
6052 }
6053 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
6054 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
6055 ectx.ec_did_emsg_before = did_emsg_before;
Bram Moolenaare99d4222021-06-13 14:01:26 +02006056 ++ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006057
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01006058 idx = total_argc - ufunc->uf_args.ga_len;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006059 if (idx > 0 && ufunc->uf_va_name == NULL)
6060 {
Matvey Tarasovd14bb1a2022-06-29 13:18:27 +01006061 semsg(NGETTEXT(e_one_argument_too_many, e_nr_arguments_too_many,
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01006062 idx), idx);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006063 goto failed_early;
6064 }
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01006065 idx = total_argc - ufunc->uf_args.ga_len + ufunc->uf_def_args.ga_len;
Bram Moolenaarc6d71532021-06-05 18:49:38 +02006066 if (idx < 0)
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02006067 {
Matvey Tarasovd14bb1a2022-06-29 13:18:27 +01006068 semsg(NGETTEXT(e_one_argument_too_few, e_nr_arguments_too_few,
Bram Moolenaar98aff652022-09-06 21:02:35 +01006069 -idx), -idx);
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02006070 goto failed_early;
6071 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006072
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01006073 // Put values from the partial and arguments on the stack, but no more than
6074 // what the function expects. A lambda can be called with more arguments
6075 // than it uses.
6076 for (idx = 0; idx < total_argc
Bram Moolenaar4c137212021-04-19 16:48:48 +02006077 && (ufunc->uf_va_name != NULL || idx < ufunc->uf_args.ga_len);
6078 ++idx)
6079 {
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01006080 int argv_idx = idx - partial_argc;
6081
6082 tv = idx < partial_argc ? partial->pt_argv + idx : argv + argv_idx;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006083 if (idx >= ufunc->uf_args.ga_len - ufunc->uf_def_args.ga_len
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01006084 && tv->v_type == VAR_SPECIAL
6085 && tv->vval.v_number == VVAL_NONE)
Bram Moolenaar4c137212021-04-19 16:48:48 +02006086 {
6087 // Use the default value.
6088 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
6089 }
6090 else
6091 {
Bram Moolenaar47bba532023-01-20 18:49:46 +00006092 int done = FALSE;
6093 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len)
6094 {
6095 type_T *expected = ufunc->uf_arg_types[idx];
6096 if (expected->tt_type == VAR_FLOAT && tv->v_type == VAR_NUMBER)
6097 {
6098 // When a float is expected and a number was given, convert
6099 // the value.
6100 STACK_TV_BOT(0)->v_type = VAR_FLOAT;
6101 STACK_TV_BOT(0)->v_lock = 0;
6102 STACK_TV_BOT(0)->vval.v_float = tv->vval.v_number;
6103 done = TRUE;
6104 }
6105 else if (check_typval_arg_type(expected, tv,
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01006106 NULL, argv_idx + 1) == FAIL)
Bram Moolenaar47bba532023-01-20 18:49:46 +00006107 goto failed_early;
6108 }
6109 if (!done)
6110 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006111 }
6112 ++ectx.ec_stack.ga_len;
6113 }
6114
6115 // Turn varargs into a list. Empty list if no args.
6116 if (ufunc->uf_va_name != NULL)
6117 {
6118 int vararg_count = argc - ufunc->uf_args.ga_len;
6119
6120 if (vararg_count < 0)
6121 vararg_count = 0;
6122 else
6123 argc -= vararg_count;
6124 if (exe_newlist(vararg_count, &ectx) == FAIL)
6125 goto failed_early;
6126
6127 // Check the type of the list items.
6128 tv = STACK_TV_BOT(-1);
6129 if (ufunc->uf_va_type != NULL
6130 && ufunc->uf_va_type != &t_list_any
6131 && ufunc->uf_va_type->tt_member != &t_any
6132 && tv->vval.v_list != NULL)
6133 {
6134 type_T *expected = ufunc->uf_va_type->tt_member;
6135 listitem_T *li = tv->vval.v_list->lv_first;
6136
6137 for (idx = 0; idx < vararg_count; ++idx)
6138 {
6139 if (check_typval_arg_type(expected, &li->li_tv,
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02006140 NULL, argc + idx + 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02006141 goto failed_early;
6142 li = li->li_next;
6143 }
6144 }
6145
6146 if (defcount > 0)
6147 // Move varargs list to below missing default arguments.
6148 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
6149 --ectx.ec_stack.ga_len;
6150 }
6151
6152 // Make space for omitted arguments, will store default value below.
6153 // Any varargs list goes after them.
6154 if (defcount > 0)
6155 for (idx = 0; idx < defcount; ++idx)
6156 {
6157 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
6158 ++ectx.ec_stack.ga_len;
6159 }
6160 if (ufunc->uf_va_name != NULL)
6161 ++ectx.ec_stack.ga_len;
6162
6163 // Frame pointer points to just after arguments.
6164 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
6165 ectx.ec_initial_frame_idx = ectx.ec_frame_idx;
6166
6167 {
6168 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6169 + ufunc->uf_dfunc_idx;
6170 ufunc_T *base_ufunc = dfunc->df_ufunc;
6171
6172 // "uf_partial" is on the ufunc that "df_ufunc" points to, as is done
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006173 // by copy_lambda_to_global_func().
Bram Moolenaar4c137212021-04-19 16:48:48 +02006174 if (partial != NULL || base_ufunc->uf_partial != NULL)
6175 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006176 ectx.ec_outer_ref = ALLOC_CLEAR_ONE(outer_ref_T);
6177 if (ectx.ec_outer_ref == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02006178 goto failed_early;
6179 if (partial != NULL)
6180 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00006181 outer_T *outer = get_pt_outer(partial);
6182
Bram Moolenaar6d313be2022-09-22 16:36:25 +01006183 if (outer->out_stack == NULL && outer->out_loop_size == 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02006184 {
Bram Moolenaar6d313be2022-09-22 16:36:25 +01006185 // no stack was set
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00006186 if (current_ectx != NULL)
6187 {
6188 if (current_ectx->ec_outer_ref != NULL
6189 && current_ectx->ec_outer_ref->or_outer != NULL)
6190 ectx.ec_outer_ref->or_outer =
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006191 current_ectx->ec_outer_ref->or_outer;
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00006192 }
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01006193 // else: should there be an error here?
Bram Moolenaar4c137212021-04-19 16:48:48 +02006194 }
6195 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006196 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00006197 ectx.ec_outer_ref->or_outer = outer;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006198 ++partial->pt_refcount;
6199 ectx.ec_outer_ref->or_partial = partial;
6200 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006201 }
6202 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006203 {
6204 ectx.ec_outer_ref->or_outer = &base_ufunc->uf_partial->pt_outer;
6205 ++base_ufunc->uf_partial->pt_refcount;
6206 ectx.ec_outer_ref->or_partial = base_ufunc->uf_partial;
6207 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006208 }
6209 }
6210
6211 // dummy frame entries
6212 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
6213 {
6214 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
6215 ++ectx.ec_stack.ga_len;
6216 }
6217
6218 {
6219 // Reserve space for local variables and any closure reference count.
6220 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6221 + ufunc->uf_dfunc_idx;
6222
Bram Moolenaar5cd64792021-12-25 18:23:24 +00006223 // Initialize variables to zero. That avoids having to generate
6224 // initializing instructions for "var nr: number", "var x: any", etc.
Bram Moolenaar4c137212021-04-19 16:48:48 +02006225 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaar5cd64792021-12-25 18:23:24 +00006226 {
6227 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
6228 STACK_TV_VAR(idx)->vval.v_number = 0;
6229 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006230 ectx.ec_stack.ga_len += dfunc->df_varcount;
Bram Moolenaarffdaca92022-12-09 21:41:48 +00006231
6232 if (object != NULL)
6233 {
6234 // the object is always the variable at index zero
6235 tv = STACK_TV_VAR(0);
6236 tv->v_type = VAR_OBJECT;
6237 tv->vval.v_object = object;
6238 }
6239
Bram Moolenaar4c137212021-04-19 16:48:48 +02006240 if (dfunc->df_has_closure)
6241 {
Bram Moolenaarcc341812022-09-19 15:54:34 +01006242 // Initialize the variable that counts how many closures were
6243 // created. This is used in handle_closure_in_use().
Bram Moolenaar4c137212021-04-19 16:48:48 +02006244 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
6245 STACK_TV_VAR(idx)->vval.v_number = 0;
6246 ++ectx.ec_stack.ga_len;
6247 }
6248
6249 ectx.ec_instr = INSTRUCTIONS(dfunc);
6250 }
6251
Bram Moolenaar58779852022-09-06 18:31:14 +01006252 // Store the execution context in funccal, used by invoke_all_defer().
6253 if (funccal != NULL)
6254 funccal->fc_ectx = &ectx;
6255
Bram Moolenaar4c137212021-04-19 16:48:48 +02006256 // Following errors are in the function, not the caller.
6257 // Commands behave like vim9script.
6258 estack_push_ufunc(ufunc, 1);
6259 current_sctx = ufunc->uf_script_ctx;
6260 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
6261
6262 // Use a specific location for storing error messages to be converted to an
6263 // exception.
6264 saved_msg_list = msg_list;
6265 msg_list = &private_msg_list;
6266
6267 // Do turn errors into exceptions.
6268 suppress_errthrow = FALSE;
6269
6270 // Do not delete the function while executing it.
6271 ++ufunc->uf_calls;
6272
6273 // When ":silent!" was used before calling then we still abort the
6274 // function. If ":silent!" is used in the function then we don't.
6275 emsg_silent_def = emsg_silent;
6276 did_emsg_def = 0;
6277
LemonBoyc5d27442023-08-19 13:02:35 +02006278 ectx.ec_where = (where_T)WHERE_INIT;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006279
Bram Moolenaar5800c792022-09-22 17:34:01 +01006280 /*
6281 * Execute the instructions until done.
6282 */
Bram Moolenaar4c137212021-04-19 16:48:48 +02006283 ret = exec_instructions(&ectx);
6284 if (ret == OK)
6285 {
6286 // function finished, get result from the stack.
6287 if (ufunc->uf_ret_type == &t_void)
6288 {
6289 rettv->v_type = VAR_VOID;
6290 }
6291 else
6292 {
6293 tv = STACK_TV_BOT(-1);
6294 *rettv = *tv;
6295 tv->v_type = VAR_UNKNOWN;
6296 }
6297 }
6298
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01006299 // When failed need to unwind the call stack.
Bram Moolenaar58779852022-09-06 18:31:14 +01006300 unwind_def_callstack(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02006301
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02006302 // Deal with any remaining closures, they may be in use somewhere.
6303 if (ectx.ec_funcrefs.ga_len > 0)
Bram Moolenaarf112f302020-12-20 17:47:52 +01006304 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02006305 handle_closure_in_use(&ectx, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00006306 ga_clear(&ectx.ec_funcrefs);
Bram Moolenaarf112f302020-12-20 17:47:52 +01006307 }
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02006308
Bram Moolenaaree8580e2020-08-28 17:19:07 +02006309 estack_pop();
6310 current_sctx = save_current_sctx;
6311
Bram Moolenaar2336c372021-12-06 15:06:54 +00006312 if (--ufunc->uf_calls <= 0 && ufunc->uf_refcount <= 0)
6313 // Function was unreferenced while being used, free it now.
6314 func_clear_free(ufunc, FALSE);
Bram Moolenaarc970e422021-03-17 15:03:04 +01006315
Bram Moolenaar352134b2020-10-17 22:04:08 +02006316 if (*msg_list != NULL && saved_msg_list != NULL)
6317 {
6318 msglist_T **plist = saved_msg_list;
6319
6320 // Append entries from the current msg_list (uncaught exceptions) to
6321 // the saved msg_list.
6322 while (*plist != NULL)
6323 plist = &(*plist)->next;
6324
6325 *plist = *msg_list;
6326 }
6327 msg_list = saved_msg_list;
6328
Bram Moolenaar4c137212021-04-19 16:48:48 +02006329 if (ectx.ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar02194d22020-10-24 23:08:38 +02006330 {
6331 cmdmod.cmod_filter_regmatch.regprog = NULL;
6332 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006333 cmdmod = ectx.ec_funclocal.floc_save_cmdmod;
Bram Moolenaar02194d22020-10-24 23:08:38 +02006334 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +01006335 emsg_silent_def = save_emsg_silent_def;
6336 did_emsg_def += save_did_emsg_def;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02006337
Bram Moolenaaree8580e2020-08-28 17:19:07 +02006338failed_early:
Bram Moolenaar0d1f55c2022-04-05 17:30:29 +01006339 // Free all arguments and local variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006340 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
Bram Moolenaar1936c762022-09-28 15:19:10 +01006341 {
6342 tv = STACK_TV(idx);
6343 if (tv->v_type != VAR_NUMBER && tv->v_type != VAR_UNKNOWN)
6344 clear_tv(tv);
6345 }
Bram Moolenaare99d4222021-06-13 14:01:26 +02006346 ex_nesting_level = orig_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02006347
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006348 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01006349 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006350 if (ectx.ec_outer_ref != NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +01006351 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006352 if (ectx.ec_outer_ref->or_outer_allocated)
6353 vim_free(ectx.ec_outer_ref->or_outer);
6354 partial_unref(ectx.ec_outer_ref->or_partial);
6355 vim_free(ectx.ec_outer_ref);
Bram Moolenaar0186e582021-01-10 18:33:11 +01006356 }
6357
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02006358 // Not sure if this is necessary.
6359 suppress_errthrow = save_suppress_errthrow;
6360
Bram Moolenaarb5841b92021-07-15 18:09:53 +02006361 if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before
6362 && !need_rethrow)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006363 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02006364 printable_func_name(ufunc));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01006365 funcdepth_restore(orig_funcdepth);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006366 return ret;
6367}
6368
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006369/*
Bram Moolenaar58779852022-09-06 18:31:14 +01006370 * Called when a def function has finished (possibly failed).
6371 * Invoke all the function returns to clean up and invoke deferred functions,
6372 * except the toplevel one.
6373 */
6374 void
6375unwind_def_callstack(ectx_T *ectx)
6376{
6377 while (ectx->ec_frame_idx != ectx->ec_initial_frame_idx)
6378 func_return(ectx);
6379}
6380
6381/*
dundargocc57b5bc2022-11-02 13:30:51 +00006382 * Invoke any deferred functions for the top function in "ectx".
Bram Moolenaar58779852022-09-06 18:31:14 +01006383 */
6384 void
6385may_invoke_defer_funcs(ectx_T *ectx)
6386{
6387 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
6388
6389 if (dfunc->df_defer_var_idx > 0)
6390 invoke_defer_funcs(ectx);
6391}
6392
6393/*
Bram Moolenaarcc341812022-09-19 15:54:34 +01006394 * Return loopvarinfo in a printable form in allocated memory.
6395 */
6396 static char_u *
6397printable_loopvarinfo(loopvarinfo_T *lvi)
6398{
6399 garray_T ga;
6400 int depth;
6401
6402 ga_init2(&ga, 1, 100);
6403 for (depth = 0; depth < lvi->lvi_depth; ++depth)
6404 {
6405 if (ga_grow(&ga, 50) == FAIL)
6406 break;
6407 if (lvi->lvi_loop[depth].var_idx == 0)
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +01006408 STRCPY((char *)ga.ga_data + ga.ga_len, " -");
Bram Moolenaarcc341812022-09-19 15:54:34 +01006409 else
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +01006410 vim_snprintf((char *)ga.ga_data + ga.ga_len, 50, " $%d-$%d",
Bram Moolenaarcc341812022-09-19 15:54:34 +01006411 lvi->lvi_loop[depth].var_idx,
6412 lvi->lvi_loop[depth].var_idx
6413 + lvi->lvi_loop[depth].var_count - 1);
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +01006414 ga.ga_len = (int)STRLEN(ga.ga_data);
Bram Moolenaarcc341812022-09-19 15:54:34 +01006415 }
6416 return ga.ga_data;
6417}
6418
6419/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02006420 * List instructions "instr" up to "instr_count" or until ISN_FINISH.
6421 * "ufunc" has the source lines, NULL for the instructions of ISN_SUBSTITUTE.
6422 * "pfx" is prefixed to every line.
6423 */
6424 static void
6425list_instructions(char *pfx, isn_T *instr, int instr_count, ufunc_T *ufunc)
6426{
6427 int line_idx = 0;
6428 int prev_current = 0;
6429 int current;
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02006430 int def_arg_idx = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006431
6432 for (current = 0; current < instr_count; ++current)
6433 {
6434 isn_T *iptr = &instr[current];
6435 char *line;
6436
6437 if (ufunc != NULL)
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02006438 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02006439 while (line_idx < iptr->isn_lnum
6440 && line_idx < ufunc->uf_lines.ga_len)
6441 {
6442 if (current > prev_current)
6443 {
6444 msg_puts("\n\n");
6445 prev_current = current;
6446 }
6447 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
6448 if (line != NULL)
6449 msg(line);
6450 }
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02006451 if (iptr->isn_type == ISN_JUMP_IF_ARG_SET)
6452 {
6453 int first_def_arg = ufunc->uf_args.ga_len
6454 - ufunc->uf_def_args.ga_len;
6455
6456 if (def_arg_idx > 0)
6457 msg_puts("\n\n");
6458 msg_start();
6459 msg_puts(" ");
6460 msg_puts(((char **)(ufunc->uf_args.ga_data))[
6461 first_def_arg + def_arg_idx]);
6462 msg_puts(" = ");
6463 msg_puts(((char **)(ufunc->uf_def_args.ga_data))[def_arg_idx++]);
6464 msg_clr_eos();
6465 msg_end();
6466 }
6467 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006468
6469 switch (iptr->isn_type)
6470 {
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006471 case ISN_CONSTRUCT:
6472 smsg("%s%4d NEW %s size %d", pfx, current,
6473 iptr->isn_arg.construct.construct_class->class_name,
6474 (int)iptr->isn_arg.construct.construct_size);
6475 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006476 case ISN_EXEC:
6477 smsg("%s%4d EXEC %s", pfx, current, iptr->isn_arg.string);
6478 break;
Bram Moolenaar20677332021-06-06 17:02:53 +02006479 case ISN_EXEC_SPLIT:
6480 smsg("%s%4d EXEC_SPLIT %s", pfx, current, iptr->isn_arg.string);
6481 break;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00006482 case ISN_EXECRANGE:
6483 smsg("%s%4d EXECRANGE %s", pfx, current, iptr->isn_arg.string);
6484 break;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02006485 case ISN_LEGACY_EVAL:
6486 smsg("%s%4d EVAL legacy %s", pfx, current,
6487 iptr->isn_arg.string);
6488 break;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02006489 case ISN_REDIRSTART:
6490 smsg("%s%4d REDIR", pfx, current);
6491 break;
6492 case ISN_REDIREND:
6493 smsg("%s%4d REDIR END%s", pfx, current,
6494 iptr->isn_arg.number ? " append" : "");
6495 break;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006496 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02006497#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006498 smsg("%s%4d CEXPR pre %s", pfx, current,
6499 cexpr_get_auname(iptr->isn_arg.number));
Bram Moolenaarb7c97812021-05-05 22:51:39 +02006500#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006501 break;
6502 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02006503#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006504 {
6505 cexprref_T *cer = iptr->isn_arg.cexpr.cexpr_ref;
6506
6507 smsg("%s%4d CEXPR core %s%s \"%s\"", pfx, current,
6508 cexpr_get_auname(cer->cer_cmdidx),
6509 cer->cer_forceit ? "!" : "",
6510 cer->cer_cmdline);
6511 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02006512#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006513 break;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006514 case ISN_INSTR:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006515 smsg("%s%4d INSTR", pfx, current);
6516 list_instructions(" ", iptr->isn_arg.instr, INT_MAX, NULL);
6517 msg(" -------------");
6518 break;
6519 case ISN_SOURCE:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006520 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006521 scriptitem_T *si = SCRIPT_ITEM(iptr->isn_arg.number);
6522
6523 smsg("%s%4d SOURCE %s", pfx, current, si->sn_name);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006524 }
6525 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006526 case ISN_SUBSTITUTE:
6527 {
6528 subs_T *subs = &iptr->isn_arg.subs;
6529
6530 smsg("%s%4d SUBSTITUTE %s", pfx, current, subs->subs_cmd);
6531 list_instructions(" ", subs->subs_instr, INT_MAX, NULL);
6532 msg(" -------------");
6533 }
6534 break;
6535 case ISN_EXECCONCAT:
6536 smsg("%s%4d EXECCONCAT %lld", pfx, current,
6537 (varnumber_T)iptr->isn_arg.number);
6538 break;
6539 case ISN_ECHO:
6540 {
6541 echo_T *echo = &iptr->isn_arg.echo;
6542
6543 smsg("%s%4d %s %d", pfx, current,
6544 echo->echo_with_white ? "ECHO" : "ECHON",
6545 echo->echo_count);
6546 }
6547 break;
6548 case ISN_EXECUTE:
6549 smsg("%s%4d EXECUTE %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02006550 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006551 break;
6552 case ISN_ECHOMSG:
6553 smsg("%s%4d ECHOMSG %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02006554 (varnumber_T)(iptr->isn_arg.number));
6555 break;
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01006556 case ISN_ECHOWINDOW:
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01006557 if (iptr->isn_arg.echowin.ewin_time > 0)
6558 smsg("%s%4d ECHOWINDOW %d (%ld sec)", pfx, current,
6559 iptr->isn_arg.echowin.ewin_count,
6560 iptr->isn_arg.echowin.ewin_time);
6561 else
6562 smsg("%s%4d ECHOWINDOW %d", pfx, current,
6563 iptr->isn_arg.echowin.ewin_count);
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01006564 break;
Bram Moolenaar7de62622021-08-07 15:05:47 +02006565 case ISN_ECHOCONSOLE:
6566 smsg("%s%4d ECHOCONSOLE %lld", pfx, current,
6567 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006568 break;
6569 case ISN_ECHOERR:
6570 smsg("%s%4d ECHOERR %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02006571 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006572 break;
6573 case ISN_LOAD:
6574 {
6575 if (iptr->isn_arg.number < 0)
6576 smsg("%s%4d LOAD arg[%lld]", pfx, current,
6577 (varnumber_T)(iptr->isn_arg.number
6578 + STACK_FRAME_SIZE));
6579 else
6580 smsg("%s%4d LOAD $%lld", pfx, current,
6581 (varnumber_T)(iptr->isn_arg.number));
6582 }
6583 break;
6584 case ISN_LOADOUTER:
6585 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006586 isn_outer_T *outer = &iptr->isn_arg.outer;
6587
6588 if (outer->outer_idx < 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02006589 smsg("%s%4d LOADOUTER level %d arg[%d]", pfx, current,
Bram Moolenaarcc341812022-09-19 15:54:34 +01006590 outer->outer_depth,
6591 outer->outer_idx + STACK_FRAME_SIZE);
6592 else if (outer->outer_depth < 0)
6593 smsg("%s%4d LOADOUTER $%d in loop level %d",
6594 pfx, current,
6595 outer->outer_idx,
6596 -outer->outer_depth);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006597 else
6598 smsg("%s%4d LOADOUTER level %d $%d", pfx, current,
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006599 outer->outer_depth,
6600 outer->outer_idx);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006601 }
6602 break;
6603 case ISN_LOADV:
6604 smsg("%s%4d LOADV v:%s", pfx, current,
6605 get_vim_var_name(iptr->isn_arg.number));
6606 break;
6607 case ISN_LOADSCRIPT:
6608 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006609 scriptref_T *sref = iptr->isn_arg.script.scriptref;
6610 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
6611 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006612
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006613 sv = get_script_svar(sref, -1);
6614 if (sv == NULL)
6615 smsg("%s%4d LOADSCRIPT [deleted] from %s",
6616 pfx, current, si->sn_name);
6617 else
6618 smsg("%s%4d LOADSCRIPT %s-%d from %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02006619 sv->sv_name,
6620 sref->sref_idx,
6621 si->sn_name);
6622 }
6623 break;
6624 case ISN_LOADS:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006625 case ISN_LOADEXPORT:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006626 {
6627 scriptitem_T *si = SCRIPT_ITEM(
6628 iptr->isn_arg.loadstore.ls_sid);
6629
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006630 smsg("%s%4d %s s:%s from %s", pfx, current,
6631 iptr->isn_type == ISN_LOADS ? "LOADS"
6632 : "LOADEXPORT",
6633 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006634 }
6635 break;
6636 case ISN_LOADAUTO:
6637 smsg("%s%4d LOADAUTO %s", pfx, current, iptr->isn_arg.string);
6638 break;
6639 case ISN_LOADG:
6640 smsg("%s%4d LOADG g:%s", pfx, current, iptr->isn_arg.string);
6641 break;
6642 case ISN_LOADB:
6643 smsg("%s%4d LOADB b:%s", pfx, current, iptr->isn_arg.string);
6644 break;
6645 case ISN_LOADW:
6646 smsg("%s%4d LOADW w:%s", pfx, current, iptr->isn_arg.string);
6647 break;
6648 case ISN_LOADT:
6649 smsg("%s%4d LOADT t:%s", pfx, current, iptr->isn_arg.string);
6650 break;
6651 case ISN_LOADGDICT:
6652 smsg("%s%4d LOAD g:", pfx, current);
6653 break;
6654 case ISN_LOADBDICT:
6655 smsg("%s%4d LOAD b:", pfx, current);
6656 break;
6657 case ISN_LOADWDICT:
6658 smsg("%s%4d LOAD w:", pfx, current);
6659 break;
6660 case ISN_LOADTDICT:
6661 smsg("%s%4d LOAD t:", pfx, current);
6662 break;
6663 case ISN_LOADOPT:
6664 smsg("%s%4d LOADOPT %s", pfx, current, iptr->isn_arg.string);
6665 break;
6666 case ISN_LOADENV:
6667 smsg("%s%4d LOADENV %s", pfx, current, iptr->isn_arg.string);
6668 break;
6669 case ISN_LOADREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006670 smsg("%s%4d LOADREG @%c", pfx, current,
6671 (int)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006672 break;
6673
6674 case ISN_STORE:
6675 if (iptr->isn_arg.number < 0)
6676 smsg("%s%4d STORE arg[%lld]", pfx, current,
6677 iptr->isn_arg.number + STACK_FRAME_SIZE);
6678 else
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006679 smsg("%s%4d STORE $%lld", pfx, current,
6680 iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006681 break;
6682 case ISN_STOREOUTER:
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006683 {
6684 isn_outer_T *outer = &iptr->isn_arg.outer;
6685
6686 if (outer->outer_depth == OUTER_LOOP_DEPTH)
6687 smsg("%s%4d STOREOUTER level 1 $%d in loop",
6688 pfx, current, outer->outer_idx);
6689 else
6690 smsg("%s%4d STOREOUTER level %d $%d", pfx, current,
6691 outer->outer_depth, outer->outer_idx);
6692 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006693 break;
6694 case ISN_STOREV:
6695 smsg("%s%4d STOREV v:%s", pfx, current,
6696 get_vim_var_name(iptr->isn_arg.number));
6697 break;
6698 case ISN_STOREAUTO:
6699 smsg("%s%4d STOREAUTO %s", pfx, current, iptr->isn_arg.string);
6700 break;
6701 case ISN_STOREG:
6702 smsg("%s%4d STOREG %s", pfx, current, iptr->isn_arg.string);
6703 break;
6704 case ISN_STOREB:
6705 smsg("%s%4d STOREB %s", pfx, current, iptr->isn_arg.string);
6706 break;
6707 case ISN_STOREW:
6708 smsg("%s%4d STOREW %s", pfx, current, iptr->isn_arg.string);
6709 break;
6710 case ISN_STORET:
6711 smsg("%s%4d STORET %s", pfx, current, iptr->isn_arg.string);
6712 break;
6713 case ISN_STORES:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006714 case ISN_STOREEXPORT:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006715 {
6716 scriptitem_T *si = SCRIPT_ITEM(
6717 iptr->isn_arg.loadstore.ls_sid);
6718
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006719 smsg("%s%4d %s %s in %s", pfx, current,
6720 iptr->isn_type == ISN_STORES
6721 ? "STORES" : "STOREEXPORT",
Bram Moolenaar4c137212021-04-19 16:48:48 +02006722 iptr->isn_arg.loadstore.ls_name, si->sn_name);
6723 }
6724 break;
6725 case ISN_STORESCRIPT:
6726 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006727 scriptref_T *sref = iptr->isn_arg.script.scriptref;
6728 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
6729 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006730
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006731 sv = get_script_svar(sref, -1);
6732 if (sv == NULL)
6733 smsg("%s%4d STORESCRIPT [deleted] in %s",
6734 pfx, current, si->sn_name);
6735 else
6736 smsg("%s%4d STORESCRIPT %s-%d in %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02006737 sv->sv_name,
6738 sref->sref_idx,
6739 si->sn_name);
6740 }
6741 break;
6742 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00006743 case ISN_STOREFUNCOPT:
6744 smsg("%s%4d %s &%s", pfx, current,
6745 iptr->isn_type == ISN_STOREOPT ? "STOREOPT" : "STOREFUNCOPT",
Bram Moolenaar4c137212021-04-19 16:48:48 +02006746 iptr->isn_arg.storeopt.so_name);
6747 break;
6748 case ISN_STOREENV:
6749 smsg("%s%4d STOREENV $%s", pfx, current, iptr->isn_arg.string);
6750 break;
6751 case ISN_STOREREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006752 smsg("%s%4d STOREREG @%c", pfx, current,
6753 (int)iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006754 break;
6755 case ISN_STORENR:
6756 smsg("%s%4d STORE %lld in $%d", pfx, current,
6757 iptr->isn_arg.storenr.stnr_val,
6758 iptr->isn_arg.storenr.stnr_idx);
6759 break;
6760
6761 case ISN_STOREINDEX:
6762 smsg("%s%4d STOREINDEX %s", pfx, current,
Bram Moolenaarf7d1c6e2023-01-16 20:47:57 +00006763 vartype_name(iptr->isn_arg.storeindex.si_vartype));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006764 break;
6765
6766 case ISN_STORERANGE:
6767 smsg("%s%4d STORERANGE", pfx, current);
6768 break;
6769
Bram Moolenaard505d172022-12-18 21:42:55 +00006770 case ISN_LOAD_CLASSMEMBER:
6771 case ISN_STORE_CLASSMEMBER:
6772 {
6773 class_T *cl = iptr->isn_arg.classmember.cm_class;
6774 int idx = iptr->isn_arg.classmember.cm_idx;
6775 ocmember_T *ocm = &cl->class_class_members[idx];
6776 smsg("%s%4d %s CLASSMEMBER %s.%s", pfx, current,
6777 iptr->isn_type == ISN_LOAD_CLASSMEMBER
6778 ? "LOAD" : "STORE",
6779 cl->class_name, ocm->ocm_name);
6780 }
6781 break;
6782
Bram Moolenaar4c137212021-04-19 16:48:48 +02006783 // constants
6784 case ISN_PUSHNR:
6785 smsg("%s%4d PUSHNR %lld", pfx, current,
6786 (varnumber_T)(iptr->isn_arg.number));
6787 break;
6788 case ISN_PUSHBOOL:
6789 case ISN_PUSHSPEC:
6790 smsg("%s%4d PUSH %s", pfx, current,
6791 get_var_special_name(iptr->isn_arg.number));
6792 break;
6793 case ISN_PUSHF:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006794 smsg("%s%4d PUSHF %g", pfx, current, iptr->isn_arg.fnumber);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006795 break;
6796 case ISN_PUSHS:
6797 smsg("%s%4d PUSHS \"%s\"", pfx, current, iptr->isn_arg.string);
6798 break;
6799 case ISN_PUSHBLOB:
6800 {
6801 char_u *r;
6802 char_u numbuf[NUMBUFLEN];
6803 char_u *tofree;
6804
6805 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
6806 smsg("%s%4d PUSHBLOB %s", pfx, current, r);
6807 vim_free(tofree);
6808 }
6809 break;
6810 case ISN_PUSHFUNC:
6811 {
6812 char *name = (char *)iptr->isn_arg.string;
6813
6814 smsg("%s%4d PUSHFUNC \"%s\"", pfx, current,
6815 name == NULL ? "[none]" : name);
6816 }
6817 break;
6818 case ISN_PUSHCHANNEL:
6819#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar397a87a2022-03-20 21:14:15 +00006820 smsg("%s%4d PUSHCHANNEL 0", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006821#endif
6822 break;
6823 case ISN_PUSHJOB:
6824#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar397a87a2022-03-20 21:14:15 +00006825 smsg("%s%4d PUSHJOB \"no process\"", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006826#endif
6827 break;
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00006828 case ISN_PUSHOBJ:
6829 smsg("%s%4d PUSHOBJ null", pfx, current);
6830 break;
6831 case ISN_PUSHCLASS:
6832 smsg("%s%4d PUSHCLASS %s", pfx, current,
Bram Moolenaar30a84472023-02-27 08:07:14 +00006833 iptr->isn_arg.classarg == NULL ? "null"
6834 : (char *)iptr->isn_arg.classarg->class_name);
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00006835 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006836 case ISN_PUSHEXC:
6837 smsg("%s%4d PUSH v:exception", pfx, current);
6838 break;
Bram Moolenaar06b77222022-01-25 15:51:56 +00006839 case ISN_AUTOLOAD:
6840 smsg("%s%4d AUTOLOAD %s", pfx, current, iptr->isn_arg.string);
6841 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006842 case ISN_UNLET:
6843 smsg("%s%4d UNLET%s %s", pfx, current,
6844 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
6845 iptr->isn_arg.unlet.ul_name);
6846 break;
6847 case ISN_UNLETENV:
6848 smsg("%s%4d UNLETENV%s $%s", pfx, current,
6849 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
6850 iptr->isn_arg.unlet.ul_name);
6851 break;
6852 case ISN_UNLETINDEX:
6853 smsg("%s%4d UNLETINDEX", pfx, current);
6854 break;
6855 case ISN_UNLETRANGE:
6856 smsg("%s%4d UNLETRANGE", pfx, current);
6857 break;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02006858 case ISN_LOCKUNLOCK:
6859 smsg("%s%4d LOCKUNLOCK %s", pfx, current, iptr->isn_arg.string);
6860 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006861 case ISN_LOCKCONST:
6862 smsg("%s%4d LOCKCONST", pfx, current);
6863 break;
6864 case ISN_NEWLIST:
6865 smsg("%s%4d NEWLIST size %lld", pfx, current,
Bram Moolenaar1936c762022-09-28 15:19:10 +01006866 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006867 break;
6868 case ISN_NEWDICT:
6869 smsg("%s%4d NEWDICT size %lld", pfx, current,
Bram Moolenaar1936c762022-09-28 15:19:10 +01006870 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006871 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00006872 case ISN_NEWPARTIAL:
6873 smsg("%s%4d NEWPARTIAL", pfx, current);
6874 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006875
6876 // function call
6877 case ISN_BCALL:
6878 {
6879 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
6880
6881 smsg("%s%4d BCALL %s(argc %d)", pfx, current,
6882 internal_func_name(cbfunc->cbf_idx),
6883 cbfunc->cbf_argcount);
6884 }
6885 break;
6886 case ISN_DCALL:
6887 {
6888 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
6889 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
6890 + cdfunc->cdf_idx;
6891
6892 smsg("%s%4d DCALL %s(argc %d)", pfx, current,
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006893 printable_func_name(df->df_ufunc),
6894 cdfunc->cdf_argcount);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006895 }
6896 break;
Bram Moolenaard0200c82023-01-28 15:19:40 +00006897 case ISN_METHODCALL:
6898 {
6899 cmfunc_T *mfunc = iptr->isn_arg.mfunc;
6900
6901 smsg("%s%4d METHODCALL %s.%s(argc %d)", pfx, current,
6902 mfunc->cmf_itf->class_name,
6903 mfunc->cmf_itf->class_obj_methods[
6904 mfunc->cmf_idx]->uf_name,
6905 mfunc->cmf_argcount);
6906 }
6907 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006908 case ISN_UCALL:
6909 {
6910 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
6911
6912 smsg("%s%4d UCALL %s(argc %d)", pfx, current,
6913 cufunc->cuf_name, cufunc->cuf_argcount);
6914 }
6915 break;
6916 case ISN_PCALL:
6917 {
6918 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
6919
6920 smsg("%s%4d PCALL%s (argc %d)", pfx, current,
6921 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
6922 }
6923 break;
6924 case ISN_PCALL_END:
6925 smsg("%s%4d PCALL end", pfx, current);
6926 break;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006927 case ISN_DEFER:
Yegappan Lakshmananf3eac692023-10-17 11:00:45 +02006928 smsg("%s%4d DEFER %d args", pfx, current,
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006929 (int)iptr->isn_arg.defer.defer_argcount);
6930 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006931 case ISN_RETURN:
6932 smsg("%s%4d RETURN", pfx, current);
6933 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02006934 case ISN_RETURN_VOID:
6935 smsg("%s%4d RETURN void", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006936 break;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006937 case ISN_RETURN_OBJECT:
6938 smsg("%s%4d RETURN object", pfx, current);
6939 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006940 case ISN_FUNCREF:
6941 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006942 funcref_T *funcref = &iptr->isn_arg.funcref;
6943 funcref_extra_T *extra = funcref->fr_extra;
6944 char_u *name;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006945
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006946 if (extra == NULL || extra->fre_func_name == NULL)
Bram Moolenaar38453522021-11-28 22:00:12 +00006947 {
6948 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
6949 + funcref->fr_dfunc_idx;
6950 name = df->df_ufunc->uf_name;
6951 }
6952 else
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006953 name = extra->fre_func_name;
Bram Moolenaar313e4722023-02-08 20:55:27 +00006954 if (extra != NULL && extra->fre_class != NULL)
6955 {
6956 smsg("%s%4d FUNCREF %s.%s", pfx, current,
6957 extra->fre_class->class_name, name);
6958 }
6959 else if (extra == NULL
6960 || extra->fre_loopvar_info.lvi_depth == 0)
6961 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006962 smsg("%s%4d FUNCREF %s", pfx, current, name);
Bram Moolenaar313e4722023-02-08 20:55:27 +00006963 }
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006964 else
Bram Moolenaarcc341812022-09-19 15:54:34 +01006965 {
6966 char_u *info = printable_loopvarinfo(
6967 &extra->fre_loopvar_info);
6968
6969 smsg("%s%4d FUNCREF %s vars %s", pfx, current,
6970 name, info);
6971 vim_free(info);
6972 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006973 }
6974 break;
6975
6976 case ISN_NEWFUNC:
6977 {
Bram Moolenaarcc341812022-09-19 15:54:34 +01006978 newfuncarg_T *arg = iptr->isn_arg.newfunc.nf_arg;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006979
Bram Moolenaarcc341812022-09-19 15:54:34 +01006980 if (arg->nfa_loopvar_info.lvi_depth == 0)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006981 smsg("%s%4d NEWFUNC %s %s", pfx, current,
6982 arg->nfa_lambda, arg->nfa_global);
6983 else
Bram Moolenaarcc341812022-09-19 15:54:34 +01006984 {
6985 char_u *info = printable_loopvarinfo(
6986 &arg->nfa_loopvar_info);
6987
6988 smsg("%s%4d NEWFUNC %s %s vars %s", pfx, current,
6989 arg->nfa_lambda, arg->nfa_global, info);
6990 vim_free(info);
6991 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006992 }
6993 break;
6994
6995 case ISN_DEF:
6996 {
6997 char_u *name = iptr->isn_arg.string;
6998
6999 smsg("%s%4d DEF %s", pfx, current,
7000 name == NULL ? (char_u *)"" : name);
7001 }
7002 break;
7003
7004 case ISN_JUMP:
7005 {
7006 char *when = "?";
7007
7008 switch (iptr->isn_arg.jump.jump_when)
7009 {
7010 case JUMP_ALWAYS:
7011 when = "JUMP";
7012 break;
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02007013 case JUMP_NEVER:
7014 iemsg("JUMP_NEVER should not be used");
7015 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007016 case JUMP_AND_KEEP_IF_TRUE:
7017 when = "JUMP_AND_KEEP_IF_TRUE";
7018 break;
7019 case JUMP_IF_FALSE:
7020 when = "JUMP_IF_FALSE";
7021 break;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01007022 case JUMP_WHILE_FALSE:
7023 when = "JUMP_WHILE_FALSE"; // unused
7024 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007025 case JUMP_IF_COND_FALSE:
7026 when = "JUMP_IF_COND_FALSE";
7027 break;
7028 case JUMP_IF_COND_TRUE:
7029 when = "JUMP_IF_COND_TRUE";
7030 break;
7031 }
7032 smsg("%s%4d %s -> %d", pfx, current, when,
7033 iptr->isn_arg.jump.jump_where);
7034 }
7035 break;
7036
7037 case ISN_JUMP_IF_ARG_SET:
7038 smsg("%s%4d JUMP_IF_ARG_SET arg[%d] -> %d", pfx, current,
7039 iptr->isn_arg.jumparg.jump_arg_off + STACK_FRAME_SIZE,
7040 iptr->isn_arg.jump.jump_where);
7041 break;
7042
Bram Moolenaar65b0d162022-12-13 18:43:22 +00007043 case ISN_JUMP_IF_ARG_NOT_SET:
7044 smsg("%s%4d JUMP_IF_ARG_NOT_SET arg[%d] -> %d", pfx, current,
7045 iptr->isn_arg.jumparg.jump_arg_off + STACK_FRAME_SIZE,
7046 iptr->isn_arg.jump.jump_where);
7047 break;
7048
Bram Moolenaar4c137212021-04-19 16:48:48 +02007049 case ISN_FOR:
7050 {
7051 forloop_T *forloop = &iptr->isn_arg.forloop;
7052
7053 smsg("%s%4d FOR $%d -> %d", pfx, current,
Bram Moolenaarcc341812022-09-19 15:54:34 +01007054 forloop->for_loop_idx, forloop->for_end);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007055 }
7056 break;
7057
Bram Moolenaarb46c0832022-09-15 17:19:37 +01007058 case ISN_ENDLOOP:
7059 {
7060 endloop_T *endloop = &iptr->isn_arg.endloop;
7061
Bram Moolenaarcc341812022-09-19 15:54:34 +01007062 smsg("%s%4d ENDLOOP ref $%d save $%d-$%d depth %d",
7063 pfx, current,
Bram Moolenaarb46c0832022-09-15 17:19:37 +01007064 endloop->end_funcref_idx,
7065 endloop->end_var_idx,
Bram Moolenaarcc341812022-09-19 15:54:34 +01007066 endloop->end_var_idx + endloop->end_var_count - 1,
7067 endloop->end_depth);
Bram Moolenaarb46c0832022-09-15 17:19:37 +01007068 }
7069 break;
7070
7071 case ISN_WHILE:
7072 {
7073 whileloop_T *whileloop = &iptr->isn_arg.whileloop;
7074
7075 smsg("%s%4d WHILE $%d -> %d", pfx, current,
7076 whileloop->while_funcref_idx,
7077 whileloop->while_end);
7078 }
7079 break;
7080
Bram Moolenaar4c137212021-04-19 16:48:48 +02007081 case ISN_TRY:
7082 {
Bram Moolenaar0d807102021-12-21 09:42:09 +00007083 try_T *try = &iptr->isn_arg.tryref;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007084
7085 if (try->try_ref->try_finally == 0)
7086 smsg("%s%4d TRY catch -> %d, endtry -> %d",
7087 pfx, current,
7088 try->try_ref->try_catch,
7089 try->try_ref->try_endtry);
7090 else
7091 smsg("%s%4d TRY catch -> %d, finally -> %d, endtry -> %d",
7092 pfx, current,
7093 try->try_ref->try_catch,
7094 try->try_ref->try_finally,
7095 try->try_ref->try_endtry);
7096 }
7097 break;
7098 case ISN_CATCH:
Bram Moolenaar4c137212021-04-19 16:48:48 +02007099 smsg("%s%4d CATCH", pfx, current);
7100 break;
7101 case ISN_TRYCONT:
7102 {
7103 trycont_T *trycont = &iptr->isn_arg.trycont;
7104
7105 smsg("%s%4d TRY-CONTINUE %d level%s -> %d", pfx, current,
7106 trycont->tct_levels,
7107 trycont->tct_levels == 1 ? "" : "s",
7108 trycont->tct_where);
7109 }
7110 break;
7111 case ISN_FINALLY:
7112 smsg("%s%4d FINALLY", pfx, current);
7113 break;
7114 case ISN_ENDTRY:
7115 smsg("%s%4d ENDTRY", pfx, current);
7116 break;
7117 case ISN_THROW:
7118 smsg("%s%4d THROW", pfx, current);
7119 break;
7120
7121 // expression operations on number
7122 case ISN_OPNR:
7123 case ISN_OPFLOAT:
7124 case ISN_OPANY:
7125 {
7126 char *what;
7127 char *ins;
7128
7129 switch (iptr->isn_arg.op.op_type)
7130 {
7131 case EXPR_MULT: what = "*"; break;
7132 case EXPR_DIV: what = "/"; break;
7133 case EXPR_REM: what = "%"; break;
7134 case EXPR_SUB: what = "-"; break;
7135 case EXPR_ADD: what = "+"; break;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01007136 case EXPR_LSHIFT: what = "<<"; break;
7137 case EXPR_RSHIFT: what = ">>"; break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007138 default: what = "???"; break;
7139 }
7140 switch (iptr->isn_type)
7141 {
7142 case ISN_OPNR: ins = "OPNR"; break;
7143 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
7144 case ISN_OPANY: ins = "OPANY"; break;
7145 default: ins = "???"; break;
7146 }
7147 smsg("%s%4d %s %s", pfx, current, ins, what);
7148 }
7149 break;
7150
7151 case ISN_COMPAREBOOL:
7152 case ISN_COMPARESPECIAL:
Bram Moolenaar7a222242022-03-01 19:23:24 +00007153 case ISN_COMPARENULL:
Bram Moolenaar4c137212021-04-19 16:48:48 +02007154 case ISN_COMPARENR:
7155 case ISN_COMPAREFLOAT:
7156 case ISN_COMPARESTRING:
7157 case ISN_COMPAREBLOB:
7158 case ISN_COMPARELIST:
7159 case ISN_COMPAREDICT:
7160 case ISN_COMPAREFUNC:
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00007161 case ISN_COMPARECLASS:
7162 case ISN_COMPAREOBJECT:
Bram Moolenaar4c137212021-04-19 16:48:48 +02007163 case ISN_COMPAREANY:
7164 {
7165 char *p;
7166 char buf[10];
7167 char *type;
7168
7169 switch (iptr->isn_arg.op.op_type)
7170 {
7171 case EXPR_EQUAL: p = "=="; break;
7172 case EXPR_NEQUAL: p = "!="; break;
7173 case EXPR_GREATER: p = ">"; break;
7174 case EXPR_GEQUAL: p = ">="; break;
7175 case EXPR_SMALLER: p = "<"; break;
7176 case EXPR_SEQUAL: p = "<="; break;
7177 case EXPR_MATCH: p = "=~"; break;
7178 case EXPR_IS: p = "is"; break;
7179 case EXPR_ISNOT: p = "isnot"; break;
7180 case EXPR_NOMATCH: p = "!~"; break;
7181 default: p = "???"; break;
7182 }
7183 STRCPY(buf, p);
7184 if (iptr->isn_arg.op.op_ic == TRUE)
7185 strcat(buf, "?");
7186 switch(iptr->isn_type)
7187 {
7188 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
7189 case ISN_COMPARESPECIAL:
7190 type = "COMPARESPECIAL"; break;
Bram Moolenaar7a222242022-03-01 19:23:24 +00007191 case ISN_COMPARENULL: type = "COMPARENULL"; break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007192 case ISN_COMPARENR: type = "COMPARENR"; break;
7193 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
7194 case ISN_COMPARESTRING:
7195 type = "COMPARESTRING"; break;
7196 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
7197 case ISN_COMPARELIST: type = "COMPARELIST"; break;
7198 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
7199 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00007200 case ISN_COMPARECLASS: type = "COMPARECLASS"; break;
7201 case ISN_COMPAREOBJECT:
7202 type = "COMPAREOBJECT"; break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007203 case ISN_COMPAREANY: type = "COMPAREANY"; break;
7204 default: type = "???"; break;
7205 }
7206
7207 smsg("%s%4d %s %s", pfx, current, type, buf);
7208 }
7209 break;
7210
7211 case ISN_ADDLIST: smsg("%s%4d ADDLIST", pfx, current); break;
7212 case ISN_ADDBLOB: smsg("%s%4d ADDBLOB", pfx, current); break;
7213
7214 // expression operations
LemonBoy372bcce2022-04-25 12:43:20 +01007215 case ISN_CONCAT:
7216 smsg("%s%4d CONCAT size %lld", pfx, current,
7217 (varnumber_T)(iptr->isn_arg.number));
7218 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007219 case ISN_STRINDEX: smsg("%s%4d STRINDEX", pfx, current); break;
7220 case ISN_STRSLICE: smsg("%s%4d STRSLICE", pfx, current); break;
7221 case ISN_BLOBINDEX: smsg("%s%4d BLOBINDEX", pfx, current); break;
7222 case ISN_BLOBSLICE: smsg("%s%4d BLOBSLICE", pfx, current); break;
7223 case ISN_LISTAPPEND: smsg("%s%4d LISTAPPEND", pfx, current); break;
7224 case ISN_BLOBAPPEND: smsg("%s%4d BLOBAPPEND", pfx, current); break;
7225 case ISN_LISTINDEX: smsg("%s%4d LISTINDEX", pfx, current); break;
7226 case ISN_LISTSLICE: smsg("%s%4d LISTSLICE", pfx, current); break;
7227 case ISN_ANYINDEX: smsg("%s%4d ANYINDEX", pfx, current); break;
7228 case ISN_ANYSLICE: smsg("%s%4d ANYSLICE", pfx, current); break;
7229 case ISN_SLICE: smsg("%s%4d SLICE %lld",
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02007230 pfx, current, iptr->isn_arg.number); break;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02007231 case ISN_GETITEM: smsg("%s%4d ITEM %lld%s", pfx, current,
7232 iptr->isn_arg.getitem.gi_index,
7233 iptr->isn_arg.getitem.gi_with_op ?
7234 " with op" : ""); break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007235 case ISN_MEMBER: smsg("%s%4d MEMBER", pfx, current); break;
7236 case ISN_STRINGMEMBER: smsg("%s%4d MEMBER %s", pfx, current,
7237 iptr->isn_arg.string); break;
Yegappan Lakshmanan5a05d372023-09-29 19:43:11 +02007238 case ISN_GET_OBJ_MEMBER: smsg("%s%4d OBJ_MEMBER %d", pfx, current,
7239 (int)iptr->isn_arg.classmember.cm_idx);
Ernie Rael00df69e2023-09-05 07:38:09 +02007240 break;
Yegappan Lakshmanan5a05d372023-09-29 19:43:11 +02007241 case ISN_GET_ITF_MEMBER: smsg("%s%4d ITF_MEMBER %d on %s",
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00007242 pfx, current,
7243 (int)iptr->isn_arg.classmember.cm_idx,
Yegappan Lakshmanan5a05d372023-09-29 19:43:11 +02007244 iptr->isn_arg.classmember.cm_class->class_name);
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00007245 break;
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00007246 case ISN_STORE_THIS: smsg("%s%4d STORE_THIS %d", pfx, current,
Bram Moolenaarffdaca92022-12-09 21:41:48 +00007247 (int)iptr->isn_arg.number); break;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02007248 case ISN_CLEARDICT: smsg("%s%4d CLEARDICT", pfx, current); break;
7249 case ISN_USEDICT: smsg("%s%4d USEDICT", pfx, current); break;
7250
Bram Moolenaar4c137212021-04-19 16:48:48 +02007251 case ISN_NEGATENR: smsg("%s%4d NEGATENR", pfx, current); break;
7252
Bram Moolenaar4c137212021-04-19 16:48:48 +02007253 case ISN_CHECKTYPE:
7254 {
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01007255 checktype_T *ct = &iptr->isn_arg.type;
Bram Moolenaarc6951a72022-12-29 20:56:24 +00007256 char *tofree = NULL;
7257 char *typename;
7258
7259 if (ct->ct_type->tt_type == VAR_FLOAT
7260 && (ct->ct_type->tt_flags & TTFLAG_NUMBER_OK))
7261 typename = "float|number";
7262 else
7263 typename = type_name(ct->ct_type, &tofree);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007264
7265 if (ct->ct_arg_idx == 0)
7266 smsg("%s%4d CHECKTYPE %s stack[%d]", pfx, current,
Bram Moolenaarc6951a72022-12-29 20:56:24 +00007267 typename,
Bram Moolenaar4c137212021-04-19 16:48:48 +02007268 (int)ct->ct_off);
7269 else
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01007270 smsg("%s%4d CHECKTYPE %s stack[%d] %s %d",
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02007271 pfx, current,
Bram Moolenaarc6951a72022-12-29 20:56:24 +00007272 typename,
Bram Moolenaar4c137212021-04-19 16:48:48 +02007273 (int)ct->ct_off,
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01007274 ct->ct_is_var ? "var": "arg",
Bram Moolenaar4c137212021-04-19 16:48:48 +02007275 (int)ct->ct_arg_idx);
7276 vim_free(tofree);
7277 break;
7278 }
7279 case ISN_CHECKLEN: smsg("%s%4d CHECKLEN %s%d", pfx, current,
7280 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
7281 iptr->isn_arg.checklen.cl_min_len);
7282 break;
7283 case ISN_SETTYPE:
7284 {
7285 char *tofree;
7286
7287 smsg("%s%4d SETTYPE %s", pfx, current,
7288 type_name(iptr->isn_arg.type.ct_type, &tofree));
7289 vim_free(tofree);
7290 break;
7291 }
7292 case ISN_COND2BOOL: smsg("%s%4d COND2BOOL", pfx, current); break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007293 case ISN_2BOOL: if (iptr->isn_arg.tobool.invert)
7294 smsg("%s%4d INVERT %d (!val)", pfx, current,
7295 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007296 else
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007297 smsg("%s%4d 2BOOL %d (!!val)", pfx, current,
7298 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007299 break;
7300 case ISN_2STRING: smsg("%s%4d 2STRING stack[%lld]", pfx, current,
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007301 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02007302 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007303 case ISN_2STRING_ANY: smsg("%s%4d 2STRING_ANY stack[%lld]",
7304 pfx, current,
7305 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02007306 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007307 case ISN_RANGE: smsg("%s%4d RANGE %s", pfx, current,
7308 iptr->isn_arg.string);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007309 break;
7310 case ISN_PUT:
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01007311 if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE)
Bram Moolenaar4c137212021-04-19 16:48:48 +02007312 smsg("%s%4d PUT %c above range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007313 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007314 else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE)
7315 smsg("%s%4d PUT %c range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007316 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007317 else
7318 smsg("%s%4d PUT %c %ld", pfx, current,
7319 iptr->isn_arg.put.put_regname,
7320 (long)iptr->isn_arg.put.put_lnum);
7321 break;
7322
Bram Moolenaar4c137212021-04-19 16:48:48 +02007323 case ISN_CMDMOD:
7324 {
7325 char_u *buf;
7326 size_t len = produce_cmdmods(
7327 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
7328
7329 buf = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02007330 if (likely(buf != NULL))
Bram Moolenaar4c137212021-04-19 16:48:48 +02007331 {
7332 (void)produce_cmdmods(
7333 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
7334 smsg("%s%4d CMDMOD %s", pfx, current, buf);
7335 vim_free(buf);
7336 }
7337 break;
7338 }
7339 case ISN_CMDMOD_REV: smsg("%s%4d CMDMOD_REV", pfx, current); break;
7340
7341 case ISN_PROF_START:
Bram Moolenaare99d4222021-06-13 14:01:26 +02007342 smsg("%s%4d PROFILE START line %d", pfx, current,
7343 iptr->isn_lnum);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007344 break;
7345
7346 case ISN_PROF_END:
7347 smsg("%s%4d PROFILE END", pfx, current);
7348 break;
7349
Bram Moolenaare99d4222021-06-13 14:01:26 +02007350 case ISN_DEBUG:
Bram Moolenaar8cec9272021-06-23 20:20:53 +02007351 smsg("%s%4d DEBUG line %d-%d varcount %lld", pfx, current,
7352 iptr->isn_arg.debug.dbg_break_lnum + 1,
7353 iptr->isn_lnum,
7354 iptr->isn_arg.debug.dbg_var_names_len);
Bram Moolenaare99d4222021-06-13 14:01:26 +02007355 break;
7356
Bram Moolenaar4c137212021-04-19 16:48:48 +02007357 case ISN_UNPACK: smsg("%s%4d UNPACK %d%s", pfx, current,
7358 iptr->isn_arg.unpack.unp_count,
7359 iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : "");
7360 break;
7361 case ISN_SHUFFLE: smsg("%s%4d SHUFFLE %d up %d", pfx, current,
7362 iptr->isn_arg.shuffle.shfl_item,
7363 iptr->isn_arg.shuffle.shfl_up);
7364 break;
7365 case ISN_DROP: smsg("%s%4d DROP", pfx, current); break;
7366
7367 case ISN_FINISH: // End of list of instructions for ISN_SUBSTITUTE.
7368 return;
7369 }
7370
7371 out_flush(); // output one line at a time
7372 ui_breakcheck();
7373 if (got_int)
7374 break;
7375 }
7376}
7377
7378/*
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02007379 * Handle command line completion for the :disassemble command.
7380 */
7381 void
7382set_context_in_disassemble_cmd(expand_T *xp, char_u *arg)
7383{
7384 char_u *p;
7385
7386 // Default: expand user functions, "debug" and "profile"
7387 xp->xp_context = EXPAND_DISASSEMBLE;
7388 xp->xp_pattern = arg;
7389
7390 // first argument already typed: only user function names
7391 if (*arg != NUL && *(p = skiptowhite(arg)) != NUL)
7392 {
7393 xp->xp_context = EXPAND_USER_FUNC;
7394 xp->xp_pattern = skipwhite(p);
7395 }
7396}
7397
7398/*
7399 * Function given to ExpandGeneric() to obtain the list of :disassemble
7400 * arguments.
7401 */
7402 char_u *
7403get_disassemble_argument(expand_T *xp, int idx)
7404{
7405 if (idx == 0)
7406 return (char_u *)"debug";
7407 if (idx == 1)
7408 return (char_u *)"profile";
7409 return get_user_func_name(xp, idx - 2);
7410}
7411
7412/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01007413 * ":disassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01007414 * We don't really need this at runtime, but we do have tests that require it,
7415 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007416 */
7417 void
7418ex_disassemble(exarg_T *eap)
7419{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01007420 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01007421 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007422 dfunc_T *dfunc;
Bram Moolenaardafef512022-05-21 21:55:55 +01007423 isn_T *instr = NULL; // init to shut up gcc warning
7424 int instr_count = 0; // init to shut up gcc warning
Bram Moolenaar5a01caa2022-05-21 18:56:58 +01007425 compiletype_T compile_type = CT_NONE;
Bram Moolenaare99d4222021-06-13 14:01:26 +02007426
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01007427 ufunc = find_func_by_name(arg, &compile_type);
Bram Moolenaara26b9702020-04-18 19:53:28 +02007428 if (ufunc == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007429 return;
Bram Moolenaare99d4222021-06-13 14:01:26 +02007430 if (func_needs_compiling(ufunc, compile_type)
7431 && compile_def_function(ufunc, FALSE, compile_type, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02007432 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007433 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007434 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007435 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007436 return;
7437 }
Bram Moolenaar6db660b2021-08-01 14:08:54 +02007438 msg((char *)printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007439
7440 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
Bram Moolenaare99d4222021-06-13 14:01:26 +02007441 switch (compile_type)
7442 {
7443 case CT_PROFILE:
Bram Moolenaarf002a412021-01-24 13:34:18 +01007444#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02007445 instr = dfunc->df_instr_prof;
7446 instr_count = dfunc->df_instr_prof_count;
7447 break;
Bram Moolenaarf002a412021-01-24 13:34:18 +01007448#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02007449 // FALLTHROUGH
7450 case CT_NONE:
7451 instr = dfunc->df_instr;
7452 instr_count = dfunc->df_instr_count;
7453 break;
7454 case CT_DEBUG:
7455 instr = dfunc->df_instr_debug;
7456 instr_count = dfunc->df_instr_debug_count;
7457 break;
7458 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007459
Bram Moolenaar4c137212021-04-19 16:48:48 +02007460 list_instructions("", instr, instr_count, ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007461}
7462
7463/*
Bram Moolenaar13106602020-10-04 16:06:05 +02007464 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007465 * list, etc. Mostly like what JavaScript does, except that empty list and
7466 * empty dictionary are FALSE.
7467 */
7468 int
7469tv2bool(typval_T *tv)
7470{
7471 switch (tv->v_type)
7472 {
7473 case VAR_NUMBER:
7474 return tv->vval.v_number != 0;
7475 case VAR_FLOAT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007476 return tv->vval.v_float != 0.0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007477 case VAR_PARTIAL:
7478 return tv->vval.v_partial != NULL;
7479 case VAR_FUNC:
7480 case VAR_STRING:
7481 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
7482 case VAR_LIST:
7483 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
7484 case VAR_DICT:
7485 return tv->vval.v_dict != NULL
7486 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
7487 case VAR_BOOL:
7488 case VAR_SPECIAL:
7489 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
7490 case VAR_JOB:
7491#ifdef FEAT_JOB_CHANNEL
7492 return tv->vval.v_job != NULL;
7493#else
7494 break;
7495#endif
7496 case VAR_CHANNEL:
7497#ifdef FEAT_JOB_CHANNEL
7498 return tv->vval.v_channel != NULL;
7499#else
7500 break;
7501#endif
7502 case VAR_BLOB:
7503 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
7504 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02007505 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007506 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02007507 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00007508 case VAR_CLASS:
7509 case VAR_OBJECT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007510 break;
7511 }
7512 return FALSE;
7513}
7514
Bram Moolenaarea2d4072020-11-12 12:08:51 +01007515 void
7516emsg_using_string_as(typval_T *tv, int as_number)
7517{
7518 semsg(_(as_number ? e_using_string_as_number_str
7519 : e_using_string_as_bool_str),
7520 tv->vval.v_string == NULL
7521 ? (char_u *)"" : tv->vval.v_string);
7522}
7523
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007524/*
7525 * If "tv" is a string give an error and return FAIL.
7526 */
7527 int
7528check_not_string(typval_T *tv)
7529{
7530 if (tv->v_type == VAR_STRING)
7531 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +01007532 emsg_using_string_as(tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007533 clear_tv(tv);
7534 return FAIL;
7535 }
7536 return OK;
7537}
7538
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007539
7540#endif // FEAT_EVAL