blob: 49042a4afec1532b77e3ba11b6e9cf0d6588a2cb [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
Bram Moolenaar0d89d8a2022-12-31 14:01:24 +0000538 // Check the argument types.
539 if (check_ufunc_arg_types(ufunc, argcount, vararg_count, ectx) == FAIL)
540 return FAIL;
541
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200542 // Reserve space for:
543 // - missing arguments
544 // - stack frame
545 // - local variables
546 // - if needed: a counter for number of closures created in
547 // ectx->ec_funcrefs.
548 varcount = dfunc->df_varcount + dfunc->df_has_closure;
Bram Moolenaarb46c0832022-09-15 17:19:37 +0100549 if (GA_GROW_FAILS(&ectx->ec_stack,
550 arg_to_add + STACK_FRAME_SIZE + varcount))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100551 return FAIL;
552
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100553 // If depth of calling is getting too high, don't execute the function.
554 if (funcdepth_increment() == FAIL)
555 return FAIL;
Bram Moolenaare99d4222021-06-13 14:01:26 +0200556 ++ex_nesting_level;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100557
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100558 // Only make a copy of funclocal if it contains something to restore.
Bram Moolenaar4c137212021-04-19 16:48:48 +0200559 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100560 {
561 floc = ALLOC_ONE(funclocal_T);
562 if (floc == NULL)
563 return FAIL;
Bram Moolenaar4c137212021-04-19 16:48:48 +0200564 *floc = ectx->ec_funclocal;
565 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100566 }
567
Bram Moolenaarfe270812020-04-11 22:31:27 +0200568 // Move the vararg-list to below the missing optional arguments.
569 if (vararg_count > 0 && arg_to_add > 0)
570 *STACK_TV_BOT(arg_to_add - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100571
572 // Reserve space for omitted optional arguments, filled in soon.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200573 for (idx = 0; idx < arg_to_add; ++idx)
Bram Moolenaarfe270812020-04-11 22:31:27 +0200574 STACK_TV_BOT(idx - vararg_count)->v_type = VAR_UNKNOWN;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200575 ectx->ec_stack.ga_len += arg_to_add;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100576
577 // Store current execution state in stack frame for ISN_RETURN.
Bram Moolenaar0186e582021-01-10 18:33:11 +0100578 STACK_TV_BOT(STACK_FRAME_FUNC_OFF)->vval.v_number = ectx->ec_dfunc_idx;
579 STACK_TV_BOT(STACK_FRAME_IIDX_OFF)->vval.v_number = ectx->ec_iidx;
Bram Moolenaar5930ddc2021-04-26 20:32:59 +0200580 STACK_TV_BOT(STACK_FRAME_INSTR_OFF)->vval.v_string = (void *)ectx->ec_instr;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200581 STACK_TV_BOT(STACK_FRAME_OUTER_OFF)->vval.v_string =
582 (void *)ectx->ec_outer_ref;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100583 STACK_TV_BOT(STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string = (void *)floc;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100584 STACK_TV_BOT(STACK_FRAME_IDX_OFF)->vval.v_number = ectx->ec_frame_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200585 ectx->ec_frame_idx = ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100586
Bram Moolenaar5800c792022-09-22 17:34:01 +0100587 // Initialize all local variables to number zero. Also initialize the
588 // variable that counts how many closures were created. This is used in
589 // handle_closure_in_use().
590 int initcount = dfunc->df_varcount + (dfunc->df_has_closure ? 1 : 0);
591 for (idx = 0; idx < initcount; ++idx)
Bram Moolenaar5cd64792021-12-25 18:23:24 +0000592 {
593 typval_T *tv = STACK_TV_BOT(STACK_FRAME_SIZE + idx);
594
595 tv->v_type = VAR_NUMBER;
596 tv->vval.v_number = 0;
597 }
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200598 ectx->ec_stack.ga_len += STACK_FRAME_SIZE + varcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100599
Bram Moolenaar574950d2023-01-03 19:08:50 +0000600 // For an object method move the object from just before the arguments to
601 // the first local variable.
602 if (ufunc->uf_flags & FC_OBJECT)
603 {
604 *STACK_TV_VAR(0) = *obj;
605 obj->v_type = VAR_UNKNOWN;
606 }
607
Bram Moolenaar5800c792022-09-22 17:34:01 +0100608 partial_T *pt = pt_arg != NULL ? pt_arg : ufunc->uf_partial;
609 if (pt != NULL || (ufunc->uf_flags & FC_CLOSURE))
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100610 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200611 outer_ref_T *ref = ALLOC_CLEAR_ONE(outer_ref_T);
Bram Moolenaar0186e582021-01-10 18:33:11 +0100612
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200613 if (ref == NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +0100614 return FAIL;
615 if (pt != NULL)
616 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +0000617 ref->or_outer = get_pt_outer(pt);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200618 ++pt->pt_refcount;
619 ref->or_partial = pt;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100620 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100621 else
622 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200623 ref->or_outer = ALLOC_CLEAR_ONE(outer_T);
Dominique Pelle5a9e5842021-07-24 19:32:12 +0200624 if (unlikely(ref->or_outer == NULL))
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200625 {
626 vim_free(ref);
627 return FAIL;
628 }
629 ref->or_outer_allocated = TRUE;
630 ref->or_outer->out_stack = &ectx->ec_stack;
631 ref->or_outer->out_frame_idx = ectx->ec_frame_idx;
632 if (ectx->ec_outer_ref != NULL)
633 ref->or_outer->out_up = ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100634 }
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200635 ectx->ec_outer_ref = ref;
Bram Moolenaarab360522021-01-10 14:02:28 +0100636 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100637 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200638 ectx->ec_outer_ref = NULL;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100639
Bram Moolenaarc970e422021-03-17 15:03:04 +0100640 ++ufunc->uf_calls;
641
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100642 // Set execution state to the start of the called function.
643 ectx->ec_dfunc_idx = cdf_idx;
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100644 ectx->ec_instr = INSTRUCTIONS(dfunc);
Bram Moolenaarb2049902021-01-24 12:53:53 +0100645 entry = estack_push_ufunc(ufunc, 1);
Bram Moolenaarc620c052020-07-08 15:16:19 +0200646 if (entry != NULL)
647 {
648 // Set the script context to the script where the function was defined.
Bram Moolenaarc70fe462021-04-17 17:59:19 +0200649 // Save the current context so it can be restored on return.
650 entry->es_save_sctx = current_sctx;
651 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200652 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100653
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +0200654 // Start execution at the first instruction.
655 ectx->ec_iidx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100656
657 return OK;
658}
659
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000660// Double linked list of funcstack_T in use.
661static funcstack_T *first_funcstack = NULL;
662
663 static void
664add_funcstack_to_list(funcstack_T *funcstack)
665{
666 // Link in list of funcstacks.
667 if (first_funcstack != NULL)
668 first_funcstack->fs_prev = funcstack;
669 funcstack->fs_next = first_funcstack;
670 funcstack->fs_prev = NULL;
671 first_funcstack = funcstack;
672}
673
674 static void
675remove_funcstack_from_list(funcstack_T *funcstack)
676{
677 if (funcstack->fs_prev == NULL)
678 first_funcstack = funcstack->fs_next;
679 else
680 funcstack->fs_prev->fs_next = funcstack->fs_next;
681 if (funcstack->fs_next != NULL)
682 funcstack->fs_next->fs_prev = funcstack->fs_prev;
683}
684
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100685/*
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200686 * Used when returning from a function: Check if any closure is still
687 * referenced. If so then move the arguments and variables to a separate piece
688 * of stack to be used when the closure is called.
689 * When "free_arguments" is TRUE the arguments are to be freed.
690 * Returns FAIL when out of memory.
691 */
692 static int
693handle_closure_in_use(ectx_T *ectx, int free_arguments)
694{
695 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
696 + ectx->ec_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200697 int argcount;
698 int top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200699 int idx;
700 typval_T *tv;
701 int closure_in_use = FALSE;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200702 garray_T *gap = &ectx->ec_funcrefs;
703 varnumber_T closure_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200704
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200705 if (dfunc->df_ufunc == NULL)
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200706 return OK; // function was freed
707 if (dfunc->df_has_closure == 0)
708 return OK; // no closures
709 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + dfunc->df_varcount);
710 closure_count = tv->vval.v_number;
711 if (closure_count == 0)
712 return OK; // no funcrefs created
713
Bram Moolenaar8fa745e2022-09-16 19:04:24 +0100714 // Compute "top": the first entry in the stack used by the function.
715 // This is the first argument (after that comes the stack frame and then
716 // the local variables).
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200717 argcount = ufunc_argcount(dfunc->df_ufunc);
718 top = ectx->ec_frame_idx - argcount;
719
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200720 // Check if any created closure is still in use.
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200721 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200722 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200723 partial_T *pt;
724 int off = gap->ga_len - closure_count + idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200725
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200726 if (off < 0)
727 continue; // count is off or already done
728 pt = ((partial_T **)gap->ga_data)[off];
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200729 if (pt->pt_refcount > 1)
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200730 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200731 int refcount = pt->pt_refcount;
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200732 int i;
733
Dominique Pelleaf4a61a2021-12-27 17:21:41 +0000734 // A Reference in a local variable doesn't count, it gets
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200735 // unreferenced on return.
736 for (i = 0; i < dfunc->df_varcount; ++i)
737 {
738 typval_T *stv = STACK_TV(ectx->ec_frame_idx
739 + STACK_FRAME_SIZE + i);
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200740 if (stv->v_type == VAR_PARTIAL && pt == stv->vval.v_partial)
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200741 --refcount;
742 }
743 if (refcount > 1)
744 {
745 closure_in_use = TRUE;
746 break;
747 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200748 }
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200749 }
750
751 if (closure_in_use)
752 {
753 funcstack_T *funcstack = ALLOC_CLEAR_ONE(funcstack_T);
754 typval_T *stack;
755
756 // A closure is using the arguments and/or local variables.
757 // Move them to the called function.
758 if (funcstack == NULL)
759 return FAIL;
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000760
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200761 funcstack->fs_var_offset = argcount + STACK_FRAME_SIZE;
Bram Moolenaar1936c762022-09-28 15:19:10 +0100762 funcstack->fs_ga.ga_len = funcstack->fs_var_offset
763 + dfunc->df_varcount;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200764 stack = ALLOC_CLEAR_MULT(typval_T, funcstack->fs_ga.ga_len);
765 funcstack->fs_ga.ga_data = stack;
766 if (stack == NULL)
767 {
768 vim_free(funcstack);
769 return FAIL;
770 }
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000771 add_funcstack_to_list(funcstack);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200772
773 // Move or copy the arguments.
774 for (idx = 0; idx < argcount; ++idx)
775 {
776 tv = STACK_TV(top + idx);
777 if (free_arguments)
778 {
779 *(stack + idx) = *tv;
780 tv->v_type = VAR_UNKNOWN;
781 }
782 else
783 copy_tv(tv, stack + idx);
784 }
Bram Moolenaar8fa745e2022-09-16 19:04:24 +0100785 // Skip the stack frame.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200786 // Move the local variables.
787 for (idx = 0; idx < dfunc->df_varcount; ++idx)
788 {
789 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + idx);
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200790
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200791 // A partial created for a local function, that is also used as a
792 // local variable, has a reference count for the variable, thus
793 // will never go down to zero. When all these refcounts are one
794 // then the funcstack is unused. We need to count how many we have
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000795 // so we know when to check.
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200796 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL)
797 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200798 int i;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200799
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200800 for (i = 0; i < closure_count; ++i)
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200801 if (tv->vval.v_partial == ((partial_T **)gap->ga_data)[
802 gap->ga_len - closure_count + i])
803 ++funcstack->fs_min_refcount;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200804 }
805
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200806 *(stack + funcstack->fs_var_offset + idx) = *tv;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200807 tv->v_type = VAR_UNKNOWN;
808 }
809
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200810 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200811 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200812 partial_T *pt = ((partial_T **)gap->ga_data)[gap->ga_len
813 - closure_count + idx];
814 if (pt->pt_refcount > 1)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200815 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200816 ++funcstack->fs_refcount;
817 pt->pt_funcstack = funcstack;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100818 pt->pt_outer.out_stack = &funcstack->fs_ga;
819 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx - top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200820 }
821 }
822 }
823
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200824 for (idx = 0; idx < closure_count; ++idx)
825 partial_unref(((partial_T **)gap->ga_data)[gap->ga_len
826 - closure_count + idx]);
827 gap->ga_len -= closure_count;
828 if (gap->ga_len == 0)
829 ga_clear(gap);
830
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200831 return OK;
832}
833
834/*
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200835 * Called when a partial is freed or its reference count goes down to one. The
836 * funcstack may be the only reference to the partials in the local variables.
837 * Go over all of them, the funcref and can be freed if all partials
838 * referencing the funcstack have a reference count of one.
Bram Moolenaaracd6b992022-09-17 16:27:39 +0100839 * Returns TRUE if the funcstack is freed, the partial referencing it will then
840 * also have been freed.
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200841 */
Bram Moolenaaracd6b992022-09-17 16:27:39 +0100842 int
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200843funcstack_check_refcount(funcstack_T *funcstack)
844{
Bram Moolenaaracd6b992022-09-17 16:27:39 +0100845 int i;
846 garray_T *gap = &funcstack->fs_ga;
847 int done = 0;
848 typval_T *stack;
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200849
850 if (funcstack->fs_refcount > funcstack->fs_min_refcount)
Bram Moolenaaracd6b992022-09-17 16:27:39 +0100851 return FALSE;
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200852 for (i = funcstack->fs_var_offset; i < gap->ga_len; ++i)
853 {
854 typval_T *tv = ((typval_T *)gap->ga_data) + i;
855
856 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL
857 && tv->vval.v_partial->pt_funcstack == funcstack
858 && tv->vval.v_partial->pt_refcount == 1)
859 ++done;
860 }
Bram Moolenaaracd6b992022-09-17 16:27:39 +0100861 if (done != funcstack->fs_min_refcount)
862 return FALSE;
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200863
Bram Moolenaaracd6b992022-09-17 16:27:39 +0100864 stack = gap->ga_data;
865
866 // All partials referencing the funcstack have a reference count of
867 // one, thus the funcstack is no longer of use.
868 for (i = 0; i < gap->ga_len; ++i)
869 clear_tv(stack + i);
870 vim_free(stack);
871 remove_funcstack_from_list(funcstack);
872 vim_free(funcstack);
873
874 return TRUE;
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200875}
876
877/*
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000878 * For garbage collecting: set references in all variables referenced by
879 * all funcstacks.
880 */
881 int
882set_ref_in_funcstacks(int copyID)
883{
884 funcstack_T *funcstack;
885
886 for (funcstack = first_funcstack; funcstack != NULL;
887 funcstack = funcstack->fs_next)
888 {
889 typval_T *stack = funcstack->fs_ga.ga_data;
890 int i;
891
892 for (i = 0; i < funcstack->fs_ga.ga_len; ++i)
893 if (set_ref_in_item(stack + i, copyID, NULL, NULL))
894 return TRUE; // abort
895 }
896 return FALSE;
897}
898
Bram Moolenaar806a2732022-09-04 15:40:36 +0100899// Ugly static to avoid passing the execution context around through many
900// layers.
901static ectx_T *current_ectx = NULL;
902
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000903/*
Bram Moolenaar806a2732022-09-04 15:40:36 +0100904 * Return TRUE if currently executing a :def function.
905 * Can be used by builtin functions only.
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100906 */
Bram Moolenaar806a2732022-09-04 15:40:36 +0100907 int
908in_def_function(void)
909{
910 return current_ectx != NULL;
911}
912
913/*
Bram Moolenaar98aff652022-09-06 21:02:35 +0100914 * Clear "current_ectx" and return the previous value. To be used when calling
915 * a user function.
916 */
917 ectx_T *
dundargocc57b5bc2022-11-02 13:30:51 +0000918clear_current_ectx(void)
Bram Moolenaar98aff652022-09-06 21:02:35 +0100919{
920 ectx_T *r = current_ectx;
921
922 current_ectx = NULL;
923 return r;
924}
925
926 void
927restore_current_ectx(ectx_T *ectx)
928{
929 if (current_ectx != NULL)
930 iemsg("Restoring current_ectx while it is not NULL");
931 current_ectx = ectx;
932}
933
934/*
Bram Moolenaar806a2732022-09-04 15:40:36 +0100935 * Add an entry for a deferred function call to the currently executing
936 * function.
937 * Return the list or NULL when failed.
938 */
939 static list_T *
940add_defer_item(int var_idx, int argcount, ectx_T *ectx)
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100941{
942 typval_T *defer_tv = STACK_TV_VAR(var_idx);
943 list_T *defer_l;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100944 list_T *l;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100945 typval_T listval;
946
947 if (defer_tv->v_type != VAR_LIST)
948 {
Bram Moolenaara5348f22022-09-04 11:42:22 +0100949 // first time, allocate the list
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100950 if (rettv_list_alloc(defer_tv) == FAIL)
Bram Moolenaar806a2732022-09-04 15:40:36 +0100951 return NULL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100952 }
953 defer_l = defer_tv->vval.v_list;
954
955 l = list_alloc_with_items(argcount + 1);
956 if (l == NULL)
Bram Moolenaar806a2732022-09-04 15:40:36 +0100957 return NULL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100958 listval.v_type = VAR_LIST;
959 listval.vval.v_list = l;
960 listval.v_lock = 0;
Bram Moolenaara5348f22022-09-04 11:42:22 +0100961 if (list_insert_tv(defer_l, &listval, defer_l->lv_first) == FAIL)
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100962 {
963 vim_free(l);
Bram Moolenaar806a2732022-09-04 15:40:36 +0100964 return NULL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100965 }
966
Bram Moolenaar806a2732022-09-04 15:40:36 +0100967 return l;
968}
969
970/*
971 * Handle ISN_DEFER. Stack has a function reference and "argcount" arguments.
972 * The local variable that lists deferred functions is "var_idx".
973 * Returns OK or FAIL.
974 */
975 static int
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +0000976defer_command(int var_idx, int has_obj, int argcount, ectx_T *ectx)
Bram Moolenaar806a2732022-09-04 15:40:36 +0100977{
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +0000978 int obj_off = has_obj ? 1 : 0;
979 list_T *l = add_defer_item(var_idx, argcount + obj_off, ectx);
Bram Moolenaar806a2732022-09-04 15:40:36 +0100980 int i;
981 typval_T *func_tv;
982
983 if (l == NULL)
984 return FAIL;
985
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100986 func_tv = STACK_TV_BOT(-argcount - 1);
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +0000987 if (has_obj ? func_tv->v_type != VAR_PARTIAL : func_tv->v_type != VAR_FUNC)
Bram Moolenaarf5fec052022-09-11 11:49:22 +0100988 {
989 semsg(_(e_expected_str_but_got_str),
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +0000990 has_obj ? "partial" : "function",
Bram Moolenaarf5fec052022-09-11 11:49:22 +0100991 vartype_name(func_tv->v_type));
992 return FAIL;
993 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100994 list_set_item(l, 0, func_tv);
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +0000995 if (has_obj)
996 list_set_item(l, 1, STACK_TV_BOT(-argcount - 2));
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100997
Bram Moolenaarf5fec052022-09-11 11:49:22 +0100998 for (i = 0; i < argcount; ++i)
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +0000999 list_set_item(l, i + 1 + obj_off, STACK_TV_BOT(-argcount + i));
1000 ectx->ec_stack.ga_len -= argcount + 1 + obj_off;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001001 return OK;
1002}
1003
1004/*
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001005 * Add a deferred call for "name" with arguments "argvars[argcount]".
Bram Moolenaar806a2732022-09-04 15:40:36 +01001006 * Consumes "name", also on failure.
1007 * Only to be called when in_def_function() returns TRUE.
1008 */
1009 int
1010add_defer_function(char_u *name, int argcount, typval_T *argvars)
1011{
1012 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1013 + current_ectx->ec_dfunc_idx;
1014 list_T *l;
1015 typval_T func_tv;
1016 int i;
1017
1018 if (dfunc->df_defer_var_idx == 0)
1019 {
1020 iemsg("df_defer_var_idx is zero");
Bram Moolenaar31ea6bf2022-09-05 10:47:13 +01001021 vim_free(name);
Bram Moolenaar806a2732022-09-04 15:40:36 +01001022 return FAIL;
1023 }
Bram Moolenaar806a2732022-09-04 15:40:36 +01001024
Bram Moolenaarf5fec052022-09-11 11:49:22 +01001025 l = add_defer_item(dfunc->df_defer_var_idx - 1, argcount, current_ectx);
Bram Moolenaar806a2732022-09-04 15:40:36 +01001026 if (l == NULL)
1027 {
Bram Moolenaar31ea6bf2022-09-05 10:47:13 +01001028 vim_free(name);
Bram Moolenaar806a2732022-09-04 15:40:36 +01001029 return FAIL;
1030 }
1031
Bram Moolenaar31ea6bf2022-09-05 10:47:13 +01001032 func_tv.v_type = VAR_FUNC;
1033 func_tv.v_lock = 0;
1034 func_tv.vval.v_string = name;
Bram Moolenaar806a2732022-09-04 15:40:36 +01001035 list_set_item(l, 0, &func_tv);
Bram Moolenaar31ea6bf2022-09-05 10:47:13 +01001036
Bram Moolenaar806a2732022-09-04 15:40:36 +01001037 for (i = 0; i < argcount; ++i)
1038 list_set_item(l, i + 1, argvars + i);
1039 return OK;
1040}
1041
1042/*
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001043 * Invoked when returning from a function: Invoke any deferred calls.
1044 */
1045 static void
1046invoke_defer_funcs(ectx_T *ectx)
1047{
1048 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1049 + ectx->ec_dfunc_idx;
1050 typval_T *defer_tv = STACK_TV_VAR(dfunc->df_defer_var_idx - 1);
1051 listitem_T *li;
1052
1053 if (defer_tv->v_type != VAR_LIST)
1054 return; // no function added
1055 for (li = defer_tv->vval.v_list->lv_first; li != NULL; li = li->li_next)
1056 {
1057 list_T *l = li->li_tv.vval.v_list;
1058 typval_T rettv;
1059 typval_T argvars[MAX_FUNC_ARGS];
1060 int i;
1061 listitem_T *arg_li = l->lv_first;
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001062 typval_T *functv = &l->lv_first->li_tv;
1063 int obj_off = functv->v_type == VAR_PARTIAL ? 1 : 0;
1064 int argcount = l->lv_len - 1 - obj_off;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001065
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001066 if (obj_off == 1)
1067 arg_li = arg_li->li_next; // second list item is the object
1068 for (i = 0; i < argcount; ++i)
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001069 {
1070 arg_li = arg_li->li_next;
1071 argvars[i] = arg_li->li_tv;
1072 }
1073
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001074 funcexe_T funcexe;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001075 CLEAR_FIELD(funcexe);
1076 funcexe.fe_evaluate = TRUE;
1077 rettv.v_type = VAR_UNKNOWN;
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00001078 if (functv->v_type == VAR_PARTIAL)
1079 {
1080 funcexe.fe_partial = functv->vval.v_partial;
1081 funcexe.fe_object = l->lv_first->li_next->li_tv.vval.v_object;
1082 if (funcexe.fe_object != NULL)
1083 ++funcexe.fe_object->obj_refcount;
1084 }
1085 (void)call_func(functv->vval.v_string, -1,
1086 &rettv, argcount, argvars, &funcexe);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001087 clear_tv(&rettv);
1088 }
1089}
1090
1091/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001092 * Return from the current function.
1093 */
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001094 static int
Bram Moolenaar4c137212021-04-19 16:48:48 +02001095func_return(ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001096{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001097 int idx;
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01001098 int ret_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001099 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1100 + ectx->ec_dfunc_idx;
1101 int argcount = ufunc_argcount(dfunc->df_ufunc);
Bram Moolenaarc620c052020-07-08 15:16:19 +02001102 estack_T *entry;
Bram Moolenaar12d26532021-02-19 19:13:21 +01001103 int prev_dfunc_idx = STACK_TV(ectx->ec_frame_idx
1104 + STACK_FRAME_FUNC_OFF)->vval.v_number;
Bram Moolenaarb06b50d2021-04-26 21:39:25 +02001105 funclocal_T *floc;
1106#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01001107 dfunc_T *prev_dfunc = ((dfunc_T *)def_functions.ga_data)
1108 + prev_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001109
Bram Moolenaar12d26532021-02-19 19:13:21 +01001110 if (do_profiling == PROF_YES)
1111 {
1112 ufunc_T *caller = prev_dfunc->df_ufunc;
1113
1114 if (dfunc->df_ufunc->uf_profiling
1115 || (caller != NULL && caller->uf_profiling))
1116 {
1117 profile_may_end_func(((profinfo_T *)profile_info_ga.ga_data)
1118 + profile_info_ga.ga_len - 1, dfunc->df_ufunc, caller);
1119 --profile_info_ga.ga_len;
1120 }
1121 }
1122#endif
Bram Moolenaar919c12c2021-12-14 14:29:16 +00001123
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001124 if (dfunc->df_defer_var_idx > 0)
1125 invoke_defer_funcs(ectx);
1126
Bram Moolenaar919c12c2021-12-14 14:29:16 +00001127 // No check for uf_refcount being zero, cannot think of a way that would
1128 // happen.
Bram Moolenaarc970e422021-03-17 15:03:04 +01001129 --dfunc->df_ufunc->uf_calls;
1130
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001131 // execution context goes one level up
Bram Moolenaarc620c052020-07-08 15:16:19 +02001132 entry = estack_pop();
1133 if (entry != NULL)
Bram Moolenaarc70fe462021-04-17 17:59:19 +02001134 current_sctx = entry->es_save_sctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001135
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001136 if (handle_closure_in_use(ectx, TRUE) == FAIL)
1137 return FAIL;
1138
Bram Moolenaar574950d2023-01-03 19:08:50 +00001139 // Clear the arguments. If this was an object method also clear the
1140 // object, it is just before the arguments.
1141 int top = ectx->ec_frame_idx - argcount;
1142 if (dfunc->df_ufunc->uf_flags & FC_OBJECT)
1143 --top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001144 for (idx = top; idx < ectx->ec_frame_idx; ++idx)
1145 clear_tv(STACK_TV(idx));
1146
1147 // Clear local variables and temp values, but not the return value.
1148 for (idx = ectx->ec_frame_idx + STACK_FRAME_SIZE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001149 idx < ectx->ec_stack.ga_len - 1; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001150 clear_tv(STACK_TV(idx));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001151
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01001152 // The return value should be on top of the stack. However, when aborting
1153 // it may not be there and ec_frame_idx is the top of the stack.
1154 ret_idx = ectx->ec_stack.ga_len - 1;
Bram Moolenaar0186e582021-01-10 18:33:11 +01001155 if (ret_idx == ectx->ec_frame_idx + STACK_FRAME_IDX_OFF)
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01001156 ret_idx = 0;
1157
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001158 if (ectx->ec_outer_ref != NULL)
1159 {
1160 if (ectx->ec_outer_ref->or_outer_allocated)
1161 vim_free(ectx->ec_outer_ref->or_outer);
1162 partial_unref(ectx->ec_outer_ref->or_partial);
1163 vim_free(ectx->ec_outer_ref);
1164 }
Bram Moolenaar0186e582021-01-10 18:33:11 +01001165
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001166 // Restore the previous frame.
Bram Moolenaar12d26532021-02-19 19:13:21 +01001167 ectx->ec_dfunc_idx = prev_dfunc_idx;
Bram Moolenaar0186e582021-01-10 18:33:11 +01001168 ectx->ec_iidx = STACK_TV(ectx->ec_frame_idx
1169 + STACK_FRAME_IIDX_OFF)->vval.v_number;
Bram Moolenaar5930ddc2021-04-26 20:32:59 +02001170 ectx->ec_instr = (void *)STACK_TV(ectx->ec_frame_idx
1171 + STACK_FRAME_INSTR_OFF)->vval.v_string;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001172 ectx->ec_outer_ref = (void *)STACK_TV(ectx->ec_frame_idx
Bram Moolenaar0186e582021-01-10 18:33:11 +01001173 + STACK_FRAME_OUTER_OFF)->vval.v_string;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001174 floc = (void *)STACK_TV(ectx->ec_frame_idx
1175 + STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string;
Bram Moolenaar5366e1a2020-10-01 13:01:34 +02001176 // restoring ec_frame_idx must be last
Bram Moolenaar0186e582021-01-10 18:33:11 +01001177 ectx->ec_frame_idx = STACK_TV(ectx->ec_frame_idx
1178 + STACK_FRAME_IDX_OFF)->vval.v_number;
Bram Moolenaard386e922021-04-25 14:48:49 +02001179
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001180 if (floc == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02001181 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001182 else
1183 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02001184 ectx->ec_funclocal = *floc;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001185 vim_free(floc);
1186 }
1187
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01001188 if (ret_idx > 0)
1189 {
1190 // Reset the stack to the position before the call, with a spot for the
1191 // return value, moved there from above the frame.
1192 ectx->ec_stack.ga_len = top + 1;
1193 *STACK_TV_BOT(-1) = *STACK_TV(ret_idx);
1194 }
1195 else
1196 // Reset the stack to the position before the call.
1197 ectx->ec_stack.ga_len = top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001198
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001199 funcdepth_decrement();
Bram Moolenaare99d4222021-06-13 14:01:26 +02001200 --ex_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001201 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001202}
1203
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001204/*
1205 * Prepare arguments and rettv for calling a builtin or user function.
1206 */
1207 static int
1208call_prepare(int argcount, typval_T *argvars, ectx_T *ectx)
1209{
1210 int idx;
1211 typval_T *tv;
1212
1213 // Move arguments from bottom of the stack to argvars[] and add terminator.
1214 for (idx = 0; idx < argcount; ++idx)
1215 argvars[idx] = *STACK_TV_BOT(idx - argcount);
1216 argvars[argcount].v_type = VAR_UNKNOWN;
1217
1218 // Result replaces the arguments on the stack.
1219 if (argcount > 0)
1220 ectx->ec_stack.ga_len -= argcount - 1;
Bram Moolenaar35578162021-08-02 19:10:38 +02001221 else if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001222 return FAIL;
1223 else
1224 ++ectx->ec_stack.ga_len;
1225
1226 // Default return value is zero.
1227 tv = STACK_TV_BOT(-1);
1228 tv->v_type = VAR_NUMBER;
1229 tv->vval.v_number = 0;
Bram Moolenaar24565cf2022-03-28 18:16:52 +01001230 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001231
1232 return OK;
1233}
1234
1235/*
1236 * Call a builtin function by index.
1237 */
1238 static int
1239call_bfunc(int func_idx, int argcount, ectx_T *ectx)
1240{
1241 typval_T argvars[MAX_FUNC_ARGS];
1242 int idx;
Bram Moolenaar171fb922020-10-28 16:54:47 +01001243 int did_emsg_before = did_emsg;
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001244 ectx_T *prev_ectx = current_ectx;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001245 char *save_func_name = ectx->ec_where.wt_func_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001246
1247 if (call_prepare(argcount, argvars, ectx) == FAIL)
1248 return FAIL;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001249 ectx->ec_where.wt_func_name = internal_func_name(func_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001250
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001251 // Call the builtin function. Set "current_ectx" so that when it
1252 // recursively invokes call_def_function() a closure context can be set.
1253 current_ectx = ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001254 call_internal_func_by_idx(func_idx, argvars, STACK_TV_BOT(-1));
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001255 current_ectx = prev_ectx;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001256 ectx->ec_where.wt_func_name = save_func_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001257
1258 // Clear the arguments.
1259 for (idx = 0; idx < argcount; ++idx)
1260 clear_tv(&argvars[idx]);
Bram Moolenaar015f4262020-05-05 21:25:22 +02001261
Bram Moolenaar57f799e2020-12-12 20:42:19 +01001262 if (did_emsg > did_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +02001263 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001264 return OK;
1265}
1266
1267/*
1268 * Execute a user defined function.
Bram Moolenaarab360522021-01-10 14:02:28 +01001269 * If the function is compiled this will add a stack frame and set the
1270 * instruction pointer at the start of the function.
1271 * Otherwise the function is called here.
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001272 * If "pt" is not null use "pt->pt_outer" for ec_outer_ref->or_outer.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001273 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001274 */
1275 static int
Bram Moolenaar0186e582021-01-10 18:33:11 +01001276call_ufunc(
1277 ufunc_T *ufunc,
1278 partial_T *pt,
1279 int argcount,
1280 ectx_T *ectx,
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001281 isn_T *iptr,
1282 dict_T *selfdict)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001283{
1284 typval_T argvars[MAX_FUNC_ARGS];
1285 funcexe_T funcexe;
1286 int error;
1287 int idx;
Bram Moolenaar28ee8922020-10-28 20:20:00 +01001288 int did_emsg_before = did_emsg;
Bram Moolenaar139575d2022-03-15 19:29:30 +00001289 compiletype_T compile_type = get_compile_type(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001290
Bram Moolenaare99d4222021-06-13 14:01:26 +02001291 if (func_needs_compiling(ufunc, compile_type)
1292 && compile_def_function(ufunc, FALSE, compile_type, NULL)
1293 == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02001294 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001295 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001296 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +01001297 error = check_user_func_argcount(ufunc, argcount);
Bram Moolenaar50824712020-12-20 21:10:17 +01001298 if (error != FCERR_UNKNOWN)
1299 {
1300 if (error == FCERR_TOOMANY)
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001301 semsg(_(e_too_many_arguments_for_function_str),
1302 printable_func_name(ufunc));
Bram Moolenaar50824712020-12-20 21:10:17 +01001303 else
Bram Moolenaare1242042021-12-16 20:56:57 +00001304 semsg(_(e_not_enough_arguments_for_function_str),
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001305 printable_func_name(ufunc));
Bram Moolenaar50824712020-12-20 21:10:17 +01001306 return FAIL;
1307 }
1308
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001309 // The function has been compiled, can call it quickly. For a function
1310 // that was defined later: we can call it directly next time.
1311 if (iptr != NULL)
1312 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001313 delete_instr(iptr);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001314 iptr->isn_type = ISN_DCALL;
1315 iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1316 iptr->isn_arg.dfunc.cdf_argcount = argcount;
1317 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001318 return call_dfunc(ufunc->uf_dfunc_idx, pt, argcount, ectx);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001319 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001320
1321 if (call_prepare(argcount, argvars, ectx) == FAIL)
1322 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +02001323 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00001324 funcexe.fe_evaluate = TRUE;
1325 funcexe.fe_selfdict = selfdict != NULL ? selfdict : dict_stack_get_dict();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001326
1327 // Call the user function. Result goes in last position on the stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001328 error = call_user_func_check(ufunc, argcount, argvars,
Bram Moolenaar851f86b2021-12-13 14:26:44 +00001329 STACK_TV_BOT(-1), &funcexe, funcexe.fe_selfdict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001330
1331 // Clear the arguments.
1332 for (idx = 0; idx < argcount; ++idx)
1333 clear_tv(&argvars[idx]);
1334
1335 if (error != FCERR_NONE)
1336 {
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01001337 user_func_error(error, printable_func_name(ufunc),
1338 funcexe.fe_found_var);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001339 return FAIL;
1340 }
Bram Moolenaar28ee8922020-10-28 20:20:00 +01001341 if (did_emsg > did_emsg_before)
Bram Moolenaared677f52020-08-12 16:38:10 +02001342 // Error other than from calling the function itself.
1343 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001344 return OK;
1345}
1346
1347/*
Bram Moolenaara91a7132021-03-25 21:12:15 +01001348 * If command modifiers were applied restore them.
1349 */
1350 static void
1351may_restore_cmdmod(funclocal_T *funclocal)
1352{
1353 if (funclocal->floc_restore_cmdmod)
1354 {
1355 cmdmod.cmod_filter_regmatch.regprog = NULL;
1356 undo_cmdmod(&cmdmod);
1357 cmdmod = funclocal->floc_save_cmdmod;
1358 funclocal->floc_restore_cmdmod = FALSE;
1359 }
1360}
1361
1362/*
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001363 * Return TRUE if an error was given (not caught in try/catch) or CTRL-C was
1364 * pressed.
Bram Moolenaara1773442020-08-12 15:21:22 +02001365 */
1366 static int
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001367vim9_aborting(int prev_uncaught_emsg)
Bram Moolenaara1773442020-08-12 15:21:22 +02001368{
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001369 return uncaught_emsg > prev_uncaught_emsg || got_int || did_throw;
Bram Moolenaara1773442020-08-12 15:21:22 +02001370}
1371
1372/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001373 * Execute a function by "name".
1374 * This can be a builtin function or a user function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001375 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001376 * Returns FAIL if not found without an error message.
1377 */
1378 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001379call_by_name(
1380 char_u *name,
1381 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001382 ectx_T *ectx,
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001383 isn_T *iptr,
1384 dict_T *selfdict)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001385{
1386 ufunc_T *ufunc;
1387
1388 if (builtin_function(name, -1))
1389 {
1390 int func_idx = find_internal_func(name);
1391
Bram Moolenaar1983f1a2022-02-28 20:55:02 +00001392 if (func_idx < 0) // Impossible?
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001393 return FAIL;
Bram Moolenaar389df252020-07-09 21:20:47 +02001394 if (check_internal_func(func_idx, argcount) < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001395 return FAIL;
1396 return call_bfunc(func_idx, argcount, ectx);
1397 }
1398
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00001399 ufunc = find_func(name, FALSE);
Bram Moolenaara1773442020-08-12 15:21:22 +02001400
1401 if (ufunc == NULL)
1402 {
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001403 int prev_uncaught_emsg = uncaught_emsg;
Bram Moolenaara1773442020-08-12 15:21:22 +02001404
1405 if (script_autoload(name, TRUE))
1406 // loaded a package, search for the function again
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00001407 ufunc = find_func(name, FALSE);
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001408
1409 if (vim9_aborting(prev_uncaught_emsg))
Bram Moolenaara1773442020-08-12 15:21:22 +02001410 return FAIL; // bail out if loading the script caused an error
1411 }
1412
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001413 if (ufunc != NULL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001414 {
Bram Moolenaar0d89d8a2022-12-31 14:01:24 +00001415 if (check_ufunc_arg_types(ufunc, argcount, 0, ectx) == FAIL)
1416 return FAIL;
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001417
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001418 return call_ufunc(ufunc, NULL, argcount, ectx, iptr, selfdict);
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001419 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001420
1421 return FAIL;
1422}
1423
1424 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001425call_partial(
1426 typval_T *tv,
1427 int argcount_arg,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001428 ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001429{
Bram Moolenaara90afb92020-07-15 22:38:56 +02001430 int argcount = argcount_arg;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001431 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001432 int called_emsg_before = called_emsg;
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +01001433 int res = FAIL;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001434 dict_T *selfdict = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001435
1436 if (tv->v_type == VAR_PARTIAL)
1437 {
Bram Moolenaara90afb92020-07-15 22:38:56 +02001438 partial_T *pt = tv->vval.v_partial;
1439 int i;
1440
1441 if (pt->pt_argc > 0)
1442 {
1443 // Make space for arguments from the partial, shift the "argcount"
1444 // arguments up.
Bram Moolenaar35578162021-08-02 19:10:38 +02001445 if (GA_GROW_FAILS(&ectx->ec_stack, pt->pt_argc))
Bram Moolenaara90afb92020-07-15 22:38:56 +02001446 return FAIL;
1447 for (i = 1; i <= argcount; ++i)
1448 *STACK_TV_BOT(-i + pt->pt_argc) = *STACK_TV_BOT(-i);
1449 ectx->ec_stack.ga_len += pt->pt_argc;
1450 argcount += pt->pt_argc;
1451
1452 // copy the arguments from the partial onto the stack
1453 for (i = 0; i < pt->pt_argc; ++i)
1454 copy_tv(&pt->pt_argv[i], STACK_TV_BOT(-argcount + i));
1455 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001456 selfdict = pt->pt_dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001457
1458 if (pt->pt_func != NULL)
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001459 return call_ufunc(pt->pt_func, pt, argcount, ectx, NULL, selfdict);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001460
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001461 name = pt->pt_name;
1462 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001463 else if (tv->v_type == VAR_FUNC)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001464 name = tv->vval.v_string;
Bram Moolenaar95006e32020-08-29 17:47:08 +02001465 if (name != NULL)
1466 {
1467 char_u fname_buf[FLEN_FIXED + 1];
1468 char_u *tofree = NULL;
1469 int error = FCERR_NONE;
1470 char_u *fname;
1471
1472 // May need to translate <SNR>123_ to K_SNR.
1473 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
1474 if (error != FCERR_NONE)
1475 res = FAIL;
1476 else
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001477 res = call_by_name(fname, argcount, ectx, NULL, selfdict);
Bram Moolenaar95006e32020-08-29 17:47:08 +02001478 vim_free(tofree);
1479 }
1480
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +01001481 if (res == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001482 {
1483 if (called_emsg == called_emsg_before)
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001484 emsg_funcname(e_unknown_function_str,
Bram Moolenaar015f4262020-05-05 21:25:22 +02001485 name == NULL ? (char_u *)"[unknown]" : name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001486 return FAIL;
1487 }
1488 return OK;
1489}
1490
1491/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001492 * Check if "lock" is VAR_LOCKED or VAR_FIXED. If so give an error and return
1493 * TRUE.
1494 */
1495 static int
1496error_if_locked(int lock, char *error)
1497{
1498 if (lock & (VAR_LOCKED | VAR_FIXED))
1499 {
1500 emsg(_(error));
1501 return TRUE;
1502 }
1503 return FALSE;
1504}
1505
1506/*
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001507 * Give an error if "tv" is not a number and return FAIL.
1508 */
1509 static int
1510check_for_number(typval_T *tv)
1511{
1512 if (tv->v_type != VAR_NUMBER)
1513 {
1514 semsg(_(e_expected_str_but_got_str),
1515 vartype_name(VAR_NUMBER), vartype_name(tv->v_type));
1516 return FAIL;
1517 }
1518 return OK;
1519}
1520
1521/*
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001522 * Store "tv" in variable "name".
1523 * This is for s: and g: variables.
1524 */
1525 static void
1526store_var(char_u *name, typval_T *tv)
1527{
1528 funccal_entry_T entry;
Bram Moolenaard877a572021-04-01 19:42:48 +02001529 int flags = ASSIGN_DECL;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001530
Bram Moolenaard877a572021-04-01 19:42:48 +02001531 if (tv->v_lock)
1532 flags |= ASSIGN_CONST;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001533 save_funccal(&entry);
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001534 set_var_const(name, 0, NULL, tv, FALSE, flags, 0);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001535 restore_funccal();
1536}
1537
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001538/*
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001539 * Convert "tv" to a string.
1540 * Return FAIL if not allowed.
1541 */
1542 static int
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001543do_2string(typval_T *tv, int is_2string_any, int tolerant)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001544{
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00001545 if (tv->v_type == VAR_STRING)
1546 return OK;
1547
1548 char_u *str;
1549
1550 if (is_2string_any)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001551 {
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00001552 switch (tv->v_type)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001553 {
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00001554 case VAR_SPECIAL:
1555 case VAR_BOOL:
1556 case VAR_NUMBER:
1557 case VAR_FLOAT:
1558 case VAR_BLOB: break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001559
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00001560 case VAR_LIST:
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001561 if (tolerant)
1562 {
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001563 char_u *s, *e, *p;
1564 garray_T ga;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001565
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001566 ga_init2(&ga, sizeof(char_u *), 1);
1567
1568 // Convert to NL separated items, then
1569 // escape the items and replace the NL with
1570 // a space.
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001571 str = typval2string(tv, TRUE);
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001572 if (str == NULL)
1573 return FAIL;
1574 s = str;
1575 while ((e = vim_strchr(s, '\n')) != NULL)
1576 {
1577 *e = NUL;
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +01001578 p = vim_strsave_fnameescape(s,
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00001579 VSE_NONE);
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001580 if (p != NULL)
1581 {
1582 ga_concat(&ga, p);
1583 ga_concat(&ga, (char_u *)" ");
1584 vim_free(p);
1585 }
1586 s = e + 1;
1587 }
1588 vim_free(str);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001589 clear_tv(tv);
1590 tv->v_type = VAR_STRING;
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001591 tv->vval.v_string = ga.ga_data;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001592 return OK;
1593 }
1594 // FALLTHROUGH
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00001595 default: to_string_error(tv->v_type);
1596 return FAIL;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001597 }
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001598 }
Yegappan Lakshmanan142ed772023-01-26 12:00:00 +00001599 str = typval_tostring(tv, TRUE);
1600 clear_tv(tv);
1601 tv->v_type = VAR_STRING;
1602 tv->vval.v_string = str;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001603 return OK;
1604}
1605
1606/*
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001607 * When the value of "sv" is a null list of dict, allocate it.
1608 */
1609 static void
Bram Moolenaar859cc212022-03-28 15:22:35 +01001610allocate_if_null(svar_T *sv)
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001611{
Bram Moolenaar859cc212022-03-28 15:22:35 +01001612 typval_T *tv = sv->sv_tv;
1613
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001614 switch (tv->v_type)
1615 {
1616 case VAR_LIST:
Bram Moolenaar859cc212022-03-28 15:22:35 +01001617 if (tv->vval.v_list == NULL && sv->sv_type != &t_list_empty)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001618 (void)rettv_list_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001619 break;
1620 case VAR_DICT:
Bram Moolenaar859cc212022-03-28 15:22:35 +01001621 if (tv->vval.v_dict == NULL && sv->sv_type != &t_dict_empty)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001622 (void)rettv_dict_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001623 break;
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02001624 case VAR_BLOB:
Bram Moolenaar859cc212022-03-28 15:22:35 +01001625 if (tv->vval.v_blob == NULL && sv->sv_type != &t_blob_null)
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02001626 (void)rettv_blob_alloc(tv);
1627 break;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001628 default:
1629 break;
1630 }
1631}
Bram Moolenaard3aac292020-04-19 14:32:17 +02001632
Bram Moolenaare7525c52021-01-09 13:20:37 +01001633/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001634 * Return the character "str[index]" where "index" is the character index,
1635 * including composing characters.
1636 * If "index" is out of range NULL is returned.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001637 */
1638 char_u *
1639char_from_string(char_u *str, varnumber_T index)
1640{
1641 size_t nbyte = 0;
1642 varnumber_T nchar = index;
1643 size_t slen;
1644
1645 if (str == NULL)
1646 return NULL;
1647 slen = STRLEN(str);
1648
Bram Moolenaarff871402021-03-26 13:34:05 +01001649 // Do the same as for a list: a negative index counts from the end.
1650 // Optimization to check the first byte to be below 0x80 (and no composing
1651 // character follows) makes this a lot faster.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001652 if (index < 0)
1653 {
1654 int clen = 0;
1655
1656 for (nbyte = 0; nbyte < slen; ++clen)
Bram Moolenaarff871402021-03-26 13:34:05 +01001657 {
1658 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1659 ++nbyte;
1660 else if (enc_utf8)
1661 nbyte += utfc_ptr2len(str + nbyte);
1662 else
1663 nbyte += mb_ptr2len(str + nbyte);
1664 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001665 nchar = clen + index;
1666 if (nchar < 0)
1667 // unlike list: index out of range results in empty string
1668 return NULL;
1669 }
1670
1671 for (nbyte = 0; nchar > 0 && nbyte < slen; --nchar)
Bram Moolenaarff871402021-03-26 13:34:05 +01001672 {
1673 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1674 ++nbyte;
1675 else if (enc_utf8)
1676 nbyte += utfc_ptr2len(str + nbyte);
1677 else
1678 nbyte += mb_ptr2len(str + nbyte);
1679 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001680 if (nbyte >= slen)
1681 return NULL;
Bram Moolenaar0289a092021-03-14 18:40:19 +01001682 return vim_strnsave(str + nbyte, mb_ptr2len(str + nbyte));
Bram Moolenaare7525c52021-01-09 13:20:37 +01001683}
1684
1685/*
1686 * Get the byte index for character index "idx" in string "str" with length
Bram Moolenaar0289a092021-03-14 18:40:19 +01001687 * "str_len". Composing characters are included.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001688 * If going over the end return "str_len".
1689 * If "idx" is negative count from the end, -1 is the last character.
1690 * When going over the start return -1.
1691 */
1692 static long
1693char_idx2byte(char_u *str, size_t str_len, varnumber_T idx)
1694{
1695 varnumber_T nchar = idx;
1696 size_t nbyte = 0;
1697
1698 if (nchar >= 0)
1699 {
1700 while (nchar > 0 && nbyte < str_len)
1701 {
Bram Moolenaar0289a092021-03-14 18:40:19 +01001702 nbyte += mb_ptr2len(str + nbyte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001703 --nchar;
1704 }
1705 }
1706 else
1707 {
1708 nbyte = str_len;
1709 while (nchar < 0 && nbyte > 0)
1710 {
1711 --nbyte;
1712 nbyte -= mb_head_off(str, str + nbyte);
1713 ++nchar;
1714 }
1715 if (nchar < 0)
1716 return -1;
1717 }
1718 return (long)nbyte;
1719}
1720
1721/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001722 * Return the slice "str[first : last]" using character indexes. Composing
1723 * characters are included.
Bram Moolenaar6601b622021-01-13 21:47:15 +01001724 * "exclusive" is TRUE for slice().
Bram Moolenaare7525c52021-01-09 13:20:37 +01001725 * Return NULL when the result is empty.
1726 */
1727 char_u *
Bram Moolenaar6601b622021-01-13 21:47:15 +01001728string_slice(char_u *str, varnumber_T first, varnumber_T last, int exclusive)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001729{
1730 long start_byte, end_byte;
1731 size_t slen;
1732
1733 if (str == NULL)
1734 return NULL;
1735 slen = STRLEN(str);
1736 start_byte = char_idx2byte(str, slen, first);
1737 if (start_byte < 0)
1738 start_byte = 0; // first index very negative: use zero
Bram Moolenaar6601b622021-01-13 21:47:15 +01001739 if ((last == -1 && !exclusive) || last == VARNUM_MAX)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001740 end_byte = (long)slen;
1741 else
1742 {
1743 end_byte = char_idx2byte(str, slen, last);
Bram Moolenaar6601b622021-01-13 21:47:15 +01001744 if (!exclusive && end_byte >= 0 && end_byte < (long)slen)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001745 // end index is inclusive
Bram Moolenaar0289a092021-03-14 18:40:19 +01001746 end_byte += mb_ptr2len(str + end_byte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001747 }
1748
1749 if (start_byte >= (long)slen || end_byte <= start_byte)
1750 return NULL;
1751 return vim_strnsave(str + start_byte, end_byte - start_byte);
1752}
1753
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001754/*
1755 * Get a script variable for ISN_STORESCRIPT and ISN_LOADSCRIPT.
1756 * When "dfunc_idx" is negative don't give an error.
1757 * Returns NULL for an error.
1758 */
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001759 static svar_T *
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001760get_script_svar(scriptref_T *sref, int dfunc_idx)
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001761{
1762 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001763 dfunc_T *dfunc = dfunc_idx < 0 ? NULL
1764 : ((dfunc_T *)def_functions.ga_data) + dfunc_idx;
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001765 svar_T *sv;
1766
1767 if (sref->sref_seq != si->sn_script_seq)
1768 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001769 // The script was reloaded after the function was compiled, the
1770 // script_idx may not be valid.
1771 if (dfunc != NULL)
1772 semsg(_(e_script_variable_invalid_after_reload_in_function_str),
1773 printable_func_name(dfunc->df_ufunc));
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001774 return NULL;
1775 }
1776 sv = ((svar_T *)si->sn_var_vals.ga_data) + sref->sref_idx;
Bram Moolenaar6de22962022-09-09 21:35:36 +01001777 if (sv->sv_name == NULL)
1778 {
1779 if (dfunc != NULL)
1780 emsg(_(e_script_variable_was_deleted));
1781 return NULL;
1782 }
Bram Moolenaar60dc8272021-07-29 22:48:54 +02001783 if (!equal_type(sv->sv_type, sref->sref_type, 0))
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001784 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001785 if (dfunc != NULL)
1786 emsg(_(e_script_variable_type_changed));
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001787 return NULL;
1788 }
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001789
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01001790 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0
1791 && sref->sref_sid != current_sctx.sc_sid)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001792 {
1793 if (dfunc != NULL)
1794 semsg(_(e_item_not_exported_in_script_str), sv->sv_name);
1795 return NULL;
1796 }
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001797 return sv;
1798}
1799
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001800/*
Bram Moolenaar20677332021-06-06 17:02:53 +02001801 * Function passed to do_cmdline() for splitting a script joined by NL
1802 * characters.
1803 */
1804 static char_u *
1805get_split_sourceline(
1806 int c UNUSED,
1807 void *cookie,
1808 int indent UNUSED,
1809 getline_opt_T options UNUSED)
1810{
1811 source_cookie_T *sp = (source_cookie_T *)cookie;
1812 char_u *p;
1813 char_u *line;
1814
Bram Moolenaar20677332021-06-06 17:02:53 +02001815 p = vim_strchr(sp->nextline, '\n');
1816 if (p == NULL)
1817 {
1818 line = vim_strsave(sp->nextline);
1819 sp->nextline += STRLEN(sp->nextline);
1820 }
1821 else
1822 {
1823 line = vim_strnsave(sp->nextline, p - sp->nextline);
1824 sp->nextline = p + 1;
1825 }
1826 return line;
1827}
1828
1829/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001830 * Execute a function by "name".
1831 * This can be a builtin function, user function or a funcref.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001832 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001833 */
1834 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001835call_eval_func(
1836 char_u *name,
1837 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001838 ectx_T *ectx,
1839 isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001840{
Bram Moolenaared677f52020-08-12 16:38:10 +02001841 int called_emsg_before = called_emsg;
1842 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001843
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001844 res = call_by_name(name, argcount, ectx, iptr, NULL);
Bram Moolenaared677f52020-08-12 16:38:10 +02001845 if (res == FAIL && called_emsg == called_emsg_before)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001846 {
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001847 dictitem_T *v;
1848
1849 v = find_var(name, NULL, FALSE);
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001850 if (v == NULL || (v->di_tv.v_type != VAR_PARTIAL
1851 && v->di_tv.v_type != VAR_FUNC))
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001852 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001853 emsg_funcname(e_unknown_function_str, name);
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001854 return FAIL;
1855 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001856 return call_partial(&v->di_tv, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001857 }
Bram Moolenaared677f52020-08-12 16:38:10 +02001858 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001859}
1860
1861/*
Bram Moolenaarf112f302020-12-20 17:47:52 +01001862 * When a function reference is used, fill a partial with the information
1863 * needed, especially when it is used as a closure.
1864 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001865 int
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001866fill_partial_and_closure(
Bram Moolenaarcc341812022-09-19 15:54:34 +01001867 partial_T *pt,
1868 ufunc_T *ufunc,
1869 loopvarinfo_T *lvi,
1870 ectx_T *ectx)
Bram Moolenaarf112f302020-12-20 17:47:52 +01001871{
1872 pt->pt_func = ufunc;
1873 pt->pt_refcount = 1;
1874
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001875 if (ufunc->uf_flags & FC_CLOSURE)
Bram Moolenaarf112f302020-12-20 17:47:52 +01001876 {
1877 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1878 + ectx->ec_dfunc_idx;
1879
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001880 // The closure may need to find arguments and local variables of the
1881 // current function in the stack.
Bram Moolenaar0186e582021-01-10 18:33:11 +01001882 pt->pt_outer.out_stack = &ectx->ec_stack;
1883 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001884 if (ectx->ec_outer_ref != NULL)
1885 {
1886 // The current context already has a context, link to that one.
1887 pt->pt_outer.out_up = ectx->ec_outer_ref->or_outer;
1888 if (ectx->ec_outer_ref->or_partial != NULL)
1889 {
1890 pt->pt_outer.out_up_partial = ectx->ec_outer_ref->or_partial;
1891 ++pt->pt_outer.out_up_partial->pt_refcount;
1892 }
1893 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01001894
Bram Moolenaarcc341812022-09-19 15:54:34 +01001895 if (lvi != NULL)
1896 {
1897 int depth;
1898
1899 // The closure may need to find variables defined inside a loop,
1900 // for every nested loop. A new reference is made every time,
1901 // ISN_ENDLOOP will check if they are actually used.
1902 for (depth = 0; depth < lvi->lvi_depth; ++depth)
1903 {
1904 pt->pt_outer.out_loop[depth].stack = &ectx->ec_stack;
1905 pt->pt_outer.out_loop[depth].var_idx = ectx->ec_frame_idx
1906 + STACK_FRAME_SIZE + lvi->lvi_loop[depth].var_idx;
1907 pt->pt_outer.out_loop[depth].var_count =
1908 lvi->lvi_loop[depth].var_count;
1909 }
Bram Moolenaar6d313be2022-09-22 16:36:25 +01001910 pt->pt_outer.out_loop_size = lvi->lvi_depth;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001911 }
Bram Moolenaar6d313be2022-09-22 16:36:25 +01001912 else
1913 pt->pt_outer.out_loop_size = 0;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001914
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001915 // If the function currently executing returns and the closure is still
1916 // being referenced, we need to make a copy of the context (arguments
1917 // and local variables) so that the closure can use it later.
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001918 // Store a reference to the partial so we can handle that.
Bram Moolenaar35578162021-08-02 19:10:38 +02001919 if (GA_GROW_FAILS(&ectx->ec_funcrefs, 1))
Bram Moolenaarf112f302020-12-20 17:47:52 +01001920 {
1921 vim_free(pt);
1922 return FAIL;
1923 }
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001924 // Extra variable keeps the count of closures created in the current
1925 // function call.
Bram Moolenaarf112f302020-12-20 17:47:52 +01001926 ++(((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx
1927 + STACK_FRAME_SIZE + dfunc->df_varcount)->vval.v_number;
1928
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001929 ((partial_T **)ectx->ec_funcrefs.ga_data)[ectx->ec_funcrefs.ga_len]
1930 = pt;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001931 ++pt->pt_refcount;
1932 ++ectx->ec_funcrefs.ga_len;
1933 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001934 ++ufunc->uf_refcount;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001935 return OK;
1936}
1937
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001938/*
1939 * Execute iptr->isn_arg.string as an Ex command.
1940 */
1941 static int
1942exec_command(isn_T *iptr)
1943{
1944 source_cookie_T cookie;
1945
1946 SOURCING_LNUM = iptr->isn_lnum;
1947 // Pass getsourceline to get an error for a missing ":end"
1948 // command.
1949 CLEAR_FIELD(cookie);
1950 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1951 if (do_cmdline(iptr->isn_arg.string,
1952 getsourceline, &cookie,
1953 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED) == FAIL
1954 || did_emsg)
1955 return FAIL;
1956 return OK;
1957}
1958
Bram Moolenaar89445512022-04-14 12:58:23 +01001959/*
1960 * If script "sid" is not loaded yet then load it now.
1961 * Caller must make sure "sid" is a valid script ID.
1962 * "loaded" is set to TRUE if the script had to be loaded.
1963 * Returns FAIL if loading fails, OK if already loaded or loaded now.
1964 */
1965 int
1966may_load_script(int sid, int *loaded)
1967{
1968 scriptitem_T *si = SCRIPT_ITEM(sid);
1969
1970 if (si->sn_state == SN_STATE_NOT_LOADED)
1971 {
1972 *loaded = TRUE;
1973 if (do_source(si->sn_name, FALSE, DOSO_NONE, NULL) == FAIL)
1974 {
1975 semsg(_(e_cant_open_file_str), si->sn_name);
1976 return FAIL;
1977 }
1978 }
1979 return OK;
1980}
1981
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001982// used for v_instr of typval of VAR_INSTR
1983struct instr_S {
1984 ectx_T *instr_ectx;
1985 isn_T *instr_instr;
1986};
1987
Bram Moolenaar4c137212021-04-19 16:48:48 +02001988// used for substitute_instr
1989typedef struct subs_expr_S {
1990 ectx_T *subs_ectx;
1991 isn_T *subs_instr;
1992 int subs_status;
1993} subs_expr_T;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001994
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001995// Set when calling do_debug().
1996static ectx_T *debug_context = NULL;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001997static int debug_var_count;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001998
1999/*
2000 * When debugging lookup "name" and return the typeval.
2001 * When not found return NULL.
2002 */
2003 typval_T *
2004lookup_debug_var(char_u *name)
2005{
2006 int idx;
2007 dfunc_T *dfunc;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02002008 ufunc_T *ufunc;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002009 ectx_T *ectx = debug_context;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02002010 int varargs_off;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002011
2012 if (ectx == NULL)
2013 return NULL;
2014 dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
2015
2016 // Go through the local variable names, from last to first.
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02002017 for (idx = debug_var_count - 1; idx >= 0; --idx)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002018 {
Bram Moolenaare406ff82022-03-10 20:47:43 +00002019 char_u *varname = ((char_u **)dfunc->df_var_names.ga_data)[idx];
2020
2021 // the variable name may be NULL when not available in this block
2022 if (varname != NULL && STRCMP(varname, name) == 0)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002023 return STACK_TV_VAR(idx);
2024 }
2025
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02002026 // Go through argument names.
2027 ufunc = dfunc->df_ufunc;
2028 varargs_off = ufunc->uf_va_name == NULL ? 0 : 1;
2029 for (idx = 0; idx < ufunc->uf_args.ga_len; ++idx)
2030 if (STRCMP(((char_u **)(ufunc->uf_args.ga_data))[idx], name) == 0)
2031 return STACK_TV(ectx->ec_frame_idx - ufunc->uf_args.ga_len
2032 - varargs_off + idx);
2033 if (ufunc->uf_va_name != NULL && STRCMP(ufunc->uf_va_name, name) == 0)
2034 return STACK_TV(ectx->ec_frame_idx - 1);
2035
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002036 return NULL;
2037}
2038
Bram Moolenaar26a44842021-09-02 18:49:06 +02002039/*
2040 * Return TRUE if there might be a breakpoint in "ufunc", which is when a
2041 * breakpoint was set in that function or when there is any expression.
2042 */
2043 int
2044may_break_in_function(ufunc_T *ufunc)
2045{
2046 return ufunc->uf_has_breakpoint || debug_has_expr_breakpoint();
2047}
2048
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002049 static void
2050handle_debug(isn_T *iptr, ectx_T *ectx)
2051{
2052 char_u *line;
2053 ufunc_T *ufunc = (((dfunc_T *)def_functions.ga_data)
2054 + ectx->ec_dfunc_idx)->df_ufunc;
2055 isn_T *ni;
2056 int end_lnum = iptr->isn_lnum;
2057 garray_T ga;
2058 int lnum;
2059
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02002060 if (ex_nesting_level > debug_break_level)
2061 {
2062 linenr_T breakpoint;
2063
Bram Moolenaar26a44842021-09-02 18:49:06 +02002064 if (!may_break_in_function(ufunc))
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02002065 return;
2066
2067 // check for the next breakpoint if needed
2068 breakpoint = dbg_find_breakpoint(FALSE, ufunc->uf_name,
Bram Moolenaar8cec9272021-06-23 20:20:53 +02002069 iptr->isn_arg.debug.dbg_break_lnum);
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02002070 if (breakpoint <= 0 || breakpoint > iptr->isn_lnum)
2071 return;
2072 }
2073
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002074 SOURCING_LNUM = iptr->isn_lnum;
2075 debug_context = ectx;
Bram Moolenaar8cec9272021-06-23 20:20:53 +02002076 debug_var_count = iptr->isn_arg.debug.dbg_var_names_len;
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002077
2078 for (ni = iptr + 1; ni->isn_type != ISN_FINISH; ++ni)
2079 if (ni->isn_type == ISN_DEBUG
2080 || ni->isn_type == ISN_RETURN
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002081 || ni->isn_type == ISN_RETURN_OBJECT
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002082 || ni->isn_type == ISN_RETURN_VOID)
2083 {
Bram Moolenaar112bed02021-11-23 22:16:34 +00002084 end_lnum = ni->isn_lnum + (ni->isn_type == ISN_DEBUG ? 0 : 1);
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002085 break;
2086 }
2087
2088 if (end_lnum > iptr->isn_lnum)
2089 {
2090 ga_init2(&ga, sizeof(char_u *), 10);
Bram Moolenaar310091d2021-12-23 21:14:37 +00002091 for (lnum = iptr->isn_lnum; lnum < end_lnum
2092 && lnum <= ufunc->uf_lines.ga_len; ++lnum)
Bram Moolenaar59b50c32021-06-17 22:27:48 +02002093 {
Bram Moolenaar303215d2021-07-07 20:10:43 +02002094 char_u *p = ((char_u **)ufunc->uf_lines.ga_data)[lnum - 1];
Bram Moolenaar59b50c32021-06-17 22:27:48 +02002095
Bram Moolenaar303215d2021-07-07 20:10:43 +02002096 if (p == NULL)
2097 continue; // left over from continuation line
2098 p = skipwhite(p);
Bram Moolenaar59b50c32021-06-17 22:27:48 +02002099 if (*p == '#')
2100 break;
Bram Moolenaar35578162021-08-02 19:10:38 +02002101 if (GA_GROW_OK(&ga, 1))
Bram Moolenaar59b50c32021-06-17 22:27:48 +02002102 ((char_u **)(ga.ga_data))[ga.ga_len++] = p;
2103 if (STRNCMP(p, "def ", 4) == 0)
2104 break;
2105 }
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002106 line = ga_concat_strings(&ga, " ");
2107 vim_free(ga.ga_data);
2108 }
2109 else
2110 line = ((char_u **)ufunc->uf_lines.ga_data)[iptr->isn_lnum - 1];
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002111
Dominique Pelle6e969552021-06-17 13:53:41 +02002112 do_debug(line == NULL ? (char_u *)"[empty]" : line);
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002113 debug_context = NULL;
2114
2115 if (end_lnum > iptr->isn_lnum)
2116 vim_free(line);
2117}
2118
Bram Moolenaar4c137212021-04-19 16:48:48 +02002119/*
Bram Moolenaar65b0d162022-12-13 18:43:22 +00002120 * Store a value in a list, dict, blob or object variable.
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002121 * Returns OK, FAIL or NOTDONE (uncatchable error).
2122 */
2123 static int
2124execute_storeindex(isn_T *iptr, ectx_T *ectx)
2125{
Bram Moolenaarf7d1c6e2023-01-16 20:47:57 +00002126 vartype_T dest_type = iptr->isn_arg.storeindex.si_vartype;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002127 typval_T *tv;
2128 typval_T *tv_idx = STACK_TV_BOT(-2);
2129 typval_T *tv_dest = STACK_TV_BOT(-1);
2130 int status = OK;
2131
2132 // Stack contains:
2133 // -3 value to be stored
2134 // -2 index
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002135 // -1 dict, list, blob or object
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002136 tv = STACK_TV_BOT(-3);
2137 SOURCING_LNUM = iptr->isn_lnum;
2138 if (dest_type == VAR_ANY)
2139 {
2140 dest_type = tv_dest->v_type;
2141 if (dest_type == VAR_DICT)
2142 status = do_2string(tv_idx, TRUE, FALSE);
2143 else if (dest_type == VAR_LIST && tv_idx->v_type != VAR_NUMBER)
2144 {
2145 emsg(_(e_number_expected));
2146 status = FAIL;
2147 }
2148 }
Bram Moolenaare08be092022-02-17 13:08:26 +00002149
2150 if (status == OK)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002151 {
Bram Moolenaare08be092022-02-17 13:08:26 +00002152 if (dest_type == VAR_LIST)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002153 {
Bram Moolenaare08be092022-02-17 13:08:26 +00002154 long lidx = (long)tv_idx->vval.v_number;
2155 list_T *list = tv_dest->vval.v_list;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002156
Bram Moolenaare08be092022-02-17 13:08:26 +00002157 if (list == NULL)
2158 {
2159 emsg(_(e_list_not_set));
2160 return FAIL;
2161 }
2162 if (lidx < 0 && list->lv_len + lidx >= 0)
2163 // negative index is relative to the end
2164 lidx = list->lv_len + lidx;
2165 if (lidx < 0 || lidx > list->lv_len)
2166 {
2167 semsg(_(e_list_index_out_of_range_nr), lidx);
2168 return FAIL;
2169 }
2170 if (lidx < list->lv_len)
2171 {
2172 listitem_T *li = list_find(list, lidx);
2173
2174 if (error_if_locked(li->li_tv.v_lock,
Bram Moolenaar70c43d82022-01-26 21:01:15 +00002175 e_cannot_change_locked_list_item))
Bram Moolenaare08be092022-02-17 13:08:26 +00002176 return FAIL;
2177 // overwrite existing list item
2178 clear_tv(&li->li_tv);
2179 li->li_tv = *tv;
2180 }
2181 else
2182 {
2183 if (error_if_locked(list->lv_lock, e_cannot_change_locked_list))
2184 return FAIL;
2185 // append to list, only fails when out of memory
2186 if (list_append_tv(list, tv) == FAIL)
2187 return NOTDONE;
2188 clear_tv(tv);
2189 }
2190 }
2191 else if (dest_type == VAR_DICT)
2192 {
2193 char_u *key = tv_idx->vval.v_string;
2194 dict_T *dict = tv_dest->vval.v_dict;
2195 dictitem_T *di;
2196
2197 SOURCING_LNUM = iptr->isn_lnum;
2198 if (dict == NULL)
2199 {
2200 emsg(_(e_dictionary_not_set));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002201 return FAIL;
Bram Moolenaare08be092022-02-17 13:08:26 +00002202 }
2203 if (key == NULL)
2204 key = (char_u *)"";
2205 di = dict_find(dict, key, -1);
2206 if (di != NULL)
2207 {
2208 if (error_if_locked(di->di_tv.v_lock,
2209 e_cannot_change_dict_item))
2210 return FAIL;
2211 // overwrite existing value
2212 clear_tv(&di->di_tv);
2213 di->di_tv = *tv;
2214 }
2215 else
2216 {
2217 if (error_if_locked(dict->dv_lock, e_cannot_change_dict))
2218 return FAIL;
2219 // add to dict, only fails when out of memory
2220 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
2221 return NOTDONE;
2222 clear_tv(tv);
2223 }
2224 }
2225 else if (dest_type == VAR_BLOB)
2226 {
2227 long lidx = (long)tv_idx->vval.v_number;
2228 blob_T *blob = tv_dest->vval.v_blob;
Bram Moolenaar65b0d162022-12-13 18:43:22 +00002229 varnumber_T nr;
2230 int error = FALSE;
2231 int len;
Bram Moolenaare08be092022-02-17 13:08:26 +00002232
2233 if (blob == NULL)
2234 {
2235 emsg(_(e_blob_not_set));
2236 return FAIL;
2237 }
2238 len = blob_len(blob);
2239 if (lidx < 0 && len + lidx >= 0)
2240 // negative index is relative to the end
2241 lidx = len + lidx;
2242
2243 // Can add one byte at the end.
2244 if (lidx < 0 || lidx > len)
2245 {
2246 semsg(_(e_blob_index_out_of_range_nr), lidx);
2247 return FAIL;
2248 }
2249 if (value_check_lock(blob->bv_lock, (char_u *)"blob", FALSE))
2250 return FAIL;
2251 nr = tv_get_number_chk(tv, &error);
2252 if (error)
2253 return FAIL;
2254 blob_set_append(blob, lidx, nr);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002255 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002256 else if (dest_type == VAR_CLASS || dest_type == VAR_OBJECT)
2257 {
2258 long idx = (long)tv_idx->vval.v_number;
2259 object_T *obj = tv_dest->vval.v_object;
2260 typval_T *otv = (typval_T *)(obj + 1);
Bram Moolenaarf7d1c6e2023-01-16 20:47:57 +00002261
2262 class_T *itf = iptr->isn_arg.storeindex.si_class;
2263 if (itf != NULL)
2264 // convert interface member index to class member index
Bram Moolenaard0200c82023-01-28 15:19:40 +00002265 idx = object_index_from_itf_index(itf, FALSE,
2266 idx, obj->obj_class);
Bram Moolenaarf7d1c6e2023-01-16 20:47:57 +00002267
Bram Moolenaar65b0d162022-12-13 18:43:22 +00002268 clear_tv(&otv[idx]);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002269 otv[idx] = *tv;
2270 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002271 else
2272 {
Bram Moolenaare08be092022-02-17 13:08:26 +00002273 status = FAIL;
2274 semsg(_(e_cannot_index_str), vartype_name(dest_type));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002275 }
2276 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002277
2278 clear_tv(tv_idx);
2279 clear_tv(tv_dest);
2280 ectx->ec_stack.ga_len -= 3;
2281 if (status == FAIL)
2282 {
2283 clear_tv(tv);
2284 return FAIL;
2285 }
2286 return OK;
2287}
2288
2289/*
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002290 * Store a value in a list or blob range.
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002291 */
2292 static int
2293execute_storerange(isn_T *iptr, ectx_T *ectx)
2294{
2295 typval_T *tv;
2296 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2297 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2298 typval_T *tv_dest = STACK_TV_BOT(-1);
2299 int status = OK;
2300
2301 // Stack contains:
2302 // -4 value to be stored
2303 // -3 first index or "none"
2304 // -2 second index or "none"
2305 // -1 destination list or blob
2306 tv = STACK_TV_BOT(-4);
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002307 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002308 if (tv_dest->v_type == VAR_LIST)
2309 {
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002310 long n1;
2311 long n2;
2312 listitem_T *li1;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002313
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002314 n1 = (long)tv_get_number_chk(tv_idx1, NULL);
2315 if (tv_idx2->v_type == VAR_SPECIAL
2316 && tv_idx2->vval.v_number == VVAL_NONE)
2317 n2 = list_len(tv_dest->vval.v_list) - 1;
2318 else
2319 n2 = (long)tv_get_number_chk(tv_idx2, NULL);
2320
Bram Moolenaar22ebd172022-04-01 15:26:58 +01002321 li1 = check_range_index_one(tv_dest->vval.v_list, &n1, TRUE, FALSE);
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002322 if (li1 == NULL)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002323 status = FAIL;
2324 else
2325 {
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002326 status = check_range_index_two(tv_dest->vval.v_list,
2327 &n1, li1, &n2, FALSE);
2328 if (status != FAIL)
2329 status = list_assign_range(
2330 tv_dest->vval.v_list,
2331 tv->vval.v_list,
2332 n1,
2333 n2,
2334 tv_idx2->v_type == VAR_SPECIAL,
2335 (char_u *)"=",
2336 (char_u *)"[unknown]");
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002337 }
2338 }
2339 else if (tv_dest->v_type == VAR_BLOB)
2340 {
2341 varnumber_T n1;
2342 varnumber_T n2;
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002343 long bloblen;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002344
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002345 n1 = tv_get_number_chk(tv_idx1, NULL);
2346 if (tv_idx2->v_type == VAR_SPECIAL
2347 && tv_idx2->vval.v_number == VVAL_NONE)
2348 n2 = blob_len(tv_dest->vval.v_blob) - 1;
2349 else
2350 n2 = tv_get_number_chk(tv_idx2, NULL);
2351 bloblen = blob_len(tv_dest->vval.v_blob);
2352
2353 if (check_blob_index(bloblen, n1, FALSE) == FAIL
2354 || check_blob_range(bloblen, n1, n2, FALSE) == FAIL)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002355 status = FAIL;
2356 else
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002357 status = blob_set_range(tv_dest->vval.v_blob, n1, n2, tv);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002358 }
2359 else
2360 {
2361 status = FAIL;
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002362 emsg(_(e_list_or_blob_required));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002363 }
2364
2365 clear_tv(tv_idx1);
2366 clear_tv(tv_idx2);
2367 clear_tv(tv_dest);
2368 ectx->ec_stack.ga_len -= 4;
2369 clear_tv(tv);
2370
2371 return status;
2372}
2373
2374/*
2375 * Unlet item in list or dict variable.
2376 */
2377 static int
2378execute_unletindex(isn_T *iptr, ectx_T *ectx)
2379{
2380 typval_T *tv_idx = STACK_TV_BOT(-2);
2381 typval_T *tv_dest = STACK_TV_BOT(-1);
2382 int status = OK;
2383
2384 // Stack contains:
2385 // -2 index
2386 // -1 dict or list
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002387 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002388 if (tv_dest->v_type == VAR_DICT)
2389 {
2390 // unlet a dict item, index must be a string
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002391 if (tv_idx->v_type != VAR_STRING && tv_idx->v_type != VAR_NUMBER)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002392 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002393 semsg(_(e_expected_str_but_got_str),
2394 vartype_name(VAR_STRING),
2395 vartype_name(tv_idx->v_type));
2396 status = FAIL;
2397 }
2398 else
2399 {
2400 dict_T *d = tv_dest->vval.v_dict;
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002401 char_u *key;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002402 dictitem_T *di = NULL;
2403
2404 if (d != NULL && value_check_lock(
2405 d->dv_lock, NULL, FALSE))
2406 status = FAIL;
2407 else
2408 {
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002409 if (tv_idx->v_type == VAR_STRING)
2410 {
2411 key = tv_idx->vval.v_string;
2412 if (key == NULL)
2413 key = (char_u *)"";
2414 }
2415 else
2416 {
2417 key = tv_get_string(tv_idx);
2418 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002419 if (d != NULL)
2420 di = dict_find(d, key, (int)STRLEN(key));
2421 if (di == NULL)
2422 {
2423 // NULL dict is equivalent to empty dict
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00002424 semsg(_(e_key_not_present_in_dictionary_str), key);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002425 status = FAIL;
2426 }
2427 else if (var_check_fixed(di->di_flags,
2428 NULL, FALSE)
2429 || var_check_ro(di->di_flags,
2430 NULL, FALSE))
2431 status = FAIL;
2432 else
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002433 dictitem_remove(d, di, "unlet");
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002434 }
2435 }
2436 }
2437 else if (tv_dest->v_type == VAR_LIST)
2438 {
2439 // unlet a List item, index must be a number
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002440 if (check_for_number(tv_idx) == FAIL)
2441 {
2442 status = FAIL;
2443 }
2444 else
2445 {
2446 list_T *l = tv_dest->vval.v_list;
2447 long n = (long)tv_idx->vval.v_number;
2448
2449 if (l != NULL && value_check_lock(
2450 l->lv_lock, NULL, FALSE))
2451 status = FAIL;
2452 else
2453 {
2454 listitem_T *li = list_find(l, n);
2455
2456 if (li == NULL)
2457 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002458 semsg(_(e_list_index_out_of_range_nr), n);
2459 status = FAIL;
2460 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002461 else
2462 listitem_remove(l, li);
2463 }
2464 }
2465 }
2466 else
2467 {
2468 status = FAIL;
2469 semsg(_(e_cannot_index_str),
2470 vartype_name(tv_dest->v_type));
2471 }
2472
2473 clear_tv(tv_idx);
2474 clear_tv(tv_dest);
2475 ectx->ec_stack.ga_len -= 2;
2476
2477 return status;
2478}
2479
2480/*
2481 * Unlet a range of items in a list variable.
2482 */
2483 static int
2484execute_unletrange(isn_T *iptr, ectx_T *ectx)
2485{
2486 // Stack contains:
2487 // -3 index1
2488 // -2 index2
2489 // -1 dict or list
2490 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2491 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2492 typval_T *tv_dest = STACK_TV_BOT(-1);
2493 int status = OK;
2494
2495 if (tv_dest->v_type == VAR_LIST)
2496 {
2497 // indexes must be a number
2498 SOURCING_LNUM = iptr->isn_lnum;
2499 if (check_for_number(tv_idx1) == FAIL
2500 || (tv_idx2->v_type != VAR_SPECIAL
2501 && check_for_number(tv_idx2) == FAIL))
2502 {
2503 status = FAIL;
2504 }
2505 else
2506 {
2507 list_T *l = tv_dest->vval.v_list;
2508 long n1 = (long)tv_idx1->vval.v_number;
2509 long n2 = tv_idx2->v_type == VAR_SPECIAL
2510 ? 0 : (long)tv_idx2->vval.v_number;
2511 listitem_T *li;
2512
2513 li = list_find_index(l, &n1);
2514 if (li == NULL)
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002515 {
2516 semsg(_(e_list_index_out_of_range_nr),
2517 (long)tv_idx1->vval.v_number);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002518 status = FAIL;
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002519 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002520 else
2521 {
2522 if (n1 < 0)
2523 n1 = list_idx_of_item(l, li);
2524 if (n2 < 0)
2525 {
2526 listitem_T *li2 = list_find(l, n2);
2527
2528 if (li2 == NULL)
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002529 {
2530 semsg(_(e_list_index_out_of_range_nr), n2);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002531 status = FAIL;
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002532 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002533 else
2534 n2 = list_idx_of_item(l, li2);
2535 }
2536 if (status != FAIL
2537 && tv_idx2->v_type != VAR_SPECIAL
2538 && n2 < n1)
2539 {
2540 semsg(_(e_list_index_out_of_range_nr), n2);
2541 status = FAIL;
2542 }
Bram Moolenaar6b8c7ba2022-03-20 17:46:06 +00002543 if (status != FAIL)
2544 list_unlet_range(l, li, n1,
2545 tv_idx2->v_type != VAR_SPECIAL, n2);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002546 }
2547 }
2548 }
2549 else
2550 {
2551 status = FAIL;
2552 SOURCING_LNUM = iptr->isn_lnum;
2553 semsg(_(e_cannot_index_str),
2554 vartype_name(tv_dest->v_type));
2555 }
2556
2557 clear_tv(tv_idx1);
2558 clear_tv(tv_idx2);
2559 clear_tv(tv_dest);
2560 ectx->ec_stack.ga_len -= 3;
2561
2562 return status;
2563}
2564
2565/*
2566 * Top of a for loop.
2567 */
2568 static int
2569execute_for(isn_T *iptr, ectx_T *ectx)
2570{
2571 typval_T *tv;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002572 int jump = FALSE;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002573 typval_T *ltv = STACK_TV_BOT(-1);
2574 typval_T *idxtv =
Bram Moolenaarcc341812022-09-19 15:54:34 +01002575 STACK_TV_VAR(iptr->isn_arg.forloop.for_loop_idx);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002576
2577 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
2578 return FAIL;
2579 if (ltv->v_type == VAR_LIST)
2580 {
2581 list_T *list = ltv->vval.v_list;
2582
2583 // push the next item from the list
2584 ++idxtv->vval.v_number;
2585 if (list == NULL
2586 || idxtv->vval.v_number >= list->lv_len)
2587 {
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002588 jump = TRUE;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002589 }
2590 else if (list->lv_first == &range_list_item)
2591 {
2592 // non-materialized range() list
2593 tv = STACK_TV_BOT(0);
2594 tv->v_type = VAR_NUMBER;
2595 tv->v_lock = 0;
2596 tv->vval.v_number = list_find_nr(
2597 list, idxtv->vval.v_number, NULL);
2598 ++ectx->ec_stack.ga_len;
2599 }
2600 else
2601 {
2602 listitem_T *li = list_find(list,
2603 idxtv->vval.v_number);
2604
2605 copy_tv(&li->li_tv, STACK_TV_BOT(0));
2606 ++ectx->ec_stack.ga_len;
2607 }
2608 }
2609 else if (ltv->v_type == VAR_STRING)
2610 {
2611 char_u *str = ltv->vval.v_string;
2612
2613 // The index is for the last byte of the previous
2614 // character.
2615 ++idxtv->vval.v_number;
2616 if (str == NULL || str[idxtv->vval.v_number] == NUL)
2617 {
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002618 jump = TRUE;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002619 }
2620 else
2621 {
2622 int clen = mb_ptr2len(str + idxtv->vval.v_number);
2623
2624 // Push the next character from the string.
2625 tv = STACK_TV_BOT(0);
2626 tv->v_type = VAR_STRING;
2627 tv->vval.v_string = vim_strnsave(
2628 str + idxtv->vval.v_number, clen);
2629 ++ectx->ec_stack.ga_len;
2630 idxtv->vval.v_number += clen - 1;
2631 }
2632 }
2633 else if (ltv->v_type == VAR_BLOB)
2634 {
2635 blob_T *blob = ltv->vval.v_blob;
2636
2637 // When we get here the first time make a copy of the
2638 // blob, so that the iteration still works when it is
2639 // changed.
2640 if (idxtv->vval.v_number == -1 && blob != NULL)
2641 {
2642 blob_copy(blob, ltv);
2643 blob_unref(blob);
2644 blob = ltv->vval.v_blob;
2645 }
2646
2647 // The index is for the previous byte.
2648 ++idxtv->vval.v_number;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002649 if (blob == NULL || idxtv->vval.v_number >= blob_len(blob))
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002650 {
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002651 jump = TRUE;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002652 }
2653 else
2654 {
2655 // Push the next byte from the blob.
2656 tv = STACK_TV_BOT(0);
2657 tv->v_type = VAR_NUMBER;
2658 tv->vval.v_number = blob_get(blob,
2659 idxtv->vval.v_number);
2660 ++ectx->ec_stack.ga_len;
2661 }
2662 }
2663 else
2664 {
2665 semsg(_(e_for_loop_on_str_not_supported),
2666 vartype_name(ltv->v_type));
2667 return FAIL;
2668 }
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002669
2670 if (jump)
2671 {
2672 // past the end of the list/string/blob, jump to "endfor"
2673 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
2674 may_restore_cmdmod(&ectx->ec_funclocal);
2675 }
2676 else
2677 {
2678 // Store the current number of funcrefs, this may be used in
2679 // ISN_LOOPEND. The variable index is always one more than the loop
2680 // variable index.
Bram Moolenaarcc341812022-09-19 15:54:34 +01002681 tv = STACK_TV_VAR(iptr->isn_arg.forloop.for_loop_idx + 1);
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002682 tv->vval.v_number = ectx->ec_funcrefs.ga_len;
2683 }
2684
2685 return OK;
2686}
2687
2688/*
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002689 * Code for handling variables declared inside a loop and used in a closure.
2690 * This is very similar to what is done with funcstack_T. The difference is
2691 * that the funcstack_T has the scope of a function, while a loopvars_T has the
2692 * scope of the block inside a loop and each loop may have its own.
2693 */
2694
2695// Double linked list of loopvars_T in use.
2696static loopvars_T *first_loopvars = NULL;
2697
2698 static void
2699add_loopvars_to_list(loopvars_T *loopvars)
2700{
2701 // Link in list of loopvarss.
2702 if (first_loopvars != NULL)
2703 first_loopvars->lvs_prev = loopvars;
2704 loopvars->lvs_next = first_loopvars;
2705 loopvars->lvs_prev = NULL;
2706 first_loopvars = loopvars;
2707}
2708
2709 static void
2710remove_loopvars_from_list(loopvars_T *loopvars)
2711{
2712 if (loopvars->lvs_prev == NULL)
2713 first_loopvars = loopvars->lvs_next;
2714 else
2715 loopvars->lvs_prev->lvs_next = loopvars->lvs_next;
2716 if (loopvars->lvs_next != NULL)
2717 loopvars->lvs_next->lvs_prev = loopvars->lvs_prev;
2718}
2719
2720/*
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002721 * End of a for or while loop: Handle any variables used by a closure.
2722 */
2723 static int
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002724execute_endloop(isn_T *iptr, ectx_T *ectx)
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002725{
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002726 endloop_T *endloop = &iptr->isn_arg.endloop;
2727 typval_T *tv_refcount = STACK_TV_VAR(endloop->end_funcref_idx);
2728 int prev_closure_count = tv_refcount->vval.v_number;
Bram Moolenaarcc341812022-09-19 15:54:34 +01002729 int depth = endloop->end_depth;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002730 garray_T *gap = &ectx->ec_funcrefs;
2731 int closure_in_use = FALSE;
2732 loopvars_T *loopvars;
2733 typval_T *stack;
2734 int idx;
2735
Bram Moolenaarcc341812022-09-19 15:54:34 +01002736 // Check if any created closure is still being referenced and loopvars have
2737 // not been saved yet for the current depth.
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002738 for (idx = prev_closure_count; idx < gap->ga_len; ++idx)
2739 {
2740 partial_T *pt = ((partial_T **)gap->ga_data)[idx];
2741
Bram Moolenaarcc341812022-09-19 15:54:34 +01002742 if (pt->pt_refcount > 1 && pt->pt_loopvars[depth] == NULL)
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002743 {
2744 int refcount = pt->pt_refcount;
2745 int i;
2746
2747 // A Reference in a variable inside the loop doesn't count, it gets
2748 // unreferenced at the end of the loop.
2749 for (i = 0; i < endloop->end_var_count; ++i)
2750 {
2751 typval_T *stv = STACK_TV_VAR(endloop->end_var_idx + i);
2752
2753 if (stv->v_type == VAR_PARTIAL && pt == stv->vval.v_partial)
2754 --refcount;
2755 }
2756 if (refcount > 1)
2757 {
2758 closure_in_use = TRUE;
2759 break;
2760 }
2761 }
2762 }
2763
2764 // If no function reference were created since the start of the loop block
2765 // or it is no longer referenced there is nothing to do.
2766 if (!closure_in_use)
2767 return OK;
2768
2769 // A closure is using variables declared inside the loop.
2770 // Move them to the called function.
2771 loopvars = ALLOC_CLEAR_ONE(loopvars_T);
2772 if (loopvars == NULL)
2773 return FAIL;
2774
2775 loopvars->lvs_ga.ga_len = endloop->end_var_count;
2776 stack = ALLOC_CLEAR_MULT(typval_T, loopvars->lvs_ga.ga_len);
2777 loopvars->lvs_ga.ga_data = stack;
2778 if (stack == NULL)
2779 {
2780 vim_free(loopvars);
2781 return FAIL;
2782 }
2783 add_loopvars_to_list(loopvars);
2784
2785 // Move the variable values.
2786 for (idx = 0; idx < endloop->end_var_count; ++idx)
2787 {
2788 typval_T *tv = STACK_TV_VAR(endloop->end_var_idx + idx);
2789
2790 *(stack + idx) = *tv;
2791 tv->v_type = VAR_UNKNOWN;
2792 }
2793
2794 for (idx = prev_closure_count; idx < gap->ga_len; ++idx)
2795 {
2796 partial_T *pt = ((partial_T **)gap->ga_data)[idx];
2797
Bram Moolenaarcc341812022-09-19 15:54:34 +01002798 if (pt->pt_refcount > 1 && pt->pt_loopvars[depth] == NULL)
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002799 {
2800 ++loopvars->lvs_refcount;
Bram Moolenaarcc341812022-09-19 15:54:34 +01002801 pt->pt_loopvars[depth] = loopvars;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002802
Bram Moolenaarcc341812022-09-19 15:54:34 +01002803 pt->pt_outer.out_loop[depth].stack = &loopvars->lvs_ga;
2804 pt->pt_outer.out_loop[depth].var_idx -=
2805 ectx->ec_frame_idx + STACK_FRAME_SIZE + endloop->end_var_idx;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002806 }
2807 }
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002808
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002809 return OK;
2810}
2811
2812/*
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002813 * Called when a partial is freed or its reference count goes down to one. The
2814 * loopvars may be the only reference to the partials in the local variables.
2815 * Go over all of them, the funcref and can be freed if all partials
2816 * referencing the loopvars have a reference count of one.
Bram Moolenaarcc341812022-09-19 15:54:34 +01002817 * Return TRUE if it was freed.
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002818 */
Bram Moolenaarcc341812022-09-19 15:54:34 +01002819 int
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002820loopvars_check_refcount(loopvars_T *loopvars)
2821{
2822 int i;
2823 garray_T *gap = &loopvars->lvs_ga;
2824 int done = 0;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002825 typval_T *stack = gap->ga_data;
2826
Bram Moolenaarcc341812022-09-19 15:54:34 +01002827 if (loopvars->lvs_refcount > loopvars->lvs_min_refcount)
2828 return FALSE;
2829 for (i = 0; i < gap->ga_len; ++i)
2830 {
2831 typval_T *tv = ((typval_T *)gap->ga_data) + i;
2832
2833 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL
2834 && tv->vval.v_partial->pt_refcount == 1)
2835 {
2836 int depth;
2837
2838 for (depth = 0; depth < MAX_LOOP_DEPTH; ++depth)
2839 if (tv->vval.v_partial->pt_loopvars[depth] == loopvars)
2840 ++done;
2841 }
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002842 }
Bram Moolenaarcc341812022-09-19 15:54:34 +01002843 if (done != loopvars->lvs_min_refcount)
2844 return FALSE;
2845
2846 // All partials referencing the loopvars have a reference count of
2847 // one, thus the loopvars is no longer of use.
2848 stack = gap->ga_data;
2849 for (i = 0; i < gap->ga_len; ++i)
2850 clear_tv(stack + i);
2851 vim_free(stack);
2852 remove_loopvars_from_list(loopvars);
2853 vim_free(loopvars);
2854 return TRUE;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002855}
2856
2857/*
2858 * For garbage collecting: set references in all variables referenced by
2859 * all loopvars.
2860 */
2861 int
2862set_ref_in_loopvars(int copyID)
2863{
2864 loopvars_T *loopvars;
2865
2866 for (loopvars = first_loopvars; loopvars != NULL;
2867 loopvars = loopvars->lvs_next)
2868 {
2869 typval_T *stack = loopvars->lvs_ga.ga_data;
2870 int i;
2871
2872 for (i = 0; i < loopvars->lvs_ga.ga_len; ++i)
2873 if (set_ref_in_item(stack + i, copyID, NULL, NULL))
2874 return TRUE; // abort
2875 }
2876 return FALSE;
2877}
2878
2879/*
Bram Moolenaar06b77222022-01-25 15:51:56 +00002880 * Load instruction for w:/b:/g:/t: variable.
2881 * "isn_type" is used instead of "iptr->isn_type".
2882 */
2883 static int
2884load_namespace_var(ectx_T *ectx, isntype_T isn_type, isn_T *iptr)
2885{
2886 dictitem_T *di = NULL;
2887 hashtab_T *ht = NULL;
2888 char namespace;
2889
2890 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
2891 return NOTDONE;
2892 switch (isn_type)
2893 {
2894 case ISN_LOADG:
2895 ht = get_globvar_ht();
2896 namespace = 'g';
2897 break;
2898 case ISN_LOADB:
2899 ht = &curbuf->b_vars->dv_hashtab;
2900 namespace = 'b';
2901 break;
2902 case ISN_LOADW:
2903 ht = &curwin->w_vars->dv_hashtab;
2904 namespace = 'w';
2905 break;
2906 case ISN_LOADT:
2907 ht = &curtab->tp_vars->dv_hashtab;
2908 namespace = 't';
2909 break;
2910 default: // Cannot reach here
2911 return NOTDONE;
2912 }
2913 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
2914
Bram Moolenaar06b77222022-01-25 15:51:56 +00002915 if (di == NULL)
2916 {
Bram Moolenaarfe732552022-02-22 19:39:13 +00002917 if (isn_type == ISN_LOADG)
2918 {
2919 ufunc_T *ufunc = find_func(iptr->isn_arg.string, TRUE);
2920
2921 // g:Something could be a function
2922 if (ufunc != NULL)
2923 {
2924 typval_T *tv = STACK_TV_BOT(0);
2925
2926 ++ectx->ec_stack.ga_len;
2927 tv->v_type = VAR_FUNC;
2928 tv->vval.v_string = alloc(STRLEN(iptr->isn_arg.string) + 3);
2929 if (tv->vval.v_string == NULL)
2930 return FAIL;
2931 STRCPY(tv->vval.v_string, "g:");
2932 STRCPY(tv->vval.v_string + 2, iptr->isn_arg.string);
2933 return OK;
2934 }
2935 }
Bram Moolenaar06b77222022-01-25 15:51:56 +00002936 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarfe732552022-02-22 19:39:13 +00002937 if (vim_strchr(iptr->isn_arg.string, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar06b77222022-01-25 15:51:56 +00002938 // no check if the item exists in the script but
2939 // isn't exported, it is too complicated
Bram Moolenaarfe732552022-02-22 19:39:13 +00002940 semsg(_(e_item_not_found_in_script_str), iptr->isn_arg.string);
Bram Moolenaar06b77222022-01-25 15:51:56 +00002941 else
2942 semsg(_(e_undefined_variable_char_str),
Bram Moolenaarfe732552022-02-22 19:39:13 +00002943 namespace, iptr->isn_arg.string);
Bram Moolenaar06b77222022-01-25 15:51:56 +00002944 return FAIL;
2945 }
2946 else
2947 {
2948 copy_tv(&di->di_tv, STACK_TV_BOT(0));
2949 ++ectx->ec_stack.ga_len;
2950 }
2951 return OK;
2952}
2953
Bram Moolenaard0200c82023-01-28 15:19:40 +00002954
2955 static void
2956object_required_error(typval_T *tv)
2957{
2958 garray_T type_list;
2959 ga_init2(&type_list, sizeof(type_T *), 10);
2960 type_T *type = typval2type(tv, get_copyID(), &type_list, TVTT_DO_MEMBER);
2961 char *tofree = NULL;
2962 char *typename = type_name(type, &tofree);
2963 semsg(_(e_object_required_found_str), typename);
2964 vim_free(tofree);
2965 clear_type_list(&type_list);
2966}
2967
Bram Moolenaar06b77222022-01-25 15:51:56 +00002968/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02002969 * Execute instructions in execution context "ectx".
2970 * Return OK or FAIL;
2971 */
2972 static int
2973exec_instructions(ectx_T *ectx)
2974{
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002975 int ret = FAIL;
2976 int save_trylevel_at_start = ectx->ec_trylevel_at_start;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02002977 int dict_stack_len_at_start = dict_stack.ga_len;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01002978
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02002979 // Start execution at the first instruction.
Bram Moolenaar4c137212021-04-19 16:48:48 +02002980 ectx->ec_iidx = 0;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01002981
Bram Moolenaarff652882021-05-16 15:24:49 +02002982 // Only catch exceptions in this instruction list.
2983 ectx->ec_trylevel_at_start = trylevel;
2984
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002985 for (;;)
2986 {
Bram Moolenaara7490192021-07-22 12:26:14 +02002987 static int breakcheck_count = 0; // using "static" makes it faster
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002988 isn_T *iptr;
Bram Moolenaara7490192021-07-22 12:26:14 +02002989 typval_T *tv;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002990
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002991 if (unlikely(++breakcheck_count >= 100))
Bram Moolenaar270d0382020-05-15 21:42:53 +02002992 {
2993 line_breakcheck();
2994 breakcheck_count = 0;
2995 }
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002996 if (unlikely(got_int))
Bram Moolenaar20431c92020-03-20 18:39:46 +01002997 {
2998 // Turn CTRL-C into an exception.
2999 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +01003000 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003001 goto theend;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003002 did_throw = TRUE;
3003 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003004
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003005 if (unlikely(did_emsg && msg_list != NULL && *msg_list != NULL))
Bram Moolenaara26b9702020-04-18 19:53:28 +02003006 {
3007 // Turn an error message into an exception.
3008 did_emsg = FALSE;
3009 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003010 goto theend;
Bram Moolenaara26b9702020-04-18 19:53:28 +02003011 did_throw = TRUE;
3012 *msg_list = NULL;
3013 }
3014
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003015 if (unlikely(did_throw))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003016 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003017 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003018 trycmd_T *trycmd = NULL;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003019 int index = trystack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003020
3021 // An exception jumps to the first catch, finally, or returns from
3022 // the current function.
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003023 while (index > 0)
3024 {
3025 trycmd = ((trycmd_T *)trystack->ga_data) + index - 1;
Bram Moolenaar834193a2021-06-30 20:39:15 +02003026 if (!trycmd->tcd_in_catch || trycmd->tcd_finally_idx != 0)
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003027 break;
3028 // In the catch and finally block of this try we have to go up
3029 // one level.
3030 --index;
3031 trycmd = NULL;
3032 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003033 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003034 {
Bram Moolenaar834193a2021-06-30 20:39:15 +02003035 if (trycmd->tcd_in_catch)
3036 {
3037 // exception inside ":catch", jump to ":finally" once
3038 ectx->ec_iidx = trycmd->tcd_finally_idx;
3039 trycmd->tcd_finally_idx = 0;
3040 }
3041 else
3042 // jump to first ":catch"
3043 ectx->ec_iidx = trycmd->tcd_catch_idx;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003044 trycmd->tcd_in_catch = TRUE;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003045 did_throw = FALSE; // don't come back here until :endtry
3046 trycmd->tcd_did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003047 }
3048 else
3049 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02003050 // Not inside try or need to return from current functions.
3051 // Push a dummy return value.
Bram Moolenaar35578162021-08-02 19:10:38 +02003052 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003053 goto theend;
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02003054 tv = STACK_TV_BOT(0);
3055 tv->v_type = VAR_NUMBER;
3056 tv->vval.v_number = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003057 ++ectx->ec_stack.ga_len;
3058 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003059 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02003060 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +01003061 need_rethrow = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003062 if (handle_closure_in_use(ectx, FALSE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003063 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003064 goto done;
3065 }
3066
Bram Moolenaar4c137212021-04-19 16:48:48 +02003067 if (func_return(ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003068 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003069 }
3070 continue;
3071 }
3072
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003073 /*
3074 * Big switch on the instruction. Most compilers will be turning this
3075 * into an efficient lookup table, since the "case" values are an enum
3076 * with sequential numbers. It may look ugly, but it should be the
3077 * most efficient way.
3078 */
Bram Moolenaar4c137212021-04-19 16:48:48 +02003079 iptr = &ectx->ec_instr[ectx->ec_iidx++];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003080 switch (iptr->isn_type)
3081 {
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00003082 // Constructor, first instruction in a new() method.
Bram Moolenaar00b28d62022-12-08 15:32:33 +00003083 case ISN_CONSTRUCT:
3084 // "this" is always the local variable at index zero
3085 tv = STACK_TV_VAR(0);
3086 tv->v_type = VAR_OBJECT;
3087 tv->vval.v_object = alloc_clear(
3088 iptr->isn_arg.construct.construct_size);
3089 tv->vval.v_object->obj_class =
3090 iptr->isn_arg.construct.construct_class;
Bram Moolenaard28d7b92022-12-08 20:42:00 +00003091 ++tv->vval.v_object->obj_class->class_refcount;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00003092 tv->vval.v_object->obj_refcount = 1;
Bram Moolenaard28d7b92022-12-08 20:42:00 +00003093 object_created(tv->vval.v_object);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00003094 break;
3095
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003096 // execute Ex command line
3097 case ISN_EXEC:
Bram Moolenaaraacc9662021-08-13 19:40:51 +02003098 if (exec_command(iptr) == FAIL)
3099 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003100 break;
3101
Bram Moolenaar20677332021-06-06 17:02:53 +02003102 // execute Ex command line split at NL characters.
3103 case ISN_EXEC_SPLIT:
3104 {
3105 source_cookie_T cookie;
Bram Moolenaar518df272021-06-06 17:34:13 +02003106 char_u *line;
Bram Moolenaar20677332021-06-06 17:02:53 +02003107
3108 SOURCING_LNUM = iptr->isn_lnum;
3109 CLEAR_FIELD(cookie);
3110 cookie.sourcing_lnum = iptr->isn_lnum - 1;
3111 cookie.nextline = iptr->isn_arg.string;
Bram Moolenaar518df272021-06-06 17:34:13 +02003112 line = get_split_sourceline(0, &cookie, 0, 0);
3113 if (do_cmdline(line,
Bram Moolenaar20677332021-06-06 17:02:53 +02003114 get_split_sourceline, &cookie,
3115 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED)
3116 == FAIL
3117 || did_emsg)
Bram Moolenaar518df272021-06-06 17:34:13 +02003118 {
3119 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02003120 goto on_error;
Bram Moolenaar518df272021-06-06 17:34:13 +02003121 }
3122 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02003123 }
3124 break;
3125
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00003126 // execute Ex command line that is only a range
3127 case ISN_EXECRANGE:
3128 {
3129 exarg_T ea;
3130 char *error = NULL;
3131
3132 CLEAR_FIELD(ea);
3133 ea.cmdidx = CMD_SIZE;
3134 ea.addr_type = ADDR_LINES;
3135 ea.cmd = iptr->isn_arg.string;
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00003136 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00003137 parse_cmd_address(&ea, &error, FALSE);
Bram Moolenaar01a4dcb2021-12-04 13:15:10 +00003138 if (ea.cmd == NULL)
3139 goto on_error;
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00003140 // error is always NULL when using ADDR_LINES
3141 error = ex_range_without_command(&ea);
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00003142 if (error != NULL)
3143 {
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00003144 emsg(error);
3145 goto on_error;
3146 }
3147 }
3148 break;
3149
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02003150 // Evaluate an expression with legacy syntax, push it onto the
3151 // stack.
3152 case ISN_LEGACY_EVAL:
3153 {
3154 char_u *arg = iptr->isn_arg.string;
3155 int res;
3156 int save_flags = cmdmod.cmod_flags;
3157
Bram Moolenaar35578162021-08-02 19:10:38 +02003158 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003159 goto theend;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02003160 tv = STACK_TV_BOT(0);
3161 init_tv(tv);
3162 cmdmod.cmod_flags |= CMOD_LEGACY;
3163 res = eval0(arg, tv, NULL, &EVALARG_EVALUATE);
3164 cmdmod.cmod_flags = save_flags;
3165 if (res == FAIL)
3166 goto on_error;
3167 ++ectx->ec_stack.ga_len;
3168 }
3169 break;
3170
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003171 // push typeval VAR_INSTR with instructions to be executed
3172 case ISN_INSTR:
3173 {
Bram Moolenaar35578162021-08-02 19:10:38 +02003174 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003175 goto theend;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003176 tv = STACK_TV_BOT(0);
3177 tv->vval.v_instr = ALLOC_ONE(instr_T);
3178 if (tv->vval.v_instr == NULL)
3179 goto on_error;
3180 ++ectx->ec_stack.ga_len;
3181
3182 tv->v_type = VAR_INSTR;
3183 tv->vval.v_instr->instr_ectx = ectx;
3184 tv->vval.v_instr->instr_instr = iptr->isn_arg.instr;
3185 }
3186 break;
3187
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003188 case ISN_SOURCE:
3189 {
Bram Moolenaar89445512022-04-14 12:58:23 +01003190 int notused;
LemonBoy77142312022-04-15 20:50:46 +01003191
Bram Moolenaar89445512022-04-14 12:58:23 +01003192 SOURCING_LNUM = iptr->isn_lnum;
3193 if (may_load_script((int)iptr->isn_arg.number, &notused)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003194 == FAIL)
Bram Moolenaar89445512022-04-14 12:58:23 +01003195 goto on_error;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003196 }
3197 break;
3198
Bram Moolenaar4c137212021-04-19 16:48:48 +02003199 // execute :substitute with an expression
3200 case ISN_SUBSTITUTE:
3201 {
3202 subs_T *subs = &iptr->isn_arg.subs;
3203 source_cookie_T cookie;
3204 struct subs_expr_S *save_instr = substitute_instr;
3205 struct subs_expr_S subs_instr;
3206 int res;
3207
3208 subs_instr.subs_ectx = ectx;
3209 subs_instr.subs_instr = subs->subs_instr;
3210 subs_instr.subs_status = OK;
3211 substitute_instr = &subs_instr;
3212
3213 SOURCING_LNUM = iptr->isn_lnum;
3214 // This is very much like ISN_EXEC
3215 CLEAR_FIELD(cookie);
3216 cookie.sourcing_lnum = iptr->isn_lnum - 1;
3217 res = do_cmdline(subs->subs_cmd,
3218 getsourceline, &cookie,
3219 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
3220 substitute_instr = save_instr;
3221
3222 if (res == FAIL || did_emsg
3223 || subs_instr.subs_status == FAIL)
3224 goto on_error;
3225 }
3226 break;
3227
3228 case ISN_FINISH:
3229 goto done;
3230
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02003231 case ISN_REDIRSTART:
3232 // create a dummy entry for var_redir_str()
3233 if (alloc_redir_lval() == FAIL)
3234 goto on_error;
3235
3236 // The output is stored in growarray "redir_ga" until
3237 // redirection ends.
3238 init_redir_ga();
3239 redir_vname = 1;
3240 break;
3241
3242 case ISN_REDIREND:
3243 {
3244 char_u *res = get_clear_redir_ga();
3245
3246 // End redirection, put redirected text on the stack.
3247 clear_redir_lval();
3248 redir_vname = 0;
3249
Bram Moolenaar35578162021-08-02 19:10:38 +02003250 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02003251 {
3252 vim_free(res);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003253 goto theend;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02003254 }
3255 tv = STACK_TV_BOT(0);
3256 tv->v_type = VAR_STRING;
3257 tv->vval.v_string = res;
3258 ++ectx->ec_stack.ga_len;
3259 }
3260 break;
3261
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003262 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02003263#ifdef FEAT_QUICKFIX
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003264 force_abort = TRUE;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003265 if (trigger_cexpr_autocmd(iptr->isn_arg.number) == FAIL)
3266 goto on_error;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003267 force_abort = FALSE;
Bram Moolenaarb7c97812021-05-05 22:51:39 +02003268#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003269 break;
3270
3271 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02003272#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003273 {
3274 exarg_T ea;
3275 int res;
3276
3277 CLEAR_FIELD(ea);
3278 ea.cmdidx = iptr->isn_arg.cexpr.cexpr_ref->cer_cmdidx;
3279 ea.forceit = iptr->isn_arg.cexpr.cexpr_ref->cer_forceit;
3280 ea.cmdlinep = &iptr->isn_arg.cexpr.cexpr_ref->cer_cmdline;
3281 --ectx->ec_stack.ga_len;
3282 tv = STACK_TV_BOT(0);
Bram Moolenaarbd683e32022-07-18 17:49:03 +01003283 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003284 res = cexpr_core(&ea, tv);
3285 clear_tv(tv);
3286 if (res == FAIL)
3287 goto on_error;
3288 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02003289#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003290 break;
3291
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003292 // execute Ex command from pieces on the stack
3293 case ISN_EXECCONCAT:
3294 {
3295 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +02003296 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003297 int pass;
3298 int i;
3299 char_u *cmd = NULL;
3300 char_u *str;
3301
3302 for (pass = 1; pass <= 2; ++pass)
3303 {
3304 for (i = 0; i < count; ++i)
3305 {
3306 tv = STACK_TV_BOT(i - count);
3307 str = tv->vval.v_string;
3308 if (str != NULL && *str != NUL)
3309 {
3310 if (pass == 2)
3311 STRCPY(cmd + len, str);
3312 len += STRLEN(str);
3313 }
3314 if (pass == 2)
3315 clear_tv(tv);
3316 }
3317 if (pass == 1)
3318 {
3319 cmd = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003320 if (unlikely(cmd == NULL))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003321 goto theend;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003322 len = 0;
3323 }
3324 }
3325
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02003326 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003327 do_cmdline_cmd(cmd);
3328 vim_free(cmd);
3329 }
3330 break;
3331
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003332 // execute :echo {string} ...
3333 case ISN_ECHO:
3334 {
3335 int count = iptr->isn_arg.echo.echo_count;
3336 int atstart = TRUE;
3337 int needclr = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003338 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003339
3340 for (idx = 0; idx < count; ++idx)
3341 {
3342 tv = STACK_TV_BOT(idx - count);
3343 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
3344 &atstart, &needclr);
3345 clear_tv(tv);
3346 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01003347 if (needclr)
3348 msg_clr_eos();
Bram Moolenaar4c137212021-04-19 16:48:48 +02003349 ectx->ec_stack.ga_len -= count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003350 }
3351 break;
3352
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003353 // :execute {string} ...
3354 // :echomsg {string} ...
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01003355 // :echowindow {string} ...
Bram Moolenaar7de62622021-08-07 15:05:47 +02003356 // :echoconsole {string} ...
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003357 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01003358 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003359 case ISN_ECHOMSG:
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01003360 case ISN_ECHOWINDOW:
Bram Moolenaar7de62622021-08-07 15:05:47 +02003361 case ISN_ECHOCONSOLE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003362 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01003363 {
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01003364 int count;
Bram Moolenaarad39c092020-02-26 18:23:43 +01003365 garray_T ga;
3366 char_u buf[NUMBUFLEN];
3367 char_u *p;
3368 int len;
3369 int failed = FALSE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003370 int idx;
Bram Moolenaarad39c092020-02-26 18:23:43 +01003371
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01003372 if (iptr->isn_type == ISN_ECHOWINDOW)
3373 count = iptr->isn_arg.echowin.ewin_count;
3374 else
3375 count = iptr->isn_arg.number;
Bram Moolenaarad39c092020-02-26 18:23:43 +01003376 ga_init2(&ga, 1, 80);
3377 for (idx = 0; idx < count; ++idx)
3378 {
3379 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003380 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01003381 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003382 if (tv->v_type == VAR_CHANNEL
3383 || tv->v_type == VAR_JOB)
3384 {
3385 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar68db9962021-05-09 23:19:22 +02003386 semsg(_(e_using_invalid_value_as_string_str),
3387 vartype_name(tv->v_type));
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003388 break;
3389 }
3390 else
3391 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01003392 }
3393 else
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003394 p = tv_stringify(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01003395
3396 len = (int)STRLEN(p);
Bram Moolenaar35578162021-08-02 19:10:38 +02003397 if (GA_GROW_FAILS(&ga, len + 2))
Bram Moolenaarad39c092020-02-26 18:23:43 +01003398 failed = TRUE;
3399 else
3400 {
3401 if (ga.ga_len > 0)
3402 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
3403 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
3404 ga.ga_len += len;
3405 }
3406 clear_tv(tv);
3407 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003408 ectx->ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003409 if (failed)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003410 {
3411 ga_clear(&ga);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003412 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003413 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01003414
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003415 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003416 {
3417 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02003418 {
3419 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003420 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01003421 if (did_emsg)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003422 {
3423 ga_clear(&ga);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01003424 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003425 }
Bram Moolenaar430deb12020-08-23 16:29:11 +02003426 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003427 else
3428 {
3429 msg_sb_eol();
3430 if (iptr->isn_type == ISN_ECHOMSG)
3431 {
3432 msg_attr(ga.ga_data, echo_attr);
3433 out_flush();
3434 }
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01003435#ifdef HAS_MESSAGE_WINDOW
3436 else if (iptr->isn_type == ISN_ECHOWINDOW)
3437 {
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01003438 start_echowindow(
3439 iptr->isn_arg.echowin.ewin_time);
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01003440 msg_attr(ga.ga_data, echo_attr);
3441 end_echowindow();
3442 }
3443#endif
Bram Moolenaar7de62622021-08-07 15:05:47 +02003444 else if (iptr->isn_type == ISN_ECHOCONSOLE)
3445 {
3446 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data),
3447 TRUE);
3448 ui_write((char_u *)"\r\n", 2, TRUE);
3449 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003450 else
3451 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003452 SOURCING_LNUM = iptr->isn_lnum;
3453 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003454 }
3455 }
3456 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01003457 ga_clear(&ga);
3458 }
3459 break;
3460
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003461 // load local variable or argument
3462 case ISN_LOAD:
Bram Moolenaar35578162021-08-02 19:10:38 +02003463 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003464 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003465 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003466 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003467 break;
3468
3469 // load v: variable
3470 case ISN_LOADV:
Bram Moolenaar35578162021-08-02 19:10:38 +02003471 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003472 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003473 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003474 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003475 break;
3476
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003477 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003478 case ISN_LOADSCRIPT:
3479 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01003480 scriptref_T *sref = iptr->isn_arg.script.scriptref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003481 svar_T *sv;
3482
Bram Moolenaar6db660b2021-08-01 14:08:54 +02003483 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003484 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003485 goto theend;
Bram Moolenaar859cc212022-03-28 15:22:35 +01003486 allocate_if_null(sv);
Bram Moolenaar35578162021-08-02 19:10:38 +02003487 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003488 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003489 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003490 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003491 }
3492 break;
3493
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003494 // load s: variable in old script or autoload import
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003495 case ISN_LOADS:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003496 case ISN_LOADEXPORT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003497 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003498 int sid = iptr->isn_arg.loadstore.ls_sid;
3499 hashtab_T *ht = &SCRIPT_VARS(sid);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003500 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003501 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003502
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003503 if (di == NULL)
3504 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003505 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003506 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003507 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003508 }
3509 else
3510 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003511 if (iptr->isn_type == ISN_LOADEXPORT)
3512 {
3513 int idx = get_script_item_idx(sid, name, 0,
3514 NULL, NULL);
3515 svar_T *sv;
3516
3517 if (idx >= 0)
3518 {
3519 sv = ((svar_T *)SCRIPT_ITEM(sid)
3520 ->sn_var_vals.ga_data) + idx;
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003521 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003522 {
3523 SOURCING_LNUM = iptr->isn_lnum;
3524 semsg(_(e_item_not_exported_in_script_str),
3525 name);
3526 goto on_error;
3527 }
3528 }
3529 }
Bram Moolenaar35578162021-08-02 19:10:38 +02003530 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003531 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003532 copy_tv(&di->di_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003533 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003534 }
3535 }
3536 break;
3537
Bram Moolenaard3aac292020-04-19 14:32:17 +02003538 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003539 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02003540 case ISN_LOADB:
3541 case ISN_LOADW:
3542 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003543 {
Bram Moolenaar06b77222022-01-25 15:51:56 +00003544 int res = load_namespace_var(ectx, iptr->isn_type, iptr);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003545
Bram Moolenaar06b77222022-01-25 15:51:56 +00003546 if (res == NOTDONE)
Bram Moolenaarcbbc48f2022-01-18 12:58:28 +00003547 goto theend;
Bram Moolenaar06b77222022-01-25 15:51:56 +00003548 if (res == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003549 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003550 }
Bram Moolenaar06b77222022-01-25 15:51:56 +00003551
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003552 break;
3553
Bram Moolenaar03290b82020-12-19 16:30:44 +01003554 // load autoload variable
3555 case ISN_LOADAUTO:
3556 {
3557 char_u *name = iptr->isn_arg.string;
3558
Bram Moolenaar35578162021-08-02 19:10:38 +02003559 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003560 goto theend;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003561 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003562 if (eval_variable(name, (int)STRLEN(name), 0,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003563 STACK_TV_BOT(0), NULL, EVAL_VAR_VERBOSE) == FAIL)
Bram Moolenaar03290b82020-12-19 16:30:44 +01003564 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003565 ++ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003566 }
3567 break;
3568
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003569 // load g:/b:/w:/t: namespace
3570 case ISN_LOADGDICT:
3571 case ISN_LOADBDICT:
3572 case ISN_LOADWDICT:
3573 case ISN_LOADTDICT:
3574 {
3575 dict_T *d = NULL;
3576
3577 switch (iptr->isn_type)
3578 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02003579 case ISN_LOADGDICT: d = get_globvar_dict(); break;
3580 case ISN_LOADBDICT: d = curbuf->b_vars; break;
3581 case ISN_LOADWDICT: d = curwin->w_vars; break;
3582 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003583 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003584 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003585 }
Bram Moolenaar35578162021-08-02 19:10:38 +02003586 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003587 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003588 tv = STACK_TV_BOT(0);
3589 tv->v_type = VAR_DICT;
3590 tv->v_lock = 0;
3591 tv->vval.v_dict = d;
Bram Moolenaar1bd3cb22021-02-24 12:27:31 +01003592 ++d->dv_refcount;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003593 ++ectx->ec_stack.ga_len;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003594 }
3595 break;
3596
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003597 // load &option
3598 case ISN_LOADOPT:
3599 {
3600 typval_T optval;
3601 char_u *name = iptr->isn_arg.string;
3602
Bram Moolenaara8c17702020-04-01 21:17:24 +02003603 // This is not expected to fail, name is checked during
3604 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar35578162021-08-02 19:10:38 +02003605 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003606 goto theend;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003607 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003608 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003609 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003610 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003611 }
3612 break;
3613
3614 // load $ENV
3615 case ISN_LOADENV:
3616 {
3617 typval_T optval;
3618 char_u *name = iptr->isn_arg.string;
3619
Bram Moolenaar35578162021-08-02 19:10:38 +02003620 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003621 goto theend;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003622 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003623 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003624 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003625 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003626 }
3627 break;
3628
3629 // load @register
3630 case ISN_LOADREG:
Bram Moolenaar35578162021-08-02 19:10:38 +02003631 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003632 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003633 tv = STACK_TV_BOT(0);
3634 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003635 tv->v_lock = 0;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02003636 // This may result in NULL, which should be equivalent to an
3637 // empty string.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003638 tv->vval.v_string = get_reg_contents(
3639 iptr->isn_arg.number, GREG_EXPR_SRC);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003640 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003641 break;
3642
3643 // store local variable
3644 case ISN_STORE:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003645 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003646 tv = STACK_TV_VAR(iptr->isn_arg.number);
3647 clear_tv(tv);
3648 *tv = *STACK_TV_BOT(0);
3649 break;
3650
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003651 // store s: variable in old script or autoload import
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003652 case ISN_STORES:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003653 case ISN_STOREEXPORT:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003654 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003655 int sid = iptr->isn_arg.loadstore.ls_sid;
3656 hashtab_T *ht = &SCRIPT_VARS(sid);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003657 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003658 dictitem_T *di = find_var_in_ht(ht, 0,
3659 iptr->isn_type == ISN_STORES
3660 ? name + 2 : name, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003661
Bram Moolenaar4c137212021-04-19 16:48:48 +02003662 --ectx->ec_stack.ga_len;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003663 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003664 if (di == NULL)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003665 {
3666 if (iptr->isn_type == ISN_STOREEXPORT)
3667 {
3668 semsg(_(e_undefined_variable_str), name);
Bram Moolenaard1d26842022-03-31 10:13:47 +01003669 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003670 goto on_error;
3671 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003672 store_var(name, STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003673 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003674 else
3675 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003676 if (iptr->isn_type == ISN_STOREEXPORT)
3677 {
3678 int idx = get_script_item_idx(sid, name, 0,
3679 NULL, NULL);
3680
Bram Moolenaar06651632022-04-27 17:54:25 +01003681 // can this ever fail?
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003682 if (idx >= 0)
3683 {
3684 svar_T *sv = ((svar_T *)SCRIPT_ITEM(sid)
3685 ->sn_var_vals.ga_data) + idx;
3686
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003687 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003688 {
3689 semsg(_(e_item_not_exported_in_script_str),
3690 name);
Bram Moolenaard1d26842022-03-31 10:13:47 +01003691 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003692 goto on_error;
3693 }
3694 }
3695 }
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02003696 if (var_check_permission(di, name) == FAIL)
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003697 {
3698 clear_tv(STACK_TV_BOT(0));
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02003699 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003700 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003701 clear_tv(&di->di_tv);
3702 di->di_tv = *STACK_TV_BOT(0);
3703 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003704 }
3705 break;
3706
3707 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003708 case ISN_STORESCRIPT:
3709 {
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003710 scriptref_T *sref = iptr->isn_arg.script.scriptref;
3711 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003712
Bram Moolenaar6db660b2021-08-01 14:08:54 +02003713 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003714 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003715 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003716 --ectx->ec_stack.ga_len;
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003717
3718 // "const" and "final" are checked at compile time, locking
3719 // the value needs to be checked here.
3720 SOURCING_LNUM = iptr->isn_lnum;
3721 if (value_check_lock(sv->sv_tv->v_lock, sv->sv_name, FALSE))
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003722 {
3723 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003724 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003725 }
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003726
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003727 clear_tv(sv->sv_tv);
3728 *sv->sv_tv = *STACK_TV_BOT(0);
3729 }
3730 break;
3731
3732 // store option
3733 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003734 case ISN_STOREFUNCOPT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003735 {
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003736 char_u *opt_name = iptr->isn_arg.storeopt.so_name;
3737 int opt_flags = iptr->isn_arg.storeopt.so_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003738 long n = 0;
3739 char_u *s = NULL;
3740 char *msg;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003741 char_u numbuf[NUMBUFLEN];
3742 char_u *tofree = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003743
Bram Moolenaar4c137212021-04-19 16:48:48 +02003744 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003745 tv = STACK_TV_BOT(0);
3746 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01003747 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003748 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01003749 if (s == NULL)
3750 s = (char_u *)"";
3751 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003752 else if (iptr->isn_type == ISN_STOREFUNCOPT)
3753 {
3754 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003755 // If the option can be set to a function reference or
3756 // a lambda and the passed value is a function
3757 // reference, then convert it to the name (string) of
3758 // the function reference.
3759 s = tv2string(tv, &tofree, numbuf, 0);
3760 if (s == NULL || *s == NUL)
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003761 {
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003762 // cannot happen?
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003763 clear_tv(tv);
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003764 vim_free(tofree);
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003765 goto on_error;
3766 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003767 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003768 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003769 // must be VAR_NUMBER, CHECKTYPE makes sure
3770 n = tv->vval.v_number;
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003771 msg = set_option_value(opt_name, n, s, opt_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02003772 clear_tv(tv);
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003773 vim_free(tofree);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003774 if (msg != NULL)
3775 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003776 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003777 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02003778 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003779 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003780 }
3781 break;
3782
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003783 // store $ENV
3784 case ISN_STOREENV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003785 --ectx->ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003786 tv = STACK_TV_BOT(0);
3787 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
3788 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003789 break;
3790
3791 // store @r
3792 case ISN_STOREREG:
3793 {
3794 int reg = iptr->isn_arg.number;
3795
Bram Moolenaar4c137212021-04-19 16:48:48 +02003796 --ectx->ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01003797 tv = STACK_TV_BOT(0);
Bram Moolenaar74f4a962021-06-17 21:03:07 +02003798 write_reg_contents(reg, tv_get_string(tv), -1, FALSE);
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01003799 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003800 }
3801 break;
3802
3803 // store v: variable
3804 case ISN_STOREV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003805 --ectx->ec_stack.ga_len;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003806 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
3807 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003808 // should not happen, type is checked when compiling
3809 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003810 break;
3811
Bram Moolenaard3aac292020-04-19 14:32:17 +02003812 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003813 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02003814 case ISN_STOREB:
3815 case ISN_STOREW:
3816 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003817 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003818 dictitem_T *di;
3819 hashtab_T *ht;
3820 char_u *name = iptr->isn_arg.string + 2;
3821
Bram Moolenaard3aac292020-04-19 14:32:17 +02003822 switch (iptr->isn_type)
3823 {
3824 case ISN_STOREG:
3825 ht = get_globvar_ht();
3826 break;
3827 case ISN_STOREB:
3828 ht = &curbuf->b_vars->dv_hashtab;
3829 break;
3830 case ISN_STOREW:
3831 ht = &curwin->w_vars->dv_hashtab;
3832 break;
3833 case ISN_STORET:
3834 ht = &curtab->tp_vars->dv_hashtab;
3835 break;
3836 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003837 goto theend;
Bram Moolenaard3aac292020-04-19 14:32:17 +02003838 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003839
Bram Moolenaar4c137212021-04-19 16:48:48 +02003840 --ectx->ec_stack.ga_len;
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003841 di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003842 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003843 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003844 else
3845 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003846 SOURCING_LNUM = iptr->isn_lnum;
3847 if (var_check_permission(di, name) == FAIL)
3848 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003849 clear_tv(&di->di_tv);
3850 di->di_tv = *STACK_TV_BOT(0);
3851 }
3852 }
3853 break;
3854
Bram Moolenaar03290b82020-12-19 16:30:44 +01003855 // store an autoload variable
3856 case ISN_STOREAUTO:
3857 SOURCING_LNUM = iptr->isn_lnum;
3858 set_var(iptr->isn_arg.string, STACK_TV_BOT(-1), TRUE);
3859 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003860 --ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003861 break;
3862
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003863 // store number in local variable
3864 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01003865 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003866 clear_tv(tv);
3867 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01003868 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003869 break;
3870
Bram Moolenaar590162c2022-12-24 21:24:06 +00003871 // Store a value in a list, dict, blob or object variable.
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01003872 case ISN_STOREINDEX:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003873 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003874 int res = execute_storeindex(iptr, ectx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003875
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003876 if (res == FAIL)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02003877 goto on_error;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003878 if (res == NOTDONE)
3879 goto theend;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003880 }
3881 break;
3882
Bram Moolenaarea5c8982022-02-17 14:42:02 +00003883 // store value in list or blob range
Bram Moolenaar68452172021-04-12 21:21:02 +02003884 case ISN_STORERANGE:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003885 if (execute_storerange(iptr, ectx) == FAIL)
3886 goto on_error;
Bram Moolenaar68452172021-04-12 21:21:02 +02003887 break;
3888
Bram Moolenaard505d172022-12-18 21:42:55 +00003889 case ISN_LOAD_CLASSMEMBER:
3890 {
3891 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
3892 goto theend;
3893 classmember_T *cm = &iptr->isn_arg.classmember;
3894 *STACK_TV_BOT(0) =
3895 cm->cm_class->class_members_tv[cm->cm_idx];
3896 ++ectx->ec_stack.ga_len;
3897 }
3898 break;
3899
3900 case ISN_STORE_CLASSMEMBER:
3901 {
3902 classmember_T *cm = &iptr->isn_arg.classmember;
3903 tv = &cm->cm_class->class_members_tv[cm->cm_idx];
3904 clear_tv(tv);
3905 *tv = *STACK_TV_BOT(-1);
3906 --ectx->ec_stack.ga_len;
3907 }
3908 break;
3909
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01003910 // Load or store variable or argument from outer scope.
Bram Moolenaar0186e582021-01-10 18:33:11 +01003911 case ISN_LOADOUTER:
3912 case ISN_STOREOUTER:
3913 {
3914 int depth = iptr->isn_arg.outer.outer_depth;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02003915 outer_T *outer = ectx->ec_outer_ref == NULL ? NULL
3916 : ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003917
3918 while (depth > 1 && outer != NULL)
3919 {
3920 outer = outer->out_up;
3921 --depth;
3922 }
3923 if (outer == NULL)
3924 {
3925 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar69c76172021-12-02 16:38:52 +00003926 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx
3927 || ectx->ec_outer_ref == NULL)
3928 // Possibly :def function called from legacy
3929 // context.
3930 emsg(_(e_closure_called_from_invalid_context));
3931 else
3932 iemsg("LOADOUTER depth more than scope levels");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003933 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003934 }
Bram Moolenaarcc341812022-09-19 15:54:34 +01003935 if (depth < 0)
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01003936 // Variable declared in loop. May be copied if the
3937 // loop block has already ended.
Bram Moolenaarcc341812022-09-19 15:54:34 +01003938 tv = ((typval_T *)outer->out_loop[-depth - 1]
3939 .stack->ga_data)
3940 + outer->out_loop[-depth - 1].var_idx
3941 + iptr->isn_arg.outer.outer_idx;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01003942 else
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01003943 // Variable declared in a function. May be copied if
3944 // the function has already returned.
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01003945 tv = ((typval_T *)outer->out_stack->ga_data)
3946 + outer->out_frame_idx + STACK_FRAME_SIZE
3947 + iptr->isn_arg.outer.outer_idx;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003948 if (iptr->isn_type == ISN_LOADOUTER)
3949 {
Bram Moolenaar35578162021-08-02 19:10:38 +02003950 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003951 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003952 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003953 ++ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003954 }
3955 else
3956 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003957 --ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003958 clear_tv(tv);
3959 *tv = *STACK_TV_BOT(0);
3960 }
3961 }
3962 break;
3963
Bram Moolenaar752fc692021-01-04 21:57:11 +01003964 // unlet item in list or dict variable
3965 case ISN_UNLETINDEX:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003966 if (execute_unletindex(iptr, ectx) == FAIL)
3967 goto on_error;
Bram Moolenaar752fc692021-01-04 21:57:11 +01003968 break;
3969
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01003970 // unlet range of items in list variable
3971 case ISN_UNLETRANGE:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003972 if (execute_unletrange(iptr, ectx) == FAIL)
3973 goto on_error;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01003974 break;
3975
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003976 // push constant
3977 case ISN_PUSHNR:
3978 case ISN_PUSHBOOL:
3979 case ISN_PUSHSPEC:
3980 case ISN_PUSHF:
3981 case ISN_PUSHS:
3982 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003983 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003984 case ISN_PUSHCHANNEL:
3985 case ISN_PUSHJOB:
Bram Moolenaar35578162021-08-02 19:10:38 +02003986 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003987 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003988 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003989 tv->v_lock = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003990 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003991 switch (iptr->isn_type)
3992 {
3993 case ISN_PUSHNR:
3994 tv->v_type = VAR_NUMBER;
3995 tv->vval.v_number = iptr->isn_arg.number;
3996 break;
3997 case ISN_PUSHBOOL:
3998 tv->v_type = VAR_BOOL;
3999 tv->vval.v_number = iptr->isn_arg.number;
4000 break;
4001 case ISN_PUSHSPEC:
4002 tv->v_type = VAR_SPECIAL;
4003 tv->vval.v_number = iptr->isn_arg.number;
4004 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004005 case ISN_PUSHF:
4006 tv->v_type = VAR_FLOAT;
4007 tv->vval.v_float = iptr->isn_arg.fnumber;
4008 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004009 case ISN_PUSHBLOB:
4010 blob_copy(iptr->isn_arg.blob, tv);
4011 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004012 case ISN_PUSHFUNC:
4013 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01004014 if (iptr->isn_arg.string == NULL)
4015 tv->vval.v_string = NULL;
4016 else
4017 tv->vval.v_string =
4018 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004019 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004020 case ISN_PUSHCHANNEL:
4021#ifdef FEAT_JOB_CHANNEL
4022 tv->v_type = VAR_CHANNEL;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00004023 tv->vval.v_channel = NULL;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004024#endif
4025 break;
4026 case ISN_PUSHJOB:
4027#ifdef FEAT_JOB_CHANNEL
4028 tv->v_type = VAR_JOB;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00004029 tv->vval.v_job = NULL;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004030#endif
4031 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004032 default:
4033 tv->v_type = VAR_STRING;
Bram Moolenaarf8691002022-03-10 12:20:53 +00004034 tv->vval.v_string = iptr->isn_arg.string == NULL
4035 ? NULL : vim_strsave(iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004036 }
4037 break;
4038
Bram Moolenaar06b77222022-01-25 15:51:56 +00004039 case ISN_AUTOLOAD:
4040 {
4041 char_u *name = iptr->isn_arg.string;
4042
4043 (void)script_autoload(name, FALSE);
4044 if (find_func(name, TRUE))
4045 {
4046 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
4047 goto theend;
4048 tv = STACK_TV_BOT(0);
4049 tv->v_lock = 0;
4050 ++ectx->ec_stack.ga_len;
4051 tv->v_type = VAR_FUNC;
4052 tv->vval.v_string = vim_strsave(name);
4053 }
4054 else
4055 {
4056 int res = load_namespace_var(ectx, ISN_LOADG, iptr);
4057
4058 if (res == NOTDONE)
4059 goto theend;
4060 if (res == FAIL)
4061 goto on_error;
4062 }
4063 }
4064 break;
4065
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004066 case ISN_UNLET:
4067 if (do_unlet(iptr->isn_arg.unlet.ul_name,
4068 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02004069 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004070 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02004071 case ISN_UNLETENV:
LemonBoy77142312022-04-15 20:50:46 +01004072 vim_unsetenv_ext(iptr->isn_arg.unlet.ul_name);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02004073 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004074
Bram Moolenaaraacc9662021-08-13 19:40:51 +02004075 case ISN_LOCKUNLOCK:
4076 {
4077 typval_T *lval_root_save = lval_root;
4078 int res;
4079
4080 // Stack has the local variable, argument the whole :lock
4081 // or :unlock command, like ISN_EXEC.
4082 --ectx->ec_stack.ga_len;
4083 lval_root = STACK_TV_BOT(0);
4084 res = exec_command(iptr);
4085 clear_tv(lval_root);
4086 lval_root = lval_root_save;
4087 if (res == FAIL)
4088 goto on_error;
4089 }
4090 break;
4091
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02004092 case ISN_LOCKCONST:
4093 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
4094 break;
4095
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004096 // create a list from items on the stack; uses a single allocation
4097 // for the list header and the items
4098 case ISN_NEWLIST:
Bram Moolenaar4c137212021-04-19 16:48:48 +02004099 if (exe_newlist(iptr->isn_arg.number, ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004100 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004101 break;
4102
4103 // create a dict from items on the stack
4104 case ISN_NEWDICT:
4105 {
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01004106 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004107
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01004108 SOURCING_LNUM = iptr->isn_lnum;
4109 res = exe_newdict(iptr->isn_arg.number, ectx);
4110 if (res == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004111 goto theend;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01004112 if (res == MAYBE)
4113 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004114 }
4115 break;
4116
LemonBoy372bcce2022-04-25 12:43:20 +01004117 case ISN_CONCAT:
4118 if (exe_concat(iptr->isn_arg.number, ectx) == FAIL)
4119 goto theend;
4120 break;
4121
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004122 // create a partial with NULL value
4123 case ISN_NEWPARTIAL:
4124 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
4125 goto theend;
4126 ++ectx->ec_stack.ga_len;
4127 tv = STACK_TV_BOT(-1);
4128 tv->v_type = VAR_PARTIAL;
4129 tv->v_lock = 0;
4130 tv->vval.v_partial = NULL;
4131 break;
4132
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004133 // call a :def function
4134 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02004135 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01004136 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
4137 NULL,
4138 iptr->isn_arg.dfunc.cdf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02004139 ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02004140 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004141 break;
4142
Bram Moolenaard0200c82023-01-28 15:19:40 +00004143 // call a method on an interface
4144 case ISN_METHODCALL:
4145 {
Bram Moolenaar094cf9f2023-02-10 15:52:25 +00004146 cmfunc_T *mfunc = iptr->isn_arg.mfunc;
4147
Bram Moolenaard0200c82023-01-28 15:19:40 +00004148 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar094cf9f2023-02-10 15:52:25 +00004149 tv = STACK_TV_BOT(-1 - mfunc->cmf_argcount);
Bram Moolenaard0200c82023-01-28 15:19:40 +00004150 if (tv->v_type != VAR_OBJECT)
4151 {
4152 object_required_error(tv);
4153 goto on_error;
4154 }
4155 object_T *obj = tv->vval.v_object;
4156 class_T *cl = obj->obj_class;
4157
4158 // convert the interface index to the object index
Bram Moolenaard0200c82023-01-28 15:19:40 +00004159 int idx = object_index_from_itf_index(mfunc->cmf_itf,
4160 TRUE, mfunc->cmf_idx, cl);
4161
4162 if (call_ufunc(cl->class_obj_methods[idx], NULL,
4163 mfunc->cmf_argcount, ectx, NULL, NULL) == FAIL)
4164 goto on_error;
4165 }
4166 break;
4167
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004168 // call a builtin function
4169 case ISN_BCALL:
4170 SOURCING_LNUM = iptr->isn_lnum;
4171 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
4172 iptr->isn_arg.bfunc.cbf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02004173 ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004174 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004175 break;
4176
4177 // call a funcref or partial
4178 case ISN_PCALL:
4179 {
4180 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
4181 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02004182 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004183
4184 SOURCING_LNUM = iptr->isn_lnum;
4185 if (pfunc->cpf_top)
4186 {
4187 // funcref is above the arguments
4188 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
4189 }
4190 else
4191 {
4192 // Get the funcref from the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004193 --ectx->ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02004194 partial_tv = *STACK_TV_BOT(0);
4195 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004196 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004197 r = call_partial(tv, pfunc->cpf_argcount, ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02004198 if (tv == &partial_tv)
4199 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004200 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004201 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004202 }
4203 break;
4204
Bram Moolenaarbd5da372020-03-31 23:13:10 +02004205 case ISN_PCALL_END:
4206 // PCALL finished, arguments have been consumed and replaced by
4207 // the return value. Now clear the funcref from the stack,
4208 // and move the return value in its place.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004209 --ectx->ec_stack.ga_len;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02004210 clear_tv(STACK_TV_BOT(-1));
4211 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
4212 break;
4213
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004214 // call a user defined function or funcref/partial
4215 case ISN_UCALL:
4216 {
4217 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
4218
4219 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01004220 if (call_eval_func(cufunc->cuf_name, cufunc->cuf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02004221 ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004222 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004223 }
4224 break;
4225
Bram Moolenaar1d84f762022-09-03 21:35:53 +01004226 // :defer func(arg)
4227 case ISN_DEFER:
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00004228 case ISN_DEFEROBJ:
Bram Moolenaar806a2732022-09-04 15:40:36 +01004229 if (defer_command(iptr->isn_arg.defer.defer_var_idx,
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00004230 iptr->isn_type == ISN_DEFEROBJ,
Bram Moolenaar1d84f762022-09-03 21:35:53 +01004231 iptr->isn_arg.defer.defer_argcount, ectx) == FAIL)
4232 goto on_error;
4233 break;
4234
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004235 // Return from a :def function call without a value.
4236 // Return from a constructor.
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02004237 case ISN_RETURN_VOID:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004238 case ISN_RETURN_OBJECT:
Bram Moolenaar35578162021-08-02 19:10:38 +02004239 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004240 goto theend;
Bram Moolenaar299f3032021-01-08 20:53:09 +01004241 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004242 ++ectx->ec_stack.ga_len;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004243 if (iptr->isn_type == ISN_RETURN_VOID)
4244 {
4245 tv->v_type = VAR_VOID;
4246 tv->vval.v_number = 0;
4247 tv->v_lock = 0;
4248 }
4249 else
4250 {
4251 *tv = *STACK_TV_VAR(0);
4252 ++tv->vval.v_object->obj_refcount;
4253 }
Bram Moolenaar299f3032021-01-08 20:53:09 +01004254 // FALLTHROUGH
4255
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02004256 // return from a :def function call with what is on the stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004257 case ISN_RETURN:
4258 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004259 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01004260 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01004261
4262 if (trystack->ga_len > 0)
4263 trycmd = ((trycmd_T *)trystack->ga_data)
4264 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004265 if (trycmd != NULL
Bram Moolenaar4c137212021-04-19 16:48:48 +02004266 && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004267 {
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01004268 // jump to ":finally" or ":endtry"
4269 if (trycmd->tcd_finally_idx != 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004270 ectx->ec_iidx = trycmd->tcd_finally_idx;
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01004271 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02004272 ectx->ec_iidx = trycmd->tcd_endtry_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004273 trycmd->tcd_return = TRUE;
4274 }
4275 else
Bram Moolenaard032f342020-07-18 18:13:02 +02004276 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004277 }
4278 break;
4279
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004280 // push a partial, a reference to a compiled function
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004281 case ISN_FUNCREF:
4282 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004283 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
4284 ufunc_T *ufunc;
4285 funcref_T *funcref = &iptr->isn_arg.funcref;
4286 funcref_extra_T *extra = funcref->fr_extra;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004287
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004288 if (pt == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004289 goto theend;
Bram Moolenaar35578162021-08-02 19:10:38 +02004290 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02004291 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004292 vim_free(pt);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004293 goto theend;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004294 }
Bram Moolenaar313e4722023-02-08 20:55:27 +00004295 if (extra != NULL && extra->fre_class != NULL)
4296 {
4297 tv = STACK_TV_BOT(-1);
4298 if (tv->v_type != VAR_OBJECT)
4299 {
4300 object_required_error(tv);
4301 vim_free(pt);
4302 goto on_error;
4303 }
4304 object_T *obj = tv->vval.v_object;
4305 class_T *cl = obj->obj_class;
4306
4307 // convert the interface index to the object index
4308 int idx = object_index_from_itf_index(extra->fre_class,
4309 TRUE, extra->fre_method_idx, cl);
4310 ufunc = cl->class_obj_methods[idx];
4311 }
4312 else if (extra == NULL || extra->fre_func_name == NULL)
Bram Moolenaar38453522021-11-28 22:00:12 +00004313 {
4314 dfunc_T *pt_dfunc = ((dfunc_T *)def_functions.ga_data)
4315 + funcref->fr_dfunc_idx;
4316
4317 ufunc = pt_dfunc->df_ufunc;
4318 }
4319 else
Bram Moolenaar313e4722023-02-08 20:55:27 +00004320 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004321 ufunc = find_func(extra->fre_func_name, FALSE);
Bram Moolenaar313e4722023-02-08 20:55:27 +00004322 }
Bram Moolenaar56acd1f2022-02-18 13:24:52 +00004323 if (ufunc == NULL)
4324 {
4325 SOURCING_LNUM = iptr->isn_lnum;
4326 iemsg("ufunc unexpectedly NULL for FUNCREF");
4327 goto theend;
4328 }
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004329 if (fill_partial_and_closure(pt, ufunc,
Bram Moolenaarcc341812022-09-19 15:54:34 +01004330 extra == NULL ? NULL : &extra->fre_loopvar_info,
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004331 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004332 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004333 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004334 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004335 tv->vval.v_partial = pt;
4336 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02004337 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004338 }
4339 break;
4340
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004341 // Create a global function from a lambda.
4342 case ISN_NEWFUNC:
4343 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004344 newfuncarg_T *arg = iptr->isn_arg.newfunc.nf_arg;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004345
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004346 if (copy_lambda_to_global_func(arg->nfa_lambda,
Bram Moolenaarcc341812022-09-19 15:54:34 +01004347 arg->nfa_global, &arg->nfa_loopvar_info,
4348 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004349 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004350 }
4351 break;
4352
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004353 // List functions
4354 case ISN_DEF:
4355 if (iptr->isn_arg.string == NULL)
4356 list_functions(NULL);
4357 else
4358 {
Bram Moolenaar14336722022-01-08 16:02:59 +00004359 exarg_T ea;
4360 garray_T lines_to_free;
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004361
4362 CLEAR_FIELD(ea);
4363 ea.cmd = ea.arg = iptr->isn_arg.string;
Bram Moolenaar14336722022-01-08 16:02:59 +00004364 ga_init2(&lines_to_free, sizeof(char_u *), 50);
Bram Moolenaar554d0312023-01-05 19:59:18 +00004365 define_function(&ea, NULL, &lines_to_free, 0);
Bram Moolenaar14336722022-01-08 16:02:59 +00004366 ga_clear_strings(&lines_to_free);
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004367 }
4368 break;
4369
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004370 // jump if a condition is met
4371 case ISN_JUMP:
4372 {
4373 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004374 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004375 int jump = TRUE;
4376
4377 if (when != JUMP_ALWAYS)
4378 {
4379 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004380 if (when == JUMP_IF_COND_FALSE
Bram Moolenaar13106602020-10-04 16:06:05 +02004381 || when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004382 || when == JUMP_IF_COND_TRUE)
4383 {
4384 SOURCING_LNUM = iptr->isn_lnum;
4385 jump = tv_get_bool_chk(tv, &error);
4386 if (error)
4387 goto on_error;
4388 }
4389 else
4390 jump = tv2bool(tv);
Bram Moolenaarf6ced982022-04-28 12:00:49 +01004391 if (when == JUMP_IF_FALSE || when == JUMP_IF_COND_FALSE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004392 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01004393 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004394 {
4395 // drop the value from the stack
4396 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004397 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004398 }
4399 }
4400 if (jump)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004401 ectx->ec_iidx = iptr->isn_arg.jump.jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004402 }
4403 break;
4404
Bram Moolenaarb46c0832022-09-15 17:19:37 +01004405 // "while": jump to end if a condition is false
4406 case ISN_WHILE:
4407 {
4408 int error = FALSE;
4409 int jump = TRUE;
4410
4411 tv = STACK_TV_BOT(-1);
4412 SOURCING_LNUM = iptr->isn_lnum;
4413 jump = !tv_get_bool_chk(tv, &error);
4414 if (error)
4415 goto on_error;
4416 // drop the value from the stack
4417 clear_tv(tv);
4418 --ectx->ec_stack.ga_len;
4419 if (jump)
4420 ectx->ec_iidx = iptr->isn_arg.whileloop.while_end;
4421
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01004422 // Store the current funcref count, may be used by
Bram Moolenaarcc341812022-09-19 15:54:34 +01004423 // ISN_ENDLOOP later
Bram Moolenaarb46c0832022-09-15 17:19:37 +01004424 tv = STACK_TV_VAR(
4425 iptr->isn_arg.whileloop.while_funcref_idx);
4426 tv->vval.v_number = ectx->ec_funcrefs.ga_len;
4427 }
4428 break;
4429
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02004430 // Jump if an argument with a default value was already set and not
4431 // v:none.
4432 case ISN_JUMP_IF_ARG_SET:
Bram Moolenaar65b0d162022-12-13 18:43:22 +00004433 case ISN_JUMP_IF_ARG_NOT_SET:
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02004434 tv = STACK_TV_VAR(iptr->isn_arg.jumparg.jump_arg_off);
Bram Moolenaar65b0d162022-12-13 18:43:22 +00004435 int arg_set = tv->v_type != VAR_UNKNOWN
4436 && !(tv->v_type == VAR_SPECIAL
4437 && tv->vval.v_number == VVAL_NONE);
4438 if (iptr->isn_type == ISN_JUMP_IF_ARG_SET ? arg_set : !arg_set)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004439 ectx->ec_iidx = iptr->isn_arg.jumparg.jump_where;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02004440 break;
4441
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004442 // top of a for loop
4443 case ISN_FOR:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00004444 if (execute_for(iptr, ectx) == FAIL)
4445 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004446 break;
4447
Bram Moolenaarb46c0832022-09-15 17:19:37 +01004448 // end of a for or while loop
4449 case ISN_ENDLOOP:
4450 if (execute_endloop(iptr, ectx) == FAIL)
4451 goto theend;
4452 break;
4453
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004454 // start of ":try" block
4455 case ISN_TRY:
4456 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01004457 trycmd_T *trycmd = NULL;
4458
Bram Moolenaar35578162021-08-02 19:10:38 +02004459 if (GA_GROW_FAILS(&ectx->ec_trystack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004460 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004461 trycmd = ((trycmd_T *)ectx->ec_trystack.ga_data)
4462 + ectx->ec_trystack.ga_len;
4463 ++ectx->ec_trystack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004464 ++trylevel;
Bram Moolenaar8d4be892021-02-13 18:33:02 +01004465 CLEAR_POINTER(trycmd);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004466 trycmd->tcd_frame_idx = ectx->ec_frame_idx;
4467 trycmd->tcd_stack_len = ectx->ec_stack.ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01004468 trycmd->tcd_catch_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00004469 iptr->isn_arg.tryref.try_ref->try_catch;
Bram Moolenaara91a7132021-03-25 21:12:15 +01004470 trycmd->tcd_finally_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00004471 iptr->isn_arg.tryref.try_ref->try_finally;
Bram Moolenaara91a7132021-03-25 21:12:15 +01004472 trycmd->tcd_endtry_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00004473 iptr->isn_arg.tryref.try_ref->try_endtry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004474 }
4475 break;
4476
4477 case ISN_PUSHEXC:
4478 if (current_exception == NULL)
4479 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004480 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004481 iemsg("Evaluating catch while current_exception is NULL");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004482 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004483 }
Bram Moolenaar35578162021-08-02 19:10:38 +02004484 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004485 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004486 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004487 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004488 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02004489 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004490 tv->vval.v_string = vim_strsave(
4491 (char_u *)current_exception->value);
4492 break;
4493
4494 case ISN_CATCH:
4495 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004496 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar06651632022-04-27 17:54:25 +01004497 trycmd_T *trycmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004498
Bram Moolenaar4c137212021-04-19 16:48:48 +02004499 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar06651632022-04-27 17:54:25 +01004500 trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004501 + trystack->ga_len - 1;
Bram Moolenaar06651632022-04-27 17:54:25 +01004502 trycmd->tcd_caught = TRUE;
4503 trycmd->tcd_did_throw = FALSE;
4504
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004505 did_emsg = got_int = did_throw = FALSE;
Bram Moolenaar1430cee2021-01-17 19:20:32 +01004506 force_abort = need_rethrow = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004507 catch_exception(current_exception);
4508 }
4509 break;
4510
Bram Moolenaarc150c092021-02-13 15:02:46 +01004511 case ISN_TRYCONT:
4512 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004513 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004514 trycont_T *trycont = &iptr->isn_arg.trycont;
4515 int i;
4516 trycmd_T *trycmd;
4517 int iidx = trycont->tct_where;
4518
4519 if (trystack->ga_len < trycont->tct_levels)
4520 {
4521 siemsg("TRYCONT: expected %d levels, found %d",
4522 trycont->tct_levels, trystack->ga_len);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004523 goto theend;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004524 }
4525 // Make :endtry jump to any outer try block and the last
4526 // :endtry inside the loop to the loop start.
4527 for (i = trycont->tct_levels; i > 0; --i)
4528 {
4529 trycmd = ((trycmd_T *)trystack->ga_data)
4530 + trystack->ga_len - i;
Bram Moolenaar2e34c342021-03-14 12:13:33 +01004531 // Add one to tcd_cont to be able to jump to
4532 // instruction with index zero.
4533 trycmd->tcd_cont = iidx + 1;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01004534 iidx = trycmd->tcd_finally_idx == 0
4535 ? trycmd->tcd_endtry_idx : trycmd->tcd_finally_idx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004536 }
4537 // jump to :finally or :endtry of current try statement
Bram Moolenaar4c137212021-04-19 16:48:48 +02004538 ectx->ec_iidx = iidx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004539 }
4540 break;
4541
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01004542 case ISN_FINALLY:
4543 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004544 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01004545 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
4546 + trystack->ga_len - 1;
4547
4548 // Reset the index to avoid a return statement jumps here
4549 // again.
4550 trycmd->tcd_finally_idx = 0;
4551 break;
4552 }
4553
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004554 // end of ":try" block
4555 case ISN_ENDTRY:
4556 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004557 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar06651632022-04-27 17:54:25 +01004558 trycmd_T *trycmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004559
Bram Moolenaar06651632022-04-27 17:54:25 +01004560 --trystack->ga_len;
4561 --trylevel;
4562 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len;
4563 if (trycmd->tcd_did_throw)
4564 did_throw = TRUE;
4565 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004566 {
Bram Moolenaar06651632022-04-27 17:54:25 +01004567 // discard the exception
4568 if (caught_stack == current_exception)
4569 caught_stack = caught_stack->caught;
4570 discard_current_exception();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004571 }
Bram Moolenaar06651632022-04-27 17:54:25 +01004572
4573 if (trycmd->tcd_return)
4574 goto func_return;
4575
4576 while (ectx->ec_stack.ga_len > trycmd->tcd_stack_len)
4577 {
4578 --ectx->ec_stack.ga_len;
4579 clear_tv(STACK_TV_BOT(0));
4580 }
4581 if (trycmd->tcd_cont != 0)
4582 // handling :continue: jump to outer try block or
4583 // start of the loop
4584 ectx->ec_iidx = trycmd->tcd_cont - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004585 }
4586 break;
4587
4588 case ISN_THROW:
Bram Moolenaar8f81b222021-01-14 21:47:06 +01004589 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004590 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02004591
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004592 if (trystack->ga_len == 0 && trylevel == 0 && emsg_silent)
4593 {
4594 // throwing an exception while using "silent!" causes
4595 // the function to abort but not display an error.
4596 tv = STACK_TV_BOT(-1);
4597 clear_tv(tv);
4598 tv->v_type = VAR_NUMBER;
4599 tv->vval.v_number = 0;
4600 goto done;
4601 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004602 --ectx->ec_stack.ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004603 tv = STACK_TV_BOT(0);
4604 if (tv->vval.v_string == NULL
4605 || *skipwhite(tv->vval.v_string) == NUL)
4606 {
4607 vim_free(tv->vval.v_string);
4608 SOURCING_LNUM = iptr->isn_lnum;
4609 emsg(_(e_throw_with_empty_string));
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004610 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004611 }
4612
4613 // Inside a "catch" we need to first discard the caught
4614 // exception.
4615 if (trystack->ga_len > 0)
4616 {
4617 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
4618 + trystack->ga_len - 1;
4619 if (trycmd->tcd_caught && current_exception != NULL)
4620 {
4621 // discard the exception
4622 if (caught_stack == current_exception)
4623 caught_stack = caught_stack->caught;
4624 discard_current_exception();
4625 trycmd->tcd_caught = FALSE;
4626 }
4627 }
4628
Bram Moolenaar90a57162022-02-12 14:23:17 +00004629 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004630 if (throw_exception(tv->vval.v_string, ET_USER, NULL)
4631 == FAIL)
4632 {
4633 vim_free(tv->vval.v_string);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004634 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004635 }
4636 did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004637 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004638 break;
4639
4640 // compare with special values
4641 case ISN_COMPAREBOOL:
4642 case ISN_COMPARESPECIAL:
4643 {
4644 typval_T *tv1 = STACK_TV_BOT(-2);
4645 typval_T *tv2 = STACK_TV_BOT(-1);
4646 varnumber_T arg1 = tv1->vval.v_number;
4647 varnumber_T arg2 = tv2->vval.v_number;
4648 int res;
4649
Bram Moolenaar06651632022-04-27 17:54:25 +01004650 if (iptr->isn_arg.op.op_type == EXPR_EQUAL)
4651 res = arg1 == arg2;
4652 else
4653 res = arg1 != arg2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004654
Bram Moolenaar4c137212021-04-19 16:48:48 +02004655 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004656 tv1->v_type = VAR_BOOL;
4657 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4658 }
4659 break;
4660
Bram Moolenaar7a222242022-03-01 19:23:24 +00004661 case ISN_COMPARENULL:
4662 {
4663 typval_T *tv1 = STACK_TV_BOT(-2);
4664 typval_T *tv2 = STACK_TV_BOT(-1);
4665 int res;
4666
4667 res = typval_compare_null(tv1, tv2);
4668 if (res == MAYBE)
4669 goto on_error;
4670 if (iptr->isn_arg.op.op_type == EXPR_NEQUAL)
4671 res = !res;
4672 clear_tv(tv1);
4673 clear_tv(tv2);
4674 --ectx->ec_stack.ga_len;
4675 tv1->v_type = VAR_BOOL;
4676 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4677 }
4678 break;
4679
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004680 // Operation with two number arguments
4681 case ISN_OPNR:
4682 case ISN_COMPARENR:
4683 {
4684 typval_T *tv1 = STACK_TV_BOT(-2);
4685 typval_T *tv2 = STACK_TV_BOT(-1);
4686 varnumber_T arg1 = tv1->vval.v_number;
4687 varnumber_T arg2 = tv2->vval.v_number;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004688 varnumber_T res = 0;
4689 int div_zero = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004690
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004691 if (iptr->isn_arg.op.op_type == EXPR_LSHIFT
4692 || iptr->isn_arg.op.op_type == EXPR_RSHIFT)
4693 {
4694 if (arg2 < 0)
4695 {
4696 SOURCING_LNUM = iptr->isn_lnum;
dundargocc57b5bc2022-11-02 13:30:51 +00004697 emsg(_(e_bitshift_ops_must_be_positive));
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004698 goto on_error;
4699 }
4700 }
4701
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004702 switch (iptr->isn_arg.op.op_type)
4703 {
4704 case EXPR_MULT: res = arg1 * arg2; break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004705 case EXPR_DIV: if (arg2 == 0)
4706 div_zero = TRUE;
4707 else
4708 res = arg1 / arg2;
4709 break;
4710 case EXPR_REM: if (arg2 == 0)
4711 div_zero = TRUE;
4712 else
4713 res = arg1 % arg2;
4714 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004715 case EXPR_SUB: res = arg1 - arg2; break;
4716 case EXPR_ADD: res = arg1 + arg2; break;
4717
4718 case EXPR_EQUAL: res = arg1 == arg2; break;
4719 case EXPR_NEQUAL: res = arg1 != arg2; break;
4720 case EXPR_GREATER: res = arg1 > arg2; break;
4721 case EXPR_GEQUAL: res = arg1 >= arg2; break;
4722 case EXPR_SMALLER: res = arg1 < arg2; break;
4723 case EXPR_SEQUAL: res = arg1 <= arg2; break;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004724 case EXPR_LSHIFT: if (arg2 > MAX_LSHIFT_BITS)
4725 res = 0;
4726 else
Bram Moolenaar68e64d22022-05-22 22:07:52 +01004727 res = (uvarnumber_T)arg1 << arg2;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004728 break;
4729 case EXPR_RSHIFT: if (arg2 > MAX_LSHIFT_BITS)
4730 res = 0;
4731 else
Bram Moolenaar338bf582022-05-22 20:16:32 +01004732 res = (uvarnumber_T)arg1 >> arg2;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004733 break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004734 default: break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004735 }
4736
Bram Moolenaar4c137212021-04-19 16:48:48 +02004737 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004738 if (iptr->isn_type == ISN_COMPARENR)
4739 {
4740 tv1->v_type = VAR_BOOL;
4741 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4742 }
4743 else
4744 tv1->vval.v_number = res;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004745 if (div_zero)
4746 {
4747 SOURCING_LNUM = iptr->isn_lnum;
4748 emsg(_(e_divide_by_zero));
4749 goto on_error;
4750 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004751 }
4752 break;
4753
4754 // Computation with two float arguments
4755 case ISN_OPFLOAT:
4756 case ISN_COMPAREFLOAT:
4757 {
4758 typval_T *tv1 = STACK_TV_BOT(-2);
4759 typval_T *tv2 = STACK_TV_BOT(-1);
4760 float_T arg1 = tv1->vval.v_float;
4761 float_T arg2 = tv2->vval.v_float;
4762 float_T res = 0;
4763 int cmp = FALSE;
4764
4765 switch (iptr->isn_arg.op.op_type)
4766 {
4767 case EXPR_MULT: res = arg1 * arg2; break;
4768 case EXPR_DIV: res = arg1 / arg2; break;
4769 case EXPR_SUB: res = arg1 - arg2; break;
4770 case EXPR_ADD: res = arg1 + arg2; break;
4771
4772 case EXPR_EQUAL: cmp = arg1 == arg2; break;
4773 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
4774 case EXPR_GREATER: cmp = arg1 > arg2; break;
4775 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
4776 case EXPR_SMALLER: cmp = arg1 < arg2; break;
4777 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
4778 default: cmp = 0; break;
4779 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004780 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004781 if (iptr->isn_type == ISN_COMPAREFLOAT)
4782 {
4783 tv1->v_type = VAR_BOOL;
4784 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
4785 }
4786 else
4787 tv1->vval.v_float = res;
4788 }
4789 break;
4790
4791 case ISN_COMPARELIST:
Bram Moolenaar265f8112021-12-19 12:33:05 +00004792 case ISN_COMPAREDICT:
4793 case ISN_COMPAREFUNC:
4794 case ISN_COMPARESTRING:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004795 case ISN_COMPAREBLOB:
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00004796 case ISN_COMPARECLASS:
4797 case ISN_COMPAREOBJECT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004798 {
4799 typval_T *tv1 = STACK_TV_BOT(-2);
4800 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar265f8112021-12-19 12:33:05 +00004801 exprtype_T exprtype = iptr->isn_arg.op.op_type;
4802 int ic = iptr->isn_arg.op.op_ic;
4803 int res = FALSE;
4804 int status = OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004805
Bram Moolenaar265f8112021-12-19 12:33:05 +00004806 SOURCING_LNUM = iptr->isn_lnum;
4807 if (iptr->isn_type == ISN_COMPARELIST)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004808 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00004809 status = typval_compare_list(tv1, tv2,
4810 exprtype, ic, &res);
4811 }
4812 else if (iptr->isn_type == ISN_COMPAREDICT)
4813 {
4814 status = typval_compare_dict(tv1, tv2,
4815 exprtype, ic, &res);
4816 }
4817 else if (iptr->isn_type == ISN_COMPAREFUNC)
4818 {
4819 status = typval_compare_func(tv1, tv2,
4820 exprtype, ic, &res);
4821 }
4822 else if (iptr->isn_type == ISN_COMPARESTRING)
4823 {
4824 status = typval_compare_string(tv1, tv2,
4825 exprtype, ic, &res);
4826 }
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00004827 else if (iptr->isn_type == ISN_COMPAREBLOB)
Bram Moolenaar265f8112021-12-19 12:33:05 +00004828 {
4829 status = typval_compare_blob(tv1, tv2, exprtype, &res);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004830 }
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00004831 else if (iptr->isn_type == ISN_COMPARECLASS)
4832 {
Bram Moolenaar03ff0c62023-01-02 20:38:01 +00004833 status = typval_compare_class(tv1, tv2,
4834 exprtype, FALSE, &res);
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00004835 }
4836 else // ISN_COMPAREOBJECT
4837 {
4838 status = typval_compare_object(tv1, tv2,
Bram Moolenaar03ff0c62023-01-02 20:38:01 +00004839 exprtype, FALSE, &res);
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00004840 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004841 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004842 clear_tv(tv1);
4843 clear_tv(tv2);
4844 tv1->v_type = VAR_BOOL;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004845 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4846 if (status == FAIL)
4847 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004848 }
4849 break;
4850
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004851 case ISN_COMPAREANY:
4852 {
4853 typval_T *tv1 = STACK_TV_BOT(-2);
4854 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar657137c2021-01-09 15:45:23 +01004855 exprtype_T exprtype = iptr->isn_arg.op.op_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004856 int ic = iptr->isn_arg.op.op_ic;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004857 int status;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004858
Bram Moolenaareb26f432020-09-14 16:50:05 +02004859 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004860 status = typval_compare(tv1, tv2, exprtype, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004861 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004862 --ectx->ec_stack.ga_len;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004863 if (status == FAIL)
4864 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004865 }
4866 break;
4867
4868 case ISN_ADDLIST:
4869 case ISN_ADDBLOB:
4870 {
4871 typval_T *tv1 = STACK_TV_BOT(-2);
4872 typval_T *tv2 = STACK_TV_BOT(-1);
4873
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004874 // add two lists or blobs
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004875 if (iptr->isn_type == ISN_ADDLIST)
Bram Moolenaar07802042021-09-09 23:01:14 +02004876 {
4877 if (iptr->isn_arg.op.op_type == EXPR_APPEND
4878 && tv1->vval.v_list != NULL)
4879 list_extend(tv1->vval.v_list, tv2->vval.v_list,
4880 NULL);
4881 else
4882 eval_addlist(tv1, tv2);
4883 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004884 else
4885 eval_addblob(tv1, tv2);
4886 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004887 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004888 }
4889 break;
4890
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004891 case ISN_LISTAPPEND:
4892 {
4893 typval_T *tv1 = STACK_TV_BOT(-2);
4894 typval_T *tv2 = STACK_TV_BOT(-1);
4895 list_T *l = tv1->vval.v_list;
4896
4897 // add an item to a list
Bram Moolenaar1f4a3452022-01-01 18:29:21 +00004898 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004899 if (l == NULL)
4900 {
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004901 emsg(_(e_cannot_add_to_null_list));
4902 goto on_error;
4903 }
Bram Moolenaar1f4a3452022-01-01 18:29:21 +00004904 if (value_check_lock(l->lv_lock, NULL, FALSE))
4905 goto on_error;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004906 if (list_append_tv(l, tv2) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004907 goto theend;
Bram Moolenaar955347c2020-10-19 23:01:46 +02004908 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004909 --ectx->ec_stack.ga_len;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004910 }
4911 break;
4912
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02004913 case ISN_BLOBAPPEND:
4914 {
4915 typval_T *tv1 = STACK_TV_BOT(-2);
4916 typval_T *tv2 = STACK_TV_BOT(-1);
4917 blob_T *b = tv1->vval.v_blob;
4918 int error = FALSE;
4919 varnumber_T n;
4920
4921 // add a number to a blob
4922 if (b == NULL)
4923 {
4924 SOURCING_LNUM = iptr->isn_lnum;
4925 emsg(_(e_cannot_add_to_null_blob));
4926 goto on_error;
4927 }
4928 n = tv_get_number_chk(tv2, &error);
4929 if (error)
4930 goto on_error;
4931 ga_append(&b->bv_ga, (int)n);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004932 --ectx->ec_stack.ga_len;
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02004933 }
4934 break;
4935
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004936 // Computation with two arguments of unknown type
4937 case ISN_OPANY:
4938 {
4939 typval_T *tv1 = STACK_TV_BOT(-2);
4940 typval_T *tv2 = STACK_TV_BOT(-1);
4941 varnumber_T n1, n2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004942 float_T f1 = 0, f2 = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004943 int error = FALSE;
4944
4945 if (iptr->isn_arg.op.op_type == EXPR_ADD)
4946 {
4947 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
4948 {
4949 eval_addlist(tv1, tv2);
4950 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004951 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004952 break;
4953 }
4954 else if (tv1->v_type == VAR_BLOB
4955 && tv2->v_type == VAR_BLOB)
4956 {
4957 eval_addblob(tv1, tv2);
4958 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004959 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004960 break;
4961 }
4962 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004963 if (tv1->v_type == VAR_FLOAT)
4964 {
4965 f1 = tv1->vval.v_float;
4966 n1 = 0;
4967 }
4968 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004969 {
Bram Moolenaarf665e972020-12-05 19:17:16 +01004970 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004971 n1 = tv_get_number_chk(tv1, &error);
4972 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02004973 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004974 if (tv2->v_type == VAR_FLOAT)
4975 f1 = n1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004976 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004977 if (tv2->v_type == VAR_FLOAT)
4978 {
4979 f2 = tv2->vval.v_float;
4980 n2 = 0;
4981 }
4982 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004983 {
4984 n2 = tv_get_number_chk(tv2, &error);
4985 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02004986 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004987 if (tv1->v_type == VAR_FLOAT)
4988 f2 = n2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004989 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004990 // if there is a float on either side the result is a float
4991 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
4992 {
4993 switch (iptr->isn_arg.op.op_type)
4994 {
4995 case EXPR_MULT: f1 = f1 * f2; break;
4996 case EXPR_DIV: f1 = f1 / f2; break;
4997 case EXPR_SUB: f1 = f1 - f2; break;
4998 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004999 default: SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005000 emsg(_(e_cannot_use_percent_with_float));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02005001 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005002 }
5003 clear_tv(tv1);
5004 clear_tv(tv2);
5005 tv1->v_type = VAR_FLOAT;
5006 tv1->vval.v_float = f1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005007 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005008 }
5009 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005010 {
Bram Moolenaarb1f28572021-01-21 13:03:20 +01005011 int failed = FALSE;
5012
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005013 switch (iptr->isn_arg.op.op_type)
5014 {
5015 case EXPR_MULT: n1 = n1 * n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01005016 case EXPR_DIV: n1 = num_divide(n1, n2, &failed);
5017 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01005018 goto on_error;
5019 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005020 case EXPR_SUB: n1 = n1 - n2; break;
5021 case EXPR_ADD: n1 = n1 + n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01005022 default: n1 = num_modulus(n1, n2, &failed);
5023 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01005024 goto on_error;
5025 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005026 }
5027 clear_tv(tv1);
5028 clear_tv(tv2);
5029 tv1->v_type = VAR_NUMBER;
5030 tv1->vval.v_number = n1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005031 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005032 }
5033 }
5034 break;
5035
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005036 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005037 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005038 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005039 int is_slice = iptr->isn_type == ISN_STRSLICE;
5040 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005041 char_u *res;
5042
5043 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005044 // string slice: string is at stack-3, first index at
5045 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005046 if (is_slice)
5047 {
5048 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005049 n1 = tv->vval.v_number;
5050 }
5051
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005052 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005053 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005054
Bram Moolenaar4c137212021-04-19 16:48:48 +02005055 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005056 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005057 if (is_slice)
5058 // Slice: Select the characters from the string
Bram Moolenaar6601b622021-01-13 21:47:15 +01005059 res = string_slice(tv->vval.v_string, n1, n2, FALSE);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005060 else
5061 // Index: The resulting variable is a string of a
Bram Moolenaar0289a092021-03-14 18:40:19 +01005062 // single character (including composing characters).
5063 // If the index is too big or negative the result is
5064 // empty.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02005065 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02005066 vim_free(tv->vval.v_string);
5067 tv->vval.v_string = res;
5068 }
5069 break;
5070
5071 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02005072 case ISN_LISTSLICE:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02005073 case ISN_BLOBINDEX:
5074 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005075 {
Bram Moolenaarcfc30232021-04-11 20:26:34 +02005076 int is_slice = iptr->isn_type == ISN_LISTSLICE
5077 || iptr->isn_type == ISN_BLOBSLICE;
5078 int is_blob = iptr->isn_type == ISN_BLOBINDEX
5079 || iptr->isn_type == ISN_BLOBSLICE;
Bram Moolenaared591872020-08-15 22:14:53 +02005080 varnumber_T n1, n2;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02005081 typval_T *val_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005082
5083 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02005084 // list slice: list is at stack-3, indexes at stack-2 and
5085 // stack-1
Bram Moolenaarcfc30232021-04-11 20:26:34 +02005086 // Same for blob.
5087 val_tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005088
5089 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02005090 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005091 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02005092
5093 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005094 {
Bram Moolenaared591872020-08-15 22:14:53 +02005095 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02005096 n1 = tv->vval.v_number;
5097 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005098 }
Bram Moolenaared591872020-08-15 22:14:53 +02005099
Bram Moolenaar4c137212021-04-19 16:48:48 +02005100 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02005101 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02005102 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02005103 if (is_blob)
5104 {
5105 if (blob_slice_or_index(val_tv->vval.v_blob, is_slice,
5106 n1, n2, FALSE, tv) == FAIL)
5107 goto on_error;
5108 }
5109 else
5110 {
5111 if (list_slice_or_index(val_tv->vval.v_list, is_slice,
5112 n1, n2, FALSE, tv, TRUE) == FAIL)
5113 goto on_error;
5114 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005115 }
5116 break;
5117
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005118 case ISN_ANYINDEX:
5119 case ISN_ANYSLICE:
5120 {
5121 int is_slice = iptr->isn_type == ISN_ANYSLICE;
5122 typval_T *var1, *var2;
5123 int res;
5124
5125 // index: composite is at stack-2, index at stack-1
5126 // slice: composite is at stack-3, indexes at stack-2 and
5127 // stack-1
5128 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02005129 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005130 if (check_can_index(tv, TRUE, TRUE) == FAIL)
5131 goto on_error;
5132 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
5133 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
Bram Moolenaar6601b622021-01-13 21:47:15 +01005134 res = eval_index_inner(tv, is_slice, var1, var2,
5135 FALSE, NULL, -1, TRUE);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005136 clear_tv(var1);
5137 if (is_slice)
5138 clear_tv(var2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005139 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005140 if (res == FAIL)
5141 goto on_error;
5142 }
5143 break;
5144
Bram Moolenaar9af78762020-06-16 11:34:42 +02005145 case ISN_SLICE:
5146 {
5147 list_T *list;
5148 int count = iptr->isn_arg.number;
5149
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02005150 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02005151 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02005152 list = tv->vval.v_list;
5153
5154 // no error for short list, expect it to be checked earlier
5155 if (list != NULL && list->lv_len >= count)
5156 {
5157 list_T *newlist = list_slice(list,
5158 count, list->lv_len - 1);
5159
5160 if (newlist != NULL)
5161 {
5162 list_unref(list);
5163 tv->vval.v_list = newlist;
5164 ++newlist->lv_refcount;
5165 }
5166 }
5167 }
5168 break;
5169
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005170 case ISN_GETITEM:
5171 {
5172 listitem_T *li;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02005173 getitem_T *gi = &iptr->isn_arg.getitem;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005174
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02005175 // Get list item: list is at stack-1, push item.
5176 // List type and length is checked for when compiling.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02005177 tv = STACK_TV_BOT(-1 - gi->gi_with_op);
5178 li = list_find(tv->vval.v_list, gi->gi_index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005179
Bram Moolenaar35578162021-08-02 19:10:38 +02005180 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005181 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005182 ++ectx->ec_stack.ga_len;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005183 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
Bram Moolenaarf785aa12021-02-11 21:19:34 +01005184
5185 // Useful when used in unpack assignment. Reset at
5186 // ISN_DROP.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02005187 ectx->ec_where.wt_index = gi->gi_index + 1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005188 ectx->ec_where.wt_variable = TRUE;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005189 }
5190 break;
5191
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005192 case ISN_MEMBER:
5193 {
5194 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005195 char_u *key;
5196 dictitem_T *di;
5197
5198 // dict member: dict is at stack-2, key at stack-1
5199 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02005200 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005201 dict = tv->vval.v_dict;
5202
5203 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02005204 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005205 key = tv->vval.v_string;
Bram Moolenaar086fc9a2020-10-30 18:33:02 +01005206 if (key == NULL)
5207 key = (char_u *)"";
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02005208
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005209 if ((di = dict_find(dict, key, -1)) == NULL)
5210 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005211 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00005212 semsg(_(e_key_not_present_in_dictionary_str), key);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01005213
5214 // If :silent! is used we will continue, make sure the
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005215 // stack contents makes sense and the dict stack is
5216 // updated.
Bram Moolenaar4029cab2020-12-05 18:13:27 +01005217 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005218 --ectx->ec_stack.ga_len;
Bram Moolenaar4029cab2020-12-05 18:13:27 +01005219 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005220 (void) dict_stack_save(tv);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01005221 tv->v_type = VAR_NUMBER;
5222 tv->vval.v_number = 0;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01005223 goto on_fatal_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005224 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005225 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005226 --ectx->ec_stack.ga_len;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005227 // Put the dict used on the dict stack, it might be used by
5228 // a dict function later.
Bram Moolenaar50788ef2020-07-05 16:51:26 +02005229 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005230 if (dict_stack_save(tv) == FAIL)
5231 goto on_fatal_error;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02005232 copy_tv(&di->di_tv, tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005233 }
5234 break;
5235
5236 // dict member with string key
5237 case ISN_STRINGMEMBER:
5238 {
5239 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005240 dictitem_T *di;
5241
5242 tv = STACK_TV_BOT(-1);
5243 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
5244 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005245 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005246 emsg(_(e_dictionary_required));
Bram Moolenaard032f342020-07-18 18:13:02 +02005247 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005248 }
5249 dict = tv->vval.v_dict;
5250
5251 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
5252 == NULL)
5253 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005254 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00005255 semsg(_(e_key_not_present_in_dictionary_str),
Bram Moolenaar381692b2022-02-02 20:01:27 +00005256 iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02005257 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005258 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005259 // Put the dict used on the dict stack, it might be used by
5260 // a dict function later.
5261 if (dict_stack_save(tv) == FAIL)
5262 goto on_fatal_error;
5263
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005264 copy_tv(&di->di_tv, tv);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005265 }
5266 break;
5267
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005268 case ISN_GET_OBJ_MEMBER:
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00005269 case ISN_GET_ITF_MEMBER:
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005270 {
5271 tv = STACK_TV_BOT(-1);
5272 if (tv->v_type != VAR_OBJECT)
5273 {
5274 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaard0200c82023-01-28 15:19:40 +00005275 object_required_error(tv);
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005276 goto on_error;
5277 }
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00005278
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005279 object_T *obj = tv->vval.v_object;
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00005280 int idx;
5281 if (iptr->isn_type == ISN_GET_OBJ_MEMBER)
5282 idx = iptr->isn_arg.number;
5283 else
5284 {
5285 idx = iptr->isn_arg.classmember.cm_idx;
5286 // convert the interface index to the object index
5287 idx = object_index_from_itf_index(
Bram Moolenaard0200c82023-01-28 15:19:40 +00005288 iptr->isn_arg.classmember.cm_class,
5289 FALSE, idx, obj->obj_class);
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00005290 }
5291
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005292 // the members are located right after the object struct
5293 typval_T *mtv = ((typval_T *)(obj + 1)) + idx;
Bram Moolenaar590162c2022-12-24 21:24:06 +00005294 copy_tv(mtv, tv);
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005295
5296 // Unreference the object after getting the member, it may
5297 // be freed.
5298 object_unref(obj);
5299 }
5300 break;
5301
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005302 case ISN_STORE_THIS:
5303 {
5304 int idx = iptr->isn_arg.number;
5305 object_T *obj = STACK_TV_VAR(0)->vval.v_object;
5306 // the members are located right after the object struct
5307 typval_T *mtv = ((typval_T *)(obj + 1)) + idx;
5308 clear_tv(mtv);
5309 *mtv = *STACK_TV_BOT(-1);
5310 --ectx->ec_stack.ga_len;
5311 }
5312 break;
5313
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005314 case ISN_CLEARDICT:
5315 dict_stack_drop();
5316 break;
5317
5318 case ISN_USEDICT:
5319 {
5320 typval_T *dict_tv = dict_stack_get_tv();
5321
5322 // Turn "dict.Func" into a partial for "Func" bound to
5323 // "dict". Don't do this when "Func" is already a partial
5324 // that was bound explicitly (pt_auto is FALSE).
5325 tv = STACK_TV_BOT(-1);
5326 if (dict_tv != NULL
5327 && dict_tv->v_type == VAR_DICT
5328 && dict_tv->vval.v_dict != NULL
5329 && (tv->v_type == VAR_FUNC
5330 || (tv->v_type == VAR_PARTIAL
5331 && (tv->vval.v_partial->pt_auto
5332 || tv->vval.v_partial->pt_dict == NULL))))
5333 dict_tv->vval.v_dict =
5334 make_partial(dict_tv->vval.v_dict, tv);
5335 dict_stack_drop();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005336 }
5337 break;
5338
5339 case ISN_NEGATENR:
5340 tv = STACK_TV_BOT(-1);
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00005341 // CHECKTYPE should have checked the variable type
Bram Moolenaarc58164c2020-03-29 18:40:30 +02005342 if (tv->v_type == VAR_FLOAT)
5343 tv->vval.v_float = -tv->vval.v_float;
5344 else
Bram Moolenaarc58164c2020-03-29 18:40:30 +02005345 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005346 break;
5347
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005348 case ISN_CHECKTYPE:
5349 {
5350 checktype_T *ct = &iptr->isn_arg.type;
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01005351 int save_wt_variable = ectx->ec_where.wt_variable;
Bram Moolenaarb1040dc2022-05-18 11:00:48 +01005352 int r;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005353
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01005354 tv = STACK_TV_BOT((int)ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02005355 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005356 if (!ectx->ec_where.wt_variable)
5357 ectx->ec_where.wt_index = ct->ct_arg_idx;
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01005358 ectx->ec_where.wt_variable = ct->ct_is_var;
Bram Moolenaarb1040dc2022-05-18 11:00:48 +01005359 r = check_typval_type(ct->ct_type, tv, ectx->ec_where);
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01005360 ectx->ec_where.wt_variable = save_wt_variable;
Bram Moolenaarb1040dc2022-05-18 11:00:48 +01005361 if (r == FAIL)
5362 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005363 if (!ectx->ec_where.wt_variable)
5364 ectx->ec_where.wt_index = 0;
Bram Moolenaar5e654232020-09-16 15:22:00 +02005365
5366 // number 0 is FALSE, number 1 is TRUE
5367 if (tv->v_type == VAR_NUMBER
5368 && ct->ct_type->tt_type == VAR_BOOL
5369 && (tv->vval.v_number == 0
5370 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005371 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02005372 tv->v_type = VAR_BOOL;
5373 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02005374 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005375 }
5376 }
5377 break;
5378
Bram Moolenaar9af78762020-06-16 11:34:42 +02005379 case ISN_CHECKLEN:
5380 {
5381 int min_len = iptr->isn_arg.checklen.cl_min_len;
5382 list_T *list = NULL;
5383
5384 tv = STACK_TV_BOT(-1);
5385 if (tv->v_type == VAR_LIST)
5386 list = tv->vval.v_list;
5387 if (list == NULL || list->lv_len < min_len
5388 || (list->lv_len > min_len
5389 && !iptr->isn_arg.checklen.cl_more_OK))
5390 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005391 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005392 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02005393 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02005394 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02005395 }
5396 }
5397 break;
5398
Bram Moolenaaraa210a32021-01-02 15:41:03 +01005399 case ISN_SETTYPE:
Bram Moolenaar381692b2022-02-02 20:01:27 +00005400 set_tv_type(STACK_TV_BOT(-1), iptr->isn_arg.type.ct_type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01005401 break;
5402
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005403 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005404 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005405 {
5406 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005407 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005408
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005409 if (iptr->isn_type == ISN_2BOOL)
5410 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005411 tv = STACK_TV_BOT(iptr->isn_arg.tobool.offset);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005412 n = tv2bool(tv);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005413 if (iptr->isn_arg.tobool.invert)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005414 n = !n;
5415 }
5416 else
5417 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005418 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005419 SOURCING_LNUM = iptr->isn_lnum;
5420 n = tv_get_bool_chk(tv, &error);
5421 if (error)
5422 goto on_error;
5423 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005424 clear_tv(tv);
5425 tv->v_type = VAR_BOOL;
5426 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
5427 }
5428 break;
5429
5430 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02005431 case ISN_2STRING_ANY:
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01005432 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005433 if (do_2string(STACK_TV_BOT(iptr->isn_arg.tostring.offset),
5434 iptr->isn_type == ISN_2STRING_ANY,
5435 iptr->isn_arg.tostring.tolerant) == FAIL)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01005436 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005437 break;
5438
Bram Moolenaar08597872020-12-10 19:43:40 +01005439 case ISN_RANGE:
5440 {
5441 exarg_T ea;
5442 char *errormsg;
5443
Bram Moolenaarece0b872021-01-08 20:40:45 +01005444 ea.line2 = 0;
Bram Moolenaard1510ee2021-01-04 16:15:58 +01005445 ea.addr_count = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01005446 ea.addr_type = ADDR_LINES;
5447 ea.cmd = iptr->isn_arg.string;
Bram Moolenaarece0b872021-01-08 20:40:45 +01005448 ea.skip = FALSE;
Bram Moolenaar08597872020-12-10 19:43:40 +01005449 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaarece0b872021-01-08 20:40:45 +01005450 goto on_error;
Bram Moolenaara28639e2021-01-19 22:48:09 +01005451
Bram Moolenaar35578162021-08-02 19:10:38 +02005452 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005453 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005454 ++ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01005455 tv = STACK_TV_BOT(-1);
5456 tv->v_type = VAR_NUMBER;
5457 tv->v_lock = 0;
Bram Moolenaar06651632022-04-27 17:54:25 +01005458 tv->vval.v_number = ea.line2;
Bram Moolenaar08597872020-12-10 19:43:40 +01005459 }
5460 break;
5461
Bram Moolenaarc3516f72020-09-08 22:45:35 +02005462 case ISN_PUT:
5463 {
5464 int regname = iptr->isn_arg.put.put_regname;
5465 linenr_T lnum = iptr->isn_arg.put.put_lnum;
5466 char_u *expr = NULL;
5467 int dir = FORWARD;
5468
Bram Moolenaar08597872020-12-10 19:43:40 +01005469 if (lnum < -2)
5470 {
5471 // line number was put on the stack by ISN_RANGE
5472 tv = STACK_TV_BOT(-1);
5473 curwin->w_cursor.lnum = tv->vval.v_number;
5474 if (lnum == LNUM_VARIABLE_RANGE_ABOVE)
5475 dir = BACKWARD;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005476 --ectx->ec_stack.ga_len;
Bram Moolenaar08597872020-12-10 19:43:40 +01005477 }
5478 else if (lnum == -2)
Bram Moolenaarc3516f72020-09-08 22:45:35 +02005479 // :put! above cursor
5480 dir = BACKWARD;
5481 else if (lnum >= 0)
Bram Moolenaar4e713ba2022-02-07 15:31:37 +00005482 {
5483 curwin->w_cursor.lnum = lnum;
5484 if (lnum == 0)
5485 // check_cursor() below will move to line 1
5486 dir = BACKWARD;
5487 }
Bram Moolenaara28639e2021-01-19 22:48:09 +01005488
5489 if (regname == '=')
5490 {
5491 tv = STACK_TV_BOT(-1);
5492 if (tv->v_type == VAR_STRING)
5493 expr = tv->vval.v_string;
5494 else
5495 {
5496 expr = typval2string(tv, TRUE); // allocates value
5497 clear_tv(tv);
5498 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005499 --ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01005500 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02005501 check_cursor();
5502 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
5503 vim_free(expr);
5504 }
5505 break;
5506
Bram Moolenaar02194d22020-10-24 23:08:38 +02005507 case ISN_CMDMOD:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005508 ectx->ec_funclocal.floc_save_cmdmod = cmdmod;
5509 ectx->ec_funclocal.floc_restore_cmdmod = TRUE;
5510 ectx->ec_funclocal.floc_restore_cmdmod_stacklen =
5511 ectx->ec_stack.ga_len;
Bram Moolenaar02194d22020-10-24 23:08:38 +02005512 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
5513 apply_cmdmod(&cmdmod);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02005514 break;
5515
Bram Moolenaar02194d22020-10-24 23:08:38 +02005516 case ISN_CMDMOD_REV:
5517 // filter regprog is owned by the instruction, don't free it
5518 cmdmod.cmod_filter_regmatch.regprog = NULL;
5519 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005520 cmdmod = ectx->ec_funclocal.floc_save_cmdmod;
5521 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02005522 break;
5523
Bram Moolenaar792f7862020-11-23 08:31:18 +01005524 case ISN_UNPACK:
5525 {
5526 int count = iptr->isn_arg.unpack.unp_count;
5527 int semicolon = iptr->isn_arg.unpack.unp_semicolon;
5528 list_T *l;
5529 listitem_T *li;
5530 int i;
5531
5532 // Check there is a valid list to unpack.
5533 tv = STACK_TV_BOT(-1);
5534 if (tv->v_type != VAR_LIST)
5535 {
5536 SOURCING_LNUM = iptr->isn_lnum;
5537 emsg(_(e_for_argument_must_be_sequence_of_lists));
5538 goto on_error;
5539 }
5540 l = tv->vval.v_list;
5541 if (l == NULL
5542 || l->lv_len < (semicolon ? count - 1 : count))
5543 {
5544 SOURCING_LNUM = iptr->isn_lnum;
5545 emsg(_(e_list_value_does_not_have_enough_items));
5546 goto on_error;
5547 }
5548 else if (!semicolon && l->lv_len > count)
5549 {
5550 SOURCING_LNUM = iptr->isn_lnum;
5551 emsg(_(e_list_value_has_more_items_than_targets));
5552 goto on_error;
5553 }
5554
5555 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar35578162021-08-02 19:10:38 +02005556 if (GA_GROW_FAILS(&ectx->ec_stack, count - 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005557 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005558 ectx->ec_stack.ga_len += count - 1;
Bram Moolenaar792f7862020-11-23 08:31:18 +01005559
5560 // Variable after semicolon gets a list with the remaining
5561 // items.
5562 if (semicolon)
5563 {
5564 list_T *rem_list =
5565 list_alloc_with_items(l->lv_len - count + 1);
5566
5567 if (rem_list == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005568 goto theend;
Bram Moolenaar792f7862020-11-23 08:31:18 +01005569 tv = STACK_TV_BOT(-count);
5570 tv->vval.v_list = rem_list;
5571 ++rem_list->lv_refcount;
5572 tv->v_lock = 0;
5573 li = l->lv_first;
5574 for (i = 0; i < count - 1; ++i)
5575 li = li->li_next;
5576 for (i = 0; li != NULL; ++i)
5577 {
Bram Moolenaar61efa162022-03-18 13:10:48 +00005578 typval_T tvcopy;
5579
5580 copy_tv(&li->li_tv, &tvcopy);
5581 list_set_item(rem_list, i, &tvcopy);
Bram Moolenaar792f7862020-11-23 08:31:18 +01005582 li = li->li_next;
5583 }
5584 --count;
5585 }
5586
5587 // Produce the values in reverse order, first item last.
5588 li = l->lv_first;
5589 for (i = 0; i < count; ++i)
5590 {
5591 tv = STACK_TV_BOT(-i - 1);
5592 copy_tv(&li->li_tv, tv);
5593 li = li->li_next;
5594 }
5595
5596 list_unref(l);
5597 }
5598 break;
5599
Bram Moolenaarb2049902021-01-24 12:53:53 +01005600 case ISN_PROF_START:
5601 case ISN_PROF_END:
5602 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01005603#ifdef FEAT_PROFILE
Bram Moolenaarca16c602022-09-06 18:57:08 +01005604 funccall_T cookie;
5605 ufunc_T *cur_ufunc =
Bram Moolenaarb2049902021-01-24 12:53:53 +01005606 (((dfunc_T *)def_functions.ga_data)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02005607 + ectx->ec_dfunc_idx)->df_ufunc;
Bram Moolenaarb2049902021-01-24 12:53:53 +01005608
Bram Moolenaarca16c602022-09-06 18:57:08 +01005609 cookie.fc_func = cur_ufunc;
Bram Moolenaarb2049902021-01-24 12:53:53 +01005610 if (iptr->isn_type == ISN_PROF_START)
5611 {
5612 func_line_start(&cookie, iptr->isn_lnum);
5613 // if we get here the instruction is executed
5614 func_line_exec(&cookie);
5615 }
5616 else
5617 func_line_end(&cookie);
Bram Moolenaarf002a412021-01-24 13:34:18 +01005618#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01005619 }
5620 break;
5621
Bram Moolenaare99d4222021-06-13 14:01:26 +02005622 case ISN_DEBUG:
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02005623 handle_debug(iptr, ectx);
Bram Moolenaare99d4222021-06-13 14:01:26 +02005624 break;
5625
Bram Moolenaar389df252020-07-09 21:20:47 +02005626 case ISN_SHUFFLE:
5627 {
Bram Moolenaar792f7862020-11-23 08:31:18 +01005628 typval_T tmp_tv;
5629 int item = iptr->isn_arg.shuffle.shfl_item;
5630 int up = iptr->isn_arg.shuffle.shfl_up;
Bram Moolenaar389df252020-07-09 21:20:47 +02005631
5632 tmp_tv = *STACK_TV_BOT(-item);
5633 for ( ; up > 0 && item > 1; --up)
5634 {
5635 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
5636 --item;
5637 }
5638 *STACK_TV_BOT(-item) = tmp_tv;
5639 }
5640 break;
5641
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005642 case ISN_DROP:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005643 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005644 clear_tv(STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005645 ectx->ec_where.wt_index = 0;
5646 ectx->ec_where.wt_variable = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005647 break;
5648 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02005649 continue;
5650
Bram Moolenaard032f342020-07-18 18:13:02 +02005651func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02005652 // Restore previous function. If the frame pointer is where we started
5653 // then there is none and we are done.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005654 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02005655 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02005656
Bram Moolenaar4c137212021-04-19 16:48:48 +02005657 if (func_return(ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02005658 // only fails when out of memory
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005659 goto theend;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02005660 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02005661
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02005662on_error:
Bram Moolenaaraf0df472020-12-02 20:51:22 +01005663 // Jump here for an error that does not require aborting execution.
Bram Moolenaar56602ba2020-12-05 21:22:08 +01005664 // If "emsg_silent" is set then ignore the error, unless it was set
5665 // when calling the function.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005666 if (did_emsg_cumul + did_emsg == ectx->ec_did_emsg_before
Bram Moolenaar56602ba2020-12-05 21:22:08 +01005667 && emsg_silent && did_emsg_def == 0)
Bram Moolenaarf9041332021-01-21 19:41:16 +01005668 {
5669 // If a sequence of instructions causes an error while ":silent!"
5670 // was used, restore the stack length and jump ahead to restoring
5671 // the cmdmod.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005672 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaarf9041332021-01-21 19:41:16 +01005673 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005674 while (ectx->ec_stack.ga_len
5675 > ectx->ec_funclocal.floc_restore_cmdmod_stacklen)
Bram Moolenaarf9041332021-01-21 19:41:16 +01005676 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005677 --ectx->ec_stack.ga_len;
Bram Moolenaarf9041332021-01-21 19:41:16 +01005678 clear_tv(STACK_TV_BOT(0));
5679 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005680 while (ectx->ec_instr[ectx->ec_iidx].isn_type != ISN_CMDMOD_REV)
5681 ++ectx->ec_iidx;
Bram Moolenaarf9041332021-01-21 19:41:16 +01005682 }
Bram Moolenaarcd030c42020-10-30 21:49:40 +01005683 continue;
Bram Moolenaarf9041332021-01-21 19:41:16 +01005684 }
Bram Moolenaaraf0df472020-12-02 20:51:22 +01005685on_fatal_error:
5686 // Jump here for an error that messes up the stack.
Bram Moolenaar171fb922020-10-28 16:54:47 +01005687 // If we are not inside a try-catch started here, abort execution.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005688 if (trylevel <= ectx->ec_trylevel_at_start)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005689 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005690 }
5691
5692done:
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005693 ret = OK;
5694theend:
Bram Moolenaar58779852022-09-06 18:31:14 +01005695 may_invoke_defer_funcs(ectx);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01005696
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005697 dict_stack_clear(dict_stack_len_at_start);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005698 ectx->ec_trylevel_at_start = save_trylevel_at_start;
5699 return ret;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005700}
5701
5702/*
Bram Moolenaar806a2732022-09-04 15:40:36 +01005703 * Execute the instructions from a VAR_INSTR typval and put the result in
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005704 * "rettv".
5705 * Return OK or FAIL.
5706 */
5707 int
5708exe_typval_instr(typval_T *tv, typval_T *rettv)
5709{
5710 ectx_T *ectx = tv->vval.v_instr->instr_ectx;
5711 isn_T *save_instr = ectx->ec_instr;
5712 int save_iidx = ectx->ec_iidx;
5713 int res;
5714
LemonBoyf3b48952022-05-05 13:53:03 +01005715 // Initialize rettv so that it is safe for caller to invoke clear_tv(rettv)
5716 // even when the compilation fails.
5717 rettv->v_type = VAR_UNKNOWN;
5718
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005719 ectx->ec_instr = tv->vval.v_instr->instr_instr;
5720 res = exec_instructions(ectx);
5721 if (res == OK)
5722 {
5723 *rettv = *STACK_TV_BOT(-1);
5724 --ectx->ec_stack.ga_len;
5725 }
5726
5727 ectx->ec_instr = save_instr;
5728 ectx->ec_iidx = save_iidx;
5729
5730 return res;
5731}
5732
5733/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02005734 * Execute the instructions from an ISN_SUBSTITUTE command, which are in
5735 * "substitute_instr".
5736 */
5737 char_u *
5738exe_substitute_instr(void)
5739{
5740 ectx_T *ectx = substitute_instr->subs_ectx;
5741 isn_T *save_instr = ectx->ec_instr;
5742 int save_iidx = ectx->ec_iidx;
5743 char_u *res;
5744
5745 ectx->ec_instr = substitute_instr->subs_instr;
5746 if (exec_instructions(ectx) == OK)
Bram Moolenaar90193e62021-04-04 20:49:50 +02005747 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005748 typval_T *tv = STACK_TV_BOT(-1);
5749
Bram Moolenaar27523602021-06-05 21:36:19 +02005750 res = typval2string(tv, TRUE);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005751 --ectx->ec_stack.ga_len;
5752 clear_tv(tv);
Bram Moolenaar90193e62021-04-04 20:49:50 +02005753 }
5754 else
5755 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005756 substitute_instr->subs_status = FAIL;
5757 res = vim_strsave((char_u *)"");
Bram Moolenaar90193e62021-04-04 20:49:50 +02005758 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005759
Bram Moolenaar4c137212021-04-19 16:48:48 +02005760 ectx->ec_instr = save_instr;
5761 ectx->ec_iidx = save_iidx;
5762
5763 return res;
5764}
5765
5766/*
5767 * Call a "def" function from old Vim script.
5768 * Return OK or FAIL.
5769 */
5770 int
5771call_def_function(
5772 ufunc_T *ufunc,
5773 int argc_arg, // nr of arguments
5774 typval_T *argv, // arguments
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005775 int flags, // DEF_ flags
Bram Moolenaar4c137212021-04-19 16:48:48 +02005776 partial_T *partial, // optional partial for context
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005777 object_T *object, // object, e.g. for this.Func()
Bram Moolenaar58779852022-09-06 18:31:14 +01005778 funccall_T *funccal,
Bram Moolenaar4c137212021-04-19 16:48:48 +02005779 typval_T *rettv) // return value
5780{
5781 ectx_T ectx; // execution context
5782 int argc = argc_arg;
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005783 int partial_argc = partial == NULL
5784 || (flags & DEF_USE_PT_ARGV) == 0
5785 ? 0 : partial->pt_argc;
5786 int total_argc = argc + partial_argc;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005787 typval_T *tv;
5788 int idx;
5789 int ret = FAIL;
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005790 int defcount = ufunc->uf_args.ga_len - total_argc;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005791 sctx_T save_current_sctx = current_sctx;
5792 int did_emsg_before = did_emsg_cumul + did_emsg;
5793 int save_suppress_errthrow = suppress_errthrow;
5794 msglist_T **saved_msg_list = NULL;
5795 msglist_T *private_msg_list = NULL;
5796 int save_emsg_silent_def = emsg_silent_def;
5797 int save_did_emsg_def = did_emsg_def;
5798 int orig_funcdepth;
Bram Moolenaare99d4222021-06-13 14:01:26 +02005799 int orig_nesting_level = ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005800
5801// Get pointer to item in the stack.
5802#undef STACK_TV
5803#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
5804
5805// Get pointer to item at the bottom of the stack, -1 is the bottom.
5806#undef STACK_TV_BOT
5807#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
5808
5809// Get pointer to a local variable on the stack. Negative for arguments.
5810#undef STACK_TV_VAR
5811#define STACK_TV_VAR(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_frame_idx + STACK_FRAME_SIZE + idx)
5812
5813 if (ufunc->uf_def_status == UF_NOT_COMPILED
5814 || ufunc->uf_def_status == UF_COMPILE_ERROR
Bram Moolenaar139575d2022-03-15 19:29:30 +00005815 || (func_needs_compiling(ufunc, get_compile_type(ufunc))
5816 && compile_def_function(ufunc, FALSE,
5817 get_compile_type(ufunc), NULL) == FAIL))
Bram Moolenaar4c137212021-04-19 16:48:48 +02005818 {
5819 if (did_emsg_cumul + did_emsg == did_emsg_before)
5820 semsg(_(e_function_is_not_compiled_str),
5821 printable_func_name(ufunc));
5822 return FAIL;
5823 }
5824
5825 {
5826 // Check the function was really compiled.
5827 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5828 + ufunc->uf_dfunc_idx;
Bram Moolenaarcc341812022-09-19 15:54:34 +01005829 if (dfunc->df_ufunc == NULL)
5830 {
5831 semsg(_(e_function_was_deleted_str), printable_func_name(ufunc));
5832 return FAIL;
5833 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005834 if (INSTRUCTIONS(dfunc) == NULL)
5835 {
5836 iemsg("using call_def_function() on not compiled function");
5837 return FAIL;
5838 }
5839 }
5840
5841 // If depth of calling is getting too high, don't execute the function.
5842 orig_funcdepth = funcdepth_get();
5843 if (funcdepth_increment() == FAIL)
5844 return FAIL;
5845
5846 CLEAR_FIELD(ectx);
5847 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
5848 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
Bram Moolenaar35578162021-08-02 19:10:38 +02005849 if (GA_GROW_FAILS(&ectx.ec_stack, 20))
Bram Moolenaar4c137212021-04-19 16:48:48 +02005850 {
5851 funcdepth_decrement();
5852 return FAIL;
5853 }
5854 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
5855 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
5856 ectx.ec_did_emsg_before = did_emsg_before;
Bram Moolenaare99d4222021-06-13 14:01:26 +02005857 ++ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005858
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005859 idx = total_argc - ufunc->uf_args.ga_len;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005860 if (idx > 0 && ufunc->uf_va_name == NULL)
5861 {
Matvey Tarasovd14bb1a2022-06-29 13:18:27 +01005862 semsg(NGETTEXT(e_one_argument_too_many, e_nr_arguments_too_many,
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005863 idx), idx);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005864 goto failed_early;
5865 }
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005866 idx = total_argc - ufunc->uf_args.ga_len + ufunc->uf_def_args.ga_len;
Bram Moolenaarc6d71532021-06-05 18:49:38 +02005867 if (idx < 0)
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02005868 {
Matvey Tarasovd14bb1a2022-06-29 13:18:27 +01005869 semsg(NGETTEXT(e_one_argument_too_few, e_nr_arguments_too_few,
Bram Moolenaar98aff652022-09-06 21:02:35 +01005870 -idx), -idx);
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02005871 goto failed_early;
5872 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005873
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005874 // Put values from the partial and arguments on the stack, but no more than
5875 // what the function expects. A lambda can be called with more arguments
5876 // than it uses.
5877 for (idx = 0; idx < total_argc
Bram Moolenaar4c137212021-04-19 16:48:48 +02005878 && (ufunc->uf_va_name != NULL || idx < ufunc->uf_args.ga_len);
5879 ++idx)
5880 {
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005881 int argv_idx = idx - partial_argc;
5882
5883 tv = idx < partial_argc ? partial->pt_argv + idx : argv + argv_idx;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005884 if (idx >= ufunc->uf_args.ga_len - ufunc->uf_def_args.ga_len
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005885 && tv->v_type == VAR_SPECIAL
5886 && tv->vval.v_number == VVAL_NONE)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005887 {
5888 // Use the default value.
5889 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
5890 }
5891 else
5892 {
Bram Moolenaar47bba532023-01-20 18:49:46 +00005893 int done = FALSE;
5894 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len)
5895 {
5896 type_T *expected = ufunc->uf_arg_types[idx];
5897 if (expected->tt_type == VAR_FLOAT && tv->v_type == VAR_NUMBER)
5898 {
5899 // When a float is expected and a number was given, convert
5900 // the value.
5901 STACK_TV_BOT(0)->v_type = VAR_FLOAT;
5902 STACK_TV_BOT(0)->v_lock = 0;
5903 STACK_TV_BOT(0)->vval.v_float = tv->vval.v_number;
5904 done = TRUE;
5905 }
5906 else if (check_typval_arg_type(expected, tv,
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005907 NULL, argv_idx + 1) == FAIL)
Bram Moolenaar47bba532023-01-20 18:49:46 +00005908 goto failed_early;
5909 }
5910 if (!done)
5911 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005912 }
5913 ++ectx.ec_stack.ga_len;
5914 }
5915
5916 // Turn varargs into a list. Empty list if no args.
5917 if (ufunc->uf_va_name != NULL)
5918 {
5919 int vararg_count = argc - ufunc->uf_args.ga_len;
5920
5921 if (vararg_count < 0)
5922 vararg_count = 0;
5923 else
5924 argc -= vararg_count;
5925 if (exe_newlist(vararg_count, &ectx) == FAIL)
5926 goto failed_early;
5927
5928 // Check the type of the list items.
5929 tv = STACK_TV_BOT(-1);
5930 if (ufunc->uf_va_type != NULL
5931 && ufunc->uf_va_type != &t_list_any
5932 && ufunc->uf_va_type->tt_member != &t_any
5933 && tv->vval.v_list != NULL)
5934 {
5935 type_T *expected = ufunc->uf_va_type->tt_member;
5936 listitem_T *li = tv->vval.v_list->lv_first;
5937
5938 for (idx = 0; idx < vararg_count; ++idx)
5939 {
5940 if (check_typval_arg_type(expected, &li->li_tv,
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02005941 NULL, argc + idx + 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005942 goto failed_early;
5943 li = li->li_next;
5944 }
5945 }
5946
5947 if (defcount > 0)
5948 // Move varargs list to below missing default arguments.
5949 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
5950 --ectx.ec_stack.ga_len;
5951 }
5952
5953 // Make space for omitted arguments, will store default value below.
5954 // Any varargs list goes after them.
5955 if (defcount > 0)
5956 for (idx = 0; idx < defcount; ++idx)
5957 {
5958 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
5959 ++ectx.ec_stack.ga_len;
5960 }
5961 if (ufunc->uf_va_name != NULL)
5962 ++ectx.ec_stack.ga_len;
5963
5964 // Frame pointer points to just after arguments.
5965 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
5966 ectx.ec_initial_frame_idx = ectx.ec_frame_idx;
5967
5968 {
5969 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5970 + ufunc->uf_dfunc_idx;
5971 ufunc_T *base_ufunc = dfunc->df_ufunc;
5972
5973 // "uf_partial" is on the ufunc that "df_ufunc" points to, as is done
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01005974 // by copy_lambda_to_global_func().
Bram Moolenaar4c137212021-04-19 16:48:48 +02005975 if (partial != NULL || base_ufunc->uf_partial != NULL)
5976 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005977 ectx.ec_outer_ref = ALLOC_CLEAR_ONE(outer_ref_T);
5978 if (ectx.ec_outer_ref == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005979 goto failed_early;
5980 if (partial != NULL)
5981 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00005982 outer_T *outer = get_pt_outer(partial);
5983
Bram Moolenaar6d313be2022-09-22 16:36:25 +01005984 if (outer->out_stack == NULL && outer->out_loop_size == 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005985 {
Bram Moolenaar6d313be2022-09-22 16:36:25 +01005986 // no stack was set
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00005987 if (current_ectx != NULL)
5988 {
5989 if (current_ectx->ec_outer_ref != NULL
5990 && current_ectx->ec_outer_ref->or_outer != NULL)
5991 ectx.ec_outer_ref->or_outer =
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005992 current_ectx->ec_outer_ref->or_outer;
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00005993 }
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005994 // else: should there be an error here?
Bram Moolenaar4c137212021-04-19 16:48:48 +02005995 }
5996 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005997 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00005998 ectx.ec_outer_ref->or_outer = outer;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005999 ++partial->pt_refcount;
6000 ectx.ec_outer_ref->or_partial = partial;
6001 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006002 }
6003 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006004 {
6005 ectx.ec_outer_ref->or_outer = &base_ufunc->uf_partial->pt_outer;
6006 ++base_ufunc->uf_partial->pt_refcount;
6007 ectx.ec_outer_ref->or_partial = base_ufunc->uf_partial;
6008 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006009 }
6010 }
6011
6012 // dummy frame entries
6013 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
6014 {
6015 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
6016 ++ectx.ec_stack.ga_len;
6017 }
6018
6019 {
6020 // Reserve space for local variables and any closure reference count.
6021 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
6022 + ufunc->uf_dfunc_idx;
6023
Bram Moolenaar5cd64792021-12-25 18:23:24 +00006024 // Initialize variables to zero. That avoids having to generate
6025 // initializing instructions for "var nr: number", "var x: any", etc.
Bram Moolenaar4c137212021-04-19 16:48:48 +02006026 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaar5cd64792021-12-25 18:23:24 +00006027 {
6028 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
6029 STACK_TV_VAR(idx)->vval.v_number = 0;
6030 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006031 ectx.ec_stack.ga_len += dfunc->df_varcount;
Bram Moolenaarffdaca92022-12-09 21:41:48 +00006032
6033 if (object != NULL)
6034 {
6035 // the object is always the variable at index zero
6036 tv = STACK_TV_VAR(0);
6037 tv->v_type = VAR_OBJECT;
6038 tv->vval.v_object = object;
6039 }
6040
Bram Moolenaar4c137212021-04-19 16:48:48 +02006041 if (dfunc->df_has_closure)
6042 {
Bram Moolenaarcc341812022-09-19 15:54:34 +01006043 // Initialize the variable that counts how many closures were
6044 // created. This is used in handle_closure_in_use().
Bram Moolenaar4c137212021-04-19 16:48:48 +02006045 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
6046 STACK_TV_VAR(idx)->vval.v_number = 0;
6047 ++ectx.ec_stack.ga_len;
6048 }
6049
6050 ectx.ec_instr = INSTRUCTIONS(dfunc);
6051 }
6052
Bram Moolenaar58779852022-09-06 18:31:14 +01006053 // Store the execution context in funccal, used by invoke_all_defer().
6054 if (funccal != NULL)
6055 funccal->fc_ectx = &ectx;
6056
Bram Moolenaar4c137212021-04-19 16:48:48 +02006057 // Following errors are in the function, not the caller.
6058 // Commands behave like vim9script.
6059 estack_push_ufunc(ufunc, 1);
6060 current_sctx = ufunc->uf_script_ctx;
6061 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
6062
6063 // Use a specific location for storing error messages to be converted to an
6064 // exception.
6065 saved_msg_list = msg_list;
6066 msg_list = &private_msg_list;
6067
6068 // Do turn errors into exceptions.
6069 suppress_errthrow = FALSE;
6070
6071 // Do not delete the function while executing it.
6072 ++ufunc->uf_calls;
6073
6074 // When ":silent!" was used before calling then we still abort the
6075 // function. If ":silent!" is used in the function then we don't.
6076 emsg_silent_def = emsg_silent;
6077 did_emsg_def = 0;
6078
6079 ectx.ec_where.wt_index = 0;
6080 ectx.ec_where.wt_variable = FALSE;
6081
Bram Moolenaar5800c792022-09-22 17:34:01 +01006082 /*
6083 * Execute the instructions until done.
6084 */
Bram Moolenaar4c137212021-04-19 16:48:48 +02006085 ret = exec_instructions(&ectx);
6086 if (ret == OK)
6087 {
6088 // function finished, get result from the stack.
6089 if (ufunc->uf_ret_type == &t_void)
6090 {
6091 rettv->v_type = VAR_VOID;
6092 }
6093 else
6094 {
6095 tv = STACK_TV_BOT(-1);
6096 *rettv = *tv;
6097 tv->v_type = VAR_UNKNOWN;
6098 }
6099 }
6100
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01006101 // When failed need to unwind the call stack.
Bram Moolenaar58779852022-09-06 18:31:14 +01006102 unwind_def_callstack(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02006103
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02006104 // Deal with any remaining closures, they may be in use somewhere.
6105 if (ectx.ec_funcrefs.ga_len > 0)
Bram Moolenaarf112f302020-12-20 17:47:52 +01006106 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02006107 handle_closure_in_use(&ectx, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00006108 ga_clear(&ectx.ec_funcrefs);
Bram Moolenaarf112f302020-12-20 17:47:52 +01006109 }
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02006110
Bram Moolenaaree8580e2020-08-28 17:19:07 +02006111 estack_pop();
6112 current_sctx = save_current_sctx;
6113
Bram Moolenaar2336c372021-12-06 15:06:54 +00006114 if (--ufunc->uf_calls <= 0 && ufunc->uf_refcount <= 0)
6115 // Function was unreferenced while being used, free it now.
6116 func_clear_free(ufunc, FALSE);
Bram Moolenaarc970e422021-03-17 15:03:04 +01006117
Bram Moolenaar352134b2020-10-17 22:04:08 +02006118 if (*msg_list != NULL && saved_msg_list != NULL)
6119 {
6120 msglist_T **plist = saved_msg_list;
6121
6122 // Append entries from the current msg_list (uncaught exceptions) to
6123 // the saved msg_list.
6124 while (*plist != NULL)
6125 plist = &(*plist)->next;
6126
6127 *plist = *msg_list;
6128 }
6129 msg_list = saved_msg_list;
6130
Bram Moolenaar4c137212021-04-19 16:48:48 +02006131 if (ectx.ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar02194d22020-10-24 23:08:38 +02006132 {
6133 cmdmod.cmod_filter_regmatch.regprog = NULL;
6134 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006135 cmdmod = ectx.ec_funclocal.floc_save_cmdmod;
Bram Moolenaar02194d22020-10-24 23:08:38 +02006136 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +01006137 emsg_silent_def = save_emsg_silent_def;
6138 did_emsg_def += save_did_emsg_def;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02006139
Bram Moolenaaree8580e2020-08-28 17:19:07 +02006140failed_early:
Bram Moolenaar0d1f55c2022-04-05 17:30:29 +01006141 // Free all arguments and local variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006142 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
Bram Moolenaar1936c762022-09-28 15:19:10 +01006143 {
6144 tv = STACK_TV(idx);
6145 if (tv->v_type != VAR_NUMBER && tv->v_type != VAR_UNKNOWN)
6146 clear_tv(tv);
6147 }
Bram Moolenaare99d4222021-06-13 14:01:26 +02006148 ex_nesting_level = orig_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02006149
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006150 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01006151 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006152 if (ectx.ec_outer_ref != NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +01006153 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006154 if (ectx.ec_outer_ref->or_outer_allocated)
6155 vim_free(ectx.ec_outer_ref->or_outer);
6156 partial_unref(ectx.ec_outer_ref->or_partial);
6157 vim_free(ectx.ec_outer_ref);
Bram Moolenaar0186e582021-01-10 18:33:11 +01006158 }
6159
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02006160 // Not sure if this is necessary.
6161 suppress_errthrow = save_suppress_errthrow;
6162
Bram Moolenaarb5841b92021-07-15 18:09:53 +02006163 if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before
6164 && !need_rethrow)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006165 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02006166 printable_func_name(ufunc));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01006167 funcdepth_restore(orig_funcdepth);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006168 return ret;
6169}
6170
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006171/*
Bram Moolenaar58779852022-09-06 18:31:14 +01006172 * Called when a def function has finished (possibly failed).
6173 * Invoke all the function returns to clean up and invoke deferred functions,
6174 * except the toplevel one.
6175 */
6176 void
6177unwind_def_callstack(ectx_T *ectx)
6178{
6179 while (ectx->ec_frame_idx != ectx->ec_initial_frame_idx)
6180 func_return(ectx);
6181}
6182
6183/*
dundargocc57b5bc2022-11-02 13:30:51 +00006184 * Invoke any deferred functions for the top function in "ectx".
Bram Moolenaar58779852022-09-06 18:31:14 +01006185 */
6186 void
6187may_invoke_defer_funcs(ectx_T *ectx)
6188{
6189 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
6190
6191 if (dfunc->df_defer_var_idx > 0)
6192 invoke_defer_funcs(ectx);
6193}
6194
6195/*
Bram Moolenaarcc341812022-09-19 15:54:34 +01006196 * Return loopvarinfo in a printable form in allocated memory.
6197 */
6198 static char_u *
6199printable_loopvarinfo(loopvarinfo_T *lvi)
6200{
6201 garray_T ga;
6202 int depth;
6203
6204 ga_init2(&ga, 1, 100);
6205 for (depth = 0; depth < lvi->lvi_depth; ++depth)
6206 {
6207 if (ga_grow(&ga, 50) == FAIL)
6208 break;
6209 if (lvi->lvi_loop[depth].var_idx == 0)
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +01006210 STRCPY((char *)ga.ga_data + ga.ga_len, " -");
Bram Moolenaarcc341812022-09-19 15:54:34 +01006211 else
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +01006212 vim_snprintf((char *)ga.ga_data + ga.ga_len, 50, " $%d-$%d",
Bram Moolenaarcc341812022-09-19 15:54:34 +01006213 lvi->lvi_loop[depth].var_idx,
6214 lvi->lvi_loop[depth].var_idx
6215 + lvi->lvi_loop[depth].var_count - 1);
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +01006216 ga.ga_len = (int)STRLEN(ga.ga_data);
Bram Moolenaarcc341812022-09-19 15:54:34 +01006217 }
6218 return ga.ga_data;
6219}
6220
6221/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02006222 * List instructions "instr" up to "instr_count" or until ISN_FINISH.
6223 * "ufunc" has the source lines, NULL for the instructions of ISN_SUBSTITUTE.
6224 * "pfx" is prefixed to every line.
6225 */
6226 static void
6227list_instructions(char *pfx, isn_T *instr, int instr_count, ufunc_T *ufunc)
6228{
6229 int line_idx = 0;
6230 int prev_current = 0;
6231 int current;
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02006232 int def_arg_idx = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006233
6234 for (current = 0; current < instr_count; ++current)
6235 {
6236 isn_T *iptr = &instr[current];
6237 char *line;
6238
6239 if (ufunc != NULL)
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02006240 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02006241 while (line_idx < iptr->isn_lnum
6242 && line_idx < ufunc->uf_lines.ga_len)
6243 {
6244 if (current > prev_current)
6245 {
6246 msg_puts("\n\n");
6247 prev_current = current;
6248 }
6249 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
6250 if (line != NULL)
6251 msg(line);
6252 }
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02006253 if (iptr->isn_type == ISN_JUMP_IF_ARG_SET)
6254 {
6255 int first_def_arg = ufunc->uf_args.ga_len
6256 - ufunc->uf_def_args.ga_len;
6257
6258 if (def_arg_idx > 0)
6259 msg_puts("\n\n");
6260 msg_start();
6261 msg_puts(" ");
6262 msg_puts(((char **)(ufunc->uf_args.ga_data))[
6263 first_def_arg + def_arg_idx]);
6264 msg_puts(" = ");
6265 msg_puts(((char **)(ufunc->uf_def_args.ga_data))[def_arg_idx++]);
6266 msg_clr_eos();
6267 msg_end();
6268 }
6269 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006270
6271 switch (iptr->isn_type)
6272 {
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006273 case ISN_CONSTRUCT:
6274 smsg("%s%4d NEW %s size %d", pfx, current,
6275 iptr->isn_arg.construct.construct_class->class_name,
6276 (int)iptr->isn_arg.construct.construct_size);
6277 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006278 case ISN_EXEC:
6279 smsg("%s%4d EXEC %s", pfx, current, iptr->isn_arg.string);
6280 break;
Bram Moolenaar20677332021-06-06 17:02:53 +02006281 case ISN_EXEC_SPLIT:
6282 smsg("%s%4d EXEC_SPLIT %s", pfx, current, iptr->isn_arg.string);
6283 break;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00006284 case ISN_EXECRANGE:
6285 smsg("%s%4d EXECRANGE %s", pfx, current, iptr->isn_arg.string);
6286 break;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02006287 case ISN_LEGACY_EVAL:
6288 smsg("%s%4d EVAL legacy %s", pfx, current,
6289 iptr->isn_arg.string);
6290 break;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02006291 case ISN_REDIRSTART:
6292 smsg("%s%4d REDIR", pfx, current);
6293 break;
6294 case ISN_REDIREND:
6295 smsg("%s%4d REDIR END%s", pfx, current,
6296 iptr->isn_arg.number ? " append" : "");
6297 break;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006298 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02006299#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006300 smsg("%s%4d CEXPR pre %s", pfx, current,
6301 cexpr_get_auname(iptr->isn_arg.number));
Bram Moolenaarb7c97812021-05-05 22:51:39 +02006302#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006303 break;
6304 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02006305#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006306 {
6307 cexprref_T *cer = iptr->isn_arg.cexpr.cexpr_ref;
6308
6309 smsg("%s%4d CEXPR core %s%s \"%s\"", pfx, current,
6310 cexpr_get_auname(cer->cer_cmdidx),
6311 cer->cer_forceit ? "!" : "",
6312 cer->cer_cmdline);
6313 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02006314#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006315 break;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006316 case ISN_INSTR:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006317 smsg("%s%4d INSTR", pfx, current);
6318 list_instructions(" ", iptr->isn_arg.instr, INT_MAX, NULL);
6319 msg(" -------------");
6320 break;
6321 case ISN_SOURCE:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006322 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006323 scriptitem_T *si = SCRIPT_ITEM(iptr->isn_arg.number);
6324
6325 smsg("%s%4d SOURCE %s", pfx, current, si->sn_name);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006326 }
6327 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006328 case ISN_SUBSTITUTE:
6329 {
6330 subs_T *subs = &iptr->isn_arg.subs;
6331
6332 smsg("%s%4d SUBSTITUTE %s", pfx, current, subs->subs_cmd);
6333 list_instructions(" ", subs->subs_instr, INT_MAX, NULL);
6334 msg(" -------------");
6335 }
6336 break;
6337 case ISN_EXECCONCAT:
6338 smsg("%s%4d EXECCONCAT %lld", pfx, current,
6339 (varnumber_T)iptr->isn_arg.number);
6340 break;
6341 case ISN_ECHO:
6342 {
6343 echo_T *echo = &iptr->isn_arg.echo;
6344
6345 smsg("%s%4d %s %d", pfx, current,
6346 echo->echo_with_white ? "ECHO" : "ECHON",
6347 echo->echo_count);
6348 }
6349 break;
6350 case ISN_EXECUTE:
6351 smsg("%s%4d EXECUTE %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02006352 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006353 break;
6354 case ISN_ECHOMSG:
6355 smsg("%s%4d ECHOMSG %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02006356 (varnumber_T)(iptr->isn_arg.number));
6357 break;
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01006358 case ISN_ECHOWINDOW:
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01006359 if (iptr->isn_arg.echowin.ewin_time > 0)
6360 smsg("%s%4d ECHOWINDOW %d (%ld sec)", pfx, current,
6361 iptr->isn_arg.echowin.ewin_count,
6362 iptr->isn_arg.echowin.ewin_time);
6363 else
6364 smsg("%s%4d ECHOWINDOW %d", pfx, current,
6365 iptr->isn_arg.echowin.ewin_count);
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01006366 break;
Bram Moolenaar7de62622021-08-07 15:05:47 +02006367 case ISN_ECHOCONSOLE:
6368 smsg("%s%4d ECHOCONSOLE %lld", pfx, current,
6369 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006370 break;
6371 case ISN_ECHOERR:
6372 smsg("%s%4d ECHOERR %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02006373 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006374 break;
6375 case ISN_LOAD:
6376 {
6377 if (iptr->isn_arg.number < 0)
6378 smsg("%s%4d LOAD arg[%lld]", pfx, current,
6379 (varnumber_T)(iptr->isn_arg.number
6380 + STACK_FRAME_SIZE));
6381 else
6382 smsg("%s%4d LOAD $%lld", pfx, current,
6383 (varnumber_T)(iptr->isn_arg.number));
6384 }
6385 break;
6386 case ISN_LOADOUTER:
6387 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006388 isn_outer_T *outer = &iptr->isn_arg.outer;
6389
6390 if (outer->outer_idx < 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02006391 smsg("%s%4d LOADOUTER level %d arg[%d]", pfx, current,
Bram Moolenaarcc341812022-09-19 15:54:34 +01006392 outer->outer_depth,
6393 outer->outer_idx + STACK_FRAME_SIZE);
6394 else if (outer->outer_depth < 0)
6395 smsg("%s%4d LOADOUTER $%d in loop level %d",
6396 pfx, current,
6397 outer->outer_idx,
6398 -outer->outer_depth);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006399 else
6400 smsg("%s%4d LOADOUTER level %d $%d", pfx, current,
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006401 outer->outer_depth,
6402 outer->outer_idx);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006403 }
6404 break;
6405 case ISN_LOADV:
6406 smsg("%s%4d LOADV v:%s", pfx, current,
6407 get_vim_var_name(iptr->isn_arg.number));
6408 break;
6409 case ISN_LOADSCRIPT:
6410 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006411 scriptref_T *sref = iptr->isn_arg.script.scriptref;
6412 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
6413 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006414
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006415 sv = get_script_svar(sref, -1);
6416 if (sv == NULL)
6417 smsg("%s%4d LOADSCRIPT [deleted] from %s",
6418 pfx, current, si->sn_name);
6419 else
6420 smsg("%s%4d LOADSCRIPT %s-%d from %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02006421 sv->sv_name,
6422 sref->sref_idx,
6423 si->sn_name);
6424 }
6425 break;
6426 case ISN_LOADS:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006427 case ISN_LOADEXPORT:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006428 {
6429 scriptitem_T *si = SCRIPT_ITEM(
6430 iptr->isn_arg.loadstore.ls_sid);
6431
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006432 smsg("%s%4d %s s:%s from %s", pfx, current,
6433 iptr->isn_type == ISN_LOADS ? "LOADS"
6434 : "LOADEXPORT",
6435 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006436 }
6437 break;
6438 case ISN_LOADAUTO:
6439 smsg("%s%4d LOADAUTO %s", pfx, current, iptr->isn_arg.string);
6440 break;
6441 case ISN_LOADG:
6442 smsg("%s%4d LOADG g:%s", pfx, current, iptr->isn_arg.string);
6443 break;
6444 case ISN_LOADB:
6445 smsg("%s%4d LOADB b:%s", pfx, current, iptr->isn_arg.string);
6446 break;
6447 case ISN_LOADW:
6448 smsg("%s%4d LOADW w:%s", pfx, current, iptr->isn_arg.string);
6449 break;
6450 case ISN_LOADT:
6451 smsg("%s%4d LOADT t:%s", pfx, current, iptr->isn_arg.string);
6452 break;
6453 case ISN_LOADGDICT:
6454 smsg("%s%4d LOAD g:", pfx, current);
6455 break;
6456 case ISN_LOADBDICT:
6457 smsg("%s%4d LOAD b:", pfx, current);
6458 break;
6459 case ISN_LOADWDICT:
6460 smsg("%s%4d LOAD w:", pfx, current);
6461 break;
6462 case ISN_LOADTDICT:
6463 smsg("%s%4d LOAD t:", pfx, current);
6464 break;
6465 case ISN_LOADOPT:
6466 smsg("%s%4d LOADOPT %s", pfx, current, iptr->isn_arg.string);
6467 break;
6468 case ISN_LOADENV:
6469 smsg("%s%4d LOADENV %s", pfx, current, iptr->isn_arg.string);
6470 break;
6471 case ISN_LOADREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006472 smsg("%s%4d LOADREG @%c", pfx, current,
6473 (int)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006474 break;
6475
6476 case ISN_STORE:
6477 if (iptr->isn_arg.number < 0)
6478 smsg("%s%4d STORE arg[%lld]", pfx, current,
6479 iptr->isn_arg.number + STACK_FRAME_SIZE);
6480 else
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006481 smsg("%s%4d STORE $%lld", pfx, current,
6482 iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006483 break;
6484 case ISN_STOREOUTER:
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006485 {
6486 isn_outer_T *outer = &iptr->isn_arg.outer;
6487
6488 if (outer->outer_depth == OUTER_LOOP_DEPTH)
6489 smsg("%s%4d STOREOUTER level 1 $%d in loop",
6490 pfx, current, outer->outer_idx);
6491 else
6492 smsg("%s%4d STOREOUTER level %d $%d", pfx, current,
6493 outer->outer_depth, outer->outer_idx);
6494 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006495 break;
6496 case ISN_STOREV:
6497 smsg("%s%4d STOREV v:%s", pfx, current,
6498 get_vim_var_name(iptr->isn_arg.number));
6499 break;
6500 case ISN_STOREAUTO:
6501 smsg("%s%4d STOREAUTO %s", pfx, current, iptr->isn_arg.string);
6502 break;
6503 case ISN_STOREG:
6504 smsg("%s%4d STOREG %s", pfx, current, iptr->isn_arg.string);
6505 break;
6506 case ISN_STOREB:
6507 smsg("%s%4d STOREB %s", pfx, current, iptr->isn_arg.string);
6508 break;
6509 case ISN_STOREW:
6510 smsg("%s%4d STOREW %s", pfx, current, iptr->isn_arg.string);
6511 break;
6512 case ISN_STORET:
6513 smsg("%s%4d STORET %s", pfx, current, iptr->isn_arg.string);
6514 break;
6515 case ISN_STORES:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006516 case ISN_STOREEXPORT:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006517 {
6518 scriptitem_T *si = SCRIPT_ITEM(
6519 iptr->isn_arg.loadstore.ls_sid);
6520
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006521 smsg("%s%4d %s %s in %s", pfx, current,
6522 iptr->isn_type == ISN_STORES
6523 ? "STORES" : "STOREEXPORT",
Bram Moolenaar4c137212021-04-19 16:48:48 +02006524 iptr->isn_arg.loadstore.ls_name, si->sn_name);
6525 }
6526 break;
6527 case ISN_STORESCRIPT:
6528 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006529 scriptref_T *sref = iptr->isn_arg.script.scriptref;
6530 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
6531 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006532
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006533 sv = get_script_svar(sref, -1);
6534 if (sv == NULL)
6535 smsg("%s%4d STORESCRIPT [deleted] in %s",
6536 pfx, current, si->sn_name);
6537 else
6538 smsg("%s%4d STORESCRIPT %s-%d in %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02006539 sv->sv_name,
6540 sref->sref_idx,
6541 si->sn_name);
6542 }
6543 break;
6544 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00006545 case ISN_STOREFUNCOPT:
6546 smsg("%s%4d %s &%s", pfx, current,
6547 iptr->isn_type == ISN_STOREOPT ? "STOREOPT" : "STOREFUNCOPT",
Bram Moolenaar4c137212021-04-19 16:48:48 +02006548 iptr->isn_arg.storeopt.so_name);
6549 break;
6550 case ISN_STOREENV:
6551 smsg("%s%4d STOREENV $%s", pfx, current, iptr->isn_arg.string);
6552 break;
6553 case ISN_STOREREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006554 smsg("%s%4d STOREREG @%c", pfx, current,
6555 (int)iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006556 break;
6557 case ISN_STORENR:
6558 smsg("%s%4d STORE %lld in $%d", pfx, current,
6559 iptr->isn_arg.storenr.stnr_val,
6560 iptr->isn_arg.storenr.stnr_idx);
6561 break;
6562
6563 case ISN_STOREINDEX:
6564 smsg("%s%4d STOREINDEX %s", pfx, current,
Bram Moolenaarf7d1c6e2023-01-16 20:47:57 +00006565 vartype_name(iptr->isn_arg.storeindex.si_vartype));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006566 break;
6567
6568 case ISN_STORERANGE:
6569 smsg("%s%4d STORERANGE", pfx, current);
6570 break;
6571
Bram Moolenaard505d172022-12-18 21:42:55 +00006572 case ISN_LOAD_CLASSMEMBER:
6573 case ISN_STORE_CLASSMEMBER:
6574 {
6575 class_T *cl = iptr->isn_arg.classmember.cm_class;
6576 int idx = iptr->isn_arg.classmember.cm_idx;
6577 ocmember_T *ocm = &cl->class_class_members[idx];
6578 smsg("%s%4d %s CLASSMEMBER %s.%s", pfx, current,
6579 iptr->isn_type == ISN_LOAD_CLASSMEMBER
6580 ? "LOAD" : "STORE",
6581 cl->class_name, ocm->ocm_name);
6582 }
6583 break;
6584
Bram Moolenaar4c137212021-04-19 16:48:48 +02006585 // constants
6586 case ISN_PUSHNR:
6587 smsg("%s%4d PUSHNR %lld", pfx, current,
6588 (varnumber_T)(iptr->isn_arg.number));
6589 break;
6590 case ISN_PUSHBOOL:
6591 case ISN_PUSHSPEC:
6592 smsg("%s%4d PUSH %s", pfx, current,
6593 get_var_special_name(iptr->isn_arg.number));
6594 break;
6595 case ISN_PUSHF:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006596 smsg("%s%4d PUSHF %g", pfx, current, iptr->isn_arg.fnumber);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006597 break;
6598 case ISN_PUSHS:
6599 smsg("%s%4d PUSHS \"%s\"", pfx, current, iptr->isn_arg.string);
6600 break;
6601 case ISN_PUSHBLOB:
6602 {
6603 char_u *r;
6604 char_u numbuf[NUMBUFLEN];
6605 char_u *tofree;
6606
6607 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
6608 smsg("%s%4d PUSHBLOB %s", pfx, current, r);
6609 vim_free(tofree);
6610 }
6611 break;
6612 case ISN_PUSHFUNC:
6613 {
6614 char *name = (char *)iptr->isn_arg.string;
6615
6616 smsg("%s%4d PUSHFUNC \"%s\"", pfx, current,
6617 name == NULL ? "[none]" : name);
6618 }
6619 break;
6620 case ISN_PUSHCHANNEL:
6621#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar397a87a2022-03-20 21:14:15 +00006622 smsg("%s%4d PUSHCHANNEL 0", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006623#endif
6624 break;
6625 case ISN_PUSHJOB:
6626#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar397a87a2022-03-20 21:14:15 +00006627 smsg("%s%4d PUSHJOB \"no process\"", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006628#endif
6629 break;
6630 case ISN_PUSHEXC:
6631 smsg("%s%4d PUSH v:exception", pfx, current);
6632 break;
Bram Moolenaar06b77222022-01-25 15:51:56 +00006633 case ISN_AUTOLOAD:
6634 smsg("%s%4d AUTOLOAD %s", pfx, current, iptr->isn_arg.string);
6635 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006636 case ISN_UNLET:
6637 smsg("%s%4d UNLET%s %s", pfx, current,
6638 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
6639 iptr->isn_arg.unlet.ul_name);
6640 break;
6641 case ISN_UNLETENV:
6642 smsg("%s%4d UNLETENV%s $%s", pfx, current,
6643 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
6644 iptr->isn_arg.unlet.ul_name);
6645 break;
6646 case ISN_UNLETINDEX:
6647 smsg("%s%4d UNLETINDEX", pfx, current);
6648 break;
6649 case ISN_UNLETRANGE:
6650 smsg("%s%4d UNLETRANGE", pfx, current);
6651 break;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02006652 case ISN_LOCKUNLOCK:
6653 smsg("%s%4d LOCKUNLOCK %s", pfx, current, iptr->isn_arg.string);
6654 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006655 case ISN_LOCKCONST:
6656 smsg("%s%4d LOCKCONST", pfx, current);
6657 break;
6658 case ISN_NEWLIST:
6659 smsg("%s%4d NEWLIST size %lld", pfx, current,
Bram Moolenaar1936c762022-09-28 15:19:10 +01006660 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006661 break;
6662 case ISN_NEWDICT:
6663 smsg("%s%4d NEWDICT size %lld", pfx, current,
Bram Moolenaar1936c762022-09-28 15:19:10 +01006664 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006665 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00006666 case ISN_NEWPARTIAL:
6667 smsg("%s%4d NEWPARTIAL", pfx, current);
6668 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006669
6670 // function call
6671 case ISN_BCALL:
6672 {
6673 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
6674
6675 smsg("%s%4d BCALL %s(argc %d)", pfx, current,
6676 internal_func_name(cbfunc->cbf_idx),
6677 cbfunc->cbf_argcount);
6678 }
6679 break;
6680 case ISN_DCALL:
6681 {
6682 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
6683 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
6684 + cdfunc->cdf_idx;
6685
6686 smsg("%s%4d DCALL %s(argc %d)", pfx, current,
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006687 printable_func_name(df->df_ufunc),
6688 cdfunc->cdf_argcount);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006689 }
6690 break;
Bram Moolenaard0200c82023-01-28 15:19:40 +00006691 case ISN_METHODCALL:
6692 {
6693 cmfunc_T *mfunc = iptr->isn_arg.mfunc;
6694
6695 smsg("%s%4d METHODCALL %s.%s(argc %d)", pfx, current,
6696 mfunc->cmf_itf->class_name,
6697 mfunc->cmf_itf->class_obj_methods[
6698 mfunc->cmf_idx]->uf_name,
6699 mfunc->cmf_argcount);
6700 }
6701 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006702 case ISN_UCALL:
6703 {
6704 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
6705
6706 smsg("%s%4d UCALL %s(argc %d)", pfx, current,
6707 cufunc->cuf_name, cufunc->cuf_argcount);
6708 }
6709 break;
6710 case ISN_PCALL:
6711 {
6712 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
6713
6714 smsg("%s%4d PCALL%s (argc %d)", pfx, current,
6715 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
6716 }
6717 break;
6718 case ISN_PCALL_END:
6719 smsg("%s%4d PCALL end", pfx, current);
6720 break;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006721 case ISN_DEFER:
Bram Moolenaar8dbab1d2023-01-27 20:14:02 +00006722 case ISN_DEFEROBJ:
6723 smsg("%s%4d %s %d args", pfx, current,
6724 iptr->isn_type == ISN_DEFER ? "DEFER" : "DEFEROBJ",
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006725 (int)iptr->isn_arg.defer.defer_argcount);
6726 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006727 case ISN_RETURN:
6728 smsg("%s%4d RETURN", pfx, current);
6729 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02006730 case ISN_RETURN_VOID:
6731 smsg("%s%4d RETURN void", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006732 break;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006733 case ISN_RETURN_OBJECT:
6734 smsg("%s%4d RETURN object", pfx, current);
6735 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006736 case ISN_FUNCREF:
6737 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006738 funcref_T *funcref = &iptr->isn_arg.funcref;
6739 funcref_extra_T *extra = funcref->fr_extra;
6740 char_u *name;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006741
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006742 if (extra == NULL || extra->fre_func_name == NULL)
Bram Moolenaar38453522021-11-28 22:00:12 +00006743 {
6744 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
6745 + funcref->fr_dfunc_idx;
6746 name = df->df_ufunc->uf_name;
6747 }
6748 else
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006749 name = extra->fre_func_name;
Bram Moolenaar313e4722023-02-08 20:55:27 +00006750 if (extra != NULL && extra->fre_class != NULL)
6751 {
6752 smsg("%s%4d FUNCREF %s.%s", pfx, current,
6753 extra->fre_class->class_name, name);
6754 }
6755 else if (extra == NULL
6756 || extra->fre_loopvar_info.lvi_depth == 0)
6757 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006758 smsg("%s%4d FUNCREF %s", pfx, current, name);
Bram Moolenaar313e4722023-02-08 20:55:27 +00006759 }
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006760 else
Bram Moolenaarcc341812022-09-19 15:54:34 +01006761 {
6762 char_u *info = printable_loopvarinfo(
6763 &extra->fre_loopvar_info);
6764
6765 smsg("%s%4d FUNCREF %s vars %s", pfx, current,
6766 name, info);
6767 vim_free(info);
6768 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006769 }
6770 break;
6771
6772 case ISN_NEWFUNC:
6773 {
Bram Moolenaarcc341812022-09-19 15:54:34 +01006774 newfuncarg_T *arg = iptr->isn_arg.newfunc.nf_arg;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006775
Bram Moolenaarcc341812022-09-19 15:54:34 +01006776 if (arg->nfa_loopvar_info.lvi_depth == 0)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006777 smsg("%s%4d NEWFUNC %s %s", pfx, current,
6778 arg->nfa_lambda, arg->nfa_global);
6779 else
Bram Moolenaarcc341812022-09-19 15:54:34 +01006780 {
6781 char_u *info = printable_loopvarinfo(
6782 &arg->nfa_loopvar_info);
6783
6784 smsg("%s%4d NEWFUNC %s %s vars %s", pfx, current,
6785 arg->nfa_lambda, arg->nfa_global, info);
6786 vim_free(info);
6787 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006788 }
6789 break;
6790
6791 case ISN_DEF:
6792 {
6793 char_u *name = iptr->isn_arg.string;
6794
6795 smsg("%s%4d DEF %s", pfx, current,
6796 name == NULL ? (char_u *)"" : name);
6797 }
6798 break;
6799
6800 case ISN_JUMP:
6801 {
6802 char *when = "?";
6803
6804 switch (iptr->isn_arg.jump.jump_when)
6805 {
6806 case JUMP_ALWAYS:
6807 when = "JUMP";
6808 break;
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02006809 case JUMP_NEVER:
6810 iemsg("JUMP_NEVER should not be used");
6811 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006812 case JUMP_AND_KEEP_IF_TRUE:
6813 when = "JUMP_AND_KEEP_IF_TRUE";
6814 break;
6815 case JUMP_IF_FALSE:
6816 when = "JUMP_IF_FALSE";
6817 break;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01006818 case JUMP_WHILE_FALSE:
6819 when = "JUMP_WHILE_FALSE"; // unused
6820 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006821 case JUMP_IF_COND_FALSE:
6822 when = "JUMP_IF_COND_FALSE";
6823 break;
6824 case JUMP_IF_COND_TRUE:
6825 when = "JUMP_IF_COND_TRUE";
6826 break;
6827 }
6828 smsg("%s%4d %s -> %d", pfx, current, when,
6829 iptr->isn_arg.jump.jump_where);
6830 }
6831 break;
6832
6833 case ISN_JUMP_IF_ARG_SET:
6834 smsg("%s%4d JUMP_IF_ARG_SET arg[%d] -> %d", pfx, current,
6835 iptr->isn_arg.jumparg.jump_arg_off + STACK_FRAME_SIZE,
6836 iptr->isn_arg.jump.jump_where);
6837 break;
6838
Bram Moolenaar65b0d162022-12-13 18:43:22 +00006839 case ISN_JUMP_IF_ARG_NOT_SET:
6840 smsg("%s%4d JUMP_IF_ARG_NOT_SET arg[%d] -> %d", pfx, current,
6841 iptr->isn_arg.jumparg.jump_arg_off + STACK_FRAME_SIZE,
6842 iptr->isn_arg.jump.jump_where);
6843 break;
6844
Bram Moolenaar4c137212021-04-19 16:48:48 +02006845 case ISN_FOR:
6846 {
6847 forloop_T *forloop = &iptr->isn_arg.forloop;
6848
6849 smsg("%s%4d FOR $%d -> %d", pfx, current,
Bram Moolenaarcc341812022-09-19 15:54:34 +01006850 forloop->for_loop_idx, forloop->for_end);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006851 }
6852 break;
6853
Bram Moolenaarb46c0832022-09-15 17:19:37 +01006854 case ISN_ENDLOOP:
6855 {
6856 endloop_T *endloop = &iptr->isn_arg.endloop;
6857
Bram Moolenaarcc341812022-09-19 15:54:34 +01006858 smsg("%s%4d ENDLOOP ref $%d save $%d-$%d depth %d",
6859 pfx, current,
Bram Moolenaarb46c0832022-09-15 17:19:37 +01006860 endloop->end_funcref_idx,
6861 endloop->end_var_idx,
Bram Moolenaarcc341812022-09-19 15:54:34 +01006862 endloop->end_var_idx + endloop->end_var_count - 1,
6863 endloop->end_depth);
Bram Moolenaarb46c0832022-09-15 17:19:37 +01006864 }
6865 break;
6866
6867 case ISN_WHILE:
6868 {
6869 whileloop_T *whileloop = &iptr->isn_arg.whileloop;
6870
6871 smsg("%s%4d WHILE $%d -> %d", pfx, current,
6872 whileloop->while_funcref_idx,
6873 whileloop->while_end);
6874 }
6875 break;
6876
Bram Moolenaar4c137212021-04-19 16:48:48 +02006877 case ISN_TRY:
6878 {
Bram Moolenaar0d807102021-12-21 09:42:09 +00006879 try_T *try = &iptr->isn_arg.tryref;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006880
6881 if (try->try_ref->try_finally == 0)
6882 smsg("%s%4d TRY catch -> %d, endtry -> %d",
6883 pfx, current,
6884 try->try_ref->try_catch,
6885 try->try_ref->try_endtry);
6886 else
6887 smsg("%s%4d TRY catch -> %d, finally -> %d, endtry -> %d",
6888 pfx, current,
6889 try->try_ref->try_catch,
6890 try->try_ref->try_finally,
6891 try->try_ref->try_endtry);
6892 }
6893 break;
6894 case ISN_CATCH:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006895 smsg("%s%4d CATCH", pfx, current);
6896 break;
6897 case ISN_TRYCONT:
6898 {
6899 trycont_T *trycont = &iptr->isn_arg.trycont;
6900
6901 smsg("%s%4d TRY-CONTINUE %d level%s -> %d", pfx, current,
6902 trycont->tct_levels,
6903 trycont->tct_levels == 1 ? "" : "s",
6904 trycont->tct_where);
6905 }
6906 break;
6907 case ISN_FINALLY:
6908 smsg("%s%4d FINALLY", pfx, current);
6909 break;
6910 case ISN_ENDTRY:
6911 smsg("%s%4d ENDTRY", pfx, current);
6912 break;
6913 case ISN_THROW:
6914 smsg("%s%4d THROW", pfx, current);
6915 break;
6916
6917 // expression operations on number
6918 case ISN_OPNR:
6919 case ISN_OPFLOAT:
6920 case ISN_OPANY:
6921 {
6922 char *what;
6923 char *ins;
6924
6925 switch (iptr->isn_arg.op.op_type)
6926 {
6927 case EXPR_MULT: what = "*"; break;
6928 case EXPR_DIV: what = "/"; break;
6929 case EXPR_REM: what = "%"; break;
6930 case EXPR_SUB: what = "-"; break;
6931 case EXPR_ADD: what = "+"; break;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01006932 case EXPR_LSHIFT: what = "<<"; break;
6933 case EXPR_RSHIFT: what = ">>"; break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006934 default: what = "???"; break;
6935 }
6936 switch (iptr->isn_type)
6937 {
6938 case ISN_OPNR: ins = "OPNR"; break;
6939 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
6940 case ISN_OPANY: ins = "OPANY"; break;
6941 default: ins = "???"; break;
6942 }
6943 smsg("%s%4d %s %s", pfx, current, ins, what);
6944 }
6945 break;
6946
6947 case ISN_COMPAREBOOL:
6948 case ISN_COMPARESPECIAL:
Bram Moolenaar7a222242022-03-01 19:23:24 +00006949 case ISN_COMPARENULL:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006950 case ISN_COMPARENR:
6951 case ISN_COMPAREFLOAT:
6952 case ISN_COMPARESTRING:
6953 case ISN_COMPAREBLOB:
6954 case ISN_COMPARELIST:
6955 case ISN_COMPAREDICT:
6956 case ISN_COMPAREFUNC:
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00006957 case ISN_COMPARECLASS:
6958 case ISN_COMPAREOBJECT:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006959 case ISN_COMPAREANY:
6960 {
6961 char *p;
6962 char buf[10];
6963 char *type;
6964
6965 switch (iptr->isn_arg.op.op_type)
6966 {
6967 case EXPR_EQUAL: p = "=="; break;
6968 case EXPR_NEQUAL: p = "!="; break;
6969 case EXPR_GREATER: p = ">"; break;
6970 case EXPR_GEQUAL: p = ">="; break;
6971 case EXPR_SMALLER: p = "<"; break;
6972 case EXPR_SEQUAL: p = "<="; break;
6973 case EXPR_MATCH: p = "=~"; break;
6974 case EXPR_IS: p = "is"; break;
6975 case EXPR_ISNOT: p = "isnot"; break;
6976 case EXPR_NOMATCH: p = "!~"; break;
6977 default: p = "???"; break;
6978 }
6979 STRCPY(buf, p);
6980 if (iptr->isn_arg.op.op_ic == TRUE)
6981 strcat(buf, "?");
6982 switch(iptr->isn_type)
6983 {
6984 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
6985 case ISN_COMPARESPECIAL:
6986 type = "COMPARESPECIAL"; break;
Bram Moolenaar7a222242022-03-01 19:23:24 +00006987 case ISN_COMPARENULL: type = "COMPARENULL"; break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006988 case ISN_COMPARENR: type = "COMPARENR"; break;
6989 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
6990 case ISN_COMPARESTRING:
6991 type = "COMPARESTRING"; break;
6992 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
6993 case ISN_COMPARELIST: type = "COMPARELIST"; break;
6994 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
6995 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00006996 case ISN_COMPARECLASS: type = "COMPARECLASS"; break;
6997 case ISN_COMPAREOBJECT:
6998 type = "COMPAREOBJECT"; break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006999 case ISN_COMPAREANY: type = "COMPAREANY"; break;
7000 default: type = "???"; break;
7001 }
7002
7003 smsg("%s%4d %s %s", pfx, current, type, buf);
7004 }
7005 break;
7006
7007 case ISN_ADDLIST: smsg("%s%4d ADDLIST", pfx, current); break;
7008 case ISN_ADDBLOB: smsg("%s%4d ADDBLOB", pfx, current); break;
7009
7010 // expression operations
LemonBoy372bcce2022-04-25 12:43:20 +01007011 case ISN_CONCAT:
7012 smsg("%s%4d CONCAT size %lld", pfx, current,
7013 (varnumber_T)(iptr->isn_arg.number));
7014 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007015 case ISN_STRINDEX: smsg("%s%4d STRINDEX", pfx, current); break;
7016 case ISN_STRSLICE: smsg("%s%4d STRSLICE", pfx, current); break;
7017 case ISN_BLOBINDEX: smsg("%s%4d BLOBINDEX", pfx, current); break;
7018 case ISN_BLOBSLICE: smsg("%s%4d BLOBSLICE", pfx, current); break;
7019 case ISN_LISTAPPEND: smsg("%s%4d LISTAPPEND", pfx, current); break;
7020 case ISN_BLOBAPPEND: smsg("%s%4d BLOBAPPEND", pfx, current); break;
7021 case ISN_LISTINDEX: smsg("%s%4d LISTINDEX", pfx, current); break;
7022 case ISN_LISTSLICE: smsg("%s%4d LISTSLICE", pfx, current); break;
7023 case ISN_ANYINDEX: smsg("%s%4d ANYINDEX", pfx, current); break;
7024 case ISN_ANYSLICE: smsg("%s%4d ANYSLICE", pfx, current); break;
7025 case ISN_SLICE: smsg("%s%4d SLICE %lld",
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02007026 pfx, current, iptr->isn_arg.number); break;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02007027 case ISN_GETITEM: smsg("%s%4d ITEM %lld%s", pfx, current,
7028 iptr->isn_arg.getitem.gi_index,
7029 iptr->isn_arg.getitem.gi_with_op ?
7030 " with op" : ""); break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02007031 case ISN_MEMBER: smsg("%s%4d MEMBER", pfx, current); break;
7032 case ISN_STRINGMEMBER: smsg("%s%4d MEMBER %s", pfx, current,
7033 iptr->isn_arg.string); break;
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00007034 case ISN_GET_OBJ_MEMBER: smsg("%s%4d OBJ_MEMBER %d", pfx, current,
7035 (int)iptr->isn_arg.number); break;
Bram Moolenaar29ac5df2023-01-16 19:43:47 +00007036 case ISN_GET_ITF_MEMBER: smsg("%s%4d ITF_MEMBER %d on %s",
7037 pfx, current,
7038 (int)iptr->isn_arg.classmember.cm_idx,
7039 iptr->isn_arg.classmember.cm_class->class_name);
7040 break;
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00007041 case ISN_STORE_THIS: smsg("%s%4d STORE_THIS %d", pfx, current,
Bram Moolenaarffdaca92022-12-09 21:41:48 +00007042 (int)iptr->isn_arg.number); break;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02007043 case ISN_CLEARDICT: smsg("%s%4d CLEARDICT", pfx, current); break;
7044 case ISN_USEDICT: smsg("%s%4d USEDICT", pfx, current); break;
7045
Bram Moolenaar4c137212021-04-19 16:48:48 +02007046 case ISN_NEGATENR: smsg("%s%4d NEGATENR", pfx, current); break;
7047
Bram Moolenaar4c137212021-04-19 16:48:48 +02007048 case ISN_CHECKTYPE:
7049 {
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01007050 checktype_T *ct = &iptr->isn_arg.type;
Bram Moolenaarc6951a72022-12-29 20:56:24 +00007051 char *tofree = NULL;
7052 char *typename;
7053
7054 if (ct->ct_type->tt_type == VAR_FLOAT
7055 && (ct->ct_type->tt_flags & TTFLAG_NUMBER_OK))
7056 typename = "float|number";
7057 else
7058 typename = type_name(ct->ct_type, &tofree);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007059
7060 if (ct->ct_arg_idx == 0)
7061 smsg("%s%4d CHECKTYPE %s stack[%d]", pfx, current,
Bram Moolenaarc6951a72022-12-29 20:56:24 +00007062 typename,
Bram Moolenaar4c137212021-04-19 16:48:48 +02007063 (int)ct->ct_off);
7064 else
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01007065 smsg("%s%4d CHECKTYPE %s stack[%d] %s %d",
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02007066 pfx, current,
Bram Moolenaarc6951a72022-12-29 20:56:24 +00007067 typename,
Bram Moolenaar4c137212021-04-19 16:48:48 +02007068 (int)ct->ct_off,
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01007069 ct->ct_is_var ? "var": "arg",
Bram Moolenaar4c137212021-04-19 16:48:48 +02007070 (int)ct->ct_arg_idx);
7071 vim_free(tofree);
7072 break;
7073 }
7074 case ISN_CHECKLEN: smsg("%s%4d CHECKLEN %s%d", pfx, current,
7075 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
7076 iptr->isn_arg.checklen.cl_min_len);
7077 break;
7078 case ISN_SETTYPE:
7079 {
7080 char *tofree;
7081
7082 smsg("%s%4d SETTYPE %s", pfx, current,
7083 type_name(iptr->isn_arg.type.ct_type, &tofree));
7084 vim_free(tofree);
7085 break;
7086 }
7087 case ISN_COND2BOOL: smsg("%s%4d COND2BOOL", pfx, current); break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007088 case ISN_2BOOL: if (iptr->isn_arg.tobool.invert)
7089 smsg("%s%4d INVERT %d (!val)", pfx, current,
7090 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007091 else
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007092 smsg("%s%4d 2BOOL %d (!!val)", pfx, current,
7093 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007094 break;
7095 case ISN_2STRING: smsg("%s%4d 2STRING stack[%lld]", pfx, current,
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007096 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02007097 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007098 case ISN_2STRING_ANY: smsg("%s%4d 2STRING_ANY stack[%lld]",
7099 pfx, current,
7100 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02007101 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007102 case ISN_RANGE: smsg("%s%4d RANGE %s", pfx, current,
7103 iptr->isn_arg.string);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007104 break;
7105 case ISN_PUT:
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01007106 if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE)
Bram Moolenaar4c137212021-04-19 16:48:48 +02007107 smsg("%s%4d PUT %c above range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007108 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007109 else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE)
7110 smsg("%s%4d PUT %c range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02007111 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007112 else
7113 smsg("%s%4d PUT %c %ld", pfx, current,
7114 iptr->isn_arg.put.put_regname,
7115 (long)iptr->isn_arg.put.put_lnum);
7116 break;
7117
Bram Moolenaar4c137212021-04-19 16:48:48 +02007118 case ISN_CMDMOD:
7119 {
7120 char_u *buf;
7121 size_t len = produce_cmdmods(
7122 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
7123
7124 buf = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02007125 if (likely(buf != NULL))
Bram Moolenaar4c137212021-04-19 16:48:48 +02007126 {
7127 (void)produce_cmdmods(
7128 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
7129 smsg("%s%4d CMDMOD %s", pfx, current, buf);
7130 vim_free(buf);
7131 }
7132 break;
7133 }
7134 case ISN_CMDMOD_REV: smsg("%s%4d CMDMOD_REV", pfx, current); break;
7135
7136 case ISN_PROF_START:
Bram Moolenaare99d4222021-06-13 14:01:26 +02007137 smsg("%s%4d PROFILE START line %d", pfx, current,
7138 iptr->isn_lnum);
Bram Moolenaar4c137212021-04-19 16:48:48 +02007139 break;
7140
7141 case ISN_PROF_END:
7142 smsg("%s%4d PROFILE END", pfx, current);
7143 break;
7144
Bram Moolenaare99d4222021-06-13 14:01:26 +02007145 case ISN_DEBUG:
Bram Moolenaar8cec9272021-06-23 20:20:53 +02007146 smsg("%s%4d DEBUG line %d-%d varcount %lld", pfx, current,
7147 iptr->isn_arg.debug.dbg_break_lnum + 1,
7148 iptr->isn_lnum,
7149 iptr->isn_arg.debug.dbg_var_names_len);
Bram Moolenaare99d4222021-06-13 14:01:26 +02007150 break;
7151
Bram Moolenaar4c137212021-04-19 16:48:48 +02007152 case ISN_UNPACK: smsg("%s%4d UNPACK %d%s", pfx, current,
7153 iptr->isn_arg.unpack.unp_count,
7154 iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : "");
7155 break;
7156 case ISN_SHUFFLE: smsg("%s%4d SHUFFLE %d up %d", pfx, current,
7157 iptr->isn_arg.shuffle.shfl_item,
7158 iptr->isn_arg.shuffle.shfl_up);
7159 break;
7160 case ISN_DROP: smsg("%s%4d DROP", pfx, current); break;
7161
7162 case ISN_FINISH: // End of list of instructions for ISN_SUBSTITUTE.
7163 return;
7164 }
7165
7166 out_flush(); // output one line at a time
7167 ui_breakcheck();
7168 if (got_int)
7169 break;
7170 }
7171}
7172
7173/*
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02007174 * Handle command line completion for the :disassemble command.
7175 */
7176 void
7177set_context_in_disassemble_cmd(expand_T *xp, char_u *arg)
7178{
7179 char_u *p;
7180
7181 // Default: expand user functions, "debug" and "profile"
7182 xp->xp_context = EXPAND_DISASSEMBLE;
7183 xp->xp_pattern = arg;
7184
7185 // first argument already typed: only user function names
7186 if (*arg != NUL && *(p = skiptowhite(arg)) != NUL)
7187 {
7188 xp->xp_context = EXPAND_USER_FUNC;
7189 xp->xp_pattern = skipwhite(p);
7190 }
7191}
7192
7193/*
7194 * Function given to ExpandGeneric() to obtain the list of :disassemble
7195 * arguments.
7196 */
7197 char_u *
7198get_disassemble_argument(expand_T *xp, int idx)
7199{
7200 if (idx == 0)
7201 return (char_u *)"debug";
7202 if (idx == 1)
7203 return (char_u *)"profile";
7204 return get_user_func_name(xp, idx - 2);
7205}
7206
7207/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01007208 * ":disassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01007209 * We don't really need this at runtime, but we do have tests that require it,
7210 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007211 */
7212 void
7213ex_disassemble(exarg_T *eap)
7214{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01007215 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01007216 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007217 dfunc_T *dfunc;
Bram Moolenaardafef512022-05-21 21:55:55 +01007218 isn_T *instr = NULL; // init to shut up gcc warning
7219 int instr_count = 0; // init to shut up gcc warning
Bram Moolenaar5a01caa2022-05-21 18:56:58 +01007220 compiletype_T compile_type = CT_NONE;
Bram Moolenaare99d4222021-06-13 14:01:26 +02007221
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01007222 ufunc = find_func_by_name(arg, &compile_type);
Bram Moolenaara26b9702020-04-18 19:53:28 +02007223 if (ufunc == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007224 return;
Bram Moolenaare99d4222021-06-13 14:01:26 +02007225 if (func_needs_compiling(ufunc, compile_type)
7226 && compile_def_function(ufunc, FALSE, compile_type, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02007227 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007228 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007229 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007230 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007231 return;
7232 }
Bram Moolenaar6db660b2021-08-01 14:08:54 +02007233 msg((char *)printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007234
7235 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
Bram Moolenaare99d4222021-06-13 14:01:26 +02007236 switch (compile_type)
7237 {
7238 case CT_PROFILE:
Bram Moolenaarf002a412021-01-24 13:34:18 +01007239#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02007240 instr = dfunc->df_instr_prof;
7241 instr_count = dfunc->df_instr_prof_count;
7242 break;
Bram Moolenaarf002a412021-01-24 13:34:18 +01007243#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02007244 // FALLTHROUGH
7245 case CT_NONE:
7246 instr = dfunc->df_instr;
7247 instr_count = dfunc->df_instr_count;
7248 break;
7249 case CT_DEBUG:
7250 instr = dfunc->df_instr_debug;
7251 instr_count = dfunc->df_instr_debug_count;
7252 break;
7253 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007254
Bram Moolenaar4c137212021-04-19 16:48:48 +02007255 list_instructions("", instr, instr_count, ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007256}
7257
7258/*
Bram Moolenaar13106602020-10-04 16:06:05 +02007259 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007260 * list, etc. Mostly like what JavaScript does, except that empty list and
7261 * empty dictionary are FALSE.
7262 */
7263 int
7264tv2bool(typval_T *tv)
7265{
7266 switch (tv->v_type)
7267 {
7268 case VAR_NUMBER:
7269 return tv->vval.v_number != 0;
7270 case VAR_FLOAT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007271 return tv->vval.v_float != 0.0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007272 case VAR_PARTIAL:
7273 return tv->vval.v_partial != NULL;
7274 case VAR_FUNC:
7275 case VAR_STRING:
7276 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
7277 case VAR_LIST:
7278 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
7279 case VAR_DICT:
7280 return tv->vval.v_dict != NULL
7281 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
7282 case VAR_BOOL:
7283 case VAR_SPECIAL:
7284 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
7285 case VAR_JOB:
7286#ifdef FEAT_JOB_CHANNEL
7287 return tv->vval.v_job != NULL;
7288#else
7289 break;
7290#endif
7291 case VAR_CHANNEL:
7292#ifdef FEAT_JOB_CHANNEL
7293 return tv->vval.v_channel != NULL;
7294#else
7295 break;
7296#endif
7297 case VAR_BLOB:
7298 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
7299 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02007300 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007301 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02007302 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00007303 case VAR_CLASS:
7304 case VAR_OBJECT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007305 break;
7306 }
7307 return FALSE;
7308}
7309
Bram Moolenaarea2d4072020-11-12 12:08:51 +01007310 void
7311emsg_using_string_as(typval_T *tv, int as_number)
7312{
7313 semsg(_(as_number ? e_using_string_as_number_str
7314 : e_using_string_as_bool_str),
7315 tv->vval.v_string == NULL
7316 ? (char_u *)"" : tv->vval.v_string);
7317}
7318
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007319/*
7320 * If "tv" is a string give an error and return FAIL.
7321 */
7322 int
7323check_not_string(typval_T *tv)
7324{
7325 if (tv->v_type == VAR_STRING)
7326 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +01007327 emsg_using_string_as(tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007328 clear_tv(tv);
7329 return FAIL;
7330 }
7331 return OK;
7332}
7333
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007334
7335#endif // FEAT_EVAL