blob: a6bf4715a88b44bc8b4371ecb87f30faac3b15f1 [file] [log] [blame]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * vim9execute.c: execute Vim9 script instructions
12 */
13
14#define USING_FLOAT_STUFF
15#include "vim.h"
16
17#if defined(FEAT_EVAL) || defined(PROTO)
18
Bram Moolenaardc7c3662021-12-20 15:04:29 +000019// When not generating protos this is included in proto.h
20#ifdef PROTO
21# include "vim9.h"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010022#endif
23
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010024
25// Structure put on ec_trystack when ISN_TRY is encountered.
26typedef struct {
Bram Moolenaard9d77892021-02-12 21:32:47 +010027 int tcd_frame_idx; // ec_frame_idx at ISN_TRY
28 int tcd_stack_len; // size of ectx.ec_stack at ISN_TRY
Bram Moolenaard3d8fee2021-06-30 19:54:43 +020029 int tcd_in_catch; // in catch or finally block
30 int tcd_did_throw; // set did_throw in :endtry
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +010031 int tcd_catch_idx; // instruction of the first :catch or :finally
32 int tcd_finally_idx; // instruction of the :finally block or zero
33 int tcd_endtry_idx; // instruction of the :endtry
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010034 int tcd_caught; // catch block entered
Bram Moolenaar2e34c342021-03-14 12:13:33 +010035 int tcd_cont; // :continue encountered, jump here (minus one)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010036 int tcd_return; // when TRUE return from end of :finally
37} trycmd_T;
38
Bram Moolenaar4c137212021-04-19 16:48:48 +020039// Data local to a function.
40// On a function call, if not empty, is saved on the stack and restored when
41// returning.
42typedef struct {
43 int floc_restore_cmdmod;
44 cmdmod_T floc_save_cmdmod;
45 int floc_restore_cmdmod_stacklen;
46} funclocal_T;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010047
Bram Moolenaarc04f2a42021-06-09 19:30:03 +020048// Structure to hold a reference to an outer_T, with information of whether it
49// was allocated.
50typedef struct {
51 outer_T *or_outer;
52 partial_T *or_partial; // decrement "or_partial->pt_refcount" later
53 int or_outer_allocated; // free "or_outer" later
54} outer_ref_T;
55
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010056// A stack is used to store:
57// - arguments passed to a :def function
58// - info about the calling function, to use when returning
59// - local variables
60// - temporary values
61//
62// In detail (FP == Frame Pointer):
63// arg1 first argument from caller (if present)
64// arg2 second argument from caller (if present)
65// extra_arg1 any missing optional argument default value
66// FP -> cur_func calling function
Bram Moolenaar6ed545e2022-05-09 20:09:23 +010067// current previous instruction pointer
68// frame_ptr previous Frame Pointer
69// var1 space for local variable
70// var2 space for local variable
71// .... fixed space for max. number of local variables
72// temp temporary values
73// .... flexible space for temporary values (can grow big)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010074
75/*
76 * Execution context.
77 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010078struct ectx_S {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010079 garray_T ec_stack; // stack of typval_T values
Bram Moolenaarbf67ea12020-05-02 17:52:42 +020080 int ec_frame_idx; // index in ec_stack: context of ec_dfunc_idx
Bram Moolenaar4c137212021-04-19 16:48:48 +020081 int ec_initial_frame_idx; // frame index when called
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010082
Bram Moolenaarc04f2a42021-06-09 19:30:03 +020083 outer_ref_T *ec_outer_ref; // outer scope used for closures, allocated
Bram Moolenaar4c137212021-04-19 16:48:48 +020084 funclocal_T ec_funclocal;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +020085
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010086 garray_T ec_trystack; // stack of trycmd_T values
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010087
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010088 isn_T *ec_instr; // array with instructions
Dominique Pelle3276f582021-08-07 12:44:41 +020089 int ec_dfunc_idx; // current function index
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010090 int ec_iidx; // index in ec_instr: instruction to execute
Bram Moolenaar148ce7a2020-09-23 21:57:23 +020091
92 garray_T ec_funcrefs; // partials that might be a closure
Bram Moolenaar4c137212021-04-19 16:48:48 +020093
94 int ec_did_emsg_before;
95 int ec_trylevel_at_start;
96 where_T ec_where;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010097};
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010098
Bram Moolenaar12d26532021-02-19 19:13:21 +010099#ifdef FEAT_PROFILE
100// stack of profinfo_T used when profiling.
101static garray_T profile_info_ga = {0, 0, sizeof(profinfo_T), 20, NULL};
102#endif
103
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100104// Get pointer to item in the stack.
105#define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
106
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100107// Get pointer to item relative to the bottom of the stack, -1 is the last one.
Bram Moolenaar11107ba2020-08-15 21:10:16 +0200108#define STACK_TV_BOT(idx) (((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_stack.ga_len + (idx))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100109
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100110// Get pointer to a local variable on the stack. Negative for arguments.
111#define STACK_TV_VAR(idx) (((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx + STACK_FRAME_SIZE + idx)
112
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200113 void
114to_string_error(vartype_T vartype)
115{
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200116 semsg(_(e_cannot_convert_str_to_string), vartype_name(vartype));
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200117}
118
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100119/*
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100120 * Return the number of arguments, including optional arguments and any vararg.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100121 */
122 static int
123ufunc_argcount(ufunc_T *ufunc)
124{
125 return ufunc->uf_args.ga_len + (ufunc->uf_va_name != NULL ? 1 : 0);
126}
127
128/*
LemonBoy372bcce2022-04-25 12:43:20 +0100129 * Create a new string from "count" items at the bottom of the stack.
130 * A trailing NUL is appended.
131 * When "count" is zero an empty string is added to the stack.
132 */
133 static int
134exe_concat(int count, ectx_T *ectx)
135{
136 int idx;
137 int len = 0;
138 typval_T *tv;
139 garray_T ga;
140
141 ga_init2(&ga, sizeof(char), 1);
142 // Preallocate enough space for the whole string to avoid having to grow
143 // and copy.
144 for (idx = 0; idx < count; ++idx)
145 {
146 tv = STACK_TV_BOT(idx - count);
147 if (tv->vval.v_string != NULL)
148 len += (int)STRLEN(tv->vval.v_string);
149 }
150
151 if (ga_grow(&ga, len + 1) == FAIL)
152 return FAIL;
153
154 for (idx = 0; idx < count; ++idx)
155 {
156 tv = STACK_TV_BOT(idx - count);
157 ga_concat(&ga, tv->vval.v_string);
158 clear_tv(tv);
159 }
160
161 // add a terminating NUL
162 ga_append(&ga, NUL);
163
164 ectx->ec_stack.ga_len -= count - 1;
165 STACK_TV_BOT(-1)->vval.v_string = ga.ga_data;
166
167 return OK;
168}
169
170/*
Bram Moolenaarfe270812020-04-11 22:31:27 +0200171 * Create a new list from "count" items at the bottom of the stack.
172 * When "count" is zero an empty list is added to the stack.
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100173 * When "count" is -1 a NULL list is added to the stack.
Bram Moolenaarfe270812020-04-11 22:31:27 +0200174 */
175 static int
176exe_newlist(int count, ectx_T *ectx)
177{
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100178 list_T *list = NULL;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200179 int idx;
180 typval_T *tv;
181
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100182 if (count >= 0)
183 {
184 list = list_alloc_with_items(count);
185 if (list == NULL)
186 return FAIL;
187 for (idx = 0; idx < count; ++idx)
188 list_set_item(list, idx, STACK_TV_BOT(idx - count));
189 }
Bram Moolenaarfe270812020-04-11 22:31:27 +0200190
191 if (count > 0)
192 ectx->ec_stack.ga_len -= count - 1;
Bram Moolenaar35578162021-08-02 19:10:38 +0200193 else if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100194 {
195 list_unref(list);
Bram Moolenaarfe270812020-04-11 22:31:27 +0200196 return FAIL;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100197 }
Bram Moolenaarfe270812020-04-11 22:31:27 +0200198 else
199 ++ectx->ec_stack.ga_len;
200 tv = STACK_TV_BOT(-1);
201 tv->v_type = VAR_LIST;
202 tv->vval.v_list = list;
Bram Moolenaar566badc2022-09-18 13:46:08 +0100203 tv->v_lock = 0;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100204 if (list != NULL)
205 ++list->lv_refcount;
206 return OK;
207}
208
209/*
210 * Implementation of ISN_NEWDICT.
211 * Returns FAIL on total failure, MAYBE on error.
212 */
213 static int
214exe_newdict(int count, ectx_T *ectx)
215{
216 dict_T *dict = NULL;
217 dictitem_T *item;
218 char_u *key;
219 int idx;
220 typval_T *tv;
221
222 if (count >= 0)
223 {
224 dict = dict_alloc();
225 if (unlikely(dict == NULL))
226 return FAIL;
227 for (idx = 0; idx < count; ++idx)
228 {
229 // have already checked key type is VAR_STRING
230 tv = STACK_TV_BOT(2 * (idx - count));
231 // check key is unique
Bram Moolenaara9fa8c52023-01-02 18:10:04 +0000232 key = tv->vval.v_string == NULL ? (char_u *)"" : tv->vval.v_string;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100233 item = dict_find(dict, key, -1);
234 if (item != NULL)
235 {
Bram Moolenaara9fa8c52023-01-02 18:10:04 +0000236 semsg(_(e_duplicate_key_in_dictionary_str), key);
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100237 dict_unref(dict);
238 return MAYBE;
239 }
240 item = dictitem_alloc(key);
241 clear_tv(tv);
242 if (unlikely(item == NULL))
243 {
244 dict_unref(dict);
245 return FAIL;
246 }
Bram Moolenaar0d1f55c2022-04-05 17:30:29 +0100247 tv = STACK_TV_BOT(2 * (idx - count) + 1);
248 item->di_tv = *tv;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100249 item->di_tv.v_lock = 0;
Bram Moolenaar0d1f55c2022-04-05 17:30:29 +0100250 tv->v_type = VAR_UNKNOWN;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100251 if (dict_add(dict, item) == FAIL)
252 {
253 // can this ever happen?
254 dict_unref(dict);
255 return FAIL;
256 }
257 }
258 }
259
260 if (count > 0)
261 ectx->ec_stack.ga_len -= 2 * count - 1;
262 else if (GA_GROW_FAILS(&ectx->ec_stack, 1))
263 return FAIL;
264 else
265 ++ectx->ec_stack.ga_len;
266 tv = STACK_TV_BOT(-1);
267 tv->v_type = VAR_DICT;
268 tv->v_lock = 0;
269 tv->vval.v_dict = dict;
270 if (dict != NULL)
271 ++dict->dv_refcount;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200272 return OK;
273}
274
275/*
Bram Moolenaar4f8f5422021-06-20 19:28:14 +0200276 * If debug_tick changed check if "ufunc" has a breakpoint and update
277 * "uf_has_breakpoint".
278 */
Bram Moolenaar96923b72022-03-15 15:57:04 +0000279 void
Bram Moolenaar4f8f5422021-06-20 19:28:14 +0200280update_has_breakpoint(ufunc_T *ufunc)
281{
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000282 if (ufunc->uf_debug_tick == debug_tick)
283 return;
Bram Moolenaar4f8f5422021-06-20 19:28:14 +0200284
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000285 linenr_T breakpoint;
286
287 ufunc->uf_debug_tick = debug_tick;
288 breakpoint = dbg_find_breakpoint(FALSE, ufunc->uf_name, 0);
289 ufunc->uf_has_breakpoint = breakpoint > 0;
Bram Moolenaar4f8f5422021-06-20 19:28:14 +0200290}
291
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +0200292static garray_T dict_stack = GA_EMPTY;
293
294/*
295 * Put a value on the dict stack. This consumes "tv".
296 */
297 static int
298dict_stack_save(typval_T *tv)
299{
300 if (dict_stack.ga_growsize == 0)
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000301 ga_init2(&dict_stack, sizeof(typval_T), 10);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +0200302 if (ga_grow(&dict_stack, 1) == FAIL)
303 return FAIL;
304 ((typval_T *)dict_stack.ga_data)[dict_stack.ga_len] = *tv;
305 ++dict_stack.ga_len;
306 return OK;
307}
308
309/*
310 * Get the typval at top of the dict stack.
311 */
312 static typval_T *
313dict_stack_get_tv(void)
314{
315 if (dict_stack.ga_len == 0)
316 return NULL;
317 return ((typval_T *)dict_stack.ga_data) + dict_stack.ga_len - 1;
318}
319
320/*
321 * Get the dict at top of the dict stack.
322 */
323 static dict_T *
324dict_stack_get_dict(void)
325{
326 typval_T *tv;
327
328 if (dict_stack.ga_len == 0)
329 return NULL;
330 tv = ((typval_T *)dict_stack.ga_data) + dict_stack.ga_len - 1;
331 if (tv->v_type == VAR_DICT)
332 return tv->vval.v_dict;
333 return NULL;
334}
335
336/*
337 * Drop an item from the dict stack.
338 */
339 static void
340dict_stack_drop(void)
341{
342 if (dict_stack.ga_len == 0)
343 {
344 iemsg("Dict stack underflow");
345 return;
346 }
347 --dict_stack.ga_len;
348 clear_tv(((typval_T *)dict_stack.ga_data) + dict_stack.ga_len);
349}
350
351/*
352 * Drop items from the dict stack until the length is equal to "len".
353 */
354 static void
355dict_stack_clear(int len)
356{
357 while (dict_stack.ga_len > len)
358 dict_stack_drop();
359}
360
Bram Moolenaar4f8f5422021-06-20 19:28:14 +0200361/*
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +0000362 * Get a pointer to useful "pt_outer" of "pt".
363 */
364 static outer_T *
365get_pt_outer(partial_T *pt)
366{
367 partial_T *ptref = pt->pt_outer_partial;
368
369 if (ptref == NULL)
370 return &pt->pt_outer;
371
372 // partial using partial (recursively)
373 while (ptref->pt_outer_partial != NULL)
374 ptref = ptref->pt_outer_partial;
375 return &ptref->pt_outer;
376}
377
378/*
Bram Moolenaar0d89d8a2022-12-31 14:01:24 +0000379 * Check "argcount" arguments on the stack against what "ufunc" expects.
380 * "off" is the offset of arguments on the stack.
381 * Return OK or FAIL.
382 */
383 static int
384check_ufunc_arg_types(ufunc_T *ufunc, int argcount, int off, ectx_T *ectx)
385{
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000386 if (ufunc->uf_arg_types == NULL && ufunc->uf_va_type == NULL)
387 return OK;
388
389 typval_T *argv = STACK_TV_BOT(0) - argcount - off;
390
391 // The function can change at runtime, check that the argument
392 // types are correct.
393 for (int i = 0; i < argcount; ++i)
Bram Moolenaar0d89d8a2022-12-31 14:01:24 +0000394 {
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000395 type_T *type = NULL;
Bram Moolenaar0d89d8a2022-12-31 14:01:24 +0000396
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000397 // assume a v:none argument, using the default value, is always OK
398 if (argv[i].v_type == VAR_SPECIAL
399 && argv[i].vval.v_number == VVAL_NONE)
400 continue;
Bram Moolenaar0d89d8a2022-12-31 14:01:24 +0000401
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +0000402 if (i < ufunc->uf_args.ga_len && ufunc->uf_arg_types != NULL)
403 type = ufunc->uf_arg_types[i];
404 else if (ufunc->uf_va_type != NULL)
405 type = ufunc->uf_va_type->tt_member;
406 if (type != NULL && check_typval_arg_type(type,
407 &argv[i], NULL, i + 1) == FAIL)
408 return FAIL;
Bram Moolenaar0d89d8a2022-12-31 14:01:24 +0000409 }
410 return OK;
411}
412
413/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100414 * Call compiled function "cdf_idx" from compiled code.
Bram Moolenaarab360522021-01-10 14:02:28 +0100415 * This adds a stack frame and sets the instruction pointer to the start of the
416 * called function.
Bram Moolenaar5800c792022-09-22 17:34:01 +0100417 * If "pt_arg" is not NULL use "pt_arg->pt_outer" for ec_outer_ref->or_outer.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100418 *
419 * Stack has:
420 * - current arguments (already there)
421 * - omitted optional argument (default values) added here
422 * - stack frame:
423 * - pointer to calling function
424 * - Index of next instruction in calling function
425 * - previous frame pointer
426 * - reserved space for local variables
427 */
428 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100429call_dfunc(
430 int cdf_idx,
Bram Moolenaar5800c792022-09-22 17:34:01 +0100431 partial_T *pt_arg,
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100432 int argcount_arg,
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100433 ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100434{
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100435 int argcount = argcount_arg;
436 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + cdf_idx;
437 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200438 int did_emsg_before = did_emsg_cumul + did_emsg;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100439 int arg_to_add;
440 int vararg_count = 0;
441 int varcount;
442 int idx;
443 estack_T *entry;
444 funclocal_T *floc = NULL;
Bram Moolenaar648594e2021-07-11 17:55:01 +0200445 int res = OK;
Bram Moolenaar139575d2022-03-15 19:29:30 +0000446 compiletype_T compile_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100447
448 if (dfunc->df_deleted)
449 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100450 // don't use ufunc->uf_name, it may have been freed
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000451 emsg_funcname(e_function_was_deleted_str,
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100452 dfunc->df_name == NULL ? (char_u *)"unknown" : dfunc->df_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100453 return FAIL;
454 }
455
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100456#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +0100457 if (do_profiling == PROF_YES)
458 {
Bram Moolenaar35578162021-08-02 19:10:38 +0200459 if (GA_GROW_OK(&profile_info_ga, 1))
Bram Moolenaar12d26532021-02-19 19:13:21 +0100460 {
461 profinfo_T *info = ((profinfo_T *)profile_info_ga.ga_data)
462 + profile_info_ga.ga_len;
463 ++profile_info_ga.ga_len;
464 CLEAR_POINTER(info);
465 profile_may_start_func(info, ufunc,
466 (((dfunc_T *)def_functions.ga_data)
467 + ectx->ec_dfunc_idx)->df_ufunc);
468 }
Bram Moolenaar12d26532021-02-19 19:13:21 +0100469 }
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100470#endif
471
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200472 // When debugging and using "cont" switches to the not-debugged
473 // instructions, may need to still compile them.
Bram Moolenaar139575d2022-03-15 19:29:30 +0000474 compile_type = get_compile_type(ufunc);
475 if (func_needs_compiling(ufunc, compile_type))
Bram Moolenaar648594e2021-07-11 17:55:01 +0200476 {
Bram Moolenaar139575d2022-03-15 19:29:30 +0000477 res = compile_def_function(ufunc, FALSE, compile_type, NULL);
Bram Moolenaar648594e2021-07-11 17:55:01 +0200478
479 // compile_def_function() may cause def_functions.ga_data to change
480 dfunc = ((dfunc_T *)def_functions.ga_data) + cdf_idx;
481 }
482 if (res == FAIL || INSTRUCTIONS(dfunc) == NULL)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200483 {
484 if (did_emsg_cumul + did_emsg == did_emsg_before)
485 semsg(_(e_function_is_not_compiled_str),
486 printable_func_name(ufunc));
487 return FAIL;
488 }
489
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200490 if (ufunc->uf_va_name != NULL)
491 {
Bram Moolenaarfe270812020-04-11 22:31:27 +0200492 // Need to make a list out of the vararg arguments.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200493 // Stack at time of call with 2 varargs:
494 // normal_arg
495 // optional_arg
496 // vararg_1
497 // vararg_2
Bram Moolenaarfe270812020-04-11 22:31:27 +0200498 // After creating the list:
499 // normal_arg
500 // optional_arg
501 // vararg-list
502 // With missing optional arguments we get:
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200503 // normal_arg
Bram Moolenaarfe270812020-04-11 22:31:27 +0200504 // After creating the list
505 // normal_arg
506 // (space for optional_arg)
507 // vararg-list
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200508 vararg_count = argcount - ufunc->uf_args.ga_len;
509 if (vararg_count < 0)
510 vararg_count = 0;
511 else
512 argcount -= vararg_count;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200513 if (exe_newlist(vararg_count, ectx) == FAIL)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200514 return FAIL;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200515
516 vararg_count = 1;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200517 }
518
Bram Moolenaarfe270812020-04-11 22:31:27 +0200519 arg_to_add = ufunc->uf_args.ga_len - argcount;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200520 if (arg_to_add < 0)
521 {
Matvey Tarasovd14bb1a2022-06-29 13:18:27 +0100522 semsg(NGETTEXT(e_one_argument_too_many, e_nr_arguments_too_many,
523 -arg_to_add), -arg_to_add);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200524 return FAIL;
525 }
Bram Moolenaarcd1cda22022-02-16 21:48:25 +0000526 else if (arg_to_add > ufunc->uf_def_args.ga_len)
527 {
528 int missing = arg_to_add - ufunc->uf_def_args.ga_len;
529
Matvey Tarasovd14bb1a2022-06-29 13:18:27 +0100530 semsg(NGETTEXT(e_one_argument_too_few, e_nr_arguments_too_few,
531 missing), missing);
Bram Moolenaarcd1cda22022-02-16 21:48:25 +0000532 return FAIL;
533 }
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200534
Bram Moolenaar574950d2023-01-03 19:08:50 +0000535 // If this is an object method, the object is just before the arguments.
536 typval_T *obj = STACK_TV_BOT(0) - argcount - vararg_count - 1;
537
Yegappan Lakshmananc1946262023-09-25 21:00:46 +0200538 if (IS_OBJECT_METHOD(ufunc) && !IS_CONSTRUCTOR_METHOD(ufunc)
539 && obj->v_type == VAR_OBJECT && obj->vval.v_object == NULL)
540 {
541 // If this is not a constructor method, then a valid object is
542 // needed.
543 emsg(_(e_using_null_object));
544 return FAIL;
545 }
546
Bram Moolenaar0d89d8a2022-12-31 14:01:24 +0000547 // Check the argument types.
548 if (check_ufunc_arg_types(ufunc, argcount, vararg_count, ectx) == FAIL)
549 return FAIL;
550
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200551 // Reserve space for:
552 // - missing arguments
553 // - stack frame
554 // - local variables
555 // - if needed: a counter for number of closures created in
556 // ectx->ec_funcrefs.
557 varcount = dfunc->df_varcount + dfunc->df_has_closure;
Bram Moolenaarb46c0832022-09-15 17:19:37 +0100558 if (GA_GROW_FAILS(&ectx->ec_stack,
559 arg_to_add + STACK_FRAME_SIZE + varcount))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100560 return FAIL;
561
Yegappan Lakshmanan1087b8c2023-10-07 22:03:18 +0200562 // The object pointer is in the execution typval stack. The GA_GROW call
563 // above may have reallocated the execution typval stack. So the object
564 // pointer may not be valid anymore. Get the object pointer again from the
565 // execution stack.
566 obj = STACK_TV_BOT(0) - argcount - vararg_count - 1;
567
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100568 // If depth of calling is getting too high, don't execute the function.
569 if (funcdepth_increment() == FAIL)
570 return FAIL;
Bram Moolenaare99d4222021-06-13 14:01:26 +0200571 ++ex_nesting_level;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100572
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100573 // Only make a copy of funclocal if it contains something to restore.
Bram Moolenaar4c137212021-04-19 16:48:48 +0200574 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100575 {
576 floc = ALLOC_ONE(funclocal_T);
577 if (floc == NULL)
578 return FAIL;
Bram Moolenaar4c137212021-04-19 16:48:48 +0200579 *floc = ectx->ec_funclocal;
580 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100581 }
582
Bram Moolenaarfe270812020-04-11 22:31:27 +0200583 // Move the vararg-list to below the missing optional arguments.
584 if (vararg_count > 0 && arg_to_add > 0)
585 *STACK_TV_BOT(arg_to_add - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100586
587 // Reserve space for omitted optional arguments, filled in soon.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200588 for (idx = 0; idx < arg_to_add; ++idx)
Bram Moolenaarfe270812020-04-11 22:31:27 +0200589 STACK_TV_BOT(idx - vararg_count)->v_type = VAR_UNKNOWN;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200590 ectx->ec_stack.ga_len += arg_to_add;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100591
592 // Store current execution state in stack frame for ISN_RETURN.
Bram Moolenaar0186e582021-01-10 18:33:11 +0100593 STACK_TV_BOT(STACK_FRAME_FUNC_OFF)->vval.v_number = ectx->ec_dfunc_idx;
594 STACK_TV_BOT(STACK_FRAME_IIDX_OFF)->vval.v_number = ectx->ec_iidx;
Bram Moolenaar5930ddc2021-04-26 20:32:59 +0200595 STACK_TV_BOT(STACK_FRAME_INSTR_OFF)->vval.v_string = (void *)ectx->ec_instr;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200596 STACK_TV_BOT(STACK_FRAME_OUTER_OFF)->vval.v_string =
597 (void *)ectx->ec_outer_ref;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100598 STACK_TV_BOT(STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string = (void *)floc;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100599 STACK_TV_BOT(STACK_FRAME_IDX_OFF)->vval.v_number = ectx->ec_frame_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200600 ectx->ec_frame_idx = ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100601
Bram Moolenaar5800c792022-09-22 17:34:01 +0100602 // Initialize all local variables to number zero. Also initialize the
603 // variable that counts how many closures were created. This is used in
604 // handle_closure_in_use().
605 int initcount = dfunc->df_varcount + (dfunc->df_has_closure ? 1 : 0);
606 for (idx = 0; idx < initcount; ++idx)
Bram Moolenaar5cd64792021-12-25 18:23:24 +0000607 {
608 typval_T *tv = STACK_TV_BOT(STACK_FRAME_SIZE + idx);
609
610 tv->v_type = VAR_NUMBER;
611 tv->vval.v_number = 0;
612 }
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200613 ectx->ec_stack.ga_len += STACK_FRAME_SIZE + varcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100614
Bram Moolenaar574950d2023-01-03 19:08:50 +0000615 // For an object method move the object from just before the arguments to
616 // the first local variable.
Yegappan Lakshmanan1db15142023-09-19 20:34:05 +0200617 if (IS_OBJECT_METHOD(ufunc))
Bram Moolenaar574950d2023-01-03 19:08:50 +0000618 {
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +0200619 if (obj->v_type != VAR_OBJECT)
620 {
621 semsg(_(e_internal_error_str), "type in stack is not an object");
622 return FAIL;
623 }
624
Bram Moolenaar574950d2023-01-03 19:08:50 +0000625 *STACK_TV_VAR(0) = *obj;
626 obj->v_type = VAR_UNKNOWN;
627 }
628
Bram Moolenaar5800c792022-09-22 17:34:01 +0100629 partial_T *pt = pt_arg != NULL ? pt_arg : ufunc->uf_partial;
630 if (pt != NULL || (ufunc->uf_flags & FC_CLOSURE))
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100631 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200632 outer_ref_T *ref = ALLOC_CLEAR_ONE(outer_ref_T);
Bram Moolenaar0186e582021-01-10 18:33:11 +0100633
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200634 if (ref == NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +0100635 return FAIL;
636 if (pt != NULL)
637 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +0000638 ref->or_outer = get_pt_outer(pt);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200639 ++pt->pt_refcount;
640 ref->or_partial = pt;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100641 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100642 else
643 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200644 ref->or_outer = ALLOC_CLEAR_ONE(outer_T);
Dominique Pelle5a9e5842021-07-24 19:32:12 +0200645 if (unlikely(ref->or_outer == NULL))
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200646 {
647 vim_free(ref);
648 return FAIL;
649 }
650 ref->or_outer_allocated = TRUE;
651 ref->or_outer->out_stack = &ectx->ec_stack;
652 ref->or_outer->out_frame_idx = ectx->ec_frame_idx;
653 if (ectx->ec_outer_ref != NULL)
654 ref->or_outer->out_up = ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100655 }
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200656 ectx->ec_outer_ref = ref;
Bram Moolenaarab360522021-01-10 14:02:28 +0100657 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100658 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200659 ectx->ec_outer_ref = NULL;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100660
Bram Moolenaarc970e422021-03-17 15:03:04 +0100661 ++ufunc->uf_calls;
662
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100663 // Set execution state to the start of the called function.
664 ectx->ec_dfunc_idx = cdf_idx;
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100665 ectx->ec_instr = INSTRUCTIONS(dfunc);
Bram Moolenaarb2049902021-01-24 12:53:53 +0100666 entry = estack_push_ufunc(ufunc, 1);
Bram Moolenaarc620c052020-07-08 15:16:19 +0200667 if (entry != NULL)
668 {
669 // Set the script context to the script where the function was defined.
Bram Moolenaarc70fe462021-04-17 17:59:19 +0200670 // Save the current context so it can be restored on return.
671 entry->es_save_sctx = current_sctx;
672 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200673 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100674
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +0200675 // Start execution at the first instruction.
676 ectx->ec_iidx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100677
678 return OK;
679}
680
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000681// Double linked list of funcstack_T in use.
682static funcstack_T *first_funcstack = NULL;
683
684 static void
685add_funcstack_to_list(funcstack_T *funcstack)
686{
687 // Link in list of funcstacks.
688 if (first_funcstack != NULL)
689 first_funcstack->fs_prev = funcstack;
690 funcstack->fs_next = first_funcstack;
691 funcstack->fs_prev = NULL;
692 first_funcstack = funcstack;
693}
694
695 static void
696remove_funcstack_from_list(funcstack_T *funcstack)
697{
698 if (funcstack->fs_prev == NULL)
699 first_funcstack = funcstack->fs_next;
700 else
701 funcstack->fs_prev->fs_next = funcstack->fs_next;
702 if (funcstack->fs_next != NULL)
703 funcstack->fs_next->fs_prev = funcstack->fs_prev;
704}
705
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100706/*
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200707 * Used when returning from a function: Check if any closure is still
708 * referenced. If so then move the arguments and variables to a separate piece
709 * of stack to be used when the closure is called.
710 * When "free_arguments" is TRUE the arguments are to be freed.
711 * Returns FAIL when out of memory.
712 */
713 static int
714handle_closure_in_use(ectx_T *ectx, int free_arguments)
715{
716 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
717 + ectx->ec_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200718 int argcount;
719 int top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200720 int idx;
721 typval_T *tv;
722 int closure_in_use = FALSE;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200723 garray_T *gap = &ectx->ec_funcrefs;
724 varnumber_T closure_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200725
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200726 if (dfunc->df_ufunc == NULL)
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200727 return OK; // function was freed
728 if (dfunc->df_has_closure == 0)
729 return OK; // no closures
730 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + dfunc->df_varcount);
731 closure_count = tv->vval.v_number;
732 if (closure_count == 0)
733 return OK; // no funcrefs created
734
Bram Moolenaar8fa745e2022-09-16 19:04:24 +0100735 // Compute "top": the first entry in the stack used by the function.
736 // This is the first argument (after that comes the stack frame and then
737 // the local variables).
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200738 argcount = ufunc_argcount(dfunc->df_ufunc);
739 top = ectx->ec_frame_idx - argcount;
740
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200741 // Check if any created closure is still in use.
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200742 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200743 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200744 partial_T *pt;
745 int off = gap->ga_len - closure_count + idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200746
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200747 if (off < 0)
748 continue; // count is off or already done
749 pt = ((partial_T **)gap->ga_data)[off];
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200750 if (pt->pt_refcount > 1)
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200751 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200752 int refcount = pt->pt_refcount;
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200753 int i;
754
Dominique Pelleaf4a61a2021-12-27 17:21:41 +0000755 // A Reference in a local variable doesn't count, it gets
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200756 // unreferenced on return.
757 for (i = 0; i < dfunc->df_varcount; ++i)
758 {
759 typval_T *stv = STACK_TV(ectx->ec_frame_idx
760 + STACK_FRAME_SIZE + i);
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200761 if (stv->v_type == VAR_PARTIAL && pt == stv->vval.v_partial)
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200762 --refcount;
763 }
764 if (refcount > 1)
765 {
766 closure_in_use = TRUE;
767 break;
768 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200769 }
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200770 }
771
772 if (closure_in_use)
773 {
774 funcstack_T *funcstack = ALLOC_CLEAR_ONE(funcstack_T);
775 typval_T *stack;
776
777 // A closure is using the arguments and/or local variables.
778 // Move them to the called function.
779 if (funcstack == NULL)
780 return FAIL;
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000781
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200782 funcstack->fs_var_offset = argcount + STACK_FRAME_SIZE;
Bram Moolenaar1936c762022-09-28 15:19:10 +0100783 funcstack->fs_ga.ga_len = funcstack->fs_var_offset
784 + dfunc->df_varcount;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200785 stack = ALLOC_CLEAR_MULT(typval_T, funcstack->fs_ga.ga_len);
786 funcstack->fs_ga.ga_data = stack;
787 if (stack == NULL)
788 {
789 vim_free(funcstack);
790 return FAIL;
791 }
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000792 add_funcstack_to_list(funcstack);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200793
794 // Move or copy the arguments.
795 for (idx = 0; idx < argcount; ++idx)
796 {
797 tv = STACK_TV(top + idx);
798 if (free_arguments)
799 {
800 *(stack + idx) = *tv;
801 tv->v_type = VAR_UNKNOWN;
802 }
803 else
804 copy_tv(tv, stack + idx);
805 }
Bram Moolenaar8fa745e2022-09-16 19:04:24 +0100806 // Skip the stack frame.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200807 // Move the local variables.
808 for (idx = 0; idx < dfunc->df_varcount; ++idx)
809 {
810 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + idx);
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200811
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200812 // A partial created for a local function, that is also used as a
813 // local variable, has a reference count for the variable, thus
814 // will never go down to zero. When all these refcounts are one
815 // then the funcstack is unused. We need to count how many we have
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000816 // so we know when to check.
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200817 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL)
818 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200819 int i;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200820
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200821 for (i = 0; i < closure_count; ++i)
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200822 if (tv->vval.v_partial == ((partial_T **)gap->ga_data)[
823 gap->ga_len - closure_count + i])
824 ++funcstack->fs_min_refcount;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200825 }
826
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200827 *(stack + funcstack->fs_var_offset + idx) = *tv;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200828 tv->v_type = VAR_UNKNOWN;
829 }
830
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200831 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200832 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200833 partial_T *pt = ((partial_T **)gap->ga_data)[gap->ga_len
834 - closure_count + idx];
835 if (pt->pt_refcount > 1)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200836 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200837 ++funcstack->fs_refcount;
838 pt->pt_funcstack = funcstack;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100839 pt->pt_outer.out_stack = &funcstack->fs_ga;
840 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx - top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200841 }
842 }
843 }
844
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200845 for (idx = 0; idx < closure_count; ++idx)
846 partial_unref(((partial_T **)gap->ga_data)[gap->ga_len
847 - closure_count + idx]);
848 gap->ga_len -= closure_count;
849 if (gap->ga_len == 0)
850 ga_clear(gap);
851
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200852 return OK;
853}
854
855/*
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200856 * Called when a partial is freed or its reference count goes down to one. The
857 * funcstack may be the only reference to the partials in the local variables.
858 * Go over all of them, the funcref and can be freed if all partials
859 * referencing the funcstack have a reference count of one.
Bram Moolenaaracd6b992022-09-17 16:27:39 +0100860 * Returns TRUE if the funcstack is freed, the partial referencing it will then
861 * also have been freed.
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200862 */
Bram Moolenaaracd6b992022-09-17 16:27:39 +0100863 int
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200864funcstack_check_refcount(funcstack_T *funcstack)
865{
Bram Moolenaaracd6b992022-09-17 16:27:39 +0100866 int i;
867 garray_T *gap = &funcstack->fs_ga;
868 int done = 0;
869 typval_T *stack;
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200870
871 if (funcstack->fs_refcount > funcstack->fs_min_refcount)
Bram Moolenaaracd6b992022-09-17 16:27:39 +0100872 return FALSE;
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200873 for (i = funcstack->fs_var_offset; i < gap->ga_len; ++i)
874 {
875 typval_T *tv = ((typval_T *)gap->ga_data) + i;
876
877 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL
878 && tv->vval.v_partial->pt_funcstack == funcstack
879 && tv->vval.v_partial->pt_refcount == 1)
880 ++done;
881 }
Bram Moolenaaracd6b992022-09-17 16:27:39 +0100882 if (done != funcstack->fs_min_refcount)
883 return FALSE;
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200884
Bram Moolenaaracd6b992022-09-17 16:27:39 +0100885 stack = gap->ga_data;
886
887 // All partials referencing the funcstack have a reference count of
888 // one, thus the funcstack is no longer of use.
889 for (i = 0; i < gap->ga_len; ++i)
890 clear_tv(stack + i);
891 vim_free(stack);
892 remove_funcstack_from_list(funcstack);
893 vim_free(funcstack);
894
895 return TRUE;
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200896}
897
898/*
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000899 * For garbage collecting: set references in all variables referenced by
900 * all funcstacks.
901 */
902 int
903set_ref_in_funcstacks(int copyID)
904{
905 funcstack_T *funcstack;
906
907 for (funcstack = first_funcstack; funcstack != NULL;
908 funcstack = funcstack->fs_next)
909 {
910 typval_T *stack = funcstack->fs_ga.ga_data;
911 int i;
912
913 for (i = 0; i < funcstack->fs_ga.ga_len; ++i)
914 if (set_ref_in_item(stack + i, copyID, NULL, NULL))
915 return TRUE; // abort
916 }
917 return FALSE;
918}
919
Bram Moolenaar806a2732022-09-04 15:40:36 +0100920// Ugly static to avoid passing the execution context around through many
921// layers.
922static ectx_T *current_ectx = NULL;
923
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000924/*
Bram Moolenaar806a2732022-09-04 15:40:36 +0100925 * Return TRUE if currently executing a :def function.
926 * Can be used by builtin functions only.
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100927 */
Bram Moolenaar806a2732022-09-04 15:40:36 +0100928 int
929in_def_function(void)
930{
931 return current_ectx != NULL;
932}
933
934/*
Ernie Rael4c8da022023-10-11 21:35:11 +0200935 * If executing a class/object method, then fill in the lval_T.
936 * Set lr_tv to the executing item, and lr_exec_class to the executing class;
937 * use free_tv and class_unref when finished with the lval_root.
938 * For use by builtin functions.
939 *
940 * Return FAIL and do nothing if not executing in a class; otherwise OK.
941 */
942 int
943fill_exec_lval_root(lval_root_T *root)
944{
945 ectx_T *ectx = current_ectx;
946 if (ectx != NULL)
947 {
948 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
949 + current_ectx->ec_dfunc_idx;
950 ufunc_T *ufunc = dfunc->df_ufunc;
951 if (ufunc->uf_class != NULL) // executing a method?
952 {
953 typval_T *tv = alloc_tv();
954 if (tv != NULL)
955 {
956 CLEAR_POINTER(root);
957 root->lr_tv = tv;
958 copy_tv(STACK_TV_VAR(0), root->lr_tv);
959 root->lr_cl_exec = ufunc->uf_class;
960 ++root->lr_cl_exec->class_refcount;
961 return OK;
962 }
963 }
964 }
965
966 return FAIL;
967}
968
969/*
Bram Moolenaar98aff652022-09-06 21:02:35 +0100970 * Clear "current_ectx" and return the previous value. To be used when calling
971 * a user function.
972 */
973 ectx_T *
dundargocc57b5bc2022-11-02 13:30:51 +0000974clear_current_ectx(void)
Bram Moolenaar98aff652022-09-06 21:02:35 +0100975{
976 ectx_T *r = current_ectx;
977
978 current_ectx = NULL;
979 return r;
980}
981
982 void
983restore_current_ectx(ectx_T *ectx)
984{
985 if (current_ectx != NULL)
986 iemsg("Restoring current_ectx while it is not NULL");
987 current_ectx = ectx;
988}
989
990/*
Bram Moolenaar806a2732022-09-04 15:40:36 +0100991 * Add an entry for a deferred function call to the currently executing
992 * function.
993 * Return the list or NULL when failed.
994 */
995 static list_T *
996add_defer_item(int var_idx, int argcount, ectx_T *ectx)
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100997{
998 typval_T *defer_tv = STACK_TV_VAR(var_idx);
999 list_T *defer_l;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001000 list_T *l;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001001 typval_T listval;
1002
1003 if (defer_tv->v_type != VAR_LIST)
1004 {
Bram Moolenaara5348f22022-09-04 11:42:22 +01001005 // first time, allocate the list
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001006 if (rettv_list_alloc(defer_tv) == FAIL)
Bram Moolenaar806a2732022-09-04 15:40:36 +01001007 return NULL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001008 }
1009 defer_l = defer_tv->vval.v_list;
1010
1011 l = list_alloc_with_items(argcount + 1);
1012 if (l == NULL)
Bram Moolenaar806a2732022-09-04 15:40:36 +01001013 return NULL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001014 listval.v_type = VAR_LIST;
1015 listval.vval.v_list = l;
1016 listval.v_lock = 0;
Bram Moolenaara5348f22022-09-04 11:42:22 +01001017 if (list_insert_tv(defer_l, &listval, defer_l->lv_first) == FAIL)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001018 {
1019 vim_free(l);
Bram Moolenaar806a2732022-09-04 15:40:36 +01001020 return NULL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001021 }
1022
Bram Moolenaar806a2732022-09-04 15:40:36 +01001023 return l;
1024}
1025
1026/*
1027 * Handle ISN_DEFER. Stack has a function reference and "argcount" arguments.
1028 * The local variable that lists deferred functions is "var_idx".
1029 * Returns OK or FAIL.
1030 */
1031 static int
Yegappan Lakshmananf3eac692023-10-17 11:00:45 +02001032defer_command(int var_idx, int argcount, ectx_T *ectx)
Bram Moolenaar806a2732022-09-04 15:40:36 +01001033{
Yegappan Lakshmananf3eac692023-10-17 11:00:45 +02001034 list_T *l = add_defer_item(var_idx, argcount, ectx);
Bram Moolenaar806a2732022-09-04 15:40:36 +01001035 int i;
1036 typval_T *func_tv;
1037
1038 if (l == NULL)
1039 return FAIL;
1040
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001041 func_tv = STACK_TV_BOT(-argcount - 1);
Yegappan Lakshmananf3eac692023-10-17 11:00:45 +02001042 if (func_tv->v_type != VAR_PARTIAL && func_tv->v_type != VAR_FUNC)
Bram Moolenaarf5fec052022-09-11 11:49:22 +01001043 {
1044 semsg(_(e_expected_str_but_got_str),
Yegappan Lakshmananf3eac692023-10-17 11:00:45 +02001045 "function or partial",
Bram Moolenaarf5fec052022-09-11 11:49:22 +01001046 vartype_name(func_tv->v_type));
1047 return FAIL;
1048 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001049 list_set_item(l, 0, func_tv);
1050
Bram Moolenaarf5fec052022-09-11 11:49:22 +01001051 for (i = 0; i < argcount; ++i)
Yegappan Lakshmananf3eac692023-10-17 11:00:45 +02001052 list_set_item(l, i + 1, STACK_TV_BOT(-argcount + i));
1053 ectx->ec_stack.ga_len -= argcount + 1;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001054 return OK;
1055}
1056
1057/*
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001058 * Add a deferred call for "name" with arguments "argvars[argcount]".
Bram Moolenaar806a2732022-09-04 15:40:36 +01001059 * Consumes "name", also on failure.
1060 * Only to be called when in_def_function() returns TRUE.
1061 */
1062 int
1063add_defer_function(char_u *name, int argcount, typval_T *argvars)
1064{
1065 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1066 + current_ectx->ec_dfunc_idx;
1067 list_T *l;
1068 typval_T func_tv;
1069 int i;
1070
1071 if (dfunc->df_defer_var_idx == 0)
1072 {
1073 iemsg("df_defer_var_idx is zero");
Bram Moolenaar31ea6bf2022-09-05 10:47:13 +01001074 vim_free(name);
Bram Moolenaar806a2732022-09-04 15:40:36 +01001075 return FAIL;
1076 }
Bram Moolenaar806a2732022-09-04 15:40:36 +01001077
Bram Moolenaarf5fec052022-09-11 11:49:22 +01001078 l = add_defer_item(dfunc->df_defer_var_idx - 1, argcount, current_ectx);
Bram Moolenaar806a2732022-09-04 15:40:36 +01001079 if (l == NULL)
1080 {
Bram Moolenaar31ea6bf2022-09-05 10:47:13 +01001081 vim_free(name);
Bram Moolenaar806a2732022-09-04 15:40:36 +01001082 return FAIL;
1083 }
1084
Bram Moolenaar31ea6bf2022-09-05 10:47:13 +01001085 func_tv.v_type = VAR_FUNC;
1086 func_tv.v_lock = 0;
1087 func_tv.vval.v_string = name;
Bram Moolenaar806a2732022-09-04 15:40:36 +01001088 list_set_item(l, 0, &func_tv);
Bram Moolenaar31ea6bf2022-09-05 10:47:13 +01001089
Bram Moolenaar806a2732022-09-04 15:40:36 +01001090 for (i = 0; i < argcount; ++i)
1091 list_set_item(l, i + 1, argvars + i);
1092 return OK;
1093}
1094
1095/*
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001096 * Invoked when returning from a function: Invoke any deferred calls.
1097 */
1098 static void
1099invoke_defer_funcs(ectx_T *ectx)
1100{
1101 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1102 + ectx->ec_dfunc_idx;
1103 typval_T *defer_tv = STACK_TV_VAR(dfunc->df_defer_var_idx - 1);
1104 listitem_T *li;
1105
1106 if (defer_tv->v_type != VAR_LIST)
1107 return; // no function added
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00001108 FOR_ALL_LIST_ITEMS(defer_tv->vval.v_list, li)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001109 {
1110 list_T *l = li->li_tv.vval.v_list;
1111 typval_T rettv;
1112 typval_T argvars[MAX_FUNC_ARGS];
1113 int i;
1114 listitem_T *arg_li = l->lv_first;
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001115 typval_T *functv = &l->lv_first->li_tv;
Yegappan Lakshmananf3eac692023-10-17 11:00:45 +02001116 int argcount = l->lv_len - 1;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001117
zeertzjqa1f2b5d2023-04-18 21:04:53 +01001118 if (functv->vval.v_string == NULL)
1119 // already being called, can happen if function does ":qa"
1120 continue;
1121
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001122 for (i = 0; i < argcount; ++i)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001123 {
1124 arg_li = arg_li->li_next;
1125 argvars[i] = arg_li->li_tv;
1126 }
1127
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001128 funcexe_T funcexe;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001129 CLEAR_FIELD(funcexe);
1130 funcexe.fe_evaluate = TRUE;
1131 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001132 if (functv->v_type == VAR_PARTIAL)
1133 {
1134 funcexe.fe_partial = functv->vval.v_partial;
Yegappan Lakshmananf3eac692023-10-17 11:00:45 +02001135 funcexe.fe_object = functv->vval.v_partial->pt_obj;
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001136 if (funcexe.fe_object != NULL)
1137 ++funcexe.fe_object->obj_refcount;
1138 }
zeertzjqa1f2b5d2023-04-18 21:04:53 +01001139
1140 char_u *name = functv->vval.v_string;
1141 functv->vval.v_string = NULL;
1142
Yegappan Lakshmanan06725952023-10-18 11:47:37 +02001143 // If the deferred function is called after an exception, then only the
Yegappan Lakshmananc59c1e02023-10-19 10:52:34 +02001144 // first statement in the function will be executed (because of the
1145 // exception). So save and restore the try/catch/throw exception
1146 // state.
1147 exception_state_T estate;
1148 exception_state_save(&estate);
1149 exception_state_clear();
Yegappan Lakshmanan06725952023-10-18 11:47:37 +02001150
zeertzjqa1f2b5d2023-04-18 21:04:53 +01001151 (void)call_func(name, -1, &rettv, argcount, argvars, &funcexe);
1152
Yegappan Lakshmananc59c1e02023-10-19 10:52:34 +02001153 exception_state_restore(&estate);
Yegappan Lakshmanan06725952023-10-18 11:47:37 +02001154
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001155 clear_tv(&rettv);
zeertzjqa1f2b5d2023-04-18 21:04:53 +01001156 vim_free(name);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001157 }
1158}
1159
1160/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001161 * Return from the current function.
1162 */
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001163 static int
Bram Moolenaar4c137212021-04-19 16:48:48 +02001164func_return(ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001165{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001166 int idx;
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01001167 int ret_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001168 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1169 + ectx->ec_dfunc_idx;
1170 int argcount = ufunc_argcount(dfunc->df_ufunc);
Bram Moolenaarc620c052020-07-08 15:16:19 +02001171 estack_T *entry;
Bram Moolenaar12d26532021-02-19 19:13:21 +01001172 int prev_dfunc_idx = STACK_TV(ectx->ec_frame_idx
1173 + STACK_FRAME_FUNC_OFF)->vval.v_number;
Bram Moolenaarb06b50d2021-04-26 21:39:25 +02001174 funclocal_T *floc;
1175#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01001176 dfunc_T *prev_dfunc = ((dfunc_T *)def_functions.ga_data)
1177 + prev_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001178
Bram Moolenaar12d26532021-02-19 19:13:21 +01001179 if (do_profiling == PROF_YES)
1180 {
1181 ufunc_T *caller = prev_dfunc->df_ufunc;
1182
1183 if (dfunc->df_ufunc->uf_profiling
1184 || (caller != NULL && caller->uf_profiling))
1185 {
1186 profile_may_end_func(((profinfo_T *)profile_info_ga.ga_data)
1187 + profile_info_ga.ga_len - 1, dfunc->df_ufunc, caller);
1188 --profile_info_ga.ga_len;
1189 }
1190 }
1191#endif
Bram Moolenaar919c12c2021-12-14 14:29:16 +00001192
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001193 if (dfunc->df_defer_var_idx > 0)
1194 invoke_defer_funcs(ectx);
1195
Bram Moolenaar919c12c2021-12-14 14:29:16 +00001196 // No check for uf_refcount being zero, cannot think of a way that would
1197 // happen.
Bram Moolenaarc970e422021-03-17 15:03:04 +01001198 --dfunc->df_ufunc->uf_calls;
1199
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001200 // execution context goes one level up
Bram Moolenaarc620c052020-07-08 15:16:19 +02001201 entry = estack_pop();
1202 if (entry != NULL)
Bram Moolenaarc70fe462021-04-17 17:59:19 +02001203 current_sctx = entry->es_save_sctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001204
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001205 if (handle_closure_in_use(ectx, TRUE) == FAIL)
1206 return FAIL;
1207
Bram Moolenaar574950d2023-01-03 19:08:50 +00001208 // Clear the arguments. If this was an object method also clear the
1209 // object, it is just before the arguments.
1210 int top = ectx->ec_frame_idx - argcount;
Yegappan Lakshmanan1db15142023-09-19 20:34:05 +02001211 if (IS_OBJECT_METHOD(dfunc->df_ufunc))
Bram Moolenaar574950d2023-01-03 19:08:50 +00001212 --top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001213 for (idx = top; idx < ectx->ec_frame_idx; ++idx)
1214 clear_tv(STACK_TV(idx));
1215
1216 // Clear local variables and temp values, but not the return value.
1217 for (idx = ectx->ec_frame_idx + STACK_FRAME_SIZE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001218 idx < ectx->ec_stack.ga_len - 1; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001219 clear_tv(STACK_TV(idx));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001220
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01001221 // The return value should be on top of the stack. However, when aborting
1222 // it may not be there and ec_frame_idx is the top of the stack.
1223 ret_idx = ectx->ec_stack.ga_len - 1;
Bram Moolenaar0186e582021-01-10 18:33:11 +01001224 if (ret_idx == ectx->ec_frame_idx + STACK_FRAME_IDX_OFF)
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01001225 ret_idx = 0;
1226
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001227 if (ectx->ec_outer_ref != NULL)
1228 {
1229 if (ectx->ec_outer_ref->or_outer_allocated)
1230 vim_free(ectx->ec_outer_ref->or_outer);
1231 partial_unref(ectx->ec_outer_ref->or_partial);
1232 vim_free(ectx->ec_outer_ref);
1233 }
Bram Moolenaar0186e582021-01-10 18:33:11 +01001234
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001235 // Restore the previous frame.
Bram Moolenaar12d26532021-02-19 19:13:21 +01001236 ectx->ec_dfunc_idx = prev_dfunc_idx;
Bram Moolenaar0186e582021-01-10 18:33:11 +01001237 ectx->ec_iidx = STACK_TV(ectx->ec_frame_idx
1238 + STACK_FRAME_IIDX_OFF)->vval.v_number;
Bram Moolenaar5930ddc2021-04-26 20:32:59 +02001239 ectx->ec_instr = (void *)STACK_TV(ectx->ec_frame_idx
1240 + STACK_FRAME_INSTR_OFF)->vval.v_string;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001241 ectx->ec_outer_ref = (void *)STACK_TV(ectx->ec_frame_idx
Bram Moolenaar0186e582021-01-10 18:33:11 +01001242 + STACK_FRAME_OUTER_OFF)->vval.v_string;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001243 floc = (void *)STACK_TV(ectx->ec_frame_idx
1244 + STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string;
Bram Moolenaar5366e1a2020-10-01 13:01:34 +02001245 // restoring ec_frame_idx must be last
Bram Moolenaar0186e582021-01-10 18:33:11 +01001246 ectx->ec_frame_idx = STACK_TV(ectx->ec_frame_idx
1247 + STACK_FRAME_IDX_OFF)->vval.v_number;
Bram Moolenaard386e922021-04-25 14:48:49 +02001248
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001249 if (floc == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02001250 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001251 else
1252 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02001253 ectx->ec_funclocal = *floc;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001254 vim_free(floc);
1255 }
1256
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01001257 if (ret_idx > 0)
1258 {
1259 // Reset the stack to the position before the call, with a spot for the
1260 // return value, moved there from above the frame.
1261 ectx->ec_stack.ga_len = top + 1;
1262 *STACK_TV_BOT(-1) = *STACK_TV(ret_idx);
1263 }
1264 else
1265 // Reset the stack to the position before the call.
1266 ectx->ec_stack.ga_len = top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001267
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001268 funcdepth_decrement();
Bram Moolenaare99d4222021-06-13 14:01:26 +02001269 --ex_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001270 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001271}
1272
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001273/*
1274 * Prepare arguments and rettv for calling a builtin or user function.
1275 */
1276 static int
1277call_prepare(int argcount, typval_T *argvars, ectx_T *ectx)
1278{
1279 int idx;
1280 typval_T *tv;
1281
1282 // Move arguments from bottom of the stack to argvars[] and add terminator.
1283 for (idx = 0; idx < argcount; ++idx)
1284 argvars[idx] = *STACK_TV_BOT(idx - argcount);
1285 argvars[argcount].v_type = VAR_UNKNOWN;
1286
1287 // Result replaces the arguments on the stack.
1288 if (argcount > 0)
1289 ectx->ec_stack.ga_len -= argcount - 1;
Bram Moolenaar35578162021-08-02 19:10:38 +02001290 else if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001291 return FAIL;
1292 else
1293 ++ectx->ec_stack.ga_len;
1294
1295 // Default return value is zero.
1296 tv = STACK_TV_BOT(-1);
1297 tv->v_type = VAR_NUMBER;
1298 tv->vval.v_number = 0;
Bram Moolenaar24565cf2022-03-28 18:16:52 +01001299 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001300
1301 return OK;
1302}
1303
1304/*
1305 * Call a builtin function by index.
1306 */
1307 static int
1308call_bfunc(int func_idx, int argcount, ectx_T *ectx)
1309{
1310 typval_T argvars[MAX_FUNC_ARGS];
1311 int idx;
Bram Moolenaar171fb922020-10-28 16:54:47 +01001312 int did_emsg_before = did_emsg;
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001313 ectx_T *prev_ectx = current_ectx;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001314 char *save_func_name = ectx->ec_where.wt_func_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001315
1316 if (call_prepare(argcount, argvars, ectx) == FAIL)
1317 return FAIL;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001318 ectx->ec_where.wt_func_name = internal_func_name(func_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001319
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001320 // Call the builtin function. Set "current_ectx" so that when it
1321 // recursively invokes call_def_function() a closure context can be set.
1322 current_ectx = ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001323 call_internal_func_by_idx(func_idx, argvars, STACK_TV_BOT(-1));
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001324 current_ectx = prev_ectx;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001325 ectx->ec_where.wt_func_name = save_func_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001326
1327 // Clear the arguments.
1328 for (idx = 0; idx < argcount; ++idx)
1329 clear_tv(&argvars[idx]);
Bram Moolenaar015f4262020-05-05 21:25:22 +02001330
Bram Moolenaar57f799e2020-12-12 20:42:19 +01001331 if (did_emsg > did_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +02001332 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001333 return OK;
1334}
1335
1336/*
1337 * Execute a user defined function.
Bram Moolenaarab360522021-01-10 14:02:28 +01001338 * If the function is compiled this will add a stack frame and set the
1339 * instruction pointer at the start of the function.
1340 * Otherwise the function is called here.
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001341 * If "pt" is not null use "pt->pt_outer" for ec_outer_ref->or_outer.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001342 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001343 */
1344 static int
Bram Moolenaar0186e582021-01-10 18:33:11 +01001345call_ufunc(
1346 ufunc_T *ufunc,
1347 partial_T *pt,
1348 int argcount,
1349 ectx_T *ectx,
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001350 isn_T *iptr,
1351 dict_T *selfdict)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001352{
1353 typval_T argvars[MAX_FUNC_ARGS];
1354 funcexe_T funcexe;
Bram Moolenaar0917e862023-02-18 14:42:44 +00001355 funcerror_T error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001356 int idx;
Bram Moolenaar28ee8922020-10-28 20:20:00 +01001357 int did_emsg_before = did_emsg;
Bram Moolenaar139575d2022-03-15 19:29:30 +00001358 compiletype_T compile_type = get_compile_type(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001359
Bram Moolenaare99d4222021-06-13 14:01:26 +02001360 if (func_needs_compiling(ufunc, compile_type)
1361 && compile_def_function(ufunc, FALSE, compile_type, NULL)
1362 == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02001363 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001364 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001365 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +01001366 error = check_user_func_argcount(ufunc, argcount);
Bram Moolenaar50824712020-12-20 21:10:17 +01001367 if (error != FCERR_UNKNOWN)
1368 {
1369 if (error == FCERR_TOOMANY)
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001370 semsg(_(e_too_many_arguments_for_function_str),
1371 printable_func_name(ufunc));
Bram Moolenaar50824712020-12-20 21:10:17 +01001372 else
Bram Moolenaare1242042021-12-16 20:56:57 +00001373 semsg(_(e_not_enough_arguments_for_function_str),
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001374 printable_func_name(ufunc));
Bram Moolenaar50824712020-12-20 21:10:17 +01001375 return FAIL;
1376 }
1377
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001378 // The function has been compiled, can call it quickly. For a function
1379 // that was defined later: we can call it directly next time.
1380 if (iptr != NULL)
1381 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001382 delete_instr(iptr);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001383 iptr->isn_type = ISN_DCALL;
1384 iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1385 iptr->isn_arg.dfunc.cdf_argcount = argcount;
1386 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001387 return call_dfunc(ufunc->uf_dfunc_idx, pt, argcount, ectx);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001388 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001389
1390 if (call_prepare(argcount, argvars, ectx) == FAIL)
1391 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +02001392 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00001393 funcexe.fe_evaluate = TRUE;
1394 funcexe.fe_selfdict = selfdict != NULL ? selfdict : dict_stack_get_dict();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001395
1396 // Call the user function. Result goes in last position on the stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001397 error = call_user_func_check(ufunc, argcount, argvars,
Bram Moolenaar851f86b2021-12-13 14:26:44 +00001398 STACK_TV_BOT(-1), &funcexe, funcexe.fe_selfdict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001399
1400 // Clear the arguments.
1401 for (idx = 0; idx < argcount; ++idx)
1402 clear_tv(&argvars[idx]);
1403
1404 if (error != FCERR_NONE)
1405 {
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01001406 user_func_error(error, printable_func_name(ufunc),
1407 funcexe.fe_found_var);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001408 return FAIL;
1409 }
Bram Moolenaar28ee8922020-10-28 20:20:00 +01001410 if (did_emsg > did_emsg_before)
Bram Moolenaared677f52020-08-12 16:38:10 +02001411 // Error other than from calling the function itself.
1412 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001413 return OK;
1414}
1415
1416/*
Bram Moolenaara91a7132021-03-25 21:12:15 +01001417 * If command modifiers were applied restore them.
1418 */
1419 static void
1420may_restore_cmdmod(funclocal_T *funclocal)
1421{
1422 if (funclocal->floc_restore_cmdmod)
1423 {
1424 cmdmod.cmod_filter_regmatch.regprog = NULL;
1425 undo_cmdmod(&cmdmod);
1426 cmdmod = funclocal->floc_save_cmdmod;
1427 funclocal->floc_restore_cmdmod = FALSE;
1428 }
1429}
1430
1431/*
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001432 * Return TRUE if an error was given (not caught in try/catch) or CTRL-C was
1433 * pressed.
Bram Moolenaara1773442020-08-12 15:21:22 +02001434 */
1435 static int
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001436vim9_aborting(int prev_uncaught_emsg)
Bram Moolenaara1773442020-08-12 15:21:22 +02001437{
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001438 return uncaught_emsg > prev_uncaught_emsg || got_int || did_throw;
Bram Moolenaara1773442020-08-12 15:21:22 +02001439}
1440
1441/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001442 * Execute a function by "name".
1443 * This can be a builtin function or a user function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001444 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001445 * Returns FAIL if not found without an error message.
1446 */
1447 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001448call_by_name(
1449 char_u *name,
1450 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001451 ectx_T *ectx,
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001452 isn_T *iptr,
1453 dict_T *selfdict)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001454{
1455 ufunc_T *ufunc;
1456
1457 if (builtin_function(name, -1))
1458 {
1459 int func_idx = find_internal_func(name);
1460
Bram Moolenaar1983f1a2022-02-28 20:55:02 +00001461 if (func_idx < 0) // Impossible?
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001462 return FAIL;
Bram Moolenaar389df252020-07-09 21:20:47 +02001463 if (check_internal_func(func_idx, argcount) < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001464 return FAIL;
1465 return call_bfunc(func_idx, argcount, ectx);
1466 }
1467
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00001468 ufunc = find_func(name, FALSE);
Bram Moolenaara1773442020-08-12 15:21:22 +02001469
1470 if (ufunc == NULL)
1471 {
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001472 int prev_uncaught_emsg = uncaught_emsg;
Bram Moolenaara1773442020-08-12 15:21:22 +02001473
1474 if (script_autoload(name, TRUE))
1475 // loaded a package, search for the function again
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00001476 ufunc = find_func(name, FALSE);
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001477
1478 if (vim9_aborting(prev_uncaught_emsg))
Bram Moolenaara1773442020-08-12 15:21:22 +02001479 return FAIL; // bail out if loading the script caused an error
1480 }
1481
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001482 if (ufunc != NULL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001483 {
Bram Moolenaar0d89d8a2022-12-31 14:01:24 +00001484 if (check_ufunc_arg_types(ufunc, argcount, 0, ectx) == FAIL)
1485 return FAIL;
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001486
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001487 return call_ufunc(ufunc, NULL, argcount, ectx, iptr, selfdict);
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001488 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001489
1490 return FAIL;
1491}
1492
1493 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001494call_partial(
1495 typval_T *tv,
1496 int argcount_arg,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001497 ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001498{
Bram Moolenaara90afb92020-07-15 22:38:56 +02001499 int argcount = argcount_arg;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001500 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001501 int called_emsg_before = called_emsg;
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +01001502 int res = FAIL;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001503 dict_T *selfdict = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001504
1505 if (tv->v_type == VAR_PARTIAL)
1506 {
Bram Moolenaara90afb92020-07-15 22:38:56 +02001507 partial_T *pt = tv->vval.v_partial;
1508 int i;
1509
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +02001510 if (pt->pt_obj != NULL)
1511 {
1512 // partial with an object method. Push the object before the
1513 // function arguments.
1514 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
1515 return FAIL;
1516 for (i = 1; i <= argcount; ++i)
1517 *STACK_TV_BOT(-i + 1) = *STACK_TV_BOT(-i);
1518
1519 typval_T *obj_tv = STACK_TV_BOT(-argcount);
1520 obj_tv->v_type = VAR_OBJECT;
1521 obj_tv->v_lock = 0;
1522 obj_tv->vval.v_object = pt->pt_obj;
1523 ++pt->pt_obj->obj_refcount;
1524 ++ectx->ec_stack.ga_len;
1525 }
1526
Bram Moolenaara90afb92020-07-15 22:38:56 +02001527 if (pt->pt_argc > 0)
1528 {
1529 // Make space for arguments from the partial, shift the "argcount"
1530 // arguments up.
Bram Moolenaar35578162021-08-02 19:10:38 +02001531 if (GA_GROW_FAILS(&ectx->ec_stack, pt->pt_argc))
Bram Moolenaara90afb92020-07-15 22:38:56 +02001532 return FAIL;
1533 for (i = 1; i <= argcount; ++i)
1534 *STACK_TV_BOT(-i + pt->pt_argc) = *STACK_TV_BOT(-i);
1535 ectx->ec_stack.ga_len += pt->pt_argc;
1536 argcount += pt->pt_argc;
1537
1538 // copy the arguments from the partial onto the stack
1539 for (i = 0; i < pt->pt_argc; ++i)
1540 copy_tv(&pt->pt_argv[i], STACK_TV_BOT(-argcount + i));
1541 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001542 selfdict = pt->pt_dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001543
1544 if (pt->pt_func != NULL)
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001545 return call_ufunc(pt->pt_func, pt, argcount, ectx, NULL, selfdict);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001546
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001547 name = pt->pt_name;
1548 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001549 else if (tv->v_type == VAR_FUNC)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001550 name = tv->vval.v_string;
Bram Moolenaar95006e32020-08-29 17:47:08 +02001551 if (name != NULL)
1552 {
Bram Moolenaar0917e862023-02-18 14:42:44 +00001553 char_u fname_buf[FLEN_FIXED + 1];
1554 char_u *tofree = NULL;
1555 funcerror_T error = FCERR_NONE;
1556 char_u *fname;
Bram Moolenaar95006e32020-08-29 17:47:08 +02001557
1558 // May need to translate <SNR>123_ to K_SNR.
1559 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
1560 if (error != FCERR_NONE)
1561 res = FAIL;
1562 else
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001563 res = call_by_name(fname, argcount, ectx, NULL, selfdict);
Bram Moolenaar95006e32020-08-29 17:47:08 +02001564 vim_free(tofree);
1565 }
1566
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +01001567 if (res == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001568 {
1569 if (called_emsg == called_emsg_before)
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001570 emsg_funcname(e_unknown_function_str,
Bram Moolenaar015f4262020-05-05 21:25:22 +02001571 name == NULL ? (char_u *)"[unknown]" : name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001572 return FAIL;
1573 }
1574 return OK;
1575}
1576
1577/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001578 * Check if "lock" is VAR_LOCKED or VAR_FIXED. If so give an error and return
1579 * TRUE.
1580 */
1581 static int
1582error_if_locked(int lock, char *error)
1583{
1584 if (lock & (VAR_LOCKED | VAR_FIXED))
1585 {
1586 emsg(_(error));
1587 return TRUE;
1588 }
1589 return FALSE;
1590}
1591
1592/*
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001593 * Give an error if "tv" is not a number and return FAIL.
1594 */
1595 static int
1596check_for_number(typval_T *tv)
1597{
1598 if (tv->v_type != VAR_NUMBER)
1599 {
1600 semsg(_(e_expected_str_but_got_str),
1601 vartype_name(VAR_NUMBER), vartype_name(tv->v_type));
1602 return FAIL;
1603 }
1604 return OK;
1605}
1606
1607/*
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001608 * Store "tv" in variable "name".
1609 * This is for s: and g: variables.
1610 */
1611 static void
1612store_var(char_u *name, typval_T *tv)
1613{
1614 funccal_entry_T entry;
Bram Moolenaard877a572021-04-01 19:42:48 +02001615 int flags = ASSIGN_DECL;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001616
Bram Moolenaard877a572021-04-01 19:42:48 +02001617 if (tv->v_lock)
1618 flags |= ASSIGN_CONST;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001619 save_funccal(&entry);
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001620 set_var_const(name, 0, NULL, tv, FALSE, flags, 0);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001621 restore_funccal();
1622}
1623
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001624/*
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001625 * Convert "tv" to a string.
1626 * Return FAIL if not allowed.
1627 */
1628 static int
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001629do_2string(typval_T *tv, int is_2string_any, int tolerant)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001630{
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00001631 if (tv->v_type == VAR_STRING)
1632 return OK;
1633
1634 char_u *str;
1635
1636 if (is_2string_any)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001637 {
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00001638 switch (tv->v_type)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001639 {
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00001640 case VAR_SPECIAL:
1641 case VAR_BOOL:
1642 case VAR_NUMBER:
1643 case VAR_FLOAT:
1644 case VAR_BLOB: break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001645
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00001646 case VAR_LIST:
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001647 if (tolerant)
1648 {
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001649 char_u *s, *e, *p;
1650 garray_T ga;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001651
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001652 ga_init2(&ga, sizeof(char_u *), 1);
1653
1654 // Convert to NL separated items, then
1655 // escape the items and replace the NL with
1656 // a space.
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001657 str = typval2string(tv, TRUE);
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001658 if (str == NULL)
1659 return FAIL;
1660 s = str;
1661 while ((e = vim_strchr(s, '\n')) != NULL)
1662 {
1663 *e = NUL;
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +01001664 p = vim_strsave_fnameescape(s,
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00001665 VSE_NONE);
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001666 if (p != NULL)
1667 {
1668 ga_concat(&ga, p);
1669 ga_concat(&ga, (char_u *)" ");
1670 vim_free(p);
1671 }
1672 s = e + 1;
1673 }
1674 vim_free(str);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001675 clear_tv(tv);
1676 tv->v_type = VAR_STRING;
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001677 tv->vval.v_string = ga.ga_data;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001678 return OK;
1679 }
1680 // FALLTHROUGH
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00001681 default: to_string_error(tv->v_type);
1682 return FAIL;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001683 }
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001684 }
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00001685 str = typval_tostring(tv, TRUE);
1686 clear_tv(tv);
1687 tv->v_type = VAR_STRING;
1688 tv->vval.v_string = str;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001689 return OK;
1690}
1691
1692/*
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001693 * When the value of "sv" is a null list of dict, allocate it.
1694 */
1695 static void
Bram Moolenaar859cc212022-03-28 15:22:35 +01001696allocate_if_null(svar_T *sv)
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001697{
Bram Moolenaar859cc212022-03-28 15:22:35 +01001698 typval_T *tv = sv->sv_tv;
1699
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001700 switch (tv->v_type)
1701 {
1702 case VAR_LIST:
Bram Moolenaar859cc212022-03-28 15:22:35 +01001703 if (tv->vval.v_list == NULL && sv->sv_type != &t_list_empty)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001704 (void)rettv_list_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001705 break;
1706 case VAR_DICT:
Bram Moolenaar859cc212022-03-28 15:22:35 +01001707 if (tv->vval.v_dict == NULL && sv->sv_type != &t_dict_empty)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001708 (void)rettv_dict_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001709 break;
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02001710 case VAR_BLOB:
Bram Moolenaar859cc212022-03-28 15:22:35 +01001711 if (tv->vval.v_blob == NULL && sv->sv_type != &t_blob_null)
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02001712 (void)rettv_blob_alloc(tv);
1713 break;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001714 default:
1715 break;
1716 }
1717}
Bram Moolenaard3aac292020-04-19 14:32:17 +02001718
Bram Moolenaare7525c52021-01-09 13:20:37 +01001719/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001720 * Return the character "str[index]" where "index" is the character index,
1721 * including composing characters.
1722 * If "index" is out of range NULL is returned.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001723 */
1724 char_u *
1725char_from_string(char_u *str, varnumber_T index)
1726{
1727 size_t nbyte = 0;
1728 varnumber_T nchar = index;
1729 size_t slen;
1730
1731 if (str == NULL)
1732 return NULL;
1733 slen = STRLEN(str);
1734
Bram Moolenaarff871402021-03-26 13:34:05 +01001735 // Do the same as for a list: a negative index counts from the end.
1736 // Optimization to check the first byte to be below 0x80 (and no composing
1737 // character follows) makes this a lot faster.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001738 if (index < 0)
1739 {
1740 int clen = 0;
1741
1742 for (nbyte = 0; nbyte < slen; ++clen)
Bram Moolenaarff871402021-03-26 13:34:05 +01001743 {
1744 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1745 ++nbyte;
1746 else if (enc_utf8)
1747 nbyte += utfc_ptr2len(str + nbyte);
1748 else
1749 nbyte += mb_ptr2len(str + nbyte);
1750 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001751 nchar = clen + index;
1752 if (nchar < 0)
1753 // unlike list: index out of range results in empty string
1754 return NULL;
1755 }
1756
1757 for (nbyte = 0; nchar > 0 && nbyte < slen; --nchar)
Bram Moolenaarff871402021-03-26 13:34:05 +01001758 {
1759 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1760 ++nbyte;
1761 else if (enc_utf8)
1762 nbyte += utfc_ptr2len(str + nbyte);
1763 else
1764 nbyte += mb_ptr2len(str + nbyte);
1765 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001766 if (nbyte >= slen)
1767 return NULL;
Bram Moolenaar0289a092021-03-14 18:40:19 +01001768 return vim_strnsave(str + nbyte, mb_ptr2len(str + nbyte));
Bram Moolenaare7525c52021-01-09 13:20:37 +01001769}
1770
1771/*
1772 * Get the byte index for character index "idx" in string "str" with length
Bram Moolenaar0289a092021-03-14 18:40:19 +01001773 * "str_len". Composing characters are included.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001774 * If going over the end return "str_len".
1775 * If "idx" is negative count from the end, -1 is the last character.
1776 * When going over the start return -1.
1777 */
1778 static long
1779char_idx2byte(char_u *str, size_t str_len, varnumber_T idx)
1780{
1781 varnumber_T nchar = idx;
1782 size_t nbyte = 0;
1783
1784 if (nchar >= 0)
1785 {
1786 while (nchar > 0 && nbyte < str_len)
1787 {
Bram Moolenaar0289a092021-03-14 18:40:19 +01001788 nbyte += mb_ptr2len(str + nbyte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001789 --nchar;
1790 }
1791 }
1792 else
1793 {
1794 nbyte = str_len;
1795 while (nchar < 0 && nbyte > 0)
1796 {
1797 --nbyte;
1798 nbyte -= mb_head_off(str, str + nbyte);
1799 ++nchar;
1800 }
1801 if (nchar < 0)
1802 return -1;
1803 }
1804 return (long)nbyte;
1805}
1806
1807/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001808 * Return the slice "str[first : last]" using character indexes. Composing
1809 * characters are included.
Bram Moolenaar6601b622021-01-13 21:47:15 +01001810 * "exclusive" is TRUE for slice().
Bram Moolenaare7525c52021-01-09 13:20:37 +01001811 * Return NULL when the result is empty.
1812 */
1813 char_u *
Bram Moolenaar6601b622021-01-13 21:47:15 +01001814string_slice(char_u *str, varnumber_T first, varnumber_T last, int exclusive)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001815{
1816 long start_byte, end_byte;
1817 size_t slen;
1818
1819 if (str == NULL)
1820 return NULL;
1821 slen = STRLEN(str);
1822 start_byte = char_idx2byte(str, slen, first);
1823 if (start_byte < 0)
1824 start_byte = 0; // first index very negative: use zero
Bram Moolenaar6601b622021-01-13 21:47:15 +01001825 if ((last == -1 && !exclusive) || last == VARNUM_MAX)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001826 end_byte = (long)slen;
1827 else
1828 {
1829 end_byte = char_idx2byte(str, slen, last);
Bram Moolenaar6601b622021-01-13 21:47:15 +01001830 if (!exclusive && end_byte >= 0 && end_byte < (long)slen)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001831 // end index is inclusive
Bram Moolenaar0289a092021-03-14 18:40:19 +01001832 end_byte += mb_ptr2len(str + end_byte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001833 }
1834
1835 if (start_byte >= (long)slen || end_byte <= start_byte)
1836 return NULL;
1837 return vim_strnsave(str + start_byte, end_byte - start_byte);
1838}
1839
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001840/*
1841 * Get a script variable for ISN_STORESCRIPT and ISN_LOADSCRIPT.
1842 * When "dfunc_idx" is negative don't give an error.
1843 * Returns NULL for an error.
1844 */
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001845 static svar_T *
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001846get_script_svar(scriptref_T *sref, int dfunc_idx)
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001847{
1848 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001849 dfunc_T *dfunc = dfunc_idx < 0 ? NULL
1850 : ((dfunc_T *)def_functions.ga_data) + dfunc_idx;
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001851 svar_T *sv;
1852
1853 if (sref->sref_seq != si->sn_script_seq)
1854 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001855 // The script was reloaded after the function was compiled, the
1856 // script_idx may not be valid.
1857 if (dfunc != NULL)
1858 semsg(_(e_script_variable_invalid_after_reload_in_function_str),
1859 printable_func_name(dfunc->df_ufunc));
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001860 return NULL;
1861 }
1862 sv = ((svar_T *)si->sn_var_vals.ga_data) + sref->sref_idx;
Bram Moolenaar6de22962022-09-09 21:35:36 +01001863 if (sv->sv_name == NULL)
1864 {
1865 if (dfunc != NULL)
1866 emsg(_(e_script_variable_was_deleted));
1867 return NULL;
1868 }
Bram Moolenaar60dc8272021-07-29 22:48:54 +02001869 if (!equal_type(sv->sv_type, sref->sref_type, 0))
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001870 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001871 if (dfunc != NULL)
1872 emsg(_(e_script_variable_type_changed));
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001873 return NULL;
1874 }
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001875
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01001876 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0
1877 && sref->sref_sid != current_sctx.sc_sid)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001878 {
1879 if (dfunc != NULL)
1880 semsg(_(e_item_not_exported_in_script_str), sv->sv_name);
1881 return NULL;
1882 }
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001883 return sv;
1884}
1885
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001886/*
Bram Moolenaar20677332021-06-06 17:02:53 +02001887 * Function passed to do_cmdline() for splitting a script joined by NL
1888 * characters.
1889 */
1890 static char_u *
1891get_split_sourceline(
1892 int c UNUSED,
1893 void *cookie,
1894 int indent UNUSED,
1895 getline_opt_T options UNUSED)
1896{
1897 source_cookie_T *sp = (source_cookie_T *)cookie;
1898 char_u *p;
1899 char_u *line;
1900
Bram Moolenaar20677332021-06-06 17:02:53 +02001901 p = vim_strchr(sp->nextline, '\n');
1902 if (p == NULL)
1903 {
1904 line = vim_strsave(sp->nextline);
1905 sp->nextline += STRLEN(sp->nextline);
1906 }
1907 else
1908 {
1909 line = vim_strnsave(sp->nextline, p - sp->nextline);
1910 sp->nextline = p + 1;
1911 }
1912 return line;
1913}
1914
1915/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001916 * Execute a function by "name".
1917 * This can be a builtin function, user function or a funcref.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001918 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001919 */
1920 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001921call_eval_func(
1922 char_u *name,
1923 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001924 ectx_T *ectx,
1925 isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001926{
Bram Moolenaared677f52020-08-12 16:38:10 +02001927 int called_emsg_before = called_emsg;
1928 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001929
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001930 res = call_by_name(name, argcount, ectx, iptr, NULL);
Bram Moolenaared677f52020-08-12 16:38:10 +02001931 if (res == FAIL && called_emsg == called_emsg_before)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001932 {
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001933 dictitem_T *v;
1934
1935 v = find_var(name, NULL, FALSE);
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001936 if (v == NULL || (v->di_tv.v_type != VAR_PARTIAL
1937 && v->di_tv.v_type != VAR_FUNC))
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001938 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001939 emsg_funcname(e_unknown_function_str, name);
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001940 return FAIL;
1941 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001942 return call_partial(&v->di_tv, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001943 }
Bram Moolenaared677f52020-08-12 16:38:10 +02001944 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001945}
1946
1947/*
Bram Moolenaarf112f302020-12-20 17:47:52 +01001948 * When a function reference is used, fill a partial with the information
1949 * needed, especially when it is used as a closure.
1950 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001951 int
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001952fill_partial_and_closure(
Bram Moolenaarcc341812022-09-19 15:54:34 +01001953 partial_T *pt,
1954 ufunc_T *ufunc,
1955 loopvarinfo_T *lvi,
1956 ectx_T *ectx)
Bram Moolenaarf112f302020-12-20 17:47:52 +01001957{
1958 pt->pt_func = ufunc;
1959 pt->pt_refcount = 1;
1960
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001961 if (ufunc->uf_flags & FC_CLOSURE)
Bram Moolenaarf112f302020-12-20 17:47:52 +01001962 {
1963 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1964 + ectx->ec_dfunc_idx;
1965
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001966 // The closure may need to find arguments and local variables of the
1967 // current function in the stack.
Bram Moolenaar0186e582021-01-10 18:33:11 +01001968 pt->pt_outer.out_stack = &ectx->ec_stack;
1969 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001970 if (ectx->ec_outer_ref != NULL)
1971 {
1972 // The current context already has a context, link to that one.
1973 pt->pt_outer.out_up = ectx->ec_outer_ref->or_outer;
1974 if (ectx->ec_outer_ref->or_partial != NULL)
1975 {
1976 pt->pt_outer.out_up_partial = ectx->ec_outer_ref->or_partial;
1977 ++pt->pt_outer.out_up_partial->pt_refcount;
1978 }
1979 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01001980
Bram Moolenaarcc341812022-09-19 15:54:34 +01001981 if (lvi != NULL)
1982 {
1983 int depth;
1984
1985 // The closure may need to find variables defined inside a loop,
1986 // for every nested loop. A new reference is made every time,
1987 // ISN_ENDLOOP will check if they are actually used.
1988 for (depth = 0; depth < lvi->lvi_depth; ++depth)
1989 {
1990 pt->pt_outer.out_loop[depth].stack = &ectx->ec_stack;
1991 pt->pt_outer.out_loop[depth].var_idx = ectx->ec_frame_idx
1992 + STACK_FRAME_SIZE + lvi->lvi_loop[depth].var_idx;
1993 pt->pt_outer.out_loop[depth].var_count =
1994 lvi->lvi_loop[depth].var_count;
1995 }
Bram Moolenaar6d313be2022-09-22 16:36:25 +01001996 pt->pt_outer.out_loop_size = lvi->lvi_depth;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001997 }
Bram Moolenaar6d313be2022-09-22 16:36:25 +01001998 else
1999 pt->pt_outer.out_loop_size = 0;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002000
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002001 // If the function currently executing returns and the closure is still
2002 // being referenced, we need to make a copy of the context (arguments
2003 // and local variables) so that the closure can use it later.
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02002004 // Store a reference to the partial so we can handle that.
Bram Moolenaar35578162021-08-02 19:10:38 +02002005 if (GA_GROW_FAILS(&ectx->ec_funcrefs, 1))
Bram Moolenaarf112f302020-12-20 17:47:52 +01002006 {
2007 vim_free(pt);
2008 return FAIL;
2009 }
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02002010 // Extra variable keeps the count of closures created in the current
2011 // function call.
Bram Moolenaarf112f302020-12-20 17:47:52 +01002012 ++(((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx
2013 + STACK_FRAME_SIZE + dfunc->df_varcount)->vval.v_number;
2014
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002015 ((partial_T **)ectx->ec_funcrefs.ga_data)[ectx->ec_funcrefs.ga_len]
2016 = pt;
Bram Moolenaarf112f302020-12-20 17:47:52 +01002017 ++pt->pt_refcount;
2018 ++ectx->ec_funcrefs.ga_len;
2019 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002020 ++ufunc->uf_refcount;
Bram Moolenaarf112f302020-12-20 17:47:52 +01002021 return OK;
2022}
2023
Bram Moolenaaraacc9662021-08-13 19:40:51 +02002024/*
2025 * Execute iptr->isn_arg.string as an Ex command.
2026 */
2027 static int
Ernie Raelee865f32023-09-29 19:53:55 +02002028exec_command(isn_T *iptr, char_u *cmd_string)
Bram Moolenaaraacc9662021-08-13 19:40:51 +02002029{
2030 source_cookie_T cookie;
2031
2032 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00002033 // Pass getsourceline to get an error for a missing ":end" command.
Bram Moolenaaraacc9662021-08-13 19:40:51 +02002034 CLEAR_FIELD(cookie);
2035 cookie.sourcing_lnum = iptr->isn_lnum - 1;
Ernie Raelee865f32023-09-29 19:53:55 +02002036 if (do_cmdline(cmd_string, getsourceline, &cookie,
Bram Moolenaaraacc9662021-08-13 19:40:51 +02002037 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED) == FAIL
2038 || did_emsg)
2039 return FAIL;
2040 return OK;
2041}
2042
Bram Moolenaar89445512022-04-14 12:58:23 +01002043/*
2044 * If script "sid" is not loaded yet then load it now.
2045 * Caller must make sure "sid" is a valid script ID.
2046 * "loaded" is set to TRUE if the script had to be loaded.
2047 * Returns FAIL if loading fails, OK if already loaded or loaded now.
2048 */
2049 int
2050may_load_script(int sid, int *loaded)
2051{
2052 scriptitem_T *si = SCRIPT_ITEM(sid);
2053
2054 if (si->sn_state == SN_STATE_NOT_LOADED)
2055 {
2056 *loaded = TRUE;
2057 if (do_source(si->sn_name, FALSE, DOSO_NONE, NULL) == FAIL)
2058 {
2059 semsg(_(e_cant_open_file_str), si->sn_name);
2060 return FAIL;
2061 }
2062 }
2063 return OK;
2064}
2065
Bram Moolenaarf18332f2021-05-07 17:55:55 +02002066// used for v_instr of typval of VAR_INSTR
2067struct instr_S {
2068 ectx_T *instr_ectx;
2069 isn_T *instr_instr;
2070};
2071
Bram Moolenaar4c137212021-04-19 16:48:48 +02002072// used for substitute_instr
2073typedef struct subs_expr_S {
2074 ectx_T *subs_ectx;
2075 isn_T *subs_instr;
2076 int subs_status;
2077} subs_expr_T;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002078
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002079// Set when calling do_debug().
2080static ectx_T *debug_context = NULL;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02002081static int debug_var_count;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002082
2083/*
2084 * When debugging lookup "name" and return the typeval.
2085 * When not found return NULL.
2086 */
2087 typval_T *
2088lookup_debug_var(char_u *name)
2089{
2090 int idx;
2091 dfunc_T *dfunc;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02002092 ufunc_T *ufunc;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002093 ectx_T *ectx = debug_context;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02002094 int varargs_off;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002095
2096 if (ectx == NULL)
2097 return NULL;
2098 dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
2099
2100 // Go through the local variable names, from last to first.
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02002101 for (idx = debug_var_count - 1; idx >= 0; --idx)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002102 {
Bram Moolenaare406ff82022-03-10 20:47:43 +00002103 char_u *varname = ((char_u **)dfunc->df_var_names.ga_data)[idx];
2104
2105 // the variable name may be NULL when not available in this block
2106 if (varname != NULL && STRCMP(varname, name) == 0)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002107 return STACK_TV_VAR(idx);
2108 }
2109
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02002110 // Go through argument names.
2111 ufunc = dfunc->df_ufunc;
2112 varargs_off = ufunc->uf_va_name == NULL ? 0 : 1;
2113 for (idx = 0; idx < ufunc->uf_args.ga_len; ++idx)
2114 if (STRCMP(((char_u **)(ufunc->uf_args.ga_data))[idx], name) == 0)
2115 return STACK_TV(ectx->ec_frame_idx - ufunc->uf_args.ga_len
2116 - varargs_off + idx);
2117 if (ufunc->uf_va_name != NULL && STRCMP(ufunc->uf_va_name, name) == 0)
2118 return STACK_TV(ectx->ec_frame_idx - 1);
2119
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002120 return NULL;
2121}
2122
Bram Moolenaar26a44842021-09-02 18:49:06 +02002123/*
2124 * Return TRUE if there might be a breakpoint in "ufunc", which is when a
2125 * breakpoint was set in that function or when there is any expression.
2126 */
2127 int
2128may_break_in_function(ufunc_T *ufunc)
2129{
2130 return ufunc->uf_has_breakpoint || debug_has_expr_breakpoint();
2131}
2132
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002133 static void
2134handle_debug(isn_T *iptr, ectx_T *ectx)
2135{
2136 char_u *line;
2137 ufunc_T *ufunc = (((dfunc_T *)def_functions.ga_data)
2138 + ectx->ec_dfunc_idx)->df_ufunc;
2139 isn_T *ni;
2140 int end_lnum = iptr->isn_lnum;
2141 garray_T ga;
2142 int lnum;
2143
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02002144 if (ex_nesting_level > debug_break_level)
2145 {
2146 linenr_T breakpoint;
2147
Bram Moolenaar26a44842021-09-02 18:49:06 +02002148 if (!may_break_in_function(ufunc))
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02002149 return;
2150
2151 // check for the next breakpoint if needed
2152 breakpoint = dbg_find_breakpoint(FALSE, ufunc->uf_name,
Bram Moolenaar8cec9272021-06-23 20:20:53 +02002153 iptr->isn_arg.debug.dbg_break_lnum);
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02002154 if (breakpoint <= 0 || breakpoint > iptr->isn_lnum)
2155 return;
2156 }
2157
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002158 SOURCING_LNUM = iptr->isn_lnum;
2159 debug_context = ectx;
Bram Moolenaar8cec9272021-06-23 20:20:53 +02002160 debug_var_count = iptr->isn_arg.debug.dbg_var_names_len;
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002161
2162 for (ni = iptr + 1; ni->isn_type != ISN_FINISH; ++ni)
2163 if (ni->isn_type == ISN_DEBUG
2164 || ni->isn_type == ISN_RETURN
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002165 || ni->isn_type == ISN_RETURN_OBJECT
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002166 || ni->isn_type == ISN_RETURN_VOID)
2167 {
Bram Moolenaar112bed02021-11-23 22:16:34 +00002168 end_lnum = ni->isn_lnum + (ni->isn_type == ISN_DEBUG ? 0 : 1);
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002169 break;
2170 }
2171
2172 if (end_lnum > iptr->isn_lnum)
2173 {
2174 ga_init2(&ga, sizeof(char_u *), 10);
Bram Moolenaar310091d2021-12-23 21:14:37 +00002175 for (lnum = iptr->isn_lnum; lnum < end_lnum
2176 && lnum <= ufunc->uf_lines.ga_len; ++lnum)
Bram Moolenaar59b50c32021-06-17 22:27:48 +02002177 {
Bram Moolenaar303215d2021-07-07 20:10:43 +02002178 char_u *p = ((char_u **)ufunc->uf_lines.ga_data)[lnum - 1];
Bram Moolenaar59b50c32021-06-17 22:27:48 +02002179
Bram Moolenaar303215d2021-07-07 20:10:43 +02002180 if (p == NULL)
2181 continue; // left over from continuation line
2182 p = skipwhite(p);
Bram Moolenaar59b50c32021-06-17 22:27:48 +02002183 if (*p == '#')
2184 break;
Bram Moolenaar35578162021-08-02 19:10:38 +02002185 if (GA_GROW_OK(&ga, 1))
Bram Moolenaar59b50c32021-06-17 22:27:48 +02002186 ((char_u **)(ga.ga_data))[ga.ga_len++] = p;
2187 if (STRNCMP(p, "def ", 4) == 0)
2188 break;
2189 }
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002190 line = ga_concat_strings(&ga, " ");
2191 vim_free(ga.ga_data);
2192 }
2193 else
2194 line = ((char_u **)ufunc->uf_lines.ga_data)[iptr->isn_lnum - 1];
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002195
Dominique Pelle6e969552021-06-17 13:53:41 +02002196 do_debug(line == NULL ? (char_u *)"[empty]" : line);
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002197 debug_context = NULL;
2198
2199 if (end_lnum > iptr->isn_lnum)
2200 vim_free(line);
2201}
2202
Bram Moolenaar4c137212021-04-19 16:48:48 +02002203/*
Bram Moolenaar65b0d162022-12-13 18:43:22 +00002204 * Store a value in a list, dict, blob or object variable.
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002205 * Returns OK, FAIL or NOTDONE (uncatchable error).
2206 */
2207 static int
2208execute_storeindex(isn_T *iptr, ectx_T *ectx)
2209{
Bram Moolenaarf7d1c6e2023-01-16 20:47:57 +00002210 vartype_T dest_type = iptr->isn_arg.storeindex.si_vartype;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002211 typval_T *tv;
2212 typval_T *tv_idx = STACK_TV_BOT(-2);
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002213 long lidx = 0;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002214 typval_T *tv_dest = STACK_TV_BOT(-1);
2215 int status = OK;
2216
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002217 if (tv_idx->v_type == VAR_NUMBER)
2218 lidx = (long)tv_idx->vval.v_number;
2219
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002220 // Stack contains:
2221 // -3 value to be stored
2222 // -2 index
Yegappan Lakshmanan3775f772023-09-01 22:05:45 +02002223 // -1 dict, list, blob, object or class
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002224 tv = STACK_TV_BOT(-3);
2225 SOURCING_LNUM = iptr->isn_lnum;
Gianmaria Bajod7085a02023-08-31 18:15:26 +02002226
2227 // Make sure an object has been initialized
2228 if (dest_type == VAR_OBJECT && tv_dest->vval.v_object == NULL)
2229 {
2230 emsg(_(e_using_null_object));
2231 status = FAIL;
2232 }
2233 else if (dest_type == VAR_ANY)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002234 {
2235 dest_type = tv_dest->v_type;
2236 if (dest_type == VAR_DICT)
2237 status = do_2string(tv_idx, TRUE, FALSE);
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002238 else if (dest_type == VAR_OBJECT && tv_idx->v_type == VAR_STRING)
2239 {
2240 // Need to get the member index now that the class is known.
2241 object_T *obj = tv_dest->vval.v_object;
2242 class_T *cl = obj->obj_class;
2243 char_u *member = tv_idx->vval.v_string;
2244
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02002245 int m_idx;
2246 ocmember_T *m = object_member_lookup(cl, member, 0, &m_idx);
2247 if (m != NULL)
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002248 {
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02002249 if (*member == '_')
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002250 {
Ernie Raele6c9aa52023-10-06 19:55:52 +02002251 emsg_var_cl_define(e_cannot_access_private_variable_str,
2252 m->ocm_name, 0, cl);
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02002253 status = FAIL;
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002254 }
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002255
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02002256 lidx = m_idx;
2257 }
2258 else
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002259 {
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +02002260 member_not_found_msg(cl, VAR_OBJECT, member, 0);
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002261 status = FAIL;
2262 }
2263 }
2264 else if ((dest_type == VAR_LIST || dest_type == VAR_OBJECT)
2265 && tv_idx->v_type != VAR_NUMBER)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002266 {
2267 emsg(_(e_number_expected));
2268 status = FAIL;
2269 }
2270 }
Bram Moolenaare08be092022-02-17 13:08:26 +00002271
2272 if (status == OK)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002273 {
Bram Moolenaare08be092022-02-17 13:08:26 +00002274 if (dest_type == VAR_LIST)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002275 {
Bram Moolenaare08be092022-02-17 13:08:26 +00002276 list_T *list = tv_dest->vval.v_list;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002277
Bram Moolenaare08be092022-02-17 13:08:26 +00002278 if (list == NULL)
2279 {
2280 emsg(_(e_list_not_set));
2281 return FAIL;
2282 }
2283 if (lidx < 0 && list->lv_len + lidx >= 0)
2284 // negative index is relative to the end
2285 lidx = list->lv_len + lidx;
2286 if (lidx < 0 || lidx > list->lv_len)
2287 {
2288 semsg(_(e_list_index_out_of_range_nr), lidx);
2289 return FAIL;
2290 }
2291 if (lidx < list->lv_len)
2292 {
2293 listitem_T *li = list_find(list, lidx);
2294
2295 if (error_if_locked(li->li_tv.v_lock,
Bram Moolenaar70c43d82022-01-26 21:01:15 +00002296 e_cannot_change_locked_list_item))
Bram Moolenaare08be092022-02-17 13:08:26 +00002297 return FAIL;
2298 // overwrite existing list item
2299 clear_tv(&li->li_tv);
2300 li->li_tv = *tv;
2301 }
2302 else
2303 {
2304 if (error_if_locked(list->lv_lock, e_cannot_change_locked_list))
2305 return FAIL;
2306 // append to list, only fails when out of memory
2307 if (list_append_tv(list, tv) == FAIL)
2308 return NOTDONE;
2309 clear_tv(tv);
2310 }
2311 }
2312 else if (dest_type == VAR_DICT)
2313 {
2314 char_u *key = tv_idx->vval.v_string;
2315 dict_T *dict = tv_dest->vval.v_dict;
2316 dictitem_T *di;
2317
2318 SOURCING_LNUM = iptr->isn_lnum;
2319 if (dict == NULL)
2320 {
2321 emsg(_(e_dictionary_not_set));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002322 return FAIL;
Bram Moolenaare08be092022-02-17 13:08:26 +00002323 }
2324 if (key == NULL)
2325 key = (char_u *)"";
2326 di = dict_find(dict, key, -1);
2327 if (di != NULL)
2328 {
2329 if (error_if_locked(di->di_tv.v_lock,
2330 e_cannot_change_dict_item))
2331 return FAIL;
2332 // overwrite existing value
2333 clear_tv(&di->di_tv);
2334 di->di_tv = *tv;
2335 }
2336 else
2337 {
2338 if (error_if_locked(dict->dv_lock, e_cannot_change_dict))
2339 return FAIL;
2340 // add to dict, only fails when out of memory
2341 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
2342 return NOTDONE;
2343 clear_tv(tv);
2344 }
2345 }
2346 else if (dest_type == VAR_BLOB)
2347 {
Bram Moolenaare08be092022-02-17 13:08:26 +00002348 blob_T *blob = tv_dest->vval.v_blob;
Bram Moolenaar65b0d162022-12-13 18:43:22 +00002349 varnumber_T nr;
2350 int error = FALSE;
2351 int len;
Bram Moolenaare08be092022-02-17 13:08:26 +00002352
2353 if (blob == NULL)
2354 {
2355 emsg(_(e_blob_not_set));
2356 return FAIL;
2357 }
2358 len = blob_len(blob);
2359 if (lidx < 0 && len + lidx >= 0)
2360 // negative index is relative to the end
2361 lidx = len + lidx;
2362
2363 // Can add one byte at the end.
2364 if (lidx < 0 || lidx > len)
2365 {
2366 semsg(_(e_blob_index_out_of_range_nr), lidx);
2367 return FAIL;
2368 }
2369 if (value_check_lock(blob->bv_lock, (char_u *)"blob", FALSE))
2370 return FAIL;
2371 nr = tv_get_number_chk(tv, &error);
2372 if (error)
2373 return FAIL;
2374 blob_set_append(blob, lidx, nr);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002375 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002376 else if (dest_type == VAR_CLASS || dest_type == VAR_OBJECT)
2377 {
Yegappan Lakshmanan3775f772023-09-01 22:05:45 +02002378 typval_T *otv;
Bram Moolenaarf7d1c6e2023-01-16 20:47:57 +00002379
Yegappan Lakshmanan3775f772023-09-01 22:05:45 +02002380 if (dest_type == VAR_OBJECT)
2381 {
2382 object_T *obj = tv_dest->vval.v_object;
2383
2384 otv = (typval_T *)(obj + 1);
2385 class_T *itf = iptr->isn_arg.storeindex.si_class;
2386 if (itf != NULL)
2387 // convert interface member index to class member index
Ernie Rael18143d32023-09-04 22:30:41 +02002388 lidx = object_index_from_itf_index(itf, FALSE, lidx,
Yegappan Lakshmanan5a05d372023-09-29 19:43:11 +02002389 obj->obj_class);
Yegappan Lakshmanan3775f772023-09-01 22:05:45 +02002390 }
2391 else
2392 {
2393 // VAR_CLASS
2394 class_T *class = tv_dest->vval.v_class;
2395 otv = class->class_members_tv;
2396 }
Bram Moolenaarf7d1c6e2023-01-16 20:47:57 +00002397
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002398 clear_tv(&otv[lidx]);
2399 otv[lidx] = *tv;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002400 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002401 else
2402 {
Bram Moolenaare08be092022-02-17 13:08:26 +00002403 status = FAIL;
2404 semsg(_(e_cannot_index_str), vartype_name(dest_type));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002405 }
2406 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002407
2408 clear_tv(tv_idx);
2409 clear_tv(tv_dest);
2410 ectx->ec_stack.ga_len -= 3;
2411 if (status == FAIL)
2412 {
2413 clear_tv(tv);
2414 return FAIL;
2415 }
2416 return OK;
2417}
2418
2419/*
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002420 * Store a value in a list or blob range.
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002421 */
2422 static int
2423execute_storerange(isn_T *iptr, ectx_T *ectx)
2424{
2425 typval_T *tv;
2426 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2427 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2428 typval_T *tv_dest = STACK_TV_BOT(-1);
2429 int status = OK;
2430
2431 // Stack contains:
2432 // -4 value to be stored
2433 // -3 first index or "none"
2434 // -2 second index or "none"
2435 // -1 destination list or blob
2436 tv = STACK_TV_BOT(-4);
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002437 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002438 if (tv_dest->v_type == VAR_LIST)
2439 {
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002440 long n1;
2441 long n2;
2442 listitem_T *li1;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002443
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002444 n1 = (long)tv_get_number_chk(tv_idx1, NULL);
2445 if (tv_idx2->v_type == VAR_SPECIAL
2446 && tv_idx2->vval.v_number == VVAL_NONE)
2447 n2 = list_len(tv_dest->vval.v_list) - 1;
2448 else
2449 n2 = (long)tv_get_number_chk(tv_idx2, NULL);
2450
Bram Moolenaar22ebd172022-04-01 15:26:58 +01002451 li1 = check_range_index_one(tv_dest->vval.v_list, &n1, TRUE, FALSE);
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002452 if (li1 == NULL)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002453 status = FAIL;
2454 else
2455 {
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002456 status = check_range_index_two(tv_dest->vval.v_list,
2457 &n1, li1, &n2, FALSE);
2458 if (status != FAIL)
2459 status = list_assign_range(
2460 tv_dest->vval.v_list,
2461 tv->vval.v_list,
2462 n1,
2463 n2,
2464 tv_idx2->v_type == VAR_SPECIAL,
2465 (char_u *)"=",
2466 (char_u *)"[unknown]");
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002467 }
2468 }
2469 else if (tv_dest->v_type == VAR_BLOB)
2470 {
2471 varnumber_T n1;
2472 varnumber_T n2;
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002473 long bloblen;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002474
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002475 n1 = tv_get_number_chk(tv_idx1, NULL);
2476 if (tv_idx2->v_type == VAR_SPECIAL
2477 && tv_idx2->vval.v_number == VVAL_NONE)
2478 n2 = blob_len(tv_dest->vval.v_blob) - 1;
2479 else
2480 n2 = tv_get_number_chk(tv_idx2, NULL);
2481 bloblen = blob_len(tv_dest->vval.v_blob);
2482
2483 if (check_blob_index(bloblen, n1, FALSE) == FAIL
2484 || check_blob_range(bloblen, n1, n2, FALSE) == FAIL)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002485 status = FAIL;
2486 else
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002487 status = blob_set_range(tv_dest->vval.v_blob, n1, n2, tv);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002488 }
2489 else
2490 {
2491 status = FAIL;
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002492 emsg(_(e_list_or_blob_required));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002493 }
2494
2495 clear_tv(tv_idx1);
2496 clear_tv(tv_idx2);
2497 clear_tv(tv_dest);
2498 ectx->ec_stack.ga_len -= 4;
2499 clear_tv(tv);
2500
2501 return status;
2502}
2503
2504/*
2505 * Unlet item in list or dict variable.
2506 */
2507 static int
2508execute_unletindex(isn_T *iptr, ectx_T *ectx)
2509{
2510 typval_T *tv_idx = STACK_TV_BOT(-2);
2511 typval_T *tv_dest = STACK_TV_BOT(-1);
2512 int status = OK;
2513
2514 // Stack contains:
2515 // -2 index
2516 // -1 dict or list
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002517 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002518 if (tv_dest->v_type == VAR_DICT)
2519 {
2520 // unlet a dict item, index must be a string
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002521 if (tv_idx->v_type != VAR_STRING && tv_idx->v_type != VAR_NUMBER)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002522 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002523 semsg(_(e_expected_str_but_got_str),
2524 vartype_name(VAR_STRING),
2525 vartype_name(tv_idx->v_type));
2526 status = FAIL;
2527 }
2528 else
2529 {
2530 dict_T *d = tv_dest->vval.v_dict;
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002531 char_u *key;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002532 dictitem_T *di = NULL;
2533
2534 if (d != NULL && value_check_lock(
2535 d->dv_lock, NULL, FALSE))
2536 status = FAIL;
2537 else
2538 {
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002539 if (tv_idx->v_type == VAR_STRING)
2540 {
2541 key = tv_idx->vval.v_string;
2542 if (key == NULL)
2543 key = (char_u *)"";
2544 }
2545 else
2546 {
2547 key = tv_get_string(tv_idx);
2548 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002549 if (d != NULL)
2550 di = dict_find(d, key, (int)STRLEN(key));
2551 if (di == NULL)
2552 {
2553 // NULL dict is equivalent to empty dict
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00002554 semsg(_(e_key_not_present_in_dictionary_str), key);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002555 status = FAIL;
2556 }
2557 else if (var_check_fixed(di->di_flags,
2558 NULL, FALSE)
2559 || var_check_ro(di->di_flags,
2560 NULL, FALSE))
2561 status = FAIL;
2562 else
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002563 dictitem_remove(d, di, "unlet");
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002564 }
2565 }
2566 }
2567 else if (tv_dest->v_type == VAR_LIST)
2568 {
2569 // unlet a List item, index must be a number
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002570 if (check_for_number(tv_idx) == FAIL)
2571 {
2572 status = FAIL;
2573 }
2574 else
2575 {
2576 list_T *l = tv_dest->vval.v_list;
2577 long n = (long)tv_idx->vval.v_number;
2578
2579 if (l != NULL && value_check_lock(
2580 l->lv_lock, NULL, FALSE))
2581 status = FAIL;
2582 else
2583 {
2584 listitem_T *li = list_find(l, n);
2585
2586 if (li == NULL)
2587 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002588 semsg(_(e_list_index_out_of_range_nr), n);
2589 status = FAIL;
2590 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002591 else
2592 listitem_remove(l, li);
2593 }
2594 }
2595 }
2596 else
2597 {
2598 status = FAIL;
2599 semsg(_(e_cannot_index_str),
2600 vartype_name(tv_dest->v_type));
2601 }
2602
2603 clear_tv(tv_idx);
2604 clear_tv(tv_dest);
2605 ectx->ec_stack.ga_len -= 2;
2606
2607 return status;
2608}
2609
2610/*
2611 * Unlet a range of items in a list variable.
2612 */
2613 static int
2614execute_unletrange(isn_T *iptr, ectx_T *ectx)
2615{
2616 // Stack contains:
2617 // -3 index1
2618 // -2 index2
2619 // -1 dict or list
2620 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2621 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2622 typval_T *tv_dest = STACK_TV_BOT(-1);
2623 int status = OK;
2624
2625 if (tv_dest->v_type == VAR_LIST)
2626 {
2627 // indexes must be a number
2628 SOURCING_LNUM = iptr->isn_lnum;
2629 if (check_for_number(tv_idx1) == FAIL
2630 || (tv_idx2->v_type != VAR_SPECIAL
2631 && check_for_number(tv_idx2) == FAIL))
2632 {
2633 status = FAIL;
2634 }
2635 else
2636 {
2637 list_T *l = tv_dest->vval.v_list;
2638 long n1 = (long)tv_idx1->vval.v_number;
2639 long n2 = tv_idx2->v_type == VAR_SPECIAL
2640 ? 0 : (long)tv_idx2->vval.v_number;
2641 listitem_T *li;
2642
2643 li = list_find_index(l, &n1);
2644 if (li == NULL)
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002645 {
2646 semsg(_(e_list_index_out_of_range_nr),
2647 (long)tv_idx1->vval.v_number);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002648 status = FAIL;
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002649 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002650 else
2651 {
2652 if (n1 < 0)
2653 n1 = list_idx_of_item(l, li);
2654 if (n2 < 0)
2655 {
2656 listitem_T *li2 = list_find(l, n2);
2657
2658 if (li2 == NULL)
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002659 {
2660 semsg(_(e_list_index_out_of_range_nr), n2);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002661 status = FAIL;
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002662 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002663 else
2664 n2 = list_idx_of_item(l, li2);
2665 }
2666 if (status != FAIL
2667 && tv_idx2->v_type != VAR_SPECIAL
2668 && n2 < n1)
2669 {
2670 semsg(_(e_list_index_out_of_range_nr), n2);
2671 status = FAIL;
2672 }
Bram Moolenaar6b8c7ba2022-03-20 17:46:06 +00002673 if (status != FAIL)
2674 list_unlet_range(l, li, n1,
2675 tv_idx2->v_type != VAR_SPECIAL, n2);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002676 }
2677 }
2678 }
2679 else
2680 {
2681 status = FAIL;
2682 SOURCING_LNUM = iptr->isn_lnum;
2683 semsg(_(e_cannot_index_str),
2684 vartype_name(tv_dest->v_type));
2685 }
2686
2687 clear_tv(tv_idx1);
2688 clear_tv(tv_idx2);
2689 clear_tv(tv_dest);
2690 ectx->ec_stack.ga_len -= 3;
2691
2692 return status;
2693}
2694
2695/*
2696 * Top of a for loop.
2697 */
2698 static int
2699execute_for(isn_T *iptr, ectx_T *ectx)
2700{
2701 typval_T *tv;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002702 int jump = FALSE;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002703 typval_T *ltv = STACK_TV_BOT(-1);
2704 typval_T *idxtv =
Bram Moolenaarcc341812022-09-19 15:54:34 +01002705 STACK_TV_VAR(iptr->isn_arg.forloop.for_loop_idx);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002706
2707 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
2708 return FAIL;
2709 if (ltv->v_type == VAR_LIST)
2710 {
2711 list_T *list = ltv->vval.v_list;
2712
2713 // push the next item from the list
2714 ++idxtv->vval.v_number;
2715 if (list == NULL
2716 || idxtv->vval.v_number >= list->lv_len)
2717 {
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002718 jump = TRUE;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002719 }
2720 else if (list->lv_first == &range_list_item)
2721 {
2722 // non-materialized range() list
2723 tv = STACK_TV_BOT(0);
2724 tv->v_type = VAR_NUMBER;
2725 tv->v_lock = 0;
2726 tv->vval.v_number = list_find_nr(
2727 list, idxtv->vval.v_number, NULL);
2728 ++ectx->ec_stack.ga_len;
2729 }
2730 else
2731 {
2732 listitem_T *li = list_find(list,
2733 idxtv->vval.v_number);
2734
2735 copy_tv(&li->li_tv, STACK_TV_BOT(0));
2736 ++ectx->ec_stack.ga_len;
2737 }
2738 }
2739 else if (ltv->v_type == VAR_STRING)
2740 {
2741 char_u *str = ltv->vval.v_string;
2742
2743 // The index is for the last byte of the previous
2744 // character.
2745 ++idxtv->vval.v_number;
2746 if (str == NULL || str[idxtv->vval.v_number] == NUL)
2747 {
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002748 jump = TRUE;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002749 }
2750 else
2751 {
2752 int clen = mb_ptr2len(str + idxtv->vval.v_number);
2753
2754 // Push the next character from the string.
2755 tv = STACK_TV_BOT(0);
2756 tv->v_type = VAR_STRING;
2757 tv->vval.v_string = vim_strnsave(
2758 str + idxtv->vval.v_number, clen);
2759 ++ectx->ec_stack.ga_len;
2760 idxtv->vval.v_number += clen - 1;
2761 }
2762 }
2763 else if (ltv->v_type == VAR_BLOB)
2764 {
2765 blob_T *blob = ltv->vval.v_blob;
2766
2767 // When we get here the first time make a copy of the
2768 // blob, so that the iteration still works when it is
2769 // changed.
2770 if (idxtv->vval.v_number == -1 && blob != NULL)
2771 {
2772 blob_copy(blob, ltv);
2773 blob_unref(blob);
2774 blob = ltv->vval.v_blob;
2775 }
2776
2777 // The index is for the previous byte.
2778 ++idxtv->vval.v_number;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002779 if (blob == NULL || idxtv->vval.v_number >= blob_len(blob))
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002780 {
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002781 jump = TRUE;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002782 }
2783 else
2784 {
2785 // Push the next byte from the blob.
2786 tv = STACK_TV_BOT(0);
2787 tv->v_type = VAR_NUMBER;
2788 tv->vval.v_number = blob_get(blob,
2789 idxtv->vval.v_number);
2790 ++ectx->ec_stack.ga_len;
2791 }
2792 }
2793 else
2794 {
2795 semsg(_(e_for_loop_on_str_not_supported),
2796 vartype_name(ltv->v_type));
2797 return FAIL;
2798 }
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002799
2800 if (jump)
2801 {
2802 // past the end of the list/string/blob, jump to "endfor"
2803 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
2804 may_restore_cmdmod(&ectx->ec_funclocal);
2805 }
2806 else
2807 {
2808 // Store the current number of funcrefs, this may be used in
2809 // ISN_LOOPEND. The variable index is always one more than the loop
2810 // variable index.
Bram Moolenaarcc341812022-09-19 15:54:34 +01002811 tv = STACK_TV_VAR(iptr->isn_arg.forloop.for_loop_idx + 1);
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002812 tv->vval.v_number = ectx->ec_funcrefs.ga_len;
2813 }
2814
2815 return OK;
2816}
2817
2818/*
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002819 * Code for handling variables declared inside a loop and used in a closure.
2820 * This is very similar to what is done with funcstack_T. The difference is
2821 * that the funcstack_T has the scope of a function, while a loopvars_T has the
2822 * scope of the block inside a loop and each loop may have its own.
2823 */
2824
2825// Double linked list of loopvars_T in use.
2826static loopvars_T *first_loopvars = NULL;
2827
2828 static void
2829add_loopvars_to_list(loopvars_T *loopvars)
2830{
2831 // Link in list of loopvarss.
2832 if (first_loopvars != NULL)
2833 first_loopvars->lvs_prev = loopvars;
2834 loopvars->lvs_next = first_loopvars;
2835 loopvars->lvs_prev = NULL;
2836 first_loopvars = loopvars;
2837}
2838
2839 static void
2840remove_loopvars_from_list(loopvars_T *loopvars)
2841{
2842 if (loopvars->lvs_prev == NULL)
2843 first_loopvars = loopvars->lvs_next;
2844 else
2845 loopvars->lvs_prev->lvs_next = loopvars->lvs_next;
2846 if (loopvars->lvs_next != NULL)
2847 loopvars->lvs_next->lvs_prev = loopvars->lvs_prev;
2848}
2849
2850/*
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002851 * End of a for or while loop: Handle any variables used by a closure.
2852 */
2853 static int
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002854execute_endloop(isn_T *iptr, ectx_T *ectx)
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002855{
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002856 endloop_T *endloop = &iptr->isn_arg.endloop;
2857 typval_T *tv_refcount = STACK_TV_VAR(endloop->end_funcref_idx);
2858 int prev_closure_count = tv_refcount->vval.v_number;
Bram Moolenaarcc341812022-09-19 15:54:34 +01002859 int depth = endloop->end_depth;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002860 garray_T *gap = &ectx->ec_funcrefs;
2861 int closure_in_use = FALSE;
2862 loopvars_T *loopvars;
2863 typval_T *stack;
2864 int idx;
2865
Bram Moolenaarcc341812022-09-19 15:54:34 +01002866 // Check if any created closure is still being referenced and loopvars have
2867 // not been saved yet for the current depth.
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002868 for (idx = prev_closure_count; idx < gap->ga_len; ++idx)
2869 {
2870 partial_T *pt = ((partial_T **)gap->ga_data)[idx];
2871
Bram Moolenaarcc341812022-09-19 15:54:34 +01002872 if (pt->pt_refcount > 1 && pt->pt_loopvars[depth] == NULL)
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002873 {
2874 int refcount = pt->pt_refcount;
2875 int i;
2876
2877 // A Reference in a variable inside the loop doesn't count, it gets
2878 // unreferenced at the end of the loop.
2879 for (i = 0; i < endloop->end_var_count; ++i)
2880 {
2881 typval_T *stv = STACK_TV_VAR(endloop->end_var_idx + i);
2882
2883 if (stv->v_type == VAR_PARTIAL && pt == stv->vval.v_partial)
2884 --refcount;
2885 }
2886 if (refcount > 1)
2887 {
2888 closure_in_use = TRUE;
2889 break;
2890 }
2891 }
2892 }
2893
2894 // If no function reference were created since the start of the loop block
2895 // or it is no longer referenced there is nothing to do.
2896 if (!closure_in_use)
2897 return OK;
2898
2899 // A closure is using variables declared inside the loop.
2900 // Move them to the called function.
2901 loopvars = ALLOC_CLEAR_ONE(loopvars_T);
2902 if (loopvars == NULL)
2903 return FAIL;
2904
2905 loopvars->lvs_ga.ga_len = endloop->end_var_count;
2906 stack = ALLOC_CLEAR_MULT(typval_T, loopvars->lvs_ga.ga_len);
2907 loopvars->lvs_ga.ga_data = stack;
2908 if (stack == NULL)
2909 {
2910 vim_free(loopvars);
2911 return FAIL;
2912 }
2913 add_loopvars_to_list(loopvars);
2914
2915 // Move the variable values.
2916 for (idx = 0; idx < endloop->end_var_count; ++idx)
2917 {
2918 typval_T *tv = STACK_TV_VAR(endloop->end_var_idx + idx);
2919
2920 *(stack + idx) = *tv;
2921 tv->v_type = VAR_UNKNOWN;
2922 }
2923
2924 for (idx = prev_closure_count; idx < gap->ga_len; ++idx)
2925 {
2926 partial_T *pt = ((partial_T **)gap->ga_data)[idx];
2927
Bram Moolenaarcc341812022-09-19 15:54:34 +01002928 if (pt->pt_refcount > 1 && pt->pt_loopvars[depth] == NULL)
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002929 {
2930 ++loopvars->lvs_refcount;
Bram Moolenaarcc341812022-09-19 15:54:34 +01002931 pt->pt_loopvars[depth] = loopvars;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002932
Bram Moolenaarcc341812022-09-19 15:54:34 +01002933 pt->pt_outer.out_loop[depth].stack = &loopvars->lvs_ga;
2934 pt->pt_outer.out_loop[depth].var_idx -=
2935 ectx->ec_frame_idx + STACK_FRAME_SIZE + endloop->end_var_idx;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002936 }
2937 }
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002938
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002939 return OK;
2940}
2941
2942/*
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002943 * Called when a partial is freed or its reference count goes down to one. The
2944 * loopvars may be the only reference to the partials in the local variables.
2945 * Go over all of them, the funcref and can be freed if all partials
2946 * referencing the loopvars have a reference count of one.
Bram Moolenaarcc341812022-09-19 15:54:34 +01002947 * Return TRUE if it was freed.
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002948 */
Bram Moolenaarcc341812022-09-19 15:54:34 +01002949 int
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002950loopvars_check_refcount(loopvars_T *loopvars)
2951{
2952 int i;
2953 garray_T *gap = &loopvars->lvs_ga;
2954 int done = 0;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002955 typval_T *stack = gap->ga_data;
2956
Bram Moolenaarcc341812022-09-19 15:54:34 +01002957 if (loopvars->lvs_refcount > loopvars->lvs_min_refcount)
2958 return FALSE;
2959 for (i = 0; i < gap->ga_len; ++i)
2960 {
2961 typval_T *tv = ((typval_T *)gap->ga_data) + i;
2962
2963 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL
2964 && tv->vval.v_partial->pt_refcount == 1)
2965 {
2966 int depth;
2967
2968 for (depth = 0; depth < MAX_LOOP_DEPTH; ++depth)
2969 if (tv->vval.v_partial->pt_loopvars[depth] == loopvars)
2970 ++done;
2971 }
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002972 }
Bram Moolenaarcc341812022-09-19 15:54:34 +01002973 if (done != loopvars->lvs_min_refcount)
2974 return FALSE;
2975
2976 // All partials referencing the loopvars have a reference count of
2977 // one, thus the loopvars is no longer of use.
2978 stack = gap->ga_data;
2979 for (i = 0; i < gap->ga_len; ++i)
2980 clear_tv(stack + i);
2981 vim_free(stack);
2982 remove_loopvars_from_list(loopvars);
2983 vim_free(loopvars);
2984 return TRUE;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002985}
2986
2987/*
2988 * For garbage collecting: set references in all variables referenced by
2989 * all loopvars.
2990 */
2991 int
2992set_ref_in_loopvars(int copyID)
2993{
2994 loopvars_T *loopvars;
2995
2996 for (loopvars = first_loopvars; loopvars != NULL;
2997 loopvars = loopvars->lvs_next)
2998 {
2999 typval_T *stack = loopvars->lvs_ga.ga_data;
3000 int i;
3001
3002 for (i = 0; i < loopvars->lvs_ga.ga_len; ++i)
3003 if (set_ref_in_item(stack + i, copyID, NULL, NULL))
3004 return TRUE; // abort
3005 }
3006 return FALSE;
3007}
3008
3009/*
Bram Moolenaar06b77222022-01-25 15:51:56 +00003010 * Load instruction for w:/b:/g:/t: variable.
3011 * "isn_type" is used instead of "iptr->isn_type".
3012 */
3013 static int
3014load_namespace_var(ectx_T *ectx, isntype_T isn_type, isn_T *iptr)
3015{
3016 dictitem_T *di = NULL;
3017 hashtab_T *ht = NULL;
3018 char namespace;
3019
3020 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
3021 return NOTDONE;
3022 switch (isn_type)
3023 {
3024 case ISN_LOADG:
3025 ht = get_globvar_ht();
3026 namespace = 'g';
3027 break;
3028 case ISN_LOADB:
3029 ht = &curbuf->b_vars->dv_hashtab;
3030 namespace = 'b';
3031 break;
3032 case ISN_LOADW:
3033 ht = &curwin->w_vars->dv_hashtab;
3034 namespace = 'w';
3035 break;
3036 case ISN_LOADT:
3037 ht = &curtab->tp_vars->dv_hashtab;
3038 namespace = 't';
3039 break;
3040 default: // Cannot reach here
3041 return NOTDONE;
3042 }
3043 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
3044
Bram Moolenaar06b77222022-01-25 15:51:56 +00003045 if (di == NULL)
3046 {
Bram Moolenaarfe732552022-02-22 19:39:13 +00003047 if (isn_type == ISN_LOADG)
3048 {
3049 ufunc_T *ufunc = find_func(iptr->isn_arg.string, TRUE);
3050
3051 // g:Something could be a function
3052 if (ufunc != NULL)
3053 {
3054 typval_T *tv = STACK_TV_BOT(0);
3055
3056 ++ectx->ec_stack.ga_len;
3057 tv->v_type = VAR_FUNC;
3058 tv->vval.v_string = alloc(STRLEN(iptr->isn_arg.string) + 3);
3059 if (tv->vval.v_string == NULL)
3060 return FAIL;
3061 STRCPY(tv->vval.v_string, "g:");
3062 STRCPY(tv->vval.v_string + 2, iptr->isn_arg.string);
3063 return OK;
3064 }
3065 }
Bram Moolenaar06b77222022-01-25 15:51:56 +00003066 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarfe732552022-02-22 19:39:13 +00003067 if (vim_strchr(iptr->isn_arg.string, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar06b77222022-01-25 15:51:56 +00003068 // no check if the item exists in the script but
3069 // isn't exported, it is too complicated
Bram Moolenaarfe732552022-02-22 19:39:13 +00003070 semsg(_(e_item_not_found_in_script_str), iptr->isn_arg.string);
Bram Moolenaar06b77222022-01-25 15:51:56 +00003071 else
3072 semsg(_(e_undefined_variable_char_str),
Bram Moolenaarfe732552022-02-22 19:39:13 +00003073 namespace, iptr->isn_arg.string);
Bram Moolenaar06b77222022-01-25 15:51:56 +00003074 return FAIL;
3075 }
3076 else
3077 {
3078 copy_tv(&di->di_tv, STACK_TV_BOT(0));
3079 ++ectx->ec_stack.ga_len;
3080 }
3081 return OK;
3082}
3083
Bram Moolenaard0200c82023-01-28 15:19:40 +00003084
3085 static void
3086object_required_error(typval_T *tv)
3087{
3088 garray_T type_list;
3089 ga_init2(&type_list, sizeof(type_T *), 10);
3090 type_T *type = typval2type(tv, get_copyID(), &type_list, TVTT_DO_MEMBER);
3091 char *tofree = NULL;
3092 char *typename = type_name(type, &tofree);
3093 semsg(_(e_object_required_found_str), typename);
3094 vim_free(tofree);
3095 clear_type_list(&type_list);
3096}
3097
Bram Moolenaar06b77222022-01-25 15:51:56 +00003098/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02003099 * Execute instructions in execution context "ectx".
3100 * Return OK or FAIL;
3101 */
3102 static int
3103exec_instructions(ectx_T *ectx)
3104{
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003105 int ret = FAIL;
3106 int save_trylevel_at_start = ectx->ec_trylevel_at_start;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02003107 int dict_stack_len_at_start = dict_stack.ga_len;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003108
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02003109 // Start execution at the first instruction.
Bram Moolenaar4c137212021-04-19 16:48:48 +02003110 ectx->ec_iidx = 0;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01003111
Bram Moolenaarff652882021-05-16 15:24:49 +02003112 // Only catch exceptions in this instruction list.
3113 ectx->ec_trylevel_at_start = trylevel;
3114
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003115 for (;;)
3116 {
Bram Moolenaara7490192021-07-22 12:26:14 +02003117 static int breakcheck_count = 0; // using "static" makes it faster
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003118 isn_T *iptr;
Bram Moolenaara7490192021-07-22 12:26:14 +02003119 typval_T *tv;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003120
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003121 if (unlikely(++breakcheck_count >= 100))
Bram Moolenaar270d0382020-05-15 21:42:53 +02003122 {
3123 line_breakcheck();
3124 breakcheck_count = 0;
3125 }
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003126 if (unlikely(got_int))
Bram Moolenaar20431c92020-03-20 18:39:46 +01003127 {
3128 // Turn CTRL-C into an exception.
3129 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +01003130 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003131 goto theend;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003132 did_throw = TRUE;
3133 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003134
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003135 if (unlikely(did_emsg && msg_list != NULL && *msg_list != NULL))
Bram Moolenaara26b9702020-04-18 19:53:28 +02003136 {
3137 // Turn an error message into an exception.
3138 did_emsg = FALSE;
3139 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003140 goto theend;
Bram Moolenaara26b9702020-04-18 19:53:28 +02003141 did_throw = TRUE;
3142 *msg_list = NULL;
Bram Moolenaar3ef2e412023-04-30 18:50:48 +01003143
3144 // This exception was not caught (yet).
3145 garray_T *trystack = &ectx->ec_trystack;
3146 if (trystack->ga_len > 0)
3147 {
3148 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
3149 + trystack->ga_len - 1;
3150 if (trycmd->tcd_frame_idx == ectx->ec_frame_idx)
3151 trycmd->tcd_caught = FALSE;
3152 }
Bram Moolenaara26b9702020-04-18 19:53:28 +02003153 }
3154
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003155 if (unlikely(did_throw))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003156 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003157 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003158 trycmd_T *trycmd = NULL;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003159 int index = trystack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003160
3161 // An exception jumps to the first catch, finally, or returns from
3162 // the current function.
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003163 while (index > 0)
3164 {
3165 trycmd = ((trycmd_T *)trystack->ga_data) + index - 1;
Bram Moolenaar3ef2e412023-04-30 18:50:48 +01003166 // 1. after :try and before :catch - jump to first :catch
3167 // 2. in :catch block - jump to :finally
3168 // 3. in :catch block and no finally - jump to :endtry
3169 if (!trycmd->tcd_in_catch || trycmd->tcd_finally_idx != 0
3170 || trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003171 break;
3172 // In the catch and finally block of this try we have to go up
3173 // one level.
3174 --index;
3175 trycmd = NULL;
3176 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003177 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003178 {
Bram Moolenaar834193a2021-06-30 20:39:15 +02003179 if (trycmd->tcd_in_catch)
3180 {
Bram Moolenaar3ef2e412023-04-30 18:50:48 +01003181 if (trycmd->tcd_finally_idx > 0)
3182 {
3183 // exception inside ":catch", jump to ":finally" once
3184 ectx->ec_iidx = trycmd->tcd_finally_idx;
3185 trycmd->tcd_finally_idx = 0;
3186 }
3187 else
3188 {
3189 // exception inside ":catch" or ":finally", jump to
3190 // ":endtry"
3191 ectx->ec_iidx = trycmd->tcd_endtry_idx;
3192 }
Bram Moolenaar834193a2021-06-30 20:39:15 +02003193 }
3194 else
Bram Moolenaar3ef2e412023-04-30 18:50:48 +01003195 {
Bram Moolenaar834193a2021-06-30 20:39:15 +02003196 // jump to first ":catch"
3197 ectx->ec_iidx = trycmd->tcd_catch_idx;
Bram Moolenaar3ef2e412023-04-30 18:50:48 +01003198 trycmd->tcd_in_catch = TRUE;
3199 }
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003200 did_throw = FALSE; // don't come back here until :endtry
3201 trycmd->tcd_did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003202 }
3203 else
3204 {
Bram Moolenaar3ef2e412023-04-30 18:50:48 +01003205 // Not inside try or need to return from current function.
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02003206 // Push a dummy return value.
Bram Moolenaar35578162021-08-02 19:10:38 +02003207 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003208 goto theend;
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02003209 tv = STACK_TV_BOT(0);
3210 tv->v_type = VAR_NUMBER;
3211 tv->vval.v_number = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003212 ++ectx->ec_stack.ga_len;
3213 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003214 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02003215 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +01003216 need_rethrow = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003217 if (handle_closure_in_use(ectx, FALSE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003218 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003219 goto done;
3220 }
3221
Bram Moolenaar4c137212021-04-19 16:48:48 +02003222 if (func_return(ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003223 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003224 }
3225 continue;
3226 }
3227
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003228 /*
3229 * Big switch on the instruction. Most compilers will be turning this
3230 * into an efficient lookup table, since the "case" values are an enum
3231 * with sequential numbers. It may look ugly, but it should be the
3232 * most efficient way.
3233 */
Bram Moolenaar4c137212021-04-19 16:48:48 +02003234 iptr = &ectx->ec_instr[ectx->ec_iidx++];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003235 switch (iptr->isn_type)
3236 {
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00003237 // Constructor, first instruction in a new() method.
Bram Moolenaar00b28d62022-12-08 15:32:33 +00003238 case ISN_CONSTRUCT:
3239 // "this" is always the local variable at index zero
3240 tv = STACK_TV_VAR(0);
3241 tv->v_type = VAR_OBJECT;
3242 tv->vval.v_object = alloc_clear(
3243 iptr->isn_arg.construct.construct_size);
3244 tv->vval.v_object->obj_class =
3245 iptr->isn_arg.construct.construct_class;
Bram Moolenaard28d7b92022-12-08 20:42:00 +00003246 ++tv->vval.v_object->obj_class->class_refcount;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00003247 tv->vval.v_object->obj_refcount = 1;
Bram Moolenaard28d7b92022-12-08 20:42:00 +00003248 object_created(tv->vval.v_object);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00003249 break;
3250
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003251 // execute Ex command line
3252 case ISN_EXEC:
Ernie Raelee865f32023-09-29 19:53:55 +02003253 if (exec_command(iptr, iptr->isn_arg.string) == FAIL)
Bram Moolenaaraacc9662021-08-13 19:40:51 +02003254 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003255 break;
3256
Bram Moolenaar20677332021-06-06 17:02:53 +02003257 // execute Ex command line split at NL characters.
3258 case ISN_EXEC_SPLIT:
3259 {
3260 source_cookie_T cookie;
Bram Moolenaar518df272021-06-06 17:34:13 +02003261 char_u *line;
Bram Moolenaar20677332021-06-06 17:02:53 +02003262
3263 SOURCING_LNUM = iptr->isn_lnum;
3264 CLEAR_FIELD(cookie);
3265 cookie.sourcing_lnum = iptr->isn_lnum - 1;
3266 cookie.nextline = iptr->isn_arg.string;
Bram Moolenaar518df272021-06-06 17:34:13 +02003267 line = get_split_sourceline(0, &cookie, 0, 0);
3268 if (do_cmdline(line,
Bram Moolenaar20677332021-06-06 17:02:53 +02003269 get_split_sourceline, &cookie,
3270 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED)
3271 == FAIL
3272 || did_emsg)
Bram Moolenaar518df272021-06-06 17:34:13 +02003273 {
3274 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02003275 goto on_error;
Bram Moolenaar518df272021-06-06 17:34:13 +02003276 }
3277 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02003278 }
3279 break;
3280
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00003281 // execute Ex command line that is only a range
3282 case ISN_EXECRANGE:
3283 {
3284 exarg_T ea;
3285 char *error = NULL;
3286
3287 CLEAR_FIELD(ea);
3288 ea.cmdidx = CMD_SIZE;
3289 ea.addr_type = ADDR_LINES;
3290 ea.cmd = iptr->isn_arg.string;
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00003291 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00003292 parse_cmd_address(&ea, &error, FALSE);
Bram Moolenaar01a4dcb2021-12-04 13:15:10 +00003293 if (ea.cmd == NULL)
3294 goto on_error;
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00003295 // error is always NULL when using ADDR_LINES
3296 error = ex_range_without_command(&ea);
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00003297 if (error != NULL)
3298 {
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00003299 emsg(error);
3300 goto on_error;
3301 }
3302 }
3303 break;
3304
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02003305 // Evaluate an expression with legacy syntax, push it onto the
3306 // stack.
3307 case ISN_LEGACY_EVAL:
3308 {
3309 char_u *arg = iptr->isn_arg.string;
3310 int res;
3311 int save_flags = cmdmod.cmod_flags;
3312
Bram Moolenaar35578162021-08-02 19:10:38 +02003313 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003314 goto theend;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02003315 tv = STACK_TV_BOT(0);
3316 init_tv(tv);
3317 cmdmod.cmod_flags |= CMOD_LEGACY;
3318 res = eval0(arg, tv, NULL, &EVALARG_EVALUATE);
3319 cmdmod.cmod_flags = save_flags;
3320 if (res == FAIL)
3321 goto on_error;
3322 ++ectx->ec_stack.ga_len;
3323 }
3324 break;
3325
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003326 // push typeval VAR_INSTR with instructions to be executed
3327 case ISN_INSTR:
3328 {
Bram Moolenaar35578162021-08-02 19:10:38 +02003329 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003330 goto theend;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003331 tv = STACK_TV_BOT(0);
3332 tv->vval.v_instr = ALLOC_ONE(instr_T);
3333 if (tv->vval.v_instr == NULL)
3334 goto on_error;
3335 ++ectx->ec_stack.ga_len;
3336
3337 tv->v_type = VAR_INSTR;
3338 tv->vval.v_instr->instr_ectx = ectx;
3339 tv->vval.v_instr->instr_instr = iptr->isn_arg.instr;
3340 }
3341 break;
3342
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003343 case ISN_SOURCE:
3344 {
Bram Moolenaar89445512022-04-14 12:58:23 +01003345 int notused;
LemonBoy77142312022-04-15 20:50:46 +01003346
Bram Moolenaar89445512022-04-14 12:58:23 +01003347 SOURCING_LNUM = iptr->isn_lnum;
3348 if (may_load_script((int)iptr->isn_arg.number, &notused)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003349 == FAIL)
Bram Moolenaar89445512022-04-14 12:58:23 +01003350 goto on_error;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003351 }
3352 break;
3353
Bram Moolenaar4c137212021-04-19 16:48:48 +02003354 // execute :substitute with an expression
3355 case ISN_SUBSTITUTE:
3356 {
3357 subs_T *subs = &iptr->isn_arg.subs;
3358 source_cookie_T cookie;
3359 struct subs_expr_S *save_instr = substitute_instr;
3360 struct subs_expr_S subs_instr;
3361 int res;
3362
3363 subs_instr.subs_ectx = ectx;
3364 subs_instr.subs_instr = subs->subs_instr;
3365 subs_instr.subs_status = OK;
3366 substitute_instr = &subs_instr;
3367
3368 SOURCING_LNUM = iptr->isn_lnum;
3369 // This is very much like ISN_EXEC
3370 CLEAR_FIELD(cookie);
3371 cookie.sourcing_lnum = iptr->isn_lnum - 1;
3372 res = do_cmdline(subs->subs_cmd,
3373 getsourceline, &cookie,
3374 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
3375 substitute_instr = save_instr;
3376
3377 if (res == FAIL || did_emsg
3378 || subs_instr.subs_status == FAIL)
3379 goto on_error;
3380 }
3381 break;
3382
3383 case ISN_FINISH:
3384 goto done;
3385
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02003386 case ISN_REDIRSTART:
3387 // create a dummy entry for var_redir_str()
3388 if (alloc_redir_lval() == FAIL)
3389 goto on_error;
3390
3391 // The output is stored in growarray "redir_ga" until
3392 // redirection ends.
3393 init_redir_ga();
3394 redir_vname = 1;
3395 break;
3396
3397 case ISN_REDIREND:
3398 {
3399 char_u *res = get_clear_redir_ga();
3400
3401 // End redirection, put redirected text on the stack.
3402 clear_redir_lval();
3403 redir_vname = 0;
3404
Bram Moolenaar35578162021-08-02 19:10:38 +02003405 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02003406 {
3407 vim_free(res);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003408 goto theend;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02003409 }
3410 tv = STACK_TV_BOT(0);
3411 tv->v_type = VAR_STRING;
3412 tv->vval.v_string = res;
3413 ++ectx->ec_stack.ga_len;
3414 }
3415 break;
3416
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003417 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02003418#ifdef FEAT_QUICKFIX
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003419 force_abort = TRUE;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003420 if (trigger_cexpr_autocmd(iptr->isn_arg.number) == FAIL)
3421 goto on_error;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003422 force_abort = FALSE;
Bram Moolenaarb7c97812021-05-05 22:51:39 +02003423#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003424 break;
3425
3426 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02003427#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003428 {
3429 exarg_T ea;
3430 int res;
3431
3432 CLEAR_FIELD(ea);
3433 ea.cmdidx = iptr->isn_arg.cexpr.cexpr_ref->cer_cmdidx;
3434 ea.forceit = iptr->isn_arg.cexpr.cexpr_ref->cer_forceit;
3435 ea.cmdlinep = &iptr->isn_arg.cexpr.cexpr_ref->cer_cmdline;
3436 --ectx->ec_stack.ga_len;
3437 tv = STACK_TV_BOT(0);
Bram Moolenaarbd683e32022-07-18 17:49:03 +01003438 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003439 res = cexpr_core(&ea, tv);
3440 clear_tv(tv);
3441 if (res == FAIL)
3442 goto on_error;
3443 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02003444#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003445 break;
3446
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003447 // execute Ex command from pieces on the stack
3448 case ISN_EXECCONCAT:
3449 {
3450 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +02003451 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003452 int pass;
3453 int i;
3454 char_u *cmd = NULL;
3455 char_u *str;
3456
3457 for (pass = 1; pass <= 2; ++pass)
3458 {
3459 for (i = 0; i < count; ++i)
3460 {
3461 tv = STACK_TV_BOT(i - count);
3462 str = tv->vval.v_string;
3463 if (str != NULL && *str != NUL)
3464 {
3465 if (pass == 2)
3466 STRCPY(cmd + len, str);
3467 len += STRLEN(str);
3468 }
3469 if (pass == 2)
3470 clear_tv(tv);
3471 }
3472 if (pass == 1)
3473 {
3474 cmd = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003475 if (unlikely(cmd == NULL))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003476 goto theend;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003477 len = 0;
3478 }
3479 }
3480
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02003481 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003482 do_cmdline_cmd(cmd);
3483 vim_free(cmd);
3484 }
3485 break;
3486
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003487 // execute :echo {string} ...
3488 case ISN_ECHO:
3489 {
3490 int count = iptr->isn_arg.echo.echo_count;
3491 int atstart = TRUE;
3492 int needclr = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003493 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003494
3495 for (idx = 0; idx < count; ++idx)
3496 {
3497 tv = STACK_TV_BOT(idx - count);
3498 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
3499 &atstart, &needclr);
3500 clear_tv(tv);
3501 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01003502 if (needclr)
3503 msg_clr_eos();
Bram Moolenaar4c137212021-04-19 16:48:48 +02003504 ectx->ec_stack.ga_len -= count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003505 }
3506 break;
3507
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003508 // :execute {string} ...
3509 // :echomsg {string} ...
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01003510 // :echowindow {string} ...
Bram Moolenaar7de62622021-08-07 15:05:47 +02003511 // :echoconsole {string} ...
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003512 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01003513 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003514 case ISN_ECHOMSG:
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01003515 case ISN_ECHOWINDOW:
Bram Moolenaar7de62622021-08-07 15:05:47 +02003516 case ISN_ECHOCONSOLE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003517 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01003518 {
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01003519 int count;
Bram Moolenaarad39c092020-02-26 18:23:43 +01003520 garray_T ga;
3521 char_u buf[NUMBUFLEN];
3522 char_u *p;
3523 int len;
3524 int failed = FALSE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003525 int idx;
Bram Moolenaarad39c092020-02-26 18:23:43 +01003526
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01003527 if (iptr->isn_type == ISN_ECHOWINDOW)
3528 count = iptr->isn_arg.echowin.ewin_count;
3529 else
3530 count = iptr->isn_arg.number;
Bram Moolenaarad39c092020-02-26 18:23:43 +01003531 ga_init2(&ga, 1, 80);
3532 for (idx = 0; idx < count; ++idx)
3533 {
3534 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003535 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01003536 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003537 if (tv->v_type == VAR_CHANNEL
3538 || tv->v_type == VAR_JOB)
3539 {
3540 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar68db9962021-05-09 23:19:22 +02003541 semsg(_(e_using_invalid_value_as_string_str),
3542 vartype_name(tv->v_type));
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003543 break;
3544 }
3545 else
3546 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01003547 }
3548 else
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003549 p = tv_stringify(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01003550
3551 len = (int)STRLEN(p);
Bram Moolenaar35578162021-08-02 19:10:38 +02003552 if (GA_GROW_FAILS(&ga, len + 2))
Bram Moolenaarad39c092020-02-26 18:23:43 +01003553 failed = TRUE;
3554 else
3555 {
3556 if (ga.ga_len > 0)
3557 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
3558 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
3559 ga.ga_len += len;
3560 }
3561 clear_tv(tv);
3562 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003563 ectx->ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003564 if (failed)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003565 {
3566 ga_clear(&ga);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003567 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003568 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01003569
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003570 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003571 {
3572 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02003573 {
3574 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003575 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01003576 if (did_emsg)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003577 {
3578 ga_clear(&ga);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01003579 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003580 }
Bram Moolenaar430deb12020-08-23 16:29:11 +02003581 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003582 else
3583 {
3584 msg_sb_eol();
3585 if (iptr->isn_type == ISN_ECHOMSG)
3586 {
3587 msg_attr(ga.ga_data, echo_attr);
3588 out_flush();
3589 }
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01003590#ifdef HAS_MESSAGE_WINDOW
3591 else if (iptr->isn_type == ISN_ECHOWINDOW)
3592 {
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01003593 start_echowindow(
3594 iptr->isn_arg.echowin.ewin_time);
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01003595 msg_attr(ga.ga_data, echo_attr);
3596 end_echowindow();
3597 }
3598#endif
Bram Moolenaar7de62622021-08-07 15:05:47 +02003599 else if (iptr->isn_type == ISN_ECHOCONSOLE)
3600 {
3601 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data),
3602 TRUE);
3603 ui_write((char_u *)"\r\n", 2, TRUE);
3604 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003605 else
3606 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003607 SOURCING_LNUM = iptr->isn_lnum;
3608 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003609 }
3610 }
3611 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01003612 ga_clear(&ga);
3613 }
3614 break;
3615
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003616 // load local variable or argument
3617 case ISN_LOAD:
Bram Moolenaar35578162021-08-02 19:10:38 +02003618 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003619 goto theend;
Bram Moolenaar2ed57ac2023-04-01 22:05:38 +01003620 tv = STACK_TV_VAR(iptr->isn_arg.number);
3621 if (tv->v_type == VAR_UNKNOWN)
3622 {
3623 // missing argument or default value v:none
3624 STACK_TV_BOT(0)->v_type = VAR_SPECIAL;
3625 STACK_TV_BOT(0)->vval.v_number = VVAL_NONE;
3626 }
3627 else
3628 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003629 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003630 break;
3631
3632 // load v: variable
3633 case ISN_LOADV:
Bram Moolenaar35578162021-08-02 19:10:38 +02003634 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003635 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003636 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003637 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003638 break;
3639
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003640 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003641 case ISN_LOADSCRIPT:
3642 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01003643 scriptref_T *sref = iptr->isn_arg.script.scriptref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003644 svar_T *sv;
3645
Bram Moolenaar6db660b2021-08-01 14:08:54 +02003646 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003647 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003648 goto theend;
Bram Moolenaar859cc212022-03-28 15:22:35 +01003649 allocate_if_null(sv);
Bram Moolenaar35578162021-08-02 19:10:38 +02003650 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003651 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003652 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003653 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003654 }
3655 break;
3656
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003657 // load s: variable in old script or autoload import
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003658 case ISN_LOADS:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003659 case ISN_LOADEXPORT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003660 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003661 int sid = iptr->isn_arg.loadstore.ls_sid;
3662 hashtab_T *ht = &SCRIPT_VARS(sid);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003663 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003664 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003665
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003666 if (di == NULL)
3667 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003668 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003669 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003670 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003671 }
3672 else
3673 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003674 if (iptr->isn_type == ISN_LOADEXPORT)
3675 {
3676 int idx = get_script_item_idx(sid, name, 0,
3677 NULL, NULL);
3678 svar_T *sv;
3679
3680 if (idx >= 0)
3681 {
3682 sv = ((svar_T *)SCRIPT_ITEM(sid)
3683 ->sn_var_vals.ga_data) + idx;
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003684 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003685 {
3686 SOURCING_LNUM = iptr->isn_lnum;
3687 semsg(_(e_item_not_exported_in_script_str),
3688 name);
3689 goto on_error;
3690 }
3691 }
3692 }
Bram Moolenaar35578162021-08-02 19:10:38 +02003693 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003694 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003695 copy_tv(&di->di_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003696 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003697 }
3698 }
3699 break;
3700
Bram Moolenaard3aac292020-04-19 14:32:17 +02003701 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003702 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02003703 case ISN_LOADB:
3704 case ISN_LOADW:
3705 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003706 {
Bram Moolenaar06b77222022-01-25 15:51:56 +00003707 int res = load_namespace_var(ectx, iptr->isn_type, iptr);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003708
Bram Moolenaar06b77222022-01-25 15:51:56 +00003709 if (res == NOTDONE)
Bram Moolenaarcbbc48f2022-01-18 12:58:28 +00003710 goto theend;
Bram Moolenaar06b77222022-01-25 15:51:56 +00003711 if (res == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003712 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003713 }
Bram Moolenaar06b77222022-01-25 15:51:56 +00003714
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003715 break;
3716
Bram Moolenaar03290b82020-12-19 16:30:44 +01003717 // load autoload variable
3718 case ISN_LOADAUTO:
3719 {
3720 char_u *name = iptr->isn_arg.string;
3721
Bram Moolenaar35578162021-08-02 19:10:38 +02003722 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003723 goto theend;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003724 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003725 if (eval_variable(name, (int)STRLEN(name), 0,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003726 STACK_TV_BOT(0), NULL, EVAL_VAR_VERBOSE) == FAIL)
Bram Moolenaar03290b82020-12-19 16:30:44 +01003727 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003728 ++ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003729 }
3730 break;
3731
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003732 // load g:/b:/w:/t: namespace
3733 case ISN_LOADGDICT:
3734 case ISN_LOADBDICT:
3735 case ISN_LOADWDICT:
3736 case ISN_LOADTDICT:
3737 {
3738 dict_T *d = NULL;
3739
3740 switch (iptr->isn_type)
3741 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02003742 case ISN_LOADGDICT: d = get_globvar_dict(); break;
3743 case ISN_LOADBDICT: d = curbuf->b_vars; break;
3744 case ISN_LOADWDICT: d = curwin->w_vars; break;
3745 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003746 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003747 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003748 }
Bram Moolenaar35578162021-08-02 19:10:38 +02003749 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003750 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003751 tv = STACK_TV_BOT(0);
3752 tv->v_type = VAR_DICT;
3753 tv->v_lock = 0;
3754 tv->vval.v_dict = d;
Bram Moolenaar1bd3cb22021-02-24 12:27:31 +01003755 ++d->dv_refcount;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003756 ++ectx->ec_stack.ga_len;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003757 }
3758 break;
3759
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003760 // load &option
3761 case ISN_LOADOPT:
3762 {
3763 typval_T optval;
3764 char_u *name = iptr->isn_arg.string;
3765
Bram Moolenaara8c17702020-04-01 21:17:24 +02003766 // This is not expected to fail, name is checked during
3767 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar35578162021-08-02 19:10:38 +02003768 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003769 goto theend;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003770 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003771 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003772 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003773 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003774 }
3775 break;
3776
3777 // load $ENV
3778 case ISN_LOADENV:
3779 {
3780 typval_T optval;
3781 char_u *name = iptr->isn_arg.string;
3782
Bram Moolenaar35578162021-08-02 19:10:38 +02003783 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003784 goto theend;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003785 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003786 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003787 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003788 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003789 }
3790 break;
3791
3792 // load @register
3793 case ISN_LOADREG:
Bram Moolenaar35578162021-08-02 19:10:38 +02003794 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003795 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003796 tv = STACK_TV_BOT(0);
3797 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003798 tv->v_lock = 0;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02003799 // This may result in NULL, which should be equivalent to an
3800 // empty string.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003801 tv->vval.v_string = get_reg_contents(
3802 iptr->isn_arg.number, GREG_EXPR_SRC);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003803 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003804 break;
3805
3806 // store local variable
3807 case ISN_STORE:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003808 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003809 tv = STACK_TV_VAR(iptr->isn_arg.number);
3810 clear_tv(tv);
3811 *tv = *STACK_TV_BOT(0);
3812 break;
3813
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003814 // store s: variable in old script or autoload import
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003815 case ISN_STORES:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003816 case ISN_STOREEXPORT:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003817 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003818 int sid = iptr->isn_arg.loadstore.ls_sid;
3819 hashtab_T *ht = &SCRIPT_VARS(sid);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003820 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003821 dictitem_T *di = find_var_in_ht(ht, 0,
3822 iptr->isn_type == ISN_STORES
3823 ? name + 2 : name, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003824
Bram Moolenaar4c137212021-04-19 16:48:48 +02003825 --ectx->ec_stack.ga_len;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003826 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003827 if (di == NULL)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003828 {
3829 if (iptr->isn_type == ISN_STOREEXPORT)
3830 {
3831 semsg(_(e_undefined_variable_str), name);
Bram Moolenaard1d26842022-03-31 10:13:47 +01003832 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003833 goto on_error;
3834 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003835 store_var(name, STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003836 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003837 else
3838 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003839 if (iptr->isn_type == ISN_STOREEXPORT)
3840 {
3841 int idx = get_script_item_idx(sid, name, 0,
3842 NULL, NULL);
3843
Bram Moolenaar06651632022-04-27 17:54:25 +01003844 // can this ever fail?
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003845 if (idx >= 0)
3846 {
3847 svar_T *sv = ((svar_T *)SCRIPT_ITEM(sid)
3848 ->sn_var_vals.ga_data) + idx;
3849
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003850 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003851 {
3852 semsg(_(e_item_not_exported_in_script_str),
3853 name);
Bram Moolenaard1d26842022-03-31 10:13:47 +01003854 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003855 goto on_error;
3856 }
3857 }
3858 }
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02003859 if (var_check_permission(di, name) == FAIL)
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003860 {
3861 clear_tv(STACK_TV_BOT(0));
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02003862 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003863 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003864 clear_tv(&di->di_tv);
3865 di->di_tv = *STACK_TV_BOT(0);
3866 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003867 }
3868 break;
3869
3870 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003871 case ISN_STORESCRIPT:
3872 {
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003873 scriptref_T *sref = iptr->isn_arg.script.scriptref;
3874 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003875
Bram Moolenaar6db660b2021-08-01 14:08:54 +02003876 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003877 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003878 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003879 --ectx->ec_stack.ga_len;
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003880
3881 // "const" and "final" are checked at compile time, locking
3882 // the value needs to be checked here.
3883 SOURCING_LNUM = iptr->isn_lnum;
3884 if (value_check_lock(sv->sv_tv->v_lock, sv->sv_name, FALSE))
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003885 {
3886 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003887 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003888 }
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003889
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003890 clear_tv(sv->sv_tv);
3891 *sv->sv_tv = *STACK_TV_BOT(0);
3892 }
3893 break;
3894
3895 // store option
3896 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003897 case ISN_STOREFUNCOPT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003898 {
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003899 char_u *opt_name = iptr->isn_arg.storeopt.so_name;
3900 int opt_flags = iptr->isn_arg.storeopt.so_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003901 long n = 0;
3902 char_u *s = NULL;
3903 char *msg;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003904 char_u numbuf[NUMBUFLEN];
3905 char_u *tofree = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003906
Bram Moolenaar4c137212021-04-19 16:48:48 +02003907 --ectx->ec_stack.ga_len;
LemonBoy32f34612023-09-02 21:52:05 +02003908 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003909 tv = STACK_TV_BOT(0);
3910 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01003911 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003912 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01003913 if (s == NULL)
3914 s = (char_u *)"";
3915 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003916 else if (iptr->isn_type == ISN_STOREFUNCOPT)
3917 {
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003918 // If the option can be set to a function reference or
3919 // a lambda and the passed value is a function
3920 // reference, then convert it to the name (string) of
3921 // the function reference.
3922 s = tv2string(tv, &tofree, numbuf, 0);
3923 if (s == NULL || *s == NUL)
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003924 {
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003925 // cannot happen?
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003926 clear_tv(tv);
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003927 vim_free(tofree);
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003928 goto on_error;
3929 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003930 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003931 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003932 // must be VAR_NUMBER, CHECKTYPE makes sure
3933 n = tv->vval.v_number;
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003934 msg = set_option_value(opt_name, n, s, opt_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02003935 clear_tv(tv);
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003936 vim_free(tofree);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003937 if (msg != NULL)
3938 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003939 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003940 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02003941 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003942 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003943 }
3944 break;
3945
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003946 // store $ENV
3947 case ISN_STOREENV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003948 --ectx->ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003949 tv = STACK_TV_BOT(0);
3950 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
3951 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003952 break;
3953
3954 // store @r
3955 case ISN_STOREREG:
3956 {
3957 int reg = iptr->isn_arg.number;
3958
Bram Moolenaar4c137212021-04-19 16:48:48 +02003959 --ectx->ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01003960 tv = STACK_TV_BOT(0);
Bram Moolenaar74f4a962021-06-17 21:03:07 +02003961 write_reg_contents(reg, tv_get_string(tv), -1, FALSE);
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01003962 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003963 }
3964 break;
3965
3966 // store v: variable
3967 case ISN_STOREV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003968 --ectx->ec_stack.ga_len;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003969 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
3970 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003971 // should not happen, type is checked when compiling
3972 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003973 break;
3974
Bram Moolenaard3aac292020-04-19 14:32:17 +02003975 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003976 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02003977 case ISN_STOREB:
3978 case ISN_STOREW:
3979 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003980 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003981 dictitem_T *di;
3982 hashtab_T *ht;
3983 char_u *name = iptr->isn_arg.string + 2;
3984
Bram Moolenaard3aac292020-04-19 14:32:17 +02003985 switch (iptr->isn_type)
3986 {
3987 case ISN_STOREG:
3988 ht = get_globvar_ht();
3989 break;
3990 case ISN_STOREB:
3991 ht = &curbuf->b_vars->dv_hashtab;
3992 break;
3993 case ISN_STOREW:
3994 ht = &curwin->w_vars->dv_hashtab;
3995 break;
3996 case ISN_STORET:
3997 ht = &curtab->tp_vars->dv_hashtab;
3998 break;
3999 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004000 goto theend;
Bram Moolenaard3aac292020-04-19 14:32:17 +02004001 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004002
Bram Moolenaar4c137212021-04-19 16:48:48 +02004003 --ectx->ec_stack.ga_len;
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01004004 di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004005 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01004006 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004007 else
4008 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01004009 SOURCING_LNUM = iptr->isn_lnum;
4010 if (var_check_permission(di, name) == FAIL)
4011 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004012 clear_tv(&di->di_tv);
4013 di->di_tv = *STACK_TV_BOT(0);
4014 }
4015 }
4016 break;
4017
Bram Moolenaar03290b82020-12-19 16:30:44 +01004018 // store an autoload variable
4019 case ISN_STOREAUTO:
4020 SOURCING_LNUM = iptr->isn_lnum;
4021 set_var(iptr->isn_arg.string, STACK_TV_BOT(-1), TRUE);
4022 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02004023 --ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01004024 break;
4025
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004026 // store number in local variable
4027 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01004028 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004029 clear_tv(tv);
4030 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01004031 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004032 break;
4033
Bram Moolenaar590162c2022-12-24 21:24:06 +00004034 // Store a value in a list, dict, blob or object variable.
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01004035 case ISN_STOREINDEX:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004036 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00004037 int res = execute_storeindex(iptr, ectx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004038
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00004039 if (res == FAIL)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004040 goto on_error;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00004041 if (res == NOTDONE)
4042 goto theend;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004043 }
4044 break;
4045
Bram Moolenaarea5c8982022-02-17 14:42:02 +00004046 // store value in list or blob range
Bram Moolenaar68452172021-04-12 21:21:02 +02004047 case ISN_STORERANGE:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00004048 if (execute_storerange(iptr, ectx) == FAIL)
4049 goto on_error;
Bram Moolenaar68452172021-04-12 21:21:02 +02004050 break;
4051
Bram Moolenaard505d172022-12-18 21:42:55 +00004052 case ISN_LOAD_CLASSMEMBER:
4053 {
4054 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
4055 goto theend;
4056 classmember_T *cm = &iptr->isn_arg.classmember;
Bram Moolenaar4e2406c2023-06-24 19:22:21 +01004057 copy_tv(cm->cm_class->class_members_tv + cm->cm_idx,
4058 STACK_TV_BOT(0));
Bram Moolenaard505d172022-12-18 21:42:55 +00004059 ++ectx->ec_stack.ga_len;
4060 }
4061 break;
4062
4063 case ISN_STORE_CLASSMEMBER:
4064 {
4065 classmember_T *cm = &iptr->isn_arg.classmember;
4066 tv = &cm->cm_class->class_members_tv[cm->cm_idx];
4067 clear_tv(tv);
4068 *tv = *STACK_TV_BOT(-1);
4069 --ectx->ec_stack.ga_len;
4070 }
4071 break;
4072
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01004073 // Load or store variable or argument from outer scope.
Bram Moolenaar0186e582021-01-10 18:33:11 +01004074 case ISN_LOADOUTER:
4075 case ISN_STOREOUTER:
4076 {
4077 int depth = iptr->isn_arg.outer.outer_depth;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004078 outer_T *outer = ectx->ec_outer_ref == NULL ? NULL
4079 : ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +01004080
4081 while (depth > 1 && outer != NULL)
4082 {
4083 outer = outer->out_up;
4084 --depth;
4085 }
4086 if (outer == NULL)
4087 {
4088 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar69c76172021-12-02 16:38:52 +00004089 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx
4090 || ectx->ec_outer_ref == NULL)
4091 // Possibly :def function called from legacy
4092 // context.
4093 emsg(_(e_closure_called_from_invalid_context));
4094 else
4095 iemsg("LOADOUTER depth more than scope levels");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004096 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01004097 }
Bram Moolenaarcc341812022-09-19 15:54:34 +01004098 if (depth < 0)
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01004099 // Variable declared in loop. May be copied if the
4100 // loop block has already ended.
Bram Moolenaarcc341812022-09-19 15:54:34 +01004101 tv = ((typval_T *)outer->out_loop[-depth - 1]
4102 .stack->ga_data)
4103 + outer->out_loop[-depth - 1].var_idx
4104 + iptr->isn_arg.outer.outer_idx;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004105 else
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01004106 // Variable declared in a function. May be copied if
4107 // the function has already returned.
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004108 tv = ((typval_T *)outer->out_stack->ga_data)
4109 + outer->out_frame_idx + STACK_FRAME_SIZE
4110 + iptr->isn_arg.outer.outer_idx;
Bram Moolenaar0186e582021-01-10 18:33:11 +01004111 if (iptr->isn_type == ISN_LOADOUTER)
4112 {
Bram Moolenaar35578162021-08-02 19:10:38 +02004113 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004114 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01004115 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02004116 ++ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01004117 }
4118 else
4119 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004120 --ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01004121 clear_tv(tv);
4122 *tv = *STACK_TV_BOT(0);
4123 }
4124 }
4125 break;
4126
Bram Moolenaar752fc692021-01-04 21:57:11 +01004127 // unlet item in list or dict variable
4128 case ISN_UNLETINDEX:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00004129 if (execute_unletindex(iptr, ectx) == FAIL)
4130 goto on_error;
Bram Moolenaar752fc692021-01-04 21:57:11 +01004131 break;
4132
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01004133 // unlet range of items in list variable
4134 case ISN_UNLETRANGE:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00004135 if (execute_unletrange(iptr, ectx) == FAIL)
4136 goto on_error;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01004137 break;
4138
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004139 // push constant
4140 case ISN_PUSHNR:
4141 case ISN_PUSHBOOL:
4142 case ISN_PUSHSPEC:
4143 case ISN_PUSHF:
4144 case ISN_PUSHS:
4145 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004146 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004147 case ISN_PUSHCHANNEL:
4148 case ISN_PUSHJOB:
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00004149 case ISN_PUSHOBJ:
4150 case ISN_PUSHCLASS:
Bram Moolenaar35578162021-08-02 19:10:38 +02004151 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004152 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004153 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02004154 tv->v_lock = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004155 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004156 switch (iptr->isn_type)
4157 {
4158 case ISN_PUSHNR:
4159 tv->v_type = VAR_NUMBER;
4160 tv->vval.v_number = iptr->isn_arg.number;
4161 break;
4162 case ISN_PUSHBOOL:
4163 tv->v_type = VAR_BOOL;
4164 tv->vval.v_number = iptr->isn_arg.number;
4165 break;
4166 case ISN_PUSHSPEC:
4167 tv->v_type = VAR_SPECIAL;
4168 tv->vval.v_number = iptr->isn_arg.number;
4169 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004170 case ISN_PUSHF:
4171 tv->v_type = VAR_FLOAT;
4172 tv->vval.v_float = iptr->isn_arg.fnumber;
4173 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004174 case ISN_PUSHBLOB:
4175 blob_copy(iptr->isn_arg.blob, tv);
4176 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004177 case ISN_PUSHFUNC:
4178 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01004179 if (iptr->isn_arg.string == NULL)
4180 tv->vval.v_string = NULL;
4181 else
4182 tv->vval.v_string =
4183 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004184 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004185 case ISN_PUSHCHANNEL:
4186#ifdef FEAT_JOB_CHANNEL
4187 tv->v_type = VAR_CHANNEL;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00004188 tv->vval.v_channel = NULL;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004189#endif
4190 break;
4191 case ISN_PUSHJOB:
4192#ifdef FEAT_JOB_CHANNEL
4193 tv->v_type = VAR_JOB;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00004194 tv->vval.v_job = NULL;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004195#endif
4196 break;
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00004197 case ISN_PUSHOBJ:
4198 tv->v_type = VAR_OBJECT;
4199 tv->vval.v_object = NULL;
4200 break;
4201 case ISN_PUSHCLASS:
4202 tv->v_type = VAR_CLASS;
Bram Moolenaar30a84472023-02-27 08:07:14 +00004203 tv->vval.v_class = iptr->isn_arg.classarg;
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00004204 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004205 default:
4206 tv->v_type = VAR_STRING;
Bram Moolenaarf8691002022-03-10 12:20:53 +00004207 tv->vval.v_string = iptr->isn_arg.string == NULL
4208 ? NULL : vim_strsave(iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004209 }
4210 break;
4211
Bram Moolenaar06b77222022-01-25 15:51:56 +00004212 case ISN_AUTOLOAD:
4213 {
4214 char_u *name = iptr->isn_arg.string;
4215
4216 (void)script_autoload(name, FALSE);
4217 if (find_func(name, TRUE))
4218 {
4219 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
4220 goto theend;
4221 tv = STACK_TV_BOT(0);
4222 tv->v_lock = 0;
4223 ++ectx->ec_stack.ga_len;
4224 tv->v_type = VAR_FUNC;
4225 tv->vval.v_string = vim_strsave(name);
4226 }
4227 else
4228 {
4229 int res = load_namespace_var(ectx, ISN_LOADG, iptr);
4230
4231 if (res == NOTDONE)
4232 goto theend;
4233 if (res == FAIL)
4234 goto on_error;
4235 }
4236 }
4237 break;
4238
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004239 case ISN_UNLET:
4240 if (do_unlet(iptr->isn_arg.unlet.ul_name,
4241 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02004242 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004243 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02004244 case ISN_UNLETENV:
LemonBoy77142312022-04-15 20:50:46 +01004245 vim_unsetenv_ext(iptr->isn_arg.unlet.ul_name);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02004246 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004247
Bram Moolenaaraacc9662021-08-13 19:40:51 +02004248 case ISN_LOCKUNLOCK:
4249 {
Ernie Raelee865f32023-09-29 19:53:55 +02004250#ifdef LOG_LOCKVAR
4251 ch_log(NULL, "LKVAR: execute INS_LOCKUNLOCK isn_arg %s",
4252 iptr->isn_arg.string);
4253#endif
Ernie Rael4c8da022023-10-11 21:35:11 +02004254 lval_root_T *lval_root_save = lval_root;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02004255
4256 // Stack has the local variable, argument the whole :lock
4257 // or :unlock command, like ISN_EXEC.
4258 --ectx->ec_stack.ga_len;
Ernie Rael4c8da022023-10-11 21:35:11 +02004259 lval_root_T root = { .lr_tv = STACK_TV_BOT(0),
4260 .lr_cl_exec = iptr->isn_arg.lockunlock.lu_cl_exec,
4261 .lr_is_arg = iptr->isn_arg.lockunlock.lu_is_arg };
Ernie Rael64885642023-10-04 20:16:22 +02004262 lval_root = &root;
Ernie Rael4c8da022023-10-11 21:35:11 +02004263 int res = exec_command(iptr,
Ernie Rael64885642023-10-04 20:16:22 +02004264 iptr->isn_arg.lockunlock.lu_string);
4265 clear_tv(root.lr_tv);
Bram Moolenaaraacc9662021-08-13 19:40:51 +02004266 lval_root = lval_root_save;
4267 if (res == FAIL)
4268 goto on_error;
4269 }
4270 break;
4271
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02004272 case ISN_LOCKCONST:
4273 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
4274 break;
4275
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004276 // create a list from items on the stack; uses a single allocation
4277 // for the list header and the items
4278 case ISN_NEWLIST:
Bram Moolenaar4c137212021-04-19 16:48:48 +02004279 if (exe_newlist(iptr->isn_arg.number, ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004280 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004281 break;
4282
4283 // create a dict from items on the stack
4284 case ISN_NEWDICT:
4285 {
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01004286 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004287
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01004288 SOURCING_LNUM = iptr->isn_lnum;
4289 res = exe_newdict(iptr->isn_arg.number, ectx);
4290 if (res == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004291 goto theend;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01004292 if (res == MAYBE)
4293 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004294 }
4295 break;
4296
LemonBoy372bcce2022-04-25 12:43:20 +01004297 case ISN_CONCAT:
4298 if (exe_concat(iptr->isn_arg.number, ectx) == FAIL)
4299 goto theend;
4300 break;
4301
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004302 // create a partial with NULL value
4303 case ISN_NEWPARTIAL:
4304 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
4305 goto theend;
4306 ++ectx->ec_stack.ga_len;
4307 tv = STACK_TV_BOT(-1);
4308 tv->v_type = VAR_PARTIAL;
4309 tv->v_lock = 0;
4310 tv->vval.v_partial = NULL;
4311 break;
4312
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004313 // call a :def function
4314 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02004315 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01004316 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
4317 NULL,
4318 iptr->isn_arg.dfunc.cdf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02004319 ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02004320 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004321 break;
4322
Bram Moolenaard0200c82023-01-28 15:19:40 +00004323 // call a method on an interface
4324 case ISN_METHODCALL:
4325 {
Bram Moolenaar094cf9f2023-02-10 15:52:25 +00004326 cmfunc_T *mfunc = iptr->isn_arg.mfunc;
4327
Bram Moolenaard0200c82023-01-28 15:19:40 +00004328 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar094cf9f2023-02-10 15:52:25 +00004329 tv = STACK_TV_BOT(-1 - mfunc->cmf_argcount);
Bram Moolenaard0200c82023-01-28 15:19:40 +00004330 if (tv->v_type != VAR_OBJECT)
4331 {
4332 object_required_error(tv);
4333 goto on_error;
4334 }
4335 object_T *obj = tv->vval.v_object;
4336 class_T *cl = obj->obj_class;
4337
4338 // convert the interface index to the object index
Bram Moolenaard0200c82023-01-28 15:19:40 +00004339 int idx = object_index_from_itf_index(mfunc->cmf_itf,
Yegappan Lakshmanan5a05d372023-09-29 19:43:11 +02004340 TRUE, mfunc->cmf_idx, cl);
Bram Moolenaard0200c82023-01-28 15:19:40 +00004341
4342 if (call_ufunc(cl->class_obj_methods[idx], NULL,
4343 mfunc->cmf_argcount, ectx, NULL, NULL) == FAIL)
4344 goto on_error;
4345 }
4346 break;
4347
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004348 // call a builtin function
4349 case ISN_BCALL:
4350 SOURCING_LNUM = iptr->isn_lnum;
4351 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
4352 iptr->isn_arg.bfunc.cbf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02004353 ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004354 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004355 break;
4356
4357 // call a funcref or partial
4358 case ISN_PCALL:
4359 {
4360 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
4361 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02004362 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004363
4364 SOURCING_LNUM = iptr->isn_lnum;
4365 if (pfunc->cpf_top)
4366 {
4367 // funcref is above the arguments
4368 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
4369 }
4370 else
4371 {
4372 // Get the funcref from the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004373 --ectx->ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02004374 partial_tv = *STACK_TV_BOT(0);
4375 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004376 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004377 r = call_partial(tv, pfunc->cpf_argcount, ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02004378 if (tv == &partial_tv)
4379 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004380 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004381 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004382 }
4383 break;
4384
Bram Moolenaarbd5da372020-03-31 23:13:10 +02004385 case ISN_PCALL_END:
4386 // PCALL finished, arguments have been consumed and replaced by
4387 // the return value. Now clear the funcref from the stack,
4388 // and move the return value in its place.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004389 --ectx->ec_stack.ga_len;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02004390 clear_tv(STACK_TV_BOT(-1));
4391 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
4392 break;
4393
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004394 // call a user defined function or funcref/partial
4395 case ISN_UCALL:
4396 {
4397 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
4398
4399 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01004400 if (call_eval_func(cufunc->cuf_name, cufunc->cuf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02004401 ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004402 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004403 }
4404 break;
4405
Bram Moolenaar1d84f762022-09-03 21:35:53 +01004406 // :defer func(arg)
4407 case ISN_DEFER:
Bram Moolenaar806a2732022-09-04 15:40:36 +01004408 if (defer_command(iptr->isn_arg.defer.defer_var_idx,
Bram Moolenaar1d84f762022-09-03 21:35:53 +01004409 iptr->isn_arg.defer.defer_argcount, ectx) == FAIL)
4410 goto on_error;
4411 break;
4412
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004413 // Return from a :def function call without a value.
4414 // Return from a constructor.
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02004415 case ISN_RETURN_VOID:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004416 case ISN_RETURN_OBJECT:
Bram Moolenaar35578162021-08-02 19:10:38 +02004417 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004418 goto theend;
Bram Moolenaar299f3032021-01-08 20:53:09 +01004419 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004420 ++ectx->ec_stack.ga_len;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004421 if (iptr->isn_type == ISN_RETURN_VOID)
4422 {
4423 tv->v_type = VAR_VOID;
4424 tv->vval.v_number = 0;
4425 tv->v_lock = 0;
4426 }
4427 else
4428 {
4429 *tv = *STACK_TV_VAR(0);
4430 ++tv->vval.v_object->obj_refcount;
4431 }
Bram Moolenaar299f3032021-01-08 20:53:09 +01004432 // FALLTHROUGH
4433
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02004434 // return from a :def function call with what is on the stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004435 case ISN_RETURN:
4436 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004437 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01004438 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01004439
4440 if (trystack->ga_len > 0)
4441 trycmd = ((trycmd_T *)trystack->ga_data)
4442 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004443 if (trycmd != NULL
Bram Moolenaar4c137212021-04-19 16:48:48 +02004444 && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004445 {
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01004446 // jump to ":finally" or ":endtry"
4447 if (trycmd->tcd_finally_idx != 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004448 ectx->ec_iidx = trycmd->tcd_finally_idx;
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01004449 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02004450 ectx->ec_iidx = trycmd->tcd_endtry_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004451 trycmd->tcd_return = TRUE;
4452 }
4453 else
Bram Moolenaard032f342020-07-18 18:13:02 +02004454 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004455 }
4456 break;
4457
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004458 // push a partial, a reference to a compiled function
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004459 case ISN_FUNCREF:
4460 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004461 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
4462 ufunc_T *ufunc;
4463 funcref_T *funcref = &iptr->isn_arg.funcref;
4464 funcref_extra_T *extra = funcref->fr_extra;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004465
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004466 if (pt == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004467 goto theend;
Bram Moolenaar35578162021-08-02 19:10:38 +02004468 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02004469 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004470 vim_free(pt);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004471 goto theend;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004472 }
Bram Moolenaar313e4722023-02-08 20:55:27 +00004473 if (extra != NULL && extra->fre_class != NULL)
4474 {
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +02004475 class_T *cl;
4476 if (extra->fre_object_method)
Bram Moolenaar313e4722023-02-08 20:55:27 +00004477 {
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +02004478 tv = STACK_TV_BOT(-1);
4479 if (tv->v_type != VAR_OBJECT)
4480 {
4481 object_required_error(tv);
4482 vim_free(pt);
4483 goto on_error;
4484 }
Bram Moolenaar313e4722023-02-08 20:55:27 +00004485
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +02004486 object_T *obj = tv->vval.v_object;
4487 cl = obj->obj_class;
4488 // drop the value from the stack
4489 clear_tv(tv);
4490 --ectx->ec_stack.ga_len;
4491
4492 pt->pt_obj = obj;
4493 ++obj->obj_refcount;
4494 }
4495 else
4496 cl = extra->fre_class;
4497
4498 if (extra->fre_object_method)
4499 {
4500 // object method
4501 // convert the interface index to the object index
4502 int idx =
4503 object_index_from_itf_index(extra->fre_class,
4504 TRUE, extra->fre_method_idx, cl);
4505 ufunc = cl->class_obj_methods[idx];
4506 }
4507 else
4508 {
4509 // class method
4510 ufunc =
4511 cl->class_class_functions[extra->fre_method_idx];
4512 }
Bram Moolenaar313e4722023-02-08 20:55:27 +00004513 }
4514 else if (extra == NULL || extra->fre_func_name == NULL)
Bram Moolenaar38453522021-11-28 22:00:12 +00004515 {
4516 dfunc_T *pt_dfunc = ((dfunc_T *)def_functions.ga_data)
4517 + funcref->fr_dfunc_idx;
4518
4519 ufunc = pt_dfunc->df_ufunc;
4520 }
4521 else
Bram Moolenaar313e4722023-02-08 20:55:27 +00004522 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004523 ufunc = find_func(extra->fre_func_name, FALSE);
Bram Moolenaar313e4722023-02-08 20:55:27 +00004524 }
Bram Moolenaar56acd1f2022-02-18 13:24:52 +00004525 if (ufunc == NULL)
4526 {
4527 SOURCING_LNUM = iptr->isn_lnum;
4528 iemsg("ufunc unexpectedly NULL for FUNCREF");
4529 goto theend;
4530 }
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004531 if (fill_partial_and_closure(pt, ufunc,
Bram Moolenaarcc341812022-09-19 15:54:34 +01004532 extra == NULL ? NULL : &extra->fre_loopvar_info,
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004533 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004534 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004535 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004536 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004537 tv->vval.v_partial = pt;
4538 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02004539 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004540 }
4541 break;
4542
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004543 // Create a global function from a lambda.
4544 case ISN_NEWFUNC:
4545 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004546 newfuncarg_T *arg = iptr->isn_arg.newfunc.nf_arg;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004547
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004548 if (copy_lambda_to_global_func(arg->nfa_lambda,
Bram Moolenaarcc341812022-09-19 15:54:34 +01004549 arg->nfa_global, &arg->nfa_loopvar_info,
4550 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004551 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004552 }
4553 break;
4554
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004555 // List functions
4556 case ISN_DEF:
4557 if (iptr->isn_arg.string == NULL)
4558 list_functions(NULL);
4559 else
4560 {
Bram Moolenaar14336722022-01-08 16:02:59 +00004561 exarg_T ea;
4562 garray_T lines_to_free;
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004563
4564 CLEAR_FIELD(ea);
4565 ea.cmd = ea.arg = iptr->isn_arg.string;
Bram Moolenaar14336722022-01-08 16:02:59 +00004566 ga_init2(&lines_to_free, sizeof(char_u *), 50);
Bram Moolenaard4a9b7f2023-05-23 14:48:42 +01004567 SOURCING_LNUM = iptr->isn_lnum;
h-eastb895b0f2023-09-24 15:46:31 +02004568 define_function(&ea, NULL, &lines_to_free, 0, NULL, 0);
Bram Moolenaar14336722022-01-08 16:02:59 +00004569 ga_clear_strings(&lines_to_free);
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004570 }
4571 break;
4572
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004573 // jump if a condition is met
4574 case ISN_JUMP:
4575 {
4576 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004577 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004578 int jump = TRUE;
4579
4580 if (when != JUMP_ALWAYS)
4581 {
4582 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004583 if (when == JUMP_IF_COND_FALSE
Bram Moolenaar13106602020-10-04 16:06:05 +02004584 || when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004585 || when == JUMP_IF_COND_TRUE)
4586 {
4587 SOURCING_LNUM = iptr->isn_lnum;
4588 jump = tv_get_bool_chk(tv, &error);
4589 if (error)
4590 goto on_error;
4591 }
4592 else
4593 jump = tv2bool(tv);
Bram Moolenaarf6ced982022-04-28 12:00:49 +01004594 if (when == JUMP_IF_FALSE || when == JUMP_IF_COND_FALSE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004595 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01004596 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004597 {
4598 // drop the value from the stack
4599 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004600 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004601 }
4602 }
4603 if (jump)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004604 ectx->ec_iidx = iptr->isn_arg.jump.jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004605 }
4606 break;
4607
Bram Moolenaarb46c0832022-09-15 17:19:37 +01004608 // "while": jump to end if a condition is false
4609 case ISN_WHILE:
4610 {
4611 int error = FALSE;
4612 int jump = TRUE;
4613
4614 tv = STACK_TV_BOT(-1);
4615 SOURCING_LNUM = iptr->isn_lnum;
4616 jump = !tv_get_bool_chk(tv, &error);
4617 if (error)
4618 goto on_error;
4619 // drop the value from the stack
4620 clear_tv(tv);
4621 --ectx->ec_stack.ga_len;
4622 if (jump)
4623 ectx->ec_iidx = iptr->isn_arg.whileloop.while_end;
4624
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01004625 // Store the current funcref count, may be used by
Bram Moolenaarcc341812022-09-19 15:54:34 +01004626 // ISN_ENDLOOP later
Bram Moolenaarb46c0832022-09-15 17:19:37 +01004627 tv = STACK_TV_VAR(
4628 iptr->isn_arg.whileloop.while_funcref_idx);
4629 tv->vval.v_number = ectx->ec_funcrefs.ga_len;
4630 }
4631 break;
4632
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02004633 // Jump if an argument with a default value was already set and not
4634 // v:none.
4635 case ISN_JUMP_IF_ARG_SET:
Bram Moolenaar65b0d162022-12-13 18:43:22 +00004636 case ISN_JUMP_IF_ARG_NOT_SET:
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02004637 tv = STACK_TV_VAR(iptr->isn_arg.jumparg.jump_arg_off);
Bram Moolenaar65b0d162022-12-13 18:43:22 +00004638 int arg_set = tv->v_type != VAR_UNKNOWN
4639 && !(tv->v_type == VAR_SPECIAL
4640 && tv->vval.v_number == VVAL_NONE);
4641 if (iptr->isn_type == ISN_JUMP_IF_ARG_SET ? arg_set : !arg_set)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004642 ectx->ec_iidx = iptr->isn_arg.jumparg.jump_where;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02004643 break;
4644
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004645 // top of a for loop
4646 case ISN_FOR:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00004647 if (execute_for(iptr, ectx) == FAIL)
4648 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004649 break;
4650
Bram Moolenaarb46c0832022-09-15 17:19:37 +01004651 // end of a for or while loop
4652 case ISN_ENDLOOP:
4653 if (execute_endloop(iptr, ectx) == FAIL)
4654 goto theend;
4655 break;
4656
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004657 // start of ":try" block
4658 case ISN_TRY:
4659 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01004660 trycmd_T *trycmd = NULL;
4661
Bram Moolenaar35578162021-08-02 19:10:38 +02004662 if (GA_GROW_FAILS(&ectx->ec_trystack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004663 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004664 trycmd = ((trycmd_T *)ectx->ec_trystack.ga_data)
4665 + ectx->ec_trystack.ga_len;
4666 ++ectx->ec_trystack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004667 ++trylevel;
Bram Moolenaar8d4be892021-02-13 18:33:02 +01004668 CLEAR_POINTER(trycmd);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004669 trycmd->tcd_frame_idx = ectx->ec_frame_idx;
4670 trycmd->tcd_stack_len = ectx->ec_stack.ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01004671 trycmd->tcd_catch_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00004672 iptr->isn_arg.tryref.try_ref->try_catch;
Bram Moolenaara91a7132021-03-25 21:12:15 +01004673 trycmd->tcd_finally_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00004674 iptr->isn_arg.tryref.try_ref->try_finally;
Bram Moolenaara91a7132021-03-25 21:12:15 +01004675 trycmd->tcd_endtry_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00004676 iptr->isn_arg.tryref.try_ref->try_endtry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004677 }
4678 break;
4679
4680 case ISN_PUSHEXC:
4681 if (current_exception == NULL)
4682 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004683 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004684 iemsg("Evaluating catch while current_exception is NULL");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004685 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004686 }
Bram Moolenaar35578162021-08-02 19:10:38 +02004687 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004688 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004689 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004690 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004691 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02004692 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004693 tv->vval.v_string = vim_strsave(
4694 (char_u *)current_exception->value);
4695 break;
4696
4697 case ISN_CATCH:
4698 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004699 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar06651632022-04-27 17:54:25 +01004700 trycmd_T *trycmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004701
Bram Moolenaar4c137212021-04-19 16:48:48 +02004702 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar06651632022-04-27 17:54:25 +01004703 trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004704 + trystack->ga_len - 1;
Bram Moolenaar06651632022-04-27 17:54:25 +01004705 trycmd->tcd_caught = TRUE;
4706 trycmd->tcd_did_throw = FALSE;
4707
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004708 did_emsg = got_int = did_throw = FALSE;
Bram Moolenaar1430cee2021-01-17 19:20:32 +01004709 force_abort = need_rethrow = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004710 catch_exception(current_exception);
4711 }
4712 break;
4713
Bram Moolenaarc150c092021-02-13 15:02:46 +01004714 case ISN_TRYCONT:
4715 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004716 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004717 trycont_T *trycont = &iptr->isn_arg.trycont;
4718 int i;
4719 trycmd_T *trycmd;
4720 int iidx = trycont->tct_where;
4721
4722 if (trystack->ga_len < trycont->tct_levels)
4723 {
4724 siemsg("TRYCONT: expected %d levels, found %d",
4725 trycont->tct_levels, trystack->ga_len);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004726 goto theend;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004727 }
4728 // Make :endtry jump to any outer try block and the last
4729 // :endtry inside the loop to the loop start.
4730 for (i = trycont->tct_levels; i > 0; --i)
4731 {
4732 trycmd = ((trycmd_T *)trystack->ga_data)
4733 + trystack->ga_len - i;
Bram Moolenaar2e34c342021-03-14 12:13:33 +01004734 // Add one to tcd_cont to be able to jump to
4735 // instruction with index zero.
4736 trycmd->tcd_cont = iidx + 1;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01004737 iidx = trycmd->tcd_finally_idx == 0
4738 ? trycmd->tcd_endtry_idx : trycmd->tcd_finally_idx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004739 }
4740 // jump to :finally or :endtry of current try statement
Bram Moolenaar4c137212021-04-19 16:48:48 +02004741 ectx->ec_iidx = iidx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004742 }
4743 break;
4744
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01004745 case ISN_FINALLY:
4746 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004747 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01004748 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
4749 + trystack->ga_len - 1;
4750
4751 // Reset the index to avoid a return statement jumps here
4752 // again.
4753 trycmd->tcd_finally_idx = 0;
4754 break;
4755 }
4756
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004757 // end of ":try" block
4758 case ISN_ENDTRY:
4759 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004760 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar06651632022-04-27 17:54:25 +01004761 trycmd_T *trycmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004762
Bram Moolenaar06651632022-04-27 17:54:25 +01004763 --trystack->ga_len;
4764 --trylevel;
4765 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len;
4766 if (trycmd->tcd_did_throw)
4767 did_throw = TRUE;
4768 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004769 {
Bram Moolenaar06651632022-04-27 17:54:25 +01004770 // discard the exception
4771 if (caught_stack == current_exception)
4772 caught_stack = caught_stack->caught;
4773 discard_current_exception();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004774 }
Bram Moolenaar06651632022-04-27 17:54:25 +01004775
4776 if (trycmd->tcd_return)
4777 goto func_return;
4778
4779 while (ectx->ec_stack.ga_len > trycmd->tcd_stack_len)
4780 {
4781 --ectx->ec_stack.ga_len;
4782 clear_tv(STACK_TV_BOT(0));
4783 }
4784 if (trycmd->tcd_cont != 0)
4785 // handling :continue: jump to outer try block or
4786 // start of the loop
4787 ectx->ec_iidx = trycmd->tcd_cont - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004788 }
4789 break;
4790
4791 case ISN_THROW:
Bram Moolenaar8f81b222021-01-14 21:47:06 +01004792 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004793 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02004794
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004795 if (trystack->ga_len == 0 && trylevel == 0 && emsg_silent)
4796 {
Bram Moolenaar3ef2e412023-04-30 18:50:48 +01004797 // Throwing an exception while using "silent!" causes
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004798 // the function to abort but not display an error.
4799 tv = STACK_TV_BOT(-1);
4800 clear_tv(tv);
4801 tv->v_type = VAR_NUMBER;
4802 tv->vval.v_number = 0;
4803 goto done;
4804 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004805 --ectx->ec_stack.ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004806 tv = STACK_TV_BOT(0);
4807 if (tv->vval.v_string == NULL
4808 || *skipwhite(tv->vval.v_string) == NUL)
4809 {
4810 vim_free(tv->vval.v_string);
4811 SOURCING_LNUM = iptr->isn_lnum;
4812 emsg(_(e_throw_with_empty_string));
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004813 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004814 }
4815
4816 // Inside a "catch" we need to first discard the caught
4817 // exception.
4818 if (trystack->ga_len > 0)
4819 {
4820 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
4821 + trystack->ga_len - 1;
4822 if (trycmd->tcd_caught && current_exception != NULL)
4823 {
4824 // discard the exception
4825 if (caught_stack == current_exception)
4826 caught_stack = caught_stack->caught;
4827 discard_current_exception();
4828 trycmd->tcd_caught = FALSE;
4829 }
4830 }
4831
Bram Moolenaar90a57162022-02-12 14:23:17 +00004832 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004833 if (throw_exception(tv->vval.v_string, ET_USER, NULL)
4834 == FAIL)
4835 {
4836 vim_free(tv->vval.v_string);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004837 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004838 }
4839 did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004840 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004841 break;
4842
4843 // compare with special values
4844 case ISN_COMPAREBOOL:
4845 case ISN_COMPARESPECIAL:
4846 {
4847 typval_T *tv1 = STACK_TV_BOT(-2);
4848 typval_T *tv2 = STACK_TV_BOT(-1);
4849 varnumber_T arg1 = tv1->vval.v_number;
4850 varnumber_T arg2 = tv2->vval.v_number;
4851 int res;
4852
Bram Moolenaar06651632022-04-27 17:54:25 +01004853 if (iptr->isn_arg.op.op_type == EXPR_EQUAL)
4854 res = arg1 == arg2;
4855 else
4856 res = arg1 != arg2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004857
Bram Moolenaar4c137212021-04-19 16:48:48 +02004858 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004859 tv1->v_type = VAR_BOOL;
4860 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4861 }
4862 break;
4863
Bram Moolenaar7a222242022-03-01 19:23:24 +00004864 case ISN_COMPARENULL:
4865 {
4866 typval_T *tv1 = STACK_TV_BOT(-2);
4867 typval_T *tv2 = STACK_TV_BOT(-1);
4868 int res;
4869
4870 res = typval_compare_null(tv1, tv2);
4871 if (res == MAYBE)
4872 goto on_error;
4873 if (iptr->isn_arg.op.op_type == EXPR_NEQUAL)
4874 res = !res;
4875 clear_tv(tv1);
4876 clear_tv(tv2);
4877 --ectx->ec_stack.ga_len;
4878 tv1->v_type = VAR_BOOL;
4879 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4880 }
4881 break;
4882
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004883 // Operation with two number arguments
4884 case ISN_OPNR:
4885 case ISN_COMPARENR:
4886 {
4887 typval_T *tv1 = STACK_TV_BOT(-2);
4888 typval_T *tv2 = STACK_TV_BOT(-1);
4889 varnumber_T arg1 = tv1->vval.v_number;
4890 varnumber_T arg2 = tv2->vval.v_number;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004891 varnumber_T res = 0;
4892 int div_zero = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004893
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004894 if (iptr->isn_arg.op.op_type == EXPR_LSHIFT
4895 || iptr->isn_arg.op.op_type == EXPR_RSHIFT)
4896 {
4897 if (arg2 < 0)
4898 {
4899 SOURCING_LNUM = iptr->isn_lnum;
dundargocc57b5bc2022-11-02 13:30:51 +00004900 emsg(_(e_bitshift_ops_must_be_positive));
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004901 goto on_error;
4902 }
4903 }
4904
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004905 switch (iptr->isn_arg.op.op_type)
4906 {
4907 case EXPR_MULT: res = arg1 * arg2; break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004908 case EXPR_DIV: if (arg2 == 0)
4909 div_zero = TRUE;
4910 else
4911 res = arg1 / arg2;
4912 break;
4913 case EXPR_REM: if (arg2 == 0)
4914 div_zero = TRUE;
4915 else
4916 res = arg1 % arg2;
4917 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004918 case EXPR_SUB: res = arg1 - arg2; break;
4919 case EXPR_ADD: res = arg1 + arg2; break;
4920
4921 case EXPR_EQUAL: res = arg1 == arg2; break;
4922 case EXPR_NEQUAL: res = arg1 != arg2; break;
4923 case EXPR_GREATER: res = arg1 > arg2; break;
4924 case EXPR_GEQUAL: res = arg1 >= arg2; break;
4925 case EXPR_SMALLER: res = arg1 < arg2; break;
4926 case EXPR_SEQUAL: res = arg1 <= arg2; break;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004927 case EXPR_LSHIFT: if (arg2 > MAX_LSHIFT_BITS)
4928 res = 0;
4929 else
Bram Moolenaar68e64d22022-05-22 22:07:52 +01004930 res = (uvarnumber_T)arg1 << arg2;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004931 break;
4932 case EXPR_RSHIFT: if (arg2 > MAX_LSHIFT_BITS)
4933 res = 0;
4934 else
Bram Moolenaar338bf582022-05-22 20:16:32 +01004935 res = (uvarnumber_T)arg1 >> arg2;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004936 break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004937 default: break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004938 }
4939
Bram Moolenaar4c137212021-04-19 16:48:48 +02004940 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004941 if (iptr->isn_type == ISN_COMPARENR)
4942 {
4943 tv1->v_type = VAR_BOOL;
4944 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4945 }
4946 else
4947 tv1->vval.v_number = res;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004948 if (div_zero)
4949 {
4950 SOURCING_LNUM = iptr->isn_lnum;
4951 emsg(_(e_divide_by_zero));
4952 goto on_error;
4953 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004954 }
4955 break;
4956
4957 // Computation with two float arguments
4958 case ISN_OPFLOAT:
4959 case ISN_COMPAREFLOAT:
4960 {
4961 typval_T *tv1 = STACK_TV_BOT(-2);
4962 typval_T *tv2 = STACK_TV_BOT(-1);
4963 float_T arg1 = tv1->vval.v_float;
4964 float_T arg2 = tv2->vval.v_float;
4965 float_T res = 0;
4966 int cmp = FALSE;
4967
4968 switch (iptr->isn_arg.op.op_type)
4969 {
4970 case EXPR_MULT: res = arg1 * arg2; break;
4971 case EXPR_DIV: res = arg1 / arg2; break;
4972 case EXPR_SUB: res = arg1 - arg2; break;
4973 case EXPR_ADD: res = arg1 + arg2; break;
4974
4975 case EXPR_EQUAL: cmp = arg1 == arg2; break;
4976 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
4977 case EXPR_GREATER: cmp = arg1 > arg2; break;
4978 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
4979 case EXPR_SMALLER: cmp = arg1 < arg2; break;
4980 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
4981 default: cmp = 0; break;
4982 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004983 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004984 if (iptr->isn_type == ISN_COMPAREFLOAT)
4985 {
4986 tv1->v_type = VAR_BOOL;
4987 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
4988 }
4989 else
4990 tv1->vval.v_float = res;
4991 }
4992 break;
4993
4994 case ISN_COMPARELIST:
Bram Moolenaar265f8112021-12-19 12:33:05 +00004995 case ISN_COMPAREDICT:
4996 case ISN_COMPAREFUNC:
4997 case ISN_COMPARESTRING:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004998 case ISN_COMPAREBLOB:
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00004999 case ISN_COMPARECLASS:
5000 case ISN_COMPAREOBJECT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005001 {
5002 typval_T *tv1 = STACK_TV_BOT(-2);
5003 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar265f8112021-12-19 12:33:05 +00005004 exprtype_T exprtype = iptr->isn_arg.op.op_type;
5005 int ic = iptr->isn_arg.op.op_ic;
5006 int res = FALSE;
5007 int status = OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005008
Bram Moolenaar265f8112021-12-19 12:33:05 +00005009 SOURCING_LNUM = iptr->isn_lnum;
5010 if (iptr->isn_type == ISN_COMPARELIST)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005011 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00005012 status = typval_compare_list(tv1, tv2,
5013 exprtype, ic, &res);
5014 }
5015 else if (iptr->isn_type == ISN_COMPAREDICT)
5016 {
5017 status = typval_compare_dict(tv1, tv2,
5018 exprtype, ic, &res);
5019 }
5020 else if (iptr->isn_type == ISN_COMPAREFUNC)
5021 {
5022 status = typval_compare_func(tv1, tv2,
5023 exprtype, ic, &res);
5024 }
5025 else if (iptr->isn_type == ISN_COMPARESTRING)
5026 {
5027 status = typval_compare_string(tv1, tv2,
5028 exprtype, ic, &res);
5029 }
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00005030 else if (iptr->isn_type == ISN_COMPAREBLOB)
Bram Moolenaar265f8112021-12-19 12:33:05 +00005031 {
5032 status = typval_compare_blob(tv1, tv2, exprtype, &res);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005033 }
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00005034 else if (iptr->isn_type == ISN_COMPARECLASS)
5035 {
Bram Moolenaar03ff0c62023-01-02 20:38:01 +00005036 status = typval_compare_class(tv1, tv2,
5037 exprtype, FALSE, &res);
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00005038 }
5039 else // ISN_COMPAREOBJECT
5040 {
5041 status = typval_compare_object(tv1, tv2,
Bram Moolenaar03ff0c62023-01-02 20:38:01 +00005042 exprtype, FALSE, &res);
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00005043 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005044 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005045 clear_tv(tv1);
5046 clear_tv(tv2);
5047 tv1->v_type = VAR_BOOL;
Bram Moolenaar265f8112021-12-19 12:33:05 +00005048 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
5049 if (status == FAIL)
5050 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005051 }
5052 break;
5053
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005054 case ISN_COMPAREANY:
5055 {
5056 typval_T *tv1 = STACK_TV_BOT(-2);
5057 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar657137c2021-01-09 15:45:23 +01005058 exprtype_T exprtype = iptr->isn_arg.op.op_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005059 int ic = iptr->isn_arg.op.op_ic;
Bram Moolenaar265f8112021-12-19 12:33:05 +00005060 int status;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005061
Bram Moolenaareb26f432020-09-14 16:50:05 +02005062 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar265f8112021-12-19 12:33:05 +00005063 status = typval_compare(tv1, tv2, exprtype, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005064 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005065 --ectx->ec_stack.ga_len;
Bram Moolenaar265f8112021-12-19 12:33:05 +00005066 if (status == FAIL)
5067 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005068 }
5069 break;
5070
5071 case ISN_ADDLIST:
5072 case ISN_ADDBLOB:
5073 {
5074 typval_T *tv1 = STACK_TV_BOT(-2);
5075 typval_T *tv2 = STACK_TV_BOT(-1);
5076
Bram Moolenaar1dcae592020-10-19 19:02:42 +02005077 // add two lists or blobs
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005078 if (iptr->isn_type == ISN_ADDLIST)
Bram Moolenaar07802042021-09-09 23:01:14 +02005079 {
5080 if (iptr->isn_arg.op.op_type == EXPR_APPEND
5081 && tv1->vval.v_list != NULL)
5082 list_extend(tv1->vval.v_list, tv2->vval.v_list,
5083 NULL);
5084 else
5085 eval_addlist(tv1, tv2);
5086 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005087 else
5088 eval_addblob(tv1, tv2);
5089 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005090 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005091 }
5092 break;
5093
Bram Moolenaar1dcae592020-10-19 19:02:42 +02005094 case ISN_LISTAPPEND:
5095 {
5096 typval_T *tv1 = STACK_TV_BOT(-2);
5097 typval_T *tv2 = STACK_TV_BOT(-1);
5098 list_T *l = tv1->vval.v_list;
5099
5100 // add an item to a list
Bram Moolenaar1f4a3452022-01-01 18:29:21 +00005101 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02005102 if (l == NULL)
5103 {
Bram Moolenaar1dcae592020-10-19 19:02:42 +02005104 emsg(_(e_cannot_add_to_null_list));
5105 goto on_error;
5106 }
Bram Moolenaar1f4a3452022-01-01 18:29:21 +00005107 if (value_check_lock(l->lv_lock, NULL, FALSE))
5108 goto on_error;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02005109 if (list_append_tv(l, tv2) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005110 goto theend;
Bram Moolenaar955347c2020-10-19 23:01:46 +02005111 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005112 --ectx->ec_stack.ga_len;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02005113 }
5114 break;
5115
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02005116 case ISN_BLOBAPPEND:
5117 {
5118 typval_T *tv1 = STACK_TV_BOT(-2);
5119 typval_T *tv2 = STACK_TV_BOT(-1);
5120 blob_T *b = tv1->vval.v_blob;
5121 int error = FALSE;
5122 varnumber_T n;
5123
5124 // add a number to a blob
5125 if (b == NULL)
5126 {
5127 SOURCING_LNUM = iptr->isn_lnum;
5128 emsg(_(e_cannot_add_to_null_blob));
5129 goto on_error;
5130 }
5131 n = tv_get_number_chk(tv2, &error);
5132 if (error)
5133 goto on_error;
5134 ga_append(&b->bv_ga, (int)n);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005135 --ectx->ec_stack.ga_len;
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02005136 }
5137 break;
5138
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005139 // Computation with two arguments of unknown type
5140 case ISN_OPANY:
5141 {
5142 typval_T *tv1 = STACK_TV_BOT(-2);
5143 typval_T *tv2 = STACK_TV_BOT(-1);
5144 varnumber_T n1, n2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005145 float_T f1 = 0, f2 = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005146 int error = FALSE;
5147
5148 if (iptr->isn_arg.op.op_type == EXPR_ADD)
5149 {
5150 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
5151 {
5152 eval_addlist(tv1, tv2);
5153 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005154 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005155 break;
5156 }
5157 else if (tv1->v_type == VAR_BLOB
5158 && tv2->v_type == VAR_BLOB)
5159 {
5160 eval_addblob(tv1, tv2);
5161 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005162 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005163 break;
5164 }
5165 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005166 if (tv1->v_type == VAR_FLOAT)
5167 {
5168 f1 = tv1->vval.v_float;
5169 n1 = 0;
5170 }
5171 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005172 {
Bram Moolenaarf665e972020-12-05 19:17:16 +01005173 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005174 n1 = tv_get_number_chk(tv1, &error);
5175 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02005176 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005177 if (tv2->v_type == VAR_FLOAT)
5178 f1 = n1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005179 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005180 if (tv2->v_type == VAR_FLOAT)
5181 {
5182 f2 = tv2->vval.v_float;
5183 n2 = 0;
5184 }
5185 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005186 {
5187 n2 = tv_get_number_chk(tv2, &error);
5188 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02005189 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005190 if (tv1->v_type == VAR_FLOAT)
5191 f2 = n2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005192 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005193 // if there is a float on either side the result is a float
5194 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
5195 {
5196 switch (iptr->isn_arg.op.op_type)
5197 {
5198 case EXPR_MULT: f1 = f1 * f2; break;
5199 case EXPR_DIV: f1 = f1 / f2; break;
5200 case EXPR_SUB: f1 = f1 - f2; break;
5201 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005202 default: SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005203 emsg(_(e_cannot_use_percent_with_float));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02005204 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005205 }
5206 clear_tv(tv1);
5207 clear_tv(tv2);
5208 tv1->v_type = VAR_FLOAT;
5209 tv1->vval.v_float = f1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005210 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005211 }
5212 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005213 {
Bram Moolenaarb1f28572021-01-21 13:03:20 +01005214 int failed = FALSE;
5215
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005216 switch (iptr->isn_arg.op.op_type)
5217 {
5218 case EXPR_MULT: n1 = n1 * n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01005219 case EXPR_DIV: n1 = num_divide(n1, n2, &failed);
5220 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01005221 goto on_error;
5222 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005223 case EXPR_SUB: n1 = n1 - n2; break;
5224 case EXPR_ADD: n1 = n1 + n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01005225 default: n1 = num_modulus(n1, n2, &failed);
5226 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01005227 goto on_error;
5228 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005229 }
5230 clear_tv(tv1);
5231 clear_tv(tv2);
5232 tv1->v_type = VAR_NUMBER;
5233 tv1->vval.v_number = n1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005234 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005235 }
5236 }
5237 break;
5238
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005239 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005240 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005241 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005242 int is_slice = iptr->isn_type == ISN_STRSLICE;
5243 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005244 char_u *res;
5245
5246 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005247 // string slice: string is at stack-3, first index at
5248 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005249 if (is_slice)
5250 {
5251 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005252 n1 = tv->vval.v_number;
5253 }
5254
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005255 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005256 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005257
Bram Moolenaar4c137212021-04-19 16:48:48 +02005258 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005259 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005260 if (is_slice)
5261 // Slice: Select the characters from the string
Bram Moolenaar6601b622021-01-13 21:47:15 +01005262 res = string_slice(tv->vval.v_string, n1, n2, FALSE);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005263 else
5264 // Index: The resulting variable is a string of a
Bram Moolenaar0289a092021-03-14 18:40:19 +01005265 // single character (including composing characters).
5266 // If the index is too big or negative the result is
5267 // empty.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005268 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005269 vim_free(tv->vval.v_string);
5270 tv->vval.v_string = res;
5271 }
5272 break;
5273
5274 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02005275 case ISN_LISTSLICE:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02005276 case ISN_BLOBINDEX:
5277 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005278 {
Bram Moolenaarcfc30232021-04-11 20:26:34 +02005279 int is_slice = iptr->isn_type == ISN_LISTSLICE
5280 || iptr->isn_type == ISN_BLOBSLICE;
5281 int is_blob = iptr->isn_type == ISN_BLOBINDEX
5282 || iptr->isn_type == ISN_BLOBSLICE;
Bram Moolenaared591872020-08-15 22:14:53 +02005283 varnumber_T n1, n2;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02005284 typval_T *val_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005285
5286 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02005287 // list slice: list is at stack-3, indexes at stack-2 and
5288 // stack-1
Bram Moolenaarcfc30232021-04-11 20:26:34 +02005289 // Same for blob.
5290 val_tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005291
5292 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02005293 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005294 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02005295
5296 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005297 {
Bram Moolenaared591872020-08-15 22:14:53 +02005298 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02005299 n1 = tv->vval.v_number;
5300 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005301 }
Bram Moolenaared591872020-08-15 22:14:53 +02005302
Bram Moolenaar4c137212021-04-19 16:48:48 +02005303 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02005304 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02005305 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02005306 if (is_blob)
5307 {
5308 if (blob_slice_or_index(val_tv->vval.v_blob, is_slice,
5309 n1, n2, FALSE, tv) == FAIL)
5310 goto on_error;
5311 }
5312 else
5313 {
5314 if (list_slice_or_index(val_tv->vval.v_list, is_slice,
5315 n1, n2, FALSE, tv, TRUE) == FAIL)
5316 goto on_error;
5317 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005318 }
5319 break;
5320
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005321 case ISN_ANYINDEX:
5322 case ISN_ANYSLICE:
5323 {
5324 int is_slice = iptr->isn_type == ISN_ANYSLICE;
5325 typval_T *var1, *var2;
5326 int res;
5327
5328 // index: composite is at stack-2, index at stack-1
5329 // slice: composite is at stack-3, indexes at stack-2 and
5330 // stack-1
5331 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02005332 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005333 if (check_can_index(tv, TRUE, TRUE) == FAIL)
5334 goto on_error;
5335 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
5336 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
Bram Moolenaar6601b622021-01-13 21:47:15 +01005337 res = eval_index_inner(tv, is_slice, var1, var2,
5338 FALSE, NULL, -1, TRUE);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005339 clear_tv(var1);
5340 if (is_slice)
5341 clear_tv(var2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005342 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005343 if (res == FAIL)
5344 goto on_error;
5345 }
5346 break;
5347
Bram Moolenaar9af78762020-06-16 11:34:42 +02005348 case ISN_SLICE:
5349 {
5350 list_T *list;
5351 int count = iptr->isn_arg.number;
5352
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02005353 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02005354 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02005355 list = tv->vval.v_list;
5356
5357 // no error for short list, expect it to be checked earlier
5358 if (list != NULL && list->lv_len >= count)
5359 {
5360 list_T *newlist = list_slice(list,
5361 count, list->lv_len - 1);
5362
5363 if (newlist != NULL)
5364 {
5365 list_unref(list);
5366 tv->vval.v_list = newlist;
5367 ++newlist->lv_refcount;
5368 }
5369 }
5370 }
5371 break;
5372
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005373 case ISN_GETITEM:
5374 {
5375 listitem_T *li;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02005376 getitem_T *gi = &iptr->isn_arg.getitem;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005377
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02005378 // Get list item: list is at stack-1, push item.
5379 // List type and length is checked for when compiling.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02005380 tv = STACK_TV_BOT(-1 - gi->gi_with_op);
5381 li = list_find(tv->vval.v_list, gi->gi_index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005382
Bram Moolenaar35578162021-08-02 19:10:38 +02005383 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005384 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005385 ++ectx->ec_stack.ga_len;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005386 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
Bram Moolenaarf785aa12021-02-11 21:19:34 +01005387
5388 // Useful when used in unpack assignment. Reset at
5389 // ISN_DROP.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02005390 ectx->ec_where.wt_index = gi->gi_index + 1;
LemonBoyc5d27442023-08-19 13:02:35 +02005391 ectx->ec_where.wt_kind = WT_VARIABLE;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005392 }
5393 break;
5394
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005395 case ISN_MEMBER:
5396 {
5397 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005398 char_u *key;
5399 dictitem_T *di;
5400
5401 // dict member: dict is at stack-2, key at stack-1
5402 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02005403 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005404 dict = tv->vval.v_dict;
5405
5406 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02005407 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005408 key = tv->vval.v_string;
Bram Moolenaar086fc9a2020-10-30 18:33:02 +01005409 if (key == NULL)
5410 key = (char_u *)"";
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02005411
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005412 if ((di = dict_find(dict, key, -1)) == NULL)
5413 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005414 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00005415 semsg(_(e_key_not_present_in_dictionary_str), key);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01005416
5417 // If :silent! is used we will continue, make sure the
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005418 // stack contents makes sense and the dict stack is
5419 // updated.
Bram Moolenaar4029cab2020-12-05 18:13:27 +01005420 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005421 --ectx->ec_stack.ga_len;
Bram Moolenaar4029cab2020-12-05 18:13:27 +01005422 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005423 (void) dict_stack_save(tv);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01005424 tv->v_type = VAR_NUMBER;
5425 tv->vval.v_number = 0;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01005426 goto on_fatal_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005427 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005428 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005429 --ectx->ec_stack.ga_len;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005430 // Put the dict used on the dict stack, it might be used by
5431 // a dict function later.
Bram Moolenaar50788ef2020-07-05 16:51:26 +02005432 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005433 if (dict_stack_save(tv) == FAIL)
5434 goto on_fatal_error;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02005435 copy_tv(&di->di_tv, tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005436 }
5437 break;
5438
5439 // dict member with string key
5440 case ISN_STRINGMEMBER:
5441 {
5442 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005443 dictitem_T *di;
5444
5445 tv = STACK_TV_BOT(-1);
5446 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
5447 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005448 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005449 emsg(_(e_dictionary_required));
Bram Moolenaard032f342020-07-18 18:13:02 +02005450 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005451 }
5452 dict = tv->vval.v_dict;
5453
5454 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
5455 == NULL)
5456 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005457 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00005458 semsg(_(e_key_not_present_in_dictionary_str),
Bram Moolenaar381692b2022-02-02 20:01:27 +00005459 iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02005460 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005461 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005462 // Put the dict used on the dict stack, it might be used by
5463 // a dict function later.
5464 if (dict_stack_save(tv) == FAIL)
5465 goto on_fatal_error;
5466
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005467 copy_tv(&di->di_tv, tv);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005468 }
5469 break;
5470
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005471 case ISN_GET_OBJ_MEMBER:
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00005472 case ISN_GET_ITF_MEMBER:
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005473 {
5474 tv = STACK_TV_BOT(-1);
5475 if (tv->v_type != VAR_OBJECT)
5476 {
5477 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaard0200c82023-01-28 15:19:40 +00005478 object_required_error(tv);
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005479 goto on_error;
5480 }
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00005481
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005482 object_T *obj = tv->vval.v_object;
Bram Moolenaarc3f971f2023-03-02 17:38:33 +00005483 if (obj == NULL)
5484 {
5485 SOURCING_LNUM = iptr->isn_lnum;
5486 emsg(_(e_using_null_object));
5487 goto on_error;
5488 }
5489
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00005490 int idx;
5491 if (iptr->isn_type == ISN_GET_OBJ_MEMBER)
Ernie Rael18143d32023-09-04 22:30:41 +02005492 idx = iptr->isn_arg.classmember.cm_idx;
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00005493 else
5494 {
5495 idx = iptr->isn_arg.classmember.cm_idx;
5496 // convert the interface index to the object index
5497 idx = object_index_from_itf_index(
Ernie Rael18143d32023-09-04 22:30:41 +02005498 iptr->isn_arg.classmember.cm_class,
Yegappan Lakshmanan5a05d372023-09-29 19:43:11 +02005499 FALSE, idx, obj->obj_class);
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00005500 }
5501
Ernie Rael18143d32023-09-04 22:30:41 +02005502 // The members are located right after the object struct.
Yegappan Lakshmanan5a05d372023-09-29 19:43:11 +02005503 typval_T *mtv = ((typval_T *)(obj + 1)) + idx;
Bram Moolenaar590162c2022-12-24 21:24:06 +00005504 copy_tv(mtv, tv);
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005505
5506 // Unreference the object after getting the member, it may
5507 // be freed.
5508 object_unref(obj);
5509 }
5510 break;
5511
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005512 case ISN_STORE_THIS:
5513 {
5514 int idx = iptr->isn_arg.number;
5515 object_T *obj = STACK_TV_VAR(0)->vval.v_object;
5516 // the members are located right after the object struct
5517 typval_T *mtv = ((typval_T *)(obj + 1)) + idx;
5518 clear_tv(mtv);
5519 *mtv = *STACK_TV_BOT(-1);
5520 --ectx->ec_stack.ga_len;
5521 }
5522 break;
5523
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005524 case ISN_CLEARDICT:
5525 dict_stack_drop();
5526 break;
5527
5528 case ISN_USEDICT:
5529 {
5530 typval_T *dict_tv = dict_stack_get_tv();
5531
5532 // Turn "dict.Func" into a partial for "Func" bound to
5533 // "dict". Don't do this when "Func" is already a partial
5534 // that was bound explicitly (pt_auto is FALSE).
5535 tv = STACK_TV_BOT(-1);
5536 if (dict_tv != NULL
5537 && dict_tv->v_type == VAR_DICT
5538 && dict_tv->vval.v_dict != NULL
5539 && (tv->v_type == VAR_FUNC
5540 || (tv->v_type == VAR_PARTIAL
5541 && (tv->vval.v_partial->pt_auto
5542 || tv->vval.v_partial->pt_dict == NULL))))
5543 dict_tv->vval.v_dict =
5544 make_partial(dict_tv->vval.v_dict, tv);
5545 dict_stack_drop();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005546 }
5547 break;
5548
5549 case ISN_NEGATENR:
5550 tv = STACK_TV_BOT(-1);
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00005551 // CHECKTYPE should have checked the variable type
Bram Moolenaarc58164c2020-03-29 18:40:30 +02005552 if (tv->v_type == VAR_FLOAT)
5553 tv->vval.v_float = -tv->vval.v_float;
5554 else
Bram Moolenaarc58164c2020-03-29 18:40:30 +02005555 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005556 break;
5557
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005558 case ISN_CHECKTYPE:
5559 {
5560 checktype_T *ct = &iptr->isn_arg.type;
Bram Moolenaarb1040dc2022-05-18 11:00:48 +01005561 int r;
LemonBoyc5d27442023-08-19 13:02:35 +02005562 where_T where = WHERE_INIT;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005563
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01005564 tv = STACK_TV_BOT((int)ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02005565 SOURCING_LNUM = iptr->isn_lnum;
LemonBoyc5d27442023-08-19 13:02:35 +02005566 if (ct->ct_arg_idx > 0)
5567 {
5568 where.wt_index = ct->ct_arg_idx;
5569 where.wt_kind = ct->ct_is_var ? WT_VARIABLE : WT_ARGUMENT;
LemonBoyc5d27442023-08-19 13:02:35 +02005570 }
LemonBoyf244b2f2023-08-19 16:02:04 +02005571 where.wt_func_name = ectx->ec_where.wt_func_name;
LemonBoyc5d27442023-08-19 13:02:35 +02005572 r = check_typval_type(ct->ct_type, tv, where);
Bram Moolenaarb1040dc2022-05-18 11:00:48 +01005573 if (r == FAIL)
5574 goto on_error;
Bram Moolenaar5e654232020-09-16 15:22:00 +02005575
5576 // number 0 is FALSE, number 1 is TRUE
5577 if (tv->v_type == VAR_NUMBER
5578 && ct->ct_type->tt_type == VAR_BOOL
5579 && (tv->vval.v_number == 0
5580 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005581 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02005582 tv->v_type = VAR_BOOL;
5583 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02005584 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005585 }
5586 }
5587 break;
5588
Bram Moolenaar9af78762020-06-16 11:34:42 +02005589 case ISN_CHECKLEN:
5590 {
5591 int min_len = iptr->isn_arg.checklen.cl_min_len;
5592 list_T *list = NULL;
5593
5594 tv = STACK_TV_BOT(-1);
5595 if (tv->v_type == VAR_LIST)
5596 list = tv->vval.v_list;
5597 if (list == NULL || list->lv_len < min_len
5598 || (list->lv_len > min_len
5599 && !iptr->isn_arg.checklen.cl_more_OK))
5600 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005601 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005602 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02005603 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02005604 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02005605 }
5606 }
5607 break;
5608
Bram Moolenaaraa210a32021-01-02 15:41:03 +01005609 case ISN_SETTYPE:
Bram Moolenaar381692b2022-02-02 20:01:27 +00005610 set_tv_type(STACK_TV_BOT(-1), iptr->isn_arg.type.ct_type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01005611 break;
5612
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005613 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005614 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005615 {
5616 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005617 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005618
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005619 if (iptr->isn_type == ISN_2BOOL)
5620 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005621 tv = STACK_TV_BOT(iptr->isn_arg.tobool.offset);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005622 n = tv2bool(tv);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005623 if (iptr->isn_arg.tobool.invert)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005624 n = !n;
5625 }
5626 else
5627 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005628 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005629 SOURCING_LNUM = iptr->isn_lnum;
5630 n = tv_get_bool_chk(tv, &error);
5631 if (error)
5632 goto on_error;
5633 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005634 clear_tv(tv);
5635 tv->v_type = VAR_BOOL;
5636 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
5637 }
5638 break;
5639
5640 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02005641 case ISN_2STRING_ANY:
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01005642 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005643 if (do_2string(STACK_TV_BOT(iptr->isn_arg.tostring.offset),
5644 iptr->isn_type == ISN_2STRING_ANY,
5645 iptr->isn_arg.tostring.tolerant) == FAIL)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01005646 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005647 break;
5648
Bram Moolenaar08597872020-12-10 19:43:40 +01005649 case ISN_RANGE:
5650 {
5651 exarg_T ea;
5652 char *errormsg;
5653
Bram Moolenaarece0b872021-01-08 20:40:45 +01005654 ea.line2 = 0;
Bram Moolenaard1510ee2021-01-04 16:15:58 +01005655 ea.addr_count = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01005656 ea.addr_type = ADDR_LINES;
5657 ea.cmd = iptr->isn_arg.string;
Bram Moolenaarece0b872021-01-08 20:40:45 +01005658 ea.skip = FALSE;
Bram Moolenaar08597872020-12-10 19:43:40 +01005659 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaarece0b872021-01-08 20:40:45 +01005660 goto on_error;
Bram Moolenaara28639e2021-01-19 22:48:09 +01005661
Bram Moolenaar35578162021-08-02 19:10:38 +02005662 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005663 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005664 ++ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01005665 tv = STACK_TV_BOT(-1);
5666 tv->v_type = VAR_NUMBER;
5667 tv->v_lock = 0;
Bram Moolenaar06651632022-04-27 17:54:25 +01005668 tv->vval.v_number = ea.line2;
Bram Moolenaar08597872020-12-10 19:43:40 +01005669 }
5670 break;
5671
Bram Moolenaarc3516f72020-09-08 22:45:35 +02005672 case ISN_PUT:
5673 {
5674 int regname = iptr->isn_arg.put.put_regname;
5675 linenr_T lnum = iptr->isn_arg.put.put_lnum;
5676 char_u *expr = NULL;
5677 int dir = FORWARD;
5678
Bram Moolenaar08597872020-12-10 19:43:40 +01005679 if (lnum < -2)
5680 {
5681 // line number was put on the stack by ISN_RANGE
5682 tv = STACK_TV_BOT(-1);
5683 curwin->w_cursor.lnum = tv->vval.v_number;
5684 if (lnum == LNUM_VARIABLE_RANGE_ABOVE)
5685 dir = BACKWARD;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005686 --ectx->ec_stack.ga_len;
Bram Moolenaar08597872020-12-10 19:43:40 +01005687 }
5688 else if (lnum == -2)
Bram Moolenaarc3516f72020-09-08 22:45:35 +02005689 // :put! above cursor
5690 dir = BACKWARD;
5691 else if (lnum >= 0)
Bram Moolenaar4e713ba2022-02-07 15:31:37 +00005692 {
5693 curwin->w_cursor.lnum = lnum;
5694 if (lnum == 0)
5695 // check_cursor() below will move to line 1
5696 dir = BACKWARD;
5697 }
Bram Moolenaara28639e2021-01-19 22:48:09 +01005698
5699 if (regname == '=')
5700 {
5701 tv = STACK_TV_BOT(-1);
5702 if (tv->v_type == VAR_STRING)
5703 expr = tv->vval.v_string;
5704 else
5705 {
5706 expr = typval2string(tv, TRUE); // allocates value
5707 clear_tv(tv);
5708 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005709 --ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01005710 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02005711 check_cursor();
5712 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
5713 vim_free(expr);
5714 }
5715 break;
5716
Bram Moolenaar02194d22020-10-24 23:08:38 +02005717 case ISN_CMDMOD:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005718 ectx->ec_funclocal.floc_save_cmdmod = cmdmod;
5719 ectx->ec_funclocal.floc_restore_cmdmod = TRUE;
5720 ectx->ec_funclocal.floc_restore_cmdmod_stacklen =
5721 ectx->ec_stack.ga_len;
Bram Moolenaar02194d22020-10-24 23:08:38 +02005722 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
5723 apply_cmdmod(&cmdmod);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02005724 break;
5725
Bram Moolenaar02194d22020-10-24 23:08:38 +02005726 case ISN_CMDMOD_REV:
5727 // filter regprog is owned by the instruction, don't free it
5728 cmdmod.cmod_filter_regmatch.regprog = NULL;
5729 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005730 cmdmod = ectx->ec_funclocal.floc_save_cmdmod;
5731 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02005732 break;
5733
Bram Moolenaar792f7862020-11-23 08:31:18 +01005734 case ISN_UNPACK:
5735 {
5736 int count = iptr->isn_arg.unpack.unp_count;
5737 int semicolon = iptr->isn_arg.unpack.unp_semicolon;
5738 list_T *l;
5739 listitem_T *li;
5740 int i;
5741
5742 // Check there is a valid list to unpack.
5743 tv = STACK_TV_BOT(-1);
5744 if (tv->v_type != VAR_LIST)
5745 {
5746 SOURCING_LNUM = iptr->isn_lnum;
5747 emsg(_(e_for_argument_must_be_sequence_of_lists));
5748 goto on_error;
5749 }
5750 l = tv->vval.v_list;
5751 if (l == NULL
5752 || l->lv_len < (semicolon ? count - 1 : count))
5753 {
5754 SOURCING_LNUM = iptr->isn_lnum;
5755 emsg(_(e_list_value_does_not_have_enough_items));
5756 goto on_error;
5757 }
5758 else if (!semicolon && l->lv_len > count)
5759 {
5760 SOURCING_LNUM = iptr->isn_lnum;
5761 emsg(_(e_list_value_has_more_items_than_targets));
5762 goto on_error;
5763 }
5764
5765 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar35578162021-08-02 19:10:38 +02005766 if (GA_GROW_FAILS(&ectx->ec_stack, count - 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005767 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005768 ectx->ec_stack.ga_len += count - 1;
Bram Moolenaar792f7862020-11-23 08:31:18 +01005769
5770 // Variable after semicolon gets a list with the remaining
5771 // items.
5772 if (semicolon)
5773 {
5774 list_T *rem_list =
5775 list_alloc_with_items(l->lv_len - count + 1);
5776
5777 if (rem_list == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005778 goto theend;
Bram Moolenaar792f7862020-11-23 08:31:18 +01005779 tv = STACK_TV_BOT(-count);
5780 tv->vval.v_list = rem_list;
5781 ++rem_list->lv_refcount;
5782 tv->v_lock = 0;
5783 li = l->lv_first;
5784 for (i = 0; i < count - 1; ++i)
5785 li = li->li_next;
5786 for (i = 0; li != NULL; ++i)
5787 {
Bram Moolenaar61efa162022-03-18 13:10:48 +00005788 typval_T tvcopy;
5789
5790 copy_tv(&li->li_tv, &tvcopy);
5791 list_set_item(rem_list, i, &tvcopy);
Bram Moolenaar792f7862020-11-23 08:31:18 +01005792 li = li->li_next;
5793 }
5794 --count;
5795 }
5796
5797 // Produce the values in reverse order, first item last.
5798 li = l->lv_first;
5799 for (i = 0; i < count; ++i)
5800 {
5801 tv = STACK_TV_BOT(-i - 1);
5802 copy_tv(&li->li_tv, tv);
5803 li = li->li_next;
5804 }
5805
5806 list_unref(l);
5807 }
5808 break;
5809
Bram Moolenaarb2049902021-01-24 12:53:53 +01005810 case ISN_PROF_START:
5811 case ISN_PROF_END:
5812 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01005813#ifdef FEAT_PROFILE
Bram Moolenaarca16c602022-09-06 18:57:08 +01005814 funccall_T cookie;
5815 ufunc_T *cur_ufunc =
Bram Moolenaarb2049902021-01-24 12:53:53 +01005816 (((dfunc_T *)def_functions.ga_data)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02005817 + ectx->ec_dfunc_idx)->df_ufunc;
Bram Moolenaarb2049902021-01-24 12:53:53 +01005818
Bram Moolenaarca16c602022-09-06 18:57:08 +01005819 cookie.fc_func = cur_ufunc;
Bram Moolenaarb2049902021-01-24 12:53:53 +01005820 if (iptr->isn_type == ISN_PROF_START)
5821 {
5822 func_line_start(&cookie, iptr->isn_lnum);
5823 // if we get here the instruction is executed
5824 func_line_exec(&cookie);
5825 }
5826 else
5827 func_line_end(&cookie);
Bram Moolenaarf002a412021-01-24 13:34:18 +01005828#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01005829 }
5830 break;
5831
Bram Moolenaare99d4222021-06-13 14:01:26 +02005832 case ISN_DEBUG:
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02005833 handle_debug(iptr, ectx);
Bram Moolenaare99d4222021-06-13 14:01:26 +02005834 break;
5835
Bram Moolenaar389df252020-07-09 21:20:47 +02005836 case ISN_SHUFFLE:
5837 {
Bram Moolenaar792f7862020-11-23 08:31:18 +01005838 typval_T tmp_tv;
5839 int item = iptr->isn_arg.shuffle.shfl_item;
5840 int up = iptr->isn_arg.shuffle.shfl_up;
Bram Moolenaar389df252020-07-09 21:20:47 +02005841
5842 tmp_tv = *STACK_TV_BOT(-item);
5843 for ( ; up > 0 && item > 1; --up)
5844 {
5845 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
5846 --item;
5847 }
5848 *STACK_TV_BOT(-item) = tmp_tv;
5849 }
5850 break;
5851
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005852 case ISN_DROP:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005853 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005854 clear_tv(STACK_TV_BOT(0));
LemonBoyc5d27442023-08-19 13:02:35 +02005855 ectx->ec_where = (where_T)WHERE_INIT;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005856 break;
5857 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02005858 continue;
5859
Bram Moolenaard032f342020-07-18 18:13:02 +02005860func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02005861 // Restore previous function. If the frame pointer is where we started
5862 // then there is none and we are done.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005863 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02005864 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02005865
Bram Moolenaar4c137212021-04-19 16:48:48 +02005866 if (func_return(ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02005867 // only fails when out of memory
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005868 goto theend;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02005869 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02005870
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02005871on_error:
Bram Moolenaaraf0df472020-12-02 20:51:22 +01005872 // Jump here for an error that does not require aborting execution.
Bram Moolenaar56602ba2020-12-05 21:22:08 +01005873 // If "emsg_silent" is set then ignore the error, unless it was set
5874 // when calling the function.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005875 if (did_emsg_cumul + did_emsg == ectx->ec_did_emsg_before
Bram Moolenaar56602ba2020-12-05 21:22:08 +01005876 && emsg_silent && did_emsg_def == 0)
Bram Moolenaarf9041332021-01-21 19:41:16 +01005877 {
5878 // If a sequence of instructions causes an error while ":silent!"
5879 // was used, restore the stack length and jump ahead to restoring
5880 // the cmdmod.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005881 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaarf9041332021-01-21 19:41:16 +01005882 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005883 while (ectx->ec_stack.ga_len
5884 > ectx->ec_funclocal.floc_restore_cmdmod_stacklen)
Bram Moolenaarf9041332021-01-21 19:41:16 +01005885 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005886 --ectx->ec_stack.ga_len;
Bram Moolenaarf9041332021-01-21 19:41:16 +01005887 clear_tv(STACK_TV_BOT(0));
5888 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005889 while (ectx->ec_instr[ectx->ec_iidx].isn_type != ISN_CMDMOD_REV)
5890 ++ectx->ec_iidx;
Bram Moolenaarf9041332021-01-21 19:41:16 +01005891 }
Bram Moolenaarcd030c42020-10-30 21:49:40 +01005892 continue;
Bram Moolenaarf9041332021-01-21 19:41:16 +01005893 }
Bram Moolenaaraf0df472020-12-02 20:51:22 +01005894on_fatal_error:
5895 // Jump here for an error that messes up the stack.
Bram Moolenaar171fb922020-10-28 16:54:47 +01005896 // If we are not inside a try-catch started here, abort execution.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005897 if (trylevel <= ectx->ec_trylevel_at_start)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005898 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005899 }
5900
5901done:
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005902 ret = OK;
5903theend:
Bram Moolenaar58779852022-09-06 18:31:14 +01005904 may_invoke_defer_funcs(ectx);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01005905
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005906 dict_stack_clear(dict_stack_len_at_start);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005907 ectx->ec_trylevel_at_start = save_trylevel_at_start;
5908 return ret;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005909}
5910
5911/*
Bram Moolenaar806a2732022-09-04 15:40:36 +01005912 * Execute the instructions from a VAR_INSTR typval and put the result in
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005913 * "rettv".
5914 * Return OK or FAIL.
5915 */
5916 int
5917exe_typval_instr(typval_T *tv, typval_T *rettv)
5918{
5919 ectx_T *ectx = tv->vval.v_instr->instr_ectx;
5920 isn_T *save_instr = ectx->ec_instr;
5921 int save_iidx = ectx->ec_iidx;
5922 int res;
5923
LemonBoyf3b48952022-05-05 13:53:03 +01005924 // Initialize rettv so that it is safe for caller to invoke clear_tv(rettv)
5925 // even when the compilation fails.
5926 rettv->v_type = VAR_UNKNOWN;
5927
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005928 ectx->ec_instr = tv->vval.v_instr->instr_instr;
5929 res = exec_instructions(ectx);
5930 if (res == OK)
5931 {
5932 *rettv = *STACK_TV_BOT(-1);
5933 --ectx->ec_stack.ga_len;
5934 }
5935
5936 ectx->ec_instr = save_instr;
5937 ectx->ec_iidx = save_iidx;
5938
5939 return res;
5940}
5941
5942/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02005943 * Execute the instructions from an ISN_SUBSTITUTE command, which are in
5944 * "substitute_instr".
5945 */
5946 char_u *
5947exe_substitute_instr(void)
5948{
5949 ectx_T *ectx = substitute_instr->subs_ectx;
5950 isn_T *save_instr = ectx->ec_instr;
5951 int save_iidx = ectx->ec_iidx;
5952 char_u *res;
5953
5954 ectx->ec_instr = substitute_instr->subs_instr;
5955 if (exec_instructions(ectx) == OK)
Bram Moolenaar90193e62021-04-04 20:49:50 +02005956 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005957 typval_T *tv = STACK_TV_BOT(-1);
5958
Bram Moolenaar27523602021-06-05 21:36:19 +02005959 res = typval2string(tv, TRUE);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005960 --ectx->ec_stack.ga_len;
5961 clear_tv(tv);
Bram Moolenaar90193e62021-04-04 20:49:50 +02005962 }
5963 else
5964 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005965 substitute_instr->subs_status = FAIL;
5966 res = vim_strsave((char_u *)"");
Bram Moolenaar90193e62021-04-04 20:49:50 +02005967 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005968
Bram Moolenaar4c137212021-04-19 16:48:48 +02005969 ectx->ec_instr = save_instr;
5970 ectx->ec_iidx = save_iidx;
5971
5972 return res;
5973}
5974
5975/*
5976 * Call a "def" function from old Vim script.
5977 * Return OK or FAIL.
5978 */
5979 int
5980call_def_function(
5981 ufunc_T *ufunc,
5982 int argc_arg, // nr of arguments
5983 typval_T *argv, // arguments
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005984 int flags, // DEF_ flags
Bram Moolenaar4c137212021-04-19 16:48:48 +02005985 partial_T *partial, // optional partial for context
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005986 object_T *object, // object, e.g. for this.Func()
Bram Moolenaar58779852022-09-06 18:31:14 +01005987 funccall_T *funccal,
Bram Moolenaar4c137212021-04-19 16:48:48 +02005988 typval_T *rettv) // return value
5989{
5990 ectx_T ectx; // execution context
5991 int argc = argc_arg;
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005992 int partial_argc = partial == NULL
5993 || (flags & DEF_USE_PT_ARGV) == 0
5994 ? 0 : partial->pt_argc;
5995 int total_argc = argc + partial_argc;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005996 typval_T *tv;
5997 int idx;
5998 int ret = FAIL;
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005999 int defcount = ufunc->uf_args.ga_len - total_argc;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006000 sctx_T save_current_sctx = current_sctx;
6001 int did_emsg_before = did_emsg_cumul + did_emsg;
6002 int save_suppress_errthrow = suppress_errthrow;
6003 msglist_T **saved_msg_list = NULL;
6004 msglist_T *private_msg_list = NULL;
6005 int save_emsg_silent_def = emsg_silent_def;
6006 int save_did_emsg_def = did_emsg_def;
6007 int orig_funcdepth;
Bram Moolenaare99d4222021-06-13 14:01:26 +02006008 int orig_nesting_level = ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006009
6010// Get pointer to item in the stack.
6011#undef STACK_TV
6012#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
6013
6014// Get pointer to item at the bottom of the stack, -1 is the bottom.
6015#undef STACK_TV_BOT
6016#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
6017
6018// Get pointer to a local variable on the stack. Negative for arguments.
6019#undef STACK_TV_VAR
6020#define STACK_TV_VAR(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_frame_idx + STACK_FRAME_SIZE + idx)
6021
6022 if (ufunc->uf_def_status == UF_NOT_COMPILED
6023 || ufunc->uf_def_status == UF_COMPILE_ERROR
Bram Moolenaar139575d2022-03-15 19:29:30 +00006024 || (func_needs_compiling(ufunc, get_compile_type(ufunc))
6025 && compile_def_function(ufunc, FALSE,
6026 get_compile_type(ufunc), NULL) == FAIL))
Bram Moolenaar4c137212021-04-19 16:48:48 +02006027 {
6028 if (did_emsg_cumul + did_emsg == did_emsg_before)
6029 semsg(_(e_function_is_not_compiled_str),
6030 printable_func_name(ufunc));
6031 return FAIL;
6032 }
6033
6034 {
6035 // Check the function was really compiled.
6036 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6037 + ufunc->uf_dfunc_idx;
Bram Moolenaarcc341812022-09-19 15:54:34 +01006038 if (dfunc->df_ufunc == NULL)
6039 {
6040 semsg(_(e_function_was_deleted_str), printable_func_name(ufunc));
6041 return FAIL;
6042 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006043 if (INSTRUCTIONS(dfunc) == NULL)
6044 {
6045 iemsg("using call_def_function() on not compiled function");
6046 return FAIL;
6047 }
6048 }
6049
6050 // If depth of calling is getting too high, don't execute the function.
6051 orig_funcdepth = funcdepth_get();
6052 if (funcdepth_increment() == FAIL)
6053 return FAIL;
6054
6055 CLEAR_FIELD(ectx);
6056 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
6057 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
Bram Moolenaar35578162021-08-02 19:10:38 +02006058 if (GA_GROW_FAILS(&ectx.ec_stack, 20))
Bram Moolenaar4c137212021-04-19 16:48:48 +02006059 {
6060 funcdepth_decrement();
6061 return FAIL;
6062 }
6063 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
6064 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
6065 ectx.ec_did_emsg_before = did_emsg_before;
Bram Moolenaare99d4222021-06-13 14:01:26 +02006066 ++ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006067
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01006068 idx = total_argc - ufunc->uf_args.ga_len;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006069 if (idx > 0 && ufunc->uf_va_name == NULL)
6070 {
Matvey Tarasovd14bb1a2022-06-29 13:18:27 +01006071 semsg(NGETTEXT(e_one_argument_too_many, e_nr_arguments_too_many,
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01006072 idx), idx);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006073 goto failed_early;
6074 }
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01006075 idx = total_argc - ufunc->uf_args.ga_len + ufunc->uf_def_args.ga_len;
Bram Moolenaarc6d71532021-06-05 18:49:38 +02006076 if (idx < 0)
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02006077 {
Matvey Tarasovd14bb1a2022-06-29 13:18:27 +01006078 semsg(NGETTEXT(e_one_argument_too_few, e_nr_arguments_too_few,
Bram Moolenaar98aff652022-09-06 21:02:35 +01006079 -idx), -idx);
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02006080 goto failed_early;
6081 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006082
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01006083 // Put values from the partial and arguments on the stack, but no more than
6084 // what the function expects. A lambda can be called with more arguments
6085 // than it uses.
6086 for (idx = 0; idx < total_argc
Bram Moolenaar4c137212021-04-19 16:48:48 +02006087 && (ufunc->uf_va_name != NULL || idx < ufunc->uf_args.ga_len);
6088 ++idx)
6089 {
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01006090 int argv_idx = idx - partial_argc;
6091
6092 tv = idx < partial_argc ? partial->pt_argv + idx : argv + argv_idx;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006093 if (idx >= ufunc->uf_args.ga_len - ufunc->uf_def_args.ga_len
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01006094 && tv->v_type == VAR_SPECIAL
6095 && tv->vval.v_number == VVAL_NONE)
Bram Moolenaar4c137212021-04-19 16:48:48 +02006096 {
6097 // Use the default value.
6098 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
6099 }
6100 else
6101 {
Bram Moolenaar47bba532023-01-20 18:49:46 +00006102 int done = FALSE;
6103 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len)
6104 {
6105 type_T *expected = ufunc->uf_arg_types[idx];
6106 if (expected->tt_type == VAR_FLOAT && tv->v_type == VAR_NUMBER)
6107 {
6108 // When a float is expected and a number was given, convert
6109 // the value.
6110 STACK_TV_BOT(0)->v_type = VAR_FLOAT;
6111 STACK_TV_BOT(0)->v_lock = 0;
6112 STACK_TV_BOT(0)->vval.v_float = tv->vval.v_number;
6113 done = TRUE;
6114 }
6115 else if (check_typval_arg_type(expected, tv,
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01006116 NULL, argv_idx + 1) == FAIL)
Bram Moolenaar47bba532023-01-20 18:49:46 +00006117 goto failed_early;
6118 }
6119 if (!done)
6120 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006121 }
6122 ++ectx.ec_stack.ga_len;
6123 }
6124
6125 // Turn varargs into a list. Empty list if no args.
6126 if (ufunc->uf_va_name != NULL)
6127 {
6128 int vararg_count = argc - ufunc->uf_args.ga_len;
6129
6130 if (vararg_count < 0)
6131 vararg_count = 0;
6132 else
6133 argc -= vararg_count;
6134 if (exe_newlist(vararg_count, &ectx) == FAIL)
6135 goto failed_early;
6136
6137 // Check the type of the list items.
6138 tv = STACK_TV_BOT(-1);
6139 if (ufunc->uf_va_type != NULL
6140 && ufunc->uf_va_type != &t_list_any
6141 && ufunc->uf_va_type->tt_member != &t_any
6142 && tv->vval.v_list != NULL)
6143 {
6144 type_T *expected = ufunc->uf_va_type->tt_member;
6145 listitem_T *li = tv->vval.v_list->lv_first;
6146
6147 for (idx = 0; idx < vararg_count; ++idx)
6148 {
6149 if (check_typval_arg_type(expected, &li->li_tv,
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02006150 NULL, argc + idx + 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02006151 goto failed_early;
6152 li = li->li_next;
6153 }
6154 }
6155
6156 if (defcount > 0)
6157 // Move varargs list to below missing default arguments.
6158 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
6159 --ectx.ec_stack.ga_len;
6160 }
6161
6162 // Make space for omitted arguments, will store default value below.
6163 // Any varargs list goes after them.
6164 if (defcount > 0)
6165 for (idx = 0; idx < defcount; ++idx)
6166 {
6167 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
6168 ++ectx.ec_stack.ga_len;
6169 }
6170 if (ufunc->uf_va_name != NULL)
6171 ++ectx.ec_stack.ga_len;
6172
6173 // Frame pointer points to just after arguments.
6174 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
6175 ectx.ec_initial_frame_idx = ectx.ec_frame_idx;
6176
6177 {
6178 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6179 + ufunc->uf_dfunc_idx;
6180 ufunc_T *base_ufunc = dfunc->df_ufunc;
6181
6182 // "uf_partial" is on the ufunc that "df_ufunc" points to, as is done
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006183 // by copy_lambda_to_global_func().
Bram Moolenaar4c137212021-04-19 16:48:48 +02006184 if (partial != NULL || base_ufunc->uf_partial != NULL)
6185 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006186 ectx.ec_outer_ref = ALLOC_CLEAR_ONE(outer_ref_T);
6187 if (ectx.ec_outer_ref == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02006188 goto failed_early;
6189 if (partial != NULL)
6190 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00006191 outer_T *outer = get_pt_outer(partial);
6192
Bram Moolenaar6d313be2022-09-22 16:36:25 +01006193 if (outer->out_stack == NULL && outer->out_loop_size == 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02006194 {
Bram Moolenaar6d313be2022-09-22 16:36:25 +01006195 // no stack was set
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00006196 if (current_ectx != NULL)
6197 {
6198 if (current_ectx->ec_outer_ref != NULL
6199 && current_ectx->ec_outer_ref->or_outer != NULL)
6200 ectx.ec_outer_ref->or_outer =
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006201 current_ectx->ec_outer_ref->or_outer;
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00006202 }
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01006203 // else: should there be an error here?
Bram Moolenaar4c137212021-04-19 16:48:48 +02006204 }
6205 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006206 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00006207 ectx.ec_outer_ref->or_outer = outer;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006208 ++partial->pt_refcount;
6209 ectx.ec_outer_ref->or_partial = partial;
6210 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006211 }
6212 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006213 {
6214 ectx.ec_outer_ref->or_outer = &base_ufunc->uf_partial->pt_outer;
6215 ++base_ufunc->uf_partial->pt_refcount;
6216 ectx.ec_outer_ref->or_partial = base_ufunc->uf_partial;
6217 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006218 }
6219 }
6220
6221 // dummy frame entries
6222 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
6223 {
6224 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
6225 ++ectx.ec_stack.ga_len;
6226 }
6227
6228 {
6229 // Reserve space for local variables and any closure reference count.
6230 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6231 + ufunc->uf_dfunc_idx;
6232
Bram Moolenaar5cd64792021-12-25 18:23:24 +00006233 // Initialize variables to zero. That avoids having to generate
6234 // initializing instructions for "var nr: number", "var x: any", etc.
Bram Moolenaar4c137212021-04-19 16:48:48 +02006235 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaar5cd64792021-12-25 18:23:24 +00006236 {
6237 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
6238 STACK_TV_VAR(idx)->vval.v_number = 0;
6239 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006240 ectx.ec_stack.ga_len += dfunc->df_varcount;
Bram Moolenaarffdaca92022-12-09 21:41:48 +00006241
6242 if (object != NULL)
6243 {
6244 // the object is always the variable at index zero
6245 tv = STACK_TV_VAR(0);
6246 tv->v_type = VAR_OBJECT;
6247 tv->vval.v_object = object;
6248 }
6249
Bram Moolenaar4c137212021-04-19 16:48:48 +02006250 if (dfunc->df_has_closure)
6251 {
Bram Moolenaarcc341812022-09-19 15:54:34 +01006252 // Initialize the variable that counts how many closures were
6253 // created. This is used in handle_closure_in_use().
Bram Moolenaar4c137212021-04-19 16:48:48 +02006254 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
6255 STACK_TV_VAR(idx)->vval.v_number = 0;
6256 ++ectx.ec_stack.ga_len;
6257 }
6258
6259 ectx.ec_instr = INSTRUCTIONS(dfunc);
6260 }
6261
Bram Moolenaar58779852022-09-06 18:31:14 +01006262 // Store the execution context in funccal, used by invoke_all_defer().
6263 if (funccal != NULL)
6264 funccal->fc_ectx = &ectx;
6265
Bram Moolenaar4c137212021-04-19 16:48:48 +02006266 // Following errors are in the function, not the caller.
6267 // Commands behave like vim9script.
6268 estack_push_ufunc(ufunc, 1);
6269 current_sctx = ufunc->uf_script_ctx;
6270 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
6271
6272 // Use a specific location for storing error messages to be converted to an
6273 // exception.
6274 saved_msg_list = msg_list;
6275 msg_list = &private_msg_list;
6276
6277 // Do turn errors into exceptions.
6278 suppress_errthrow = FALSE;
6279
6280 // Do not delete the function while executing it.
6281 ++ufunc->uf_calls;
6282
6283 // When ":silent!" was used before calling then we still abort the
6284 // function. If ":silent!" is used in the function then we don't.
6285 emsg_silent_def = emsg_silent;
6286 did_emsg_def = 0;
6287
LemonBoyc5d27442023-08-19 13:02:35 +02006288 ectx.ec_where = (where_T)WHERE_INIT;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006289
Bram Moolenaar5800c792022-09-22 17:34:01 +01006290 /*
6291 * Execute the instructions until done.
6292 */
Bram Moolenaar4c137212021-04-19 16:48:48 +02006293 ret = exec_instructions(&ectx);
6294 if (ret == OK)
6295 {
6296 // function finished, get result from the stack.
6297 if (ufunc->uf_ret_type == &t_void)
6298 {
6299 rettv->v_type = VAR_VOID;
6300 }
6301 else
6302 {
6303 tv = STACK_TV_BOT(-1);
6304 *rettv = *tv;
6305 tv->v_type = VAR_UNKNOWN;
6306 }
6307 }
6308
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01006309 // When failed need to unwind the call stack.
Bram Moolenaar58779852022-09-06 18:31:14 +01006310 unwind_def_callstack(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02006311
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02006312 // Deal with any remaining closures, they may be in use somewhere.
6313 if (ectx.ec_funcrefs.ga_len > 0)
Bram Moolenaarf112f302020-12-20 17:47:52 +01006314 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02006315 handle_closure_in_use(&ectx, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00006316 ga_clear(&ectx.ec_funcrefs);
Bram Moolenaarf112f302020-12-20 17:47:52 +01006317 }
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02006318
Bram Moolenaaree8580e2020-08-28 17:19:07 +02006319 estack_pop();
6320 current_sctx = save_current_sctx;
6321
Bram Moolenaar2336c372021-12-06 15:06:54 +00006322 if (--ufunc->uf_calls <= 0 && ufunc->uf_refcount <= 0)
6323 // Function was unreferenced while being used, free it now.
6324 func_clear_free(ufunc, FALSE);
Bram Moolenaarc970e422021-03-17 15:03:04 +01006325
Bram Moolenaar352134b2020-10-17 22:04:08 +02006326 if (*msg_list != NULL && saved_msg_list != NULL)
6327 {
6328 msglist_T **plist = saved_msg_list;
6329
6330 // Append entries from the current msg_list (uncaught exceptions) to
6331 // the saved msg_list.
6332 while (*plist != NULL)
6333 plist = &(*plist)->next;
6334
6335 *plist = *msg_list;
6336 }
6337 msg_list = saved_msg_list;
6338
Bram Moolenaar4c137212021-04-19 16:48:48 +02006339 if (ectx.ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar02194d22020-10-24 23:08:38 +02006340 {
6341 cmdmod.cmod_filter_regmatch.regprog = NULL;
6342 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006343 cmdmod = ectx.ec_funclocal.floc_save_cmdmod;
Bram Moolenaar02194d22020-10-24 23:08:38 +02006344 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +01006345 emsg_silent_def = save_emsg_silent_def;
6346 did_emsg_def += save_did_emsg_def;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02006347
Bram Moolenaaree8580e2020-08-28 17:19:07 +02006348failed_early:
Bram Moolenaar0d1f55c2022-04-05 17:30:29 +01006349 // Free all arguments and local variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006350 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
Bram Moolenaar1936c762022-09-28 15:19:10 +01006351 {
6352 tv = STACK_TV(idx);
6353 if (tv->v_type != VAR_NUMBER && tv->v_type != VAR_UNKNOWN)
6354 clear_tv(tv);
6355 }
Bram Moolenaare99d4222021-06-13 14:01:26 +02006356 ex_nesting_level = orig_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02006357
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006358 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01006359 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006360 if (ectx.ec_outer_ref != NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +01006361 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006362 if (ectx.ec_outer_ref->or_outer_allocated)
6363 vim_free(ectx.ec_outer_ref->or_outer);
6364 partial_unref(ectx.ec_outer_ref->or_partial);
6365 vim_free(ectx.ec_outer_ref);
Bram Moolenaar0186e582021-01-10 18:33:11 +01006366 }
6367
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02006368 // Not sure if this is necessary.
6369 suppress_errthrow = save_suppress_errthrow;
6370
Bram Moolenaarb5841b92021-07-15 18:09:53 +02006371 if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before
6372 && !need_rethrow)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006373 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02006374 printable_func_name(ufunc));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01006375 funcdepth_restore(orig_funcdepth);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006376 return ret;
6377}
6378
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006379/*
Bram Moolenaar58779852022-09-06 18:31:14 +01006380 * Called when a def function has finished (possibly failed).
6381 * Invoke all the function returns to clean up and invoke deferred functions,
6382 * except the toplevel one.
6383 */
6384 void
6385unwind_def_callstack(ectx_T *ectx)
6386{
6387 while (ectx->ec_frame_idx != ectx->ec_initial_frame_idx)
6388 func_return(ectx);
6389}
6390
6391/*
dundargocc57b5bc2022-11-02 13:30:51 +00006392 * Invoke any deferred functions for the top function in "ectx".
Bram Moolenaar58779852022-09-06 18:31:14 +01006393 */
6394 void
6395may_invoke_defer_funcs(ectx_T *ectx)
6396{
6397 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
6398
6399 if (dfunc->df_defer_var_idx > 0)
6400 invoke_defer_funcs(ectx);
6401}
6402
6403/*
Bram Moolenaarcc341812022-09-19 15:54:34 +01006404 * Return loopvarinfo in a printable form in allocated memory.
6405 */
6406 static char_u *
6407printable_loopvarinfo(loopvarinfo_T *lvi)
6408{
6409 garray_T ga;
6410 int depth;
6411
6412 ga_init2(&ga, 1, 100);
6413 for (depth = 0; depth < lvi->lvi_depth; ++depth)
6414 {
6415 if (ga_grow(&ga, 50) == FAIL)
6416 break;
6417 if (lvi->lvi_loop[depth].var_idx == 0)
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +01006418 STRCPY((char *)ga.ga_data + ga.ga_len, " -");
Bram Moolenaarcc341812022-09-19 15:54:34 +01006419 else
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +01006420 vim_snprintf((char *)ga.ga_data + ga.ga_len, 50, " $%d-$%d",
Bram Moolenaarcc341812022-09-19 15:54:34 +01006421 lvi->lvi_loop[depth].var_idx,
6422 lvi->lvi_loop[depth].var_idx
6423 + lvi->lvi_loop[depth].var_count - 1);
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +01006424 ga.ga_len = (int)STRLEN(ga.ga_data);
Bram Moolenaarcc341812022-09-19 15:54:34 +01006425 }
6426 return ga.ga_data;
6427}
6428
6429/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02006430 * List instructions "instr" up to "instr_count" or until ISN_FINISH.
6431 * "ufunc" has the source lines, NULL for the instructions of ISN_SUBSTITUTE.
6432 * "pfx" is prefixed to every line.
6433 */
6434 static void
6435list_instructions(char *pfx, isn_T *instr, int instr_count, ufunc_T *ufunc)
6436{
6437 int line_idx = 0;
6438 int prev_current = 0;
6439 int current;
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02006440 int def_arg_idx = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006441
6442 for (current = 0; current < instr_count; ++current)
6443 {
6444 isn_T *iptr = &instr[current];
6445 char *line;
6446
6447 if (ufunc != NULL)
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02006448 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02006449 while (line_idx < iptr->isn_lnum
6450 && line_idx < ufunc->uf_lines.ga_len)
6451 {
6452 if (current > prev_current)
6453 {
6454 msg_puts("\n\n");
6455 prev_current = current;
6456 }
6457 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
6458 if (line != NULL)
6459 msg(line);
6460 }
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02006461 if (iptr->isn_type == ISN_JUMP_IF_ARG_SET)
6462 {
6463 int first_def_arg = ufunc->uf_args.ga_len
6464 - ufunc->uf_def_args.ga_len;
6465
6466 if (def_arg_idx > 0)
6467 msg_puts("\n\n");
6468 msg_start();
6469 msg_puts(" ");
6470 msg_puts(((char **)(ufunc->uf_args.ga_data))[
6471 first_def_arg + def_arg_idx]);
6472 msg_puts(" = ");
6473 msg_puts(((char **)(ufunc->uf_def_args.ga_data))[def_arg_idx++]);
6474 msg_clr_eos();
6475 msg_end();
6476 }
6477 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006478
6479 switch (iptr->isn_type)
6480 {
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006481 case ISN_CONSTRUCT:
6482 smsg("%s%4d NEW %s size %d", pfx, current,
6483 iptr->isn_arg.construct.construct_class->class_name,
6484 (int)iptr->isn_arg.construct.construct_size);
6485 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006486 case ISN_EXEC:
6487 smsg("%s%4d EXEC %s", pfx, current, iptr->isn_arg.string);
6488 break;
Bram Moolenaar20677332021-06-06 17:02:53 +02006489 case ISN_EXEC_SPLIT:
6490 smsg("%s%4d EXEC_SPLIT %s", pfx, current, iptr->isn_arg.string);
6491 break;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00006492 case ISN_EXECRANGE:
6493 smsg("%s%4d EXECRANGE %s", pfx, current, iptr->isn_arg.string);
6494 break;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02006495 case ISN_LEGACY_EVAL:
6496 smsg("%s%4d EVAL legacy %s", pfx, current,
6497 iptr->isn_arg.string);
6498 break;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02006499 case ISN_REDIRSTART:
6500 smsg("%s%4d REDIR", pfx, current);
6501 break;
6502 case ISN_REDIREND:
6503 smsg("%s%4d REDIR END%s", pfx, current,
6504 iptr->isn_arg.number ? " append" : "");
6505 break;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006506 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02006507#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006508 smsg("%s%4d CEXPR pre %s", pfx, current,
6509 cexpr_get_auname(iptr->isn_arg.number));
Bram Moolenaarb7c97812021-05-05 22:51:39 +02006510#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006511 break;
6512 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02006513#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006514 {
6515 cexprref_T *cer = iptr->isn_arg.cexpr.cexpr_ref;
6516
6517 smsg("%s%4d CEXPR core %s%s \"%s\"", pfx, current,
6518 cexpr_get_auname(cer->cer_cmdidx),
6519 cer->cer_forceit ? "!" : "",
6520 cer->cer_cmdline);
6521 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02006522#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006523 break;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006524 case ISN_INSTR:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006525 smsg("%s%4d INSTR", pfx, current);
6526 list_instructions(" ", iptr->isn_arg.instr, INT_MAX, NULL);
6527 msg(" -------------");
6528 break;
6529 case ISN_SOURCE:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006530 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006531 scriptitem_T *si = SCRIPT_ITEM(iptr->isn_arg.number);
6532
6533 smsg("%s%4d SOURCE %s", pfx, current, si->sn_name);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006534 }
6535 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006536 case ISN_SUBSTITUTE:
6537 {
6538 subs_T *subs = &iptr->isn_arg.subs;
6539
6540 smsg("%s%4d SUBSTITUTE %s", pfx, current, subs->subs_cmd);
6541 list_instructions(" ", subs->subs_instr, INT_MAX, NULL);
6542 msg(" -------------");
6543 }
6544 break;
6545 case ISN_EXECCONCAT:
6546 smsg("%s%4d EXECCONCAT %lld", pfx, current,
6547 (varnumber_T)iptr->isn_arg.number);
6548 break;
6549 case ISN_ECHO:
6550 {
6551 echo_T *echo = &iptr->isn_arg.echo;
6552
6553 smsg("%s%4d %s %d", pfx, current,
6554 echo->echo_with_white ? "ECHO" : "ECHON",
6555 echo->echo_count);
6556 }
6557 break;
6558 case ISN_EXECUTE:
6559 smsg("%s%4d EXECUTE %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02006560 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006561 break;
6562 case ISN_ECHOMSG:
6563 smsg("%s%4d ECHOMSG %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02006564 (varnumber_T)(iptr->isn_arg.number));
6565 break;
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01006566 case ISN_ECHOWINDOW:
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01006567 if (iptr->isn_arg.echowin.ewin_time > 0)
6568 smsg("%s%4d ECHOWINDOW %d (%ld sec)", pfx, current,
6569 iptr->isn_arg.echowin.ewin_count,
6570 iptr->isn_arg.echowin.ewin_time);
6571 else
6572 smsg("%s%4d ECHOWINDOW %d", pfx, current,
6573 iptr->isn_arg.echowin.ewin_count);
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01006574 break;
Bram Moolenaar7de62622021-08-07 15:05:47 +02006575 case ISN_ECHOCONSOLE:
6576 smsg("%s%4d ECHOCONSOLE %lld", pfx, current,
6577 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006578 break;
6579 case ISN_ECHOERR:
6580 smsg("%s%4d ECHOERR %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02006581 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006582 break;
6583 case ISN_LOAD:
6584 {
6585 if (iptr->isn_arg.number < 0)
6586 smsg("%s%4d LOAD arg[%lld]", pfx, current,
6587 (varnumber_T)(iptr->isn_arg.number
6588 + STACK_FRAME_SIZE));
6589 else
6590 smsg("%s%4d LOAD $%lld", pfx, current,
6591 (varnumber_T)(iptr->isn_arg.number));
6592 }
6593 break;
6594 case ISN_LOADOUTER:
6595 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006596 isn_outer_T *outer = &iptr->isn_arg.outer;
6597
6598 if (outer->outer_idx < 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02006599 smsg("%s%4d LOADOUTER level %d arg[%d]", pfx, current,
Bram Moolenaarcc341812022-09-19 15:54:34 +01006600 outer->outer_depth,
6601 outer->outer_idx + STACK_FRAME_SIZE);
6602 else if (outer->outer_depth < 0)
6603 smsg("%s%4d LOADOUTER $%d in loop level %d",
6604 pfx, current,
6605 outer->outer_idx,
6606 -outer->outer_depth);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006607 else
6608 smsg("%s%4d LOADOUTER level %d $%d", pfx, current,
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006609 outer->outer_depth,
6610 outer->outer_idx);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006611 }
6612 break;
6613 case ISN_LOADV:
6614 smsg("%s%4d LOADV v:%s", pfx, current,
6615 get_vim_var_name(iptr->isn_arg.number));
6616 break;
6617 case ISN_LOADSCRIPT:
6618 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006619 scriptref_T *sref = iptr->isn_arg.script.scriptref;
6620 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
6621 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006622
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006623 sv = get_script_svar(sref, -1);
6624 if (sv == NULL)
6625 smsg("%s%4d LOADSCRIPT [deleted] from %s",
6626 pfx, current, si->sn_name);
6627 else
6628 smsg("%s%4d LOADSCRIPT %s-%d from %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02006629 sv->sv_name,
6630 sref->sref_idx,
6631 si->sn_name);
6632 }
6633 break;
6634 case ISN_LOADS:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006635 case ISN_LOADEXPORT:
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:%s from %s", pfx, current,
6641 iptr->isn_type == ISN_LOADS ? "LOADS"
6642 : "LOADEXPORT",
6643 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006644 }
6645 break;
6646 case ISN_LOADAUTO:
6647 smsg("%s%4d LOADAUTO %s", pfx, current, iptr->isn_arg.string);
6648 break;
6649 case ISN_LOADG:
6650 smsg("%s%4d LOADG g:%s", pfx, current, iptr->isn_arg.string);
6651 break;
6652 case ISN_LOADB:
6653 smsg("%s%4d LOADB b:%s", pfx, current, iptr->isn_arg.string);
6654 break;
6655 case ISN_LOADW:
6656 smsg("%s%4d LOADW w:%s", pfx, current, iptr->isn_arg.string);
6657 break;
6658 case ISN_LOADT:
6659 smsg("%s%4d LOADT t:%s", pfx, current, iptr->isn_arg.string);
6660 break;
6661 case ISN_LOADGDICT:
6662 smsg("%s%4d LOAD g:", pfx, current);
6663 break;
6664 case ISN_LOADBDICT:
6665 smsg("%s%4d LOAD b:", pfx, current);
6666 break;
6667 case ISN_LOADWDICT:
6668 smsg("%s%4d LOAD w:", pfx, current);
6669 break;
6670 case ISN_LOADTDICT:
6671 smsg("%s%4d LOAD t:", pfx, current);
6672 break;
6673 case ISN_LOADOPT:
6674 smsg("%s%4d LOADOPT %s", pfx, current, iptr->isn_arg.string);
6675 break;
6676 case ISN_LOADENV:
6677 smsg("%s%4d LOADENV %s", pfx, current, iptr->isn_arg.string);
6678 break;
6679 case ISN_LOADREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006680 smsg("%s%4d LOADREG @%c", pfx, current,
6681 (int)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006682 break;
6683
6684 case ISN_STORE:
6685 if (iptr->isn_arg.number < 0)
6686 smsg("%s%4d STORE arg[%lld]", pfx, current,
6687 iptr->isn_arg.number + STACK_FRAME_SIZE);
6688 else
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006689 smsg("%s%4d STORE $%lld", pfx, current,
6690 iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006691 break;
6692 case ISN_STOREOUTER:
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006693 {
6694 isn_outer_T *outer = &iptr->isn_arg.outer;
6695
6696 if (outer->outer_depth == OUTER_LOOP_DEPTH)
6697 smsg("%s%4d STOREOUTER level 1 $%d in loop",
6698 pfx, current, outer->outer_idx);
6699 else
6700 smsg("%s%4d STOREOUTER level %d $%d", pfx, current,
6701 outer->outer_depth, outer->outer_idx);
6702 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006703 break;
6704 case ISN_STOREV:
6705 smsg("%s%4d STOREV v:%s", pfx, current,
6706 get_vim_var_name(iptr->isn_arg.number));
6707 break;
6708 case ISN_STOREAUTO:
6709 smsg("%s%4d STOREAUTO %s", pfx, current, iptr->isn_arg.string);
6710 break;
6711 case ISN_STOREG:
6712 smsg("%s%4d STOREG %s", pfx, current, iptr->isn_arg.string);
6713 break;
6714 case ISN_STOREB:
6715 smsg("%s%4d STOREB %s", pfx, current, iptr->isn_arg.string);
6716 break;
6717 case ISN_STOREW:
6718 smsg("%s%4d STOREW %s", pfx, current, iptr->isn_arg.string);
6719 break;
6720 case ISN_STORET:
6721 smsg("%s%4d STORET %s", pfx, current, iptr->isn_arg.string);
6722 break;
6723 case ISN_STORES:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006724 case ISN_STOREEXPORT:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006725 {
6726 scriptitem_T *si = SCRIPT_ITEM(
6727 iptr->isn_arg.loadstore.ls_sid);
6728
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006729 smsg("%s%4d %s %s in %s", pfx, current,
6730 iptr->isn_type == ISN_STORES
6731 ? "STORES" : "STOREEXPORT",
Bram Moolenaar4c137212021-04-19 16:48:48 +02006732 iptr->isn_arg.loadstore.ls_name, si->sn_name);
6733 }
6734 break;
6735 case ISN_STORESCRIPT:
6736 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006737 scriptref_T *sref = iptr->isn_arg.script.scriptref;
6738 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
6739 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006740
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006741 sv = get_script_svar(sref, -1);
6742 if (sv == NULL)
6743 smsg("%s%4d STORESCRIPT [deleted] in %s",
6744 pfx, current, si->sn_name);
6745 else
6746 smsg("%s%4d STORESCRIPT %s-%d in %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02006747 sv->sv_name,
6748 sref->sref_idx,
6749 si->sn_name);
6750 }
6751 break;
6752 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00006753 case ISN_STOREFUNCOPT:
6754 smsg("%s%4d %s &%s", pfx, current,
6755 iptr->isn_type == ISN_STOREOPT ? "STOREOPT" : "STOREFUNCOPT",
Bram Moolenaar4c137212021-04-19 16:48:48 +02006756 iptr->isn_arg.storeopt.so_name);
6757 break;
6758 case ISN_STOREENV:
6759 smsg("%s%4d STOREENV $%s", pfx, current, iptr->isn_arg.string);
6760 break;
6761 case ISN_STOREREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006762 smsg("%s%4d STOREREG @%c", pfx, current,
6763 (int)iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006764 break;
6765 case ISN_STORENR:
6766 smsg("%s%4d STORE %lld in $%d", pfx, current,
6767 iptr->isn_arg.storenr.stnr_val,
6768 iptr->isn_arg.storenr.stnr_idx);
6769 break;
6770
6771 case ISN_STOREINDEX:
6772 smsg("%s%4d STOREINDEX %s", pfx, current,
Bram Moolenaarf7d1c6e2023-01-16 20:47:57 +00006773 vartype_name(iptr->isn_arg.storeindex.si_vartype));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006774 break;
6775
6776 case ISN_STORERANGE:
6777 smsg("%s%4d STORERANGE", pfx, current);
6778 break;
6779
Bram Moolenaard505d172022-12-18 21:42:55 +00006780 case ISN_LOAD_CLASSMEMBER:
6781 case ISN_STORE_CLASSMEMBER:
6782 {
6783 class_T *cl = iptr->isn_arg.classmember.cm_class;
6784 int idx = iptr->isn_arg.classmember.cm_idx;
6785 ocmember_T *ocm = &cl->class_class_members[idx];
6786 smsg("%s%4d %s CLASSMEMBER %s.%s", pfx, current,
6787 iptr->isn_type == ISN_LOAD_CLASSMEMBER
6788 ? "LOAD" : "STORE",
6789 cl->class_name, ocm->ocm_name);
6790 }
6791 break;
6792
Bram Moolenaar4c137212021-04-19 16:48:48 +02006793 // constants
6794 case ISN_PUSHNR:
6795 smsg("%s%4d PUSHNR %lld", pfx, current,
6796 (varnumber_T)(iptr->isn_arg.number));
6797 break;
6798 case ISN_PUSHBOOL:
6799 case ISN_PUSHSPEC:
6800 smsg("%s%4d PUSH %s", pfx, current,
6801 get_var_special_name(iptr->isn_arg.number));
6802 break;
6803 case ISN_PUSHF:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006804 smsg("%s%4d PUSHF %g", pfx, current, iptr->isn_arg.fnumber);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006805 break;
6806 case ISN_PUSHS:
6807 smsg("%s%4d PUSHS \"%s\"", pfx, current, iptr->isn_arg.string);
6808 break;
6809 case ISN_PUSHBLOB:
6810 {
6811 char_u *r;
6812 char_u numbuf[NUMBUFLEN];
6813 char_u *tofree;
6814
6815 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
6816 smsg("%s%4d PUSHBLOB %s", pfx, current, r);
6817 vim_free(tofree);
6818 }
6819 break;
6820 case ISN_PUSHFUNC:
6821 {
6822 char *name = (char *)iptr->isn_arg.string;
6823
6824 smsg("%s%4d PUSHFUNC \"%s\"", pfx, current,
6825 name == NULL ? "[none]" : name);
6826 }
6827 break;
6828 case ISN_PUSHCHANNEL:
6829#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar397a87a2022-03-20 21:14:15 +00006830 smsg("%s%4d PUSHCHANNEL 0", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006831#endif
6832 break;
6833 case ISN_PUSHJOB:
6834#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar397a87a2022-03-20 21:14:15 +00006835 smsg("%s%4d PUSHJOB \"no process\"", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006836#endif
6837 break;
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00006838 case ISN_PUSHOBJ:
6839 smsg("%s%4d PUSHOBJ null", pfx, current);
6840 break;
6841 case ISN_PUSHCLASS:
6842 smsg("%s%4d PUSHCLASS %s", pfx, current,
Bram Moolenaar30a84472023-02-27 08:07:14 +00006843 iptr->isn_arg.classarg == NULL ? "null"
6844 : (char *)iptr->isn_arg.classarg->class_name);
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00006845 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006846 case ISN_PUSHEXC:
6847 smsg("%s%4d PUSH v:exception", pfx, current);
6848 break;
Bram Moolenaar06b77222022-01-25 15:51:56 +00006849 case ISN_AUTOLOAD:
6850 smsg("%s%4d AUTOLOAD %s", pfx, current, iptr->isn_arg.string);
6851 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006852 case ISN_UNLET:
6853 smsg("%s%4d UNLET%s %s", pfx, current,
6854 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
6855 iptr->isn_arg.unlet.ul_name);
6856 break;
6857 case ISN_UNLETENV:
6858 smsg("%s%4d UNLETENV%s $%s", pfx, current,
6859 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
6860 iptr->isn_arg.unlet.ul_name);
6861 break;
6862 case ISN_UNLETINDEX:
6863 smsg("%s%4d UNLETINDEX", pfx, current);
6864 break;
6865 case ISN_UNLETRANGE:
6866 smsg("%s%4d UNLETRANGE", pfx, current);
6867 break;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02006868 case ISN_LOCKUNLOCK:
6869 smsg("%s%4d LOCKUNLOCK %s", pfx, current, iptr->isn_arg.string);
6870 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006871 case ISN_LOCKCONST:
6872 smsg("%s%4d LOCKCONST", pfx, current);
6873 break;
6874 case ISN_NEWLIST:
6875 smsg("%s%4d NEWLIST size %lld", pfx, current,
Bram Moolenaar1936c762022-09-28 15:19:10 +01006876 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006877 break;
6878 case ISN_NEWDICT:
6879 smsg("%s%4d NEWDICT size %lld", pfx, current,
Bram Moolenaar1936c762022-09-28 15:19:10 +01006880 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006881 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00006882 case ISN_NEWPARTIAL:
6883 smsg("%s%4d NEWPARTIAL", pfx, current);
6884 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006885
6886 // function call
6887 case ISN_BCALL:
6888 {
6889 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
6890
6891 smsg("%s%4d BCALL %s(argc %d)", pfx, current,
6892 internal_func_name(cbfunc->cbf_idx),
6893 cbfunc->cbf_argcount);
6894 }
6895 break;
6896 case ISN_DCALL:
6897 {
6898 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
6899 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
6900 + cdfunc->cdf_idx;
6901
6902 smsg("%s%4d DCALL %s(argc %d)", pfx, current,
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006903 printable_func_name(df->df_ufunc),
6904 cdfunc->cdf_argcount);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006905 }
6906 break;
Bram Moolenaard0200c82023-01-28 15:19:40 +00006907 case ISN_METHODCALL:
6908 {
6909 cmfunc_T *mfunc = iptr->isn_arg.mfunc;
6910
6911 smsg("%s%4d METHODCALL %s.%s(argc %d)", pfx, current,
6912 mfunc->cmf_itf->class_name,
6913 mfunc->cmf_itf->class_obj_methods[
6914 mfunc->cmf_idx]->uf_name,
6915 mfunc->cmf_argcount);
6916 }
6917 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006918 case ISN_UCALL:
6919 {
6920 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
6921
6922 smsg("%s%4d UCALL %s(argc %d)", pfx, current,
6923 cufunc->cuf_name, cufunc->cuf_argcount);
6924 }
6925 break;
6926 case ISN_PCALL:
6927 {
6928 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
6929
6930 smsg("%s%4d PCALL%s (argc %d)", pfx, current,
6931 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
6932 }
6933 break;
6934 case ISN_PCALL_END:
6935 smsg("%s%4d PCALL end", pfx, current);
6936 break;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006937 case ISN_DEFER:
Yegappan Lakshmananf3eac692023-10-17 11:00:45 +02006938 smsg("%s%4d DEFER %d args", pfx, current,
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006939 (int)iptr->isn_arg.defer.defer_argcount);
6940 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006941 case ISN_RETURN:
6942 smsg("%s%4d RETURN", pfx, current);
6943 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02006944 case ISN_RETURN_VOID:
6945 smsg("%s%4d RETURN void", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006946 break;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006947 case ISN_RETURN_OBJECT:
6948 smsg("%s%4d RETURN object", pfx, current);
6949 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006950 case ISN_FUNCREF:
6951 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006952 funcref_T *funcref = &iptr->isn_arg.funcref;
6953 funcref_extra_T *extra = funcref->fr_extra;
6954 char_u *name;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006955
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006956 if (extra == NULL || extra->fre_func_name == NULL)
Bram Moolenaar38453522021-11-28 22:00:12 +00006957 {
6958 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
6959 + funcref->fr_dfunc_idx;
6960 name = df->df_ufunc->uf_name;
6961 }
6962 else
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006963 name = extra->fre_func_name;
Bram Moolenaar313e4722023-02-08 20:55:27 +00006964 if (extra != NULL && extra->fre_class != NULL)
6965 {
6966 smsg("%s%4d FUNCREF %s.%s", pfx, current,
6967 extra->fre_class->class_name, name);
6968 }
6969 else if (extra == NULL
6970 || extra->fre_loopvar_info.lvi_depth == 0)
6971 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006972 smsg("%s%4d FUNCREF %s", pfx, current, name);
Bram Moolenaar313e4722023-02-08 20:55:27 +00006973 }
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006974 else
Bram Moolenaarcc341812022-09-19 15:54:34 +01006975 {
6976 char_u *info = printable_loopvarinfo(
6977 &extra->fre_loopvar_info);
6978
6979 smsg("%s%4d FUNCREF %s vars %s", pfx, current,
6980 name, info);
6981 vim_free(info);
6982 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006983 }
6984 break;
6985
6986 case ISN_NEWFUNC:
6987 {
Bram Moolenaarcc341812022-09-19 15:54:34 +01006988 newfuncarg_T *arg = iptr->isn_arg.newfunc.nf_arg;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006989
Bram Moolenaarcc341812022-09-19 15:54:34 +01006990 if (arg->nfa_loopvar_info.lvi_depth == 0)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006991 smsg("%s%4d NEWFUNC %s %s", pfx, current,
6992 arg->nfa_lambda, arg->nfa_global);
6993 else
Bram Moolenaarcc341812022-09-19 15:54:34 +01006994 {
6995 char_u *info = printable_loopvarinfo(
6996 &arg->nfa_loopvar_info);
6997
6998 smsg("%s%4d NEWFUNC %s %s vars %s", pfx, current,
6999 arg->nfa_lambda, arg->nfa_global, info);
7000 vim_free(info);
7001 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02007002 }
7003 break;
7004
7005 case ISN_DEF:
7006 {
7007 char_u *name = iptr->isn_arg.string;
7008
7009 smsg("%s%4d DEF %s", pfx, current,
7010 name == NULL ? (char_u *)"" : name);
7011 }
7012 break;
7013
7014 case ISN_JUMP:
7015 {
7016 char *when = "?";
7017
7018 switch (iptr->isn_arg.jump.jump_when)
7019 {
7020 case JUMP_ALWAYS:
7021 when = "JUMP";
7022 break;
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02007023 case JUMP_NEVER:
7024 iemsg("JUMP_NEVER should not be used");
7025 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007026 case JUMP_AND_KEEP_IF_TRUE:
7027 when = "JUMP_AND_KEEP_IF_TRUE";
7028 break;
7029 case JUMP_IF_FALSE:
7030 when = "JUMP_IF_FALSE";
7031 break;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01007032 case JUMP_WHILE_FALSE:
7033 when = "JUMP_WHILE_FALSE"; // unused
7034 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007035 case JUMP_IF_COND_FALSE:
7036 when = "JUMP_IF_COND_FALSE";
7037 break;
7038 case JUMP_IF_COND_TRUE:
7039 when = "JUMP_IF_COND_TRUE";
7040 break;
7041 }
7042 smsg("%s%4d %s -> %d", pfx, current, when,
7043 iptr->isn_arg.jump.jump_where);
7044 }
7045 break;
7046
7047 case ISN_JUMP_IF_ARG_SET:
7048 smsg("%s%4d JUMP_IF_ARG_SET arg[%d] -> %d", pfx, current,
7049 iptr->isn_arg.jumparg.jump_arg_off + STACK_FRAME_SIZE,
7050 iptr->isn_arg.jump.jump_where);
7051 break;
7052
Bram Moolenaar65b0d162022-12-13 18:43:22 +00007053 case ISN_JUMP_IF_ARG_NOT_SET:
7054 smsg("%s%4d JUMP_IF_ARG_NOT_SET arg[%d] -> %d", pfx, current,
7055 iptr->isn_arg.jumparg.jump_arg_off + STACK_FRAME_SIZE,
7056 iptr->isn_arg.jump.jump_where);
7057 break;
7058
Bram Moolenaar4c137212021-04-19 16:48:48 +02007059 case ISN_FOR:
7060 {
7061 forloop_T *forloop = &iptr->isn_arg.forloop;
7062
7063 smsg("%s%4d FOR $%d -> %d", pfx, current,
Bram Moolenaarcc341812022-09-19 15:54:34 +01007064 forloop->for_loop_idx, forloop->for_end);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007065 }
7066 break;
7067
Bram Moolenaarb46c0832022-09-15 17:19:37 +01007068 case ISN_ENDLOOP:
7069 {
7070 endloop_T *endloop = &iptr->isn_arg.endloop;
7071
Bram Moolenaarcc341812022-09-19 15:54:34 +01007072 smsg("%s%4d ENDLOOP ref $%d save $%d-$%d depth %d",
7073 pfx, current,
Bram Moolenaarb46c0832022-09-15 17:19:37 +01007074 endloop->end_funcref_idx,
7075 endloop->end_var_idx,
Bram Moolenaarcc341812022-09-19 15:54:34 +01007076 endloop->end_var_idx + endloop->end_var_count - 1,
7077 endloop->end_depth);
Bram Moolenaarb46c0832022-09-15 17:19:37 +01007078 }
7079 break;
7080
7081 case ISN_WHILE:
7082 {
7083 whileloop_T *whileloop = &iptr->isn_arg.whileloop;
7084
7085 smsg("%s%4d WHILE $%d -> %d", pfx, current,
7086 whileloop->while_funcref_idx,
7087 whileloop->while_end);
7088 }
7089 break;
7090
Bram Moolenaar4c137212021-04-19 16:48:48 +02007091 case ISN_TRY:
7092 {
Bram Moolenaar0d807102021-12-21 09:42:09 +00007093 try_T *try = &iptr->isn_arg.tryref;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007094
7095 if (try->try_ref->try_finally == 0)
7096 smsg("%s%4d TRY catch -> %d, endtry -> %d",
7097 pfx, current,
7098 try->try_ref->try_catch,
7099 try->try_ref->try_endtry);
7100 else
7101 smsg("%s%4d TRY catch -> %d, finally -> %d, endtry -> %d",
7102 pfx, current,
7103 try->try_ref->try_catch,
7104 try->try_ref->try_finally,
7105 try->try_ref->try_endtry);
7106 }
7107 break;
7108 case ISN_CATCH:
Bram Moolenaar4c137212021-04-19 16:48:48 +02007109 smsg("%s%4d CATCH", pfx, current);
7110 break;
7111 case ISN_TRYCONT:
7112 {
7113 trycont_T *trycont = &iptr->isn_arg.trycont;
7114
7115 smsg("%s%4d TRY-CONTINUE %d level%s -> %d", pfx, current,
7116 trycont->tct_levels,
7117 trycont->tct_levels == 1 ? "" : "s",
7118 trycont->tct_where);
7119 }
7120 break;
7121 case ISN_FINALLY:
7122 smsg("%s%4d FINALLY", pfx, current);
7123 break;
7124 case ISN_ENDTRY:
7125 smsg("%s%4d ENDTRY", pfx, current);
7126 break;
7127 case ISN_THROW:
7128 smsg("%s%4d THROW", pfx, current);
7129 break;
7130
7131 // expression operations on number
7132 case ISN_OPNR:
7133 case ISN_OPFLOAT:
7134 case ISN_OPANY:
7135 {
7136 char *what;
7137 char *ins;
7138
7139 switch (iptr->isn_arg.op.op_type)
7140 {
7141 case EXPR_MULT: what = "*"; break;
7142 case EXPR_DIV: what = "/"; break;
7143 case EXPR_REM: what = "%"; break;
7144 case EXPR_SUB: what = "-"; break;
7145 case EXPR_ADD: what = "+"; break;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01007146 case EXPR_LSHIFT: what = "<<"; break;
7147 case EXPR_RSHIFT: what = ">>"; break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007148 default: what = "???"; break;
7149 }
7150 switch (iptr->isn_type)
7151 {
7152 case ISN_OPNR: ins = "OPNR"; break;
7153 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
7154 case ISN_OPANY: ins = "OPANY"; break;
7155 default: ins = "???"; break;
7156 }
7157 smsg("%s%4d %s %s", pfx, current, ins, what);
7158 }
7159 break;
7160
7161 case ISN_COMPAREBOOL:
7162 case ISN_COMPARESPECIAL:
Bram Moolenaar7a222242022-03-01 19:23:24 +00007163 case ISN_COMPARENULL:
Bram Moolenaar4c137212021-04-19 16:48:48 +02007164 case ISN_COMPARENR:
7165 case ISN_COMPAREFLOAT:
7166 case ISN_COMPARESTRING:
7167 case ISN_COMPAREBLOB:
7168 case ISN_COMPARELIST:
7169 case ISN_COMPAREDICT:
7170 case ISN_COMPAREFUNC:
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00007171 case ISN_COMPARECLASS:
7172 case ISN_COMPAREOBJECT:
Bram Moolenaar4c137212021-04-19 16:48:48 +02007173 case ISN_COMPAREANY:
7174 {
7175 char *p;
7176 char buf[10];
7177 char *type;
7178
7179 switch (iptr->isn_arg.op.op_type)
7180 {
7181 case EXPR_EQUAL: p = "=="; break;
7182 case EXPR_NEQUAL: p = "!="; break;
7183 case EXPR_GREATER: p = ">"; break;
7184 case EXPR_GEQUAL: p = ">="; break;
7185 case EXPR_SMALLER: p = "<"; break;
7186 case EXPR_SEQUAL: p = "<="; break;
7187 case EXPR_MATCH: p = "=~"; break;
7188 case EXPR_IS: p = "is"; break;
7189 case EXPR_ISNOT: p = "isnot"; break;
7190 case EXPR_NOMATCH: p = "!~"; break;
7191 default: p = "???"; break;
7192 }
7193 STRCPY(buf, p);
7194 if (iptr->isn_arg.op.op_ic == TRUE)
7195 strcat(buf, "?");
7196 switch(iptr->isn_type)
7197 {
7198 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
7199 case ISN_COMPARESPECIAL:
7200 type = "COMPARESPECIAL"; break;
Bram Moolenaar7a222242022-03-01 19:23:24 +00007201 case ISN_COMPARENULL: type = "COMPARENULL"; break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007202 case ISN_COMPARENR: type = "COMPARENR"; break;
7203 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
7204 case ISN_COMPARESTRING:
7205 type = "COMPARESTRING"; break;
7206 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
7207 case ISN_COMPARELIST: type = "COMPARELIST"; break;
7208 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
7209 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00007210 case ISN_COMPARECLASS: type = "COMPARECLASS"; break;
7211 case ISN_COMPAREOBJECT:
7212 type = "COMPAREOBJECT"; break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007213 case ISN_COMPAREANY: type = "COMPAREANY"; break;
7214 default: type = "???"; break;
7215 }
7216
7217 smsg("%s%4d %s %s", pfx, current, type, buf);
7218 }
7219 break;
7220
7221 case ISN_ADDLIST: smsg("%s%4d ADDLIST", pfx, current); break;
7222 case ISN_ADDBLOB: smsg("%s%4d ADDBLOB", pfx, current); break;
7223
7224 // expression operations
LemonBoy372bcce2022-04-25 12:43:20 +01007225 case ISN_CONCAT:
7226 smsg("%s%4d CONCAT size %lld", pfx, current,
7227 (varnumber_T)(iptr->isn_arg.number));
7228 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007229 case ISN_STRINDEX: smsg("%s%4d STRINDEX", pfx, current); break;
7230 case ISN_STRSLICE: smsg("%s%4d STRSLICE", pfx, current); break;
7231 case ISN_BLOBINDEX: smsg("%s%4d BLOBINDEX", pfx, current); break;
7232 case ISN_BLOBSLICE: smsg("%s%4d BLOBSLICE", pfx, current); break;
7233 case ISN_LISTAPPEND: smsg("%s%4d LISTAPPEND", pfx, current); break;
7234 case ISN_BLOBAPPEND: smsg("%s%4d BLOBAPPEND", pfx, current); break;
7235 case ISN_LISTINDEX: smsg("%s%4d LISTINDEX", pfx, current); break;
7236 case ISN_LISTSLICE: smsg("%s%4d LISTSLICE", pfx, current); break;
7237 case ISN_ANYINDEX: smsg("%s%4d ANYINDEX", pfx, current); break;
7238 case ISN_ANYSLICE: smsg("%s%4d ANYSLICE", pfx, current); break;
7239 case ISN_SLICE: smsg("%s%4d SLICE %lld",
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02007240 pfx, current, iptr->isn_arg.number); break;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02007241 case ISN_GETITEM: smsg("%s%4d ITEM %lld%s", pfx, current,
7242 iptr->isn_arg.getitem.gi_index,
7243 iptr->isn_arg.getitem.gi_with_op ?
7244 " with op" : ""); break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007245 case ISN_MEMBER: smsg("%s%4d MEMBER", pfx, current); break;
7246 case ISN_STRINGMEMBER: smsg("%s%4d MEMBER %s", pfx, current,
7247 iptr->isn_arg.string); break;
Yegappan Lakshmanan5a05d372023-09-29 19:43:11 +02007248 case ISN_GET_OBJ_MEMBER: smsg("%s%4d OBJ_MEMBER %d", pfx, current,
7249 (int)iptr->isn_arg.classmember.cm_idx);
Ernie Rael00df69e2023-09-05 07:38:09 +02007250 break;
Yegappan Lakshmanan5a05d372023-09-29 19:43:11 +02007251 case ISN_GET_ITF_MEMBER: smsg("%s%4d ITF_MEMBER %d on %s",
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00007252 pfx, current,
7253 (int)iptr->isn_arg.classmember.cm_idx,
Yegappan Lakshmanan5a05d372023-09-29 19:43:11 +02007254 iptr->isn_arg.classmember.cm_class->class_name);
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00007255 break;
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00007256 case ISN_STORE_THIS: smsg("%s%4d STORE_THIS %d", pfx, current,
Bram Moolenaarffdaca92022-12-09 21:41:48 +00007257 (int)iptr->isn_arg.number); break;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02007258 case ISN_CLEARDICT: smsg("%s%4d CLEARDICT", pfx, current); break;
7259 case ISN_USEDICT: smsg("%s%4d USEDICT", pfx, current); break;
7260
Bram Moolenaar4c137212021-04-19 16:48:48 +02007261 case ISN_NEGATENR: smsg("%s%4d NEGATENR", pfx, current); break;
7262
Bram Moolenaar4c137212021-04-19 16:48:48 +02007263 case ISN_CHECKTYPE:
7264 {
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01007265 checktype_T *ct = &iptr->isn_arg.type;
Bram Moolenaarc6951a72022-12-29 20:56:24 +00007266 char *tofree = NULL;
7267 char *typename;
7268
7269 if (ct->ct_type->tt_type == VAR_FLOAT
7270 && (ct->ct_type->tt_flags & TTFLAG_NUMBER_OK))
7271 typename = "float|number";
7272 else
7273 typename = type_name(ct->ct_type, &tofree);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007274
7275 if (ct->ct_arg_idx == 0)
7276 smsg("%s%4d CHECKTYPE %s stack[%d]", pfx, current,
Bram Moolenaarc6951a72022-12-29 20:56:24 +00007277 typename,
Bram Moolenaar4c137212021-04-19 16:48:48 +02007278 (int)ct->ct_off);
7279 else
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01007280 smsg("%s%4d CHECKTYPE %s stack[%d] %s %d",
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02007281 pfx, current,
Bram Moolenaarc6951a72022-12-29 20:56:24 +00007282 typename,
Bram Moolenaar4c137212021-04-19 16:48:48 +02007283 (int)ct->ct_off,
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01007284 ct->ct_is_var ? "var": "arg",
Bram Moolenaar4c137212021-04-19 16:48:48 +02007285 (int)ct->ct_arg_idx);
7286 vim_free(tofree);
7287 break;
7288 }
7289 case ISN_CHECKLEN: smsg("%s%4d CHECKLEN %s%d", pfx, current,
7290 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
7291 iptr->isn_arg.checklen.cl_min_len);
7292 break;
7293 case ISN_SETTYPE:
7294 {
7295 char *tofree;
7296
7297 smsg("%s%4d SETTYPE %s", pfx, current,
7298 type_name(iptr->isn_arg.type.ct_type, &tofree));
7299 vim_free(tofree);
7300 break;
7301 }
7302 case ISN_COND2BOOL: smsg("%s%4d COND2BOOL", pfx, current); break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007303 case ISN_2BOOL: if (iptr->isn_arg.tobool.invert)
7304 smsg("%s%4d INVERT %d (!val)", pfx, current,
7305 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007306 else
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007307 smsg("%s%4d 2BOOL %d (!!val)", pfx, current,
7308 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007309 break;
7310 case ISN_2STRING: smsg("%s%4d 2STRING stack[%lld]", pfx, current,
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007311 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02007312 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007313 case ISN_2STRING_ANY: smsg("%s%4d 2STRING_ANY stack[%lld]",
7314 pfx, current,
7315 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02007316 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007317 case ISN_RANGE: smsg("%s%4d RANGE %s", pfx, current,
7318 iptr->isn_arg.string);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007319 break;
7320 case ISN_PUT:
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01007321 if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE)
Bram Moolenaar4c137212021-04-19 16:48:48 +02007322 smsg("%s%4d PUT %c above range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007323 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007324 else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE)
7325 smsg("%s%4d PUT %c range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007326 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007327 else
7328 smsg("%s%4d PUT %c %ld", pfx, current,
7329 iptr->isn_arg.put.put_regname,
7330 (long)iptr->isn_arg.put.put_lnum);
7331 break;
7332
Bram Moolenaar4c137212021-04-19 16:48:48 +02007333 case ISN_CMDMOD:
7334 {
7335 char_u *buf;
7336 size_t len = produce_cmdmods(
7337 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
7338
7339 buf = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02007340 if (likely(buf != NULL))
Bram Moolenaar4c137212021-04-19 16:48:48 +02007341 {
7342 (void)produce_cmdmods(
7343 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
7344 smsg("%s%4d CMDMOD %s", pfx, current, buf);
7345 vim_free(buf);
7346 }
7347 break;
7348 }
7349 case ISN_CMDMOD_REV: smsg("%s%4d CMDMOD_REV", pfx, current); break;
7350
7351 case ISN_PROF_START:
Bram Moolenaare99d4222021-06-13 14:01:26 +02007352 smsg("%s%4d PROFILE START line %d", pfx, current,
7353 iptr->isn_lnum);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007354 break;
7355
7356 case ISN_PROF_END:
7357 smsg("%s%4d PROFILE END", pfx, current);
7358 break;
7359
Bram Moolenaare99d4222021-06-13 14:01:26 +02007360 case ISN_DEBUG:
Bram Moolenaar8cec9272021-06-23 20:20:53 +02007361 smsg("%s%4d DEBUG line %d-%d varcount %lld", pfx, current,
7362 iptr->isn_arg.debug.dbg_break_lnum + 1,
7363 iptr->isn_lnum,
7364 iptr->isn_arg.debug.dbg_var_names_len);
Bram Moolenaare99d4222021-06-13 14:01:26 +02007365 break;
7366
Bram Moolenaar4c137212021-04-19 16:48:48 +02007367 case ISN_UNPACK: smsg("%s%4d UNPACK %d%s", pfx, current,
7368 iptr->isn_arg.unpack.unp_count,
7369 iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : "");
7370 break;
7371 case ISN_SHUFFLE: smsg("%s%4d SHUFFLE %d up %d", pfx, current,
7372 iptr->isn_arg.shuffle.shfl_item,
7373 iptr->isn_arg.shuffle.shfl_up);
7374 break;
7375 case ISN_DROP: smsg("%s%4d DROP", pfx, current); break;
7376
7377 case ISN_FINISH: // End of list of instructions for ISN_SUBSTITUTE.
7378 return;
7379 }
7380
7381 out_flush(); // output one line at a time
7382 ui_breakcheck();
7383 if (got_int)
7384 break;
7385 }
7386}
7387
7388/*
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02007389 * Handle command line completion for the :disassemble command.
7390 */
7391 void
7392set_context_in_disassemble_cmd(expand_T *xp, char_u *arg)
7393{
7394 char_u *p;
7395
7396 // Default: expand user functions, "debug" and "profile"
7397 xp->xp_context = EXPAND_DISASSEMBLE;
7398 xp->xp_pattern = arg;
7399
7400 // first argument already typed: only user function names
7401 if (*arg != NUL && *(p = skiptowhite(arg)) != NUL)
7402 {
7403 xp->xp_context = EXPAND_USER_FUNC;
7404 xp->xp_pattern = skipwhite(p);
7405 }
7406}
7407
7408/*
7409 * Function given to ExpandGeneric() to obtain the list of :disassemble
7410 * arguments.
7411 */
7412 char_u *
7413get_disassemble_argument(expand_T *xp, int idx)
7414{
7415 if (idx == 0)
7416 return (char_u *)"debug";
7417 if (idx == 1)
7418 return (char_u *)"profile";
7419 return get_user_func_name(xp, idx - 2);
7420}
7421
7422/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01007423 * ":disassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01007424 * We don't really need this at runtime, but we do have tests that require it,
7425 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007426 */
7427 void
7428ex_disassemble(exarg_T *eap)
7429{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01007430 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01007431 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007432 dfunc_T *dfunc;
Bram Moolenaardafef512022-05-21 21:55:55 +01007433 isn_T *instr = NULL; // init to shut up gcc warning
7434 int instr_count = 0; // init to shut up gcc warning
Bram Moolenaar5a01caa2022-05-21 18:56:58 +01007435 compiletype_T compile_type = CT_NONE;
Bram Moolenaare99d4222021-06-13 14:01:26 +02007436
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01007437 ufunc = find_func_by_name(arg, &compile_type);
Bram Moolenaara26b9702020-04-18 19:53:28 +02007438 if (ufunc == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007439 return;
Bram Moolenaare99d4222021-06-13 14:01:26 +02007440 if (func_needs_compiling(ufunc, compile_type)
7441 && compile_def_function(ufunc, FALSE, compile_type, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02007442 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007443 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007444 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007445 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007446 return;
7447 }
Bram Moolenaar6db660b2021-08-01 14:08:54 +02007448 msg((char *)printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007449
7450 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
Bram Moolenaare99d4222021-06-13 14:01:26 +02007451 switch (compile_type)
7452 {
7453 case CT_PROFILE:
Bram Moolenaarf002a412021-01-24 13:34:18 +01007454#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02007455 instr = dfunc->df_instr_prof;
7456 instr_count = dfunc->df_instr_prof_count;
7457 break;
Bram Moolenaarf002a412021-01-24 13:34:18 +01007458#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02007459 // FALLTHROUGH
7460 case CT_NONE:
7461 instr = dfunc->df_instr;
7462 instr_count = dfunc->df_instr_count;
7463 break;
7464 case CT_DEBUG:
7465 instr = dfunc->df_instr_debug;
7466 instr_count = dfunc->df_instr_debug_count;
7467 break;
7468 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007469
Bram Moolenaar4c137212021-04-19 16:48:48 +02007470 list_instructions("", instr, instr_count, ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007471}
7472
7473/*
Bram Moolenaar13106602020-10-04 16:06:05 +02007474 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007475 * list, etc. Mostly like what JavaScript does, except that empty list and
7476 * empty dictionary are FALSE.
7477 */
7478 int
7479tv2bool(typval_T *tv)
7480{
7481 switch (tv->v_type)
7482 {
7483 case VAR_NUMBER:
7484 return tv->vval.v_number != 0;
7485 case VAR_FLOAT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007486 return tv->vval.v_float != 0.0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007487 case VAR_PARTIAL:
7488 return tv->vval.v_partial != NULL;
7489 case VAR_FUNC:
7490 case VAR_STRING:
7491 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
7492 case VAR_LIST:
7493 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
7494 case VAR_DICT:
7495 return tv->vval.v_dict != NULL
7496 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
7497 case VAR_BOOL:
7498 case VAR_SPECIAL:
7499 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
7500 case VAR_JOB:
7501#ifdef FEAT_JOB_CHANNEL
7502 return tv->vval.v_job != NULL;
7503#else
7504 break;
7505#endif
7506 case VAR_CHANNEL:
7507#ifdef FEAT_JOB_CHANNEL
7508 return tv->vval.v_channel != NULL;
7509#else
7510 break;
7511#endif
7512 case VAR_BLOB:
7513 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
7514 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02007515 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007516 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02007517 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00007518 case VAR_CLASS:
7519 case VAR_OBJECT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007520 break;
7521 }
7522 return FALSE;
7523}
7524
Bram Moolenaarea2d4072020-11-12 12:08:51 +01007525 void
7526emsg_using_string_as(typval_T *tv, int as_number)
7527{
7528 semsg(_(as_number ? e_using_string_as_number_str
7529 : e_using_string_as_bool_str),
7530 tv->vval.v_string == NULL
7531 ? (char_u *)"" : tv->vval.v_string);
7532}
7533
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007534/*
7535 * If "tv" is a string give an error and return FAIL.
7536 */
7537 int
7538check_not_string(typval_T *tv)
7539{
7540 if (tv->v_type == VAR_STRING)
7541 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +01007542 emsg_using_string_as(tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007543 clear_tv(tv);
7544 return FAIL;
7545 }
7546 return OK;
7547}
7548
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007549
7550#endif // FEAT_EVAL