blob: 0aed737ad5739dc55009bb0161c97f852107ca0e [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{
282 if (ufunc->uf_debug_tick != debug_tick)
283 {
284 linenr_T breakpoint;
285
286 ufunc->uf_debug_tick = debug_tick;
287 breakpoint = dbg_find_breakpoint(FALSE, ufunc->uf_name, 0);
288 ufunc->uf_has_breakpoint = breakpoint > 0;
289 }
290}
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{
386 if (ufunc->uf_arg_types != NULL || ufunc->uf_va_type != NULL)
387 {
388 typval_T *argv = STACK_TV_BOT(0) - argcount - off;
389
390 // The function can change at runtime, check that the argument
391 // types are correct.
392 for (int i = 0; i < argcount; ++i)
393 {
394 type_T *type = NULL;
395
396 // assume a v:none argument, using the default value, is always OK
397 if (argv[i].v_type == VAR_SPECIAL
398 && argv[i].vval.v_number == VVAL_NONE)
399 continue;
400
401 if (i < ufunc->uf_args.ga_len && ufunc->uf_arg_types != NULL)
402 type = ufunc->uf_arg_types[i];
403 else if (ufunc->uf_va_type != NULL)
404 type = ufunc->uf_va_type->tt_member;
405 if (type != NULL && check_typval_arg_type(type,
406 &argv[i], NULL, i + 1) == FAIL)
407 return FAIL;
408 }
409 }
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 Moolenaar0d89d8a2022-12-31 14:01:24 +0000535 // Check the argument types.
536 if (check_ufunc_arg_types(ufunc, argcount, vararg_count, ectx) == FAIL)
537 return FAIL;
538
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200539 // Reserve space for:
540 // - missing arguments
541 // - stack frame
542 // - local variables
543 // - if needed: a counter for number of closures created in
544 // ectx->ec_funcrefs.
545 varcount = dfunc->df_varcount + dfunc->df_has_closure;
Bram Moolenaarb46c0832022-09-15 17:19:37 +0100546 if (GA_GROW_FAILS(&ectx->ec_stack,
547 arg_to_add + STACK_FRAME_SIZE + varcount))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100548 return FAIL;
549
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100550 // If depth of calling is getting too high, don't execute the function.
551 if (funcdepth_increment() == FAIL)
552 return FAIL;
Bram Moolenaare99d4222021-06-13 14:01:26 +0200553 ++ex_nesting_level;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100554
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100555 // Only make a copy of funclocal if it contains something to restore.
Bram Moolenaar4c137212021-04-19 16:48:48 +0200556 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100557 {
558 floc = ALLOC_ONE(funclocal_T);
559 if (floc == NULL)
560 return FAIL;
Bram Moolenaar4c137212021-04-19 16:48:48 +0200561 *floc = ectx->ec_funclocal;
562 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100563 }
564
Bram Moolenaarfe270812020-04-11 22:31:27 +0200565 // Move the vararg-list to below the missing optional arguments.
566 if (vararg_count > 0 && arg_to_add > 0)
567 *STACK_TV_BOT(arg_to_add - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100568
569 // Reserve space for omitted optional arguments, filled in soon.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200570 for (idx = 0; idx < arg_to_add; ++idx)
Bram Moolenaarfe270812020-04-11 22:31:27 +0200571 STACK_TV_BOT(idx - vararg_count)->v_type = VAR_UNKNOWN;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200572 ectx->ec_stack.ga_len += arg_to_add;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100573
574 // Store current execution state in stack frame for ISN_RETURN.
Bram Moolenaar0186e582021-01-10 18:33:11 +0100575 STACK_TV_BOT(STACK_FRAME_FUNC_OFF)->vval.v_number = ectx->ec_dfunc_idx;
576 STACK_TV_BOT(STACK_FRAME_IIDX_OFF)->vval.v_number = ectx->ec_iidx;
Bram Moolenaar5930ddc2021-04-26 20:32:59 +0200577 STACK_TV_BOT(STACK_FRAME_INSTR_OFF)->vval.v_string = (void *)ectx->ec_instr;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200578 STACK_TV_BOT(STACK_FRAME_OUTER_OFF)->vval.v_string =
579 (void *)ectx->ec_outer_ref;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100580 STACK_TV_BOT(STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string = (void *)floc;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100581 STACK_TV_BOT(STACK_FRAME_IDX_OFF)->vval.v_number = ectx->ec_frame_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200582 ectx->ec_frame_idx = ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100583
Bram Moolenaar5800c792022-09-22 17:34:01 +0100584 // Initialize all local variables to number zero. Also initialize the
585 // variable that counts how many closures were created. This is used in
586 // handle_closure_in_use().
587 int initcount = dfunc->df_varcount + (dfunc->df_has_closure ? 1 : 0);
588 for (idx = 0; idx < initcount; ++idx)
Bram Moolenaar5cd64792021-12-25 18:23:24 +0000589 {
590 typval_T *tv = STACK_TV_BOT(STACK_FRAME_SIZE + idx);
591
592 tv->v_type = VAR_NUMBER;
593 tv->vval.v_number = 0;
594 }
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200595 ectx->ec_stack.ga_len += STACK_FRAME_SIZE + varcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100596
Bram Moolenaar5800c792022-09-22 17:34:01 +0100597 partial_T *pt = pt_arg != NULL ? pt_arg : ufunc->uf_partial;
598 if (pt != NULL || (ufunc->uf_flags & FC_CLOSURE))
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100599 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200600 outer_ref_T *ref = ALLOC_CLEAR_ONE(outer_ref_T);
Bram Moolenaar0186e582021-01-10 18:33:11 +0100601
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200602 if (ref == NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +0100603 return FAIL;
604 if (pt != NULL)
605 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +0000606 ref->or_outer = get_pt_outer(pt);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200607 ++pt->pt_refcount;
608 ref->or_partial = pt;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100609 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100610 else
611 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200612 ref->or_outer = ALLOC_CLEAR_ONE(outer_T);
Dominique Pelle5a9e5842021-07-24 19:32:12 +0200613 if (unlikely(ref->or_outer == NULL))
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200614 {
615 vim_free(ref);
616 return FAIL;
617 }
618 ref->or_outer_allocated = TRUE;
619 ref->or_outer->out_stack = &ectx->ec_stack;
620 ref->or_outer->out_frame_idx = ectx->ec_frame_idx;
621 if (ectx->ec_outer_ref != NULL)
622 ref->or_outer->out_up = ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100623 }
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200624 ectx->ec_outer_ref = ref;
Bram Moolenaarab360522021-01-10 14:02:28 +0100625 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100626 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200627 ectx->ec_outer_ref = NULL;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100628
Bram Moolenaarc970e422021-03-17 15:03:04 +0100629 ++ufunc->uf_calls;
630
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100631 // Set execution state to the start of the called function.
632 ectx->ec_dfunc_idx = cdf_idx;
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100633 ectx->ec_instr = INSTRUCTIONS(dfunc);
Bram Moolenaarb2049902021-01-24 12:53:53 +0100634 entry = estack_push_ufunc(ufunc, 1);
Bram Moolenaarc620c052020-07-08 15:16:19 +0200635 if (entry != NULL)
636 {
637 // Set the script context to the script where the function was defined.
Bram Moolenaarc70fe462021-04-17 17:59:19 +0200638 // Save the current context so it can be restored on return.
639 entry->es_save_sctx = current_sctx;
640 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200641 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100642
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +0200643 // Start execution at the first instruction.
644 ectx->ec_iidx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100645
646 return OK;
647}
648
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000649// Double linked list of funcstack_T in use.
650static funcstack_T *first_funcstack = NULL;
651
652 static void
653add_funcstack_to_list(funcstack_T *funcstack)
654{
655 // Link in list of funcstacks.
656 if (first_funcstack != NULL)
657 first_funcstack->fs_prev = funcstack;
658 funcstack->fs_next = first_funcstack;
659 funcstack->fs_prev = NULL;
660 first_funcstack = funcstack;
661}
662
663 static void
664remove_funcstack_from_list(funcstack_T *funcstack)
665{
666 if (funcstack->fs_prev == NULL)
667 first_funcstack = funcstack->fs_next;
668 else
669 funcstack->fs_prev->fs_next = funcstack->fs_next;
670 if (funcstack->fs_next != NULL)
671 funcstack->fs_next->fs_prev = funcstack->fs_prev;
672}
673
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100674/*
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200675 * Used when returning from a function: Check if any closure is still
676 * referenced. If so then move the arguments and variables to a separate piece
677 * of stack to be used when the closure is called.
678 * When "free_arguments" is TRUE the arguments are to be freed.
679 * Returns FAIL when out of memory.
680 */
681 static int
682handle_closure_in_use(ectx_T *ectx, int free_arguments)
683{
684 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
685 + ectx->ec_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200686 int argcount;
687 int top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200688 int idx;
689 typval_T *tv;
690 int closure_in_use = FALSE;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200691 garray_T *gap = &ectx->ec_funcrefs;
692 varnumber_T closure_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200693
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200694 if (dfunc->df_ufunc == NULL)
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200695 return OK; // function was freed
696 if (dfunc->df_has_closure == 0)
697 return OK; // no closures
698 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + dfunc->df_varcount);
699 closure_count = tv->vval.v_number;
700 if (closure_count == 0)
701 return OK; // no funcrefs created
702
Bram Moolenaar8fa745e2022-09-16 19:04:24 +0100703 // Compute "top": the first entry in the stack used by the function.
704 // This is the first argument (after that comes the stack frame and then
705 // the local variables).
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200706 argcount = ufunc_argcount(dfunc->df_ufunc);
707 top = ectx->ec_frame_idx - argcount;
708
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200709 // Check if any created closure is still in use.
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200710 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200711 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200712 partial_T *pt;
713 int off = gap->ga_len - closure_count + idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200714
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200715 if (off < 0)
716 continue; // count is off or already done
717 pt = ((partial_T **)gap->ga_data)[off];
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200718 if (pt->pt_refcount > 1)
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200719 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200720 int refcount = pt->pt_refcount;
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200721 int i;
722
Dominique Pelleaf4a61a2021-12-27 17:21:41 +0000723 // A Reference in a local variable doesn't count, it gets
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200724 // unreferenced on return.
725 for (i = 0; i < dfunc->df_varcount; ++i)
726 {
727 typval_T *stv = STACK_TV(ectx->ec_frame_idx
728 + STACK_FRAME_SIZE + i);
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200729 if (stv->v_type == VAR_PARTIAL && pt == stv->vval.v_partial)
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200730 --refcount;
731 }
732 if (refcount > 1)
733 {
734 closure_in_use = TRUE;
735 break;
736 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200737 }
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200738 }
739
740 if (closure_in_use)
741 {
742 funcstack_T *funcstack = ALLOC_CLEAR_ONE(funcstack_T);
743 typval_T *stack;
744
745 // A closure is using the arguments and/or local variables.
746 // Move them to the called function.
747 if (funcstack == NULL)
748 return FAIL;
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000749
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200750 funcstack->fs_var_offset = argcount + STACK_FRAME_SIZE;
Bram Moolenaar1936c762022-09-28 15:19:10 +0100751 funcstack->fs_ga.ga_len = funcstack->fs_var_offset
752 + dfunc->df_varcount;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200753 stack = ALLOC_CLEAR_MULT(typval_T, funcstack->fs_ga.ga_len);
754 funcstack->fs_ga.ga_data = stack;
755 if (stack == NULL)
756 {
757 vim_free(funcstack);
758 return FAIL;
759 }
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000760 add_funcstack_to_list(funcstack);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200761
762 // Move or copy the arguments.
763 for (idx = 0; idx < argcount; ++idx)
764 {
765 tv = STACK_TV(top + idx);
766 if (free_arguments)
767 {
768 *(stack + idx) = *tv;
769 tv->v_type = VAR_UNKNOWN;
770 }
771 else
772 copy_tv(tv, stack + idx);
773 }
Bram Moolenaar8fa745e2022-09-16 19:04:24 +0100774 // Skip the stack frame.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200775 // Move the local variables.
776 for (idx = 0; idx < dfunc->df_varcount; ++idx)
777 {
778 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + idx);
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200779
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200780 // A partial created for a local function, that is also used as a
781 // local variable, has a reference count for the variable, thus
782 // will never go down to zero. When all these refcounts are one
783 // then the funcstack is unused. We need to count how many we have
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000784 // so we know when to check.
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200785 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL)
786 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200787 int i;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200788
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200789 for (i = 0; i < closure_count; ++i)
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200790 if (tv->vval.v_partial == ((partial_T **)gap->ga_data)[
791 gap->ga_len - closure_count + i])
792 ++funcstack->fs_min_refcount;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200793 }
794
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200795 *(stack + funcstack->fs_var_offset + idx) = *tv;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200796 tv->v_type = VAR_UNKNOWN;
797 }
798
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200799 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200800 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200801 partial_T *pt = ((partial_T **)gap->ga_data)[gap->ga_len
802 - closure_count + idx];
803 if (pt->pt_refcount > 1)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200804 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200805 ++funcstack->fs_refcount;
806 pt->pt_funcstack = funcstack;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100807 pt->pt_outer.out_stack = &funcstack->fs_ga;
808 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx - top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200809 }
810 }
811 }
812
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200813 for (idx = 0; idx < closure_count; ++idx)
814 partial_unref(((partial_T **)gap->ga_data)[gap->ga_len
815 - closure_count + idx]);
816 gap->ga_len -= closure_count;
817 if (gap->ga_len == 0)
818 ga_clear(gap);
819
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200820 return OK;
821}
822
823/*
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200824 * Called when a partial is freed or its reference count goes down to one. The
825 * funcstack may be the only reference to the partials in the local variables.
826 * Go over all of them, the funcref and can be freed if all partials
827 * referencing the funcstack have a reference count of one.
Bram Moolenaaracd6b992022-09-17 16:27:39 +0100828 * Returns TRUE if the funcstack is freed, the partial referencing it will then
829 * also have been freed.
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200830 */
Bram Moolenaaracd6b992022-09-17 16:27:39 +0100831 int
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200832funcstack_check_refcount(funcstack_T *funcstack)
833{
Bram Moolenaaracd6b992022-09-17 16:27:39 +0100834 int i;
835 garray_T *gap = &funcstack->fs_ga;
836 int done = 0;
837 typval_T *stack;
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200838
839 if (funcstack->fs_refcount > funcstack->fs_min_refcount)
Bram Moolenaaracd6b992022-09-17 16:27:39 +0100840 return FALSE;
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200841 for (i = funcstack->fs_var_offset; i < gap->ga_len; ++i)
842 {
843 typval_T *tv = ((typval_T *)gap->ga_data) + i;
844
845 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL
846 && tv->vval.v_partial->pt_funcstack == funcstack
847 && tv->vval.v_partial->pt_refcount == 1)
848 ++done;
849 }
Bram Moolenaaracd6b992022-09-17 16:27:39 +0100850 if (done != funcstack->fs_min_refcount)
851 return FALSE;
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200852
Bram Moolenaaracd6b992022-09-17 16:27:39 +0100853 stack = gap->ga_data;
854
855 // All partials referencing the funcstack have a reference count of
856 // one, thus the funcstack is no longer of use.
857 for (i = 0; i < gap->ga_len; ++i)
858 clear_tv(stack + i);
859 vim_free(stack);
860 remove_funcstack_from_list(funcstack);
861 vim_free(funcstack);
862
863 return TRUE;
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200864}
865
866/*
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000867 * For garbage collecting: set references in all variables referenced by
868 * all funcstacks.
869 */
870 int
871set_ref_in_funcstacks(int copyID)
872{
873 funcstack_T *funcstack;
874
875 for (funcstack = first_funcstack; funcstack != NULL;
876 funcstack = funcstack->fs_next)
877 {
878 typval_T *stack = funcstack->fs_ga.ga_data;
879 int i;
880
881 for (i = 0; i < funcstack->fs_ga.ga_len; ++i)
882 if (set_ref_in_item(stack + i, copyID, NULL, NULL))
883 return TRUE; // abort
884 }
885 return FALSE;
886}
887
Bram Moolenaar806a2732022-09-04 15:40:36 +0100888// Ugly static to avoid passing the execution context around through many
889// layers.
890static ectx_T *current_ectx = NULL;
891
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000892/*
Bram Moolenaar806a2732022-09-04 15:40:36 +0100893 * Return TRUE if currently executing a :def function.
894 * Can be used by builtin functions only.
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100895 */
Bram Moolenaar806a2732022-09-04 15:40:36 +0100896 int
897in_def_function(void)
898{
899 return current_ectx != NULL;
900}
901
902/*
Bram Moolenaar98aff652022-09-06 21:02:35 +0100903 * Clear "current_ectx" and return the previous value. To be used when calling
904 * a user function.
905 */
906 ectx_T *
dundargocc57b5bc2022-11-02 13:30:51 +0000907clear_current_ectx(void)
Bram Moolenaar98aff652022-09-06 21:02:35 +0100908{
909 ectx_T *r = current_ectx;
910
911 current_ectx = NULL;
912 return r;
913}
914
915 void
916restore_current_ectx(ectx_T *ectx)
917{
918 if (current_ectx != NULL)
919 iemsg("Restoring current_ectx while it is not NULL");
920 current_ectx = ectx;
921}
922
923/*
Bram Moolenaar806a2732022-09-04 15:40:36 +0100924 * Add an entry for a deferred function call to the currently executing
925 * function.
926 * Return the list or NULL when failed.
927 */
928 static list_T *
929add_defer_item(int var_idx, int argcount, ectx_T *ectx)
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100930{
931 typval_T *defer_tv = STACK_TV_VAR(var_idx);
932 list_T *defer_l;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100933 list_T *l;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100934 typval_T listval;
935
936 if (defer_tv->v_type != VAR_LIST)
937 {
Bram Moolenaara5348f22022-09-04 11:42:22 +0100938 // first time, allocate the list
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100939 if (rettv_list_alloc(defer_tv) == FAIL)
Bram Moolenaar806a2732022-09-04 15:40:36 +0100940 return NULL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100941 }
942 defer_l = defer_tv->vval.v_list;
943
944 l = list_alloc_with_items(argcount + 1);
945 if (l == NULL)
Bram Moolenaar806a2732022-09-04 15:40:36 +0100946 return NULL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100947 listval.v_type = VAR_LIST;
948 listval.vval.v_list = l;
949 listval.v_lock = 0;
Bram Moolenaara5348f22022-09-04 11:42:22 +0100950 if (list_insert_tv(defer_l, &listval, defer_l->lv_first) == FAIL)
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100951 {
952 vim_free(l);
Bram Moolenaar806a2732022-09-04 15:40:36 +0100953 return NULL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100954 }
955
Bram Moolenaar806a2732022-09-04 15:40:36 +0100956 return l;
957}
958
959/*
960 * Handle ISN_DEFER. Stack has a function reference and "argcount" arguments.
961 * The local variable that lists deferred functions is "var_idx".
962 * Returns OK or FAIL.
963 */
964 static int
965defer_command(int var_idx, int argcount, ectx_T *ectx)
966{
967 list_T *l = add_defer_item(var_idx, argcount, ectx);
968 int i;
969 typval_T *func_tv;
970
971 if (l == NULL)
972 return FAIL;
973
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100974 func_tv = STACK_TV_BOT(-argcount - 1);
Bram Moolenaarf5fec052022-09-11 11:49:22 +0100975 if (func_tv->v_type != VAR_FUNC && func_tv->v_type != VAR_PARTIAL)
976 {
977 semsg(_(e_expected_str_but_got_str),
978 "function or partial",
979 vartype_name(func_tv->v_type));
980 return FAIL;
981 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100982 list_set_item(l, 0, func_tv);
983
Bram Moolenaarf5fec052022-09-11 11:49:22 +0100984 for (i = 0; i < argcount; ++i)
985 list_set_item(l, i + 1, STACK_TV_BOT(-argcount + i));
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100986 ectx->ec_stack.ga_len -= argcount + 1;
987 return OK;
988}
989
990/*
Bram Moolenaar806a2732022-09-04 15:40:36 +0100991 * Add a deferred function "name" with one argument "arg_tv".
992 * Consumes "name", also on failure.
993 * Only to be called when in_def_function() returns TRUE.
994 */
995 int
996add_defer_function(char_u *name, int argcount, typval_T *argvars)
997{
998 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
999 + current_ectx->ec_dfunc_idx;
1000 list_T *l;
1001 typval_T func_tv;
1002 int i;
1003
1004 if (dfunc->df_defer_var_idx == 0)
1005 {
1006 iemsg("df_defer_var_idx is zero");
Bram Moolenaar31ea6bf2022-09-05 10:47:13 +01001007 vim_free(name);
Bram Moolenaar806a2732022-09-04 15:40:36 +01001008 return FAIL;
1009 }
Bram Moolenaar806a2732022-09-04 15:40:36 +01001010
Bram Moolenaarf5fec052022-09-11 11:49:22 +01001011 l = add_defer_item(dfunc->df_defer_var_idx - 1, argcount, current_ectx);
Bram Moolenaar806a2732022-09-04 15:40:36 +01001012 if (l == NULL)
1013 {
Bram Moolenaar31ea6bf2022-09-05 10:47:13 +01001014 vim_free(name);
Bram Moolenaar806a2732022-09-04 15:40:36 +01001015 return FAIL;
1016 }
1017
Bram Moolenaar31ea6bf2022-09-05 10:47:13 +01001018 func_tv.v_type = VAR_FUNC;
1019 func_tv.v_lock = 0;
1020 func_tv.vval.v_string = name;
Bram Moolenaar806a2732022-09-04 15:40:36 +01001021 list_set_item(l, 0, &func_tv);
Bram Moolenaar31ea6bf2022-09-05 10:47:13 +01001022
Bram Moolenaar806a2732022-09-04 15:40:36 +01001023 for (i = 0; i < argcount; ++i)
1024 list_set_item(l, i + 1, argvars + i);
1025 return OK;
1026}
1027
1028/*
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001029 * Invoked when returning from a function: Invoke any deferred calls.
1030 */
1031 static void
1032invoke_defer_funcs(ectx_T *ectx)
1033{
1034 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1035 + ectx->ec_dfunc_idx;
1036 typval_T *defer_tv = STACK_TV_VAR(dfunc->df_defer_var_idx - 1);
1037 listitem_T *li;
1038
1039 if (defer_tv->v_type != VAR_LIST)
1040 return; // no function added
1041 for (li = defer_tv->vval.v_list->lv_first; li != NULL; li = li->li_next)
1042 {
1043 list_T *l = li->li_tv.vval.v_list;
1044 typval_T rettv;
1045 typval_T argvars[MAX_FUNC_ARGS];
1046 int i;
1047 listitem_T *arg_li = l->lv_first;
1048 funcexe_T funcexe;
1049
1050 for (i = 0; i < l->lv_len - 1; ++i)
1051 {
1052 arg_li = arg_li->li_next;
1053 argvars[i] = arg_li->li_tv;
1054 }
1055
1056 CLEAR_FIELD(funcexe);
1057 funcexe.fe_evaluate = TRUE;
1058 rettv.v_type = VAR_UNKNOWN;
1059 (void)call_func(l->lv_first->li_tv.vval.v_string, -1,
1060 &rettv, l->lv_len - 1, argvars, &funcexe);
1061 clear_tv(&rettv);
1062 }
1063}
1064
1065/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001066 * Return from the current function.
1067 */
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001068 static int
Bram Moolenaar4c137212021-04-19 16:48:48 +02001069func_return(ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001070{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001071 int idx;
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01001072 int ret_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001073 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1074 + ectx->ec_dfunc_idx;
1075 int argcount = ufunc_argcount(dfunc->df_ufunc);
1076 int top = ectx->ec_frame_idx - argcount;
Bram Moolenaarc620c052020-07-08 15:16:19 +02001077 estack_T *entry;
Bram Moolenaar12d26532021-02-19 19:13:21 +01001078 int prev_dfunc_idx = STACK_TV(ectx->ec_frame_idx
1079 + STACK_FRAME_FUNC_OFF)->vval.v_number;
Bram Moolenaarb06b50d2021-04-26 21:39:25 +02001080 funclocal_T *floc;
1081#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01001082 dfunc_T *prev_dfunc = ((dfunc_T *)def_functions.ga_data)
1083 + prev_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001084
Bram Moolenaar12d26532021-02-19 19:13:21 +01001085 if (do_profiling == PROF_YES)
1086 {
1087 ufunc_T *caller = prev_dfunc->df_ufunc;
1088
1089 if (dfunc->df_ufunc->uf_profiling
1090 || (caller != NULL && caller->uf_profiling))
1091 {
1092 profile_may_end_func(((profinfo_T *)profile_info_ga.ga_data)
1093 + profile_info_ga.ga_len - 1, dfunc->df_ufunc, caller);
1094 --profile_info_ga.ga_len;
1095 }
1096 }
1097#endif
Bram Moolenaar919c12c2021-12-14 14:29:16 +00001098
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001099 if (dfunc->df_defer_var_idx > 0)
1100 invoke_defer_funcs(ectx);
1101
Bram Moolenaar919c12c2021-12-14 14:29:16 +00001102 // No check for uf_refcount being zero, cannot think of a way that would
1103 // happen.
Bram Moolenaarc970e422021-03-17 15:03:04 +01001104 --dfunc->df_ufunc->uf_calls;
1105
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001106 // execution context goes one level up
Bram Moolenaarc620c052020-07-08 15:16:19 +02001107 entry = estack_pop();
1108 if (entry != NULL)
Bram Moolenaarc70fe462021-04-17 17:59:19 +02001109 current_sctx = entry->es_save_sctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001110
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001111 if (handle_closure_in_use(ectx, TRUE) == FAIL)
1112 return FAIL;
1113
1114 // Clear the arguments.
1115 for (idx = top; idx < ectx->ec_frame_idx; ++idx)
1116 clear_tv(STACK_TV(idx));
1117
1118 // Clear local variables and temp values, but not the return value.
1119 for (idx = ectx->ec_frame_idx + STACK_FRAME_SIZE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001120 idx < ectx->ec_stack.ga_len - 1; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001121 clear_tv(STACK_TV(idx));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001122
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01001123 // The return value should be on top of the stack. However, when aborting
1124 // it may not be there and ec_frame_idx is the top of the stack.
1125 ret_idx = ectx->ec_stack.ga_len - 1;
Bram Moolenaar0186e582021-01-10 18:33:11 +01001126 if (ret_idx == ectx->ec_frame_idx + STACK_FRAME_IDX_OFF)
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01001127 ret_idx = 0;
1128
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001129 if (ectx->ec_outer_ref != NULL)
1130 {
1131 if (ectx->ec_outer_ref->or_outer_allocated)
1132 vim_free(ectx->ec_outer_ref->or_outer);
1133 partial_unref(ectx->ec_outer_ref->or_partial);
1134 vim_free(ectx->ec_outer_ref);
1135 }
Bram Moolenaar0186e582021-01-10 18:33:11 +01001136
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001137 // Restore the previous frame.
Bram Moolenaar12d26532021-02-19 19:13:21 +01001138 ectx->ec_dfunc_idx = prev_dfunc_idx;
Bram Moolenaar0186e582021-01-10 18:33:11 +01001139 ectx->ec_iidx = STACK_TV(ectx->ec_frame_idx
1140 + STACK_FRAME_IIDX_OFF)->vval.v_number;
Bram Moolenaar5930ddc2021-04-26 20:32:59 +02001141 ectx->ec_instr = (void *)STACK_TV(ectx->ec_frame_idx
1142 + STACK_FRAME_INSTR_OFF)->vval.v_string;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001143 ectx->ec_outer_ref = (void *)STACK_TV(ectx->ec_frame_idx
Bram Moolenaar0186e582021-01-10 18:33:11 +01001144 + STACK_FRAME_OUTER_OFF)->vval.v_string;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001145 floc = (void *)STACK_TV(ectx->ec_frame_idx
1146 + STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string;
Bram Moolenaar5366e1a2020-10-01 13:01:34 +02001147 // restoring ec_frame_idx must be last
Bram Moolenaar0186e582021-01-10 18:33:11 +01001148 ectx->ec_frame_idx = STACK_TV(ectx->ec_frame_idx
1149 + STACK_FRAME_IDX_OFF)->vval.v_number;
Bram Moolenaard386e922021-04-25 14:48:49 +02001150
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001151 if (floc == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02001152 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001153 else
1154 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02001155 ectx->ec_funclocal = *floc;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001156 vim_free(floc);
1157 }
1158
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01001159 if (ret_idx > 0)
1160 {
1161 // Reset the stack to the position before the call, with a spot for the
1162 // return value, moved there from above the frame.
1163 ectx->ec_stack.ga_len = top + 1;
1164 *STACK_TV_BOT(-1) = *STACK_TV(ret_idx);
1165 }
1166 else
1167 // Reset the stack to the position before the call.
1168 ectx->ec_stack.ga_len = top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001169
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001170 funcdepth_decrement();
Bram Moolenaare99d4222021-06-13 14:01:26 +02001171 --ex_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001172 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001173}
1174
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001175/*
1176 * Prepare arguments and rettv for calling a builtin or user function.
1177 */
1178 static int
1179call_prepare(int argcount, typval_T *argvars, ectx_T *ectx)
1180{
1181 int idx;
1182 typval_T *tv;
1183
1184 // Move arguments from bottom of the stack to argvars[] and add terminator.
1185 for (idx = 0; idx < argcount; ++idx)
1186 argvars[idx] = *STACK_TV_BOT(idx - argcount);
1187 argvars[argcount].v_type = VAR_UNKNOWN;
1188
1189 // Result replaces the arguments on the stack.
1190 if (argcount > 0)
1191 ectx->ec_stack.ga_len -= argcount - 1;
Bram Moolenaar35578162021-08-02 19:10:38 +02001192 else if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001193 return FAIL;
1194 else
1195 ++ectx->ec_stack.ga_len;
1196
1197 // Default return value is zero.
1198 tv = STACK_TV_BOT(-1);
1199 tv->v_type = VAR_NUMBER;
1200 tv->vval.v_number = 0;
Bram Moolenaar24565cf2022-03-28 18:16:52 +01001201 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001202
1203 return OK;
1204}
1205
1206/*
1207 * Call a builtin function by index.
1208 */
1209 static int
1210call_bfunc(int func_idx, int argcount, ectx_T *ectx)
1211{
1212 typval_T argvars[MAX_FUNC_ARGS];
1213 int idx;
Bram Moolenaar171fb922020-10-28 16:54:47 +01001214 int did_emsg_before = did_emsg;
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001215 ectx_T *prev_ectx = current_ectx;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001216 char *save_func_name = ectx->ec_where.wt_func_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001217
1218 if (call_prepare(argcount, argvars, ectx) == FAIL)
1219 return FAIL;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001220 ectx->ec_where.wt_func_name = internal_func_name(func_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001221
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001222 // Call the builtin function. Set "current_ectx" so that when it
1223 // recursively invokes call_def_function() a closure context can be set.
1224 current_ectx = ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001225 call_internal_func_by_idx(func_idx, argvars, STACK_TV_BOT(-1));
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001226 current_ectx = prev_ectx;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001227 ectx->ec_where.wt_func_name = save_func_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001228
1229 // Clear the arguments.
1230 for (idx = 0; idx < argcount; ++idx)
1231 clear_tv(&argvars[idx]);
Bram Moolenaar015f4262020-05-05 21:25:22 +02001232
Bram Moolenaar57f799e2020-12-12 20:42:19 +01001233 if (did_emsg > did_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +02001234 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001235 return OK;
1236}
1237
1238/*
1239 * Execute a user defined function.
Bram Moolenaarab360522021-01-10 14:02:28 +01001240 * If the function is compiled this will add a stack frame and set the
1241 * instruction pointer at the start of the function.
1242 * Otherwise the function is called here.
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001243 * If "pt" is not null use "pt->pt_outer" for ec_outer_ref->or_outer.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001244 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001245 */
1246 static int
Bram Moolenaar0186e582021-01-10 18:33:11 +01001247call_ufunc(
1248 ufunc_T *ufunc,
1249 partial_T *pt,
1250 int argcount,
1251 ectx_T *ectx,
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001252 isn_T *iptr,
1253 dict_T *selfdict)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001254{
1255 typval_T argvars[MAX_FUNC_ARGS];
1256 funcexe_T funcexe;
1257 int error;
1258 int idx;
Bram Moolenaar28ee8922020-10-28 20:20:00 +01001259 int did_emsg_before = did_emsg;
Bram Moolenaar139575d2022-03-15 19:29:30 +00001260 compiletype_T compile_type = get_compile_type(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001261
Bram Moolenaare99d4222021-06-13 14:01:26 +02001262 if (func_needs_compiling(ufunc, compile_type)
1263 && compile_def_function(ufunc, FALSE, compile_type, NULL)
1264 == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02001265 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001266 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001267 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +01001268 error = check_user_func_argcount(ufunc, argcount);
Bram Moolenaar50824712020-12-20 21:10:17 +01001269 if (error != FCERR_UNKNOWN)
1270 {
1271 if (error == FCERR_TOOMANY)
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001272 semsg(_(e_too_many_arguments_for_function_str),
1273 printable_func_name(ufunc));
Bram Moolenaar50824712020-12-20 21:10:17 +01001274 else
Bram Moolenaare1242042021-12-16 20:56:57 +00001275 semsg(_(e_not_enough_arguments_for_function_str),
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001276 printable_func_name(ufunc));
Bram Moolenaar50824712020-12-20 21:10:17 +01001277 return FAIL;
1278 }
1279
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001280 // The function has been compiled, can call it quickly. For a function
1281 // that was defined later: we can call it directly next time.
1282 if (iptr != NULL)
1283 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001284 delete_instr(iptr);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001285 iptr->isn_type = ISN_DCALL;
1286 iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1287 iptr->isn_arg.dfunc.cdf_argcount = argcount;
1288 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001289 return call_dfunc(ufunc->uf_dfunc_idx, pt, argcount, ectx);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001290 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001291
1292 if (call_prepare(argcount, argvars, ectx) == FAIL)
1293 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +02001294 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00001295 funcexe.fe_evaluate = TRUE;
1296 funcexe.fe_selfdict = selfdict != NULL ? selfdict : dict_stack_get_dict();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001297
1298 // Call the user function. Result goes in last position on the stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001299 error = call_user_func_check(ufunc, argcount, argvars,
Bram Moolenaar851f86b2021-12-13 14:26:44 +00001300 STACK_TV_BOT(-1), &funcexe, funcexe.fe_selfdict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001301
1302 // Clear the arguments.
1303 for (idx = 0; idx < argcount; ++idx)
1304 clear_tv(&argvars[idx]);
1305
1306 if (error != FCERR_NONE)
1307 {
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01001308 user_func_error(error, printable_func_name(ufunc),
1309 funcexe.fe_found_var);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001310 return FAIL;
1311 }
Bram Moolenaar28ee8922020-10-28 20:20:00 +01001312 if (did_emsg > did_emsg_before)
Bram Moolenaared677f52020-08-12 16:38:10 +02001313 // Error other than from calling the function itself.
1314 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001315 return OK;
1316}
1317
1318/*
Bram Moolenaara91a7132021-03-25 21:12:15 +01001319 * If command modifiers were applied restore them.
1320 */
1321 static void
1322may_restore_cmdmod(funclocal_T *funclocal)
1323{
1324 if (funclocal->floc_restore_cmdmod)
1325 {
1326 cmdmod.cmod_filter_regmatch.regprog = NULL;
1327 undo_cmdmod(&cmdmod);
1328 cmdmod = funclocal->floc_save_cmdmod;
1329 funclocal->floc_restore_cmdmod = FALSE;
1330 }
1331}
1332
1333/*
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001334 * Return TRUE if an error was given (not caught in try/catch) or CTRL-C was
1335 * pressed.
Bram Moolenaara1773442020-08-12 15:21:22 +02001336 */
1337 static int
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001338vim9_aborting(int prev_uncaught_emsg)
Bram Moolenaara1773442020-08-12 15:21:22 +02001339{
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001340 return uncaught_emsg > prev_uncaught_emsg || got_int || did_throw;
Bram Moolenaara1773442020-08-12 15:21:22 +02001341}
1342
1343/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001344 * Execute a function by "name".
1345 * This can be a builtin function or a user function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001346 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001347 * Returns FAIL if not found without an error message.
1348 */
1349 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001350call_by_name(
1351 char_u *name,
1352 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001353 ectx_T *ectx,
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001354 isn_T *iptr,
1355 dict_T *selfdict)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001356{
1357 ufunc_T *ufunc;
1358
1359 if (builtin_function(name, -1))
1360 {
1361 int func_idx = find_internal_func(name);
1362
Bram Moolenaar1983f1a2022-02-28 20:55:02 +00001363 if (func_idx < 0) // Impossible?
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001364 return FAIL;
Bram Moolenaar389df252020-07-09 21:20:47 +02001365 if (check_internal_func(func_idx, argcount) < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001366 return FAIL;
1367 return call_bfunc(func_idx, argcount, ectx);
1368 }
1369
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00001370 ufunc = find_func(name, FALSE);
Bram Moolenaara1773442020-08-12 15:21:22 +02001371
1372 if (ufunc == NULL)
1373 {
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001374 int prev_uncaught_emsg = uncaught_emsg;
Bram Moolenaara1773442020-08-12 15:21:22 +02001375
1376 if (script_autoload(name, TRUE))
1377 // loaded a package, search for the function again
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00001378 ufunc = find_func(name, FALSE);
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001379
1380 if (vim9_aborting(prev_uncaught_emsg))
Bram Moolenaara1773442020-08-12 15:21:22 +02001381 return FAIL; // bail out if loading the script caused an error
1382 }
1383
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001384 if (ufunc != NULL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001385 {
Bram Moolenaar0d89d8a2022-12-31 14:01:24 +00001386 if (check_ufunc_arg_types(ufunc, argcount, 0, ectx) == FAIL)
1387 return FAIL;
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001388
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001389 return call_ufunc(ufunc, NULL, argcount, ectx, iptr, selfdict);
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001390 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001391
1392 return FAIL;
1393}
1394
1395 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001396call_partial(
1397 typval_T *tv,
1398 int argcount_arg,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001399 ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001400{
Bram Moolenaara90afb92020-07-15 22:38:56 +02001401 int argcount = argcount_arg;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001402 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001403 int called_emsg_before = called_emsg;
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +01001404 int res = FAIL;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001405 dict_T *selfdict = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001406
1407 if (tv->v_type == VAR_PARTIAL)
1408 {
Bram Moolenaara90afb92020-07-15 22:38:56 +02001409 partial_T *pt = tv->vval.v_partial;
1410 int i;
1411
1412 if (pt->pt_argc > 0)
1413 {
1414 // Make space for arguments from the partial, shift the "argcount"
1415 // arguments up.
Bram Moolenaar35578162021-08-02 19:10:38 +02001416 if (GA_GROW_FAILS(&ectx->ec_stack, pt->pt_argc))
Bram Moolenaara90afb92020-07-15 22:38:56 +02001417 return FAIL;
1418 for (i = 1; i <= argcount; ++i)
1419 *STACK_TV_BOT(-i + pt->pt_argc) = *STACK_TV_BOT(-i);
1420 ectx->ec_stack.ga_len += pt->pt_argc;
1421 argcount += pt->pt_argc;
1422
1423 // copy the arguments from the partial onto the stack
1424 for (i = 0; i < pt->pt_argc; ++i)
1425 copy_tv(&pt->pt_argv[i], STACK_TV_BOT(-argcount + i));
1426 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001427 selfdict = pt->pt_dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001428
1429 if (pt->pt_func != NULL)
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001430 return call_ufunc(pt->pt_func, pt, argcount, ectx, NULL, selfdict);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001431
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001432 name = pt->pt_name;
1433 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001434 else if (tv->v_type == VAR_FUNC)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001435 name = tv->vval.v_string;
Bram Moolenaar95006e32020-08-29 17:47:08 +02001436 if (name != NULL)
1437 {
1438 char_u fname_buf[FLEN_FIXED + 1];
1439 char_u *tofree = NULL;
1440 int error = FCERR_NONE;
1441 char_u *fname;
1442
1443 // May need to translate <SNR>123_ to K_SNR.
1444 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
1445 if (error != FCERR_NONE)
1446 res = FAIL;
1447 else
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001448 res = call_by_name(fname, argcount, ectx, NULL, selfdict);
Bram Moolenaar95006e32020-08-29 17:47:08 +02001449 vim_free(tofree);
1450 }
1451
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +01001452 if (res == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001453 {
1454 if (called_emsg == called_emsg_before)
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001455 emsg_funcname(e_unknown_function_str,
Bram Moolenaar015f4262020-05-05 21:25:22 +02001456 name == NULL ? (char_u *)"[unknown]" : name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001457 return FAIL;
1458 }
1459 return OK;
1460}
1461
1462/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001463 * Check if "lock" is VAR_LOCKED or VAR_FIXED. If so give an error and return
1464 * TRUE.
1465 */
1466 static int
1467error_if_locked(int lock, char *error)
1468{
1469 if (lock & (VAR_LOCKED | VAR_FIXED))
1470 {
1471 emsg(_(error));
1472 return TRUE;
1473 }
1474 return FALSE;
1475}
1476
1477/*
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001478 * Give an error if "tv" is not a number and return FAIL.
1479 */
1480 static int
1481check_for_number(typval_T *tv)
1482{
1483 if (tv->v_type != VAR_NUMBER)
1484 {
1485 semsg(_(e_expected_str_but_got_str),
1486 vartype_name(VAR_NUMBER), vartype_name(tv->v_type));
1487 return FAIL;
1488 }
1489 return OK;
1490}
1491
1492/*
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001493 * Store "tv" in variable "name".
1494 * This is for s: and g: variables.
1495 */
1496 static void
1497store_var(char_u *name, typval_T *tv)
1498{
1499 funccal_entry_T entry;
Bram Moolenaard877a572021-04-01 19:42:48 +02001500 int flags = ASSIGN_DECL;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001501
Bram Moolenaard877a572021-04-01 19:42:48 +02001502 if (tv->v_lock)
1503 flags |= ASSIGN_CONST;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001504 save_funccal(&entry);
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001505 set_var_const(name, 0, NULL, tv, FALSE, flags, 0);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001506 restore_funccal();
1507}
1508
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001509/*
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001510 * Convert "tv" to a string.
1511 * Return FAIL if not allowed.
1512 */
1513 static int
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001514do_2string(typval_T *tv, int is_2string_any, int tolerant)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001515{
1516 if (tv->v_type != VAR_STRING)
1517 {
1518 char_u *str;
1519
1520 if (is_2string_any)
1521 {
1522 switch (tv->v_type)
1523 {
1524 case VAR_SPECIAL:
1525 case VAR_BOOL:
1526 case VAR_NUMBER:
1527 case VAR_FLOAT:
1528 case VAR_BLOB: break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001529
1530 case VAR_LIST:
1531 if (tolerant)
1532 {
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001533 char_u *s, *e, *p;
1534 garray_T ga;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001535
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001536 ga_init2(&ga, sizeof(char_u *), 1);
1537
1538 // Convert to NL separated items, then
1539 // escape the items and replace the NL with
1540 // a space.
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001541 str = typval2string(tv, TRUE);
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001542 if (str == NULL)
1543 return FAIL;
1544 s = str;
1545 while ((e = vim_strchr(s, '\n')) != NULL)
1546 {
1547 *e = NUL;
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +01001548 p = vim_strsave_fnameescape(s,
1549 VSE_NONE);
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001550 if (p != NULL)
1551 {
1552 ga_concat(&ga, p);
1553 ga_concat(&ga, (char_u *)" ");
1554 vim_free(p);
1555 }
1556 s = e + 1;
1557 }
1558 vim_free(str);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001559 clear_tv(tv);
1560 tv->v_type = VAR_STRING;
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001561 tv->vval.v_string = ga.ga_data;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001562 return OK;
1563 }
1564 // FALLTHROUGH
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001565 default: to_string_error(tv->v_type);
1566 return FAIL;
1567 }
1568 }
Bram Moolenaar34453202021-01-31 13:08:38 +01001569 str = typval_tostring(tv, TRUE);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001570 clear_tv(tv);
1571 tv->v_type = VAR_STRING;
1572 tv->vval.v_string = str;
1573 }
1574 return OK;
1575}
1576
1577/*
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001578 * When the value of "sv" is a null list of dict, allocate it.
1579 */
1580 static void
Bram Moolenaar859cc212022-03-28 15:22:35 +01001581allocate_if_null(svar_T *sv)
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001582{
Bram Moolenaar859cc212022-03-28 15:22:35 +01001583 typval_T *tv = sv->sv_tv;
1584
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001585 switch (tv->v_type)
1586 {
1587 case VAR_LIST:
Bram Moolenaar859cc212022-03-28 15:22:35 +01001588 if (tv->vval.v_list == NULL && sv->sv_type != &t_list_empty)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001589 (void)rettv_list_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001590 break;
1591 case VAR_DICT:
Bram Moolenaar859cc212022-03-28 15:22:35 +01001592 if (tv->vval.v_dict == NULL && sv->sv_type != &t_dict_empty)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001593 (void)rettv_dict_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001594 break;
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02001595 case VAR_BLOB:
Bram Moolenaar859cc212022-03-28 15:22:35 +01001596 if (tv->vval.v_blob == NULL && sv->sv_type != &t_blob_null)
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02001597 (void)rettv_blob_alloc(tv);
1598 break;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001599 default:
1600 break;
1601 }
1602}
Bram Moolenaard3aac292020-04-19 14:32:17 +02001603
Bram Moolenaare7525c52021-01-09 13:20:37 +01001604/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001605 * Return the character "str[index]" where "index" is the character index,
1606 * including composing characters.
1607 * If "index" is out of range NULL is returned.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001608 */
1609 char_u *
1610char_from_string(char_u *str, varnumber_T index)
1611{
1612 size_t nbyte = 0;
1613 varnumber_T nchar = index;
1614 size_t slen;
1615
1616 if (str == NULL)
1617 return NULL;
1618 slen = STRLEN(str);
1619
Bram Moolenaarff871402021-03-26 13:34:05 +01001620 // Do the same as for a list: a negative index counts from the end.
1621 // Optimization to check the first byte to be below 0x80 (and no composing
1622 // character follows) makes this a lot faster.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001623 if (index < 0)
1624 {
1625 int clen = 0;
1626
1627 for (nbyte = 0; nbyte < slen; ++clen)
Bram Moolenaarff871402021-03-26 13:34:05 +01001628 {
1629 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1630 ++nbyte;
1631 else if (enc_utf8)
1632 nbyte += utfc_ptr2len(str + nbyte);
1633 else
1634 nbyte += mb_ptr2len(str + nbyte);
1635 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001636 nchar = clen + index;
1637 if (nchar < 0)
1638 // unlike list: index out of range results in empty string
1639 return NULL;
1640 }
1641
1642 for (nbyte = 0; nchar > 0 && nbyte < slen; --nchar)
Bram Moolenaarff871402021-03-26 13:34:05 +01001643 {
1644 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1645 ++nbyte;
1646 else if (enc_utf8)
1647 nbyte += utfc_ptr2len(str + nbyte);
1648 else
1649 nbyte += mb_ptr2len(str + nbyte);
1650 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001651 if (nbyte >= slen)
1652 return NULL;
Bram Moolenaar0289a092021-03-14 18:40:19 +01001653 return vim_strnsave(str + nbyte, mb_ptr2len(str + nbyte));
Bram Moolenaare7525c52021-01-09 13:20:37 +01001654}
1655
1656/*
1657 * Get the byte index for character index "idx" in string "str" with length
Bram Moolenaar0289a092021-03-14 18:40:19 +01001658 * "str_len". Composing characters are included.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001659 * If going over the end return "str_len".
1660 * If "idx" is negative count from the end, -1 is the last character.
1661 * When going over the start return -1.
1662 */
1663 static long
1664char_idx2byte(char_u *str, size_t str_len, varnumber_T idx)
1665{
1666 varnumber_T nchar = idx;
1667 size_t nbyte = 0;
1668
1669 if (nchar >= 0)
1670 {
1671 while (nchar > 0 && nbyte < str_len)
1672 {
Bram Moolenaar0289a092021-03-14 18:40:19 +01001673 nbyte += mb_ptr2len(str + nbyte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001674 --nchar;
1675 }
1676 }
1677 else
1678 {
1679 nbyte = str_len;
1680 while (nchar < 0 && nbyte > 0)
1681 {
1682 --nbyte;
1683 nbyte -= mb_head_off(str, str + nbyte);
1684 ++nchar;
1685 }
1686 if (nchar < 0)
1687 return -1;
1688 }
1689 return (long)nbyte;
1690}
1691
1692/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001693 * Return the slice "str[first : last]" using character indexes. Composing
1694 * characters are included.
Bram Moolenaar6601b622021-01-13 21:47:15 +01001695 * "exclusive" is TRUE for slice().
Bram Moolenaare7525c52021-01-09 13:20:37 +01001696 * Return NULL when the result is empty.
1697 */
1698 char_u *
Bram Moolenaar6601b622021-01-13 21:47:15 +01001699string_slice(char_u *str, varnumber_T first, varnumber_T last, int exclusive)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001700{
1701 long start_byte, end_byte;
1702 size_t slen;
1703
1704 if (str == NULL)
1705 return NULL;
1706 slen = STRLEN(str);
1707 start_byte = char_idx2byte(str, slen, first);
1708 if (start_byte < 0)
1709 start_byte = 0; // first index very negative: use zero
Bram Moolenaar6601b622021-01-13 21:47:15 +01001710 if ((last == -1 && !exclusive) || last == VARNUM_MAX)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001711 end_byte = (long)slen;
1712 else
1713 {
1714 end_byte = char_idx2byte(str, slen, last);
Bram Moolenaar6601b622021-01-13 21:47:15 +01001715 if (!exclusive && end_byte >= 0 && end_byte < (long)slen)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001716 // end index is inclusive
Bram Moolenaar0289a092021-03-14 18:40:19 +01001717 end_byte += mb_ptr2len(str + end_byte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001718 }
1719
1720 if (start_byte >= (long)slen || end_byte <= start_byte)
1721 return NULL;
1722 return vim_strnsave(str + start_byte, end_byte - start_byte);
1723}
1724
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001725/*
1726 * Get a script variable for ISN_STORESCRIPT and ISN_LOADSCRIPT.
1727 * When "dfunc_idx" is negative don't give an error.
1728 * Returns NULL for an error.
1729 */
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001730 static svar_T *
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001731get_script_svar(scriptref_T *sref, int dfunc_idx)
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001732{
1733 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001734 dfunc_T *dfunc = dfunc_idx < 0 ? NULL
1735 : ((dfunc_T *)def_functions.ga_data) + dfunc_idx;
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001736 svar_T *sv;
1737
1738 if (sref->sref_seq != si->sn_script_seq)
1739 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001740 // The script was reloaded after the function was compiled, the
1741 // script_idx may not be valid.
1742 if (dfunc != NULL)
1743 semsg(_(e_script_variable_invalid_after_reload_in_function_str),
1744 printable_func_name(dfunc->df_ufunc));
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001745 return NULL;
1746 }
1747 sv = ((svar_T *)si->sn_var_vals.ga_data) + sref->sref_idx;
Bram Moolenaar6de22962022-09-09 21:35:36 +01001748 if (sv->sv_name == NULL)
1749 {
1750 if (dfunc != NULL)
1751 emsg(_(e_script_variable_was_deleted));
1752 return NULL;
1753 }
Bram Moolenaar60dc8272021-07-29 22:48:54 +02001754 if (!equal_type(sv->sv_type, sref->sref_type, 0))
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001755 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001756 if (dfunc != NULL)
1757 emsg(_(e_script_variable_type_changed));
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001758 return NULL;
1759 }
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001760
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01001761 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0
1762 && sref->sref_sid != current_sctx.sc_sid)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001763 {
1764 if (dfunc != NULL)
1765 semsg(_(e_item_not_exported_in_script_str), sv->sv_name);
1766 return NULL;
1767 }
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001768 return sv;
1769}
1770
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001771/*
Bram Moolenaar20677332021-06-06 17:02:53 +02001772 * Function passed to do_cmdline() for splitting a script joined by NL
1773 * characters.
1774 */
1775 static char_u *
1776get_split_sourceline(
1777 int c UNUSED,
1778 void *cookie,
1779 int indent UNUSED,
1780 getline_opt_T options UNUSED)
1781{
1782 source_cookie_T *sp = (source_cookie_T *)cookie;
1783 char_u *p;
1784 char_u *line;
1785
Bram Moolenaar20677332021-06-06 17:02:53 +02001786 p = vim_strchr(sp->nextline, '\n');
1787 if (p == NULL)
1788 {
1789 line = vim_strsave(sp->nextline);
1790 sp->nextline += STRLEN(sp->nextline);
1791 }
1792 else
1793 {
1794 line = vim_strnsave(sp->nextline, p - sp->nextline);
1795 sp->nextline = p + 1;
1796 }
1797 return line;
1798}
1799
1800/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001801 * Execute a function by "name".
1802 * This can be a builtin function, user function or a funcref.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001803 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001804 */
1805 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001806call_eval_func(
1807 char_u *name,
1808 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001809 ectx_T *ectx,
1810 isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001811{
Bram Moolenaared677f52020-08-12 16:38:10 +02001812 int called_emsg_before = called_emsg;
1813 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001814
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001815 res = call_by_name(name, argcount, ectx, iptr, NULL);
Bram Moolenaared677f52020-08-12 16:38:10 +02001816 if (res == FAIL && called_emsg == called_emsg_before)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001817 {
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001818 dictitem_T *v;
1819
1820 v = find_var(name, NULL, FALSE);
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001821 if (v == NULL || (v->di_tv.v_type != VAR_PARTIAL
1822 && v->di_tv.v_type != VAR_FUNC))
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001823 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001824 emsg_funcname(e_unknown_function_str, name);
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001825 return FAIL;
1826 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001827 return call_partial(&v->di_tv, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001828 }
Bram Moolenaared677f52020-08-12 16:38:10 +02001829 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001830}
1831
1832/*
Bram Moolenaarf112f302020-12-20 17:47:52 +01001833 * When a function reference is used, fill a partial with the information
1834 * needed, especially when it is used as a closure.
1835 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001836 int
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001837fill_partial_and_closure(
Bram Moolenaarcc341812022-09-19 15:54:34 +01001838 partial_T *pt,
1839 ufunc_T *ufunc,
1840 loopvarinfo_T *lvi,
1841 ectx_T *ectx)
Bram Moolenaarf112f302020-12-20 17:47:52 +01001842{
1843 pt->pt_func = ufunc;
1844 pt->pt_refcount = 1;
1845
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001846 if (ufunc->uf_flags & FC_CLOSURE)
Bram Moolenaarf112f302020-12-20 17:47:52 +01001847 {
1848 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1849 + ectx->ec_dfunc_idx;
1850
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001851 // The closure may need to find arguments and local variables of the
1852 // current function in the stack.
Bram Moolenaar0186e582021-01-10 18:33:11 +01001853 pt->pt_outer.out_stack = &ectx->ec_stack;
1854 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001855 if (ectx->ec_outer_ref != NULL)
1856 {
1857 // The current context already has a context, link to that one.
1858 pt->pt_outer.out_up = ectx->ec_outer_ref->or_outer;
1859 if (ectx->ec_outer_ref->or_partial != NULL)
1860 {
1861 pt->pt_outer.out_up_partial = ectx->ec_outer_ref->or_partial;
1862 ++pt->pt_outer.out_up_partial->pt_refcount;
1863 }
1864 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01001865
Bram Moolenaarcc341812022-09-19 15:54:34 +01001866 if (lvi != NULL)
1867 {
1868 int depth;
1869
1870 // The closure may need to find variables defined inside a loop,
1871 // for every nested loop. A new reference is made every time,
1872 // ISN_ENDLOOP will check if they are actually used.
1873 for (depth = 0; depth < lvi->lvi_depth; ++depth)
1874 {
1875 pt->pt_outer.out_loop[depth].stack = &ectx->ec_stack;
1876 pt->pt_outer.out_loop[depth].var_idx = ectx->ec_frame_idx
1877 + STACK_FRAME_SIZE + lvi->lvi_loop[depth].var_idx;
1878 pt->pt_outer.out_loop[depth].var_count =
1879 lvi->lvi_loop[depth].var_count;
1880 }
Bram Moolenaar6d313be2022-09-22 16:36:25 +01001881 pt->pt_outer.out_loop_size = lvi->lvi_depth;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001882 }
Bram Moolenaar6d313be2022-09-22 16:36:25 +01001883 else
1884 pt->pt_outer.out_loop_size = 0;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001885
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001886 // If the function currently executing returns and the closure is still
1887 // being referenced, we need to make a copy of the context (arguments
1888 // and local variables) so that the closure can use it later.
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001889 // Store a reference to the partial so we can handle that.
Bram Moolenaar35578162021-08-02 19:10:38 +02001890 if (GA_GROW_FAILS(&ectx->ec_funcrefs, 1))
Bram Moolenaarf112f302020-12-20 17:47:52 +01001891 {
1892 vim_free(pt);
1893 return FAIL;
1894 }
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001895 // Extra variable keeps the count of closures created in the current
1896 // function call.
Bram Moolenaarf112f302020-12-20 17:47:52 +01001897 ++(((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx
1898 + STACK_FRAME_SIZE + dfunc->df_varcount)->vval.v_number;
1899
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001900 ((partial_T **)ectx->ec_funcrefs.ga_data)[ectx->ec_funcrefs.ga_len]
1901 = pt;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001902 ++pt->pt_refcount;
1903 ++ectx->ec_funcrefs.ga_len;
1904 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001905 ++ufunc->uf_refcount;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001906 return OK;
1907}
1908
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001909/*
1910 * Execute iptr->isn_arg.string as an Ex command.
1911 */
1912 static int
1913exec_command(isn_T *iptr)
1914{
1915 source_cookie_T cookie;
1916
1917 SOURCING_LNUM = iptr->isn_lnum;
1918 // Pass getsourceline to get an error for a missing ":end"
1919 // command.
1920 CLEAR_FIELD(cookie);
1921 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1922 if (do_cmdline(iptr->isn_arg.string,
1923 getsourceline, &cookie,
1924 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED) == FAIL
1925 || did_emsg)
1926 return FAIL;
1927 return OK;
1928}
1929
Bram Moolenaar89445512022-04-14 12:58:23 +01001930/*
1931 * If script "sid" is not loaded yet then load it now.
1932 * Caller must make sure "sid" is a valid script ID.
1933 * "loaded" is set to TRUE if the script had to be loaded.
1934 * Returns FAIL if loading fails, OK if already loaded or loaded now.
1935 */
1936 int
1937may_load_script(int sid, int *loaded)
1938{
1939 scriptitem_T *si = SCRIPT_ITEM(sid);
1940
1941 if (si->sn_state == SN_STATE_NOT_LOADED)
1942 {
1943 *loaded = TRUE;
1944 if (do_source(si->sn_name, FALSE, DOSO_NONE, NULL) == FAIL)
1945 {
1946 semsg(_(e_cant_open_file_str), si->sn_name);
1947 return FAIL;
1948 }
1949 }
1950 return OK;
1951}
1952
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001953// used for v_instr of typval of VAR_INSTR
1954struct instr_S {
1955 ectx_T *instr_ectx;
1956 isn_T *instr_instr;
1957};
1958
Bram Moolenaar4c137212021-04-19 16:48:48 +02001959// used for substitute_instr
1960typedef struct subs_expr_S {
1961 ectx_T *subs_ectx;
1962 isn_T *subs_instr;
1963 int subs_status;
1964} subs_expr_T;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001965
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001966// Set when calling do_debug().
1967static ectx_T *debug_context = NULL;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001968static int debug_var_count;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001969
1970/*
1971 * When debugging lookup "name" and return the typeval.
1972 * When not found return NULL.
1973 */
1974 typval_T *
1975lookup_debug_var(char_u *name)
1976{
1977 int idx;
1978 dfunc_T *dfunc;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001979 ufunc_T *ufunc;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001980 ectx_T *ectx = debug_context;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001981 int varargs_off;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001982
1983 if (ectx == NULL)
1984 return NULL;
1985 dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
1986
1987 // Go through the local variable names, from last to first.
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001988 for (idx = debug_var_count - 1; idx >= 0; --idx)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001989 {
Bram Moolenaare406ff82022-03-10 20:47:43 +00001990 char_u *varname = ((char_u **)dfunc->df_var_names.ga_data)[idx];
1991
1992 // the variable name may be NULL when not available in this block
1993 if (varname != NULL && STRCMP(varname, name) == 0)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001994 return STACK_TV_VAR(idx);
1995 }
1996
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001997 // Go through argument names.
1998 ufunc = dfunc->df_ufunc;
1999 varargs_off = ufunc->uf_va_name == NULL ? 0 : 1;
2000 for (idx = 0; idx < ufunc->uf_args.ga_len; ++idx)
2001 if (STRCMP(((char_u **)(ufunc->uf_args.ga_data))[idx], name) == 0)
2002 return STACK_TV(ectx->ec_frame_idx - ufunc->uf_args.ga_len
2003 - varargs_off + idx);
2004 if (ufunc->uf_va_name != NULL && STRCMP(ufunc->uf_va_name, name) == 0)
2005 return STACK_TV(ectx->ec_frame_idx - 1);
2006
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02002007 return NULL;
2008}
2009
Bram Moolenaar26a44842021-09-02 18:49:06 +02002010/*
2011 * Return TRUE if there might be a breakpoint in "ufunc", which is when a
2012 * breakpoint was set in that function or when there is any expression.
2013 */
2014 int
2015may_break_in_function(ufunc_T *ufunc)
2016{
2017 return ufunc->uf_has_breakpoint || debug_has_expr_breakpoint();
2018}
2019
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002020 static void
2021handle_debug(isn_T *iptr, ectx_T *ectx)
2022{
2023 char_u *line;
2024 ufunc_T *ufunc = (((dfunc_T *)def_functions.ga_data)
2025 + ectx->ec_dfunc_idx)->df_ufunc;
2026 isn_T *ni;
2027 int end_lnum = iptr->isn_lnum;
2028 garray_T ga;
2029 int lnum;
2030
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02002031 if (ex_nesting_level > debug_break_level)
2032 {
2033 linenr_T breakpoint;
2034
Bram Moolenaar26a44842021-09-02 18:49:06 +02002035 if (!may_break_in_function(ufunc))
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02002036 return;
2037
2038 // check for the next breakpoint if needed
2039 breakpoint = dbg_find_breakpoint(FALSE, ufunc->uf_name,
Bram Moolenaar8cec9272021-06-23 20:20:53 +02002040 iptr->isn_arg.debug.dbg_break_lnum);
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02002041 if (breakpoint <= 0 || breakpoint > iptr->isn_lnum)
2042 return;
2043 }
2044
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002045 SOURCING_LNUM = iptr->isn_lnum;
2046 debug_context = ectx;
Bram Moolenaar8cec9272021-06-23 20:20:53 +02002047 debug_var_count = iptr->isn_arg.debug.dbg_var_names_len;
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002048
2049 for (ni = iptr + 1; ni->isn_type != ISN_FINISH; ++ni)
2050 if (ni->isn_type == ISN_DEBUG
2051 || ni->isn_type == ISN_RETURN
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002052 || ni->isn_type == ISN_RETURN_OBJECT
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002053 || ni->isn_type == ISN_RETURN_VOID)
2054 {
Bram Moolenaar112bed02021-11-23 22:16:34 +00002055 end_lnum = ni->isn_lnum + (ni->isn_type == ISN_DEBUG ? 0 : 1);
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002056 break;
2057 }
2058
2059 if (end_lnum > iptr->isn_lnum)
2060 {
2061 ga_init2(&ga, sizeof(char_u *), 10);
Bram Moolenaar310091d2021-12-23 21:14:37 +00002062 for (lnum = iptr->isn_lnum; lnum < end_lnum
2063 && lnum <= ufunc->uf_lines.ga_len; ++lnum)
Bram Moolenaar59b50c32021-06-17 22:27:48 +02002064 {
Bram Moolenaar303215d2021-07-07 20:10:43 +02002065 char_u *p = ((char_u **)ufunc->uf_lines.ga_data)[lnum - 1];
Bram Moolenaar59b50c32021-06-17 22:27:48 +02002066
Bram Moolenaar303215d2021-07-07 20:10:43 +02002067 if (p == NULL)
2068 continue; // left over from continuation line
2069 p = skipwhite(p);
Bram Moolenaar59b50c32021-06-17 22:27:48 +02002070 if (*p == '#')
2071 break;
Bram Moolenaar35578162021-08-02 19:10:38 +02002072 if (GA_GROW_OK(&ga, 1))
Bram Moolenaar59b50c32021-06-17 22:27:48 +02002073 ((char_u **)(ga.ga_data))[ga.ga_len++] = p;
2074 if (STRNCMP(p, "def ", 4) == 0)
2075 break;
2076 }
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002077 line = ga_concat_strings(&ga, " ");
2078 vim_free(ga.ga_data);
2079 }
2080 else
2081 line = ((char_u **)ufunc->uf_lines.ga_data)[iptr->isn_lnum - 1];
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002082
Dominique Pelle6e969552021-06-17 13:53:41 +02002083 do_debug(line == NULL ? (char_u *)"[empty]" : line);
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002084 debug_context = NULL;
2085
2086 if (end_lnum > iptr->isn_lnum)
2087 vim_free(line);
2088}
2089
Bram Moolenaar4c137212021-04-19 16:48:48 +02002090/*
Bram Moolenaar65b0d162022-12-13 18:43:22 +00002091 * Store a value in a list, dict, blob or object variable.
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002092 * Returns OK, FAIL or NOTDONE (uncatchable error).
2093 */
2094 static int
2095execute_storeindex(isn_T *iptr, ectx_T *ectx)
2096{
2097 vartype_T dest_type = iptr->isn_arg.vartype;
2098 typval_T *tv;
2099 typval_T *tv_idx = STACK_TV_BOT(-2);
2100 typval_T *tv_dest = STACK_TV_BOT(-1);
2101 int status = OK;
2102
2103 // Stack contains:
2104 // -3 value to be stored
2105 // -2 index
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002106 // -1 dict, list, blob or object
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002107 tv = STACK_TV_BOT(-3);
2108 SOURCING_LNUM = iptr->isn_lnum;
2109 if (dest_type == VAR_ANY)
2110 {
2111 dest_type = tv_dest->v_type;
2112 if (dest_type == VAR_DICT)
2113 status = do_2string(tv_idx, TRUE, FALSE);
2114 else if (dest_type == VAR_LIST && tv_idx->v_type != VAR_NUMBER)
2115 {
2116 emsg(_(e_number_expected));
2117 status = FAIL;
2118 }
2119 }
Bram Moolenaare08be092022-02-17 13:08:26 +00002120
2121 if (status == OK)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002122 {
Bram Moolenaare08be092022-02-17 13:08:26 +00002123 if (dest_type == VAR_LIST)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002124 {
Bram Moolenaare08be092022-02-17 13:08:26 +00002125 long lidx = (long)tv_idx->vval.v_number;
2126 list_T *list = tv_dest->vval.v_list;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002127
Bram Moolenaare08be092022-02-17 13:08:26 +00002128 if (list == NULL)
2129 {
2130 emsg(_(e_list_not_set));
2131 return FAIL;
2132 }
2133 if (lidx < 0 && list->lv_len + lidx >= 0)
2134 // negative index is relative to the end
2135 lidx = list->lv_len + lidx;
2136 if (lidx < 0 || lidx > list->lv_len)
2137 {
2138 semsg(_(e_list_index_out_of_range_nr), lidx);
2139 return FAIL;
2140 }
2141 if (lidx < list->lv_len)
2142 {
2143 listitem_T *li = list_find(list, lidx);
2144
2145 if (error_if_locked(li->li_tv.v_lock,
Bram Moolenaar70c43d82022-01-26 21:01:15 +00002146 e_cannot_change_locked_list_item))
Bram Moolenaare08be092022-02-17 13:08:26 +00002147 return FAIL;
2148 // overwrite existing list item
2149 clear_tv(&li->li_tv);
2150 li->li_tv = *tv;
2151 }
2152 else
2153 {
2154 if (error_if_locked(list->lv_lock, e_cannot_change_locked_list))
2155 return FAIL;
2156 // append to list, only fails when out of memory
2157 if (list_append_tv(list, tv) == FAIL)
2158 return NOTDONE;
2159 clear_tv(tv);
2160 }
2161 }
2162 else if (dest_type == VAR_DICT)
2163 {
2164 char_u *key = tv_idx->vval.v_string;
2165 dict_T *dict = tv_dest->vval.v_dict;
2166 dictitem_T *di;
2167
2168 SOURCING_LNUM = iptr->isn_lnum;
2169 if (dict == NULL)
2170 {
2171 emsg(_(e_dictionary_not_set));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002172 return FAIL;
Bram Moolenaare08be092022-02-17 13:08:26 +00002173 }
2174 if (key == NULL)
2175 key = (char_u *)"";
2176 di = dict_find(dict, key, -1);
2177 if (di != NULL)
2178 {
2179 if (error_if_locked(di->di_tv.v_lock,
2180 e_cannot_change_dict_item))
2181 return FAIL;
2182 // overwrite existing value
2183 clear_tv(&di->di_tv);
2184 di->di_tv = *tv;
2185 }
2186 else
2187 {
2188 if (error_if_locked(dict->dv_lock, e_cannot_change_dict))
2189 return FAIL;
2190 // add to dict, only fails when out of memory
2191 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
2192 return NOTDONE;
2193 clear_tv(tv);
2194 }
2195 }
2196 else if (dest_type == VAR_BLOB)
2197 {
2198 long lidx = (long)tv_idx->vval.v_number;
2199 blob_T *blob = tv_dest->vval.v_blob;
Bram Moolenaar65b0d162022-12-13 18:43:22 +00002200 varnumber_T nr;
2201 int error = FALSE;
2202 int len;
Bram Moolenaare08be092022-02-17 13:08:26 +00002203
2204 if (blob == NULL)
2205 {
2206 emsg(_(e_blob_not_set));
2207 return FAIL;
2208 }
2209 len = blob_len(blob);
2210 if (lidx < 0 && len + lidx >= 0)
2211 // negative index is relative to the end
2212 lidx = len + lidx;
2213
2214 // Can add one byte at the end.
2215 if (lidx < 0 || lidx > len)
2216 {
2217 semsg(_(e_blob_index_out_of_range_nr), lidx);
2218 return FAIL;
2219 }
2220 if (value_check_lock(blob->bv_lock, (char_u *)"blob", FALSE))
2221 return FAIL;
2222 nr = tv_get_number_chk(tv, &error);
2223 if (error)
2224 return FAIL;
2225 blob_set_append(blob, lidx, nr);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002226 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002227 else if (dest_type == VAR_CLASS || dest_type == VAR_OBJECT)
2228 {
2229 long idx = (long)tv_idx->vval.v_number;
2230 object_T *obj = tv_dest->vval.v_object;
2231 typval_T *otv = (typval_T *)(obj + 1);
Bram Moolenaar65b0d162022-12-13 18:43:22 +00002232 clear_tv(&otv[idx]);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002233 otv[idx] = *tv;
2234 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002235 else
2236 {
Bram Moolenaare08be092022-02-17 13:08:26 +00002237 status = FAIL;
2238 semsg(_(e_cannot_index_str), vartype_name(dest_type));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002239 }
2240 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002241
2242 clear_tv(tv_idx);
2243 clear_tv(tv_dest);
2244 ectx->ec_stack.ga_len -= 3;
2245 if (status == FAIL)
2246 {
2247 clear_tv(tv);
2248 return FAIL;
2249 }
2250 return OK;
2251}
2252
2253/*
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002254 * Store a value in a list or blob range.
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002255 */
2256 static int
2257execute_storerange(isn_T *iptr, ectx_T *ectx)
2258{
2259 typval_T *tv;
2260 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2261 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2262 typval_T *tv_dest = STACK_TV_BOT(-1);
2263 int status = OK;
2264
2265 // Stack contains:
2266 // -4 value to be stored
2267 // -3 first index or "none"
2268 // -2 second index or "none"
2269 // -1 destination list or blob
2270 tv = STACK_TV_BOT(-4);
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002271 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002272 if (tv_dest->v_type == VAR_LIST)
2273 {
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002274 long n1;
2275 long n2;
2276 listitem_T *li1;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002277
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002278 n1 = (long)tv_get_number_chk(tv_idx1, NULL);
2279 if (tv_idx2->v_type == VAR_SPECIAL
2280 && tv_idx2->vval.v_number == VVAL_NONE)
2281 n2 = list_len(tv_dest->vval.v_list) - 1;
2282 else
2283 n2 = (long)tv_get_number_chk(tv_idx2, NULL);
2284
Bram Moolenaar22ebd172022-04-01 15:26:58 +01002285 li1 = check_range_index_one(tv_dest->vval.v_list, &n1, TRUE, FALSE);
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002286 if (li1 == NULL)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002287 status = FAIL;
2288 else
2289 {
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002290 status = check_range_index_two(tv_dest->vval.v_list,
2291 &n1, li1, &n2, FALSE);
2292 if (status != FAIL)
2293 status = list_assign_range(
2294 tv_dest->vval.v_list,
2295 tv->vval.v_list,
2296 n1,
2297 n2,
2298 tv_idx2->v_type == VAR_SPECIAL,
2299 (char_u *)"=",
2300 (char_u *)"[unknown]");
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002301 }
2302 }
2303 else if (tv_dest->v_type == VAR_BLOB)
2304 {
2305 varnumber_T n1;
2306 varnumber_T n2;
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002307 long bloblen;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002308
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002309 n1 = tv_get_number_chk(tv_idx1, NULL);
2310 if (tv_idx2->v_type == VAR_SPECIAL
2311 && tv_idx2->vval.v_number == VVAL_NONE)
2312 n2 = blob_len(tv_dest->vval.v_blob) - 1;
2313 else
2314 n2 = tv_get_number_chk(tv_idx2, NULL);
2315 bloblen = blob_len(tv_dest->vval.v_blob);
2316
2317 if (check_blob_index(bloblen, n1, FALSE) == FAIL
2318 || check_blob_range(bloblen, n1, n2, FALSE) == FAIL)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002319 status = FAIL;
2320 else
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002321 status = blob_set_range(tv_dest->vval.v_blob, n1, n2, tv);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002322 }
2323 else
2324 {
2325 status = FAIL;
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002326 emsg(_(e_list_or_blob_required));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002327 }
2328
2329 clear_tv(tv_idx1);
2330 clear_tv(tv_idx2);
2331 clear_tv(tv_dest);
2332 ectx->ec_stack.ga_len -= 4;
2333 clear_tv(tv);
2334
2335 return status;
2336}
2337
2338/*
2339 * Unlet item in list or dict variable.
2340 */
2341 static int
2342execute_unletindex(isn_T *iptr, ectx_T *ectx)
2343{
2344 typval_T *tv_idx = STACK_TV_BOT(-2);
2345 typval_T *tv_dest = STACK_TV_BOT(-1);
2346 int status = OK;
2347
2348 // Stack contains:
2349 // -2 index
2350 // -1 dict or list
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002351 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002352 if (tv_dest->v_type == VAR_DICT)
2353 {
2354 // unlet a dict item, index must be a string
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002355 if (tv_idx->v_type != VAR_STRING && tv_idx->v_type != VAR_NUMBER)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002356 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002357 semsg(_(e_expected_str_but_got_str),
2358 vartype_name(VAR_STRING),
2359 vartype_name(tv_idx->v_type));
2360 status = FAIL;
2361 }
2362 else
2363 {
2364 dict_T *d = tv_dest->vval.v_dict;
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002365 char_u *key;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002366 dictitem_T *di = NULL;
2367
2368 if (d != NULL && value_check_lock(
2369 d->dv_lock, NULL, FALSE))
2370 status = FAIL;
2371 else
2372 {
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002373 if (tv_idx->v_type == VAR_STRING)
2374 {
2375 key = tv_idx->vval.v_string;
2376 if (key == NULL)
2377 key = (char_u *)"";
2378 }
2379 else
2380 {
2381 key = tv_get_string(tv_idx);
2382 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002383 if (d != NULL)
2384 di = dict_find(d, key, (int)STRLEN(key));
2385 if (di == NULL)
2386 {
2387 // NULL dict is equivalent to empty dict
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00002388 semsg(_(e_key_not_present_in_dictionary_str), key);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002389 status = FAIL;
2390 }
2391 else if (var_check_fixed(di->di_flags,
2392 NULL, FALSE)
2393 || var_check_ro(di->di_flags,
2394 NULL, FALSE))
2395 status = FAIL;
2396 else
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002397 dictitem_remove(d, di, "unlet");
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002398 }
2399 }
2400 }
2401 else if (tv_dest->v_type == VAR_LIST)
2402 {
2403 // unlet a List item, index must be a number
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002404 if (check_for_number(tv_idx) == FAIL)
2405 {
2406 status = FAIL;
2407 }
2408 else
2409 {
2410 list_T *l = tv_dest->vval.v_list;
2411 long n = (long)tv_idx->vval.v_number;
2412
2413 if (l != NULL && value_check_lock(
2414 l->lv_lock, NULL, FALSE))
2415 status = FAIL;
2416 else
2417 {
2418 listitem_T *li = list_find(l, n);
2419
2420 if (li == NULL)
2421 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002422 semsg(_(e_list_index_out_of_range_nr), n);
2423 status = FAIL;
2424 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002425 else
2426 listitem_remove(l, li);
2427 }
2428 }
2429 }
2430 else
2431 {
2432 status = FAIL;
2433 semsg(_(e_cannot_index_str),
2434 vartype_name(tv_dest->v_type));
2435 }
2436
2437 clear_tv(tv_idx);
2438 clear_tv(tv_dest);
2439 ectx->ec_stack.ga_len -= 2;
2440
2441 return status;
2442}
2443
2444/*
2445 * Unlet a range of items in a list variable.
2446 */
2447 static int
2448execute_unletrange(isn_T *iptr, ectx_T *ectx)
2449{
2450 // Stack contains:
2451 // -3 index1
2452 // -2 index2
2453 // -1 dict or list
2454 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2455 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2456 typval_T *tv_dest = STACK_TV_BOT(-1);
2457 int status = OK;
2458
2459 if (tv_dest->v_type == VAR_LIST)
2460 {
2461 // indexes must be a number
2462 SOURCING_LNUM = iptr->isn_lnum;
2463 if (check_for_number(tv_idx1) == FAIL
2464 || (tv_idx2->v_type != VAR_SPECIAL
2465 && check_for_number(tv_idx2) == FAIL))
2466 {
2467 status = FAIL;
2468 }
2469 else
2470 {
2471 list_T *l = tv_dest->vval.v_list;
2472 long n1 = (long)tv_idx1->vval.v_number;
2473 long n2 = tv_idx2->v_type == VAR_SPECIAL
2474 ? 0 : (long)tv_idx2->vval.v_number;
2475 listitem_T *li;
2476
2477 li = list_find_index(l, &n1);
2478 if (li == NULL)
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002479 {
2480 semsg(_(e_list_index_out_of_range_nr),
2481 (long)tv_idx1->vval.v_number);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002482 status = FAIL;
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002483 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002484 else
2485 {
2486 if (n1 < 0)
2487 n1 = list_idx_of_item(l, li);
2488 if (n2 < 0)
2489 {
2490 listitem_T *li2 = list_find(l, n2);
2491
2492 if (li2 == NULL)
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002493 {
2494 semsg(_(e_list_index_out_of_range_nr), n2);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002495 status = FAIL;
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002496 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002497 else
2498 n2 = list_idx_of_item(l, li2);
2499 }
2500 if (status != FAIL
2501 && tv_idx2->v_type != VAR_SPECIAL
2502 && n2 < n1)
2503 {
2504 semsg(_(e_list_index_out_of_range_nr), n2);
2505 status = FAIL;
2506 }
Bram Moolenaar6b8c7ba2022-03-20 17:46:06 +00002507 if (status != FAIL)
2508 list_unlet_range(l, li, n1,
2509 tv_idx2->v_type != VAR_SPECIAL, n2);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002510 }
2511 }
2512 }
2513 else
2514 {
2515 status = FAIL;
2516 SOURCING_LNUM = iptr->isn_lnum;
2517 semsg(_(e_cannot_index_str),
2518 vartype_name(tv_dest->v_type));
2519 }
2520
2521 clear_tv(tv_idx1);
2522 clear_tv(tv_idx2);
2523 clear_tv(tv_dest);
2524 ectx->ec_stack.ga_len -= 3;
2525
2526 return status;
2527}
2528
2529/*
2530 * Top of a for loop.
2531 */
2532 static int
2533execute_for(isn_T *iptr, ectx_T *ectx)
2534{
2535 typval_T *tv;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002536 int jump = FALSE;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002537 typval_T *ltv = STACK_TV_BOT(-1);
2538 typval_T *idxtv =
Bram Moolenaarcc341812022-09-19 15:54:34 +01002539 STACK_TV_VAR(iptr->isn_arg.forloop.for_loop_idx);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002540
2541 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
2542 return FAIL;
2543 if (ltv->v_type == VAR_LIST)
2544 {
2545 list_T *list = ltv->vval.v_list;
2546
2547 // push the next item from the list
2548 ++idxtv->vval.v_number;
2549 if (list == NULL
2550 || idxtv->vval.v_number >= list->lv_len)
2551 {
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002552 jump = TRUE;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002553 }
2554 else if (list->lv_first == &range_list_item)
2555 {
2556 // non-materialized range() list
2557 tv = STACK_TV_BOT(0);
2558 tv->v_type = VAR_NUMBER;
2559 tv->v_lock = 0;
2560 tv->vval.v_number = list_find_nr(
2561 list, idxtv->vval.v_number, NULL);
2562 ++ectx->ec_stack.ga_len;
2563 }
2564 else
2565 {
2566 listitem_T *li = list_find(list,
2567 idxtv->vval.v_number);
2568
2569 copy_tv(&li->li_tv, STACK_TV_BOT(0));
2570 ++ectx->ec_stack.ga_len;
2571 }
2572 }
2573 else if (ltv->v_type == VAR_STRING)
2574 {
2575 char_u *str = ltv->vval.v_string;
2576
2577 // The index is for the last byte of the previous
2578 // character.
2579 ++idxtv->vval.v_number;
2580 if (str == NULL || str[idxtv->vval.v_number] == NUL)
2581 {
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002582 jump = TRUE;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002583 }
2584 else
2585 {
2586 int clen = mb_ptr2len(str + idxtv->vval.v_number);
2587
2588 // Push the next character from the string.
2589 tv = STACK_TV_BOT(0);
2590 tv->v_type = VAR_STRING;
2591 tv->vval.v_string = vim_strnsave(
2592 str + idxtv->vval.v_number, clen);
2593 ++ectx->ec_stack.ga_len;
2594 idxtv->vval.v_number += clen - 1;
2595 }
2596 }
2597 else if (ltv->v_type == VAR_BLOB)
2598 {
2599 blob_T *blob = ltv->vval.v_blob;
2600
2601 // When we get here the first time make a copy of the
2602 // blob, so that the iteration still works when it is
2603 // changed.
2604 if (idxtv->vval.v_number == -1 && blob != NULL)
2605 {
2606 blob_copy(blob, ltv);
2607 blob_unref(blob);
2608 blob = ltv->vval.v_blob;
2609 }
2610
2611 // The index is for the previous byte.
2612 ++idxtv->vval.v_number;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002613 if (blob == NULL || idxtv->vval.v_number >= blob_len(blob))
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002614 {
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002615 jump = TRUE;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002616 }
2617 else
2618 {
2619 // Push the next byte from the blob.
2620 tv = STACK_TV_BOT(0);
2621 tv->v_type = VAR_NUMBER;
2622 tv->vval.v_number = blob_get(blob,
2623 idxtv->vval.v_number);
2624 ++ectx->ec_stack.ga_len;
2625 }
2626 }
2627 else
2628 {
2629 semsg(_(e_for_loop_on_str_not_supported),
2630 vartype_name(ltv->v_type));
2631 return FAIL;
2632 }
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002633
2634 if (jump)
2635 {
2636 // past the end of the list/string/blob, jump to "endfor"
2637 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
2638 may_restore_cmdmod(&ectx->ec_funclocal);
2639 }
2640 else
2641 {
2642 // Store the current number of funcrefs, this may be used in
2643 // ISN_LOOPEND. The variable index is always one more than the loop
2644 // variable index.
Bram Moolenaarcc341812022-09-19 15:54:34 +01002645 tv = STACK_TV_VAR(iptr->isn_arg.forloop.for_loop_idx + 1);
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002646 tv->vval.v_number = ectx->ec_funcrefs.ga_len;
2647 }
2648
2649 return OK;
2650}
2651
2652/*
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002653 * Code for handling variables declared inside a loop and used in a closure.
2654 * This is very similar to what is done with funcstack_T. The difference is
2655 * that the funcstack_T has the scope of a function, while a loopvars_T has the
2656 * scope of the block inside a loop and each loop may have its own.
2657 */
2658
2659// Double linked list of loopvars_T in use.
2660static loopvars_T *first_loopvars = NULL;
2661
2662 static void
2663add_loopvars_to_list(loopvars_T *loopvars)
2664{
2665 // Link in list of loopvarss.
2666 if (first_loopvars != NULL)
2667 first_loopvars->lvs_prev = loopvars;
2668 loopvars->lvs_next = first_loopvars;
2669 loopvars->lvs_prev = NULL;
2670 first_loopvars = loopvars;
2671}
2672
2673 static void
2674remove_loopvars_from_list(loopvars_T *loopvars)
2675{
2676 if (loopvars->lvs_prev == NULL)
2677 first_loopvars = loopvars->lvs_next;
2678 else
2679 loopvars->lvs_prev->lvs_next = loopvars->lvs_next;
2680 if (loopvars->lvs_next != NULL)
2681 loopvars->lvs_next->lvs_prev = loopvars->lvs_prev;
2682}
2683
2684/*
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002685 * End of a for or while loop: Handle any variables used by a closure.
2686 */
2687 static int
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002688execute_endloop(isn_T *iptr, ectx_T *ectx)
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002689{
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002690 endloop_T *endloop = &iptr->isn_arg.endloop;
2691 typval_T *tv_refcount = STACK_TV_VAR(endloop->end_funcref_idx);
2692 int prev_closure_count = tv_refcount->vval.v_number;
Bram Moolenaarcc341812022-09-19 15:54:34 +01002693 int depth = endloop->end_depth;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002694 garray_T *gap = &ectx->ec_funcrefs;
2695 int closure_in_use = FALSE;
2696 loopvars_T *loopvars;
2697 typval_T *stack;
2698 int idx;
2699
Bram Moolenaarcc341812022-09-19 15:54:34 +01002700 // Check if any created closure is still being referenced and loopvars have
2701 // not been saved yet for the current depth.
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002702 for (idx = prev_closure_count; idx < gap->ga_len; ++idx)
2703 {
2704 partial_T *pt = ((partial_T **)gap->ga_data)[idx];
2705
Bram Moolenaarcc341812022-09-19 15:54:34 +01002706 if (pt->pt_refcount > 1 && pt->pt_loopvars[depth] == NULL)
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002707 {
2708 int refcount = pt->pt_refcount;
2709 int i;
2710
2711 // A Reference in a variable inside the loop doesn't count, it gets
2712 // unreferenced at the end of the loop.
2713 for (i = 0; i < endloop->end_var_count; ++i)
2714 {
2715 typval_T *stv = STACK_TV_VAR(endloop->end_var_idx + i);
2716
2717 if (stv->v_type == VAR_PARTIAL && pt == stv->vval.v_partial)
2718 --refcount;
2719 }
2720 if (refcount > 1)
2721 {
2722 closure_in_use = TRUE;
2723 break;
2724 }
2725 }
2726 }
2727
2728 // If no function reference were created since the start of the loop block
2729 // or it is no longer referenced there is nothing to do.
2730 if (!closure_in_use)
2731 return OK;
2732
2733 // A closure is using variables declared inside the loop.
2734 // Move them to the called function.
2735 loopvars = ALLOC_CLEAR_ONE(loopvars_T);
2736 if (loopvars == NULL)
2737 return FAIL;
2738
2739 loopvars->lvs_ga.ga_len = endloop->end_var_count;
2740 stack = ALLOC_CLEAR_MULT(typval_T, loopvars->lvs_ga.ga_len);
2741 loopvars->lvs_ga.ga_data = stack;
2742 if (stack == NULL)
2743 {
2744 vim_free(loopvars);
2745 return FAIL;
2746 }
2747 add_loopvars_to_list(loopvars);
2748
2749 // Move the variable values.
2750 for (idx = 0; idx < endloop->end_var_count; ++idx)
2751 {
2752 typval_T *tv = STACK_TV_VAR(endloop->end_var_idx + idx);
2753
2754 *(stack + idx) = *tv;
2755 tv->v_type = VAR_UNKNOWN;
2756 }
2757
2758 for (idx = prev_closure_count; idx < gap->ga_len; ++idx)
2759 {
2760 partial_T *pt = ((partial_T **)gap->ga_data)[idx];
2761
Bram Moolenaarcc341812022-09-19 15:54:34 +01002762 if (pt->pt_refcount > 1 && pt->pt_loopvars[depth] == NULL)
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002763 {
2764 ++loopvars->lvs_refcount;
Bram Moolenaarcc341812022-09-19 15:54:34 +01002765 pt->pt_loopvars[depth] = loopvars;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002766
Bram Moolenaarcc341812022-09-19 15:54:34 +01002767 pt->pt_outer.out_loop[depth].stack = &loopvars->lvs_ga;
2768 pt->pt_outer.out_loop[depth].var_idx -=
2769 ectx->ec_frame_idx + STACK_FRAME_SIZE + endloop->end_var_idx;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002770 }
2771 }
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002772
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002773 return OK;
2774}
2775
2776/*
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002777 * Called when a partial is freed or its reference count goes down to one. The
2778 * loopvars may be the only reference to the partials in the local variables.
2779 * Go over all of them, the funcref and can be freed if all partials
2780 * referencing the loopvars have a reference count of one.
Bram Moolenaarcc341812022-09-19 15:54:34 +01002781 * Return TRUE if it was freed.
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002782 */
Bram Moolenaarcc341812022-09-19 15:54:34 +01002783 int
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002784loopvars_check_refcount(loopvars_T *loopvars)
2785{
2786 int i;
2787 garray_T *gap = &loopvars->lvs_ga;
2788 int done = 0;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002789 typval_T *stack = gap->ga_data;
2790
Bram Moolenaarcc341812022-09-19 15:54:34 +01002791 if (loopvars->lvs_refcount > loopvars->lvs_min_refcount)
2792 return FALSE;
2793 for (i = 0; i < gap->ga_len; ++i)
2794 {
2795 typval_T *tv = ((typval_T *)gap->ga_data) + i;
2796
2797 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL
2798 && tv->vval.v_partial->pt_refcount == 1)
2799 {
2800 int depth;
2801
2802 for (depth = 0; depth < MAX_LOOP_DEPTH; ++depth)
2803 if (tv->vval.v_partial->pt_loopvars[depth] == loopvars)
2804 ++done;
2805 }
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002806 }
Bram Moolenaarcc341812022-09-19 15:54:34 +01002807 if (done != loopvars->lvs_min_refcount)
2808 return FALSE;
2809
2810 // All partials referencing the loopvars have a reference count of
2811 // one, thus the loopvars is no longer of use.
2812 stack = gap->ga_data;
2813 for (i = 0; i < gap->ga_len; ++i)
2814 clear_tv(stack + i);
2815 vim_free(stack);
2816 remove_loopvars_from_list(loopvars);
2817 vim_free(loopvars);
2818 return TRUE;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002819}
2820
2821/*
2822 * For garbage collecting: set references in all variables referenced by
2823 * all loopvars.
2824 */
2825 int
2826set_ref_in_loopvars(int copyID)
2827{
2828 loopvars_T *loopvars;
2829
2830 for (loopvars = first_loopvars; loopvars != NULL;
2831 loopvars = loopvars->lvs_next)
2832 {
2833 typval_T *stack = loopvars->lvs_ga.ga_data;
2834 int i;
2835
2836 for (i = 0; i < loopvars->lvs_ga.ga_len; ++i)
2837 if (set_ref_in_item(stack + i, copyID, NULL, NULL))
2838 return TRUE; // abort
2839 }
2840 return FALSE;
2841}
2842
2843/*
Bram Moolenaar06b77222022-01-25 15:51:56 +00002844 * Load instruction for w:/b:/g:/t: variable.
2845 * "isn_type" is used instead of "iptr->isn_type".
2846 */
2847 static int
2848load_namespace_var(ectx_T *ectx, isntype_T isn_type, isn_T *iptr)
2849{
2850 dictitem_T *di = NULL;
2851 hashtab_T *ht = NULL;
2852 char namespace;
2853
2854 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
2855 return NOTDONE;
2856 switch (isn_type)
2857 {
2858 case ISN_LOADG:
2859 ht = get_globvar_ht();
2860 namespace = 'g';
2861 break;
2862 case ISN_LOADB:
2863 ht = &curbuf->b_vars->dv_hashtab;
2864 namespace = 'b';
2865 break;
2866 case ISN_LOADW:
2867 ht = &curwin->w_vars->dv_hashtab;
2868 namespace = 'w';
2869 break;
2870 case ISN_LOADT:
2871 ht = &curtab->tp_vars->dv_hashtab;
2872 namespace = 't';
2873 break;
2874 default: // Cannot reach here
2875 return NOTDONE;
2876 }
2877 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
2878
Bram Moolenaar06b77222022-01-25 15:51:56 +00002879 if (di == NULL)
2880 {
Bram Moolenaarfe732552022-02-22 19:39:13 +00002881 if (isn_type == ISN_LOADG)
2882 {
2883 ufunc_T *ufunc = find_func(iptr->isn_arg.string, TRUE);
2884
2885 // g:Something could be a function
2886 if (ufunc != NULL)
2887 {
2888 typval_T *tv = STACK_TV_BOT(0);
2889
2890 ++ectx->ec_stack.ga_len;
2891 tv->v_type = VAR_FUNC;
2892 tv->vval.v_string = alloc(STRLEN(iptr->isn_arg.string) + 3);
2893 if (tv->vval.v_string == NULL)
2894 return FAIL;
2895 STRCPY(tv->vval.v_string, "g:");
2896 STRCPY(tv->vval.v_string + 2, iptr->isn_arg.string);
2897 return OK;
2898 }
2899 }
Bram Moolenaar06b77222022-01-25 15:51:56 +00002900 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarfe732552022-02-22 19:39:13 +00002901 if (vim_strchr(iptr->isn_arg.string, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar06b77222022-01-25 15:51:56 +00002902 // no check if the item exists in the script but
2903 // isn't exported, it is too complicated
Bram Moolenaarfe732552022-02-22 19:39:13 +00002904 semsg(_(e_item_not_found_in_script_str), iptr->isn_arg.string);
Bram Moolenaar06b77222022-01-25 15:51:56 +00002905 else
2906 semsg(_(e_undefined_variable_char_str),
Bram Moolenaarfe732552022-02-22 19:39:13 +00002907 namespace, iptr->isn_arg.string);
Bram Moolenaar06b77222022-01-25 15:51:56 +00002908 return FAIL;
2909 }
2910 else
2911 {
2912 copy_tv(&di->di_tv, STACK_TV_BOT(0));
2913 ++ectx->ec_stack.ga_len;
2914 }
2915 return OK;
2916}
2917
2918/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02002919 * Execute instructions in execution context "ectx".
2920 * Return OK or FAIL;
2921 */
2922 static int
2923exec_instructions(ectx_T *ectx)
2924{
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002925 int ret = FAIL;
2926 int save_trylevel_at_start = ectx->ec_trylevel_at_start;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02002927 int dict_stack_len_at_start = dict_stack.ga_len;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01002928
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02002929 // Start execution at the first instruction.
Bram Moolenaar4c137212021-04-19 16:48:48 +02002930 ectx->ec_iidx = 0;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01002931
Bram Moolenaarff652882021-05-16 15:24:49 +02002932 // Only catch exceptions in this instruction list.
2933 ectx->ec_trylevel_at_start = trylevel;
2934
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002935 for (;;)
2936 {
Bram Moolenaara7490192021-07-22 12:26:14 +02002937 static int breakcheck_count = 0; // using "static" makes it faster
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002938 isn_T *iptr;
Bram Moolenaara7490192021-07-22 12:26:14 +02002939 typval_T *tv;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002940
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002941 if (unlikely(++breakcheck_count >= 100))
Bram Moolenaar270d0382020-05-15 21:42:53 +02002942 {
2943 line_breakcheck();
2944 breakcheck_count = 0;
2945 }
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002946 if (unlikely(got_int))
Bram Moolenaar20431c92020-03-20 18:39:46 +01002947 {
2948 // Turn CTRL-C into an exception.
2949 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +01002950 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002951 goto theend;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002952 did_throw = TRUE;
2953 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002954
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002955 if (unlikely(did_emsg && msg_list != NULL && *msg_list != NULL))
Bram Moolenaara26b9702020-04-18 19:53:28 +02002956 {
2957 // Turn an error message into an exception.
2958 did_emsg = FALSE;
2959 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002960 goto theend;
Bram Moolenaara26b9702020-04-18 19:53:28 +02002961 did_throw = TRUE;
2962 *msg_list = NULL;
2963 }
2964
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002965 if (unlikely(did_throw))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002966 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002967 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002968 trycmd_T *trycmd = NULL;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002969 int index = trystack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002970
2971 // An exception jumps to the first catch, finally, or returns from
2972 // the current function.
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002973 while (index > 0)
2974 {
2975 trycmd = ((trycmd_T *)trystack->ga_data) + index - 1;
Bram Moolenaar834193a2021-06-30 20:39:15 +02002976 if (!trycmd->tcd_in_catch || trycmd->tcd_finally_idx != 0)
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002977 break;
2978 // In the catch and finally block of this try we have to go up
2979 // one level.
2980 --index;
2981 trycmd = NULL;
2982 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02002983 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002984 {
Bram Moolenaar834193a2021-06-30 20:39:15 +02002985 if (trycmd->tcd_in_catch)
2986 {
2987 // exception inside ":catch", jump to ":finally" once
2988 ectx->ec_iidx = trycmd->tcd_finally_idx;
2989 trycmd->tcd_finally_idx = 0;
2990 }
2991 else
2992 // jump to first ":catch"
2993 ectx->ec_iidx = trycmd->tcd_catch_idx;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002994 trycmd->tcd_in_catch = TRUE;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002995 did_throw = FALSE; // don't come back here until :endtry
2996 trycmd->tcd_did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002997 }
2998 else
2999 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02003000 // Not inside try or need to return from current functions.
3001 // Push a dummy return value.
Bram Moolenaar35578162021-08-02 19:10:38 +02003002 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003003 goto theend;
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02003004 tv = STACK_TV_BOT(0);
3005 tv->v_type = VAR_NUMBER;
3006 tv->vval.v_number = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003007 ++ectx->ec_stack.ga_len;
3008 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003009 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02003010 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +01003011 need_rethrow = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003012 if (handle_closure_in_use(ectx, FALSE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003013 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003014 goto done;
3015 }
3016
Bram Moolenaar4c137212021-04-19 16:48:48 +02003017 if (func_return(ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003018 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003019 }
3020 continue;
3021 }
3022
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003023 /*
3024 * Big switch on the instruction. Most compilers will be turning this
3025 * into an efficient lookup table, since the "case" values are an enum
3026 * with sequential numbers. It may look ugly, but it should be the
3027 * most efficient way.
3028 */
Bram Moolenaar4c137212021-04-19 16:48:48 +02003029 iptr = &ectx->ec_instr[ectx->ec_iidx++];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003030 switch (iptr->isn_type)
3031 {
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00003032 // Constructor, first instruction in a new() method.
Bram Moolenaar00b28d62022-12-08 15:32:33 +00003033 case ISN_CONSTRUCT:
3034 // "this" is always the local variable at index zero
3035 tv = STACK_TV_VAR(0);
3036 tv->v_type = VAR_OBJECT;
3037 tv->vval.v_object = alloc_clear(
3038 iptr->isn_arg.construct.construct_size);
3039 tv->vval.v_object->obj_class =
3040 iptr->isn_arg.construct.construct_class;
Bram Moolenaard28d7b92022-12-08 20:42:00 +00003041 ++tv->vval.v_object->obj_class->class_refcount;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00003042 tv->vval.v_object->obj_refcount = 1;
Bram Moolenaard28d7b92022-12-08 20:42:00 +00003043 object_created(tv->vval.v_object);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00003044 break;
3045
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003046 // execute Ex command line
3047 case ISN_EXEC:
Bram Moolenaaraacc9662021-08-13 19:40:51 +02003048 if (exec_command(iptr) == FAIL)
3049 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003050 break;
3051
Bram Moolenaar20677332021-06-06 17:02:53 +02003052 // execute Ex command line split at NL characters.
3053 case ISN_EXEC_SPLIT:
3054 {
3055 source_cookie_T cookie;
Bram Moolenaar518df272021-06-06 17:34:13 +02003056 char_u *line;
Bram Moolenaar20677332021-06-06 17:02:53 +02003057
3058 SOURCING_LNUM = iptr->isn_lnum;
3059 CLEAR_FIELD(cookie);
3060 cookie.sourcing_lnum = iptr->isn_lnum - 1;
3061 cookie.nextline = iptr->isn_arg.string;
Bram Moolenaar518df272021-06-06 17:34:13 +02003062 line = get_split_sourceline(0, &cookie, 0, 0);
3063 if (do_cmdline(line,
Bram Moolenaar20677332021-06-06 17:02:53 +02003064 get_split_sourceline, &cookie,
3065 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED)
3066 == FAIL
3067 || did_emsg)
Bram Moolenaar518df272021-06-06 17:34:13 +02003068 {
3069 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02003070 goto on_error;
Bram Moolenaar518df272021-06-06 17:34:13 +02003071 }
3072 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02003073 }
3074 break;
3075
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00003076 // execute Ex command line that is only a range
3077 case ISN_EXECRANGE:
3078 {
3079 exarg_T ea;
3080 char *error = NULL;
3081
3082 CLEAR_FIELD(ea);
3083 ea.cmdidx = CMD_SIZE;
3084 ea.addr_type = ADDR_LINES;
3085 ea.cmd = iptr->isn_arg.string;
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00003086 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00003087 parse_cmd_address(&ea, &error, FALSE);
Bram Moolenaar01a4dcb2021-12-04 13:15:10 +00003088 if (ea.cmd == NULL)
3089 goto on_error;
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00003090 // error is always NULL when using ADDR_LINES
3091 error = ex_range_without_command(&ea);
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00003092 if (error != NULL)
3093 {
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00003094 emsg(error);
3095 goto on_error;
3096 }
3097 }
3098 break;
3099
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02003100 // Evaluate an expression with legacy syntax, push it onto the
3101 // stack.
3102 case ISN_LEGACY_EVAL:
3103 {
3104 char_u *arg = iptr->isn_arg.string;
3105 int res;
3106 int save_flags = cmdmod.cmod_flags;
3107
Bram Moolenaar35578162021-08-02 19:10:38 +02003108 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003109 goto theend;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02003110 tv = STACK_TV_BOT(0);
3111 init_tv(tv);
3112 cmdmod.cmod_flags |= CMOD_LEGACY;
3113 res = eval0(arg, tv, NULL, &EVALARG_EVALUATE);
3114 cmdmod.cmod_flags = save_flags;
3115 if (res == FAIL)
3116 goto on_error;
3117 ++ectx->ec_stack.ga_len;
3118 }
3119 break;
3120
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003121 // push typeval VAR_INSTR with instructions to be executed
3122 case ISN_INSTR:
3123 {
Bram Moolenaar35578162021-08-02 19:10:38 +02003124 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003125 goto theend;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003126 tv = STACK_TV_BOT(0);
3127 tv->vval.v_instr = ALLOC_ONE(instr_T);
3128 if (tv->vval.v_instr == NULL)
3129 goto on_error;
3130 ++ectx->ec_stack.ga_len;
3131
3132 tv->v_type = VAR_INSTR;
3133 tv->vval.v_instr->instr_ectx = ectx;
3134 tv->vval.v_instr->instr_instr = iptr->isn_arg.instr;
3135 }
3136 break;
3137
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003138 case ISN_SOURCE:
3139 {
Bram Moolenaar89445512022-04-14 12:58:23 +01003140 int notused;
LemonBoy77142312022-04-15 20:50:46 +01003141
Bram Moolenaar89445512022-04-14 12:58:23 +01003142 SOURCING_LNUM = iptr->isn_lnum;
3143 if (may_load_script((int)iptr->isn_arg.number, &notused)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003144 == FAIL)
Bram Moolenaar89445512022-04-14 12:58:23 +01003145 goto on_error;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003146 }
3147 break;
3148
Bram Moolenaar4c137212021-04-19 16:48:48 +02003149 // execute :substitute with an expression
3150 case ISN_SUBSTITUTE:
3151 {
3152 subs_T *subs = &iptr->isn_arg.subs;
3153 source_cookie_T cookie;
3154 struct subs_expr_S *save_instr = substitute_instr;
3155 struct subs_expr_S subs_instr;
3156 int res;
3157
3158 subs_instr.subs_ectx = ectx;
3159 subs_instr.subs_instr = subs->subs_instr;
3160 subs_instr.subs_status = OK;
3161 substitute_instr = &subs_instr;
3162
3163 SOURCING_LNUM = iptr->isn_lnum;
3164 // This is very much like ISN_EXEC
3165 CLEAR_FIELD(cookie);
3166 cookie.sourcing_lnum = iptr->isn_lnum - 1;
3167 res = do_cmdline(subs->subs_cmd,
3168 getsourceline, &cookie,
3169 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
3170 substitute_instr = save_instr;
3171
3172 if (res == FAIL || did_emsg
3173 || subs_instr.subs_status == FAIL)
3174 goto on_error;
3175 }
3176 break;
3177
3178 case ISN_FINISH:
3179 goto done;
3180
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02003181 case ISN_REDIRSTART:
3182 // create a dummy entry for var_redir_str()
3183 if (alloc_redir_lval() == FAIL)
3184 goto on_error;
3185
3186 // The output is stored in growarray "redir_ga" until
3187 // redirection ends.
3188 init_redir_ga();
3189 redir_vname = 1;
3190 break;
3191
3192 case ISN_REDIREND:
3193 {
3194 char_u *res = get_clear_redir_ga();
3195
3196 // End redirection, put redirected text on the stack.
3197 clear_redir_lval();
3198 redir_vname = 0;
3199
Bram Moolenaar35578162021-08-02 19:10:38 +02003200 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02003201 {
3202 vim_free(res);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003203 goto theend;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02003204 }
3205 tv = STACK_TV_BOT(0);
3206 tv->v_type = VAR_STRING;
3207 tv->vval.v_string = res;
3208 ++ectx->ec_stack.ga_len;
3209 }
3210 break;
3211
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003212 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02003213#ifdef FEAT_QUICKFIX
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003214 force_abort = TRUE;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003215 if (trigger_cexpr_autocmd(iptr->isn_arg.number) == FAIL)
3216 goto on_error;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003217 force_abort = FALSE;
Bram Moolenaarb7c97812021-05-05 22:51:39 +02003218#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003219 break;
3220
3221 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02003222#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003223 {
3224 exarg_T ea;
3225 int res;
3226
3227 CLEAR_FIELD(ea);
3228 ea.cmdidx = iptr->isn_arg.cexpr.cexpr_ref->cer_cmdidx;
3229 ea.forceit = iptr->isn_arg.cexpr.cexpr_ref->cer_forceit;
3230 ea.cmdlinep = &iptr->isn_arg.cexpr.cexpr_ref->cer_cmdline;
3231 --ectx->ec_stack.ga_len;
3232 tv = STACK_TV_BOT(0);
Bram Moolenaarbd683e32022-07-18 17:49:03 +01003233 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003234 res = cexpr_core(&ea, tv);
3235 clear_tv(tv);
3236 if (res == FAIL)
3237 goto on_error;
3238 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02003239#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003240 break;
3241
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003242 // execute Ex command from pieces on the stack
3243 case ISN_EXECCONCAT:
3244 {
3245 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +02003246 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003247 int pass;
3248 int i;
3249 char_u *cmd = NULL;
3250 char_u *str;
3251
3252 for (pass = 1; pass <= 2; ++pass)
3253 {
3254 for (i = 0; i < count; ++i)
3255 {
3256 tv = STACK_TV_BOT(i - count);
3257 str = tv->vval.v_string;
3258 if (str != NULL && *str != NUL)
3259 {
3260 if (pass == 2)
3261 STRCPY(cmd + len, str);
3262 len += STRLEN(str);
3263 }
3264 if (pass == 2)
3265 clear_tv(tv);
3266 }
3267 if (pass == 1)
3268 {
3269 cmd = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003270 if (unlikely(cmd == NULL))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003271 goto theend;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003272 len = 0;
3273 }
3274 }
3275
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02003276 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003277 do_cmdline_cmd(cmd);
3278 vim_free(cmd);
3279 }
3280 break;
3281
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003282 // execute :echo {string} ...
3283 case ISN_ECHO:
3284 {
3285 int count = iptr->isn_arg.echo.echo_count;
3286 int atstart = TRUE;
3287 int needclr = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003288 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003289
3290 for (idx = 0; idx < count; ++idx)
3291 {
3292 tv = STACK_TV_BOT(idx - count);
3293 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
3294 &atstart, &needclr);
3295 clear_tv(tv);
3296 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01003297 if (needclr)
3298 msg_clr_eos();
Bram Moolenaar4c137212021-04-19 16:48:48 +02003299 ectx->ec_stack.ga_len -= count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003300 }
3301 break;
3302
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003303 // :execute {string} ...
3304 // :echomsg {string} ...
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01003305 // :echowindow {string} ...
Bram Moolenaar7de62622021-08-07 15:05:47 +02003306 // :echoconsole {string} ...
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003307 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01003308 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003309 case ISN_ECHOMSG:
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01003310 case ISN_ECHOWINDOW:
Bram Moolenaar7de62622021-08-07 15:05:47 +02003311 case ISN_ECHOCONSOLE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003312 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01003313 {
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01003314 int count;
Bram Moolenaarad39c092020-02-26 18:23:43 +01003315 garray_T ga;
3316 char_u buf[NUMBUFLEN];
3317 char_u *p;
3318 int len;
3319 int failed = FALSE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003320 int idx;
Bram Moolenaarad39c092020-02-26 18:23:43 +01003321
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01003322 if (iptr->isn_type == ISN_ECHOWINDOW)
3323 count = iptr->isn_arg.echowin.ewin_count;
3324 else
3325 count = iptr->isn_arg.number;
Bram Moolenaarad39c092020-02-26 18:23:43 +01003326 ga_init2(&ga, 1, 80);
3327 for (idx = 0; idx < count; ++idx)
3328 {
3329 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003330 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01003331 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003332 if (tv->v_type == VAR_CHANNEL
3333 || tv->v_type == VAR_JOB)
3334 {
3335 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar68db9962021-05-09 23:19:22 +02003336 semsg(_(e_using_invalid_value_as_string_str),
3337 vartype_name(tv->v_type));
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003338 break;
3339 }
3340 else
3341 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01003342 }
3343 else
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003344 p = tv_stringify(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01003345
3346 len = (int)STRLEN(p);
Bram Moolenaar35578162021-08-02 19:10:38 +02003347 if (GA_GROW_FAILS(&ga, len + 2))
Bram Moolenaarad39c092020-02-26 18:23:43 +01003348 failed = TRUE;
3349 else
3350 {
3351 if (ga.ga_len > 0)
3352 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
3353 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
3354 ga.ga_len += len;
3355 }
3356 clear_tv(tv);
3357 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003358 ectx->ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003359 if (failed)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003360 {
3361 ga_clear(&ga);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003362 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003363 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01003364
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003365 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003366 {
3367 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02003368 {
3369 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003370 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01003371 if (did_emsg)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003372 {
3373 ga_clear(&ga);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01003374 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003375 }
Bram Moolenaar430deb12020-08-23 16:29:11 +02003376 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003377 else
3378 {
3379 msg_sb_eol();
3380 if (iptr->isn_type == ISN_ECHOMSG)
3381 {
3382 msg_attr(ga.ga_data, echo_attr);
3383 out_flush();
3384 }
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01003385#ifdef HAS_MESSAGE_WINDOW
3386 else if (iptr->isn_type == ISN_ECHOWINDOW)
3387 {
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01003388 start_echowindow(
3389 iptr->isn_arg.echowin.ewin_time);
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01003390 msg_attr(ga.ga_data, echo_attr);
3391 end_echowindow();
3392 }
3393#endif
Bram Moolenaar7de62622021-08-07 15:05:47 +02003394 else if (iptr->isn_type == ISN_ECHOCONSOLE)
3395 {
3396 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data),
3397 TRUE);
3398 ui_write((char_u *)"\r\n", 2, TRUE);
3399 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003400 else
3401 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003402 SOURCING_LNUM = iptr->isn_lnum;
3403 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003404 }
3405 }
3406 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01003407 ga_clear(&ga);
3408 }
3409 break;
3410
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003411 // load local variable or argument
3412 case ISN_LOAD:
Bram Moolenaar35578162021-08-02 19:10:38 +02003413 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003414 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003415 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003416 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003417 break;
3418
3419 // load v: variable
3420 case ISN_LOADV:
Bram Moolenaar35578162021-08-02 19:10:38 +02003421 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003422 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003423 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003424 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003425 break;
3426
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003427 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003428 case ISN_LOADSCRIPT:
3429 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01003430 scriptref_T *sref = iptr->isn_arg.script.scriptref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003431 svar_T *sv;
3432
Bram Moolenaar6db660b2021-08-01 14:08:54 +02003433 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003434 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003435 goto theend;
Bram Moolenaar859cc212022-03-28 15:22:35 +01003436 allocate_if_null(sv);
Bram Moolenaar35578162021-08-02 19:10:38 +02003437 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003438 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003439 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003440 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003441 }
3442 break;
3443
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003444 // load s: variable in old script or autoload import
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003445 case ISN_LOADS:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003446 case ISN_LOADEXPORT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003447 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003448 int sid = iptr->isn_arg.loadstore.ls_sid;
3449 hashtab_T *ht = &SCRIPT_VARS(sid);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003450 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003451 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003452
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003453 if (di == NULL)
3454 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003455 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003456 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003457 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003458 }
3459 else
3460 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003461 if (iptr->isn_type == ISN_LOADEXPORT)
3462 {
3463 int idx = get_script_item_idx(sid, name, 0,
3464 NULL, NULL);
3465 svar_T *sv;
3466
3467 if (idx >= 0)
3468 {
3469 sv = ((svar_T *)SCRIPT_ITEM(sid)
3470 ->sn_var_vals.ga_data) + idx;
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003471 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003472 {
3473 SOURCING_LNUM = iptr->isn_lnum;
3474 semsg(_(e_item_not_exported_in_script_str),
3475 name);
3476 goto on_error;
3477 }
3478 }
3479 }
Bram Moolenaar35578162021-08-02 19:10:38 +02003480 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003481 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003482 copy_tv(&di->di_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003483 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003484 }
3485 }
3486 break;
3487
Bram Moolenaard3aac292020-04-19 14:32:17 +02003488 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003489 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02003490 case ISN_LOADB:
3491 case ISN_LOADW:
3492 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003493 {
Bram Moolenaar06b77222022-01-25 15:51:56 +00003494 int res = load_namespace_var(ectx, iptr->isn_type, iptr);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003495
Bram Moolenaar06b77222022-01-25 15:51:56 +00003496 if (res == NOTDONE)
Bram Moolenaarcbbc48f2022-01-18 12:58:28 +00003497 goto theend;
Bram Moolenaar06b77222022-01-25 15:51:56 +00003498 if (res == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003499 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003500 }
Bram Moolenaar06b77222022-01-25 15:51:56 +00003501
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003502 break;
3503
Bram Moolenaar03290b82020-12-19 16:30:44 +01003504 // load autoload variable
3505 case ISN_LOADAUTO:
3506 {
3507 char_u *name = iptr->isn_arg.string;
3508
Bram Moolenaar35578162021-08-02 19:10:38 +02003509 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003510 goto theend;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003511 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003512 if (eval_variable(name, (int)STRLEN(name), 0,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003513 STACK_TV_BOT(0), NULL, EVAL_VAR_VERBOSE) == FAIL)
Bram Moolenaar03290b82020-12-19 16:30:44 +01003514 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003515 ++ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003516 }
3517 break;
3518
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003519 // load g:/b:/w:/t: namespace
3520 case ISN_LOADGDICT:
3521 case ISN_LOADBDICT:
3522 case ISN_LOADWDICT:
3523 case ISN_LOADTDICT:
3524 {
3525 dict_T *d = NULL;
3526
3527 switch (iptr->isn_type)
3528 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02003529 case ISN_LOADGDICT: d = get_globvar_dict(); break;
3530 case ISN_LOADBDICT: d = curbuf->b_vars; break;
3531 case ISN_LOADWDICT: d = curwin->w_vars; break;
3532 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003533 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003534 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003535 }
Bram Moolenaar35578162021-08-02 19:10:38 +02003536 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003537 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003538 tv = STACK_TV_BOT(0);
3539 tv->v_type = VAR_DICT;
3540 tv->v_lock = 0;
3541 tv->vval.v_dict = d;
Bram Moolenaar1bd3cb22021-02-24 12:27:31 +01003542 ++d->dv_refcount;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003543 ++ectx->ec_stack.ga_len;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003544 }
3545 break;
3546
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003547 // load &option
3548 case ISN_LOADOPT:
3549 {
3550 typval_T optval;
3551 char_u *name = iptr->isn_arg.string;
3552
Bram Moolenaara8c17702020-04-01 21:17:24 +02003553 // This is not expected to fail, name is checked during
3554 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar35578162021-08-02 19:10:38 +02003555 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003556 goto theend;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003557 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003558 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003559 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003560 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003561 }
3562 break;
3563
3564 // load $ENV
3565 case ISN_LOADENV:
3566 {
3567 typval_T optval;
3568 char_u *name = iptr->isn_arg.string;
3569
Bram Moolenaar35578162021-08-02 19:10:38 +02003570 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003571 goto theend;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003572 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003573 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003574 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003575 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003576 }
3577 break;
3578
3579 // load @register
3580 case ISN_LOADREG:
Bram Moolenaar35578162021-08-02 19:10:38 +02003581 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003582 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003583 tv = STACK_TV_BOT(0);
3584 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003585 tv->v_lock = 0;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02003586 // This may result in NULL, which should be equivalent to an
3587 // empty string.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003588 tv->vval.v_string = get_reg_contents(
3589 iptr->isn_arg.number, GREG_EXPR_SRC);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003590 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003591 break;
3592
3593 // store local variable
3594 case ISN_STORE:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003595 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003596 tv = STACK_TV_VAR(iptr->isn_arg.number);
3597 clear_tv(tv);
3598 *tv = *STACK_TV_BOT(0);
3599 break;
3600
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003601 // store s: variable in old script or autoload import
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003602 case ISN_STORES:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003603 case ISN_STOREEXPORT:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003604 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003605 int sid = iptr->isn_arg.loadstore.ls_sid;
3606 hashtab_T *ht = &SCRIPT_VARS(sid);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003607 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003608 dictitem_T *di = find_var_in_ht(ht, 0,
3609 iptr->isn_type == ISN_STORES
3610 ? name + 2 : name, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003611
Bram Moolenaar4c137212021-04-19 16:48:48 +02003612 --ectx->ec_stack.ga_len;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003613 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003614 if (di == NULL)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003615 {
3616 if (iptr->isn_type == ISN_STOREEXPORT)
3617 {
3618 semsg(_(e_undefined_variable_str), name);
Bram Moolenaard1d26842022-03-31 10:13:47 +01003619 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003620 goto on_error;
3621 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003622 store_var(name, STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003623 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003624 else
3625 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003626 if (iptr->isn_type == ISN_STOREEXPORT)
3627 {
3628 int idx = get_script_item_idx(sid, name, 0,
3629 NULL, NULL);
3630
Bram Moolenaar06651632022-04-27 17:54:25 +01003631 // can this ever fail?
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003632 if (idx >= 0)
3633 {
3634 svar_T *sv = ((svar_T *)SCRIPT_ITEM(sid)
3635 ->sn_var_vals.ga_data) + idx;
3636
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003637 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003638 {
3639 semsg(_(e_item_not_exported_in_script_str),
3640 name);
Bram Moolenaard1d26842022-03-31 10:13:47 +01003641 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003642 goto on_error;
3643 }
3644 }
3645 }
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02003646 if (var_check_permission(di, name) == FAIL)
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003647 {
3648 clear_tv(STACK_TV_BOT(0));
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02003649 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003650 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003651 clear_tv(&di->di_tv);
3652 di->di_tv = *STACK_TV_BOT(0);
3653 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003654 }
3655 break;
3656
3657 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003658 case ISN_STORESCRIPT:
3659 {
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003660 scriptref_T *sref = iptr->isn_arg.script.scriptref;
3661 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003662
Bram Moolenaar6db660b2021-08-01 14:08:54 +02003663 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003664 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003665 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003666 --ectx->ec_stack.ga_len;
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003667
3668 // "const" and "final" are checked at compile time, locking
3669 // the value needs to be checked here.
3670 SOURCING_LNUM = iptr->isn_lnum;
3671 if (value_check_lock(sv->sv_tv->v_lock, sv->sv_name, FALSE))
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003672 {
3673 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003674 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003675 }
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003676
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003677 clear_tv(sv->sv_tv);
3678 *sv->sv_tv = *STACK_TV_BOT(0);
3679 }
3680 break;
3681
3682 // store option
3683 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003684 case ISN_STOREFUNCOPT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003685 {
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003686 char_u *opt_name = iptr->isn_arg.storeopt.so_name;
3687 int opt_flags = iptr->isn_arg.storeopt.so_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003688 long n = 0;
3689 char_u *s = NULL;
3690 char *msg;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003691 char_u numbuf[NUMBUFLEN];
3692 char_u *tofree = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003693
Bram Moolenaar4c137212021-04-19 16:48:48 +02003694 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003695 tv = STACK_TV_BOT(0);
3696 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01003697 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003698 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01003699 if (s == NULL)
3700 s = (char_u *)"";
3701 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003702 else if (iptr->isn_type == ISN_STOREFUNCOPT)
3703 {
3704 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003705 // If the option can be set to a function reference or
3706 // a lambda and the passed value is a function
3707 // reference, then convert it to the name (string) of
3708 // the function reference.
3709 s = tv2string(tv, &tofree, numbuf, 0);
3710 if (s == NULL || *s == NUL)
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003711 {
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003712 // cannot happen?
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003713 clear_tv(tv);
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003714 vim_free(tofree);
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003715 goto on_error;
3716 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003717 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003718 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003719 // must be VAR_NUMBER, CHECKTYPE makes sure
3720 n = tv->vval.v_number;
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003721 msg = set_option_value(opt_name, n, s, opt_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02003722 clear_tv(tv);
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003723 vim_free(tofree);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003724 if (msg != NULL)
3725 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003726 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003727 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02003728 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003729 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003730 }
3731 break;
3732
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003733 // store $ENV
3734 case ISN_STOREENV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003735 --ectx->ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003736 tv = STACK_TV_BOT(0);
3737 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
3738 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003739 break;
3740
3741 // store @r
3742 case ISN_STOREREG:
3743 {
3744 int reg = iptr->isn_arg.number;
3745
Bram Moolenaar4c137212021-04-19 16:48:48 +02003746 --ectx->ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01003747 tv = STACK_TV_BOT(0);
Bram Moolenaar74f4a962021-06-17 21:03:07 +02003748 write_reg_contents(reg, tv_get_string(tv), -1, FALSE);
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01003749 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003750 }
3751 break;
3752
3753 // store v: variable
3754 case ISN_STOREV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003755 --ectx->ec_stack.ga_len;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003756 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
3757 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003758 // should not happen, type is checked when compiling
3759 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003760 break;
3761
Bram Moolenaard3aac292020-04-19 14:32:17 +02003762 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003763 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02003764 case ISN_STOREB:
3765 case ISN_STOREW:
3766 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003767 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003768 dictitem_T *di;
3769 hashtab_T *ht;
3770 char_u *name = iptr->isn_arg.string + 2;
3771
Bram Moolenaard3aac292020-04-19 14:32:17 +02003772 switch (iptr->isn_type)
3773 {
3774 case ISN_STOREG:
3775 ht = get_globvar_ht();
3776 break;
3777 case ISN_STOREB:
3778 ht = &curbuf->b_vars->dv_hashtab;
3779 break;
3780 case ISN_STOREW:
3781 ht = &curwin->w_vars->dv_hashtab;
3782 break;
3783 case ISN_STORET:
3784 ht = &curtab->tp_vars->dv_hashtab;
3785 break;
3786 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003787 goto theend;
Bram Moolenaard3aac292020-04-19 14:32:17 +02003788 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003789
Bram Moolenaar4c137212021-04-19 16:48:48 +02003790 --ectx->ec_stack.ga_len;
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003791 di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003792 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003793 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003794 else
3795 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003796 SOURCING_LNUM = iptr->isn_lnum;
3797 if (var_check_permission(di, name) == FAIL)
3798 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003799 clear_tv(&di->di_tv);
3800 di->di_tv = *STACK_TV_BOT(0);
3801 }
3802 }
3803 break;
3804
Bram Moolenaar03290b82020-12-19 16:30:44 +01003805 // store an autoload variable
3806 case ISN_STOREAUTO:
3807 SOURCING_LNUM = iptr->isn_lnum;
3808 set_var(iptr->isn_arg.string, STACK_TV_BOT(-1), TRUE);
3809 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003810 --ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003811 break;
3812
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003813 // store number in local variable
3814 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01003815 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003816 clear_tv(tv);
3817 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01003818 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003819 break;
3820
Bram Moolenaar590162c2022-12-24 21:24:06 +00003821 // Store a value in a list, dict, blob or object variable.
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01003822 case ISN_STOREINDEX:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003823 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003824 int res = execute_storeindex(iptr, ectx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003825
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003826 if (res == FAIL)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02003827 goto on_error;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003828 if (res == NOTDONE)
3829 goto theend;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003830 }
3831 break;
3832
Bram Moolenaarea5c8982022-02-17 14:42:02 +00003833 // store value in list or blob range
Bram Moolenaar68452172021-04-12 21:21:02 +02003834 case ISN_STORERANGE:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003835 if (execute_storerange(iptr, ectx) == FAIL)
3836 goto on_error;
Bram Moolenaar68452172021-04-12 21:21:02 +02003837 break;
3838
Bram Moolenaard505d172022-12-18 21:42:55 +00003839 case ISN_LOAD_CLASSMEMBER:
3840 {
3841 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
3842 goto theend;
3843 classmember_T *cm = &iptr->isn_arg.classmember;
3844 *STACK_TV_BOT(0) =
3845 cm->cm_class->class_members_tv[cm->cm_idx];
3846 ++ectx->ec_stack.ga_len;
3847 }
3848 break;
3849
3850 case ISN_STORE_CLASSMEMBER:
3851 {
3852 classmember_T *cm = &iptr->isn_arg.classmember;
3853 tv = &cm->cm_class->class_members_tv[cm->cm_idx];
3854 clear_tv(tv);
3855 *tv = *STACK_TV_BOT(-1);
3856 --ectx->ec_stack.ga_len;
3857 }
3858 break;
3859
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01003860 // Load or store variable or argument from outer scope.
Bram Moolenaar0186e582021-01-10 18:33:11 +01003861 case ISN_LOADOUTER:
3862 case ISN_STOREOUTER:
3863 {
3864 int depth = iptr->isn_arg.outer.outer_depth;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02003865 outer_T *outer = ectx->ec_outer_ref == NULL ? NULL
3866 : ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003867
3868 while (depth > 1 && outer != NULL)
3869 {
3870 outer = outer->out_up;
3871 --depth;
3872 }
3873 if (outer == NULL)
3874 {
3875 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar69c76172021-12-02 16:38:52 +00003876 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx
3877 || ectx->ec_outer_ref == NULL)
3878 // Possibly :def function called from legacy
3879 // context.
3880 emsg(_(e_closure_called_from_invalid_context));
3881 else
3882 iemsg("LOADOUTER depth more than scope levels");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003883 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003884 }
Bram Moolenaarcc341812022-09-19 15:54:34 +01003885 if (depth < 0)
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01003886 // Variable declared in loop. May be copied if the
3887 // loop block has already ended.
Bram Moolenaarcc341812022-09-19 15:54:34 +01003888 tv = ((typval_T *)outer->out_loop[-depth - 1]
3889 .stack->ga_data)
3890 + outer->out_loop[-depth - 1].var_idx
3891 + iptr->isn_arg.outer.outer_idx;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01003892 else
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01003893 // Variable declared in a function. May be copied if
3894 // the function has already returned.
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01003895 tv = ((typval_T *)outer->out_stack->ga_data)
3896 + outer->out_frame_idx + STACK_FRAME_SIZE
3897 + iptr->isn_arg.outer.outer_idx;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003898 if (iptr->isn_type == ISN_LOADOUTER)
3899 {
Bram Moolenaar35578162021-08-02 19:10:38 +02003900 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003901 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003902 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003903 ++ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003904 }
3905 else
3906 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003907 --ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003908 clear_tv(tv);
3909 *tv = *STACK_TV_BOT(0);
3910 }
3911 }
3912 break;
3913
Bram Moolenaar752fc692021-01-04 21:57:11 +01003914 // unlet item in list or dict variable
3915 case ISN_UNLETINDEX:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003916 if (execute_unletindex(iptr, ectx) == FAIL)
3917 goto on_error;
Bram Moolenaar752fc692021-01-04 21:57:11 +01003918 break;
3919
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01003920 // unlet range of items in list variable
3921 case ISN_UNLETRANGE:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003922 if (execute_unletrange(iptr, ectx) == FAIL)
3923 goto on_error;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01003924 break;
3925
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003926 // push constant
3927 case ISN_PUSHNR:
3928 case ISN_PUSHBOOL:
3929 case ISN_PUSHSPEC:
3930 case ISN_PUSHF:
3931 case ISN_PUSHS:
3932 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003933 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003934 case ISN_PUSHCHANNEL:
3935 case ISN_PUSHJOB:
Bram Moolenaar35578162021-08-02 19:10:38 +02003936 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003937 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003938 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003939 tv->v_lock = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003940 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003941 switch (iptr->isn_type)
3942 {
3943 case ISN_PUSHNR:
3944 tv->v_type = VAR_NUMBER;
3945 tv->vval.v_number = iptr->isn_arg.number;
3946 break;
3947 case ISN_PUSHBOOL:
3948 tv->v_type = VAR_BOOL;
3949 tv->vval.v_number = iptr->isn_arg.number;
3950 break;
3951 case ISN_PUSHSPEC:
3952 tv->v_type = VAR_SPECIAL;
3953 tv->vval.v_number = iptr->isn_arg.number;
3954 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003955 case ISN_PUSHF:
3956 tv->v_type = VAR_FLOAT;
3957 tv->vval.v_float = iptr->isn_arg.fnumber;
3958 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003959 case ISN_PUSHBLOB:
3960 blob_copy(iptr->isn_arg.blob, tv);
3961 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003962 case ISN_PUSHFUNC:
3963 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01003964 if (iptr->isn_arg.string == NULL)
3965 tv->vval.v_string = NULL;
3966 else
3967 tv->vval.v_string =
3968 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003969 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003970 case ISN_PUSHCHANNEL:
3971#ifdef FEAT_JOB_CHANNEL
3972 tv->v_type = VAR_CHANNEL;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003973 tv->vval.v_channel = NULL;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003974#endif
3975 break;
3976 case ISN_PUSHJOB:
3977#ifdef FEAT_JOB_CHANNEL
3978 tv->v_type = VAR_JOB;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003979 tv->vval.v_job = NULL;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003980#endif
3981 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003982 default:
3983 tv->v_type = VAR_STRING;
Bram Moolenaarf8691002022-03-10 12:20:53 +00003984 tv->vval.v_string = iptr->isn_arg.string == NULL
3985 ? NULL : vim_strsave(iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003986 }
3987 break;
3988
Bram Moolenaar06b77222022-01-25 15:51:56 +00003989 case ISN_AUTOLOAD:
3990 {
3991 char_u *name = iptr->isn_arg.string;
3992
3993 (void)script_autoload(name, FALSE);
3994 if (find_func(name, TRUE))
3995 {
3996 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
3997 goto theend;
3998 tv = STACK_TV_BOT(0);
3999 tv->v_lock = 0;
4000 ++ectx->ec_stack.ga_len;
4001 tv->v_type = VAR_FUNC;
4002 tv->vval.v_string = vim_strsave(name);
4003 }
4004 else
4005 {
4006 int res = load_namespace_var(ectx, ISN_LOADG, iptr);
4007
4008 if (res == NOTDONE)
4009 goto theend;
4010 if (res == FAIL)
4011 goto on_error;
4012 }
4013 }
4014 break;
4015
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004016 case ISN_UNLET:
4017 if (do_unlet(iptr->isn_arg.unlet.ul_name,
4018 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02004019 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004020 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02004021 case ISN_UNLETENV:
LemonBoy77142312022-04-15 20:50:46 +01004022 vim_unsetenv_ext(iptr->isn_arg.unlet.ul_name);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02004023 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004024
Bram Moolenaaraacc9662021-08-13 19:40:51 +02004025 case ISN_LOCKUNLOCK:
4026 {
4027 typval_T *lval_root_save = lval_root;
4028 int res;
4029
4030 // Stack has the local variable, argument the whole :lock
4031 // or :unlock command, like ISN_EXEC.
4032 --ectx->ec_stack.ga_len;
4033 lval_root = STACK_TV_BOT(0);
4034 res = exec_command(iptr);
4035 clear_tv(lval_root);
4036 lval_root = lval_root_save;
4037 if (res == FAIL)
4038 goto on_error;
4039 }
4040 break;
4041
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02004042 case ISN_LOCKCONST:
4043 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
4044 break;
4045
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004046 // create a list from items on the stack; uses a single allocation
4047 // for the list header and the items
4048 case ISN_NEWLIST:
Bram Moolenaar4c137212021-04-19 16:48:48 +02004049 if (exe_newlist(iptr->isn_arg.number, ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004050 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004051 break;
4052
4053 // create a dict from items on the stack
4054 case ISN_NEWDICT:
4055 {
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01004056 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004057
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01004058 SOURCING_LNUM = iptr->isn_lnum;
4059 res = exe_newdict(iptr->isn_arg.number, ectx);
4060 if (res == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004061 goto theend;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01004062 if (res == MAYBE)
4063 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004064 }
4065 break;
4066
LemonBoy372bcce2022-04-25 12:43:20 +01004067 case ISN_CONCAT:
4068 if (exe_concat(iptr->isn_arg.number, ectx) == FAIL)
4069 goto theend;
4070 break;
4071
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004072 // create a partial with NULL value
4073 case ISN_NEWPARTIAL:
4074 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
4075 goto theend;
4076 ++ectx->ec_stack.ga_len;
4077 tv = STACK_TV_BOT(-1);
4078 tv->v_type = VAR_PARTIAL;
4079 tv->v_lock = 0;
4080 tv->vval.v_partial = NULL;
4081 break;
4082
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004083 // call a :def function
4084 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02004085 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01004086 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
4087 NULL,
4088 iptr->isn_arg.dfunc.cdf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02004089 ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02004090 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004091 break;
4092
4093 // call a builtin function
4094 case ISN_BCALL:
4095 SOURCING_LNUM = iptr->isn_lnum;
4096 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
4097 iptr->isn_arg.bfunc.cbf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02004098 ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004099 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004100 break;
4101
4102 // call a funcref or partial
4103 case ISN_PCALL:
4104 {
4105 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
4106 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02004107 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004108
4109 SOURCING_LNUM = iptr->isn_lnum;
4110 if (pfunc->cpf_top)
4111 {
4112 // funcref is above the arguments
4113 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
4114 }
4115 else
4116 {
4117 // Get the funcref from the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004118 --ectx->ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02004119 partial_tv = *STACK_TV_BOT(0);
4120 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004121 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004122 r = call_partial(tv, pfunc->cpf_argcount, ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02004123 if (tv == &partial_tv)
4124 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004125 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004126 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004127 }
4128 break;
4129
Bram Moolenaarbd5da372020-03-31 23:13:10 +02004130 case ISN_PCALL_END:
4131 // PCALL finished, arguments have been consumed and replaced by
4132 // the return value. Now clear the funcref from the stack,
4133 // and move the return value in its place.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004134 --ectx->ec_stack.ga_len;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02004135 clear_tv(STACK_TV_BOT(-1));
4136 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
4137 break;
4138
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004139 // call a user defined function or funcref/partial
4140 case ISN_UCALL:
4141 {
4142 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
4143
4144 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01004145 if (call_eval_func(cufunc->cuf_name, cufunc->cuf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02004146 ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004147 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004148 }
4149 break;
4150
Bram Moolenaar1d84f762022-09-03 21:35:53 +01004151 // :defer func(arg)
4152 case ISN_DEFER:
Bram Moolenaar806a2732022-09-04 15:40:36 +01004153 if (defer_command(iptr->isn_arg.defer.defer_var_idx,
Bram Moolenaar1d84f762022-09-03 21:35:53 +01004154 iptr->isn_arg.defer.defer_argcount, ectx) == FAIL)
4155 goto on_error;
4156 break;
4157
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004158 // Return from a :def function call without a value.
4159 // Return from a constructor.
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02004160 case ISN_RETURN_VOID:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004161 case ISN_RETURN_OBJECT:
Bram Moolenaar35578162021-08-02 19:10:38 +02004162 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004163 goto theend;
Bram Moolenaar299f3032021-01-08 20:53:09 +01004164 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004165 ++ectx->ec_stack.ga_len;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004166 if (iptr->isn_type == ISN_RETURN_VOID)
4167 {
4168 tv->v_type = VAR_VOID;
4169 tv->vval.v_number = 0;
4170 tv->v_lock = 0;
4171 }
4172 else
4173 {
4174 *tv = *STACK_TV_VAR(0);
4175 ++tv->vval.v_object->obj_refcount;
4176 }
Bram Moolenaar299f3032021-01-08 20:53:09 +01004177 // FALLTHROUGH
4178
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02004179 // return from a :def function call with what is on the stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004180 case ISN_RETURN:
4181 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004182 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01004183 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01004184
4185 if (trystack->ga_len > 0)
4186 trycmd = ((trycmd_T *)trystack->ga_data)
4187 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004188 if (trycmd != NULL
Bram Moolenaar4c137212021-04-19 16:48:48 +02004189 && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004190 {
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01004191 // jump to ":finally" or ":endtry"
4192 if (trycmd->tcd_finally_idx != 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004193 ectx->ec_iidx = trycmd->tcd_finally_idx;
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01004194 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02004195 ectx->ec_iidx = trycmd->tcd_endtry_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004196 trycmd->tcd_return = TRUE;
4197 }
4198 else
Bram Moolenaard032f342020-07-18 18:13:02 +02004199 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004200 }
4201 break;
4202
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004203 // push a partial, a reference to a compiled function
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004204 case ISN_FUNCREF:
4205 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004206 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
4207 ufunc_T *ufunc;
4208 funcref_T *funcref = &iptr->isn_arg.funcref;
4209 funcref_extra_T *extra = funcref->fr_extra;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004210
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004211 if (pt == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004212 goto theend;
Bram Moolenaar35578162021-08-02 19:10:38 +02004213 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02004214 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004215 vim_free(pt);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004216 goto theend;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004217 }
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004218 if (extra == NULL || extra->fre_func_name == NULL)
Bram Moolenaar38453522021-11-28 22:00:12 +00004219 {
4220 dfunc_T *pt_dfunc = ((dfunc_T *)def_functions.ga_data)
4221 + funcref->fr_dfunc_idx;
4222
4223 ufunc = pt_dfunc->df_ufunc;
4224 }
4225 else
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004226 ufunc = find_func(extra->fre_func_name, FALSE);
Bram Moolenaar56acd1f2022-02-18 13:24:52 +00004227 if (ufunc == NULL)
4228 {
4229 SOURCING_LNUM = iptr->isn_lnum;
4230 iemsg("ufunc unexpectedly NULL for FUNCREF");
4231 goto theend;
4232 }
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004233 if (fill_partial_and_closure(pt, ufunc,
Bram Moolenaarcc341812022-09-19 15:54:34 +01004234 extra == NULL ? NULL : &extra->fre_loopvar_info,
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004235 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004236 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004237 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004238 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004239 tv->vval.v_partial = pt;
4240 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02004241 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004242 }
4243 break;
4244
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004245 // Create a global function from a lambda.
4246 case ISN_NEWFUNC:
4247 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004248 newfuncarg_T *arg = iptr->isn_arg.newfunc.nf_arg;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004249
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004250 if (copy_lambda_to_global_func(arg->nfa_lambda,
Bram Moolenaarcc341812022-09-19 15:54:34 +01004251 arg->nfa_global, &arg->nfa_loopvar_info,
4252 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004253 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004254 }
4255 break;
4256
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004257 // List functions
4258 case ISN_DEF:
4259 if (iptr->isn_arg.string == NULL)
4260 list_functions(NULL);
4261 else
4262 {
Bram Moolenaar14336722022-01-08 16:02:59 +00004263 exarg_T ea;
4264 garray_T lines_to_free;
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004265
4266 CLEAR_FIELD(ea);
4267 ea.cmd = ea.arg = iptr->isn_arg.string;
Bram Moolenaar14336722022-01-08 16:02:59 +00004268 ga_init2(&lines_to_free, sizeof(char_u *), 50);
Bram Moolenaarffdaca92022-12-09 21:41:48 +00004269 define_function(&ea, NULL, &lines_to_free, FALSE);
Bram Moolenaar14336722022-01-08 16:02:59 +00004270 ga_clear_strings(&lines_to_free);
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004271 }
4272 break;
4273
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004274 // jump if a condition is met
4275 case ISN_JUMP:
4276 {
4277 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004278 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004279 int jump = TRUE;
4280
4281 if (when != JUMP_ALWAYS)
4282 {
4283 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004284 if (when == JUMP_IF_COND_FALSE
Bram Moolenaar13106602020-10-04 16:06:05 +02004285 || when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004286 || when == JUMP_IF_COND_TRUE)
4287 {
4288 SOURCING_LNUM = iptr->isn_lnum;
4289 jump = tv_get_bool_chk(tv, &error);
4290 if (error)
4291 goto on_error;
4292 }
4293 else
4294 jump = tv2bool(tv);
Bram Moolenaarf6ced982022-04-28 12:00:49 +01004295 if (when == JUMP_IF_FALSE || when == JUMP_IF_COND_FALSE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004296 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01004297 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004298 {
4299 // drop the value from the stack
4300 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004301 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004302 }
4303 }
4304 if (jump)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004305 ectx->ec_iidx = iptr->isn_arg.jump.jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004306 }
4307 break;
4308
Bram Moolenaarb46c0832022-09-15 17:19:37 +01004309 // "while": jump to end if a condition is false
4310 case ISN_WHILE:
4311 {
4312 int error = FALSE;
4313 int jump = TRUE;
4314
4315 tv = STACK_TV_BOT(-1);
4316 SOURCING_LNUM = iptr->isn_lnum;
4317 jump = !tv_get_bool_chk(tv, &error);
4318 if (error)
4319 goto on_error;
4320 // drop the value from the stack
4321 clear_tv(tv);
4322 --ectx->ec_stack.ga_len;
4323 if (jump)
4324 ectx->ec_iidx = iptr->isn_arg.whileloop.while_end;
4325
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01004326 // Store the current funcref count, may be used by
Bram Moolenaarcc341812022-09-19 15:54:34 +01004327 // ISN_ENDLOOP later
Bram Moolenaarb46c0832022-09-15 17:19:37 +01004328 tv = STACK_TV_VAR(
4329 iptr->isn_arg.whileloop.while_funcref_idx);
4330 tv->vval.v_number = ectx->ec_funcrefs.ga_len;
4331 }
4332 break;
4333
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02004334 // Jump if an argument with a default value was already set and not
4335 // v:none.
4336 case ISN_JUMP_IF_ARG_SET:
Bram Moolenaar65b0d162022-12-13 18:43:22 +00004337 case ISN_JUMP_IF_ARG_NOT_SET:
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02004338 tv = STACK_TV_VAR(iptr->isn_arg.jumparg.jump_arg_off);
Bram Moolenaar65b0d162022-12-13 18:43:22 +00004339 int arg_set = tv->v_type != VAR_UNKNOWN
4340 && !(tv->v_type == VAR_SPECIAL
4341 && tv->vval.v_number == VVAL_NONE);
4342 if (iptr->isn_type == ISN_JUMP_IF_ARG_SET ? arg_set : !arg_set)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004343 ectx->ec_iidx = iptr->isn_arg.jumparg.jump_where;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02004344 break;
4345
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004346 // top of a for loop
4347 case ISN_FOR:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00004348 if (execute_for(iptr, ectx) == FAIL)
4349 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004350 break;
4351
Bram Moolenaarb46c0832022-09-15 17:19:37 +01004352 // end of a for or while loop
4353 case ISN_ENDLOOP:
4354 if (execute_endloop(iptr, ectx) == FAIL)
4355 goto theend;
4356 break;
4357
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004358 // start of ":try" block
4359 case ISN_TRY:
4360 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01004361 trycmd_T *trycmd = NULL;
4362
Bram Moolenaar35578162021-08-02 19:10:38 +02004363 if (GA_GROW_FAILS(&ectx->ec_trystack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004364 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004365 trycmd = ((trycmd_T *)ectx->ec_trystack.ga_data)
4366 + ectx->ec_trystack.ga_len;
4367 ++ectx->ec_trystack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004368 ++trylevel;
Bram Moolenaar8d4be892021-02-13 18:33:02 +01004369 CLEAR_POINTER(trycmd);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004370 trycmd->tcd_frame_idx = ectx->ec_frame_idx;
4371 trycmd->tcd_stack_len = ectx->ec_stack.ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01004372 trycmd->tcd_catch_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00004373 iptr->isn_arg.tryref.try_ref->try_catch;
Bram Moolenaara91a7132021-03-25 21:12:15 +01004374 trycmd->tcd_finally_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00004375 iptr->isn_arg.tryref.try_ref->try_finally;
Bram Moolenaara91a7132021-03-25 21:12:15 +01004376 trycmd->tcd_endtry_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00004377 iptr->isn_arg.tryref.try_ref->try_endtry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004378 }
4379 break;
4380
4381 case ISN_PUSHEXC:
4382 if (current_exception == NULL)
4383 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004384 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004385 iemsg("Evaluating catch while current_exception is NULL");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004386 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004387 }
Bram Moolenaar35578162021-08-02 19:10:38 +02004388 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004389 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004390 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004391 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004392 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02004393 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004394 tv->vval.v_string = vim_strsave(
4395 (char_u *)current_exception->value);
4396 break;
4397
4398 case ISN_CATCH:
4399 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004400 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar06651632022-04-27 17:54:25 +01004401 trycmd_T *trycmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004402
Bram Moolenaar4c137212021-04-19 16:48:48 +02004403 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar06651632022-04-27 17:54:25 +01004404 trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004405 + trystack->ga_len - 1;
Bram Moolenaar06651632022-04-27 17:54:25 +01004406 trycmd->tcd_caught = TRUE;
4407 trycmd->tcd_did_throw = FALSE;
4408
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004409 did_emsg = got_int = did_throw = FALSE;
Bram Moolenaar1430cee2021-01-17 19:20:32 +01004410 force_abort = need_rethrow = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004411 catch_exception(current_exception);
4412 }
4413 break;
4414
Bram Moolenaarc150c092021-02-13 15:02:46 +01004415 case ISN_TRYCONT:
4416 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004417 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004418 trycont_T *trycont = &iptr->isn_arg.trycont;
4419 int i;
4420 trycmd_T *trycmd;
4421 int iidx = trycont->tct_where;
4422
4423 if (trystack->ga_len < trycont->tct_levels)
4424 {
4425 siemsg("TRYCONT: expected %d levels, found %d",
4426 trycont->tct_levels, trystack->ga_len);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004427 goto theend;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004428 }
4429 // Make :endtry jump to any outer try block and the last
4430 // :endtry inside the loop to the loop start.
4431 for (i = trycont->tct_levels; i > 0; --i)
4432 {
4433 trycmd = ((trycmd_T *)trystack->ga_data)
4434 + trystack->ga_len - i;
Bram Moolenaar2e34c342021-03-14 12:13:33 +01004435 // Add one to tcd_cont to be able to jump to
4436 // instruction with index zero.
4437 trycmd->tcd_cont = iidx + 1;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01004438 iidx = trycmd->tcd_finally_idx == 0
4439 ? trycmd->tcd_endtry_idx : trycmd->tcd_finally_idx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004440 }
4441 // jump to :finally or :endtry of current try statement
Bram Moolenaar4c137212021-04-19 16:48:48 +02004442 ectx->ec_iidx = iidx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004443 }
4444 break;
4445
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01004446 case ISN_FINALLY:
4447 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004448 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01004449 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
4450 + trystack->ga_len - 1;
4451
4452 // Reset the index to avoid a return statement jumps here
4453 // again.
4454 trycmd->tcd_finally_idx = 0;
4455 break;
4456 }
4457
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004458 // end of ":try" block
4459 case ISN_ENDTRY:
4460 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004461 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar06651632022-04-27 17:54:25 +01004462 trycmd_T *trycmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004463
Bram Moolenaar06651632022-04-27 17:54:25 +01004464 --trystack->ga_len;
4465 --trylevel;
4466 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len;
4467 if (trycmd->tcd_did_throw)
4468 did_throw = TRUE;
4469 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004470 {
Bram Moolenaar06651632022-04-27 17:54:25 +01004471 // discard the exception
4472 if (caught_stack == current_exception)
4473 caught_stack = caught_stack->caught;
4474 discard_current_exception();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004475 }
Bram Moolenaar06651632022-04-27 17:54:25 +01004476
4477 if (trycmd->tcd_return)
4478 goto func_return;
4479
4480 while (ectx->ec_stack.ga_len > trycmd->tcd_stack_len)
4481 {
4482 --ectx->ec_stack.ga_len;
4483 clear_tv(STACK_TV_BOT(0));
4484 }
4485 if (trycmd->tcd_cont != 0)
4486 // handling :continue: jump to outer try block or
4487 // start of the loop
4488 ectx->ec_iidx = trycmd->tcd_cont - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004489 }
4490 break;
4491
4492 case ISN_THROW:
Bram Moolenaar8f81b222021-01-14 21:47:06 +01004493 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004494 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02004495
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004496 if (trystack->ga_len == 0 && trylevel == 0 && emsg_silent)
4497 {
4498 // throwing an exception while using "silent!" causes
4499 // the function to abort but not display an error.
4500 tv = STACK_TV_BOT(-1);
4501 clear_tv(tv);
4502 tv->v_type = VAR_NUMBER;
4503 tv->vval.v_number = 0;
4504 goto done;
4505 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004506 --ectx->ec_stack.ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004507 tv = STACK_TV_BOT(0);
4508 if (tv->vval.v_string == NULL
4509 || *skipwhite(tv->vval.v_string) == NUL)
4510 {
4511 vim_free(tv->vval.v_string);
4512 SOURCING_LNUM = iptr->isn_lnum;
4513 emsg(_(e_throw_with_empty_string));
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004514 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004515 }
4516
4517 // Inside a "catch" we need to first discard the caught
4518 // exception.
4519 if (trystack->ga_len > 0)
4520 {
4521 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
4522 + trystack->ga_len - 1;
4523 if (trycmd->tcd_caught && current_exception != NULL)
4524 {
4525 // discard the exception
4526 if (caught_stack == current_exception)
4527 caught_stack = caught_stack->caught;
4528 discard_current_exception();
4529 trycmd->tcd_caught = FALSE;
4530 }
4531 }
4532
Bram Moolenaar90a57162022-02-12 14:23:17 +00004533 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004534 if (throw_exception(tv->vval.v_string, ET_USER, NULL)
4535 == FAIL)
4536 {
4537 vim_free(tv->vval.v_string);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004538 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004539 }
4540 did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004541 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004542 break;
4543
4544 // compare with special values
4545 case ISN_COMPAREBOOL:
4546 case ISN_COMPARESPECIAL:
4547 {
4548 typval_T *tv1 = STACK_TV_BOT(-2);
4549 typval_T *tv2 = STACK_TV_BOT(-1);
4550 varnumber_T arg1 = tv1->vval.v_number;
4551 varnumber_T arg2 = tv2->vval.v_number;
4552 int res;
4553
Bram Moolenaar06651632022-04-27 17:54:25 +01004554 if (iptr->isn_arg.op.op_type == EXPR_EQUAL)
4555 res = arg1 == arg2;
4556 else
4557 res = arg1 != arg2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004558
Bram Moolenaar4c137212021-04-19 16:48:48 +02004559 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004560 tv1->v_type = VAR_BOOL;
4561 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4562 }
4563 break;
4564
Bram Moolenaar7a222242022-03-01 19:23:24 +00004565 case ISN_COMPARENULL:
4566 {
4567 typval_T *tv1 = STACK_TV_BOT(-2);
4568 typval_T *tv2 = STACK_TV_BOT(-1);
4569 int res;
4570
4571 res = typval_compare_null(tv1, tv2);
4572 if (res == MAYBE)
4573 goto on_error;
4574 if (iptr->isn_arg.op.op_type == EXPR_NEQUAL)
4575 res = !res;
4576 clear_tv(tv1);
4577 clear_tv(tv2);
4578 --ectx->ec_stack.ga_len;
4579 tv1->v_type = VAR_BOOL;
4580 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4581 }
4582 break;
4583
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004584 // Operation with two number arguments
4585 case ISN_OPNR:
4586 case ISN_COMPARENR:
4587 {
4588 typval_T *tv1 = STACK_TV_BOT(-2);
4589 typval_T *tv2 = STACK_TV_BOT(-1);
4590 varnumber_T arg1 = tv1->vval.v_number;
4591 varnumber_T arg2 = tv2->vval.v_number;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004592 varnumber_T res = 0;
4593 int div_zero = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004594
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004595 if (iptr->isn_arg.op.op_type == EXPR_LSHIFT
4596 || iptr->isn_arg.op.op_type == EXPR_RSHIFT)
4597 {
4598 if (arg2 < 0)
4599 {
4600 SOURCING_LNUM = iptr->isn_lnum;
dundargocc57b5bc2022-11-02 13:30:51 +00004601 emsg(_(e_bitshift_ops_must_be_positive));
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004602 goto on_error;
4603 }
4604 }
4605
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004606 switch (iptr->isn_arg.op.op_type)
4607 {
4608 case EXPR_MULT: res = arg1 * arg2; break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004609 case EXPR_DIV: if (arg2 == 0)
4610 div_zero = TRUE;
4611 else
4612 res = arg1 / arg2;
4613 break;
4614 case EXPR_REM: if (arg2 == 0)
4615 div_zero = TRUE;
4616 else
4617 res = arg1 % arg2;
4618 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004619 case EXPR_SUB: res = arg1 - arg2; break;
4620 case EXPR_ADD: res = arg1 + arg2; break;
4621
4622 case EXPR_EQUAL: res = arg1 == arg2; break;
4623 case EXPR_NEQUAL: res = arg1 != arg2; break;
4624 case EXPR_GREATER: res = arg1 > arg2; break;
4625 case EXPR_GEQUAL: res = arg1 >= arg2; break;
4626 case EXPR_SMALLER: res = arg1 < arg2; break;
4627 case EXPR_SEQUAL: res = arg1 <= arg2; break;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004628 case EXPR_LSHIFT: if (arg2 > MAX_LSHIFT_BITS)
4629 res = 0;
4630 else
Bram Moolenaar68e64d22022-05-22 22:07:52 +01004631 res = (uvarnumber_T)arg1 << arg2;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004632 break;
4633 case EXPR_RSHIFT: if (arg2 > MAX_LSHIFT_BITS)
4634 res = 0;
4635 else
Bram Moolenaar338bf582022-05-22 20:16:32 +01004636 res = (uvarnumber_T)arg1 >> arg2;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004637 break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004638 default: break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004639 }
4640
Bram Moolenaar4c137212021-04-19 16:48:48 +02004641 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004642 if (iptr->isn_type == ISN_COMPARENR)
4643 {
4644 tv1->v_type = VAR_BOOL;
4645 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4646 }
4647 else
4648 tv1->vval.v_number = res;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004649 if (div_zero)
4650 {
4651 SOURCING_LNUM = iptr->isn_lnum;
4652 emsg(_(e_divide_by_zero));
4653 goto on_error;
4654 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004655 }
4656 break;
4657
4658 // Computation with two float arguments
4659 case ISN_OPFLOAT:
4660 case ISN_COMPAREFLOAT:
4661 {
4662 typval_T *tv1 = STACK_TV_BOT(-2);
4663 typval_T *tv2 = STACK_TV_BOT(-1);
4664 float_T arg1 = tv1->vval.v_float;
4665 float_T arg2 = tv2->vval.v_float;
4666 float_T res = 0;
4667 int cmp = FALSE;
4668
4669 switch (iptr->isn_arg.op.op_type)
4670 {
4671 case EXPR_MULT: res = arg1 * arg2; break;
4672 case EXPR_DIV: res = arg1 / arg2; break;
4673 case EXPR_SUB: res = arg1 - arg2; break;
4674 case EXPR_ADD: res = arg1 + arg2; break;
4675
4676 case EXPR_EQUAL: cmp = arg1 == arg2; break;
4677 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
4678 case EXPR_GREATER: cmp = arg1 > arg2; break;
4679 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
4680 case EXPR_SMALLER: cmp = arg1 < arg2; break;
4681 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
4682 default: cmp = 0; break;
4683 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004684 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004685 if (iptr->isn_type == ISN_COMPAREFLOAT)
4686 {
4687 tv1->v_type = VAR_BOOL;
4688 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
4689 }
4690 else
4691 tv1->vval.v_float = res;
4692 }
4693 break;
4694
4695 case ISN_COMPARELIST:
Bram Moolenaar265f8112021-12-19 12:33:05 +00004696 case ISN_COMPAREDICT:
4697 case ISN_COMPAREFUNC:
4698 case ISN_COMPARESTRING:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004699 case ISN_COMPAREBLOB:
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00004700 case ISN_COMPARECLASS:
4701 case ISN_COMPAREOBJECT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004702 {
4703 typval_T *tv1 = STACK_TV_BOT(-2);
4704 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar265f8112021-12-19 12:33:05 +00004705 exprtype_T exprtype = iptr->isn_arg.op.op_type;
4706 int ic = iptr->isn_arg.op.op_ic;
4707 int res = FALSE;
4708 int status = OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004709
Bram Moolenaar265f8112021-12-19 12:33:05 +00004710 SOURCING_LNUM = iptr->isn_lnum;
4711 if (iptr->isn_type == ISN_COMPARELIST)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004712 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00004713 status = typval_compare_list(tv1, tv2,
4714 exprtype, ic, &res);
4715 }
4716 else if (iptr->isn_type == ISN_COMPAREDICT)
4717 {
4718 status = typval_compare_dict(tv1, tv2,
4719 exprtype, ic, &res);
4720 }
4721 else if (iptr->isn_type == ISN_COMPAREFUNC)
4722 {
4723 status = typval_compare_func(tv1, tv2,
4724 exprtype, ic, &res);
4725 }
4726 else if (iptr->isn_type == ISN_COMPARESTRING)
4727 {
4728 status = typval_compare_string(tv1, tv2,
4729 exprtype, ic, &res);
4730 }
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00004731 else if (iptr->isn_type == ISN_COMPAREBLOB)
Bram Moolenaar265f8112021-12-19 12:33:05 +00004732 {
4733 status = typval_compare_blob(tv1, tv2, exprtype, &res);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004734 }
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00004735 else if (iptr->isn_type == ISN_COMPARECLASS)
4736 {
Bram Moolenaar03ff0c62023-01-02 20:38:01 +00004737 status = typval_compare_class(tv1, tv2,
4738 exprtype, FALSE, &res);
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00004739 }
4740 else // ISN_COMPAREOBJECT
4741 {
4742 status = typval_compare_object(tv1, tv2,
Bram Moolenaar03ff0c62023-01-02 20:38:01 +00004743 exprtype, FALSE, &res);
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00004744 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004745 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004746 clear_tv(tv1);
4747 clear_tv(tv2);
4748 tv1->v_type = VAR_BOOL;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004749 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4750 if (status == FAIL)
4751 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004752 }
4753 break;
4754
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004755 case ISN_COMPAREANY:
4756 {
4757 typval_T *tv1 = STACK_TV_BOT(-2);
4758 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar657137c2021-01-09 15:45:23 +01004759 exprtype_T exprtype = iptr->isn_arg.op.op_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004760 int ic = iptr->isn_arg.op.op_ic;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004761 int status;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004762
Bram Moolenaareb26f432020-09-14 16:50:05 +02004763 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004764 status = typval_compare(tv1, tv2, exprtype, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004765 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004766 --ectx->ec_stack.ga_len;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004767 if (status == FAIL)
4768 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004769 }
4770 break;
4771
4772 case ISN_ADDLIST:
4773 case ISN_ADDBLOB:
4774 {
4775 typval_T *tv1 = STACK_TV_BOT(-2);
4776 typval_T *tv2 = STACK_TV_BOT(-1);
4777
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004778 // add two lists or blobs
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004779 if (iptr->isn_type == ISN_ADDLIST)
Bram Moolenaar07802042021-09-09 23:01:14 +02004780 {
4781 if (iptr->isn_arg.op.op_type == EXPR_APPEND
4782 && tv1->vval.v_list != NULL)
4783 list_extend(tv1->vval.v_list, tv2->vval.v_list,
4784 NULL);
4785 else
4786 eval_addlist(tv1, tv2);
4787 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004788 else
4789 eval_addblob(tv1, tv2);
4790 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004791 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004792 }
4793 break;
4794
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004795 case ISN_LISTAPPEND:
4796 {
4797 typval_T *tv1 = STACK_TV_BOT(-2);
4798 typval_T *tv2 = STACK_TV_BOT(-1);
4799 list_T *l = tv1->vval.v_list;
4800
4801 // add an item to a list
Bram Moolenaar1f4a3452022-01-01 18:29:21 +00004802 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004803 if (l == NULL)
4804 {
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004805 emsg(_(e_cannot_add_to_null_list));
4806 goto on_error;
4807 }
Bram Moolenaar1f4a3452022-01-01 18:29:21 +00004808 if (value_check_lock(l->lv_lock, NULL, FALSE))
4809 goto on_error;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004810 if (list_append_tv(l, tv2) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004811 goto theend;
Bram Moolenaar955347c2020-10-19 23:01:46 +02004812 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004813 --ectx->ec_stack.ga_len;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004814 }
4815 break;
4816
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02004817 case ISN_BLOBAPPEND:
4818 {
4819 typval_T *tv1 = STACK_TV_BOT(-2);
4820 typval_T *tv2 = STACK_TV_BOT(-1);
4821 blob_T *b = tv1->vval.v_blob;
4822 int error = FALSE;
4823 varnumber_T n;
4824
4825 // add a number to a blob
4826 if (b == NULL)
4827 {
4828 SOURCING_LNUM = iptr->isn_lnum;
4829 emsg(_(e_cannot_add_to_null_blob));
4830 goto on_error;
4831 }
4832 n = tv_get_number_chk(tv2, &error);
4833 if (error)
4834 goto on_error;
4835 ga_append(&b->bv_ga, (int)n);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004836 --ectx->ec_stack.ga_len;
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02004837 }
4838 break;
4839
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004840 // Computation with two arguments of unknown type
4841 case ISN_OPANY:
4842 {
4843 typval_T *tv1 = STACK_TV_BOT(-2);
4844 typval_T *tv2 = STACK_TV_BOT(-1);
4845 varnumber_T n1, n2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004846 float_T f1 = 0, f2 = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004847 int error = FALSE;
4848
4849 if (iptr->isn_arg.op.op_type == EXPR_ADD)
4850 {
4851 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
4852 {
4853 eval_addlist(tv1, tv2);
4854 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004855 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004856 break;
4857 }
4858 else if (tv1->v_type == VAR_BLOB
4859 && tv2->v_type == VAR_BLOB)
4860 {
4861 eval_addblob(tv1, tv2);
4862 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004863 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004864 break;
4865 }
4866 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004867 if (tv1->v_type == VAR_FLOAT)
4868 {
4869 f1 = tv1->vval.v_float;
4870 n1 = 0;
4871 }
4872 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004873 {
Bram Moolenaarf665e972020-12-05 19:17:16 +01004874 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004875 n1 = tv_get_number_chk(tv1, &error);
4876 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02004877 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004878 if (tv2->v_type == VAR_FLOAT)
4879 f1 = n1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004880 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004881 if (tv2->v_type == VAR_FLOAT)
4882 {
4883 f2 = tv2->vval.v_float;
4884 n2 = 0;
4885 }
4886 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004887 {
4888 n2 = tv_get_number_chk(tv2, &error);
4889 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02004890 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004891 if (tv1->v_type == VAR_FLOAT)
4892 f2 = n2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004893 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004894 // if there is a float on either side the result is a float
4895 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
4896 {
4897 switch (iptr->isn_arg.op.op_type)
4898 {
4899 case EXPR_MULT: f1 = f1 * f2; break;
4900 case EXPR_DIV: f1 = f1 / f2; break;
4901 case EXPR_SUB: f1 = f1 - f2; break;
4902 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004903 default: SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004904 emsg(_(e_cannot_use_percent_with_float));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004905 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004906 }
4907 clear_tv(tv1);
4908 clear_tv(tv2);
4909 tv1->v_type = VAR_FLOAT;
4910 tv1->vval.v_float = f1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004911 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004912 }
4913 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004914 {
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004915 int failed = FALSE;
4916
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004917 switch (iptr->isn_arg.op.op_type)
4918 {
4919 case EXPR_MULT: n1 = n1 * n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004920 case EXPR_DIV: n1 = num_divide(n1, n2, &failed);
4921 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01004922 goto on_error;
4923 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004924 case EXPR_SUB: n1 = n1 - n2; break;
4925 case EXPR_ADD: n1 = n1 + n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004926 default: n1 = num_modulus(n1, n2, &failed);
4927 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01004928 goto on_error;
4929 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004930 }
4931 clear_tv(tv1);
4932 clear_tv(tv2);
4933 tv1->v_type = VAR_NUMBER;
4934 tv1->vval.v_number = n1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004935 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004936 }
4937 }
4938 break;
4939
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004940 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004941 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004942 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004943 int is_slice = iptr->isn_type == ISN_STRSLICE;
4944 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004945 char_u *res;
4946
4947 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004948 // string slice: string is at stack-3, first index at
4949 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004950 if (is_slice)
4951 {
4952 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004953 n1 = tv->vval.v_number;
4954 }
4955
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004956 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004957 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004958
Bram Moolenaar4c137212021-04-19 16:48:48 +02004959 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004960 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004961 if (is_slice)
4962 // Slice: Select the characters from the string
Bram Moolenaar6601b622021-01-13 21:47:15 +01004963 res = string_slice(tv->vval.v_string, n1, n2, FALSE);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004964 else
4965 // Index: The resulting variable is a string of a
Bram Moolenaar0289a092021-03-14 18:40:19 +01004966 // single character (including composing characters).
4967 // If the index is too big or negative the result is
4968 // empty.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004969 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004970 vim_free(tv->vval.v_string);
4971 tv->vval.v_string = res;
4972 }
4973 break;
4974
4975 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02004976 case ISN_LISTSLICE:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004977 case ISN_BLOBINDEX:
4978 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004979 {
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004980 int is_slice = iptr->isn_type == ISN_LISTSLICE
4981 || iptr->isn_type == ISN_BLOBSLICE;
4982 int is_blob = iptr->isn_type == ISN_BLOBINDEX
4983 || iptr->isn_type == ISN_BLOBSLICE;
Bram Moolenaared591872020-08-15 22:14:53 +02004984 varnumber_T n1, n2;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004985 typval_T *val_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004986
4987 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02004988 // list slice: list is at stack-3, indexes at stack-2 and
4989 // stack-1
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004990 // Same for blob.
4991 val_tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004992
4993 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02004994 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004995 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02004996
4997 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004998 {
Bram Moolenaared591872020-08-15 22:14:53 +02004999 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02005000 n1 = tv->vval.v_number;
5001 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005002 }
Bram Moolenaared591872020-08-15 22:14:53 +02005003
Bram Moolenaar4c137212021-04-19 16:48:48 +02005004 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02005005 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02005006 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02005007 if (is_blob)
5008 {
5009 if (blob_slice_or_index(val_tv->vval.v_blob, is_slice,
5010 n1, n2, FALSE, tv) == FAIL)
5011 goto on_error;
5012 }
5013 else
5014 {
5015 if (list_slice_or_index(val_tv->vval.v_list, is_slice,
5016 n1, n2, FALSE, tv, TRUE) == FAIL)
5017 goto on_error;
5018 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005019 }
5020 break;
5021
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005022 case ISN_ANYINDEX:
5023 case ISN_ANYSLICE:
5024 {
5025 int is_slice = iptr->isn_type == ISN_ANYSLICE;
5026 typval_T *var1, *var2;
5027 int res;
5028
5029 // index: composite is at stack-2, index at stack-1
5030 // slice: composite is at stack-3, indexes at stack-2 and
5031 // stack-1
5032 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02005033 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005034 if (check_can_index(tv, TRUE, TRUE) == FAIL)
5035 goto on_error;
5036 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
5037 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
Bram Moolenaar6601b622021-01-13 21:47:15 +01005038 res = eval_index_inner(tv, is_slice, var1, var2,
5039 FALSE, NULL, -1, TRUE);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005040 clear_tv(var1);
5041 if (is_slice)
5042 clear_tv(var2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005043 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005044 if (res == FAIL)
5045 goto on_error;
5046 }
5047 break;
5048
Bram Moolenaar9af78762020-06-16 11:34:42 +02005049 case ISN_SLICE:
5050 {
5051 list_T *list;
5052 int count = iptr->isn_arg.number;
5053
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02005054 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02005055 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02005056 list = tv->vval.v_list;
5057
5058 // no error for short list, expect it to be checked earlier
5059 if (list != NULL && list->lv_len >= count)
5060 {
5061 list_T *newlist = list_slice(list,
5062 count, list->lv_len - 1);
5063
5064 if (newlist != NULL)
5065 {
5066 list_unref(list);
5067 tv->vval.v_list = newlist;
5068 ++newlist->lv_refcount;
5069 }
5070 }
5071 }
5072 break;
5073
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005074 case ISN_GETITEM:
5075 {
5076 listitem_T *li;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02005077 getitem_T *gi = &iptr->isn_arg.getitem;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005078
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02005079 // Get list item: list is at stack-1, push item.
5080 // List type and length is checked for when compiling.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02005081 tv = STACK_TV_BOT(-1 - gi->gi_with_op);
5082 li = list_find(tv->vval.v_list, gi->gi_index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005083
Bram Moolenaar35578162021-08-02 19:10:38 +02005084 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005085 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005086 ++ectx->ec_stack.ga_len;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005087 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
Bram Moolenaarf785aa12021-02-11 21:19:34 +01005088
5089 // Useful when used in unpack assignment. Reset at
5090 // ISN_DROP.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02005091 ectx->ec_where.wt_index = gi->gi_index + 1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005092 ectx->ec_where.wt_variable = TRUE;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005093 }
5094 break;
5095
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005096 case ISN_MEMBER:
5097 {
5098 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005099 char_u *key;
5100 dictitem_T *di;
5101
5102 // dict member: dict is at stack-2, key at stack-1
5103 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02005104 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005105 dict = tv->vval.v_dict;
5106
5107 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02005108 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005109 key = tv->vval.v_string;
Bram Moolenaar086fc9a2020-10-30 18:33:02 +01005110 if (key == NULL)
5111 key = (char_u *)"";
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02005112
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005113 if ((di = dict_find(dict, key, -1)) == NULL)
5114 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005115 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00005116 semsg(_(e_key_not_present_in_dictionary_str), key);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01005117
5118 // If :silent! is used we will continue, make sure the
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005119 // stack contents makes sense and the dict stack is
5120 // updated.
Bram Moolenaar4029cab2020-12-05 18:13:27 +01005121 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005122 --ectx->ec_stack.ga_len;
Bram Moolenaar4029cab2020-12-05 18:13:27 +01005123 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005124 (void) dict_stack_save(tv);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01005125 tv->v_type = VAR_NUMBER;
5126 tv->vval.v_number = 0;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01005127 goto on_fatal_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005128 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005129 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005130 --ectx->ec_stack.ga_len;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005131 // Put the dict used on the dict stack, it might be used by
5132 // a dict function later.
Bram Moolenaar50788ef2020-07-05 16:51:26 +02005133 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005134 if (dict_stack_save(tv) == FAIL)
5135 goto on_fatal_error;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02005136 copy_tv(&di->di_tv, tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005137 }
5138 break;
5139
5140 // dict member with string key
5141 case ISN_STRINGMEMBER:
5142 {
5143 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005144 dictitem_T *di;
5145
5146 tv = STACK_TV_BOT(-1);
5147 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
5148 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005149 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005150 emsg(_(e_dictionary_required));
Bram Moolenaard032f342020-07-18 18:13:02 +02005151 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005152 }
5153 dict = tv->vval.v_dict;
5154
5155 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
5156 == NULL)
5157 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005158 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00005159 semsg(_(e_key_not_present_in_dictionary_str),
Bram Moolenaar381692b2022-02-02 20:01:27 +00005160 iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02005161 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005162 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005163 // Put the dict used on the dict stack, it might be used by
5164 // a dict function later.
5165 if (dict_stack_save(tv) == FAIL)
5166 goto on_fatal_error;
5167
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005168 copy_tv(&di->di_tv, tv);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005169 }
5170 break;
5171
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005172 case ISN_GET_OBJ_MEMBER:
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005173 {
5174 tv = STACK_TV_BOT(-1);
5175 if (tv->v_type != VAR_OBJECT)
5176 {
5177 SOURCING_LNUM = iptr->isn_lnum;
5178 garray_T type_list;
5179 ga_init2(&type_list, sizeof(type_T *), 10);
5180 type_T *type = typval2type(tv, get_copyID(),
5181 &type_list, TVTT_DO_MEMBER);
5182 char *tofree = NULL;
5183 char *typename = type_name(type, &tofree);
5184 semsg(_(e_object_required_found_str), typename);
5185 vim_free(tofree);
5186 clear_type_list(&type_list);
5187 goto on_error;
5188 }
5189 int idx = iptr->isn_arg.number;
5190 object_T *obj = tv->vval.v_object;
5191 // the members are located right after the object struct
5192 typval_T *mtv = ((typval_T *)(obj + 1)) + idx;
Bram Moolenaar590162c2022-12-24 21:24:06 +00005193 copy_tv(mtv, tv);
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005194
5195 // Unreference the object after getting the member, it may
5196 // be freed.
5197 object_unref(obj);
5198 }
5199 break;
5200
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005201 case ISN_STORE_THIS:
5202 {
5203 int idx = iptr->isn_arg.number;
5204 object_T *obj = STACK_TV_VAR(0)->vval.v_object;
5205 // the members are located right after the object struct
5206 typval_T *mtv = ((typval_T *)(obj + 1)) + idx;
5207 clear_tv(mtv);
5208 *mtv = *STACK_TV_BOT(-1);
5209 --ectx->ec_stack.ga_len;
5210 }
5211 break;
5212
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005213 case ISN_CLEARDICT:
5214 dict_stack_drop();
5215 break;
5216
5217 case ISN_USEDICT:
5218 {
5219 typval_T *dict_tv = dict_stack_get_tv();
5220
5221 // Turn "dict.Func" into a partial for "Func" bound to
5222 // "dict". Don't do this when "Func" is already a partial
5223 // that was bound explicitly (pt_auto is FALSE).
5224 tv = STACK_TV_BOT(-1);
5225 if (dict_tv != NULL
5226 && dict_tv->v_type == VAR_DICT
5227 && dict_tv->vval.v_dict != NULL
5228 && (tv->v_type == VAR_FUNC
5229 || (tv->v_type == VAR_PARTIAL
5230 && (tv->vval.v_partial->pt_auto
5231 || tv->vval.v_partial->pt_dict == NULL))))
5232 dict_tv->vval.v_dict =
5233 make_partial(dict_tv->vval.v_dict, tv);
5234 dict_stack_drop();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005235 }
5236 break;
5237
5238 case ISN_NEGATENR:
5239 tv = STACK_TV_BOT(-1);
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00005240 // CHECKTYPE should have checked the variable type
Bram Moolenaarc58164c2020-03-29 18:40:30 +02005241 if (tv->v_type == VAR_FLOAT)
5242 tv->vval.v_float = -tv->vval.v_float;
5243 else
Bram Moolenaarc58164c2020-03-29 18:40:30 +02005244 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005245 break;
5246
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005247 case ISN_CHECKTYPE:
5248 {
5249 checktype_T *ct = &iptr->isn_arg.type;
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01005250 int save_wt_variable = ectx->ec_where.wt_variable;
Bram Moolenaarb1040dc2022-05-18 11:00:48 +01005251 int r;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005252
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01005253 tv = STACK_TV_BOT((int)ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02005254 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005255 if (!ectx->ec_where.wt_variable)
5256 ectx->ec_where.wt_index = ct->ct_arg_idx;
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01005257 ectx->ec_where.wt_variable = ct->ct_is_var;
Bram Moolenaarb1040dc2022-05-18 11:00:48 +01005258 r = check_typval_type(ct->ct_type, tv, ectx->ec_where);
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01005259 ectx->ec_where.wt_variable = save_wt_variable;
Bram Moolenaarb1040dc2022-05-18 11:00:48 +01005260 if (r == FAIL)
5261 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005262 if (!ectx->ec_where.wt_variable)
5263 ectx->ec_where.wt_index = 0;
Bram Moolenaar5e654232020-09-16 15:22:00 +02005264
5265 // number 0 is FALSE, number 1 is TRUE
5266 if (tv->v_type == VAR_NUMBER
5267 && ct->ct_type->tt_type == VAR_BOOL
5268 && (tv->vval.v_number == 0
5269 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005270 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02005271 tv->v_type = VAR_BOOL;
5272 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02005273 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005274 }
5275 }
5276 break;
5277
Bram Moolenaar9af78762020-06-16 11:34:42 +02005278 case ISN_CHECKLEN:
5279 {
5280 int min_len = iptr->isn_arg.checklen.cl_min_len;
5281 list_T *list = NULL;
5282
5283 tv = STACK_TV_BOT(-1);
5284 if (tv->v_type == VAR_LIST)
5285 list = tv->vval.v_list;
5286 if (list == NULL || list->lv_len < min_len
5287 || (list->lv_len > min_len
5288 && !iptr->isn_arg.checklen.cl_more_OK))
5289 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005290 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005291 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02005292 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02005293 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02005294 }
5295 }
5296 break;
5297
Bram Moolenaaraa210a32021-01-02 15:41:03 +01005298 case ISN_SETTYPE:
Bram Moolenaar381692b2022-02-02 20:01:27 +00005299 set_tv_type(STACK_TV_BOT(-1), iptr->isn_arg.type.ct_type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01005300 break;
5301
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005302 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005303 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005304 {
5305 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005306 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005307
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005308 if (iptr->isn_type == ISN_2BOOL)
5309 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005310 tv = STACK_TV_BOT(iptr->isn_arg.tobool.offset);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005311 n = tv2bool(tv);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005312 if (iptr->isn_arg.tobool.invert)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005313 n = !n;
5314 }
5315 else
5316 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005317 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005318 SOURCING_LNUM = iptr->isn_lnum;
5319 n = tv_get_bool_chk(tv, &error);
5320 if (error)
5321 goto on_error;
5322 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005323 clear_tv(tv);
5324 tv->v_type = VAR_BOOL;
5325 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
5326 }
5327 break;
5328
5329 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02005330 case ISN_2STRING_ANY:
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01005331 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005332 if (do_2string(STACK_TV_BOT(iptr->isn_arg.tostring.offset),
5333 iptr->isn_type == ISN_2STRING_ANY,
5334 iptr->isn_arg.tostring.tolerant) == FAIL)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01005335 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005336 break;
5337
Bram Moolenaar08597872020-12-10 19:43:40 +01005338 case ISN_RANGE:
5339 {
5340 exarg_T ea;
5341 char *errormsg;
5342
Bram Moolenaarece0b872021-01-08 20:40:45 +01005343 ea.line2 = 0;
Bram Moolenaard1510ee2021-01-04 16:15:58 +01005344 ea.addr_count = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01005345 ea.addr_type = ADDR_LINES;
5346 ea.cmd = iptr->isn_arg.string;
Bram Moolenaarece0b872021-01-08 20:40:45 +01005347 ea.skip = FALSE;
Bram Moolenaar08597872020-12-10 19:43:40 +01005348 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaarece0b872021-01-08 20:40:45 +01005349 goto on_error;
Bram Moolenaara28639e2021-01-19 22:48:09 +01005350
Bram Moolenaar35578162021-08-02 19:10:38 +02005351 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005352 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005353 ++ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01005354 tv = STACK_TV_BOT(-1);
5355 tv->v_type = VAR_NUMBER;
5356 tv->v_lock = 0;
Bram Moolenaar06651632022-04-27 17:54:25 +01005357 tv->vval.v_number = ea.line2;
Bram Moolenaar08597872020-12-10 19:43:40 +01005358 }
5359 break;
5360
Bram Moolenaarc3516f72020-09-08 22:45:35 +02005361 case ISN_PUT:
5362 {
5363 int regname = iptr->isn_arg.put.put_regname;
5364 linenr_T lnum = iptr->isn_arg.put.put_lnum;
5365 char_u *expr = NULL;
5366 int dir = FORWARD;
5367
Bram Moolenaar08597872020-12-10 19:43:40 +01005368 if (lnum < -2)
5369 {
5370 // line number was put on the stack by ISN_RANGE
5371 tv = STACK_TV_BOT(-1);
5372 curwin->w_cursor.lnum = tv->vval.v_number;
5373 if (lnum == LNUM_VARIABLE_RANGE_ABOVE)
5374 dir = BACKWARD;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005375 --ectx->ec_stack.ga_len;
Bram Moolenaar08597872020-12-10 19:43:40 +01005376 }
5377 else if (lnum == -2)
Bram Moolenaarc3516f72020-09-08 22:45:35 +02005378 // :put! above cursor
5379 dir = BACKWARD;
5380 else if (lnum >= 0)
Bram Moolenaar4e713ba2022-02-07 15:31:37 +00005381 {
5382 curwin->w_cursor.lnum = lnum;
5383 if (lnum == 0)
5384 // check_cursor() below will move to line 1
5385 dir = BACKWARD;
5386 }
Bram Moolenaara28639e2021-01-19 22:48:09 +01005387
5388 if (regname == '=')
5389 {
5390 tv = STACK_TV_BOT(-1);
5391 if (tv->v_type == VAR_STRING)
5392 expr = tv->vval.v_string;
5393 else
5394 {
5395 expr = typval2string(tv, TRUE); // allocates value
5396 clear_tv(tv);
5397 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005398 --ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01005399 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02005400 check_cursor();
5401 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
5402 vim_free(expr);
5403 }
5404 break;
5405
Bram Moolenaar02194d22020-10-24 23:08:38 +02005406 case ISN_CMDMOD:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005407 ectx->ec_funclocal.floc_save_cmdmod = cmdmod;
5408 ectx->ec_funclocal.floc_restore_cmdmod = TRUE;
5409 ectx->ec_funclocal.floc_restore_cmdmod_stacklen =
5410 ectx->ec_stack.ga_len;
Bram Moolenaar02194d22020-10-24 23:08:38 +02005411 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
5412 apply_cmdmod(&cmdmod);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02005413 break;
5414
Bram Moolenaar02194d22020-10-24 23:08:38 +02005415 case ISN_CMDMOD_REV:
5416 // filter regprog is owned by the instruction, don't free it
5417 cmdmod.cmod_filter_regmatch.regprog = NULL;
5418 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005419 cmdmod = ectx->ec_funclocal.floc_save_cmdmod;
5420 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02005421 break;
5422
Bram Moolenaar792f7862020-11-23 08:31:18 +01005423 case ISN_UNPACK:
5424 {
5425 int count = iptr->isn_arg.unpack.unp_count;
5426 int semicolon = iptr->isn_arg.unpack.unp_semicolon;
5427 list_T *l;
5428 listitem_T *li;
5429 int i;
5430
5431 // Check there is a valid list to unpack.
5432 tv = STACK_TV_BOT(-1);
5433 if (tv->v_type != VAR_LIST)
5434 {
5435 SOURCING_LNUM = iptr->isn_lnum;
5436 emsg(_(e_for_argument_must_be_sequence_of_lists));
5437 goto on_error;
5438 }
5439 l = tv->vval.v_list;
5440 if (l == NULL
5441 || l->lv_len < (semicolon ? count - 1 : count))
5442 {
5443 SOURCING_LNUM = iptr->isn_lnum;
5444 emsg(_(e_list_value_does_not_have_enough_items));
5445 goto on_error;
5446 }
5447 else if (!semicolon && l->lv_len > count)
5448 {
5449 SOURCING_LNUM = iptr->isn_lnum;
5450 emsg(_(e_list_value_has_more_items_than_targets));
5451 goto on_error;
5452 }
5453
5454 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar35578162021-08-02 19:10:38 +02005455 if (GA_GROW_FAILS(&ectx->ec_stack, count - 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005456 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005457 ectx->ec_stack.ga_len += count - 1;
Bram Moolenaar792f7862020-11-23 08:31:18 +01005458
5459 // Variable after semicolon gets a list with the remaining
5460 // items.
5461 if (semicolon)
5462 {
5463 list_T *rem_list =
5464 list_alloc_with_items(l->lv_len - count + 1);
5465
5466 if (rem_list == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005467 goto theend;
Bram Moolenaar792f7862020-11-23 08:31:18 +01005468 tv = STACK_TV_BOT(-count);
5469 tv->vval.v_list = rem_list;
5470 ++rem_list->lv_refcount;
5471 tv->v_lock = 0;
5472 li = l->lv_first;
5473 for (i = 0; i < count - 1; ++i)
5474 li = li->li_next;
5475 for (i = 0; li != NULL; ++i)
5476 {
Bram Moolenaar61efa162022-03-18 13:10:48 +00005477 typval_T tvcopy;
5478
5479 copy_tv(&li->li_tv, &tvcopy);
5480 list_set_item(rem_list, i, &tvcopy);
Bram Moolenaar792f7862020-11-23 08:31:18 +01005481 li = li->li_next;
5482 }
5483 --count;
5484 }
5485
5486 // Produce the values in reverse order, first item last.
5487 li = l->lv_first;
5488 for (i = 0; i < count; ++i)
5489 {
5490 tv = STACK_TV_BOT(-i - 1);
5491 copy_tv(&li->li_tv, tv);
5492 li = li->li_next;
5493 }
5494
5495 list_unref(l);
5496 }
5497 break;
5498
Bram Moolenaarb2049902021-01-24 12:53:53 +01005499 case ISN_PROF_START:
5500 case ISN_PROF_END:
5501 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01005502#ifdef FEAT_PROFILE
Bram Moolenaarca16c602022-09-06 18:57:08 +01005503 funccall_T cookie;
5504 ufunc_T *cur_ufunc =
Bram Moolenaarb2049902021-01-24 12:53:53 +01005505 (((dfunc_T *)def_functions.ga_data)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02005506 + ectx->ec_dfunc_idx)->df_ufunc;
Bram Moolenaarb2049902021-01-24 12:53:53 +01005507
Bram Moolenaarca16c602022-09-06 18:57:08 +01005508 cookie.fc_func = cur_ufunc;
Bram Moolenaarb2049902021-01-24 12:53:53 +01005509 if (iptr->isn_type == ISN_PROF_START)
5510 {
5511 func_line_start(&cookie, iptr->isn_lnum);
5512 // if we get here the instruction is executed
5513 func_line_exec(&cookie);
5514 }
5515 else
5516 func_line_end(&cookie);
Bram Moolenaarf002a412021-01-24 13:34:18 +01005517#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01005518 }
5519 break;
5520
Bram Moolenaare99d4222021-06-13 14:01:26 +02005521 case ISN_DEBUG:
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02005522 handle_debug(iptr, ectx);
Bram Moolenaare99d4222021-06-13 14:01:26 +02005523 break;
5524
Bram Moolenaar389df252020-07-09 21:20:47 +02005525 case ISN_SHUFFLE:
5526 {
Bram Moolenaar792f7862020-11-23 08:31:18 +01005527 typval_T tmp_tv;
5528 int item = iptr->isn_arg.shuffle.shfl_item;
5529 int up = iptr->isn_arg.shuffle.shfl_up;
Bram Moolenaar389df252020-07-09 21:20:47 +02005530
5531 tmp_tv = *STACK_TV_BOT(-item);
5532 for ( ; up > 0 && item > 1; --up)
5533 {
5534 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
5535 --item;
5536 }
5537 *STACK_TV_BOT(-item) = tmp_tv;
5538 }
5539 break;
5540
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005541 case ISN_DROP:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005542 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005543 clear_tv(STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005544 ectx->ec_where.wt_index = 0;
5545 ectx->ec_where.wt_variable = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005546 break;
5547 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02005548 continue;
5549
Bram Moolenaard032f342020-07-18 18:13:02 +02005550func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02005551 // Restore previous function. If the frame pointer is where we started
5552 // then there is none and we are done.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005553 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02005554 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02005555
Bram Moolenaar4c137212021-04-19 16:48:48 +02005556 if (func_return(ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02005557 // only fails when out of memory
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005558 goto theend;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02005559 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02005560
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02005561on_error:
Bram Moolenaaraf0df472020-12-02 20:51:22 +01005562 // Jump here for an error that does not require aborting execution.
Bram Moolenaar56602ba2020-12-05 21:22:08 +01005563 // If "emsg_silent" is set then ignore the error, unless it was set
5564 // when calling the function.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005565 if (did_emsg_cumul + did_emsg == ectx->ec_did_emsg_before
Bram Moolenaar56602ba2020-12-05 21:22:08 +01005566 && emsg_silent && did_emsg_def == 0)
Bram Moolenaarf9041332021-01-21 19:41:16 +01005567 {
5568 // If a sequence of instructions causes an error while ":silent!"
5569 // was used, restore the stack length and jump ahead to restoring
5570 // the cmdmod.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005571 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaarf9041332021-01-21 19:41:16 +01005572 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005573 while (ectx->ec_stack.ga_len
5574 > ectx->ec_funclocal.floc_restore_cmdmod_stacklen)
Bram Moolenaarf9041332021-01-21 19:41:16 +01005575 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005576 --ectx->ec_stack.ga_len;
Bram Moolenaarf9041332021-01-21 19:41:16 +01005577 clear_tv(STACK_TV_BOT(0));
5578 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005579 while (ectx->ec_instr[ectx->ec_iidx].isn_type != ISN_CMDMOD_REV)
5580 ++ectx->ec_iidx;
Bram Moolenaarf9041332021-01-21 19:41:16 +01005581 }
Bram Moolenaarcd030c42020-10-30 21:49:40 +01005582 continue;
Bram Moolenaarf9041332021-01-21 19:41:16 +01005583 }
Bram Moolenaaraf0df472020-12-02 20:51:22 +01005584on_fatal_error:
5585 // Jump here for an error that messes up the stack.
Bram Moolenaar171fb922020-10-28 16:54:47 +01005586 // If we are not inside a try-catch started here, abort execution.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005587 if (trylevel <= ectx->ec_trylevel_at_start)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005588 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005589 }
5590
5591done:
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005592 ret = OK;
5593theend:
Bram Moolenaar58779852022-09-06 18:31:14 +01005594 may_invoke_defer_funcs(ectx);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01005595
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005596 dict_stack_clear(dict_stack_len_at_start);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005597 ectx->ec_trylevel_at_start = save_trylevel_at_start;
5598 return ret;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005599}
5600
5601/*
Bram Moolenaar806a2732022-09-04 15:40:36 +01005602 * Execute the instructions from a VAR_INSTR typval and put the result in
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005603 * "rettv".
5604 * Return OK or FAIL.
5605 */
5606 int
5607exe_typval_instr(typval_T *tv, typval_T *rettv)
5608{
5609 ectx_T *ectx = tv->vval.v_instr->instr_ectx;
5610 isn_T *save_instr = ectx->ec_instr;
5611 int save_iidx = ectx->ec_iidx;
5612 int res;
5613
LemonBoyf3b48952022-05-05 13:53:03 +01005614 // Initialize rettv so that it is safe for caller to invoke clear_tv(rettv)
5615 // even when the compilation fails.
5616 rettv->v_type = VAR_UNKNOWN;
5617
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005618 ectx->ec_instr = tv->vval.v_instr->instr_instr;
5619 res = exec_instructions(ectx);
5620 if (res == OK)
5621 {
5622 *rettv = *STACK_TV_BOT(-1);
5623 --ectx->ec_stack.ga_len;
5624 }
5625
5626 ectx->ec_instr = save_instr;
5627 ectx->ec_iidx = save_iidx;
5628
5629 return res;
5630}
5631
5632/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02005633 * Execute the instructions from an ISN_SUBSTITUTE command, which are in
5634 * "substitute_instr".
5635 */
5636 char_u *
5637exe_substitute_instr(void)
5638{
5639 ectx_T *ectx = substitute_instr->subs_ectx;
5640 isn_T *save_instr = ectx->ec_instr;
5641 int save_iidx = ectx->ec_iidx;
5642 char_u *res;
5643
5644 ectx->ec_instr = substitute_instr->subs_instr;
5645 if (exec_instructions(ectx) == OK)
Bram Moolenaar90193e62021-04-04 20:49:50 +02005646 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005647 typval_T *tv = STACK_TV_BOT(-1);
5648
Bram Moolenaar27523602021-06-05 21:36:19 +02005649 res = typval2string(tv, TRUE);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005650 --ectx->ec_stack.ga_len;
5651 clear_tv(tv);
Bram Moolenaar90193e62021-04-04 20:49:50 +02005652 }
5653 else
5654 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005655 substitute_instr->subs_status = FAIL;
5656 res = vim_strsave((char_u *)"");
Bram Moolenaar90193e62021-04-04 20:49:50 +02005657 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005658
Bram Moolenaar4c137212021-04-19 16:48:48 +02005659 ectx->ec_instr = save_instr;
5660 ectx->ec_iidx = save_iidx;
5661
5662 return res;
5663}
5664
5665/*
5666 * Call a "def" function from old Vim script.
5667 * Return OK or FAIL.
5668 */
5669 int
5670call_def_function(
5671 ufunc_T *ufunc,
5672 int argc_arg, // nr of arguments
5673 typval_T *argv, // arguments
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005674 int flags, // DEF_ flags
Bram Moolenaar4c137212021-04-19 16:48:48 +02005675 partial_T *partial, // optional partial for context
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005676 object_T *object, // object, e.g. for this.Func()
Bram Moolenaar58779852022-09-06 18:31:14 +01005677 funccall_T *funccal,
Bram Moolenaar4c137212021-04-19 16:48:48 +02005678 typval_T *rettv) // return value
5679{
5680 ectx_T ectx; // execution context
5681 int argc = argc_arg;
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005682 int partial_argc = partial == NULL
5683 || (flags & DEF_USE_PT_ARGV) == 0
5684 ? 0 : partial->pt_argc;
5685 int total_argc = argc + partial_argc;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005686 typval_T *tv;
5687 int idx;
5688 int ret = FAIL;
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005689 int defcount = ufunc->uf_args.ga_len - total_argc;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005690 sctx_T save_current_sctx = current_sctx;
5691 int did_emsg_before = did_emsg_cumul + did_emsg;
5692 int save_suppress_errthrow = suppress_errthrow;
5693 msglist_T **saved_msg_list = NULL;
5694 msglist_T *private_msg_list = NULL;
5695 int save_emsg_silent_def = emsg_silent_def;
5696 int save_did_emsg_def = did_emsg_def;
5697 int orig_funcdepth;
Bram Moolenaare99d4222021-06-13 14:01:26 +02005698 int orig_nesting_level = ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005699
5700// Get pointer to item in the stack.
5701#undef STACK_TV
5702#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
5703
5704// Get pointer to item at the bottom of the stack, -1 is the bottom.
5705#undef STACK_TV_BOT
5706#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
5707
5708// Get pointer to a local variable on the stack. Negative for arguments.
5709#undef STACK_TV_VAR
5710#define STACK_TV_VAR(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_frame_idx + STACK_FRAME_SIZE + idx)
5711
5712 if (ufunc->uf_def_status == UF_NOT_COMPILED
5713 || ufunc->uf_def_status == UF_COMPILE_ERROR
Bram Moolenaar139575d2022-03-15 19:29:30 +00005714 || (func_needs_compiling(ufunc, get_compile_type(ufunc))
5715 && compile_def_function(ufunc, FALSE,
5716 get_compile_type(ufunc), NULL) == FAIL))
Bram Moolenaar4c137212021-04-19 16:48:48 +02005717 {
5718 if (did_emsg_cumul + did_emsg == did_emsg_before)
5719 semsg(_(e_function_is_not_compiled_str),
5720 printable_func_name(ufunc));
5721 return FAIL;
5722 }
5723
5724 {
5725 // Check the function was really compiled.
5726 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5727 + ufunc->uf_dfunc_idx;
Bram Moolenaarcc341812022-09-19 15:54:34 +01005728 if (dfunc->df_ufunc == NULL)
5729 {
5730 semsg(_(e_function_was_deleted_str), printable_func_name(ufunc));
5731 return FAIL;
5732 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005733 if (INSTRUCTIONS(dfunc) == NULL)
5734 {
5735 iemsg("using call_def_function() on not compiled function");
5736 return FAIL;
5737 }
5738 }
5739
5740 // If depth of calling is getting too high, don't execute the function.
5741 orig_funcdepth = funcdepth_get();
5742 if (funcdepth_increment() == FAIL)
5743 return FAIL;
5744
5745 CLEAR_FIELD(ectx);
5746 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
5747 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
Bram Moolenaar35578162021-08-02 19:10:38 +02005748 if (GA_GROW_FAILS(&ectx.ec_stack, 20))
Bram Moolenaar4c137212021-04-19 16:48:48 +02005749 {
5750 funcdepth_decrement();
5751 return FAIL;
5752 }
5753 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
5754 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
5755 ectx.ec_did_emsg_before = did_emsg_before;
Bram Moolenaare99d4222021-06-13 14:01:26 +02005756 ++ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005757
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005758 idx = total_argc - ufunc->uf_args.ga_len;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005759 if (idx > 0 && ufunc->uf_va_name == NULL)
5760 {
Matvey Tarasovd14bb1a2022-06-29 13:18:27 +01005761 semsg(NGETTEXT(e_one_argument_too_many, e_nr_arguments_too_many,
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005762 idx), idx);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005763 goto failed_early;
5764 }
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005765 idx = total_argc - ufunc->uf_args.ga_len + ufunc->uf_def_args.ga_len;
Bram Moolenaarc6d71532021-06-05 18:49:38 +02005766 if (idx < 0)
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02005767 {
Matvey Tarasovd14bb1a2022-06-29 13:18:27 +01005768 semsg(NGETTEXT(e_one_argument_too_few, e_nr_arguments_too_few,
Bram Moolenaar98aff652022-09-06 21:02:35 +01005769 -idx), -idx);
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02005770 goto failed_early;
5771 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005772
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005773 // Put values from the partial and arguments on the stack, but no more than
5774 // what the function expects. A lambda can be called with more arguments
5775 // than it uses.
5776 for (idx = 0; idx < total_argc
Bram Moolenaar4c137212021-04-19 16:48:48 +02005777 && (ufunc->uf_va_name != NULL || idx < ufunc->uf_args.ga_len);
5778 ++idx)
5779 {
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005780 int argv_idx = idx - partial_argc;
5781
5782 tv = idx < partial_argc ? partial->pt_argv + idx : argv + argv_idx;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005783 if (idx >= ufunc->uf_args.ga_len - ufunc->uf_def_args.ga_len
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005784 && tv->v_type == VAR_SPECIAL
5785 && tv->vval.v_number == VVAL_NONE)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005786 {
5787 // Use the default value.
5788 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
5789 }
5790 else
5791 {
5792 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
5793 && check_typval_arg_type(
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005794 ufunc->uf_arg_types[idx], tv,
5795 NULL, argv_idx + 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005796 goto failed_early;
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005797 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005798 }
5799 ++ectx.ec_stack.ga_len;
5800 }
5801
5802 // Turn varargs into a list. Empty list if no args.
5803 if (ufunc->uf_va_name != NULL)
5804 {
5805 int vararg_count = argc - ufunc->uf_args.ga_len;
5806
5807 if (vararg_count < 0)
5808 vararg_count = 0;
5809 else
5810 argc -= vararg_count;
5811 if (exe_newlist(vararg_count, &ectx) == FAIL)
5812 goto failed_early;
5813
5814 // Check the type of the list items.
5815 tv = STACK_TV_BOT(-1);
5816 if (ufunc->uf_va_type != NULL
5817 && ufunc->uf_va_type != &t_list_any
5818 && ufunc->uf_va_type->tt_member != &t_any
5819 && tv->vval.v_list != NULL)
5820 {
5821 type_T *expected = ufunc->uf_va_type->tt_member;
5822 listitem_T *li = tv->vval.v_list->lv_first;
5823
5824 for (idx = 0; idx < vararg_count; ++idx)
5825 {
5826 if (check_typval_arg_type(expected, &li->li_tv,
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02005827 NULL, argc + idx + 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005828 goto failed_early;
5829 li = li->li_next;
5830 }
5831 }
5832
5833 if (defcount > 0)
5834 // Move varargs list to below missing default arguments.
5835 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
5836 --ectx.ec_stack.ga_len;
5837 }
5838
5839 // Make space for omitted arguments, will store default value below.
5840 // Any varargs list goes after them.
5841 if (defcount > 0)
5842 for (idx = 0; idx < defcount; ++idx)
5843 {
5844 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
5845 ++ectx.ec_stack.ga_len;
5846 }
5847 if (ufunc->uf_va_name != NULL)
5848 ++ectx.ec_stack.ga_len;
5849
5850 // Frame pointer points to just after arguments.
5851 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
5852 ectx.ec_initial_frame_idx = ectx.ec_frame_idx;
5853
5854 {
5855 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5856 + ufunc->uf_dfunc_idx;
5857 ufunc_T *base_ufunc = dfunc->df_ufunc;
5858
5859 // "uf_partial" is on the ufunc that "df_ufunc" points to, as is done
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01005860 // by copy_lambda_to_global_func().
Bram Moolenaar4c137212021-04-19 16:48:48 +02005861 if (partial != NULL || base_ufunc->uf_partial != NULL)
5862 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005863 ectx.ec_outer_ref = ALLOC_CLEAR_ONE(outer_ref_T);
5864 if (ectx.ec_outer_ref == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005865 goto failed_early;
5866 if (partial != NULL)
5867 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00005868 outer_T *outer = get_pt_outer(partial);
5869
Bram Moolenaar6d313be2022-09-22 16:36:25 +01005870 if (outer->out_stack == NULL && outer->out_loop_size == 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005871 {
Bram Moolenaar6d313be2022-09-22 16:36:25 +01005872 // no stack was set
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00005873 if (current_ectx != NULL)
5874 {
5875 if (current_ectx->ec_outer_ref != NULL
5876 && current_ectx->ec_outer_ref->or_outer != NULL)
5877 ectx.ec_outer_ref->or_outer =
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005878 current_ectx->ec_outer_ref->or_outer;
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00005879 }
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005880 // else: should there be an error here?
Bram Moolenaar4c137212021-04-19 16:48:48 +02005881 }
5882 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005883 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00005884 ectx.ec_outer_ref->or_outer = outer;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005885 ++partial->pt_refcount;
5886 ectx.ec_outer_ref->or_partial = partial;
5887 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005888 }
5889 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005890 {
5891 ectx.ec_outer_ref->or_outer = &base_ufunc->uf_partial->pt_outer;
5892 ++base_ufunc->uf_partial->pt_refcount;
5893 ectx.ec_outer_ref->or_partial = base_ufunc->uf_partial;
5894 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005895 }
5896 }
5897
5898 // dummy frame entries
5899 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
5900 {
5901 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
5902 ++ectx.ec_stack.ga_len;
5903 }
5904
5905 {
5906 // Reserve space for local variables and any closure reference count.
5907 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5908 + ufunc->uf_dfunc_idx;
5909
Bram Moolenaar5cd64792021-12-25 18:23:24 +00005910 // Initialize variables to zero. That avoids having to generate
5911 // initializing instructions for "var nr: number", "var x: any", etc.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005912 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaar5cd64792021-12-25 18:23:24 +00005913 {
5914 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
5915 STACK_TV_VAR(idx)->vval.v_number = 0;
5916 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005917 ectx.ec_stack.ga_len += dfunc->df_varcount;
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005918
5919 if (object != NULL)
5920 {
5921 // the object is always the variable at index zero
5922 tv = STACK_TV_VAR(0);
5923 tv->v_type = VAR_OBJECT;
5924 tv->vval.v_object = object;
5925 }
5926
Bram Moolenaar4c137212021-04-19 16:48:48 +02005927 if (dfunc->df_has_closure)
5928 {
Bram Moolenaarcc341812022-09-19 15:54:34 +01005929 // Initialize the variable that counts how many closures were
5930 // created. This is used in handle_closure_in_use().
Bram Moolenaar4c137212021-04-19 16:48:48 +02005931 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
5932 STACK_TV_VAR(idx)->vval.v_number = 0;
5933 ++ectx.ec_stack.ga_len;
5934 }
5935
5936 ectx.ec_instr = INSTRUCTIONS(dfunc);
5937 }
5938
Bram Moolenaar58779852022-09-06 18:31:14 +01005939 // Store the execution context in funccal, used by invoke_all_defer().
5940 if (funccal != NULL)
5941 funccal->fc_ectx = &ectx;
5942
Bram Moolenaar4c137212021-04-19 16:48:48 +02005943 // Following errors are in the function, not the caller.
5944 // Commands behave like vim9script.
5945 estack_push_ufunc(ufunc, 1);
5946 current_sctx = ufunc->uf_script_ctx;
5947 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
5948
5949 // Use a specific location for storing error messages to be converted to an
5950 // exception.
5951 saved_msg_list = msg_list;
5952 msg_list = &private_msg_list;
5953
5954 // Do turn errors into exceptions.
5955 suppress_errthrow = FALSE;
5956
5957 // Do not delete the function while executing it.
5958 ++ufunc->uf_calls;
5959
5960 // When ":silent!" was used before calling then we still abort the
5961 // function. If ":silent!" is used in the function then we don't.
5962 emsg_silent_def = emsg_silent;
5963 did_emsg_def = 0;
5964
5965 ectx.ec_where.wt_index = 0;
5966 ectx.ec_where.wt_variable = FALSE;
5967
Bram Moolenaar5800c792022-09-22 17:34:01 +01005968 /*
5969 * Execute the instructions until done.
5970 */
Bram Moolenaar4c137212021-04-19 16:48:48 +02005971 ret = exec_instructions(&ectx);
5972 if (ret == OK)
5973 {
5974 // function finished, get result from the stack.
5975 if (ufunc->uf_ret_type == &t_void)
5976 {
5977 rettv->v_type = VAR_VOID;
5978 }
5979 else
5980 {
5981 tv = STACK_TV_BOT(-1);
5982 *rettv = *tv;
5983 tv->v_type = VAR_UNKNOWN;
5984 }
5985 }
5986
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01005987 // When failed need to unwind the call stack.
Bram Moolenaar58779852022-09-06 18:31:14 +01005988 unwind_def_callstack(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02005989
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005990 // Deal with any remaining closures, they may be in use somewhere.
5991 if (ectx.ec_funcrefs.ga_len > 0)
Bram Moolenaarf112f302020-12-20 17:47:52 +01005992 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005993 handle_closure_in_use(&ectx, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00005994 ga_clear(&ectx.ec_funcrefs);
Bram Moolenaarf112f302020-12-20 17:47:52 +01005995 }
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005996
Bram Moolenaaree8580e2020-08-28 17:19:07 +02005997 estack_pop();
5998 current_sctx = save_current_sctx;
5999
Bram Moolenaar2336c372021-12-06 15:06:54 +00006000 if (--ufunc->uf_calls <= 0 && ufunc->uf_refcount <= 0)
6001 // Function was unreferenced while being used, free it now.
6002 func_clear_free(ufunc, FALSE);
Bram Moolenaarc970e422021-03-17 15:03:04 +01006003
Bram Moolenaar352134b2020-10-17 22:04:08 +02006004 if (*msg_list != NULL && saved_msg_list != NULL)
6005 {
6006 msglist_T **plist = saved_msg_list;
6007
6008 // Append entries from the current msg_list (uncaught exceptions) to
6009 // the saved msg_list.
6010 while (*plist != NULL)
6011 plist = &(*plist)->next;
6012
6013 *plist = *msg_list;
6014 }
6015 msg_list = saved_msg_list;
6016
Bram Moolenaar4c137212021-04-19 16:48:48 +02006017 if (ectx.ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar02194d22020-10-24 23:08:38 +02006018 {
6019 cmdmod.cmod_filter_regmatch.regprog = NULL;
6020 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006021 cmdmod = ectx.ec_funclocal.floc_save_cmdmod;
Bram Moolenaar02194d22020-10-24 23:08:38 +02006022 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +01006023 emsg_silent_def = save_emsg_silent_def;
6024 did_emsg_def += save_did_emsg_def;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02006025
Bram Moolenaaree8580e2020-08-28 17:19:07 +02006026failed_early:
Bram Moolenaar0d1f55c2022-04-05 17:30:29 +01006027 // Free all arguments and local variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006028 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
Bram Moolenaar1936c762022-09-28 15:19:10 +01006029 {
6030 tv = STACK_TV(idx);
6031 if (tv->v_type != VAR_NUMBER && tv->v_type != VAR_UNKNOWN)
6032 clear_tv(tv);
6033 }
Bram Moolenaare99d4222021-06-13 14:01:26 +02006034 ex_nesting_level = orig_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02006035
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006036 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01006037 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006038 if (ectx.ec_outer_ref != NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +01006039 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006040 if (ectx.ec_outer_ref->or_outer_allocated)
6041 vim_free(ectx.ec_outer_ref->or_outer);
6042 partial_unref(ectx.ec_outer_ref->or_partial);
6043 vim_free(ectx.ec_outer_ref);
Bram Moolenaar0186e582021-01-10 18:33:11 +01006044 }
6045
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02006046 // Not sure if this is necessary.
6047 suppress_errthrow = save_suppress_errthrow;
6048
Bram Moolenaarb5841b92021-07-15 18:09:53 +02006049 if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before
6050 && !need_rethrow)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006051 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02006052 printable_func_name(ufunc));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01006053 funcdepth_restore(orig_funcdepth);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006054 return ret;
6055}
6056
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006057/*
Bram Moolenaar58779852022-09-06 18:31:14 +01006058 * Called when a def function has finished (possibly failed).
6059 * Invoke all the function returns to clean up and invoke deferred functions,
6060 * except the toplevel one.
6061 */
6062 void
6063unwind_def_callstack(ectx_T *ectx)
6064{
6065 while (ectx->ec_frame_idx != ectx->ec_initial_frame_idx)
6066 func_return(ectx);
6067}
6068
6069/*
dundargocc57b5bc2022-11-02 13:30:51 +00006070 * Invoke any deferred functions for the top function in "ectx".
Bram Moolenaar58779852022-09-06 18:31:14 +01006071 */
6072 void
6073may_invoke_defer_funcs(ectx_T *ectx)
6074{
6075 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
6076
6077 if (dfunc->df_defer_var_idx > 0)
6078 invoke_defer_funcs(ectx);
6079}
6080
6081/*
Bram Moolenaarcc341812022-09-19 15:54:34 +01006082 * Return loopvarinfo in a printable form in allocated memory.
6083 */
6084 static char_u *
6085printable_loopvarinfo(loopvarinfo_T *lvi)
6086{
6087 garray_T ga;
6088 int depth;
6089
6090 ga_init2(&ga, 1, 100);
6091 for (depth = 0; depth < lvi->lvi_depth; ++depth)
6092 {
6093 if (ga_grow(&ga, 50) == FAIL)
6094 break;
6095 if (lvi->lvi_loop[depth].var_idx == 0)
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +01006096 STRCPY((char *)ga.ga_data + ga.ga_len, " -");
Bram Moolenaarcc341812022-09-19 15:54:34 +01006097 else
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +01006098 vim_snprintf((char *)ga.ga_data + ga.ga_len, 50, " $%d-$%d",
Bram Moolenaarcc341812022-09-19 15:54:34 +01006099 lvi->lvi_loop[depth].var_idx,
6100 lvi->lvi_loop[depth].var_idx
6101 + lvi->lvi_loop[depth].var_count - 1);
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +01006102 ga.ga_len = (int)STRLEN(ga.ga_data);
Bram Moolenaarcc341812022-09-19 15:54:34 +01006103 }
6104 return ga.ga_data;
6105}
6106
6107/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02006108 * List instructions "instr" up to "instr_count" or until ISN_FINISH.
6109 * "ufunc" has the source lines, NULL for the instructions of ISN_SUBSTITUTE.
6110 * "pfx" is prefixed to every line.
6111 */
6112 static void
6113list_instructions(char *pfx, isn_T *instr, int instr_count, ufunc_T *ufunc)
6114{
6115 int line_idx = 0;
6116 int prev_current = 0;
6117 int current;
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02006118 int def_arg_idx = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006119
6120 for (current = 0; current < instr_count; ++current)
6121 {
6122 isn_T *iptr = &instr[current];
6123 char *line;
6124
6125 if (ufunc != NULL)
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02006126 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02006127 while (line_idx < iptr->isn_lnum
6128 && line_idx < ufunc->uf_lines.ga_len)
6129 {
6130 if (current > prev_current)
6131 {
6132 msg_puts("\n\n");
6133 prev_current = current;
6134 }
6135 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
6136 if (line != NULL)
6137 msg(line);
6138 }
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02006139 if (iptr->isn_type == ISN_JUMP_IF_ARG_SET)
6140 {
6141 int first_def_arg = ufunc->uf_args.ga_len
6142 - ufunc->uf_def_args.ga_len;
6143
6144 if (def_arg_idx > 0)
6145 msg_puts("\n\n");
6146 msg_start();
6147 msg_puts(" ");
6148 msg_puts(((char **)(ufunc->uf_args.ga_data))[
6149 first_def_arg + def_arg_idx]);
6150 msg_puts(" = ");
6151 msg_puts(((char **)(ufunc->uf_def_args.ga_data))[def_arg_idx++]);
6152 msg_clr_eos();
6153 msg_end();
6154 }
6155 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006156
6157 switch (iptr->isn_type)
6158 {
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006159 case ISN_CONSTRUCT:
6160 smsg("%s%4d NEW %s size %d", pfx, current,
6161 iptr->isn_arg.construct.construct_class->class_name,
6162 (int)iptr->isn_arg.construct.construct_size);
6163 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006164 case ISN_EXEC:
6165 smsg("%s%4d EXEC %s", pfx, current, iptr->isn_arg.string);
6166 break;
Bram Moolenaar20677332021-06-06 17:02:53 +02006167 case ISN_EXEC_SPLIT:
6168 smsg("%s%4d EXEC_SPLIT %s", pfx, current, iptr->isn_arg.string);
6169 break;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00006170 case ISN_EXECRANGE:
6171 smsg("%s%4d EXECRANGE %s", pfx, current, iptr->isn_arg.string);
6172 break;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02006173 case ISN_LEGACY_EVAL:
6174 smsg("%s%4d EVAL legacy %s", pfx, current,
6175 iptr->isn_arg.string);
6176 break;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02006177 case ISN_REDIRSTART:
6178 smsg("%s%4d REDIR", pfx, current);
6179 break;
6180 case ISN_REDIREND:
6181 smsg("%s%4d REDIR END%s", pfx, current,
6182 iptr->isn_arg.number ? " append" : "");
6183 break;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006184 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02006185#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006186 smsg("%s%4d CEXPR pre %s", pfx, current,
6187 cexpr_get_auname(iptr->isn_arg.number));
Bram Moolenaarb7c97812021-05-05 22:51:39 +02006188#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006189 break;
6190 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02006191#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006192 {
6193 cexprref_T *cer = iptr->isn_arg.cexpr.cexpr_ref;
6194
6195 smsg("%s%4d CEXPR core %s%s \"%s\"", pfx, current,
6196 cexpr_get_auname(cer->cer_cmdidx),
6197 cer->cer_forceit ? "!" : "",
6198 cer->cer_cmdline);
6199 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02006200#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006201 break;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006202 case ISN_INSTR:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006203 smsg("%s%4d INSTR", pfx, current);
6204 list_instructions(" ", iptr->isn_arg.instr, INT_MAX, NULL);
6205 msg(" -------------");
6206 break;
6207 case ISN_SOURCE:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006208 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006209 scriptitem_T *si = SCRIPT_ITEM(iptr->isn_arg.number);
6210
6211 smsg("%s%4d SOURCE %s", pfx, current, si->sn_name);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006212 }
6213 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006214 case ISN_SUBSTITUTE:
6215 {
6216 subs_T *subs = &iptr->isn_arg.subs;
6217
6218 smsg("%s%4d SUBSTITUTE %s", pfx, current, subs->subs_cmd);
6219 list_instructions(" ", subs->subs_instr, INT_MAX, NULL);
6220 msg(" -------------");
6221 }
6222 break;
6223 case ISN_EXECCONCAT:
6224 smsg("%s%4d EXECCONCAT %lld", pfx, current,
6225 (varnumber_T)iptr->isn_arg.number);
6226 break;
6227 case ISN_ECHO:
6228 {
6229 echo_T *echo = &iptr->isn_arg.echo;
6230
6231 smsg("%s%4d %s %d", pfx, current,
6232 echo->echo_with_white ? "ECHO" : "ECHON",
6233 echo->echo_count);
6234 }
6235 break;
6236 case ISN_EXECUTE:
6237 smsg("%s%4d EXECUTE %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02006238 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006239 break;
6240 case ISN_ECHOMSG:
6241 smsg("%s%4d ECHOMSG %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02006242 (varnumber_T)(iptr->isn_arg.number));
6243 break;
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01006244 case ISN_ECHOWINDOW:
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01006245 if (iptr->isn_arg.echowin.ewin_time > 0)
6246 smsg("%s%4d ECHOWINDOW %d (%ld sec)", pfx, current,
6247 iptr->isn_arg.echowin.ewin_count,
6248 iptr->isn_arg.echowin.ewin_time);
6249 else
6250 smsg("%s%4d ECHOWINDOW %d", pfx, current,
6251 iptr->isn_arg.echowin.ewin_count);
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01006252 break;
Bram Moolenaar7de62622021-08-07 15:05:47 +02006253 case ISN_ECHOCONSOLE:
6254 smsg("%s%4d ECHOCONSOLE %lld", pfx, current,
6255 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006256 break;
6257 case ISN_ECHOERR:
6258 smsg("%s%4d ECHOERR %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02006259 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006260 break;
6261 case ISN_LOAD:
6262 {
6263 if (iptr->isn_arg.number < 0)
6264 smsg("%s%4d LOAD arg[%lld]", pfx, current,
6265 (varnumber_T)(iptr->isn_arg.number
6266 + STACK_FRAME_SIZE));
6267 else
6268 smsg("%s%4d LOAD $%lld", pfx, current,
6269 (varnumber_T)(iptr->isn_arg.number));
6270 }
6271 break;
6272 case ISN_LOADOUTER:
6273 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006274 isn_outer_T *outer = &iptr->isn_arg.outer;
6275
6276 if (outer->outer_idx < 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02006277 smsg("%s%4d LOADOUTER level %d arg[%d]", pfx, current,
Bram Moolenaarcc341812022-09-19 15:54:34 +01006278 outer->outer_depth,
6279 outer->outer_idx + STACK_FRAME_SIZE);
6280 else if (outer->outer_depth < 0)
6281 smsg("%s%4d LOADOUTER $%d in loop level %d",
6282 pfx, current,
6283 outer->outer_idx,
6284 -outer->outer_depth);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006285 else
6286 smsg("%s%4d LOADOUTER level %d $%d", pfx, current,
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006287 outer->outer_depth,
6288 outer->outer_idx);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006289 }
6290 break;
6291 case ISN_LOADV:
6292 smsg("%s%4d LOADV v:%s", pfx, current,
6293 get_vim_var_name(iptr->isn_arg.number));
6294 break;
6295 case ISN_LOADSCRIPT:
6296 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006297 scriptref_T *sref = iptr->isn_arg.script.scriptref;
6298 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
6299 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006300
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006301 sv = get_script_svar(sref, -1);
6302 if (sv == NULL)
6303 smsg("%s%4d LOADSCRIPT [deleted] from %s",
6304 pfx, current, si->sn_name);
6305 else
6306 smsg("%s%4d LOADSCRIPT %s-%d from %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02006307 sv->sv_name,
6308 sref->sref_idx,
6309 si->sn_name);
6310 }
6311 break;
6312 case ISN_LOADS:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006313 case ISN_LOADEXPORT:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006314 {
6315 scriptitem_T *si = SCRIPT_ITEM(
6316 iptr->isn_arg.loadstore.ls_sid);
6317
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006318 smsg("%s%4d %s s:%s from %s", pfx, current,
6319 iptr->isn_type == ISN_LOADS ? "LOADS"
6320 : "LOADEXPORT",
6321 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006322 }
6323 break;
6324 case ISN_LOADAUTO:
6325 smsg("%s%4d LOADAUTO %s", pfx, current, iptr->isn_arg.string);
6326 break;
6327 case ISN_LOADG:
6328 smsg("%s%4d LOADG g:%s", pfx, current, iptr->isn_arg.string);
6329 break;
6330 case ISN_LOADB:
6331 smsg("%s%4d LOADB b:%s", pfx, current, iptr->isn_arg.string);
6332 break;
6333 case ISN_LOADW:
6334 smsg("%s%4d LOADW w:%s", pfx, current, iptr->isn_arg.string);
6335 break;
6336 case ISN_LOADT:
6337 smsg("%s%4d LOADT t:%s", pfx, current, iptr->isn_arg.string);
6338 break;
6339 case ISN_LOADGDICT:
6340 smsg("%s%4d LOAD g:", pfx, current);
6341 break;
6342 case ISN_LOADBDICT:
6343 smsg("%s%4d LOAD b:", pfx, current);
6344 break;
6345 case ISN_LOADWDICT:
6346 smsg("%s%4d LOAD w:", pfx, current);
6347 break;
6348 case ISN_LOADTDICT:
6349 smsg("%s%4d LOAD t:", pfx, current);
6350 break;
6351 case ISN_LOADOPT:
6352 smsg("%s%4d LOADOPT %s", pfx, current, iptr->isn_arg.string);
6353 break;
6354 case ISN_LOADENV:
6355 smsg("%s%4d LOADENV %s", pfx, current, iptr->isn_arg.string);
6356 break;
6357 case ISN_LOADREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006358 smsg("%s%4d LOADREG @%c", pfx, current,
6359 (int)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006360 break;
6361
6362 case ISN_STORE:
6363 if (iptr->isn_arg.number < 0)
6364 smsg("%s%4d STORE arg[%lld]", pfx, current,
6365 iptr->isn_arg.number + STACK_FRAME_SIZE);
6366 else
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006367 smsg("%s%4d STORE $%lld", pfx, current,
6368 iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006369 break;
6370 case ISN_STOREOUTER:
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006371 {
6372 isn_outer_T *outer = &iptr->isn_arg.outer;
6373
6374 if (outer->outer_depth == OUTER_LOOP_DEPTH)
6375 smsg("%s%4d STOREOUTER level 1 $%d in loop",
6376 pfx, current, outer->outer_idx);
6377 else
6378 smsg("%s%4d STOREOUTER level %d $%d", pfx, current,
6379 outer->outer_depth, outer->outer_idx);
6380 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006381 break;
6382 case ISN_STOREV:
6383 smsg("%s%4d STOREV v:%s", pfx, current,
6384 get_vim_var_name(iptr->isn_arg.number));
6385 break;
6386 case ISN_STOREAUTO:
6387 smsg("%s%4d STOREAUTO %s", pfx, current, iptr->isn_arg.string);
6388 break;
6389 case ISN_STOREG:
6390 smsg("%s%4d STOREG %s", pfx, current, iptr->isn_arg.string);
6391 break;
6392 case ISN_STOREB:
6393 smsg("%s%4d STOREB %s", pfx, current, iptr->isn_arg.string);
6394 break;
6395 case ISN_STOREW:
6396 smsg("%s%4d STOREW %s", pfx, current, iptr->isn_arg.string);
6397 break;
6398 case ISN_STORET:
6399 smsg("%s%4d STORET %s", pfx, current, iptr->isn_arg.string);
6400 break;
6401 case ISN_STORES:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006402 case ISN_STOREEXPORT:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006403 {
6404 scriptitem_T *si = SCRIPT_ITEM(
6405 iptr->isn_arg.loadstore.ls_sid);
6406
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006407 smsg("%s%4d %s %s in %s", pfx, current,
6408 iptr->isn_type == ISN_STORES
6409 ? "STORES" : "STOREEXPORT",
Bram Moolenaar4c137212021-04-19 16:48:48 +02006410 iptr->isn_arg.loadstore.ls_name, si->sn_name);
6411 }
6412 break;
6413 case ISN_STORESCRIPT:
6414 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006415 scriptref_T *sref = iptr->isn_arg.script.scriptref;
6416 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
6417 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006418
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006419 sv = get_script_svar(sref, -1);
6420 if (sv == NULL)
6421 smsg("%s%4d STORESCRIPT [deleted] in %s",
6422 pfx, current, si->sn_name);
6423 else
6424 smsg("%s%4d STORESCRIPT %s-%d in %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02006425 sv->sv_name,
6426 sref->sref_idx,
6427 si->sn_name);
6428 }
6429 break;
6430 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00006431 case ISN_STOREFUNCOPT:
6432 smsg("%s%4d %s &%s", pfx, current,
6433 iptr->isn_type == ISN_STOREOPT ? "STOREOPT" : "STOREFUNCOPT",
Bram Moolenaar4c137212021-04-19 16:48:48 +02006434 iptr->isn_arg.storeopt.so_name);
6435 break;
6436 case ISN_STOREENV:
6437 smsg("%s%4d STOREENV $%s", pfx, current, iptr->isn_arg.string);
6438 break;
6439 case ISN_STOREREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006440 smsg("%s%4d STOREREG @%c", pfx, current,
6441 (int)iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006442 break;
6443 case ISN_STORENR:
6444 smsg("%s%4d STORE %lld in $%d", pfx, current,
6445 iptr->isn_arg.storenr.stnr_val,
6446 iptr->isn_arg.storenr.stnr_idx);
6447 break;
6448
6449 case ISN_STOREINDEX:
6450 smsg("%s%4d STOREINDEX %s", pfx, current,
6451 vartype_name(iptr->isn_arg.vartype));
6452 break;
6453
6454 case ISN_STORERANGE:
6455 smsg("%s%4d STORERANGE", pfx, current);
6456 break;
6457
Bram Moolenaard505d172022-12-18 21:42:55 +00006458 case ISN_LOAD_CLASSMEMBER:
6459 case ISN_STORE_CLASSMEMBER:
6460 {
6461 class_T *cl = iptr->isn_arg.classmember.cm_class;
6462 int idx = iptr->isn_arg.classmember.cm_idx;
6463 ocmember_T *ocm = &cl->class_class_members[idx];
6464 smsg("%s%4d %s CLASSMEMBER %s.%s", pfx, current,
6465 iptr->isn_type == ISN_LOAD_CLASSMEMBER
6466 ? "LOAD" : "STORE",
6467 cl->class_name, ocm->ocm_name);
6468 }
6469 break;
6470
Bram Moolenaar4c137212021-04-19 16:48:48 +02006471 // constants
6472 case ISN_PUSHNR:
6473 smsg("%s%4d PUSHNR %lld", pfx, current,
6474 (varnumber_T)(iptr->isn_arg.number));
6475 break;
6476 case ISN_PUSHBOOL:
6477 case ISN_PUSHSPEC:
6478 smsg("%s%4d PUSH %s", pfx, current,
6479 get_var_special_name(iptr->isn_arg.number));
6480 break;
6481 case ISN_PUSHF:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006482 smsg("%s%4d PUSHF %g", pfx, current, iptr->isn_arg.fnumber);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006483 break;
6484 case ISN_PUSHS:
6485 smsg("%s%4d PUSHS \"%s\"", pfx, current, iptr->isn_arg.string);
6486 break;
6487 case ISN_PUSHBLOB:
6488 {
6489 char_u *r;
6490 char_u numbuf[NUMBUFLEN];
6491 char_u *tofree;
6492
6493 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
6494 smsg("%s%4d PUSHBLOB %s", pfx, current, r);
6495 vim_free(tofree);
6496 }
6497 break;
6498 case ISN_PUSHFUNC:
6499 {
6500 char *name = (char *)iptr->isn_arg.string;
6501
6502 smsg("%s%4d PUSHFUNC \"%s\"", pfx, current,
6503 name == NULL ? "[none]" : name);
6504 }
6505 break;
6506 case ISN_PUSHCHANNEL:
6507#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar397a87a2022-03-20 21:14:15 +00006508 smsg("%s%4d PUSHCHANNEL 0", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006509#endif
6510 break;
6511 case ISN_PUSHJOB:
6512#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar397a87a2022-03-20 21:14:15 +00006513 smsg("%s%4d PUSHJOB \"no process\"", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006514#endif
6515 break;
6516 case ISN_PUSHEXC:
6517 smsg("%s%4d PUSH v:exception", pfx, current);
6518 break;
Bram Moolenaar06b77222022-01-25 15:51:56 +00006519 case ISN_AUTOLOAD:
6520 smsg("%s%4d AUTOLOAD %s", pfx, current, iptr->isn_arg.string);
6521 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006522 case ISN_UNLET:
6523 smsg("%s%4d UNLET%s %s", pfx, current,
6524 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
6525 iptr->isn_arg.unlet.ul_name);
6526 break;
6527 case ISN_UNLETENV:
6528 smsg("%s%4d UNLETENV%s $%s", pfx, current,
6529 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
6530 iptr->isn_arg.unlet.ul_name);
6531 break;
6532 case ISN_UNLETINDEX:
6533 smsg("%s%4d UNLETINDEX", pfx, current);
6534 break;
6535 case ISN_UNLETRANGE:
6536 smsg("%s%4d UNLETRANGE", pfx, current);
6537 break;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02006538 case ISN_LOCKUNLOCK:
6539 smsg("%s%4d LOCKUNLOCK %s", pfx, current, iptr->isn_arg.string);
6540 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006541 case ISN_LOCKCONST:
6542 smsg("%s%4d LOCKCONST", pfx, current);
6543 break;
6544 case ISN_NEWLIST:
6545 smsg("%s%4d NEWLIST size %lld", pfx, current,
Bram Moolenaar1936c762022-09-28 15:19:10 +01006546 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006547 break;
6548 case ISN_NEWDICT:
6549 smsg("%s%4d NEWDICT size %lld", pfx, current,
Bram Moolenaar1936c762022-09-28 15:19:10 +01006550 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006551 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00006552 case ISN_NEWPARTIAL:
6553 smsg("%s%4d NEWPARTIAL", pfx, current);
6554 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006555
6556 // function call
6557 case ISN_BCALL:
6558 {
6559 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
6560
6561 smsg("%s%4d BCALL %s(argc %d)", pfx, current,
6562 internal_func_name(cbfunc->cbf_idx),
6563 cbfunc->cbf_argcount);
6564 }
6565 break;
6566 case ISN_DCALL:
6567 {
6568 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
6569 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
6570 + cdfunc->cdf_idx;
6571
6572 smsg("%s%4d DCALL %s(argc %d)", pfx, current,
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006573 printable_func_name(df->df_ufunc),
6574 cdfunc->cdf_argcount);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006575 }
6576 break;
6577 case ISN_UCALL:
6578 {
6579 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
6580
6581 smsg("%s%4d UCALL %s(argc %d)", pfx, current,
6582 cufunc->cuf_name, cufunc->cuf_argcount);
6583 }
6584 break;
6585 case ISN_PCALL:
6586 {
6587 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
6588
6589 smsg("%s%4d PCALL%s (argc %d)", pfx, current,
6590 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
6591 }
6592 break;
6593 case ISN_PCALL_END:
6594 smsg("%s%4d PCALL end", pfx, current);
6595 break;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006596 case ISN_DEFER:
6597 smsg("%s%4d DEFER %d args", pfx, current,
6598 (int)iptr->isn_arg.defer.defer_argcount);
6599 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006600 case ISN_RETURN:
6601 smsg("%s%4d RETURN", pfx, current);
6602 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02006603 case ISN_RETURN_VOID:
6604 smsg("%s%4d RETURN void", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006605 break;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006606 case ISN_RETURN_OBJECT:
6607 smsg("%s%4d RETURN object", pfx, current);
6608 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006609 case ISN_FUNCREF:
6610 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006611 funcref_T *funcref = &iptr->isn_arg.funcref;
6612 funcref_extra_T *extra = funcref->fr_extra;
6613 char_u *name;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006614
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006615 if (extra == NULL || extra->fre_func_name == NULL)
Bram Moolenaar38453522021-11-28 22:00:12 +00006616 {
6617 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
6618 + funcref->fr_dfunc_idx;
6619 name = df->df_ufunc->uf_name;
6620 }
6621 else
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006622 name = extra->fre_func_name;
Bram Moolenaarcc341812022-09-19 15:54:34 +01006623 if (extra == NULL || extra->fre_loopvar_info.lvi_depth == 0)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006624 smsg("%s%4d FUNCREF %s", pfx, current, name);
6625 else
Bram Moolenaarcc341812022-09-19 15:54:34 +01006626 {
6627 char_u *info = printable_loopvarinfo(
6628 &extra->fre_loopvar_info);
6629
6630 smsg("%s%4d FUNCREF %s vars %s", pfx, current,
6631 name, info);
6632 vim_free(info);
6633 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006634 }
6635 break;
6636
6637 case ISN_NEWFUNC:
6638 {
Bram Moolenaarcc341812022-09-19 15:54:34 +01006639 newfuncarg_T *arg = iptr->isn_arg.newfunc.nf_arg;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006640
Bram Moolenaarcc341812022-09-19 15:54:34 +01006641 if (arg->nfa_loopvar_info.lvi_depth == 0)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006642 smsg("%s%4d NEWFUNC %s %s", pfx, current,
6643 arg->nfa_lambda, arg->nfa_global);
6644 else
Bram Moolenaarcc341812022-09-19 15:54:34 +01006645 {
6646 char_u *info = printable_loopvarinfo(
6647 &arg->nfa_loopvar_info);
6648
6649 smsg("%s%4d NEWFUNC %s %s vars %s", pfx, current,
6650 arg->nfa_lambda, arg->nfa_global, info);
6651 vim_free(info);
6652 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006653 }
6654 break;
6655
6656 case ISN_DEF:
6657 {
6658 char_u *name = iptr->isn_arg.string;
6659
6660 smsg("%s%4d DEF %s", pfx, current,
6661 name == NULL ? (char_u *)"" : name);
6662 }
6663 break;
6664
6665 case ISN_JUMP:
6666 {
6667 char *when = "?";
6668
6669 switch (iptr->isn_arg.jump.jump_when)
6670 {
6671 case JUMP_ALWAYS:
6672 when = "JUMP";
6673 break;
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02006674 case JUMP_NEVER:
6675 iemsg("JUMP_NEVER should not be used");
6676 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006677 case JUMP_AND_KEEP_IF_TRUE:
6678 when = "JUMP_AND_KEEP_IF_TRUE";
6679 break;
6680 case JUMP_IF_FALSE:
6681 when = "JUMP_IF_FALSE";
6682 break;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01006683 case JUMP_WHILE_FALSE:
6684 when = "JUMP_WHILE_FALSE"; // unused
6685 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006686 case JUMP_IF_COND_FALSE:
6687 when = "JUMP_IF_COND_FALSE";
6688 break;
6689 case JUMP_IF_COND_TRUE:
6690 when = "JUMP_IF_COND_TRUE";
6691 break;
6692 }
6693 smsg("%s%4d %s -> %d", pfx, current, when,
6694 iptr->isn_arg.jump.jump_where);
6695 }
6696 break;
6697
6698 case ISN_JUMP_IF_ARG_SET:
6699 smsg("%s%4d JUMP_IF_ARG_SET arg[%d] -> %d", pfx, current,
6700 iptr->isn_arg.jumparg.jump_arg_off + STACK_FRAME_SIZE,
6701 iptr->isn_arg.jump.jump_where);
6702 break;
6703
Bram Moolenaar65b0d162022-12-13 18:43:22 +00006704 case ISN_JUMP_IF_ARG_NOT_SET:
6705 smsg("%s%4d JUMP_IF_ARG_NOT_SET arg[%d] -> %d", pfx, current,
6706 iptr->isn_arg.jumparg.jump_arg_off + STACK_FRAME_SIZE,
6707 iptr->isn_arg.jump.jump_where);
6708 break;
6709
Bram Moolenaar4c137212021-04-19 16:48:48 +02006710 case ISN_FOR:
6711 {
6712 forloop_T *forloop = &iptr->isn_arg.forloop;
6713
6714 smsg("%s%4d FOR $%d -> %d", pfx, current,
Bram Moolenaarcc341812022-09-19 15:54:34 +01006715 forloop->for_loop_idx, forloop->for_end);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006716 }
6717 break;
6718
Bram Moolenaarb46c0832022-09-15 17:19:37 +01006719 case ISN_ENDLOOP:
6720 {
6721 endloop_T *endloop = &iptr->isn_arg.endloop;
6722
Bram Moolenaarcc341812022-09-19 15:54:34 +01006723 smsg("%s%4d ENDLOOP ref $%d save $%d-$%d depth %d",
6724 pfx, current,
Bram Moolenaarb46c0832022-09-15 17:19:37 +01006725 endloop->end_funcref_idx,
6726 endloop->end_var_idx,
Bram Moolenaarcc341812022-09-19 15:54:34 +01006727 endloop->end_var_idx + endloop->end_var_count - 1,
6728 endloop->end_depth);
Bram Moolenaarb46c0832022-09-15 17:19:37 +01006729 }
6730 break;
6731
6732 case ISN_WHILE:
6733 {
6734 whileloop_T *whileloop = &iptr->isn_arg.whileloop;
6735
6736 smsg("%s%4d WHILE $%d -> %d", pfx, current,
6737 whileloop->while_funcref_idx,
6738 whileloop->while_end);
6739 }
6740 break;
6741
Bram Moolenaar4c137212021-04-19 16:48:48 +02006742 case ISN_TRY:
6743 {
Bram Moolenaar0d807102021-12-21 09:42:09 +00006744 try_T *try = &iptr->isn_arg.tryref;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006745
6746 if (try->try_ref->try_finally == 0)
6747 smsg("%s%4d TRY catch -> %d, endtry -> %d",
6748 pfx, current,
6749 try->try_ref->try_catch,
6750 try->try_ref->try_endtry);
6751 else
6752 smsg("%s%4d TRY catch -> %d, finally -> %d, endtry -> %d",
6753 pfx, current,
6754 try->try_ref->try_catch,
6755 try->try_ref->try_finally,
6756 try->try_ref->try_endtry);
6757 }
6758 break;
6759 case ISN_CATCH:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006760 smsg("%s%4d CATCH", pfx, current);
6761 break;
6762 case ISN_TRYCONT:
6763 {
6764 trycont_T *trycont = &iptr->isn_arg.trycont;
6765
6766 smsg("%s%4d TRY-CONTINUE %d level%s -> %d", pfx, current,
6767 trycont->tct_levels,
6768 trycont->tct_levels == 1 ? "" : "s",
6769 trycont->tct_where);
6770 }
6771 break;
6772 case ISN_FINALLY:
6773 smsg("%s%4d FINALLY", pfx, current);
6774 break;
6775 case ISN_ENDTRY:
6776 smsg("%s%4d ENDTRY", pfx, current);
6777 break;
6778 case ISN_THROW:
6779 smsg("%s%4d THROW", pfx, current);
6780 break;
6781
6782 // expression operations on number
6783 case ISN_OPNR:
6784 case ISN_OPFLOAT:
6785 case ISN_OPANY:
6786 {
6787 char *what;
6788 char *ins;
6789
6790 switch (iptr->isn_arg.op.op_type)
6791 {
6792 case EXPR_MULT: what = "*"; break;
6793 case EXPR_DIV: what = "/"; break;
6794 case EXPR_REM: what = "%"; break;
6795 case EXPR_SUB: what = "-"; break;
6796 case EXPR_ADD: what = "+"; break;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01006797 case EXPR_LSHIFT: what = "<<"; break;
6798 case EXPR_RSHIFT: what = ">>"; break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006799 default: what = "???"; break;
6800 }
6801 switch (iptr->isn_type)
6802 {
6803 case ISN_OPNR: ins = "OPNR"; break;
6804 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
6805 case ISN_OPANY: ins = "OPANY"; break;
6806 default: ins = "???"; break;
6807 }
6808 smsg("%s%4d %s %s", pfx, current, ins, what);
6809 }
6810 break;
6811
6812 case ISN_COMPAREBOOL:
6813 case ISN_COMPARESPECIAL:
Bram Moolenaar7a222242022-03-01 19:23:24 +00006814 case ISN_COMPARENULL:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006815 case ISN_COMPARENR:
6816 case ISN_COMPAREFLOAT:
6817 case ISN_COMPARESTRING:
6818 case ISN_COMPAREBLOB:
6819 case ISN_COMPARELIST:
6820 case ISN_COMPAREDICT:
6821 case ISN_COMPAREFUNC:
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00006822 case ISN_COMPARECLASS:
6823 case ISN_COMPAREOBJECT:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006824 case ISN_COMPAREANY:
6825 {
6826 char *p;
6827 char buf[10];
6828 char *type;
6829
6830 switch (iptr->isn_arg.op.op_type)
6831 {
6832 case EXPR_EQUAL: p = "=="; break;
6833 case EXPR_NEQUAL: p = "!="; break;
6834 case EXPR_GREATER: p = ">"; break;
6835 case EXPR_GEQUAL: p = ">="; break;
6836 case EXPR_SMALLER: p = "<"; break;
6837 case EXPR_SEQUAL: p = "<="; break;
6838 case EXPR_MATCH: p = "=~"; break;
6839 case EXPR_IS: p = "is"; break;
6840 case EXPR_ISNOT: p = "isnot"; break;
6841 case EXPR_NOMATCH: p = "!~"; break;
6842 default: p = "???"; break;
6843 }
6844 STRCPY(buf, p);
6845 if (iptr->isn_arg.op.op_ic == TRUE)
6846 strcat(buf, "?");
6847 switch(iptr->isn_type)
6848 {
6849 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
6850 case ISN_COMPARESPECIAL:
6851 type = "COMPARESPECIAL"; break;
Bram Moolenaar7a222242022-03-01 19:23:24 +00006852 case ISN_COMPARENULL: type = "COMPARENULL"; break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006853 case ISN_COMPARENR: type = "COMPARENR"; break;
6854 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
6855 case ISN_COMPARESTRING:
6856 type = "COMPARESTRING"; break;
6857 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
6858 case ISN_COMPARELIST: type = "COMPARELIST"; break;
6859 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
6860 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
Bram Moolenaarbcf31ec2023-01-02 20:32:24 +00006861 case ISN_COMPARECLASS: type = "COMPARECLASS"; break;
6862 case ISN_COMPAREOBJECT:
6863 type = "COMPAREOBJECT"; break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006864 case ISN_COMPAREANY: type = "COMPAREANY"; break;
6865 default: type = "???"; break;
6866 }
6867
6868 smsg("%s%4d %s %s", pfx, current, type, buf);
6869 }
6870 break;
6871
6872 case ISN_ADDLIST: smsg("%s%4d ADDLIST", pfx, current); break;
6873 case ISN_ADDBLOB: smsg("%s%4d ADDBLOB", pfx, current); break;
6874
6875 // expression operations
LemonBoy372bcce2022-04-25 12:43:20 +01006876 case ISN_CONCAT:
6877 smsg("%s%4d CONCAT size %lld", pfx, current,
6878 (varnumber_T)(iptr->isn_arg.number));
6879 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006880 case ISN_STRINDEX: smsg("%s%4d STRINDEX", pfx, current); break;
6881 case ISN_STRSLICE: smsg("%s%4d STRSLICE", pfx, current); break;
6882 case ISN_BLOBINDEX: smsg("%s%4d BLOBINDEX", pfx, current); break;
6883 case ISN_BLOBSLICE: smsg("%s%4d BLOBSLICE", pfx, current); break;
6884 case ISN_LISTAPPEND: smsg("%s%4d LISTAPPEND", pfx, current); break;
6885 case ISN_BLOBAPPEND: smsg("%s%4d BLOBAPPEND", pfx, current); break;
6886 case ISN_LISTINDEX: smsg("%s%4d LISTINDEX", pfx, current); break;
6887 case ISN_LISTSLICE: smsg("%s%4d LISTSLICE", pfx, current); break;
6888 case ISN_ANYINDEX: smsg("%s%4d ANYINDEX", pfx, current); break;
6889 case ISN_ANYSLICE: smsg("%s%4d ANYSLICE", pfx, current); break;
6890 case ISN_SLICE: smsg("%s%4d SLICE %lld",
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02006891 pfx, current, iptr->isn_arg.number); break;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02006892 case ISN_GETITEM: smsg("%s%4d ITEM %lld%s", pfx, current,
6893 iptr->isn_arg.getitem.gi_index,
6894 iptr->isn_arg.getitem.gi_with_op ?
6895 " with op" : ""); break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006896 case ISN_MEMBER: smsg("%s%4d MEMBER", pfx, current); break;
6897 case ISN_STRINGMEMBER: smsg("%s%4d MEMBER %s", pfx, current,
6898 iptr->isn_arg.string); break;
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00006899 case ISN_GET_OBJ_MEMBER: smsg("%s%4d OBJ_MEMBER %d", pfx, current,
6900 (int)iptr->isn_arg.number); break;
6901 case ISN_STORE_THIS: smsg("%s%4d STORE_THIS %d", pfx, current,
Bram Moolenaarffdaca92022-12-09 21:41:48 +00006902 (int)iptr->isn_arg.number); break;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02006903 case ISN_CLEARDICT: smsg("%s%4d CLEARDICT", pfx, current); break;
6904 case ISN_USEDICT: smsg("%s%4d USEDICT", pfx, current); break;
6905
Bram Moolenaar4c137212021-04-19 16:48:48 +02006906 case ISN_NEGATENR: smsg("%s%4d NEGATENR", pfx, current); break;
6907
Bram Moolenaar4c137212021-04-19 16:48:48 +02006908 case ISN_CHECKTYPE:
6909 {
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01006910 checktype_T *ct = &iptr->isn_arg.type;
Bram Moolenaarc6951a72022-12-29 20:56:24 +00006911 char *tofree = NULL;
6912 char *typename;
6913
6914 if (ct->ct_type->tt_type == VAR_FLOAT
6915 && (ct->ct_type->tt_flags & TTFLAG_NUMBER_OK))
6916 typename = "float|number";
6917 else
6918 typename = type_name(ct->ct_type, &tofree);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006919
6920 if (ct->ct_arg_idx == 0)
6921 smsg("%s%4d CHECKTYPE %s stack[%d]", pfx, current,
Bram Moolenaarc6951a72022-12-29 20:56:24 +00006922 typename,
Bram Moolenaar4c137212021-04-19 16:48:48 +02006923 (int)ct->ct_off);
6924 else
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01006925 smsg("%s%4d CHECKTYPE %s stack[%d] %s %d",
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02006926 pfx, current,
Bram Moolenaarc6951a72022-12-29 20:56:24 +00006927 typename,
Bram Moolenaar4c137212021-04-19 16:48:48 +02006928 (int)ct->ct_off,
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01006929 ct->ct_is_var ? "var": "arg",
Bram Moolenaar4c137212021-04-19 16:48:48 +02006930 (int)ct->ct_arg_idx);
6931 vim_free(tofree);
6932 break;
6933 }
6934 case ISN_CHECKLEN: smsg("%s%4d CHECKLEN %s%d", pfx, current,
6935 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
6936 iptr->isn_arg.checklen.cl_min_len);
6937 break;
6938 case ISN_SETTYPE:
6939 {
6940 char *tofree;
6941
6942 smsg("%s%4d SETTYPE %s", pfx, current,
6943 type_name(iptr->isn_arg.type.ct_type, &tofree));
6944 vim_free(tofree);
6945 break;
6946 }
6947 case ISN_COND2BOOL: smsg("%s%4d COND2BOOL", pfx, current); break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006948 case ISN_2BOOL: if (iptr->isn_arg.tobool.invert)
6949 smsg("%s%4d INVERT %d (!val)", pfx, current,
6950 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006951 else
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006952 smsg("%s%4d 2BOOL %d (!!val)", pfx, current,
6953 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006954 break;
6955 case ISN_2STRING: smsg("%s%4d 2STRING stack[%lld]", pfx, current,
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006956 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006957 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006958 case ISN_2STRING_ANY: smsg("%s%4d 2STRING_ANY stack[%lld]",
6959 pfx, current,
6960 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006961 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006962 case ISN_RANGE: smsg("%s%4d RANGE %s", pfx, current,
6963 iptr->isn_arg.string);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006964 break;
6965 case ISN_PUT:
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01006966 if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE)
Bram Moolenaar4c137212021-04-19 16:48:48 +02006967 smsg("%s%4d PUT %c above range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006968 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006969 else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE)
6970 smsg("%s%4d PUT %c range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006971 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006972 else
6973 smsg("%s%4d PUT %c %ld", pfx, current,
6974 iptr->isn_arg.put.put_regname,
6975 (long)iptr->isn_arg.put.put_lnum);
6976 break;
6977
Bram Moolenaar4c137212021-04-19 16:48:48 +02006978 case ISN_CMDMOD:
6979 {
6980 char_u *buf;
6981 size_t len = produce_cmdmods(
6982 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
6983
6984 buf = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02006985 if (likely(buf != NULL))
Bram Moolenaar4c137212021-04-19 16:48:48 +02006986 {
6987 (void)produce_cmdmods(
6988 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
6989 smsg("%s%4d CMDMOD %s", pfx, current, buf);
6990 vim_free(buf);
6991 }
6992 break;
6993 }
6994 case ISN_CMDMOD_REV: smsg("%s%4d CMDMOD_REV", pfx, current); break;
6995
6996 case ISN_PROF_START:
Bram Moolenaare99d4222021-06-13 14:01:26 +02006997 smsg("%s%4d PROFILE START line %d", pfx, current,
6998 iptr->isn_lnum);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006999 break;
7000
7001 case ISN_PROF_END:
7002 smsg("%s%4d PROFILE END", pfx, current);
7003 break;
7004
Bram Moolenaare99d4222021-06-13 14:01:26 +02007005 case ISN_DEBUG:
Bram Moolenaar8cec9272021-06-23 20:20:53 +02007006 smsg("%s%4d DEBUG line %d-%d varcount %lld", pfx, current,
7007 iptr->isn_arg.debug.dbg_break_lnum + 1,
7008 iptr->isn_lnum,
7009 iptr->isn_arg.debug.dbg_var_names_len);
Bram Moolenaare99d4222021-06-13 14:01:26 +02007010 break;
7011
Bram Moolenaar4c137212021-04-19 16:48:48 +02007012 case ISN_UNPACK: smsg("%s%4d UNPACK %d%s", pfx, current,
7013 iptr->isn_arg.unpack.unp_count,
7014 iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : "");
7015 break;
7016 case ISN_SHUFFLE: smsg("%s%4d SHUFFLE %d up %d", pfx, current,
7017 iptr->isn_arg.shuffle.shfl_item,
7018 iptr->isn_arg.shuffle.shfl_up);
7019 break;
7020 case ISN_DROP: smsg("%s%4d DROP", pfx, current); break;
7021
7022 case ISN_FINISH: // End of list of instructions for ISN_SUBSTITUTE.
7023 return;
7024 }
7025
7026 out_flush(); // output one line at a time
7027 ui_breakcheck();
7028 if (got_int)
7029 break;
7030 }
7031}
7032
7033/*
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02007034 * Handle command line completion for the :disassemble command.
7035 */
7036 void
7037set_context_in_disassemble_cmd(expand_T *xp, char_u *arg)
7038{
7039 char_u *p;
7040
7041 // Default: expand user functions, "debug" and "profile"
7042 xp->xp_context = EXPAND_DISASSEMBLE;
7043 xp->xp_pattern = arg;
7044
7045 // first argument already typed: only user function names
7046 if (*arg != NUL && *(p = skiptowhite(arg)) != NUL)
7047 {
7048 xp->xp_context = EXPAND_USER_FUNC;
7049 xp->xp_pattern = skipwhite(p);
7050 }
7051}
7052
7053/*
7054 * Function given to ExpandGeneric() to obtain the list of :disassemble
7055 * arguments.
7056 */
7057 char_u *
7058get_disassemble_argument(expand_T *xp, int idx)
7059{
7060 if (idx == 0)
7061 return (char_u *)"debug";
7062 if (idx == 1)
7063 return (char_u *)"profile";
7064 return get_user_func_name(xp, idx - 2);
7065}
7066
7067/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01007068 * ":disassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01007069 * We don't really need this at runtime, but we do have tests that require it,
7070 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007071 */
7072 void
7073ex_disassemble(exarg_T *eap)
7074{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01007075 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01007076 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007077 dfunc_T *dfunc;
Bram Moolenaardafef512022-05-21 21:55:55 +01007078 isn_T *instr = NULL; // init to shut up gcc warning
7079 int instr_count = 0; // init to shut up gcc warning
Bram Moolenaar5a01caa2022-05-21 18:56:58 +01007080 compiletype_T compile_type = CT_NONE;
Bram Moolenaare99d4222021-06-13 14:01:26 +02007081
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01007082 ufunc = find_func_by_name(arg, &compile_type);
Bram Moolenaara26b9702020-04-18 19:53:28 +02007083 if (ufunc == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007084 return;
Bram Moolenaare99d4222021-06-13 14:01:26 +02007085 if (func_needs_compiling(ufunc, compile_type)
7086 && compile_def_function(ufunc, FALSE, compile_type, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02007087 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007088 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007089 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007090 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007091 return;
7092 }
Bram Moolenaar6db660b2021-08-01 14:08:54 +02007093 msg((char *)printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007094
7095 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
Bram Moolenaare99d4222021-06-13 14:01:26 +02007096 switch (compile_type)
7097 {
7098 case CT_PROFILE:
Bram Moolenaarf002a412021-01-24 13:34:18 +01007099#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02007100 instr = dfunc->df_instr_prof;
7101 instr_count = dfunc->df_instr_prof_count;
7102 break;
Bram Moolenaarf002a412021-01-24 13:34:18 +01007103#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02007104 // FALLTHROUGH
7105 case CT_NONE:
7106 instr = dfunc->df_instr;
7107 instr_count = dfunc->df_instr_count;
7108 break;
7109 case CT_DEBUG:
7110 instr = dfunc->df_instr_debug;
7111 instr_count = dfunc->df_instr_debug_count;
7112 break;
7113 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007114
Bram Moolenaar4c137212021-04-19 16:48:48 +02007115 list_instructions("", instr, instr_count, ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007116}
7117
7118/*
Bram Moolenaar13106602020-10-04 16:06:05 +02007119 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007120 * list, etc. Mostly like what JavaScript does, except that empty list and
7121 * empty dictionary are FALSE.
7122 */
7123 int
7124tv2bool(typval_T *tv)
7125{
7126 switch (tv->v_type)
7127 {
7128 case VAR_NUMBER:
7129 return tv->vval.v_number != 0;
7130 case VAR_FLOAT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007131 return tv->vval.v_float != 0.0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007132 case VAR_PARTIAL:
7133 return tv->vval.v_partial != NULL;
7134 case VAR_FUNC:
7135 case VAR_STRING:
7136 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
7137 case VAR_LIST:
7138 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
7139 case VAR_DICT:
7140 return tv->vval.v_dict != NULL
7141 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
7142 case VAR_BOOL:
7143 case VAR_SPECIAL:
7144 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
7145 case VAR_JOB:
7146#ifdef FEAT_JOB_CHANNEL
7147 return tv->vval.v_job != NULL;
7148#else
7149 break;
7150#endif
7151 case VAR_CHANNEL:
7152#ifdef FEAT_JOB_CHANNEL
7153 return tv->vval.v_channel != NULL;
7154#else
7155 break;
7156#endif
7157 case VAR_BLOB:
7158 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
7159 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02007160 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007161 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02007162 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00007163 case VAR_CLASS:
7164 case VAR_OBJECT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007165 break;
7166 }
7167 return FALSE;
7168}
7169
Bram Moolenaarea2d4072020-11-12 12:08:51 +01007170 void
7171emsg_using_string_as(typval_T *tv, int as_number)
7172{
7173 semsg(_(as_number ? e_using_string_as_number_str
7174 : e_using_string_as_bool_str),
7175 tv->vval.v_string == NULL
7176 ? (char_u *)"" : tv->vval.v_string);
7177}
7178
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007179/*
7180 * If "tv" is a string give an error and return FAIL.
7181 */
7182 int
7183check_not_string(typval_T *tv)
7184{
7185 if (tv->v_type == VAR_STRING)
7186 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +01007187 emsg_using_string_as(tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007188 clear_tv(tv);
7189 return FAIL;
7190 }
7191 return OK;
7192}
7193
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007194
7195#endif // FEAT_EVAL