blob: dd3d263b523bbf320df67c7924bf768b42b15f48 [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
1144 // first statement in the function will be executed. Save and restore
1145 // the try/catch/throw exception state.
1146 int save_trylevel = trylevel;
1147 int save_did_throw = did_throw;
1148 int save_need_rethrow = need_rethrow;
1149
1150 trylevel = 0;
1151 did_throw = FALSE;
1152 need_rethrow = FALSE;
1153
zeertzjqa1f2b5d2023-04-18 21:04:53 +01001154 (void)call_func(name, -1, &rettv, argcount, argvars, &funcexe);
1155
Yegappan Lakshmanan06725952023-10-18 11:47:37 +02001156 trylevel = save_trylevel;
1157 did_throw = save_did_throw;
1158 need_rethrow = save_need_rethrow;
1159
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001160 clear_tv(&rettv);
zeertzjqa1f2b5d2023-04-18 21:04:53 +01001161 vim_free(name);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001162 }
1163}
1164
1165/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001166 * Return from the current function.
1167 */
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001168 static int
Bram Moolenaar4c137212021-04-19 16:48:48 +02001169func_return(ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001170{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001171 int idx;
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01001172 int ret_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001173 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1174 + ectx->ec_dfunc_idx;
1175 int argcount = ufunc_argcount(dfunc->df_ufunc);
Bram Moolenaarc620c052020-07-08 15:16:19 +02001176 estack_T *entry;
Bram Moolenaar12d26532021-02-19 19:13:21 +01001177 int prev_dfunc_idx = STACK_TV(ectx->ec_frame_idx
1178 + STACK_FRAME_FUNC_OFF)->vval.v_number;
Bram Moolenaarb06b50d2021-04-26 21:39:25 +02001179 funclocal_T *floc;
1180#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01001181 dfunc_T *prev_dfunc = ((dfunc_T *)def_functions.ga_data)
1182 + prev_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001183
Bram Moolenaar12d26532021-02-19 19:13:21 +01001184 if (do_profiling == PROF_YES)
1185 {
1186 ufunc_T *caller = prev_dfunc->df_ufunc;
1187
1188 if (dfunc->df_ufunc->uf_profiling
1189 || (caller != NULL && caller->uf_profiling))
1190 {
1191 profile_may_end_func(((profinfo_T *)profile_info_ga.ga_data)
1192 + profile_info_ga.ga_len - 1, dfunc->df_ufunc, caller);
1193 --profile_info_ga.ga_len;
1194 }
1195 }
1196#endif
Bram Moolenaar919c12c2021-12-14 14:29:16 +00001197
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001198 if (dfunc->df_defer_var_idx > 0)
1199 invoke_defer_funcs(ectx);
1200
Bram Moolenaar919c12c2021-12-14 14:29:16 +00001201 // No check for uf_refcount being zero, cannot think of a way that would
1202 // happen.
Bram Moolenaarc970e422021-03-17 15:03:04 +01001203 --dfunc->df_ufunc->uf_calls;
1204
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001205 // execution context goes one level up
Bram Moolenaarc620c052020-07-08 15:16:19 +02001206 entry = estack_pop();
1207 if (entry != NULL)
Bram Moolenaarc70fe462021-04-17 17:59:19 +02001208 current_sctx = entry->es_save_sctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001209
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001210 if (handle_closure_in_use(ectx, TRUE) == FAIL)
1211 return FAIL;
1212
Bram Moolenaar574950d2023-01-03 19:08:50 +00001213 // Clear the arguments. If this was an object method also clear the
1214 // object, it is just before the arguments.
1215 int top = ectx->ec_frame_idx - argcount;
Yegappan Lakshmanan1db15142023-09-19 20:34:05 +02001216 if (IS_OBJECT_METHOD(dfunc->df_ufunc))
Bram Moolenaar574950d2023-01-03 19:08:50 +00001217 --top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001218 for (idx = top; idx < ectx->ec_frame_idx; ++idx)
1219 clear_tv(STACK_TV(idx));
1220
1221 // Clear local variables and temp values, but not the return value.
1222 for (idx = ectx->ec_frame_idx + STACK_FRAME_SIZE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001223 idx < ectx->ec_stack.ga_len - 1; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001224 clear_tv(STACK_TV(idx));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001225
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01001226 // The return value should be on top of the stack. However, when aborting
1227 // it may not be there and ec_frame_idx is the top of the stack.
1228 ret_idx = ectx->ec_stack.ga_len - 1;
Bram Moolenaar0186e582021-01-10 18:33:11 +01001229 if (ret_idx == ectx->ec_frame_idx + STACK_FRAME_IDX_OFF)
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01001230 ret_idx = 0;
1231
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001232 if (ectx->ec_outer_ref != NULL)
1233 {
1234 if (ectx->ec_outer_ref->or_outer_allocated)
1235 vim_free(ectx->ec_outer_ref->or_outer);
1236 partial_unref(ectx->ec_outer_ref->or_partial);
1237 vim_free(ectx->ec_outer_ref);
1238 }
Bram Moolenaar0186e582021-01-10 18:33:11 +01001239
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001240 // Restore the previous frame.
Bram Moolenaar12d26532021-02-19 19:13:21 +01001241 ectx->ec_dfunc_idx = prev_dfunc_idx;
Bram Moolenaar0186e582021-01-10 18:33:11 +01001242 ectx->ec_iidx = STACK_TV(ectx->ec_frame_idx
1243 + STACK_FRAME_IIDX_OFF)->vval.v_number;
Bram Moolenaar5930ddc2021-04-26 20:32:59 +02001244 ectx->ec_instr = (void *)STACK_TV(ectx->ec_frame_idx
1245 + STACK_FRAME_INSTR_OFF)->vval.v_string;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001246 ectx->ec_outer_ref = (void *)STACK_TV(ectx->ec_frame_idx
Bram Moolenaar0186e582021-01-10 18:33:11 +01001247 + STACK_FRAME_OUTER_OFF)->vval.v_string;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001248 floc = (void *)STACK_TV(ectx->ec_frame_idx
1249 + STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string;
Bram Moolenaar5366e1a2020-10-01 13:01:34 +02001250 // restoring ec_frame_idx must be last
Bram Moolenaar0186e582021-01-10 18:33:11 +01001251 ectx->ec_frame_idx = STACK_TV(ectx->ec_frame_idx
1252 + STACK_FRAME_IDX_OFF)->vval.v_number;
Bram Moolenaard386e922021-04-25 14:48:49 +02001253
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001254 if (floc == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02001255 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001256 else
1257 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02001258 ectx->ec_funclocal = *floc;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001259 vim_free(floc);
1260 }
1261
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01001262 if (ret_idx > 0)
1263 {
1264 // Reset the stack to the position before the call, with a spot for the
1265 // return value, moved there from above the frame.
1266 ectx->ec_stack.ga_len = top + 1;
1267 *STACK_TV_BOT(-1) = *STACK_TV(ret_idx);
1268 }
1269 else
1270 // Reset the stack to the position before the call.
1271 ectx->ec_stack.ga_len = top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001272
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001273 funcdepth_decrement();
Bram Moolenaare99d4222021-06-13 14:01:26 +02001274 --ex_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001275 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001276}
1277
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001278/*
1279 * Prepare arguments and rettv for calling a builtin or user function.
1280 */
1281 static int
1282call_prepare(int argcount, typval_T *argvars, ectx_T *ectx)
1283{
1284 int idx;
1285 typval_T *tv;
1286
1287 // Move arguments from bottom of the stack to argvars[] and add terminator.
1288 for (idx = 0; idx < argcount; ++idx)
1289 argvars[idx] = *STACK_TV_BOT(idx - argcount);
1290 argvars[argcount].v_type = VAR_UNKNOWN;
1291
1292 // Result replaces the arguments on the stack.
1293 if (argcount > 0)
1294 ectx->ec_stack.ga_len -= argcount - 1;
Bram Moolenaar35578162021-08-02 19:10:38 +02001295 else if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001296 return FAIL;
1297 else
1298 ++ectx->ec_stack.ga_len;
1299
1300 // Default return value is zero.
1301 tv = STACK_TV_BOT(-1);
1302 tv->v_type = VAR_NUMBER;
1303 tv->vval.v_number = 0;
Bram Moolenaar24565cf2022-03-28 18:16:52 +01001304 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001305
1306 return OK;
1307}
1308
1309/*
1310 * Call a builtin function by index.
1311 */
1312 static int
1313call_bfunc(int func_idx, int argcount, ectx_T *ectx)
1314{
1315 typval_T argvars[MAX_FUNC_ARGS];
1316 int idx;
Bram Moolenaar171fb922020-10-28 16:54:47 +01001317 int did_emsg_before = did_emsg;
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001318 ectx_T *prev_ectx = current_ectx;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001319 char *save_func_name = ectx->ec_where.wt_func_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001320
1321 if (call_prepare(argcount, argvars, ectx) == FAIL)
1322 return FAIL;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001323 ectx->ec_where.wt_func_name = internal_func_name(func_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001324
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001325 // Call the builtin function. Set "current_ectx" so that when it
1326 // recursively invokes call_def_function() a closure context can be set.
1327 current_ectx = ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001328 call_internal_func_by_idx(func_idx, argvars, STACK_TV_BOT(-1));
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001329 current_ectx = prev_ectx;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001330 ectx->ec_where.wt_func_name = save_func_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001331
1332 // Clear the arguments.
1333 for (idx = 0; idx < argcount; ++idx)
1334 clear_tv(&argvars[idx]);
Bram Moolenaar015f4262020-05-05 21:25:22 +02001335
Bram Moolenaar57f799e2020-12-12 20:42:19 +01001336 if (did_emsg > did_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +02001337 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001338 return OK;
1339}
1340
1341/*
1342 * Execute a user defined function.
Bram Moolenaarab360522021-01-10 14:02:28 +01001343 * If the function is compiled this will add a stack frame and set the
1344 * instruction pointer at the start of the function.
1345 * Otherwise the function is called here.
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001346 * If "pt" is not null use "pt->pt_outer" for ec_outer_ref->or_outer.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001347 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001348 */
1349 static int
Bram Moolenaar0186e582021-01-10 18:33:11 +01001350call_ufunc(
1351 ufunc_T *ufunc,
1352 partial_T *pt,
1353 int argcount,
1354 ectx_T *ectx,
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001355 isn_T *iptr,
1356 dict_T *selfdict)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001357{
1358 typval_T argvars[MAX_FUNC_ARGS];
1359 funcexe_T funcexe;
Bram Moolenaar0917e862023-02-18 14:42:44 +00001360 funcerror_T error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001361 int idx;
Bram Moolenaar28ee8922020-10-28 20:20:00 +01001362 int did_emsg_before = did_emsg;
Bram Moolenaar139575d2022-03-15 19:29:30 +00001363 compiletype_T compile_type = get_compile_type(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001364
Bram Moolenaare99d4222021-06-13 14:01:26 +02001365 if (func_needs_compiling(ufunc, compile_type)
1366 && compile_def_function(ufunc, FALSE, compile_type, NULL)
1367 == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02001368 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001369 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001370 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +01001371 error = check_user_func_argcount(ufunc, argcount);
Bram Moolenaar50824712020-12-20 21:10:17 +01001372 if (error != FCERR_UNKNOWN)
1373 {
1374 if (error == FCERR_TOOMANY)
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001375 semsg(_(e_too_many_arguments_for_function_str),
1376 printable_func_name(ufunc));
Bram Moolenaar50824712020-12-20 21:10:17 +01001377 else
Bram Moolenaare1242042021-12-16 20:56:57 +00001378 semsg(_(e_not_enough_arguments_for_function_str),
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001379 printable_func_name(ufunc));
Bram Moolenaar50824712020-12-20 21:10:17 +01001380 return FAIL;
1381 }
1382
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001383 // The function has been compiled, can call it quickly. For a function
1384 // that was defined later: we can call it directly next time.
1385 if (iptr != NULL)
1386 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001387 delete_instr(iptr);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001388 iptr->isn_type = ISN_DCALL;
1389 iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1390 iptr->isn_arg.dfunc.cdf_argcount = argcount;
1391 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001392 return call_dfunc(ufunc->uf_dfunc_idx, pt, argcount, ectx);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001393 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001394
1395 if (call_prepare(argcount, argvars, ectx) == FAIL)
1396 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +02001397 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00001398 funcexe.fe_evaluate = TRUE;
1399 funcexe.fe_selfdict = selfdict != NULL ? selfdict : dict_stack_get_dict();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001400
1401 // Call the user function. Result goes in last position on the stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001402 error = call_user_func_check(ufunc, argcount, argvars,
Bram Moolenaar851f86b2021-12-13 14:26:44 +00001403 STACK_TV_BOT(-1), &funcexe, funcexe.fe_selfdict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001404
1405 // Clear the arguments.
1406 for (idx = 0; idx < argcount; ++idx)
1407 clear_tv(&argvars[idx]);
1408
1409 if (error != FCERR_NONE)
1410 {
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01001411 user_func_error(error, printable_func_name(ufunc),
1412 funcexe.fe_found_var);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001413 return FAIL;
1414 }
Bram Moolenaar28ee8922020-10-28 20:20:00 +01001415 if (did_emsg > did_emsg_before)
Bram Moolenaared677f52020-08-12 16:38:10 +02001416 // Error other than from calling the function itself.
1417 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001418 return OK;
1419}
1420
1421/*
Bram Moolenaara91a7132021-03-25 21:12:15 +01001422 * If command modifiers were applied restore them.
1423 */
1424 static void
1425may_restore_cmdmod(funclocal_T *funclocal)
1426{
1427 if (funclocal->floc_restore_cmdmod)
1428 {
1429 cmdmod.cmod_filter_regmatch.regprog = NULL;
1430 undo_cmdmod(&cmdmod);
1431 cmdmod = funclocal->floc_save_cmdmod;
1432 funclocal->floc_restore_cmdmod = FALSE;
1433 }
1434}
1435
1436/*
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001437 * Return TRUE if an error was given (not caught in try/catch) or CTRL-C was
1438 * pressed.
Bram Moolenaara1773442020-08-12 15:21:22 +02001439 */
1440 static int
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001441vim9_aborting(int prev_uncaught_emsg)
Bram Moolenaara1773442020-08-12 15:21:22 +02001442{
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001443 return uncaught_emsg > prev_uncaught_emsg || got_int || did_throw;
Bram Moolenaara1773442020-08-12 15:21:22 +02001444}
1445
1446/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001447 * Execute a function by "name".
1448 * This can be a builtin function or a user function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001449 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001450 * Returns FAIL if not found without an error message.
1451 */
1452 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001453call_by_name(
1454 char_u *name,
1455 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001456 ectx_T *ectx,
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001457 isn_T *iptr,
1458 dict_T *selfdict)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001459{
1460 ufunc_T *ufunc;
1461
1462 if (builtin_function(name, -1))
1463 {
1464 int func_idx = find_internal_func(name);
1465
Bram Moolenaar1983f1a2022-02-28 20:55:02 +00001466 if (func_idx < 0) // Impossible?
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001467 return FAIL;
Bram Moolenaar389df252020-07-09 21:20:47 +02001468 if (check_internal_func(func_idx, argcount) < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001469 return FAIL;
1470 return call_bfunc(func_idx, argcount, ectx);
1471 }
1472
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00001473 ufunc = find_func(name, FALSE);
Bram Moolenaara1773442020-08-12 15:21:22 +02001474
1475 if (ufunc == NULL)
1476 {
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001477 int prev_uncaught_emsg = uncaught_emsg;
Bram Moolenaara1773442020-08-12 15:21:22 +02001478
1479 if (script_autoload(name, TRUE))
1480 // loaded a package, search for the function again
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00001481 ufunc = find_func(name, FALSE);
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001482
1483 if (vim9_aborting(prev_uncaught_emsg))
Bram Moolenaara1773442020-08-12 15:21:22 +02001484 return FAIL; // bail out if loading the script caused an error
1485 }
1486
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001487 if (ufunc != NULL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001488 {
Bram Moolenaar0d89d8a2022-12-31 14:01:24 +00001489 if (check_ufunc_arg_types(ufunc, argcount, 0, ectx) == FAIL)
1490 return FAIL;
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001491
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001492 return call_ufunc(ufunc, NULL, argcount, ectx, iptr, selfdict);
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001493 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001494
1495 return FAIL;
1496}
1497
1498 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001499call_partial(
1500 typval_T *tv,
1501 int argcount_arg,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001502 ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001503{
Bram Moolenaara90afb92020-07-15 22:38:56 +02001504 int argcount = argcount_arg;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001505 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001506 int called_emsg_before = called_emsg;
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +01001507 int res = FAIL;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001508 dict_T *selfdict = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001509
1510 if (tv->v_type == VAR_PARTIAL)
1511 {
Bram Moolenaara90afb92020-07-15 22:38:56 +02001512 partial_T *pt = tv->vval.v_partial;
1513 int i;
1514
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +02001515 if (pt->pt_obj != NULL)
1516 {
1517 // partial with an object method. Push the object before the
1518 // function arguments.
1519 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
1520 return FAIL;
1521 for (i = 1; i <= argcount; ++i)
1522 *STACK_TV_BOT(-i + 1) = *STACK_TV_BOT(-i);
1523
1524 typval_T *obj_tv = STACK_TV_BOT(-argcount);
1525 obj_tv->v_type = VAR_OBJECT;
1526 obj_tv->v_lock = 0;
1527 obj_tv->vval.v_object = pt->pt_obj;
1528 ++pt->pt_obj->obj_refcount;
1529 ++ectx->ec_stack.ga_len;
1530 }
1531
Bram Moolenaara90afb92020-07-15 22:38:56 +02001532 if (pt->pt_argc > 0)
1533 {
1534 // Make space for arguments from the partial, shift the "argcount"
1535 // arguments up.
Bram Moolenaar35578162021-08-02 19:10:38 +02001536 if (GA_GROW_FAILS(&ectx->ec_stack, pt->pt_argc))
Bram Moolenaara90afb92020-07-15 22:38:56 +02001537 return FAIL;
1538 for (i = 1; i <= argcount; ++i)
1539 *STACK_TV_BOT(-i + pt->pt_argc) = *STACK_TV_BOT(-i);
1540 ectx->ec_stack.ga_len += pt->pt_argc;
1541 argcount += pt->pt_argc;
1542
1543 // copy the arguments from the partial onto the stack
1544 for (i = 0; i < pt->pt_argc; ++i)
1545 copy_tv(&pt->pt_argv[i], STACK_TV_BOT(-argcount + i));
1546 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001547 selfdict = pt->pt_dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001548
1549 if (pt->pt_func != NULL)
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001550 return call_ufunc(pt->pt_func, pt, argcount, ectx, NULL, selfdict);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001551
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001552 name = pt->pt_name;
1553 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001554 else if (tv->v_type == VAR_FUNC)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001555 name = tv->vval.v_string;
Bram Moolenaar95006e32020-08-29 17:47:08 +02001556 if (name != NULL)
1557 {
Bram Moolenaar0917e862023-02-18 14:42:44 +00001558 char_u fname_buf[FLEN_FIXED + 1];
1559 char_u *tofree = NULL;
1560 funcerror_T error = FCERR_NONE;
1561 char_u *fname;
Bram Moolenaar95006e32020-08-29 17:47:08 +02001562
1563 // May need to translate <SNR>123_ to K_SNR.
1564 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
1565 if (error != FCERR_NONE)
1566 res = FAIL;
1567 else
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001568 res = call_by_name(fname, argcount, ectx, NULL, selfdict);
Bram Moolenaar95006e32020-08-29 17:47:08 +02001569 vim_free(tofree);
1570 }
1571
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +01001572 if (res == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001573 {
1574 if (called_emsg == called_emsg_before)
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001575 emsg_funcname(e_unknown_function_str,
Bram Moolenaar015f4262020-05-05 21:25:22 +02001576 name == NULL ? (char_u *)"[unknown]" : name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001577 return FAIL;
1578 }
1579 return OK;
1580}
1581
1582/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001583 * Check if "lock" is VAR_LOCKED or VAR_FIXED. If so give an error and return
1584 * TRUE.
1585 */
1586 static int
1587error_if_locked(int lock, char *error)
1588{
1589 if (lock & (VAR_LOCKED | VAR_FIXED))
1590 {
1591 emsg(_(error));
1592 return TRUE;
1593 }
1594 return FALSE;
1595}
1596
1597/*
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001598 * Give an error if "tv" is not a number and return FAIL.
1599 */
1600 static int
1601check_for_number(typval_T *tv)
1602{
1603 if (tv->v_type != VAR_NUMBER)
1604 {
1605 semsg(_(e_expected_str_but_got_str),
1606 vartype_name(VAR_NUMBER), vartype_name(tv->v_type));
1607 return FAIL;
1608 }
1609 return OK;
1610}
1611
1612/*
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001613 * Store "tv" in variable "name".
1614 * This is for s: and g: variables.
1615 */
1616 static void
1617store_var(char_u *name, typval_T *tv)
1618{
1619 funccal_entry_T entry;
Bram Moolenaard877a572021-04-01 19:42:48 +02001620 int flags = ASSIGN_DECL;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001621
Bram Moolenaard877a572021-04-01 19:42:48 +02001622 if (tv->v_lock)
1623 flags |= ASSIGN_CONST;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001624 save_funccal(&entry);
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001625 set_var_const(name, 0, NULL, tv, FALSE, flags, 0);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001626 restore_funccal();
1627}
1628
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001629/*
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001630 * Convert "tv" to a string.
1631 * Return FAIL if not allowed.
1632 */
1633 static int
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001634do_2string(typval_T *tv, int is_2string_any, int tolerant)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001635{
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00001636 if (tv->v_type == VAR_STRING)
1637 return OK;
1638
1639 char_u *str;
1640
1641 if (is_2string_any)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001642 {
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00001643 switch (tv->v_type)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001644 {
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00001645 case VAR_SPECIAL:
1646 case VAR_BOOL:
1647 case VAR_NUMBER:
1648 case VAR_FLOAT:
1649 case VAR_BLOB: break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001650
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00001651 case VAR_LIST:
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001652 if (tolerant)
1653 {
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001654 char_u *s, *e, *p;
1655 garray_T ga;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001656
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001657 ga_init2(&ga, sizeof(char_u *), 1);
1658
1659 // Convert to NL separated items, then
1660 // escape the items and replace the NL with
1661 // a space.
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001662 str = typval2string(tv, TRUE);
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001663 if (str == NULL)
1664 return FAIL;
1665 s = str;
1666 while ((e = vim_strchr(s, '\n')) != NULL)
1667 {
1668 *e = NUL;
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +01001669 p = vim_strsave_fnameescape(s,
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00001670 VSE_NONE);
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001671 if (p != NULL)
1672 {
1673 ga_concat(&ga, p);
1674 ga_concat(&ga, (char_u *)" ");
1675 vim_free(p);
1676 }
1677 s = e + 1;
1678 }
1679 vim_free(str);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001680 clear_tv(tv);
1681 tv->v_type = VAR_STRING;
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001682 tv->vval.v_string = ga.ga_data;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001683 return OK;
1684 }
1685 // FALLTHROUGH
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00001686 default: to_string_error(tv->v_type);
1687 return FAIL;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001688 }
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001689 }
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00001690 str = typval_tostring(tv, TRUE);
1691 clear_tv(tv);
1692 tv->v_type = VAR_STRING;
1693 tv->vval.v_string = str;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001694 return OK;
1695}
1696
1697/*
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001698 * When the value of "sv" is a null list of dict, allocate it.
1699 */
1700 static void
Bram Moolenaar859cc212022-03-28 15:22:35 +01001701allocate_if_null(svar_T *sv)
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001702{
Bram Moolenaar859cc212022-03-28 15:22:35 +01001703 typval_T *tv = sv->sv_tv;
1704
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001705 switch (tv->v_type)
1706 {
1707 case VAR_LIST:
Bram Moolenaar859cc212022-03-28 15:22:35 +01001708 if (tv->vval.v_list == NULL && sv->sv_type != &t_list_empty)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001709 (void)rettv_list_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001710 break;
1711 case VAR_DICT:
Bram Moolenaar859cc212022-03-28 15:22:35 +01001712 if (tv->vval.v_dict == NULL && sv->sv_type != &t_dict_empty)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001713 (void)rettv_dict_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001714 break;
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02001715 case VAR_BLOB:
Bram Moolenaar859cc212022-03-28 15:22:35 +01001716 if (tv->vval.v_blob == NULL && sv->sv_type != &t_blob_null)
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02001717 (void)rettv_blob_alloc(tv);
1718 break;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001719 default:
1720 break;
1721 }
1722}
Bram Moolenaard3aac292020-04-19 14:32:17 +02001723
Bram Moolenaare7525c52021-01-09 13:20:37 +01001724/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001725 * Return the character "str[index]" where "index" is the character index,
1726 * including composing characters.
1727 * If "index" is out of range NULL is returned.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001728 */
1729 char_u *
1730char_from_string(char_u *str, varnumber_T index)
1731{
1732 size_t nbyte = 0;
1733 varnumber_T nchar = index;
1734 size_t slen;
1735
1736 if (str == NULL)
1737 return NULL;
1738 slen = STRLEN(str);
1739
Bram Moolenaarff871402021-03-26 13:34:05 +01001740 // Do the same as for a list: a negative index counts from the end.
1741 // Optimization to check the first byte to be below 0x80 (and no composing
1742 // character follows) makes this a lot faster.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001743 if (index < 0)
1744 {
1745 int clen = 0;
1746
1747 for (nbyte = 0; nbyte < slen; ++clen)
Bram Moolenaarff871402021-03-26 13:34:05 +01001748 {
1749 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1750 ++nbyte;
1751 else if (enc_utf8)
1752 nbyte += utfc_ptr2len(str + nbyte);
1753 else
1754 nbyte += mb_ptr2len(str + nbyte);
1755 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001756 nchar = clen + index;
1757 if (nchar < 0)
1758 // unlike list: index out of range results in empty string
1759 return NULL;
1760 }
1761
1762 for (nbyte = 0; nchar > 0 && nbyte < slen; --nchar)
Bram Moolenaarff871402021-03-26 13:34:05 +01001763 {
1764 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1765 ++nbyte;
1766 else if (enc_utf8)
1767 nbyte += utfc_ptr2len(str + nbyte);
1768 else
1769 nbyte += mb_ptr2len(str + nbyte);
1770 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001771 if (nbyte >= slen)
1772 return NULL;
Bram Moolenaar0289a092021-03-14 18:40:19 +01001773 return vim_strnsave(str + nbyte, mb_ptr2len(str + nbyte));
Bram Moolenaare7525c52021-01-09 13:20:37 +01001774}
1775
1776/*
1777 * Get the byte index for character index "idx" in string "str" with length
Bram Moolenaar0289a092021-03-14 18:40:19 +01001778 * "str_len". Composing characters are included.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001779 * If going over the end return "str_len".
1780 * If "idx" is negative count from the end, -1 is the last character.
1781 * When going over the start return -1.
1782 */
1783 static long
1784char_idx2byte(char_u *str, size_t str_len, varnumber_T idx)
1785{
1786 varnumber_T nchar = idx;
1787 size_t nbyte = 0;
1788
1789 if (nchar >= 0)
1790 {
1791 while (nchar > 0 && nbyte < str_len)
1792 {
Bram Moolenaar0289a092021-03-14 18:40:19 +01001793 nbyte += mb_ptr2len(str + nbyte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001794 --nchar;
1795 }
1796 }
1797 else
1798 {
1799 nbyte = str_len;
1800 while (nchar < 0 && nbyte > 0)
1801 {
1802 --nbyte;
1803 nbyte -= mb_head_off(str, str + nbyte);
1804 ++nchar;
1805 }
1806 if (nchar < 0)
1807 return -1;
1808 }
1809 return (long)nbyte;
1810}
1811
1812/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001813 * Return the slice "str[first : last]" using character indexes. Composing
1814 * characters are included.
Bram Moolenaar6601b622021-01-13 21:47:15 +01001815 * "exclusive" is TRUE for slice().
Bram Moolenaare7525c52021-01-09 13:20:37 +01001816 * Return NULL when the result is empty.
1817 */
1818 char_u *
Bram Moolenaar6601b622021-01-13 21:47:15 +01001819string_slice(char_u *str, varnumber_T first, varnumber_T last, int exclusive)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001820{
1821 long start_byte, end_byte;
1822 size_t slen;
1823
1824 if (str == NULL)
1825 return NULL;
1826 slen = STRLEN(str);
1827 start_byte = char_idx2byte(str, slen, first);
1828 if (start_byte < 0)
1829 start_byte = 0; // first index very negative: use zero
Bram Moolenaar6601b622021-01-13 21:47:15 +01001830 if ((last == -1 && !exclusive) || last == VARNUM_MAX)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001831 end_byte = (long)slen;
1832 else
1833 {
1834 end_byte = char_idx2byte(str, slen, last);
Bram Moolenaar6601b622021-01-13 21:47:15 +01001835 if (!exclusive && end_byte >= 0 && end_byte < (long)slen)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001836 // end index is inclusive
Bram Moolenaar0289a092021-03-14 18:40:19 +01001837 end_byte += mb_ptr2len(str + end_byte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001838 }
1839
1840 if (start_byte >= (long)slen || end_byte <= start_byte)
1841 return NULL;
1842 return vim_strnsave(str + start_byte, end_byte - start_byte);
1843}
1844
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001845/*
1846 * Get a script variable for ISN_STORESCRIPT and ISN_LOADSCRIPT.
1847 * When "dfunc_idx" is negative don't give an error.
1848 * Returns NULL for an error.
1849 */
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001850 static svar_T *
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001851get_script_svar(scriptref_T *sref, int dfunc_idx)
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001852{
1853 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001854 dfunc_T *dfunc = dfunc_idx < 0 ? NULL
1855 : ((dfunc_T *)def_functions.ga_data) + dfunc_idx;
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001856 svar_T *sv;
1857
1858 if (sref->sref_seq != si->sn_script_seq)
1859 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001860 // The script was reloaded after the function was compiled, the
1861 // script_idx may not be valid.
1862 if (dfunc != NULL)
1863 semsg(_(e_script_variable_invalid_after_reload_in_function_str),
1864 printable_func_name(dfunc->df_ufunc));
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001865 return NULL;
1866 }
1867 sv = ((svar_T *)si->sn_var_vals.ga_data) + sref->sref_idx;
Bram Moolenaar6de22962022-09-09 21:35:36 +01001868 if (sv->sv_name == NULL)
1869 {
1870 if (dfunc != NULL)
1871 emsg(_(e_script_variable_was_deleted));
1872 return NULL;
1873 }
Bram Moolenaar60dc8272021-07-29 22:48:54 +02001874 if (!equal_type(sv->sv_type, sref->sref_type, 0))
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001875 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001876 if (dfunc != NULL)
1877 emsg(_(e_script_variable_type_changed));
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001878 return NULL;
1879 }
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001880
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01001881 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0
1882 && sref->sref_sid != current_sctx.sc_sid)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001883 {
1884 if (dfunc != NULL)
1885 semsg(_(e_item_not_exported_in_script_str), sv->sv_name);
1886 return NULL;
1887 }
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001888 return sv;
1889}
1890
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001891/*
Bram Moolenaar20677332021-06-06 17:02:53 +02001892 * Function passed to do_cmdline() for splitting a script joined by NL
1893 * characters.
1894 */
1895 static char_u *
1896get_split_sourceline(
1897 int c UNUSED,
1898 void *cookie,
1899 int indent UNUSED,
1900 getline_opt_T options UNUSED)
1901{
1902 source_cookie_T *sp = (source_cookie_T *)cookie;
1903 char_u *p;
1904 char_u *line;
1905
Bram Moolenaar20677332021-06-06 17:02:53 +02001906 p = vim_strchr(sp->nextline, '\n');
1907 if (p == NULL)
1908 {
1909 line = vim_strsave(sp->nextline);
1910 sp->nextline += STRLEN(sp->nextline);
1911 }
1912 else
1913 {
1914 line = vim_strnsave(sp->nextline, p - sp->nextline);
1915 sp->nextline = p + 1;
1916 }
1917 return line;
1918}
1919
1920/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001921 * Execute a function by "name".
1922 * This can be a builtin function, user function or a funcref.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001923 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001924 */
1925 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001926call_eval_func(
1927 char_u *name,
1928 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001929 ectx_T *ectx,
1930 isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001931{
Bram Moolenaared677f52020-08-12 16:38:10 +02001932 int called_emsg_before = called_emsg;
1933 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001934
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001935 res = call_by_name(name, argcount, ectx, iptr, NULL);
Bram Moolenaared677f52020-08-12 16:38:10 +02001936 if (res == FAIL && called_emsg == called_emsg_before)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001937 {
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001938 dictitem_T *v;
1939
1940 v = find_var(name, NULL, FALSE);
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001941 if (v == NULL || (v->di_tv.v_type != VAR_PARTIAL
1942 && v->di_tv.v_type != VAR_FUNC))
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001943 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001944 emsg_funcname(e_unknown_function_str, name);
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001945 return FAIL;
1946 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001947 return call_partial(&v->di_tv, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001948 }
Bram Moolenaared677f52020-08-12 16:38:10 +02001949 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001950}
1951
1952/*
Bram Moolenaarf112f302020-12-20 17:47:52 +01001953 * When a function reference is used, fill a partial with the information
1954 * needed, especially when it is used as a closure.
1955 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001956 int
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001957fill_partial_and_closure(
Bram Moolenaarcc341812022-09-19 15:54:34 +01001958 partial_T *pt,
1959 ufunc_T *ufunc,
1960 loopvarinfo_T *lvi,
1961 ectx_T *ectx)
Bram Moolenaarf112f302020-12-20 17:47:52 +01001962{
1963 pt->pt_func = ufunc;
1964 pt->pt_refcount = 1;
1965
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001966 if (ufunc->uf_flags & FC_CLOSURE)
Bram Moolenaarf112f302020-12-20 17:47:52 +01001967 {
1968 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1969 + ectx->ec_dfunc_idx;
1970
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001971 // The closure may need to find arguments and local variables of the
1972 // current function in the stack.
Bram Moolenaar0186e582021-01-10 18:33:11 +01001973 pt->pt_outer.out_stack = &ectx->ec_stack;
1974 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001975 if (ectx->ec_outer_ref != NULL)
1976 {
1977 // The current context already has a context, link to that one.
1978 pt->pt_outer.out_up = ectx->ec_outer_ref->or_outer;
1979 if (ectx->ec_outer_ref->or_partial != NULL)
1980 {
1981 pt->pt_outer.out_up_partial = ectx->ec_outer_ref->or_partial;
1982 ++pt->pt_outer.out_up_partial->pt_refcount;
1983 }
1984 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01001985
Bram Moolenaarcc341812022-09-19 15:54:34 +01001986 if (lvi != NULL)
1987 {
1988 int depth;
1989
1990 // The closure may need to find variables defined inside a loop,
1991 // for every nested loop. A new reference is made every time,
1992 // ISN_ENDLOOP will check if they are actually used.
1993 for (depth = 0; depth < lvi->lvi_depth; ++depth)
1994 {
1995 pt->pt_outer.out_loop[depth].stack = &ectx->ec_stack;
1996 pt->pt_outer.out_loop[depth].var_idx = ectx->ec_frame_idx
1997 + STACK_FRAME_SIZE + lvi->lvi_loop[depth].var_idx;
1998 pt->pt_outer.out_loop[depth].var_count =
1999 lvi->lvi_loop[depth].var_count;
2000 }
Bram Moolenaar6d313be2022-09-22 16:36:25 +01002001 pt->pt_outer.out_loop_size = lvi->lvi_depth;
Bram Moolenaarcc341812022-09-19 15:54:34 +01002002 }
Bram Moolenaar6d313be2022-09-22 16:36:25 +01002003 else
2004 pt->pt_outer.out_loop_size = 0;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002005
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002006 // If the function currently executing returns and the closure is still
2007 // being referenced, we need to make a copy of the context (arguments
2008 // and local variables) so that the closure can use it later.
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02002009 // Store a reference to the partial so we can handle that.
Bram Moolenaar35578162021-08-02 19:10:38 +02002010 if (GA_GROW_FAILS(&ectx->ec_funcrefs, 1))
Bram Moolenaarf112f302020-12-20 17:47:52 +01002011 {
2012 vim_free(pt);
2013 return FAIL;
2014 }
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02002015 // Extra variable keeps the count of closures created in the current
2016 // function call.
Bram Moolenaarf112f302020-12-20 17:47:52 +01002017 ++(((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx
2018 + STACK_FRAME_SIZE + dfunc->df_varcount)->vval.v_number;
2019
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01002020 ((partial_T **)ectx->ec_funcrefs.ga_data)[ectx->ec_funcrefs.ga_len]
2021 = pt;
Bram Moolenaarf112f302020-12-20 17:47:52 +01002022 ++pt->pt_refcount;
2023 ++ectx->ec_funcrefs.ga_len;
2024 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01002025 ++ufunc->uf_refcount;
Bram Moolenaarf112f302020-12-20 17:47:52 +01002026 return OK;
2027}
2028
Bram Moolenaaraacc9662021-08-13 19:40:51 +02002029/*
2030 * Execute iptr->isn_arg.string as an Ex command.
2031 */
2032 static int
Ernie Raelee865f32023-09-29 19:53:55 +02002033exec_command(isn_T *iptr, char_u *cmd_string)
Bram Moolenaaraacc9662021-08-13 19:40:51 +02002034{
2035 source_cookie_T cookie;
2036
2037 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00002038 // Pass getsourceline to get an error for a missing ":end" command.
Bram Moolenaaraacc9662021-08-13 19:40:51 +02002039 CLEAR_FIELD(cookie);
2040 cookie.sourcing_lnum = iptr->isn_lnum - 1;
Ernie Raelee865f32023-09-29 19:53:55 +02002041 if (do_cmdline(cmd_string, getsourceline, &cookie,
Bram Moolenaaraacc9662021-08-13 19:40:51 +02002042 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED) == FAIL
2043 || did_emsg)
2044 return FAIL;
2045 return OK;
2046}
2047
Bram Moolenaar89445512022-04-14 12:58:23 +01002048/*
2049 * If script "sid" is not loaded yet then load it now.
2050 * Caller must make sure "sid" is a valid script ID.
2051 * "loaded" is set to TRUE if the script had to be loaded.
2052 * Returns FAIL if loading fails, OK if already loaded or loaded now.
2053 */
2054 int
2055may_load_script(int sid, int *loaded)
2056{
2057 scriptitem_T *si = SCRIPT_ITEM(sid);
2058
2059 if (si->sn_state == SN_STATE_NOT_LOADED)
2060 {
2061 *loaded = TRUE;
2062 if (do_source(si->sn_name, FALSE, DOSO_NONE, NULL) == FAIL)
2063 {
2064 semsg(_(e_cant_open_file_str), si->sn_name);
2065 return FAIL;
2066 }
2067 }
2068 return OK;
2069}
2070
Bram Moolenaarf18332f2021-05-07 17:55:55 +02002071// used for v_instr of typval of VAR_INSTR
2072struct instr_S {
2073 ectx_T *instr_ectx;
2074 isn_T *instr_instr;
2075};
2076
Bram Moolenaar4c137212021-04-19 16:48:48 +02002077// used for substitute_instr
2078typedef struct subs_expr_S {
2079 ectx_T *subs_ectx;
2080 isn_T *subs_instr;
2081 int subs_status;
2082} subs_expr_T;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002083
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002084// Set when calling do_debug().
2085static ectx_T *debug_context = NULL;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02002086static int debug_var_count;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002087
2088/*
2089 * When debugging lookup "name" and return the typeval.
2090 * When not found return NULL.
2091 */
2092 typval_T *
2093lookup_debug_var(char_u *name)
2094{
2095 int idx;
2096 dfunc_T *dfunc;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02002097 ufunc_T *ufunc;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002098 ectx_T *ectx = debug_context;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02002099 int varargs_off;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002100
2101 if (ectx == NULL)
2102 return NULL;
2103 dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
2104
2105 // Go through the local variable names, from last to first.
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02002106 for (idx = debug_var_count - 1; idx >= 0; --idx)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002107 {
Bram Moolenaare406ff82022-03-10 20:47:43 +00002108 char_u *varname = ((char_u **)dfunc->df_var_names.ga_data)[idx];
2109
2110 // the variable name may be NULL when not available in this block
2111 if (varname != NULL && STRCMP(varname, name) == 0)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002112 return STACK_TV_VAR(idx);
2113 }
2114
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02002115 // Go through argument names.
2116 ufunc = dfunc->df_ufunc;
2117 varargs_off = ufunc->uf_va_name == NULL ? 0 : 1;
2118 for (idx = 0; idx < ufunc->uf_args.ga_len; ++idx)
2119 if (STRCMP(((char_u **)(ufunc->uf_args.ga_data))[idx], name) == 0)
2120 return STACK_TV(ectx->ec_frame_idx - ufunc->uf_args.ga_len
2121 - varargs_off + idx);
2122 if (ufunc->uf_va_name != NULL && STRCMP(ufunc->uf_va_name, name) == 0)
2123 return STACK_TV(ectx->ec_frame_idx - 1);
2124
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002125 return NULL;
2126}
2127
Bram Moolenaar26a44842021-09-02 18:49:06 +02002128/*
2129 * Return TRUE if there might be a breakpoint in "ufunc", which is when a
2130 * breakpoint was set in that function or when there is any expression.
2131 */
2132 int
2133may_break_in_function(ufunc_T *ufunc)
2134{
2135 return ufunc->uf_has_breakpoint || debug_has_expr_breakpoint();
2136}
2137
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002138 static void
2139handle_debug(isn_T *iptr, ectx_T *ectx)
2140{
2141 char_u *line;
2142 ufunc_T *ufunc = (((dfunc_T *)def_functions.ga_data)
2143 + ectx->ec_dfunc_idx)->df_ufunc;
2144 isn_T *ni;
2145 int end_lnum = iptr->isn_lnum;
2146 garray_T ga;
2147 int lnum;
2148
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02002149 if (ex_nesting_level > debug_break_level)
2150 {
2151 linenr_T breakpoint;
2152
Bram Moolenaar26a44842021-09-02 18:49:06 +02002153 if (!may_break_in_function(ufunc))
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02002154 return;
2155
2156 // check for the next breakpoint if needed
2157 breakpoint = dbg_find_breakpoint(FALSE, ufunc->uf_name,
Bram Moolenaar8cec9272021-06-23 20:20:53 +02002158 iptr->isn_arg.debug.dbg_break_lnum);
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02002159 if (breakpoint <= 0 || breakpoint > iptr->isn_lnum)
2160 return;
2161 }
2162
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002163 SOURCING_LNUM = iptr->isn_lnum;
2164 debug_context = ectx;
Bram Moolenaar8cec9272021-06-23 20:20:53 +02002165 debug_var_count = iptr->isn_arg.debug.dbg_var_names_len;
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002166
2167 for (ni = iptr + 1; ni->isn_type != ISN_FINISH; ++ni)
2168 if (ni->isn_type == ISN_DEBUG
2169 || ni->isn_type == ISN_RETURN
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002170 || ni->isn_type == ISN_RETURN_OBJECT
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002171 || ni->isn_type == ISN_RETURN_VOID)
2172 {
Bram Moolenaar112bed02021-11-23 22:16:34 +00002173 end_lnum = ni->isn_lnum + (ni->isn_type == ISN_DEBUG ? 0 : 1);
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002174 break;
2175 }
2176
2177 if (end_lnum > iptr->isn_lnum)
2178 {
2179 ga_init2(&ga, sizeof(char_u *), 10);
Bram Moolenaar310091d2021-12-23 21:14:37 +00002180 for (lnum = iptr->isn_lnum; lnum < end_lnum
2181 && lnum <= ufunc->uf_lines.ga_len; ++lnum)
Bram Moolenaar59b50c32021-06-17 22:27:48 +02002182 {
Bram Moolenaar303215d2021-07-07 20:10:43 +02002183 char_u *p = ((char_u **)ufunc->uf_lines.ga_data)[lnum - 1];
Bram Moolenaar59b50c32021-06-17 22:27:48 +02002184
Bram Moolenaar303215d2021-07-07 20:10:43 +02002185 if (p == NULL)
2186 continue; // left over from continuation line
2187 p = skipwhite(p);
Bram Moolenaar59b50c32021-06-17 22:27:48 +02002188 if (*p == '#')
2189 break;
Bram Moolenaar35578162021-08-02 19:10:38 +02002190 if (GA_GROW_OK(&ga, 1))
Bram Moolenaar59b50c32021-06-17 22:27:48 +02002191 ((char_u **)(ga.ga_data))[ga.ga_len++] = p;
2192 if (STRNCMP(p, "def ", 4) == 0)
2193 break;
2194 }
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002195 line = ga_concat_strings(&ga, " ");
2196 vim_free(ga.ga_data);
2197 }
2198 else
2199 line = ((char_u **)ufunc->uf_lines.ga_data)[iptr->isn_lnum - 1];
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002200
Dominique Pelle6e969552021-06-17 13:53:41 +02002201 do_debug(line == NULL ? (char_u *)"[empty]" : line);
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002202 debug_context = NULL;
2203
2204 if (end_lnum > iptr->isn_lnum)
2205 vim_free(line);
2206}
2207
Bram Moolenaar4c137212021-04-19 16:48:48 +02002208/*
Bram Moolenaar65b0d162022-12-13 18:43:22 +00002209 * Store a value in a list, dict, blob or object variable.
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002210 * Returns OK, FAIL or NOTDONE (uncatchable error).
2211 */
2212 static int
2213execute_storeindex(isn_T *iptr, ectx_T *ectx)
2214{
Bram Moolenaarf7d1c6e2023-01-16 20:47:57 +00002215 vartype_T dest_type = iptr->isn_arg.storeindex.si_vartype;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002216 typval_T *tv;
2217 typval_T *tv_idx = STACK_TV_BOT(-2);
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002218 long lidx = 0;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002219 typval_T *tv_dest = STACK_TV_BOT(-1);
2220 int status = OK;
2221
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002222 if (tv_idx->v_type == VAR_NUMBER)
2223 lidx = (long)tv_idx->vval.v_number;
2224
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002225 // Stack contains:
2226 // -3 value to be stored
2227 // -2 index
Yegappan Lakshmanan3775f772023-09-01 22:05:45 +02002228 // -1 dict, list, blob, object or class
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002229 tv = STACK_TV_BOT(-3);
2230 SOURCING_LNUM = iptr->isn_lnum;
Gianmaria Bajod7085a02023-08-31 18:15:26 +02002231
2232 // Make sure an object has been initialized
2233 if (dest_type == VAR_OBJECT && tv_dest->vval.v_object == NULL)
2234 {
2235 emsg(_(e_using_null_object));
2236 status = FAIL;
2237 }
2238 else if (dest_type == VAR_ANY)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002239 {
2240 dest_type = tv_dest->v_type;
2241 if (dest_type == VAR_DICT)
2242 status = do_2string(tv_idx, TRUE, FALSE);
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002243 else if (dest_type == VAR_OBJECT && tv_idx->v_type == VAR_STRING)
2244 {
2245 // Need to get the member index now that the class is known.
2246 object_T *obj = tv_dest->vval.v_object;
2247 class_T *cl = obj->obj_class;
2248 char_u *member = tv_idx->vval.v_string;
2249
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02002250 int m_idx;
2251 ocmember_T *m = object_member_lookup(cl, member, 0, &m_idx);
2252 if (m != NULL)
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002253 {
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02002254 if (*member == '_')
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002255 {
Ernie Raele6c9aa52023-10-06 19:55:52 +02002256 emsg_var_cl_define(e_cannot_access_private_variable_str,
2257 m->ocm_name, 0, cl);
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02002258 status = FAIL;
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002259 }
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002260
Yegappan Lakshmananf36bbcd2023-09-10 18:19:06 +02002261 lidx = m_idx;
2262 }
2263 else
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002264 {
Yegappan Lakshmananc30a90d2023-09-15 20:14:55 +02002265 member_not_found_msg(cl, VAR_OBJECT, member, 0);
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002266 status = FAIL;
2267 }
2268 }
2269 else if ((dest_type == VAR_LIST || dest_type == VAR_OBJECT)
2270 && tv_idx->v_type != VAR_NUMBER)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002271 {
2272 emsg(_(e_number_expected));
2273 status = FAIL;
2274 }
2275 }
Bram Moolenaare08be092022-02-17 13:08:26 +00002276
2277 if (status == OK)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002278 {
Bram Moolenaare08be092022-02-17 13:08:26 +00002279 if (dest_type == VAR_LIST)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002280 {
Bram Moolenaare08be092022-02-17 13:08:26 +00002281 list_T *list = tv_dest->vval.v_list;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002282
Bram Moolenaare08be092022-02-17 13:08:26 +00002283 if (list == NULL)
2284 {
2285 emsg(_(e_list_not_set));
2286 return FAIL;
2287 }
2288 if (lidx < 0 && list->lv_len + lidx >= 0)
2289 // negative index is relative to the end
2290 lidx = list->lv_len + lidx;
2291 if (lidx < 0 || lidx > list->lv_len)
2292 {
2293 semsg(_(e_list_index_out_of_range_nr), lidx);
2294 return FAIL;
2295 }
2296 if (lidx < list->lv_len)
2297 {
2298 listitem_T *li = list_find(list, lidx);
2299
2300 if (error_if_locked(li->li_tv.v_lock,
Bram Moolenaar70c43d82022-01-26 21:01:15 +00002301 e_cannot_change_locked_list_item))
Bram Moolenaare08be092022-02-17 13:08:26 +00002302 return FAIL;
2303 // overwrite existing list item
2304 clear_tv(&li->li_tv);
2305 li->li_tv = *tv;
2306 }
2307 else
2308 {
2309 if (error_if_locked(list->lv_lock, e_cannot_change_locked_list))
2310 return FAIL;
2311 // append to list, only fails when out of memory
2312 if (list_append_tv(list, tv) == FAIL)
2313 return NOTDONE;
2314 clear_tv(tv);
2315 }
2316 }
2317 else if (dest_type == VAR_DICT)
2318 {
2319 char_u *key = tv_idx->vval.v_string;
2320 dict_T *dict = tv_dest->vval.v_dict;
2321 dictitem_T *di;
2322
2323 SOURCING_LNUM = iptr->isn_lnum;
2324 if (dict == NULL)
2325 {
2326 emsg(_(e_dictionary_not_set));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002327 return FAIL;
Bram Moolenaare08be092022-02-17 13:08:26 +00002328 }
2329 if (key == NULL)
2330 key = (char_u *)"";
2331 di = dict_find(dict, key, -1);
2332 if (di != NULL)
2333 {
2334 if (error_if_locked(di->di_tv.v_lock,
2335 e_cannot_change_dict_item))
2336 return FAIL;
2337 // overwrite existing value
2338 clear_tv(&di->di_tv);
2339 di->di_tv = *tv;
2340 }
2341 else
2342 {
2343 if (error_if_locked(dict->dv_lock, e_cannot_change_dict))
2344 return FAIL;
2345 // add to dict, only fails when out of memory
2346 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
2347 return NOTDONE;
2348 clear_tv(tv);
2349 }
2350 }
2351 else if (dest_type == VAR_BLOB)
2352 {
Bram Moolenaare08be092022-02-17 13:08:26 +00002353 blob_T *blob = tv_dest->vval.v_blob;
Bram Moolenaar65b0d162022-12-13 18:43:22 +00002354 varnumber_T nr;
2355 int error = FALSE;
2356 int len;
Bram Moolenaare08be092022-02-17 13:08:26 +00002357
2358 if (blob == NULL)
2359 {
2360 emsg(_(e_blob_not_set));
2361 return FAIL;
2362 }
2363 len = blob_len(blob);
2364 if (lidx < 0 && len + lidx >= 0)
2365 // negative index is relative to the end
2366 lidx = len + lidx;
2367
2368 // Can add one byte at the end.
2369 if (lidx < 0 || lidx > len)
2370 {
2371 semsg(_(e_blob_index_out_of_range_nr), lidx);
2372 return FAIL;
2373 }
2374 if (value_check_lock(blob->bv_lock, (char_u *)"blob", FALSE))
2375 return FAIL;
2376 nr = tv_get_number_chk(tv, &error);
2377 if (error)
2378 return FAIL;
2379 blob_set_append(blob, lidx, nr);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002380 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002381 else if (dest_type == VAR_CLASS || dest_type == VAR_OBJECT)
2382 {
Yegappan Lakshmanan3775f772023-09-01 22:05:45 +02002383 typval_T *otv;
Bram Moolenaarf7d1c6e2023-01-16 20:47:57 +00002384
Yegappan Lakshmanan3775f772023-09-01 22:05:45 +02002385 if (dest_type == VAR_OBJECT)
2386 {
2387 object_T *obj = tv_dest->vval.v_object;
2388
2389 otv = (typval_T *)(obj + 1);
2390 class_T *itf = iptr->isn_arg.storeindex.si_class;
2391 if (itf != NULL)
2392 // convert interface member index to class member index
Ernie Rael18143d32023-09-04 22:30:41 +02002393 lidx = object_index_from_itf_index(itf, FALSE, lidx,
Yegappan Lakshmanan5a05d372023-09-29 19:43:11 +02002394 obj->obj_class);
Yegappan Lakshmanan3775f772023-09-01 22:05:45 +02002395 }
2396 else
2397 {
2398 // VAR_CLASS
2399 class_T *class = tv_dest->vval.v_class;
2400 otv = class->class_members_tv;
2401 }
Bram Moolenaarf7d1c6e2023-01-16 20:47:57 +00002402
Bram Moolenaar2c1c8032023-02-18 18:38:37 +00002403 clear_tv(&otv[lidx]);
2404 otv[lidx] = *tv;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002405 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002406 else
2407 {
Bram Moolenaare08be092022-02-17 13:08:26 +00002408 status = FAIL;
2409 semsg(_(e_cannot_index_str), vartype_name(dest_type));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002410 }
2411 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002412
2413 clear_tv(tv_idx);
2414 clear_tv(tv_dest);
2415 ectx->ec_stack.ga_len -= 3;
2416 if (status == FAIL)
2417 {
2418 clear_tv(tv);
2419 return FAIL;
2420 }
2421 return OK;
2422}
2423
2424/*
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002425 * Store a value in a list or blob range.
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002426 */
2427 static int
2428execute_storerange(isn_T *iptr, ectx_T *ectx)
2429{
2430 typval_T *tv;
2431 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2432 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2433 typval_T *tv_dest = STACK_TV_BOT(-1);
2434 int status = OK;
2435
2436 // Stack contains:
2437 // -4 value to be stored
2438 // -3 first index or "none"
2439 // -2 second index or "none"
2440 // -1 destination list or blob
2441 tv = STACK_TV_BOT(-4);
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002442 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002443 if (tv_dest->v_type == VAR_LIST)
2444 {
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002445 long n1;
2446 long n2;
2447 listitem_T *li1;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002448
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002449 n1 = (long)tv_get_number_chk(tv_idx1, NULL);
2450 if (tv_idx2->v_type == VAR_SPECIAL
2451 && tv_idx2->vval.v_number == VVAL_NONE)
2452 n2 = list_len(tv_dest->vval.v_list) - 1;
2453 else
2454 n2 = (long)tv_get_number_chk(tv_idx2, NULL);
2455
Bram Moolenaar22ebd172022-04-01 15:26:58 +01002456 li1 = check_range_index_one(tv_dest->vval.v_list, &n1, TRUE, FALSE);
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002457 if (li1 == NULL)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002458 status = FAIL;
2459 else
2460 {
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002461 status = check_range_index_two(tv_dest->vval.v_list,
2462 &n1, li1, &n2, FALSE);
2463 if (status != FAIL)
2464 status = list_assign_range(
2465 tv_dest->vval.v_list,
2466 tv->vval.v_list,
2467 n1,
2468 n2,
2469 tv_idx2->v_type == VAR_SPECIAL,
2470 (char_u *)"=",
2471 (char_u *)"[unknown]");
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002472 }
2473 }
2474 else if (tv_dest->v_type == VAR_BLOB)
2475 {
2476 varnumber_T n1;
2477 varnumber_T n2;
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002478 long bloblen;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002479
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002480 n1 = tv_get_number_chk(tv_idx1, NULL);
2481 if (tv_idx2->v_type == VAR_SPECIAL
2482 && tv_idx2->vval.v_number == VVAL_NONE)
2483 n2 = blob_len(tv_dest->vval.v_blob) - 1;
2484 else
2485 n2 = tv_get_number_chk(tv_idx2, NULL);
2486 bloblen = blob_len(tv_dest->vval.v_blob);
2487
2488 if (check_blob_index(bloblen, n1, FALSE) == FAIL
2489 || check_blob_range(bloblen, n1, n2, FALSE) == FAIL)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002490 status = FAIL;
2491 else
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002492 status = blob_set_range(tv_dest->vval.v_blob, n1, n2, tv);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002493 }
2494 else
2495 {
2496 status = FAIL;
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002497 emsg(_(e_list_or_blob_required));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002498 }
2499
2500 clear_tv(tv_idx1);
2501 clear_tv(tv_idx2);
2502 clear_tv(tv_dest);
2503 ectx->ec_stack.ga_len -= 4;
2504 clear_tv(tv);
2505
2506 return status;
2507}
2508
2509/*
2510 * Unlet item in list or dict variable.
2511 */
2512 static int
2513execute_unletindex(isn_T *iptr, ectx_T *ectx)
2514{
2515 typval_T *tv_idx = STACK_TV_BOT(-2);
2516 typval_T *tv_dest = STACK_TV_BOT(-1);
2517 int status = OK;
2518
2519 // Stack contains:
2520 // -2 index
2521 // -1 dict or list
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002522 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002523 if (tv_dest->v_type == VAR_DICT)
2524 {
2525 // unlet a dict item, index must be a string
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002526 if (tv_idx->v_type != VAR_STRING && tv_idx->v_type != VAR_NUMBER)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002527 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002528 semsg(_(e_expected_str_but_got_str),
2529 vartype_name(VAR_STRING),
2530 vartype_name(tv_idx->v_type));
2531 status = FAIL;
2532 }
2533 else
2534 {
2535 dict_T *d = tv_dest->vval.v_dict;
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002536 char_u *key;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002537 dictitem_T *di = NULL;
2538
2539 if (d != NULL && value_check_lock(
2540 d->dv_lock, NULL, FALSE))
2541 status = FAIL;
2542 else
2543 {
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002544 if (tv_idx->v_type == VAR_STRING)
2545 {
2546 key = tv_idx->vval.v_string;
2547 if (key == NULL)
2548 key = (char_u *)"";
2549 }
2550 else
2551 {
2552 key = tv_get_string(tv_idx);
2553 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002554 if (d != NULL)
2555 di = dict_find(d, key, (int)STRLEN(key));
2556 if (di == NULL)
2557 {
2558 // NULL dict is equivalent to empty dict
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00002559 semsg(_(e_key_not_present_in_dictionary_str), key);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002560 status = FAIL;
2561 }
2562 else if (var_check_fixed(di->di_flags,
2563 NULL, FALSE)
2564 || var_check_ro(di->di_flags,
2565 NULL, FALSE))
2566 status = FAIL;
2567 else
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002568 dictitem_remove(d, di, "unlet");
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002569 }
2570 }
2571 }
2572 else if (tv_dest->v_type == VAR_LIST)
2573 {
2574 // unlet a List item, index must be a number
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002575 if (check_for_number(tv_idx) == FAIL)
2576 {
2577 status = FAIL;
2578 }
2579 else
2580 {
2581 list_T *l = tv_dest->vval.v_list;
2582 long n = (long)tv_idx->vval.v_number;
2583
2584 if (l != NULL && value_check_lock(
2585 l->lv_lock, NULL, FALSE))
2586 status = FAIL;
2587 else
2588 {
2589 listitem_T *li = list_find(l, n);
2590
2591 if (li == NULL)
2592 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002593 semsg(_(e_list_index_out_of_range_nr), n);
2594 status = FAIL;
2595 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002596 else
2597 listitem_remove(l, li);
2598 }
2599 }
2600 }
2601 else
2602 {
2603 status = FAIL;
2604 semsg(_(e_cannot_index_str),
2605 vartype_name(tv_dest->v_type));
2606 }
2607
2608 clear_tv(tv_idx);
2609 clear_tv(tv_dest);
2610 ectx->ec_stack.ga_len -= 2;
2611
2612 return status;
2613}
2614
2615/*
2616 * Unlet a range of items in a list variable.
2617 */
2618 static int
2619execute_unletrange(isn_T *iptr, ectx_T *ectx)
2620{
2621 // Stack contains:
2622 // -3 index1
2623 // -2 index2
2624 // -1 dict or list
2625 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2626 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2627 typval_T *tv_dest = STACK_TV_BOT(-1);
2628 int status = OK;
2629
2630 if (tv_dest->v_type == VAR_LIST)
2631 {
2632 // indexes must be a number
2633 SOURCING_LNUM = iptr->isn_lnum;
2634 if (check_for_number(tv_idx1) == FAIL
2635 || (tv_idx2->v_type != VAR_SPECIAL
2636 && check_for_number(tv_idx2) == FAIL))
2637 {
2638 status = FAIL;
2639 }
2640 else
2641 {
2642 list_T *l = tv_dest->vval.v_list;
2643 long n1 = (long)tv_idx1->vval.v_number;
2644 long n2 = tv_idx2->v_type == VAR_SPECIAL
2645 ? 0 : (long)tv_idx2->vval.v_number;
2646 listitem_T *li;
2647
2648 li = list_find_index(l, &n1);
2649 if (li == NULL)
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002650 {
2651 semsg(_(e_list_index_out_of_range_nr),
2652 (long)tv_idx1->vval.v_number);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002653 status = FAIL;
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002654 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002655 else
2656 {
2657 if (n1 < 0)
2658 n1 = list_idx_of_item(l, li);
2659 if (n2 < 0)
2660 {
2661 listitem_T *li2 = list_find(l, n2);
2662
2663 if (li2 == NULL)
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002664 {
2665 semsg(_(e_list_index_out_of_range_nr), n2);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002666 status = FAIL;
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002667 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002668 else
2669 n2 = list_idx_of_item(l, li2);
2670 }
2671 if (status != FAIL
2672 && tv_idx2->v_type != VAR_SPECIAL
2673 && n2 < n1)
2674 {
2675 semsg(_(e_list_index_out_of_range_nr), n2);
2676 status = FAIL;
2677 }
Bram Moolenaar6b8c7ba2022-03-20 17:46:06 +00002678 if (status != FAIL)
2679 list_unlet_range(l, li, n1,
2680 tv_idx2->v_type != VAR_SPECIAL, n2);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002681 }
2682 }
2683 }
2684 else
2685 {
2686 status = FAIL;
2687 SOURCING_LNUM = iptr->isn_lnum;
2688 semsg(_(e_cannot_index_str),
2689 vartype_name(tv_dest->v_type));
2690 }
2691
2692 clear_tv(tv_idx1);
2693 clear_tv(tv_idx2);
2694 clear_tv(tv_dest);
2695 ectx->ec_stack.ga_len -= 3;
2696
2697 return status;
2698}
2699
2700/*
2701 * Top of a for loop.
2702 */
2703 static int
2704execute_for(isn_T *iptr, ectx_T *ectx)
2705{
2706 typval_T *tv;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002707 int jump = FALSE;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002708 typval_T *ltv = STACK_TV_BOT(-1);
2709 typval_T *idxtv =
Bram Moolenaarcc341812022-09-19 15:54:34 +01002710 STACK_TV_VAR(iptr->isn_arg.forloop.for_loop_idx);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002711
2712 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
2713 return FAIL;
2714 if (ltv->v_type == VAR_LIST)
2715 {
2716 list_T *list = ltv->vval.v_list;
2717
2718 // push the next item from the list
2719 ++idxtv->vval.v_number;
2720 if (list == NULL
2721 || idxtv->vval.v_number >= list->lv_len)
2722 {
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002723 jump = TRUE;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002724 }
2725 else if (list->lv_first == &range_list_item)
2726 {
2727 // non-materialized range() list
2728 tv = STACK_TV_BOT(0);
2729 tv->v_type = VAR_NUMBER;
2730 tv->v_lock = 0;
2731 tv->vval.v_number = list_find_nr(
2732 list, idxtv->vval.v_number, NULL);
2733 ++ectx->ec_stack.ga_len;
2734 }
2735 else
2736 {
2737 listitem_T *li = list_find(list,
2738 idxtv->vval.v_number);
2739
2740 copy_tv(&li->li_tv, STACK_TV_BOT(0));
2741 ++ectx->ec_stack.ga_len;
2742 }
2743 }
2744 else if (ltv->v_type == VAR_STRING)
2745 {
2746 char_u *str = ltv->vval.v_string;
2747
2748 // The index is for the last byte of the previous
2749 // character.
2750 ++idxtv->vval.v_number;
2751 if (str == NULL || str[idxtv->vval.v_number] == NUL)
2752 {
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002753 jump = TRUE;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002754 }
2755 else
2756 {
2757 int clen = mb_ptr2len(str + idxtv->vval.v_number);
2758
2759 // Push the next character from the string.
2760 tv = STACK_TV_BOT(0);
2761 tv->v_type = VAR_STRING;
2762 tv->vval.v_string = vim_strnsave(
2763 str + idxtv->vval.v_number, clen);
2764 ++ectx->ec_stack.ga_len;
2765 idxtv->vval.v_number += clen - 1;
2766 }
2767 }
2768 else if (ltv->v_type == VAR_BLOB)
2769 {
2770 blob_T *blob = ltv->vval.v_blob;
2771
2772 // When we get here the first time make a copy of the
2773 // blob, so that the iteration still works when it is
2774 // changed.
2775 if (idxtv->vval.v_number == -1 && blob != NULL)
2776 {
2777 blob_copy(blob, ltv);
2778 blob_unref(blob);
2779 blob = ltv->vval.v_blob;
2780 }
2781
2782 // The index is for the previous byte.
2783 ++idxtv->vval.v_number;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002784 if (blob == NULL || idxtv->vval.v_number >= blob_len(blob))
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002785 {
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002786 jump = TRUE;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002787 }
2788 else
2789 {
2790 // Push the next byte from the blob.
2791 tv = STACK_TV_BOT(0);
2792 tv->v_type = VAR_NUMBER;
2793 tv->vval.v_number = blob_get(blob,
2794 idxtv->vval.v_number);
2795 ++ectx->ec_stack.ga_len;
2796 }
2797 }
2798 else
2799 {
2800 semsg(_(e_for_loop_on_str_not_supported),
2801 vartype_name(ltv->v_type));
2802 return FAIL;
2803 }
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002804
2805 if (jump)
2806 {
2807 // past the end of the list/string/blob, jump to "endfor"
2808 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
2809 may_restore_cmdmod(&ectx->ec_funclocal);
2810 }
2811 else
2812 {
2813 // Store the current number of funcrefs, this may be used in
2814 // ISN_LOOPEND. The variable index is always one more than the loop
2815 // variable index.
Bram Moolenaarcc341812022-09-19 15:54:34 +01002816 tv = STACK_TV_VAR(iptr->isn_arg.forloop.for_loop_idx + 1);
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002817 tv->vval.v_number = ectx->ec_funcrefs.ga_len;
2818 }
2819
2820 return OK;
2821}
2822
2823/*
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002824 * Code for handling variables declared inside a loop and used in a closure.
2825 * This is very similar to what is done with funcstack_T. The difference is
2826 * that the funcstack_T has the scope of a function, while a loopvars_T has the
2827 * scope of the block inside a loop and each loop may have its own.
2828 */
2829
2830// Double linked list of loopvars_T in use.
2831static loopvars_T *first_loopvars = NULL;
2832
2833 static void
2834add_loopvars_to_list(loopvars_T *loopvars)
2835{
2836 // Link in list of loopvarss.
2837 if (first_loopvars != NULL)
2838 first_loopvars->lvs_prev = loopvars;
2839 loopvars->lvs_next = first_loopvars;
2840 loopvars->lvs_prev = NULL;
2841 first_loopvars = loopvars;
2842}
2843
2844 static void
2845remove_loopvars_from_list(loopvars_T *loopvars)
2846{
2847 if (loopvars->lvs_prev == NULL)
2848 first_loopvars = loopvars->lvs_next;
2849 else
2850 loopvars->lvs_prev->lvs_next = loopvars->lvs_next;
2851 if (loopvars->lvs_next != NULL)
2852 loopvars->lvs_next->lvs_prev = loopvars->lvs_prev;
2853}
2854
2855/*
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002856 * End of a for or while loop: Handle any variables used by a closure.
2857 */
2858 static int
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002859execute_endloop(isn_T *iptr, ectx_T *ectx)
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002860{
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002861 endloop_T *endloop = &iptr->isn_arg.endloop;
2862 typval_T *tv_refcount = STACK_TV_VAR(endloop->end_funcref_idx);
2863 int prev_closure_count = tv_refcount->vval.v_number;
Bram Moolenaarcc341812022-09-19 15:54:34 +01002864 int depth = endloop->end_depth;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002865 garray_T *gap = &ectx->ec_funcrefs;
2866 int closure_in_use = FALSE;
2867 loopvars_T *loopvars;
2868 typval_T *stack;
2869 int idx;
2870
Bram Moolenaarcc341812022-09-19 15:54:34 +01002871 // Check if any created closure is still being referenced and loopvars have
2872 // not been saved yet for the current depth.
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002873 for (idx = prev_closure_count; idx < gap->ga_len; ++idx)
2874 {
2875 partial_T *pt = ((partial_T **)gap->ga_data)[idx];
2876
Bram Moolenaarcc341812022-09-19 15:54:34 +01002877 if (pt->pt_refcount > 1 && pt->pt_loopvars[depth] == NULL)
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002878 {
2879 int refcount = pt->pt_refcount;
2880 int i;
2881
2882 // A Reference in a variable inside the loop doesn't count, it gets
2883 // unreferenced at the end of the loop.
2884 for (i = 0; i < endloop->end_var_count; ++i)
2885 {
2886 typval_T *stv = STACK_TV_VAR(endloop->end_var_idx + i);
2887
2888 if (stv->v_type == VAR_PARTIAL && pt == stv->vval.v_partial)
2889 --refcount;
2890 }
2891 if (refcount > 1)
2892 {
2893 closure_in_use = TRUE;
2894 break;
2895 }
2896 }
2897 }
2898
2899 // If no function reference were created since the start of the loop block
2900 // or it is no longer referenced there is nothing to do.
2901 if (!closure_in_use)
2902 return OK;
2903
2904 // A closure is using variables declared inside the loop.
2905 // Move them to the called function.
2906 loopvars = ALLOC_CLEAR_ONE(loopvars_T);
2907 if (loopvars == NULL)
2908 return FAIL;
2909
2910 loopvars->lvs_ga.ga_len = endloop->end_var_count;
2911 stack = ALLOC_CLEAR_MULT(typval_T, loopvars->lvs_ga.ga_len);
2912 loopvars->lvs_ga.ga_data = stack;
2913 if (stack == NULL)
2914 {
2915 vim_free(loopvars);
2916 return FAIL;
2917 }
2918 add_loopvars_to_list(loopvars);
2919
2920 // Move the variable values.
2921 for (idx = 0; idx < endloop->end_var_count; ++idx)
2922 {
2923 typval_T *tv = STACK_TV_VAR(endloop->end_var_idx + idx);
2924
2925 *(stack + idx) = *tv;
2926 tv->v_type = VAR_UNKNOWN;
2927 }
2928
2929 for (idx = prev_closure_count; idx < gap->ga_len; ++idx)
2930 {
2931 partial_T *pt = ((partial_T **)gap->ga_data)[idx];
2932
Bram Moolenaarcc341812022-09-19 15:54:34 +01002933 if (pt->pt_refcount > 1 && pt->pt_loopvars[depth] == NULL)
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002934 {
2935 ++loopvars->lvs_refcount;
Bram Moolenaarcc341812022-09-19 15:54:34 +01002936 pt->pt_loopvars[depth] = loopvars;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002937
Bram Moolenaarcc341812022-09-19 15:54:34 +01002938 pt->pt_outer.out_loop[depth].stack = &loopvars->lvs_ga;
2939 pt->pt_outer.out_loop[depth].var_idx -=
2940 ectx->ec_frame_idx + STACK_FRAME_SIZE + endloop->end_var_idx;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002941 }
2942 }
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002943
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002944 return OK;
2945}
2946
2947/*
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002948 * Called when a partial is freed or its reference count goes down to one. The
2949 * loopvars may be the only reference to the partials in the local variables.
2950 * Go over all of them, the funcref and can be freed if all partials
2951 * referencing the loopvars have a reference count of one.
Bram Moolenaarcc341812022-09-19 15:54:34 +01002952 * Return TRUE if it was freed.
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002953 */
Bram Moolenaarcc341812022-09-19 15:54:34 +01002954 int
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002955loopvars_check_refcount(loopvars_T *loopvars)
2956{
2957 int i;
2958 garray_T *gap = &loopvars->lvs_ga;
2959 int done = 0;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002960 typval_T *stack = gap->ga_data;
2961
Bram Moolenaarcc341812022-09-19 15:54:34 +01002962 if (loopvars->lvs_refcount > loopvars->lvs_min_refcount)
2963 return FALSE;
2964 for (i = 0; i < gap->ga_len; ++i)
2965 {
2966 typval_T *tv = ((typval_T *)gap->ga_data) + i;
2967
2968 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL
2969 && tv->vval.v_partial->pt_refcount == 1)
2970 {
2971 int depth;
2972
2973 for (depth = 0; depth < MAX_LOOP_DEPTH; ++depth)
2974 if (tv->vval.v_partial->pt_loopvars[depth] == loopvars)
2975 ++done;
2976 }
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002977 }
Bram Moolenaarcc341812022-09-19 15:54:34 +01002978 if (done != loopvars->lvs_min_refcount)
2979 return FALSE;
2980
2981 // All partials referencing the loopvars have a reference count of
2982 // one, thus the loopvars is no longer of use.
2983 stack = gap->ga_data;
2984 for (i = 0; i < gap->ga_len; ++i)
2985 clear_tv(stack + i);
2986 vim_free(stack);
2987 remove_loopvars_from_list(loopvars);
2988 vim_free(loopvars);
2989 return TRUE;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002990}
2991
2992/*
2993 * For garbage collecting: set references in all variables referenced by
2994 * all loopvars.
2995 */
2996 int
2997set_ref_in_loopvars(int copyID)
2998{
2999 loopvars_T *loopvars;
3000
3001 for (loopvars = first_loopvars; loopvars != NULL;
3002 loopvars = loopvars->lvs_next)
3003 {
3004 typval_T *stack = loopvars->lvs_ga.ga_data;
3005 int i;
3006
3007 for (i = 0; i < loopvars->lvs_ga.ga_len; ++i)
3008 if (set_ref_in_item(stack + i, copyID, NULL, NULL))
3009 return TRUE; // abort
3010 }
3011 return FALSE;
3012}
3013
3014/*
Bram Moolenaar06b77222022-01-25 15:51:56 +00003015 * Load instruction for w:/b:/g:/t: variable.
3016 * "isn_type" is used instead of "iptr->isn_type".
3017 */
3018 static int
3019load_namespace_var(ectx_T *ectx, isntype_T isn_type, isn_T *iptr)
3020{
3021 dictitem_T *di = NULL;
3022 hashtab_T *ht = NULL;
3023 char namespace;
3024
3025 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
3026 return NOTDONE;
3027 switch (isn_type)
3028 {
3029 case ISN_LOADG:
3030 ht = get_globvar_ht();
3031 namespace = 'g';
3032 break;
3033 case ISN_LOADB:
3034 ht = &curbuf->b_vars->dv_hashtab;
3035 namespace = 'b';
3036 break;
3037 case ISN_LOADW:
3038 ht = &curwin->w_vars->dv_hashtab;
3039 namespace = 'w';
3040 break;
3041 case ISN_LOADT:
3042 ht = &curtab->tp_vars->dv_hashtab;
3043 namespace = 't';
3044 break;
3045 default: // Cannot reach here
3046 return NOTDONE;
3047 }
3048 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
3049
Bram Moolenaar06b77222022-01-25 15:51:56 +00003050 if (di == NULL)
3051 {
Bram Moolenaarfe732552022-02-22 19:39:13 +00003052 if (isn_type == ISN_LOADG)
3053 {
3054 ufunc_T *ufunc = find_func(iptr->isn_arg.string, TRUE);
3055
3056 // g:Something could be a function
3057 if (ufunc != NULL)
3058 {
3059 typval_T *tv = STACK_TV_BOT(0);
3060
3061 ++ectx->ec_stack.ga_len;
3062 tv->v_type = VAR_FUNC;
3063 tv->vval.v_string = alloc(STRLEN(iptr->isn_arg.string) + 3);
3064 if (tv->vval.v_string == NULL)
3065 return FAIL;
3066 STRCPY(tv->vval.v_string, "g:");
3067 STRCPY(tv->vval.v_string + 2, iptr->isn_arg.string);
3068 return OK;
3069 }
3070 }
Bram Moolenaar06b77222022-01-25 15:51:56 +00003071 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarfe732552022-02-22 19:39:13 +00003072 if (vim_strchr(iptr->isn_arg.string, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar06b77222022-01-25 15:51:56 +00003073 // no check if the item exists in the script but
3074 // isn't exported, it is too complicated
Bram Moolenaarfe732552022-02-22 19:39:13 +00003075 semsg(_(e_item_not_found_in_script_str), iptr->isn_arg.string);
Bram Moolenaar06b77222022-01-25 15:51:56 +00003076 else
3077 semsg(_(e_undefined_variable_char_str),
Bram Moolenaarfe732552022-02-22 19:39:13 +00003078 namespace, iptr->isn_arg.string);
Bram Moolenaar06b77222022-01-25 15:51:56 +00003079 return FAIL;
3080 }
3081 else
3082 {
3083 copy_tv(&di->di_tv, STACK_TV_BOT(0));
3084 ++ectx->ec_stack.ga_len;
3085 }
3086 return OK;
3087}
3088
Bram Moolenaard0200c82023-01-28 15:19:40 +00003089
3090 static void
3091object_required_error(typval_T *tv)
3092{
3093 garray_T type_list;
3094 ga_init2(&type_list, sizeof(type_T *), 10);
3095 type_T *type = typval2type(tv, get_copyID(), &type_list, TVTT_DO_MEMBER);
3096 char *tofree = NULL;
3097 char *typename = type_name(type, &tofree);
3098 semsg(_(e_object_required_found_str), typename);
3099 vim_free(tofree);
3100 clear_type_list(&type_list);
3101}
3102
Bram Moolenaar06b77222022-01-25 15:51:56 +00003103/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02003104 * Execute instructions in execution context "ectx".
3105 * Return OK or FAIL;
3106 */
3107 static int
3108exec_instructions(ectx_T *ectx)
3109{
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003110 int ret = FAIL;
3111 int save_trylevel_at_start = ectx->ec_trylevel_at_start;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02003112 int dict_stack_len_at_start = dict_stack.ga_len;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003113
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02003114 // Start execution at the first instruction.
Bram Moolenaar4c137212021-04-19 16:48:48 +02003115 ectx->ec_iidx = 0;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01003116
Bram Moolenaarff652882021-05-16 15:24:49 +02003117 // Only catch exceptions in this instruction list.
3118 ectx->ec_trylevel_at_start = trylevel;
3119
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003120 for (;;)
3121 {
Bram Moolenaara7490192021-07-22 12:26:14 +02003122 static int breakcheck_count = 0; // using "static" makes it faster
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003123 isn_T *iptr;
Bram Moolenaara7490192021-07-22 12:26:14 +02003124 typval_T *tv;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003125
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003126 if (unlikely(++breakcheck_count >= 100))
Bram Moolenaar270d0382020-05-15 21:42:53 +02003127 {
3128 line_breakcheck();
3129 breakcheck_count = 0;
3130 }
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003131 if (unlikely(got_int))
Bram Moolenaar20431c92020-03-20 18:39:46 +01003132 {
3133 // Turn CTRL-C into an exception.
3134 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +01003135 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003136 goto theend;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003137 did_throw = TRUE;
3138 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003139
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003140 if (unlikely(did_emsg && msg_list != NULL && *msg_list != NULL))
Bram Moolenaara26b9702020-04-18 19:53:28 +02003141 {
3142 // Turn an error message into an exception.
3143 did_emsg = FALSE;
3144 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003145 goto theend;
Bram Moolenaara26b9702020-04-18 19:53:28 +02003146 did_throw = TRUE;
3147 *msg_list = NULL;
Bram Moolenaar3ef2e412023-04-30 18:50:48 +01003148
3149 // This exception was not caught (yet).
3150 garray_T *trystack = &ectx->ec_trystack;
3151 if (trystack->ga_len > 0)
3152 {
3153 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
3154 + trystack->ga_len - 1;
3155 if (trycmd->tcd_frame_idx == ectx->ec_frame_idx)
3156 trycmd->tcd_caught = FALSE;
3157 }
Bram Moolenaara26b9702020-04-18 19:53:28 +02003158 }
3159
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003160 if (unlikely(did_throw))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003161 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003162 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003163 trycmd_T *trycmd = NULL;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003164 int index = trystack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003165
3166 // An exception jumps to the first catch, finally, or returns from
3167 // the current function.
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003168 while (index > 0)
3169 {
3170 trycmd = ((trycmd_T *)trystack->ga_data) + index - 1;
Bram Moolenaar3ef2e412023-04-30 18:50:48 +01003171 // 1. after :try and before :catch - jump to first :catch
3172 // 2. in :catch block - jump to :finally
3173 // 3. in :catch block and no finally - jump to :endtry
3174 if (!trycmd->tcd_in_catch || trycmd->tcd_finally_idx != 0
3175 || trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003176 break;
3177 // In the catch and finally block of this try we have to go up
3178 // one level.
3179 --index;
3180 trycmd = NULL;
3181 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003182 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003183 {
Bram Moolenaar834193a2021-06-30 20:39:15 +02003184 if (trycmd->tcd_in_catch)
3185 {
Bram Moolenaar3ef2e412023-04-30 18:50:48 +01003186 if (trycmd->tcd_finally_idx > 0)
3187 {
3188 // exception inside ":catch", jump to ":finally" once
3189 ectx->ec_iidx = trycmd->tcd_finally_idx;
3190 trycmd->tcd_finally_idx = 0;
3191 }
3192 else
3193 {
3194 // exception inside ":catch" or ":finally", jump to
3195 // ":endtry"
3196 ectx->ec_iidx = trycmd->tcd_endtry_idx;
3197 }
Bram Moolenaar834193a2021-06-30 20:39:15 +02003198 }
3199 else
Bram Moolenaar3ef2e412023-04-30 18:50:48 +01003200 {
Bram Moolenaar834193a2021-06-30 20:39:15 +02003201 // jump to first ":catch"
3202 ectx->ec_iidx = trycmd->tcd_catch_idx;
Bram Moolenaar3ef2e412023-04-30 18:50:48 +01003203 trycmd->tcd_in_catch = TRUE;
3204 }
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003205 did_throw = FALSE; // don't come back here until :endtry
3206 trycmd->tcd_did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003207 }
3208 else
3209 {
Bram Moolenaar3ef2e412023-04-30 18:50:48 +01003210 // Not inside try or need to return from current function.
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02003211 // Push a dummy return value.
Bram Moolenaar35578162021-08-02 19:10:38 +02003212 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003213 goto theend;
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02003214 tv = STACK_TV_BOT(0);
3215 tv->v_type = VAR_NUMBER;
3216 tv->vval.v_number = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003217 ++ectx->ec_stack.ga_len;
3218 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003219 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02003220 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +01003221 need_rethrow = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003222 if (handle_closure_in_use(ectx, FALSE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003223 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003224 goto done;
3225 }
3226
Bram Moolenaar4c137212021-04-19 16:48:48 +02003227 if (func_return(ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003228 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003229 }
3230 continue;
3231 }
3232
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003233 /*
3234 * Big switch on the instruction. Most compilers will be turning this
3235 * into an efficient lookup table, since the "case" values are an enum
3236 * with sequential numbers. It may look ugly, but it should be the
3237 * most efficient way.
3238 */
Bram Moolenaar4c137212021-04-19 16:48:48 +02003239 iptr = &ectx->ec_instr[ectx->ec_iidx++];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003240 switch (iptr->isn_type)
3241 {
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00003242 // Constructor, first instruction in a new() method.
Bram Moolenaar00b28d62022-12-08 15:32:33 +00003243 case ISN_CONSTRUCT:
3244 // "this" is always the local variable at index zero
3245 tv = STACK_TV_VAR(0);
3246 tv->v_type = VAR_OBJECT;
3247 tv->vval.v_object = alloc_clear(
3248 iptr->isn_arg.construct.construct_size);
3249 tv->vval.v_object->obj_class =
3250 iptr->isn_arg.construct.construct_class;
Bram Moolenaard28d7b92022-12-08 20:42:00 +00003251 ++tv->vval.v_object->obj_class->class_refcount;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00003252 tv->vval.v_object->obj_refcount = 1;
Bram Moolenaard28d7b92022-12-08 20:42:00 +00003253 object_created(tv->vval.v_object);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00003254 break;
3255
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003256 // execute Ex command line
3257 case ISN_EXEC:
Ernie Raelee865f32023-09-29 19:53:55 +02003258 if (exec_command(iptr, iptr->isn_arg.string) == FAIL)
Bram Moolenaaraacc9662021-08-13 19:40:51 +02003259 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003260 break;
3261
Bram Moolenaar20677332021-06-06 17:02:53 +02003262 // execute Ex command line split at NL characters.
3263 case ISN_EXEC_SPLIT:
3264 {
3265 source_cookie_T cookie;
Bram Moolenaar518df272021-06-06 17:34:13 +02003266 char_u *line;
Bram Moolenaar20677332021-06-06 17:02:53 +02003267
3268 SOURCING_LNUM = iptr->isn_lnum;
3269 CLEAR_FIELD(cookie);
3270 cookie.sourcing_lnum = iptr->isn_lnum - 1;
3271 cookie.nextline = iptr->isn_arg.string;
Bram Moolenaar518df272021-06-06 17:34:13 +02003272 line = get_split_sourceline(0, &cookie, 0, 0);
3273 if (do_cmdline(line,
Bram Moolenaar20677332021-06-06 17:02:53 +02003274 get_split_sourceline, &cookie,
3275 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED)
3276 == FAIL
3277 || did_emsg)
Bram Moolenaar518df272021-06-06 17:34:13 +02003278 {
3279 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02003280 goto on_error;
Bram Moolenaar518df272021-06-06 17:34:13 +02003281 }
3282 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02003283 }
3284 break;
3285
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00003286 // execute Ex command line that is only a range
3287 case ISN_EXECRANGE:
3288 {
3289 exarg_T ea;
3290 char *error = NULL;
3291
3292 CLEAR_FIELD(ea);
3293 ea.cmdidx = CMD_SIZE;
3294 ea.addr_type = ADDR_LINES;
3295 ea.cmd = iptr->isn_arg.string;
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00003296 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00003297 parse_cmd_address(&ea, &error, FALSE);
Bram Moolenaar01a4dcb2021-12-04 13:15:10 +00003298 if (ea.cmd == NULL)
3299 goto on_error;
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00003300 // error is always NULL when using ADDR_LINES
3301 error = ex_range_without_command(&ea);
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00003302 if (error != NULL)
3303 {
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00003304 emsg(error);
3305 goto on_error;
3306 }
3307 }
3308 break;
3309
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02003310 // Evaluate an expression with legacy syntax, push it onto the
3311 // stack.
3312 case ISN_LEGACY_EVAL:
3313 {
3314 char_u *arg = iptr->isn_arg.string;
3315 int res;
3316 int save_flags = cmdmod.cmod_flags;
3317
Bram Moolenaar35578162021-08-02 19:10:38 +02003318 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003319 goto theend;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02003320 tv = STACK_TV_BOT(0);
3321 init_tv(tv);
3322 cmdmod.cmod_flags |= CMOD_LEGACY;
3323 res = eval0(arg, tv, NULL, &EVALARG_EVALUATE);
3324 cmdmod.cmod_flags = save_flags;
3325 if (res == FAIL)
3326 goto on_error;
3327 ++ectx->ec_stack.ga_len;
3328 }
3329 break;
3330
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003331 // push typeval VAR_INSTR with instructions to be executed
3332 case ISN_INSTR:
3333 {
Bram Moolenaar35578162021-08-02 19:10:38 +02003334 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003335 goto theend;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003336 tv = STACK_TV_BOT(0);
3337 tv->vval.v_instr = ALLOC_ONE(instr_T);
3338 if (tv->vval.v_instr == NULL)
3339 goto on_error;
3340 ++ectx->ec_stack.ga_len;
3341
3342 tv->v_type = VAR_INSTR;
3343 tv->vval.v_instr->instr_ectx = ectx;
3344 tv->vval.v_instr->instr_instr = iptr->isn_arg.instr;
3345 }
3346 break;
3347
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003348 case ISN_SOURCE:
3349 {
Bram Moolenaar89445512022-04-14 12:58:23 +01003350 int notused;
LemonBoy77142312022-04-15 20:50:46 +01003351
Bram Moolenaar89445512022-04-14 12:58:23 +01003352 SOURCING_LNUM = iptr->isn_lnum;
3353 if (may_load_script((int)iptr->isn_arg.number, &notused)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003354 == FAIL)
Bram Moolenaar89445512022-04-14 12:58:23 +01003355 goto on_error;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003356 }
3357 break;
3358
Bram Moolenaar4c137212021-04-19 16:48:48 +02003359 // execute :substitute with an expression
3360 case ISN_SUBSTITUTE:
3361 {
3362 subs_T *subs = &iptr->isn_arg.subs;
3363 source_cookie_T cookie;
3364 struct subs_expr_S *save_instr = substitute_instr;
3365 struct subs_expr_S subs_instr;
3366 int res;
3367
3368 subs_instr.subs_ectx = ectx;
3369 subs_instr.subs_instr = subs->subs_instr;
3370 subs_instr.subs_status = OK;
3371 substitute_instr = &subs_instr;
3372
3373 SOURCING_LNUM = iptr->isn_lnum;
3374 // This is very much like ISN_EXEC
3375 CLEAR_FIELD(cookie);
3376 cookie.sourcing_lnum = iptr->isn_lnum - 1;
3377 res = do_cmdline(subs->subs_cmd,
3378 getsourceline, &cookie,
3379 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
3380 substitute_instr = save_instr;
3381
3382 if (res == FAIL || did_emsg
3383 || subs_instr.subs_status == FAIL)
3384 goto on_error;
3385 }
3386 break;
3387
3388 case ISN_FINISH:
3389 goto done;
3390
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02003391 case ISN_REDIRSTART:
3392 // create a dummy entry for var_redir_str()
3393 if (alloc_redir_lval() == FAIL)
3394 goto on_error;
3395
3396 // The output is stored in growarray "redir_ga" until
3397 // redirection ends.
3398 init_redir_ga();
3399 redir_vname = 1;
3400 break;
3401
3402 case ISN_REDIREND:
3403 {
3404 char_u *res = get_clear_redir_ga();
3405
3406 // End redirection, put redirected text on the stack.
3407 clear_redir_lval();
3408 redir_vname = 0;
3409
Bram Moolenaar35578162021-08-02 19:10:38 +02003410 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02003411 {
3412 vim_free(res);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003413 goto theend;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02003414 }
3415 tv = STACK_TV_BOT(0);
3416 tv->v_type = VAR_STRING;
3417 tv->vval.v_string = res;
3418 ++ectx->ec_stack.ga_len;
3419 }
3420 break;
3421
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003422 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02003423#ifdef FEAT_QUICKFIX
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003424 force_abort = TRUE;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003425 if (trigger_cexpr_autocmd(iptr->isn_arg.number) == FAIL)
3426 goto on_error;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003427 force_abort = FALSE;
Bram Moolenaarb7c97812021-05-05 22:51:39 +02003428#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003429 break;
3430
3431 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02003432#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003433 {
3434 exarg_T ea;
3435 int res;
3436
3437 CLEAR_FIELD(ea);
3438 ea.cmdidx = iptr->isn_arg.cexpr.cexpr_ref->cer_cmdidx;
3439 ea.forceit = iptr->isn_arg.cexpr.cexpr_ref->cer_forceit;
3440 ea.cmdlinep = &iptr->isn_arg.cexpr.cexpr_ref->cer_cmdline;
3441 --ectx->ec_stack.ga_len;
3442 tv = STACK_TV_BOT(0);
Bram Moolenaarbd683e32022-07-18 17:49:03 +01003443 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003444 res = cexpr_core(&ea, tv);
3445 clear_tv(tv);
3446 if (res == FAIL)
3447 goto on_error;
3448 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02003449#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003450 break;
3451
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003452 // execute Ex command from pieces on the stack
3453 case ISN_EXECCONCAT:
3454 {
3455 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +02003456 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003457 int pass;
3458 int i;
3459 char_u *cmd = NULL;
3460 char_u *str;
3461
3462 for (pass = 1; pass <= 2; ++pass)
3463 {
3464 for (i = 0; i < count; ++i)
3465 {
3466 tv = STACK_TV_BOT(i - count);
3467 str = tv->vval.v_string;
3468 if (str != NULL && *str != NUL)
3469 {
3470 if (pass == 2)
3471 STRCPY(cmd + len, str);
3472 len += STRLEN(str);
3473 }
3474 if (pass == 2)
3475 clear_tv(tv);
3476 }
3477 if (pass == 1)
3478 {
3479 cmd = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003480 if (unlikely(cmd == NULL))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003481 goto theend;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003482 len = 0;
3483 }
3484 }
3485
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02003486 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003487 do_cmdline_cmd(cmd);
3488 vim_free(cmd);
3489 }
3490 break;
3491
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003492 // execute :echo {string} ...
3493 case ISN_ECHO:
3494 {
3495 int count = iptr->isn_arg.echo.echo_count;
3496 int atstart = TRUE;
3497 int needclr = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003498 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003499
3500 for (idx = 0; idx < count; ++idx)
3501 {
3502 tv = STACK_TV_BOT(idx - count);
3503 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
3504 &atstart, &needclr);
3505 clear_tv(tv);
3506 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01003507 if (needclr)
3508 msg_clr_eos();
Bram Moolenaar4c137212021-04-19 16:48:48 +02003509 ectx->ec_stack.ga_len -= count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003510 }
3511 break;
3512
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003513 // :execute {string} ...
3514 // :echomsg {string} ...
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01003515 // :echowindow {string} ...
Bram Moolenaar7de62622021-08-07 15:05:47 +02003516 // :echoconsole {string} ...
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003517 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01003518 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003519 case ISN_ECHOMSG:
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01003520 case ISN_ECHOWINDOW:
Bram Moolenaar7de62622021-08-07 15:05:47 +02003521 case ISN_ECHOCONSOLE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003522 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01003523 {
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01003524 int count;
Bram Moolenaarad39c092020-02-26 18:23:43 +01003525 garray_T ga;
3526 char_u buf[NUMBUFLEN];
3527 char_u *p;
3528 int len;
3529 int failed = FALSE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003530 int idx;
Bram Moolenaarad39c092020-02-26 18:23:43 +01003531
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01003532 if (iptr->isn_type == ISN_ECHOWINDOW)
3533 count = iptr->isn_arg.echowin.ewin_count;
3534 else
3535 count = iptr->isn_arg.number;
Bram Moolenaarad39c092020-02-26 18:23:43 +01003536 ga_init2(&ga, 1, 80);
3537 for (idx = 0; idx < count; ++idx)
3538 {
3539 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003540 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01003541 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003542 if (tv->v_type == VAR_CHANNEL
3543 || tv->v_type == VAR_JOB)
3544 {
3545 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar68db9962021-05-09 23:19:22 +02003546 semsg(_(e_using_invalid_value_as_string_str),
3547 vartype_name(tv->v_type));
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003548 break;
3549 }
3550 else
3551 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01003552 }
3553 else
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003554 p = tv_stringify(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01003555
3556 len = (int)STRLEN(p);
Bram Moolenaar35578162021-08-02 19:10:38 +02003557 if (GA_GROW_FAILS(&ga, len + 2))
Bram Moolenaarad39c092020-02-26 18:23:43 +01003558 failed = TRUE;
3559 else
3560 {
3561 if (ga.ga_len > 0)
3562 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
3563 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
3564 ga.ga_len += len;
3565 }
3566 clear_tv(tv);
3567 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003568 ectx->ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003569 if (failed)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003570 {
3571 ga_clear(&ga);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003572 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003573 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01003574
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003575 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003576 {
3577 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02003578 {
3579 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003580 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01003581 if (did_emsg)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003582 {
3583 ga_clear(&ga);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01003584 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003585 }
Bram Moolenaar430deb12020-08-23 16:29:11 +02003586 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003587 else
3588 {
3589 msg_sb_eol();
3590 if (iptr->isn_type == ISN_ECHOMSG)
3591 {
3592 msg_attr(ga.ga_data, echo_attr);
3593 out_flush();
3594 }
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01003595#ifdef HAS_MESSAGE_WINDOW
3596 else if (iptr->isn_type == ISN_ECHOWINDOW)
3597 {
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01003598 start_echowindow(
3599 iptr->isn_arg.echowin.ewin_time);
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01003600 msg_attr(ga.ga_data, echo_attr);
3601 end_echowindow();
3602 }
3603#endif
Bram Moolenaar7de62622021-08-07 15:05:47 +02003604 else if (iptr->isn_type == ISN_ECHOCONSOLE)
3605 {
3606 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data),
3607 TRUE);
3608 ui_write((char_u *)"\r\n", 2, TRUE);
3609 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003610 else
3611 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003612 SOURCING_LNUM = iptr->isn_lnum;
3613 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003614 }
3615 }
3616 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01003617 ga_clear(&ga);
3618 }
3619 break;
3620
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003621 // load local variable or argument
3622 case ISN_LOAD:
Bram Moolenaar35578162021-08-02 19:10:38 +02003623 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003624 goto theend;
Bram Moolenaar2ed57ac2023-04-01 22:05:38 +01003625 tv = STACK_TV_VAR(iptr->isn_arg.number);
3626 if (tv->v_type == VAR_UNKNOWN)
3627 {
3628 // missing argument or default value v:none
3629 STACK_TV_BOT(0)->v_type = VAR_SPECIAL;
3630 STACK_TV_BOT(0)->vval.v_number = VVAL_NONE;
3631 }
3632 else
3633 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003634 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003635 break;
3636
3637 // load v: variable
3638 case ISN_LOADV:
Bram Moolenaar35578162021-08-02 19:10:38 +02003639 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003640 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003641 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003642 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003643 break;
3644
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003645 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003646 case ISN_LOADSCRIPT:
3647 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01003648 scriptref_T *sref = iptr->isn_arg.script.scriptref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003649 svar_T *sv;
3650
Bram Moolenaar6db660b2021-08-01 14:08:54 +02003651 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003652 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003653 goto theend;
Bram Moolenaar859cc212022-03-28 15:22:35 +01003654 allocate_if_null(sv);
Bram Moolenaar35578162021-08-02 19:10:38 +02003655 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003656 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003657 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003658 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003659 }
3660 break;
3661
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003662 // load s: variable in old script or autoload import
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003663 case ISN_LOADS:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003664 case ISN_LOADEXPORT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003665 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003666 int sid = iptr->isn_arg.loadstore.ls_sid;
3667 hashtab_T *ht = &SCRIPT_VARS(sid);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003668 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003669 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003670
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003671 if (di == NULL)
3672 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003673 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003674 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003675 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003676 }
3677 else
3678 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003679 if (iptr->isn_type == ISN_LOADEXPORT)
3680 {
3681 int idx = get_script_item_idx(sid, name, 0,
3682 NULL, NULL);
3683 svar_T *sv;
3684
3685 if (idx >= 0)
3686 {
3687 sv = ((svar_T *)SCRIPT_ITEM(sid)
3688 ->sn_var_vals.ga_data) + idx;
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003689 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003690 {
3691 SOURCING_LNUM = iptr->isn_lnum;
3692 semsg(_(e_item_not_exported_in_script_str),
3693 name);
3694 goto on_error;
3695 }
3696 }
3697 }
Bram Moolenaar35578162021-08-02 19:10:38 +02003698 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003699 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003700 copy_tv(&di->di_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003701 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003702 }
3703 }
3704 break;
3705
Bram Moolenaard3aac292020-04-19 14:32:17 +02003706 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003707 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02003708 case ISN_LOADB:
3709 case ISN_LOADW:
3710 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003711 {
Bram Moolenaar06b77222022-01-25 15:51:56 +00003712 int res = load_namespace_var(ectx, iptr->isn_type, iptr);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003713
Bram Moolenaar06b77222022-01-25 15:51:56 +00003714 if (res == NOTDONE)
Bram Moolenaarcbbc48f2022-01-18 12:58:28 +00003715 goto theend;
Bram Moolenaar06b77222022-01-25 15:51:56 +00003716 if (res == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003717 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003718 }
Bram Moolenaar06b77222022-01-25 15:51:56 +00003719
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003720 break;
3721
Bram Moolenaar03290b82020-12-19 16:30:44 +01003722 // load autoload variable
3723 case ISN_LOADAUTO:
3724 {
3725 char_u *name = iptr->isn_arg.string;
3726
Bram Moolenaar35578162021-08-02 19:10:38 +02003727 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003728 goto theend;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003729 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003730 if (eval_variable(name, (int)STRLEN(name), 0,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003731 STACK_TV_BOT(0), NULL, EVAL_VAR_VERBOSE) == FAIL)
Bram Moolenaar03290b82020-12-19 16:30:44 +01003732 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003733 ++ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003734 }
3735 break;
3736
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003737 // load g:/b:/w:/t: namespace
3738 case ISN_LOADGDICT:
3739 case ISN_LOADBDICT:
3740 case ISN_LOADWDICT:
3741 case ISN_LOADTDICT:
3742 {
3743 dict_T *d = NULL;
3744
3745 switch (iptr->isn_type)
3746 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02003747 case ISN_LOADGDICT: d = get_globvar_dict(); break;
3748 case ISN_LOADBDICT: d = curbuf->b_vars; break;
3749 case ISN_LOADWDICT: d = curwin->w_vars; break;
3750 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003751 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003752 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003753 }
Bram Moolenaar35578162021-08-02 19:10:38 +02003754 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003755 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003756 tv = STACK_TV_BOT(0);
3757 tv->v_type = VAR_DICT;
3758 tv->v_lock = 0;
3759 tv->vval.v_dict = d;
Bram Moolenaar1bd3cb22021-02-24 12:27:31 +01003760 ++d->dv_refcount;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003761 ++ectx->ec_stack.ga_len;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003762 }
3763 break;
3764
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003765 // load &option
3766 case ISN_LOADOPT:
3767 {
3768 typval_T optval;
3769 char_u *name = iptr->isn_arg.string;
3770
Bram Moolenaara8c17702020-04-01 21:17:24 +02003771 // This is not expected to fail, name is checked during
3772 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar35578162021-08-02 19:10:38 +02003773 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003774 goto theend;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003775 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003776 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003777 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003778 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003779 }
3780 break;
3781
3782 // load $ENV
3783 case ISN_LOADENV:
3784 {
3785 typval_T optval;
3786 char_u *name = iptr->isn_arg.string;
3787
Bram Moolenaar35578162021-08-02 19:10:38 +02003788 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003789 goto theend;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003790 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003791 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003792 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003793 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003794 }
3795 break;
3796
3797 // load @register
3798 case ISN_LOADREG:
Bram Moolenaar35578162021-08-02 19:10:38 +02003799 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003800 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003801 tv = STACK_TV_BOT(0);
3802 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003803 tv->v_lock = 0;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02003804 // This may result in NULL, which should be equivalent to an
3805 // empty string.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003806 tv->vval.v_string = get_reg_contents(
3807 iptr->isn_arg.number, GREG_EXPR_SRC);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003808 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003809 break;
3810
3811 // store local variable
3812 case ISN_STORE:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003813 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003814 tv = STACK_TV_VAR(iptr->isn_arg.number);
3815 clear_tv(tv);
3816 *tv = *STACK_TV_BOT(0);
3817 break;
3818
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003819 // store s: variable in old script or autoload import
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003820 case ISN_STORES:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003821 case ISN_STOREEXPORT:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003822 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003823 int sid = iptr->isn_arg.loadstore.ls_sid;
3824 hashtab_T *ht = &SCRIPT_VARS(sid);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003825 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003826 dictitem_T *di = find_var_in_ht(ht, 0,
3827 iptr->isn_type == ISN_STORES
3828 ? name + 2 : name, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003829
Bram Moolenaar4c137212021-04-19 16:48:48 +02003830 --ectx->ec_stack.ga_len;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003831 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003832 if (di == NULL)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003833 {
3834 if (iptr->isn_type == ISN_STOREEXPORT)
3835 {
3836 semsg(_(e_undefined_variable_str), name);
Bram Moolenaard1d26842022-03-31 10:13:47 +01003837 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003838 goto on_error;
3839 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003840 store_var(name, STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003841 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003842 else
3843 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003844 if (iptr->isn_type == ISN_STOREEXPORT)
3845 {
3846 int idx = get_script_item_idx(sid, name, 0,
3847 NULL, NULL);
3848
Bram Moolenaar06651632022-04-27 17:54:25 +01003849 // can this ever fail?
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003850 if (idx >= 0)
3851 {
3852 svar_T *sv = ((svar_T *)SCRIPT_ITEM(sid)
3853 ->sn_var_vals.ga_data) + idx;
3854
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003855 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003856 {
3857 semsg(_(e_item_not_exported_in_script_str),
3858 name);
Bram Moolenaard1d26842022-03-31 10:13:47 +01003859 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003860 goto on_error;
3861 }
3862 }
3863 }
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02003864 if (var_check_permission(di, name) == FAIL)
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003865 {
3866 clear_tv(STACK_TV_BOT(0));
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02003867 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003868 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003869 clear_tv(&di->di_tv);
3870 di->di_tv = *STACK_TV_BOT(0);
3871 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003872 }
3873 break;
3874
3875 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003876 case ISN_STORESCRIPT:
3877 {
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003878 scriptref_T *sref = iptr->isn_arg.script.scriptref;
3879 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003880
Bram Moolenaar6db660b2021-08-01 14:08:54 +02003881 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003882 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003883 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003884 --ectx->ec_stack.ga_len;
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003885
3886 // "const" and "final" are checked at compile time, locking
3887 // the value needs to be checked here.
3888 SOURCING_LNUM = iptr->isn_lnum;
3889 if (value_check_lock(sv->sv_tv->v_lock, sv->sv_name, FALSE))
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003890 {
3891 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003892 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003893 }
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003894
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003895 clear_tv(sv->sv_tv);
3896 *sv->sv_tv = *STACK_TV_BOT(0);
3897 }
3898 break;
3899
3900 // store option
3901 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003902 case ISN_STOREFUNCOPT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003903 {
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003904 char_u *opt_name = iptr->isn_arg.storeopt.so_name;
3905 int opt_flags = iptr->isn_arg.storeopt.so_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003906 long n = 0;
3907 char_u *s = NULL;
3908 char *msg;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003909 char_u numbuf[NUMBUFLEN];
3910 char_u *tofree = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003911
Bram Moolenaar4c137212021-04-19 16:48:48 +02003912 --ectx->ec_stack.ga_len;
LemonBoy32f34612023-09-02 21:52:05 +02003913 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003914 tv = STACK_TV_BOT(0);
3915 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01003916 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003917 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01003918 if (s == NULL)
3919 s = (char_u *)"";
3920 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003921 else if (iptr->isn_type == ISN_STOREFUNCOPT)
3922 {
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003923 // If the option can be set to a function reference or
3924 // a lambda and the passed value is a function
3925 // reference, then convert it to the name (string) of
3926 // the function reference.
3927 s = tv2string(tv, &tofree, numbuf, 0);
3928 if (s == NULL || *s == NUL)
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003929 {
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003930 // cannot happen?
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003931 clear_tv(tv);
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003932 vim_free(tofree);
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003933 goto on_error;
3934 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003935 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003936 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003937 // must be VAR_NUMBER, CHECKTYPE makes sure
3938 n = tv->vval.v_number;
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003939 msg = set_option_value(opt_name, n, s, opt_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02003940 clear_tv(tv);
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003941 vim_free(tofree);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003942 if (msg != NULL)
3943 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003944 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003945 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02003946 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003947 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003948 }
3949 break;
3950
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003951 // store $ENV
3952 case ISN_STOREENV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003953 --ectx->ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003954 tv = STACK_TV_BOT(0);
3955 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
3956 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003957 break;
3958
3959 // store @r
3960 case ISN_STOREREG:
3961 {
3962 int reg = iptr->isn_arg.number;
3963
Bram Moolenaar4c137212021-04-19 16:48:48 +02003964 --ectx->ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01003965 tv = STACK_TV_BOT(0);
Bram Moolenaar74f4a962021-06-17 21:03:07 +02003966 write_reg_contents(reg, tv_get_string(tv), -1, FALSE);
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01003967 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003968 }
3969 break;
3970
3971 // store v: variable
3972 case ISN_STOREV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003973 --ectx->ec_stack.ga_len;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003974 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
3975 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003976 // should not happen, type is checked when compiling
3977 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003978 break;
3979
Bram Moolenaard3aac292020-04-19 14:32:17 +02003980 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003981 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02003982 case ISN_STOREB:
3983 case ISN_STOREW:
3984 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003985 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003986 dictitem_T *di;
3987 hashtab_T *ht;
3988 char_u *name = iptr->isn_arg.string + 2;
3989
Bram Moolenaard3aac292020-04-19 14:32:17 +02003990 switch (iptr->isn_type)
3991 {
3992 case ISN_STOREG:
3993 ht = get_globvar_ht();
3994 break;
3995 case ISN_STOREB:
3996 ht = &curbuf->b_vars->dv_hashtab;
3997 break;
3998 case ISN_STOREW:
3999 ht = &curwin->w_vars->dv_hashtab;
4000 break;
4001 case ISN_STORET:
4002 ht = &curtab->tp_vars->dv_hashtab;
4003 break;
4004 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004005 goto theend;
Bram Moolenaard3aac292020-04-19 14:32:17 +02004006 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004007
Bram Moolenaar4c137212021-04-19 16:48:48 +02004008 --ectx->ec_stack.ga_len;
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01004009 di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004010 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01004011 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004012 else
4013 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01004014 SOURCING_LNUM = iptr->isn_lnum;
4015 if (var_check_permission(di, name) == FAIL)
4016 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004017 clear_tv(&di->di_tv);
4018 di->di_tv = *STACK_TV_BOT(0);
4019 }
4020 }
4021 break;
4022
Bram Moolenaar03290b82020-12-19 16:30:44 +01004023 // store an autoload variable
4024 case ISN_STOREAUTO:
4025 SOURCING_LNUM = iptr->isn_lnum;
4026 set_var(iptr->isn_arg.string, STACK_TV_BOT(-1), TRUE);
4027 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02004028 --ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01004029 break;
4030
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004031 // store number in local variable
4032 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01004033 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004034 clear_tv(tv);
4035 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01004036 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004037 break;
4038
Bram Moolenaar590162c2022-12-24 21:24:06 +00004039 // Store a value in a list, dict, blob or object variable.
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01004040 case ISN_STOREINDEX:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004041 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00004042 int res = execute_storeindex(iptr, ectx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004043
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00004044 if (res == FAIL)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02004045 goto on_error;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00004046 if (res == NOTDONE)
4047 goto theend;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004048 }
4049 break;
4050
Bram Moolenaarea5c8982022-02-17 14:42:02 +00004051 // store value in list or blob range
Bram Moolenaar68452172021-04-12 21:21:02 +02004052 case ISN_STORERANGE:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00004053 if (execute_storerange(iptr, ectx) == FAIL)
4054 goto on_error;
Bram Moolenaar68452172021-04-12 21:21:02 +02004055 break;
4056
Bram Moolenaard505d172022-12-18 21:42:55 +00004057 case ISN_LOAD_CLASSMEMBER:
4058 {
4059 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
4060 goto theend;
4061 classmember_T *cm = &iptr->isn_arg.classmember;
Bram Moolenaar4e2406c2023-06-24 19:22:21 +01004062 copy_tv(cm->cm_class->class_members_tv + cm->cm_idx,
4063 STACK_TV_BOT(0));
Bram Moolenaard505d172022-12-18 21:42:55 +00004064 ++ectx->ec_stack.ga_len;
4065 }
4066 break;
4067
4068 case ISN_STORE_CLASSMEMBER:
4069 {
4070 classmember_T *cm = &iptr->isn_arg.classmember;
4071 tv = &cm->cm_class->class_members_tv[cm->cm_idx];
4072 clear_tv(tv);
4073 *tv = *STACK_TV_BOT(-1);
4074 --ectx->ec_stack.ga_len;
4075 }
4076 break;
4077
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01004078 // Load or store variable or argument from outer scope.
Bram Moolenaar0186e582021-01-10 18:33:11 +01004079 case ISN_LOADOUTER:
4080 case ISN_STOREOUTER:
4081 {
4082 int depth = iptr->isn_arg.outer.outer_depth;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004083 outer_T *outer = ectx->ec_outer_ref == NULL ? NULL
4084 : ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +01004085
4086 while (depth > 1 && outer != NULL)
4087 {
4088 outer = outer->out_up;
4089 --depth;
4090 }
4091 if (outer == NULL)
4092 {
4093 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar69c76172021-12-02 16:38:52 +00004094 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx
4095 || ectx->ec_outer_ref == NULL)
4096 // Possibly :def function called from legacy
4097 // context.
4098 emsg(_(e_closure_called_from_invalid_context));
4099 else
4100 iemsg("LOADOUTER depth more than scope levels");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004101 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01004102 }
Bram Moolenaarcc341812022-09-19 15:54:34 +01004103 if (depth < 0)
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01004104 // Variable declared in loop. May be copied if the
4105 // loop block has already ended.
Bram Moolenaarcc341812022-09-19 15:54:34 +01004106 tv = ((typval_T *)outer->out_loop[-depth - 1]
4107 .stack->ga_data)
4108 + outer->out_loop[-depth - 1].var_idx
4109 + iptr->isn_arg.outer.outer_idx;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004110 else
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01004111 // Variable declared in a function. May be copied if
4112 // the function has already returned.
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004113 tv = ((typval_T *)outer->out_stack->ga_data)
4114 + outer->out_frame_idx + STACK_FRAME_SIZE
4115 + iptr->isn_arg.outer.outer_idx;
Bram Moolenaar0186e582021-01-10 18:33:11 +01004116 if (iptr->isn_type == ISN_LOADOUTER)
4117 {
Bram Moolenaar35578162021-08-02 19:10:38 +02004118 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004119 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01004120 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02004121 ++ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01004122 }
4123 else
4124 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004125 --ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01004126 clear_tv(tv);
4127 *tv = *STACK_TV_BOT(0);
4128 }
4129 }
4130 break;
4131
Bram Moolenaar752fc692021-01-04 21:57:11 +01004132 // unlet item in list or dict variable
4133 case ISN_UNLETINDEX:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00004134 if (execute_unletindex(iptr, ectx) == FAIL)
4135 goto on_error;
Bram Moolenaar752fc692021-01-04 21:57:11 +01004136 break;
4137
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01004138 // unlet range of items in list variable
4139 case ISN_UNLETRANGE:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00004140 if (execute_unletrange(iptr, ectx) == FAIL)
4141 goto on_error;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01004142 break;
4143
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004144 // push constant
4145 case ISN_PUSHNR:
4146 case ISN_PUSHBOOL:
4147 case ISN_PUSHSPEC:
4148 case ISN_PUSHF:
4149 case ISN_PUSHS:
4150 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004151 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004152 case ISN_PUSHCHANNEL:
4153 case ISN_PUSHJOB:
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00004154 case ISN_PUSHOBJ:
4155 case ISN_PUSHCLASS:
Bram Moolenaar35578162021-08-02 19:10:38 +02004156 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004157 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004158 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02004159 tv->v_lock = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004160 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004161 switch (iptr->isn_type)
4162 {
4163 case ISN_PUSHNR:
4164 tv->v_type = VAR_NUMBER;
4165 tv->vval.v_number = iptr->isn_arg.number;
4166 break;
4167 case ISN_PUSHBOOL:
4168 tv->v_type = VAR_BOOL;
4169 tv->vval.v_number = iptr->isn_arg.number;
4170 break;
4171 case ISN_PUSHSPEC:
4172 tv->v_type = VAR_SPECIAL;
4173 tv->vval.v_number = iptr->isn_arg.number;
4174 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004175 case ISN_PUSHF:
4176 tv->v_type = VAR_FLOAT;
4177 tv->vval.v_float = iptr->isn_arg.fnumber;
4178 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004179 case ISN_PUSHBLOB:
4180 blob_copy(iptr->isn_arg.blob, tv);
4181 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004182 case ISN_PUSHFUNC:
4183 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01004184 if (iptr->isn_arg.string == NULL)
4185 tv->vval.v_string = NULL;
4186 else
4187 tv->vval.v_string =
4188 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004189 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004190 case ISN_PUSHCHANNEL:
4191#ifdef FEAT_JOB_CHANNEL
4192 tv->v_type = VAR_CHANNEL;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00004193 tv->vval.v_channel = NULL;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004194#endif
4195 break;
4196 case ISN_PUSHJOB:
4197#ifdef FEAT_JOB_CHANNEL
4198 tv->v_type = VAR_JOB;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00004199 tv->vval.v_job = NULL;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004200#endif
4201 break;
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00004202 case ISN_PUSHOBJ:
4203 tv->v_type = VAR_OBJECT;
4204 tv->vval.v_object = NULL;
4205 break;
4206 case ISN_PUSHCLASS:
4207 tv->v_type = VAR_CLASS;
Bram Moolenaar30a84472023-02-27 08:07:14 +00004208 tv->vval.v_class = iptr->isn_arg.classarg;
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00004209 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004210 default:
4211 tv->v_type = VAR_STRING;
Bram Moolenaarf8691002022-03-10 12:20:53 +00004212 tv->vval.v_string = iptr->isn_arg.string == NULL
4213 ? NULL : vim_strsave(iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004214 }
4215 break;
4216
Bram Moolenaar06b77222022-01-25 15:51:56 +00004217 case ISN_AUTOLOAD:
4218 {
4219 char_u *name = iptr->isn_arg.string;
4220
4221 (void)script_autoload(name, FALSE);
4222 if (find_func(name, TRUE))
4223 {
4224 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
4225 goto theend;
4226 tv = STACK_TV_BOT(0);
4227 tv->v_lock = 0;
4228 ++ectx->ec_stack.ga_len;
4229 tv->v_type = VAR_FUNC;
4230 tv->vval.v_string = vim_strsave(name);
4231 }
4232 else
4233 {
4234 int res = load_namespace_var(ectx, ISN_LOADG, iptr);
4235
4236 if (res == NOTDONE)
4237 goto theend;
4238 if (res == FAIL)
4239 goto on_error;
4240 }
4241 }
4242 break;
4243
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004244 case ISN_UNLET:
4245 if (do_unlet(iptr->isn_arg.unlet.ul_name,
4246 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02004247 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004248 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02004249 case ISN_UNLETENV:
LemonBoy77142312022-04-15 20:50:46 +01004250 vim_unsetenv_ext(iptr->isn_arg.unlet.ul_name);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02004251 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004252
Bram Moolenaaraacc9662021-08-13 19:40:51 +02004253 case ISN_LOCKUNLOCK:
4254 {
Ernie Raelee865f32023-09-29 19:53:55 +02004255#ifdef LOG_LOCKVAR
4256 ch_log(NULL, "LKVAR: execute INS_LOCKUNLOCK isn_arg %s",
4257 iptr->isn_arg.string);
4258#endif
Ernie Rael4c8da022023-10-11 21:35:11 +02004259 lval_root_T *lval_root_save = lval_root;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02004260
4261 // Stack has the local variable, argument the whole :lock
4262 // or :unlock command, like ISN_EXEC.
4263 --ectx->ec_stack.ga_len;
Ernie Rael4c8da022023-10-11 21:35:11 +02004264 lval_root_T root = { .lr_tv = STACK_TV_BOT(0),
4265 .lr_cl_exec = iptr->isn_arg.lockunlock.lu_cl_exec,
4266 .lr_is_arg = iptr->isn_arg.lockunlock.lu_is_arg };
Ernie Rael64885642023-10-04 20:16:22 +02004267 lval_root = &root;
Ernie Rael4c8da022023-10-11 21:35:11 +02004268 int res = exec_command(iptr,
Ernie Rael64885642023-10-04 20:16:22 +02004269 iptr->isn_arg.lockunlock.lu_string);
4270 clear_tv(root.lr_tv);
Bram Moolenaaraacc9662021-08-13 19:40:51 +02004271 lval_root = lval_root_save;
4272 if (res == FAIL)
4273 goto on_error;
4274 }
4275 break;
4276
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02004277 case ISN_LOCKCONST:
4278 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
4279 break;
4280
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004281 // create a list from items on the stack; uses a single allocation
4282 // for the list header and the items
4283 case ISN_NEWLIST:
Bram Moolenaar4c137212021-04-19 16:48:48 +02004284 if (exe_newlist(iptr->isn_arg.number, ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004285 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004286 break;
4287
4288 // create a dict from items on the stack
4289 case ISN_NEWDICT:
4290 {
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01004291 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004292
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01004293 SOURCING_LNUM = iptr->isn_lnum;
4294 res = exe_newdict(iptr->isn_arg.number, ectx);
4295 if (res == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004296 goto theend;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01004297 if (res == MAYBE)
4298 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004299 }
4300 break;
4301
LemonBoy372bcce2022-04-25 12:43:20 +01004302 case ISN_CONCAT:
4303 if (exe_concat(iptr->isn_arg.number, ectx) == FAIL)
4304 goto theend;
4305 break;
4306
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004307 // create a partial with NULL value
4308 case ISN_NEWPARTIAL:
4309 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
4310 goto theend;
4311 ++ectx->ec_stack.ga_len;
4312 tv = STACK_TV_BOT(-1);
4313 tv->v_type = VAR_PARTIAL;
4314 tv->v_lock = 0;
4315 tv->vval.v_partial = NULL;
4316 break;
4317
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004318 // call a :def function
4319 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02004320 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01004321 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
4322 NULL,
4323 iptr->isn_arg.dfunc.cdf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02004324 ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02004325 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004326 break;
4327
Bram Moolenaard0200c82023-01-28 15:19:40 +00004328 // call a method on an interface
4329 case ISN_METHODCALL:
4330 {
Bram Moolenaar094cf9f2023-02-10 15:52:25 +00004331 cmfunc_T *mfunc = iptr->isn_arg.mfunc;
4332
Bram Moolenaard0200c82023-01-28 15:19:40 +00004333 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar094cf9f2023-02-10 15:52:25 +00004334 tv = STACK_TV_BOT(-1 - mfunc->cmf_argcount);
Bram Moolenaard0200c82023-01-28 15:19:40 +00004335 if (tv->v_type != VAR_OBJECT)
4336 {
4337 object_required_error(tv);
4338 goto on_error;
4339 }
4340 object_T *obj = tv->vval.v_object;
4341 class_T *cl = obj->obj_class;
4342
4343 // convert the interface index to the object index
Bram Moolenaard0200c82023-01-28 15:19:40 +00004344 int idx = object_index_from_itf_index(mfunc->cmf_itf,
Yegappan Lakshmanan5a05d372023-09-29 19:43:11 +02004345 TRUE, mfunc->cmf_idx, cl);
Bram Moolenaard0200c82023-01-28 15:19:40 +00004346
4347 if (call_ufunc(cl->class_obj_methods[idx], NULL,
4348 mfunc->cmf_argcount, ectx, NULL, NULL) == FAIL)
4349 goto on_error;
4350 }
4351 break;
4352
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004353 // call a builtin function
4354 case ISN_BCALL:
4355 SOURCING_LNUM = iptr->isn_lnum;
4356 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
4357 iptr->isn_arg.bfunc.cbf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02004358 ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004359 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004360 break;
4361
4362 // call a funcref or partial
4363 case ISN_PCALL:
4364 {
4365 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
4366 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02004367 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004368
4369 SOURCING_LNUM = iptr->isn_lnum;
4370 if (pfunc->cpf_top)
4371 {
4372 // funcref is above the arguments
4373 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
4374 }
4375 else
4376 {
4377 // Get the funcref from the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004378 --ectx->ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02004379 partial_tv = *STACK_TV_BOT(0);
4380 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004381 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004382 r = call_partial(tv, pfunc->cpf_argcount, ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02004383 if (tv == &partial_tv)
4384 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004385 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004386 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004387 }
4388 break;
4389
Bram Moolenaarbd5da372020-03-31 23:13:10 +02004390 case ISN_PCALL_END:
4391 // PCALL finished, arguments have been consumed and replaced by
4392 // the return value. Now clear the funcref from the stack,
4393 // and move the return value in its place.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004394 --ectx->ec_stack.ga_len;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02004395 clear_tv(STACK_TV_BOT(-1));
4396 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
4397 break;
4398
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004399 // call a user defined function or funcref/partial
4400 case ISN_UCALL:
4401 {
4402 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
4403
4404 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01004405 if (call_eval_func(cufunc->cuf_name, cufunc->cuf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02004406 ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004407 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004408 }
4409 break;
4410
Bram Moolenaar1d84f762022-09-03 21:35:53 +01004411 // :defer func(arg)
4412 case ISN_DEFER:
Bram Moolenaar806a2732022-09-04 15:40:36 +01004413 if (defer_command(iptr->isn_arg.defer.defer_var_idx,
Bram Moolenaar1d84f762022-09-03 21:35:53 +01004414 iptr->isn_arg.defer.defer_argcount, ectx) == FAIL)
4415 goto on_error;
4416 break;
4417
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004418 // Return from a :def function call without a value.
4419 // Return from a constructor.
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02004420 case ISN_RETURN_VOID:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004421 case ISN_RETURN_OBJECT:
Bram Moolenaar35578162021-08-02 19:10:38 +02004422 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004423 goto theend;
Bram Moolenaar299f3032021-01-08 20:53:09 +01004424 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004425 ++ectx->ec_stack.ga_len;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004426 if (iptr->isn_type == ISN_RETURN_VOID)
4427 {
4428 tv->v_type = VAR_VOID;
4429 tv->vval.v_number = 0;
4430 tv->v_lock = 0;
4431 }
4432 else
4433 {
4434 *tv = *STACK_TV_VAR(0);
4435 ++tv->vval.v_object->obj_refcount;
4436 }
Bram Moolenaar299f3032021-01-08 20:53:09 +01004437 // FALLTHROUGH
4438
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02004439 // return from a :def function call with what is on the stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004440 case ISN_RETURN:
4441 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004442 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01004443 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01004444
4445 if (trystack->ga_len > 0)
4446 trycmd = ((trycmd_T *)trystack->ga_data)
4447 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004448 if (trycmd != NULL
Bram Moolenaar4c137212021-04-19 16:48:48 +02004449 && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004450 {
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01004451 // jump to ":finally" or ":endtry"
4452 if (trycmd->tcd_finally_idx != 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004453 ectx->ec_iidx = trycmd->tcd_finally_idx;
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01004454 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02004455 ectx->ec_iidx = trycmd->tcd_endtry_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004456 trycmd->tcd_return = TRUE;
4457 }
4458 else
Bram Moolenaard032f342020-07-18 18:13:02 +02004459 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004460 }
4461 break;
4462
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004463 // push a partial, a reference to a compiled function
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004464 case ISN_FUNCREF:
4465 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004466 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
4467 ufunc_T *ufunc;
4468 funcref_T *funcref = &iptr->isn_arg.funcref;
4469 funcref_extra_T *extra = funcref->fr_extra;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004470
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004471 if (pt == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004472 goto theend;
Bram Moolenaar35578162021-08-02 19:10:38 +02004473 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02004474 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004475 vim_free(pt);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004476 goto theend;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004477 }
Bram Moolenaar313e4722023-02-08 20:55:27 +00004478 if (extra != NULL && extra->fre_class != NULL)
4479 {
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +02004480 class_T *cl;
4481 if (extra->fre_object_method)
Bram Moolenaar313e4722023-02-08 20:55:27 +00004482 {
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +02004483 tv = STACK_TV_BOT(-1);
4484 if (tv->v_type != VAR_OBJECT)
4485 {
4486 object_required_error(tv);
4487 vim_free(pt);
4488 goto on_error;
4489 }
Bram Moolenaar313e4722023-02-08 20:55:27 +00004490
Yegappan Lakshmanan29bb67f2023-10-14 11:18:50 +02004491 object_T *obj = tv->vval.v_object;
4492 cl = obj->obj_class;
4493 // drop the value from the stack
4494 clear_tv(tv);
4495 --ectx->ec_stack.ga_len;
4496
4497 pt->pt_obj = obj;
4498 ++obj->obj_refcount;
4499 }
4500 else
4501 cl = extra->fre_class;
4502
4503 if (extra->fre_object_method)
4504 {
4505 // object method
4506 // convert the interface index to the object index
4507 int idx =
4508 object_index_from_itf_index(extra->fre_class,
4509 TRUE, extra->fre_method_idx, cl);
4510 ufunc = cl->class_obj_methods[idx];
4511 }
4512 else
4513 {
4514 // class method
4515 ufunc =
4516 cl->class_class_functions[extra->fre_method_idx];
4517 }
Bram Moolenaar313e4722023-02-08 20:55:27 +00004518 }
4519 else if (extra == NULL || extra->fre_func_name == NULL)
Bram Moolenaar38453522021-11-28 22:00:12 +00004520 {
4521 dfunc_T *pt_dfunc = ((dfunc_T *)def_functions.ga_data)
4522 + funcref->fr_dfunc_idx;
4523
4524 ufunc = pt_dfunc->df_ufunc;
4525 }
4526 else
Bram Moolenaar313e4722023-02-08 20:55:27 +00004527 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004528 ufunc = find_func(extra->fre_func_name, FALSE);
Bram Moolenaar313e4722023-02-08 20:55:27 +00004529 }
Bram Moolenaar56acd1f2022-02-18 13:24:52 +00004530 if (ufunc == NULL)
4531 {
4532 SOURCING_LNUM = iptr->isn_lnum;
4533 iemsg("ufunc unexpectedly NULL for FUNCREF");
4534 goto theend;
4535 }
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004536 if (fill_partial_and_closure(pt, ufunc,
Bram Moolenaarcc341812022-09-19 15:54:34 +01004537 extra == NULL ? NULL : &extra->fre_loopvar_info,
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004538 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004539 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004540 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004541 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004542 tv->vval.v_partial = pt;
4543 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02004544 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004545 }
4546 break;
4547
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004548 // Create a global function from a lambda.
4549 case ISN_NEWFUNC:
4550 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004551 newfuncarg_T *arg = iptr->isn_arg.newfunc.nf_arg;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004552
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004553 if (copy_lambda_to_global_func(arg->nfa_lambda,
Bram Moolenaarcc341812022-09-19 15:54:34 +01004554 arg->nfa_global, &arg->nfa_loopvar_info,
4555 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004556 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004557 }
4558 break;
4559
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004560 // List functions
4561 case ISN_DEF:
4562 if (iptr->isn_arg.string == NULL)
4563 list_functions(NULL);
4564 else
4565 {
Bram Moolenaar14336722022-01-08 16:02:59 +00004566 exarg_T ea;
4567 garray_T lines_to_free;
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004568
4569 CLEAR_FIELD(ea);
4570 ea.cmd = ea.arg = iptr->isn_arg.string;
Bram Moolenaar14336722022-01-08 16:02:59 +00004571 ga_init2(&lines_to_free, sizeof(char_u *), 50);
Bram Moolenaard4a9b7f2023-05-23 14:48:42 +01004572 SOURCING_LNUM = iptr->isn_lnum;
h-eastb895b0f2023-09-24 15:46:31 +02004573 define_function(&ea, NULL, &lines_to_free, 0, NULL, 0);
Bram Moolenaar14336722022-01-08 16:02:59 +00004574 ga_clear_strings(&lines_to_free);
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004575 }
4576 break;
4577
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004578 // jump if a condition is met
4579 case ISN_JUMP:
4580 {
4581 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004582 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004583 int jump = TRUE;
4584
4585 if (when != JUMP_ALWAYS)
4586 {
4587 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004588 if (when == JUMP_IF_COND_FALSE
Bram Moolenaar13106602020-10-04 16:06:05 +02004589 || when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004590 || when == JUMP_IF_COND_TRUE)
4591 {
4592 SOURCING_LNUM = iptr->isn_lnum;
4593 jump = tv_get_bool_chk(tv, &error);
4594 if (error)
4595 goto on_error;
4596 }
4597 else
4598 jump = tv2bool(tv);
Bram Moolenaarf6ced982022-04-28 12:00:49 +01004599 if (when == JUMP_IF_FALSE || when == JUMP_IF_COND_FALSE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004600 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01004601 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004602 {
4603 // drop the value from the stack
4604 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004605 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004606 }
4607 }
4608 if (jump)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004609 ectx->ec_iidx = iptr->isn_arg.jump.jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004610 }
4611 break;
4612
Bram Moolenaarb46c0832022-09-15 17:19:37 +01004613 // "while": jump to end if a condition is false
4614 case ISN_WHILE:
4615 {
4616 int error = FALSE;
4617 int jump = TRUE;
4618
4619 tv = STACK_TV_BOT(-1);
4620 SOURCING_LNUM = iptr->isn_lnum;
4621 jump = !tv_get_bool_chk(tv, &error);
4622 if (error)
4623 goto on_error;
4624 // drop the value from the stack
4625 clear_tv(tv);
4626 --ectx->ec_stack.ga_len;
4627 if (jump)
4628 ectx->ec_iidx = iptr->isn_arg.whileloop.while_end;
4629
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01004630 // Store the current funcref count, may be used by
Bram Moolenaarcc341812022-09-19 15:54:34 +01004631 // ISN_ENDLOOP later
Bram Moolenaarb46c0832022-09-15 17:19:37 +01004632 tv = STACK_TV_VAR(
4633 iptr->isn_arg.whileloop.while_funcref_idx);
4634 tv->vval.v_number = ectx->ec_funcrefs.ga_len;
4635 }
4636 break;
4637
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02004638 // Jump if an argument with a default value was already set and not
4639 // v:none.
4640 case ISN_JUMP_IF_ARG_SET:
Bram Moolenaar65b0d162022-12-13 18:43:22 +00004641 case ISN_JUMP_IF_ARG_NOT_SET:
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02004642 tv = STACK_TV_VAR(iptr->isn_arg.jumparg.jump_arg_off);
Bram Moolenaar65b0d162022-12-13 18:43:22 +00004643 int arg_set = tv->v_type != VAR_UNKNOWN
4644 && !(tv->v_type == VAR_SPECIAL
4645 && tv->vval.v_number == VVAL_NONE);
4646 if (iptr->isn_type == ISN_JUMP_IF_ARG_SET ? arg_set : !arg_set)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004647 ectx->ec_iidx = iptr->isn_arg.jumparg.jump_where;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02004648 break;
4649
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004650 // top of a for loop
4651 case ISN_FOR:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00004652 if (execute_for(iptr, ectx) == FAIL)
4653 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004654 break;
4655
Bram Moolenaarb46c0832022-09-15 17:19:37 +01004656 // end of a for or while loop
4657 case ISN_ENDLOOP:
4658 if (execute_endloop(iptr, ectx) == FAIL)
4659 goto theend;
4660 break;
4661
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004662 // start of ":try" block
4663 case ISN_TRY:
4664 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01004665 trycmd_T *trycmd = NULL;
4666
Bram Moolenaar35578162021-08-02 19:10:38 +02004667 if (GA_GROW_FAILS(&ectx->ec_trystack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004668 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004669 trycmd = ((trycmd_T *)ectx->ec_trystack.ga_data)
4670 + ectx->ec_trystack.ga_len;
4671 ++ectx->ec_trystack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004672 ++trylevel;
Bram Moolenaar8d4be892021-02-13 18:33:02 +01004673 CLEAR_POINTER(trycmd);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004674 trycmd->tcd_frame_idx = ectx->ec_frame_idx;
4675 trycmd->tcd_stack_len = ectx->ec_stack.ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01004676 trycmd->tcd_catch_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00004677 iptr->isn_arg.tryref.try_ref->try_catch;
Bram Moolenaara91a7132021-03-25 21:12:15 +01004678 trycmd->tcd_finally_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00004679 iptr->isn_arg.tryref.try_ref->try_finally;
Bram Moolenaara91a7132021-03-25 21:12:15 +01004680 trycmd->tcd_endtry_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00004681 iptr->isn_arg.tryref.try_ref->try_endtry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004682 }
4683 break;
4684
4685 case ISN_PUSHEXC:
4686 if (current_exception == NULL)
4687 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004688 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004689 iemsg("Evaluating catch while current_exception is NULL");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004690 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004691 }
Bram Moolenaar35578162021-08-02 19:10:38 +02004692 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004693 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004694 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004695 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004696 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02004697 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004698 tv->vval.v_string = vim_strsave(
4699 (char_u *)current_exception->value);
4700 break;
4701
4702 case ISN_CATCH:
4703 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004704 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar06651632022-04-27 17:54:25 +01004705 trycmd_T *trycmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004706
Bram Moolenaar4c137212021-04-19 16:48:48 +02004707 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar06651632022-04-27 17:54:25 +01004708 trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004709 + trystack->ga_len - 1;
Bram Moolenaar06651632022-04-27 17:54:25 +01004710 trycmd->tcd_caught = TRUE;
4711 trycmd->tcd_did_throw = FALSE;
4712
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004713 did_emsg = got_int = did_throw = FALSE;
Bram Moolenaar1430cee2021-01-17 19:20:32 +01004714 force_abort = need_rethrow = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004715 catch_exception(current_exception);
4716 }
4717 break;
4718
Bram Moolenaarc150c092021-02-13 15:02:46 +01004719 case ISN_TRYCONT:
4720 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004721 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004722 trycont_T *trycont = &iptr->isn_arg.trycont;
4723 int i;
4724 trycmd_T *trycmd;
4725 int iidx = trycont->tct_where;
4726
4727 if (trystack->ga_len < trycont->tct_levels)
4728 {
4729 siemsg("TRYCONT: expected %d levels, found %d",
4730 trycont->tct_levels, trystack->ga_len);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004731 goto theend;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004732 }
4733 // Make :endtry jump to any outer try block and the last
4734 // :endtry inside the loop to the loop start.
4735 for (i = trycont->tct_levels; i > 0; --i)
4736 {
4737 trycmd = ((trycmd_T *)trystack->ga_data)
4738 + trystack->ga_len - i;
Bram Moolenaar2e34c342021-03-14 12:13:33 +01004739 // Add one to tcd_cont to be able to jump to
4740 // instruction with index zero.
4741 trycmd->tcd_cont = iidx + 1;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01004742 iidx = trycmd->tcd_finally_idx == 0
4743 ? trycmd->tcd_endtry_idx : trycmd->tcd_finally_idx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004744 }
4745 // jump to :finally or :endtry of current try statement
Bram Moolenaar4c137212021-04-19 16:48:48 +02004746 ectx->ec_iidx = iidx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004747 }
4748 break;
4749
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01004750 case ISN_FINALLY:
4751 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004752 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01004753 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
4754 + trystack->ga_len - 1;
4755
4756 // Reset the index to avoid a return statement jumps here
4757 // again.
4758 trycmd->tcd_finally_idx = 0;
4759 break;
4760 }
4761
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004762 // end of ":try" block
4763 case ISN_ENDTRY:
4764 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004765 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar06651632022-04-27 17:54:25 +01004766 trycmd_T *trycmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004767
Bram Moolenaar06651632022-04-27 17:54:25 +01004768 --trystack->ga_len;
4769 --trylevel;
4770 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len;
4771 if (trycmd->tcd_did_throw)
4772 did_throw = TRUE;
4773 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004774 {
Bram Moolenaar06651632022-04-27 17:54:25 +01004775 // discard the exception
4776 if (caught_stack == current_exception)
4777 caught_stack = caught_stack->caught;
4778 discard_current_exception();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004779 }
Bram Moolenaar06651632022-04-27 17:54:25 +01004780
4781 if (trycmd->tcd_return)
4782 goto func_return;
4783
4784 while (ectx->ec_stack.ga_len > trycmd->tcd_stack_len)
4785 {
4786 --ectx->ec_stack.ga_len;
4787 clear_tv(STACK_TV_BOT(0));
4788 }
4789 if (trycmd->tcd_cont != 0)
4790 // handling :continue: jump to outer try block or
4791 // start of the loop
4792 ectx->ec_iidx = trycmd->tcd_cont - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004793 }
4794 break;
4795
4796 case ISN_THROW:
Bram Moolenaar8f81b222021-01-14 21:47:06 +01004797 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004798 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02004799
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004800 if (trystack->ga_len == 0 && trylevel == 0 && emsg_silent)
4801 {
Bram Moolenaar3ef2e412023-04-30 18:50:48 +01004802 // Throwing an exception while using "silent!" causes
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004803 // the function to abort but not display an error.
4804 tv = STACK_TV_BOT(-1);
4805 clear_tv(tv);
4806 tv->v_type = VAR_NUMBER;
4807 tv->vval.v_number = 0;
4808 goto done;
4809 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004810 --ectx->ec_stack.ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004811 tv = STACK_TV_BOT(0);
4812 if (tv->vval.v_string == NULL
4813 || *skipwhite(tv->vval.v_string) == NUL)
4814 {
4815 vim_free(tv->vval.v_string);
4816 SOURCING_LNUM = iptr->isn_lnum;
4817 emsg(_(e_throw_with_empty_string));
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004818 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004819 }
4820
4821 // Inside a "catch" we need to first discard the caught
4822 // exception.
4823 if (trystack->ga_len > 0)
4824 {
4825 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
4826 + trystack->ga_len - 1;
4827 if (trycmd->tcd_caught && current_exception != NULL)
4828 {
4829 // discard the exception
4830 if (caught_stack == current_exception)
4831 caught_stack = caught_stack->caught;
4832 discard_current_exception();
4833 trycmd->tcd_caught = FALSE;
4834 }
4835 }
4836
Bram Moolenaar90a57162022-02-12 14:23:17 +00004837 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004838 if (throw_exception(tv->vval.v_string, ET_USER, NULL)
4839 == FAIL)
4840 {
4841 vim_free(tv->vval.v_string);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004842 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004843 }
4844 did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004845 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004846 break;
4847
4848 // compare with special values
4849 case ISN_COMPAREBOOL:
4850 case ISN_COMPARESPECIAL:
4851 {
4852 typval_T *tv1 = STACK_TV_BOT(-2);
4853 typval_T *tv2 = STACK_TV_BOT(-1);
4854 varnumber_T arg1 = tv1->vval.v_number;
4855 varnumber_T arg2 = tv2->vval.v_number;
4856 int res;
4857
Bram Moolenaar06651632022-04-27 17:54:25 +01004858 if (iptr->isn_arg.op.op_type == EXPR_EQUAL)
4859 res = arg1 == arg2;
4860 else
4861 res = arg1 != arg2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004862
Bram Moolenaar4c137212021-04-19 16:48:48 +02004863 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004864 tv1->v_type = VAR_BOOL;
4865 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4866 }
4867 break;
4868
Bram Moolenaar7a222242022-03-01 19:23:24 +00004869 case ISN_COMPARENULL:
4870 {
4871 typval_T *tv1 = STACK_TV_BOT(-2);
4872 typval_T *tv2 = STACK_TV_BOT(-1);
4873 int res;
4874
4875 res = typval_compare_null(tv1, tv2);
4876 if (res == MAYBE)
4877 goto on_error;
4878 if (iptr->isn_arg.op.op_type == EXPR_NEQUAL)
4879 res = !res;
4880 clear_tv(tv1);
4881 clear_tv(tv2);
4882 --ectx->ec_stack.ga_len;
4883 tv1->v_type = VAR_BOOL;
4884 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4885 }
4886 break;
4887
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004888 // Operation with two number arguments
4889 case ISN_OPNR:
4890 case ISN_COMPARENR:
4891 {
4892 typval_T *tv1 = STACK_TV_BOT(-2);
4893 typval_T *tv2 = STACK_TV_BOT(-1);
4894 varnumber_T arg1 = tv1->vval.v_number;
4895 varnumber_T arg2 = tv2->vval.v_number;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004896 varnumber_T res = 0;
4897 int div_zero = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004898
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004899 if (iptr->isn_arg.op.op_type == EXPR_LSHIFT
4900 || iptr->isn_arg.op.op_type == EXPR_RSHIFT)
4901 {
4902 if (arg2 < 0)
4903 {
4904 SOURCING_LNUM = iptr->isn_lnum;
dundargocc57b5bc2022-11-02 13:30:51 +00004905 emsg(_(e_bitshift_ops_must_be_positive));
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004906 goto on_error;
4907 }
4908 }
4909
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004910 switch (iptr->isn_arg.op.op_type)
4911 {
4912 case EXPR_MULT: res = arg1 * arg2; break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004913 case EXPR_DIV: if (arg2 == 0)
4914 div_zero = TRUE;
4915 else
4916 res = arg1 / arg2;
4917 break;
4918 case EXPR_REM: if (arg2 == 0)
4919 div_zero = TRUE;
4920 else
4921 res = arg1 % arg2;
4922 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004923 case EXPR_SUB: res = arg1 - arg2; break;
4924 case EXPR_ADD: res = arg1 + arg2; break;
4925
4926 case EXPR_EQUAL: res = arg1 == arg2; break;
4927 case EXPR_NEQUAL: res = arg1 != arg2; break;
4928 case EXPR_GREATER: res = arg1 > arg2; break;
4929 case EXPR_GEQUAL: res = arg1 >= arg2; break;
4930 case EXPR_SMALLER: res = arg1 < arg2; break;
4931 case EXPR_SEQUAL: res = arg1 <= arg2; break;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004932 case EXPR_LSHIFT: if (arg2 > MAX_LSHIFT_BITS)
4933 res = 0;
4934 else
Bram Moolenaar68e64d22022-05-22 22:07:52 +01004935 res = (uvarnumber_T)arg1 << arg2;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004936 break;
4937 case EXPR_RSHIFT: if (arg2 > MAX_LSHIFT_BITS)
4938 res = 0;
4939 else
Bram Moolenaar338bf582022-05-22 20:16:32 +01004940 res = (uvarnumber_T)arg1 >> arg2;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004941 break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004942 default: break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004943 }
4944
Bram Moolenaar4c137212021-04-19 16:48:48 +02004945 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004946 if (iptr->isn_type == ISN_COMPARENR)
4947 {
4948 tv1->v_type = VAR_BOOL;
4949 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4950 }
4951 else
4952 tv1->vval.v_number = res;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004953 if (div_zero)
4954 {
4955 SOURCING_LNUM = iptr->isn_lnum;
4956 emsg(_(e_divide_by_zero));
4957 goto on_error;
4958 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004959 }
4960 break;
4961
4962 // Computation with two float arguments
4963 case ISN_OPFLOAT:
4964 case ISN_COMPAREFLOAT:
4965 {
4966 typval_T *tv1 = STACK_TV_BOT(-2);
4967 typval_T *tv2 = STACK_TV_BOT(-1);
4968 float_T arg1 = tv1->vval.v_float;
4969 float_T arg2 = tv2->vval.v_float;
4970 float_T res = 0;
4971 int cmp = FALSE;
4972
4973 switch (iptr->isn_arg.op.op_type)
4974 {
4975 case EXPR_MULT: res = arg1 * arg2; break;
4976 case EXPR_DIV: res = arg1 / arg2; break;
4977 case EXPR_SUB: res = arg1 - arg2; break;
4978 case EXPR_ADD: res = arg1 + arg2; break;
4979
4980 case EXPR_EQUAL: cmp = arg1 == arg2; break;
4981 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
4982 case EXPR_GREATER: cmp = arg1 > arg2; break;
4983 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
4984 case EXPR_SMALLER: cmp = arg1 < arg2; break;
4985 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
4986 default: cmp = 0; break;
4987 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004988 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004989 if (iptr->isn_type == ISN_COMPAREFLOAT)
4990 {
4991 tv1->v_type = VAR_BOOL;
4992 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
4993 }
4994 else
4995 tv1->vval.v_float = res;
4996 }
4997 break;
4998
4999 case ISN_COMPARELIST:
Bram Moolenaar265f8112021-12-19 12:33:05 +00005000 case ISN_COMPAREDICT:
5001 case ISN_COMPAREFUNC:
5002 case ISN_COMPARESTRING:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005003 case ISN_COMPAREBLOB:
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00005004 case ISN_COMPARECLASS:
5005 case ISN_COMPAREOBJECT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005006 {
5007 typval_T *tv1 = STACK_TV_BOT(-2);
5008 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar265f8112021-12-19 12:33:05 +00005009 exprtype_T exprtype = iptr->isn_arg.op.op_type;
5010 int ic = iptr->isn_arg.op.op_ic;
5011 int res = FALSE;
5012 int status = OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005013
Bram Moolenaar265f8112021-12-19 12:33:05 +00005014 SOURCING_LNUM = iptr->isn_lnum;
5015 if (iptr->isn_type == ISN_COMPARELIST)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005016 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00005017 status = typval_compare_list(tv1, tv2,
5018 exprtype, ic, &res);
5019 }
5020 else if (iptr->isn_type == ISN_COMPAREDICT)
5021 {
5022 status = typval_compare_dict(tv1, tv2,
5023 exprtype, ic, &res);
5024 }
5025 else if (iptr->isn_type == ISN_COMPAREFUNC)
5026 {
5027 status = typval_compare_func(tv1, tv2,
5028 exprtype, ic, &res);
5029 }
5030 else if (iptr->isn_type == ISN_COMPARESTRING)
5031 {
5032 status = typval_compare_string(tv1, tv2,
5033 exprtype, ic, &res);
5034 }
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00005035 else if (iptr->isn_type == ISN_COMPAREBLOB)
Bram Moolenaar265f8112021-12-19 12:33:05 +00005036 {
5037 status = typval_compare_blob(tv1, tv2, exprtype, &res);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005038 }
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00005039 else if (iptr->isn_type == ISN_COMPARECLASS)
5040 {
Bram Moolenaar03ff0c62023-01-02 20:38:01 +00005041 status = typval_compare_class(tv1, tv2,
5042 exprtype, FALSE, &res);
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00005043 }
5044 else // ISN_COMPAREOBJECT
5045 {
5046 status = typval_compare_object(tv1, tv2,
Bram Moolenaar03ff0c62023-01-02 20:38:01 +00005047 exprtype, FALSE, &res);
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00005048 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005049 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005050 clear_tv(tv1);
5051 clear_tv(tv2);
5052 tv1->v_type = VAR_BOOL;
Bram Moolenaar265f8112021-12-19 12:33:05 +00005053 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
5054 if (status == FAIL)
5055 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005056 }
5057 break;
5058
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005059 case ISN_COMPAREANY:
5060 {
5061 typval_T *tv1 = STACK_TV_BOT(-2);
5062 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar657137c2021-01-09 15:45:23 +01005063 exprtype_T exprtype = iptr->isn_arg.op.op_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005064 int ic = iptr->isn_arg.op.op_ic;
Bram Moolenaar265f8112021-12-19 12:33:05 +00005065 int status;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005066
Bram Moolenaareb26f432020-09-14 16:50:05 +02005067 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar265f8112021-12-19 12:33:05 +00005068 status = typval_compare(tv1, tv2, exprtype, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005069 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005070 --ectx->ec_stack.ga_len;
Bram Moolenaar265f8112021-12-19 12:33:05 +00005071 if (status == FAIL)
5072 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005073 }
5074 break;
5075
5076 case ISN_ADDLIST:
5077 case ISN_ADDBLOB:
5078 {
5079 typval_T *tv1 = STACK_TV_BOT(-2);
5080 typval_T *tv2 = STACK_TV_BOT(-1);
5081
Bram Moolenaar1dcae592020-10-19 19:02:42 +02005082 // add two lists or blobs
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005083 if (iptr->isn_type == ISN_ADDLIST)
Bram Moolenaar07802042021-09-09 23:01:14 +02005084 {
5085 if (iptr->isn_arg.op.op_type == EXPR_APPEND
5086 && tv1->vval.v_list != NULL)
5087 list_extend(tv1->vval.v_list, tv2->vval.v_list,
5088 NULL);
5089 else
5090 eval_addlist(tv1, tv2);
5091 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005092 else
5093 eval_addblob(tv1, tv2);
5094 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005095 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005096 }
5097 break;
5098
Bram Moolenaar1dcae592020-10-19 19:02:42 +02005099 case ISN_LISTAPPEND:
5100 {
5101 typval_T *tv1 = STACK_TV_BOT(-2);
5102 typval_T *tv2 = STACK_TV_BOT(-1);
5103 list_T *l = tv1->vval.v_list;
5104
5105 // add an item to a list
Bram Moolenaar1f4a3452022-01-01 18:29:21 +00005106 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02005107 if (l == NULL)
5108 {
Bram Moolenaar1dcae592020-10-19 19:02:42 +02005109 emsg(_(e_cannot_add_to_null_list));
5110 goto on_error;
5111 }
Bram Moolenaar1f4a3452022-01-01 18:29:21 +00005112 if (value_check_lock(l->lv_lock, NULL, FALSE))
5113 goto on_error;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02005114 if (list_append_tv(l, tv2) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005115 goto theend;
Bram Moolenaar955347c2020-10-19 23:01:46 +02005116 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005117 --ectx->ec_stack.ga_len;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02005118 }
5119 break;
5120
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02005121 case ISN_BLOBAPPEND:
5122 {
5123 typval_T *tv1 = STACK_TV_BOT(-2);
5124 typval_T *tv2 = STACK_TV_BOT(-1);
5125 blob_T *b = tv1->vval.v_blob;
5126 int error = FALSE;
5127 varnumber_T n;
5128
5129 // add a number to a blob
5130 if (b == NULL)
5131 {
5132 SOURCING_LNUM = iptr->isn_lnum;
5133 emsg(_(e_cannot_add_to_null_blob));
5134 goto on_error;
5135 }
5136 n = tv_get_number_chk(tv2, &error);
5137 if (error)
5138 goto on_error;
5139 ga_append(&b->bv_ga, (int)n);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005140 --ectx->ec_stack.ga_len;
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02005141 }
5142 break;
5143
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005144 // Computation with two arguments of unknown type
5145 case ISN_OPANY:
5146 {
5147 typval_T *tv1 = STACK_TV_BOT(-2);
5148 typval_T *tv2 = STACK_TV_BOT(-1);
5149 varnumber_T n1, n2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005150 float_T f1 = 0, f2 = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005151 int error = FALSE;
5152
5153 if (iptr->isn_arg.op.op_type == EXPR_ADD)
5154 {
5155 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
5156 {
5157 eval_addlist(tv1, tv2);
5158 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005159 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005160 break;
5161 }
5162 else if (tv1->v_type == VAR_BLOB
5163 && tv2->v_type == VAR_BLOB)
5164 {
5165 eval_addblob(tv1, tv2);
5166 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005167 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005168 break;
5169 }
5170 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005171 if (tv1->v_type == VAR_FLOAT)
5172 {
5173 f1 = tv1->vval.v_float;
5174 n1 = 0;
5175 }
5176 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005177 {
Bram Moolenaarf665e972020-12-05 19:17:16 +01005178 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005179 n1 = tv_get_number_chk(tv1, &error);
5180 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02005181 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005182 if (tv2->v_type == VAR_FLOAT)
5183 f1 = n1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005184 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005185 if (tv2->v_type == VAR_FLOAT)
5186 {
5187 f2 = tv2->vval.v_float;
5188 n2 = 0;
5189 }
5190 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005191 {
5192 n2 = tv_get_number_chk(tv2, &error);
5193 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02005194 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005195 if (tv1->v_type == VAR_FLOAT)
5196 f2 = n2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005197 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005198 // if there is a float on either side the result is a float
5199 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
5200 {
5201 switch (iptr->isn_arg.op.op_type)
5202 {
5203 case EXPR_MULT: f1 = f1 * f2; break;
5204 case EXPR_DIV: f1 = f1 / f2; break;
5205 case EXPR_SUB: f1 = f1 - f2; break;
5206 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005207 default: SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005208 emsg(_(e_cannot_use_percent_with_float));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02005209 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005210 }
5211 clear_tv(tv1);
5212 clear_tv(tv2);
5213 tv1->v_type = VAR_FLOAT;
5214 tv1->vval.v_float = f1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005215 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005216 }
5217 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005218 {
Bram Moolenaarb1f28572021-01-21 13:03:20 +01005219 int failed = FALSE;
5220
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005221 switch (iptr->isn_arg.op.op_type)
5222 {
5223 case EXPR_MULT: n1 = n1 * n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01005224 case EXPR_DIV: n1 = num_divide(n1, n2, &failed);
5225 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01005226 goto on_error;
5227 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005228 case EXPR_SUB: n1 = n1 - n2; break;
5229 case EXPR_ADD: n1 = n1 + n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01005230 default: n1 = num_modulus(n1, n2, &failed);
5231 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01005232 goto on_error;
5233 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005234 }
5235 clear_tv(tv1);
5236 clear_tv(tv2);
5237 tv1->v_type = VAR_NUMBER;
5238 tv1->vval.v_number = n1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005239 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005240 }
5241 }
5242 break;
5243
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005244 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005245 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005246 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005247 int is_slice = iptr->isn_type == ISN_STRSLICE;
5248 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005249 char_u *res;
5250
5251 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005252 // string slice: string is at stack-3, first index at
5253 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005254 if (is_slice)
5255 {
5256 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005257 n1 = tv->vval.v_number;
5258 }
5259
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005260 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005261 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005262
Bram Moolenaar4c137212021-04-19 16:48:48 +02005263 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005264 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005265 if (is_slice)
5266 // Slice: Select the characters from the string
Bram Moolenaar6601b622021-01-13 21:47:15 +01005267 res = string_slice(tv->vval.v_string, n1, n2, FALSE);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005268 else
5269 // Index: The resulting variable is a string of a
Bram Moolenaar0289a092021-03-14 18:40:19 +01005270 // single character (including composing characters).
5271 // If the index is too big or negative the result is
5272 // empty.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005273 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005274 vim_free(tv->vval.v_string);
5275 tv->vval.v_string = res;
5276 }
5277 break;
5278
5279 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02005280 case ISN_LISTSLICE:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02005281 case ISN_BLOBINDEX:
5282 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005283 {
Bram Moolenaarcfc30232021-04-11 20:26:34 +02005284 int is_slice = iptr->isn_type == ISN_LISTSLICE
5285 || iptr->isn_type == ISN_BLOBSLICE;
5286 int is_blob = iptr->isn_type == ISN_BLOBINDEX
5287 || iptr->isn_type == ISN_BLOBSLICE;
Bram Moolenaared591872020-08-15 22:14:53 +02005288 varnumber_T n1, n2;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02005289 typval_T *val_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005290
5291 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02005292 // list slice: list is at stack-3, indexes at stack-2 and
5293 // stack-1
Bram Moolenaarcfc30232021-04-11 20:26:34 +02005294 // Same for blob.
5295 val_tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005296
5297 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02005298 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005299 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02005300
5301 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005302 {
Bram Moolenaared591872020-08-15 22:14:53 +02005303 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02005304 n1 = tv->vval.v_number;
5305 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005306 }
Bram Moolenaared591872020-08-15 22:14:53 +02005307
Bram Moolenaar4c137212021-04-19 16:48:48 +02005308 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02005309 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02005310 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02005311 if (is_blob)
5312 {
5313 if (blob_slice_or_index(val_tv->vval.v_blob, is_slice,
5314 n1, n2, FALSE, tv) == FAIL)
5315 goto on_error;
5316 }
5317 else
5318 {
5319 if (list_slice_or_index(val_tv->vval.v_list, is_slice,
5320 n1, n2, FALSE, tv, TRUE) == FAIL)
5321 goto on_error;
5322 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005323 }
5324 break;
5325
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005326 case ISN_ANYINDEX:
5327 case ISN_ANYSLICE:
5328 {
5329 int is_slice = iptr->isn_type == ISN_ANYSLICE;
5330 typval_T *var1, *var2;
5331 int res;
5332
5333 // index: composite is at stack-2, index at stack-1
5334 // slice: composite is at stack-3, indexes at stack-2 and
5335 // stack-1
5336 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02005337 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005338 if (check_can_index(tv, TRUE, TRUE) == FAIL)
5339 goto on_error;
5340 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
5341 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
Bram Moolenaar6601b622021-01-13 21:47:15 +01005342 res = eval_index_inner(tv, is_slice, var1, var2,
5343 FALSE, NULL, -1, TRUE);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005344 clear_tv(var1);
5345 if (is_slice)
5346 clear_tv(var2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005347 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005348 if (res == FAIL)
5349 goto on_error;
5350 }
5351 break;
5352
Bram Moolenaar9af78762020-06-16 11:34:42 +02005353 case ISN_SLICE:
5354 {
5355 list_T *list;
5356 int count = iptr->isn_arg.number;
5357
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02005358 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02005359 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02005360 list = tv->vval.v_list;
5361
5362 // no error for short list, expect it to be checked earlier
5363 if (list != NULL && list->lv_len >= count)
5364 {
5365 list_T *newlist = list_slice(list,
5366 count, list->lv_len - 1);
5367
5368 if (newlist != NULL)
5369 {
5370 list_unref(list);
5371 tv->vval.v_list = newlist;
5372 ++newlist->lv_refcount;
5373 }
5374 }
5375 }
5376 break;
5377
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005378 case ISN_GETITEM:
5379 {
5380 listitem_T *li;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02005381 getitem_T *gi = &iptr->isn_arg.getitem;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005382
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02005383 // Get list item: list is at stack-1, push item.
5384 // List type and length is checked for when compiling.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02005385 tv = STACK_TV_BOT(-1 - gi->gi_with_op);
5386 li = list_find(tv->vval.v_list, gi->gi_index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005387
Bram Moolenaar35578162021-08-02 19:10:38 +02005388 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005389 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005390 ++ectx->ec_stack.ga_len;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005391 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
Bram Moolenaarf785aa12021-02-11 21:19:34 +01005392
5393 // Useful when used in unpack assignment. Reset at
5394 // ISN_DROP.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02005395 ectx->ec_where.wt_index = gi->gi_index + 1;
LemonBoyc5d27442023-08-19 13:02:35 +02005396 ectx->ec_where.wt_kind = WT_VARIABLE;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005397 }
5398 break;
5399
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005400 case ISN_MEMBER:
5401 {
5402 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005403 char_u *key;
5404 dictitem_T *di;
5405
5406 // dict member: dict is at stack-2, key at stack-1
5407 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02005408 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005409 dict = tv->vval.v_dict;
5410
5411 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02005412 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005413 key = tv->vval.v_string;
Bram Moolenaar086fc9a2020-10-30 18:33:02 +01005414 if (key == NULL)
5415 key = (char_u *)"";
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02005416
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005417 if ((di = dict_find(dict, key, -1)) == NULL)
5418 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005419 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00005420 semsg(_(e_key_not_present_in_dictionary_str), key);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01005421
5422 // If :silent! is used we will continue, make sure the
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005423 // stack contents makes sense and the dict stack is
5424 // updated.
Bram Moolenaar4029cab2020-12-05 18:13:27 +01005425 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005426 --ectx->ec_stack.ga_len;
Bram Moolenaar4029cab2020-12-05 18:13:27 +01005427 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005428 (void) dict_stack_save(tv);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01005429 tv->v_type = VAR_NUMBER;
5430 tv->vval.v_number = 0;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01005431 goto on_fatal_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005432 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005433 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005434 --ectx->ec_stack.ga_len;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005435 // Put the dict used on the dict stack, it might be used by
5436 // a dict function later.
Bram Moolenaar50788ef2020-07-05 16:51:26 +02005437 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005438 if (dict_stack_save(tv) == FAIL)
5439 goto on_fatal_error;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02005440 copy_tv(&di->di_tv, tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005441 }
5442 break;
5443
5444 // dict member with string key
5445 case ISN_STRINGMEMBER:
5446 {
5447 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005448 dictitem_T *di;
5449
5450 tv = STACK_TV_BOT(-1);
5451 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
5452 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005453 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005454 emsg(_(e_dictionary_required));
Bram Moolenaard032f342020-07-18 18:13:02 +02005455 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005456 }
5457 dict = tv->vval.v_dict;
5458
5459 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
5460 == NULL)
5461 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005462 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00005463 semsg(_(e_key_not_present_in_dictionary_str),
Bram Moolenaar381692b2022-02-02 20:01:27 +00005464 iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02005465 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005466 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005467 // Put the dict used on the dict stack, it might be used by
5468 // a dict function later.
5469 if (dict_stack_save(tv) == FAIL)
5470 goto on_fatal_error;
5471
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005472 copy_tv(&di->di_tv, tv);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005473 }
5474 break;
5475
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005476 case ISN_GET_OBJ_MEMBER:
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00005477 case ISN_GET_ITF_MEMBER:
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005478 {
5479 tv = STACK_TV_BOT(-1);
5480 if (tv->v_type != VAR_OBJECT)
5481 {
5482 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaard0200c82023-01-28 15:19:40 +00005483 object_required_error(tv);
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005484 goto on_error;
5485 }
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00005486
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005487 object_T *obj = tv->vval.v_object;
Bram Moolenaarc3f971f2023-03-02 17:38:33 +00005488 if (obj == NULL)
5489 {
5490 SOURCING_LNUM = iptr->isn_lnum;
5491 emsg(_(e_using_null_object));
5492 goto on_error;
5493 }
5494
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00005495 int idx;
5496 if (iptr->isn_type == ISN_GET_OBJ_MEMBER)
Ernie Rael18143d32023-09-04 22:30:41 +02005497 idx = iptr->isn_arg.classmember.cm_idx;
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00005498 else
5499 {
5500 idx = iptr->isn_arg.classmember.cm_idx;
5501 // convert the interface index to the object index
5502 idx = object_index_from_itf_index(
Ernie Rael18143d32023-09-04 22:30:41 +02005503 iptr->isn_arg.classmember.cm_class,
Yegappan Lakshmanan5a05d372023-09-29 19:43:11 +02005504 FALSE, idx, obj->obj_class);
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00005505 }
5506
Ernie Rael18143d32023-09-04 22:30:41 +02005507 // The members are located right after the object struct.
Yegappan Lakshmanan5a05d372023-09-29 19:43:11 +02005508 typval_T *mtv = ((typval_T *)(obj + 1)) + idx;
Bram Moolenaar590162c2022-12-24 21:24:06 +00005509 copy_tv(mtv, tv);
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005510
5511 // Unreference the object after getting the member, it may
5512 // be freed.
5513 object_unref(obj);
5514 }
5515 break;
5516
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005517 case ISN_STORE_THIS:
5518 {
5519 int idx = iptr->isn_arg.number;
5520 object_T *obj = STACK_TV_VAR(0)->vval.v_object;
5521 // the members are located right after the object struct
5522 typval_T *mtv = ((typval_T *)(obj + 1)) + idx;
5523 clear_tv(mtv);
5524 *mtv = *STACK_TV_BOT(-1);
5525 --ectx->ec_stack.ga_len;
5526 }
5527 break;
5528
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005529 case ISN_CLEARDICT:
5530 dict_stack_drop();
5531 break;
5532
5533 case ISN_USEDICT:
5534 {
5535 typval_T *dict_tv = dict_stack_get_tv();
5536
5537 // Turn "dict.Func" into a partial for "Func" bound to
5538 // "dict". Don't do this when "Func" is already a partial
5539 // that was bound explicitly (pt_auto is FALSE).
5540 tv = STACK_TV_BOT(-1);
5541 if (dict_tv != NULL
5542 && dict_tv->v_type == VAR_DICT
5543 && dict_tv->vval.v_dict != NULL
5544 && (tv->v_type == VAR_FUNC
5545 || (tv->v_type == VAR_PARTIAL
5546 && (tv->vval.v_partial->pt_auto
5547 || tv->vval.v_partial->pt_dict == NULL))))
5548 dict_tv->vval.v_dict =
5549 make_partial(dict_tv->vval.v_dict, tv);
5550 dict_stack_drop();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005551 }
5552 break;
5553
5554 case ISN_NEGATENR:
5555 tv = STACK_TV_BOT(-1);
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00005556 // CHECKTYPE should have checked the variable type
Bram Moolenaarc58164c2020-03-29 18:40:30 +02005557 if (tv->v_type == VAR_FLOAT)
5558 tv->vval.v_float = -tv->vval.v_float;
5559 else
Bram Moolenaarc58164c2020-03-29 18:40:30 +02005560 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005561 break;
5562
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005563 case ISN_CHECKTYPE:
5564 {
5565 checktype_T *ct = &iptr->isn_arg.type;
Bram Moolenaarb1040dc2022-05-18 11:00:48 +01005566 int r;
LemonBoyc5d27442023-08-19 13:02:35 +02005567 where_T where = WHERE_INIT;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005568
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01005569 tv = STACK_TV_BOT((int)ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02005570 SOURCING_LNUM = iptr->isn_lnum;
LemonBoyc5d27442023-08-19 13:02:35 +02005571 if (ct->ct_arg_idx > 0)
5572 {
5573 where.wt_index = ct->ct_arg_idx;
5574 where.wt_kind = ct->ct_is_var ? WT_VARIABLE : WT_ARGUMENT;
LemonBoyc5d27442023-08-19 13:02:35 +02005575 }
LemonBoyf244b2f2023-08-19 16:02:04 +02005576 where.wt_func_name = ectx->ec_where.wt_func_name;
LemonBoyc5d27442023-08-19 13:02:35 +02005577 r = check_typval_type(ct->ct_type, tv, where);
Bram Moolenaarb1040dc2022-05-18 11:00:48 +01005578 if (r == FAIL)
5579 goto on_error;
Bram Moolenaar5e654232020-09-16 15:22:00 +02005580
5581 // number 0 is FALSE, number 1 is TRUE
5582 if (tv->v_type == VAR_NUMBER
5583 && ct->ct_type->tt_type == VAR_BOOL
5584 && (tv->vval.v_number == 0
5585 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005586 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02005587 tv->v_type = VAR_BOOL;
5588 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02005589 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005590 }
5591 }
5592 break;
5593
Bram Moolenaar9af78762020-06-16 11:34:42 +02005594 case ISN_CHECKLEN:
5595 {
5596 int min_len = iptr->isn_arg.checklen.cl_min_len;
5597 list_T *list = NULL;
5598
5599 tv = STACK_TV_BOT(-1);
5600 if (tv->v_type == VAR_LIST)
5601 list = tv->vval.v_list;
5602 if (list == NULL || list->lv_len < min_len
5603 || (list->lv_len > min_len
5604 && !iptr->isn_arg.checklen.cl_more_OK))
5605 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005606 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005607 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02005608 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02005609 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02005610 }
5611 }
5612 break;
5613
Bram Moolenaaraa210a32021-01-02 15:41:03 +01005614 case ISN_SETTYPE:
Bram Moolenaar381692b2022-02-02 20:01:27 +00005615 set_tv_type(STACK_TV_BOT(-1), iptr->isn_arg.type.ct_type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01005616 break;
5617
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005618 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005619 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005620 {
5621 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005622 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005623
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005624 if (iptr->isn_type == ISN_2BOOL)
5625 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005626 tv = STACK_TV_BOT(iptr->isn_arg.tobool.offset);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005627 n = tv2bool(tv);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005628 if (iptr->isn_arg.tobool.invert)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005629 n = !n;
5630 }
5631 else
5632 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005633 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005634 SOURCING_LNUM = iptr->isn_lnum;
5635 n = tv_get_bool_chk(tv, &error);
5636 if (error)
5637 goto on_error;
5638 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005639 clear_tv(tv);
5640 tv->v_type = VAR_BOOL;
5641 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
5642 }
5643 break;
5644
5645 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02005646 case ISN_2STRING_ANY:
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01005647 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005648 if (do_2string(STACK_TV_BOT(iptr->isn_arg.tostring.offset),
5649 iptr->isn_type == ISN_2STRING_ANY,
5650 iptr->isn_arg.tostring.tolerant) == FAIL)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01005651 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005652 break;
5653
Bram Moolenaar08597872020-12-10 19:43:40 +01005654 case ISN_RANGE:
5655 {
5656 exarg_T ea;
5657 char *errormsg;
5658
Bram Moolenaarece0b872021-01-08 20:40:45 +01005659 ea.line2 = 0;
Bram Moolenaard1510ee2021-01-04 16:15:58 +01005660 ea.addr_count = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01005661 ea.addr_type = ADDR_LINES;
5662 ea.cmd = iptr->isn_arg.string;
Bram Moolenaarece0b872021-01-08 20:40:45 +01005663 ea.skip = FALSE;
Bram Moolenaar08597872020-12-10 19:43:40 +01005664 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaarece0b872021-01-08 20:40:45 +01005665 goto on_error;
Bram Moolenaara28639e2021-01-19 22:48:09 +01005666
Bram Moolenaar35578162021-08-02 19:10:38 +02005667 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005668 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005669 ++ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01005670 tv = STACK_TV_BOT(-1);
5671 tv->v_type = VAR_NUMBER;
5672 tv->v_lock = 0;
Bram Moolenaar06651632022-04-27 17:54:25 +01005673 tv->vval.v_number = ea.line2;
Bram Moolenaar08597872020-12-10 19:43:40 +01005674 }
5675 break;
5676
Bram Moolenaarc3516f72020-09-08 22:45:35 +02005677 case ISN_PUT:
5678 {
5679 int regname = iptr->isn_arg.put.put_regname;
5680 linenr_T lnum = iptr->isn_arg.put.put_lnum;
5681 char_u *expr = NULL;
5682 int dir = FORWARD;
5683
Bram Moolenaar08597872020-12-10 19:43:40 +01005684 if (lnum < -2)
5685 {
5686 // line number was put on the stack by ISN_RANGE
5687 tv = STACK_TV_BOT(-1);
5688 curwin->w_cursor.lnum = tv->vval.v_number;
5689 if (lnum == LNUM_VARIABLE_RANGE_ABOVE)
5690 dir = BACKWARD;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005691 --ectx->ec_stack.ga_len;
Bram Moolenaar08597872020-12-10 19:43:40 +01005692 }
5693 else if (lnum == -2)
Bram Moolenaarc3516f72020-09-08 22:45:35 +02005694 // :put! above cursor
5695 dir = BACKWARD;
5696 else if (lnum >= 0)
Bram Moolenaar4e713ba2022-02-07 15:31:37 +00005697 {
5698 curwin->w_cursor.lnum = lnum;
5699 if (lnum == 0)
5700 // check_cursor() below will move to line 1
5701 dir = BACKWARD;
5702 }
Bram Moolenaara28639e2021-01-19 22:48:09 +01005703
5704 if (regname == '=')
5705 {
5706 tv = STACK_TV_BOT(-1);
5707 if (tv->v_type == VAR_STRING)
5708 expr = tv->vval.v_string;
5709 else
5710 {
5711 expr = typval2string(tv, TRUE); // allocates value
5712 clear_tv(tv);
5713 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005714 --ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01005715 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02005716 check_cursor();
5717 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
5718 vim_free(expr);
5719 }
5720 break;
5721
Bram Moolenaar02194d22020-10-24 23:08:38 +02005722 case ISN_CMDMOD:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005723 ectx->ec_funclocal.floc_save_cmdmod = cmdmod;
5724 ectx->ec_funclocal.floc_restore_cmdmod = TRUE;
5725 ectx->ec_funclocal.floc_restore_cmdmod_stacklen =
5726 ectx->ec_stack.ga_len;
Bram Moolenaar02194d22020-10-24 23:08:38 +02005727 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
5728 apply_cmdmod(&cmdmod);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02005729 break;
5730
Bram Moolenaar02194d22020-10-24 23:08:38 +02005731 case ISN_CMDMOD_REV:
5732 // filter regprog is owned by the instruction, don't free it
5733 cmdmod.cmod_filter_regmatch.regprog = NULL;
5734 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005735 cmdmod = ectx->ec_funclocal.floc_save_cmdmod;
5736 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02005737 break;
5738
Bram Moolenaar792f7862020-11-23 08:31:18 +01005739 case ISN_UNPACK:
5740 {
5741 int count = iptr->isn_arg.unpack.unp_count;
5742 int semicolon = iptr->isn_arg.unpack.unp_semicolon;
5743 list_T *l;
5744 listitem_T *li;
5745 int i;
5746
5747 // Check there is a valid list to unpack.
5748 tv = STACK_TV_BOT(-1);
5749 if (tv->v_type != VAR_LIST)
5750 {
5751 SOURCING_LNUM = iptr->isn_lnum;
5752 emsg(_(e_for_argument_must_be_sequence_of_lists));
5753 goto on_error;
5754 }
5755 l = tv->vval.v_list;
5756 if (l == NULL
5757 || l->lv_len < (semicolon ? count - 1 : count))
5758 {
5759 SOURCING_LNUM = iptr->isn_lnum;
5760 emsg(_(e_list_value_does_not_have_enough_items));
5761 goto on_error;
5762 }
5763 else if (!semicolon && l->lv_len > count)
5764 {
5765 SOURCING_LNUM = iptr->isn_lnum;
5766 emsg(_(e_list_value_has_more_items_than_targets));
5767 goto on_error;
5768 }
5769
5770 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar35578162021-08-02 19:10:38 +02005771 if (GA_GROW_FAILS(&ectx->ec_stack, count - 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005772 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005773 ectx->ec_stack.ga_len += count - 1;
Bram Moolenaar792f7862020-11-23 08:31:18 +01005774
5775 // Variable after semicolon gets a list with the remaining
5776 // items.
5777 if (semicolon)
5778 {
5779 list_T *rem_list =
5780 list_alloc_with_items(l->lv_len - count + 1);
5781
5782 if (rem_list == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005783 goto theend;
Bram Moolenaar792f7862020-11-23 08:31:18 +01005784 tv = STACK_TV_BOT(-count);
5785 tv->vval.v_list = rem_list;
5786 ++rem_list->lv_refcount;
5787 tv->v_lock = 0;
5788 li = l->lv_first;
5789 for (i = 0; i < count - 1; ++i)
5790 li = li->li_next;
5791 for (i = 0; li != NULL; ++i)
5792 {
Bram Moolenaar61efa162022-03-18 13:10:48 +00005793 typval_T tvcopy;
5794
5795 copy_tv(&li->li_tv, &tvcopy);
5796 list_set_item(rem_list, i, &tvcopy);
Bram Moolenaar792f7862020-11-23 08:31:18 +01005797 li = li->li_next;
5798 }
5799 --count;
5800 }
5801
5802 // Produce the values in reverse order, first item last.
5803 li = l->lv_first;
5804 for (i = 0; i < count; ++i)
5805 {
5806 tv = STACK_TV_BOT(-i - 1);
5807 copy_tv(&li->li_tv, tv);
5808 li = li->li_next;
5809 }
5810
5811 list_unref(l);
5812 }
5813 break;
5814
Bram Moolenaarb2049902021-01-24 12:53:53 +01005815 case ISN_PROF_START:
5816 case ISN_PROF_END:
5817 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01005818#ifdef FEAT_PROFILE
Bram Moolenaarca16c602022-09-06 18:57:08 +01005819 funccall_T cookie;
5820 ufunc_T *cur_ufunc =
Bram Moolenaarb2049902021-01-24 12:53:53 +01005821 (((dfunc_T *)def_functions.ga_data)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02005822 + ectx->ec_dfunc_idx)->df_ufunc;
Bram Moolenaarb2049902021-01-24 12:53:53 +01005823
Bram Moolenaarca16c602022-09-06 18:57:08 +01005824 cookie.fc_func = cur_ufunc;
Bram Moolenaarb2049902021-01-24 12:53:53 +01005825 if (iptr->isn_type == ISN_PROF_START)
5826 {
5827 func_line_start(&cookie, iptr->isn_lnum);
5828 // if we get here the instruction is executed
5829 func_line_exec(&cookie);
5830 }
5831 else
5832 func_line_end(&cookie);
Bram Moolenaarf002a412021-01-24 13:34:18 +01005833#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01005834 }
5835 break;
5836
Bram Moolenaare99d4222021-06-13 14:01:26 +02005837 case ISN_DEBUG:
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02005838 handle_debug(iptr, ectx);
Bram Moolenaare99d4222021-06-13 14:01:26 +02005839 break;
5840
Bram Moolenaar389df252020-07-09 21:20:47 +02005841 case ISN_SHUFFLE:
5842 {
Bram Moolenaar792f7862020-11-23 08:31:18 +01005843 typval_T tmp_tv;
5844 int item = iptr->isn_arg.shuffle.shfl_item;
5845 int up = iptr->isn_arg.shuffle.shfl_up;
Bram Moolenaar389df252020-07-09 21:20:47 +02005846
5847 tmp_tv = *STACK_TV_BOT(-item);
5848 for ( ; up > 0 && item > 1; --up)
5849 {
5850 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
5851 --item;
5852 }
5853 *STACK_TV_BOT(-item) = tmp_tv;
5854 }
5855 break;
5856
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005857 case ISN_DROP:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005858 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005859 clear_tv(STACK_TV_BOT(0));
LemonBoyc5d27442023-08-19 13:02:35 +02005860 ectx->ec_where = (where_T)WHERE_INIT;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005861 break;
5862 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02005863 continue;
5864
Bram Moolenaard032f342020-07-18 18:13:02 +02005865func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02005866 // Restore previous function. If the frame pointer is where we started
5867 // then there is none and we are done.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005868 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02005869 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02005870
Bram Moolenaar4c137212021-04-19 16:48:48 +02005871 if (func_return(ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02005872 // only fails when out of memory
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005873 goto theend;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02005874 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02005875
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02005876on_error:
Bram Moolenaaraf0df472020-12-02 20:51:22 +01005877 // Jump here for an error that does not require aborting execution.
Bram Moolenaar56602ba2020-12-05 21:22:08 +01005878 // If "emsg_silent" is set then ignore the error, unless it was set
5879 // when calling the function.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005880 if (did_emsg_cumul + did_emsg == ectx->ec_did_emsg_before
Bram Moolenaar56602ba2020-12-05 21:22:08 +01005881 && emsg_silent && did_emsg_def == 0)
Bram Moolenaarf9041332021-01-21 19:41:16 +01005882 {
5883 // If a sequence of instructions causes an error while ":silent!"
5884 // was used, restore the stack length and jump ahead to restoring
5885 // the cmdmod.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005886 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaarf9041332021-01-21 19:41:16 +01005887 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005888 while (ectx->ec_stack.ga_len
5889 > ectx->ec_funclocal.floc_restore_cmdmod_stacklen)
Bram Moolenaarf9041332021-01-21 19:41:16 +01005890 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005891 --ectx->ec_stack.ga_len;
Bram Moolenaarf9041332021-01-21 19:41:16 +01005892 clear_tv(STACK_TV_BOT(0));
5893 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005894 while (ectx->ec_instr[ectx->ec_iidx].isn_type != ISN_CMDMOD_REV)
5895 ++ectx->ec_iidx;
Bram Moolenaarf9041332021-01-21 19:41:16 +01005896 }
Bram Moolenaarcd030c42020-10-30 21:49:40 +01005897 continue;
Bram Moolenaarf9041332021-01-21 19:41:16 +01005898 }
Bram Moolenaaraf0df472020-12-02 20:51:22 +01005899on_fatal_error:
5900 // Jump here for an error that messes up the stack.
Bram Moolenaar171fb922020-10-28 16:54:47 +01005901 // If we are not inside a try-catch started here, abort execution.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005902 if (trylevel <= ectx->ec_trylevel_at_start)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005903 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005904 }
5905
5906done:
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005907 ret = OK;
5908theend:
Bram Moolenaar58779852022-09-06 18:31:14 +01005909 may_invoke_defer_funcs(ectx);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01005910
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005911 dict_stack_clear(dict_stack_len_at_start);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005912 ectx->ec_trylevel_at_start = save_trylevel_at_start;
5913 return ret;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005914}
5915
5916/*
Bram Moolenaar806a2732022-09-04 15:40:36 +01005917 * Execute the instructions from a VAR_INSTR typval and put the result in
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005918 * "rettv".
5919 * Return OK or FAIL.
5920 */
5921 int
5922exe_typval_instr(typval_T *tv, typval_T *rettv)
5923{
5924 ectx_T *ectx = tv->vval.v_instr->instr_ectx;
5925 isn_T *save_instr = ectx->ec_instr;
5926 int save_iidx = ectx->ec_iidx;
5927 int res;
5928
LemonBoyf3b48952022-05-05 13:53:03 +01005929 // Initialize rettv so that it is safe for caller to invoke clear_tv(rettv)
5930 // even when the compilation fails.
5931 rettv->v_type = VAR_UNKNOWN;
5932
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005933 ectx->ec_instr = tv->vval.v_instr->instr_instr;
5934 res = exec_instructions(ectx);
5935 if (res == OK)
5936 {
5937 *rettv = *STACK_TV_BOT(-1);
5938 --ectx->ec_stack.ga_len;
5939 }
5940
5941 ectx->ec_instr = save_instr;
5942 ectx->ec_iidx = save_iidx;
5943
5944 return res;
5945}
5946
5947/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02005948 * Execute the instructions from an ISN_SUBSTITUTE command, which are in
5949 * "substitute_instr".
5950 */
5951 char_u *
5952exe_substitute_instr(void)
5953{
5954 ectx_T *ectx = substitute_instr->subs_ectx;
5955 isn_T *save_instr = ectx->ec_instr;
5956 int save_iidx = ectx->ec_iidx;
5957 char_u *res;
5958
5959 ectx->ec_instr = substitute_instr->subs_instr;
5960 if (exec_instructions(ectx) == OK)
Bram Moolenaar90193e62021-04-04 20:49:50 +02005961 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005962 typval_T *tv = STACK_TV_BOT(-1);
5963
Bram Moolenaar27523602021-06-05 21:36:19 +02005964 res = typval2string(tv, TRUE);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005965 --ectx->ec_stack.ga_len;
5966 clear_tv(tv);
Bram Moolenaar90193e62021-04-04 20:49:50 +02005967 }
5968 else
5969 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005970 substitute_instr->subs_status = FAIL;
5971 res = vim_strsave((char_u *)"");
Bram Moolenaar90193e62021-04-04 20:49:50 +02005972 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005973
Bram Moolenaar4c137212021-04-19 16:48:48 +02005974 ectx->ec_instr = save_instr;
5975 ectx->ec_iidx = save_iidx;
5976
5977 return res;
5978}
5979
5980/*
5981 * Call a "def" function from old Vim script.
5982 * Return OK or FAIL.
5983 */
5984 int
5985call_def_function(
5986 ufunc_T *ufunc,
5987 int argc_arg, // nr of arguments
5988 typval_T *argv, // arguments
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005989 int flags, // DEF_ flags
Bram Moolenaar4c137212021-04-19 16:48:48 +02005990 partial_T *partial, // optional partial for context
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005991 object_T *object, // object, e.g. for this.Func()
Bram Moolenaar58779852022-09-06 18:31:14 +01005992 funccall_T *funccal,
Bram Moolenaar4c137212021-04-19 16:48:48 +02005993 typval_T *rettv) // return value
5994{
5995 ectx_T ectx; // execution context
5996 int argc = argc_arg;
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005997 int partial_argc = partial == NULL
5998 || (flags & DEF_USE_PT_ARGV) == 0
5999 ? 0 : partial->pt_argc;
6000 int total_argc = argc + partial_argc;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006001 typval_T *tv;
6002 int idx;
6003 int ret = FAIL;
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01006004 int defcount = ufunc->uf_args.ga_len - total_argc;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006005 sctx_T save_current_sctx = current_sctx;
6006 int did_emsg_before = did_emsg_cumul + did_emsg;
6007 int save_suppress_errthrow = suppress_errthrow;
6008 msglist_T **saved_msg_list = NULL;
6009 msglist_T *private_msg_list = NULL;
6010 int save_emsg_silent_def = emsg_silent_def;
6011 int save_did_emsg_def = did_emsg_def;
6012 int orig_funcdepth;
Bram Moolenaare99d4222021-06-13 14:01:26 +02006013 int orig_nesting_level = ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006014
6015// Get pointer to item in the stack.
6016#undef STACK_TV
6017#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
6018
6019// Get pointer to item at the bottom of the stack, -1 is the bottom.
6020#undef STACK_TV_BOT
6021#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
6022
6023// Get pointer to a local variable on the stack. Negative for arguments.
6024#undef STACK_TV_VAR
6025#define STACK_TV_VAR(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_frame_idx + STACK_FRAME_SIZE + idx)
6026
6027 if (ufunc->uf_def_status == UF_NOT_COMPILED
6028 || ufunc->uf_def_status == UF_COMPILE_ERROR
Bram Moolenaar139575d2022-03-15 19:29:30 +00006029 || (func_needs_compiling(ufunc, get_compile_type(ufunc))
6030 && compile_def_function(ufunc, FALSE,
6031 get_compile_type(ufunc), NULL) == FAIL))
Bram Moolenaar4c137212021-04-19 16:48:48 +02006032 {
6033 if (did_emsg_cumul + did_emsg == did_emsg_before)
6034 semsg(_(e_function_is_not_compiled_str),
6035 printable_func_name(ufunc));
6036 return FAIL;
6037 }
6038
6039 {
6040 // Check the function was really compiled.
6041 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6042 + ufunc->uf_dfunc_idx;
Bram Moolenaarcc341812022-09-19 15:54:34 +01006043 if (dfunc->df_ufunc == NULL)
6044 {
6045 semsg(_(e_function_was_deleted_str), printable_func_name(ufunc));
6046 return FAIL;
6047 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006048 if (INSTRUCTIONS(dfunc) == NULL)
6049 {
6050 iemsg("using call_def_function() on not compiled function");
6051 return FAIL;
6052 }
6053 }
6054
6055 // If depth of calling is getting too high, don't execute the function.
6056 orig_funcdepth = funcdepth_get();
6057 if (funcdepth_increment() == FAIL)
6058 return FAIL;
6059
6060 CLEAR_FIELD(ectx);
6061 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
6062 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
Bram Moolenaar35578162021-08-02 19:10:38 +02006063 if (GA_GROW_FAILS(&ectx.ec_stack, 20))
Bram Moolenaar4c137212021-04-19 16:48:48 +02006064 {
6065 funcdepth_decrement();
6066 return FAIL;
6067 }
6068 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
6069 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
6070 ectx.ec_did_emsg_before = did_emsg_before;
Bram Moolenaare99d4222021-06-13 14:01:26 +02006071 ++ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006072
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01006073 idx = total_argc - ufunc->uf_args.ga_len;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006074 if (idx > 0 && ufunc->uf_va_name == NULL)
6075 {
Matvey Tarasovd14bb1a2022-06-29 13:18:27 +01006076 semsg(NGETTEXT(e_one_argument_too_many, e_nr_arguments_too_many,
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01006077 idx), idx);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006078 goto failed_early;
6079 }
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01006080 idx = total_argc - ufunc->uf_args.ga_len + ufunc->uf_def_args.ga_len;
Bram Moolenaarc6d71532021-06-05 18:49:38 +02006081 if (idx < 0)
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02006082 {
Matvey Tarasovd14bb1a2022-06-29 13:18:27 +01006083 semsg(NGETTEXT(e_one_argument_too_few, e_nr_arguments_too_few,
Bram Moolenaar98aff652022-09-06 21:02:35 +01006084 -idx), -idx);
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02006085 goto failed_early;
6086 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006087
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01006088 // Put values from the partial and arguments on the stack, but no more than
6089 // what the function expects. A lambda can be called with more arguments
6090 // than it uses.
6091 for (idx = 0; idx < total_argc
Bram Moolenaar4c137212021-04-19 16:48:48 +02006092 && (ufunc->uf_va_name != NULL || idx < ufunc->uf_args.ga_len);
6093 ++idx)
6094 {
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01006095 int argv_idx = idx - partial_argc;
6096
6097 tv = idx < partial_argc ? partial->pt_argv + idx : argv + argv_idx;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006098 if (idx >= ufunc->uf_args.ga_len - ufunc->uf_def_args.ga_len
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01006099 && tv->v_type == VAR_SPECIAL
6100 && tv->vval.v_number == VVAL_NONE)
Bram Moolenaar4c137212021-04-19 16:48:48 +02006101 {
6102 // Use the default value.
6103 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
6104 }
6105 else
6106 {
Bram Moolenaar47bba532023-01-20 18:49:46 +00006107 int done = FALSE;
6108 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len)
6109 {
6110 type_T *expected = ufunc->uf_arg_types[idx];
6111 if (expected->tt_type == VAR_FLOAT && tv->v_type == VAR_NUMBER)
6112 {
6113 // When a float is expected and a number was given, convert
6114 // the value.
6115 STACK_TV_BOT(0)->v_type = VAR_FLOAT;
6116 STACK_TV_BOT(0)->v_lock = 0;
6117 STACK_TV_BOT(0)->vval.v_float = tv->vval.v_number;
6118 done = TRUE;
6119 }
6120 else if (check_typval_arg_type(expected, tv,
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01006121 NULL, argv_idx + 1) == FAIL)
Bram Moolenaar47bba532023-01-20 18:49:46 +00006122 goto failed_early;
6123 }
6124 if (!done)
6125 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006126 }
6127 ++ectx.ec_stack.ga_len;
6128 }
6129
6130 // Turn varargs into a list. Empty list if no args.
6131 if (ufunc->uf_va_name != NULL)
6132 {
6133 int vararg_count = argc - ufunc->uf_args.ga_len;
6134
6135 if (vararg_count < 0)
6136 vararg_count = 0;
6137 else
6138 argc -= vararg_count;
6139 if (exe_newlist(vararg_count, &ectx) == FAIL)
6140 goto failed_early;
6141
6142 // Check the type of the list items.
6143 tv = STACK_TV_BOT(-1);
6144 if (ufunc->uf_va_type != NULL
6145 && ufunc->uf_va_type != &t_list_any
6146 && ufunc->uf_va_type->tt_member != &t_any
6147 && tv->vval.v_list != NULL)
6148 {
6149 type_T *expected = ufunc->uf_va_type->tt_member;
6150 listitem_T *li = tv->vval.v_list->lv_first;
6151
6152 for (idx = 0; idx < vararg_count; ++idx)
6153 {
6154 if (check_typval_arg_type(expected, &li->li_tv,
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02006155 NULL, argc + idx + 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02006156 goto failed_early;
6157 li = li->li_next;
6158 }
6159 }
6160
6161 if (defcount > 0)
6162 // Move varargs list to below missing default arguments.
6163 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
6164 --ectx.ec_stack.ga_len;
6165 }
6166
6167 // Make space for omitted arguments, will store default value below.
6168 // Any varargs list goes after them.
6169 if (defcount > 0)
6170 for (idx = 0; idx < defcount; ++idx)
6171 {
6172 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
6173 ++ectx.ec_stack.ga_len;
6174 }
6175 if (ufunc->uf_va_name != NULL)
6176 ++ectx.ec_stack.ga_len;
6177
6178 // Frame pointer points to just after arguments.
6179 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
6180 ectx.ec_initial_frame_idx = ectx.ec_frame_idx;
6181
6182 {
6183 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6184 + ufunc->uf_dfunc_idx;
6185 ufunc_T *base_ufunc = dfunc->df_ufunc;
6186
6187 // "uf_partial" is on the ufunc that "df_ufunc" points to, as is done
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006188 // by copy_lambda_to_global_func().
Bram Moolenaar4c137212021-04-19 16:48:48 +02006189 if (partial != NULL || base_ufunc->uf_partial != NULL)
6190 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006191 ectx.ec_outer_ref = ALLOC_CLEAR_ONE(outer_ref_T);
6192 if (ectx.ec_outer_ref == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02006193 goto failed_early;
6194 if (partial != NULL)
6195 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00006196 outer_T *outer = get_pt_outer(partial);
6197
Bram Moolenaar6d313be2022-09-22 16:36:25 +01006198 if (outer->out_stack == NULL && outer->out_loop_size == 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02006199 {
Bram Moolenaar6d313be2022-09-22 16:36:25 +01006200 // no stack was set
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00006201 if (current_ectx != NULL)
6202 {
6203 if (current_ectx->ec_outer_ref != NULL
6204 && current_ectx->ec_outer_ref->or_outer != NULL)
6205 ectx.ec_outer_ref->or_outer =
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006206 current_ectx->ec_outer_ref->or_outer;
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00006207 }
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01006208 // else: should there be an error here?
Bram Moolenaar4c137212021-04-19 16:48:48 +02006209 }
6210 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006211 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00006212 ectx.ec_outer_ref->or_outer = outer;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006213 ++partial->pt_refcount;
6214 ectx.ec_outer_ref->or_partial = partial;
6215 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006216 }
6217 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006218 {
6219 ectx.ec_outer_ref->or_outer = &base_ufunc->uf_partial->pt_outer;
6220 ++base_ufunc->uf_partial->pt_refcount;
6221 ectx.ec_outer_ref->or_partial = base_ufunc->uf_partial;
6222 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006223 }
6224 }
6225
6226 // dummy frame entries
6227 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
6228 {
6229 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
6230 ++ectx.ec_stack.ga_len;
6231 }
6232
6233 {
6234 // Reserve space for local variables and any closure reference count.
6235 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6236 + ufunc->uf_dfunc_idx;
6237
Bram Moolenaar5cd64792021-12-25 18:23:24 +00006238 // Initialize variables to zero. That avoids having to generate
6239 // initializing instructions for "var nr: number", "var x: any", etc.
Bram Moolenaar4c137212021-04-19 16:48:48 +02006240 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaar5cd64792021-12-25 18:23:24 +00006241 {
6242 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
6243 STACK_TV_VAR(idx)->vval.v_number = 0;
6244 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006245 ectx.ec_stack.ga_len += dfunc->df_varcount;
Bram Moolenaarffdaca92022-12-09 21:41:48 +00006246
6247 if (object != NULL)
6248 {
6249 // the object is always the variable at index zero
6250 tv = STACK_TV_VAR(0);
6251 tv->v_type = VAR_OBJECT;
6252 tv->vval.v_object = object;
6253 }
6254
Bram Moolenaar4c137212021-04-19 16:48:48 +02006255 if (dfunc->df_has_closure)
6256 {
Bram Moolenaarcc341812022-09-19 15:54:34 +01006257 // Initialize the variable that counts how many closures were
6258 // created. This is used in handle_closure_in_use().
Bram Moolenaar4c137212021-04-19 16:48:48 +02006259 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
6260 STACK_TV_VAR(idx)->vval.v_number = 0;
6261 ++ectx.ec_stack.ga_len;
6262 }
6263
6264 ectx.ec_instr = INSTRUCTIONS(dfunc);
6265 }
6266
Bram Moolenaar58779852022-09-06 18:31:14 +01006267 // Store the execution context in funccal, used by invoke_all_defer().
6268 if (funccal != NULL)
6269 funccal->fc_ectx = &ectx;
6270
Bram Moolenaar4c137212021-04-19 16:48:48 +02006271 // Following errors are in the function, not the caller.
6272 // Commands behave like vim9script.
6273 estack_push_ufunc(ufunc, 1);
6274 current_sctx = ufunc->uf_script_ctx;
6275 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
6276
6277 // Use a specific location for storing error messages to be converted to an
6278 // exception.
6279 saved_msg_list = msg_list;
6280 msg_list = &private_msg_list;
6281
6282 // Do turn errors into exceptions.
6283 suppress_errthrow = FALSE;
6284
6285 // Do not delete the function while executing it.
6286 ++ufunc->uf_calls;
6287
6288 // When ":silent!" was used before calling then we still abort the
6289 // function. If ":silent!" is used in the function then we don't.
6290 emsg_silent_def = emsg_silent;
6291 did_emsg_def = 0;
6292
LemonBoyc5d27442023-08-19 13:02:35 +02006293 ectx.ec_where = (where_T)WHERE_INIT;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006294
Bram Moolenaar5800c792022-09-22 17:34:01 +01006295 /*
6296 * Execute the instructions until done.
6297 */
Bram Moolenaar4c137212021-04-19 16:48:48 +02006298 ret = exec_instructions(&ectx);
6299 if (ret == OK)
6300 {
6301 // function finished, get result from the stack.
6302 if (ufunc->uf_ret_type == &t_void)
6303 {
6304 rettv->v_type = VAR_VOID;
6305 }
6306 else
6307 {
6308 tv = STACK_TV_BOT(-1);
6309 *rettv = *tv;
6310 tv->v_type = VAR_UNKNOWN;
6311 }
6312 }
6313
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01006314 // When failed need to unwind the call stack.
Bram Moolenaar58779852022-09-06 18:31:14 +01006315 unwind_def_callstack(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02006316
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02006317 // Deal with any remaining closures, they may be in use somewhere.
6318 if (ectx.ec_funcrefs.ga_len > 0)
Bram Moolenaarf112f302020-12-20 17:47:52 +01006319 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02006320 handle_closure_in_use(&ectx, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00006321 ga_clear(&ectx.ec_funcrefs);
Bram Moolenaarf112f302020-12-20 17:47:52 +01006322 }
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02006323
Bram Moolenaaree8580e2020-08-28 17:19:07 +02006324 estack_pop();
6325 current_sctx = save_current_sctx;
6326
Bram Moolenaar2336c372021-12-06 15:06:54 +00006327 if (--ufunc->uf_calls <= 0 && ufunc->uf_refcount <= 0)
6328 // Function was unreferenced while being used, free it now.
6329 func_clear_free(ufunc, FALSE);
Bram Moolenaarc970e422021-03-17 15:03:04 +01006330
Bram Moolenaar352134b2020-10-17 22:04:08 +02006331 if (*msg_list != NULL && saved_msg_list != NULL)
6332 {
6333 msglist_T **plist = saved_msg_list;
6334
6335 // Append entries from the current msg_list (uncaught exceptions) to
6336 // the saved msg_list.
6337 while (*plist != NULL)
6338 plist = &(*plist)->next;
6339
6340 *plist = *msg_list;
6341 }
6342 msg_list = saved_msg_list;
6343
Bram Moolenaar4c137212021-04-19 16:48:48 +02006344 if (ectx.ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar02194d22020-10-24 23:08:38 +02006345 {
6346 cmdmod.cmod_filter_regmatch.regprog = NULL;
6347 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006348 cmdmod = ectx.ec_funclocal.floc_save_cmdmod;
Bram Moolenaar02194d22020-10-24 23:08:38 +02006349 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +01006350 emsg_silent_def = save_emsg_silent_def;
6351 did_emsg_def += save_did_emsg_def;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02006352
Bram Moolenaaree8580e2020-08-28 17:19:07 +02006353failed_early:
Bram Moolenaar0d1f55c2022-04-05 17:30:29 +01006354 // Free all arguments and local variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006355 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
Bram Moolenaar1936c762022-09-28 15:19:10 +01006356 {
6357 tv = STACK_TV(idx);
6358 if (tv->v_type != VAR_NUMBER && tv->v_type != VAR_UNKNOWN)
6359 clear_tv(tv);
6360 }
Bram Moolenaare99d4222021-06-13 14:01:26 +02006361 ex_nesting_level = orig_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02006362
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006363 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01006364 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006365 if (ectx.ec_outer_ref != NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +01006366 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006367 if (ectx.ec_outer_ref->or_outer_allocated)
6368 vim_free(ectx.ec_outer_ref->or_outer);
6369 partial_unref(ectx.ec_outer_ref->or_partial);
6370 vim_free(ectx.ec_outer_ref);
Bram Moolenaar0186e582021-01-10 18:33:11 +01006371 }
6372
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02006373 // Not sure if this is necessary.
6374 suppress_errthrow = save_suppress_errthrow;
6375
Bram Moolenaarb5841b92021-07-15 18:09:53 +02006376 if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before
6377 && !need_rethrow)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006378 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02006379 printable_func_name(ufunc));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01006380 funcdepth_restore(orig_funcdepth);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006381 return ret;
6382}
6383
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006384/*
Bram Moolenaar58779852022-09-06 18:31:14 +01006385 * Called when a def function has finished (possibly failed).
6386 * Invoke all the function returns to clean up and invoke deferred functions,
6387 * except the toplevel one.
6388 */
6389 void
6390unwind_def_callstack(ectx_T *ectx)
6391{
6392 while (ectx->ec_frame_idx != ectx->ec_initial_frame_idx)
6393 func_return(ectx);
6394}
6395
6396/*
dundargocc57b5bc2022-11-02 13:30:51 +00006397 * Invoke any deferred functions for the top function in "ectx".
Bram Moolenaar58779852022-09-06 18:31:14 +01006398 */
6399 void
6400may_invoke_defer_funcs(ectx_T *ectx)
6401{
6402 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
6403
6404 if (dfunc->df_defer_var_idx > 0)
6405 invoke_defer_funcs(ectx);
6406}
6407
6408/*
Bram Moolenaarcc341812022-09-19 15:54:34 +01006409 * Return loopvarinfo in a printable form in allocated memory.
6410 */
6411 static char_u *
6412printable_loopvarinfo(loopvarinfo_T *lvi)
6413{
6414 garray_T ga;
6415 int depth;
6416
6417 ga_init2(&ga, 1, 100);
6418 for (depth = 0; depth < lvi->lvi_depth; ++depth)
6419 {
6420 if (ga_grow(&ga, 50) == FAIL)
6421 break;
6422 if (lvi->lvi_loop[depth].var_idx == 0)
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +01006423 STRCPY((char *)ga.ga_data + ga.ga_len, " -");
Bram Moolenaarcc341812022-09-19 15:54:34 +01006424 else
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +01006425 vim_snprintf((char *)ga.ga_data + ga.ga_len, 50, " $%d-$%d",
Bram Moolenaarcc341812022-09-19 15:54:34 +01006426 lvi->lvi_loop[depth].var_idx,
6427 lvi->lvi_loop[depth].var_idx
6428 + lvi->lvi_loop[depth].var_count - 1);
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +01006429 ga.ga_len = (int)STRLEN(ga.ga_data);
Bram Moolenaarcc341812022-09-19 15:54:34 +01006430 }
6431 return ga.ga_data;
6432}
6433
6434/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02006435 * List instructions "instr" up to "instr_count" or until ISN_FINISH.
6436 * "ufunc" has the source lines, NULL for the instructions of ISN_SUBSTITUTE.
6437 * "pfx" is prefixed to every line.
6438 */
6439 static void
6440list_instructions(char *pfx, isn_T *instr, int instr_count, ufunc_T *ufunc)
6441{
6442 int line_idx = 0;
6443 int prev_current = 0;
6444 int current;
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02006445 int def_arg_idx = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006446
6447 for (current = 0; current < instr_count; ++current)
6448 {
6449 isn_T *iptr = &instr[current];
6450 char *line;
6451
6452 if (ufunc != NULL)
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02006453 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02006454 while (line_idx < iptr->isn_lnum
6455 && line_idx < ufunc->uf_lines.ga_len)
6456 {
6457 if (current > prev_current)
6458 {
6459 msg_puts("\n\n");
6460 prev_current = current;
6461 }
6462 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
6463 if (line != NULL)
6464 msg(line);
6465 }
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02006466 if (iptr->isn_type == ISN_JUMP_IF_ARG_SET)
6467 {
6468 int first_def_arg = ufunc->uf_args.ga_len
6469 - ufunc->uf_def_args.ga_len;
6470
6471 if (def_arg_idx > 0)
6472 msg_puts("\n\n");
6473 msg_start();
6474 msg_puts(" ");
6475 msg_puts(((char **)(ufunc->uf_args.ga_data))[
6476 first_def_arg + def_arg_idx]);
6477 msg_puts(" = ");
6478 msg_puts(((char **)(ufunc->uf_def_args.ga_data))[def_arg_idx++]);
6479 msg_clr_eos();
6480 msg_end();
6481 }
6482 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006483
6484 switch (iptr->isn_type)
6485 {
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006486 case ISN_CONSTRUCT:
6487 smsg("%s%4d NEW %s size %d", pfx, current,
6488 iptr->isn_arg.construct.construct_class->class_name,
6489 (int)iptr->isn_arg.construct.construct_size);
6490 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006491 case ISN_EXEC:
6492 smsg("%s%4d EXEC %s", pfx, current, iptr->isn_arg.string);
6493 break;
Bram Moolenaar20677332021-06-06 17:02:53 +02006494 case ISN_EXEC_SPLIT:
6495 smsg("%s%4d EXEC_SPLIT %s", pfx, current, iptr->isn_arg.string);
6496 break;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00006497 case ISN_EXECRANGE:
6498 smsg("%s%4d EXECRANGE %s", pfx, current, iptr->isn_arg.string);
6499 break;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02006500 case ISN_LEGACY_EVAL:
6501 smsg("%s%4d EVAL legacy %s", pfx, current,
6502 iptr->isn_arg.string);
6503 break;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02006504 case ISN_REDIRSTART:
6505 smsg("%s%4d REDIR", pfx, current);
6506 break;
6507 case ISN_REDIREND:
6508 smsg("%s%4d REDIR END%s", pfx, current,
6509 iptr->isn_arg.number ? " append" : "");
6510 break;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006511 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02006512#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006513 smsg("%s%4d CEXPR pre %s", pfx, current,
6514 cexpr_get_auname(iptr->isn_arg.number));
Bram Moolenaarb7c97812021-05-05 22:51:39 +02006515#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006516 break;
6517 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02006518#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006519 {
6520 cexprref_T *cer = iptr->isn_arg.cexpr.cexpr_ref;
6521
6522 smsg("%s%4d CEXPR core %s%s \"%s\"", pfx, current,
6523 cexpr_get_auname(cer->cer_cmdidx),
6524 cer->cer_forceit ? "!" : "",
6525 cer->cer_cmdline);
6526 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02006527#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006528 break;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006529 case ISN_INSTR:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006530 smsg("%s%4d INSTR", pfx, current);
6531 list_instructions(" ", iptr->isn_arg.instr, INT_MAX, NULL);
6532 msg(" -------------");
6533 break;
6534 case ISN_SOURCE:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006535 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006536 scriptitem_T *si = SCRIPT_ITEM(iptr->isn_arg.number);
6537
6538 smsg("%s%4d SOURCE %s", pfx, current, si->sn_name);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006539 }
6540 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006541 case ISN_SUBSTITUTE:
6542 {
6543 subs_T *subs = &iptr->isn_arg.subs;
6544
6545 smsg("%s%4d SUBSTITUTE %s", pfx, current, subs->subs_cmd);
6546 list_instructions(" ", subs->subs_instr, INT_MAX, NULL);
6547 msg(" -------------");
6548 }
6549 break;
6550 case ISN_EXECCONCAT:
6551 smsg("%s%4d EXECCONCAT %lld", pfx, current,
6552 (varnumber_T)iptr->isn_arg.number);
6553 break;
6554 case ISN_ECHO:
6555 {
6556 echo_T *echo = &iptr->isn_arg.echo;
6557
6558 smsg("%s%4d %s %d", pfx, current,
6559 echo->echo_with_white ? "ECHO" : "ECHON",
6560 echo->echo_count);
6561 }
6562 break;
6563 case ISN_EXECUTE:
6564 smsg("%s%4d EXECUTE %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02006565 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006566 break;
6567 case ISN_ECHOMSG:
6568 smsg("%s%4d ECHOMSG %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02006569 (varnumber_T)(iptr->isn_arg.number));
6570 break;
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01006571 case ISN_ECHOWINDOW:
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01006572 if (iptr->isn_arg.echowin.ewin_time > 0)
6573 smsg("%s%4d ECHOWINDOW %d (%ld sec)", pfx, current,
6574 iptr->isn_arg.echowin.ewin_count,
6575 iptr->isn_arg.echowin.ewin_time);
6576 else
6577 smsg("%s%4d ECHOWINDOW %d", pfx, current,
6578 iptr->isn_arg.echowin.ewin_count);
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01006579 break;
Bram Moolenaar7de62622021-08-07 15:05:47 +02006580 case ISN_ECHOCONSOLE:
6581 smsg("%s%4d ECHOCONSOLE %lld", pfx, current,
6582 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006583 break;
6584 case ISN_ECHOERR:
6585 smsg("%s%4d ECHOERR %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02006586 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006587 break;
6588 case ISN_LOAD:
6589 {
6590 if (iptr->isn_arg.number < 0)
6591 smsg("%s%4d LOAD arg[%lld]", pfx, current,
6592 (varnumber_T)(iptr->isn_arg.number
6593 + STACK_FRAME_SIZE));
6594 else
6595 smsg("%s%4d LOAD $%lld", pfx, current,
6596 (varnumber_T)(iptr->isn_arg.number));
6597 }
6598 break;
6599 case ISN_LOADOUTER:
6600 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006601 isn_outer_T *outer = &iptr->isn_arg.outer;
6602
6603 if (outer->outer_idx < 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02006604 smsg("%s%4d LOADOUTER level %d arg[%d]", pfx, current,
Bram Moolenaarcc341812022-09-19 15:54:34 +01006605 outer->outer_depth,
6606 outer->outer_idx + STACK_FRAME_SIZE);
6607 else if (outer->outer_depth < 0)
6608 smsg("%s%4d LOADOUTER $%d in loop level %d",
6609 pfx, current,
6610 outer->outer_idx,
6611 -outer->outer_depth);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006612 else
6613 smsg("%s%4d LOADOUTER level %d $%d", pfx, current,
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006614 outer->outer_depth,
6615 outer->outer_idx);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006616 }
6617 break;
6618 case ISN_LOADV:
6619 smsg("%s%4d LOADV v:%s", pfx, current,
6620 get_vim_var_name(iptr->isn_arg.number));
6621 break;
6622 case ISN_LOADSCRIPT:
6623 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006624 scriptref_T *sref = iptr->isn_arg.script.scriptref;
6625 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
6626 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006627
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006628 sv = get_script_svar(sref, -1);
6629 if (sv == NULL)
6630 smsg("%s%4d LOADSCRIPT [deleted] from %s",
6631 pfx, current, si->sn_name);
6632 else
6633 smsg("%s%4d LOADSCRIPT %s-%d from %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02006634 sv->sv_name,
6635 sref->sref_idx,
6636 si->sn_name);
6637 }
6638 break;
6639 case ISN_LOADS:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006640 case ISN_LOADEXPORT:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006641 {
6642 scriptitem_T *si = SCRIPT_ITEM(
6643 iptr->isn_arg.loadstore.ls_sid);
6644
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006645 smsg("%s%4d %s s:%s from %s", pfx, current,
6646 iptr->isn_type == ISN_LOADS ? "LOADS"
6647 : "LOADEXPORT",
6648 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006649 }
6650 break;
6651 case ISN_LOADAUTO:
6652 smsg("%s%4d LOADAUTO %s", pfx, current, iptr->isn_arg.string);
6653 break;
6654 case ISN_LOADG:
6655 smsg("%s%4d LOADG g:%s", pfx, current, iptr->isn_arg.string);
6656 break;
6657 case ISN_LOADB:
6658 smsg("%s%4d LOADB b:%s", pfx, current, iptr->isn_arg.string);
6659 break;
6660 case ISN_LOADW:
6661 smsg("%s%4d LOADW w:%s", pfx, current, iptr->isn_arg.string);
6662 break;
6663 case ISN_LOADT:
6664 smsg("%s%4d LOADT t:%s", pfx, current, iptr->isn_arg.string);
6665 break;
6666 case ISN_LOADGDICT:
6667 smsg("%s%4d LOAD g:", pfx, current);
6668 break;
6669 case ISN_LOADBDICT:
6670 smsg("%s%4d LOAD b:", pfx, current);
6671 break;
6672 case ISN_LOADWDICT:
6673 smsg("%s%4d LOAD w:", pfx, current);
6674 break;
6675 case ISN_LOADTDICT:
6676 smsg("%s%4d LOAD t:", pfx, current);
6677 break;
6678 case ISN_LOADOPT:
6679 smsg("%s%4d LOADOPT %s", pfx, current, iptr->isn_arg.string);
6680 break;
6681 case ISN_LOADENV:
6682 smsg("%s%4d LOADENV %s", pfx, current, iptr->isn_arg.string);
6683 break;
6684 case ISN_LOADREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006685 smsg("%s%4d LOADREG @%c", pfx, current,
6686 (int)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006687 break;
6688
6689 case ISN_STORE:
6690 if (iptr->isn_arg.number < 0)
6691 smsg("%s%4d STORE arg[%lld]", pfx, current,
6692 iptr->isn_arg.number + STACK_FRAME_SIZE);
6693 else
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006694 smsg("%s%4d STORE $%lld", pfx, current,
6695 iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006696 break;
6697 case ISN_STOREOUTER:
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006698 {
6699 isn_outer_T *outer = &iptr->isn_arg.outer;
6700
6701 if (outer->outer_depth == OUTER_LOOP_DEPTH)
6702 smsg("%s%4d STOREOUTER level 1 $%d in loop",
6703 pfx, current, outer->outer_idx);
6704 else
6705 smsg("%s%4d STOREOUTER level %d $%d", pfx, current,
6706 outer->outer_depth, outer->outer_idx);
6707 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006708 break;
6709 case ISN_STOREV:
6710 smsg("%s%4d STOREV v:%s", pfx, current,
6711 get_vim_var_name(iptr->isn_arg.number));
6712 break;
6713 case ISN_STOREAUTO:
6714 smsg("%s%4d STOREAUTO %s", pfx, current, iptr->isn_arg.string);
6715 break;
6716 case ISN_STOREG:
6717 smsg("%s%4d STOREG %s", pfx, current, iptr->isn_arg.string);
6718 break;
6719 case ISN_STOREB:
6720 smsg("%s%4d STOREB %s", pfx, current, iptr->isn_arg.string);
6721 break;
6722 case ISN_STOREW:
6723 smsg("%s%4d STOREW %s", pfx, current, iptr->isn_arg.string);
6724 break;
6725 case ISN_STORET:
6726 smsg("%s%4d STORET %s", pfx, current, iptr->isn_arg.string);
6727 break;
6728 case ISN_STORES:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006729 case ISN_STOREEXPORT:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006730 {
6731 scriptitem_T *si = SCRIPT_ITEM(
6732 iptr->isn_arg.loadstore.ls_sid);
6733
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006734 smsg("%s%4d %s %s in %s", pfx, current,
6735 iptr->isn_type == ISN_STORES
6736 ? "STORES" : "STOREEXPORT",
Bram Moolenaar4c137212021-04-19 16:48:48 +02006737 iptr->isn_arg.loadstore.ls_name, si->sn_name);
6738 }
6739 break;
6740 case ISN_STORESCRIPT:
6741 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006742 scriptref_T *sref = iptr->isn_arg.script.scriptref;
6743 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
6744 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006745
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006746 sv = get_script_svar(sref, -1);
6747 if (sv == NULL)
6748 smsg("%s%4d STORESCRIPT [deleted] in %s",
6749 pfx, current, si->sn_name);
6750 else
6751 smsg("%s%4d STORESCRIPT %s-%d in %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02006752 sv->sv_name,
6753 sref->sref_idx,
6754 si->sn_name);
6755 }
6756 break;
6757 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00006758 case ISN_STOREFUNCOPT:
6759 smsg("%s%4d %s &%s", pfx, current,
6760 iptr->isn_type == ISN_STOREOPT ? "STOREOPT" : "STOREFUNCOPT",
Bram Moolenaar4c137212021-04-19 16:48:48 +02006761 iptr->isn_arg.storeopt.so_name);
6762 break;
6763 case ISN_STOREENV:
6764 smsg("%s%4d STOREENV $%s", pfx, current, iptr->isn_arg.string);
6765 break;
6766 case ISN_STOREREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006767 smsg("%s%4d STOREREG @%c", pfx, current,
6768 (int)iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006769 break;
6770 case ISN_STORENR:
6771 smsg("%s%4d STORE %lld in $%d", pfx, current,
6772 iptr->isn_arg.storenr.stnr_val,
6773 iptr->isn_arg.storenr.stnr_idx);
6774 break;
6775
6776 case ISN_STOREINDEX:
6777 smsg("%s%4d STOREINDEX %s", pfx, current,
Bram Moolenaarf7d1c6e2023-01-16 20:47:57 +00006778 vartype_name(iptr->isn_arg.storeindex.si_vartype));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006779 break;
6780
6781 case ISN_STORERANGE:
6782 smsg("%s%4d STORERANGE", pfx, current);
6783 break;
6784
Bram Moolenaard505d172022-12-18 21:42:55 +00006785 case ISN_LOAD_CLASSMEMBER:
6786 case ISN_STORE_CLASSMEMBER:
6787 {
6788 class_T *cl = iptr->isn_arg.classmember.cm_class;
6789 int idx = iptr->isn_arg.classmember.cm_idx;
6790 ocmember_T *ocm = &cl->class_class_members[idx];
6791 smsg("%s%4d %s CLASSMEMBER %s.%s", pfx, current,
6792 iptr->isn_type == ISN_LOAD_CLASSMEMBER
6793 ? "LOAD" : "STORE",
6794 cl->class_name, ocm->ocm_name);
6795 }
6796 break;
6797
Bram Moolenaar4c137212021-04-19 16:48:48 +02006798 // constants
6799 case ISN_PUSHNR:
6800 smsg("%s%4d PUSHNR %lld", pfx, current,
6801 (varnumber_T)(iptr->isn_arg.number));
6802 break;
6803 case ISN_PUSHBOOL:
6804 case ISN_PUSHSPEC:
6805 smsg("%s%4d PUSH %s", pfx, current,
6806 get_var_special_name(iptr->isn_arg.number));
6807 break;
6808 case ISN_PUSHF:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006809 smsg("%s%4d PUSHF %g", pfx, current, iptr->isn_arg.fnumber);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006810 break;
6811 case ISN_PUSHS:
6812 smsg("%s%4d PUSHS \"%s\"", pfx, current, iptr->isn_arg.string);
6813 break;
6814 case ISN_PUSHBLOB:
6815 {
6816 char_u *r;
6817 char_u numbuf[NUMBUFLEN];
6818 char_u *tofree;
6819
6820 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
6821 smsg("%s%4d PUSHBLOB %s", pfx, current, r);
6822 vim_free(tofree);
6823 }
6824 break;
6825 case ISN_PUSHFUNC:
6826 {
6827 char *name = (char *)iptr->isn_arg.string;
6828
6829 smsg("%s%4d PUSHFUNC \"%s\"", pfx, current,
6830 name == NULL ? "[none]" : name);
6831 }
6832 break;
6833 case ISN_PUSHCHANNEL:
6834#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar397a87a2022-03-20 21:14:15 +00006835 smsg("%s%4d PUSHCHANNEL 0", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006836#endif
6837 break;
6838 case ISN_PUSHJOB:
6839#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar397a87a2022-03-20 21:14:15 +00006840 smsg("%s%4d PUSHJOB \"no process\"", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006841#endif
6842 break;
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00006843 case ISN_PUSHOBJ:
6844 smsg("%s%4d PUSHOBJ null", pfx, current);
6845 break;
6846 case ISN_PUSHCLASS:
6847 smsg("%s%4d PUSHCLASS %s", pfx, current,
Bram Moolenaar30a84472023-02-27 08:07:14 +00006848 iptr->isn_arg.classarg == NULL ? "null"
6849 : (char *)iptr->isn_arg.classarg->class_name);
Bram Moolenaarc4e1b862023-02-26 18:58:23 +00006850 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006851 case ISN_PUSHEXC:
6852 smsg("%s%4d PUSH v:exception", pfx, current);
6853 break;
Bram Moolenaar06b77222022-01-25 15:51:56 +00006854 case ISN_AUTOLOAD:
6855 smsg("%s%4d AUTOLOAD %s", pfx, current, iptr->isn_arg.string);
6856 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006857 case ISN_UNLET:
6858 smsg("%s%4d UNLET%s %s", pfx, current,
6859 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
6860 iptr->isn_arg.unlet.ul_name);
6861 break;
6862 case ISN_UNLETENV:
6863 smsg("%s%4d UNLETENV%s $%s", pfx, current,
6864 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
6865 iptr->isn_arg.unlet.ul_name);
6866 break;
6867 case ISN_UNLETINDEX:
6868 smsg("%s%4d UNLETINDEX", pfx, current);
6869 break;
6870 case ISN_UNLETRANGE:
6871 smsg("%s%4d UNLETRANGE", pfx, current);
6872 break;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02006873 case ISN_LOCKUNLOCK:
6874 smsg("%s%4d LOCKUNLOCK %s", pfx, current, iptr->isn_arg.string);
6875 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006876 case ISN_LOCKCONST:
6877 smsg("%s%4d LOCKCONST", pfx, current);
6878 break;
6879 case ISN_NEWLIST:
6880 smsg("%s%4d NEWLIST size %lld", pfx, current,
Bram Moolenaar1936c762022-09-28 15:19:10 +01006881 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006882 break;
6883 case ISN_NEWDICT:
6884 smsg("%s%4d NEWDICT size %lld", pfx, current,
Bram Moolenaar1936c762022-09-28 15:19:10 +01006885 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006886 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00006887 case ISN_NEWPARTIAL:
6888 smsg("%s%4d NEWPARTIAL", pfx, current);
6889 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006890
6891 // function call
6892 case ISN_BCALL:
6893 {
6894 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
6895
6896 smsg("%s%4d BCALL %s(argc %d)", pfx, current,
6897 internal_func_name(cbfunc->cbf_idx),
6898 cbfunc->cbf_argcount);
6899 }
6900 break;
6901 case ISN_DCALL:
6902 {
6903 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
6904 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
6905 + cdfunc->cdf_idx;
6906
6907 smsg("%s%4d DCALL %s(argc %d)", pfx, current,
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006908 printable_func_name(df->df_ufunc),
6909 cdfunc->cdf_argcount);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006910 }
6911 break;
Bram Moolenaard0200c82023-01-28 15:19:40 +00006912 case ISN_METHODCALL:
6913 {
6914 cmfunc_T *mfunc = iptr->isn_arg.mfunc;
6915
6916 smsg("%s%4d METHODCALL %s.%s(argc %d)", pfx, current,
6917 mfunc->cmf_itf->class_name,
6918 mfunc->cmf_itf->class_obj_methods[
6919 mfunc->cmf_idx]->uf_name,
6920 mfunc->cmf_argcount);
6921 }
6922 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006923 case ISN_UCALL:
6924 {
6925 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
6926
6927 smsg("%s%4d UCALL %s(argc %d)", pfx, current,
6928 cufunc->cuf_name, cufunc->cuf_argcount);
6929 }
6930 break;
6931 case ISN_PCALL:
6932 {
6933 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
6934
6935 smsg("%s%4d PCALL%s (argc %d)", pfx, current,
6936 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
6937 }
6938 break;
6939 case ISN_PCALL_END:
6940 smsg("%s%4d PCALL end", pfx, current);
6941 break;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006942 case ISN_DEFER:
Yegappan Lakshmananf3eac692023-10-17 11:00:45 +02006943 smsg("%s%4d DEFER %d args", pfx, current,
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006944 (int)iptr->isn_arg.defer.defer_argcount);
6945 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006946 case ISN_RETURN:
6947 smsg("%s%4d RETURN", pfx, current);
6948 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02006949 case ISN_RETURN_VOID:
6950 smsg("%s%4d RETURN void", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006951 break;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006952 case ISN_RETURN_OBJECT:
6953 smsg("%s%4d RETURN object", pfx, current);
6954 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006955 case ISN_FUNCREF:
6956 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006957 funcref_T *funcref = &iptr->isn_arg.funcref;
6958 funcref_extra_T *extra = funcref->fr_extra;
6959 char_u *name;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006960
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006961 if (extra == NULL || extra->fre_func_name == NULL)
Bram Moolenaar38453522021-11-28 22:00:12 +00006962 {
6963 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
6964 + funcref->fr_dfunc_idx;
6965 name = df->df_ufunc->uf_name;
6966 }
6967 else
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006968 name = extra->fre_func_name;
Bram Moolenaar313e4722023-02-08 20:55:27 +00006969 if (extra != NULL && extra->fre_class != NULL)
6970 {
6971 smsg("%s%4d FUNCREF %s.%s", pfx, current,
6972 extra->fre_class->class_name, name);
6973 }
6974 else if (extra == NULL
6975 || extra->fre_loopvar_info.lvi_depth == 0)
6976 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006977 smsg("%s%4d FUNCREF %s", pfx, current, name);
Bram Moolenaar313e4722023-02-08 20:55:27 +00006978 }
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006979 else
Bram Moolenaarcc341812022-09-19 15:54:34 +01006980 {
6981 char_u *info = printable_loopvarinfo(
6982 &extra->fre_loopvar_info);
6983
6984 smsg("%s%4d FUNCREF %s vars %s", pfx, current,
6985 name, info);
6986 vim_free(info);
6987 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006988 }
6989 break;
6990
6991 case ISN_NEWFUNC:
6992 {
Bram Moolenaarcc341812022-09-19 15:54:34 +01006993 newfuncarg_T *arg = iptr->isn_arg.newfunc.nf_arg;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006994
Bram Moolenaarcc341812022-09-19 15:54:34 +01006995 if (arg->nfa_loopvar_info.lvi_depth == 0)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006996 smsg("%s%4d NEWFUNC %s %s", pfx, current,
6997 arg->nfa_lambda, arg->nfa_global);
6998 else
Bram Moolenaarcc341812022-09-19 15:54:34 +01006999 {
7000 char_u *info = printable_loopvarinfo(
7001 &arg->nfa_loopvar_info);
7002
7003 smsg("%s%4d NEWFUNC %s %s vars %s", pfx, current,
7004 arg->nfa_lambda, arg->nfa_global, info);
7005 vim_free(info);
7006 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02007007 }
7008 break;
7009
7010 case ISN_DEF:
7011 {
7012 char_u *name = iptr->isn_arg.string;
7013
7014 smsg("%s%4d DEF %s", pfx, current,
7015 name == NULL ? (char_u *)"" : name);
7016 }
7017 break;
7018
7019 case ISN_JUMP:
7020 {
7021 char *when = "?";
7022
7023 switch (iptr->isn_arg.jump.jump_when)
7024 {
7025 case JUMP_ALWAYS:
7026 when = "JUMP";
7027 break;
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02007028 case JUMP_NEVER:
7029 iemsg("JUMP_NEVER should not be used");
7030 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007031 case JUMP_AND_KEEP_IF_TRUE:
7032 when = "JUMP_AND_KEEP_IF_TRUE";
7033 break;
7034 case JUMP_IF_FALSE:
7035 when = "JUMP_IF_FALSE";
7036 break;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01007037 case JUMP_WHILE_FALSE:
7038 when = "JUMP_WHILE_FALSE"; // unused
7039 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007040 case JUMP_IF_COND_FALSE:
7041 when = "JUMP_IF_COND_FALSE";
7042 break;
7043 case JUMP_IF_COND_TRUE:
7044 when = "JUMP_IF_COND_TRUE";
7045 break;
7046 }
7047 smsg("%s%4d %s -> %d", pfx, current, when,
7048 iptr->isn_arg.jump.jump_where);
7049 }
7050 break;
7051
7052 case ISN_JUMP_IF_ARG_SET:
7053 smsg("%s%4d JUMP_IF_ARG_SET arg[%d] -> %d", pfx, current,
7054 iptr->isn_arg.jumparg.jump_arg_off + STACK_FRAME_SIZE,
7055 iptr->isn_arg.jump.jump_where);
7056 break;
7057
Bram Moolenaar65b0d162022-12-13 18:43:22 +00007058 case ISN_JUMP_IF_ARG_NOT_SET:
7059 smsg("%s%4d JUMP_IF_ARG_NOT_SET arg[%d] -> %d", pfx, current,
7060 iptr->isn_arg.jumparg.jump_arg_off + STACK_FRAME_SIZE,
7061 iptr->isn_arg.jump.jump_where);
7062 break;
7063
Bram Moolenaar4c137212021-04-19 16:48:48 +02007064 case ISN_FOR:
7065 {
7066 forloop_T *forloop = &iptr->isn_arg.forloop;
7067
7068 smsg("%s%4d FOR $%d -> %d", pfx, current,
Bram Moolenaarcc341812022-09-19 15:54:34 +01007069 forloop->for_loop_idx, forloop->for_end);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007070 }
7071 break;
7072
Bram Moolenaarb46c0832022-09-15 17:19:37 +01007073 case ISN_ENDLOOP:
7074 {
7075 endloop_T *endloop = &iptr->isn_arg.endloop;
7076
Bram Moolenaarcc341812022-09-19 15:54:34 +01007077 smsg("%s%4d ENDLOOP ref $%d save $%d-$%d depth %d",
7078 pfx, current,
Bram Moolenaarb46c0832022-09-15 17:19:37 +01007079 endloop->end_funcref_idx,
7080 endloop->end_var_idx,
Bram Moolenaarcc341812022-09-19 15:54:34 +01007081 endloop->end_var_idx + endloop->end_var_count - 1,
7082 endloop->end_depth);
Bram Moolenaarb46c0832022-09-15 17:19:37 +01007083 }
7084 break;
7085
7086 case ISN_WHILE:
7087 {
7088 whileloop_T *whileloop = &iptr->isn_arg.whileloop;
7089
7090 smsg("%s%4d WHILE $%d -> %d", pfx, current,
7091 whileloop->while_funcref_idx,
7092 whileloop->while_end);
7093 }
7094 break;
7095
Bram Moolenaar4c137212021-04-19 16:48:48 +02007096 case ISN_TRY:
7097 {
Bram Moolenaar0d807102021-12-21 09:42:09 +00007098 try_T *try = &iptr->isn_arg.tryref;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007099
7100 if (try->try_ref->try_finally == 0)
7101 smsg("%s%4d TRY catch -> %d, endtry -> %d",
7102 pfx, current,
7103 try->try_ref->try_catch,
7104 try->try_ref->try_endtry);
7105 else
7106 smsg("%s%4d TRY catch -> %d, finally -> %d, endtry -> %d",
7107 pfx, current,
7108 try->try_ref->try_catch,
7109 try->try_ref->try_finally,
7110 try->try_ref->try_endtry);
7111 }
7112 break;
7113 case ISN_CATCH:
Bram Moolenaar4c137212021-04-19 16:48:48 +02007114 smsg("%s%4d CATCH", pfx, current);
7115 break;
7116 case ISN_TRYCONT:
7117 {
7118 trycont_T *trycont = &iptr->isn_arg.trycont;
7119
7120 smsg("%s%4d TRY-CONTINUE %d level%s -> %d", pfx, current,
7121 trycont->tct_levels,
7122 trycont->tct_levels == 1 ? "" : "s",
7123 trycont->tct_where);
7124 }
7125 break;
7126 case ISN_FINALLY:
7127 smsg("%s%4d FINALLY", pfx, current);
7128 break;
7129 case ISN_ENDTRY:
7130 smsg("%s%4d ENDTRY", pfx, current);
7131 break;
7132 case ISN_THROW:
7133 smsg("%s%4d THROW", pfx, current);
7134 break;
7135
7136 // expression operations on number
7137 case ISN_OPNR:
7138 case ISN_OPFLOAT:
7139 case ISN_OPANY:
7140 {
7141 char *what;
7142 char *ins;
7143
7144 switch (iptr->isn_arg.op.op_type)
7145 {
7146 case EXPR_MULT: what = "*"; break;
7147 case EXPR_DIV: what = "/"; break;
7148 case EXPR_REM: what = "%"; break;
7149 case EXPR_SUB: what = "-"; break;
7150 case EXPR_ADD: what = "+"; break;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01007151 case EXPR_LSHIFT: what = "<<"; break;
7152 case EXPR_RSHIFT: what = ">>"; break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007153 default: what = "???"; break;
7154 }
7155 switch (iptr->isn_type)
7156 {
7157 case ISN_OPNR: ins = "OPNR"; break;
7158 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
7159 case ISN_OPANY: ins = "OPANY"; break;
7160 default: ins = "???"; break;
7161 }
7162 smsg("%s%4d %s %s", pfx, current, ins, what);
7163 }
7164 break;
7165
7166 case ISN_COMPAREBOOL:
7167 case ISN_COMPARESPECIAL:
Bram Moolenaar7a222242022-03-01 19:23:24 +00007168 case ISN_COMPARENULL:
Bram Moolenaar4c137212021-04-19 16:48:48 +02007169 case ISN_COMPARENR:
7170 case ISN_COMPAREFLOAT:
7171 case ISN_COMPARESTRING:
7172 case ISN_COMPAREBLOB:
7173 case ISN_COMPARELIST:
7174 case ISN_COMPAREDICT:
7175 case ISN_COMPAREFUNC:
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00007176 case ISN_COMPARECLASS:
7177 case ISN_COMPAREOBJECT:
Bram Moolenaar4c137212021-04-19 16:48:48 +02007178 case ISN_COMPAREANY:
7179 {
7180 char *p;
7181 char buf[10];
7182 char *type;
7183
7184 switch (iptr->isn_arg.op.op_type)
7185 {
7186 case EXPR_EQUAL: p = "=="; break;
7187 case EXPR_NEQUAL: p = "!="; break;
7188 case EXPR_GREATER: p = ">"; break;
7189 case EXPR_GEQUAL: p = ">="; break;
7190 case EXPR_SMALLER: p = "<"; break;
7191 case EXPR_SEQUAL: p = "<="; break;
7192 case EXPR_MATCH: p = "=~"; break;
7193 case EXPR_IS: p = "is"; break;
7194 case EXPR_ISNOT: p = "isnot"; break;
7195 case EXPR_NOMATCH: p = "!~"; break;
7196 default: p = "???"; break;
7197 }
7198 STRCPY(buf, p);
7199 if (iptr->isn_arg.op.op_ic == TRUE)
7200 strcat(buf, "?");
7201 switch(iptr->isn_type)
7202 {
7203 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
7204 case ISN_COMPARESPECIAL:
7205 type = "COMPARESPECIAL"; break;
Bram Moolenaar7a222242022-03-01 19:23:24 +00007206 case ISN_COMPARENULL: type = "COMPARENULL"; break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007207 case ISN_COMPARENR: type = "COMPARENR"; break;
7208 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
7209 case ISN_COMPARESTRING:
7210 type = "COMPARESTRING"; break;
7211 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
7212 case ISN_COMPARELIST: type = "COMPARELIST"; break;
7213 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
7214 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00007215 case ISN_COMPARECLASS: type = "COMPARECLASS"; break;
7216 case ISN_COMPAREOBJECT:
7217 type = "COMPAREOBJECT"; break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007218 case ISN_COMPAREANY: type = "COMPAREANY"; break;
7219 default: type = "???"; break;
7220 }
7221
7222 smsg("%s%4d %s %s", pfx, current, type, buf);
7223 }
7224 break;
7225
7226 case ISN_ADDLIST: smsg("%s%4d ADDLIST", pfx, current); break;
7227 case ISN_ADDBLOB: smsg("%s%4d ADDBLOB", pfx, current); break;
7228
7229 // expression operations
LemonBoy372bcce2022-04-25 12:43:20 +01007230 case ISN_CONCAT:
7231 smsg("%s%4d CONCAT size %lld", pfx, current,
7232 (varnumber_T)(iptr->isn_arg.number));
7233 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007234 case ISN_STRINDEX: smsg("%s%4d STRINDEX", pfx, current); break;
7235 case ISN_STRSLICE: smsg("%s%4d STRSLICE", pfx, current); break;
7236 case ISN_BLOBINDEX: smsg("%s%4d BLOBINDEX", pfx, current); break;
7237 case ISN_BLOBSLICE: smsg("%s%4d BLOBSLICE", pfx, current); break;
7238 case ISN_LISTAPPEND: smsg("%s%4d LISTAPPEND", pfx, current); break;
7239 case ISN_BLOBAPPEND: smsg("%s%4d BLOBAPPEND", pfx, current); break;
7240 case ISN_LISTINDEX: smsg("%s%4d LISTINDEX", pfx, current); break;
7241 case ISN_LISTSLICE: smsg("%s%4d LISTSLICE", pfx, current); break;
7242 case ISN_ANYINDEX: smsg("%s%4d ANYINDEX", pfx, current); break;
7243 case ISN_ANYSLICE: smsg("%s%4d ANYSLICE", pfx, current); break;
7244 case ISN_SLICE: smsg("%s%4d SLICE %lld",
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02007245 pfx, current, iptr->isn_arg.number); break;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02007246 case ISN_GETITEM: smsg("%s%4d ITEM %lld%s", pfx, current,
7247 iptr->isn_arg.getitem.gi_index,
7248 iptr->isn_arg.getitem.gi_with_op ?
7249 " with op" : ""); break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007250 case ISN_MEMBER: smsg("%s%4d MEMBER", pfx, current); break;
7251 case ISN_STRINGMEMBER: smsg("%s%4d MEMBER %s", pfx, current,
7252 iptr->isn_arg.string); break;
Yegappan Lakshmanan5a05d372023-09-29 19:43:11 +02007253 case ISN_GET_OBJ_MEMBER: smsg("%s%4d OBJ_MEMBER %d", pfx, current,
7254 (int)iptr->isn_arg.classmember.cm_idx);
Ernie Rael00df69e2023-09-05 07:38:09 +02007255 break;
Yegappan Lakshmanan5a05d372023-09-29 19:43:11 +02007256 case ISN_GET_ITF_MEMBER: smsg("%s%4d ITF_MEMBER %d on %s",
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00007257 pfx, current,
7258 (int)iptr->isn_arg.classmember.cm_idx,
Yegappan Lakshmanan5a05d372023-09-29 19:43:11 +02007259 iptr->isn_arg.classmember.cm_class->class_name);
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00007260 break;
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00007261 case ISN_STORE_THIS: smsg("%s%4d STORE_THIS %d", pfx, current,
Bram Moolenaarffdaca92022-12-09 21:41:48 +00007262 (int)iptr->isn_arg.number); break;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02007263 case ISN_CLEARDICT: smsg("%s%4d CLEARDICT", pfx, current); break;
7264 case ISN_USEDICT: smsg("%s%4d USEDICT", pfx, current); break;
7265
Bram Moolenaar4c137212021-04-19 16:48:48 +02007266 case ISN_NEGATENR: smsg("%s%4d NEGATENR", pfx, current); break;
7267
Bram Moolenaar4c137212021-04-19 16:48:48 +02007268 case ISN_CHECKTYPE:
7269 {
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01007270 checktype_T *ct = &iptr->isn_arg.type;
Bram Moolenaarc6951a72022-12-29 20:56:24 +00007271 char *tofree = NULL;
7272 char *typename;
7273
7274 if (ct->ct_type->tt_type == VAR_FLOAT
7275 && (ct->ct_type->tt_flags & TTFLAG_NUMBER_OK))
7276 typename = "float|number";
7277 else
7278 typename = type_name(ct->ct_type, &tofree);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007279
7280 if (ct->ct_arg_idx == 0)
7281 smsg("%s%4d CHECKTYPE %s stack[%d]", pfx, current,
Bram Moolenaarc6951a72022-12-29 20:56:24 +00007282 typename,
Bram Moolenaar4c137212021-04-19 16:48:48 +02007283 (int)ct->ct_off);
7284 else
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01007285 smsg("%s%4d CHECKTYPE %s stack[%d] %s %d",
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02007286 pfx, current,
Bram Moolenaarc6951a72022-12-29 20:56:24 +00007287 typename,
Bram Moolenaar4c137212021-04-19 16:48:48 +02007288 (int)ct->ct_off,
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01007289 ct->ct_is_var ? "var": "arg",
Bram Moolenaar4c137212021-04-19 16:48:48 +02007290 (int)ct->ct_arg_idx);
7291 vim_free(tofree);
7292 break;
7293 }
7294 case ISN_CHECKLEN: smsg("%s%4d CHECKLEN %s%d", pfx, current,
7295 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
7296 iptr->isn_arg.checklen.cl_min_len);
7297 break;
7298 case ISN_SETTYPE:
7299 {
7300 char *tofree;
7301
7302 smsg("%s%4d SETTYPE %s", pfx, current,
7303 type_name(iptr->isn_arg.type.ct_type, &tofree));
7304 vim_free(tofree);
7305 break;
7306 }
7307 case ISN_COND2BOOL: smsg("%s%4d COND2BOOL", pfx, current); break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007308 case ISN_2BOOL: if (iptr->isn_arg.tobool.invert)
7309 smsg("%s%4d INVERT %d (!val)", pfx, current,
7310 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007311 else
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007312 smsg("%s%4d 2BOOL %d (!!val)", pfx, current,
7313 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007314 break;
7315 case ISN_2STRING: smsg("%s%4d 2STRING stack[%lld]", pfx, current,
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007316 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02007317 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007318 case ISN_2STRING_ANY: smsg("%s%4d 2STRING_ANY stack[%lld]",
7319 pfx, current,
7320 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02007321 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007322 case ISN_RANGE: smsg("%s%4d RANGE %s", pfx, current,
7323 iptr->isn_arg.string);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007324 break;
7325 case ISN_PUT:
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01007326 if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE)
Bram Moolenaar4c137212021-04-19 16:48:48 +02007327 smsg("%s%4d PUT %c above range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007328 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007329 else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE)
7330 smsg("%s%4d PUT %c range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007331 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007332 else
7333 smsg("%s%4d PUT %c %ld", pfx, current,
7334 iptr->isn_arg.put.put_regname,
7335 (long)iptr->isn_arg.put.put_lnum);
7336 break;
7337
Bram Moolenaar4c137212021-04-19 16:48:48 +02007338 case ISN_CMDMOD:
7339 {
7340 char_u *buf;
7341 size_t len = produce_cmdmods(
7342 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
7343
7344 buf = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02007345 if (likely(buf != NULL))
Bram Moolenaar4c137212021-04-19 16:48:48 +02007346 {
7347 (void)produce_cmdmods(
7348 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
7349 smsg("%s%4d CMDMOD %s", pfx, current, buf);
7350 vim_free(buf);
7351 }
7352 break;
7353 }
7354 case ISN_CMDMOD_REV: smsg("%s%4d CMDMOD_REV", pfx, current); break;
7355
7356 case ISN_PROF_START:
Bram Moolenaare99d4222021-06-13 14:01:26 +02007357 smsg("%s%4d PROFILE START line %d", pfx, current,
7358 iptr->isn_lnum);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007359 break;
7360
7361 case ISN_PROF_END:
7362 smsg("%s%4d PROFILE END", pfx, current);
7363 break;
7364
Bram Moolenaare99d4222021-06-13 14:01:26 +02007365 case ISN_DEBUG:
Bram Moolenaar8cec9272021-06-23 20:20:53 +02007366 smsg("%s%4d DEBUG line %d-%d varcount %lld", pfx, current,
7367 iptr->isn_arg.debug.dbg_break_lnum + 1,
7368 iptr->isn_lnum,
7369 iptr->isn_arg.debug.dbg_var_names_len);
Bram Moolenaare99d4222021-06-13 14:01:26 +02007370 break;
7371
Bram Moolenaar4c137212021-04-19 16:48:48 +02007372 case ISN_UNPACK: smsg("%s%4d UNPACK %d%s", pfx, current,
7373 iptr->isn_arg.unpack.unp_count,
7374 iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : "");
7375 break;
7376 case ISN_SHUFFLE: smsg("%s%4d SHUFFLE %d up %d", pfx, current,
7377 iptr->isn_arg.shuffle.shfl_item,
7378 iptr->isn_arg.shuffle.shfl_up);
7379 break;
7380 case ISN_DROP: smsg("%s%4d DROP", pfx, current); break;
7381
7382 case ISN_FINISH: // End of list of instructions for ISN_SUBSTITUTE.
7383 return;
7384 }
7385
7386 out_flush(); // output one line at a time
7387 ui_breakcheck();
7388 if (got_int)
7389 break;
7390 }
7391}
7392
7393/*
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02007394 * Handle command line completion for the :disassemble command.
7395 */
7396 void
7397set_context_in_disassemble_cmd(expand_T *xp, char_u *arg)
7398{
7399 char_u *p;
7400
7401 // Default: expand user functions, "debug" and "profile"
7402 xp->xp_context = EXPAND_DISASSEMBLE;
7403 xp->xp_pattern = arg;
7404
7405 // first argument already typed: only user function names
7406 if (*arg != NUL && *(p = skiptowhite(arg)) != NUL)
7407 {
7408 xp->xp_context = EXPAND_USER_FUNC;
7409 xp->xp_pattern = skipwhite(p);
7410 }
7411}
7412
7413/*
7414 * Function given to ExpandGeneric() to obtain the list of :disassemble
7415 * arguments.
7416 */
7417 char_u *
7418get_disassemble_argument(expand_T *xp, int idx)
7419{
7420 if (idx == 0)
7421 return (char_u *)"debug";
7422 if (idx == 1)
7423 return (char_u *)"profile";
7424 return get_user_func_name(xp, idx - 2);
7425}
7426
7427/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01007428 * ":disassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01007429 * We don't really need this at runtime, but we do have tests that require it,
7430 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007431 */
7432 void
7433ex_disassemble(exarg_T *eap)
7434{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01007435 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01007436 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007437 dfunc_T *dfunc;
Bram Moolenaardafef512022-05-21 21:55:55 +01007438 isn_T *instr = NULL; // init to shut up gcc warning
7439 int instr_count = 0; // init to shut up gcc warning
Bram Moolenaar5a01caa2022-05-21 18:56:58 +01007440 compiletype_T compile_type = CT_NONE;
Bram Moolenaare99d4222021-06-13 14:01:26 +02007441
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01007442 ufunc = find_func_by_name(arg, &compile_type);
Bram Moolenaara26b9702020-04-18 19:53:28 +02007443 if (ufunc == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007444 return;
Bram Moolenaare99d4222021-06-13 14:01:26 +02007445 if (func_needs_compiling(ufunc, compile_type)
7446 && compile_def_function(ufunc, FALSE, compile_type, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02007447 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007448 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007449 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007450 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007451 return;
7452 }
Bram Moolenaar6db660b2021-08-01 14:08:54 +02007453 msg((char *)printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007454
7455 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
Bram Moolenaare99d4222021-06-13 14:01:26 +02007456 switch (compile_type)
7457 {
7458 case CT_PROFILE:
Bram Moolenaarf002a412021-01-24 13:34:18 +01007459#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02007460 instr = dfunc->df_instr_prof;
7461 instr_count = dfunc->df_instr_prof_count;
7462 break;
Bram Moolenaarf002a412021-01-24 13:34:18 +01007463#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02007464 // FALLTHROUGH
7465 case CT_NONE:
7466 instr = dfunc->df_instr;
7467 instr_count = dfunc->df_instr_count;
7468 break;
7469 case CT_DEBUG:
7470 instr = dfunc->df_instr_debug;
7471 instr_count = dfunc->df_instr_debug_count;
7472 break;
7473 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007474
Bram Moolenaar4c137212021-04-19 16:48:48 +02007475 list_instructions("", instr, instr_count, ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007476}
7477
7478/*
Bram Moolenaar13106602020-10-04 16:06:05 +02007479 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007480 * list, etc. Mostly like what JavaScript does, except that empty list and
7481 * empty dictionary are FALSE.
7482 */
7483 int
7484tv2bool(typval_T *tv)
7485{
7486 switch (tv->v_type)
7487 {
7488 case VAR_NUMBER:
7489 return tv->vval.v_number != 0;
7490 case VAR_FLOAT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007491 return tv->vval.v_float != 0.0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007492 case VAR_PARTIAL:
7493 return tv->vval.v_partial != NULL;
7494 case VAR_FUNC:
7495 case VAR_STRING:
7496 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
7497 case VAR_LIST:
7498 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
7499 case VAR_DICT:
7500 return tv->vval.v_dict != NULL
7501 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
7502 case VAR_BOOL:
7503 case VAR_SPECIAL:
7504 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
7505 case VAR_JOB:
7506#ifdef FEAT_JOB_CHANNEL
7507 return tv->vval.v_job != NULL;
7508#else
7509 break;
7510#endif
7511 case VAR_CHANNEL:
7512#ifdef FEAT_JOB_CHANNEL
7513 return tv->vval.v_channel != NULL;
7514#else
7515 break;
7516#endif
7517 case VAR_BLOB:
7518 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
7519 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02007520 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007521 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02007522 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00007523 case VAR_CLASS:
7524 case VAR_OBJECT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007525 break;
7526 }
7527 return FALSE;
7528}
7529
Bram Moolenaarea2d4072020-11-12 12:08:51 +01007530 void
7531emsg_using_string_as(typval_T *tv, int as_number)
7532{
7533 semsg(_(as_number ? e_using_string_as_number_str
7534 : e_using_string_as_bool_str),
7535 tv->vval.v_string == NULL
7536 ? (char_u *)"" : tv->vval.v_string);
7537}
7538
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007539/*
7540 * If "tv" is a string give an error and return FAIL.
7541 */
7542 int
7543check_not_string(typval_T *tv)
7544{
7545 if (tv->v_type == VAR_STRING)
7546 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +01007547 emsg_using_string_as(tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007548 clear_tv(tv);
7549 return FAIL;
7550 }
7551 return OK;
7552}
7553
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007554
7555#endif // FEAT_EVAL