blob: 209f86fb5303752ad1ff77df211dbf2a47053ec8 [file] [log] [blame]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * vim9execute.c: execute Vim9 script instructions
12 */
13
14#define USING_FLOAT_STUFF
15#include "vim.h"
16
17#if defined(FEAT_EVAL) || defined(PROTO)
18
Bram Moolenaardc7c3662021-12-20 15:04:29 +000019// When not generating protos this is included in proto.h
20#ifdef PROTO
21# include "vim9.h"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010022#endif
23
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010024
25// Structure put on ec_trystack when ISN_TRY is encountered.
26typedef struct {
Bram Moolenaard9d77892021-02-12 21:32:47 +010027 int tcd_frame_idx; // ec_frame_idx at ISN_TRY
28 int tcd_stack_len; // size of ectx.ec_stack at ISN_TRY
Bram Moolenaard3d8fee2021-06-30 19:54:43 +020029 int tcd_in_catch; // in catch or finally block
30 int tcd_did_throw; // set did_throw in :endtry
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +010031 int tcd_catch_idx; // instruction of the first :catch or :finally
32 int tcd_finally_idx; // instruction of the :finally block or zero
33 int tcd_endtry_idx; // instruction of the :endtry
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010034 int tcd_caught; // catch block entered
Bram Moolenaar2e34c342021-03-14 12:13:33 +010035 int tcd_cont; // :continue encountered, jump here (minus one)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010036 int tcd_return; // when TRUE return from end of :finally
37} trycmd_T;
38
Bram Moolenaar4c137212021-04-19 16:48:48 +020039// Data local to a function.
40// On a function call, if not empty, is saved on the stack and restored when
41// returning.
42typedef struct {
43 int floc_restore_cmdmod;
44 cmdmod_T floc_save_cmdmod;
45 int floc_restore_cmdmod_stacklen;
46} funclocal_T;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010047
Bram Moolenaarc04f2a42021-06-09 19:30:03 +020048// Structure to hold a reference to an outer_T, with information of whether it
49// was allocated.
50typedef struct {
51 outer_T *or_outer;
52 partial_T *or_partial; // decrement "or_partial->pt_refcount" later
53 int or_outer_allocated; // free "or_outer" later
54} outer_ref_T;
55
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010056// A stack is used to store:
57// - arguments passed to a :def function
58// - info about the calling function, to use when returning
59// - local variables
60// - temporary values
61//
62// In detail (FP == Frame Pointer):
63// arg1 first argument from caller (if present)
64// arg2 second argument from caller (if present)
65// extra_arg1 any missing optional argument default value
66// FP -> cur_func calling function
Bram Moolenaar6ed545e2022-05-09 20:09:23 +010067// current previous instruction pointer
68// frame_ptr previous Frame Pointer
69// var1 space for local variable
70// var2 space for local variable
71// .... fixed space for max. number of local variables
72// temp temporary values
73// .... flexible space for temporary values (can grow big)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010074
75/*
76 * Execution context.
77 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010078struct ectx_S {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010079 garray_T ec_stack; // stack of typval_T values
Bram Moolenaarbf67ea12020-05-02 17:52:42 +020080 int ec_frame_idx; // index in ec_stack: context of ec_dfunc_idx
Bram Moolenaar4c137212021-04-19 16:48:48 +020081 int ec_initial_frame_idx; // frame index when called
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010082
Bram Moolenaarc04f2a42021-06-09 19:30:03 +020083 outer_ref_T *ec_outer_ref; // outer scope used for closures, allocated
Bram Moolenaar4c137212021-04-19 16:48:48 +020084 funclocal_T ec_funclocal;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +020085
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010086 garray_T ec_trystack; // stack of trycmd_T values
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010087
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010088 isn_T *ec_instr; // array with instructions
Dominique Pelle3276f582021-08-07 12:44:41 +020089 int ec_dfunc_idx; // current function index
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010090 int ec_iidx; // index in ec_instr: instruction to execute
Bram Moolenaar148ce7a2020-09-23 21:57:23 +020091
92 garray_T ec_funcrefs; // partials that might be a closure
Bram Moolenaar4c137212021-04-19 16:48:48 +020093
94 int ec_did_emsg_before;
95 int ec_trylevel_at_start;
96 where_T ec_where;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010097};
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010098
Bram Moolenaar12d26532021-02-19 19:13:21 +010099#ifdef FEAT_PROFILE
100// stack of profinfo_T used when profiling.
101static garray_T profile_info_ga = {0, 0, sizeof(profinfo_T), 20, NULL};
102#endif
103
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100104// Get pointer to item in the stack.
105#define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
106
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100107// Get pointer to item relative to the bottom of the stack, -1 is the last one.
Bram Moolenaar11107ba2020-08-15 21:10:16 +0200108#define STACK_TV_BOT(idx) (((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_stack.ga_len + (idx))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100109
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100110// Get pointer to a local variable on the stack. Negative for arguments.
111#define STACK_TV_VAR(idx) (((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx + STACK_FRAME_SIZE + idx)
112
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200113 void
114to_string_error(vartype_T vartype)
115{
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200116 semsg(_(e_cannot_convert_str_to_string), vartype_name(vartype));
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200117}
118
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100119/*
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100120 * Return the number of arguments, including optional arguments and any vararg.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100121 */
122 static int
123ufunc_argcount(ufunc_T *ufunc)
124{
125 return ufunc->uf_args.ga_len + (ufunc->uf_va_name != NULL ? 1 : 0);
126}
127
128/*
LemonBoy372bcce2022-04-25 12:43:20 +0100129 * Create a new string from "count" items at the bottom of the stack.
130 * A trailing NUL is appended.
131 * When "count" is zero an empty string is added to the stack.
132 */
133 static int
134exe_concat(int count, ectx_T *ectx)
135{
136 int idx;
137 int len = 0;
138 typval_T *tv;
139 garray_T ga;
140
141 ga_init2(&ga, sizeof(char), 1);
142 // Preallocate enough space for the whole string to avoid having to grow
143 // and copy.
144 for (idx = 0; idx < count; ++idx)
145 {
146 tv = STACK_TV_BOT(idx - count);
147 if (tv->vval.v_string != NULL)
148 len += (int)STRLEN(tv->vval.v_string);
149 }
150
151 if (ga_grow(&ga, len + 1) == FAIL)
152 return FAIL;
153
154 for (idx = 0; idx < count; ++idx)
155 {
156 tv = STACK_TV_BOT(idx - count);
157 ga_concat(&ga, tv->vval.v_string);
158 clear_tv(tv);
159 }
160
161 // add a terminating NUL
162 ga_append(&ga, NUL);
163
164 ectx->ec_stack.ga_len -= count - 1;
165 STACK_TV_BOT(-1)->vval.v_string = ga.ga_data;
166
167 return OK;
168}
169
170/*
Bram Moolenaarfe270812020-04-11 22:31:27 +0200171 * Create a new list from "count" items at the bottom of the stack.
172 * When "count" is zero an empty list is added to the stack.
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100173 * When "count" is -1 a NULL list is added to the stack.
Bram Moolenaarfe270812020-04-11 22:31:27 +0200174 */
175 static int
176exe_newlist(int count, ectx_T *ectx)
177{
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100178 list_T *list = NULL;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200179 int idx;
180 typval_T *tv;
181
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100182 if (count >= 0)
183 {
184 list = list_alloc_with_items(count);
185 if (list == NULL)
186 return FAIL;
187 for (idx = 0; idx < count; ++idx)
188 list_set_item(list, idx, STACK_TV_BOT(idx - count));
189 }
Bram Moolenaarfe270812020-04-11 22:31:27 +0200190
191 if (count > 0)
192 ectx->ec_stack.ga_len -= count - 1;
Bram Moolenaar35578162021-08-02 19:10:38 +0200193 else if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100194 {
195 list_unref(list);
Bram Moolenaarfe270812020-04-11 22:31:27 +0200196 return FAIL;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100197 }
Bram Moolenaarfe270812020-04-11 22:31:27 +0200198 else
199 ++ectx->ec_stack.ga_len;
200 tv = STACK_TV_BOT(-1);
201 tv->v_type = VAR_LIST;
202 tv->vval.v_list = list;
Bram Moolenaar566badc2022-09-18 13:46:08 +0100203 tv->v_lock = 0;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100204 if (list != NULL)
205 ++list->lv_refcount;
206 return OK;
207}
208
209/*
210 * Implementation of ISN_NEWDICT.
211 * Returns FAIL on total failure, MAYBE on error.
212 */
213 static int
214exe_newdict(int count, ectx_T *ectx)
215{
216 dict_T *dict = NULL;
217 dictitem_T *item;
218 char_u *key;
219 int idx;
220 typval_T *tv;
221
222 if (count >= 0)
223 {
224 dict = dict_alloc();
225 if (unlikely(dict == NULL))
226 return FAIL;
227 for (idx = 0; idx < count; ++idx)
228 {
229 // have already checked key type is VAR_STRING
230 tv = STACK_TV_BOT(2 * (idx - count));
231 // check key is unique
Bram Moolenaara9fa8c52023-01-02 18:10:04 +0000232 key = tv->vval.v_string == NULL ? (char_u *)"" : tv->vval.v_string;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100233 item = dict_find(dict, key, -1);
234 if (item != NULL)
235 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +0000236 semsg(_(e_duplicate_key_in_dictionary_str), key);
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100237 dict_unref(dict);
238 return MAYBE;
239 }
240 item = dictitem_alloc(key);
241 clear_tv(tv);
242 if (unlikely(item == NULL))
243 {
244 dict_unref(dict);
245 return FAIL;
246 }
Bram Moolenaar0d1f55c2022-04-05 17:30:29 +0100247 tv = STACK_TV_BOT(2 * (idx - count) + 1);
248 item->di_tv = *tv;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100249 item->di_tv.v_lock = 0;
Bram Moolenaar0d1f55c2022-04-05 17:30:29 +0100250 tv->v_type = VAR_UNKNOWN;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100251 if (dict_add(dict, item) == FAIL)
252 {
253 // can this ever happen?
254 dict_unref(dict);
255 return FAIL;
256 }
257 }
258 }
259
260 if (count > 0)
261 ectx->ec_stack.ga_len -= 2 * count - 1;
262 else if (GA_GROW_FAILS(&ectx->ec_stack, 1))
263 return FAIL;
264 else
265 ++ectx->ec_stack.ga_len;
266 tv = STACK_TV_BOT(-1);
267 tv->v_type = VAR_DICT;
268 tv->v_lock = 0;
269 tv->vval.v_dict = dict;
270 if (dict != NULL)
271 ++dict->dv_refcount;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200272 return OK;
273}
274
275/*
Bram Moolenaar4f8f5422021-06-20 19:28:14 +0200276 * If debug_tick changed check if "ufunc" has a breakpoint and update
277 * "uf_has_breakpoint".
278 */
Bram Moolenaar96923b72022-03-15 15:57:04 +0000279 void
Bram Moolenaar4f8f5422021-06-20 19:28:14 +0200280update_has_breakpoint(ufunc_T *ufunc)
281{
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000282 if (ufunc->uf_debug_tick == debug_tick)
283 return;
Bram Moolenaar4f8f5422021-06-20 19:28:14 +0200284
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000285 linenr_T breakpoint;
286
287 ufunc->uf_debug_tick = debug_tick;
288 breakpoint = dbg_find_breakpoint(FALSE, ufunc->uf_name, 0);
289 ufunc->uf_has_breakpoint = breakpoint > 0;
Bram Moolenaar4f8f5422021-06-20 19:28:14 +0200290}
291
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +0200292static garray_T dict_stack = GA_EMPTY;
293
294/*
295 * Put a value on the dict stack. This consumes "tv".
296 */
297 static int
298dict_stack_save(typval_T *tv)
299{
300 if (dict_stack.ga_growsize == 0)
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000301 ga_init2(&dict_stack, sizeof(typval_T), 10);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +0200302 if (ga_grow(&dict_stack, 1) == FAIL)
303 return FAIL;
304 ((typval_T *)dict_stack.ga_data)[dict_stack.ga_len] = *tv;
305 ++dict_stack.ga_len;
306 return OK;
307}
308
309/*
310 * Get the typval at top of the dict stack.
311 */
312 static typval_T *
313dict_stack_get_tv(void)
314{
315 if (dict_stack.ga_len == 0)
316 return NULL;
317 return ((typval_T *)dict_stack.ga_data) + dict_stack.ga_len - 1;
318}
319
320/*
321 * Get the dict at top of the dict stack.
322 */
323 static dict_T *
324dict_stack_get_dict(void)
325{
326 typval_T *tv;
327
328 if (dict_stack.ga_len == 0)
329 return NULL;
330 tv = ((typval_T *)dict_stack.ga_data) + dict_stack.ga_len - 1;
331 if (tv->v_type == VAR_DICT)
332 return tv->vval.v_dict;
333 return NULL;
334}
335
336/*
337 * Drop an item from the dict stack.
338 */
339 static void
340dict_stack_drop(void)
341{
342 if (dict_stack.ga_len == 0)
343 {
344 iemsg("Dict stack underflow");
345 return;
346 }
347 --dict_stack.ga_len;
348 clear_tv(((typval_T *)dict_stack.ga_data) + dict_stack.ga_len);
349}
350
351/*
352 * Drop items from the dict stack until the length is equal to "len".
353 */
354 static void
355dict_stack_clear(int len)
356{
357 while (dict_stack.ga_len > len)
358 dict_stack_drop();
359}
360
Bram Moolenaar4f8f5422021-06-20 19:28:14 +0200361/*
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +0000362 * Get a pointer to useful "pt_outer" of "pt".
363 */
364 static outer_T *
365get_pt_outer(partial_T *pt)
366{
367 partial_T *ptref = pt->pt_outer_partial;
368
369 if (ptref == NULL)
370 return &pt->pt_outer;
371
372 // partial using partial (recursively)
373 while (ptref->pt_outer_partial != NULL)
374 ptref = ptref->pt_outer_partial;
375 return &ptref->pt_outer;
376}
377
378/*
Bram Moolenaar0d89d8a2022-12-31 14:01:24 +0000379 * Check "argcount" arguments on the stack against what "ufunc" expects.
380 * "off" is the offset of arguments on the stack.
381 * Return OK or FAIL.
382 */
383 static int
384check_ufunc_arg_types(ufunc_T *ufunc, int argcount, int off, ectx_T *ectx)
385{
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000386 if (ufunc->uf_arg_types == NULL && ufunc->uf_va_type == NULL)
387 return OK;
388
389 typval_T *argv = STACK_TV_BOT(0) - argcount - off;
390
391 // The function can change at runtime, check that the argument
392 // types are correct.
393 for (int i = 0; i < argcount; ++i)
Bram Moolenaar0d89d8a2022-12-31 14:01:24 +0000394 {
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000395 type_T *type = NULL;
Bram Moolenaar0d89d8a2022-12-31 14:01:24 +0000396
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000397 // assume a v:none argument, using the default value, is always OK
398 if (argv[i].v_type == VAR_SPECIAL
399 && argv[i].vval.v_number == VVAL_NONE)
400 continue;
Bram Moolenaar0d89d8a2022-12-31 14:01:24 +0000401
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000402 if (i < ufunc->uf_args.ga_len && ufunc->uf_arg_types != NULL)
403 type = ufunc->uf_arg_types[i];
404 else if (ufunc->uf_va_type != NULL)
405 type = ufunc->uf_va_type->tt_member;
406 if (type != NULL && check_typval_arg_type(type,
407 &argv[i], NULL, i + 1) == FAIL)
408 return FAIL;
Bram Moolenaar0d89d8a2022-12-31 14:01:24 +0000409 }
410 return OK;
411}
412
413/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100414 * Call compiled function "cdf_idx" from compiled code.
Bram Moolenaarab360522021-01-10 14:02:28 +0100415 * This adds a stack frame and sets the instruction pointer to the start of the
416 * called function.
Bram Moolenaar5800c792022-09-22 17:34:01 +0100417 * If "pt_arg" is not NULL use "pt_arg->pt_outer" for ec_outer_ref->or_outer.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100418 *
419 * Stack has:
420 * - current arguments (already there)
421 * - omitted optional argument (default values) added here
422 * - stack frame:
423 * - pointer to calling function
424 * - Index of next instruction in calling function
425 * - previous frame pointer
426 * - reserved space for local variables
427 */
428 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100429call_dfunc(
430 int cdf_idx,
Bram Moolenaar5800c792022-09-22 17:34:01 +0100431 partial_T *pt_arg,
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100432 int argcount_arg,
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100433 ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100434{
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100435 int argcount = argcount_arg;
436 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + cdf_idx;
437 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200438 int did_emsg_before = did_emsg_cumul + did_emsg;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100439 int arg_to_add;
440 int vararg_count = 0;
441 int varcount;
442 int idx;
443 estack_T *entry;
444 funclocal_T *floc = NULL;
Bram Moolenaar648594e2021-07-11 17:55:01 +0200445 int res = OK;
Bram Moolenaar139575d2022-03-15 19:29:30 +0000446 compiletype_T compile_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100447
448 if (dfunc->df_deleted)
449 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100450 // don't use ufunc->uf_name, it may have been freed
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000451 emsg_funcname(e_function_was_deleted_str,
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100452 dfunc->df_name == NULL ? (char_u *)"unknown" : dfunc->df_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100453 return FAIL;
454 }
455
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100456#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +0100457 if (do_profiling == PROF_YES)
458 {
Bram Moolenaar35578162021-08-02 19:10:38 +0200459 if (GA_GROW_OK(&profile_info_ga, 1))
Bram Moolenaar12d26532021-02-19 19:13:21 +0100460 {
461 profinfo_T *info = ((profinfo_T *)profile_info_ga.ga_data)
462 + profile_info_ga.ga_len;
463 ++profile_info_ga.ga_len;
464 CLEAR_POINTER(info);
465 profile_may_start_func(info, ufunc,
466 (((dfunc_T *)def_functions.ga_data)
467 + ectx->ec_dfunc_idx)->df_ufunc);
468 }
Bram Moolenaar12d26532021-02-19 19:13:21 +0100469 }
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100470#endif
471
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200472 // When debugging and using "cont" switches to the not-debugged
473 // instructions, may need to still compile them.
Bram Moolenaar139575d2022-03-15 19:29:30 +0000474 compile_type = get_compile_type(ufunc);
475 if (func_needs_compiling(ufunc, compile_type))
Bram Moolenaar648594e2021-07-11 17:55:01 +0200476 {
Bram Moolenaar139575d2022-03-15 19:29:30 +0000477 res = compile_def_function(ufunc, FALSE, compile_type, NULL);
Bram Moolenaar648594e2021-07-11 17:55:01 +0200478
479 // compile_def_function() may cause def_functions.ga_data to change
480 dfunc = ((dfunc_T *)def_functions.ga_data) + cdf_idx;
481 }
482 if (res == FAIL || INSTRUCTIONS(dfunc) == NULL)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200483 {
484 if (did_emsg_cumul + did_emsg == did_emsg_before)
485 semsg(_(e_function_is_not_compiled_str),
486 printable_func_name(ufunc));
487 return FAIL;
488 }
489
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200490 if (ufunc->uf_va_name != NULL)
491 {
Bram Moolenaarfe270812020-04-11 22:31:27 +0200492 // Need to make a list out of the vararg arguments.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200493 // Stack at time of call with 2 varargs:
494 // normal_arg
495 // optional_arg
496 // vararg_1
497 // vararg_2
Bram Moolenaarfe270812020-04-11 22:31:27 +0200498 // After creating the list:
499 // normal_arg
500 // optional_arg
501 // vararg-list
502 // With missing optional arguments we get:
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200503 // normal_arg
Bram Moolenaarfe270812020-04-11 22:31:27 +0200504 // After creating the list
505 // normal_arg
506 // (space for optional_arg)
507 // vararg-list
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200508 vararg_count = argcount - ufunc->uf_args.ga_len;
509 if (vararg_count < 0)
510 vararg_count = 0;
511 else
512 argcount -= vararg_count;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200513 if (exe_newlist(vararg_count, ectx) == FAIL)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200514 return FAIL;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200515
516 vararg_count = 1;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200517 }
518
Bram Moolenaarfe270812020-04-11 22:31:27 +0200519 arg_to_add = ufunc->uf_args.ga_len - argcount;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200520 if (arg_to_add < 0)
521 {
Matvey Tarasovd14bb1a2022-06-29 13:18:27 +0100522 semsg(NGETTEXT(e_one_argument_too_many, e_nr_arguments_too_many,
523 -arg_to_add), -arg_to_add);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200524 return FAIL;
525 }
Bram Moolenaarcd1cda22022-02-16 21:48:25 +0000526 else if (arg_to_add > ufunc->uf_def_args.ga_len)
527 {
528 int missing = arg_to_add - ufunc->uf_def_args.ga_len;
529
Matvey Tarasovd14bb1a2022-06-29 13:18:27 +0100530 semsg(NGETTEXT(e_one_argument_too_few, e_nr_arguments_too_few,
531 missing), missing);
Bram Moolenaarcd1cda22022-02-16 21:48:25 +0000532 return FAIL;
533 }
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200534
Bram Moolenaar574950d2023-01-03 19:08:50 +0000535 // If this is an object method, the object is just before the arguments.
536 typval_T *obj = STACK_TV_BOT(0) - argcount - vararg_count - 1;
537
Yegappan Lakshmananc1946262023-09-25 21:00:46 +0200538 if (IS_OBJECT_METHOD(ufunc) && !IS_CONSTRUCTOR_METHOD(ufunc)
539 && obj->v_type == VAR_OBJECT && obj->vval.v_object == NULL)
540 {
541 // If this is not a constructor method, then a valid object is
542 // needed.
543 emsg(_(e_using_null_object));
544 return FAIL;
545 }
546
Bram Moolenaar0d89d8a2022-12-31 14:01:24 +0000547 // Check the argument types.
548 if (check_ufunc_arg_types(ufunc, argcount, vararg_count, ectx) == FAIL)
549 return FAIL;
550
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200551 // Reserve space for:
552 // - missing arguments
553 // - stack frame
554 // - local variables
555 // - if needed: a counter for number of closures created in
556 // ectx->ec_funcrefs.
557 varcount = dfunc->df_varcount + dfunc->df_has_closure;
Bram Moolenaarb46c0832022-09-15 17:19:37 +0100558 if (GA_GROW_FAILS(&ectx->ec_stack,
559 arg_to_add + STACK_FRAME_SIZE + varcount))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100560 return FAIL;
561
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100562 // If depth of calling is getting too high, don't execute the function.
563 if (funcdepth_increment() == FAIL)
564 return FAIL;
Bram Moolenaare99d4222021-06-13 14:01:26 +0200565 ++ex_nesting_level;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100566
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100567 // Only make a copy of funclocal if it contains something to restore.
Bram Moolenaar4c137212021-04-19 16:48:48 +0200568 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100569 {
570 floc = ALLOC_ONE(funclocal_T);
571 if (floc == NULL)
572 return FAIL;
Bram Moolenaar4c137212021-04-19 16:48:48 +0200573 *floc = ectx->ec_funclocal;
574 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100575 }
576
Bram Moolenaarfe270812020-04-11 22:31:27 +0200577 // Move the vararg-list to below the missing optional arguments.
578 if (vararg_count > 0 && arg_to_add > 0)
579 *STACK_TV_BOT(arg_to_add - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100580
581 // Reserve space for omitted optional arguments, filled in soon.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200582 for (idx = 0; idx < arg_to_add; ++idx)
Bram Moolenaarfe270812020-04-11 22:31:27 +0200583 STACK_TV_BOT(idx - vararg_count)->v_type = VAR_UNKNOWN;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200584 ectx->ec_stack.ga_len += arg_to_add;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100585
586 // Store current execution state in stack frame for ISN_RETURN.
Bram Moolenaar0186e582021-01-10 18:33:11 +0100587 STACK_TV_BOT(STACK_FRAME_FUNC_OFF)->vval.v_number = ectx->ec_dfunc_idx;
588 STACK_TV_BOT(STACK_FRAME_IIDX_OFF)->vval.v_number = ectx->ec_iidx;
Bram Moolenaar5930ddc2021-04-26 20:32:59 +0200589 STACK_TV_BOT(STACK_FRAME_INSTR_OFF)->vval.v_string = (void *)ectx->ec_instr;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200590 STACK_TV_BOT(STACK_FRAME_OUTER_OFF)->vval.v_string =
591 (void *)ectx->ec_outer_ref;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100592 STACK_TV_BOT(STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string = (void *)floc;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100593 STACK_TV_BOT(STACK_FRAME_IDX_OFF)->vval.v_number = ectx->ec_frame_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200594 ectx->ec_frame_idx = ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100595
Bram Moolenaar5800c792022-09-22 17:34:01 +0100596 // Initialize all local variables to number zero. Also initialize the
597 // variable that counts how many closures were created. This is used in
598 // handle_closure_in_use().
599 int initcount = dfunc->df_varcount + (dfunc->df_has_closure ? 1 : 0);
600 for (idx = 0; idx < initcount; ++idx)
Bram Moolenaar5cd64792021-12-25 18:23:24 +0000601 {
602 typval_T *tv = STACK_TV_BOT(STACK_FRAME_SIZE + idx);
603
604 tv->v_type = VAR_NUMBER;
605 tv->vval.v_number = 0;
606 }
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200607 ectx->ec_stack.ga_len += STACK_FRAME_SIZE + varcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100608
Bram Moolenaar574950d2023-01-03 19:08:50 +0000609 // For an object method move the object from just before the arguments to
610 // the first local variable.
Yegappan Lakshmanan1db15142023-09-19 20:34:05 +0200611 if (IS_OBJECT_METHOD(ufunc))
Bram Moolenaar574950d2023-01-03 19:08:50 +0000612 {
613 *STACK_TV_VAR(0) = *obj;
614 obj->v_type = VAR_UNKNOWN;
615 }
616
Bram Moolenaar5800c792022-09-22 17:34:01 +0100617 partial_T *pt = pt_arg != NULL ? pt_arg : ufunc->uf_partial;
618 if (pt != NULL || (ufunc->uf_flags & FC_CLOSURE))
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100619 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200620 outer_ref_T *ref = ALLOC_CLEAR_ONE(outer_ref_T);
Bram Moolenaar0186e582021-01-10 18:33:11 +0100621
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200622 if (ref == NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +0100623 return FAIL;
624 if (pt != NULL)
625 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +0000626 ref->or_outer = get_pt_outer(pt);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200627 ++pt->pt_refcount;
628 ref->or_partial = pt;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100629 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100630 else
631 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200632 ref->or_outer = ALLOC_CLEAR_ONE(outer_T);
Dominique Pelle5a9e5842021-07-24 19:32:12 +0200633 if (unlikely(ref->or_outer == NULL))
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200634 {
635 vim_free(ref);
636 return FAIL;
637 }
638 ref->or_outer_allocated = TRUE;
639 ref->or_outer->out_stack = &ectx->ec_stack;
640 ref->or_outer->out_frame_idx = ectx->ec_frame_idx;
641 if (ectx->ec_outer_ref != NULL)
642 ref->or_outer->out_up = ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100643 }
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200644 ectx->ec_outer_ref = ref;
Bram Moolenaarab360522021-01-10 14:02:28 +0100645 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100646 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200647 ectx->ec_outer_ref = NULL;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100648
Bram Moolenaarc970e422021-03-17 15:03:04 +0100649 ++ufunc->uf_calls;
650
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100651 // Set execution state to the start of the called function.
652 ectx->ec_dfunc_idx = cdf_idx;
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100653 ectx->ec_instr = INSTRUCTIONS(dfunc);
Bram Moolenaarb2049902021-01-24 12:53:53 +0100654 entry = estack_push_ufunc(ufunc, 1);
Bram Moolenaarc620c052020-07-08 15:16:19 +0200655 if (entry != NULL)
656 {
657 // Set the script context to the script where the function was defined.
Bram Moolenaarc70fe462021-04-17 17:59:19 +0200658 // Save the current context so it can be restored on return.
659 entry->es_save_sctx = current_sctx;
660 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200661 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100662
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +0200663 // Start execution at the first instruction.
664 ectx->ec_iidx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100665
666 return OK;
667}
668
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000669// Double linked list of funcstack_T in use.
670static funcstack_T *first_funcstack = NULL;
671
672 static void
673add_funcstack_to_list(funcstack_T *funcstack)
674{
675 // Link in list of funcstacks.
676 if (first_funcstack != NULL)
677 first_funcstack->fs_prev = funcstack;
678 funcstack->fs_next = first_funcstack;
679 funcstack->fs_prev = NULL;
680 first_funcstack = funcstack;
681}
682
683 static void
684remove_funcstack_from_list(funcstack_T *funcstack)
685{
686 if (funcstack->fs_prev == NULL)
687 first_funcstack = funcstack->fs_next;
688 else
689 funcstack->fs_prev->fs_next = funcstack->fs_next;
690 if (funcstack->fs_next != NULL)
691 funcstack->fs_next->fs_prev = funcstack->fs_prev;
692}
693
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100694/*
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200695 * Used when returning from a function: Check if any closure is still
696 * referenced. If so then move the arguments and variables to a separate piece
697 * of stack to be used when the closure is called.
698 * When "free_arguments" is TRUE the arguments are to be freed.
699 * Returns FAIL when out of memory.
700 */
701 static int
702handle_closure_in_use(ectx_T *ectx, int free_arguments)
703{
704 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
705 + ectx->ec_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200706 int argcount;
707 int top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200708 int idx;
709 typval_T *tv;
710 int closure_in_use = FALSE;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200711 garray_T *gap = &ectx->ec_funcrefs;
712 varnumber_T closure_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200713
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200714 if (dfunc->df_ufunc == NULL)
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200715 return OK; // function was freed
716 if (dfunc->df_has_closure == 0)
717 return OK; // no closures
718 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + dfunc->df_varcount);
719 closure_count = tv->vval.v_number;
720 if (closure_count == 0)
721 return OK; // no funcrefs created
722
Bram Moolenaar8fa745e2022-09-16 19:04:24 +0100723 // Compute "top": the first entry in the stack used by the function.
724 // This is the first argument (after that comes the stack frame and then
725 // the local variables).
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200726 argcount = ufunc_argcount(dfunc->df_ufunc);
727 top = ectx->ec_frame_idx - argcount;
728
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200729 // Check if any created closure is still in use.
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200730 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200731 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200732 partial_T *pt;
733 int off = gap->ga_len - closure_count + idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200734
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200735 if (off < 0)
736 continue; // count is off or already done
737 pt = ((partial_T **)gap->ga_data)[off];
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200738 if (pt->pt_refcount > 1)
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200739 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200740 int refcount = pt->pt_refcount;
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200741 int i;
742
Dominique Pelleaf4a61a2021-12-27 17:21:41 +0000743 // A Reference in a local variable doesn't count, it gets
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200744 // unreferenced on return.
745 for (i = 0; i < dfunc->df_varcount; ++i)
746 {
747 typval_T *stv = STACK_TV(ectx->ec_frame_idx
748 + STACK_FRAME_SIZE + i);
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200749 if (stv->v_type == VAR_PARTIAL && pt == stv->vval.v_partial)
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200750 --refcount;
751 }
752 if (refcount > 1)
753 {
754 closure_in_use = TRUE;
755 break;
756 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200757 }
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200758 }
759
760 if (closure_in_use)
761 {
762 funcstack_T *funcstack = ALLOC_CLEAR_ONE(funcstack_T);
763 typval_T *stack;
764
765 // A closure is using the arguments and/or local variables.
766 // Move them to the called function.
767 if (funcstack == NULL)
768 return FAIL;
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000769
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200770 funcstack->fs_var_offset = argcount + STACK_FRAME_SIZE;
Bram Moolenaar1936c762022-09-28 15:19:10 +0100771 funcstack->fs_ga.ga_len = funcstack->fs_var_offset
772 + dfunc->df_varcount;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200773 stack = ALLOC_CLEAR_MULT(typval_T, funcstack->fs_ga.ga_len);
774 funcstack->fs_ga.ga_data = stack;
775 if (stack == NULL)
776 {
777 vim_free(funcstack);
778 return FAIL;
779 }
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000780 add_funcstack_to_list(funcstack);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200781
782 // Move or copy the arguments.
783 for (idx = 0; idx < argcount; ++idx)
784 {
785 tv = STACK_TV(top + idx);
786 if (free_arguments)
787 {
788 *(stack + idx) = *tv;
789 tv->v_type = VAR_UNKNOWN;
790 }
791 else
792 copy_tv(tv, stack + idx);
793 }
Bram Moolenaar8fa745e2022-09-16 19:04:24 +0100794 // Skip the stack frame.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200795 // Move the local variables.
796 for (idx = 0; idx < dfunc->df_varcount; ++idx)
797 {
798 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + idx);
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200799
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200800 // A partial created for a local function, that is also used as a
801 // local variable, has a reference count for the variable, thus
802 // will never go down to zero. When all these refcounts are one
803 // then the funcstack is unused. We need to count how many we have
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000804 // so we know when to check.
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200805 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL)
806 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200807 int i;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200808
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200809 for (i = 0; i < closure_count; ++i)
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200810 if (tv->vval.v_partial == ((partial_T **)gap->ga_data)[
811 gap->ga_len - closure_count + i])
812 ++funcstack->fs_min_refcount;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200813 }
814
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200815 *(stack + funcstack->fs_var_offset + idx) = *tv;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200816 tv->v_type = VAR_UNKNOWN;
817 }
818
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200819 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200820 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200821 partial_T *pt = ((partial_T **)gap->ga_data)[gap->ga_len
822 - closure_count + idx];
823 if (pt->pt_refcount > 1)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200824 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200825 ++funcstack->fs_refcount;
826 pt->pt_funcstack = funcstack;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100827 pt->pt_outer.out_stack = &funcstack->fs_ga;
828 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx - top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200829 }
830 }
831 }
832
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200833 for (idx = 0; idx < closure_count; ++idx)
834 partial_unref(((partial_T **)gap->ga_data)[gap->ga_len
835 - closure_count + idx]);
836 gap->ga_len -= closure_count;
837 if (gap->ga_len == 0)
838 ga_clear(gap);
839
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200840 return OK;
841}
842
843/*
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200844 * Called when a partial is freed or its reference count goes down to one. The
845 * funcstack may be the only reference to the partials in the local variables.
846 * Go over all of them, the funcref and can be freed if all partials
847 * referencing the funcstack have a reference count of one.
Bram Moolenaaracd6b992022-09-17 16:27:39 +0100848 * Returns TRUE if the funcstack is freed, the partial referencing it will then
849 * also have been freed.
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200850 */
Bram Moolenaaracd6b992022-09-17 16:27:39 +0100851 int
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200852funcstack_check_refcount(funcstack_T *funcstack)
853{
Bram Moolenaaracd6b992022-09-17 16:27:39 +0100854 int i;
855 garray_T *gap = &funcstack->fs_ga;
856 int done = 0;
857 typval_T *stack;
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200858
859 if (funcstack->fs_refcount > funcstack->fs_min_refcount)
Bram Moolenaaracd6b992022-09-17 16:27:39 +0100860 return FALSE;
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200861 for (i = funcstack->fs_var_offset; i < gap->ga_len; ++i)
862 {
863 typval_T *tv = ((typval_T *)gap->ga_data) + i;
864
865 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL
866 && tv->vval.v_partial->pt_funcstack == funcstack
867 && tv->vval.v_partial->pt_refcount == 1)
868 ++done;
869 }
Bram Moolenaaracd6b992022-09-17 16:27:39 +0100870 if (done != funcstack->fs_min_refcount)
871 return FALSE;
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200872
Bram Moolenaaracd6b992022-09-17 16:27:39 +0100873 stack = gap->ga_data;
874
875 // All partials referencing the funcstack have a reference count of
876 // one, thus the funcstack is no longer of use.
877 for (i = 0; i < gap->ga_len; ++i)
878 clear_tv(stack + i);
879 vim_free(stack);
880 remove_funcstack_from_list(funcstack);
881 vim_free(funcstack);
882
883 return TRUE;
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200884}
885
886/*
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000887 * For garbage collecting: set references in all variables referenced by
888 * all funcstacks.
889 */
890 int
891set_ref_in_funcstacks(int copyID)
892{
893 funcstack_T *funcstack;
894
895 for (funcstack = first_funcstack; funcstack != NULL;
896 funcstack = funcstack->fs_next)
897 {
898 typval_T *stack = funcstack->fs_ga.ga_data;
899 int i;
900
901 for (i = 0; i < funcstack->fs_ga.ga_len; ++i)
902 if (set_ref_in_item(stack + i, copyID, NULL, NULL))
903 return TRUE; // abort
904 }
905 return FALSE;
906}
907
Bram Moolenaar806a2732022-09-04 15:40:36 +0100908// Ugly static to avoid passing the execution context around through many
909// layers.
910static ectx_T *current_ectx = NULL;
911
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000912/*
Bram Moolenaar806a2732022-09-04 15:40:36 +0100913 * Return TRUE if currently executing a :def function.
914 * Can be used by builtin functions only.
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100915 */
Bram Moolenaar806a2732022-09-04 15:40:36 +0100916 int
917in_def_function(void)
918{
919 return current_ectx != NULL;
920}
921
922/*
Bram Moolenaar98aff652022-09-06 21:02:35 +0100923 * Clear "current_ectx" and return the previous value. To be used when calling
924 * a user function.
925 */
926 ectx_T *
dundargocc57b5bc2022-11-02 13:30:51 +0000927clear_current_ectx(void)
Bram Moolenaar98aff652022-09-06 21:02:35 +0100928{
929 ectx_T *r = current_ectx;
930
931 current_ectx = NULL;
932 return r;
933}
934
935 void
936restore_current_ectx(ectx_T *ectx)
937{
938 if (current_ectx != NULL)
939 iemsg("Restoring current_ectx while it is not NULL");
940 current_ectx = ectx;
941}
942
943/*
Bram Moolenaar806a2732022-09-04 15:40:36 +0100944 * Add an entry for a deferred function call to the currently executing
945 * function.
946 * Return the list or NULL when failed.
947 */
948 static list_T *
949add_defer_item(int var_idx, int argcount, ectx_T *ectx)
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100950{
951 typval_T *defer_tv = STACK_TV_VAR(var_idx);
952 list_T *defer_l;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100953 list_T *l;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100954 typval_T listval;
955
956 if (defer_tv->v_type != VAR_LIST)
957 {
Bram Moolenaara5348f22022-09-04 11:42:22 +0100958 // first time, allocate the list
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100959 if (rettv_list_alloc(defer_tv) == FAIL)
Bram Moolenaar806a2732022-09-04 15:40:36 +0100960 return NULL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100961 }
962 defer_l = defer_tv->vval.v_list;
963
964 l = list_alloc_with_items(argcount + 1);
965 if (l == NULL)
Bram Moolenaar806a2732022-09-04 15:40:36 +0100966 return NULL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100967 listval.v_type = VAR_LIST;
968 listval.vval.v_list = l;
969 listval.v_lock = 0;
Bram Moolenaara5348f22022-09-04 11:42:22 +0100970 if (list_insert_tv(defer_l, &listval, defer_l->lv_first) == FAIL)
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100971 {
972 vim_free(l);
Bram Moolenaar806a2732022-09-04 15:40:36 +0100973 return NULL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100974 }
975
Bram Moolenaar806a2732022-09-04 15:40:36 +0100976 return l;
977}
978
979/*
980 * Handle ISN_DEFER. Stack has a function reference and "argcount" arguments.
981 * The local variable that lists deferred functions is "var_idx".
982 * Returns OK or FAIL.
983 */
984 static int
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +0000985defer_command(int var_idx, int has_obj, int argcount, ectx_T *ectx)
Bram Moolenaar806a2732022-09-04 15:40:36 +0100986{
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +0000987 int obj_off = has_obj ? 1 : 0;
988 list_T *l = add_defer_item(var_idx, argcount + obj_off, ectx);
Bram Moolenaar806a2732022-09-04 15:40:36 +0100989 int i;
990 typval_T *func_tv;
991
992 if (l == NULL)
993 return FAIL;
994
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100995 func_tv = STACK_TV_BOT(-argcount - 1);
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +0000996 if (has_obj ? func_tv->v_type != VAR_PARTIAL : func_tv->v_type != VAR_FUNC)
Bram Moolenaarf5fec052022-09-11 11:49:22 +0100997 {
998 semsg(_(e_expected_str_but_got_str),
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +0000999 has_obj ? "partial" : "function",
Bram Moolenaarf5fec052022-09-11 11:49:22 +01001000 vartype_name(func_tv->v_type));
1001 return FAIL;
1002 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001003 list_set_item(l, 0, func_tv);
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001004 if (has_obj)
1005 list_set_item(l, 1, STACK_TV_BOT(-argcount - 2));
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001006
Bram Moolenaarf5fec052022-09-11 11:49:22 +01001007 for (i = 0; i < argcount; ++i)
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001008 list_set_item(l, i + 1 + obj_off, STACK_TV_BOT(-argcount + i));
1009 ectx->ec_stack.ga_len -= argcount + 1 + obj_off;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001010 return OK;
1011}
1012
1013/*
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001014 * Add a deferred call for "name" with arguments "argvars[argcount]".
Bram Moolenaar806a2732022-09-04 15:40:36 +01001015 * Consumes "name", also on failure.
1016 * Only to be called when in_def_function() returns TRUE.
1017 */
1018 int
1019add_defer_function(char_u *name, int argcount, typval_T *argvars)
1020{
1021 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1022 + current_ectx->ec_dfunc_idx;
1023 list_T *l;
1024 typval_T func_tv;
1025 int i;
1026
1027 if (dfunc->df_defer_var_idx == 0)
1028 {
1029 iemsg("df_defer_var_idx is zero");
Bram Moolenaar31ea6bf2022-09-05 10:47:13 +01001030 vim_free(name);
Bram Moolenaar806a2732022-09-04 15:40:36 +01001031 return FAIL;
1032 }
Bram Moolenaar806a2732022-09-04 15:40:36 +01001033
Bram Moolenaarf5fec052022-09-11 11:49:22 +01001034 l = add_defer_item(dfunc->df_defer_var_idx - 1, argcount, current_ectx);
Bram Moolenaar806a2732022-09-04 15:40:36 +01001035 if (l == NULL)
1036 {
Bram Moolenaar31ea6bf2022-09-05 10:47:13 +01001037 vim_free(name);
Bram Moolenaar806a2732022-09-04 15:40:36 +01001038 return FAIL;
1039 }
1040
Bram Moolenaar31ea6bf2022-09-05 10:47:13 +01001041 func_tv.v_type = VAR_FUNC;
1042 func_tv.v_lock = 0;
1043 func_tv.vval.v_string = name;
Bram Moolenaar806a2732022-09-04 15:40:36 +01001044 list_set_item(l, 0, &func_tv);
Bram Moolenaar31ea6bf2022-09-05 10:47:13 +01001045
Bram Moolenaar806a2732022-09-04 15:40:36 +01001046 for (i = 0; i < argcount; ++i)
1047 list_set_item(l, i + 1, argvars + i);
1048 return OK;
1049}
1050
1051/*
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001052 * Invoked when returning from a function: Invoke any deferred calls.
1053 */
1054 static void
1055invoke_defer_funcs(ectx_T *ectx)
1056{
1057 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1058 + ectx->ec_dfunc_idx;
1059 typval_T *defer_tv = STACK_TV_VAR(dfunc->df_defer_var_idx - 1);
1060 listitem_T *li;
1061
1062 if (defer_tv->v_type != VAR_LIST)
1063 return; // no function added
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00001064 FOR_ALL_LIST_ITEMS(defer_tv->vval.v_list, li)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001065 {
1066 list_T *l = li->li_tv.vval.v_list;
1067 typval_T rettv;
1068 typval_T argvars[MAX_FUNC_ARGS];
1069 int i;
1070 listitem_T *arg_li = l->lv_first;
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001071 typval_T *functv = &l->lv_first->li_tv;
1072 int obj_off = functv->v_type == VAR_PARTIAL ? 1 : 0;
1073 int argcount = l->lv_len - 1 - obj_off;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001074
zeertzjqa1f2b5d2023-04-18 21:04:53 +01001075 if (functv->vval.v_string == NULL)
1076 // already being called, can happen if function does ":qa"
1077 continue;
1078
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001079 if (obj_off == 1)
1080 arg_li = arg_li->li_next; // second list item is the object
1081 for (i = 0; i < argcount; ++i)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001082 {
1083 arg_li = arg_li->li_next;
1084 argvars[i] = arg_li->li_tv;
1085 }
1086
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001087 funcexe_T funcexe;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001088 CLEAR_FIELD(funcexe);
1089 funcexe.fe_evaluate = TRUE;
1090 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001091 if (functv->v_type == VAR_PARTIAL)
1092 {
1093 funcexe.fe_partial = functv->vval.v_partial;
1094 funcexe.fe_object = l->lv_first->li_next->li_tv.vval.v_object;
1095 if (funcexe.fe_object != NULL)
1096 ++funcexe.fe_object->obj_refcount;
1097 }
zeertzjqa1f2b5d2023-04-18 21:04:53 +01001098
1099 char_u *name = functv->vval.v_string;
1100 functv->vval.v_string = NULL;
1101
1102 (void)call_func(name, -1, &rettv, argcount, argvars, &funcexe);
1103
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001104 clear_tv(&rettv);
zeertzjqa1f2b5d2023-04-18 21:04:53 +01001105 vim_free(name);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001106 }
1107}
1108
1109/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001110 * Return from the current function.
1111 */
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001112 static int
Bram Moolenaar4c137212021-04-19 16:48:48 +02001113func_return(ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001114{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001115 int idx;
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01001116 int ret_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001117 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1118 + ectx->ec_dfunc_idx;
1119 int argcount = ufunc_argcount(dfunc->df_ufunc);
Bram Moolenaarc620c052020-07-08 15:16:19 +02001120 estack_T *entry;
Bram Moolenaar12d26532021-02-19 19:13:21 +01001121 int prev_dfunc_idx = STACK_TV(ectx->ec_frame_idx
1122 + STACK_FRAME_FUNC_OFF)->vval.v_number;
Bram Moolenaarb06b50d2021-04-26 21:39:25 +02001123 funclocal_T *floc;
1124#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01001125 dfunc_T *prev_dfunc = ((dfunc_T *)def_functions.ga_data)
1126 + prev_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001127
Bram Moolenaar12d26532021-02-19 19:13:21 +01001128 if (do_profiling == PROF_YES)
1129 {
1130 ufunc_T *caller = prev_dfunc->df_ufunc;
1131
1132 if (dfunc->df_ufunc->uf_profiling
1133 || (caller != NULL && caller->uf_profiling))
1134 {
1135 profile_may_end_func(((profinfo_T *)profile_info_ga.ga_data)
1136 + profile_info_ga.ga_len - 1, dfunc->df_ufunc, caller);
1137 --profile_info_ga.ga_len;
1138 }
1139 }
1140#endif
Bram Moolenaar919c12c2021-12-14 14:29:16 +00001141
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001142 if (dfunc->df_defer_var_idx > 0)
1143 invoke_defer_funcs(ectx);
1144
Bram Moolenaar919c12c2021-12-14 14:29:16 +00001145 // No check for uf_refcount being zero, cannot think of a way that would
1146 // happen.
Bram Moolenaarc970e422021-03-17 15:03:04 +01001147 --dfunc->df_ufunc->uf_calls;
1148
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001149 // execution context goes one level up
Bram Moolenaarc620c052020-07-08 15:16:19 +02001150 entry = estack_pop();
1151 if (entry != NULL)
Bram Moolenaarc70fe462021-04-17 17:59:19 +02001152 current_sctx = entry->es_save_sctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001153
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001154 if (handle_closure_in_use(ectx, TRUE) == FAIL)
1155 return FAIL;
1156
Bram Moolenaar574950d2023-01-03 19:08:50 +00001157 // Clear the arguments. If this was an object method also clear the
1158 // object, it is just before the arguments.
1159 int top = ectx->ec_frame_idx - argcount;
Yegappan Lakshmanan1db15142023-09-19 20:34:05 +02001160 if (IS_OBJECT_METHOD(dfunc->df_ufunc))
Bram Moolenaar574950d2023-01-03 19:08:50 +00001161 --top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001162 for (idx = top; idx < ectx->ec_frame_idx; ++idx)
1163 clear_tv(STACK_TV(idx));
1164
1165 // Clear local variables and temp values, but not the return value.
1166 for (idx = ectx->ec_frame_idx + STACK_FRAME_SIZE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001167 idx < ectx->ec_stack.ga_len - 1; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001168 clear_tv(STACK_TV(idx));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001169
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01001170 // The return value should be on top of the stack. However, when aborting
1171 // it may not be there and ec_frame_idx is the top of the stack.
1172 ret_idx = ectx->ec_stack.ga_len - 1;
Bram Moolenaar0186e582021-01-10 18:33:11 +01001173 if (ret_idx == ectx->ec_frame_idx + STACK_FRAME_IDX_OFF)
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01001174 ret_idx = 0;
1175
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001176 if (ectx->ec_outer_ref != NULL)
1177 {
1178 if (ectx->ec_outer_ref->or_outer_allocated)
1179 vim_free(ectx->ec_outer_ref->or_outer);
1180 partial_unref(ectx->ec_outer_ref->or_partial);
1181 vim_free(ectx->ec_outer_ref);
1182 }
Bram Moolenaar0186e582021-01-10 18:33:11 +01001183
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001184 // Restore the previous frame.
Bram Moolenaar12d26532021-02-19 19:13:21 +01001185 ectx->ec_dfunc_idx = prev_dfunc_idx;
Bram Moolenaar0186e582021-01-10 18:33:11 +01001186 ectx->ec_iidx = STACK_TV(ectx->ec_frame_idx
1187 + STACK_FRAME_IIDX_OFF)->vval.v_number;
Bram Moolenaar5930ddc2021-04-26 20:32:59 +02001188 ectx->ec_instr = (void *)STACK_TV(ectx->ec_frame_idx
1189 + STACK_FRAME_INSTR_OFF)->vval.v_string;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001190 ectx->ec_outer_ref = (void *)STACK_TV(ectx->ec_frame_idx
Bram Moolenaar0186e582021-01-10 18:33:11 +01001191 + STACK_FRAME_OUTER_OFF)->vval.v_string;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001192 floc = (void *)STACK_TV(ectx->ec_frame_idx
1193 + STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string;
Bram Moolenaar5366e1a2020-10-01 13:01:34 +02001194 // restoring ec_frame_idx must be last
Bram Moolenaar0186e582021-01-10 18:33:11 +01001195 ectx->ec_frame_idx = STACK_TV(ectx->ec_frame_idx
1196 + STACK_FRAME_IDX_OFF)->vval.v_number;
Bram Moolenaard386e922021-04-25 14:48:49 +02001197
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001198 if (floc == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02001199 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001200 else
1201 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02001202 ectx->ec_funclocal = *floc;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001203 vim_free(floc);
1204 }
1205
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01001206 if (ret_idx > 0)
1207 {
1208 // Reset the stack to the position before the call, with a spot for the
1209 // return value, moved there from above the frame.
1210 ectx->ec_stack.ga_len = top + 1;
1211 *STACK_TV_BOT(-1) = *STACK_TV(ret_idx);
1212 }
1213 else
1214 // Reset the stack to the position before the call.
1215 ectx->ec_stack.ga_len = top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001216
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001217 funcdepth_decrement();
Bram Moolenaare99d4222021-06-13 14:01:26 +02001218 --ex_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001219 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001220}
1221
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001222/*
1223 * Prepare arguments and rettv for calling a builtin or user function.
1224 */
1225 static int
1226call_prepare(int argcount, typval_T *argvars, ectx_T *ectx)
1227{
1228 int idx;
1229 typval_T *tv;
1230
1231 // Move arguments from bottom of the stack to argvars[] and add terminator.
1232 for (idx = 0; idx < argcount; ++idx)
1233 argvars[idx] = *STACK_TV_BOT(idx - argcount);
1234 argvars[argcount].v_type = VAR_UNKNOWN;
1235
1236 // Result replaces the arguments on the stack.
1237 if (argcount > 0)
1238 ectx->ec_stack.ga_len -= argcount - 1;
Bram Moolenaar35578162021-08-02 19:10:38 +02001239 else if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001240 return FAIL;
1241 else
1242 ++ectx->ec_stack.ga_len;
1243
1244 // Default return value is zero.
1245 tv = STACK_TV_BOT(-1);
1246 tv->v_type = VAR_NUMBER;
1247 tv->vval.v_number = 0;
Bram Moolenaar24565cf2022-03-28 18:16:52 +01001248 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001249
1250 return OK;
1251}
1252
1253/*
1254 * Call a builtin function by index.
1255 */
1256 static int
1257call_bfunc(int func_idx, int argcount, ectx_T *ectx)
1258{
1259 typval_T argvars[MAX_FUNC_ARGS];
1260 int idx;
Bram Moolenaar171fb922020-10-28 16:54:47 +01001261 int did_emsg_before = did_emsg;
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001262 ectx_T *prev_ectx = current_ectx;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001263 char *save_func_name = ectx->ec_where.wt_func_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001264
1265 if (call_prepare(argcount, argvars, ectx) == FAIL)
1266 return FAIL;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001267 ectx->ec_where.wt_func_name = internal_func_name(func_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001268
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001269 // Call the builtin function. Set "current_ectx" so that when it
1270 // recursively invokes call_def_function() a closure context can be set.
1271 current_ectx = ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001272 call_internal_func_by_idx(func_idx, argvars, STACK_TV_BOT(-1));
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001273 current_ectx = prev_ectx;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001274 ectx->ec_where.wt_func_name = save_func_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001275
1276 // Clear the arguments.
1277 for (idx = 0; idx < argcount; ++idx)
1278 clear_tv(&argvars[idx]);
Bram Moolenaar015f4262020-05-05 21:25:22 +02001279
Bram Moolenaar57f799e2020-12-12 20:42:19 +01001280 if (did_emsg > did_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +02001281 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001282 return OK;
1283}
1284
1285/*
1286 * Execute a user defined function.
Bram Moolenaarab360522021-01-10 14:02:28 +01001287 * If the function is compiled this will add a stack frame and set the
1288 * instruction pointer at the start of the function.
1289 * Otherwise the function is called here.
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001290 * If "pt" is not null use "pt->pt_outer" for ec_outer_ref->or_outer.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001291 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001292 */
1293 static int
Bram Moolenaar0186e582021-01-10 18:33:11 +01001294call_ufunc(
1295 ufunc_T *ufunc,
1296 partial_T *pt,
1297 int argcount,
1298 ectx_T *ectx,
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001299 isn_T *iptr,
1300 dict_T *selfdict)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001301{
1302 typval_T argvars[MAX_FUNC_ARGS];
1303 funcexe_T funcexe;
Bram Moolenaar0917e862023-02-18 14:42:44 +00001304 funcerror_T error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001305 int idx;
Bram Moolenaar28ee8922020-10-28 20:20:00 +01001306 int did_emsg_before = did_emsg;
Bram Moolenaar139575d2022-03-15 19:29:30 +00001307 compiletype_T compile_type = get_compile_type(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001308
Bram Moolenaare99d4222021-06-13 14:01:26 +02001309 if (func_needs_compiling(ufunc, compile_type)
1310 && compile_def_function(ufunc, FALSE, compile_type, NULL)
1311 == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02001312 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001313 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001314 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +01001315 error = check_user_func_argcount(ufunc, argcount);
Bram Moolenaar50824712020-12-20 21:10:17 +01001316 if (error != FCERR_UNKNOWN)
1317 {
1318 if (error == FCERR_TOOMANY)
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001319 semsg(_(e_too_many_arguments_for_function_str),
1320 printable_func_name(ufunc));
Bram Moolenaar50824712020-12-20 21:10:17 +01001321 else
Bram Moolenaare1242042021-12-16 20:56:57 +00001322 semsg(_(e_not_enough_arguments_for_function_str),
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001323 printable_func_name(ufunc));
Bram Moolenaar50824712020-12-20 21:10:17 +01001324 return FAIL;
1325 }
1326
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001327 // The function has been compiled, can call it quickly. For a function
1328 // that was defined later: we can call it directly next time.
1329 if (iptr != NULL)
1330 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001331 delete_instr(iptr);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001332 iptr->isn_type = ISN_DCALL;
1333 iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1334 iptr->isn_arg.dfunc.cdf_argcount = argcount;
1335 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001336 return call_dfunc(ufunc->uf_dfunc_idx, pt, argcount, ectx);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001337 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001338
1339 if (call_prepare(argcount, argvars, ectx) == FAIL)
1340 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +02001341 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00001342 funcexe.fe_evaluate = TRUE;
1343 funcexe.fe_selfdict = selfdict != NULL ? selfdict : dict_stack_get_dict();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001344
1345 // Call the user function. Result goes in last position on the stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001346 error = call_user_func_check(ufunc, argcount, argvars,
Bram Moolenaar851f86b2021-12-13 14:26:44 +00001347 STACK_TV_BOT(-1), &funcexe, funcexe.fe_selfdict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001348
1349 // Clear the arguments.
1350 for (idx = 0; idx < argcount; ++idx)
1351 clear_tv(&argvars[idx]);
1352
1353 if (error != FCERR_NONE)
1354 {
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01001355 user_func_error(error, printable_func_name(ufunc),
1356 funcexe.fe_found_var);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001357 return FAIL;
1358 }
Bram Moolenaar28ee8922020-10-28 20:20:00 +01001359 if (did_emsg > did_emsg_before)
Bram Moolenaared677f52020-08-12 16:38:10 +02001360 // Error other than from calling the function itself.
1361 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001362 return OK;
1363}
1364
1365/*
Bram Moolenaara91a7132021-03-25 21:12:15 +01001366 * If command modifiers were applied restore them.
1367 */
1368 static void
1369may_restore_cmdmod(funclocal_T *funclocal)
1370{
1371 if (funclocal->floc_restore_cmdmod)
1372 {
1373 cmdmod.cmod_filter_regmatch.regprog = NULL;
1374 undo_cmdmod(&cmdmod);
1375 cmdmod = funclocal->floc_save_cmdmod;
1376 funclocal->floc_restore_cmdmod = FALSE;
1377 }
1378}
1379
1380/*
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001381 * Return TRUE if an error was given (not caught in try/catch) or CTRL-C was
1382 * pressed.
Bram Moolenaara1773442020-08-12 15:21:22 +02001383 */
1384 static int
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001385vim9_aborting(int prev_uncaught_emsg)
Bram Moolenaara1773442020-08-12 15:21:22 +02001386{
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001387 return uncaught_emsg > prev_uncaught_emsg || got_int || did_throw;
Bram Moolenaara1773442020-08-12 15:21:22 +02001388}
1389
1390/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001391 * Execute a function by "name".
1392 * This can be a builtin function or a user function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001393 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001394 * Returns FAIL if not found without an error message.
1395 */
1396 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001397call_by_name(
1398 char_u *name,
1399 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001400 ectx_T *ectx,
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001401 isn_T *iptr,
1402 dict_T *selfdict)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001403{
1404 ufunc_T *ufunc;
1405
1406 if (builtin_function(name, -1))
1407 {
1408 int func_idx = find_internal_func(name);
1409
Bram Moolenaar1983f1a2022-02-28 20:55:02 +00001410 if (func_idx < 0) // Impossible?
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001411 return FAIL;
Bram Moolenaar389df252020-07-09 21:20:47 +02001412 if (check_internal_func(func_idx, argcount) < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001413 return FAIL;
1414 return call_bfunc(func_idx, argcount, ectx);
1415 }
1416
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00001417 ufunc = find_func(name, FALSE);
Bram Moolenaara1773442020-08-12 15:21:22 +02001418
1419 if (ufunc == NULL)
1420 {
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001421 int prev_uncaught_emsg = uncaught_emsg;
Bram Moolenaara1773442020-08-12 15:21:22 +02001422
1423 if (script_autoload(name, TRUE))
1424 // loaded a package, search for the function again
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00001425 ufunc = find_func(name, FALSE);
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001426
1427 if (vim9_aborting(prev_uncaught_emsg))
Bram Moolenaara1773442020-08-12 15:21:22 +02001428 return FAIL; // bail out if loading the script caused an error
1429 }
1430
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001431 if (ufunc != NULL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001432 {
Bram Moolenaar0d89d8a2022-12-31 14:01:24 +00001433 if (check_ufunc_arg_types(ufunc, argcount, 0, ectx) == FAIL)
1434 return FAIL;
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001435
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001436 return call_ufunc(ufunc, NULL, argcount, ectx, iptr, selfdict);
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001437 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001438
1439 return FAIL;
1440}
1441
1442 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001443call_partial(
1444 typval_T *tv,
1445 int argcount_arg,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001446 ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001447{
Bram Moolenaara90afb92020-07-15 22:38:56 +02001448 int argcount = argcount_arg;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001449 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001450 int called_emsg_before = called_emsg;
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +01001451 int res = FAIL;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001452 dict_T *selfdict = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001453
1454 if (tv->v_type == VAR_PARTIAL)
1455 {
Bram Moolenaara90afb92020-07-15 22:38:56 +02001456 partial_T *pt = tv->vval.v_partial;
1457 int i;
1458
1459 if (pt->pt_argc > 0)
1460 {
1461 // Make space for arguments from the partial, shift the "argcount"
1462 // arguments up.
Bram Moolenaar35578162021-08-02 19:10:38 +02001463 if (GA_GROW_FAILS(&ectx->ec_stack, pt->pt_argc))
Bram Moolenaara90afb92020-07-15 22:38:56 +02001464 return FAIL;
1465 for (i = 1; i <= argcount; ++i)
1466 *STACK_TV_BOT(-i + pt->pt_argc) = *STACK_TV_BOT(-i);
1467 ectx->ec_stack.ga_len += pt->pt_argc;
1468 argcount += pt->pt_argc;
1469
1470 // copy the arguments from the partial onto the stack
1471 for (i = 0; i < pt->pt_argc; ++i)
1472 copy_tv(&pt->pt_argv[i], STACK_TV_BOT(-argcount + i));
1473 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001474 selfdict = pt->pt_dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001475
1476 if (pt->pt_func != NULL)
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001477 return call_ufunc(pt->pt_func, pt, argcount, ectx, NULL, selfdict);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001478
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001479 name = pt->pt_name;
1480 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001481 else if (tv->v_type == VAR_FUNC)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001482 name = tv->vval.v_string;
Bram Moolenaar95006e32020-08-29 17:47:08 +02001483 if (name != NULL)
1484 {
Bram Moolenaar0917e862023-02-18 14:42:44 +00001485 char_u fname_buf[FLEN_FIXED + 1];
1486 char_u *tofree = NULL;
1487 funcerror_T error = FCERR_NONE;
1488 char_u *fname;
Bram Moolenaar95006e32020-08-29 17:47:08 +02001489
1490 // May need to translate <SNR>123_ to K_SNR.
1491 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
1492 if (error != FCERR_NONE)
1493 res = FAIL;
1494 else
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001495 res = call_by_name(fname, argcount, ectx, NULL, selfdict);
Bram Moolenaar95006e32020-08-29 17:47:08 +02001496 vim_free(tofree);
1497 }
1498
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +01001499 if (res == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001500 {
1501 if (called_emsg == called_emsg_before)
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001502 emsg_funcname(e_unknown_function_str,
Bram Moolenaar015f4262020-05-05 21:25:22 +02001503 name == NULL ? (char_u *)"[unknown]" : name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001504 return FAIL;
1505 }
1506 return OK;
1507}
1508
1509/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001510 * Check if "lock" is VAR_LOCKED or VAR_FIXED. If so give an error and return
1511 * TRUE.
1512 */
1513 static int
1514error_if_locked(int lock, char *error)
1515{
1516 if (lock & (VAR_LOCKED | VAR_FIXED))
1517 {
1518 emsg(_(error));
1519 return TRUE;
1520 }
1521 return FALSE;
1522}
1523
1524/*
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001525 * Give an error if "tv" is not a number and return FAIL.
1526 */
1527 static int
1528check_for_number(typval_T *tv)
1529{
1530 if (tv->v_type != VAR_NUMBER)
1531 {
1532 semsg(_(e_expected_str_but_got_str),
1533 vartype_name(VAR_NUMBER), vartype_name(tv->v_type));
1534 return FAIL;
1535 }
1536 return OK;
1537}
1538
1539/*
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001540 * Store "tv" in variable "name".
1541 * This is for s: and g: variables.
1542 */
1543 static void
1544store_var(char_u *name, typval_T *tv)
1545{
1546 funccal_entry_T entry;
Bram Moolenaard877a572021-04-01 19:42:48 +02001547 int flags = ASSIGN_DECL;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001548
Bram Moolenaard877a572021-04-01 19:42:48 +02001549 if (tv->v_lock)
1550 flags |= ASSIGN_CONST;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001551 save_funccal(&entry);
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001552 set_var_const(name, 0, NULL, tv, FALSE, flags, 0);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001553 restore_funccal();
1554}
1555
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001556/*
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001557 * Convert "tv" to a string.
1558 * Return FAIL if not allowed.
1559 */
1560 static int
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001561do_2string(typval_T *tv, int is_2string_any, int tolerant)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001562{
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00001563 if (tv->v_type == VAR_STRING)
1564 return OK;
1565
1566 char_u *str;
1567
1568 if (is_2string_any)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001569 {
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00001570 switch (tv->v_type)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001571 {
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00001572 case VAR_SPECIAL:
1573 case VAR_BOOL:
1574 case VAR_NUMBER:
1575 case VAR_FLOAT:
1576 case VAR_BLOB: break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001577
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00001578 case VAR_LIST:
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001579 if (tolerant)
1580 {
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001581 char_u *s, *e, *p;
1582 garray_T ga;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001583
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001584 ga_init2(&ga, sizeof(char_u *), 1);
1585
1586 // Convert to NL separated items, then
1587 // escape the items and replace the NL with
1588 // a space.
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001589 str = typval2string(tv, TRUE);
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001590 if (str == NULL)
1591 return FAIL;
1592 s = str;
1593 while ((e = vim_strchr(s, '\n')) != NULL)
1594 {
1595 *e = NUL;
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +01001596 p = vim_strsave_fnameescape(s,
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00001597 VSE_NONE);
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001598 if (p != NULL)
1599 {
1600 ga_concat(&ga, p);
1601 ga_concat(&ga, (char_u *)" ");
1602 vim_free(p);
1603 }
1604 s = e + 1;
1605 }
1606 vim_free(str);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001607 clear_tv(tv);
1608 tv->v_type = VAR_STRING;
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001609 tv->vval.v_string = ga.ga_data;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001610 return OK;
1611 }
1612 // FALLTHROUGH
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00001613 default: to_string_error(tv->v_type);
1614 return FAIL;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001615 }
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001616 }
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00001617 str = typval_tostring(tv, TRUE);
1618 clear_tv(tv);
1619 tv->v_type = VAR_STRING;
1620 tv->vval.v_string = str;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001621 return OK;
1622}
1623
1624/*
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001625 * When the value of "sv" is a null list of dict, allocate it.
1626 */
1627 static void
Bram Moolenaar859cc212022-03-28 15:22:35 +01001628allocate_if_null(svar_T *sv)
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001629{
Bram Moolenaar859cc212022-03-28 15:22:35 +01001630 typval_T *tv = sv->sv_tv;
1631
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001632 switch (tv->v_type)
1633 {
1634 case VAR_LIST:
Bram Moolenaar859cc212022-03-28 15:22:35 +01001635 if (tv->vval.v_list == NULL && sv->sv_type != &t_list_empty)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001636 (void)rettv_list_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001637 break;
1638 case VAR_DICT:
Bram Moolenaar859cc212022-03-28 15:22:35 +01001639 if (tv->vval.v_dict == NULL && sv->sv_type != &t_dict_empty)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001640 (void)rettv_dict_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001641 break;
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02001642 case VAR_BLOB:
Bram Moolenaar859cc212022-03-28 15:22:35 +01001643 if (tv->vval.v_blob == NULL && sv->sv_type != &t_blob_null)
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02001644 (void)rettv_blob_alloc(tv);
1645 break;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001646 default:
1647 break;
1648 }
1649}
Bram Moolenaard3aac292020-04-19 14:32:17 +02001650
Bram Moolenaare7525c52021-01-09 13:20:37 +01001651/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001652 * Return the character "str[index]" where "index" is the character index,
1653 * including composing characters.
1654 * If "index" is out of range NULL is returned.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001655 */
1656 char_u *
1657char_from_string(char_u *str, varnumber_T index)
1658{
1659 size_t nbyte = 0;
1660 varnumber_T nchar = index;
1661 size_t slen;
1662
1663 if (str == NULL)
1664 return NULL;
1665 slen = STRLEN(str);
1666
Bram Moolenaarff871402021-03-26 13:34:05 +01001667 // Do the same as for a list: a negative index counts from the end.
1668 // Optimization to check the first byte to be below 0x80 (and no composing
1669 // character follows) makes this a lot faster.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001670 if (index < 0)
1671 {
1672 int clen = 0;
1673
1674 for (nbyte = 0; nbyte < slen; ++clen)
Bram Moolenaarff871402021-03-26 13:34:05 +01001675 {
1676 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1677 ++nbyte;
1678 else if (enc_utf8)
1679 nbyte += utfc_ptr2len(str + nbyte);
1680 else
1681 nbyte += mb_ptr2len(str + nbyte);
1682 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001683 nchar = clen + index;
1684 if (nchar < 0)
1685 // unlike list: index out of range results in empty string
1686 return NULL;
1687 }
1688
1689 for (nbyte = 0; nchar > 0 && nbyte < slen; --nchar)
Bram Moolenaarff871402021-03-26 13:34:05 +01001690 {
1691 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1692 ++nbyte;
1693 else if (enc_utf8)
1694 nbyte += utfc_ptr2len(str + nbyte);
1695 else
1696 nbyte += mb_ptr2len(str + nbyte);
1697 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001698 if (nbyte >= slen)
1699 return NULL;
Bram Moolenaar0289a092021-03-14 18:40:19 +01001700 return vim_strnsave(str + nbyte, mb_ptr2len(str + nbyte));
Bram Moolenaare7525c52021-01-09 13:20:37 +01001701}
1702
1703/*
1704 * Get the byte index for character index "idx" in string "str" with length
Bram Moolenaar0289a092021-03-14 18:40:19 +01001705 * "str_len". Composing characters are included.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001706 * If going over the end return "str_len".
1707 * If "idx" is negative count from the end, -1 is the last character.
1708 * When going over the start return -1.
1709 */
1710 static long
1711char_idx2byte(char_u *str, size_t str_len, varnumber_T idx)
1712{
1713 varnumber_T nchar = idx;
1714 size_t nbyte = 0;
1715
1716 if (nchar >= 0)
1717 {
1718 while (nchar > 0 && nbyte < str_len)
1719 {
Bram Moolenaar0289a092021-03-14 18:40:19 +01001720 nbyte += mb_ptr2len(str + nbyte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001721 --nchar;
1722 }
1723 }
1724 else
1725 {
1726 nbyte = str_len;
1727 while (nchar < 0 && nbyte > 0)
1728 {
1729 --nbyte;
1730 nbyte -= mb_head_off(str, str + nbyte);
1731 ++nchar;
1732 }
1733 if (nchar < 0)
1734 return -1;
1735 }
1736 return (long)nbyte;
1737}
1738
1739/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001740 * Return the slice "str[first : last]" using character indexes. Composing
1741 * characters are included.
Bram Moolenaar6601b622021-01-13 21:47:15 +01001742 * "exclusive" is TRUE for slice().
Bram Moolenaare7525c52021-01-09 13:20:37 +01001743 * Return NULL when the result is empty.
1744 */
1745 char_u *
Bram Moolenaar6601b622021-01-13 21:47:15 +01001746string_slice(char_u *str, varnumber_T first, varnumber_T last, int exclusive)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001747{
1748 long start_byte, end_byte;
1749 size_t slen;
1750
1751 if (str == NULL)
1752 return NULL;
1753 slen = STRLEN(str);
1754 start_byte = char_idx2byte(str, slen, first);
1755 if (start_byte < 0)
1756 start_byte = 0; // first index very negative: use zero
Bram Moolenaar6601b622021-01-13 21:47:15 +01001757 if ((last == -1 && !exclusive) || last == VARNUM_MAX)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001758 end_byte = (long)slen;
1759 else
1760 {
1761 end_byte = char_idx2byte(str, slen, last);
Bram Moolenaar6601b622021-01-13 21:47:15 +01001762 if (!exclusive && end_byte >= 0 && end_byte < (long)slen)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001763 // end index is inclusive
Bram Moolenaar0289a092021-03-14 18:40:19 +01001764 end_byte += mb_ptr2len(str + end_byte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001765 }
1766
1767 if (start_byte >= (long)slen || end_byte <= start_byte)
1768 return NULL;
1769 return vim_strnsave(str + start_byte, end_byte - start_byte);
1770}
1771
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001772/*
1773 * Get a script variable for ISN_STORESCRIPT and ISN_LOADSCRIPT.
1774 * When "dfunc_idx" is negative don't give an error.
1775 * Returns NULL for an error.
1776 */
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001777 static svar_T *
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001778get_script_svar(scriptref_T *sref, int dfunc_idx)
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001779{
1780 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001781 dfunc_T *dfunc = dfunc_idx < 0 ? NULL
1782 : ((dfunc_T *)def_functions.ga_data) + dfunc_idx;
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001783 svar_T *sv;
1784
1785 if (sref->sref_seq != si->sn_script_seq)
1786 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001787 // The script was reloaded after the function was compiled, the
1788 // script_idx may not be valid.
1789 if (dfunc != NULL)
1790 semsg(_(e_script_variable_invalid_after_reload_in_function_str),
1791 printable_func_name(dfunc->df_ufunc));
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001792 return NULL;
1793 }
1794 sv = ((svar_T *)si->sn_var_vals.ga_data) + sref->sref_idx;
Bram Moolenaar6de22962022-09-09 21:35:36 +01001795 if (sv->sv_name == NULL)
1796 {
1797 if (dfunc != NULL)
1798 emsg(_(e_script_variable_was_deleted));
1799 return NULL;
1800 }
Bram Moolenaar60dc8272021-07-29 22:48:54 +02001801 if (!equal_type(sv->sv_type, sref->sref_type, 0))
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001802 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001803 if (dfunc != NULL)
1804 emsg(_(e_script_variable_type_changed));
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001805 return NULL;
1806 }
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001807
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01001808 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0
1809 && sref->sref_sid != current_sctx.sc_sid)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001810 {
1811 if (dfunc != NULL)
1812 semsg(_(e_item_not_exported_in_script_str), sv->sv_name);
1813 return NULL;
1814 }
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001815 return sv;
1816}
1817
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001818/*
Bram Moolenaar20677332021-06-06 17:02:53 +02001819 * Function passed to do_cmdline() for splitting a script joined by NL
1820 * characters.
1821 */
1822 static char_u *
1823get_split_sourceline(
1824 int c UNUSED,
1825 void *cookie,
1826 int indent UNUSED,
1827 getline_opt_T options UNUSED)
1828{
1829 source_cookie_T *sp = (source_cookie_T *)cookie;
1830 char_u *p;
1831 char_u *line;
1832
Bram Moolenaar20677332021-06-06 17:02:53 +02001833 p = vim_strchr(sp->nextline, '\n');
1834 if (p == NULL)
1835 {
1836 line = vim_strsave(sp->nextline);
1837 sp->nextline += STRLEN(sp->nextline);
1838 }
1839 else
1840 {
1841 line = vim_strnsave(sp->nextline, p - sp->nextline);
1842 sp->nextline = p + 1;
1843 }
1844 return line;
1845}
1846
1847/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001848 * Execute a function by "name".
1849 * This can be a builtin function, user function or a funcref.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001850 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001851 */
1852 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001853call_eval_func(
1854 char_u *name,
1855 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001856 ectx_T *ectx,
1857 isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001858{
Bram Moolenaared677f52020-08-12 16:38:10 +02001859 int called_emsg_before = called_emsg;
1860 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001861
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001862 res = call_by_name(name, argcount, ectx, iptr, NULL);
Bram Moolenaared677f52020-08-12 16:38:10 +02001863 if (res == FAIL && called_emsg == called_emsg_before)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001864 {
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001865 dictitem_T *v;
1866
1867 v = find_var(name, NULL, FALSE);
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001868 if (v == NULL || (v->di_tv.v_type != VAR_PARTIAL
1869 && v->di_tv.v_type != VAR_FUNC))
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001870 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001871 emsg_funcname(e_unknown_function_str, name);
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001872 return FAIL;
1873 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001874 return call_partial(&v->di_tv, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001875 }
Bram Moolenaared677f52020-08-12 16:38:10 +02001876 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001877}
1878
1879/*
Bram Moolenaarf112f302020-12-20 17:47:52 +01001880 * When a function reference is used, fill a partial with the information
1881 * needed, especially when it is used as a closure.
1882 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001883 int
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001884fill_partial_and_closure(
Bram Moolenaarcc341812022-09-19 15:54:34 +01001885 partial_T *pt,
1886 ufunc_T *ufunc,
1887 loopvarinfo_T *lvi,
1888 ectx_T *ectx)
Bram Moolenaarf112f302020-12-20 17:47:52 +01001889{
1890 pt->pt_func = ufunc;
1891 pt->pt_refcount = 1;
1892
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001893 if (ufunc->uf_flags & FC_CLOSURE)
Bram Moolenaarf112f302020-12-20 17:47:52 +01001894 {
1895 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1896 + ectx->ec_dfunc_idx;
1897
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001898 // The closure may need to find arguments and local variables of the
1899 // current function in the stack.
Bram Moolenaar0186e582021-01-10 18:33:11 +01001900 pt->pt_outer.out_stack = &ectx->ec_stack;
1901 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001902 if (ectx->ec_outer_ref != NULL)
1903 {
1904 // The current context already has a context, link to that one.
1905 pt->pt_outer.out_up = ectx->ec_outer_ref->or_outer;
1906 if (ectx->ec_outer_ref->or_partial != NULL)
1907 {
1908 pt->pt_outer.out_up_partial = ectx->ec_outer_ref->or_partial;
1909 ++pt->pt_outer.out_up_partial->pt_refcount;
1910 }
1911 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01001912
Bram Moolenaarcc341812022-09-19 15:54:34 +01001913 if (lvi != NULL)
1914 {
1915 int depth;
1916
1917 // The closure may need to find variables defined inside a loop,
1918 // for every nested loop. A new reference is made every time,
1919 // ISN_ENDLOOP will check if they are actually used.
1920 for (depth = 0; depth < lvi->lvi_depth; ++depth)
1921 {
1922 pt->pt_outer.out_loop[depth].stack = &ectx->ec_stack;
1923 pt->pt_outer.out_loop[depth].var_idx = ectx->ec_frame_idx
1924 + STACK_FRAME_SIZE + lvi->lvi_loop[depth].var_idx;
1925 pt->pt_outer.out_loop[depth].var_count =
1926 lvi->lvi_loop[depth].var_count;
1927 }
Bram Moolenaar6d313be2022-09-22 16:36:25 +01001928 pt->pt_outer.out_loop_size = lvi->lvi_depth;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001929 }
Bram Moolenaar6d313be2022-09-22 16:36:25 +01001930 else
1931 pt->pt_outer.out_loop_size = 0;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001932
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001933 // If the function currently executing returns and the closure is still
1934 // being referenced, we need to make a copy of the context (arguments
1935 // and local variables) so that the closure can use it later.
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001936 // Store a reference to the partial so we can handle that.
Bram Moolenaar35578162021-08-02 19:10:38 +02001937 if (GA_GROW_FAILS(&ectx->ec_funcrefs, 1))
Bram Moolenaarf112f302020-12-20 17:47:52 +01001938 {
1939 vim_free(pt);
1940 return FAIL;
1941 }
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001942 // Extra variable keeps the count of closures created in the current
1943 // function call.
Bram Moolenaarf112f302020-12-20 17:47:52 +01001944 ++(((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx
1945 + STACK_FRAME_SIZE + dfunc->df_varcount)->vval.v_number;
1946
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001947 ((partial_T **)ectx->ec_funcrefs.ga_data)[ectx->ec_funcrefs.ga_len]
1948 = pt;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001949 ++pt->pt_refcount;
1950 ++ectx->ec_funcrefs.ga_len;
1951 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001952 ++ufunc->uf_refcount;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001953 return OK;
1954}
1955
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001956/*
1957 * Execute iptr->isn_arg.string as an Ex command.
1958 */
1959 static int
Ernie Raelee865f32023-09-29 19:53:55 +02001960exec_command(isn_T *iptr, char_u *cmd_string)
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001961{
1962 source_cookie_T cookie;
1963
1964 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00001965 // Pass getsourceline to get an error for a missing ":end" command.
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001966 CLEAR_FIELD(cookie);
1967 cookie.sourcing_lnum = iptr->isn_lnum - 1;
Ernie Raelee865f32023-09-29 19:53:55 +02001968 if (do_cmdline(cmd_string, getsourceline, &cookie,
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001969 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED) == FAIL
1970 || did_emsg)
1971 return FAIL;
1972 return OK;
1973}
1974
Bram Moolenaar89445512022-04-14 12:58:23 +01001975/*
1976 * If script "sid" is not loaded yet then load it now.
1977 * Caller must make sure "sid" is a valid script ID.
1978 * "loaded" is set to TRUE if the script had to be loaded.
1979 * Returns FAIL if loading fails, OK if already loaded or loaded now.
1980 */
1981 int
1982may_load_script(int sid, int *loaded)
1983{
1984 scriptitem_T *si = SCRIPT_ITEM(sid);
1985
1986 if (si->sn_state == SN_STATE_NOT_LOADED)
1987 {
1988 *loaded = TRUE;
1989 if (do_source(si->sn_name, FALSE, DOSO_NONE, NULL) == FAIL)
1990 {
1991 semsg(_(e_cant_open_file_str), si->sn_name);
1992 return FAIL;
1993 }
1994 }
1995 return OK;
1996}
1997
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001998// used for v_instr of typval of VAR_INSTR
1999struct instr_S {
2000 ectx_T *instr_ectx;
2001 isn_T *instr_instr;
2002};
2003
Bram Moolenaar4c137212021-04-19 16:48:48 +02002004// used for substitute_instr
2005typedef struct subs_expr_S {
2006 ectx_T *subs_ectx;
2007 isn_T *subs_instr;
2008 int subs_status;
2009} subs_expr_T;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002010
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002011// Set when calling do_debug().
2012static ectx_T *debug_context = NULL;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02002013static int debug_var_count;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002014
2015/*
2016 * When debugging lookup "name" and return the typeval.
2017 * When not found return NULL.
2018 */
2019 typval_T *
2020lookup_debug_var(char_u *name)
2021{
2022 int idx;
2023 dfunc_T *dfunc;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02002024 ufunc_T *ufunc;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002025 ectx_T *ectx = debug_context;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02002026 int varargs_off;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002027
2028 if (ectx == NULL)
2029 return NULL;
2030 dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
2031
2032 // Go through the local variable names, from last to first.
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02002033 for (idx = debug_var_count - 1; idx >= 0; --idx)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002034 {
Bram Moolenaare406ff82022-03-10 20:47:43 +00002035 char_u *varname = ((char_u **)dfunc->df_var_names.ga_data)[idx];
2036
2037 // the variable name may be NULL when not available in this block
2038 if (varname != NULL && STRCMP(varname, name) == 0)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002039 return STACK_TV_VAR(idx);
2040 }
2041
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02002042 // Go through argument names.
2043 ufunc = dfunc->df_ufunc;
2044 varargs_off = ufunc->uf_va_name == NULL ? 0 : 1;
2045 for (idx = 0; idx < ufunc->uf_args.ga_len; ++idx)
2046 if (STRCMP(((char_u **)(ufunc->uf_args.ga_data))[idx], name) == 0)
2047 return STACK_TV(ectx->ec_frame_idx - ufunc->uf_args.ga_len
2048 - varargs_off + idx);
2049 if (ufunc->uf_va_name != NULL && STRCMP(ufunc->uf_va_name, name) == 0)
2050 return STACK_TV(ectx->ec_frame_idx - 1);
2051
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002052 return NULL;
2053}
2054
Bram Moolenaar26a44842021-09-02 18:49:06 +02002055/*
2056 * Return TRUE if there might be a breakpoint in "ufunc", which is when a
2057 * breakpoint was set in that function or when there is any expression.
2058 */
2059 int
2060may_break_in_function(ufunc_T *ufunc)
2061{
2062 return ufunc->uf_has_breakpoint || debug_has_expr_breakpoint();
2063}
2064
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002065 static void
2066handle_debug(isn_T *iptr, ectx_T *ectx)
2067{
2068 char_u *line;
2069 ufunc_T *ufunc = (((dfunc_T *)def_functions.ga_data)
2070 + ectx->ec_dfunc_idx)->df_ufunc;
2071 isn_T *ni;
2072 int end_lnum = iptr->isn_lnum;
2073 garray_T ga;
2074 int lnum;
2075
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02002076 if (ex_nesting_level > debug_break_level)
2077 {
2078 linenr_T breakpoint;
2079
Bram Moolenaar26a44842021-09-02 18:49:06 +02002080 if (!may_break_in_function(ufunc))
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02002081 return;
2082
2083 // check for the next breakpoint if needed
2084 breakpoint = dbg_find_breakpoint(FALSE, ufunc->uf_name,
Bram Moolenaar8cec9272021-06-23 20:20:53 +02002085 iptr->isn_arg.debug.dbg_break_lnum);
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02002086 if (breakpoint <= 0 || breakpoint > iptr->isn_lnum)
2087 return;
2088 }
2089
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002090 SOURCING_LNUM = iptr->isn_lnum;
2091 debug_context = ectx;
Bram Moolenaar8cec9272021-06-23 20:20:53 +02002092 debug_var_count = iptr->isn_arg.debug.dbg_var_names_len;
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002093
2094 for (ni = iptr + 1; ni->isn_type != ISN_FINISH; ++ni)
2095 if (ni->isn_type == ISN_DEBUG
2096 || ni->isn_type == ISN_RETURN
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002097 || ni->isn_type == ISN_RETURN_OBJECT
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002098 || ni->isn_type == ISN_RETURN_VOID)
2099 {
Bram Moolenaar112bed02021-11-23 22:16:34 +00002100 end_lnum = ni->isn_lnum + (ni->isn_type == ISN_DEBUG ? 0 : 1);
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002101 break;
2102 }
2103
2104 if (end_lnum > iptr->isn_lnum)
2105 {
2106 ga_init2(&ga, sizeof(char_u *), 10);
Bram Moolenaar310091d2021-12-23 21:14:37 +00002107 for (lnum = iptr->isn_lnum; lnum < end_lnum
2108 && lnum <= ufunc->uf_lines.ga_len; ++lnum)
Bram Moolenaar59b50c32021-06-17 22:27:48 +02002109 {
Bram Moolenaar303215d2021-07-07 20:10:43 +02002110 char_u *p = ((char_u **)ufunc->uf_lines.ga_data)[lnum - 1];
Bram Moolenaar59b50c32021-06-17 22:27:48 +02002111
Bram Moolenaar303215d2021-07-07 20:10:43 +02002112 if (p == NULL)
2113 continue; // left over from continuation line
2114 p = skipwhite(p);
Bram Moolenaar59b50c32021-06-17 22:27:48 +02002115 if (*p == '#')
2116 break;
Bram Moolenaar35578162021-08-02 19:10:38 +02002117 if (GA_GROW_OK(&ga, 1))
Bram Moolenaar59b50c32021-06-17 22:27:48 +02002118 ((char_u **)(ga.ga_data))[ga.ga_len++] = p;
2119 if (STRNCMP(p, "def ", 4) == 0)
2120 break;
2121 }
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002122 line = ga_concat_strings(&ga, " ");
2123 vim_free(ga.ga_data);
2124 }
2125 else
2126 line = ((char_u **)ufunc->uf_lines.ga_data)[iptr->isn_lnum - 1];
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002127
Dominique Pelle6e969552021-06-17 13:53:41 +02002128 do_debug(line == NULL ? (char_u *)"[empty]" : line);
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002129 debug_context = NULL;
2130
2131 if (end_lnum > iptr->isn_lnum)
2132 vim_free(line);
2133}
2134
Bram Moolenaar4c137212021-04-19 16:48:48 +02002135/*
Bram Moolenaar65b0d162022-12-13 18:43:22 +00002136 * Store a value in a list, dict, blob or object variable.
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002137 * Returns OK, FAIL or NOTDONE (uncatchable error).
2138 */
2139 static int
2140execute_storeindex(isn_T *iptr, ectx_T *ectx)
2141{
Bram Moolenaarf7d1c6e2023-01-16 20:47:57 +00002142 vartype_T dest_type = iptr->isn_arg.storeindex.si_vartype;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002143 typval_T *tv;
2144 typval_T *tv_idx = STACK_TV_BOT(-2);
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002145 long lidx = 0;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002146 typval_T *tv_dest = STACK_TV_BOT(-1);
2147 int status = OK;
2148
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002149 if (tv_idx->v_type == VAR_NUMBER)
2150 lidx = (long)tv_idx->vval.v_number;
2151
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002152 // Stack contains:
2153 // -3 value to be stored
2154 // -2 index
Yegappan Lakshmanan3775f772023-09-01 22:05:45 +02002155 // -1 dict, list, blob, object or class
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002156 tv = STACK_TV_BOT(-3);
2157 SOURCING_LNUM = iptr->isn_lnum;
Gianmaria Bajod7085a02023-08-31 18:15:26 +02002158
2159 // Make sure an object has been initialized
2160 if (dest_type == VAR_OBJECT && tv_dest->vval.v_object == NULL)
2161 {
2162 emsg(_(e_using_null_object));
2163 status = FAIL;
2164 }
2165 else if (dest_type == VAR_ANY)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002166 {
2167 dest_type = tv_dest->v_type;
2168 if (dest_type == VAR_DICT)
2169 status = do_2string(tv_idx, TRUE, FALSE);
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002170 else if (dest_type == VAR_OBJECT && tv_idx->v_type == VAR_STRING)
2171 {
2172 // Need to get the member index now that the class is known.
2173 object_T *obj = tv_dest->vval.v_object;
2174 class_T *cl = obj->obj_class;
2175 char_u *member = tv_idx->vval.v_string;
2176
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02002177 int m_idx;
2178 ocmember_T *m = object_member_lookup(cl, member, 0, &m_idx);
2179 if (m != NULL)
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002180 {
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02002181 if (*member == '_')
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002182 {
RestorerZ7fe8f432023-09-24 23:21:24 +02002183 semsg(_(e_cannot_access_private_variable_str), m->ocm_name);
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02002184 status = FAIL;
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002185 }
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002186
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02002187 lidx = m_idx;
2188 }
2189 else
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002190 {
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +02002191 member_not_found_msg(cl, VAR_OBJECT, member, 0);
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002192 status = FAIL;
2193 }
2194 }
2195 else if ((dest_type == VAR_LIST || dest_type == VAR_OBJECT)
2196 && tv_idx->v_type != VAR_NUMBER)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002197 {
2198 emsg(_(e_number_expected));
2199 status = FAIL;
2200 }
2201 }
Bram Moolenaare08be092022-02-17 13:08:26 +00002202
2203 if (status == OK)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002204 {
Bram Moolenaare08be092022-02-17 13:08:26 +00002205 if (dest_type == VAR_LIST)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002206 {
Bram Moolenaare08be092022-02-17 13:08:26 +00002207 list_T *list = tv_dest->vval.v_list;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002208
Bram Moolenaare08be092022-02-17 13:08:26 +00002209 if (list == NULL)
2210 {
2211 emsg(_(e_list_not_set));
2212 return FAIL;
2213 }
2214 if (lidx < 0 && list->lv_len + lidx >= 0)
2215 // negative index is relative to the end
2216 lidx = list->lv_len + lidx;
2217 if (lidx < 0 || lidx > list->lv_len)
2218 {
2219 semsg(_(e_list_index_out_of_range_nr), lidx);
2220 return FAIL;
2221 }
2222 if (lidx < list->lv_len)
2223 {
2224 listitem_T *li = list_find(list, lidx);
2225
2226 if (error_if_locked(li->li_tv.v_lock,
Bram Moolenaar70c43d82022-01-26 21:01:15 +00002227 e_cannot_change_locked_list_item))
Bram Moolenaare08be092022-02-17 13:08:26 +00002228 return FAIL;
2229 // overwrite existing list item
2230 clear_tv(&li->li_tv);
2231 li->li_tv = *tv;
2232 }
2233 else
2234 {
2235 if (error_if_locked(list->lv_lock, e_cannot_change_locked_list))
2236 return FAIL;
2237 // append to list, only fails when out of memory
2238 if (list_append_tv(list, tv) == FAIL)
2239 return NOTDONE;
2240 clear_tv(tv);
2241 }
2242 }
2243 else if (dest_type == VAR_DICT)
2244 {
2245 char_u *key = tv_idx->vval.v_string;
2246 dict_T *dict = tv_dest->vval.v_dict;
2247 dictitem_T *di;
2248
2249 SOURCING_LNUM = iptr->isn_lnum;
2250 if (dict == NULL)
2251 {
2252 emsg(_(e_dictionary_not_set));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002253 return FAIL;
Bram Moolenaare08be092022-02-17 13:08:26 +00002254 }
2255 if (key == NULL)
2256 key = (char_u *)"";
2257 di = dict_find(dict, key, -1);
2258 if (di != NULL)
2259 {
2260 if (error_if_locked(di->di_tv.v_lock,
2261 e_cannot_change_dict_item))
2262 return FAIL;
2263 // overwrite existing value
2264 clear_tv(&di->di_tv);
2265 di->di_tv = *tv;
2266 }
2267 else
2268 {
2269 if (error_if_locked(dict->dv_lock, e_cannot_change_dict))
2270 return FAIL;
2271 // add to dict, only fails when out of memory
2272 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
2273 return NOTDONE;
2274 clear_tv(tv);
2275 }
2276 }
2277 else if (dest_type == VAR_BLOB)
2278 {
Bram Moolenaare08be092022-02-17 13:08:26 +00002279 blob_T *blob = tv_dest->vval.v_blob;
Bram Moolenaar65b0d162022-12-13 18:43:22 +00002280 varnumber_T nr;
2281 int error = FALSE;
2282 int len;
Bram Moolenaare08be092022-02-17 13:08:26 +00002283
2284 if (blob == NULL)
2285 {
2286 emsg(_(e_blob_not_set));
2287 return FAIL;
2288 }
2289 len = blob_len(blob);
2290 if (lidx < 0 && len + lidx >= 0)
2291 // negative index is relative to the end
2292 lidx = len + lidx;
2293
2294 // Can add one byte at the end.
2295 if (lidx < 0 || lidx > len)
2296 {
2297 semsg(_(e_blob_index_out_of_range_nr), lidx);
2298 return FAIL;
2299 }
2300 if (value_check_lock(blob->bv_lock, (char_u *)"blob", FALSE))
2301 return FAIL;
2302 nr = tv_get_number_chk(tv, &error);
2303 if (error)
2304 return FAIL;
2305 blob_set_append(blob, lidx, nr);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002306 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002307 else if (dest_type == VAR_CLASS || dest_type == VAR_OBJECT)
2308 {
Yegappan Lakshmanan3775f772023-09-01 22:05:45 +02002309 typval_T *otv;
Bram Moolenaarf7d1c6e2023-01-16 20:47:57 +00002310
Yegappan Lakshmanan3775f772023-09-01 22:05:45 +02002311 if (dest_type == VAR_OBJECT)
2312 {
2313 object_T *obj = tv_dest->vval.v_object;
2314
2315 otv = (typval_T *)(obj + 1);
2316 class_T *itf = iptr->isn_arg.storeindex.si_class;
2317 if (itf != NULL)
2318 // convert interface member index to class member index
Ernie Rael18143d32023-09-04 22:30:41 +02002319 lidx = object_index_from_itf_index(itf, FALSE, lidx,
Yegappan Lakshmanan5a05d372023-09-29 19:43:11 +02002320 obj->obj_class);
Yegappan Lakshmanan3775f772023-09-01 22:05:45 +02002321 }
2322 else
2323 {
2324 // VAR_CLASS
2325 class_T *class = tv_dest->vval.v_class;
2326 otv = class->class_members_tv;
2327 }
Bram Moolenaarf7d1c6e2023-01-16 20:47:57 +00002328
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002329 clear_tv(&otv[lidx]);
2330 otv[lidx] = *tv;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002331 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002332 else
2333 {
Bram Moolenaare08be092022-02-17 13:08:26 +00002334 status = FAIL;
2335 semsg(_(e_cannot_index_str), vartype_name(dest_type));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002336 }
2337 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002338
2339 clear_tv(tv_idx);
2340 clear_tv(tv_dest);
2341 ectx->ec_stack.ga_len -= 3;
2342 if (status == FAIL)
2343 {
2344 clear_tv(tv);
2345 return FAIL;
2346 }
2347 return OK;
2348}
2349
2350/*
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002351 * Store a value in a list or blob range.
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002352 */
2353 static int
2354execute_storerange(isn_T *iptr, ectx_T *ectx)
2355{
2356 typval_T *tv;
2357 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2358 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2359 typval_T *tv_dest = STACK_TV_BOT(-1);
2360 int status = OK;
2361
2362 // Stack contains:
2363 // -4 value to be stored
2364 // -3 first index or "none"
2365 // -2 second index or "none"
2366 // -1 destination list or blob
2367 tv = STACK_TV_BOT(-4);
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002368 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002369 if (tv_dest->v_type == VAR_LIST)
2370 {
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002371 long n1;
2372 long n2;
2373 listitem_T *li1;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002374
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002375 n1 = (long)tv_get_number_chk(tv_idx1, NULL);
2376 if (tv_idx2->v_type == VAR_SPECIAL
2377 && tv_idx2->vval.v_number == VVAL_NONE)
2378 n2 = list_len(tv_dest->vval.v_list) - 1;
2379 else
2380 n2 = (long)tv_get_number_chk(tv_idx2, NULL);
2381
Bram Moolenaar22ebd172022-04-01 15:26:58 +01002382 li1 = check_range_index_one(tv_dest->vval.v_list, &n1, TRUE, FALSE);
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002383 if (li1 == NULL)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002384 status = FAIL;
2385 else
2386 {
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002387 status = check_range_index_two(tv_dest->vval.v_list,
2388 &n1, li1, &n2, FALSE);
2389 if (status != FAIL)
2390 status = list_assign_range(
2391 tv_dest->vval.v_list,
2392 tv->vval.v_list,
2393 n1,
2394 n2,
2395 tv_idx2->v_type == VAR_SPECIAL,
2396 (char_u *)"=",
2397 (char_u *)"[unknown]");
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002398 }
2399 }
2400 else if (tv_dest->v_type == VAR_BLOB)
2401 {
2402 varnumber_T n1;
2403 varnumber_T n2;
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002404 long bloblen;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002405
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002406 n1 = tv_get_number_chk(tv_idx1, NULL);
2407 if (tv_idx2->v_type == VAR_SPECIAL
2408 && tv_idx2->vval.v_number == VVAL_NONE)
2409 n2 = blob_len(tv_dest->vval.v_blob) - 1;
2410 else
2411 n2 = tv_get_number_chk(tv_idx2, NULL);
2412 bloblen = blob_len(tv_dest->vval.v_blob);
2413
2414 if (check_blob_index(bloblen, n1, FALSE) == FAIL
2415 || check_blob_range(bloblen, n1, n2, FALSE) == FAIL)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002416 status = FAIL;
2417 else
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002418 status = blob_set_range(tv_dest->vval.v_blob, n1, n2, tv);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002419 }
2420 else
2421 {
2422 status = FAIL;
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002423 emsg(_(e_list_or_blob_required));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002424 }
2425
2426 clear_tv(tv_idx1);
2427 clear_tv(tv_idx2);
2428 clear_tv(tv_dest);
2429 ectx->ec_stack.ga_len -= 4;
2430 clear_tv(tv);
2431
2432 return status;
2433}
2434
2435/*
2436 * Unlet item in list or dict variable.
2437 */
2438 static int
2439execute_unletindex(isn_T *iptr, ectx_T *ectx)
2440{
2441 typval_T *tv_idx = STACK_TV_BOT(-2);
2442 typval_T *tv_dest = STACK_TV_BOT(-1);
2443 int status = OK;
2444
2445 // Stack contains:
2446 // -2 index
2447 // -1 dict or list
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002448 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002449 if (tv_dest->v_type == VAR_DICT)
2450 {
2451 // unlet a dict item, index must be a string
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002452 if (tv_idx->v_type != VAR_STRING && tv_idx->v_type != VAR_NUMBER)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002453 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002454 semsg(_(e_expected_str_but_got_str),
2455 vartype_name(VAR_STRING),
2456 vartype_name(tv_idx->v_type));
2457 status = FAIL;
2458 }
2459 else
2460 {
2461 dict_T *d = tv_dest->vval.v_dict;
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002462 char_u *key;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002463 dictitem_T *di = NULL;
2464
2465 if (d != NULL && value_check_lock(
2466 d->dv_lock, NULL, FALSE))
2467 status = FAIL;
2468 else
2469 {
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002470 if (tv_idx->v_type == VAR_STRING)
2471 {
2472 key = tv_idx->vval.v_string;
2473 if (key == NULL)
2474 key = (char_u *)"";
2475 }
2476 else
2477 {
2478 key = tv_get_string(tv_idx);
2479 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002480 if (d != NULL)
2481 di = dict_find(d, key, (int)STRLEN(key));
2482 if (di == NULL)
2483 {
2484 // NULL dict is equivalent to empty dict
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00002485 semsg(_(e_key_not_present_in_dictionary_str), key);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002486 status = FAIL;
2487 }
2488 else if (var_check_fixed(di->di_flags,
2489 NULL, FALSE)
2490 || var_check_ro(di->di_flags,
2491 NULL, FALSE))
2492 status = FAIL;
2493 else
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002494 dictitem_remove(d, di, "unlet");
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002495 }
2496 }
2497 }
2498 else if (tv_dest->v_type == VAR_LIST)
2499 {
2500 // unlet a List item, index must be a number
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002501 if (check_for_number(tv_idx) == FAIL)
2502 {
2503 status = FAIL;
2504 }
2505 else
2506 {
2507 list_T *l = tv_dest->vval.v_list;
2508 long n = (long)tv_idx->vval.v_number;
2509
2510 if (l != NULL && value_check_lock(
2511 l->lv_lock, NULL, FALSE))
2512 status = FAIL;
2513 else
2514 {
2515 listitem_T *li = list_find(l, n);
2516
2517 if (li == NULL)
2518 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002519 semsg(_(e_list_index_out_of_range_nr), n);
2520 status = FAIL;
2521 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002522 else
2523 listitem_remove(l, li);
2524 }
2525 }
2526 }
2527 else
2528 {
2529 status = FAIL;
2530 semsg(_(e_cannot_index_str),
2531 vartype_name(tv_dest->v_type));
2532 }
2533
2534 clear_tv(tv_idx);
2535 clear_tv(tv_dest);
2536 ectx->ec_stack.ga_len -= 2;
2537
2538 return status;
2539}
2540
2541/*
2542 * Unlet a range of items in a list variable.
2543 */
2544 static int
2545execute_unletrange(isn_T *iptr, ectx_T *ectx)
2546{
2547 // Stack contains:
2548 // -3 index1
2549 // -2 index2
2550 // -1 dict or list
2551 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2552 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2553 typval_T *tv_dest = STACK_TV_BOT(-1);
2554 int status = OK;
2555
2556 if (tv_dest->v_type == VAR_LIST)
2557 {
2558 // indexes must be a number
2559 SOURCING_LNUM = iptr->isn_lnum;
2560 if (check_for_number(tv_idx1) == FAIL
2561 || (tv_idx2->v_type != VAR_SPECIAL
2562 && check_for_number(tv_idx2) == FAIL))
2563 {
2564 status = FAIL;
2565 }
2566 else
2567 {
2568 list_T *l = tv_dest->vval.v_list;
2569 long n1 = (long)tv_idx1->vval.v_number;
2570 long n2 = tv_idx2->v_type == VAR_SPECIAL
2571 ? 0 : (long)tv_idx2->vval.v_number;
2572 listitem_T *li;
2573
2574 li = list_find_index(l, &n1);
2575 if (li == NULL)
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002576 {
2577 semsg(_(e_list_index_out_of_range_nr),
2578 (long)tv_idx1->vval.v_number);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002579 status = FAIL;
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002580 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002581 else
2582 {
2583 if (n1 < 0)
2584 n1 = list_idx_of_item(l, li);
2585 if (n2 < 0)
2586 {
2587 listitem_T *li2 = list_find(l, n2);
2588
2589 if (li2 == NULL)
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002590 {
2591 semsg(_(e_list_index_out_of_range_nr), n2);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002592 status = FAIL;
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002593 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002594 else
2595 n2 = list_idx_of_item(l, li2);
2596 }
2597 if (status != FAIL
2598 && tv_idx2->v_type != VAR_SPECIAL
2599 && n2 < n1)
2600 {
2601 semsg(_(e_list_index_out_of_range_nr), n2);
2602 status = FAIL;
2603 }
Bram Moolenaar6b8c7ba2022-03-20 17:46:06 +00002604 if (status != FAIL)
2605 list_unlet_range(l, li, n1,
2606 tv_idx2->v_type != VAR_SPECIAL, n2);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002607 }
2608 }
2609 }
2610 else
2611 {
2612 status = FAIL;
2613 SOURCING_LNUM = iptr->isn_lnum;
2614 semsg(_(e_cannot_index_str),
2615 vartype_name(tv_dest->v_type));
2616 }
2617
2618 clear_tv(tv_idx1);
2619 clear_tv(tv_idx2);
2620 clear_tv(tv_dest);
2621 ectx->ec_stack.ga_len -= 3;
2622
2623 return status;
2624}
2625
2626/*
2627 * Top of a for loop.
2628 */
2629 static int
2630execute_for(isn_T *iptr, ectx_T *ectx)
2631{
2632 typval_T *tv;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002633 int jump = FALSE;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002634 typval_T *ltv = STACK_TV_BOT(-1);
2635 typval_T *idxtv =
Bram Moolenaarcc341812022-09-19 15:54:34 +01002636 STACK_TV_VAR(iptr->isn_arg.forloop.for_loop_idx);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002637
2638 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
2639 return FAIL;
2640 if (ltv->v_type == VAR_LIST)
2641 {
2642 list_T *list = ltv->vval.v_list;
2643
2644 // push the next item from the list
2645 ++idxtv->vval.v_number;
2646 if (list == NULL
2647 || idxtv->vval.v_number >= list->lv_len)
2648 {
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002649 jump = TRUE;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002650 }
2651 else if (list->lv_first == &range_list_item)
2652 {
2653 // non-materialized range() list
2654 tv = STACK_TV_BOT(0);
2655 tv->v_type = VAR_NUMBER;
2656 tv->v_lock = 0;
2657 tv->vval.v_number = list_find_nr(
2658 list, idxtv->vval.v_number, NULL);
2659 ++ectx->ec_stack.ga_len;
2660 }
2661 else
2662 {
2663 listitem_T *li = list_find(list,
2664 idxtv->vval.v_number);
2665
2666 copy_tv(&li->li_tv, STACK_TV_BOT(0));
2667 ++ectx->ec_stack.ga_len;
2668 }
2669 }
2670 else if (ltv->v_type == VAR_STRING)
2671 {
2672 char_u *str = ltv->vval.v_string;
2673
2674 // The index is for the last byte of the previous
2675 // character.
2676 ++idxtv->vval.v_number;
2677 if (str == NULL || str[idxtv->vval.v_number] == NUL)
2678 {
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002679 jump = TRUE;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002680 }
2681 else
2682 {
2683 int clen = mb_ptr2len(str + idxtv->vval.v_number);
2684
2685 // Push the next character from the string.
2686 tv = STACK_TV_BOT(0);
2687 tv->v_type = VAR_STRING;
2688 tv->vval.v_string = vim_strnsave(
2689 str + idxtv->vval.v_number, clen);
2690 ++ectx->ec_stack.ga_len;
2691 idxtv->vval.v_number += clen - 1;
2692 }
2693 }
2694 else if (ltv->v_type == VAR_BLOB)
2695 {
2696 blob_T *blob = ltv->vval.v_blob;
2697
2698 // When we get here the first time make a copy of the
2699 // blob, so that the iteration still works when it is
2700 // changed.
2701 if (idxtv->vval.v_number == -1 && blob != NULL)
2702 {
2703 blob_copy(blob, ltv);
2704 blob_unref(blob);
2705 blob = ltv->vval.v_blob;
2706 }
2707
2708 // The index is for the previous byte.
2709 ++idxtv->vval.v_number;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002710 if (blob == NULL || idxtv->vval.v_number >= blob_len(blob))
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002711 {
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002712 jump = TRUE;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002713 }
2714 else
2715 {
2716 // Push the next byte from the blob.
2717 tv = STACK_TV_BOT(0);
2718 tv->v_type = VAR_NUMBER;
2719 tv->vval.v_number = blob_get(blob,
2720 idxtv->vval.v_number);
2721 ++ectx->ec_stack.ga_len;
2722 }
2723 }
2724 else
2725 {
2726 semsg(_(e_for_loop_on_str_not_supported),
2727 vartype_name(ltv->v_type));
2728 return FAIL;
2729 }
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002730
2731 if (jump)
2732 {
2733 // past the end of the list/string/blob, jump to "endfor"
2734 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
2735 may_restore_cmdmod(&ectx->ec_funclocal);
2736 }
2737 else
2738 {
2739 // Store the current number of funcrefs, this may be used in
2740 // ISN_LOOPEND. The variable index is always one more than the loop
2741 // variable index.
Bram Moolenaarcc341812022-09-19 15:54:34 +01002742 tv = STACK_TV_VAR(iptr->isn_arg.forloop.for_loop_idx + 1);
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002743 tv->vval.v_number = ectx->ec_funcrefs.ga_len;
2744 }
2745
2746 return OK;
2747}
2748
2749/*
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002750 * Code for handling variables declared inside a loop and used in a closure.
2751 * This is very similar to what is done with funcstack_T. The difference is
2752 * that the funcstack_T has the scope of a function, while a loopvars_T has the
2753 * scope of the block inside a loop and each loop may have its own.
2754 */
2755
2756// Double linked list of loopvars_T in use.
2757static loopvars_T *first_loopvars = NULL;
2758
2759 static void
2760add_loopvars_to_list(loopvars_T *loopvars)
2761{
2762 // Link in list of loopvarss.
2763 if (first_loopvars != NULL)
2764 first_loopvars->lvs_prev = loopvars;
2765 loopvars->lvs_next = first_loopvars;
2766 loopvars->lvs_prev = NULL;
2767 first_loopvars = loopvars;
2768}
2769
2770 static void
2771remove_loopvars_from_list(loopvars_T *loopvars)
2772{
2773 if (loopvars->lvs_prev == NULL)
2774 first_loopvars = loopvars->lvs_next;
2775 else
2776 loopvars->lvs_prev->lvs_next = loopvars->lvs_next;
2777 if (loopvars->lvs_next != NULL)
2778 loopvars->lvs_next->lvs_prev = loopvars->lvs_prev;
2779}
2780
2781/*
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002782 * End of a for or while loop: Handle any variables used by a closure.
2783 */
2784 static int
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002785execute_endloop(isn_T *iptr, ectx_T *ectx)
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002786{
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002787 endloop_T *endloop = &iptr->isn_arg.endloop;
2788 typval_T *tv_refcount = STACK_TV_VAR(endloop->end_funcref_idx);
2789 int prev_closure_count = tv_refcount->vval.v_number;
Bram Moolenaarcc341812022-09-19 15:54:34 +01002790 int depth = endloop->end_depth;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002791 garray_T *gap = &ectx->ec_funcrefs;
2792 int closure_in_use = FALSE;
2793 loopvars_T *loopvars;
2794 typval_T *stack;
2795 int idx;
2796
Bram Moolenaarcc341812022-09-19 15:54:34 +01002797 // Check if any created closure is still being referenced and loopvars have
2798 // not been saved yet for the current depth.
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002799 for (idx = prev_closure_count; idx < gap->ga_len; ++idx)
2800 {
2801 partial_T *pt = ((partial_T **)gap->ga_data)[idx];
2802
Bram Moolenaarcc341812022-09-19 15:54:34 +01002803 if (pt->pt_refcount > 1 && pt->pt_loopvars[depth] == NULL)
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002804 {
2805 int refcount = pt->pt_refcount;
2806 int i;
2807
2808 // A Reference in a variable inside the loop doesn't count, it gets
2809 // unreferenced at the end of the loop.
2810 for (i = 0; i < endloop->end_var_count; ++i)
2811 {
2812 typval_T *stv = STACK_TV_VAR(endloop->end_var_idx + i);
2813
2814 if (stv->v_type == VAR_PARTIAL && pt == stv->vval.v_partial)
2815 --refcount;
2816 }
2817 if (refcount > 1)
2818 {
2819 closure_in_use = TRUE;
2820 break;
2821 }
2822 }
2823 }
2824
2825 // If no function reference were created since the start of the loop block
2826 // or it is no longer referenced there is nothing to do.
2827 if (!closure_in_use)
2828 return OK;
2829
2830 // A closure is using variables declared inside the loop.
2831 // Move them to the called function.
2832 loopvars = ALLOC_CLEAR_ONE(loopvars_T);
2833 if (loopvars == NULL)
2834 return FAIL;
2835
2836 loopvars->lvs_ga.ga_len = endloop->end_var_count;
2837 stack = ALLOC_CLEAR_MULT(typval_T, loopvars->lvs_ga.ga_len);
2838 loopvars->lvs_ga.ga_data = stack;
2839 if (stack == NULL)
2840 {
2841 vim_free(loopvars);
2842 return FAIL;
2843 }
2844 add_loopvars_to_list(loopvars);
2845
2846 // Move the variable values.
2847 for (idx = 0; idx < endloop->end_var_count; ++idx)
2848 {
2849 typval_T *tv = STACK_TV_VAR(endloop->end_var_idx + idx);
2850
2851 *(stack + idx) = *tv;
2852 tv->v_type = VAR_UNKNOWN;
2853 }
2854
2855 for (idx = prev_closure_count; idx < gap->ga_len; ++idx)
2856 {
2857 partial_T *pt = ((partial_T **)gap->ga_data)[idx];
2858
Bram Moolenaarcc341812022-09-19 15:54:34 +01002859 if (pt->pt_refcount > 1 && pt->pt_loopvars[depth] == NULL)
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002860 {
2861 ++loopvars->lvs_refcount;
Bram Moolenaarcc341812022-09-19 15:54:34 +01002862 pt->pt_loopvars[depth] = loopvars;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002863
Bram Moolenaarcc341812022-09-19 15:54:34 +01002864 pt->pt_outer.out_loop[depth].stack = &loopvars->lvs_ga;
2865 pt->pt_outer.out_loop[depth].var_idx -=
2866 ectx->ec_frame_idx + STACK_FRAME_SIZE + endloop->end_var_idx;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002867 }
2868 }
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002869
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002870 return OK;
2871}
2872
2873/*
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002874 * Called when a partial is freed or its reference count goes down to one. The
2875 * loopvars may be the only reference to the partials in the local variables.
2876 * Go over all of them, the funcref and can be freed if all partials
2877 * referencing the loopvars have a reference count of one.
Bram Moolenaarcc341812022-09-19 15:54:34 +01002878 * Return TRUE if it was freed.
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002879 */
Bram Moolenaarcc341812022-09-19 15:54:34 +01002880 int
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002881loopvars_check_refcount(loopvars_T *loopvars)
2882{
2883 int i;
2884 garray_T *gap = &loopvars->lvs_ga;
2885 int done = 0;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002886 typval_T *stack = gap->ga_data;
2887
Bram Moolenaarcc341812022-09-19 15:54:34 +01002888 if (loopvars->lvs_refcount > loopvars->lvs_min_refcount)
2889 return FALSE;
2890 for (i = 0; i < gap->ga_len; ++i)
2891 {
2892 typval_T *tv = ((typval_T *)gap->ga_data) + i;
2893
2894 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL
2895 && tv->vval.v_partial->pt_refcount == 1)
2896 {
2897 int depth;
2898
2899 for (depth = 0; depth < MAX_LOOP_DEPTH; ++depth)
2900 if (tv->vval.v_partial->pt_loopvars[depth] == loopvars)
2901 ++done;
2902 }
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002903 }
Bram Moolenaarcc341812022-09-19 15:54:34 +01002904 if (done != loopvars->lvs_min_refcount)
2905 return FALSE;
2906
2907 // All partials referencing the loopvars have a reference count of
2908 // one, thus the loopvars is no longer of use.
2909 stack = gap->ga_data;
2910 for (i = 0; i < gap->ga_len; ++i)
2911 clear_tv(stack + i);
2912 vim_free(stack);
2913 remove_loopvars_from_list(loopvars);
2914 vim_free(loopvars);
2915 return TRUE;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002916}
2917
2918/*
2919 * For garbage collecting: set references in all variables referenced by
2920 * all loopvars.
2921 */
2922 int
2923set_ref_in_loopvars(int copyID)
2924{
2925 loopvars_T *loopvars;
2926
2927 for (loopvars = first_loopvars; loopvars != NULL;
2928 loopvars = loopvars->lvs_next)
2929 {
2930 typval_T *stack = loopvars->lvs_ga.ga_data;
2931 int i;
2932
2933 for (i = 0; i < loopvars->lvs_ga.ga_len; ++i)
2934 if (set_ref_in_item(stack + i, copyID, NULL, NULL))
2935 return TRUE; // abort
2936 }
2937 return FALSE;
2938}
2939
2940/*
Bram Moolenaar06b77222022-01-25 15:51:56 +00002941 * Load instruction for w:/b:/g:/t: variable.
2942 * "isn_type" is used instead of "iptr->isn_type".
2943 */
2944 static int
2945load_namespace_var(ectx_T *ectx, isntype_T isn_type, isn_T *iptr)
2946{
2947 dictitem_T *di = NULL;
2948 hashtab_T *ht = NULL;
2949 char namespace;
2950
2951 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
2952 return NOTDONE;
2953 switch (isn_type)
2954 {
2955 case ISN_LOADG:
2956 ht = get_globvar_ht();
2957 namespace = 'g';
2958 break;
2959 case ISN_LOADB:
2960 ht = &curbuf->b_vars->dv_hashtab;
2961 namespace = 'b';
2962 break;
2963 case ISN_LOADW:
2964 ht = &curwin->w_vars->dv_hashtab;
2965 namespace = 'w';
2966 break;
2967 case ISN_LOADT:
2968 ht = &curtab->tp_vars->dv_hashtab;
2969 namespace = 't';
2970 break;
2971 default: // Cannot reach here
2972 return NOTDONE;
2973 }
2974 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
2975
Bram Moolenaar06b77222022-01-25 15:51:56 +00002976 if (di == NULL)
2977 {
Bram Moolenaarfe732552022-02-22 19:39:13 +00002978 if (isn_type == ISN_LOADG)
2979 {
2980 ufunc_T *ufunc = find_func(iptr->isn_arg.string, TRUE);
2981
2982 // g:Something could be a function
2983 if (ufunc != NULL)
2984 {
2985 typval_T *tv = STACK_TV_BOT(0);
2986
2987 ++ectx->ec_stack.ga_len;
2988 tv->v_type = VAR_FUNC;
2989 tv->vval.v_string = alloc(STRLEN(iptr->isn_arg.string) + 3);
2990 if (tv->vval.v_string == NULL)
2991 return FAIL;
2992 STRCPY(tv->vval.v_string, "g:");
2993 STRCPY(tv->vval.v_string + 2, iptr->isn_arg.string);
2994 return OK;
2995 }
2996 }
Bram Moolenaar06b77222022-01-25 15:51:56 +00002997 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarfe732552022-02-22 19:39:13 +00002998 if (vim_strchr(iptr->isn_arg.string, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar06b77222022-01-25 15:51:56 +00002999 // no check if the item exists in the script but
3000 // isn't exported, it is too complicated
Bram Moolenaarfe732552022-02-22 19:39:13 +00003001 semsg(_(e_item_not_found_in_script_str), iptr->isn_arg.string);
Bram Moolenaar06b77222022-01-25 15:51:56 +00003002 else
3003 semsg(_(e_undefined_variable_char_str),
Bram Moolenaarfe732552022-02-22 19:39:13 +00003004 namespace, iptr->isn_arg.string);
Bram Moolenaar06b77222022-01-25 15:51:56 +00003005 return FAIL;
3006 }
3007 else
3008 {
3009 copy_tv(&di->di_tv, STACK_TV_BOT(0));
3010 ++ectx->ec_stack.ga_len;
3011 }
3012 return OK;
3013}
3014
Bram Moolenaard0200c82023-01-28 15:19:40 +00003015
3016 static void
3017object_required_error(typval_T *tv)
3018{
3019 garray_T type_list;
3020 ga_init2(&type_list, sizeof(type_T *), 10);
3021 type_T *type = typval2type(tv, get_copyID(), &type_list, TVTT_DO_MEMBER);
3022 char *tofree = NULL;
3023 char *typename = type_name(type, &tofree);
3024 semsg(_(e_object_required_found_str), typename);
3025 vim_free(tofree);
3026 clear_type_list(&type_list);
3027}
3028
Bram Moolenaar06b77222022-01-25 15:51:56 +00003029/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02003030 * Execute instructions in execution context "ectx".
3031 * Return OK or FAIL;
3032 */
3033 static int
3034exec_instructions(ectx_T *ectx)
3035{
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003036 int ret = FAIL;
3037 int save_trylevel_at_start = ectx->ec_trylevel_at_start;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02003038 int dict_stack_len_at_start = dict_stack.ga_len;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003039
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02003040 // Start execution at the first instruction.
Bram Moolenaar4c137212021-04-19 16:48:48 +02003041 ectx->ec_iidx = 0;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01003042
Bram Moolenaarff652882021-05-16 15:24:49 +02003043 // Only catch exceptions in this instruction list.
3044 ectx->ec_trylevel_at_start = trylevel;
3045
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003046 for (;;)
3047 {
Bram Moolenaara7490192021-07-22 12:26:14 +02003048 static int breakcheck_count = 0; // using "static" makes it faster
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003049 isn_T *iptr;
Bram Moolenaara7490192021-07-22 12:26:14 +02003050 typval_T *tv;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003051
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003052 if (unlikely(++breakcheck_count >= 100))
Bram Moolenaar270d0382020-05-15 21:42:53 +02003053 {
3054 line_breakcheck();
3055 breakcheck_count = 0;
3056 }
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003057 if (unlikely(got_int))
Bram Moolenaar20431c92020-03-20 18:39:46 +01003058 {
3059 // Turn CTRL-C into an exception.
3060 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +01003061 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003062 goto theend;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003063 did_throw = TRUE;
3064 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003065
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003066 if (unlikely(did_emsg && msg_list != NULL && *msg_list != NULL))
Bram Moolenaara26b9702020-04-18 19:53:28 +02003067 {
3068 // Turn an error message into an exception.
3069 did_emsg = FALSE;
3070 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003071 goto theend;
Bram Moolenaara26b9702020-04-18 19:53:28 +02003072 did_throw = TRUE;
3073 *msg_list = NULL;
Bram Moolenaar3ef2e412023-04-30 18:50:48 +01003074
3075 // This exception was not caught (yet).
3076 garray_T *trystack = &ectx->ec_trystack;
3077 if (trystack->ga_len > 0)
3078 {
3079 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
3080 + trystack->ga_len - 1;
3081 if (trycmd->tcd_frame_idx == ectx->ec_frame_idx)
3082 trycmd->tcd_caught = FALSE;
3083 }
Bram Moolenaara26b9702020-04-18 19:53:28 +02003084 }
3085
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003086 if (unlikely(did_throw))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003087 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003088 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003089 trycmd_T *trycmd = NULL;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003090 int index = trystack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003091
3092 // An exception jumps to the first catch, finally, or returns from
3093 // the current function.
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003094 while (index > 0)
3095 {
3096 trycmd = ((trycmd_T *)trystack->ga_data) + index - 1;
Bram Moolenaar3ef2e412023-04-30 18:50:48 +01003097 // 1. after :try and before :catch - jump to first :catch
3098 // 2. in :catch block - jump to :finally
3099 // 3. in :catch block and no finally - jump to :endtry
3100 if (!trycmd->tcd_in_catch || trycmd->tcd_finally_idx != 0
3101 || trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003102 break;
3103 // In the catch and finally block of this try we have to go up
3104 // one level.
3105 --index;
3106 trycmd = NULL;
3107 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003108 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003109 {
Bram Moolenaar834193a2021-06-30 20:39:15 +02003110 if (trycmd->tcd_in_catch)
3111 {
Bram Moolenaar3ef2e412023-04-30 18:50:48 +01003112 if (trycmd->tcd_finally_idx > 0)
3113 {
3114 // exception inside ":catch", jump to ":finally" once
3115 ectx->ec_iidx = trycmd->tcd_finally_idx;
3116 trycmd->tcd_finally_idx = 0;
3117 }
3118 else
3119 {
3120 // exception inside ":catch" or ":finally", jump to
3121 // ":endtry"
3122 ectx->ec_iidx = trycmd->tcd_endtry_idx;
3123 }
Bram Moolenaar834193a2021-06-30 20:39:15 +02003124 }
3125 else
Bram Moolenaar3ef2e412023-04-30 18:50:48 +01003126 {
Bram Moolenaar834193a2021-06-30 20:39:15 +02003127 // jump to first ":catch"
3128 ectx->ec_iidx = trycmd->tcd_catch_idx;
Bram Moolenaar3ef2e412023-04-30 18:50:48 +01003129 trycmd->tcd_in_catch = TRUE;
3130 }
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003131 did_throw = FALSE; // don't come back here until :endtry
3132 trycmd->tcd_did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003133 }
3134 else
3135 {
Bram Moolenaar3ef2e412023-04-30 18:50:48 +01003136 // Not inside try or need to return from current function.
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02003137 // Push a dummy return value.
Bram Moolenaar35578162021-08-02 19:10:38 +02003138 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003139 goto theend;
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02003140 tv = STACK_TV_BOT(0);
3141 tv->v_type = VAR_NUMBER;
3142 tv->vval.v_number = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003143 ++ectx->ec_stack.ga_len;
3144 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003145 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02003146 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +01003147 need_rethrow = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003148 if (handle_closure_in_use(ectx, FALSE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003149 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003150 goto done;
3151 }
3152
Bram Moolenaar4c137212021-04-19 16:48:48 +02003153 if (func_return(ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003154 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003155 }
3156 continue;
3157 }
3158
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003159 /*
3160 * Big switch on the instruction. Most compilers will be turning this
3161 * into an efficient lookup table, since the "case" values are an enum
3162 * with sequential numbers. It may look ugly, but it should be the
3163 * most efficient way.
3164 */
Bram Moolenaar4c137212021-04-19 16:48:48 +02003165 iptr = &ectx->ec_instr[ectx->ec_iidx++];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003166 switch (iptr->isn_type)
3167 {
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00003168 // Constructor, first instruction in a new() method.
Bram Moolenaar00b28d62022-12-08 15:32:33 +00003169 case ISN_CONSTRUCT:
3170 // "this" is always the local variable at index zero
3171 tv = STACK_TV_VAR(0);
3172 tv->v_type = VAR_OBJECT;
3173 tv->vval.v_object = alloc_clear(
3174 iptr->isn_arg.construct.construct_size);
3175 tv->vval.v_object->obj_class =
3176 iptr->isn_arg.construct.construct_class;
Bram Moolenaard28d7b92022-12-08 20:42:00 +00003177 ++tv->vval.v_object->obj_class->class_refcount;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00003178 tv->vval.v_object->obj_refcount = 1;
Bram Moolenaard28d7b92022-12-08 20:42:00 +00003179 object_created(tv->vval.v_object);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00003180 break;
3181
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003182 // execute Ex command line
3183 case ISN_EXEC:
Ernie Raelee865f32023-09-29 19:53:55 +02003184 if (exec_command(iptr, iptr->isn_arg.string) == FAIL)
Bram Moolenaaraacc9662021-08-13 19:40:51 +02003185 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003186 break;
3187
Bram Moolenaar20677332021-06-06 17:02:53 +02003188 // execute Ex command line split at NL characters.
3189 case ISN_EXEC_SPLIT:
3190 {
3191 source_cookie_T cookie;
Bram Moolenaar518df272021-06-06 17:34:13 +02003192 char_u *line;
Bram Moolenaar20677332021-06-06 17:02:53 +02003193
3194 SOURCING_LNUM = iptr->isn_lnum;
3195 CLEAR_FIELD(cookie);
3196 cookie.sourcing_lnum = iptr->isn_lnum - 1;
3197 cookie.nextline = iptr->isn_arg.string;
Bram Moolenaar518df272021-06-06 17:34:13 +02003198 line = get_split_sourceline(0, &cookie, 0, 0);
3199 if (do_cmdline(line,
Bram Moolenaar20677332021-06-06 17:02:53 +02003200 get_split_sourceline, &cookie,
3201 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED)
3202 == FAIL
3203 || did_emsg)
Bram Moolenaar518df272021-06-06 17:34:13 +02003204 {
3205 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02003206 goto on_error;
Bram Moolenaar518df272021-06-06 17:34:13 +02003207 }
3208 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02003209 }
3210 break;
3211
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00003212 // execute Ex command line that is only a range
3213 case ISN_EXECRANGE:
3214 {
3215 exarg_T ea;
3216 char *error = NULL;
3217
3218 CLEAR_FIELD(ea);
3219 ea.cmdidx = CMD_SIZE;
3220 ea.addr_type = ADDR_LINES;
3221 ea.cmd = iptr->isn_arg.string;
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00003222 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00003223 parse_cmd_address(&ea, &error, FALSE);
Bram Moolenaar01a4dcb2021-12-04 13:15:10 +00003224 if (ea.cmd == NULL)
3225 goto on_error;
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00003226 // error is always NULL when using ADDR_LINES
3227 error = ex_range_without_command(&ea);
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00003228 if (error != NULL)
3229 {
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00003230 emsg(error);
3231 goto on_error;
3232 }
3233 }
3234 break;
3235
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02003236 // Evaluate an expression with legacy syntax, push it onto the
3237 // stack.
3238 case ISN_LEGACY_EVAL:
3239 {
3240 char_u *arg = iptr->isn_arg.string;
3241 int res;
3242 int save_flags = cmdmod.cmod_flags;
3243
Bram Moolenaar35578162021-08-02 19:10:38 +02003244 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003245 goto theend;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02003246 tv = STACK_TV_BOT(0);
3247 init_tv(tv);
3248 cmdmod.cmod_flags |= CMOD_LEGACY;
3249 res = eval0(arg, tv, NULL, &EVALARG_EVALUATE);
3250 cmdmod.cmod_flags = save_flags;
3251 if (res == FAIL)
3252 goto on_error;
3253 ++ectx->ec_stack.ga_len;
3254 }
3255 break;
3256
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003257 // push typeval VAR_INSTR with instructions to be executed
3258 case ISN_INSTR:
3259 {
Bram Moolenaar35578162021-08-02 19:10:38 +02003260 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003261 goto theend;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003262 tv = STACK_TV_BOT(0);
3263 tv->vval.v_instr = ALLOC_ONE(instr_T);
3264 if (tv->vval.v_instr == NULL)
3265 goto on_error;
3266 ++ectx->ec_stack.ga_len;
3267
3268 tv->v_type = VAR_INSTR;
3269 tv->vval.v_instr->instr_ectx = ectx;
3270 tv->vval.v_instr->instr_instr = iptr->isn_arg.instr;
3271 }
3272 break;
3273
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003274 case ISN_SOURCE:
3275 {
Bram Moolenaar89445512022-04-14 12:58:23 +01003276 int notused;
LemonBoy77142312022-04-15 20:50:46 +01003277
Bram Moolenaar89445512022-04-14 12:58:23 +01003278 SOURCING_LNUM = iptr->isn_lnum;
3279 if (may_load_script((int)iptr->isn_arg.number, &notused)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003280 == FAIL)
Bram Moolenaar89445512022-04-14 12:58:23 +01003281 goto on_error;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003282 }
3283 break;
3284
Bram Moolenaar4c137212021-04-19 16:48:48 +02003285 // execute :substitute with an expression
3286 case ISN_SUBSTITUTE:
3287 {
3288 subs_T *subs = &iptr->isn_arg.subs;
3289 source_cookie_T cookie;
3290 struct subs_expr_S *save_instr = substitute_instr;
3291 struct subs_expr_S subs_instr;
3292 int res;
3293
3294 subs_instr.subs_ectx = ectx;
3295 subs_instr.subs_instr = subs->subs_instr;
3296 subs_instr.subs_status = OK;
3297 substitute_instr = &subs_instr;
3298
3299 SOURCING_LNUM = iptr->isn_lnum;
3300 // This is very much like ISN_EXEC
3301 CLEAR_FIELD(cookie);
3302 cookie.sourcing_lnum = iptr->isn_lnum - 1;
3303 res = do_cmdline(subs->subs_cmd,
3304 getsourceline, &cookie,
3305 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
3306 substitute_instr = save_instr;
3307
3308 if (res == FAIL || did_emsg
3309 || subs_instr.subs_status == FAIL)
3310 goto on_error;
3311 }
3312 break;
3313
3314 case ISN_FINISH:
3315 goto done;
3316
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02003317 case ISN_REDIRSTART:
3318 // create a dummy entry for var_redir_str()
3319 if (alloc_redir_lval() == FAIL)
3320 goto on_error;
3321
3322 // The output is stored in growarray "redir_ga" until
3323 // redirection ends.
3324 init_redir_ga();
3325 redir_vname = 1;
3326 break;
3327
3328 case ISN_REDIREND:
3329 {
3330 char_u *res = get_clear_redir_ga();
3331
3332 // End redirection, put redirected text on the stack.
3333 clear_redir_lval();
3334 redir_vname = 0;
3335
Bram Moolenaar35578162021-08-02 19:10:38 +02003336 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02003337 {
3338 vim_free(res);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003339 goto theend;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02003340 }
3341 tv = STACK_TV_BOT(0);
3342 tv->v_type = VAR_STRING;
3343 tv->vval.v_string = res;
3344 ++ectx->ec_stack.ga_len;
3345 }
3346 break;
3347
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003348 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02003349#ifdef FEAT_QUICKFIX
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003350 force_abort = TRUE;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003351 if (trigger_cexpr_autocmd(iptr->isn_arg.number) == FAIL)
3352 goto on_error;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003353 force_abort = FALSE;
Bram Moolenaarb7c97812021-05-05 22:51:39 +02003354#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003355 break;
3356
3357 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02003358#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003359 {
3360 exarg_T ea;
3361 int res;
3362
3363 CLEAR_FIELD(ea);
3364 ea.cmdidx = iptr->isn_arg.cexpr.cexpr_ref->cer_cmdidx;
3365 ea.forceit = iptr->isn_arg.cexpr.cexpr_ref->cer_forceit;
3366 ea.cmdlinep = &iptr->isn_arg.cexpr.cexpr_ref->cer_cmdline;
3367 --ectx->ec_stack.ga_len;
3368 tv = STACK_TV_BOT(0);
Bram Moolenaarbd683e32022-07-18 17:49:03 +01003369 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003370 res = cexpr_core(&ea, tv);
3371 clear_tv(tv);
3372 if (res == FAIL)
3373 goto on_error;
3374 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02003375#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003376 break;
3377
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003378 // execute Ex command from pieces on the stack
3379 case ISN_EXECCONCAT:
3380 {
3381 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +02003382 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003383 int pass;
3384 int i;
3385 char_u *cmd = NULL;
3386 char_u *str;
3387
3388 for (pass = 1; pass <= 2; ++pass)
3389 {
3390 for (i = 0; i < count; ++i)
3391 {
3392 tv = STACK_TV_BOT(i - count);
3393 str = tv->vval.v_string;
3394 if (str != NULL && *str != NUL)
3395 {
3396 if (pass == 2)
3397 STRCPY(cmd + len, str);
3398 len += STRLEN(str);
3399 }
3400 if (pass == 2)
3401 clear_tv(tv);
3402 }
3403 if (pass == 1)
3404 {
3405 cmd = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003406 if (unlikely(cmd == NULL))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003407 goto theend;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003408 len = 0;
3409 }
3410 }
3411
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02003412 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003413 do_cmdline_cmd(cmd);
3414 vim_free(cmd);
3415 }
3416 break;
3417
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003418 // execute :echo {string} ...
3419 case ISN_ECHO:
3420 {
3421 int count = iptr->isn_arg.echo.echo_count;
3422 int atstart = TRUE;
3423 int needclr = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003424 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003425
3426 for (idx = 0; idx < count; ++idx)
3427 {
3428 tv = STACK_TV_BOT(idx - count);
3429 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
3430 &atstart, &needclr);
3431 clear_tv(tv);
3432 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01003433 if (needclr)
3434 msg_clr_eos();
Bram Moolenaar4c137212021-04-19 16:48:48 +02003435 ectx->ec_stack.ga_len -= count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003436 }
3437 break;
3438
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003439 // :execute {string} ...
3440 // :echomsg {string} ...
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01003441 // :echowindow {string} ...
Bram Moolenaar7de62622021-08-07 15:05:47 +02003442 // :echoconsole {string} ...
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003443 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01003444 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003445 case ISN_ECHOMSG:
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01003446 case ISN_ECHOWINDOW:
Bram Moolenaar7de62622021-08-07 15:05:47 +02003447 case ISN_ECHOCONSOLE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003448 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01003449 {
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01003450 int count;
Bram Moolenaarad39c092020-02-26 18:23:43 +01003451 garray_T ga;
3452 char_u buf[NUMBUFLEN];
3453 char_u *p;
3454 int len;
3455 int failed = FALSE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003456 int idx;
Bram Moolenaarad39c092020-02-26 18:23:43 +01003457
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01003458 if (iptr->isn_type == ISN_ECHOWINDOW)
3459 count = iptr->isn_arg.echowin.ewin_count;
3460 else
3461 count = iptr->isn_arg.number;
Bram Moolenaarad39c092020-02-26 18:23:43 +01003462 ga_init2(&ga, 1, 80);
3463 for (idx = 0; idx < count; ++idx)
3464 {
3465 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003466 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01003467 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003468 if (tv->v_type == VAR_CHANNEL
3469 || tv->v_type == VAR_JOB)
3470 {
3471 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar68db9962021-05-09 23:19:22 +02003472 semsg(_(e_using_invalid_value_as_string_str),
3473 vartype_name(tv->v_type));
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003474 break;
3475 }
3476 else
3477 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01003478 }
3479 else
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003480 p = tv_stringify(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01003481
3482 len = (int)STRLEN(p);
Bram Moolenaar35578162021-08-02 19:10:38 +02003483 if (GA_GROW_FAILS(&ga, len + 2))
Bram Moolenaarad39c092020-02-26 18:23:43 +01003484 failed = TRUE;
3485 else
3486 {
3487 if (ga.ga_len > 0)
3488 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
3489 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
3490 ga.ga_len += len;
3491 }
3492 clear_tv(tv);
3493 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003494 ectx->ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003495 if (failed)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003496 {
3497 ga_clear(&ga);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003498 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003499 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01003500
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003501 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003502 {
3503 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02003504 {
3505 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003506 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01003507 if (did_emsg)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003508 {
3509 ga_clear(&ga);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01003510 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003511 }
Bram Moolenaar430deb12020-08-23 16:29:11 +02003512 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003513 else
3514 {
3515 msg_sb_eol();
3516 if (iptr->isn_type == ISN_ECHOMSG)
3517 {
3518 msg_attr(ga.ga_data, echo_attr);
3519 out_flush();
3520 }
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01003521#ifdef HAS_MESSAGE_WINDOW
3522 else if (iptr->isn_type == ISN_ECHOWINDOW)
3523 {
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01003524 start_echowindow(
3525 iptr->isn_arg.echowin.ewin_time);
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01003526 msg_attr(ga.ga_data, echo_attr);
3527 end_echowindow();
3528 }
3529#endif
Bram Moolenaar7de62622021-08-07 15:05:47 +02003530 else if (iptr->isn_type == ISN_ECHOCONSOLE)
3531 {
3532 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data),
3533 TRUE);
3534 ui_write((char_u *)"\r\n", 2, TRUE);
3535 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003536 else
3537 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003538 SOURCING_LNUM = iptr->isn_lnum;
3539 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003540 }
3541 }
3542 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01003543 ga_clear(&ga);
3544 }
3545 break;
3546
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003547 // load local variable or argument
3548 case ISN_LOAD:
Bram Moolenaar35578162021-08-02 19:10:38 +02003549 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003550 goto theend;
Bram Moolenaar2ed57ac2023-04-01 22:05:38 +01003551 tv = STACK_TV_VAR(iptr->isn_arg.number);
3552 if (tv->v_type == VAR_UNKNOWN)
3553 {
3554 // missing argument or default value v:none
3555 STACK_TV_BOT(0)->v_type = VAR_SPECIAL;
3556 STACK_TV_BOT(0)->vval.v_number = VVAL_NONE;
3557 }
3558 else
3559 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003560 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003561 break;
3562
3563 // load v: variable
3564 case ISN_LOADV:
Bram Moolenaar35578162021-08-02 19:10:38 +02003565 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003566 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003567 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003568 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003569 break;
3570
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003571 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003572 case ISN_LOADSCRIPT:
3573 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01003574 scriptref_T *sref = iptr->isn_arg.script.scriptref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003575 svar_T *sv;
3576
Bram Moolenaar6db660b2021-08-01 14:08:54 +02003577 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003578 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003579 goto theend;
Bram Moolenaar859cc212022-03-28 15:22:35 +01003580 allocate_if_null(sv);
Bram Moolenaar35578162021-08-02 19:10:38 +02003581 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003582 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003583 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003584 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003585 }
3586 break;
3587
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003588 // load s: variable in old script or autoload import
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003589 case ISN_LOADS:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003590 case ISN_LOADEXPORT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003591 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003592 int sid = iptr->isn_arg.loadstore.ls_sid;
3593 hashtab_T *ht = &SCRIPT_VARS(sid);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003594 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003595 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003596
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003597 if (di == NULL)
3598 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003599 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003600 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003601 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003602 }
3603 else
3604 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003605 if (iptr->isn_type == ISN_LOADEXPORT)
3606 {
3607 int idx = get_script_item_idx(sid, name, 0,
3608 NULL, NULL);
3609 svar_T *sv;
3610
3611 if (idx >= 0)
3612 {
3613 sv = ((svar_T *)SCRIPT_ITEM(sid)
3614 ->sn_var_vals.ga_data) + idx;
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003615 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003616 {
3617 SOURCING_LNUM = iptr->isn_lnum;
3618 semsg(_(e_item_not_exported_in_script_str),
3619 name);
3620 goto on_error;
3621 }
3622 }
3623 }
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(&di->di_tv, 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 }
3629 }
3630 break;
3631
Bram Moolenaard3aac292020-04-19 14:32:17 +02003632 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003633 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02003634 case ISN_LOADB:
3635 case ISN_LOADW:
3636 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003637 {
Bram Moolenaar06b77222022-01-25 15:51:56 +00003638 int res = load_namespace_var(ectx, iptr->isn_type, iptr);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003639
Bram Moolenaar06b77222022-01-25 15:51:56 +00003640 if (res == NOTDONE)
Bram Moolenaarcbbc48f2022-01-18 12:58:28 +00003641 goto theend;
Bram Moolenaar06b77222022-01-25 15:51:56 +00003642 if (res == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003643 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003644 }
Bram Moolenaar06b77222022-01-25 15:51:56 +00003645
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003646 break;
3647
Bram Moolenaar03290b82020-12-19 16:30:44 +01003648 // load autoload variable
3649 case ISN_LOADAUTO:
3650 {
3651 char_u *name = iptr->isn_arg.string;
3652
Bram Moolenaar35578162021-08-02 19:10:38 +02003653 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003654 goto theend;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003655 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003656 if (eval_variable(name, (int)STRLEN(name), 0,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003657 STACK_TV_BOT(0), NULL, EVAL_VAR_VERBOSE) == FAIL)
Bram Moolenaar03290b82020-12-19 16:30:44 +01003658 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003659 ++ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003660 }
3661 break;
3662
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003663 // load g:/b:/w:/t: namespace
3664 case ISN_LOADGDICT:
3665 case ISN_LOADBDICT:
3666 case ISN_LOADWDICT:
3667 case ISN_LOADTDICT:
3668 {
3669 dict_T *d = NULL;
3670
3671 switch (iptr->isn_type)
3672 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02003673 case ISN_LOADGDICT: d = get_globvar_dict(); break;
3674 case ISN_LOADBDICT: d = curbuf->b_vars; break;
3675 case ISN_LOADWDICT: d = curwin->w_vars; break;
3676 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003677 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003678 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003679 }
Bram Moolenaar35578162021-08-02 19:10:38 +02003680 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003681 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003682 tv = STACK_TV_BOT(0);
3683 tv->v_type = VAR_DICT;
3684 tv->v_lock = 0;
3685 tv->vval.v_dict = d;
Bram Moolenaar1bd3cb22021-02-24 12:27:31 +01003686 ++d->dv_refcount;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003687 ++ectx->ec_stack.ga_len;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003688 }
3689 break;
3690
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003691 // load &option
3692 case ISN_LOADOPT:
3693 {
3694 typval_T optval;
3695 char_u *name = iptr->isn_arg.string;
3696
Bram Moolenaara8c17702020-04-01 21:17:24 +02003697 // This is not expected to fail, name is checked during
3698 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar35578162021-08-02 19:10:38 +02003699 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003700 goto theend;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003701 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003702 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003703 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003704 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003705 }
3706 break;
3707
3708 // load $ENV
3709 case ISN_LOADENV:
3710 {
3711 typval_T optval;
3712 char_u *name = iptr->isn_arg.string;
3713
Bram Moolenaar35578162021-08-02 19:10:38 +02003714 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003715 goto theend;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003716 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003717 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003718 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003719 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003720 }
3721 break;
3722
3723 // load @register
3724 case ISN_LOADREG:
Bram Moolenaar35578162021-08-02 19:10:38 +02003725 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003726 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003727 tv = STACK_TV_BOT(0);
3728 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003729 tv->v_lock = 0;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02003730 // This may result in NULL, which should be equivalent to an
3731 // empty string.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003732 tv->vval.v_string = get_reg_contents(
3733 iptr->isn_arg.number, GREG_EXPR_SRC);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003734 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003735 break;
3736
3737 // store local variable
3738 case ISN_STORE:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003739 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003740 tv = STACK_TV_VAR(iptr->isn_arg.number);
3741 clear_tv(tv);
3742 *tv = *STACK_TV_BOT(0);
3743 break;
3744
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003745 // store s: variable in old script or autoload import
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003746 case ISN_STORES:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003747 case ISN_STOREEXPORT:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003748 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003749 int sid = iptr->isn_arg.loadstore.ls_sid;
3750 hashtab_T *ht = &SCRIPT_VARS(sid);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003751 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003752 dictitem_T *di = find_var_in_ht(ht, 0,
3753 iptr->isn_type == ISN_STORES
3754 ? name + 2 : name, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003755
Bram Moolenaar4c137212021-04-19 16:48:48 +02003756 --ectx->ec_stack.ga_len;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003757 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003758 if (di == NULL)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003759 {
3760 if (iptr->isn_type == ISN_STOREEXPORT)
3761 {
3762 semsg(_(e_undefined_variable_str), name);
Bram Moolenaard1d26842022-03-31 10:13:47 +01003763 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003764 goto on_error;
3765 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003766 store_var(name, STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003767 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003768 else
3769 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003770 if (iptr->isn_type == ISN_STOREEXPORT)
3771 {
3772 int idx = get_script_item_idx(sid, name, 0,
3773 NULL, NULL);
3774
Bram Moolenaar06651632022-04-27 17:54:25 +01003775 // can this ever fail?
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003776 if (idx >= 0)
3777 {
3778 svar_T *sv = ((svar_T *)SCRIPT_ITEM(sid)
3779 ->sn_var_vals.ga_data) + idx;
3780
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003781 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003782 {
3783 semsg(_(e_item_not_exported_in_script_str),
3784 name);
Bram Moolenaard1d26842022-03-31 10:13:47 +01003785 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003786 goto on_error;
3787 }
3788 }
3789 }
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02003790 if (var_check_permission(di, name) == FAIL)
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003791 {
3792 clear_tv(STACK_TV_BOT(0));
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02003793 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003794 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003795 clear_tv(&di->di_tv);
3796 di->di_tv = *STACK_TV_BOT(0);
3797 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003798 }
3799 break;
3800
3801 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003802 case ISN_STORESCRIPT:
3803 {
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003804 scriptref_T *sref = iptr->isn_arg.script.scriptref;
3805 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003806
Bram Moolenaar6db660b2021-08-01 14:08:54 +02003807 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003808 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003809 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003810 --ectx->ec_stack.ga_len;
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003811
3812 // "const" and "final" are checked at compile time, locking
3813 // the value needs to be checked here.
3814 SOURCING_LNUM = iptr->isn_lnum;
3815 if (value_check_lock(sv->sv_tv->v_lock, sv->sv_name, FALSE))
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003816 {
3817 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003818 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003819 }
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003820
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003821 clear_tv(sv->sv_tv);
3822 *sv->sv_tv = *STACK_TV_BOT(0);
3823 }
3824 break;
3825
3826 // store option
3827 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003828 case ISN_STOREFUNCOPT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003829 {
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003830 char_u *opt_name = iptr->isn_arg.storeopt.so_name;
3831 int opt_flags = iptr->isn_arg.storeopt.so_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003832 long n = 0;
3833 char_u *s = NULL;
3834 char *msg;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003835 char_u numbuf[NUMBUFLEN];
3836 char_u *tofree = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003837
Bram Moolenaar4c137212021-04-19 16:48:48 +02003838 --ectx->ec_stack.ga_len;
LemonBoy32f34612023-09-02 21:52:05 +02003839 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003840 tv = STACK_TV_BOT(0);
3841 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01003842 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003843 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01003844 if (s == NULL)
3845 s = (char_u *)"";
3846 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003847 else if (iptr->isn_type == ISN_STOREFUNCOPT)
3848 {
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003849 // If the option can be set to a function reference or
3850 // a lambda and the passed value is a function
3851 // reference, then convert it to the name (string) of
3852 // the function reference.
3853 s = tv2string(tv, &tofree, numbuf, 0);
3854 if (s == NULL || *s == NUL)
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003855 {
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003856 // cannot happen?
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003857 clear_tv(tv);
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003858 vim_free(tofree);
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003859 goto on_error;
3860 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003861 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003862 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003863 // must be VAR_NUMBER, CHECKTYPE makes sure
3864 n = tv->vval.v_number;
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003865 msg = set_option_value(opt_name, n, s, opt_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02003866 clear_tv(tv);
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003867 vim_free(tofree);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003868 if (msg != NULL)
3869 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003870 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003871 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02003872 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003873 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003874 }
3875 break;
3876
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003877 // store $ENV
3878 case ISN_STOREENV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003879 --ectx->ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003880 tv = STACK_TV_BOT(0);
3881 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
3882 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003883 break;
3884
3885 // store @r
3886 case ISN_STOREREG:
3887 {
3888 int reg = iptr->isn_arg.number;
3889
Bram Moolenaar4c137212021-04-19 16:48:48 +02003890 --ectx->ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01003891 tv = STACK_TV_BOT(0);
Bram Moolenaar74f4a962021-06-17 21:03:07 +02003892 write_reg_contents(reg, tv_get_string(tv), -1, FALSE);
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01003893 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003894 }
3895 break;
3896
3897 // store v: variable
3898 case ISN_STOREV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003899 --ectx->ec_stack.ga_len;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003900 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
3901 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003902 // should not happen, type is checked when compiling
3903 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003904 break;
3905
Bram Moolenaard3aac292020-04-19 14:32:17 +02003906 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003907 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02003908 case ISN_STOREB:
3909 case ISN_STOREW:
3910 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003911 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003912 dictitem_T *di;
3913 hashtab_T *ht;
3914 char_u *name = iptr->isn_arg.string + 2;
3915
Bram Moolenaard3aac292020-04-19 14:32:17 +02003916 switch (iptr->isn_type)
3917 {
3918 case ISN_STOREG:
3919 ht = get_globvar_ht();
3920 break;
3921 case ISN_STOREB:
3922 ht = &curbuf->b_vars->dv_hashtab;
3923 break;
3924 case ISN_STOREW:
3925 ht = &curwin->w_vars->dv_hashtab;
3926 break;
3927 case ISN_STORET:
3928 ht = &curtab->tp_vars->dv_hashtab;
3929 break;
3930 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003931 goto theend;
Bram Moolenaard3aac292020-04-19 14:32:17 +02003932 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003933
Bram Moolenaar4c137212021-04-19 16:48:48 +02003934 --ectx->ec_stack.ga_len;
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003935 di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003936 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003937 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003938 else
3939 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003940 SOURCING_LNUM = iptr->isn_lnum;
3941 if (var_check_permission(di, name) == FAIL)
3942 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003943 clear_tv(&di->di_tv);
3944 di->di_tv = *STACK_TV_BOT(0);
3945 }
3946 }
3947 break;
3948
Bram Moolenaar03290b82020-12-19 16:30:44 +01003949 // store an autoload variable
3950 case ISN_STOREAUTO:
3951 SOURCING_LNUM = iptr->isn_lnum;
3952 set_var(iptr->isn_arg.string, STACK_TV_BOT(-1), TRUE);
3953 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003954 --ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003955 break;
3956
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003957 // store number in local variable
3958 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01003959 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003960 clear_tv(tv);
3961 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01003962 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003963 break;
3964
Bram Moolenaar590162c2022-12-24 21:24:06 +00003965 // Store a value in a list, dict, blob or object variable.
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01003966 case ISN_STOREINDEX:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003967 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003968 int res = execute_storeindex(iptr, ectx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003969
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003970 if (res == FAIL)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02003971 goto on_error;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003972 if (res == NOTDONE)
3973 goto theend;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003974 }
3975 break;
3976
Bram Moolenaarea5c8982022-02-17 14:42:02 +00003977 // store value in list or blob range
Bram Moolenaar68452172021-04-12 21:21:02 +02003978 case ISN_STORERANGE:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003979 if (execute_storerange(iptr, ectx) == FAIL)
3980 goto on_error;
Bram Moolenaar68452172021-04-12 21:21:02 +02003981 break;
3982
Bram Moolenaard505d172022-12-18 21:42:55 +00003983 case ISN_LOAD_CLASSMEMBER:
3984 {
3985 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
3986 goto theend;
3987 classmember_T *cm = &iptr->isn_arg.classmember;
Bram Moolenaar4e2406c2023-06-24 19:22:21 +01003988 copy_tv(cm->cm_class->class_members_tv + cm->cm_idx,
3989 STACK_TV_BOT(0));
Bram Moolenaard505d172022-12-18 21:42:55 +00003990 ++ectx->ec_stack.ga_len;
3991 }
3992 break;
3993
3994 case ISN_STORE_CLASSMEMBER:
3995 {
3996 classmember_T *cm = &iptr->isn_arg.classmember;
3997 tv = &cm->cm_class->class_members_tv[cm->cm_idx];
3998 clear_tv(tv);
3999 *tv = *STACK_TV_BOT(-1);
4000 --ectx->ec_stack.ga_len;
4001 }
4002 break;
4003
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01004004 // Load or store variable or argument from outer scope.
Bram Moolenaar0186e582021-01-10 18:33:11 +01004005 case ISN_LOADOUTER:
4006 case ISN_STOREOUTER:
4007 {
4008 int depth = iptr->isn_arg.outer.outer_depth;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004009 outer_T *outer = ectx->ec_outer_ref == NULL ? NULL
4010 : ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +01004011
4012 while (depth > 1 && outer != NULL)
4013 {
4014 outer = outer->out_up;
4015 --depth;
4016 }
4017 if (outer == NULL)
4018 {
4019 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar69c76172021-12-02 16:38:52 +00004020 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx
4021 || ectx->ec_outer_ref == NULL)
4022 // Possibly :def function called from legacy
4023 // context.
4024 emsg(_(e_closure_called_from_invalid_context));
4025 else
4026 iemsg("LOADOUTER depth more than scope levels");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004027 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01004028 }
Bram Moolenaarcc341812022-09-19 15:54:34 +01004029 if (depth < 0)
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01004030 // Variable declared in loop. May be copied if the
4031 // loop block has already ended.
Bram Moolenaarcc341812022-09-19 15:54:34 +01004032 tv = ((typval_T *)outer->out_loop[-depth - 1]
4033 .stack->ga_data)
4034 + outer->out_loop[-depth - 1].var_idx
4035 + iptr->isn_arg.outer.outer_idx;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004036 else
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01004037 // Variable declared in a function. May be copied if
4038 // the function has already returned.
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004039 tv = ((typval_T *)outer->out_stack->ga_data)
4040 + outer->out_frame_idx + STACK_FRAME_SIZE
4041 + iptr->isn_arg.outer.outer_idx;
Bram Moolenaar0186e582021-01-10 18:33:11 +01004042 if (iptr->isn_type == ISN_LOADOUTER)
4043 {
Bram Moolenaar35578162021-08-02 19:10:38 +02004044 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004045 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01004046 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02004047 ++ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01004048 }
4049 else
4050 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004051 --ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01004052 clear_tv(tv);
4053 *tv = *STACK_TV_BOT(0);
4054 }
4055 }
4056 break;
4057
Bram Moolenaar752fc692021-01-04 21:57:11 +01004058 // unlet item in list or dict variable
4059 case ISN_UNLETINDEX:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00004060 if (execute_unletindex(iptr, ectx) == FAIL)
4061 goto on_error;
Bram Moolenaar752fc692021-01-04 21:57:11 +01004062 break;
4063
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01004064 // unlet range of items in list variable
4065 case ISN_UNLETRANGE:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00004066 if (execute_unletrange(iptr, ectx) == FAIL)
4067 goto on_error;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01004068 break;
4069
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004070 // push constant
4071 case ISN_PUSHNR:
4072 case ISN_PUSHBOOL:
4073 case ISN_PUSHSPEC:
4074 case ISN_PUSHF:
4075 case ISN_PUSHS:
4076 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004077 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004078 case ISN_PUSHCHANNEL:
4079 case ISN_PUSHJOB:
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00004080 case ISN_PUSHOBJ:
4081 case ISN_PUSHCLASS:
Bram Moolenaar35578162021-08-02 19:10:38 +02004082 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004083 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004084 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02004085 tv->v_lock = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004086 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004087 switch (iptr->isn_type)
4088 {
4089 case ISN_PUSHNR:
4090 tv->v_type = VAR_NUMBER;
4091 tv->vval.v_number = iptr->isn_arg.number;
4092 break;
4093 case ISN_PUSHBOOL:
4094 tv->v_type = VAR_BOOL;
4095 tv->vval.v_number = iptr->isn_arg.number;
4096 break;
4097 case ISN_PUSHSPEC:
4098 tv->v_type = VAR_SPECIAL;
4099 tv->vval.v_number = iptr->isn_arg.number;
4100 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004101 case ISN_PUSHF:
4102 tv->v_type = VAR_FLOAT;
4103 tv->vval.v_float = iptr->isn_arg.fnumber;
4104 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004105 case ISN_PUSHBLOB:
4106 blob_copy(iptr->isn_arg.blob, tv);
4107 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004108 case ISN_PUSHFUNC:
4109 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01004110 if (iptr->isn_arg.string == NULL)
4111 tv->vval.v_string = NULL;
4112 else
4113 tv->vval.v_string =
4114 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004115 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004116 case ISN_PUSHCHANNEL:
4117#ifdef FEAT_JOB_CHANNEL
4118 tv->v_type = VAR_CHANNEL;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00004119 tv->vval.v_channel = NULL;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004120#endif
4121 break;
4122 case ISN_PUSHJOB:
4123#ifdef FEAT_JOB_CHANNEL
4124 tv->v_type = VAR_JOB;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00004125 tv->vval.v_job = NULL;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004126#endif
4127 break;
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00004128 case ISN_PUSHOBJ:
4129 tv->v_type = VAR_OBJECT;
4130 tv->vval.v_object = NULL;
4131 break;
4132 case ISN_PUSHCLASS:
4133 tv->v_type = VAR_CLASS;
Bram Moolenaar30a84472023-02-27 08:07:14 +00004134 tv->vval.v_class = iptr->isn_arg.classarg;
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00004135 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004136 default:
4137 tv->v_type = VAR_STRING;
Bram Moolenaarf8691002022-03-10 12:20:53 +00004138 tv->vval.v_string = iptr->isn_arg.string == NULL
4139 ? NULL : vim_strsave(iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004140 }
4141 break;
4142
Bram Moolenaar06b77222022-01-25 15:51:56 +00004143 case ISN_AUTOLOAD:
4144 {
4145 char_u *name = iptr->isn_arg.string;
4146
4147 (void)script_autoload(name, FALSE);
4148 if (find_func(name, TRUE))
4149 {
4150 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
4151 goto theend;
4152 tv = STACK_TV_BOT(0);
4153 tv->v_lock = 0;
4154 ++ectx->ec_stack.ga_len;
4155 tv->v_type = VAR_FUNC;
4156 tv->vval.v_string = vim_strsave(name);
4157 }
4158 else
4159 {
4160 int res = load_namespace_var(ectx, ISN_LOADG, iptr);
4161
4162 if (res == NOTDONE)
4163 goto theend;
4164 if (res == FAIL)
4165 goto on_error;
4166 }
4167 }
4168 break;
4169
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004170 case ISN_UNLET:
4171 if (do_unlet(iptr->isn_arg.unlet.ul_name,
4172 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02004173 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004174 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02004175 case ISN_UNLETENV:
LemonBoy77142312022-04-15 20:50:46 +01004176 vim_unsetenv_ext(iptr->isn_arg.unlet.ul_name);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02004177 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004178
Bram Moolenaaraacc9662021-08-13 19:40:51 +02004179 case ISN_LOCKUNLOCK:
4180 {
Ernie Raelee865f32023-09-29 19:53:55 +02004181 // TODO: could put lval_root info in struct
Bram Moolenaaraacc9662021-08-13 19:40:51 +02004182 typval_T *lval_root_save = lval_root;
Ernie Raelee865f32023-09-29 19:53:55 +02004183 int lval_root_is_arg_save = lval_root_is_arg;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02004184 int res;
Ernie Raelee865f32023-09-29 19:53:55 +02004185#ifdef LOG_LOCKVAR
4186 ch_log(NULL, "LKVAR: execute INS_LOCKUNLOCK isn_arg %s",
4187 iptr->isn_arg.string);
4188#endif
Bram Moolenaaraacc9662021-08-13 19:40:51 +02004189
4190 // Stack has the local variable, argument the whole :lock
4191 // or :unlock command, like ISN_EXEC.
4192 --ectx->ec_stack.ga_len;
4193 lval_root = STACK_TV_BOT(0);
Ernie Raelee865f32023-09-29 19:53:55 +02004194 lval_root_is_arg = iptr->isn_arg.lockunlock.is_arg;
4195 res = exec_command(iptr, iptr->isn_arg.lockunlock.string);
Bram Moolenaaraacc9662021-08-13 19:40:51 +02004196 clear_tv(lval_root);
4197 lval_root = lval_root_save;
Ernie Raelee865f32023-09-29 19:53:55 +02004198 lval_root_is_arg = lval_root_is_arg_save;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02004199 if (res == FAIL)
4200 goto on_error;
4201 }
4202 break;
4203
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02004204 case ISN_LOCKCONST:
4205 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
4206 break;
4207
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004208 // create a list from items on the stack; uses a single allocation
4209 // for the list header and the items
4210 case ISN_NEWLIST:
Bram Moolenaar4c137212021-04-19 16:48:48 +02004211 if (exe_newlist(iptr->isn_arg.number, ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004212 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004213 break;
4214
4215 // create a dict from items on the stack
4216 case ISN_NEWDICT:
4217 {
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01004218 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004219
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01004220 SOURCING_LNUM = iptr->isn_lnum;
4221 res = exe_newdict(iptr->isn_arg.number, ectx);
4222 if (res == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004223 goto theend;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01004224 if (res == MAYBE)
4225 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004226 }
4227 break;
4228
LemonBoy372bcce2022-04-25 12:43:20 +01004229 case ISN_CONCAT:
4230 if (exe_concat(iptr->isn_arg.number, ectx) == FAIL)
4231 goto theend;
4232 break;
4233
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004234 // create a partial with NULL value
4235 case ISN_NEWPARTIAL:
4236 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
4237 goto theend;
4238 ++ectx->ec_stack.ga_len;
4239 tv = STACK_TV_BOT(-1);
4240 tv->v_type = VAR_PARTIAL;
4241 tv->v_lock = 0;
4242 tv->vval.v_partial = NULL;
4243 break;
4244
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004245 // call a :def function
4246 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02004247 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01004248 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
4249 NULL,
4250 iptr->isn_arg.dfunc.cdf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02004251 ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02004252 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004253 break;
4254
Bram Moolenaard0200c82023-01-28 15:19:40 +00004255 // call a method on an interface
4256 case ISN_METHODCALL:
4257 {
Bram Moolenaar094cf9f2023-02-10 15:52:25 +00004258 cmfunc_T *mfunc = iptr->isn_arg.mfunc;
4259
Bram Moolenaard0200c82023-01-28 15:19:40 +00004260 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar094cf9f2023-02-10 15:52:25 +00004261 tv = STACK_TV_BOT(-1 - mfunc->cmf_argcount);
Bram Moolenaard0200c82023-01-28 15:19:40 +00004262 if (tv->v_type != VAR_OBJECT)
4263 {
4264 object_required_error(tv);
4265 goto on_error;
4266 }
4267 object_T *obj = tv->vval.v_object;
4268 class_T *cl = obj->obj_class;
4269
4270 // convert the interface index to the object index
Bram Moolenaard0200c82023-01-28 15:19:40 +00004271 int idx = object_index_from_itf_index(mfunc->cmf_itf,
Yegappan Lakshmanan5a05d372023-09-29 19:43:11 +02004272 TRUE, mfunc->cmf_idx, cl);
Bram Moolenaard0200c82023-01-28 15:19:40 +00004273
4274 if (call_ufunc(cl->class_obj_methods[idx], NULL,
4275 mfunc->cmf_argcount, ectx, NULL, NULL) == FAIL)
4276 goto on_error;
4277 }
4278 break;
4279
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004280 // call a builtin function
4281 case ISN_BCALL:
4282 SOURCING_LNUM = iptr->isn_lnum;
4283 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
4284 iptr->isn_arg.bfunc.cbf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02004285 ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004286 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004287 break;
4288
4289 // call a funcref or partial
4290 case ISN_PCALL:
4291 {
4292 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
4293 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02004294 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004295
4296 SOURCING_LNUM = iptr->isn_lnum;
4297 if (pfunc->cpf_top)
4298 {
4299 // funcref is above the arguments
4300 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
4301 }
4302 else
4303 {
4304 // Get the funcref from the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004305 --ectx->ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02004306 partial_tv = *STACK_TV_BOT(0);
4307 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004308 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004309 r = call_partial(tv, pfunc->cpf_argcount, ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02004310 if (tv == &partial_tv)
4311 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004312 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004313 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004314 }
4315 break;
4316
Bram Moolenaarbd5da372020-03-31 23:13:10 +02004317 case ISN_PCALL_END:
4318 // PCALL finished, arguments have been consumed and replaced by
4319 // the return value. Now clear the funcref from the stack,
4320 // and move the return value in its place.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004321 --ectx->ec_stack.ga_len;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02004322 clear_tv(STACK_TV_BOT(-1));
4323 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
4324 break;
4325
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004326 // call a user defined function or funcref/partial
4327 case ISN_UCALL:
4328 {
4329 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
4330
4331 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01004332 if (call_eval_func(cufunc->cuf_name, cufunc->cuf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02004333 ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004334 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004335 }
4336 break;
4337
Bram Moolenaar1d84f762022-09-03 21:35:53 +01004338 // :defer func(arg)
4339 case ISN_DEFER:
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00004340 case ISN_DEFEROBJ:
Bram Moolenaar806a2732022-09-04 15:40:36 +01004341 if (defer_command(iptr->isn_arg.defer.defer_var_idx,
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00004342 iptr->isn_type == ISN_DEFEROBJ,
Bram Moolenaar1d84f762022-09-03 21:35:53 +01004343 iptr->isn_arg.defer.defer_argcount, ectx) == FAIL)
4344 goto on_error;
4345 break;
4346
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004347 // Return from a :def function call without a value.
4348 // Return from a constructor.
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02004349 case ISN_RETURN_VOID:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004350 case ISN_RETURN_OBJECT:
Bram Moolenaar35578162021-08-02 19:10:38 +02004351 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004352 goto theend;
Bram Moolenaar299f3032021-01-08 20:53:09 +01004353 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004354 ++ectx->ec_stack.ga_len;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004355 if (iptr->isn_type == ISN_RETURN_VOID)
4356 {
4357 tv->v_type = VAR_VOID;
4358 tv->vval.v_number = 0;
4359 tv->v_lock = 0;
4360 }
4361 else
4362 {
4363 *tv = *STACK_TV_VAR(0);
4364 ++tv->vval.v_object->obj_refcount;
4365 }
Bram Moolenaar299f3032021-01-08 20:53:09 +01004366 // FALLTHROUGH
4367
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02004368 // return from a :def function call with what is on the stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004369 case ISN_RETURN:
4370 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004371 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01004372 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01004373
4374 if (trystack->ga_len > 0)
4375 trycmd = ((trycmd_T *)trystack->ga_data)
4376 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004377 if (trycmd != NULL
Bram Moolenaar4c137212021-04-19 16:48:48 +02004378 && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004379 {
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01004380 // jump to ":finally" or ":endtry"
4381 if (trycmd->tcd_finally_idx != 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004382 ectx->ec_iidx = trycmd->tcd_finally_idx;
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01004383 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02004384 ectx->ec_iidx = trycmd->tcd_endtry_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004385 trycmd->tcd_return = TRUE;
4386 }
4387 else
Bram Moolenaard032f342020-07-18 18:13:02 +02004388 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004389 }
4390 break;
4391
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004392 // push a partial, a reference to a compiled function
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004393 case ISN_FUNCREF:
4394 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004395 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
4396 ufunc_T *ufunc;
4397 funcref_T *funcref = &iptr->isn_arg.funcref;
4398 funcref_extra_T *extra = funcref->fr_extra;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004399
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004400 if (pt == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004401 goto theend;
Bram Moolenaar35578162021-08-02 19:10:38 +02004402 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02004403 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004404 vim_free(pt);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004405 goto theend;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004406 }
Bram Moolenaar313e4722023-02-08 20:55:27 +00004407 if (extra != NULL && extra->fre_class != NULL)
4408 {
4409 tv = STACK_TV_BOT(-1);
4410 if (tv->v_type != VAR_OBJECT)
4411 {
4412 object_required_error(tv);
4413 vim_free(pt);
4414 goto on_error;
4415 }
4416 object_T *obj = tv->vval.v_object;
4417 class_T *cl = obj->obj_class;
4418
4419 // convert the interface index to the object index
4420 int idx = object_index_from_itf_index(extra->fre_class,
Yegappan Lakshmanan5a05d372023-09-29 19:43:11 +02004421 TRUE, extra->fre_method_idx, cl);
Bram Moolenaar313e4722023-02-08 20:55:27 +00004422 ufunc = cl->class_obj_methods[idx];
4423 }
4424 else if (extra == NULL || extra->fre_func_name == NULL)
Bram Moolenaar38453522021-11-28 22:00:12 +00004425 {
4426 dfunc_T *pt_dfunc = ((dfunc_T *)def_functions.ga_data)
4427 + funcref->fr_dfunc_idx;
4428
4429 ufunc = pt_dfunc->df_ufunc;
4430 }
4431 else
Bram Moolenaar313e4722023-02-08 20:55:27 +00004432 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004433 ufunc = find_func(extra->fre_func_name, FALSE);
Bram Moolenaar313e4722023-02-08 20:55:27 +00004434 }
Bram Moolenaar56acd1f2022-02-18 13:24:52 +00004435 if (ufunc == NULL)
4436 {
4437 SOURCING_LNUM = iptr->isn_lnum;
4438 iemsg("ufunc unexpectedly NULL for FUNCREF");
4439 goto theend;
4440 }
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004441 if (fill_partial_and_closure(pt, ufunc,
Bram Moolenaarcc341812022-09-19 15:54:34 +01004442 extra == NULL ? NULL : &extra->fre_loopvar_info,
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004443 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004444 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004445 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004446 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004447 tv->vval.v_partial = pt;
4448 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02004449 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004450 }
4451 break;
4452
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004453 // Create a global function from a lambda.
4454 case ISN_NEWFUNC:
4455 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004456 newfuncarg_T *arg = iptr->isn_arg.newfunc.nf_arg;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004457
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004458 if (copy_lambda_to_global_func(arg->nfa_lambda,
Bram Moolenaarcc341812022-09-19 15:54:34 +01004459 arg->nfa_global, &arg->nfa_loopvar_info,
4460 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004461 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004462 }
4463 break;
4464
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004465 // List functions
4466 case ISN_DEF:
4467 if (iptr->isn_arg.string == NULL)
4468 list_functions(NULL);
4469 else
4470 {
Bram Moolenaar14336722022-01-08 16:02:59 +00004471 exarg_T ea;
4472 garray_T lines_to_free;
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004473
4474 CLEAR_FIELD(ea);
4475 ea.cmd = ea.arg = iptr->isn_arg.string;
Bram Moolenaar14336722022-01-08 16:02:59 +00004476 ga_init2(&lines_to_free, sizeof(char_u *), 50);
Bram Moolenaard4a9b7f2023-05-23 14:48:42 +01004477 SOURCING_LNUM = iptr->isn_lnum;
h-eastb895b0f2023-09-24 15:46:31 +02004478 define_function(&ea, NULL, &lines_to_free, 0, NULL, 0);
Bram Moolenaar14336722022-01-08 16:02:59 +00004479 ga_clear_strings(&lines_to_free);
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004480 }
4481 break;
4482
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004483 // jump if a condition is met
4484 case ISN_JUMP:
4485 {
4486 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004487 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004488 int jump = TRUE;
4489
4490 if (when != JUMP_ALWAYS)
4491 {
4492 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004493 if (when == JUMP_IF_COND_FALSE
Bram Moolenaar13106602020-10-04 16:06:05 +02004494 || when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004495 || when == JUMP_IF_COND_TRUE)
4496 {
4497 SOURCING_LNUM = iptr->isn_lnum;
4498 jump = tv_get_bool_chk(tv, &error);
4499 if (error)
4500 goto on_error;
4501 }
4502 else
4503 jump = tv2bool(tv);
Bram Moolenaarf6ced982022-04-28 12:00:49 +01004504 if (when == JUMP_IF_FALSE || when == JUMP_IF_COND_FALSE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004505 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01004506 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004507 {
4508 // drop the value from the stack
4509 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004510 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004511 }
4512 }
4513 if (jump)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004514 ectx->ec_iidx = iptr->isn_arg.jump.jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004515 }
4516 break;
4517
Bram Moolenaarb46c0832022-09-15 17:19:37 +01004518 // "while": jump to end if a condition is false
4519 case ISN_WHILE:
4520 {
4521 int error = FALSE;
4522 int jump = TRUE;
4523
4524 tv = STACK_TV_BOT(-1);
4525 SOURCING_LNUM = iptr->isn_lnum;
4526 jump = !tv_get_bool_chk(tv, &error);
4527 if (error)
4528 goto on_error;
4529 // drop the value from the stack
4530 clear_tv(tv);
4531 --ectx->ec_stack.ga_len;
4532 if (jump)
4533 ectx->ec_iidx = iptr->isn_arg.whileloop.while_end;
4534
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01004535 // Store the current funcref count, may be used by
Bram Moolenaarcc341812022-09-19 15:54:34 +01004536 // ISN_ENDLOOP later
Bram Moolenaarb46c0832022-09-15 17:19:37 +01004537 tv = STACK_TV_VAR(
4538 iptr->isn_arg.whileloop.while_funcref_idx);
4539 tv->vval.v_number = ectx->ec_funcrefs.ga_len;
4540 }
4541 break;
4542
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02004543 // Jump if an argument with a default value was already set and not
4544 // v:none.
4545 case ISN_JUMP_IF_ARG_SET:
Bram Moolenaar65b0d162022-12-13 18:43:22 +00004546 case ISN_JUMP_IF_ARG_NOT_SET:
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02004547 tv = STACK_TV_VAR(iptr->isn_arg.jumparg.jump_arg_off);
Bram Moolenaar65b0d162022-12-13 18:43:22 +00004548 int arg_set = tv->v_type != VAR_UNKNOWN
4549 && !(tv->v_type == VAR_SPECIAL
4550 && tv->vval.v_number == VVAL_NONE);
4551 if (iptr->isn_type == ISN_JUMP_IF_ARG_SET ? arg_set : !arg_set)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004552 ectx->ec_iidx = iptr->isn_arg.jumparg.jump_where;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02004553 break;
4554
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004555 // top of a for loop
4556 case ISN_FOR:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00004557 if (execute_for(iptr, ectx) == FAIL)
4558 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004559 break;
4560
Bram Moolenaarb46c0832022-09-15 17:19:37 +01004561 // end of a for or while loop
4562 case ISN_ENDLOOP:
4563 if (execute_endloop(iptr, ectx) == FAIL)
4564 goto theend;
4565 break;
4566
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004567 // start of ":try" block
4568 case ISN_TRY:
4569 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01004570 trycmd_T *trycmd = NULL;
4571
Bram Moolenaar35578162021-08-02 19:10:38 +02004572 if (GA_GROW_FAILS(&ectx->ec_trystack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004573 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004574 trycmd = ((trycmd_T *)ectx->ec_trystack.ga_data)
4575 + ectx->ec_trystack.ga_len;
4576 ++ectx->ec_trystack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004577 ++trylevel;
Bram Moolenaar8d4be892021-02-13 18:33:02 +01004578 CLEAR_POINTER(trycmd);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004579 trycmd->tcd_frame_idx = ectx->ec_frame_idx;
4580 trycmd->tcd_stack_len = ectx->ec_stack.ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01004581 trycmd->tcd_catch_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00004582 iptr->isn_arg.tryref.try_ref->try_catch;
Bram Moolenaara91a7132021-03-25 21:12:15 +01004583 trycmd->tcd_finally_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00004584 iptr->isn_arg.tryref.try_ref->try_finally;
Bram Moolenaara91a7132021-03-25 21:12:15 +01004585 trycmd->tcd_endtry_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00004586 iptr->isn_arg.tryref.try_ref->try_endtry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004587 }
4588 break;
4589
4590 case ISN_PUSHEXC:
4591 if (current_exception == NULL)
4592 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004593 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004594 iemsg("Evaluating catch while current_exception is NULL");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004595 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004596 }
Bram Moolenaar35578162021-08-02 19:10:38 +02004597 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004598 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004599 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004600 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004601 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02004602 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004603 tv->vval.v_string = vim_strsave(
4604 (char_u *)current_exception->value);
4605 break;
4606
4607 case ISN_CATCH:
4608 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004609 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar06651632022-04-27 17:54:25 +01004610 trycmd_T *trycmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004611
Bram Moolenaar4c137212021-04-19 16:48:48 +02004612 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar06651632022-04-27 17:54:25 +01004613 trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004614 + trystack->ga_len - 1;
Bram Moolenaar06651632022-04-27 17:54:25 +01004615 trycmd->tcd_caught = TRUE;
4616 trycmd->tcd_did_throw = FALSE;
4617
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004618 did_emsg = got_int = did_throw = FALSE;
Bram Moolenaar1430cee2021-01-17 19:20:32 +01004619 force_abort = need_rethrow = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004620 catch_exception(current_exception);
4621 }
4622 break;
4623
Bram Moolenaarc150c092021-02-13 15:02:46 +01004624 case ISN_TRYCONT:
4625 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004626 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004627 trycont_T *trycont = &iptr->isn_arg.trycont;
4628 int i;
4629 trycmd_T *trycmd;
4630 int iidx = trycont->tct_where;
4631
4632 if (trystack->ga_len < trycont->tct_levels)
4633 {
4634 siemsg("TRYCONT: expected %d levels, found %d",
4635 trycont->tct_levels, trystack->ga_len);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004636 goto theend;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004637 }
4638 // Make :endtry jump to any outer try block and the last
4639 // :endtry inside the loop to the loop start.
4640 for (i = trycont->tct_levels; i > 0; --i)
4641 {
4642 trycmd = ((trycmd_T *)trystack->ga_data)
4643 + trystack->ga_len - i;
Bram Moolenaar2e34c342021-03-14 12:13:33 +01004644 // Add one to tcd_cont to be able to jump to
4645 // instruction with index zero.
4646 trycmd->tcd_cont = iidx + 1;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01004647 iidx = trycmd->tcd_finally_idx == 0
4648 ? trycmd->tcd_endtry_idx : trycmd->tcd_finally_idx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004649 }
4650 // jump to :finally or :endtry of current try statement
Bram Moolenaar4c137212021-04-19 16:48:48 +02004651 ectx->ec_iidx = iidx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004652 }
4653 break;
4654
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01004655 case ISN_FINALLY:
4656 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004657 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01004658 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
4659 + trystack->ga_len - 1;
4660
4661 // Reset the index to avoid a return statement jumps here
4662 // again.
4663 trycmd->tcd_finally_idx = 0;
4664 break;
4665 }
4666
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004667 // end of ":try" block
4668 case ISN_ENDTRY:
4669 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004670 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar06651632022-04-27 17:54:25 +01004671 trycmd_T *trycmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004672
Bram Moolenaar06651632022-04-27 17:54:25 +01004673 --trystack->ga_len;
4674 --trylevel;
4675 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len;
4676 if (trycmd->tcd_did_throw)
4677 did_throw = TRUE;
4678 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004679 {
Bram Moolenaar06651632022-04-27 17:54:25 +01004680 // discard the exception
4681 if (caught_stack == current_exception)
4682 caught_stack = caught_stack->caught;
4683 discard_current_exception();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004684 }
Bram Moolenaar06651632022-04-27 17:54:25 +01004685
4686 if (trycmd->tcd_return)
4687 goto func_return;
4688
4689 while (ectx->ec_stack.ga_len > trycmd->tcd_stack_len)
4690 {
4691 --ectx->ec_stack.ga_len;
4692 clear_tv(STACK_TV_BOT(0));
4693 }
4694 if (trycmd->tcd_cont != 0)
4695 // handling :continue: jump to outer try block or
4696 // start of the loop
4697 ectx->ec_iidx = trycmd->tcd_cont - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004698 }
4699 break;
4700
4701 case ISN_THROW:
Bram Moolenaar8f81b222021-01-14 21:47:06 +01004702 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004703 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02004704
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004705 if (trystack->ga_len == 0 && trylevel == 0 && emsg_silent)
4706 {
Bram Moolenaar3ef2e412023-04-30 18:50:48 +01004707 // Throwing an exception while using "silent!" causes
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004708 // the function to abort but not display an error.
4709 tv = STACK_TV_BOT(-1);
4710 clear_tv(tv);
4711 tv->v_type = VAR_NUMBER;
4712 tv->vval.v_number = 0;
4713 goto done;
4714 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004715 --ectx->ec_stack.ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004716 tv = STACK_TV_BOT(0);
4717 if (tv->vval.v_string == NULL
4718 || *skipwhite(tv->vval.v_string) == NUL)
4719 {
4720 vim_free(tv->vval.v_string);
4721 SOURCING_LNUM = iptr->isn_lnum;
4722 emsg(_(e_throw_with_empty_string));
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004723 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004724 }
4725
4726 // Inside a "catch" we need to first discard the caught
4727 // exception.
4728 if (trystack->ga_len > 0)
4729 {
4730 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
4731 + trystack->ga_len - 1;
4732 if (trycmd->tcd_caught && current_exception != NULL)
4733 {
4734 // discard the exception
4735 if (caught_stack == current_exception)
4736 caught_stack = caught_stack->caught;
4737 discard_current_exception();
4738 trycmd->tcd_caught = FALSE;
4739 }
4740 }
4741
Bram Moolenaar90a57162022-02-12 14:23:17 +00004742 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004743 if (throw_exception(tv->vval.v_string, ET_USER, NULL)
4744 == FAIL)
4745 {
4746 vim_free(tv->vval.v_string);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004747 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004748 }
4749 did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004750 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004751 break;
4752
4753 // compare with special values
4754 case ISN_COMPAREBOOL:
4755 case ISN_COMPARESPECIAL:
4756 {
4757 typval_T *tv1 = STACK_TV_BOT(-2);
4758 typval_T *tv2 = STACK_TV_BOT(-1);
4759 varnumber_T arg1 = tv1->vval.v_number;
4760 varnumber_T arg2 = tv2->vval.v_number;
4761 int res;
4762
Bram Moolenaar06651632022-04-27 17:54:25 +01004763 if (iptr->isn_arg.op.op_type == EXPR_EQUAL)
4764 res = arg1 == arg2;
4765 else
4766 res = arg1 != arg2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004767
Bram Moolenaar4c137212021-04-19 16:48:48 +02004768 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004769 tv1->v_type = VAR_BOOL;
4770 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4771 }
4772 break;
4773
Bram Moolenaar7a222242022-03-01 19:23:24 +00004774 case ISN_COMPARENULL:
4775 {
4776 typval_T *tv1 = STACK_TV_BOT(-2);
4777 typval_T *tv2 = STACK_TV_BOT(-1);
4778 int res;
4779
4780 res = typval_compare_null(tv1, tv2);
4781 if (res == MAYBE)
4782 goto on_error;
4783 if (iptr->isn_arg.op.op_type == EXPR_NEQUAL)
4784 res = !res;
4785 clear_tv(tv1);
4786 clear_tv(tv2);
4787 --ectx->ec_stack.ga_len;
4788 tv1->v_type = VAR_BOOL;
4789 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4790 }
4791 break;
4792
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004793 // Operation with two number arguments
4794 case ISN_OPNR:
4795 case ISN_COMPARENR:
4796 {
4797 typval_T *tv1 = STACK_TV_BOT(-2);
4798 typval_T *tv2 = STACK_TV_BOT(-1);
4799 varnumber_T arg1 = tv1->vval.v_number;
4800 varnumber_T arg2 = tv2->vval.v_number;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004801 varnumber_T res = 0;
4802 int div_zero = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004803
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004804 if (iptr->isn_arg.op.op_type == EXPR_LSHIFT
4805 || iptr->isn_arg.op.op_type == EXPR_RSHIFT)
4806 {
4807 if (arg2 < 0)
4808 {
4809 SOURCING_LNUM = iptr->isn_lnum;
dundargocc57b5bc2022-11-02 13:30:51 +00004810 emsg(_(e_bitshift_ops_must_be_positive));
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004811 goto on_error;
4812 }
4813 }
4814
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004815 switch (iptr->isn_arg.op.op_type)
4816 {
4817 case EXPR_MULT: res = arg1 * arg2; break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004818 case EXPR_DIV: if (arg2 == 0)
4819 div_zero = TRUE;
4820 else
4821 res = arg1 / arg2;
4822 break;
4823 case EXPR_REM: if (arg2 == 0)
4824 div_zero = TRUE;
4825 else
4826 res = arg1 % arg2;
4827 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004828 case EXPR_SUB: res = arg1 - arg2; break;
4829 case EXPR_ADD: res = arg1 + arg2; break;
4830
4831 case EXPR_EQUAL: res = arg1 == arg2; break;
4832 case EXPR_NEQUAL: res = arg1 != arg2; break;
4833 case EXPR_GREATER: res = arg1 > arg2; break;
4834 case EXPR_GEQUAL: res = arg1 >= arg2; break;
4835 case EXPR_SMALLER: res = arg1 < arg2; break;
4836 case EXPR_SEQUAL: res = arg1 <= arg2; break;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004837 case EXPR_LSHIFT: if (arg2 > MAX_LSHIFT_BITS)
4838 res = 0;
4839 else
Bram Moolenaar68e64d22022-05-22 22:07:52 +01004840 res = (uvarnumber_T)arg1 << arg2;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004841 break;
4842 case EXPR_RSHIFT: if (arg2 > MAX_LSHIFT_BITS)
4843 res = 0;
4844 else
Bram Moolenaar338bf582022-05-22 20:16:32 +01004845 res = (uvarnumber_T)arg1 >> arg2;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004846 break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004847 default: break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004848 }
4849
Bram Moolenaar4c137212021-04-19 16:48:48 +02004850 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004851 if (iptr->isn_type == ISN_COMPARENR)
4852 {
4853 tv1->v_type = VAR_BOOL;
4854 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4855 }
4856 else
4857 tv1->vval.v_number = res;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004858 if (div_zero)
4859 {
4860 SOURCING_LNUM = iptr->isn_lnum;
4861 emsg(_(e_divide_by_zero));
4862 goto on_error;
4863 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004864 }
4865 break;
4866
4867 // Computation with two float arguments
4868 case ISN_OPFLOAT:
4869 case ISN_COMPAREFLOAT:
4870 {
4871 typval_T *tv1 = STACK_TV_BOT(-2);
4872 typval_T *tv2 = STACK_TV_BOT(-1);
4873 float_T arg1 = tv1->vval.v_float;
4874 float_T arg2 = tv2->vval.v_float;
4875 float_T res = 0;
4876 int cmp = FALSE;
4877
4878 switch (iptr->isn_arg.op.op_type)
4879 {
4880 case EXPR_MULT: res = arg1 * arg2; break;
4881 case EXPR_DIV: res = arg1 / arg2; break;
4882 case EXPR_SUB: res = arg1 - arg2; break;
4883 case EXPR_ADD: res = arg1 + arg2; break;
4884
4885 case EXPR_EQUAL: cmp = arg1 == arg2; break;
4886 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
4887 case EXPR_GREATER: cmp = arg1 > arg2; break;
4888 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
4889 case EXPR_SMALLER: cmp = arg1 < arg2; break;
4890 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
4891 default: cmp = 0; break;
4892 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004893 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004894 if (iptr->isn_type == ISN_COMPAREFLOAT)
4895 {
4896 tv1->v_type = VAR_BOOL;
4897 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
4898 }
4899 else
4900 tv1->vval.v_float = res;
4901 }
4902 break;
4903
4904 case ISN_COMPARELIST:
Bram Moolenaar265f8112021-12-19 12:33:05 +00004905 case ISN_COMPAREDICT:
4906 case ISN_COMPAREFUNC:
4907 case ISN_COMPARESTRING:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004908 case ISN_COMPAREBLOB:
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00004909 case ISN_COMPARECLASS:
4910 case ISN_COMPAREOBJECT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004911 {
4912 typval_T *tv1 = STACK_TV_BOT(-2);
4913 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar265f8112021-12-19 12:33:05 +00004914 exprtype_T exprtype = iptr->isn_arg.op.op_type;
4915 int ic = iptr->isn_arg.op.op_ic;
4916 int res = FALSE;
4917 int status = OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004918
Bram Moolenaar265f8112021-12-19 12:33:05 +00004919 SOURCING_LNUM = iptr->isn_lnum;
4920 if (iptr->isn_type == ISN_COMPARELIST)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004921 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00004922 status = typval_compare_list(tv1, tv2,
4923 exprtype, ic, &res);
4924 }
4925 else if (iptr->isn_type == ISN_COMPAREDICT)
4926 {
4927 status = typval_compare_dict(tv1, tv2,
4928 exprtype, ic, &res);
4929 }
4930 else if (iptr->isn_type == ISN_COMPAREFUNC)
4931 {
4932 status = typval_compare_func(tv1, tv2,
4933 exprtype, ic, &res);
4934 }
4935 else if (iptr->isn_type == ISN_COMPARESTRING)
4936 {
4937 status = typval_compare_string(tv1, tv2,
4938 exprtype, ic, &res);
4939 }
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00004940 else if (iptr->isn_type == ISN_COMPAREBLOB)
Bram Moolenaar265f8112021-12-19 12:33:05 +00004941 {
4942 status = typval_compare_blob(tv1, tv2, exprtype, &res);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004943 }
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00004944 else if (iptr->isn_type == ISN_COMPARECLASS)
4945 {
Bram Moolenaar03ff0c62023-01-02 20:38:01 +00004946 status = typval_compare_class(tv1, tv2,
4947 exprtype, FALSE, &res);
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00004948 }
4949 else // ISN_COMPAREOBJECT
4950 {
4951 status = typval_compare_object(tv1, tv2,
Bram Moolenaar03ff0c62023-01-02 20:38:01 +00004952 exprtype, FALSE, &res);
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00004953 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004954 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004955 clear_tv(tv1);
4956 clear_tv(tv2);
4957 tv1->v_type = VAR_BOOL;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004958 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4959 if (status == FAIL)
4960 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004961 }
4962 break;
4963
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004964 case ISN_COMPAREANY:
4965 {
4966 typval_T *tv1 = STACK_TV_BOT(-2);
4967 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar657137c2021-01-09 15:45:23 +01004968 exprtype_T exprtype = iptr->isn_arg.op.op_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004969 int ic = iptr->isn_arg.op.op_ic;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004970 int status;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004971
Bram Moolenaareb26f432020-09-14 16:50:05 +02004972 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004973 status = typval_compare(tv1, tv2, exprtype, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004974 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004975 --ectx->ec_stack.ga_len;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004976 if (status == FAIL)
4977 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004978 }
4979 break;
4980
4981 case ISN_ADDLIST:
4982 case ISN_ADDBLOB:
4983 {
4984 typval_T *tv1 = STACK_TV_BOT(-2);
4985 typval_T *tv2 = STACK_TV_BOT(-1);
4986
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004987 // add two lists or blobs
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004988 if (iptr->isn_type == ISN_ADDLIST)
Bram Moolenaar07802042021-09-09 23:01:14 +02004989 {
4990 if (iptr->isn_arg.op.op_type == EXPR_APPEND
4991 && tv1->vval.v_list != NULL)
4992 list_extend(tv1->vval.v_list, tv2->vval.v_list,
4993 NULL);
4994 else
4995 eval_addlist(tv1, tv2);
4996 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004997 else
4998 eval_addblob(tv1, tv2);
4999 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005000 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005001 }
5002 break;
5003
Bram Moolenaar1dcae592020-10-19 19:02:42 +02005004 case ISN_LISTAPPEND:
5005 {
5006 typval_T *tv1 = STACK_TV_BOT(-2);
5007 typval_T *tv2 = STACK_TV_BOT(-1);
5008 list_T *l = tv1->vval.v_list;
5009
5010 // add an item to a list
Bram Moolenaar1f4a3452022-01-01 18:29:21 +00005011 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02005012 if (l == NULL)
5013 {
Bram Moolenaar1dcae592020-10-19 19:02:42 +02005014 emsg(_(e_cannot_add_to_null_list));
5015 goto on_error;
5016 }
Bram Moolenaar1f4a3452022-01-01 18:29:21 +00005017 if (value_check_lock(l->lv_lock, NULL, FALSE))
5018 goto on_error;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02005019 if (list_append_tv(l, tv2) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005020 goto theend;
Bram Moolenaar955347c2020-10-19 23:01:46 +02005021 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005022 --ectx->ec_stack.ga_len;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02005023 }
5024 break;
5025
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02005026 case ISN_BLOBAPPEND:
5027 {
5028 typval_T *tv1 = STACK_TV_BOT(-2);
5029 typval_T *tv2 = STACK_TV_BOT(-1);
5030 blob_T *b = tv1->vval.v_blob;
5031 int error = FALSE;
5032 varnumber_T n;
5033
5034 // add a number to a blob
5035 if (b == NULL)
5036 {
5037 SOURCING_LNUM = iptr->isn_lnum;
5038 emsg(_(e_cannot_add_to_null_blob));
5039 goto on_error;
5040 }
5041 n = tv_get_number_chk(tv2, &error);
5042 if (error)
5043 goto on_error;
5044 ga_append(&b->bv_ga, (int)n);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005045 --ectx->ec_stack.ga_len;
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02005046 }
5047 break;
5048
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005049 // Computation with two arguments of unknown type
5050 case ISN_OPANY:
5051 {
5052 typval_T *tv1 = STACK_TV_BOT(-2);
5053 typval_T *tv2 = STACK_TV_BOT(-1);
5054 varnumber_T n1, n2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005055 float_T f1 = 0, f2 = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005056 int error = FALSE;
5057
5058 if (iptr->isn_arg.op.op_type == EXPR_ADD)
5059 {
5060 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
5061 {
5062 eval_addlist(tv1, tv2);
5063 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005064 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005065 break;
5066 }
5067 else if (tv1->v_type == VAR_BLOB
5068 && tv2->v_type == VAR_BLOB)
5069 {
5070 eval_addblob(tv1, tv2);
5071 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005072 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005073 break;
5074 }
5075 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005076 if (tv1->v_type == VAR_FLOAT)
5077 {
5078 f1 = tv1->vval.v_float;
5079 n1 = 0;
5080 }
5081 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005082 {
Bram Moolenaarf665e972020-12-05 19:17:16 +01005083 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005084 n1 = tv_get_number_chk(tv1, &error);
5085 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02005086 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005087 if (tv2->v_type == VAR_FLOAT)
5088 f1 = n1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005089 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005090 if (tv2->v_type == VAR_FLOAT)
5091 {
5092 f2 = tv2->vval.v_float;
5093 n2 = 0;
5094 }
5095 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005096 {
5097 n2 = tv_get_number_chk(tv2, &error);
5098 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02005099 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005100 if (tv1->v_type == VAR_FLOAT)
5101 f2 = n2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005102 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005103 // if there is a float on either side the result is a float
5104 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
5105 {
5106 switch (iptr->isn_arg.op.op_type)
5107 {
5108 case EXPR_MULT: f1 = f1 * f2; break;
5109 case EXPR_DIV: f1 = f1 / f2; break;
5110 case EXPR_SUB: f1 = f1 - f2; break;
5111 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005112 default: SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005113 emsg(_(e_cannot_use_percent_with_float));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02005114 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005115 }
5116 clear_tv(tv1);
5117 clear_tv(tv2);
5118 tv1->v_type = VAR_FLOAT;
5119 tv1->vval.v_float = f1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005120 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005121 }
5122 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005123 {
Bram Moolenaarb1f28572021-01-21 13:03:20 +01005124 int failed = FALSE;
5125
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005126 switch (iptr->isn_arg.op.op_type)
5127 {
5128 case EXPR_MULT: n1 = n1 * n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01005129 case EXPR_DIV: n1 = num_divide(n1, n2, &failed);
5130 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01005131 goto on_error;
5132 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005133 case EXPR_SUB: n1 = n1 - n2; break;
5134 case EXPR_ADD: n1 = n1 + n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01005135 default: n1 = num_modulus(n1, n2, &failed);
5136 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01005137 goto on_error;
5138 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005139 }
5140 clear_tv(tv1);
5141 clear_tv(tv2);
5142 tv1->v_type = VAR_NUMBER;
5143 tv1->vval.v_number = n1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005144 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005145 }
5146 }
5147 break;
5148
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005149 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005150 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005151 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005152 int is_slice = iptr->isn_type == ISN_STRSLICE;
5153 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005154 char_u *res;
5155
5156 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005157 // string slice: string is at stack-3, first index at
5158 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005159 if (is_slice)
5160 {
5161 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005162 n1 = tv->vval.v_number;
5163 }
5164
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005165 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005166 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005167
Bram Moolenaar4c137212021-04-19 16:48:48 +02005168 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005169 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005170 if (is_slice)
5171 // Slice: Select the characters from the string
Bram Moolenaar6601b622021-01-13 21:47:15 +01005172 res = string_slice(tv->vval.v_string, n1, n2, FALSE);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005173 else
5174 // Index: The resulting variable is a string of a
Bram Moolenaar0289a092021-03-14 18:40:19 +01005175 // single character (including composing characters).
5176 // If the index is too big or negative the result is
5177 // empty.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005178 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005179 vim_free(tv->vval.v_string);
5180 tv->vval.v_string = res;
5181 }
5182 break;
5183
5184 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02005185 case ISN_LISTSLICE:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02005186 case ISN_BLOBINDEX:
5187 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005188 {
Bram Moolenaarcfc30232021-04-11 20:26:34 +02005189 int is_slice = iptr->isn_type == ISN_LISTSLICE
5190 || iptr->isn_type == ISN_BLOBSLICE;
5191 int is_blob = iptr->isn_type == ISN_BLOBINDEX
5192 || iptr->isn_type == ISN_BLOBSLICE;
Bram Moolenaared591872020-08-15 22:14:53 +02005193 varnumber_T n1, n2;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02005194 typval_T *val_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005195
5196 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02005197 // list slice: list is at stack-3, indexes at stack-2 and
5198 // stack-1
Bram Moolenaarcfc30232021-04-11 20:26:34 +02005199 // Same for blob.
5200 val_tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005201
5202 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02005203 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005204 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02005205
5206 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005207 {
Bram Moolenaared591872020-08-15 22:14:53 +02005208 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02005209 n1 = tv->vval.v_number;
5210 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005211 }
Bram Moolenaared591872020-08-15 22:14:53 +02005212
Bram Moolenaar4c137212021-04-19 16:48:48 +02005213 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02005214 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02005215 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02005216 if (is_blob)
5217 {
5218 if (blob_slice_or_index(val_tv->vval.v_blob, is_slice,
5219 n1, n2, FALSE, tv) == FAIL)
5220 goto on_error;
5221 }
5222 else
5223 {
5224 if (list_slice_or_index(val_tv->vval.v_list, is_slice,
5225 n1, n2, FALSE, tv, TRUE) == FAIL)
5226 goto on_error;
5227 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005228 }
5229 break;
5230
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005231 case ISN_ANYINDEX:
5232 case ISN_ANYSLICE:
5233 {
5234 int is_slice = iptr->isn_type == ISN_ANYSLICE;
5235 typval_T *var1, *var2;
5236 int res;
5237
5238 // index: composite is at stack-2, index at stack-1
5239 // slice: composite is at stack-3, indexes at stack-2 and
5240 // stack-1
5241 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02005242 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005243 if (check_can_index(tv, TRUE, TRUE) == FAIL)
5244 goto on_error;
5245 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
5246 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
Bram Moolenaar6601b622021-01-13 21:47:15 +01005247 res = eval_index_inner(tv, is_slice, var1, var2,
5248 FALSE, NULL, -1, TRUE);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005249 clear_tv(var1);
5250 if (is_slice)
5251 clear_tv(var2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005252 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005253 if (res == FAIL)
5254 goto on_error;
5255 }
5256 break;
5257
Bram Moolenaar9af78762020-06-16 11:34:42 +02005258 case ISN_SLICE:
5259 {
5260 list_T *list;
5261 int count = iptr->isn_arg.number;
5262
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02005263 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02005264 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02005265 list = tv->vval.v_list;
5266
5267 // no error for short list, expect it to be checked earlier
5268 if (list != NULL && list->lv_len >= count)
5269 {
5270 list_T *newlist = list_slice(list,
5271 count, list->lv_len - 1);
5272
5273 if (newlist != NULL)
5274 {
5275 list_unref(list);
5276 tv->vval.v_list = newlist;
5277 ++newlist->lv_refcount;
5278 }
5279 }
5280 }
5281 break;
5282
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005283 case ISN_GETITEM:
5284 {
5285 listitem_T *li;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02005286 getitem_T *gi = &iptr->isn_arg.getitem;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005287
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02005288 // Get list item: list is at stack-1, push item.
5289 // List type and length is checked for when compiling.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02005290 tv = STACK_TV_BOT(-1 - gi->gi_with_op);
5291 li = list_find(tv->vval.v_list, gi->gi_index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005292
Bram Moolenaar35578162021-08-02 19:10:38 +02005293 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005294 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005295 ++ectx->ec_stack.ga_len;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005296 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
Bram Moolenaarf785aa12021-02-11 21:19:34 +01005297
5298 // Useful when used in unpack assignment. Reset at
5299 // ISN_DROP.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02005300 ectx->ec_where.wt_index = gi->gi_index + 1;
LemonBoyc5d27442023-08-19 13:02:35 +02005301 ectx->ec_where.wt_kind = WT_VARIABLE;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005302 }
5303 break;
5304
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005305 case ISN_MEMBER:
5306 {
5307 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005308 char_u *key;
5309 dictitem_T *di;
5310
5311 // dict member: dict is at stack-2, key at stack-1
5312 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02005313 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005314 dict = tv->vval.v_dict;
5315
5316 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02005317 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005318 key = tv->vval.v_string;
Bram Moolenaar086fc9a2020-10-30 18:33:02 +01005319 if (key == NULL)
5320 key = (char_u *)"";
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02005321
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005322 if ((di = dict_find(dict, key, -1)) == NULL)
5323 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005324 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00005325 semsg(_(e_key_not_present_in_dictionary_str), key);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01005326
5327 // If :silent! is used we will continue, make sure the
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005328 // stack contents makes sense and the dict stack is
5329 // updated.
Bram Moolenaar4029cab2020-12-05 18:13:27 +01005330 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005331 --ectx->ec_stack.ga_len;
Bram Moolenaar4029cab2020-12-05 18:13:27 +01005332 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005333 (void) dict_stack_save(tv);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01005334 tv->v_type = VAR_NUMBER;
5335 tv->vval.v_number = 0;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01005336 goto on_fatal_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005337 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005338 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005339 --ectx->ec_stack.ga_len;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005340 // Put the dict used on the dict stack, it might be used by
5341 // a dict function later.
Bram Moolenaar50788ef2020-07-05 16:51:26 +02005342 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005343 if (dict_stack_save(tv) == FAIL)
5344 goto on_fatal_error;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02005345 copy_tv(&di->di_tv, tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005346 }
5347 break;
5348
5349 // dict member with string key
5350 case ISN_STRINGMEMBER:
5351 {
5352 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005353 dictitem_T *di;
5354
5355 tv = STACK_TV_BOT(-1);
5356 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
5357 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005358 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005359 emsg(_(e_dictionary_required));
Bram Moolenaard032f342020-07-18 18:13:02 +02005360 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005361 }
5362 dict = tv->vval.v_dict;
5363
5364 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
5365 == NULL)
5366 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005367 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00005368 semsg(_(e_key_not_present_in_dictionary_str),
Bram Moolenaar381692b2022-02-02 20:01:27 +00005369 iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02005370 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005371 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005372 // Put the dict used on the dict stack, it might be used by
5373 // a dict function later.
5374 if (dict_stack_save(tv) == FAIL)
5375 goto on_fatal_error;
5376
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005377 copy_tv(&di->di_tv, tv);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005378 }
5379 break;
5380
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005381 case ISN_GET_OBJ_MEMBER:
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00005382 case ISN_GET_ITF_MEMBER:
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005383 {
5384 tv = STACK_TV_BOT(-1);
5385 if (tv->v_type != VAR_OBJECT)
5386 {
5387 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaard0200c82023-01-28 15:19:40 +00005388 object_required_error(tv);
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005389 goto on_error;
5390 }
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00005391
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005392 object_T *obj = tv->vval.v_object;
Bram Moolenaarc3f971f2023-03-02 17:38:33 +00005393 if (obj == NULL)
5394 {
5395 SOURCING_LNUM = iptr->isn_lnum;
5396 emsg(_(e_using_null_object));
5397 goto on_error;
5398 }
5399
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00005400 int idx;
5401 if (iptr->isn_type == ISN_GET_OBJ_MEMBER)
Ernie Rael18143d32023-09-04 22:30:41 +02005402 idx = iptr->isn_arg.classmember.cm_idx;
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00005403 else
5404 {
5405 idx = iptr->isn_arg.classmember.cm_idx;
5406 // convert the interface index to the object index
5407 idx = object_index_from_itf_index(
Ernie Rael18143d32023-09-04 22:30:41 +02005408 iptr->isn_arg.classmember.cm_class,
Yegappan Lakshmanan5a05d372023-09-29 19:43:11 +02005409 FALSE, idx, obj->obj_class);
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00005410 }
5411
Ernie Rael18143d32023-09-04 22:30:41 +02005412 // The members are located right after the object struct.
Yegappan Lakshmanan5a05d372023-09-29 19:43:11 +02005413 typval_T *mtv = ((typval_T *)(obj + 1)) + idx;
Bram Moolenaar590162c2022-12-24 21:24:06 +00005414 copy_tv(mtv, tv);
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005415
5416 // Unreference the object after getting the member, it may
5417 // be freed.
5418 object_unref(obj);
5419 }
5420 break;
5421
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005422 case ISN_STORE_THIS:
5423 {
5424 int idx = iptr->isn_arg.number;
5425 object_T *obj = STACK_TV_VAR(0)->vval.v_object;
5426 // the members are located right after the object struct
5427 typval_T *mtv = ((typval_T *)(obj + 1)) + idx;
5428 clear_tv(mtv);
5429 *mtv = *STACK_TV_BOT(-1);
5430 --ectx->ec_stack.ga_len;
5431 }
5432 break;
5433
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005434 case ISN_CLEARDICT:
5435 dict_stack_drop();
5436 break;
5437
5438 case ISN_USEDICT:
5439 {
5440 typval_T *dict_tv = dict_stack_get_tv();
5441
5442 // Turn "dict.Func" into a partial for "Func" bound to
5443 // "dict". Don't do this when "Func" is already a partial
5444 // that was bound explicitly (pt_auto is FALSE).
5445 tv = STACK_TV_BOT(-1);
5446 if (dict_tv != NULL
5447 && dict_tv->v_type == VAR_DICT
5448 && dict_tv->vval.v_dict != NULL
5449 && (tv->v_type == VAR_FUNC
5450 || (tv->v_type == VAR_PARTIAL
5451 && (tv->vval.v_partial->pt_auto
5452 || tv->vval.v_partial->pt_dict == NULL))))
5453 dict_tv->vval.v_dict =
5454 make_partial(dict_tv->vval.v_dict, tv);
5455 dict_stack_drop();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005456 }
5457 break;
5458
5459 case ISN_NEGATENR:
5460 tv = STACK_TV_BOT(-1);
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00005461 // CHECKTYPE should have checked the variable type
Bram Moolenaarc58164c2020-03-29 18:40:30 +02005462 if (tv->v_type == VAR_FLOAT)
5463 tv->vval.v_float = -tv->vval.v_float;
5464 else
Bram Moolenaarc58164c2020-03-29 18:40:30 +02005465 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005466 break;
5467
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005468 case ISN_CHECKTYPE:
5469 {
5470 checktype_T *ct = &iptr->isn_arg.type;
Bram Moolenaarb1040dc2022-05-18 11:00:48 +01005471 int r;
LemonBoyc5d27442023-08-19 13:02:35 +02005472 where_T where = WHERE_INIT;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005473
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01005474 tv = STACK_TV_BOT((int)ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02005475 SOURCING_LNUM = iptr->isn_lnum;
LemonBoyc5d27442023-08-19 13:02:35 +02005476 if (ct->ct_arg_idx > 0)
5477 {
5478 where.wt_index = ct->ct_arg_idx;
5479 where.wt_kind = ct->ct_is_var ? WT_VARIABLE : WT_ARGUMENT;
LemonBoyc5d27442023-08-19 13:02:35 +02005480 }
LemonBoyf244b2f2023-08-19 16:02:04 +02005481 where.wt_func_name = ectx->ec_where.wt_func_name;
LemonBoyc5d27442023-08-19 13:02:35 +02005482 r = check_typval_type(ct->ct_type, tv, where);
Bram Moolenaarb1040dc2022-05-18 11:00:48 +01005483 if (r == FAIL)
5484 goto on_error;
Bram Moolenaar5e654232020-09-16 15:22:00 +02005485
5486 // number 0 is FALSE, number 1 is TRUE
5487 if (tv->v_type == VAR_NUMBER
5488 && ct->ct_type->tt_type == VAR_BOOL
5489 && (tv->vval.v_number == 0
5490 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005491 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02005492 tv->v_type = VAR_BOOL;
5493 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02005494 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005495 }
5496 }
5497 break;
5498
Bram Moolenaar9af78762020-06-16 11:34:42 +02005499 case ISN_CHECKLEN:
5500 {
5501 int min_len = iptr->isn_arg.checklen.cl_min_len;
5502 list_T *list = NULL;
5503
5504 tv = STACK_TV_BOT(-1);
5505 if (tv->v_type == VAR_LIST)
5506 list = tv->vval.v_list;
5507 if (list == NULL || list->lv_len < min_len
5508 || (list->lv_len > min_len
5509 && !iptr->isn_arg.checklen.cl_more_OK))
5510 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005511 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005512 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02005513 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02005514 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02005515 }
5516 }
5517 break;
5518
Bram Moolenaaraa210a32021-01-02 15:41:03 +01005519 case ISN_SETTYPE:
Bram Moolenaar381692b2022-02-02 20:01:27 +00005520 set_tv_type(STACK_TV_BOT(-1), iptr->isn_arg.type.ct_type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01005521 break;
5522
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005523 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005524 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005525 {
5526 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005527 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005528
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005529 if (iptr->isn_type == ISN_2BOOL)
5530 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005531 tv = STACK_TV_BOT(iptr->isn_arg.tobool.offset);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005532 n = tv2bool(tv);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005533 if (iptr->isn_arg.tobool.invert)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005534 n = !n;
5535 }
5536 else
5537 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005538 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005539 SOURCING_LNUM = iptr->isn_lnum;
5540 n = tv_get_bool_chk(tv, &error);
5541 if (error)
5542 goto on_error;
5543 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005544 clear_tv(tv);
5545 tv->v_type = VAR_BOOL;
5546 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
5547 }
5548 break;
5549
5550 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02005551 case ISN_2STRING_ANY:
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01005552 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005553 if (do_2string(STACK_TV_BOT(iptr->isn_arg.tostring.offset),
5554 iptr->isn_type == ISN_2STRING_ANY,
5555 iptr->isn_arg.tostring.tolerant) == FAIL)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01005556 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005557 break;
5558
Bram Moolenaar08597872020-12-10 19:43:40 +01005559 case ISN_RANGE:
5560 {
5561 exarg_T ea;
5562 char *errormsg;
5563
Bram Moolenaarece0b872021-01-08 20:40:45 +01005564 ea.line2 = 0;
Bram Moolenaard1510ee2021-01-04 16:15:58 +01005565 ea.addr_count = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01005566 ea.addr_type = ADDR_LINES;
5567 ea.cmd = iptr->isn_arg.string;
Bram Moolenaarece0b872021-01-08 20:40:45 +01005568 ea.skip = FALSE;
Bram Moolenaar08597872020-12-10 19:43:40 +01005569 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaarece0b872021-01-08 20:40:45 +01005570 goto on_error;
Bram Moolenaara28639e2021-01-19 22:48:09 +01005571
Bram Moolenaar35578162021-08-02 19:10:38 +02005572 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005573 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005574 ++ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01005575 tv = STACK_TV_BOT(-1);
5576 tv->v_type = VAR_NUMBER;
5577 tv->v_lock = 0;
Bram Moolenaar06651632022-04-27 17:54:25 +01005578 tv->vval.v_number = ea.line2;
Bram Moolenaar08597872020-12-10 19:43:40 +01005579 }
5580 break;
5581
Bram Moolenaarc3516f72020-09-08 22:45:35 +02005582 case ISN_PUT:
5583 {
5584 int regname = iptr->isn_arg.put.put_regname;
5585 linenr_T lnum = iptr->isn_arg.put.put_lnum;
5586 char_u *expr = NULL;
5587 int dir = FORWARD;
5588
Bram Moolenaar08597872020-12-10 19:43:40 +01005589 if (lnum < -2)
5590 {
5591 // line number was put on the stack by ISN_RANGE
5592 tv = STACK_TV_BOT(-1);
5593 curwin->w_cursor.lnum = tv->vval.v_number;
5594 if (lnum == LNUM_VARIABLE_RANGE_ABOVE)
5595 dir = BACKWARD;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005596 --ectx->ec_stack.ga_len;
Bram Moolenaar08597872020-12-10 19:43:40 +01005597 }
5598 else if (lnum == -2)
Bram Moolenaarc3516f72020-09-08 22:45:35 +02005599 // :put! above cursor
5600 dir = BACKWARD;
5601 else if (lnum >= 0)
Bram Moolenaar4e713ba2022-02-07 15:31:37 +00005602 {
5603 curwin->w_cursor.lnum = lnum;
5604 if (lnum == 0)
5605 // check_cursor() below will move to line 1
5606 dir = BACKWARD;
5607 }
Bram Moolenaara28639e2021-01-19 22:48:09 +01005608
5609 if (regname == '=')
5610 {
5611 tv = STACK_TV_BOT(-1);
5612 if (tv->v_type == VAR_STRING)
5613 expr = tv->vval.v_string;
5614 else
5615 {
5616 expr = typval2string(tv, TRUE); // allocates value
5617 clear_tv(tv);
5618 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005619 --ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01005620 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02005621 check_cursor();
5622 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
5623 vim_free(expr);
5624 }
5625 break;
5626
Bram Moolenaar02194d22020-10-24 23:08:38 +02005627 case ISN_CMDMOD:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005628 ectx->ec_funclocal.floc_save_cmdmod = cmdmod;
5629 ectx->ec_funclocal.floc_restore_cmdmod = TRUE;
5630 ectx->ec_funclocal.floc_restore_cmdmod_stacklen =
5631 ectx->ec_stack.ga_len;
Bram Moolenaar02194d22020-10-24 23:08:38 +02005632 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
5633 apply_cmdmod(&cmdmod);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02005634 break;
5635
Bram Moolenaar02194d22020-10-24 23:08:38 +02005636 case ISN_CMDMOD_REV:
5637 // filter regprog is owned by the instruction, don't free it
5638 cmdmod.cmod_filter_regmatch.regprog = NULL;
5639 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005640 cmdmod = ectx->ec_funclocal.floc_save_cmdmod;
5641 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02005642 break;
5643
Bram Moolenaar792f7862020-11-23 08:31:18 +01005644 case ISN_UNPACK:
5645 {
5646 int count = iptr->isn_arg.unpack.unp_count;
5647 int semicolon = iptr->isn_arg.unpack.unp_semicolon;
5648 list_T *l;
5649 listitem_T *li;
5650 int i;
5651
5652 // Check there is a valid list to unpack.
5653 tv = STACK_TV_BOT(-1);
5654 if (tv->v_type != VAR_LIST)
5655 {
5656 SOURCING_LNUM = iptr->isn_lnum;
5657 emsg(_(e_for_argument_must_be_sequence_of_lists));
5658 goto on_error;
5659 }
5660 l = tv->vval.v_list;
5661 if (l == NULL
5662 || l->lv_len < (semicolon ? count - 1 : count))
5663 {
5664 SOURCING_LNUM = iptr->isn_lnum;
5665 emsg(_(e_list_value_does_not_have_enough_items));
5666 goto on_error;
5667 }
5668 else if (!semicolon && l->lv_len > count)
5669 {
5670 SOURCING_LNUM = iptr->isn_lnum;
5671 emsg(_(e_list_value_has_more_items_than_targets));
5672 goto on_error;
5673 }
5674
5675 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar35578162021-08-02 19:10:38 +02005676 if (GA_GROW_FAILS(&ectx->ec_stack, count - 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005677 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005678 ectx->ec_stack.ga_len += count - 1;
Bram Moolenaar792f7862020-11-23 08:31:18 +01005679
5680 // Variable after semicolon gets a list with the remaining
5681 // items.
5682 if (semicolon)
5683 {
5684 list_T *rem_list =
5685 list_alloc_with_items(l->lv_len - count + 1);
5686
5687 if (rem_list == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005688 goto theend;
Bram Moolenaar792f7862020-11-23 08:31:18 +01005689 tv = STACK_TV_BOT(-count);
5690 tv->vval.v_list = rem_list;
5691 ++rem_list->lv_refcount;
5692 tv->v_lock = 0;
5693 li = l->lv_first;
5694 for (i = 0; i < count - 1; ++i)
5695 li = li->li_next;
5696 for (i = 0; li != NULL; ++i)
5697 {
Bram Moolenaar61efa162022-03-18 13:10:48 +00005698 typval_T tvcopy;
5699
5700 copy_tv(&li->li_tv, &tvcopy);
5701 list_set_item(rem_list, i, &tvcopy);
Bram Moolenaar792f7862020-11-23 08:31:18 +01005702 li = li->li_next;
5703 }
5704 --count;
5705 }
5706
5707 // Produce the values in reverse order, first item last.
5708 li = l->lv_first;
5709 for (i = 0; i < count; ++i)
5710 {
5711 tv = STACK_TV_BOT(-i - 1);
5712 copy_tv(&li->li_tv, tv);
5713 li = li->li_next;
5714 }
5715
5716 list_unref(l);
5717 }
5718 break;
5719
Bram Moolenaarb2049902021-01-24 12:53:53 +01005720 case ISN_PROF_START:
5721 case ISN_PROF_END:
5722 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01005723#ifdef FEAT_PROFILE
Bram Moolenaarca16c602022-09-06 18:57:08 +01005724 funccall_T cookie;
5725 ufunc_T *cur_ufunc =
Bram Moolenaarb2049902021-01-24 12:53:53 +01005726 (((dfunc_T *)def_functions.ga_data)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02005727 + ectx->ec_dfunc_idx)->df_ufunc;
Bram Moolenaarb2049902021-01-24 12:53:53 +01005728
Bram Moolenaarca16c602022-09-06 18:57:08 +01005729 cookie.fc_func = cur_ufunc;
Bram Moolenaarb2049902021-01-24 12:53:53 +01005730 if (iptr->isn_type == ISN_PROF_START)
5731 {
5732 func_line_start(&cookie, iptr->isn_lnum);
5733 // if we get here the instruction is executed
5734 func_line_exec(&cookie);
5735 }
5736 else
5737 func_line_end(&cookie);
Bram Moolenaarf002a412021-01-24 13:34:18 +01005738#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01005739 }
5740 break;
5741
Bram Moolenaare99d4222021-06-13 14:01:26 +02005742 case ISN_DEBUG:
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02005743 handle_debug(iptr, ectx);
Bram Moolenaare99d4222021-06-13 14:01:26 +02005744 break;
5745
Bram Moolenaar389df252020-07-09 21:20:47 +02005746 case ISN_SHUFFLE:
5747 {
Bram Moolenaar792f7862020-11-23 08:31:18 +01005748 typval_T tmp_tv;
5749 int item = iptr->isn_arg.shuffle.shfl_item;
5750 int up = iptr->isn_arg.shuffle.shfl_up;
Bram Moolenaar389df252020-07-09 21:20:47 +02005751
5752 tmp_tv = *STACK_TV_BOT(-item);
5753 for ( ; up > 0 && item > 1; --up)
5754 {
5755 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
5756 --item;
5757 }
5758 *STACK_TV_BOT(-item) = tmp_tv;
5759 }
5760 break;
5761
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005762 case ISN_DROP:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005763 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005764 clear_tv(STACK_TV_BOT(0));
LemonBoyc5d27442023-08-19 13:02:35 +02005765 ectx->ec_where = (where_T)WHERE_INIT;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005766 break;
5767 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02005768 continue;
5769
Bram Moolenaard032f342020-07-18 18:13:02 +02005770func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02005771 // Restore previous function. If the frame pointer is where we started
5772 // then there is none and we are done.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005773 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02005774 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02005775
Bram Moolenaar4c137212021-04-19 16:48:48 +02005776 if (func_return(ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02005777 // only fails when out of memory
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005778 goto theend;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02005779 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02005780
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02005781on_error:
Bram Moolenaaraf0df472020-12-02 20:51:22 +01005782 // Jump here for an error that does not require aborting execution.
Bram Moolenaar56602ba2020-12-05 21:22:08 +01005783 // If "emsg_silent" is set then ignore the error, unless it was set
5784 // when calling the function.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005785 if (did_emsg_cumul + did_emsg == ectx->ec_did_emsg_before
Bram Moolenaar56602ba2020-12-05 21:22:08 +01005786 && emsg_silent && did_emsg_def == 0)
Bram Moolenaarf9041332021-01-21 19:41:16 +01005787 {
5788 // If a sequence of instructions causes an error while ":silent!"
5789 // was used, restore the stack length and jump ahead to restoring
5790 // the cmdmod.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005791 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaarf9041332021-01-21 19:41:16 +01005792 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005793 while (ectx->ec_stack.ga_len
5794 > ectx->ec_funclocal.floc_restore_cmdmod_stacklen)
Bram Moolenaarf9041332021-01-21 19:41:16 +01005795 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005796 --ectx->ec_stack.ga_len;
Bram Moolenaarf9041332021-01-21 19:41:16 +01005797 clear_tv(STACK_TV_BOT(0));
5798 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005799 while (ectx->ec_instr[ectx->ec_iidx].isn_type != ISN_CMDMOD_REV)
5800 ++ectx->ec_iidx;
Bram Moolenaarf9041332021-01-21 19:41:16 +01005801 }
Bram Moolenaarcd030c42020-10-30 21:49:40 +01005802 continue;
Bram Moolenaarf9041332021-01-21 19:41:16 +01005803 }
Bram Moolenaaraf0df472020-12-02 20:51:22 +01005804on_fatal_error:
5805 // Jump here for an error that messes up the stack.
Bram Moolenaar171fb922020-10-28 16:54:47 +01005806 // If we are not inside a try-catch started here, abort execution.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005807 if (trylevel <= ectx->ec_trylevel_at_start)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005808 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005809 }
5810
5811done:
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005812 ret = OK;
5813theend:
Bram Moolenaar58779852022-09-06 18:31:14 +01005814 may_invoke_defer_funcs(ectx);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01005815
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005816 dict_stack_clear(dict_stack_len_at_start);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005817 ectx->ec_trylevel_at_start = save_trylevel_at_start;
5818 return ret;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005819}
5820
5821/*
Bram Moolenaar806a2732022-09-04 15:40:36 +01005822 * Execute the instructions from a VAR_INSTR typval and put the result in
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005823 * "rettv".
5824 * Return OK or FAIL.
5825 */
5826 int
5827exe_typval_instr(typval_T *tv, typval_T *rettv)
5828{
5829 ectx_T *ectx = tv->vval.v_instr->instr_ectx;
5830 isn_T *save_instr = ectx->ec_instr;
5831 int save_iidx = ectx->ec_iidx;
5832 int res;
5833
LemonBoyf3b48952022-05-05 13:53:03 +01005834 // Initialize rettv so that it is safe for caller to invoke clear_tv(rettv)
5835 // even when the compilation fails.
5836 rettv->v_type = VAR_UNKNOWN;
5837
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005838 ectx->ec_instr = tv->vval.v_instr->instr_instr;
5839 res = exec_instructions(ectx);
5840 if (res == OK)
5841 {
5842 *rettv = *STACK_TV_BOT(-1);
5843 --ectx->ec_stack.ga_len;
5844 }
5845
5846 ectx->ec_instr = save_instr;
5847 ectx->ec_iidx = save_iidx;
5848
5849 return res;
5850}
5851
5852/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02005853 * Execute the instructions from an ISN_SUBSTITUTE command, which are in
5854 * "substitute_instr".
5855 */
5856 char_u *
5857exe_substitute_instr(void)
5858{
5859 ectx_T *ectx = substitute_instr->subs_ectx;
5860 isn_T *save_instr = ectx->ec_instr;
5861 int save_iidx = ectx->ec_iidx;
5862 char_u *res;
5863
5864 ectx->ec_instr = substitute_instr->subs_instr;
5865 if (exec_instructions(ectx) == OK)
Bram Moolenaar90193e62021-04-04 20:49:50 +02005866 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005867 typval_T *tv = STACK_TV_BOT(-1);
5868
Bram Moolenaar27523602021-06-05 21:36:19 +02005869 res = typval2string(tv, TRUE);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005870 --ectx->ec_stack.ga_len;
5871 clear_tv(tv);
Bram Moolenaar90193e62021-04-04 20:49:50 +02005872 }
5873 else
5874 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005875 substitute_instr->subs_status = FAIL;
5876 res = vim_strsave((char_u *)"");
Bram Moolenaar90193e62021-04-04 20:49:50 +02005877 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005878
Bram Moolenaar4c137212021-04-19 16:48:48 +02005879 ectx->ec_instr = save_instr;
5880 ectx->ec_iidx = save_iidx;
5881
5882 return res;
5883}
5884
5885/*
5886 * Call a "def" function from old Vim script.
5887 * Return OK or FAIL.
5888 */
5889 int
5890call_def_function(
5891 ufunc_T *ufunc,
5892 int argc_arg, // nr of arguments
5893 typval_T *argv, // arguments
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005894 int flags, // DEF_ flags
Bram Moolenaar4c137212021-04-19 16:48:48 +02005895 partial_T *partial, // optional partial for context
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005896 object_T *object, // object, e.g. for this.Func()
Bram Moolenaar58779852022-09-06 18:31:14 +01005897 funccall_T *funccal,
Bram Moolenaar4c137212021-04-19 16:48:48 +02005898 typval_T *rettv) // return value
5899{
5900 ectx_T ectx; // execution context
5901 int argc = argc_arg;
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005902 int partial_argc = partial == NULL
5903 || (flags & DEF_USE_PT_ARGV) == 0
5904 ? 0 : partial->pt_argc;
5905 int total_argc = argc + partial_argc;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005906 typval_T *tv;
5907 int idx;
5908 int ret = FAIL;
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005909 int defcount = ufunc->uf_args.ga_len - total_argc;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005910 sctx_T save_current_sctx = current_sctx;
5911 int did_emsg_before = did_emsg_cumul + did_emsg;
5912 int save_suppress_errthrow = suppress_errthrow;
5913 msglist_T **saved_msg_list = NULL;
5914 msglist_T *private_msg_list = NULL;
5915 int save_emsg_silent_def = emsg_silent_def;
5916 int save_did_emsg_def = did_emsg_def;
5917 int orig_funcdepth;
Bram Moolenaare99d4222021-06-13 14:01:26 +02005918 int orig_nesting_level = ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005919
5920// Get pointer to item in the stack.
5921#undef STACK_TV
5922#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
5923
5924// Get pointer to item at the bottom of the stack, -1 is the bottom.
5925#undef STACK_TV_BOT
5926#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
5927
5928// Get pointer to a local variable on the stack. Negative for arguments.
5929#undef STACK_TV_VAR
5930#define STACK_TV_VAR(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_frame_idx + STACK_FRAME_SIZE + idx)
5931
5932 if (ufunc->uf_def_status == UF_NOT_COMPILED
5933 || ufunc->uf_def_status == UF_COMPILE_ERROR
Bram Moolenaar139575d2022-03-15 19:29:30 +00005934 || (func_needs_compiling(ufunc, get_compile_type(ufunc))
5935 && compile_def_function(ufunc, FALSE,
5936 get_compile_type(ufunc), NULL) == FAIL))
Bram Moolenaar4c137212021-04-19 16:48:48 +02005937 {
5938 if (did_emsg_cumul + did_emsg == did_emsg_before)
5939 semsg(_(e_function_is_not_compiled_str),
5940 printable_func_name(ufunc));
5941 return FAIL;
5942 }
5943
5944 {
5945 // Check the function was really compiled.
5946 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5947 + ufunc->uf_dfunc_idx;
Bram Moolenaarcc341812022-09-19 15:54:34 +01005948 if (dfunc->df_ufunc == NULL)
5949 {
5950 semsg(_(e_function_was_deleted_str), printable_func_name(ufunc));
5951 return FAIL;
5952 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005953 if (INSTRUCTIONS(dfunc) == NULL)
5954 {
5955 iemsg("using call_def_function() on not compiled function");
5956 return FAIL;
5957 }
5958 }
5959
5960 // If depth of calling is getting too high, don't execute the function.
5961 orig_funcdepth = funcdepth_get();
5962 if (funcdepth_increment() == FAIL)
5963 return FAIL;
5964
5965 CLEAR_FIELD(ectx);
5966 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
5967 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
Bram Moolenaar35578162021-08-02 19:10:38 +02005968 if (GA_GROW_FAILS(&ectx.ec_stack, 20))
Bram Moolenaar4c137212021-04-19 16:48:48 +02005969 {
5970 funcdepth_decrement();
5971 return FAIL;
5972 }
5973 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
5974 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
5975 ectx.ec_did_emsg_before = did_emsg_before;
Bram Moolenaare99d4222021-06-13 14:01:26 +02005976 ++ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005977
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005978 idx = total_argc - ufunc->uf_args.ga_len;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005979 if (idx > 0 && ufunc->uf_va_name == NULL)
5980 {
Matvey Tarasovd14bb1a2022-06-29 13:18:27 +01005981 semsg(NGETTEXT(e_one_argument_too_many, e_nr_arguments_too_many,
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005982 idx), idx);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005983 goto failed_early;
5984 }
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005985 idx = total_argc - ufunc->uf_args.ga_len + ufunc->uf_def_args.ga_len;
Bram Moolenaarc6d71532021-06-05 18:49:38 +02005986 if (idx < 0)
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02005987 {
Matvey Tarasovd14bb1a2022-06-29 13:18:27 +01005988 semsg(NGETTEXT(e_one_argument_too_few, e_nr_arguments_too_few,
Bram Moolenaar98aff652022-09-06 21:02:35 +01005989 -idx), -idx);
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02005990 goto failed_early;
5991 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005992
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005993 // Put values from the partial and arguments on the stack, but no more than
5994 // what the function expects. A lambda can be called with more arguments
5995 // than it uses.
5996 for (idx = 0; idx < total_argc
Bram Moolenaar4c137212021-04-19 16:48:48 +02005997 && (ufunc->uf_va_name != NULL || idx < ufunc->uf_args.ga_len);
5998 ++idx)
5999 {
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01006000 int argv_idx = idx - partial_argc;
6001
6002 tv = idx < partial_argc ? partial->pt_argv + idx : argv + argv_idx;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006003 if (idx >= ufunc->uf_args.ga_len - ufunc->uf_def_args.ga_len
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01006004 && tv->v_type == VAR_SPECIAL
6005 && tv->vval.v_number == VVAL_NONE)
Bram Moolenaar4c137212021-04-19 16:48:48 +02006006 {
6007 // Use the default value.
6008 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
6009 }
6010 else
6011 {
Bram Moolenaar47bba532023-01-20 18:49:46 +00006012 int done = FALSE;
6013 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len)
6014 {
6015 type_T *expected = ufunc->uf_arg_types[idx];
6016 if (expected->tt_type == VAR_FLOAT && tv->v_type == VAR_NUMBER)
6017 {
6018 // When a float is expected and a number was given, convert
6019 // the value.
6020 STACK_TV_BOT(0)->v_type = VAR_FLOAT;
6021 STACK_TV_BOT(0)->v_lock = 0;
6022 STACK_TV_BOT(0)->vval.v_float = tv->vval.v_number;
6023 done = TRUE;
6024 }
6025 else if (check_typval_arg_type(expected, tv,
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01006026 NULL, argv_idx + 1) == FAIL)
Bram Moolenaar47bba532023-01-20 18:49:46 +00006027 goto failed_early;
6028 }
6029 if (!done)
6030 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006031 }
6032 ++ectx.ec_stack.ga_len;
6033 }
6034
6035 // Turn varargs into a list. Empty list if no args.
6036 if (ufunc->uf_va_name != NULL)
6037 {
6038 int vararg_count = argc - ufunc->uf_args.ga_len;
6039
6040 if (vararg_count < 0)
6041 vararg_count = 0;
6042 else
6043 argc -= vararg_count;
6044 if (exe_newlist(vararg_count, &ectx) == FAIL)
6045 goto failed_early;
6046
6047 // Check the type of the list items.
6048 tv = STACK_TV_BOT(-1);
6049 if (ufunc->uf_va_type != NULL
6050 && ufunc->uf_va_type != &t_list_any
6051 && ufunc->uf_va_type->tt_member != &t_any
6052 && tv->vval.v_list != NULL)
6053 {
6054 type_T *expected = ufunc->uf_va_type->tt_member;
6055 listitem_T *li = tv->vval.v_list->lv_first;
6056
6057 for (idx = 0; idx < vararg_count; ++idx)
6058 {
6059 if (check_typval_arg_type(expected, &li->li_tv,
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02006060 NULL, argc + idx + 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02006061 goto failed_early;
6062 li = li->li_next;
6063 }
6064 }
6065
6066 if (defcount > 0)
6067 // Move varargs list to below missing default arguments.
6068 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
6069 --ectx.ec_stack.ga_len;
6070 }
6071
6072 // Make space for omitted arguments, will store default value below.
6073 // Any varargs list goes after them.
6074 if (defcount > 0)
6075 for (idx = 0; idx < defcount; ++idx)
6076 {
6077 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
6078 ++ectx.ec_stack.ga_len;
6079 }
6080 if (ufunc->uf_va_name != NULL)
6081 ++ectx.ec_stack.ga_len;
6082
6083 // Frame pointer points to just after arguments.
6084 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
6085 ectx.ec_initial_frame_idx = ectx.ec_frame_idx;
6086
6087 {
6088 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6089 + ufunc->uf_dfunc_idx;
6090 ufunc_T *base_ufunc = dfunc->df_ufunc;
6091
6092 // "uf_partial" is on the ufunc that "df_ufunc" points to, as is done
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006093 // by copy_lambda_to_global_func().
Bram Moolenaar4c137212021-04-19 16:48:48 +02006094 if (partial != NULL || base_ufunc->uf_partial != NULL)
6095 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006096 ectx.ec_outer_ref = ALLOC_CLEAR_ONE(outer_ref_T);
6097 if (ectx.ec_outer_ref == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02006098 goto failed_early;
6099 if (partial != NULL)
6100 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00006101 outer_T *outer = get_pt_outer(partial);
6102
Bram Moolenaar6d313be2022-09-22 16:36:25 +01006103 if (outer->out_stack == NULL && outer->out_loop_size == 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02006104 {
Bram Moolenaar6d313be2022-09-22 16:36:25 +01006105 // no stack was set
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00006106 if (current_ectx != NULL)
6107 {
6108 if (current_ectx->ec_outer_ref != NULL
6109 && current_ectx->ec_outer_ref->or_outer != NULL)
6110 ectx.ec_outer_ref->or_outer =
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006111 current_ectx->ec_outer_ref->or_outer;
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00006112 }
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01006113 // else: should there be an error here?
Bram Moolenaar4c137212021-04-19 16:48:48 +02006114 }
6115 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006116 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00006117 ectx.ec_outer_ref->or_outer = outer;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006118 ++partial->pt_refcount;
6119 ectx.ec_outer_ref->or_partial = partial;
6120 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006121 }
6122 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006123 {
6124 ectx.ec_outer_ref->or_outer = &base_ufunc->uf_partial->pt_outer;
6125 ++base_ufunc->uf_partial->pt_refcount;
6126 ectx.ec_outer_ref->or_partial = base_ufunc->uf_partial;
6127 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006128 }
6129 }
6130
6131 // dummy frame entries
6132 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
6133 {
6134 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
6135 ++ectx.ec_stack.ga_len;
6136 }
6137
6138 {
6139 // Reserve space for local variables and any closure reference count.
6140 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6141 + ufunc->uf_dfunc_idx;
6142
Bram Moolenaar5cd64792021-12-25 18:23:24 +00006143 // Initialize variables to zero. That avoids having to generate
6144 // initializing instructions for "var nr: number", "var x: any", etc.
Bram Moolenaar4c137212021-04-19 16:48:48 +02006145 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaar5cd64792021-12-25 18:23:24 +00006146 {
6147 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
6148 STACK_TV_VAR(idx)->vval.v_number = 0;
6149 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006150 ectx.ec_stack.ga_len += dfunc->df_varcount;
Bram Moolenaarffdaca92022-12-09 21:41:48 +00006151
6152 if (object != NULL)
6153 {
6154 // the object is always the variable at index zero
6155 tv = STACK_TV_VAR(0);
6156 tv->v_type = VAR_OBJECT;
6157 tv->vval.v_object = object;
6158 }
6159
Bram Moolenaar4c137212021-04-19 16:48:48 +02006160 if (dfunc->df_has_closure)
6161 {
Bram Moolenaarcc341812022-09-19 15:54:34 +01006162 // Initialize the variable that counts how many closures were
6163 // created. This is used in handle_closure_in_use().
Bram Moolenaar4c137212021-04-19 16:48:48 +02006164 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
6165 STACK_TV_VAR(idx)->vval.v_number = 0;
6166 ++ectx.ec_stack.ga_len;
6167 }
6168
6169 ectx.ec_instr = INSTRUCTIONS(dfunc);
6170 }
6171
Bram Moolenaar58779852022-09-06 18:31:14 +01006172 // Store the execution context in funccal, used by invoke_all_defer().
6173 if (funccal != NULL)
6174 funccal->fc_ectx = &ectx;
6175
Bram Moolenaar4c137212021-04-19 16:48:48 +02006176 // Following errors are in the function, not the caller.
6177 // Commands behave like vim9script.
6178 estack_push_ufunc(ufunc, 1);
6179 current_sctx = ufunc->uf_script_ctx;
6180 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
6181
6182 // Use a specific location for storing error messages to be converted to an
6183 // exception.
6184 saved_msg_list = msg_list;
6185 msg_list = &private_msg_list;
6186
6187 // Do turn errors into exceptions.
6188 suppress_errthrow = FALSE;
6189
6190 // Do not delete the function while executing it.
6191 ++ufunc->uf_calls;
6192
6193 // When ":silent!" was used before calling then we still abort the
6194 // function. If ":silent!" is used in the function then we don't.
6195 emsg_silent_def = emsg_silent;
6196 did_emsg_def = 0;
6197
LemonBoyc5d27442023-08-19 13:02:35 +02006198 ectx.ec_where = (where_T)WHERE_INIT;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006199
Bram Moolenaar5800c792022-09-22 17:34:01 +01006200 /*
6201 * Execute the instructions until done.
6202 */
Bram Moolenaar4c137212021-04-19 16:48:48 +02006203 ret = exec_instructions(&ectx);
6204 if (ret == OK)
6205 {
6206 // function finished, get result from the stack.
6207 if (ufunc->uf_ret_type == &t_void)
6208 {
6209 rettv->v_type = VAR_VOID;
6210 }
6211 else
6212 {
6213 tv = STACK_TV_BOT(-1);
6214 *rettv = *tv;
6215 tv->v_type = VAR_UNKNOWN;
6216 }
6217 }
6218
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01006219 // When failed need to unwind the call stack.
Bram Moolenaar58779852022-09-06 18:31:14 +01006220 unwind_def_callstack(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02006221
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02006222 // Deal with any remaining closures, they may be in use somewhere.
6223 if (ectx.ec_funcrefs.ga_len > 0)
Bram Moolenaarf112f302020-12-20 17:47:52 +01006224 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02006225 handle_closure_in_use(&ectx, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00006226 ga_clear(&ectx.ec_funcrefs);
Bram Moolenaarf112f302020-12-20 17:47:52 +01006227 }
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02006228
Bram Moolenaaree8580e2020-08-28 17:19:07 +02006229 estack_pop();
6230 current_sctx = save_current_sctx;
6231
Bram Moolenaar2336c372021-12-06 15:06:54 +00006232 if (--ufunc->uf_calls <= 0 && ufunc->uf_refcount <= 0)
6233 // Function was unreferenced while being used, free it now.
6234 func_clear_free(ufunc, FALSE);
Bram Moolenaarc970e422021-03-17 15:03:04 +01006235
Bram Moolenaar352134b2020-10-17 22:04:08 +02006236 if (*msg_list != NULL && saved_msg_list != NULL)
6237 {
6238 msglist_T **plist = saved_msg_list;
6239
6240 // Append entries from the current msg_list (uncaught exceptions) to
6241 // the saved msg_list.
6242 while (*plist != NULL)
6243 plist = &(*plist)->next;
6244
6245 *plist = *msg_list;
6246 }
6247 msg_list = saved_msg_list;
6248
Bram Moolenaar4c137212021-04-19 16:48:48 +02006249 if (ectx.ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar02194d22020-10-24 23:08:38 +02006250 {
6251 cmdmod.cmod_filter_regmatch.regprog = NULL;
6252 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006253 cmdmod = ectx.ec_funclocal.floc_save_cmdmod;
Bram Moolenaar02194d22020-10-24 23:08:38 +02006254 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +01006255 emsg_silent_def = save_emsg_silent_def;
6256 did_emsg_def += save_did_emsg_def;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02006257
Bram Moolenaaree8580e2020-08-28 17:19:07 +02006258failed_early:
Bram Moolenaar0d1f55c2022-04-05 17:30:29 +01006259 // Free all arguments and local variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006260 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
Bram Moolenaar1936c762022-09-28 15:19:10 +01006261 {
6262 tv = STACK_TV(idx);
6263 if (tv->v_type != VAR_NUMBER && tv->v_type != VAR_UNKNOWN)
6264 clear_tv(tv);
6265 }
Bram Moolenaare99d4222021-06-13 14:01:26 +02006266 ex_nesting_level = orig_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02006267
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006268 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01006269 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006270 if (ectx.ec_outer_ref != NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +01006271 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006272 if (ectx.ec_outer_ref->or_outer_allocated)
6273 vim_free(ectx.ec_outer_ref->or_outer);
6274 partial_unref(ectx.ec_outer_ref->or_partial);
6275 vim_free(ectx.ec_outer_ref);
Bram Moolenaar0186e582021-01-10 18:33:11 +01006276 }
6277
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02006278 // Not sure if this is necessary.
6279 suppress_errthrow = save_suppress_errthrow;
6280
Bram Moolenaarb5841b92021-07-15 18:09:53 +02006281 if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before
6282 && !need_rethrow)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006283 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02006284 printable_func_name(ufunc));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01006285 funcdepth_restore(orig_funcdepth);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006286 return ret;
6287}
6288
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006289/*
Bram Moolenaar58779852022-09-06 18:31:14 +01006290 * Called when a def function has finished (possibly failed).
6291 * Invoke all the function returns to clean up and invoke deferred functions,
6292 * except the toplevel one.
6293 */
6294 void
6295unwind_def_callstack(ectx_T *ectx)
6296{
6297 while (ectx->ec_frame_idx != ectx->ec_initial_frame_idx)
6298 func_return(ectx);
6299}
6300
6301/*
dundargocc57b5bc2022-11-02 13:30:51 +00006302 * Invoke any deferred functions for the top function in "ectx".
Bram Moolenaar58779852022-09-06 18:31:14 +01006303 */
6304 void
6305may_invoke_defer_funcs(ectx_T *ectx)
6306{
6307 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
6308
6309 if (dfunc->df_defer_var_idx > 0)
6310 invoke_defer_funcs(ectx);
6311}
6312
6313/*
Bram Moolenaarcc341812022-09-19 15:54:34 +01006314 * Return loopvarinfo in a printable form in allocated memory.
6315 */
6316 static char_u *
6317printable_loopvarinfo(loopvarinfo_T *lvi)
6318{
6319 garray_T ga;
6320 int depth;
6321
6322 ga_init2(&ga, 1, 100);
6323 for (depth = 0; depth < lvi->lvi_depth; ++depth)
6324 {
6325 if (ga_grow(&ga, 50) == FAIL)
6326 break;
6327 if (lvi->lvi_loop[depth].var_idx == 0)
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +01006328 STRCPY((char *)ga.ga_data + ga.ga_len, " -");
Bram Moolenaarcc341812022-09-19 15:54:34 +01006329 else
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +01006330 vim_snprintf((char *)ga.ga_data + ga.ga_len, 50, " $%d-$%d",
Bram Moolenaarcc341812022-09-19 15:54:34 +01006331 lvi->lvi_loop[depth].var_idx,
6332 lvi->lvi_loop[depth].var_idx
6333 + lvi->lvi_loop[depth].var_count - 1);
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +01006334 ga.ga_len = (int)STRLEN(ga.ga_data);
Bram Moolenaarcc341812022-09-19 15:54:34 +01006335 }
6336 return ga.ga_data;
6337}
6338
6339/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02006340 * List instructions "instr" up to "instr_count" or until ISN_FINISH.
6341 * "ufunc" has the source lines, NULL for the instructions of ISN_SUBSTITUTE.
6342 * "pfx" is prefixed to every line.
6343 */
6344 static void
6345list_instructions(char *pfx, isn_T *instr, int instr_count, ufunc_T *ufunc)
6346{
6347 int line_idx = 0;
6348 int prev_current = 0;
6349 int current;
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02006350 int def_arg_idx = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006351
6352 for (current = 0; current < instr_count; ++current)
6353 {
6354 isn_T *iptr = &instr[current];
6355 char *line;
6356
6357 if (ufunc != NULL)
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02006358 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02006359 while (line_idx < iptr->isn_lnum
6360 && line_idx < ufunc->uf_lines.ga_len)
6361 {
6362 if (current > prev_current)
6363 {
6364 msg_puts("\n\n");
6365 prev_current = current;
6366 }
6367 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
6368 if (line != NULL)
6369 msg(line);
6370 }
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02006371 if (iptr->isn_type == ISN_JUMP_IF_ARG_SET)
6372 {
6373 int first_def_arg = ufunc->uf_args.ga_len
6374 - ufunc->uf_def_args.ga_len;
6375
6376 if (def_arg_idx > 0)
6377 msg_puts("\n\n");
6378 msg_start();
6379 msg_puts(" ");
6380 msg_puts(((char **)(ufunc->uf_args.ga_data))[
6381 first_def_arg + def_arg_idx]);
6382 msg_puts(" = ");
6383 msg_puts(((char **)(ufunc->uf_def_args.ga_data))[def_arg_idx++]);
6384 msg_clr_eos();
6385 msg_end();
6386 }
6387 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006388
6389 switch (iptr->isn_type)
6390 {
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006391 case ISN_CONSTRUCT:
6392 smsg("%s%4d NEW %s size %d", pfx, current,
6393 iptr->isn_arg.construct.construct_class->class_name,
6394 (int)iptr->isn_arg.construct.construct_size);
6395 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006396 case ISN_EXEC:
6397 smsg("%s%4d EXEC %s", pfx, current, iptr->isn_arg.string);
6398 break;
Bram Moolenaar20677332021-06-06 17:02:53 +02006399 case ISN_EXEC_SPLIT:
6400 smsg("%s%4d EXEC_SPLIT %s", pfx, current, iptr->isn_arg.string);
6401 break;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00006402 case ISN_EXECRANGE:
6403 smsg("%s%4d EXECRANGE %s", pfx, current, iptr->isn_arg.string);
6404 break;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02006405 case ISN_LEGACY_EVAL:
6406 smsg("%s%4d EVAL legacy %s", pfx, current,
6407 iptr->isn_arg.string);
6408 break;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02006409 case ISN_REDIRSTART:
6410 smsg("%s%4d REDIR", pfx, current);
6411 break;
6412 case ISN_REDIREND:
6413 smsg("%s%4d REDIR END%s", pfx, current,
6414 iptr->isn_arg.number ? " append" : "");
6415 break;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006416 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02006417#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006418 smsg("%s%4d CEXPR pre %s", pfx, current,
6419 cexpr_get_auname(iptr->isn_arg.number));
Bram Moolenaarb7c97812021-05-05 22:51:39 +02006420#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006421 break;
6422 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02006423#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006424 {
6425 cexprref_T *cer = iptr->isn_arg.cexpr.cexpr_ref;
6426
6427 smsg("%s%4d CEXPR core %s%s \"%s\"", pfx, current,
6428 cexpr_get_auname(cer->cer_cmdidx),
6429 cer->cer_forceit ? "!" : "",
6430 cer->cer_cmdline);
6431 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02006432#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006433 break;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006434 case ISN_INSTR:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006435 smsg("%s%4d INSTR", pfx, current);
6436 list_instructions(" ", iptr->isn_arg.instr, INT_MAX, NULL);
6437 msg(" -------------");
6438 break;
6439 case ISN_SOURCE:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006440 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006441 scriptitem_T *si = SCRIPT_ITEM(iptr->isn_arg.number);
6442
6443 smsg("%s%4d SOURCE %s", pfx, current, si->sn_name);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006444 }
6445 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006446 case ISN_SUBSTITUTE:
6447 {
6448 subs_T *subs = &iptr->isn_arg.subs;
6449
6450 smsg("%s%4d SUBSTITUTE %s", pfx, current, subs->subs_cmd);
6451 list_instructions(" ", subs->subs_instr, INT_MAX, NULL);
6452 msg(" -------------");
6453 }
6454 break;
6455 case ISN_EXECCONCAT:
6456 smsg("%s%4d EXECCONCAT %lld", pfx, current,
6457 (varnumber_T)iptr->isn_arg.number);
6458 break;
6459 case ISN_ECHO:
6460 {
6461 echo_T *echo = &iptr->isn_arg.echo;
6462
6463 smsg("%s%4d %s %d", pfx, current,
6464 echo->echo_with_white ? "ECHO" : "ECHON",
6465 echo->echo_count);
6466 }
6467 break;
6468 case ISN_EXECUTE:
6469 smsg("%s%4d EXECUTE %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02006470 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006471 break;
6472 case ISN_ECHOMSG:
6473 smsg("%s%4d ECHOMSG %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02006474 (varnumber_T)(iptr->isn_arg.number));
6475 break;
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01006476 case ISN_ECHOWINDOW:
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01006477 if (iptr->isn_arg.echowin.ewin_time > 0)
6478 smsg("%s%4d ECHOWINDOW %d (%ld sec)", pfx, current,
6479 iptr->isn_arg.echowin.ewin_count,
6480 iptr->isn_arg.echowin.ewin_time);
6481 else
6482 smsg("%s%4d ECHOWINDOW %d", pfx, current,
6483 iptr->isn_arg.echowin.ewin_count);
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01006484 break;
Bram Moolenaar7de62622021-08-07 15:05:47 +02006485 case ISN_ECHOCONSOLE:
6486 smsg("%s%4d ECHOCONSOLE %lld", pfx, current,
6487 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006488 break;
6489 case ISN_ECHOERR:
6490 smsg("%s%4d ECHOERR %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02006491 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006492 break;
6493 case ISN_LOAD:
6494 {
6495 if (iptr->isn_arg.number < 0)
6496 smsg("%s%4d LOAD arg[%lld]", pfx, current,
6497 (varnumber_T)(iptr->isn_arg.number
6498 + STACK_FRAME_SIZE));
6499 else
6500 smsg("%s%4d LOAD $%lld", pfx, current,
6501 (varnumber_T)(iptr->isn_arg.number));
6502 }
6503 break;
6504 case ISN_LOADOUTER:
6505 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006506 isn_outer_T *outer = &iptr->isn_arg.outer;
6507
6508 if (outer->outer_idx < 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02006509 smsg("%s%4d LOADOUTER level %d arg[%d]", pfx, current,
Bram Moolenaarcc341812022-09-19 15:54:34 +01006510 outer->outer_depth,
6511 outer->outer_idx + STACK_FRAME_SIZE);
6512 else if (outer->outer_depth < 0)
6513 smsg("%s%4d LOADOUTER $%d in loop level %d",
6514 pfx, current,
6515 outer->outer_idx,
6516 -outer->outer_depth);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006517 else
6518 smsg("%s%4d LOADOUTER level %d $%d", pfx, current,
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006519 outer->outer_depth,
6520 outer->outer_idx);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006521 }
6522 break;
6523 case ISN_LOADV:
6524 smsg("%s%4d LOADV v:%s", pfx, current,
6525 get_vim_var_name(iptr->isn_arg.number));
6526 break;
6527 case ISN_LOADSCRIPT:
6528 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006529 scriptref_T *sref = iptr->isn_arg.script.scriptref;
6530 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
6531 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006532
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006533 sv = get_script_svar(sref, -1);
6534 if (sv == NULL)
6535 smsg("%s%4d LOADSCRIPT [deleted] from %s",
6536 pfx, current, si->sn_name);
6537 else
6538 smsg("%s%4d LOADSCRIPT %s-%d from %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02006539 sv->sv_name,
6540 sref->sref_idx,
6541 si->sn_name);
6542 }
6543 break;
6544 case ISN_LOADS:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006545 case ISN_LOADEXPORT:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006546 {
6547 scriptitem_T *si = SCRIPT_ITEM(
6548 iptr->isn_arg.loadstore.ls_sid);
6549
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006550 smsg("%s%4d %s s:%s from %s", pfx, current,
6551 iptr->isn_type == ISN_LOADS ? "LOADS"
6552 : "LOADEXPORT",
6553 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006554 }
6555 break;
6556 case ISN_LOADAUTO:
6557 smsg("%s%4d LOADAUTO %s", pfx, current, iptr->isn_arg.string);
6558 break;
6559 case ISN_LOADG:
6560 smsg("%s%4d LOADG g:%s", pfx, current, iptr->isn_arg.string);
6561 break;
6562 case ISN_LOADB:
6563 smsg("%s%4d LOADB b:%s", pfx, current, iptr->isn_arg.string);
6564 break;
6565 case ISN_LOADW:
6566 smsg("%s%4d LOADW w:%s", pfx, current, iptr->isn_arg.string);
6567 break;
6568 case ISN_LOADT:
6569 smsg("%s%4d LOADT t:%s", pfx, current, iptr->isn_arg.string);
6570 break;
6571 case ISN_LOADGDICT:
6572 smsg("%s%4d LOAD g:", pfx, current);
6573 break;
6574 case ISN_LOADBDICT:
6575 smsg("%s%4d LOAD b:", pfx, current);
6576 break;
6577 case ISN_LOADWDICT:
6578 smsg("%s%4d LOAD w:", pfx, current);
6579 break;
6580 case ISN_LOADTDICT:
6581 smsg("%s%4d LOAD t:", pfx, current);
6582 break;
6583 case ISN_LOADOPT:
6584 smsg("%s%4d LOADOPT %s", pfx, current, iptr->isn_arg.string);
6585 break;
6586 case ISN_LOADENV:
6587 smsg("%s%4d LOADENV %s", pfx, current, iptr->isn_arg.string);
6588 break;
6589 case ISN_LOADREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006590 smsg("%s%4d LOADREG @%c", pfx, current,
6591 (int)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006592 break;
6593
6594 case ISN_STORE:
6595 if (iptr->isn_arg.number < 0)
6596 smsg("%s%4d STORE arg[%lld]", pfx, current,
6597 iptr->isn_arg.number + STACK_FRAME_SIZE);
6598 else
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006599 smsg("%s%4d STORE $%lld", pfx, current,
6600 iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006601 break;
6602 case ISN_STOREOUTER:
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006603 {
6604 isn_outer_T *outer = &iptr->isn_arg.outer;
6605
6606 if (outer->outer_depth == OUTER_LOOP_DEPTH)
6607 smsg("%s%4d STOREOUTER level 1 $%d in loop",
6608 pfx, current, outer->outer_idx);
6609 else
6610 smsg("%s%4d STOREOUTER level %d $%d", pfx, current,
6611 outer->outer_depth, outer->outer_idx);
6612 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006613 break;
6614 case ISN_STOREV:
6615 smsg("%s%4d STOREV v:%s", pfx, current,
6616 get_vim_var_name(iptr->isn_arg.number));
6617 break;
6618 case ISN_STOREAUTO:
6619 smsg("%s%4d STOREAUTO %s", pfx, current, iptr->isn_arg.string);
6620 break;
6621 case ISN_STOREG:
6622 smsg("%s%4d STOREG %s", pfx, current, iptr->isn_arg.string);
6623 break;
6624 case ISN_STOREB:
6625 smsg("%s%4d STOREB %s", pfx, current, iptr->isn_arg.string);
6626 break;
6627 case ISN_STOREW:
6628 smsg("%s%4d STOREW %s", pfx, current, iptr->isn_arg.string);
6629 break;
6630 case ISN_STORET:
6631 smsg("%s%4d STORET %s", pfx, current, iptr->isn_arg.string);
6632 break;
6633 case ISN_STORES:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006634 case ISN_STOREEXPORT:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006635 {
6636 scriptitem_T *si = SCRIPT_ITEM(
6637 iptr->isn_arg.loadstore.ls_sid);
6638
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006639 smsg("%s%4d %s %s in %s", pfx, current,
6640 iptr->isn_type == ISN_STORES
6641 ? "STORES" : "STOREEXPORT",
Bram Moolenaar4c137212021-04-19 16:48:48 +02006642 iptr->isn_arg.loadstore.ls_name, si->sn_name);
6643 }
6644 break;
6645 case ISN_STORESCRIPT:
6646 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006647 scriptref_T *sref = iptr->isn_arg.script.scriptref;
6648 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
6649 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006650
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006651 sv = get_script_svar(sref, -1);
6652 if (sv == NULL)
6653 smsg("%s%4d STORESCRIPT [deleted] in %s",
6654 pfx, current, si->sn_name);
6655 else
6656 smsg("%s%4d STORESCRIPT %s-%d in %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02006657 sv->sv_name,
6658 sref->sref_idx,
6659 si->sn_name);
6660 }
6661 break;
6662 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00006663 case ISN_STOREFUNCOPT:
6664 smsg("%s%4d %s &%s", pfx, current,
6665 iptr->isn_type == ISN_STOREOPT ? "STOREOPT" : "STOREFUNCOPT",
Bram Moolenaar4c137212021-04-19 16:48:48 +02006666 iptr->isn_arg.storeopt.so_name);
6667 break;
6668 case ISN_STOREENV:
6669 smsg("%s%4d STOREENV $%s", pfx, current, iptr->isn_arg.string);
6670 break;
6671 case ISN_STOREREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006672 smsg("%s%4d STOREREG @%c", pfx, current,
6673 (int)iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006674 break;
6675 case ISN_STORENR:
6676 smsg("%s%4d STORE %lld in $%d", pfx, current,
6677 iptr->isn_arg.storenr.stnr_val,
6678 iptr->isn_arg.storenr.stnr_idx);
6679 break;
6680
6681 case ISN_STOREINDEX:
6682 smsg("%s%4d STOREINDEX %s", pfx, current,
Bram Moolenaarf7d1c6e2023-01-16 20:47:57 +00006683 vartype_name(iptr->isn_arg.storeindex.si_vartype));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006684 break;
6685
6686 case ISN_STORERANGE:
6687 smsg("%s%4d STORERANGE", pfx, current);
6688 break;
6689
Bram Moolenaard505d172022-12-18 21:42:55 +00006690 case ISN_LOAD_CLASSMEMBER:
6691 case ISN_STORE_CLASSMEMBER:
6692 {
6693 class_T *cl = iptr->isn_arg.classmember.cm_class;
6694 int idx = iptr->isn_arg.classmember.cm_idx;
6695 ocmember_T *ocm = &cl->class_class_members[idx];
6696 smsg("%s%4d %s CLASSMEMBER %s.%s", pfx, current,
6697 iptr->isn_type == ISN_LOAD_CLASSMEMBER
6698 ? "LOAD" : "STORE",
6699 cl->class_name, ocm->ocm_name);
6700 }
6701 break;
6702
Bram Moolenaar4c137212021-04-19 16:48:48 +02006703 // constants
6704 case ISN_PUSHNR:
6705 smsg("%s%4d PUSHNR %lld", pfx, current,
6706 (varnumber_T)(iptr->isn_arg.number));
6707 break;
6708 case ISN_PUSHBOOL:
6709 case ISN_PUSHSPEC:
6710 smsg("%s%4d PUSH %s", pfx, current,
6711 get_var_special_name(iptr->isn_arg.number));
6712 break;
6713 case ISN_PUSHF:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006714 smsg("%s%4d PUSHF %g", pfx, current, iptr->isn_arg.fnumber);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006715 break;
6716 case ISN_PUSHS:
6717 smsg("%s%4d PUSHS \"%s\"", pfx, current, iptr->isn_arg.string);
6718 break;
6719 case ISN_PUSHBLOB:
6720 {
6721 char_u *r;
6722 char_u numbuf[NUMBUFLEN];
6723 char_u *tofree;
6724
6725 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
6726 smsg("%s%4d PUSHBLOB %s", pfx, current, r);
6727 vim_free(tofree);
6728 }
6729 break;
6730 case ISN_PUSHFUNC:
6731 {
6732 char *name = (char *)iptr->isn_arg.string;
6733
6734 smsg("%s%4d PUSHFUNC \"%s\"", pfx, current,
6735 name == NULL ? "[none]" : name);
6736 }
6737 break;
6738 case ISN_PUSHCHANNEL:
6739#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar397a87a2022-03-20 21:14:15 +00006740 smsg("%s%4d PUSHCHANNEL 0", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006741#endif
6742 break;
6743 case ISN_PUSHJOB:
6744#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar397a87a2022-03-20 21:14:15 +00006745 smsg("%s%4d PUSHJOB \"no process\"", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006746#endif
6747 break;
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00006748 case ISN_PUSHOBJ:
6749 smsg("%s%4d PUSHOBJ null", pfx, current);
6750 break;
6751 case ISN_PUSHCLASS:
6752 smsg("%s%4d PUSHCLASS %s", pfx, current,
Bram Moolenaar30a84472023-02-27 08:07:14 +00006753 iptr->isn_arg.classarg == NULL ? "null"
6754 : (char *)iptr->isn_arg.classarg->class_name);
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00006755 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006756 case ISN_PUSHEXC:
6757 smsg("%s%4d PUSH v:exception", pfx, current);
6758 break;
Bram Moolenaar06b77222022-01-25 15:51:56 +00006759 case ISN_AUTOLOAD:
6760 smsg("%s%4d AUTOLOAD %s", pfx, current, iptr->isn_arg.string);
6761 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006762 case ISN_UNLET:
6763 smsg("%s%4d UNLET%s %s", pfx, current,
6764 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
6765 iptr->isn_arg.unlet.ul_name);
6766 break;
6767 case ISN_UNLETENV:
6768 smsg("%s%4d UNLETENV%s $%s", pfx, current,
6769 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
6770 iptr->isn_arg.unlet.ul_name);
6771 break;
6772 case ISN_UNLETINDEX:
6773 smsg("%s%4d UNLETINDEX", pfx, current);
6774 break;
6775 case ISN_UNLETRANGE:
6776 smsg("%s%4d UNLETRANGE", pfx, current);
6777 break;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02006778 case ISN_LOCKUNLOCK:
6779 smsg("%s%4d LOCKUNLOCK %s", pfx, current, iptr->isn_arg.string);
6780 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006781 case ISN_LOCKCONST:
6782 smsg("%s%4d LOCKCONST", pfx, current);
6783 break;
6784 case ISN_NEWLIST:
6785 smsg("%s%4d NEWLIST size %lld", pfx, current,
Bram Moolenaar1936c762022-09-28 15:19:10 +01006786 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006787 break;
6788 case ISN_NEWDICT:
6789 smsg("%s%4d NEWDICT size %lld", pfx, current,
Bram Moolenaar1936c762022-09-28 15:19:10 +01006790 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006791 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00006792 case ISN_NEWPARTIAL:
6793 smsg("%s%4d NEWPARTIAL", pfx, current);
6794 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006795
6796 // function call
6797 case ISN_BCALL:
6798 {
6799 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
6800
6801 smsg("%s%4d BCALL %s(argc %d)", pfx, current,
6802 internal_func_name(cbfunc->cbf_idx),
6803 cbfunc->cbf_argcount);
6804 }
6805 break;
6806 case ISN_DCALL:
6807 {
6808 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
6809 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
6810 + cdfunc->cdf_idx;
6811
6812 smsg("%s%4d DCALL %s(argc %d)", pfx, current,
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006813 printable_func_name(df->df_ufunc),
6814 cdfunc->cdf_argcount);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006815 }
6816 break;
Bram Moolenaard0200c82023-01-28 15:19:40 +00006817 case ISN_METHODCALL:
6818 {
6819 cmfunc_T *mfunc = iptr->isn_arg.mfunc;
6820
6821 smsg("%s%4d METHODCALL %s.%s(argc %d)", pfx, current,
6822 mfunc->cmf_itf->class_name,
6823 mfunc->cmf_itf->class_obj_methods[
6824 mfunc->cmf_idx]->uf_name,
6825 mfunc->cmf_argcount);
6826 }
6827 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006828 case ISN_UCALL:
6829 {
6830 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
6831
6832 smsg("%s%4d UCALL %s(argc %d)", pfx, current,
6833 cufunc->cuf_name, cufunc->cuf_argcount);
6834 }
6835 break;
6836 case ISN_PCALL:
6837 {
6838 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
6839
6840 smsg("%s%4d PCALL%s (argc %d)", pfx, current,
6841 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
6842 }
6843 break;
6844 case ISN_PCALL_END:
6845 smsg("%s%4d PCALL end", pfx, current);
6846 break;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006847 case ISN_DEFER:
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00006848 case ISN_DEFEROBJ:
6849 smsg("%s%4d %s %d args", pfx, current,
6850 iptr->isn_type == ISN_DEFER ? "DEFER" : "DEFEROBJ",
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006851 (int)iptr->isn_arg.defer.defer_argcount);
6852 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006853 case ISN_RETURN:
6854 smsg("%s%4d RETURN", pfx, current);
6855 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02006856 case ISN_RETURN_VOID:
6857 smsg("%s%4d RETURN void", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006858 break;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006859 case ISN_RETURN_OBJECT:
6860 smsg("%s%4d RETURN object", pfx, current);
6861 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006862 case ISN_FUNCREF:
6863 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006864 funcref_T *funcref = &iptr->isn_arg.funcref;
6865 funcref_extra_T *extra = funcref->fr_extra;
6866 char_u *name;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006867
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006868 if (extra == NULL || extra->fre_func_name == NULL)
Bram Moolenaar38453522021-11-28 22:00:12 +00006869 {
6870 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
6871 + funcref->fr_dfunc_idx;
6872 name = df->df_ufunc->uf_name;
6873 }
6874 else
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006875 name = extra->fre_func_name;
Bram Moolenaar313e4722023-02-08 20:55:27 +00006876 if (extra != NULL && extra->fre_class != NULL)
6877 {
6878 smsg("%s%4d FUNCREF %s.%s", pfx, current,
6879 extra->fre_class->class_name, name);
6880 }
6881 else if (extra == NULL
6882 || extra->fre_loopvar_info.lvi_depth == 0)
6883 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006884 smsg("%s%4d FUNCREF %s", pfx, current, name);
Bram Moolenaar313e4722023-02-08 20:55:27 +00006885 }
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006886 else
Bram Moolenaarcc341812022-09-19 15:54:34 +01006887 {
6888 char_u *info = printable_loopvarinfo(
6889 &extra->fre_loopvar_info);
6890
6891 smsg("%s%4d FUNCREF %s vars %s", pfx, current,
6892 name, info);
6893 vim_free(info);
6894 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006895 }
6896 break;
6897
6898 case ISN_NEWFUNC:
6899 {
Bram Moolenaarcc341812022-09-19 15:54:34 +01006900 newfuncarg_T *arg = iptr->isn_arg.newfunc.nf_arg;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006901
Bram Moolenaarcc341812022-09-19 15:54:34 +01006902 if (arg->nfa_loopvar_info.lvi_depth == 0)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006903 smsg("%s%4d NEWFUNC %s %s", pfx, current,
6904 arg->nfa_lambda, arg->nfa_global);
6905 else
Bram Moolenaarcc341812022-09-19 15:54:34 +01006906 {
6907 char_u *info = printable_loopvarinfo(
6908 &arg->nfa_loopvar_info);
6909
6910 smsg("%s%4d NEWFUNC %s %s vars %s", pfx, current,
6911 arg->nfa_lambda, arg->nfa_global, info);
6912 vim_free(info);
6913 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006914 }
6915 break;
6916
6917 case ISN_DEF:
6918 {
6919 char_u *name = iptr->isn_arg.string;
6920
6921 smsg("%s%4d DEF %s", pfx, current,
6922 name == NULL ? (char_u *)"" : name);
6923 }
6924 break;
6925
6926 case ISN_JUMP:
6927 {
6928 char *when = "?";
6929
6930 switch (iptr->isn_arg.jump.jump_when)
6931 {
6932 case JUMP_ALWAYS:
6933 when = "JUMP";
6934 break;
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02006935 case JUMP_NEVER:
6936 iemsg("JUMP_NEVER should not be used");
6937 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006938 case JUMP_AND_KEEP_IF_TRUE:
6939 when = "JUMP_AND_KEEP_IF_TRUE";
6940 break;
6941 case JUMP_IF_FALSE:
6942 when = "JUMP_IF_FALSE";
6943 break;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01006944 case JUMP_WHILE_FALSE:
6945 when = "JUMP_WHILE_FALSE"; // unused
6946 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006947 case JUMP_IF_COND_FALSE:
6948 when = "JUMP_IF_COND_FALSE";
6949 break;
6950 case JUMP_IF_COND_TRUE:
6951 when = "JUMP_IF_COND_TRUE";
6952 break;
6953 }
6954 smsg("%s%4d %s -> %d", pfx, current, when,
6955 iptr->isn_arg.jump.jump_where);
6956 }
6957 break;
6958
6959 case ISN_JUMP_IF_ARG_SET:
6960 smsg("%s%4d JUMP_IF_ARG_SET arg[%d] -> %d", pfx, current,
6961 iptr->isn_arg.jumparg.jump_arg_off + STACK_FRAME_SIZE,
6962 iptr->isn_arg.jump.jump_where);
6963 break;
6964
Bram Moolenaar65b0d162022-12-13 18:43:22 +00006965 case ISN_JUMP_IF_ARG_NOT_SET:
6966 smsg("%s%4d JUMP_IF_ARG_NOT_SET arg[%d] -> %d", pfx, current,
6967 iptr->isn_arg.jumparg.jump_arg_off + STACK_FRAME_SIZE,
6968 iptr->isn_arg.jump.jump_where);
6969 break;
6970
Bram Moolenaar4c137212021-04-19 16:48:48 +02006971 case ISN_FOR:
6972 {
6973 forloop_T *forloop = &iptr->isn_arg.forloop;
6974
6975 smsg("%s%4d FOR $%d -> %d", pfx, current,
Bram Moolenaarcc341812022-09-19 15:54:34 +01006976 forloop->for_loop_idx, forloop->for_end);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006977 }
6978 break;
6979
Bram Moolenaarb46c0832022-09-15 17:19:37 +01006980 case ISN_ENDLOOP:
6981 {
6982 endloop_T *endloop = &iptr->isn_arg.endloop;
6983
Bram Moolenaarcc341812022-09-19 15:54:34 +01006984 smsg("%s%4d ENDLOOP ref $%d save $%d-$%d depth %d",
6985 pfx, current,
Bram Moolenaarb46c0832022-09-15 17:19:37 +01006986 endloop->end_funcref_idx,
6987 endloop->end_var_idx,
Bram Moolenaarcc341812022-09-19 15:54:34 +01006988 endloop->end_var_idx + endloop->end_var_count - 1,
6989 endloop->end_depth);
Bram Moolenaarb46c0832022-09-15 17:19:37 +01006990 }
6991 break;
6992
6993 case ISN_WHILE:
6994 {
6995 whileloop_T *whileloop = &iptr->isn_arg.whileloop;
6996
6997 smsg("%s%4d WHILE $%d -> %d", pfx, current,
6998 whileloop->while_funcref_idx,
6999 whileloop->while_end);
7000 }
7001 break;
7002
Bram Moolenaar4c137212021-04-19 16:48:48 +02007003 case ISN_TRY:
7004 {
Bram Moolenaar0d807102021-12-21 09:42:09 +00007005 try_T *try = &iptr->isn_arg.tryref;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007006
7007 if (try->try_ref->try_finally == 0)
7008 smsg("%s%4d TRY catch -> %d, endtry -> %d",
7009 pfx, current,
7010 try->try_ref->try_catch,
7011 try->try_ref->try_endtry);
7012 else
7013 smsg("%s%4d TRY catch -> %d, finally -> %d, endtry -> %d",
7014 pfx, current,
7015 try->try_ref->try_catch,
7016 try->try_ref->try_finally,
7017 try->try_ref->try_endtry);
7018 }
7019 break;
7020 case ISN_CATCH:
Bram Moolenaar4c137212021-04-19 16:48:48 +02007021 smsg("%s%4d CATCH", pfx, current);
7022 break;
7023 case ISN_TRYCONT:
7024 {
7025 trycont_T *trycont = &iptr->isn_arg.trycont;
7026
7027 smsg("%s%4d TRY-CONTINUE %d level%s -> %d", pfx, current,
7028 trycont->tct_levels,
7029 trycont->tct_levels == 1 ? "" : "s",
7030 trycont->tct_where);
7031 }
7032 break;
7033 case ISN_FINALLY:
7034 smsg("%s%4d FINALLY", pfx, current);
7035 break;
7036 case ISN_ENDTRY:
7037 smsg("%s%4d ENDTRY", pfx, current);
7038 break;
7039 case ISN_THROW:
7040 smsg("%s%4d THROW", pfx, current);
7041 break;
7042
7043 // expression operations on number
7044 case ISN_OPNR:
7045 case ISN_OPFLOAT:
7046 case ISN_OPANY:
7047 {
7048 char *what;
7049 char *ins;
7050
7051 switch (iptr->isn_arg.op.op_type)
7052 {
7053 case EXPR_MULT: what = "*"; break;
7054 case EXPR_DIV: what = "/"; break;
7055 case EXPR_REM: what = "%"; break;
7056 case EXPR_SUB: what = "-"; break;
7057 case EXPR_ADD: what = "+"; break;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01007058 case EXPR_LSHIFT: what = "<<"; break;
7059 case EXPR_RSHIFT: what = ">>"; break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007060 default: what = "???"; break;
7061 }
7062 switch (iptr->isn_type)
7063 {
7064 case ISN_OPNR: ins = "OPNR"; break;
7065 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
7066 case ISN_OPANY: ins = "OPANY"; break;
7067 default: ins = "???"; break;
7068 }
7069 smsg("%s%4d %s %s", pfx, current, ins, what);
7070 }
7071 break;
7072
7073 case ISN_COMPAREBOOL:
7074 case ISN_COMPARESPECIAL:
Bram Moolenaar7a222242022-03-01 19:23:24 +00007075 case ISN_COMPARENULL:
Bram Moolenaar4c137212021-04-19 16:48:48 +02007076 case ISN_COMPARENR:
7077 case ISN_COMPAREFLOAT:
7078 case ISN_COMPARESTRING:
7079 case ISN_COMPAREBLOB:
7080 case ISN_COMPARELIST:
7081 case ISN_COMPAREDICT:
7082 case ISN_COMPAREFUNC:
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00007083 case ISN_COMPARECLASS:
7084 case ISN_COMPAREOBJECT:
Bram Moolenaar4c137212021-04-19 16:48:48 +02007085 case ISN_COMPAREANY:
7086 {
7087 char *p;
7088 char buf[10];
7089 char *type;
7090
7091 switch (iptr->isn_arg.op.op_type)
7092 {
7093 case EXPR_EQUAL: p = "=="; break;
7094 case EXPR_NEQUAL: p = "!="; break;
7095 case EXPR_GREATER: p = ">"; break;
7096 case EXPR_GEQUAL: p = ">="; break;
7097 case EXPR_SMALLER: p = "<"; break;
7098 case EXPR_SEQUAL: p = "<="; break;
7099 case EXPR_MATCH: p = "=~"; break;
7100 case EXPR_IS: p = "is"; break;
7101 case EXPR_ISNOT: p = "isnot"; break;
7102 case EXPR_NOMATCH: p = "!~"; break;
7103 default: p = "???"; break;
7104 }
7105 STRCPY(buf, p);
7106 if (iptr->isn_arg.op.op_ic == TRUE)
7107 strcat(buf, "?");
7108 switch(iptr->isn_type)
7109 {
7110 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
7111 case ISN_COMPARESPECIAL:
7112 type = "COMPARESPECIAL"; break;
Bram Moolenaar7a222242022-03-01 19:23:24 +00007113 case ISN_COMPARENULL: type = "COMPARENULL"; break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007114 case ISN_COMPARENR: type = "COMPARENR"; break;
7115 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
7116 case ISN_COMPARESTRING:
7117 type = "COMPARESTRING"; break;
7118 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
7119 case ISN_COMPARELIST: type = "COMPARELIST"; break;
7120 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
7121 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00007122 case ISN_COMPARECLASS: type = "COMPARECLASS"; break;
7123 case ISN_COMPAREOBJECT:
7124 type = "COMPAREOBJECT"; break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007125 case ISN_COMPAREANY: type = "COMPAREANY"; break;
7126 default: type = "???"; break;
7127 }
7128
7129 smsg("%s%4d %s %s", pfx, current, type, buf);
7130 }
7131 break;
7132
7133 case ISN_ADDLIST: smsg("%s%4d ADDLIST", pfx, current); break;
7134 case ISN_ADDBLOB: smsg("%s%4d ADDBLOB", pfx, current); break;
7135
7136 // expression operations
LemonBoy372bcce2022-04-25 12:43:20 +01007137 case ISN_CONCAT:
7138 smsg("%s%4d CONCAT size %lld", pfx, current,
7139 (varnumber_T)(iptr->isn_arg.number));
7140 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007141 case ISN_STRINDEX: smsg("%s%4d STRINDEX", pfx, current); break;
7142 case ISN_STRSLICE: smsg("%s%4d STRSLICE", pfx, current); break;
7143 case ISN_BLOBINDEX: smsg("%s%4d BLOBINDEX", pfx, current); break;
7144 case ISN_BLOBSLICE: smsg("%s%4d BLOBSLICE", pfx, current); break;
7145 case ISN_LISTAPPEND: smsg("%s%4d LISTAPPEND", pfx, current); break;
7146 case ISN_BLOBAPPEND: smsg("%s%4d BLOBAPPEND", pfx, current); break;
7147 case ISN_LISTINDEX: smsg("%s%4d LISTINDEX", pfx, current); break;
7148 case ISN_LISTSLICE: smsg("%s%4d LISTSLICE", pfx, current); break;
7149 case ISN_ANYINDEX: smsg("%s%4d ANYINDEX", pfx, current); break;
7150 case ISN_ANYSLICE: smsg("%s%4d ANYSLICE", pfx, current); break;
7151 case ISN_SLICE: smsg("%s%4d SLICE %lld",
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02007152 pfx, current, iptr->isn_arg.number); break;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02007153 case ISN_GETITEM: smsg("%s%4d ITEM %lld%s", pfx, current,
7154 iptr->isn_arg.getitem.gi_index,
7155 iptr->isn_arg.getitem.gi_with_op ?
7156 " with op" : ""); break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007157 case ISN_MEMBER: smsg("%s%4d MEMBER", pfx, current); break;
7158 case ISN_STRINGMEMBER: smsg("%s%4d MEMBER %s", pfx, current,
7159 iptr->isn_arg.string); break;
Yegappan Lakshmanan5a05d372023-09-29 19:43:11 +02007160 case ISN_GET_OBJ_MEMBER: smsg("%s%4d OBJ_MEMBER %d", pfx, current,
7161 (int)iptr->isn_arg.classmember.cm_idx);
Ernie Rael00df69e2023-09-05 07:38:09 +02007162 break;
Yegappan Lakshmanan5a05d372023-09-29 19:43:11 +02007163 case ISN_GET_ITF_MEMBER: smsg("%s%4d ITF_MEMBER %d on %s",
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00007164 pfx, current,
7165 (int)iptr->isn_arg.classmember.cm_idx,
Yegappan Lakshmanan5a05d372023-09-29 19:43:11 +02007166 iptr->isn_arg.classmember.cm_class->class_name);
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00007167 break;
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00007168 case ISN_STORE_THIS: smsg("%s%4d STORE_THIS %d", pfx, current,
Bram Moolenaarffdaca92022-12-09 21:41:48 +00007169 (int)iptr->isn_arg.number); break;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02007170 case ISN_CLEARDICT: smsg("%s%4d CLEARDICT", pfx, current); break;
7171 case ISN_USEDICT: smsg("%s%4d USEDICT", pfx, current); break;
7172
Bram Moolenaar4c137212021-04-19 16:48:48 +02007173 case ISN_NEGATENR: smsg("%s%4d NEGATENR", pfx, current); break;
7174
Bram Moolenaar4c137212021-04-19 16:48:48 +02007175 case ISN_CHECKTYPE:
7176 {
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01007177 checktype_T *ct = &iptr->isn_arg.type;
Bram Moolenaarc6951a72022-12-29 20:56:24 +00007178 char *tofree = NULL;
7179 char *typename;
7180
7181 if (ct->ct_type->tt_type == VAR_FLOAT
7182 && (ct->ct_type->tt_flags & TTFLAG_NUMBER_OK))
7183 typename = "float|number";
7184 else
7185 typename = type_name(ct->ct_type, &tofree);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007186
7187 if (ct->ct_arg_idx == 0)
7188 smsg("%s%4d CHECKTYPE %s stack[%d]", pfx, current,
Bram Moolenaarc6951a72022-12-29 20:56:24 +00007189 typename,
Bram Moolenaar4c137212021-04-19 16:48:48 +02007190 (int)ct->ct_off);
7191 else
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01007192 smsg("%s%4d CHECKTYPE %s stack[%d] %s %d",
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02007193 pfx, current,
Bram Moolenaarc6951a72022-12-29 20:56:24 +00007194 typename,
Bram Moolenaar4c137212021-04-19 16:48:48 +02007195 (int)ct->ct_off,
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01007196 ct->ct_is_var ? "var": "arg",
Bram Moolenaar4c137212021-04-19 16:48:48 +02007197 (int)ct->ct_arg_idx);
7198 vim_free(tofree);
7199 break;
7200 }
7201 case ISN_CHECKLEN: smsg("%s%4d CHECKLEN %s%d", pfx, current,
7202 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
7203 iptr->isn_arg.checklen.cl_min_len);
7204 break;
7205 case ISN_SETTYPE:
7206 {
7207 char *tofree;
7208
7209 smsg("%s%4d SETTYPE %s", pfx, current,
7210 type_name(iptr->isn_arg.type.ct_type, &tofree));
7211 vim_free(tofree);
7212 break;
7213 }
7214 case ISN_COND2BOOL: smsg("%s%4d COND2BOOL", pfx, current); break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007215 case ISN_2BOOL: if (iptr->isn_arg.tobool.invert)
7216 smsg("%s%4d INVERT %d (!val)", pfx, current,
7217 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007218 else
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007219 smsg("%s%4d 2BOOL %d (!!val)", pfx, current,
7220 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007221 break;
7222 case ISN_2STRING: smsg("%s%4d 2STRING stack[%lld]", pfx, current,
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007223 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02007224 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007225 case ISN_2STRING_ANY: smsg("%s%4d 2STRING_ANY stack[%lld]",
7226 pfx, current,
7227 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02007228 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007229 case ISN_RANGE: smsg("%s%4d RANGE %s", pfx, current,
7230 iptr->isn_arg.string);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007231 break;
7232 case ISN_PUT:
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01007233 if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE)
Bram Moolenaar4c137212021-04-19 16:48:48 +02007234 smsg("%s%4d PUT %c above range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007235 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007236 else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE)
7237 smsg("%s%4d PUT %c range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007238 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007239 else
7240 smsg("%s%4d PUT %c %ld", pfx, current,
7241 iptr->isn_arg.put.put_regname,
7242 (long)iptr->isn_arg.put.put_lnum);
7243 break;
7244
Bram Moolenaar4c137212021-04-19 16:48:48 +02007245 case ISN_CMDMOD:
7246 {
7247 char_u *buf;
7248 size_t len = produce_cmdmods(
7249 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
7250
7251 buf = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02007252 if (likely(buf != NULL))
Bram Moolenaar4c137212021-04-19 16:48:48 +02007253 {
7254 (void)produce_cmdmods(
7255 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
7256 smsg("%s%4d CMDMOD %s", pfx, current, buf);
7257 vim_free(buf);
7258 }
7259 break;
7260 }
7261 case ISN_CMDMOD_REV: smsg("%s%4d CMDMOD_REV", pfx, current); break;
7262
7263 case ISN_PROF_START:
Bram Moolenaare99d4222021-06-13 14:01:26 +02007264 smsg("%s%4d PROFILE START line %d", pfx, current,
7265 iptr->isn_lnum);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007266 break;
7267
7268 case ISN_PROF_END:
7269 smsg("%s%4d PROFILE END", pfx, current);
7270 break;
7271
Bram Moolenaare99d4222021-06-13 14:01:26 +02007272 case ISN_DEBUG:
Bram Moolenaar8cec9272021-06-23 20:20:53 +02007273 smsg("%s%4d DEBUG line %d-%d varcount %lld", pfx, current,
7274 iptr->isn_arg.debug.dbg_break_lnum + 1,
7275 iptr->isn_lnum,
7276 iptr->isn_arg.debug.dbg_var_names_len);
Bram Moolenaare99d4222021-06-13 14:01:26 +02007277 break;
7278
Bram Moolenaar4c137212021-04-19 16:48:48 +02007279 case ISN_UNPACK: smsg("%s%4d UNPACK %d%s", pfx, current,
7280 iptr->isn_arg.unpack.unp_count,
7281 iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : "");
7282 break;
7283 case ISN_SHUFFLE: smsg("%s%4d SHUFFLE %d up %d", pfx, current,
7284 iptr->isn_arg.shuffle.shfl_item,
7285 iptr->isn_arg.shuffle.shfl_up);
7286 break;
7287 case ISN_DROP: smsg("%s%4d DROP", pfx, current); break;
7288
7289 case ISN_FINISH: // End of list of instructions for ISN_SUBSTITUTE.
7290 return;
7291 }
7292
7293 out_flush(); // output one line at a time
7294 ui_breakcheck();
7295 if (got_int)
7296 break;
7297 }
7298}
7299
7300/*
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02007301 * Handle command line completion for the :disassemble command.
7302 */
7303 void
7304set_context_in_disassemble_cmd(expand_T *xp, char_u *arg)
7305{
7306 char_u *p;
7307
7308 // Default: expand user functions, "debug" and "profile"
7309 xp->xp_context = EXPAND_DISASSEMBLE;
7310 xp->xp_pattern = arg;
7311
7312 // first argument already typed: only user function names
7313 if (*arg != NUL && *(p = skiptowhite(arg)) != NUL)
7314 {
7315 xp->xp_context = EXPAND_USER_FUNC;
7316 xp->xp_pattern = skipwhite(p);
7317 }
7318}
7319
7320/*
7321 * Function given to ExpandGeneric() to obtain the list of :disassemble
7322 * arguments.
7323 */
7324 char_u *
7325get_disassemble_argument(expand_T *xp, int idx)
7326{
7327 if (idx == 0)
7328 return (char_u *)"debug";
7329 if (idx == 1)
7330 return (char_u *)"profile";
7331 return get_user_func_name(xp, idx - 2);
7332}
7333
7334/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01007335 * ":disassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01007336 * We don't really need this at runtime, but we do have tests that require it,
7337 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007338 */
7339 void
7340ex_disassemble(exarg_T *eap)
7341{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01007342 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01007343 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007344 dfunc_T *dfunc;
Bram Moolenaardafef512022-05-21 21:55:55 +01007345 isn_T *instr = NULL; // init to shut up gcc warning
7346 int instr_count = 0; // init to shut up gcc warning
Bram Moolenaar5a01caa2022-05-21 18:56:58 +01007347 compiletype_T compile_type = CT_NONE;
Bram Moolenaare99d4222021-06-13 14:01:26 +02007348
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01007349 ufunc = find_func_by_name(arg, &compile_type);
Bram Moolenaara26b9702020-04-18 19:53:28 +02007350 if (ufunc == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007351 return;
Bram Moolenaare99d4222021-06-13 14:01:26 +02007352 if (func_needs_compiling(ufunc, compile_type)
7353 && compile_def_function(ufunc, FALSE, compile_type, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02007354 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007355 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007356 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007357 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007358 return;
7359 }
Bram Moolenaar6db660b2021-08-01 14:08:54 +02007360 msg((char *)printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007361
7362 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
Bram Moolenaare99d4222021-06-13 14:01:26 +02007363 switch (compile_type)
7364 {
7365 case CT_PROFILE:
Bram Moolenaarf002a412021-01-24 13:34:18 +01007366#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02007367 instr = dfunc->df_instr_prof;
7368 instr_count = dfunc->df_instr_prof_count;
7369 break;
Bram Moolenaarf002a412021-01-24 13:34:18 +01007370#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02007371 // FALLTHROUGH
7372 case CT_NONE:
7373 instr = dfunc->df_instr;
7374 instr_count = dfunc->df_instr_count;
7375 break;
7376 case CT_DEBUG:
7377 instr = dfunc->df_instr_debug;
7378 instr_count = dfunc->df_instr_debug_count;
7379 break;
7380 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007381
Bram Moolenaar4c137212021-04-19 16:48:48 +02007382 list_instructions("", instr, instr_count, ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007383}
7384
7385/*
Bram Moolenaar13106602020-10-04 16:06:05 +02007386 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007387 * list, etc. Mostly like what JavaScript does, except that empty list and
7388 * empty dictionary are FALSE.
7389 */
7390 int
7391tv2bool(typval_T *tv)
7392{
7393 switch (tv->v_type)
7394 {
7395 case VAR_NUMBER:
7396 return tv->vval.v_number != 0;
7397 case VAR_FLOAT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007398 return tv->vval.v_float != 0.0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007399 case VAR_PARTIAL:
7400 return tv->vval.v_partial != NULL;
7401 case VAR_FUNC:
7402 case VAR_STRING:
7403 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
7404 case VAR_LIST:
7405 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
7406 case VAR_DICT:
7407 return tv->vval.v_dict != NULL
7408 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
7409 case VAR_BOOL:
7410 case VAR_SPECIAL:
7411 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
7412 case VAR_JOB:
7413#ifdef FEAT_JOB_CHANNEL
7414 return tv->vval.v_job != NULL;
7415#else
7416 break;
7417#endif
7418 case VAR_CHANNEL:
7419#ifdef FEAT_JOB_CHANNEL
7420 return tv->vval.v_channel != NULL;
7421#else
7422 break;
7423#endif
7424 case VAR_BLOB:
7425 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
7426 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02007427 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007428 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02007429 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00007430 case VAR_CLASS:
7431 case VAR_OBJECT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007432 break;
7433 }
7434 return FALSE;
7435}
7436
Bram Moolenaarea2d4072020-11-12 12:08:51 +01007437 void
7438emsg_using_string_as(typval_T *tv, int as_number)
7439{
7440 semsg(_(as_number ? e_using_string_as_number_str
7441 : e_using_string_as_bool_str),
7442 tv->vval.v_string == NULL
7443 ? (char_u *)"" : tv->vval.v_string);
7444}
7445
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007446/*
7447 * If "tv" is a string give an error and return FAIL.
7448 */
7449 int
7450check_not_string(typval_T *tv)
7451{
7452 if (tv->v_type == VAR_STRING)
7453 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +01007454 emsg_using_string_as(tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007455 clear_tv(tv);
7456 return FAIL;
7457 }
7458 return OK;
7459}
7460
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007461
7462#endif // FEAT_EVAL