blob: f237132a896d3543796a1fab4ad209670a9a3700 [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 {
Ernie Raele6c9aa52023-10-06 19:55:52 +02002183 emsg_var_cl_define(e_cannot_access_private_variable_str,
2184 m->ocm_name, 0, cl);
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02002185 status = FAIL;
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002186 }
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002187
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02002188 lidx = m_idx;
2189 }
2190 else
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002191 {
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +02002192 member_not_found_msg(cl, VAR_OBJECT, member, 0);
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002193 status = FAIL;
2194 }
2195 }
2196 else if ((dest_type == VAR_LIST || dest_type == VAR_OBJECT)
2197 && tv_idx->v_type != VAR_NUMBER)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002198 {
2199 emsg(_(e_number_expected));
2200 status = FAIL;
2201 }
2202 }
Bram Moolenaare08be092022-02-17 13:08:26 +00002203
2204 if (status == OK)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002205 {
Bram Moolenaare08be092022-02-17 13:08:26 +00002206 if (dest_type == VAR_LIST)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002207 {
Bram Moolenaare08be092022-02-17 13:08:26 +00002208 list_T *list = tv_dest->vval.v_list;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002209
Bram Moolenaare08be092022-02-17 13:08:26 +00002210 if (list == NULL)
2211 {
2212 emsg(_(e_list_not_set));
2213 return FAIL;
2214 }
2215 if (lidx < 0 && list->lv_len + lidx >= 0)
2216 // negative index is relative to the end
2217 lidx = list->lv_len + lidx;
2218 if (lidx < 0 || lidx > list->lv_len)
2219 {
2220 semsg(_(e_list_index_out_of_range_nr), lidx);
2221 return FAIL;
2222 }
2223 if (lidx < list->lv_len)
2224 {
2225 listitem_T *li = list_find(list, lidx);
2226
2227 if (error_if_locked(li->li_tv.v_lock,
Bram Moolenaar70c43d82022-01-26 21:01:15 +00002228 e_cannot_change_locked_list_item))
Bram Moolenaare08be092022-02-17 13:08:26 +00002229 return FAIL;
2230 // overwrite existing list item
2231 clear_tv(&li->li_tv);
2232 li->li_tv = *tv;
2233 }
2234 else
2235 {
2236 if (error_if_locked(list->lv_lock, e_cannot_change_locked_list))
2237 return FAIL;
2238 // append to list, only fails when out of memory
2239 if (list_append_tv(list, tv) == FAIL)
2240 return NOTDONE;
2241 clear_tv(tv);
2242 }
2243 }
2244 else if (dest_type == VAR_DICT)
2245 {
2246 char_u *key = tv_idx->vval.v_string;
2247 dict_T *dict = tv_dest->vval.v_dict;
2248 dictitem_T *di;
2249
2250 SOURCING_LNUM = iptr->isn_lnum;
2251 if (dict == NULL)
2252 {
2253 emsg(_(e_dictionary_not_set));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002254 return FAIL;
Bram Moolenaare08be092022-02-17 13:08:26 +00002255 }
2256 if (key == NULL)
2257 key = (char_u *)"";
2258 di = dict_find(dict, key, -1);
2259 if (di != NULL)
2260 {
2261 if (error_if_locked(di->di_tv.v_lock,
2262 e_cannot_change_dict_item))
2263 return FAIL;
2264 // overwrite existing value
2265 clear_tv(&di->di_tv);
2266 di->di_tv = *tv;
2267 }
2268 else
2269 {
2270 if (error_if_locked(dict->dv_lock, e_cannot_change_dict))
2271 return FAIL;
2272 // add to dict, only fails when out of memory
2273 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
2274 return NOTDONE;
2275 clear_tv(tv);
2276 }
2277 }
2278 else if (dest_type == VAR_BLOB)
2279 {
Bram Moolenaare08be092022-02-17 13:08:26 +00002280 blob_T *blob = tv_dest->vval.v_blob;
Bram Moolenaar65b0d162022-12-13 18:43:22 +00002281 varnumber_T nr;
2282 int error = FALSE;
2283 int len;
Bram Moolenaare08be092022-02-17 13:08:26 +00002284
2285 if (blob == NULL)
2286 {
2287 emsg(_(e_blob_not_set));
2288 return FAIL;
2289 }
2290 len = blob_len(blob);
2291 if (lidx < 0 && len + lidx >= 0)
2292 // negative index is relative to the end
2293 lidx = len + lidx;
2294
2295 // Can add one byte at the end.
2296 if (lidx < 0 || lidx > len)
2297 {
2298 semsg(_(e_blob_index_out_of_range_nr), lidx);
2299 return FAIL;
2300 }
2301 if (value_check_lock(blob->bv_lock, (char_u *)"blob", FALSE))
2302 return FAIL;
2303 nr = tv_get_number_chk(tv, &error);
2304 if (error)
2305 return FAIL;
2306 blob_set_append(blob, lidx, nr);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002307 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002308 else if (dest_type == VAR_CLASS || dest_type == VAR_OBJECT)
2309 {
Yegappan Lakshmanan3775f772023-09-01 22:05:45 +02002310 typval_T *otv;
Bram Moolenaarf7d1c6e2023-01-16 20:47:57 +00002311
Yegappan Lakshmanan3775f772023-09-01 22:05:45 +02002312 if (dest_type == VAR_OBJECT)
2313 {
2314 object_T *obj = tv_dest->vval.v_object;
2315
2316 otv = (typval_T *)(obj + 1);
2317 class_T *itf = iptr->isn_arg.storeindex.si_class;
2318 if (itf != NULL)
2319 // convert interface member index to class member index
Ernie Rael18143d32023-09-04 22:30:41 +02002320 lidx = object_index_from_itf_index(itf, FALSE, lidx,
Yegappan Lakshmanan5a05d372023-09-29 19:43:11 +02002321 obj->obj_class);
Yegappan Lakshmanan3775f772023-09-01 22:05:45 +02002322 }
2323 else
2324 {
2325 // VAR_CLASS
2326 class_T *class = tv_dest->vval.v_class;
2327 otv = class->class_members_tv;
2328 }
Bram Moolenaarf7d1c6e2023-01-16 20:47:57 +00002329
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002330 clear_tv(&otv[lidx]);
2331 otv[lidx] = *tv;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002332 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002333 else
2334 {
Bram Moolenaare08be092022-02-17 13:08:26 +00002335 status = FAIL;
2336 semsg(_(e_cannot_index_str), vartype_name(dest_type));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002337 }
2338 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002339
2340 clear_tv(tv_idx);
2341 clear_tv(tv_dest);
2342 ectx->ec_stack.ga_len -= 3;
2343 if (status == FAIL)
2344 {
2345 clear_tv(tv);
2346 return FAIL;
2347 }
2348 return OK;
2349}
2350
2351/*
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002352 * Store a value in a list or blob range.
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002353 */
2354 static int
2355execute_storerange(isn_T *iptr, ectx_T *ectx)
2356{
2357 typval_T *tv;
2358 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2359 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2360 typval_T *tv_dest = STACK_TV_BOT(-1);
2361 int status = OK;
2362
2363 // Stack contains:
2364 // -4 value to be stored
2365 // -3 first index or "none"
2366 // -2 second index or "none"
2367 // -1 destination list or blob
2368 tv = STACK_TV_BOT(-4);
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002369 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002370 if (tv_dest->v_type == VAR_LIST)
2371 {
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002372 long n1;
2373 long n2;
2374 listitem_T *li1;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002375
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002376 n1 = (long)tv_get_number_chk(tv_idx1, NULL);
2377 if (tv_idx2->v_type == VAR_SPECIAL
2378 && tv_idx2->vval.v_number == VVAL_NONE)
2379 n2 = list_len(tv_dest->vval.v_list) - 1;
2380 else
2381 n2 = (long)tv_get_number_chk(tv_idx2, NULL);
2382
Bram Moolenaar22ebd172022-04-01 15:26:58 +01002383 li1 = check_range_index_one(tv_dest->vval.v_list, &n1, TRUE, FALSE);
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002384 if (li1 == NULL)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002385 status = FAIL;
2386 else
2387 {
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002388 status = check_range_index_two(tv_dest->vval.v_list,
2389 &n1, li1, &n2, FALSE);
2390 if (status != FAIL)
2391 status = list_assign_range(
2392 tv_dest->vval.v_list,
2393 tv->vval.v_list,
2394 n1,
2395 n2,
2396 tv_idx2->v_type == VAR_SPECIAL,
2397 (char_u *)"=",
2398 (char_u *)"[unknown]");
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002399 }
2400 }
2401 else if (tv_dest->v_type == VAR_BLOB)
2402 {
2403 varnumber_T n1;
2404 varnumber_T n2;
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002405 long bloblen;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002406
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002407 n1 = tv_get_number_chk(tv_idx1, NULL);
2408 if (tv_idx2->v_type == VAR_SPECIAL
2409 && tv_idx2->vval.v_number == VVAL_NONE)
2410 n2 = blob_len(tv_dest->vval.v_blob) - 1;
2411 else
2412 n2 = tv_get_number_chk(tv_idx2, NULL);
2413 bloblen = blob_len(tv_dest->vval.v_blob);
2414
2415 if (check_blob_index(bloblen, n1, FALSE) == FAIL
2416 || check_blob_range(bloblen, n1, n2, FALSE) == FAIL)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002417 status = FAIL;
2418 else
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002419 status = blob_set_range(tv_dest->vval.v_blob, n1, n2, tv);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002420 }
2421 else
2422 {
2423 status = FAIL;
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002424 emsg(_(e_list_or_blob_required));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002425 }
2426
2427 clear_tv(tv_idx1);
2428 clear_tv(tv_idx2);
2429 clear_tv(tv_dest);
2430 ectx->ec_stack.ga_len -= 4;
2431 clear_tv(tv);
2432
2433 return status;
2434}
2435
2436/*
2437 * Unlet item in list or dict variable.
2438 */
2439 static int
2440execute_unletindex(isn_T *iptr, ectx_T *ectx)
2441{
2442 typval_T *tv_idx = STACK_TV_BOT(-2);
2443 typval_T *tv_dest = STACK_TV_BOT(-1);
2444 int status = OK;
2445
2446 // Stack contains:
2447 // -2 index
2448 // -1 dict or list
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002449 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002450 if (tv_dest->v_type == VAR_DICT)
2451 {
2452 // unlet a dict item, index must be a string
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002453 if (tv_idx->v_type != VAR_STRING && tv_idx->v_type != VAR_NUMBER)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002454 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002455 semsg(_(e_expected_str_but_got_str),
2456 vartype_name(VAR_STRING),
2457 vartype_name(tv_idx->v_type));
2458 status = FAIL;
2459 }
2460 else
2461 {
2462 dict_T *d = tv_dest->vval.v_dict;
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002463 char_u *key;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002464 dictitem_T *di = NULL;
2465
2466 if (d != NULL && value_check_lock(
2467 d->dv_lock, NULL, FALSE))
2468 status = FAIL;
2469 else
2470 {
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002471 if (tv_idx->v_type == VAR_STRING)
2472 {
2473 key = tv_idx->vval.v_string;
2474 if (key == NULL)
2475 key = (char_u *)"";
2476 }
2477 else
2478 {
2479 key = tv_get_string(tv_idx);
2480 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002481 if (d != NULL)
2482 di = dict_find(d, key, (int)STRLEN(key));
2483 if (di == NULL)
2484 {
2485 // NULL dict is equivalent to empty dict
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00002486 semsg(_(e_key_not_present_in_dictionary_str), key);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002487 status = FAIL;
2488 }
2489 else if (var_check_fixed(di->di_flags,
2490 NULL, FALSE)
2491 || var_check_ro(di->di_flags,
2492 NULL, FALSE))
2493 status = FAIL;
2494 else
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002495 dictitem_remove(d, di, "unlet");
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002496 }
2497 }
2498 }
2499 else if (tv_dest->v_type == VAR_LIST)
2500 {
2501 // unlet a List item, index must be a number
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002502 if (check_for_number(tv_idx) == FAIL)
2503 {
2504 status = FAIL;
2505 }
2506 else
2507 {
2508 list_T *l = tv_dest->vval.v_list;
2509 long n = (long)tv_idx->vval.v_number;
2510
2511 if (l != NULL && value_check_lock(
2512 l->lv_lock, NULL, FALSE))
2513 status = FAIL;
2514 else
2515 {
2516 listitem_T *li = list_find(l, n);
2517
2518 if (li == NULL)
2519 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002520 semsg(_(e_list_index_out_of_range_nr), n);
2521 status = FAIL;
2522 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002523 else
2524 listitem_remove(l, li);
2525 }
2526 }
2527 }
2528 else
2529 {
2530 status = FAIL;
2531 semsg(_(e_cannot_index_str),
2532 vartype_name(tv_dest->v_type));
2533 }
2534
2535 clear_tv(tv_idx);
2536 clear_tv(tv_dest);
2537 ectx->ec_stack.ga_len -= 2;
2538
2539 return status;
2540}
2541
2542/*
2543 * Unlet a range of items in a list variable.
2544 */
2545 static int
2546execute_unletrange(isn_T *iptr, ectx_T *ectx)
2547{
2548 // Stack contains:
2549 // -3 index1
2550 // -2 index2
2551 // -1 dict or list
2552 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2553 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2554 typval_T *tv_dest = STACK_TV_BOT(-1);
2555 int status = OK;
2556
2557 if (tv_dest->v_type == VAR_LIST)
2558 {
2559 // indexes must be a number
2560 SOURCING_LNUM = iptr->isn_lnum;
2561 if (check_for_number(tv_idx1) == FAIL
2562 || (tv_idx2->v_type != VAR_SPECIAL
2563 && check_for_number(tv_idx2) == FAIL))
2564 {
2565 status = FAIL;
2566 }
2567 else
2568 {
2569 list_T *l = tv_dest->vval.v_list;
2570 long n1 = (long)tv_idx1->vval.v_number;
2571 long n2 = tv_idx2->v_type == VAR_SPECIAL
2572 ? 0 : (long)tv_idx2->vval.v_number;
2573 listitem_T *li;
2574
2575 li = list_find_index(l, &n1);
2576 if (li == NULL)
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002577 {
2578 semsg(_(e_list_index_out_of_range_nr),
2579 (long)tv_idx1->vval.v_number);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002580 status = FAIL;
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002581 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002582 else
2583 {
2584 if (n1 < 0)
2585 n1 = list_idx_of_item(l, li);
2586 if (n2 < 0)
2587 {
2588 listitem_T *li2 = list_find(l, n2);
2589
2590 if (li2 == NULL)
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002591 {
2592 semsg(_(e_list_index_out_of_range_nr), n2);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002593 status = FAIL;
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002594 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002595 else
2596 n2 = list_idx_of_item(l, li2);
2597 }
2598 if (status != FAIL
2599 && tv_idx2->v_type != VAR_SPECIAL
2600 && n2 < n1)
2601 {
2602 semsg(_(e_list_index_out_of_range_nr), n2);
2603 status = FAIL;
2604 }
Bram Moolenaar6b8c7ba2022-03-20 17:46:06 +00002605 if (status != FAIL)
2606 list_unlet_range(l, li, n1,
2607 tv_idx2->v_type != VAR_SPECIAL, n2);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002608 }
2609 }
2610 }
2611 else
2612 {
2613 status = FAIL;
2614 SOURCING_LNUM = iptr->isn_lnum;
2615 semsg(_(e_cannot_index_str),
2616 vartype_name(tv_dest->v_type));
2617 }
2618
2619 clear_tv(tv_idx1);
2620 clear_tv(tv_idx2);
2621 clear_tv(tv_dest);
2622 ectx->ec_stack.ga_len -= 3;
2623
2624 return status;
2625}
2626
2627/*
2628 * Top of a for loop.
2629 */
2630 static int
2631execute_for(isn_T *iptr, ectx_T *ectx)
2632{
2633 typval_T *tv;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002634 int jump = FALSE;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002635 typval_T *ltv = STACK_TV_BOT(-1);
2636 typval_T *idxtv =
Bram Moolenaarcc341812022-09-19 15:54:34 +01002637 STACK_TV_VAR(iptr->isn_arg.forloop.for_loop_idx);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002638
2639 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
2640 return FAIL;
2641 if (ltv->v_type == VAR_LIST)
2642 {
2643 list_T *list = ltv->vval.v_list;
2644
2645 // push the next item from the list
2646 ++idxtv->vval.v_number;
2647 if (list == NULL
2648 || idxtv->vval.v_number >= list->lv_len)
2649 {
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002650 jump = TRUE;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002651 }
2652 else if (list->lv_first == &range_list_item)
2653 {
2654 // non-materialized range() list
2655 tv = STACK_TV_BOT(0);
2656 tv->v_type = VAR_NUMBER;
2657 tv->v_lock = 0;
2658 tv->vval.v_number = list_find_nr(
2659 list, idxtv->vval.v_number, NULL);
2660 ++ectx->ec_stack.ga_len;
2661 }
2662 else
2663 {
2664 listitem_T *li = list_find(list,
2665 idxtv->vval.v_number);
2666
2667 copy_tv(&li->li_tv, STACK_TV_BOT(0));
2668 ++ectx->ec_stack.ga_len;
2669 }
2670 }
2671 else if (ltv->v_type == VAR_STRING)
2672 {
2673 char_u *str = ltv->vval.v_string;
2674
2675 // The index is for the last byte of the previous
2676 // character.
2677 ++idxtv->vval.v_number;
2678 if (str == NULL || str[idxtv->vval.v_number] == NUL)
2679 {
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002680 jump = TRUE;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002681 }
2682 else
2683 {
2684 int clen = mb_ptr2len(str + idxtv->vval.v_number);
2685
2686 // Push the next character from the string.
2687 tv = STACK_TV_BOT(0);
2688 tv->v_type = VAR_STRING;
2689 tv->vval.v_string = vim_strnsave(
2690 str + idxtv->vval.v_number, clen);
2691 ++ectx->ec_stack.ga_len;
2692 idxtv->vval.v_number += clen - 1;
2693 }
2694 }
2695 else if (ltv->v_type == VAR_BLOB)
2696 {
2697 blob_T *blob = ltv->vval.v_blob;
2698
2699 // When we get here the first time make a copy of the
2700 // blob, so that the iteration still works when it is
2701 // changed.
2702 if (idxtv->vval.v_number == -1 && blob != NULL)
2703 {
2704 blob_copy(blob, ltv);
2705 blob_unref(blob);
2706 blob = ltv->vval.v_blob;
2707 }
2708
2709 // The index is for the previous byte.
2710 ++idxtv->vval.v_number;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002711 if (blob == NULL || idxtv->vval.v_number >= blob_len(blob))
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002712 {
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002713 jump = TRUE;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002714 }
2715 else
2716 {
2717 // Push the next byte from the blob.
2718 tv = STACK_TV_BOT(0);
2719 tv->v_type = VAR_NUMBER;
2720 tv->vval.v_number = blob_get(blob,
2721 idxtv->vval.v_number);
2722 ++ectx->ec_stack.ga_len;
2723 }
2724 }
2725 else
2726 {
2727 semsg(_(e_for_loop_on_str_not_supported),
2728 vartype_name(ltv->v_type));
2729 return FAIL;
2730 }
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002731
2732 if (jump)
2733 {
2734 // past the end of the list/string/blob, jump to "endfor"
2735 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
2736 may_restore_cmdmod(&ectx->ec_funclocal);
2737 }
2738 else
2739 {
2740 // Store the current number of funcrefs, this may be used in
2741 // ISN_LOOPEND. The variable index is always one more than the loop
2742 // variable index.
Bram Moolenaarcc341812022-09-19 15:54:34 +01002743 tv = STACK_TV_VAR(iptr->isn_arg.forloop.for_loop_idx + 1);
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002744 tv->vval.v_number = ectx->ec_funcrefs.ga_len;
2745 }
2746
2747 return OK;
2748}
2749
2750/*
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002751 * Code for handling variables declared inside a loop and used in a closure.
2752 * This is very similar to what is done with funcstack_T. The difference is
2753 * that the funcstack_T has the scope of a function, while a loopvars_T has the
2754 * scope of the block inside a loop and each loop may have its own.
2755 */
2756
2757// Double linked list of loopvars_T in use.
2758static loopvars_T *first_loopvars = NULL;
2759
2760 static void
2761add_loopvars_to_list(loopvars_T *loopvars)
2762{
2763 // Link in list of loopvarss.
2764 if (first_loopvars != NULL)
2765 first_loopvars->lvs_prev = loopvars;
2766 loopvars->lvs_next = first_loopvars;
2767 loopvars->lvs_prev = NULL;
2768 first_loopvars = loopvars;
2769}
2770
2771 static void
2772remove_loopvars_from_list(loopvars_T *loopvars)
2773{
2774 if (loopvars->lvs_prev == NULL)
2775 first_loopvars = loopvars->lvs_next;
2776 else
2777 loopvars->lvs_prev->lvs_next = loopvars->lvs_next;
2778 if (loopvars->lvs_next != NULL)
2779 loopvars->lvs_next->lvs_prev = loopvars->lvs_prev;
2780}
2781
2782/*
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002783 * End of a for or while loop: Handle any variables used by a closure.
2784 */
2785 static int
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002786execute_endloop(isn_T *iptr, ectx_T *ectx)
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002787{
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002788 endloop_T *endloop = &iptr->isn_arg.endloop;
2789 typval_T *tv_refcount = STACK_TV_VAR(endloop->end_funcref_idx);
2790 int prev_closure_count = tv_refcount->vval.v_number;
Bram Moolenaarcc341812022-09-19 15:54:34 +01002791 int depth = endloop->end_depth;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002792 garray_T *gap = &ectx->ec_funcrefs;
2793 int closure_in_use = FALSE;
2794 loopvars_T *loopvars;
2795 typval_T *stack;
2796 int idx;
2797
Bram Moolenaarcc341812022-09-19 15:54:34 +01002798 // Check if any created closure is still being referenced and loopvars have
2799 // not been saved yet for the current depth.
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002800 for (idx = prev_closure_count; idx < gap->ga_len; ++idx)
2801 {
2802 partial_T *pt = ((partial_T **)gap->ga_data)[idx];
2803
Bram Moolenaarcc341812022-09-19 15:54:34 +01002804 if (pt->pt_refcount > 1 && pt->pt_loopvars[depth] == NULL)
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002805 {
2806 int refcount = pt->pt_refcount;
2807 int i;
2808
2809 // A Reference in a variable inside the loop doesn't count, it gets
2810 // unreferenced at the end of the loop.
2811 for (i = 0; i < endloop->end_var_count; ++i)
2812 {
2813 typval_T *stv = STACK_TV_VAR(endloop->end_var_idx + i);
2814
2815 if (stv->v_type == VAR_PARTIAL && pt == stv->vval.v_partial)
2816 --refcount;
2817 }
2818 if (refcount > 1)
2819 {
2820 closure_in_use = TRUE;
2821 break;
2822 }
2823 }
2824 }
2825
2826 // If no function reference were created since the start of the loop block
2827 // or it is no longer referenced there is nothing to do.
2828 if (!closure_in_use)
2829 return OK;
2830
2831 // A closure is using variables declared inside the loop.
2832 // Move them to the called function.
2833 loopvars = ALLOC_CLEAR_ONE(loopvars_T);
2834 if (loopvars == NULL)
2835 return FAIL;
2836
2837 loopvars->lvs_ga.ga_len = endloop->end_var_count;
2838 stack = ALLOC_CLEAR_MULT(typval_T, loopvars->lvs_ga.ga_len);
2839 loopvars->lvs_ga.ga_data = stack;
2840 if (stack == NULL)
2841 {
2842 vim_free(loopvars);
2843 return FAIL;
2844 }
2845 add_loopvars_to_list(loopvars);
2846
2847 // Move the variable values.
2848 for (idx = 0; idx < endloop->end_var_count; ++idx)
2849 {
2850 typval_T *tv = STACK_TV_VAR(endloop->end_var_idx + idx);
2851
2852 *(stack + idx) = *tv;
2853 tv->v_type = VAR_UNKNOWN;
2854 }
2855
2856 for (idx = prev_closure_count; idx < gap->ga_len; ++idx)
2857 {
2858 partial_T *pt = ((partial_T **)gap->ga_data)[idx];
2859
Bram Moolenaarcc341812022-09-19 15:54:34 +01002860 if (pt->pt_refcount > 1 && pt->pt_loopvars[depth] == NULL)
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002861 {
2862 ++loopvars->lvs_refcount;
Bram Moolenaarcc341812022-09-19 15:54:34 +01002863 pt->pt_loopvars[depth] = loopvars;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002864
Bram Moolenaarcc341812022-09-19 15:54:34 +01002865 pt->pt_outer.out_loop[depth].stack = &loopvars->lvs_ga;
2866 pt->pt_outer.out_loop[depth].var_idx -=
2867 ectx->ec_frame_idx + STACK_FRAME_SIZE + endloop->end_var_idx;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002868 }
2869 }
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002870
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002871 return OK;
2872}
2873
2874/*
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002875 * Called when a partial is freed or its reference count goes down to one. The
2876 * loopvars may be the only reference to the partials in the local variables.
2877 * Go over all of them, the funcref and can be freed if all partials
2878 * referencing the loopvars have a reference count of one.
Bram Moolenaarcc341812022-09-19 15:54:34 +01002879 * Return TRUE if it was freed.
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002880 */
Bram Moolenaarcc341812022-09-19 15:54:34 +01002881 int
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002882loopvars_check_refcount(loopvars_T *loopvars)
2883{
2884 int i;
2885 garray_T *gap = &loopvars->lvs_ga;
2886 int done = 0;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002887 typval_T *stack = gap->ga_data;
2888
Bram Moolenaarcc341812022-09-19 15:54:34 +01002889 if (loopvars->lvs_refcount > loopvars->lvs_min_refcount)
2890 return FALSE;
2891 for (i = 0; i < gap->ga_len; ++i)
2892 {
2893 typval_T *tv = ((typval_T *)gap->ga_data) + i;
2894
2895 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL
2896 && tv->vval.v_partial->pt_refcount == 1)
2897 {
2898 int depth;
2899
2900 for (depth = 0; depth < MAX_LOOP_DEPTH; ++depth)
2901 if (tv->vval.v_partial->pt_loopvars[depth] == loopvars)
2902 ++done;
2903 }
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002904 }
Bram Moolenaarcc341812022-09-19 15:54:34 +01002905 if (done != loopvars->lvs_min_refcount)
2906 return FALSE;
2907
2908 // All partials referencing the loopvars have a reference count of
2909 // one, thus the loopvars is no longer of use.
2910 stack = gap->ga_data;
2911 for (i = 0; i < gap->ga_len; ++i)
2912 clear_tv(stack + i);
2913 vim_free(stack);
2914 remove_loopvars_from_list(loopvars);
2915 vim_free(loopvars);
2916 return TRUE;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002917}
2918
2919/*
2920 * For garbage collecting: set references in all variables referenced by
2921 * all loopvars.
2922 */
2923 int
2924set_ref_in_loopvars(int copyID)
2925{
2926 loopvars_T *loopvars;
2927
2928 for (loopvars = first_loopvars; loopvars != NULL;
2929 loopvars = loopvars->lvs_next)
2930 {
2931 typval_T *stack = loopvars->lvs_ga.ga_data;
2932 int i;
2933
2934 for (i = 0; i < loopvars->lvs_ga.ga_len; ++i)
2935 if (set_ref_in_item(stack + i, copyID, NULL, NULL))
2936 return TRUE; // abort
2937 }
2938 return FALSE;
2939}
2940
2941/*
Bram Moolenaar06b77222022-01-25 15:51:56 +00002942 * Load instruction for w:/b:/g:/t: variable.
2943 * "isn_type" is used instead of "iptr->isn_type".
2944 */
2945 static int
2946load_namespace_var(ectx_T *ectx, isntype_T isn_type, isn_T *iptr)
2947{
2948 dictitem_T *di = NULL;
2949 hashtab_T *ht = NULL;
2950 char namespace;
2951
2952 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
2953 return NOTDONE;
2954 switch (isn_type)
2955 {
2956 case ISN_LOADG:
2957 ht = get_globvar_ht();
2958 namespace = 'g';
2959 break;
2960 case ISN_LOADB:
2961 ht = &curbuf->b_vars->dv_hashtab;
2962 namespace = 'b';
2963 break;
2964 case ISN_LOADW:
2965 ht = &curwin->w_vars->dv_hashtab;
2966 namespace = 'w';
2967 break;
2968 case ISN_LOADT:
2969 ht = &curtab->tp_vars->dv_hashtab;
2970 namespace = 't';
2971 break;
2972 default: // Cannot reach here
2973 return NOTDONE;
2974 }
2975 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
2976
Bram Moolenaar06b77222022-01-25 15:51:56 +00002977 if (di == NULL)
2978 {
Bram Moolenaarfe732552022-02-22 19:39:13 +00002979 if (isn_type == ISN_LOADG)
2980 {
2981 ufunc_T *ufunc = find_func(iptr->isn_arg.string, TRUE);
2982
2983 // g:Something could be a function
2984 if (ufunc != NULL)
2985 {
2986 typval_T *tv = STACK_TV_BOT(0);
2987
2988 ++ectx->ec_stack.ga_len;
2989 tv->v_type = VAR_FUNC;
2990 tv->vval.v_string = alloc(STRLEN(iptr->isn_arg.string) + 3);
2991 if (tv->vval.v_string == NULL)
2992 return FAIL;
2993 STRCPY(tv->vval.v_string, "g:");
2994 STRCPY(tv->vval.v_string + 2, iptr->isn_arg.string);
2995 return OK;
2996 }
2997 }
Bram Moolenaar06b77222022-01-25 15:51:56 +00002998 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarfe732552022-02-22 19:39:13 +00002999 if (vim_strchr(iptr->isn_arg.string, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar06b77222022-01-25 15:51:56 +00003000 // no check if the item exists in the script but
3001 // isn't exported, it is too complicated
Bram Moolenaarfe732552022-02-22 19:39:13 +00003002 semsg(_(e_item_not_found_in_script_str), iptr->isn_arg.string);
Bram Moolenaar06b77222022-01-25 15:51:56 +00003003 else
3004 semsg(_(e_undefined_variable_char_str),
Bram Moolenaarfe732552022-02-22 19:39:13 +00003005 namespace, iptr->isn_arg.string);
Bram Moolenaar06b77222022-01-25 15:51:56 +00003006 return FAIL;
3007 }
3008 else
3009 {
3010 copy_tv(&di->di_tv, STACK_TV_BOT(0));
3011 ++ectx->ec_stack.ga_len;
3012 }
3013 return OK;
3014}
3015
Bram Moolenaard0200c82023-01-28 15:19:40 +00003016
3017 static void
3018object_required_error(typval_T *tv)
3019{
3020 garray_T type_list;
3021 ga_init2(&type_list, sizeof(type_T *), 10);
3022 type_T *type = typval2type(tv, get_copyID(), &type_list, TVTT_DO_MEMBER);
3023 char *tofree = NULL;
3024 char *typename = type_name(type, &tofree);
3025 semsg(_(e_object_required_found_str), typename);
3026 vim_free(tofree);
3027 clear_type_list(&type_list);
3028}
3029
Bram Moolenaar06b77222022-01-25 15:51:56 +00003030/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02003031 * Execute instructions in execution context "ectx".
3032 * Return OK or FAIL;
3033 */
3034 static int
3035exec_instructions(ectx_T *ectx)
3036{
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003037 int ret = FAIL;
3038 int save_trylevel_at_start = ectx->ec_trylevel_at_start;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02003039 int dict_stack_len_at_start = dict_stack.ga_len;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003040
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02003041 // Start execution at the first instruction.
Bram Moolenaar4c137212021-04-19 16:48:48 +02003042 ectx->ec_iidx = 0;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01003043
Bram Moolenaarff652882021-05-16 15:24:49 +02003044 // Only catch exceptions in this instruction list.
3045 ectx->ec_trylevel_at_start = trylevel;
3046
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003047 for (;;)
3048 {
Bram Moolenaara7490192021-07-22 12:26:14 +02003049 static int breakcheck_count = 0; // using "static" makes it faster
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003050 isn_T *iptr;
Bram Moolenaara7490192021-07-22 12:26:14 +02003051 typval_T *tv;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003052
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003053 if (unlikely(++breakcheck_count >= 100))
Bram Moolenaar270d0382020-05-15 21:42:53 +02003054 {
3055 line_breakcheck();
3056 breakcheck_count = 0;
3057 }
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003058 if (unlikely(got_int))
Bram Moolenaar20431c92020-03-20 18:39:46 +01003059 {
3060 // Turn CTRL-C into an exception.
3061 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +01003062 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003063 goto theend;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003064 did_throw = TRUE;
3065 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003066
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003067 if (unlikely(did_emsg && msg_list != NULL && *msg_list != NULL))
Bram Moolenaara26b9702020-04-18 19:53:28 +02003068 {
3069 // Turn an error message into an exception.
3070 did_emsg = FALSE;
3071 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003072 goto theend;
Bram Moolenaara26b9702020-04-18 19:53:28 +02003073 did_throw = TRUE;
3074 *msg_list = NULL;
Bram Moolenaar3ef2e412023-04-30 18:50:48 +01003075
3076 // This exception was not caught (yet).
3077 garray_T *trystack = &ectx->ec_trystack;
3078 if (trystack->ga_len > 0)
3079 {
3080 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
3081 + trystack->ga_len - 1;
3082 if (trycmd->tcd_frame_idx == ectx->ec_frame_idx)
3083 trycmd->tcd_caught = FALSE;
3084 }
Bram Moolenaara26b9702020-04-18 19:53:28 +02003085 }
3086
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003087 if (unlikely(did_throw))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003088 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003089 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003090 trycmd_T *trycmd = NULL;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003091 int index = trystack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003092
3093 // An exception jumps to the first catch, finally, or returns from
3094 // the current function.
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003095 while (index > 0)
3096 {
3097 trycmd = ((trycmd_T *)trystack->ga_data) + index - 1;
Bram Moolenaar3ef2e412023-04-30 18:50:48 +01003098 // 1. after :try and before :catch - jump to first :catch
3099 // 2. in :catch block - jump to :finally
3100 // 3. in :catch block and no finally - jump to :endtry
3101 if (!trycmd->tcd_in_catch || trycmd->tcd_finally_idx != 0
3102 || trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003103 break;
3104 // In the catch and finally block of this try we have to go up
3105 // one level.
3106 --index;
3107 trycmd = NULL;
3108 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003109 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003110 {
Bram Moolenaar834193a2021-06-30 20:39:15 +02003111 if (trycmd->tcd_in_catch)
3112 {
Bram Moolenaar3ef2e412023-04-30 18:50:48 +01003113 if (trycmd->tcd_finally_idx > 0)
3114 {
3115 // exception inside ":catch", jump to ":finally" once
3116 ectx->ec_iidx = trycmd->tcd_finally_idx;
3117 trycmd->tcd_finally_idx = 0;
3118 }
3119 else
3120 {
3121 // exception inside ":catch" or ":finally", jump to
3122 // ":endtry"
3123 ectx->ec_iidx = trycmd->tcd_endtry_idx;
3124 }
Bram Moolenaar834193a2021-06-30 20:39:15 +02003125 }
3126 else
Bram Moolenaar3ef2e412023-04-30 18:50:48 +01003127 {
Bram Moolenaar834193a2021-06-30 20:39:15 +02003128 // jump to first ":catch"
3129 ectx->ec_iidx = trycmd->tcd_catch_idx;
Bram Moolenaar3ef2e412023-04-30 18:50:48 +01003130 trycmd->tcd_in_catch = TRUE;
3131 }
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003132 did_throw = FALSE; // don't come back here until :endtry
3133 trycmd->tcd_did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003134 }
3135 else
3136 {
Bram Moolenaar3ef2e412023-04-30 18:50:48 +01003137 // Not inside try or need to return from current function.
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02003138 // Push a dummy return value.
Bram Moolenaar35578162021-08-02 19:10:38 +02003139 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003140 goto theend;
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02003141 tv = STACK_TV_BOT(0);
3142 tv->v_type = VAR_NUMBER;
3143 tv->vval.v_number = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003144 ++ectx->ec_stack.ga_len;
3145 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003146 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02003147 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +01003148 need_rethrow = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003149 if (handle_closure_in_use(ectx, FALSE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003150 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003151 goto done;
3152 }
3153
Bram Moolenaar4c137212021-04-19 16:48:48 +02003154 if (func_return(ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003155 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003156 }
3157 continue;
3158 }
3159
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003160 /*
3161 * Big switch on the instruction. Most compilers will be turning this
3162 * into an efficient lookup table, since the "case" values are an enum
3163 * with sequential numbers. It may look ugly, but it should be the
3164 * most efficient way.
3165 */
Bram Moolenaar4c137212021-04-19 16:48:48 +02003166 iptr = &ectx->ec_instr[ectx->ec_iidx++];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003167 switch (iptr->isn_type)
3168 {
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00003169 // Constructor, first instruction in a new() method.
Bram Moolenaar00b28d62022-12-08 15:32:33 +00003170 case ISN_CONSTRUCT:
3171 // "this" is always the local variable at index zero
3172 tv = STACK_TV_VAR(0);
3173 tv->v_type = VAR_OBJECT;
3174 tv->vval.v_object = alloc_clear(
3175 iptr->isn_arg.construct.construct_size);
3176 tv->vval.v_object->obj_class =
3177 iptr->isn_arg.construct.construct_class;
Bram Moolenaard28d7b92022-12-08 20:42:00 +00003178 ++tv->vval.v_object->obj_class->class_refcount;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00003179 tv->vval.v_object->obj_refcount = 1;
Bram Moolenaard28d7b92022-12-08 20:42:00 +00003180 object_created(tv->vval.v_object);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00003181 break;
3182
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003183 // execute Ex command line
3184 case ISN_EXEC:
Ernie Raelee865f32023-09-29 19:53:55 +02003185 if (exec_command(iptr, iptr->isn_arg.string) == FAIL)
Bram Moolenaaraacc9662021-08-13 19:40:51 +02003186 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003187 break;
3188
Bram Moolenaar20677332021-06-06 17:02:53 +02003189 // execute Ex command line split at NL characters.
3190 case ISN_EXEC_SPLIT:
3191 {
3192 source_cookie_T cookie;
Bram Moolenaar518df272021-06-06 17:34:13 +02003193 char_u *line;
Bram Moolenaar20677332021-06-06 17:02:53 +02003194
3195 SOURCING_LNUM = iptr->isn_lnum;
3196 CLEAR_FIELD(cookie);
3197 cookie.sourcing_lnum = iptr->isn_lnum - 1;
3198 cookie.nextline = iptr->isn_arg.string;
Bram Moolenaar518df272021-06-06 17:34:13 +02003199 line = get_split_sourceline(0, &cookie, 0, 0);
3200 if (do_cmdline(line,
Bram Moolenaar20677332021-06-06 17:02:53 +02003201 get_split_sourceline, &cookie,
3202 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED)
3203 == FAIL
3204 || did_emsg)
Bram Moolenaar518df272021-06-06 17:34:13 +02003205 {
3206 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02003207 goto on_error;
Bram Moolenaar518df272021-06-06 17:34:13 +02003208 }
3209 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02003210 }
3211 break;
3212
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00003213 // execute Ex command line that is only a range
3214 case ISN_EXECRANGE:
3215 {
3216 exarg_T ea;
3217 char *error = NULL;
3218
3219 CLEAR_FIELD(ea);
3220 ea.cmdidx = CMD_SIZE;
3221 ea.addr_type = ADDR_LINES;
3222 ea.cmd = iptr->isn_arg.string;
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00003223 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00003224 parse_cmd_address(&ea, &error, FALSE);
Bram Moolenaar01a4dcb2021-12-04 13:15:10 +00003225 if (ea.cmd == NULL)
3226 goto on_error;
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00003227 // error is always NULL when using ADDR_LINES
3228 error = ex_range_without_command(&ea);
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00003229 if (error != NULL)
3230 {
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00003231 emsg(error);
3232 goto on_error;
3233 }
3234 }
3235 break;
3236
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02003237 // Evaluate an expression with legacy syntax, push it onto the
3238 // stack.
3239 case ISN_LEGACY_EVAL:
3240 {
3241 char_u *arg = iptr->isn_arg.string;
3242 int res;
3243 int save_flags = cmdmod.cmod_flags;
3244
Bram Moolenaar35578162021-08-02 19:10:38 +02003245 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003246 goto theend;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02003247 tv = STACK_TV_BOT(0);
3248 init_tv(tv);
3249 cmdmod.cmod_flags |= CMOD_LEGACY;
3250 res = eval0(arg, tv, NULL, &EVALARG_EVALUATE);
3251 cmdmod.cmod_flags = save_flags;
3252 if (res == FAIL)
3253 goto on_error;
3254 ++ectx->ec_stack.ga_len;
3255 }
3256 break;
3257
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003258 // push typeval VAR_INSTR with instructions to be executed
3259 case ISN_INSTR:
3260 {
Bram Moolenaar35578162021-08-02 19:10:38 +02003261 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003262 goto theend;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003263 tv = STACK_TV_BOT(0);
3264 tv->vval.v_instr = ALLOC_ONE(instr_T);
3265 if (tv->vval.v_instr == NULL)
3266 goto on_error;
3267 ++ectx->ec_stack.ga_len;
3268
3269 tv->v_type = VAR_INSTR;
3270 tv->vval.v_instr->instr_ectx = ectx;
3271 tv->vval.v_instr->instr_instr = iptr->isn_arg.instr;
3272 }
3273 break;
3274
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003275 case ISN_SOURCE:
3276 {
Bram Moolenaar89445512022-04-14 12:58:23 +01003277 int notused;
LemonBoy77142312022-04-15 20:50:46 +01003278
Bram Moolenaar89445512022-04-14 12:58:23 +01003279 SOURCING_LNUM = iptr->isn_lnum;
3280 if (may_load_script((int)iptr->isn_arg.number, &notused)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003281 == FAIL)
Bram Moolenaar89445512022-04-14 12:58:23 +01003282 goto on_error;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003283 }
3284 break;
3285
Bram Moolenaar4c137212021-04-19 16:48:48 +02003286 // execute :substitute with an expression
3287 case ISN_SUBSTITUTE:
3288 {
3289 subs_T *subs = &iptr->isn_arg.subs;
3290 source_cookie_T cookie;
3291 struct subs_expr_S *save_instr = substitute_instr;
3292 struct subs_expr_S subs_instr;
3293 int res;
3294
3295 subs_instr.subs_ectx = ectx;
3296 subs_instr.subs_instr = subs->subs_instr;
3297 subs_instr.subs_status = OK;
3298 substitute_instr = &subs_instr;
3299
3300 SOURCING_LNUM = iptr->isn_lnum;
3301 // This is very much like ISN_EXEC
3302 CLEAR_FIELD(cookie);
3303 cookie.sourcing_lnum = iptr->isn_lnum - 1;
3304 res = do_cmdline(subs->subs_cmd,
3305 getsourceline, &cookie,
3306 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
3307 substitute_instr = save_instr;
3308
3309 if (res == FAIL || did_emsg
3310 || subs_instr.subs_status == FAIL)
3311 goto on_error;
3312 }
3313 break;
3314
3315 case ISN_FINISH:
3316 goto done;
3317
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02003318 case ISN_REDIRSTART:
3319 // create a dummy entry for var_redir_str()
3320 if (alloc_redir_lval() == FAIL)
3321 goto on_error;
3322
3323 // The output is stored in growarray "redir_ga" until
3324 // redirection ends.
3325 init_redir_ga();
3326 redir_vname = 1;
3327 break;
3328
3329 case ISN_REDIREND:
3330 {
3331 char_u *res = get_clear_redir_ga();
3332
3333 // End redirection, put redirected text on the stack.
3334 clear_redir_lval();
3335 redir_vname = 0;
3336
Bram Moolenaar35578162021-08-02 19:10:38 +02003337 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02003338 {
3339 vim_free(res);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003340 goto theend;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02003341 }
3342 tv = STACK_TV_BOT(0);
3343 tv->v_type = VAR_STRING;
3344 tv->vval.v_string = res;
3345 ++ectx->ec_stack.ga_len;
3346 }
3347 break;
3348
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003349 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02003350#ifdef FEAT_QUICKFIX
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003351 force_abort = TRUE;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003352 if (trigger_cexpr_autocmd(iptr->isn_arg.number) == FAIL)
3353 goto on_error;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003354 force_abort = FALSE;
Bram Moolenaarb7c97812021-05-05 22:51:39 +02003355#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003356 break;
3357
3358 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02003359#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003360 {
3361 exarg_T ea;
3362 int res;
3363
3364 CLEAR_FIELD(ea);
3365 ea.cmdidx = iptr->isn_arg.cexpr.cexpr_ref->cer_cmdidx;
3366 ea.forceit = iptr->isn_arg.cexpr.cexpr_ref->cer_forceit;
3367 ea.cmdlinep = &iptr->isn_arg.cexpr.cexpr_ref->cer_cmdline;
3368 --ectx->ec_stack.ga_len;
3369 tv = STACK_TV_BOT(0);
Bram Moolenaarbd683e32022-07-18 17:49:03 +01003370 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003371 res = cexpr_core(&ea, tv);
3372 clear_tv(tv);
3373 if (res == FAIL)
3374 goto on_error;
3375 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02003376#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003377 break;
3378
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003379 // execute Ex command from pieces on the stack
3380 case ISN_EXECCONCAT:
3381 {
3382 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +02003383 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003384 int pass;
3385 int i;
3386 char_u *cmd = NULL;
3387 char_u *str;
3388
3389 for (pass = 1; pass <= 2; ++pass)
3390 {
3391 for (i = 0; i < count; ++i)
3392 {
3393 tv = STACK_TV_BOT(i - count);
3394 str = tv->vval.v_string;
3395 if (str != NULL && *str != NUL)
3396 {
3397 if (pass == 2)
3398 STRCPY(cmd + len, str);
3399 len += STRLEN(str);
3400 }
3401 if (pass == 2)
3402 clear_tv(tv);
3403 }
3404 if (pass == 1)
3405 {
3406 cmd = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003407 if (unlikely(cmd == NULL))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003408 goto theend;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003409 len = 0;
3410 }
3411 }
3412
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02003413 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003414 do_cmdline_cmd(cmd);
3415 vim_free(cmd);
3416 }
3417 break;
3418
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003419 // execute :echo {string} ...
3420 case ISN_ECHO:
3421 {
3422 int count = iptr->isn_arg.echo.echo_count;
3423 int atstart = TRUE;
3424 int needclr = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003425 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003426
3427 for (idx = 0; idx < count; ++idx)
3428 {
3429 tv = STACK_TV_BOT(idx - count);
3430 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
3431 &atstart, &needclr);
3432 clear_tv(tv);
3433 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01003434 if (needclr)
3435 msg_clr_eos();
Bram Moolenaar4c137212021-04-19 16:48:48 +02003436 ectx->ec_stack.ga_len -= count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003437 }
3438 break;
3439
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003440 // :execute {string} ...
3441 // :echomsg {string} ...
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01003442 // :echowindow {string} ...
Bram Moolenaar7de62622021-08-07 15:05:47 +02003443 // :echoconsole {string} ...
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003444 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01003445 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003446 case ISN_ECHOMSG:
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01003447 case ISN_ECHOWINDOW:
Bram Moolenaar7de62622021-08-07 15:05:47 +02003448 case ISN_ECHOCONSOLE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003449 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01003450 {
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01003451 int count;
Bram Moolenaarad39c092020-02-26 18:23:43 +01003452 garray_T ga;
3453 char_u buf[NUMBUFLEN];
3454 char_u *p;
3455 int len;
3456 int failed = FALSE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003457 int idx;
Bram Moolenaarad39c092020-02-26 18:23:43 +01003458
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01003459 if (iptr->isn_type == ISN_ECHOWINDOW)
3460 count = iptr->isn_arg.echowin.ewin_count;
3461 else
3462 count = iptr->isn_arg.number;
Bram Moolenaarad39c092020-02-26 18:23:43 +01003463 ga_init2(&ga, 1, 80);
3464 for (idx = 0; idx < count; ++idx)
3465 {
3466 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003467 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01003468 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003469 if (tv->v_type == VAR_CHANNEL
3470 || tv->v_type == VAR_JOB)
3471 {
3472 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar68db9962021-05-09 23:19:22 +02003473 semsg(_(e_using_invalid_value_as_string_str),
3474 vartype_name(tv->v_type));
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003475 break;
3476 }
3477 else
3478 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01003479 }
3480 else
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003481 p = tv_stringify(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01003482
3483 len = (int)STRLEN(p);
Bram Moolenaar35578162021-08-02 19:10:38 +02003484 if (GA_GROW_FAILS(&ga, len + 2))
Bram Moolenaarad39c092020-02-26 18:23:43 +01003485 failed = TRUE;
3486 else
3487 {
3488 if (ga.ga_len > 0)
3489 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
3490 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
3491 ga.ga_len += len;
3492 }
3493 clear_tv(tv);
3494 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003495 ectx->ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003496 if (failed)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003497 {
3498 ga_clear(&ga);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003499 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003500 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01003501
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003502 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003503 {
3504 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02003505 {
3506 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003507 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01003508 if (did_emsg)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003509 {
3510 ga_clear(&ga);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01003511 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003512 }
Bram Moolenaar430deb12020-08-23 16:29:11 +02003513 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003514 else
3515 {
3516 msg_sb_eol();
3517 if (iptr->isn_type == ISN_ECHOMSG)
3518 {
3519 msg_attr(ga.ga_data, echo_attr);
3520 out_flush();
3521 }
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01003522#ifdef HAS_MESSAGE_WINDOW
3523 else if (iptr->isn_type == ISN_ECHOWINDOW)
3524 {
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01003525 start_echowindow(
3526 iptr->isn_arg.echowin.ewin_time);
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01003527 msg_attr(ga.ga_data, echo_attr);
3528 end_echowindow();
3529 }
3530#endif
Bram Moolenaar7de62622021-08-07 15:05:47 +02003531 else if (iptr->isn_type == ISN_ECHOCONSOLE)
3532 {
3533 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data),
3534 TRUE);
3535 ui_write((char_u *)"\r\n", 2, TRUE);
3536 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003537 else
3538 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003539 SOURCING_LNUM = iptr->isn_lnum;
3540 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003541 }
3542 }
3543 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01003544 ga_clear(&ga);
3545 }
3546 break;
3547
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003548 // load local variable or argument
3549 case ISN_LOAD:
Bram Moolenaar35578162021-08-02 19:10:38 +02003550 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003551 goto theend;
Bram Moolenaar2ed57ac2023-04-01 22:05:38 +01003552 tv = STACK_TV_VAR(iptr->isn_arg.number);
3553 if (tv->v_type == VAR_UNKNOWN)
3554 {
3555 // missing argument or default value v:none
3556 STACK_TV_BOT(0)->v_type = VAR_SPECIAL;
3557 STACK_TV_BOT(0)->vval.v_number = VVAL_NONE;
3558 }
3559 else
3560 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003561 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003562 break;
3563
3564 // load v: variable
3565 case ISN_LOADV:
Bram Moolenaar35578162021-08-02 19:10:38 +02003566 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003567 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003568 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003569 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003570 break;
3571
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003572 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003573 case ISN_LOADSCRIPT:
3574 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01003575 scriptref_T *sref = iptr->isn_arg.script.scriptref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003576 svar_T *sv;
3577
Bram Moolenaar6db660b2021-08-01 14:08:54 +02003578 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003579 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003580 goto theend;
Bram Moolenaar859cc212022-03-28 15:22:35 +01003581 allocate_if_null(sv);
Bram Moolenaar35578162021-08-02 19:10:38 +02003582 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003583 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003584 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003585 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003586 }
3587 break;
3588
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003589 // load s: variable in old script or autoload import
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003590 case ISN_LOADS:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003591 case ISN_LOADEXPORT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003592 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003593 int sid = iptr->isn_arg.loadstore.ls_sid;
3594 hashtab_T *ht = &SCRIPT_VARS(sid);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003595 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003596 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003597
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003598 if (di == NULL)
3599 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003600 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003601 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003602 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003603 }
3604 else
3605 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003606 if (iptr->isn_type == ISN_LOADEXPORT)
3607 {
3608 int idx = get_script_item_idx(sid, name, 0,
3609 NULL, NULL);
3610 svar_T *sv;
3611
3612 if (idx >= 0)
3613 {
3614 sv = ((svar_T *)SCRIPT_ITEM(sid)
3615 ->sn_var_vals.ga_data) + idx;
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003616 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003617 {
3618 SOURCING_LNUM = iptr->isn_lnum;
3619 semsg(_(e_item_not_exported_in_script_str),
3620 name);
3621 goto on_error;
3622 }
3623 }
3624 }
Bram Moolenaar35578162021-08-02 19:10:38 +02003625 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003626 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003627 copy_tv(&di->di_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003628 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003629 }
3630 }
3631 break;
3632
Bram Moolenaard3aac292020-04-19 14:32:17 +02003633 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003634 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02003635 case ISN_LOADB:
3636 case ISN_LOADW:
3637 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003638 {
Bram Moolenaar06b77222022-01-25 15:51:56 +00003639 int res = load_namespace_var(ectx, iptr->isn_type, iptr);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003640
Bram Moolenaar06b77222022-01-25 15:51:56 +00003641 if (res == NOTDONE)
Bram Moolenaarcbbc48f2022-01-18 12:58:28 +00003642 goto theend;
Bram Moolenaar06b77222022-01-25 15:51:56 +00003643 if (res == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003644 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003645 }
Bram Moolenaar06b77222022-01-25 15:51:56 +00003646
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003647 break;
3648
Bram Moolenaar03290b82020-12-19 16:30:44 +01003649 // load autoload variable
3650 case ISN_LOADAUTO:
3651 {
3652 char_u *name = iptr->isn_arg.string;
3653
Bram Moolenaar35578162021-08-02 19:10:38 +02003654 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003655 goto theend;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003656 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003657 if (eval_variable(name, (int)STRLEN(name), 0,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003658 STACK_TV_BOT(0), NULL, EVAL_VAR_VERBOSE) == FAIL)
Bram Moolenaar03290b82020-12-19 16:30:44 +01003659 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003660 ++ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003661 }
3662 break;
3663
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003664 // load g:/b:/w:/t: namespace
3665 case ISN_LOADGDICT:
3666 case ISN_LOADBDICT:
3667 case ISN_LOADWDICT:
3668 case ISN_LOADTDICT:
3669 {
3670 dict_T *d = NULL;
3671
3672 switch (iptr->isn_type)
3673 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02003674 case ISN_LOADGDICT: d = get_globvar_dict(); break;
3675 case ISN_LOADBDICT: d = curbuf->b_vars; break;
3676 case ISN_LOADWDICT: d = curwin->w_vars; break;
3677 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003678 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003679 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003680 }
Bram Moolenaar35578162021-08-02 19:10:38 +02003681 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003682 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003683 tv = STACK_TV_BOT(0);
3684 tv->v_type = VAR_DICT;
3685 tv->v_lock = 0;
3686 tv->vval.v_dict = d;
Bram Moolenaar1bd3cb22021-02-24 12:27:31 +01003687 ++d->dv_refcount;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003688 ++ectx->ec_stack.ga_len;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003689 }
3690 break;
3691
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003692 // load &option
3693 case ISN_LOADOPT:
3694 {
3695 typval_T optval;
3696 char_u *name = iptr->isn_arg.string;
3697
Bram Moolenaara8c17702020-04-01 21:17:24 +02003698 // This is not expected to fail, name is checked during
3699 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar35578162021-08-02 19:10:38 +02003700 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003701 goto theend;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003702 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003703 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003704 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003705 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003706 }
3707 break;
3708
3709 // load $ENV
3710 case ISN_LOADENV:
3711 {
3712 typval_T optval;
3713 char_u *name = iptr->isn_arg.string;
3714
Bram Moolenaar35578162021-08-02 19:10:38 +02003715 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003716 goto theend;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003717 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003718 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003719 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003720 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003721 }
3722 break;
3723
3724 // load @register
3725 case ISN_LOADREG:
Bram Moolenaar35578162021-08-02 19:10:38 +02003726 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003727 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003728 tv = STACK_TV_BOT(0);
3729 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003730 tv->v_lock = 0;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02003731 // This may result in NULL, which should be equivalent to an
3732 // empty string.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003733 tv->vval.v_string = get_reg_contents(
3734 iptr->isn_arg.number, GREG_EXPR_SRC);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003735 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003736 break;
3737
3738 // store local variable
3739 case ISN_STORE:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003740 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003741 tv = STACK_TV_VAR(iptr->isn_arg.number);
3742 clear_tv(tv);
3743 *tv = *STACK_TV_BOT(0);
3744 break;
3745
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003746 // store s: variable in old script or autoload import
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003747 case ISN_STORES:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003748 case ISN_STOREEXPORT:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003749 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003750 int sid = iptr->isn_arg.loadstore.ls_sid;
3751 hashtab_T *ht = &SCRIPT_VARS(sid);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003752 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003753 dictitem_T *di = find_var_in_ht(ht, 0,
3754 iptr->isn_type == ISN_STORES
3755 ? name + 2 : name, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003756
Bram Moolenaar4c137212021-04-19 16:48:48 +02003757 --ectx->ec_stack.ga_len;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003758 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003759 if (di == NULL)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003760 {
3761 if (iptr->isn_type == ISN_STOREEXPORT)
3762 {
3763 semsg(_(e_undefined_variable_str), name);
Bram Moolenaard1d26842022-03-31 10:13:47 +01003764 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003765 goto on_error;
3766 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003767 store_var(name, STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003768 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003769 else
3770 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003771 if (iptr->isn_type == ISN_STOREEXPORT)
3772 {
3773 int idx = get_script_item_idx(sid, name, 0,
3774 NULL, NULL);
3775
Bram Moolenaar06651632022-04-27 17:54:25 +01003776 // can this ever fail?
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003777 if (idx >= 0)
3778 {
3779 svar_T *sv = ((svar_T *)SCRIPT_ITEM(sid)
3780 ->sn_var_vals.ga_data) + idx;
3781
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003782 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003783 {
3784 semsg(_(e_item_not_exported_in_script_str),
3785 name);
Bram Moolenaard1d26842022-03-31 10:13:47 +01003786 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003787 goto on_error;
3788 }
3789 }
3790 }
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02003791 if (var_check_permission(di, name) == FAIL)
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003792 {
3793 clear_tv(STACK_TV_BOT(0));
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02003794 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003795 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003796 clear_tv(&di->di_tv);
3797 di->di_tv = *STACK_TV_BOT(0);
3798 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003799 }
3800 break;
3801
3802 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003803 case ISN_STORESCRIPT:
3804 {
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003805 scriptref_T *sref = iptr->isn_arg.script.scriptref;
3806 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003807
Bram Moolenaar6db660b2021-08-01 14:08:54 +02003808 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003809 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003810 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003811 --ectx->ec_stack.ga_len;
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003812
3813 // "const" and "final" are checked at compile time, locking
3814 // the value needs to be checked here.
3815 SOURCING_LNUM = iptr->isn_lnum;
3816 if (value_check_lock(sv->sv_tv->v_lock, sv->sv_name, FALSE))
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003817 {
3818 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003819 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003820 }
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003821
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003822 clear_tv(sv->sv_tv);
3823 *sv->sv_tv = *STACK_TV_BOT(0);
3824 }
3825 break;
3826
3827 // store option
3828 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003829 case ISN_STOREFUNCOPT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003830 {
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003831 char_u *opt_name = iptr->isn_arg.storeopt.so_name;
3832 int opt_flags = iptr->isn_arg.storeopt.so_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003833 long n = 0;
3834 char_u *s = NULL;
3835 char *msg;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003836 char_u numbuf[NUMBUFLEN];
3837 char_u *tofree = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003838
Bram Moolenaar4c137212021-04-19 16:48:48 +02003839 --ectx->ec_stack.ga_len;
LemonBoy32f34612023-09-02 21:52:05 +02003840 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003841 tv = STACK_TV_BOT(0);
3842 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01003843 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003844 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01003845 if (s == NULL)
3846 s = (char_u *)"";
3847 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003848 else if (iptr->isn_type == ISN_STOREFUNCOPT)
3849 {
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003850 // If the option can be set to a function reference or
3851 // a lambda and the passed value is a function
3852 // reference, then convert it to the name (string) of
3853 // the function reference.
3854 s = tv2string(tv, &tofree, numbuf, 0);
3855 if (s == NULL || *s == NUL)
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003856 {
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003857 // cannot happen?
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003858 clear_tv(tv);
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003859 vim_free(tofree);
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003860 goto on_error;
3861 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003862 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003863 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003864 // must be VAR_NUMBER, CHECKTYPE makes sure
3865 n = tv->vval.v_number;
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003866 msg = set_option_value(opt_name, n, s, opt_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02003867 clear_tv(tv);
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003868 vim_free(tofree);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003869 if (msg != NULL)
3870 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003871 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003872 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02003873 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003874 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003875 }
3876 break;
3877
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003878 // store $ENV
3879 case ISN_STOREENV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003880 --ectx->ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003881 tv = STACK_TV_BOT(0);
3882 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
3883 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003884 break;
3885
3886 // store @r
3887 case ISN_STOREREG:
3888 {
3889 int reg = iptr->isn_arg.number;
3890
Bram Moolenaar4c137212021-04-19 16:48:48 +02003891 --ectx->ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01003892 tv = STACK_TV_BOT(0);
Bram Moolenaar74f4a962021-06-17 21:03:07 +02003893 write_reg_contents(reg, tv_get_string(tv), -1, FALSE);
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01003894 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003895 }
3896 break;
3897
3898 // store v: variable
3899 case ISN_STOREV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003900 --ectx->ec_stack.ga_len;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003901 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
3902 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003903 // should not happen, type is checked when compiling
3904 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003905 break;
3906
Bram Moolenaard3aac292020-04-19 14:32:17 +02003907 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003908 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02003909 case ISN_STOREB:
3910 case ISN_STOREW:
3911 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003912 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003913 dictitem_T *di;
3914 hashtab_T *ht;
3915 char_u *name = iptr->isn_arg.string + 2;
3916
Bram Moolenaard3aac292020-04-19 14:32:17 +02003917 switch (iptr->isn_type)
3918 {
3919 case ISN_STOREG:
3920 ht = get_globvar_ht();
3921 break;
3922 case ISN_STOREB:
3923 ht = &curbuf->b_vars->dv_hashtab;
3924 break;
3925 case ISN_STOREW:
3926 ht = &curwin->w_vars->dv_hashtab;
3927 break;
3928 case ISN_STORET:
3929 ht = &curtab->tp_vars->dv_hashtab;
3930 break;
3931 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003932 goto theend;
Bram Moolenaard3aac292020-04-19 14:32:17 +02003933 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003934
Bram Moolenaar4c137212021-04-19 16:48:48 +02003935 --ectx->ec_stack.ga_len;
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003936 di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003937 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003938 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003939 else
3940 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003941 SOURCING_LNUM = iptr->isn_lnum;
3942 if (var_check_permission(di, name) == FAIL)
3943 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003944 clear_tv(&di->di_tv);
3945 di->di_tv = *STACK_TV_BOT(0);
3946 }
3947 }
3948 break;
3949
Bram Moolenaar03290b82020-12-19 16:30:44 +01003950 // store an autoload variable
3951 case ISN_STOREAUTO:
3952 SOURCING_LNUM = iptr->isn_lnum;
3953 set_var(iptr->isn_arg.string, STACK_TV_BOT(-1), TRUE);
3954 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003955 --ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003956 break;
3957
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003958 // store number in local variable
3959 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01003960 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003961 clear_tv(tv);
3962 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01003963 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003964 break;
3965
Bram Moolenaar590162c2022-12-24 21:24:06 +00003966 // Store a value in a list, dict, blob or object variable.
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01003967 case ISN_STOREINDEX:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003968 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003969 int res = execute_storeindex(iptr, ectx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003970
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003971 if (res == FAIL)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02003972 goto on_error;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003973 if (res == NOTDONE)
3974 goto theend;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003975 }
3976 break;
3977
Bram Moolenaarea5c8982022-02-17 14:42:02 +00003978 // store value in list or blob range
Bram Moolenaar68452172021-04-12 21:21:02 +02003979 case ISN_STORERANGE:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003980 if (execute_storerange(iptr, ectx) == FAIL)
3981 goto on_error;
Bram Moolenaar68452172021-04-12 21:21:02 +02003982 break;
3983
Bram Moolenaard505d172022-12-18 21:42:55 +00003984 case ISN_LOAD_CLASSMEMBER:
3985 {
3986 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
3987 goto theend;
3988 classmember_T *cm = &iptr->isn_arg.classmember;
Bram Moolenaar4e2406c2023-06-24 19:22:21 +01003989 copy_tv(cm->cm_class->class_members_tv + cm->cm_idx,
3990 STACK_TV_BOT(0));
Bram Moolenaard505d172022-12-18 21:42:55 +00003991 ++ectx->ec_stack.ga_len;
3992 }
3993 break;
3994
3995 case ISN_STORE_CLASSMEMBER:
3996 {
3997 classmember_T *cm = &iptr->isn_arg.classmember;
3998 tv = &cm->cm_class->class_members_tv[cm->cm_idx];
3999 clear_tv(tv);
4000 *tv = *STACK_TV_BOT(-1);
4001 --ectx->ec_stack.ga_len;
4002 }
4003 break;
4004
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01004005 // Load or store variable or argument from outer scope.
Bram Moolenaar0186e582021-01-10 18:33:11 +01004006 case ISN_LOADOUTER:
4007 case ISN_STOREOUTER:
4008 {
4009 int depth = iptr->isn_arg.outer.outer_depth;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004010 outer_T *outer = ectx->ec_outer_ref == NULL ? NULL
4011 : ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +01004012
4013 while (depth > 1 && outer != NULL)
4014 {
4015 outer = outer->out_up;
4016 --depth;
4017 }
4018 if (outer == NULL)
4019 {
4020 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar69c76172021-12-02 16:38:52 +00004021 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx
4022 || ectx->ec_outer_ref == NULL)
4023 // Possibly :def function called from legacy
4024 // context.
4025 emsg(_(e_closure_called_from_invalid_context));
4026 else
4027 iemsg("LOADOUTER depth more than scope levels");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004028 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01004029 }
Bram Moolenaarcc341812022-09-19 15:54:34 +01004030 if (depth < 0)
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01004031 // Variable declared in loop. May be copied if the
4032 // loop block has already ended.
Bram Moolenaarcc341812022-09-19 15:54:34 +01004033 tv = ((typval_T *)outer->out_loop[-depth - 1]
4034 .stack->ga_data)
4035 + outer->out_loop[-depth - 1].var_idx
4036 + iptr->isn_arg.outer.outer_idx;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004037 else
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01004038 // Variable declared in a function. May be copied if
4039 // the function has already returned.
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004040 tv = ((typval_T *)outer->out_stack->ga_data)
4041 + outer->out_frame_idx + STACK_FRAME_SIZE
4042 + iptr->isn_arg.outer.outer_idx;
Bram Moolenaar0186e582021-01-10 18:33:11 +01004043 if (iptr->isn_type == ISN_LOADOUTER)
4044 {
Bram Moolenaar35578162021-08-02 19:10:38 +02004045 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004046 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01004047 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02004048 ++ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01004049 }
4050 else
4051 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004052 --ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01004053 clear_tv(tv);
4054 *tv = *STACK_TV_BOT(0);
4055 }
4056 }
4057 break;
4058
Bram Moolenaar752fc692021-01-04 21:57:11 +01004059 // unlet item in list or dict variable
4060 case ISN_UNLETINDEX:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00004061 if (execute_unletindex(iptr, ectx) == FAIL)
4062 goto on_error;
Bram Moolenaar752fc692021-01-04 21:57:11 +01004063 break;
4064
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01004065 // unlet range of items in list variable
4066 case ISN_UNLETRANGE:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00004067 if (execute_unletrange(iptr, ectx) == FAIL)
4068 goto on_error;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01004069 break;
4070
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004071 // push constant
4072 case ISN_PUSHNR:
4073 case ISN_PUSHBOOL:
4074 case ISN_PUSHSPEC:
4075 case ISN_PUSHF:
4076 case ISN_PUSHS:
4077 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004078 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004079 case ISN_PUSHCHANNEL:
4080 case ISN_PUSHJOB:
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00004081 case ISN_PUSHOBJ:
4082 case ISN_PUSHCLASS:
Bram Moolenaar35578162021-08-02 19:10:38 +02004083 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004084 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004085 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02004086 tv->v_lock = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004087 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004088 switch (iptr->isn_type)
4089 {
4090 case ISN_PUSHNR:
4091 tv->v_type = VAR_NUMBER;
4092 tv->vval.v_number = iptr->isn_arg.number;
4093 break;
4094 case ISN_PUSHBOOL:
4095 tv->v_type = VAR_BOOL;
4096 tv->vval.v_number = iptr->isn_arg.number;
4097 break;
4098 case ISN_PUSHSPEC:
4099 tv->v_type = VAR_SPECIAL;
4100 tv->vval.v_number = iptr->isn_arg.number;
4101 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004102 case ISN_PUSHF:
4103 tv->v_type = VAR_FLOAT;
4104 tv->vval.v_float = iptr->isn_arg.fnumber;
4105 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004106 case ISN_PUSHBLOB:
4107 blob_copy(iptr->isn_arg.blob, tv);
4108 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004109 case ISN_PUSHFUNC:
4110 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01004111 if (iptr->isn_arg.string == NULL)
4112 tv->vval.v_string = NULL;
4113 else
4114 tv->vval.v_string =
4115 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004116 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004117 case ISN_PUSHCHANNEL:
4118#ifdef FEAT_JOB_CHANNEL
4119 tv->v_type = VAR_CHANNEL;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00004120 tv->vval.v_channel = NULL;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004121#endif
4122 break;
4123 case ISN_PUSHJOB:
4124#ifdef FEAT_JOB_CHANNEL
4125 tv->v_type = VAR_JOB;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00004126 tv->vval.v_job = NULL;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004127#endif
4128 break;
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00004129 case ISN_PUSHOBJ:
4130 tv->v_type = VAR_OBJECT;
4131 tv->vval.v_object = NULL;
4132 break;
4133 case ISN_PUSHCLASS:
4134 tv->v_type = VAR_CLASS;
Bram Moolenaar30a84472023-02-27 08:07:14 +00004135 tv->vval.v_class = iptr->isn_arg.classarg;
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00004136 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004137 default:
4138 tv->v_type = VAR_STRING;
Bram Moolenaarf8691002022-03-10 12:20:53 +00004139 tv->vval.v_string = iptr->isn_arg.string == NULL
4140 ? NULL : vim_strsave(iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004141 }
4142 break;
4143
Bram Moolenaar06b77222022-01-25 15:51:56 +00004144 case ISN_AUTOLOAD:
4145 {
4146 char_u *name = iptr->isn_arg.string;
4147
4148 (void)script_autoload(name, FALSE);
4149 if (find_func(name, TRUE))
4150 {
4151 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
4152 goto theend;
4153 tv = STACK_TV_BOT(0);
4154 tv->v_lock = 0;
4155 ++ectx->ec_stack.ga_len;
4156 tv->v_type = VAR_FUNC;
4157 tv->vval.v_string = vim_strsave(name);
4158 }
4159 else
4160 {
4161 int res = load_namespace_var(ectx, ISN_LOADG, iptr);
4162
4163 if (res == NOTDONE)
4164 goto theend;
4165 if (res == FAIL)
4166 goto on_error;
4167 }
4168 }
4169 break;
4170
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004171 case ISN_UNLET:
4172 if (do_unlet(iptr->isn_arg.unlet.ul_name,
4173 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02004174 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004175 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02004176 case ISN_UNLETENV:
LemonBoy77142312022-04-15 20:50:46 +01004177 vim_unsetenv_ext(iptr->isn_arg.unlet.ul_name);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02004178 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004179
Bram Moolenaaraacc9662021-08-13 19:40:51 +02004180 case ISN_LOCKUNLOCK:
4181 {
Ernie Rael64885642023-10-04 20:16:22 +02004182 lval_root_T *lval_root_save = lval_root;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02004183 int res;
Ernie Raelee865f32023-09-29 19:53:55 +02004184#ifdef LOG_LOCKVAR
4185 ch_log(NULL, "LKVAR: execute INS_LOCKUNLOCK isn_arg %s",
4186 iptr->isn_arg.string);
4187#endif
Bram Moolenaaraacc9662021-08-13 19:40:51 +02004188
4189 // Stack has the local variable, argument the whole :lock
4190 // or :unlock command, like ISN_EXEC.
4191 --ectx->ec_stack.ga_len;
Ernie Rael64885642023-10-04 20:16:22 +02004192 lval_root_T root = { STACK_TV_BOT(0),
4193 iptr->isn_arg.lockunlock.lu_cl_exec,
4194 iptr->isn_arg.lockunlock.lu_is_arg };
4195 lval_root = &root;
4196 res = exec_command(iptr,
4197 iptr->isn_arg.lockunlock.lu_string);
4198 clear_tv(root.lr_tv);
Bram Moolenaaraacc9662021-08-13 19:40:51 +02004199 lval_root = lval_root_save;
4200 if (res == FAIL)
4201 goto on_error;
4202 }
4203 break;
4204
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02004205 case ISN_LOCKCONST:
4206 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
4207 break;
4208
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004209 // create a list from items on the stack; uses a single allocation
4210 // for the list header and the items
4211 case ISN_NEWLIST:
Bram Moolenaar4c137212021-04-19 16:48:48 +02004212 if (exe_newlist(iptr->isn_arg.number, ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004213 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004214 break;
4215
4216 // create a dict from items on the stack
4217 case ISN_NEWDICT:
4218 {
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01004219 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004220
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01004221 SOURCING_LNUM = iptr->isn_lnum;
4222 res = exe_newdict(iptr->isn_arg.number, ectx);
4223 if (res == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004224 goto theend;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01004225 if (res == MAYBE)
4226 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004227 }
4228 break;
4229
LemonBoy372bcce2022-04-25 12:43:20 +01004230 case ISN_CONCAT:
4231 if (exe_concat(iptr->isn_arg.number, ectx) == FAIL)
4232 goto theend;
4233 break;
4234
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004235 // create a partial with NULL value
4236 case ISN_NEWPARTIAL:
4237 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
4238 goto theend;
4239 ++ectx->ec_stack.ga_len;
4240 tv = STACK_TV_BOT(-1);
4241 tv->v_type = VAR_PARTIAL;
4242 tv->v_lock = 0;
4243 tv->vval.v_partial = NULL;
4244 break;
4245
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004246 // call a :def function
4247 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02004248 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01004249 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
4250 NULL,
4251 iptr->isn_arg.dfunc.cdf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02004252 ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02004253 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004254 break;
4255
Bram Moolenaard0200c82023-01-28 15:19:40 +00004256 // call a method on an interface
4257 case ISN_METHODCALL:
4258 {
Bram Moolenaar094cf9f2023-02-10 15:52:25 +00004259 cmfunc_T *mfunc = iptr->isn_arg.mfunc;
4260
Bram Moolenaard0200c82023-01-28 15:19:40 +00004261 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar094cf9f2023-02-10 15:52:25 +00004262 tv = STACK_TV_BOT(-1 - mfunc->cmf_argcount);
Bram Moolenaard0200c82023-01-28 15:19:40 +00004263 if (tv->v_type != VAR_OBJECT)
4264 {
4265 object_required_error(tv);
4266 goto on_error;
4267 }
4268 object_T *obj = tv->vval.v_object;
4269 class_T *cl = obj->obj_class;
4270
4271 // convert the interface index to the object index
Bram Moolenaard0200c82023-01-28 15:19:40 +00004272 int idx = object_index_from_itf_index(mfunc->cmf_itf,
Yegappan Lakshmanan5a05d372023-09-29 19:43:11 +02004273 TRUE, mfunc->cmf_idx, cl);
Bram Moolenaard0200c82023-01-28 15:19:40 +00004274
4275 if (call_ufunc(cl->class_obj_methods[idx], NULL,
4276 mfunc->cmf_argcount, ectx, NULL, NULL) == FAIL)
4277 goto on_error;
4278 }
4279 break;
4280
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004281 // call a builtin function
4282 case ISN_BCALL:
4283 SOURCING_LNUM = iptr->isn_lnum;
4284 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
4285 iptr->isn_arg.bfunc.cbf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02004286 ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004287 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004288 break;
4289
4290 // call a funcref or partial
4291 case ISN_PCALL:
4292 {
4293 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
4294 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02004295 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004296
4297 SOURCING_LNUM = iptr->isn_lnum;
4298 if (pfunc->cpf_top)
4299 {
4300 // funcref is above the arguments
4301 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
4302 }
4303 else
4304 {
4305 // Get the funcref from the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004306 --ectx->ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02004307 partial_tv = *STACK_TV_BOT(0);
4308 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004309 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004310 r = call_partial(tv, pfunc->cpf_argcount, ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02004311 if (tv == &partial_tv)
4312 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004313 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004314 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004315 }
4316 break;
4317
Bram Moolenaarbd5da372020-03-31 23:13:10 +02004318 case ISN_PCALL_END:
4319 // PCALL finished, arguments have been consumed and replaced by
4320 // the return value. Now clear the funcref from the stack,
4321 // and move the return value in its place.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004322 --ectx->ec_stack.ga_len;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02004323 clear_tv(STACK_TV_BOT(-1));
4324 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
4325 break;
4326
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004327 // call a user defined function or funcref/partial
4328 case ISN_UCALL:
4329 {
4330 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
4331
4332 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01004333 if (call_eval_func(cufunc->cuf_name, cufunc->cuf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02004334 ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004335 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004336 }
4337 break;
4338
Bram Moolenaar1d84f762022-09-03 21:35:53 +01004339 // :defer func(arg)
4340 case ISN_DEFER:
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00004341 case ISN_DEFEROBJ:
Bram Moolenaar806a2732022-09-04 15:40:36 +01004342 if (defer_command(iptr->isn_arg.defer.defer_var_idx,
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00004343 iptr->isn_type == ISN_DEFEROBJ,
Bram Moolenaar1d84f762022-09-03 21:35:53 +01004344 iptr->isn_arg.defer.defer_argcount, ectx) == FAIL)
4345 goto on_error;
4346 break;
4347
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004348 // Return from a :def function call without a value.
4349 // Return from a constructor.
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02004350 case ISN_RETURN_VOID:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004351 case ISN_RETURN_OBJECT:
Bram Moolenaar35578162021-08-02 19:10:38 +02004352 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004353 goto theend;
Bram Moolenaar299f3032021-01-08 20:53:09 +01004354 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004355 ++ectx->ec_stack.ga_len;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004356 if (iptr->isn_type == ISN_RETURN_VOID)
4357 {
4358 tv->v_type = VAR_VOID;
4359 tv->vval.v_number = 0;
4360 tv->v_lock = 0;
4361 }
4362 else
4363 {
4364 *tv = *STACK_TV_VAR(0);
4365 ++tv->vval.v_object->obj_refcount;
4366 }
Bram Moolenaar299f3032021-01-08 20:53:09 +01004367 // FALLTHROUGH
4368
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02004369 // return from a :def function call with what is on the stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004370 case ISN_RETURN:
4371 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004372 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01004373 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01004374
4375 if (trystack->ga_len > 0)
4376 trycmd = ((trycmd_T *)trystack->ga_data)
4377 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004378 if (trycmd != NULL
Bram Moolenaar4c137212021-04-19 16:48:48 +02004379 && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004380 {
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01004381 // jump to ":finally" or ":endtry"
4382 if (trycmd->tcd_finally_idx != 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004383 ectx->ec_iidx = trycmd->tcd_finally_idx;
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01004384 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02004385 ectx->ec_iidx = trycmd->tcd_endtry_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004386 trycmd->tcd_return = TRUE;
4387 }
4388 else
Bram Moolenaard032f342020-07-18 18:13:02 +02004389 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004390 }
4391 break;
4392
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004393 // push a partial, a reference to a compiled function
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004394 case ISN_FUNCREF:
4395 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004396 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
4397 ufunc_T *ufunc;
4398 funcref_T *funcref = &iptr->isn_arg.funcref;
4399 funcref_extra_T *extra = funcref->fr_extra;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004400
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004401 if (pt == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004402 goto theend;
Bram Moolenaar35578162021-08-02 19:10:38 +02004403 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02004404 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004405 vim_free(pt);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004406 goto theend;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004407 }
Bram Moolenaar313e4722023-02-08 20:55:27 +00004408 if (extra != NULL && extra->fre_class != NULL)
4409 {
4410 tv = STACK_TV_BOT(-1);
4411 if (tv->v_type != VAR_OBJECT)
4412 {
4413 object_required_error(tv);
4414 vim_free(pt);
4415 goto on_error;
4416 }
4417 object_T *obj = tv->vval.v_object;
4418 class_T *cl = obj->obj_class;
4419
4420 // convert the interface index to the object index
4421 int idx = object_index_from_itf_index(extra->fre_class,
Yegappan Lakshmanan5a05d372023-09-29 19:43:11 +02004422 TRUE, extra->fre_method_idx, cl);
Bram Moolenaar313e4722023-02-08 20:55:27 +00004423 ufunc = cl->class_obj_methods[idx];
4424 }
4425 else if (extra == NULL || extra->fre_func_name == NULL)
Bram Moolenaar38453522021-11-28 22:00:12 +00004426 {
4427 dfunc_T *pt_dfunc = ((dfunc_T *)def_functions.ga_data)
4428 + funcref->fr_dfunc_idx;
4429
4430 ufunc = pt_dfunc->df_ufunc;
4431 }
4432 else
Bram Moolenaar313e4722023-02-08 20:55:27 +00004433 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004434 ufunc = find_func(extra->fre_func_name, FALSE);
Bram Moolenaar313e4722023-02-08 20:55:27 +00004435 }
Bram Moolenaar56acd1f2022-02-18 13:24:52 +00004436 if (ufunc == NULL)
4437 {
4438 SOURCING_LNUM = iptr->isn_lnum;
4439 iemsg("ufunc unexpectedly NULL for FUNCREF");
4440 goto theend;
4441 }
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004442 if (fill_partial_and_closure(pt, ufunc,
Bram Moolenaarcc341812022-09-19 15:54:34 +01004443 extra == NULL ? NULL : &extra->fre_loopvar_info,
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004444 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004445 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004446 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004447 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004448 tv->vval.v_partial = pt;
4449 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02004450 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004451 }
4452 break;
4453
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004454 // Create a global function from a lambda.
4455 case ISN_NEWFUNC:
4456 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004457 newfuncarg_T *arg = iptr->isn_arg.newfunc.nf_arg;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004458
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004459 if (copy_lambda_to_global_func(arg->nfa_lambda,
Bram Moolenaarcc341812022-09-19 15:54:34 +01004460 arg->nfa_global, &arg->nfa_loopvar_info,
4461 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004462 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004463 }
4464 break;
4465
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004466 // List functions
4467 case ISN_DEF:
4468 if (iptr->isn_arg.string == NULL)
4469 list_functions(NULL);
4470 else
4471 {
Bram Moolenaar14336722022-01-08 16:02:59 +00004472 exarg_T ea;
4473 garray_T lines_to_free;
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004474
4475 CLEAR_FIELD(ea);
4476 ea.cmd = ea.arg = iptr->isn_arg.string;
Bram Moolenaar14336722022-01-08 16:02:59 +00004477 ga_init2(&lines_to_free, sizeof(char_u *), 50);
Bram Moolenaard4a9b7f2023-05-23 14:48:42 +01004478 SOURCING_LNUM = iptr->isn_lnum;
h-eastb895b0f2023-09-24 15:46:31 +02004479 define_function(&ea, NULL, &lines_to_free, 0, NULL, 0);
Bram Moolenaar14336722022-01-08 16:02:59 +00004480 ga_clear_strings(&lines_to_free);
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004481 }
4482 break;
4483
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004484 // jump if a condition is met
4485 case ISN_JUMP:
4486 {
4487 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004488 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004489 int jump = TRUE;
4490
4491 if (when != JUMP_ALWAYS)
4492 {
4493 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004494 if (when == JUMP_IF_COND_FALSE
Bram Moolenaar13106602020-10-04 16:06:05 +02004495 || when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004496 || when == JUMP_IF_COND_TRUE)
4497 {
4498 SOURCING_LNUM = iptr->isn_lnum;
4499 jump = tv_get_bool_chk(tv, &error);
4500 if (error)
4501 goto on_error;
4502 }
4503 else
4504 jump = tv2bool(tv);
Bram Moolenaarf6ced982022-04-28 12:00:49 +01004505 if (when == JUMP_IF_FALSE || when == JUMP_IF_COND_FALSE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004506 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01004507 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004508 {
4509 // drop the value from the stack
4510 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004511 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004512 }
4513 }
4514 if (jump)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004515 ectx->ec_iidx = iptr->isn_arg.jump.jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004516 }
4517 break;
4518
Bram Moolenaarb46c0832022-09-15 17:19:37 +01004519 // "while": jump to end if a condition is false
4520 case ISN_WHILE:
4521 {
4522 int error = FALSE;
4523 int jump = TRUE;
4524
4525 tv = STACK_TV_BOT(-1);
4526 SOURCING_LNUM = iptr->isn_lnum;
4527 jump = !tv_get_bool_chk(tv, &error);
4528 if (error)
4529 goto on_error;
4530 // drop the value from the stack
4531 clear_tv(tv);
4532 --ectx->ec_stack.ga_len;
4533 if (jump)
4534 ectx->ec_iidx = iptr->isn_arg.whileloop.while_end;
4535
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01004536 // Store the current funcref count, may be used by
Bram Moolenaarcc341812022-09-19 15:54:34 +01004537 // ISN_ENDLOOP later
Bram Moolenaarb46c0832022-09-15 17:19:37 +01004538 tv = STACK_TV_VAR(
4539 iptr->isn_arg.whileloop.while_funcref_idx);
4540 tv->vval.v_number = ectx->ec_funcrefs.ga_len;
4541 }
4542 break;
4543
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02004544 // Jump if an argument with a default value was already set and not
4545 // v:none.
4546 case ISN_JUMP_IF_ARG_SET:
Bram Moolenaar65b0d162022-12-13 18:43:22 +00004547 case ISN_JUMP_IF_ARG_NOT_SET:
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02004548 tv = STACK_TV_VAR(iptr->isn_arg.jumparg.jump_arg_off);
Bram Moolenaar65b0d162022-12-13 18:43:22 +00004549 int arg_set = tv->v_type != VAR_UNKNOWN
4550 && !(tv->v_type == VAR_SPECIAL
4551 && tv->vval.v_number == VVAL_NONE);
4552 if (iptr->isn_type == ISN_JUMP_IF_ARG_SET ? arg_set : !arg_set)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004553 ectx->ec_iidx = iptr->isn_arg.jumparg.jump_where;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02004554 break;
4555
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004556 // top of a for loop
4557 case ISN_FOR:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00004558 if (execute_for(iptr, ectx) == FAIL)
4559 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004560 break;
4561
Bram Moolenaarb46c0832022-09-15 17:19:37 +01004562 // end of a for or while loop
4563 case ISN_ENDLOOP:
4564 if (execute_endloop(iptr, ectx) == FAIL)
4565 goto theend;
4566 break;
4567
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004568 // start of ":try" block
4569 case ISN_TRY:
4570 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01004571 trycmd_T *trycmd = NULL;
4572
Bram Moolenaar35578162021-08-02 19:10:38 +02004573 if (GA_GROW_FAILS(&ectx->ec_trystack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004574 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004575 trycmd = ((trycmd_T *)ectx->ec_trystack.ga_data)
4576 + ectx->ec_trystack.ga_len;
4577 ++ectx->ec_trystack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004578 ++trylevel;
Bram Moolenaar8d4be892021-02-13 18:33:02 +01004579 CLEAR_POINTER(trycmd);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004580 trycmd->tcd_frame_idx = ectx->ec_frame_idx;
4581 trycmd->tcd_stack_len = ectx->ec_stack.ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01004582 trycmd->tcd_catch_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00004583 iptr->isn_arg.tryref.try_ref->try_catch;
Bram Moolenaara91a7132021-03-25 21:12:15 +01004584 trycmd->tcd_finally_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00004585 iptr->isn_arg.tryref.try_ref->try_finally;
Bram Moolenaara91a7132021-03-25 21:12:15 +01004586 trycmd->tcd_endtry_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00004587 iptr->isn_arg.tryref.try_ref->try_endtry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004588 }
4589 break;
4590
4591 case ISN_PUSHEXC:
4592 if (current_exception == NULL)
4593 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004594 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004595 iemsg("Evaluating catch while current_exception is NULL");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004596 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004597 }
Bram Moolenaar35578162021-08-02 19:10:38 +02004598 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004599 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004600 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004601 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004602 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02004603 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004604 tv->vval.v_string = vim_strsave(
4605 (char_u *)current_exception->value);
4606 break;
4607
4608 case ISN_CATCH:
4609 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004610 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar06651632022-04-27 17:54:25 +01004611 trycmd_T *trycmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004612
Bram Moolenaar4c137212021-04-19 16:48:48 +02004613 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar06651632022-04-27 17:54:25 +01004614 trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004615 + trystack->ga_len - 1;
Bram Moolenaar06651632022-04-27 17:54:25 +01004616 trycmd->tcd_caught = TRUE;
4617 trycmd->tcd_did_throw = FALSE;
4618
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004619 did_emsg = got_int = did_throw = FALSE;
Bram Moolenaar1430cee2021-01-17 19:20:32 +01004620 force_abort = need_rethrow = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004621 catch_exception(current_exception);
4622 }
4623 break;
4624
Bram Moolenaarc150c092021-02-13 15:02:46 +01004625 case ISN_TRYCONT:
4626 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004627 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004628 trycont_T *trycont = &iptr->isn_arg.trycont;
4629 int i;
4630 trycmd_T *trycmd;
4631 int iidx = trycont->tct_where;
4632
4633 if (trystack->ga_len < trycont->tct_levels)
4634 {
4635 siemsg("TRYCONT: expected %d levels, found %d",
4636 trycont->tct_levels, trystack->ga_len);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004637 goto theend;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004638 }
4639 // Make :endtry jump to any outer try block and the last
4640 // :endtry inside the loop to the loop start.
4641 for (i = trycont->tct_levels; i > 0; --i)
4642 {
4643 trycmd = ((trycmd_T *)trystack->ga_data)
4644 + trystack->ga_len - i;
Bram Moolenaar2e34c342021-03-14 12:13:33 +01004645 // Add one to tcd_cont to be able to jump to
4646 // instruction with index zero.
4647 trycmd->tcd_cont = iidx + 1;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01004648 iidx = trycmd->tcd_finally_idx == 0
4649 ? trycmd->tcd_endtry_idx : trycmd->tcd_finally_idx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004650 }
4651 // jump to :finally or :endtry of current try statement
Bram Moolenaar4c137212021-04-19 16:48:48 +02004652 ectx->ec_iidx = iidx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004653 }
4654 break;
4655
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01004656 case ISN_FINALLY:
4657 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004658 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01004659 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
4660 + trystack->ga_len - 1;
4661
4662 // Reset the index to avoid a return statement jumps here
4663 // again.
4664 trycmd->tcd_finally_idx = 0;
4665 break;
4666 }
4667
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004668 // end of ":try" block
4669 case ISN_ENDTRY:
4670 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004671 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar06651632022-04-27 17:54:25 +01004672 trycmd_T *trycmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004673
Bram Moolenaar06651632022-04-27 17:54:25 +01004674 --trystack->ga_len;
4675 --trylevel;
4676 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len;
4677 if (trycmd->tcd_did_throw)
4678 did_throw = TRUE;
4679 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004680 {
Bram Moolenaar06651632022-04-27 17:54:25 +01004681 // discard the exception
4682 if (caught_stack == current_exception)
4683 caught_stack = caught_stack->caught;
4684 discard_current_exception();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004685 }
Bram Moolenaar06651632022-04-27 17:54:25 +01004686
4687 if (trycmd->tcd_return)
4688 goto func_return;
4689
4690 while (ectx->ec_stack.ga_len > trycmd->tcd_stack_len)
4691 {
4692 --ectx->ec_stack.ga_len;
4693 clear_tv(STACK_TV_BOT(0));
4694 }
4695 if (trycmd->tcd_cont != 0)
4696 // handling :continue: jump to outer try block or
4697 // start of the loop
4698 ectx->ec_iidx = trycmd->tcd_cont - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004699 }
4700 break;
4701
4702 case ISN_THROW:
Bram Moolenaar8f81b222021-01-14 21:47:06 +01004703 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004704 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02004705
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004706 if (trystack->ga_len == 0 && trylevel == 0 && emsg_silent)
4707 {
Bram Moolenaar3ef2e412023-04-30 18:50:48 +01004708 // Throwing an exception while using "silent!" causes
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004709 // the function to abort but not display an error.
4710 tv = STACK_TV_BOT(-1);
4711 clear_tv(tv);
4712 tv->v_type = VAR_NUMBER;
4713 tv->vval.v_number = 0;
4714 goto done;
4715 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004716 --ectx->ec_stack.ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004717 tv = STACK_TV_BOT(0);
4718 if (tv->vval.v_string == NULL
4719 || *skipwhite(tv->vval.v_string) == NUL)
4720 {
4721 vim_free(tv->vval.v_string);
4722 SOURCING_LNUM = iptr->isn_lnum;
4723 emsg(_(e_throw_with_empty_string));
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004724 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004725 }
4726
4727 // Inside a "catch" we need to first discard the caught
4728 // exception.
4729 if (trystack->ga_len > 0)
4730 {
4731 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
4732 + trystack->ga_len - 1;
4733 if (trycmd->tcd_caught && current_exception != NULL)
4734 {
4735 // discard the exception
4736 if (caught_stack == current_exception)
4737 caught_stack = caught_stack->caught;
4738 discard_current_exception();
4739 trycmd->tcd_caught = FALSE;
4740 }
4741 }
4742
Bram Moolenaar90a57162022-02-12 14:23:17 +00004743 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004744 if (throw_exception(tv->vval.v_string, ET_USER, NULL)
4745 == FAIL)
4746 {
4747 vim_free(tv->vval.v_string);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004748 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004749 }
4750 did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004751 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004752 break;
4753
4754 // compare with special values
4755 case ISN_COMPAREBOOL:
4756 case ISN_COMPARESPECIAL:
4757 {
4758 typval_T *tv1 = STACK_TV_BOT(-2);
4759 typval_T *tv2 = STACK_TV_BOT(-1);
4760 varnumber_T arg1 = tv1->vval.v_number;
4761 varnumber_T arg2 = tv2->vval.v_number;
4762 int res;
4763
Bram Moolenaar06651632022-04-27 17:54:25 +01004764 if (iptr->isn_arg.op.op_type == EXPR_EQUAL)
4765 res = arg1 == arg2;
4766 else
4767 res = arg1 != arg2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004768
Bram Moolenaar4c137212021-04-19 16:48:48 +02004769 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004770 tv1->v_type = VAR_BOOL;
4771 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4772 }
4773 break;
4774
Bram Moolenaar7a222242022-03-01 19:23:24 +00004775 case ISN_COMPARENULL:
4776 {
4777 typval_T *tv1 = STACK_TV_BOT(-2);
4778 typval_T *tv2 = STACK_TV_BOT(-1);
4779 int res;
4780
4781 res = typval_compare_null(tv1, tv2);
4782 if (res == MAYBE)
4783 goto on_error;
4784 if (iptr->isn_arg.op.op_type == EXPR_NEQUAL)
4785 res = !res;
4786 clear_tv(tv1);
4787 clear_tv(tv2);
4788 --ectx->ec_stack.ga_len;
4789 tv1->v_type = VAR_BOOL;
4790 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4791 }
4792 break;
4793
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004794 // Operation with two number arguments
4795 case ISN_OPNR:
4796 case ISN_COMPARENR:
4797 {
4798 typval_T *tv1 = STACK_TV_BOT(-2);
4799 typval_T *tv2 = STACK_TV_BOT(-1);
4800 varnumber_T arg1 = tv1->vval.v_number;
4801 varnumber_T arg2 = tv2->vval.v_number;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004802 varnumber_T res = 0;
4803 int div_zero = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004804
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004805 if (iptr->isn_arg.op.op_type == EXPR_LSHIFT
4806 || iptr->isn_arg.op.op_type == EXPR_RSHIFT)
4807 {
4808 if (arg2 < 0)
4809 {
4810 SOURCING_LNUM = iptr->isn_lnum;
dundargocc57b5bc2022-11-02 13:30:51 +00004811 emsg(_(e_bitshift_ops_must_be_positive));
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004812 goto on_error;
4813 }
4814 }
4815
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004816 switch (iptr->isn_arg.op.op_type)
4817 {
4818 case EXPR_MULT: res = arg1 * arg2; break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004819 case EXPR_DIV: if (arg2 == 0)
4820 div_zero = TRUE;
4821 else
4822 res = arg1 / arg2;
4823 break;
4824 case EXPR_REM: if (arg2 == 0)
4825 div_zero = TRUE;
4826 else
4827 res = arg1 % arg2;
4828 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004829 case EXPR_SUB: res = arg1 - arg2; break;
4830 case EXPR_ADD: res = arg1 + arg2; break;
4831
4832 case EXPR_EQUAL: res = arg1 == arg2; break;
4833 case EXPR_NEQUAL: res = arg1 != arg2; break;
4834 case EXPR_GREATER: res = arg1 > arg2; break;
4835 case EXPR_GEQUAL: res = arg1 >= arg2; break;
4836 case EXPR_SMALLER: res = arg1 < arg2; break;
4837 case EXPR_SEQUAL: res = arg1 <= arg2; break;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004838 case EXPR_LSHIFT: if (arg2 > MAX_LSHIFT_BITS)
4839 res = 0;
4840 else
Bram Moolenaar68e64d22022-05-22 22:07:52 +01004841 res = (uvarnumber_T)arg1 << arg2;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004842 break;
4843 case EXPR_RSHIFT: if (arg2 > MAX_LSHIFT_BITS)
4844 res = 0;
4845 else
Bram Moolenaar338bf582022-05-22 20:16:32 +01004846 res = (uvarnumber_T)arg1 >> arg2;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004847 break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004848 default: break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004849 }
4850
Bram Moolenaar4c137212021-04-19 16:48:48 +02004851 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004852 if (iptr->isn_type == ISN_COMPARENR)
4853 {
4854 tv1->v_type = VAR_BOOL;
4855 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4856 }
4857 else
4858 tv1->vval.v_number = res;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004859 if (div_zero)
4860 {
4861 SOURCING_LNUM = iptr->isn_lnum;
4862 emsg(_(e_divide_by_zero));
4863 goto on_error;
4864 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004865 }
4866 break;
4867
4868 // Computation with two float arguments
4869 case ISN_OPFLOAT:
4870 case ISN_COMPAREFLOAT:
4871 {
4872 typval_T *tv1 = STACK_TV_BOT(-2);
4873 typval_T *tv2 = STACK_TV_BOT(-1);
4874 float_T arg1 = tv1->vval.v_float;
4875 float_T arg2 = tv2->vval.v_float;
4876 float_T res = 0;
4877 int cmp = FALSE;
4878
4879 switch (iptr->isn_arg.op.op_type)
4880 {
4881 case EXPR_MULT: res = arg1 * arg2; break;
4882 case EXPR_DIV: res = arg1 / arg2; break;
4883 case EXPR_SUB: res = arg1 - arg2; break;
4884 case EXPR_ADD: res = arg1 + arg2; break;
4885
4886 case EXPR_EQUAL: cmp = arg1 == arg2; break;
4887 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
4888 case EXPR_GREATER: cmp = arg1 > arg2; break;
4889 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
4890 case EXPR_SMALLER: cmp = arg1 < arg2; break;
4891 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
4892 default: cmp = 0; break;
4893 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004894 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004895 if (iptr->isn_type == ISN_COMPAREFLOAT)
4896 {
4897 tv1->v_type = VAR_BOOL;
4898 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
4899 }
4900 else
4901 tv1->vval.v_float = res;
4902 }
4903 break;
4904
4905 case ISN_COMPARELIST:
Bram Moolenaar265f8112021-12-19 12:33:05 +00004906 case ISN_COMPAREDICT:
4907 case ISN_COMPAREFUNC:
4908 case ISN_COMPARESTRING:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004909 case ISN_COMPAREBLOB:
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00004910 case ISN_COMPARECLASS:
4911 case ISN_COMPAREOBJECT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004912 {
4913 typval_T *tv1 = STACK_TV_BOT(-2);
4914 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar265f8112021-12-19 12:33:05 +00004915 exprtype_T exprtype = iptr->isn_arg.op.op_type;
4916 int ic = iptr->isn_arg.op.op_ic;
4917 int res = FALSE;
4918 int status = OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004919
Bram Moolenaar265f8112021-12-19 12:33:05 +00004920 SOURCING_LNUM = iptr->isn_lnum;
4921 if (iptr->isn_type == ISN_COMPARELIST)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004922 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00004923 status = typval_compare_list(tv1, tv2,
4924 exprtype, ic, &res);
4925 }
4926 else if (iptr->isn_type == ISN_COMPAREDICT)
4927 {
4928 status = typval_compare_dict(tv1, tv2,
4929 exprtype, ic, &res);
4930 }
4931 else if (iptr->isn_type == ISN_COMPAREFUNC)
4932 {
4933 status = typval_compare_func(tv1, tv2,
4934 exprtype, ic, &res);
4935 }
4936 else if (iptr->isn_type == ISN_COMPARESTRING)
4937 {
4938 status = typval_compare_string(tv1, tv2,
4939 exprtype, ic, &res);
4940 }
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00004941 else if (iptr->isn_type == ISN_COMPAREBLOB)
Bram Moolenaar265f8112021-12-19 12:33:05 +00004942 {
4943 status = typval_compare_blob(tv1, tv2, exprtype, &res);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004944 }
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00004945 else if (iptr->isn_type == ISN_COMPARECLASS)
4946 {
Bram Moolenaar03ff0c62023-01-02 20:38:01 +00004947 status = typval_compare_class(tv1, tv2,
4948 exprtype, FALSE, &res);
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00004949 }
4950 else // ISN_COMPAREOBJECT
4951 {
4952 status = typval_compare_object(tv1, tv2,
Bram Moolenaar03ff0c62023-01-02 20:38:01 +00004953 exprtype, FALSE, &res);
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00004954 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004955 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004956 clear_tv(tv1);
4957 clear_tv(tv2);
4958 tv1->v_type = VAR_BOOL;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004959 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4960 if (status == FAIL)
4961 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004962 }
4963 break;
4964
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004965 case ISN_COMPAREANY:
4966 {
4967 typval_T *tv1 = STACK_TV_BOT(-2);
4968 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar657137c2021-01-09 15:45:23 +01004969 exprtype_T exprtype = iptr->isn_arg.op.op_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004970 int ic = iptr->isn_arg.op.op_ic;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004971 int status;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004972
Bram Moolenaareb26f432020-09-14 16:50:05 +02004973 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004974 status = typval_compare(tv1, tv2, exprtype, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004975 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004976 --ectx->ec_stack.ga_len;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004977 if (status == FAIL)
4978 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004979 }
4980 break;
4981
4982 case ISN_ADDLIST:
4983 case ISN_ADDBLOB:
4984 {
4985 typval_T *tv1 = STACK_TV_BOT(-2);
4986 typval_T *tv2 = STACK_TV_BOT(-1);
4987
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004988 // add two lists or blobs
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004989 if (iptr->isn_type == ISN_ADDLIST)
Bram Moolenaar07802042021-09-09 23:01:14 +02004990 {
4991 if (iptr->isn_arg.op.op_type == EXPR_APPEND
4992 && tv1->vval.v_list != NULL)
4993 list_extend(tv1->vval.v_list, tv2->vval.v_list,
4994 NULL);
4995 else
4996 eval_addlist(tv1, tv2);
4997 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004998 else
4999 eval_addblob(tv1, tv2);
5000 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005001 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005002 }
5003 break;
5004
Bram Moolenaar1dcae592020-10-19 19:02:42 +02005005 case ISN_LISTAPPEND:
5006 {
5007 typval_T *tv1 = STACK_TV_BOT(-2);
5008 typval_T *tv2 = STACK_TV_BOT(-1);
5009 list_T *l = tv1->vval.v_list;
5010
5011 // add an item to a list
Bram Moolenaar1f4a3452022-01-01 18:29:21 +00005012 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02005013 if (l == NULL)
5014 {
Bram Moolenaar1dcae592020-10-19 19:02:42 +02005015 emsg(_(e_cannot_add_to_null_list));
5016 goto on_error;
5017 }
Bram Moolenaar1f4a3452022-01-01 18:29:21 +00005018 if (value_check_lock(l->lv_lock, NULL, FALSE))
5019 goto on_error;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02005020 if (list_append_tv(l, tv2) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005021 goto theend;
Bram Moolenaar955347c2020-10-19 23:01:46 +02005022 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005023 --ectx->ec_stack.ga_len;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02005024 }
5025 break;
5026
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02005027 case ISN_BLOBAPPEND:
5028 {
5029 typval_T *tv1 = STACK_TV_BOT(-2);
5030 typval_T *tv2 = STACK_TV_BOT(-1);
5031 blob_T *b = tv1->vval.v_blob;
5032 int error = FALSE;
5033 varnumber_T n;
5034
5035 // add a number to a blob
5036 if (b == NULL)
5037 {
5038 SOURCING_LNUM = iptr->isn_lnum;
5039 emsg(_(e_cannot_add_to_null_blob));
5040 goto on_error;
5041 }
5042 n = tv_get_number_chk(tv2, &error);
5043 if (error)
5044 goto on_error;
5045 ga_append(&b->bv_ga, (int)n);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005046 --ectx->ec_stack.ga_len;
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02005047 }
5048 break;
5049
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005050 // Computation with two arguments of unknown type
5051 case ISN_OPANY:
5052 {
5053 typval_T *tv1 = STACK_TV_BOT(-2);
5054 typval_T *tv2 = STACK_TV_BOT(-1);
5055 varnumber_T n1, n2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005056 float_T f1 = 0, f2 = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005057 int error = FALSE;
5058
5059 if (iptr->isn_arg.op.op_type == EXPR_ADD)
5060 {
5061 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
5062 {
5063 eval_addlist(tv1, tv2);
5064 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005065 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005066 break;
5067 }
5068 else if (tv1->v_type == VAR_BLOB
5069 && tv2->v_type == VAR_BLOB)
5070 {
5071 eval_addblob(tv1, tv2);
5072 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005073 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005074 break;
5075 }
5076 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005077 if (tv1->v_type == VAR_FLOAT)
5078 {
5079 f1 = tv1->vval.v_float;
5080 n1 = 0;
5081 }
5082 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005083 {
Bram Moolenaarf665e972020-12-05 19:17:16 +01005084 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005085 n1 = tv_get_number_chk(tv1, &error);
5086 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02005087 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005088 if (tv2->v_type == VAR_FLOAT)
5089 f1 = n1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005090 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005091 if (tv2->v_type == VAR_FLOAT)
5092 {
5093 f2 = tv2->vval.v_float;
5094 n2 = 0;
5095 }
5096 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005097 {
5098 n2 = tv_get_number_chk(tv2, &error);
5099 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02005100 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005101 if (tv1->v_type == VAR_FLOAT)
5102 f2 = n2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005103 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005104 // if there is a float on either side the result is a float
5105 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
5106 {
5107 switch (iptr->isn_arg.op.op_type)
5108 {
5109 case EXPR_MULT: f1 = f1 * f2; break;
5110 case EXPR_DIV: f1 = f1 / f2; break;
5111 case EXPR_SUB: f1 = f1 - f2; break;
5112 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005113 default: SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005114 emsg(_(e_cannot_use_percent_with_float));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02005115 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005116 }
5117 clear_tv(tv1);
5118 clear_tv(tv2);
5119 tv1->v_type = VAR_FLOAT;
5120 tv1->vval.v_float = f1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005121 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005122 }
5123 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005124 {
Bram Moolenaarb1f28572021-01-21 13:03:20 +01005125 int failed = FALSE;
5126
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005127 switch (iptr->isn_arg.op.op_type)
5128 {
5129 case EXPR_MULT: n1 = n1 * n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01005130 case EXPR_DIV: n1 = num_divide(n1, n2, &failed);
5131 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01005132 goto on_error;
5133 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005134 case EXPR_SUB: n1 = n1 - n2; break;
5135 case EXPR_ADD: n1 = n1 + n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01005136 default: n1 = num_modulus(n1, n2, &failed);
5137 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01005138 goto on_error;
5139 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005140 }
5141 clear_tv(tv1);
5142 clear_tv(tv2);
5143 tv1->v_type = VAR_NUMBER;
5144 tv1->vval.v_number = n1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005145 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005146 }
5147 }
5148 break;
5149
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005150 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005151 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005152 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005153 int is_slice = iptr->isn_type == ISN_STRSLICE;
5154 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005155 char_u *res;
5156
5157 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005158 // string slice: string is at stack-3, first index at
5159 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005160 if (is_slice)
5161 {
5162 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005163 n1 = tv->vval.v_number;
5164 }
5165
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005166 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005167 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005168
Bram Moolenaar4c137212021-04-19 16:48:48 +02005169 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005170 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005171 if (is_slice)
5172 // Slice: Select the characters from the string
Bram Moolenaar6601b622021-01-13 21:47:15 +01005173 res = string_slice(tv->vval.v_string, n1, n2, FALSE);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005174 else
5175 // Index: The resulting variable is a string of a
Bram Moolenaar0289a092021-03-14 18:40:19 +01005176 // single character (including composing characters).
5177 // If the index is too big or negative the result is
5178 // empty.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005179 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005180 vim_free(tv->vval.v_string);
5181 tv->vval.v_string = res;
5182 }
5183 break;
5184
5185 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02005186 case ISN_LISTSLICE:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02005187 case ISN_BLOBINDEX:
5188 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005189 {
Bram Moolenaarcfc30232021-04-11 20:26:34 +02005190 int is_slice = iptr->isn_type == ISN_LISTSLICE
5191 || iptr->isn_type == ISN_BLOBSLICE;
5192 int is_blob = iptr->isn_type == ISN_BLOBINDEX
5193 || iptr->isn_type == ISN_BLOBSLICE;
Bram Moolenaared591872020-08-15 22:14:53 +02005194 varnumber_T n1, n2;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02005195 typval_T *val_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005196
5197 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02005198 // list slice: list is at stack-3, indexes at stack-2 and
5199 // stack-1
Bram Moolenaarcfc30232021-04-11 20:26:34 +02005200 // Same for blob.
5201 val_tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005202
5203 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02005204 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005205 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02005206
5207 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005208 {
Bram Moolenaared591872020-08-15 22:14:53 +02005209 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02005210 n1 = tv->vval.v_number;
5211 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005212 }
Bram Moolenaared591872020-08-15 22:14:53 +02005213
Bram Moolenaar4c137212021-04-19 16:48:48 +02005214 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02005215 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02005216 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02005217 if (is_blob)
5218 {
5219 if (blob_slice_or_index(val_tv->vval.v_blob, is_slice,
5220 n1, n2, FALSE, tv) == FAIL)
5221 goto on_error;
5222 }
5223 else
5224 {
5225 if (list_slice_or_index(val_tv->vval.v_list, is_slice,
5226 n1, n2, FALSE, tv, TRUE) == FAIL)
5227 goto on_error;
5228 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005229 }
5230 break;
5231
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005232 case ISN_ANYINDEX:
5233 case ISN_ANYSLICE:
5234 {
5235 int is_slice = iptr->isn_type == ISN_ANYSLICE;
5236 typval_T *var1, *var2;
5237 int res;
5238
5239 // index: composite is at stack-2, index at stack-1
5240 // slice: composite is at stack-3, indexes at stack-2 and
5241 // stack-1
5242 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02005243 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005244 if (check_can_index(tv, TRUE, TRUE) == FAIL)
5245 goto on_error;
5246 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
5247 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
Bram Moolenaar6601b622021-01-13 21:47:15 +01005248 res = eval_index_inner(tv, is_slice, var1, var2,
5249 FALSE, NULL, -1, TRUE);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005250 clear_tv(var1);
5251 if (is_slice)
5252 clear_tv(var2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005253 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005254 if (res == FAIL)
5255 goto on_error;
5256 }
5257 break;
5258
Bram Moolenaar9af78762020-06-16 11:34:42 +02005259 case ISN_SLICE:
5260 {
5261 list_T *list;
5262 int count = iptr->isn_arg.number;
5263
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02005264 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02005265 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02005266 list = tv->vval.v_list;
5267
5268 // no error for short list, expect it to be checked earlier
5269 if (list != NULL && list->lv_len >= count)
5270 {
5271 list_T *newlist = list_slice(list,
5272 count, list->lv_len - 1);
5273
5274 if (newlist != NULL)
5275 {
5276 list_unref(list);
5277 tv->vval.v_list = newlist;
5278 ++newlist->lv_refcount;
5279 }
5280 }
5281 }
5282 break;
5283
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005284 case ISN_GETITEM:
5285 {
5286 listitem_T *li;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02005287 getitem_T *gi = &iptr->isn_arg.getitem;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005288
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02005289 // Get list item: list is at stack-1, push item.
5290 // List type and length is checked for when compiling.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02005291 tv = STACK_TV_BOT(-1 - gi->gi_with_op);
5292 li = list_find(tv->vval.v_list, gi->gi_index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005293
Bram Moolenaar35578162021-08-02 19:10:38 +02005294 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005295 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005296 ++ectx->ec_stack.ga_len;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005297 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
Bram Moolenaarf785aa12021-02-11 21:19:34 +01005298
5299 // Useful when used in unpack assignment. Reset at
5300 // ISN_DROP.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02005301 ectx->ec_where.wt_index = gi->gi_index + 1;
LemonBoyc5d27442023-08-19 13:02:35 +02005302 ectx->ec_where.wt_kind = WT_VARIABLE;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005303 }
5304 break;
5305
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005306 case ISN_MEMBER:
5307 {
5308 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005309 char_u *key;
5310 dictitem_T *di;
5311
5312 // dict member: dict is at stack-2, key at stack-1
5313 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02005314 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005315 dict = tv->vval.v_dict;
5316
5317 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02005318 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005319 key = tv->vval.v_string;
Bram Moolenaar086fc9a2020-10-30 18:33:02 +01005320 if (key == NULL)
5321 key = (char_u *)"";
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02005322
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005323 if ((di = dict_find(dict, key, -1)) == NULL)
5324 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005325 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00005326 semsg(_(e_key_not_present_in_dictionary_str), key);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01005327
5328 // If :silent! is used we will continue, make sure the
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005329 // stack contents makes sense and the dict stack is
5330 // updated.
Bram Moolenaar4029cab2020-12-05 18:13:27 +01005331 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005332 --ectx->ec_stack.ga_len;
Bram Moolenaar4029cab2020-12-05 18:13:27 +01005333 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005334 (void) dict_stack_save(tv);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01005335 tv->v_type = VAR_NUMBER;
5336 tv->vval.v_number = 0;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01005337 goto on_fatal_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005338 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005339 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005340 --ectx->ec_stack.ga_len;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005341 // Put the dict used on the dict stack, it might be used by
5342 // a dict function later.
Bram Moolenaar50788ef2020-07-05 16:51:26 +02005343 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005344 if (dict_stack_save(tv) == FAIL)
5345 goto on_fatal_error;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02005346 copy_tv(&di->di_tv, tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005347 }
5348 break;
5349
5350 // dict member with string key
5351 case ISN_STRINGMEMBER:
5352 {
5353 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005354 dictitem_T *di;
5355
5356 tv = STACK_TV_BOT(-1);
5357 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
5358 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005359 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005360 emsg(_(e_dictionary_required));
Bram Moolenaard032f342020-07-18 18:13:02 +02005361 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005362 }
5363 dict = tv->vval.v_dict;
5364
5365 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
5366 == NULL)
5367 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005368 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00005369 semsg(_(e_key_not_present_in_dictionary_str),
Bram Moolenaar381692b2022-02-02 20:01:27 +00005370 iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02005371 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005372 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005373 // Put the dict used on the dict stack, it might be used by
5374 // a dict function later.
5375 if (dict_stack_save(tv) == FAIL)
5376 goto on_fatal_error;
5377
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005378 copy_tv(&di->di_tv, tv);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005379 }
5380 break;
5381
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005382 case ISN_GET_OBJ_MEMBER:
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00005383 case ISN_GET_ITF_MEMBER:
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005384 {
5385 tv = STACK_TV_BOT(-1);
5386 if (tv->v_type != VAR_OBJECT)
5387 {
5388 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaard0200c82023-01-28 15:19:40 +00005389 object_required_error(tv);
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005390 goto on_error;
5391 }
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00005392
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005393 object_T *obj = tv->vval.v_object;
Bram Moolenaarc3f971f2023-03-02 17:38:33 +00005394 if (obj == NULL)
5395 {
5396 SOURCING_LNUM = iptr->isn_lnum;
5397 emsg(_(e_using_null_object));
5398 goto on_error;
5399 }
5400
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00005401 int idx;
5402 if (iptr->isn_type == ISN_GET_OBJ_MEMBER)
Ernie Rael18143d32023-09-04 22:30:41 +02005403 idx = iptr->isn_arg.classmember.cm_idx;
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00005404 else
5405 {
5406 idx = iptr->isn_arg.classmember.cm_idx;
5407 // convert the interface index to the object index
5408 idx = object_index_from_itf_index(
Ernie Rael18143d32023-09-04 22:30:41 +02005409 iptr->isn_arg.classmember.cm_class,
Yegappan Lakshmanan5a05d372023-09-29 19:43:11 +02005410 FALSE, idx, obj->obj_class);
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00005411 }
5412
Ernie Rael18143d32023-09-04 22:30:41 +02005413 // The members are located right after the object struct.
Yegappan Lakshmanan5a05d372023-09-29 19:43:11 +02005414 typval_T *mtv = ((typval_T *)(obj + 1)) + idx;
Bram Moolenaar590162c2022-12-24 21:24:06 +00005415 copy_tv(mtv, tv);
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005416
5417 // Unreference the object after getting the member, it may
5418 // be freed.
5419 object_unref(obj);
5420 }
5421 break;
5422
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005423 case ISN_STORE_THIS:
5424 {
5425 int idx = iptr->isn_arg.number;
5426 object_T *obj = STACK_TV_VAR(0)->vval.v_object;
5427 // the members are located right after the object struct
5428 typval_T *mtv = ((typval_T *)(obj + 1)) + idx;
5429 clear_tv(mtv);
5430 *mtv = *STACK_TV_BOT(-1);
5431 --ectx->ec_stack.ga_len;
5432 }
5433 break;
5434
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005435 case ISN_CLEARDICT:
5436 dict_stack_drop();
5437 break;
5438
5439 case ISN_USEDICT:
5440 {
5441 typval_T *dict_tv = dict_stack_get_tv();
5442
5443 // Turn "dict.Func" into a partial for "Func" bound to
5444 // "dict". Don't do this when "Func" is already a partial
5445 // that was bound explicitly (pt_auto is FALSE).
5446 tv = STACK_TV_BOT(-1);
5447 if (dict_tv != NULL
5448 && dict_tv->v_type == VAR_DICT
5449 && dict_tv->vval.v_dict != NULL
5450 && (tv->v_type == VAR_FUNC
5451 || (tv->v_type == VAR_PARTIAL
5452 && (tv->vval.v_partial->pt_auto
5453 || tv->vval.v_partial->pt_dict == NULL))))
5454 dict_tv->vval.v_dict =
5455 make_partial(dict_tv->vval.v_dict, tv);
5456 dict_stack_drop();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005457 }
5458 break;
5459
5460 case ISN_NEGATENR:
5461 tv = STACK_TV_BOT(-1);
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00005462 // CHECKTYPE should have checked the variable type
Bram Moolenaarc58164c2020-03-29 18:40:30 +02005463 if (tv->v_type == VAR_FLOAT)
5464 tv->vval.v_float = -tv->vval.v_float;
5465 else
Bram Moolenaarc58164c2020-03-29 18:40:30 +02005466 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005467 break;
5468
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005469 case ISN_CHECKTYPE:
5470 {
5471 checktype_T *ct = &iptr->isn_arg.type;
Bram Moolenaarb1040dc2022-05-18 11:00:48 +01005472 int r;
LemonBoyc5d27442023-08-19 13:02:35 +02005473 where_T where = WHERE_INIT;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005474
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01005475 tv = STACK_TV_BOT((int)ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02005476 SOURCING_LNUM = iptr->isn_lnum;
LemonBoyc5d27442023-08-19 13:02:35 +02005477 if (ct->ct_arg_idx > 0)
5478 {
5479 where.wt_index = ct->ct_arg_idx;
5480 where.wt_kind = ct->ct_is_var ? WT_VARIABLE : WT_ARGUMENT;
LemonBoyc5d27442023-08-19 13:02:35 +02005481 }
LemonBoyf244b2f2023-08-19 16:02:04 +02005482 where.wt_func_name = ectx->ec_where.wt_func_name;
LemonBoyc5d27442023-08-19 13:02:35 +02005483 r = check_typval_type(ct->ct_type, tv, where);
Bram Moolenaarb1040dc2022-05-18 11:00:48 +01005484 if (r == FAIL)
5485 goto on_error;
Bram Moolenaar5e654232020-09-16 15:22:00 +02005486
5487 // number 0 is FALSE, number 1 is TRUE
5488 if (tv->v_type == VAR_NUMBER
5489 && ct->ct_type->tt_type == VAR_BOOL
5490 && (tv->vval.v_number == 0
5491 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005492 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02005493 tv->v_type = VAR_BOOL;
5494 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02005495 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005496 }
5497 }
5498 break;
5499
Bram Moolenaar9af78762020-06-16 11:34:42 +02005500 case ISN_CHECKLEN:
5501 {
5502 int min_len = iptr->isn_arg.checklen.cl_min_len;
5503 list_T *list = NULL;
5504
5505 tv = STACK_TV_BOT(-1);
5506 if (tv->v_type == VAR_LIST)
5507 list = tv->vval.v_list;
5508 if (list == NULL || list->lv_len < min_len
5509 || (list->lv_len > min_len
5510 && !iptr->isn_arg.checklen.cl_more_OK))
5511 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005512 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005513 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02005514 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02005515 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02005516 }
5517 }
5518 break;
5519
Bram Moolenaaraa210a32021-01-02 15:41:03 +01005520 case ISN_SETTYPE:
Bram Moolenaar381692b2022-02-02 20:01:27 +00005521 set_tv_type(STACK_TV_BOT(-1), iptr->isn_arg.type.ct_type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01005522 break;
5523
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005524 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005525 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005526 {
5527 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005528 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005529
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005530 if (iptr->isn_type == ISN_2BOOL)
5531 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005532 tv = STACK_TV_BOT(iptr->isn_arg.tobool.offset);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005533 n = tv2bool(tv);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005534 if (iptr->isn_arg.tobool.invert)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005535 n = !n;
5536 }
5537 else
5538 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005539 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005540 SOURCING_LNUM = iptr->isn_lnum;
5541 n = tv_get_bool_chk(tv, &error);
5542 if (error)
5543 goto on_error;
5544 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005545 clear_tv(tv);
5546 tv->v_type = VAR_BOOL;
5547 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
5548 }
5549 break;
5550
5551 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02005552 case ISN_2STRING_ANY:
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01005553 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005554 if (do_2string(STACK_TV_BOT(iptr->isn_arg.tostring.offset),
5555 iptr->isn_type == ISN_2STRING_ANY,
5556 iptr->isn_arg.tostring.tolerant) == FAIL)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01005557 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005558 break;
5559
Bram Moolenaar08597872020-12-10 19:43:40 +01005560 case ISN_RANGE:
5561 {
5562 exarg_T ea;
5563 char *errormsg;
5564
Bram Moolenaarece0b872021-01-08 20:40:45 +01005565 ea.line2 = 0;
Bram Moolenaard1510ee2021-01-04 16:15:58 +01005566 ea.addr_count = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01005567 ea.addr_type = ADDR_LINES;
5568 ea.cmd = iptr->isn_arg.string;
Bram Moolenaarece0b872021-01-08 20:40:45 +01005569 ea.skip = FALSE;
Bram Moolenaar08597872020-12-10 19:43:40 +01005570 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaarece0b872021-01-08 20:40:45 +01005571 goto on_error;
Bram Moolenaara28639e2021-01-19 22:48:09 +01005572
Bram Moolenaar35578162021-08-02 19:10:38 +02005573 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005574 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005575 ++ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01005576 tv = STACK_TV_BOT(-1);
5577 tv->v_type = VAR_NUMBER;
5578 tv->v_lock = 0;
Bram Moolenaar06651632022-04-27 17:54:25 +01005579 tv->vval.v_number = ea.line2;
Bram Moolenaar08597872020-12-10 19:43:40 +01005580 }
5581 break;
5582
Bram Moolenaarc3516f72020-09-08 22:45:35 +02005583 case ISN_PUT:
5584 {
5585 int regname = iptr->isn_arg.put.put_regname;
5586 linenr_T lnum = iptr->isn_arg.put.put_lnum;
5587 char_u *expr = NULL;
5588 int dir = FORWARD;
5589
Bram Moolenaar08597872020-12-10 19:43:40 +01005590 if (lnum < -2)
5591 {
5592 // line number was put on the stack by ISN_RANGE
5593 tv = STACK_TV_BOT(-1);
5594 curwin->w_cursor.lnum = tv->vval.v_number;
5595 if (lnum == LNUM_VARIABLE_RANGE_ABOVE)
5596 dir = BACKWARD;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005597 --ectx->ec_stack.ga_len;
Bram Moolenaar08597872020-12-10 19:43:40 +01005598 }
5599 else if (lnum == -2)
Bram Moolenaarc3516f72020-09-08 22:45:35 +02005600 // :put! above cursor
5601 dir = BACKWARD;
5602 else if (lnum >= 0)
Bram Moolenaar4e713ba2022-02-07 15:31:37 +00005603 {
5604 curwin->w_cursor.lnum = lnum;
5605 if (lnum == 0)
5606 // check_cursor() below will move to line 1
5607 dir = BACKWARD;
5608 }
Bram Moolenaara28639e2021-01-19 22:48:09 +01005609
5610 if (regname == '=')
5611 {
5612 tv = STACK_TV_BOT(-1);
5613 if (tv->v_type == VAR_STRING)
5614 expr = tv->vval.v_string;
5615 else
5616 {
5617 expr = typval2string(tv, TRUE); // allocates value
5618 clear_tv(tv);
5619 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005620 --ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01005621 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02005622 check_cursor();
5623 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
5624 vim_free(expr);
5625 }
5626 break;
5627
Bram Moolenaar02194d22020-10-24 23:08:38 +02005628 case ISN_CMDMOD:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005629 ectx->ec_funclocal.floc_save_cmdmod = cmdmod;
5630 ectx->ec_funclocal.floc_restore_cmdmod = TRUE;
5631 ectx->ec_funclocal.floc_restore_cmdmod_stacklen =
5632 ectx->ec_stack.ga_len;
Bram Moolenaar02194d22020-10-24 23:08:38 +02005633 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
5634 apply_cmdmod(&cmdmod);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02005635 break;
5636
Bram Moolenaar02194d22020-10-24 23:08:38 +02005637 case ISN_CMDMOD_REV:
5638 // filter regprog is owned by the instruction, don't free it
5639 cmdmod.cmod_filter_regmatch.regprog = NULL;
5640 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005641 cmdmod = ectx->ec_funclocal.floc_save_cmdmod;
5642 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02005643 break;
5644
Bram Moolenaar792f7862020-11-23 08:31:18 +01005645 case ISN_UNPACK:
5646 {
5647 int count = iptr->isn_arg.unpack.unp_count;
5648 int semicolon = iptr->isn_arg.unpack.unp_semicolon;
5649 list_T *l;
5650 listitem_T *li;
5651 int i;
5652
5653 // Check there is a valid list to unpack.
5654 tv = STACK_TV_BOT(-1);
5655 if (tv->v_type != VAR_LIST)
5656 {
5657 SOURCING_LNUM = iptr->isn_lnum;
5658 emsg(_(e_for_argument_must_be_sequence_of_lists));
5659 goto on_error;
5660 }
5661 l = tv->vval.v_list;
5662 if (l == NULL
5663 || l->lv_len < (semicolon ? count - 1 : count))
5664 {
5665 SOURCING_LNUM = iptr->isn_lnum;
5666 emsg(_(e_list_value_does_not_have_enough_items));
5667 goto on_error;
5668 }
5669 else if (!semicolon && l->lv_len > count)
5670 {
5671 SOURCING_LNUM = iptr->isn_lnum;
5672 emsg(_(e_list_value_has_more_items_than_targets));
5673 goto on_error;
5674 }
5675
5676 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar35578162021-08-02 19:10:38 +02005677 if (GA_GROW_FAILS(&ectx->ec_stack, count - 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005678 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005679 ectx->ec_stack.ga_len += count - 1;
Bram Moolenaar792f7862020-11-23 08:31:18 +01005680
5681 // Variable after semicolon gets a list with the remaining
5682 // items.
5683 if (semicolon)
5684 {
5685 list_T *rem_list =
5686 list_alloc_with_items(l->lv_len - count + 1);
5687
5688 if (rem_list == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005689 goto theend;
Bram Moolenaar792f7862020-11-23 08:31:18 +01005690 tv = STACK_TV_BOT(-count);
5691 tv->vval.v_list = rem_list;
5692 ++rem_list->lv_refcount;
5693 tv->v_lock = 0;
5694 li = l->lv_first;
5695 for (i = 0; i < count - 1; ++i)
5696 li = li->li_next;
5697 for (i = 0; li != NULL; ++i)
5698 {
Bram Moolenaar61efa162022-03-18 13:10:48 +00005699 typval_T tvcopy;
5700
5701 copy_tv(&li->li_tv, &tvcopy);
5702 list_set_item(rem_list, i, &tvcopy);
Bram Moolenaar792f7862020-11-23 08:31:18 +01005703 li = li->li_next;
5704 }
5705 --count;
5706 }
5707
5708 // Produce the values in reverse order, first item last.
5709 li = l->lv_first;
5710 for (i = 0; i < count; ++i)
5711 {
5712 tv = STACK_TV_BOT(-i - 1);
5713 copy_tv(&li->li_tv, tv);
5714 li = li->li_next;
5715 }
5716
5717 list_unref(l);
5718 }
5719 break;
5720
Bram Moolenaarb2049902021-01-24 12:53:53 +01005721 case ISN_PROF_START:
5722 case ISN_PROF_END:
5723 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01005724#ifdef FEAT_PROFILE
Bram Moolenaarca16c602022-09-06 18:57:08 +01005725 funccall_T cookie;
5726 ufunc_T *cur_ufunc =
Bram Moolenaarb2049902021-01-24 12:53:53 +01005727 (((dfunc_T *)def_functions.ga_data)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02005728 + ectx->ec_dfunc_idx)->df_ufunc;
Bram Moolenaarb2049902021-01-24 12:53:53 +01005729
Bram Moolenaarca16c602022-09-06 18:57:08 +01005730 cookie.fc_func = cur_ufunc;
Bram Moolenaarb2049902021-01-24 12:53:53 +01005731 if (iptr->isn_type == ISN_PROF_START)
5732 {
5733 func_line_start(&cookie, iptr->isn_lnum);
5734 // if we get here the instruction is executed
5735 func_line_exec(&cookie);
5736 }
5737 else
5738 func_line_end(&cookie);
Bram Moolenaarf002a412021-01-24 13:34:18 +01005739#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01005740 }
5741 break;
5742
Bram Moolenaare99d4222021-06-13 14:01:26 +02005743 case ISN_DEBUG:
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02005744 handle_debug(iptr, ectx);
Bram Moolenaare99d4222021-06-13 14:01:26 +02005745 break;
5746
Bram Moolenaar389df252020-07-09 21:20:47 +02005747 case ISN_SHUFFLE:
5748 {
Bram Moolenaar792f7862020-11-23 08:31:18 +01005749 typval_T tmp_tv;
5750 int item = iptr->isn_arg.shuffle.shfl_item;
5751 int up = iptr->isn_arg.shuffle.shfl_up;
Bram Moolenaar389df252020-07-09 21:20:47 +02005752
5753 tmp_tv = *STACK_TV_BOT(-item);
5754 for ( ; up > 0 && item > 1; --up)
5755 {
5756 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
5757 --item;
5758 }
5759 *STACK_TV_BOT(-item) = tmp_tv;
5760 }
5761 break;
5762
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005763 case ISN_DROP:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005764 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005765 clear_tv(STACK_TV_BOT(0));
LemonBoyc5d27442023-08-19 13:02:35 +02005766 ectx->ec_where = (where_T)WHERE_INIT;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005767 break;
5768 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02005769 continue;
5770
Bram Moolenaard032f342020-07-18 18:13:02 +02005771func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02005772 // Restore previous function. If the frame pointer is where we started
5773 // then there is none and we are done.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005774 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02005775 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02005776
Bram Moolenaar4c137212021-04-19 16:48:48 +02005777 if (func_return(ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02005778 // only fails when out of memory
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005779 goto theend;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02005780 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02005781
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02005782on_error:
Bram Moolenaaraf0df472020-12-02 20:51:22 +01005783 // Jump here for an error that does not require aborting execution.
Bram Moolenaar56602ba2020-12-05 21:22:08 +01005784 // If "emsg_silent" is set then ignore the error, unless it was set
5785 // when calling the function.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005786 if (did_emsg_cumul + did_emsg == ectx->ec_did_emsg_before
Bram Moolenaar56602ba2020-12-05 21:22:08 +01005787 && emsg_silent && did_emsg_def == 0)
Bram Moolenaarf9041332021-01-21 19:41:16 +01005788 {
5789 // If a sequence of instructions causes an error while ":silent!"
5790 // was used, restore the stack length and jump ahead to restoring
5791 // the cmdmod.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005792 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaarf9041332021-01-21 19:41:16 +01005793 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005794 while (ectx->ec_stack.ga_len
5795 > ectx->ec_funclocal.floc_restore_cmdmod_stacklen)
Bram Moolenaarf9041332021-01-21 19:41:16 +01005796 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005797 --ectx->ec_stack.ga_len;
Bram Moolenaarf9041332021-01-21 19:41:16 +01005798 clear_tv(STACK_TV_BOT(0));
5799 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005800 while (ectx->ec_instr[ectx->ec_iidx].isn_type != ISN_CMDMOD_REV)
5801 ++ectx->ec_iidx;
Bram Moolenaarf9041332021-01-21 19:41:16 +01005802 }
Bram Moolenaarcd030c42020-10-30 21:49:40 +01005803 continue;
Bram Moolenaarf9041332021-01-21 19:41:16 +01005804 }
Bram Moolenaaraf0df472020-12-02 20:51:22 +01005805on_fatal_error:
5806 // Jump here for an error that messes up the stack.
Bram Moolenaar171fb922020-10-28 16:54:47 +01005807 // If we are not inside a try-catch started here, abort execution.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005808 if (trylevel <= ectx->ec_trylevel_at_start)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005809 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005810 }
5811
5812done:
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005813 ret = OK;
5814theend:
Bram Moolenaar58779852022-09-06 18:31:14 +01005815 may_invoke_defer_funcs(ectx);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01005816
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005817 dict_stack_clear(dict_stack_len_at_start);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005818 ectx->ec_trylevel_at_start = save_trylevel_at_start;
5819 return ret;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005820}
5821
5822/*
Bram Moolenaar806a2732022-09-04 15:40:36 +01005823 * Execute the instructions from a VAR_INSTR typval and put the result in
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005824 * "rettv".
5825 * Return OK or FAIL.
5826 */
5827 int
5828exe_typval_instr(typval_T *tv, typval_T *rettv)
5829{
5830 ectx_T *ectx = tv->vval.v_instr->instr_ectx;
5831 isn_T *save_instr = ectx->ec_instr;
5832 int save_iidx = ectx->ec_iidx;
5833 int res;
5834
LemonBoyf3b48952022-05-05 13:53:03 +01005835 // Initialize rettv so that it is safe for caller to invoke clear_tv(rettv)
5836 // even when the compilation fails.
5837 rettv->v_type = VAR_UNKNOWN;
5838
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005839 ectx->ec_instr = tv->vval.v_instr->instr_instr;
5840 res = exec_instructions(ectx);
5841 if (res == OK)
5842 {
5843 *rettv = *STACK_TV_BOT(-1);
5844 --ectx->ec_stack.ga_len;
5845 }
5846
5847 ectx->ec_instr = save_instr;
5848 ectx->ec_iidx = save_iidx;
5849
5850 return res;
5851}
5852
5853/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02005854 * Execute the instructions from an ISN_SUBSTITUTE command, which are in
5855 * "substitute_instr".
5856 */
5857 char_u *
5858exe_substitute_instr(void)
5859{
5860 ectx_T *ectx = substitute_instr->subs_ectx;
5861 isn_T *save_instr = ectx->ec_instr;
5862 int save_iidx = ectx->ec_iidx;
5863 char_u *res;
5864
5865 ectx->ec_instr = substitute_instr->subs_instr;
5866 if (exec_instructions(ectx) == OK)
Bram Moolenaar90193e62021-04-04 20:49:50 +02005867 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005868 typval_T *tv = STACK_TV_BOT(-1);
5869
Bram Moolenaar27523602021-06-05 21:36:19 +02005870 res = typval2string(tv, TRUE);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005871 --ectx->ec_stack.ga_len;
5872 clear_tv(tv);
Bram Moolenaar90193e62021-04-04 20:49:50 +02005873 }
5874 else
5875 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005876 substitute_instr->subs_status = FAIL;
5877 res = vim_strsave((char_u *)"");
Bram Moolenaar90193e62021-04-04 20:49:50 +02005878 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005879
Bram Moolenaar4c137212021-04-19 16:48:48 +02005880 ectx->ec_instr = save_instr;
5881 ectx->ec_iidx = save_iidx;
5882
5883 return res;
5884}
5885
5886/*
5887 * Call a "def" function from old Vim script.
5888 * Return OK or FAIL.
5889 */
5890 int
5891call_def_function(
5892 ufunc_T *ufunc,
5893 int argc_arg, // nr of arguments
5894 typval_T *argv, // arguments
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005895 int flags, // DEF_ flags
Bram Moolenaar4c137212021-04-19 16:48:48 +02005896 partial_T *partial, // optional partial for context
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005897 object_T *object, // object, e.g. for this.Func()
Bram Moolenaar58779852022-09-06 18:31:14 +01005898 funccall_T *funccal,
Bram Moolenaar4c137212021-04-19 16:48:48 +02005899 typval_T *rettv) // return value
5900{
5901 ectx_T ectx; // execution context
5902 int argc = argc_arg;
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005903 int partial_argc = partial == NULL
5904 || (flags & DEF_USE_PT_ARGV) == 0
5905 ? 0 : partial->pt_argc;
5906 int total_argc = argc + partial_argc;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005907 typval_T *tv;
5908 int idx;
5909 int ret = FAIL;
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005910 int defcount = ufunc->uf_args.ga_len - total_argc;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005911 sctx_T save_current_sctx = current_sctx;
5912 int did_emsg_before = did_emsg_cumul + did_emsg;
5913 int save_suppress_errthrow = suppress_errthrow;
5914 msglist_T **saved_msg_list = NULL;
5915 msglist_T *private_msg_list = NULL;
5916 int save_emsg_silent_def = emsg_silent_def;
5917 int save_did_emsg_def = did_emsg_def;
5918 int orig_funcdepth;
Bram Moolenaare99d4222021-06-13 14:01:26 +02005919 int orig_nesting_level = ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005920
5921// Get pointer to item in the stack.
5922#undef STACK_TV
5923#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
5924
5925// Get pointer to item at the bottom of the stack, -1 is the bottom.
5926#undef STACK_TV_BOT
5927#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
5928
5929// Get pointer to a local variable on the stack. Negative for arguments.
5930#undef STACK_TV_VAR
5931#define STACK_TV_VAR(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_frame_idx + STACK_FRAME_SIZE + idx)
5932
5933 if (ufunc->uf_def_status == UF_NOT_COMPILED
5934 || ufunc->uf_def_status == UF_COMPILE_ERROR
Bram Moolenaar139575d2022-03-15 19:29:30 +00005935 || (func_needs_compiling(ufunc, get_compile_type(ufunc))
5936 && compile_def_function(ufunc, FALSE,
5937 get_compile_type(ufunc), NULL) == FAIL))
Bram Moolenaar4c137212021-04-19 16:48:48 +02005938 {
5939 if (did_emsg_cumul + did_emsg == did_emsg_before)
5940 semsg(_(e_function_is_not_compiled_str),
5941 printable_func_name(ufunc));
5942 return FAIL;
5943 }
5944
5945 {
5946 // Check the function was really compiled.
5947 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5948 + ufunc->uf_dfunc_idx;
Bram Moolenaarcc341812022-09-19 15:54:34 +01005949 if (dfunc->df_ufunc == NULL)
5950 {
5951 semsg(_(e_function_was_deleted_str), printable_func_name(ufunc));
5952 return FAIL;
5953 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005954 if (INSTRUCTIONS(dfunc) == NULL)
5955 {
5956 iemsg("using call_def_function() on not compiled function");
5957 return FAIL;
5958 }
5959 }
5960
5961 // If depth of calling is getting too high, don't execute the function.
5962 orig_funcdepth = funcdepth_get();
5963 if (funcdepth_increment() == FAIL)
5964 return FAIL;
5965
5966 CLEAR_FIELD(ectx);
5967 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
5968 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
Bram Moolenaar35578162021-08-02 19:10:38 +02005969 if (GA_GROW_FAILS(&ectx.ec_stack, 20))
Bram Moolenaar4c137212021-04-19 16:48:48 +02005970 {
5971 funcdepth_decrement();
5972 return FAIL;
5973 }
5974 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
5975 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
5976 ectx.ec_did_emsg_before = did_emsg_before;
Bram Moolenaare99d4222021-06-13 14:01:26 +02005977 ++ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005978
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005979 idx = total_argc - ufunc->uf_args.ga_len;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005980 if (idx > 0 && ufunc->uf_va_name == NULL)
5981 {
Matvey Tarasovd14bb1a2022-06-29 13:18:27 +01005982 semsg(NGETTEXT(e_one_argument_too_many, e_nr_arguments_too_many,
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005983 idx), idx);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005984 goto failed_early;
5985 }
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005986 idx = total_argc - ufunc->uf_args.ga_len + ufunc->uf_def_args.ga_len;
Bram Moolenaarc6d71532021-06-05 18:49:38 +02005987 if (idx < 0)
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02005988 {
Matvey Tarasovd14bb1a2022-06-29 13:18:27 +01005989 semsg(NGETTEXT(e_one_argument_too_few, e_nr_arguments_too_few,
Bram Moolenaar98aff652022-09-06 21:02:35 +01005990 -idx), -idx);
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02005991 goto failed_early;
5992 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005993
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005994 // Put values from the partial and arguments on the stack, but no more than
5995 // what the function expects. A lambda can be called with more arguments
5996 // than it uses.
5997 for (idx = 0; idx < total_argc
Bram Moolenaar4c137212021-04-19 16:48:48 +02005998 && (ufunc->uf_va_name != NULL || idx < ufunc->uf_args.ga_len);
5999 ++idx)
6000 {
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01006001 int argv_idx = idx - partial_argc;
6002
6003 tv = idx < partial_argc ? partial->pt_argv + idx : argv + argv_idx;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006004 if (idx >= ufunc->uf_args.ga_len - ufunc->uf_def_args.ga_len
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01006005 && tv->v_type == VAR_SPECIAL
6006 && tv->vval.v_number == VVAL_NONE)
Bram Moolenaar4c137212021-04-19 16:48:48 +02006007 {
6008 // Use the default value.
6009 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
6010 }
6011 else
6012 {
Bram Moolenaar47bba532023-01-20 18:49:46 +00006013 int done = FALSE;
6014 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len)
6015 {
6016 type_T *expected = ufunc->uf_arg_types[idx];
6017 if (expected->tt_type == VAR_FLOAT && tv->v_type == VAR_NUMBER)
6018 {
6019 // When a float is expected and a number was given, convert
6020 // the value.
6021 STACK_TV_BOT(0)->v_type = VAR_FLOAT;
6022 STACK_TV_BOT(0)->v_lock = 0;
6023 STACK_TV_BOT(0)->vval.v_float = tv->vval.v_number;
6024 done = TRUE;
6025 }
6026 else if (check_typval_arg_type(expected, tv,
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01006027 NULL, argv_idx + 1) == FAIL)
Bram Moolenaar47bba532023-01-20 18:49:46 +00006028 goto failed_early;
6029 }
6030 if (!done)
6031 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006032 }
6033 ++ectx.ec_stack.ga_len;
6034 }
6035
6036 // Turn varargs into a list. Empty list if no args.
6037 if (ufunc->uf_va_name != NULL)
6038 {
6039 int vararg_count = argc - ufunc->uf_args.ga_len;
6040
6041 if (vararg_count < 0)
6042 vararg_count = 0;
6043 else
6044 argc -= vararg_count;
6045 if (exe_newlist(vararg_count, &ectx) == FAIL)
6046 goto failed_early;
6047
6048 // Check the type of the list items.
6049 tv = STACK_TV_BOT(-1);
6050 if (ufunc->uf_va_type != NULL
6051 && ufunc->uf_va_type != &t_list_any
6052 && ufunc->uf_va_type->tt_member != &t_any
6053 && tv->vval.v_list != NULL)
6054 {
6055 type_T *expected = ufunc->uf_va_type->tt_member;
6056 listitem_T *li = tv->vval.v_list->lv_first;
6057
6058 for (idx = 0; idx < vararg_count; ++idx)
6059 {
6060 if (check_typval_arg_type(expected, &li->li_tv,
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02006061 NULL, argc + idx + 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02006062 goto failed_early;
6063 li = li->li_next;
6064 }
6065 }
6066
6067 if (defcount > 0)
6068 // Move varargs list to below missing default arguments.
6069 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
6070 --ectx.ec_stack.ga_len;
6071 }
6072
6073 // Make space for omitted arguments, will store default value below.
6074 // Any varargs list goes after them.
6075 if (defcount > 0)
6076 for (idx = 0; idx < defcount; ++idx)
6077 {
6078 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
6079 ++ectx.ec_stack.ga_len;
6080 }
6081 if (ufunc->uf_va_name != NULL)
6082 ++ectx.ec_stack.ga_len;
6083
6084 // Frame pointer points to just after arguments.
6085 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
6086 ectx.ec_initial_frame_idx = ectx.ec_frame_idx;
6087
6088 {
6089 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6090 + ufunc->uf_dfunc_idx;
6091 ufunc_T *base_ufunc = dfunc->df_ufunc;
6092
6093 // "uf_partial" is on the ufunc that "df_ufunc" points to, as is done
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006094 // by copy_lambda_to_global_func().
Bram Moolenaar4c137212021-04-19 16:48:48 +02006095 if (partial != NULL || base_ufunc->uf_partial != NULL)
6096 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006097 ectx.ec_outer_ref = ALLOC_CLEAR_ONE(outer_ref_T);
6098 if (ectx.ec_outer_ref == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02006099 goto failed_early;
6100 if (partial != NULL)
6101 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00006102 outer_T *outer = get_pt_outer(partial);
6103
Bram Moolenaar6d313be2022-09-22 16:36:25 +01006104 if (outer->out_stack == NULL && outer->out_loop_size == 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02006105 {
Bram Moolenaar6d313be2022-09-22 16:36:25 +01006106 // no stack was set
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00006107 if (current_ectx != NULL)
6108 {
6109 if (current_ectx->ec_outer_ref != NULL
6110 && current_ectx->ec_outer_ref->or_outer != NULL)
6111 ectx.ec_outer_ref->or_outer =
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006112 current_ectx->ec_outer_ref->or_outer;
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00006113 }
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01006114 // else: should there be an error here?
Bram Moolenaar4c137212021-04-19 16:48:48 +02006115 }
6116 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006117 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00006118 ectx.ec_outer_ref->or_outer = outer;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006119 ++partial->pt_refcount;
6120 ectx.ec_outer_ref->or_partial = partial;
6121 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006122 }
6123 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006124 {
6125 ectx.ec_outer_ref->or_outer = &base_ufunc->uf_partial->pt_outer;
6126 ++base_ufunc->uf_partial->pt_refcount;
6127 ectx.ec_outer_ref->or_partial = base_ufunc->uf_partial;
6128 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006129 }
6130 }
6131
6132 // dummy frame entries
6133 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
6134 {
6135 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
6136 ++ectx.ec_stack.ga_len;
6137 }
6138
6139 {
6140 // Reserve space for local variables and any closure reference count.
6141 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6142 + ufunc->uf_dfunc_idx;
6143
Bram Moolenaar5cd64792021-12-25 18:23:24 +00006144 // Initialize variables to zero. That avoids having to generate
6145 // initializing instructions for "var nr: number", "var x: any", etc.
Bram Moolenaar4c137212021-04-19 16:48:48 +02006146 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaar5cd64792021-12-25 18:23:24 +00006147 {
6148 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
6149 STACK_TV_VAR(idx)->vval.v_number = 0;
6150 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006151 ectx.ec_stack.ga_len += dfunc->df_varcount;
Bram Moolenaarffdaca92022-12-09 21:41:48 +00006152
6153 if (object != NULL)
6154 {
6155 // the object is always the variable at index zero
6156 tv = STACK_TV_VAR(0);
6157 tv->v_type = VAR_OBJECT;
6158 tv->vval.v_object = object;
6159 }
6160
Bram Moolenaar4c137212021-04-19 16:48:48 +02006161 if (dfunc->df_has_closure)
6162 {
Bram Moolenaarcc341812022-09-19 15:54:34 +01006163 // Initialize the variable that counts how many closures were
6164 // created. This is used in handle_closure_in_use().
Bram Moolenaar4c137212021-04-19 16:48:48 +02006165 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
6166 STACK_TV_VAR(idx)->vval.v_number = 0;
6167 ++ectx.ec_stack.ga_len;
6168 }
6169
6170 ectx.ec_instr = INSTRUCTIONS(dfunc);
6171 }
6172
Bram Moolenaar58779852022-09-06 18:31:14 +01006173 // Store the execution context in funccal, used by invoke_all_defer().
6174 if (funccal != NULL)
6175 funccal->fc_ectx = &ectx;
6176
Bram Moolenaar4c137212021-04-19 16:48:48 +02006177 // Following errors are in the function, not the caller.
6178 // Commands behave like vim9script.
6179 estack_push_ufunc(ufunc, 1);
6180 current_sctx = ufunc->uf_script_ctx;
6181 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
6182
6183 // Use a specific location for storing error messages to be converted to an
6184 // exception.
6185 saved_msg_list = msg_list;
6186 msg_list = &private_msg_list;
6187
6188 // Do turn errors into exceptions.
6189 suppress_errthrow = FALSE;
6190
6191 // Do not delete the function while executing it.
6192 ++ufunc->uf_calls;
6193
6194 // When ":silent!" was used before calling then we still abort the
6195 // function. If ":silent!" is used in the function then we don't.
6196 emsg_silent_def = emsg_silent;
6197 did_emsg_def = 0;
6198
LemonBoyc5d27442023-08-19 13:02:35 +02006199 ectx.ec_where = (where_T)WHERE_INIT;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006200
Bram Moolenaar5800c792022-09-22 17:34:01 +01006201 /*
6202 * Execute the instructions until done.
6203 */
Bram Moolenaar4c137212021-04-19 16:48:48 +02006204 ret = exec_instructions(&ectx);
6205 if (ret == OK)
6206 {
6207 // function finished, get result from the stack.
6208 if (ufunc->uf_ret_type == &t_void)
6209 {
6210 rettv->v_type = VAR_VOID;
6211 }
6212 else
6213 {
6214 tv = STACK_TV_BOT(-1);
6215 *rettv = *tv;
6216 tv->v_type = VAR_UNKNOWN;
6217 }
6218 }
6219
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01006220 // When failed need to unwind the call stack.
Bram Moolenaar58779852022-09-06 18:31:14 +01006221 unwind_def_callstack(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02006222
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02006223 // Deal with any remaining closures, they may be in use somewhere.
6224 if (ectx.ec_funcrefs.ga_len > 0)
Bram Moolenaarf112f302020-12-20 17:47:52 +01006225 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02006226 handle_closure_in_use(&ectx, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00006227 ga_clear(&ectx.ec_funcrefs);
Bram Moolenaarf112f302020-12-20 17:47:52 +01006228 }
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02006229
Bram Moolenaaree8580e2020-08-28 17:19:07 +02006230 estack_pop();
6231 current_sctx = save_current_sctx;
6232
Bram Moolenaar2336c372021-12-06 15:06:54 +00006233 if (--ufunc->uf_calls <= 0 && ufunc->uf_refcount <= 0)
6234 // Function was unreferenced while being used, free it now.
6235 func_clear_free(ufunc, FALSE);
Bram Moolenaarc970e422021-03-17 15:03:04 +01006236
Bram Moolenaar352134b2020-10-17 22:04:08 +02006237 if (*msg_list != NULL && saved_msg_list != NULL)
6238 {
6239 msglist_T **plist = saved_msg_list;
6240
6241 // Append entries from the current msg_list (uncaught exceptions) to
6242 // the saved msg_list.
6243 while (*plist != NULL)
6244 plist = &(*plist)->next;
6245
6246 *plist = *msg_list;
6247 }
6248 msg_list = saved_msg_list;
6249
Bram Moolenaar4c137212021-04-19 16:48:48 +02006250 if (ectx.ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar02194d22020-10-24 23:08:38 +02006251 {
6252 cmdmod.cmod_filter_regmatch.regprog = NULL;
6253 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006254 cmdmod = ectx.ec_funclocal.floc_save_cmdmod;
Bram Moolenaar02194d22020-10-24 23:08:38 +02006255 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +01006256 emsg_silent_def = save_emsg_silent_def;
6257 did_emsg_def += save_did_emsg_def;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02006258
Bram Moolenaaree8580e2020-08-28 17:19:07 +02006259failed_early:
Bram Moolenaar0d1f55c2022-04-05 17:30:29 +01006260 // Free all arguments and local variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006261 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
Bram Moolenaar1936c762022-09-28 15:19:10 +01006262 {
6263 tv = STACK_TV(idx);
6264 if (tv->v_type != VAR_NUMBER && tv->v_type != VAR_UNKNOWN)
6265 clear_tv(tv);
6266 }
Bram Moolenaare99d4222021-06-13 14:01:26 +02006267 ex_nesting_level = orig_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02006268
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006269 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01006270 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006271 if (ectx.ec_outer_ref != NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +01006272 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006273 if (ectx.ec_outer_ref->or_outer_allocated)
6274 vim_free(ectx.ec_outer_ref->or_outer);
6275 partial_unref(ectx.ec_outer_ref->or_partial);
6276 vim_free(ectx.ec_outer_ref);
Bram Moolenaar0186e582021-01-10 18:33:11 +01006277 }
6278
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02006279 // Not sure if this is necessary.
6280 suppress_errthrow = save_suppress_errthrow;
6281
Bram Moolenaarb5841b92021-07-15 18:09:53 +02006282 if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before
6283 && !need_rethrow)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006284 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02006285 printable_func_name(ufunc));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01006286 funcdepth_restore(orig_funcdepth);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006287 return ret;
6288}
6289
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006290/*
Bram Moolenaar58779852022-09-06 18:31:14 +01006291 * Called when a def function has finished (possibly failed).
6292 * Invoke all the function returns to clean up and invoke deferred functions,
6293 * except the toplevel one.
6294 */
6295 void
6296unwind_def_callstack(ectx_T *ectx)
6297{
6298 while (ectx->ec_frame_idx != ectx->ec_initial_frame_idx)
6299 func_return(ectx);
6300}
6301
6302/*
dundargocc57b5bc2022-11-02 13:30:51 +00006303 * Invoke any deferred functions for the top function in "ectx".
Bram Moolenaar58779852022-09-06 18:31:14 +01006304 */
6305 void
6306may_invoke_defer_funcs(ectx_T *ectx)
6307{
6308 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
6309
6310 if (dfunc->df_defer_var_idx > 0)
6311 invoke_defer_funcs(ectx);
6312}
6313
6314/*
Bram Moolenaarcc341812022-09-19 15:54:34 +01006315 * Return loopvarinfo in a printable form in allocated memory.
6316 */
6317 static char_u *
6318printable_loopvarinfo(loopvarinfo_T *lvi)
6319{
6320 garray_T ga;
6321 int depth;
6322
6323 ga_init2(&ga, 1, 100);
6324 for (depth = 0; depth < lvi->lvi_depth; ++depth)
6325 {
6326 if (ga_grow(&ga, 50) == FAIL)
6327 break;
6328 if (lvi->lvi_loop[depth].var_idx == 0)
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +01006329 STRCPY((char *)ga.ga_data + ga.ga_len, " -");
Bram Moolenaarcc341812022-09-19 15:54:34 +01006330 else
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +01006331 vim_snprintf((char *)ga.ga_data + ga.ga_len, 50, " $%d-$%d",
Bram Moolenaarcc341812022-09-19 15:54:34 +01006332 lvi->lvi_loop[depth].var_idx,
6333 lvi->lvi_loop[depth].var_idx
6334 + lvi->lvi_loop[depth].var_count - 1);
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +01006335 ga.ga_len = (int)STRLEN(ga.ga_data);
Bram Moolenaarcc341812022-09-19 15:54:34 +01006336 }
6337 return ga.ga_data;
6338}
6339
6340/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02006341 * List instructions "instr" up to "instr_count" or until ISN_FINISH.
6342 * "ufunc" has the source lines, NULL for the instructions of ISN_SUBSTITUTE.
6343 * "pfx" is prefixed to every line.
6344 */
6345 static void
6346list_instructions(char *pfx, isn_T *instr, int instr_count, ufunc_T *ufunc)
6347{
6348 int line_idx = 0;
6349 int prev_current = 0;
6350 int current;
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02006351 int def_arg_idx = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006352
6353 for (current = 0; current < instr_count; ++current)
6354 {
6355 isn_T *iptr = &instr[current];
6356 char *line;
6357
6358 if (ufunc != NULL)
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02006359 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02006360 while (line_idx < iptr->isn_lnum
6361 && line_idx < ufunc->uf_lines.ga_len)
6362 {
6363 if (current > prev_current)
6364 {
6365 msg_puts("\n\n");
6366 prev_current = current;
6367 }
6368 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
6369 if (line != NULL)
6370 msg(line);
6371 }
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02006372 if (iptr->isn_type == ISN_JUMP_IF_ARG_SET)
6373 {
6374 int first_def_arg = ufunc->uf_args.ga_len
6375 - ufunc->uf_def_args.ga_len;
6376
6377 if (def_arg_idx > 0)
6378 msg_puts("\n\n");
6379 msg_start();
6380 msg_puts(" ");
6381 msg_puts(((char **)(ufunc->uf_args.ga_data))[
6382 first_def_arg + def_arg_idx]);
6383 msg_puts(" = ");
6384 msg_puts(((char **)(ufunc->uf_def_args.ga_data))[def_arg_idx++]);
6385 msg_clr_eos();
6386 msg_end();
6387 }
6388 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006389
6390 switch (iptr->isn_type)
6391 {
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006392 case ISN_CONSTRUCT:
6393 smsg("%s%4d NEW %s size %d", pfx, current,
6394 iptr->isn_arg.construct.construct_class->class_name,
6395 (int)iptr->isn_arg.construct.construct_size);
6396 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006397 case ISN_EXEC:
6398 smsg("%s%4d EXEC %s", pfx, current, iptr->isn_arg.string);
6399 break;
Bram Moolenaar20677332021-06-06 17:02:53 +02006400 case ISN_EXEC_SPLIT:
6401 smsg("%s%4d EXEC_SPLIT %s", pfx, current, iptr->isn_arg.string);
6402 break;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00006403 case ISN_EXECRANGE:
6404 smsg("%s%4d EXECRANGE %s", pfx, current, iptr->isn_arg.string);
6405 break;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02006406 case ISN_LEGACY_EVAL:
6407 smsg("%s%4d EVAL legacy %s", pfx, current,
6408 iptr->isn_arg.string);
6409 break;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02006410 case ISN_REDIRSTART:
6411 smsg("%s%4d REDIR", pfx, current);
6412 break;
6413 case ISN_REDIREND:
6414 smsg("%s%4d REDIR END%s", pfx, current,
6415 iptr->isn_arg.number ? " append" : "");
6416 break;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006417 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02006418#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006419 smsg("%s%4d CEXPR pre %s", pfx, current,
6420 cexpr_get_auname(iptr->isn_arg.number));
Bram Moolenaarb7c97812021-05-05 22:51:39 +02006421#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006422 break;
6423 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02006424#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006425 {
6426 cexprref_T *cer = iptr->isn_arg.cexpr.cexpr_ref;
6427
6428 smsg("%s%4d CEXPR core %s%s \"%s\"", pfx, current,
6429 cexpr_get_auname(cer->cer_cmdidx),
6430 cer->cer_forceit ? "!" : "",
6431 cer->cer_cmdline);
6432 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02006433#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006434 break;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006435 case ISN_INSTR:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006436 smsg("%s%4d INSTR", pfx, current);
6437 list_instructions(" ", iptr->isn_arg.instr, INT_MAX, NULL);
6438 msg(" -------------");
6439 break;
6440 case ISN_SOURCE:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006441 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006442 scriptitem_T *si = SCRIPT_ITEM(iptr->isn_arg.number);
6443
6444 smsg("%s%4d SOURCE %s", pfx, current, si->sn_name);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006445 }
6446 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006447 case ISN_SUBSTITUTE:
6448 {
6449 subs_T *subs = &iptr->isn_arg.subs;
6450
6451 smsg("%s%4d SUBSTITUTE %s", pfx, current, subs->subs_cmd);
6452 list_instructions(" ", subs->subs_instr, INT_MAX, NULL);
6453 msg(" -------------");
6454 }
6455 break;
6456 case ISN_EXECCONCAT:
6457 smsg("%s%4d EXECCONCAT %lld", pfx, current,
6458 (varnumber_T)iptr->isn_arg.number);
6459 break;
6460 case ISN_ECHO:
6461 {
6462 echo_T *echo = &iptr->isn_arg.echo;
6463
6464 smsg("%s%4d %s %d", pfx, current,
6465 echo->echo_with_white ? "ECHO" : "ECHON",
6466 echo->echo_count);
6467 }
6468 break;
6469 case ISN_EXECUTE:
6470 smsg("%s%4d EXECUTE %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02006471 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006472 break;
6473 case ISN_ECHOMSG:
6474 smsg("%s%4d ECHOMSG %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02006475 (varnumber_T)(iptr->isn_arg.number));
6476 break;
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01006477 case ISN_ECHOWINDOW:
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01006478 if (iptr->isn_arg.echowin.ewin_time > 0)
6479 smsg("%s%4d ECHOWINDOW %d (%ld sec)", pfx, current,
6480 iptr->isn_arg.echowin.ewin_count,
6481 iptr->isn_arg.echowin.ewin_time);
6482 else
6483 smsg("%s%4d ECHOWINDOW %d", pfx, current,
6484 iptr->isn_arg.echowin.ewin_count);
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01006485 break;
Bram Moolenaar7de62622021-08-07 15:05:47 +02006486 case ISN_ECHOCONSOLE:
6487 smsg("%s%4d ECHOCONSOLE %lld", pfx, current,
6488 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006489 break;
6490 case ISN_ECHOERR:
6491 smsg("%s%4d ECHOERR %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02006492 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006493 break;
6494 case ISN_LOAD:
6495 {
6496 if (iptr->isn_arg.number < 0)
6497 smsg("%s%4d LOAD arg[%lld]", pfx, current,
6498 (varnumber_T)(iptr->isn_arg.number
6499 + STACK_FRAME_SIZE));
6500 else
6501 smsg("%s%4d LOAD $%lld", pfx, current,
6502 (varnumber_T)(iptr->isn_arg.number));
6503 }
6504 break;
6505 case ISN_LOADOUTER:
6506 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006507 isn_outer_T *outer = &iptr->isn_arg.outer;
6508
6509 if (outer->outer_idx < 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02006510 smsg("%s%4d LOADOUTER level %d arg[%d]", pfx, current,
Bram Moolenaarcc341812022-09-19 15:54:34 +01006511 outer->outer_depth,
6512 outer->outer_idx + STACK_FRAME_SIZE);
6513 else if (outer->outer_depth < 0)
6514 smsg("%s%4d LOADOUTER $%d in loop level %d",
6515 pfx, current,
6516 outer->outer_idx,
6517 -outer->outer_depth);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006518 else
6519 smsg("%s%4d LOADOUTER level %d $%d", pfx, current,
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006520 outer->outer_depth,
6521 outer->outer_idx);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006522 }
6523 break;
6524 case ISN_LOADV:
6525 smsg("%s%4d LOADV v:%s", pfx, current,
6526 get_vim_var_name(iptr->isn_arg.number));
6527 break;
6528 case ISN_LOADSCRIPT:
6529 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006530 scriptref_T *sref = iptr->isn_arg.script.scriptref;
6531 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
6532 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006533
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006534 sv = get_script_svar(sref, -1);
6535 if (sv == NULL)
6536 smsg("%s%4d LOADSCRIPT [deleted] from %s",
6537 pfx, current, si->sn_name);
6538 else
6539 smsg("%s%4d LOADSCRIPT %s-%d from %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02006540 sv->sv_name,
6541 sref->sref_idx,
6542 si->sn_name);
6543 }
6544 break;
6545 case ISN_LOADS:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006546 case ISN_LOADEXPORT:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006547 {
6548 scriptitem_T *si = SCRIPT_ITEM(
6549 iptr->isn_arg.loadstore.ls_sid);
6550
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006551 smsg("%s%4d %s s:%s from %s", pfx, current,
6552 iptr->isn_type == ISN_LOADS ? "LOADS"
6553 : "LOADEXPORT",
6554 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006555 }
6556 break;
6557 case ISN_LOADAUTO:
6558 smsg("%s%4d LOADAUTO %s", pfx, current, iptr->isn_arg.string);
6559 break;
6560 case ISN_LOADG:
6561 smsg("%s%4d LOADG g:%s", pfx, current, iptr->isn_arg.string);
6562 break;
6563 case ISN_LOADB:
6564 smsg("%s%4d LOADB b:%s", pfx, current, iptr->isn_arg.string);
6565 break;
6566 case ISN_LOADW:
6567 smsg("%s%4d LOADW w:%s", pfx, current, iptr->isn_arg.string);
6568 break;
6569 case ISN_LOADT:
6570 smsg("%s%4d LOADT t:%s", pfx, current, iptr->isn_arg.string);
6571 break;
6572 case ISN_LOADGDICT:
6573 smsg("%s%4d LOAD g:", pfx, current);
6574 break;
6575 case ISN_LOADBDICT:
6576 smsg("%s%4d LOAD b:", pfx, current);
6577 break;
6578 case ISN_LOADWDICT:
6579 smsg("%s%4d LOAD w:", pfx, current);
6580 break;
6581 case ISN_LOADTDICT:
6582 smsg("%s%4d LOAD t:", pfx, current);
6583 break;
6584 case ISN_LOADOPT:
6585 smsg("%s%4d LOADOPT %s", pfx, current, iptr->isn_arg.string);
6586 break;
6587 case ISN_LOADENV:
6588 smsg("%s%4d LOADENV %s", pfx, current, iptr->isn_arg.string);
6589 break;
6590 case ISN_LOADREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006591 smsg("%s%4d LOADREG @%c", pfx, current,
6592 (int)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006593 break;
6594
6595 case ISN_STORE:
6596 if (iptr->isn_arg.number < 0)
6597 smsg("%s%4d STORE arg[%lld]", pfx, current,
6598 iptr->isn_arg.number + STACK_FRAME_SIZE);
6599 else
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006600 smsg("%s%4d STORE $%lld", pfx, current,
6601 iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006602 break;
6603 case ISN_STOREOUTER:
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006604 {
6605 isn_outer_T *outer = &iptr->isn_arg.outer;
6606
6607 if (outer->outer_depth == OUTER_LOOP_DEPTH)
6608 smsg("%s%4d STOREOUTER level 1 $%d in loop",
6609 pfx, current, outer->outer_idx);
6610 else
6611 smsg("%s%4d STOREOUTER level %d $%d", pfx, current,
6612 outer->outer_depth, outer->outer_idx);
6613 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006614 break;
6615 case ISN_STOREV:
6616 smsg("%s%4d STOREV v:%s", pfx, current,
6617 get_vim_var_name(iptr->isn_arg.number));
6618 break;
6619 case ISN_STOREAUTO:
6620 smsg("%s%4d STOREAUTO %s", pfx, current, iptr->isn_arg.string);
6621 break;
6622 case ISN_STOREG:
6623 smsg("%s%4d STOREG %s", pfx, current, iptr->isn_arg.string);
6624 break;
6625 case ISN_STOREB:
6626 smsg("%s%4d STOREB %s", pfx, current, iptr->isn_arg.string);
6627 break;
6628 case ISN_STOREW:
6629 smsg("%s%4d STOREW %s", pfx, current, iptr->isn_arg.string);
6630 break;
6631 case ISN_STORET:
6632 smsg("%s%4d STORET %s", pfx, current, iptr->isn_arg.string);
6633 break;
6634 case ISN_STORES:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006635 case ISN_STOREEXPORT:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006636 {
6637 scriptitem_T *si = SCRIPT_ITEM(
6638 iptr->isn_arg.loadstore.ls_sid);
6639
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006640 smsg("%s%4d %s %s in %s", pfx, current,
6641 iptr->isn_type == ISN_STORES
6642 ? "STORES" : "STOREEXPORT",
Bram Moolenaar4c137212021-04-19 16:48:48 +02006643 iptr->isn_arg.loadstore.ls_name, si->sn_name);
6644 }
6645 break;
6646 case ISN_STORESCRIPT:
6647 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006648 scriptref_T *sref = iptr->isn_arg.script.scriptref;
6649 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
6650 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006651
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006652 sv = get_script_svar(sref, -1);
6653 if (sv == NULL)
6654 smsg("%s%4d STORESCRIPT [deleted] in %s",
6655 pfx, current, si->sn_name);
6656 else
6657 smsg("%s%4d STORESCRIPT %s-%d in %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02006658 sv->sv_name,
6659 sref->sref_idx,
6660 si->sn_name);
6661 }
6662 break;
6663 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00006664 case ISN_STOREFUNCOPT:
6665 smsg("%s%4d %s &%s", pfx, current,
6666 iptr->isn_type == ISN_STOREOPT ? "STOREOPT" : "STOREFUNCOPT",
Bram Moolenaar4c137212021-04-19 16:48:48 +02006667 iptr->isn_arg.storeopt.so_name);
6668 break;
6669 case ISN_STOREENV:
6670 smsg("%s%4d STOREENV $%s", pfx, current, iptr->isn_arg.string);
6671 break;
6672 case ISN_STOREREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006673 smsg("%s%4d STOREREG @%c", pfx, current,
6674 (int)iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006675 break;
6676 case ISN_STORENR:
6677 smsg("%s%4d STORE %lld in $%d", pfx, current,
6678 iptr->isn_arg.storenr.stnr_val,
6679 iptr->isn_arg.storenr.stnr_idx);
6680 break;
6681
6682 case ISN_STOREINDEX:
6683 smsg("%s%4d STOREINDEX %s", pfx, current,
Bram Moolenaarf7d1c6e2023-01-16 20:47:57 +00006684 vartype_name(iptr->isn_arg.storeindex.si_vartype));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006685 break;
6686
6687 case ISN_STORERANGE:
6688 smsg("%s%4d STORERANGE", pfx, current);
6689 break;
6690
Bram Moolenaard505d172022-12-18 21:42:55 +00006691 case ISN_LOAD_CLASSMEMBER:
6692 case ISN_STORE_CLASSMEMBER:
6693 {
6694 class_T *cl = iptr->isn_arg.classmember.cm_class;
6695 int idx = iptr->isn_arg.classmember.cm_idx;
6696 ocmember_T *ocm = &cl->class_class_members[idx];
6697 smsg("%s%4d %s CLASSMEMBER %s.%s", pfx, current,
6698 iptr->isn_type == ISN_LOAD_CLASSMEMBER
6699 ? "LOAD" : "STORE",
6700 cl->class_name, ocm->ocm_name);
6701 }
6702 break;
6703
Bram Moolenaar4c137212021-04-19 16:48:48 +02006704 // constants
6705 case ISN_PUSHNR:
6706 smsg("%s%4d PUSHNR %lld", pfx, current,
6707 (varnumber_T)(iptr->isn_arg.number));
6708 break;
6709 case ISN_PUSHBOOL:
6710 case ISN_PUSHSPEC:
6711 smsg("%s%4d PUSH %s", pfx, current,
6712 get_var_special_name(iptr->isn_arg.number));
6713 break;
6714 case ISN_PUSHF:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006715 smsg("%s%4d PUSHF %g", pfx, current, iptr->isn_arg.fnumber);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006716 break;
6717 case ISN_PUSHS:
6718 smsg("%s%4d PUSHS \"%s\"", pfx, current, iptr->isn_arg.string);
6719 break;
6720 case ISN_PUSHBLOB:
6721 {
6722 char_u *r;
6723 char_u numbuf[NUMBUFLEN];
6724 char_u *tofree;
6725
6726 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
6727 smsg("%s%4d PUSHBLOB %s", pfx, current, r);
6728 vim_free(tofree);
6729 }
6730 break;
6731 case ISN_PUSHFUNC:
6732 {
6733 char *name = (char *)iptr->isn_arg.string;
6734
6735 smsg("%s%4d PUSHFUNC \"%s\"", pfx, current,
6736 name == NULL ? "[none]" : name);
6737 }
6738 break;
6739 case ISN_PUSHCHANNEL:
6740#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar397a87a2022-03-20 21:14:15 +00006741 smsg("%s%4d PUSHCHANNEL 0", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006742#endif
6743 break;
6744 case ISN_PUSHJOB:
6745#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar397a87a2022-03-20 21:14:15 +00006746 smsg("%s%4d PUSHJOB \"no process\"", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006747#endif
6748 break;
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00006749 case ISN_PUSHOBJ:
6750 smsg("%s%4d PUSHOBJ null", pfx, current);
6751 break;
6752 case ISN_PUSHCLASS:
6753 smsg("%s%4d PUSHCLASS %s", pfx, current,
Bram Moolenaar30a84472023-02-27 08:07:14 +00006754 iptr->isn_arg.classarg == NULL ? "null"
6755 : (char *)iptr->isn_arg.classarg->class_name);
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00006756 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006757 case ISN_PUSHEXC:
6758 smsg("%s%4d PUSH v:exception", pfx, current);
6759 break;
Bram Moolenaar06b77222022-01-25 15:51:56 +00006760 case ISN_AUTOLOAD:
6761 smsg("%s%4d AUTOLOAD %s", pfx, current, iptr->isn_arg.string);
6762 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006763 case ISN_UNLET:
6764 smsg("%s%4d UNLET%s %s", pfx, current,
6765 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
6766 iptr->isn_arg.unlet.ul_name);
6767 break;
6768 case ISN_UNLETENV:
6769 smsg("%s%4d UNLETENV%s $%s", pfx, current,
6770 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
6771 iptr->isn_arg.unlet.ul_name);
6772 break;
6773 case ISN_UNLETINDEX:
6774 smsg("%s%4d UNLETINDEX", pfx, current);
6775 break;
6776 case ISN_UNLETRANGE:
6777 smsg("%s%4d UNLETRANGE", pfx, current);
6778 break;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02006779 case ISN_LOCKUNLOCK:
6780 smsg("%s%4d LOCKUNLOCK %s", pfx, current, iptr->isn_arg.string);
6781 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006782 case ISN_LOCKCONST:
6783 smsg("%s%4d LOCKCONST", pfx, current);
6784 break;
6785 case ISN_NEWLIST:
6786 smsg("%s%4d NEWLIST size %lld", pfx, current,
Bram Moolenaar1936c762022-09-28 15:19:10 +01006787 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006788 break;
6789 case ISN_NEWDICT:
6790 smsg("%s%4d NEWDICT size %lld", pfx, current,
Bram Moolenaar1936c762022-09-28 15:19:10 +01006791 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006792 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00006793 case ISN_NEWPARTIAL:
6794 smsg("%s%4d NEWPARTIAL", pfx, current);
6795 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006796
6797 // function call
6798 case ISN_BCALL:
6799 {
6800 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
6801
6802 smsg("%s%4d BCALL %s(argc %d)", pfx, current,
6803 internal_func_name(cbfunc->cbf_idx),
6804 cbfunc->cbf_argcount);
6805 }
6806 break;
6807 case ISN_DCALL:
6808 {
6809 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
6810 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
6811 + cdfunc->cdf_idx;
6812
6813 smsg("%s%4d DCALL %s(argc %d)", pfx, current,
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006814 printable_func_name(df->df_ufunc),
6815 cdfunc->cdf_argcount);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006816 }
6817 break;
Bram Moolenaard0200c82023-01-28 15:19:40 +00006818 case ISN_METHODCALL:
6819 {
6820 cmfunc_T *mfunc = iptr->isn_arg.mfunc;
6821
6822 smsg("%s%4d METHODCALL %s.%s(argc %d)", pfx, current,
6823 mfunc->cmf_itf->class_name,
6824 mfunc->cmf_itf->class_obj_methods[
6825 mfunc->cmf_idx]->uf_name,
6826 mfunc->cmf_argcount);
6827 }
6828 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006829 case ISN_UCALL:
6830 {
6831 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
6832
6833 smsg("%s%4d UCALL %s(argc %d)", pfx, current,
6834 cufunc->cuf_name, cufunc->cuf_argcount);
6835 }
6836 break;
6837 case ISN_PCALL:
6838 {
6839 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
6840
6841 smsg("%s%4d PCALL%s (argc %d)", pfx, current,
6842 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
6843 }
6844 break;
6845 case ISN_PCALL_END:
6846 smsg("%s%4d PCALL end", pfx, current);
6847 break;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006848 case ISN_DEFER:
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00006849 case ISN_DEFEROBJ:
6850 smsg("%s%4d %s %d args", pfx, current,
6851 iptr->isn_type == ISN_DEFER ? "DEFER" : "DEFEROBJ",
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006852 (int)iptr->isn_arg.defer.defer_argcount);
6853 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006854 case ISN_RETURN:
6855 smsg("%s%4d RETURN", pfx, current);
6856 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02006857 case ISN_RETURN_VOID:
6858 smsg("%s%4d RETURN void", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006859 break;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006860 case ISN_RETURN_OBJECT:
6861 smsg("%s%4d RETURN object", pfx, current);
6862 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006863 case ISN_FUNCREF:
6864 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006865 funcref_T *funcref = &iptr->isn_arg.funcref;
6866 funcref_extra_T *extra = funcref->fr_extra;
6867 char_u *name;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006868
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006869 if (extra == NULL || extra->fre_func_name == NULL)
Bram Moolenaar38453522021-11-28 22:00:12 +00006870 {
6871 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
6872 + funcref->fr_dfunc_idx;
6873 name = df->df_ufunc->uf_name;
6874 }
6875 else
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006876 name = extra->fre_func_name;
Bram Moolenaar313e4722023-02-08 20:55:27 +00006877 if (extra != NULL && extra->fre_class != NULL)
6878 {
6879 smsg("%s%4d FUNCREF %s.%s", pfx, current,
6880 extra->fre_class->class_name, name);
6881 }
6882 else if (extra == NULL
6883 || extra->fre_loopvar_info.lvi_depth == 0)
6884 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006885 smsg("%s%4d FUNCREF %s", pfx, current, name);
Bram Moolenaar313e4722023-02-08 20:55:27 +00006886 }
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006887 else
Bram Moolenaarcc341812022-09-19 15:54:34 +01006888 {
6889 char_u *info = printable_loopvarinfo(
6890 &extra->fre_loopvar_info);
6891
6892 smsg("%s%4d FUNCREF %s vars %s", pfx, current,
6893 name, info);
6894 vim_free(info);
6895 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006896 }
6897 break;
6898
6899 case ISN_NEWFUNC:
6900 {
Bram Moolenaarcc341812022-09-19 15:54:34 +01006901 newfuncarg_T *arg = iptr->isn_arg.newfunc.nf_arg;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006902
Bram Moolenaarcc341812022-09-19 15:54:34 +01006903 if (arg->nfa_loopvar_info.lvi_depth == 0)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006904 smsg("%s%4d NEWFUNC %s %s", pfx, current,
6905 arg->nfa_lambda, arg->nfa_global);
6906 else
Bram Moolenaarcc341812022-09-19 15:54:34 +01006907 {
6908 char_u *info = printable_loopvarinfo(
6909 &arg->nfa_loopvar_info);
6910
6911 smsg("%s%4d NEWFUNC %s %s vars %s", pfx, current,
6912 arg->nfa_lambda, arg->nfa_global, info);
6913 vim_free(info);
6914 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006915 }
6916 break;
6917
6918 case ISN_DEF:
6919 {
6920 char_u *name = iptr->isn_arg.string;
6921
6922 smsg("%s%4d DEF %s", pfx, current,
6923 name == NULL ? (char_u *)"" : name);
6924 }
6925 break;
6926
6927 case ISN_JUMP:
6928 {
6929 char *when = "?";
6930
6931 switch (iptr->isn_arg.jump.jump_when)
6932 {
6933 case JUMP_ALWAYS:
6934 when = "JUMP";
6935 break;
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02006936 case JUMP_NEVER:
6937 iemsg("JUMP_NEVER should not be used");
6938 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006939 case JUMP_AND_KEEP_IF_TRUE:
6940 when = "JUMP_AND_KEEP_IF_TRUE";
6941 break;
6942 case JUMP_IF_FALSE:
6943 when = "JUMP_IF_FALSE";
6944 break;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01006945 case JUMP_WHILE_FALSE:
6946 when = "JUMP_WHILE_FALSE"; // unused
6947 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006948 case JUMP_IF_COND_FALSE:
6949 when = "JUMP_IF_COND_FALSE";
6950 break;
6951 case JUMP_IF_COND_TRUE:
6952 when = "JUMP_IF_COND_TRUE";
6953 break;
6954 }
6955 smsg("%s%4d %s -> %d", pfx, current, when,
6956 iptr->isn_arg.jump.jump_where);
6957 }
6958 break;
6959
6960 case ISN_JUMP_IF_ARG_SET:
6961 smsg("%s%4d JUMP_IF_ARG_SET arg[%d] -> %d", pfx, current,
6962 iptr->isn_arg.jumparg.jump_arg_off + STACK_FRAME_SIZE,
6963 iptr->isn_arg.jump.jump_where);
6964 break;
6965
Bram Moolenaar65b0d162022-12-13 18:43:22 +00006966 case ISN_JUMP_IF_ARG_NOT_SET:
6967 smsg("%s%4d JUMP_IF_ARG_NOT_SET arg[%d] -> %d", pfx, current,
6968 iptr->isn_arg.jumparg.jump_arg_off + STACK_FRAME_SIZE,
6969 iptr->isn_arg.jump.jump_where);
6970 break;
6971
Bram Moolenaar4c137212021-04-19 16:48:48 +02006972 case ISN_FOR:
6973 {
6974 forloop_T *forloop = &iptr->isn_arg.forloop;
6975
6976 smsg("%s%4d FOR $%d -> %d", pfx, current,
Bram Moolenaarcc341812022-09-19 15:54:34 +01006977 forloop->for_loop_idx, forloop->for_end);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006978 }
6979 break;
6980
Bram Moolenaarb46c0832022-09-15 17:19:37 +01006981 case ISN_ENDLOOP:
6982 {
6983 endloop_T *endloop = &iptr->isn_arg.endloop;
6984
Bram Moolenaarcc341812022-09-19 15:54:34 +01006985 smsg("%s%4d ENDLOOP ref $%d save $%d-$%d depth %d",
6986 pfx, current,
Bram Moolenaarb46c0832022-09-15 17:19:37 +01006987 endloop->end_funcref_idx,
6988 endloop->end_var_idx,
Bram Moolenaarcc341812022-09-19 15:54:34 +01006989 endloop->end_var_idx + endloop->end_var_count - 1,
6990 endloop->end_depth);
Bram Moolenaarb46c0832022-09-15 17:19:37 +01006991 }
6992 break;
6993
6994 case ISN_WHILE:
6995 {
6996 whileloop_T *whileloop = &iptr->isn_arg.whileloop;
6997
6998 smsg("%s%4d WHILE $%d -> %d", pfx, current,
6999 whileloop->while_funcref_idx,
7000 whileloop->while_end);
7001 }
7002 break;
7003
Bram Moolenaar4c137212021-04-19 16:48:48 +02007004 case ISN_TRY:
7005 {
Bram Moolenaar0d807102021-12-21 09:42:09 +00007006 try_T *try = &iptr->isn_arg.tryref;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007007
7008 if (try->try_ref->try_finally == 0)
7009 smsg("%s%4d TRY catch -> %d, endtry -> %d",
7010 pfx, current,
7011 try->try_ref->try_catch,
7012 try->try_ref->try_endtry);
7013 else
7014 smsg("%s%4d TRY catch -> %d, finally -> %d, endtry -> %d",
7015 pfx, current,
7016 try->try_ref->try_catch,
7017 try->try_ref->try_finally,
7018 try->try_ref->try_endtry);
7019 }
7020 break;
7021 case ISN_CATCH:
Bram Moolenaar4c137212021-04-19 16:48:48 +02007022 smsg("%s%4d CATCH", pfx, current);
7023 break;
7024 case ISN_TRYCONT:
7025 {
7026 trycont_T *trycont = &iptr->isn_arg.trycont;
7027
7028 smsg("%s%4d TRY-CONTINUE %d level%s -> %d", pfx, current,
7029 trycont->tct_levels,
7030 trycont->tct_levels == 1 ? "" : "s",
7031 trycont->tct_where);
7032 }
7033 break;
7034 case ISN_FINALLY:
7035 smsg("%s%4d FINALLY", pfx, current);
7036 break;
7037 case ISN_ENDTRY:
7038 smsg("%s%4d ENDTRY", pfx, current);
7039 break;
7040 case ISN_THROW:
7041 smsg("%s%4d THROW", pfx, current);
7042 break;
7043
7044 // expression operations on number
7045 case ISN_OPNR:
7046 case ISN_OPFLOAT:
7047 case ISN_OPANY:
7048 {
7049 char *what;
7050 char *ins;
7051
7052 switch (iptr->isn_arg.op.op_type)
7053 {
7054 case EXPR_MULT: what = "*"; break;
7055 case EXPR_DIV: what = "/"; break;
7056 case EXPR_REM: what = "%"; break;
7057 case EXPR_SUB: what = "-"; break;
7058 case EXPR_ADD: what = "+"; break;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01007059 case EXPR_LSHIFT: what = "<<"; break;
7060 case EXPR_RSHIFT: what = ">>"; break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007061 default: what = "???"; break;
7062 }
7063 switch (iptr->isn_type)
7064 {
7065 case ISN_OPNR: ins = "OPNR"; break;
7066 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
7067 case ISN_OPANY: ins = "OPANY"; break;
7068 default: ins = "???"; break;
7069 }
7070 smsg("%s%4d %s %s", pfx, current, ins, what);
7071 }
7072 break;
7073
7074 case ISN_COMPAREBOOL:
7075 case ISN_COMPARESPECIAL:
Bram Moolenaar7a222242022-03-01 19:23:24 +00007076 case ISN_COMPARENULL:
Bram Moolenaar4c137212021-04-19 16:48:48 +02007077 case ISN_COMPARENR:
7078 case ISN_COMPAREFLOAT:
7079 case ISN_COMPARESTRING:
7080 case ISN_COMPAREBLOB:
7081 case ISN_COMPARELIST:
7082 case ISN_COMPAREDICT:
7083 case ISN_COMPAREFUNC:
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00007084 case ISN_COMPARECLASS:
7085 case ISN_COMPAREOBJECT:
Bram Moolenaar4c137212021-04-19 16:48:48 +02007086 case ISN_COMPAREANY:
7087 {
7088 char *p;
7089 char buf[10];
7090 char *type;
7091
7092 switch (iptr->isn_arg.op.op_type)
7093 {
7094 case EXPR_EQUAL: p = "=="; break;
7095 case EXPR_NEQUAL: p = "!="; break;
7096 case EXPR_GREATER: p = ">"; break;
7097 case EXPR_GEQUAL: p = ">="; break;
7098 case EXPR_SMALLER: p = "<"; break;
7099 case EXPR_SEQUAL: p = "<="; break;
7100 case EXPR_MATCH: p = "=~"; break;
7101 case EXPR_IS: p = "is"; break;
7102 case EXPR_ISNOT: p = "isnot"; break;
7103 case EXPR_NOMATCH: p = "!~"; break;
7104 default: p = "???"; break;
7105 }
7106 STRCPY(buf, p);
7107 if (iptr->isn_arg.op.op_ic == TRUE)
7108 strcat(buf, "?");
7109 switch(iptr->isn_type)
7110 {
7111 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
7112 case ISN_COMPARESPECIAL:
7113 type = "COMPARESPECIAL"; break;
Bram Moolenaar7a222242022-03-01 19:23:24 +00007114 case ISN_COMPARENULL: type = "COMPARENULL"; break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007115 case ISN_COMPARENR: type = "COMPARENR"; break;
7116 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
7117 case ISN_COMPARESTRING:
7118 type = "COMPARESTRING"; break;
7119 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
7120 case ISN_COMPARELIST: type = "COMPARELIST"; break;
7121 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
7122 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00007123 case ISN_COMPARECLASS: type = "COMPARECLASS"; break;
7124 case ISN_COMPAREOBJECT:
7125 type = "COMPAREOBJECT"; break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007126 case ISN_COMPAREANY: type = "COMPAREANY"; break;
7127 default: type = "???"; break;
7128 }
7129
7130 smsg("%s%4d %s %s", pfx, current, type, buf);
7131 }
7132 break;
7133
7134 case ISN_ADDLIST: smsg("%s%4d ADDLIST", pfx, current); break;
7135 case ISN_ADDBLOB: smsg("%s%4d ADDBLOB", pfx, current); break;
7136
7137 // expression operations
LemonBoy372bcce2022-04-25 12:43:20 +01007138 case ISN_CONCAT:
7139 smsg("%s%4d CONCAT size %lld", pfx, current,
7140 (varnumber_T)(iptr->isn_arg.number));
7141 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007142 case ISN_STRINDEX: smsg("%s%4d STRINDEX", pfx, current); break;
7143 case ISN_STRSLICE: smsg("%s%4d STRSLICE", pfx, current); break;
7144 case ISN_BLOBINDEX: smsg("%s%4d BLOBINDEX", pfx, current); break;
7145 case ISN_BLOBSLICE: smsg("%s%4d BLOBSLICE", pfx, current); break;
7146 case ISN_LISTAPPEND: smsg("%s%4d LISTAPPEND", pfx, current); break;
7147 case ISN_BLOBAPPEND: smsg("%s%4d BLOBAPPEND", pfx, current); break;
7148 case ISN_LISTINDEX: smsg("%s%4d LISTINDEX", pfx, current); break;
7149 case ISN_LISTSLICE: smsg("%s%4d LISTSLICE", pfx, current); break;
7150 case ISN_ANYINDEX: smsg("%s%4d ANYINDEX", pfx, current); break;
7151 case ISN_ANYSLICE: smsg("%s%4d ANYSLICE", pfx, current); break;
7152 case ISN_SLICE: smsg("%s%4d SLICE %lld",
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02007153 pfx, current, iptr->isn_arg.number); break;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02007154 case ISN_GETITEM: smsg("%s%4d ITEM %lld%s", pfx, current,
7155 iptr->isn_arg.getitem.gi_index,
7156 iptr->isn_arg.getitem.gi_with_op ?
7157 " with op" : ""); break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007158 case ISN_MEMBER: smsg("%s%4d MEMBER", pfx, current); break;
7159 case ISN_STRINGMEMBER: smsg("%s%4d MEMBER %s", pfx, current,
7160 iptr->isn_arg.string); break;
Yegappan Lakshmanan5a05d372023-09-29 19:43:11 +02007161 case ISN_GET_OBJ_MEMBER: smsg("%s%4d OBJ_MEMBER %d", pfx, current,
7162 (int)iptr->isn_arg.classmember.cm_idx);
Ernie Rael00df69e2023-09-05 07:38:09 +02007163 break;
Yegappan Lakshmanan5a05d372023-09-29 19:43:11 +02007164 case ISN_GET_ITF_MEMBER: smsg("%s%4d ITF_MEMBER %d on %s",
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00007165 pfx, current,
7166 (int)iptr->isn_arg.classmember.cm_idx,
Yegappan Lakshmanan5a05d372023-09-29 19:43:11 +02007167 iptr->isn_arg.classmember.cm_class->class_name);
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00007168 break;
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00007169 case ISN_STORE_THIS: smsg("%s%4d STORE_THIS %d", pfx, current,
Bram Moolenaarffdaca92022-12-09 21:41:48 +00007170 (int)iptr->isn_arg.number); break;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02007171 case ISN_CLEARDICT: smsg("%s%4d CLEARDICT", pfx, current); break;
7172 case ISN_USEDICT: smsg("%s%4d USEDICT", pfx, current); break;
7173
Bram Moolenaar4c137212021-04-19 16:48:48 +02007174 case ISN_NEGATENR: smsg("%s%4d NEGATENR", pfx, current); break;
7175
Bram Moolenaar4c137212021-04-19 16:48:48 +02007176 case ISN_CHECKTYPE:
7177 {
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01007178 checktype_T *ct = &iptr->isn_arg.type;
Bram Moolenaarc6951a72022-12-29 20:56:24 +00007179 char *tofree = NULL;
7180 char *typename;
7181
7182 if (ct->ct_type->tt_type == VAR_FLOAT
7183 && (ct->ct_type->tt_flags & TTFLAG_NUMBER_OK))
7184 typename = "float|number";
7185 else
7186 typename = type_name(ct->ct_type, &tofree);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007187
7188 if (ct->ct_arg_idx == 0)
7189 smsg("%s%4d CHECKTYPE %s stack[%d]", pfx, current,
Bram Moolenaarc6951a72022-12-29 20:56:24 +00007190 typename,
Bram Moolenaar4c137212021-04-19 16:48:48 +02007191 (int)ct->ct_off);
7192 else
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01007193 smsg("%s%4d CHECKTYPE %s stack[%d] %s %d",
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02007194 pfx, current,
Bram Moolenaarc6951a72022-12-29 20:56:24 +00007195 typename,
Bram Moolenaar4c137212021-04-19 16:48:48 +02007196 (int)ct->ct_off,
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01007197 ct->ct_is_var ? "var": "arg",
Bram Moolenaar4c137212021-04-19 16:48:48 +02007198 (int)ct->ct_arg_idx);
7199 vim_free(tofree);
7200 break;
7201 }
7202 case ISN_CHECKLEN: smsg("%s%4d CHECKLEN %s%d", pfx, current,
7203 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
7204 iptr->isn_arg.checklen.cl_min_len);
7205 break;
7206 case ISN_SETTYPE:
7207 {
7208 char *tofree;
7209
7210 smsg("%s%4d SETTYPE %s", pfx, current,
7211 type_name(iptr->isn_arg.type.ct_type, &tofree));
7212 vim_free(tofree);
7213 break;
7214 }
7215 case ISN_COND2BOOL: smsg("%s%4d COND2BOOL", pfx, current); break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007216 case ISN_2BOOL: if (iptr->isn_arg.tobool.invert)
7217 smsg("%s%4d INVERT %d (!val)", pfx, current,
7218 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007219 else
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007220 smsg("%s%4d 2BOOL %d (!!val)", pfx, current,
7221 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007222 break;
7223 case ISN_2STRING: smsg("%s%4d 2STRING stack[%lld]", pfx, current,
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007224 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02007225 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007226 case ISN_2STRING_ANY: smsg("%s%4d 2STRING_ANY stack[%lld]",
7227 pfx, current,
7228 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02007229 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007230 case ISN_RANGE: smsg("%s%4d RANGE %s", pfx, current,
7231 iptr->isn_arg.string);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007232 break;
7233 case ISN_PUT:
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01007234 if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE)
Bram Moolenaar4c137212021-04-19 16:48:48 +02007235 smsg("%s%4d PUT %c above range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007236 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007237 else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE)
7238 smsg("%s%4d PUT %c range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007239 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007240 else
7241 smsg("%s%4d PUT %c %ld", pfx, current,
7242 iptr->isn_arg.put.put_regname,
7243 (long)iptr->isn_arg.put.put_lnum);
7244 break;
7245
Bram Moolenaar4c137212021-04-19 16:48:48 +02007246 case ISN_CMDMOD:
7247 {
7248 char_u *buf;
7249 size_t len = produce_cmdmods(
7250 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
7251
7252 buf = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02007253 if (likely(buf != NULL))
Bram Moolenaar4c137212021-04-19 16:48:48 +02007254 {
7255 (void)produce_cmdmods(
7256 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
7257 smsg("%s%4d CMDMOD %s", pfx, current, buf);
7258 vim_free(buf);
7259 }
7260 break;
7261 }
7262 case ISN_CMDMOD_REV: smsg("%s%4d CMDMOD_REV", pfx, current); break;
7263
7264 case ISN_PROF_START:
Bram Moolenaare99d4222021-06-13 14:01:26 +02007265 smsg("%s%4d PROFILE START line %d", pfx, current,
7266 iptr->isn_lnum);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007267 break;
7268
7269 case ISN_PROF_END:
7270 smsg("%s%4d PROFILE END", pfx, current);
7271 break;
7272
Bram Moolenaare99d4222021-06-13 14:01:26 +02007273 case ISN_DEBUG:
Bram Moolenaar8cec9272021-06-23 20:20:53 +02007274 smsg("%s%4d DEBUG line %d-%d varcount %lld", pfx, current,
7275 iptr->isn_arg.debug.dbg_break_lnum + 1,
7276 iptr->isn_lnum,
7277 iptr->isn_arg.debug.dbg_var_names_len);
Bram Moolenaare99d4222021-06-13 14:01:26 +02007278 break;
7279
Bram Moolenaar4c137212021-04-19 16:48:48 +02007280 case ISN_UNPACK: smsg("%s%4d UNPACK %d%s", pfx, current,
7281 iptr->isn_arg.unpack.unp_count,
7282 iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : "");
7283 break;
7284 case ISN_SHUFFLE: smsg("%s%4d SHUFFLE %d up %d", pfx, current,
7285 iptr->isn_arg.shuffle.shfl_item,
7286 iptr->isn_arg.shuffle.shfl_up);
7287 break;
7288 case ISN_DROP: smsg("%s%4d DROP", pfx, current); break;
7289
7290 case ISN_FINISH: // End of list of instructions for ISN_SUBSTITUTE.
7291 return;
7292 }
7293
7294 out_flush(); // output one line at a time
7295 ui_breakcheck();
7296 if (got_int)
7297 break;
7298 }
7299}
7300
7301/*
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02007302 * Handle command line completion for the :disassemble command.
7303 */
7304 void
7305set_context_in_disassemble_cmd(expand_T *xp, char_u *arg)
7306{
7307 char_u *p;
7308
7309 // Default: expand user functions, "debug" and "profile"
7310 xp->xp_context = EXPAND_DISASSEMBLE;
7311 xp->xp_pattern = arg;
7312
7313 // first argument already typed: only user function names
7314 if (*arg != NUL && *(p = skiptowhite(arg)) != NUL)
7315 {
7316 xp->xp_context = EXPAND_USER_FUNC;
7317 xp->xp_pattern = skipwhite(p);
7318 }
7319}
7320
7321/*
7322 * Function given to ExpandGeneric() to obtain the list of :disassemble
7323 * arguments.
7324 */
7325 char_u *
7326get_disassemble_argument(expand_T *xp, int idx)
7327{
7328 if (idx == 0)
7329 return (char_u *)"debug";
7330 if (idx == 1)
7331 return (char_u *)"profile";
7332 return get_user_func_name(xp, idx - 2);
7333}
7334
7335/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01007336 * ":disassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01007337 * We don't really need this at runtime, but we do have tests that require it,
7338 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007339 */
7340 void
7341ex_disassemble(exarg_T *eap)
7342{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01007343 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01007344 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007345 dfunc_T *dfunc;
Bram Moolenaardafef512022-05-21 21:55:55 +01007346 isn_T *instr = NULL; // init to shut up gcc warning
7347 int instr_count = 0; // init to shut up gcc warning
Bram Moolenaar5a01caa2022-05-21 18:56:58 +01007348 compiletype_T compile_type = CT_NONE;
Bram Moolenaare99d4222021-06-13 14:01:26 +02007349
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01007350 ufunc = find_func_by_name(arg, &compile_type);
Bram Moolenaara26b9702020-04-18 19:53:28 +02007351 if (ufunc == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007352 return;
Bram Moolenaare99d4222021-06-13 14:01:26 +02007353 if (func_needs_compiling(ufunc, compile_type)
7354 && compile_def_function(ufunc, FALSE, compile_type, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02007355 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007356 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007357 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007358 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007359 return;
7360 }
Bram Moolenaar6db660b2021-08-01 14:08:54 +02007361 msg((char *)printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007362
7363 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
Bram Moolenaare99d4222021-06-13 14:01:26 +02007364 switch (compile_type)
7365 {
7366 case CT_PROFILE:
Bram Moolenaarf002a412021-01-24 13:34:18 +01007367#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02007368 instr = dfunc->df_instr_prof;
7369 instr_count = dfunc->df_instr_prof_count;
7370 break;
Bram Moolenaarf002a412021-01-24 13:34:18 +01007371#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02007372 // FALLTHROUGH
7373 case CT_NONE:
7374 instr = dfunc->df_instr;
7375 instr_count = dfunc->df_instr_count;
7376 break;
7377 case CT_DEBUG:
7378 instr = dfunc->df_instr_debug;
7379 instr_count = dfunc->df_instr_debug_count;
7380 break;
7381 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007382
Bram Moolenaar4c137212021-04-19 16:48:48 +02007383 list_instructions("", instr, instr_count, ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007384}
7385
7386/*
Bram Moolenaar13106602020-10-04 16:06:05 +02007387 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007388 * list, etc. Mostly like what JavaScript does, except that empty list and
7389 * empty dictionary are FALSE.
7390 */
7391 int
7392tv2bool(typval_T *tv)
7393{
7394 switch (tv->v_type)
7395 {
7396 case VAR_NUMBER:
7397 return tv->vval.v_number != 0;
7398 case VAR_FLOAT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007399 return tv->vval.v_float != 0.0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007400 case VAR_PARTIAL:
7401 return tv->vval.v_partial != NULL;
7402 case VAR_FUNC:
7403 case VAR_STRING:
7404 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
7405 case VAR_LIST:
7406 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
7407 case VAR_DICT:
7408 return tv->vval.v_dict != NULL
7409 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
7410 case VAR_BOOL:
7411 case VAR_SPECIAL:
7412 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
7413 case VAR_JOB:
7414#ifdef FEAT_JOB_CHANNEL
7415 return tv->vval.v_job != NULL;
7416#else
7417 break;
7418#endif
7419 case VAR_CHANNEL:
7420#ifdef FEAT_JOB_CHANNEL
7421 return tv->vval.v_channel != NULL;
7422#else
7423 break;
7424#endif
7425 case VAR_BLOB:
7426 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
7427 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02007428 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007429 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02007430 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00007431 case VAR_CLASS:
7432 case VAR_OBJECT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007433 break;
7434 }
7435 return FALSE;
7436}
7437
Bram Moolenaarea2d4072020-11-12 12:08:51 +01007438 void
7439emsg_using_string_as(typval_T *tv, int as_number)
7440{
7441 semsg(_(as_number ? e_using_string_as_number_str
7442 : e_using_string_as_bool_str),
7443 tv->vval.v_string == NULL
7444 ? (char_u *)"" : tv->vval.v_string);
7445}
7446
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007447/*
7448 * If "tv" is a string give an error and return FAIL.
7449 */
7450 int
7451check_not_string(typval_T *tv)
7452{
7453 if (tv->v_type == VAR_STRING)
7454 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +01007455 emsg_using_string_as(tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007456 clear_tv(tv);
7457 return FAIL;
7458 }
7459 return OK;
7460}
7461
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007462
7463#endif // FEAT_EVAL