blob: 43bae4417d227b6baf00d78b6951e4745618dcc2 [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:
4700 {
4701 typval_T *tv1 = STACK_TV_BOT(-2);
4702 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar265f8112021-12-19 12:33:05 +00004703 exprtype_T exprtype = iptr->isn_arg.op.op_type;
4704 int ic = iptr->isn_arg.op.op_ic;
4705 int res = FALSE;
4706 int status = OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004707
Bram Moolenaar265f8112021-12-19 12:33:05 +00004708 SOURCING_LNUM = iptr->isn_lnum;
4709 if (iptr->isn_type == ISN_COMPARELIST)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004710 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00004711 status = typval_compare_list(tv1, tv2,
4712 exprtype, ic, &res);
4713 }
4714 else if (iptr->isn_type == ISN_COMPAREDICT)
4715 {
4716 status = typval_compare_dict(tv1, tv2,
4717 exprtype, ic, &res);
4718 }
4719 else if (iptr->isn_type == ISN_COMPAREFUNC)
4720 {
4721 status = typval_compare_func(tv1, tv2,
4722 exprtype, ic, &res);
4723 }
4724 else if (iptr->isn_type == ISN_COMPARESTRING)
4725 {
4726 status = typval_compare_string(tv1, tv2,
4727 exprtype, ic, &res);
4728 }
4729 else
4730 {
4731 status = typval_compare_blob(tv1, tv2, exprtype, &res);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004732 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004733 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004734 clear_tv(tv1);
4735 clear_tv(tv2);
4736 tv1->v_type = VAR_BOOL;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004737 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4738 if (status == FAIL)
4739 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004740 }
4741 break;
4742
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004743 case ISN_COMPAREANY:
4744 {
4745 typval_T *tv1 = STACK_TV_BOT(-2);
4746 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar657137c2021-01-09 15:45:23 +01004747 exprtype_T exprtype = iptr->isn_arg.op.op_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004748 int ic = iptr->isn_arg.op.op_ic;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004749 int status;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004750
Bram Moolenaareb26f432020-09-14 16:50:05 +02004751 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004752 status = typval_compare(tv1, tv2, exprtype, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004753 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004754 --ectx->ec_stack.ga_len;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004755 if (status == FAIL)
4756 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004757 }
4758 break;
4759
4760 case ISN_ADDLIST:
4761 case ISN_ADDBLOB:
4762 {
4763 typval_T *tv1 = STACK_TV_BOT(-2);
4764 typval_T *tv2 = STACK_TV_BOT(-1);
4765
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004766 // add two lists or blobs
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004767 if (iptr->isn_type == ISN_ADDLIST)
Bram Moolenaar07802042021-09-09 23:01:14 +02004768 {
4769 if (iptr->isn_arg.op.op_type == EXPR_APPEND
4770 && tv1->vval.v_list != NULL)
4771 list_extend(tv1->vval.v_list, tv2->vval.v_list,
4772 NULL);
4773 else
4774 eval_addlist(tv1, tv2);
4775 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004776 else
4777 eval_addblob(tv1, tv2);
4778 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004779 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004780 }
4781 break;
4782
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004783 case ISN_LISTAPPEND:
4784 {
4785 typval_T *tv1 = STACK_TV_BOT(-2);
4786 typval_T *tv2 = STACK_TV_BOT(-1);
4787 list_T *l = tv1->vval.v_list;
4788
4789 // add an item to a list
Bram Moolenaar1f4a3452022-01-01 18:29:21 +00004790 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004791 if (l == NULL)
4792 {
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004793 emsg(_(e_cannot_add_to_null_list));
4794 goto on_error;
4795 }
Bram Moolenaar1f4a3452022-01-01 18:29:21 +00004796 if (value_check_lock(l->lv_lock, NULL, FALSE))
4797 goto on_error;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004798 if (list_append_tv(l, tv2) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004799 goto theend;
Bram Moolenaar955347c2020-10-19 23:01:46 +02004800 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004801 --ectx->ec_stack.ga_len;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004802 }
4803 break;
4804
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02004805 case ISN_BLOBAPPEND:
4806 {
4807 typval_T *tv1 = STACK_TV_BOT(-2);
4808 typval_T *tv2 = STACK_TV_BOT(-1);
4809 blob_T *b = tv1->vval.v_blob;
4810 int error = FALSE;
4811 varnumber_T n;
4812
4813 // add a number to a blob
4814 if (b == NULL)
4815 {
4816 SOURCING_LNUM = iptr->isn_lnum;
4817 emsg(_(e_cannot_add_to_null_blob));
4818 goto on_error;
4819 }
4820 n = tv_get_number_chk(tv2, &error);
4821 if (error)
4822 goto on_error;
4823 ga_append(&b->bv_ga, (int)n);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004824 --ectx->ec_stack.ga_len;
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02004825 }
4826 break;
4827
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004828 // Computation with two arguments of unknown type
4829 case ISN_OPANY:
4830 {
4831 typval_T *tv1 = STACK_TV_BOT(-2);
4832 typval_T *tv2 = STACK_TV_BOT(-1);
4833 varnumber_T n1, n2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004834 float_T f1 = 0, f2 = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004835 int error = FALSE;
4836
4837 if (iptr->isn_arg.op.op_type == EXPR_ADD)
4838 {
4839 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
4840 {
4841 eval_addlist(tv1, tv2);
4842 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004843 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004844 break;
4845 }
4846 else if (tv1->v_type == VAR_BLOB
4847 && tv2->v_type == VAR_BLOB)
4848 {
4849 eval_addblob(tv1, tv2);
4850 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004851 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004852 break;
4853 }
4854 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004855 if (tv1->v_type == VAR_FLOAT)
4856 {
4857 f1 = tv1->vval.v_float;
4858 n1 = 0;
4859 }
4860 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004861 {
Bram Moolenaarf665e972020-12-05 19:17:16 +01004862 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004863 n1 = tv_get_number_chk(tv1, &error);
4864 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02004865 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004866 if (tv2->v_type == VAR_FLOAT)
4867 f1 = n1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004868 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004869 if (tv2->v_type == VAR_FLOAT)
4870 {
4871 f2 = tv2->vval.v_float;
4872 n2 = 0;
4873 }
4874 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004875 {
4876 n2 = tv_get_number_chk(tv2, &error);
4877 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02004878 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004879 if (tv1->v_type == VAR_FLOAT)
4880 f2 = n2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004881 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004882 // if there is a float on either side the result is a float
4883 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
4884 {
4885 switch (iptr->isn_arg.op.op_type)
4886 {
4887 case EXPR_MULT: f1 = f1 * f2; break;
4888 case EXPR_DIV: f1 = f1 / f2; break;
4889 case EXPR_SUB: f1 = f1 - f2; break;
4890 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004891 default: SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004892 emsg(_(e_cannot_use_percent_with_float));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004893 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004894 }
4895 clear_tv(tv1);
4896 clear_tv(tv2);
4897 tv1->v_type = VAR_FLOAT;
4898 tv1->vval.v_float = f1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004899 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004900 }
4901 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004902 {
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004903 int failed = FALSE;
4904
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004905 switch (iptr->isn_arg.op.op_type)
4906 {
4907 case EXPR_MULT: n1 = n1 * n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004908 case EXPR_DIV: n1 = num_divide(n1, n2, &failed);
4909 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01004910 goto on_error;
4911 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004912 case EXPR_SUB: n1 = n1 - n2; break;
4913 case EXPR_ADD: n1 = n1 + n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004914 default: n1 = num_modulus(n1, n2, &failed);
4915 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01004916 goto on_error;
4917 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004918 }
4919 clear_tv(tv1);
4920 clear_tv(tv2);
4921 tv1->v_type = VAR_NUMBER;
4922 tv1->vval.v_number = n1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004923 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004924 }
4925 }
4926 break;
4927
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004928 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004929 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004930 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004931 int is_slice = iptr->isn_type == ISN_STRSLICE;
4932 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004933 char_u *res;
4934
4935 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004936 // string slice: string is at stack-3, first index at
4937 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004938 if (is_slice)
4939 {
4940 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004941 n1 = tv->vval.v_number;
4942 }
4943
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004944 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004945 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004946
Bram Moolenaar4c137212021-04-19 16:48:48 +02004947 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004948 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004949 if (is_slice)
4950 // Slice: Select the characters from the string
Bram Moolenaar6601b622021-01-13 21:47:15 +01004951 res = string_slice(tv->vval.v_string, n1, n2, FALSE);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004952 else
4953 // Index: The resulting variable is a string of a
Bram Moolenaar0289a092021-03-14 18:40:19 +01004954 // single character (including composing characters).
4955 // If the index is too big or negative the result is
4956 // empty.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004957 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004958 vim_free(tv->vval.v_string);
4959 tv->vval.v_string = res;
4960 }
4961 break;
4962
4963 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02004964 case ISN_LISTSLICE:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004965 case ISN_BLOBINDEX:
4966 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004967 {
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004968 int is_slice = iptr->isn_type == ISN_LISTSLICE
4969 || iptr->isn_type == ISN_BLOBSLICE;
4970 int is_blob = iptr->isn_type == ISN_BLOBINDEX
4971 || iptr->isn_type == ISN_BLOBSLICE;
Bram Moolenaared591872020-08-15 22:14:53 +02004972 varnumber_T n1, n2;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004973 typval_T *val_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004974
4975 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02004976 // list slice: list is at stack-3, indexes at stack-2 and
4977 // stack-1
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004978 // Same for blob.
4979 val_tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004980
4981 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02004982 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004983 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02004984
4985 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004986 {
Bram Moolenaared591872020-08-15 22:14:53 +02004987 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02004988 n1 = tv->vval.v_number;
4989 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004990 }
Bram Moolenaared591872020-08-15 22:14:53 +02004991
Bram Moolenaar4c137212021-04-19 16:48:48 +02004992 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02004993 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02004994 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004995 if (is_blob)
4996 {
4997 if (blob_slice_or_index(val_tv->vval.v_blob, is_slice,
4998 n1, n2, FALSE, tv) == FAIL)
4999 goto on_error;
5000 }
5001 else
5002 {
5003 if (list_slice_or_index(val_tv->vval.v_list, is_slice,
5004 n1, n2, FALSE, tv, TRUE) == FAIL)
5005 goto on_error;
5006 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005007 }
5008 break;
5009
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005010 case ISN_ANYINDEX:
5011 case ISN_ANYSLICE:
5012 {
5013 int is_slice = iptr->isn_type == ISN_ANYSLICE;
5014 typval_T *var1, *var2;
5015 int res;
5016
5017 // index: composite is at stack-2, index at stack-1
5018 // slice: composite is at stack-3, indexes at stack-2 and
5019 // stack-1
5020 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02005021 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005022 if (check_can_index(tv, TRUE, TRUE) == FAIL)
5023 goto on_error;
5024 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
5025 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
Bram Moolenaar6601b622021-01-13 21:47:15 +01005026 res = eval_index_inner(tv, is_slice, var1, var2,
5027 FALSE, NULL, -1, TRUE);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005028 clear_tv(var1);
5029 if (is_slice)
5030 clear_tv(var2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005031 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005032 if (res == FAIL)
5033 goto on_error;
5034 }
5035 break;
5036
Bram Moolenaar9af78762020-06-16 11:34:42 +02005037 case ISN_SLICE:
5038 {
5039 list_T *list;
5040 int count = iptr->isn_arg.number;
5041
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02005042 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02005043 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02005044 list = tv->vval.v_list;
5045
5046 // no error for short list, expect it to be checked earlier
5047 if (list != NULL && list->lv_len >= count)
5048 {
5049 list_T *newlist = list_slice(list,
5050 count, list->lv_len - 1);
5051
5052 if (newlist != NULL)
5053 {
5054 list_unref(list);
5055 tv->vval.v_list = newlist;
5056 ++newlist->lv_refcount;
5057 }
5058 }
5059 }
5060 break;
5061
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005062 case ISN_GETITEM:
5063 {
5064 listitem_T *li;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02005065 getitem_T *gi = &iptr->isn_arg.getitem;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005066
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02005067 // Get list item: list is at stack-1, push item.
5068 // List type and length is checked for when compiling.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02005069 tv = STACK_TV_BOT(-1 - gi->gi_with_op);
5070 li = list_find(tv->vval.v_list, gi->gi_index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005071
Bram Moolenaar35578162021-08-02 19:10:38 +02005072 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005073 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005074 ++ectx->ec_stack.ga_len;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005075 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
Bram Moolenaarf785aa12021-02-11 21:19:34 +01005076
5077 // Useful when used in unpack assignment. Reset at
5078 // ISN_DROP.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02005079 ectx->ec_where.wt_index = gi->gi_index + 1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005080 ectx->ec_where.wt_variable = TRUE;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005081 }
5082 break;
5083
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005084 case ISN_MEMBER:
5085 {
5086 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005087 char_u *key;
5088 dictitem_T *di;
5089
5090 // dict member: dict is at stack-2, key at stack-1
5091 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02005092 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005093 dict = tv->vval.v_dict;
5094
5095 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02005096 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005097 key = tv->vval.v_string;
Bram Moolenaar086fc9a2020-10-30 18:33:02 +01005098 if (key == NULL)
5099 key = (char_u *)"";
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02005100
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005101 if ((di = dict_find(dict, key, -1)) == NULL)
5102 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005103 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00005104 semsg(_(e_key_not_present_in_dictionary_str), key);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01005105
5106 // If :silent! is used we will continue, make sure the
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005107 // stack contents makes sense and the dict stack is
5108 // updated.
Bram Moolenaar4029cab2020-12-05 18:13:27 +01005109 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005110 --ectx->ec_stack.ga_len;
Bram Moolenaar4029cab2020-12-05 18:13:27 +01005111 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005112 (void) dict_stack_save(tv);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01005113 tv->v_type = VAR_NUMBER;
5114 tv->vval.v_number = 0;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01005115 goto on_fatal_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005116 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005117 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005118 --ectx->ec_stack.ga_len;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005119 // Put the dict used on the dict stack, it might be used by
5120 // a dict function later.
Bram Moolenaar50788ef2020-07-05 16:51:26 +02005121 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005122 if (dict_stack_save(tv) == FAIL)
5123 goto on_fatal_error;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02005124 copy_tv(&di->di_tv, tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005125 }
5126 break;
5127
5128 // dict member with string key
5129 case ISN_STRINGMEMBER:
5130 {
5131 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005132 dictitem_T *di;
5133
5134 tv = STACK_TV_BOT(-1);
5135 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
5136 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005137 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005138 emsg(_(e_dictionary_required));
Bram Moolenaard032f342020-07-18 18:13:02 +02005139 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005140 }
5141 dict = tv->vval.v_dict;
5142
5143 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
5144 == NULL)
5145 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005146 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00005147 semsg(_(e_key_not_present_in_dictionary_str),
Bram Moolenaar381692b2022-02-02 20:01:27 +00005148 iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02005149 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005150 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005151 // Put the dict used on the dict stack, it might be used by
5152 // a dict function later.
5153 if (dict_stack_save(tv) == FAIL)
5154 goto on_fatal_error;
5155
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005156 copy_tv(&di->di_tv, tv);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005157 }
5158 break;
5159
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005160 case ISN_GET_OBJ_MEMBER:
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005161 {
5162 tv = STACK_TV_BOT(-1);
5163 if (tv->v_type != VAR_OBJECT)
5164 {
5165 SOURCING_LNUM = iptr->isn_lnum;
5166 garray_T type_list;
5167 ga_init2(&type_list, sizeof(type_T *), 10);
5168 type_T *type = typval2type(tv, get_copyID(),
5169 &type_list, TVTT_DO_MEMBER);
5170 char *tofree = NULL;
5171 char *typename = type_name(type, &tofree);
5172 semsg(_(e_object_required_found_str), typename);
5173 vim_free(tofree);
5174 clear_type_list(&type_list);
5175 goto on_error;
5176 }
5177 int idx = iptr->isn_arg.number;
5178 object_T *obj = tv->vval.v_object;
5179 // the members are located right after the object struct
5180 typval_T *mtv = ((typval_T *)(obj + 1)) + idx;
Bram Moolenaar590162c2022-12-24 21:24:06 +00005181 copy_tv(mtv, tv);
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005182
5183 // Unreference the object after getting the member, it may
5184 // be freed.
5185 object_unref(obj);
5186 }
5187 break;
5188
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005189 case ISN_STORE_THIS:
5190 {
5191 int idx = iptr->isn_arg.number;
5192 object_T *obj = STACK_TV_VAR(0)->vval.v_object;
5193 // the members are located right after the object struct
5194 typval_T *mtv = ((typval_T *)(obj + 1)) + idx;
5195 clear_tv(mtv);
5196 *mtv = *STACK_TV_BOT(-1);
5197 --ectx->ec_stack.ga_len;
5198 }
5199 break;
5200
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005201 case ISN_CLEARDICT:
5202 dict_stack_drop();
5203 break;
5204
5205 case ISN_USEDICT:
5206 {
5207 typval_T *dict_tv = dict_stack_get_tv();
5208
5209 // Turn "dict.Func" into a partial for "Func" bound to
5210 // "dict". Don't do this when "Func" is already a partial
5211 // that was bound explicitly (pt_auto is FALSE).
5212 tv = STACK_TV_BOT(-1);
5213 if (dict_tv != NULL
5214 && dict_tv->v_type == VAR_DICT
5215 && dict_tv->vval.v_dict != NULL
5216 && (tv->v_type == VAR_FUNC
5217 || (tv->v_type == VAR_PARTIAL
5218 && (tv->vval.v_partial->pt_auto
5219 || tv->vval.v_partial->pt_dict == NULL))))
5220 dict_tv->vval.v_dict =
5221 make_partial(dict_tv->vval.v_dict, tv);
5222 dict_stack_drop();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005223 }
5224 break;
5225
5226 case ISN_NEGATENR:
5227 tv = STACK_TV_BOT(-1);
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00005228 // CHECKTYPE should have checked the variable type
Bram Moolenaarc58164c2020-03-29 18:40:30 +02005229 if (tv->v_type == VAR_FLOAT)
5230 tv->vval.v_float = -tv->vval.v_float;
5231 else
Bram Moolenaarc58164c2020-03-29 18:40:30 +02005232 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005233 break;
5234
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005235 case ISN_CHECKTYPE:
5236 {
5237 checktype_T *ct = &iptr->isn_arg.type;
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01005238 int save_wt_variable = ectx->ec_where.wt_variable;
Bram Moolenaarb1040dc2022-05-18 11:00:48 +01005239 int r;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005240
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01005241 tv = STACK_TV_BOT((int)ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02005242 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005243 if (!ectx->ec_where.wt_variable)
5244 ectx->ec_where.wt_index = ct->ct_arg_idx;
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01005245 ectx->ec_where.wt_variable = ct->ct_is_var;
Bram Moolenaarb1040dc2022-05-18 11:00:48 +01005246 r = check_typval_type(ct->ct_type, tv, ectx->ec_where);
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01005247 ectx->ec_where.wt_variable = save_wt_variable;
Bram Moolenaarb1040dc2022-05-18 11:00:48 +01005248 if (r == FAIL)
5249 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005250 if (!ectx->ec_where.wt_variable)
5251 ectx->ec_where.wt_index = 0;
Bram Moolenaar5e654232020-09-16 15:22:00 +02005252
5253 // number 0 is FALSE, number 1 is TRUE
5254 if (tv->v_type == VAR_NUMBER
5255 && ct->ct_type->tt_type == VAR_BOOL
5256 && (tv->vval.v_number == 0
5257 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005258 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02005259 tv->v_type = VAR_BOOL;
5260 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02005261 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005262 }
5263 }
5264 break;
5265
Bram Moolenaar9af78762020-06-16 11:34:42 +02005266 case ISN_CHECKLEN:
5267 {
5268 int min_len = iptr->isn_arg.checklen.cl_min_len;
5269 list_T *list = NULL;
5270
5271 tv = STACK_TV_BOT(-1);
5272 if (tv->v_type == VAR_LIST)
5273 list = tv->vval.v_list;
5274 if (list == NULL || list->lv_len < min_len
5275 || (list->lv_len > min_len
5276 && !iptr->isn_arg.checklen.cl_more_OK))
5277 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005278 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005279 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02005280 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02005281 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02005282 }
5283 }
5284 break;
5285
Bram Moolenaaraa210a32021-01-02 15:41:03 +01005286 case ISN_SETTYPE:
Bram Moolenaar381692b2022-02-02 20:01:27 +00005287 set_tv_type(STACK_TV_BOT(-1), iptr->isn_arg.type.ct_type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01005288 break;
5289
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005290 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005291 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005292 {
5293 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005294 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005295
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005296 if (iptr->isn_type == ISN_2BOOL)
5297 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005298 tv = STACK_TV_BOT(iptr->isn_arg.tobool.offset);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005299 n = tv2bool(tv);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005300 if (iptr->isn_arg.tobool.invert)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005301 n = !n;
5302 }
5303 else
5304 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005305 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005306 SOURCING_LNUM = iptr->isn_lnum;
5307 n = tv_get_bool_chk(tv, &error);
5308 if (error)
5309 goto on_error;
5310 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005311 clear_tv(tv);
5312 tv->v_type = VAR_BOOL;
5313 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
5314 }
5315 break;
5316
5317 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02005318 case ISN_2STRING_ANY:
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01005319 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005320 if (do_2string(STACK_TV_BOT(iptr->isn_arg.tostring.offset),
5321 iptr->isn_type == ISN_2STRING_ANY,
5322 iptr->isn_arg.tostring.tolerant) == FAIL)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01005323 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005324 break;
5325
Bram Moolenaar08597872020-12-10 19:43:40 +01005326 case ISN_RANGE:
5327 {
5328 exarg_T ea;
5329 char *errormsg;
5330
Bram Moolenaarece0b872021-01-08 20:40:45 +01005331 ea.line2 = 0;
Bram Moolenaard1510ee2021-01-04 16:15:58 +01005332 ea.addr_count = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01005333 ea.addr_type = ADDR_LINES;
5334 ea.cmd = iptr->isn_arg.string;
Bram Moolenaarece0b872021-01-08 20:40:45 +01005335 ea.skip = FALSE;
Bram Moolenaar08597872020-12-10 19:43:40 +01005336 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaarece0b872021-01-08 20:40:45 +01005337 goto on_error;
Bram Moolenaara28639e2021-01-19 22:48:09 +01005338
Bram Moolenaar35578162021-08-02 19:10:38 +02005339 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005340 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005341 ++ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01005342 tv = STACK_TV_BOT(-1);
5343 tv->v_type = VAR_NUMBER;
5344 tv->v_lock = 0;
Bram Moolenaar06651632022-04-27 17:54:25 +01005345 tv->vval.v_number = ea.line2;
Bram Moolenaar08597872020-12-10 19:43:40 +01005346 }
5347 break;
5348
Bram Moolenaarc3516f72020-09-08 22:45:35 +02005349 case ISN_PUT:
5350 {
5351 int regname = iptr->isn_arg.put.put_regname;
5352 linenr_T lnum = iptr->isn_arg.put.put_lnum;
5353 char_u *expr = NULL;
5354 int dir = FORWARD;
5355
Bram Moolenaar08597872020-12-10 19:43:40 +01005356 if (lnum < -2)
5357 {
5358 // line number was put on the stack by ISN_RANGE
5359 tv = STACK_TV_BOT(-1);
5360 curwin->w_cursor.lnum = tv->vval.v_number;
5361 if (lnum == LNUM_VARIABLE_RANGE_ABOVE)
5362 dir = BACKWARD;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005363 --ectx->ec_stack.ga_len;
Bram Moolenaar08597872020-12-10 19:43:40 +01005364 }
5365 else if (lnum == -2)
Bram Moolenaarc3516f72020-09-08 22:45:35 +02005366 // :put! above cursor
5367 dir = BACKWARD;
5368 else if (lnum >= 0)
Bram Moolenaar4e713ba2022-02-07 15:31:37 +00005369 {
5370 curwin->w_cursor.lnum = lnum;
5371 if (lnum == 0)
5372 // check_cursor() below will move to line 1
5373 dir = BACKWARD;
5374 }
Bram Moolenaara28639e2021-01-19 22:48:09 +01005375
5376 if (regname == '=')
5377 {
5378 tv = STACK_TV_BOT(-1);
5379 if (tv->v_type == VAR_STRING)
5380 expr = tv->vval.v_string;
5381 else
5382 {
5383 expr = typval2string(tv, TRUE); // allocates value
5384 clear_tv(tv);
5385 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005386 --ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01005387 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02005388 check_cursor();
5389 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
5390 vim_free(expr);
5391 }
5392 break;
5393
Bram Moolenaar02194d22020-10-24 23:08:38 +02005394 case ISN_CMDMOD:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005395 ectx->ec_funclocal.floc_save_cmdmod = cmdmod;
5396 ectx->ec_funclocal.floc_restore_cmdmod = TRUE;
5397 ectx->ec_funclocal.floc_restore_cmdmod_stacklen =
5398 ectx->ec_stack.ga_len;
Bram Moolenaar02194d22020-10-24 23:08:38 +02005399 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
5400 apply_cmdmod(&cmdmod);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02005401 break;
5402
Bram Moolenaar02194d22020-10-24 23:08:38 +02005403 case ISN_CMDMOD_REV:
5404 // filter regprog is owned by the instruction, don't free it
5405 cmdmod.cmod_filter_regmatch.regprog = NULL;
5406 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005407 cmdmod = ectx->ec_funclocal.floc_save_cmdmod;
5408 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02005409 break;
5410
Bram Moolenaar792f7862020-11-23 08:31:18 +01005411 case ISN_UNPACK:
5412 {
5413 int count = iptr->isn_arg.unpack.unp_count;
5414 int semicolon = iptr->isn_arg.unpack.unp_semicolon;
5415 list_T *l;
5416 listitem_T *li;
5417 int i;
5418
5419 // Check there is a valid list to unpack.
5420 tv = STACK_TV_BOT(-1);
5421 if (tv->v_type != VAR_LIST)
5422 {
5423 SOURCING_LNUM = iptr->isn_lnum;
5424 emsg(_(e_for_argument_must_be_sequence_of_lists));
5425 goto on_error;
5426 }
5427 l = tv->vval.v_list;
5428 if (l == NULL
5429 || l->lv_len < (semicolon ? count - 1 : count))
5430 {
5431 SOURCING_LNUM = iptr->isn_lnum;
5432 emsg(_(e_list_value_does_not_have_enough_items));
5433 goto on_error;
5434 }
5435 else if (!semicolon && l->lv_len > count)
5436 {
5437 SOURCING_LNUM = iptr->isn_lnum;
5438 emsg(_(e_list_value_has_more_items_than_targets));
5439 goto on_error;
5440 }
5441
5442 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar35578162021-08-02 19:10:38 +02005443 if (GA_GROW_FAILS(&ectx->ec_stack, count - 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005444 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005445 ectx->ec_stack.ga_len += count - 1;
Bram Moolenaar792f7862020-11-23 08:31:18 +01005446
5447 // Variable after semicolon gets a list with the remaining
5448 // items.
5449 if (semicolon)
5450 {
5451 list_T *rem_list =
5452 list_alloc_with_items(l->lv_len - count + 1);
5453
5454 if (rem_list == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005455 goto theend;
Bram Moolenaar792f7862020-11-23 08:31:18 +01005456 tv = STACK_TV_BOT(-count);
5457 tv->vval.v_list = rem_list;
5458 ++rem_list->lv_refcount;
5459 tv->v_lock = 0;
5460 li = l->lv_first;
5461 for (i = 0; i < count - 1; ++i)
5462 li = li->li_next;
5463 for (i = 0; li != NULL; ++i)
5464 {
Bram Moolenaar61efa162022-03-18 13:10:48 +00005465 typval_T tvcopy;
5466
5467 copy_tv(&li->li_tv, &tvcopy);
5468 list_set_item(rem_list, i, &tvcopy);
Bram Moolenaar792f7862020-11-23 08:31:18 +01005469 li = li->li_next;
5470 }
5471 --count;
5472 }
5473
5474 // Produce the values in reverse order, first item last.
5475 li = l->lv_first;
5476 for (i = 0; i < count; ++i)
5477 {
5478 tv = STACK_TV_BOT(-i - 1);
5479 copy_tv(&li->li_tv, tv);
5480 li = li->li_next;
5481 }
5482
5483 list_unref(l);
5484 }
5485 break;
5486
Bram Moolenaarb2049902021-01-24 12:53:53 +01005487 case ISN_PROF_START:
5488 case ISN_PROF_END:
5489 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01005490#ifdef FEAT_PROFILE
Bram Moolenaarca16c602022-09-06 18:57:08 +01005491 funccall_T cookie;
5492 ufunc_T *cur_ufunc =
Bram Moolenaarb2049902021-01-24 12:53:53 +01005493 (((dfunc_T *)def_functions.ga_data)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02005494 + ectx->ec_dfunc_idx)->df_ufunc;
Bram Moolenaarb2049902021-01-24 12:53:53 +01005495
Bram Moolenaarca16c602022-09-06 18:57:08 +01005496 cookie.fc_func = cur_ufunc;
Bram Moolenaarb2049902021-01-24 12:53:53 +01005497 if (iptr->isn_type == ISN_PROF_START)
5498 {
5499 func_line_start(&cookie, iptr->isn_lnum);
5500 // if we get here the instruction is executed
5501 func_line_exec(&cookie);
5502 }
5503 else
5504 func_line_end(&cookie);
Bram Moolenaarf002a412021-01-24 13:34:18 +01005505#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01005506 }
5507 break;
5508
Bram Moolenaare99d4222021-06-13 14:01:26 +02005509 case ISN_DEBUG:
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02005510 handle_debug(iptr, ectx);
Bram Moolenaare99d4222021-06-13 14:01:26 +02005511 break;
5512
Bram Moolenaar389df252020-07-09 21:20:47 +02005513 case ISN_SHUFFLE:
5514 {
Bram Moolenaar792f7862020-11-23 08:31:18 +01005515 typval_T tmp_tv;
5516 int item = iptr->isn_arg.shuffle.shfl_item;
5517 int up = iptr->isn_arg.shuffle.shfl_up;
Bram Moolenaar389df252020-07-09 21:20:47 +02005518
5519 tmp_tv = *STACK_TV_BOT(-item);
5520 for ( ; up > 0 && item > 1; --up)
5521 {
5522 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
5523 --item;
5524 }
5525 *STACK_TV_BOT(-item) = tmp_tv;
5526 }
5527 break;
5528
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005529 case ISN_DROP:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005530 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005531 clear_tv(STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005532 ectx->ec_where.wt_index = 0;
5533 ectx->ec_where.wt_variable = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005534 break;
5535 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02005536 continue;
5537
Bram Moolenaard032f342020-07-18 18:13:02 +02005538func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02005539 // Restore previous function. If the frame pointer is where we started
5540 // then there is none and we are done.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005541 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02005542 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02005543
Bram Moolenaar4c137212021-04-19 16:48:48 +02005544 if (func_return(ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02005545 // only fails when out of memory
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005546 goto theend;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02005547 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02005548
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02005549on_error:
Bram Moolenaaraf0df472020-12-02 20:51:22 +01005550 // Jump here for an error that does not require aborting execution.
Bram Moolenaar56602ba2020-12-05 21:22:08 +01005551 // If "emsg_silent" is set then ignore the error, unless it was set
5552 // when calling the function.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005553 if (did_emsg_cumul + did_emsg == ectx->ec_did_emsg_before
Bram Moolenaar56602ba2020-12-05 21:22:08 +01005554 && emsg_silent && did_emsg_def == 0)
Bram Moolenaarf9041332021-01-21 19:41:16 +01005555 {
5556 // If a sequence of instructions causes an error while ":silent!"
5557 // was used, restore the stack length and jump ahead to restoring
5558 // the cmdmod.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005559 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaarf9041332021-01-21 19:41:16 +01005560 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005561 while (ectx->ec_stack.ga_len
5562 > ectx->ec_funclocal.floc_restore_cmdmod_stacklen)
Bram Moolenaarf9041332021-01-21 19:41:16 +01005563 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005564 --ectx->ec_stack.ga_len;
Bram Moolenaarf9041332021-01-21 19:41:16 +01005565 clear_tv(STACK_TV_BOT(0));
5566 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005567 while (ectx->ec_instr[ectx->ec_iidx].isn_type != ISN_CMDMOD_REV)
5568 ++ectx->ec_iidx;
Bram Moolenaarf9041332021-01-21 19:41:16 +01005569 }
Bram Moolenaarcd030c42020-10-30 21:49:40 +01005570 continue;
Bram Moolenaarf9041332021-01-21 19:41:16 +01005571 }
Bram Moolenaaraf0df472020-12-02 20:51:22 +01005572on_fatal_error:
5573 // Jump here for an error that messes up the stack.
Bram Moolenaar171fb922020-10-28 16:54:47 +01005574 // If we are not inside a try-catch started here, abort execution.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005575 if (trylevel <= ectx->ec_trylevel_at_start)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005576 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005577 }
5578
5579done:
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005580 ret = OK;
5581theend:
Bram Moolenaar58779852022-09-06 18:31:14 +01005582 may_invoke_defer_funcs(ectx);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01005583
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005584 dict_stack_clear(dict_stack_len_at_start);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005585 ectx->ec_trylevel_at_start = save_trylevel_at_start;
5586 return ret;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005587}
5588
5589/*
Bram Moolenaar806a2732022-09-04 15:40:36 +01005590 * Execute the instructions from a VAR_INSTR typval and put the result in
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005591 * "rettv".
5592 * Return OK or FAIL.
5593 */
5594 int
5595exe_typval_instr(typval_T *tv, typval_T *rettv)
5596{
5597 ectx_T *ectx = tv->vval.v_instr->instr_ectx;
5598 isn_T *save_instr = ectx->ec_instr;
5599 int save_iidx = ectx->ec_iidx;
5600 int res;
5601
LemonBoyf3b48952022-05-05 13:53:03 +01005602 // Initialize rettv so that it is safe for caller to invoke clear_tv(rettv)
5603 // even when the compilation fails.
5604 rettv->v_type = VAR_UNKNOWN;
5605
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005606 ectx->ec_instr = tv->vval.v_instr->instr_instr;
5607 res = exec_instructions(ectx);
5608 if (res == OK)
5609 {
5610 *rettv = *STACK_TV_BOT(-1);
5611 --ectx->ec_stack.ga_len;
5612 }
5613
5614 ectx->ec_instr = save_instr;
5615 ectx->ec_iidx = save_iidx;
5616
5617 return res;
5618}
5619
5620/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02005621 * Execute the instructions from an ISN_SUBSTITUTE command, which are in
5622 * "substitute_instr".
5623 */
5624 char_u *
5625exe_substitute_instr(void)
5626{
5627 ectx_T *ectx = substitute_instr->subs_ectx;
5628 isn_T *save_instr = ectx->ec_instr;
5629 int save_iidx = ectx->ec_iidx;
5630 char_u *res;
5631
5632 ectx->ec_instr = substitute_instr->subs_instr;
5633 if (exec_instructions(ectx) == OK)
Bram Moolenaar90193e62021-04-04 20:49:50 +02005634 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005635 typval_T *tv = STACK_TV_BOT(-1);
5636
Bram Moolenaar27523602021-06-05 21:36:19 +02005637 res = typval2string(tv, TRUE);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005638 --ectx->ec_stack.ga_len;
5639 clear_tv(tv);
Bram Moolenaar90193e62021-04-04 20:49:50 +02005640 }
5641 else
5642 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005643 substitute_instr->subs_status = FAIL;
5644 res = vim_strsave((char_u *)"");
Bram Moolenaar90193e62021-04-04 20:49:50 +02005645 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005646
Bram Moolenaar4c137212021-04-19 16:48:48 +02005647 ectx->ec_instr = save_instr;
5648 ectx->ec_iidx = save_iidx;
5649
5650 return res;
5651}
5652
5653/*
5654 * Call a "def" function from old Vim script.
5655 * Return OK or FAIL.
5656 */
5657 int
5658call_def_function(
5659 ufunc_T *ufunc,
5660 int argc_arg, // nr of arguments
5661 typval_T *argv, // arguments
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005662 int flags, // DEF_ flags
Bram Moolenaar4c137212021-04-19 16:48:48 +02005663 partial_T *partial, // optional partial for context
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005664 object_T *object, // object, e.g. for this.Func()
Bram Moolenaar58779852022-09-06 18:31:14 +01005665 funccall_T *funccal,
Bram Moolenaar4c137212021-04-19 16:48:48 +02005666 typval_T *rettv) // return value
5667{
5668 ectx_T ectx; // execution context
5669 int argc = argc_arg;
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005670 int partial_argc = partial == NULL
5671 || (flags & DEF_USE_PT_ARGV) == 0
5672 ? 0 : partial->pt_argc;
5673 int total_argc = argc + partial_argc;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005674 typval_T *tv;
5675 int idx;
5676 int ret = FAIL;
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005677 int defcount = ufunc->uf_args.ga_len - total_argc;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005678 sctx_T save_current_sctx = current_sctx;
5679 int did_emsg_before = did_emsg_cumul + did_emsg;
5680 int save_suppress_errthrow = suppress_errthrow;
5681 msglist_T **saved_msg_list = NULL;
5682 msglist_T *private_msg_list = NULL;
5683 int save_emsg_silent_def = emsg_silent_def;
5684 int save_did_emsg_def = did_emsg_def;
5685 int orig_funcdepth;
Bram Moolenaare99d4222021-06-13 14:01:26 +02005686 int orig_nesting_level = ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005687
5688// Get pointer to item in the stack.
5689#undef STACK_TV
5690#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
5691
5692// Get pointer to item at the bottom of the stack, -1 is the bottom.
5693#undef STACK_TV_BOT
5694#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
5695
5696// Get pointer to a local variable on the stack. Negative for arguments.
5697#undef STACK_TV_VAR
5698#define STACK_TV_VAR(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_frame_idx + STACK_FRAME_SIZE + idx)
5699
5700 if (ufunc->uf_def_status == UF_NOT_COMPILED
5701 || ufunc->uf_def_status == UF_COMPILE_ERROR
Bram Moolenaar139575d2022-03-15 19:29:30 +00005702 || (func_needs_compiling(ufunc, get_compile_type(ufunc))
5703 && compile_def_function(ufunc, FALSE,
5704 get_compile_type(ufunc), NULL) == FAIL))
Bram Moolenaar4c137212021-04-19 16:48:48 +02005705 {
5706 if (did_emsg_cumul + did_emsg == did_emsg_before)
5707 semsg(_(e_function_is_not_compiled_str),
5708 printable_func_name(ufunc));
5709 return FAIL;
5710 }
5711
5712 {
5713 // Check the function was really compiled.
5714 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5715 + ufunc->uf_dfunc_idx;
Bram Moolenaarcc341812022-09-19 15:54:34 +01005716 if (dfunc->df_ufunc == NULL)
5717 {
5718 semsg(_(e_function_was_deleted_str), printable_func_name(ufunc));
5719 return FAIL;
5720 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005721 if (INSTRUCTIONS(dfunc) == NULL)
5722 {
5723 iemsg("using call_def_function() on not compiled function");
5724 return FAIL;
5725 }
5726 }
5727
5728 // If depth of calling is getting too high, don't execute the function.
5729 orig_funcdepth = funcdepth_get();
5730 if (funcdepth_increment() == FAIL)
5731 return FAIL;
5732
5733 CLEAR_FIELD(ectx);
5734 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
5735 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
Bram Moolenaar35578162021-08-02 19:10:38 +02005736 if (GA_GROW_FAILS(&ectx.ec_stack, 20))
Bram Moolenaar4c137212021-04-19 16:48:48 +02005737 {
5738 funcdepth_decrement();
5739 return FAIL;
5740 }
5741 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
5742 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
5743 ectx.ec_did_emsg_before = did_emsg_before;
Bram Moolenaare99d4222021-06-13 14:01:26 +02005744 ++ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005745
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005746 idx = total_argc - ufunc->uf_args.ga_len;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005747 if (idx > 0 && ufunc->uf_va_name == NULL)
5748 {
Matvey Tarasovd14bb1a2022-06-29 13:18:27 +01005749 semsg(NGETTEXT(e_one_argument_too_many, e_nr_arguments_too_many,
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005750 idx), idx);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005751 goto failed_early;
5752 }
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005753 idx = total_argc - ufunc->uf_args.ga_len + ufunc->uf_def_args.ga_len;
Bram Moolenaarc6d71532021-06-05 18:49:38 +02005754 if (idx < 0)
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02005755 {
Matvey Tarasovd14bb1a2022-06-29 13:18:27 +01005756 semsg(NGETTEXT(e_one_argument_too_few, e_nr_arguments_too_few,
Bram Moolenaar98aff652022-09-06 21:02:35 +01005757 -idx), -idx);
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02005758 goto failed_early;
5759 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005760
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005761 // Put values from the partial and arguments on the stack, but no more than
5762 // what the function expects. A lambda can be called with more arguments
5763 // than it uses.
5764 for (idx = 0; idx < total_argc
Bram Moolenaar4c137212021-04-19 16:48:48 +02005765 && (ufunc->uf_va_name != NULL || idx < ufunc->uf_args.ga_len);
5766 ++idx)
5767 {
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005768 int argv_idx = idx - partial_argc;
5769
5770 tv = idx < partial_argc ? partial->pt_argv + idx : argv + argv_idx;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005771 if (idx >= ufunc->uf_args.ga_len - ufunc->uf_def_args.ga_len
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005772 && tv->v_type == VAR_SPECIAL
5773 && tv->vval.v_number == VVAL_NONE)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005774 {
5775 // Use the default value.
5776 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
5777 }
5778 else
5779 {
5780 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
5781 && check_typval_arg_type(
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005782 ufunc->uf_arg_types[idx], tv,
5783 NULL, argv_idx + 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005784 goto failed_early;
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005785 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005786 }
5787 ++ectx.ec_stack.ga_len;
5788 }
5789
5790 // Turn varargs into a list. Empty list if no args.
5791 if (ufunc->uf_va_name != NULL)
5792 {
5793 int vararg_count = argc - ufunc->uf_args.ga_len;
5794
5795 if (vararg_count < 0)
5796 vararg_count = 0;
5797 else
5798 argc -= vararg_count;
5799 if (exe_newlist(vararg_count, &ectx) == FAIL)
5800 goto failed_early;
5801
5802 // Check the type of the list items.
5803 tv = STACK_TV_BOT(-1);
5804 if (ufunc->uf_va_type != NULL
5805 && ufunc->uf_va_type != &t_list_any
5806 && ufunc->uf_va_type->tt_member != &t_any
5807 && tv->vval.v_list != NULL)
5808 {
5809 type_T *expected = ufunc->uf_va_type->tt_member;
5810 listitem_T *li = tv->vval.v_list->lv_first;
5811
5812 for (idx = 0; idx < vararg_count; ++idx)
5813 {
5814 if (check_typval_arg_type(expected, &li->li_tv,
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02005815 NULL, argc + idx + 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005816 goto failed_early;
5817 li = li->li_next;
5818 }
5819 }
5820
5821 if (defcount > 0)
5822 // Move varargs list to below missing default arguments.
5823 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
5824 --ectx.ec_stack.ga_len;
5825 }
5826
5827 // Make space for omitted arguments, will store default value below.
5828 // Any varargs list goes after them.
5829 if (defcount > 0)
5830 for (idx = 0; idx < defcount; ++idx)
5831 {
5832 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
5833 ++ectx.ec_stack.ga_len;
5834 }
5835 if (ufunc->uf_va_name != NULL)
5836 ++ectx.ec_stack.ga_len;
5837
5838 // Frame pointer points to just after arguments.
5839 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
5840 ectx.ec_initial_frame_idx = ectx.ec_frame_idx;
5841
5842 {
5843 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5844 + ufunc->uf_dfunc_idx;
5845 ufunc_T *base_ufunc = dfunc->df_ufunc;
5846
5847 // "uf_partial" is on the ufunc that "df_ufunc" points to, as is done
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01005848 // by copy_lambda_to_global_func().
Bram Moolenaar4c137212021-04-19 16:48:48 +02005849 if (partial != NULL || base_ufunc->uf_partial != NULL)
5850 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005851 ectx.ec_outer_ref = ALLOC_CLEAR_ONE(outer_ref_T);
5852 if (ectx.ec_outer_ref == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005853 goto failed_early;
5854 if (partial != NULL)
5855 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00005856 outer_T *outer = get_pt_outer(partial);
5857
Bram Moolenaar6d313be2022-09-22 16:36:25 +01005858 if (outer->out_stack == NULL && outer->out_loop_size == 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005859 {
Bram Moolenaar6d313be2022-09-22 16:36:25 +01005860 // no stack was set
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00005861 if (current_ectx != NULL)
5862 {
5863 if (current_ectx->ec_outer_ref != NULL
5864 && current_ectx->ec_outer_ref->or_outer != NULL)
5865 ectx.ec_outer_ref->or_outer =
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005866 current_ectx->ec_outer_ref->or_outer;
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00005867 }
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005868 // else: should there be an error here?
Bram Moolenaar4c137212021-04-19 16:48:48 +02005869 }
5870 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005871 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00005872 ectx.ec_outer_ref->or_outer = outer;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005873 ++partial->pt_refcount;
5874 ectx.ec_outer_ref->or_partial = partial;
5875 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005876 }
5877 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005878 {
5879 ectx.ec_outer_ref->or_outer = &base_ufunc->uf_partial->pt_outer;
5880 ++base_ufunc->uf_partial->pt_refcount;
5881 ectx.ec_outer_ref->or_partial = base_ufunc->uf_partial;
5882 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005883 }
5884 }
5885
5886 // dummy frame entries
5887 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
5888 {
5889 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
5890 ++ectx.ec_stack.ga_len;
5891 }
5892
5893 {
5894 // Reserve space for local variables and any closure reference count.
5895 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5896 + ufunc->uf_dfunc_idx;
5897
Bram Moolenaar5cd64792021-12-25 18:23:24 +00005898 // Initialize variables to zero. That avoids having to generate
5899 // initializing instructions for "var nr: number", "var x: any", etc.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005900 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaar5cd64792021-12-25 18:23:24 +00005901 {
5902 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
5903 STACK_TV_VAR(idx)->vval.v_number = 0;
5904 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005905 ectx.ec_stack.ga_len += dfunc->df_varcount;
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005906
5907 if (object != NULL)
5908 {
5909 // the object is always the variable at index zero
5910 tv = STACK_TV_VAR(0);
5911 tv->v_type = VAR_OBJECT;
5912 tv->vval.v_object = object;
5913 }
5914
Bram Moolenaar4c137212021-04-19 16:48:48 +02005915 if (dfunc->df_has_closure)
5916 {
Bram Moolenaarcc341812022-09-19 15:54:34 +01005917 // Initialize the variable that counts how many closures were
5918 // created. This is used in handle_closure_in_use().
Bram Moolenaar4c137212021-04-19 16:48:48 +02005919 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
5920 STACK_TV_VAR(idx)->vval.v_number = 0;
5921 ++ectx.ec_stack.ga_len;
5922 }
5923
5924 ectx.ec_instr = INSTRUCTIONS(dfunc);
5925 }
5926
Bram Moolenaar58779852022-09-06 18:31:14 +01005927 // Store the execution context in funccal, used by invoke_all_defer().
5928 if (funccal != NULL)
5929 funccal->fc_ectx = &ectx;
5930
Bram Moolenaar4c137212021-04-19 16:48:48 +02005931 // Following errors are in the function, not the caller.
5932 // Commands behave like vim9script.
5933 estack_push_ufunc(ufunc, 1);
5934 current_sctx = ufunc->uf_script_ctx;
5935 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
5936
5937 // Use a specific location for storing error messages to be converted to an
5938 // exception.
5939 saved_msg_list = msg_list;
5940 msg_list = &private_msg_list;
5941
5942 // Do turn errors into exceptions.
5943 suppress_errthrow = FALSE;
5944
5945 // Do not delete the function while executing it.
5946 ++ufunc->uf_calls;
5947
5948 // When ":silent!" was used before calling then we still abort the
5949 // function. If ":silent!" is used in the function then we don't.
5950 emsg_silent_def = emsg_silent;
5951 did_emsg_def = 0;
5952
5953 ectx.ec_where.wt_index = 0;
5954 ectx.ec_where.wt_variable = FALSE;
5955
Bram Moolenaar5800c792022-09-22 17:34:01 +01005956 /*
5957 * Execute the instructions until done.
5958 */
Bram Moolenaar4c137212021-04-19 16:48:48 +02005959 ret = exec_instructions(&ectx);
5960 if (ret == OK)
5961 {
5962 // function finished, get result from the stack.
5963 if (ufunc->uf_ret_type == &t_void)
5964 {
5965 rettv->v_type = VAR_VOID;
5966 }
5967 else
5968 {
5969 tv = STACK_TV_BOT(-1);
5970 *rettv = *tv;
5971 tv->v_type = VAR_UNKNOWN;
5972 }
5973 }
5974
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01005975 // When failed need to unwind the call stack.
Bram Moolenaar58779852022-09-06 18:31:14 +01005976 unwind_def_callstack(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02005977
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005978 // Deal with any remaining closures, they may be in use somewhere.
5979 if (ectx.ec_funcrefs.ga_len > 0)
Bram Moolenaarf112f302020-12-20 17:47:52 +01005980 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005981 handle_closure_in_use(&ectx, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00005982 ga_clear(&ectx.ec_funcrefs);
Bram Moolenaarf112f302020-12-20 17:47:52 +01005983 }
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005984
Bram Moolenaaree8580e2020-08-28 17:19:07 +02005985 estack_pop();
5986 current_sctx = save_current_sctx;
5987
Bram Moolenaar2336c372021-12-06 15:06:54 +00005988 if (--ufunc->uf_calls <= 0 && ufunc->uf_refcount <= 0)
5989 // Function was unreferenced while being used, free it now.
5990 func_clear_free(ufunc, FALSE);
Bram Moolenaarc970e422021-03-17 15:03:04 +01005991
Bram Moolenaar352134b2020-10-17 22:04:08 +02005992 if (*msg_list != NULL && saved_msg_list != NULL)
5993 {
5994 msglist_T **plist = saved_msg_list;
5995
5996 // Append entries from the current msg_list (uncaught exceptions) to
5997 // the saved msg_list.
5998 while (*plist != NULL)
5999 plist = &(*plist)->next;
6000
6001 *plist = *msg_list;
6002 }
6003 msg_list = saved_msg_list;
6004
Bram Moolenaar4c137212021-04-19 16:48:48 +02006005 if (ectx.ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar02194d22020-10-24 23:08:38 +02006006 {
6007 cmdmod.cmod_filter_regmatch.regprog = NULL;
6008 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006009 cmdmod = ectx.ec_funclocal.floc_save_cmdmod;
Bram Moolenaar02194d22020-10-24 23:08:38 +02006010 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +01006011 emsg_silent_def = save_emsg_silent_def;
6012 did_emsg_def += save_did_emsg_def;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02006013
Bram Moolenaaree8580e2020-08-28 17:19:07 +02006014failed_early:
Bram Moolenaar0d1f55c2022-04-05 17:30:29 +01006015 // Free all arguments and local variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006016 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
Bram Moolenaar1936c762022-09-28 15:19:10 +01006017 {
6018 tv = STACK_TV(idx);
6019 if (tv->v_type != VAR_NUMBER && tv->v_type != VAR_UNKNOWN)
6020 clear_tv(tv);
6021 }
Bram Moolenaare99d4222021-06-13 14:01:26 +02006022 ex_nesting_level = orig_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02006023
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006024 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01006025 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006026 if (ectx.ec_outer_ref != NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +01006027 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006028 if (ectx.ec_outer_ref->or_outer_allocated)
6029 vim_free(ectx.ec_outer_ref->or_outer);
6030 partial_unref(ectx.ec_outer_ref->or_partial);
6031 vim_free(ectx.ec_outer_ref);
Bram Moolenaar0186e582021-01-10 18:33:11 +01006032 }
6033
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02006034 // Not sure if this is necessary.
6035 suppress_errthrow = save_suppress_errthrow;
6036
Bram Moolenaarb5841b92021-07-15 18:09:53 +02006037 if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before
6038 && !need_rethrow)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006039 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02006040 printable_func_name(ufunc));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01006041 funcdepth_restore(orig_funcdepth);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006042 return ret;
6043}
6044
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006045/*
Bram Moolenaar58779852022-09-06 18:31:14 +01006046 * Called when a def function has finished (possibly failed).
6047 * Invoke all the function returns to clean up and invoke deferred functions,
6048 * except the toplevel one.
6049 */
6050 void
6051unwind_def_callstack(ectx_T *ectx)
6052{
6053 while (ectx->ec_frame_idx != ectx->ec_initial_frame_idx)
6054 func_return(ectx);
6055}
6056
6057/*
dundargocc57b5bc2022-11-02 13:30:51 +00006058 * Invoke any deferred functions for the top function in "ectx".
Bram Moolenaar58779852022-09-06 18:31:14 +01006059 */
6060 void
6061may_invoke_defer_funcs(ectx_T *ectx)
6062{
6063 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
6064
6065 if (dfunc->df_defer_var_idx > 0)
6066 invoke_defer_funcs(ectx);
6067}
6068
6069/*
Bram Moolenaarcc341812022-09-19 15:54:34 +01006070 * Return loopvarinfo in a printable form in allocated memory.
6071 */
6072 static char_u *
6073printable_loopvarinfo(loopvarinfo_T *lvi)
6074{
6075 garray_T ga;
6076 int depth;
6077
6078 ga_init2(&ga, 1, 100);
6079 for (depth = 0; depth < lvi->lvi_depth; ++depth)
6080 {
6081 if (ga_grow(&ga, 50) == FAIL)
6082 break;
6083 if (lvi->lvi_loop[depth].var_idx == 0)
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +01006084 STRCPY((char *)ga.ga_data + ga.ga_len, " -");
Bram Moolenaarcc341812022-09-19 15:54:34 +01006085 else
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +01006086 vim_snprintf((char *)ga.ga_data + ga.ga_len, 50, " $%d-$%d",
Bram Moolenaarcc341812022-09-19 15:54:34 +01006087 lvi->lvi_loop[depth].var_idx,
6088 lvi->lvi_loop[depth].var_idx
6089 + lvi->lvi_loop[depth].var_count - 1);
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +01006090 ga.ga_len = (int)STRLEN(ga.ga_data);
Bram Moolenaarcc341812022-09-19 15:54:34 +01006091 }
6092 return ga.ga_data;
6093}
6094
6095/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02006096 * List instructions "instr" up to "instr_count" or until ISN_FINISH.
6097 * "ufunc" has the source lines, NULL for the instructions of ISN_SUBSTITUTE.
6098 * "pfx" is prefixed to every line.
6099 */
6100 static void
6101list_instructions(char *pfx, isn_T *instr, int instr_count, ufunc_T *ufunc)
6102{
6103 int line_idx = 0;
6104 int prev_current = 0;
6105 int current;
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02006106 int def_arg_idx = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006107
6108 for (current = 0; current < instr_count; ++current)
6109 {
6110 isn_T *iptr = &instr[current];
6111 char *line;
6112
6113 if (ufunc != NULL)
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02006114 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02006115 while (line_idx < iptr->isn_lnum
6116 && line_idx < ufunc->uf_lines.ga_len)
6117 {
6118 if (current > prev_current)
6119 {
6120 msg_puts("\n\n");
6121 prev_current = current;
6122 }
6123 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
6124 if (line != NULL)
6125 msg(line);
6126 }
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02006127 if (iptr->isn_type == ISN_JUMP_IF_ARG_SET)
6128 {
6129 int first_def_arg = ufunc->uf_args.ga_len
6130 - ufunc->uf_def_args.ga_len;
6131
6132 if (def_arg_idx > 0)
6133 msg_puts("\n\n");
6134 msg_start();
6135 msg_puts(" ");
6136 msg_puts(((char **)(ufunc->uf_args.ga_data))[
6137 first_def_arg + def_arg_idx]);
6138 msg_puts(" = ");
6139 msg_puts(((char **)(ufunc->uf_def_args.ga_data))[def_arg_idx++]);
6140 msg_clr_eos();
6141 msg_end();
6142 }
6143 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006144
6145 switch (iptr->isn_type)
6146 {
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006147 case ISN_CONSTRUCT:
6148 smsg("%s%4d NEW %s size %d", pfx, current,
6149 iptr->isn_arg.construct.construct_class->class_name,
6150 (int)iptr->isn_arg.construct.construct_size);
6151 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006152 case ISN_EXEC:
6153 smsg("%s%4d EXEC %s", pfx, current, iptr->isn_arg.string);
6154 break;
Bram Moolenaar20677332021-06-06 17:02:53 +02006155 case ISN_EXEC_SPLIT:
6156 smsg("%s%4d EXEC_SPLIT %s", pfx, current, iptr->isn_arg.string);
6157 break;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00006158 case ISN_EXECRANGE:
6159 smsg("%s%4d EXECRANGE %s", pfx, current, iptr->isn_arg.string);
6160 break;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02006161 case ISN_LEGACY_EVAL:
6162 smsg("%s%4d EVAL legacy %s", pfx, current,
6163 iptr->isn_arg.string);
6164 break;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02006165 case ISN_REDIRSTART:
6166 smsg("%s%4d REDIR", pfx, current);
6167 break;
6168 case ISN_REDIREND:
6169 smsg("%s%4d REDIR END%s", pfx, current,
6170 iptr->isn_arg.number ? " append" : "");
6171 break;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006172 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02006173#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006174 smsg("%s%4d CEXPR pre %s", pfx, current,
6175 cexpr_get_auname(iptr->isn_arg.number));
Bram Moolenaarb7c97812021-05-05 22:51:39 +02006176#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006177 break;
6178 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02006179#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006180 {
6181 cexprref_T *cer = iptr->isn_arg.cexpr.cexpr_ref;
6182
6183 smsg("%s%4d CEXPR core %s%s \"%s\"", pfx, current,
6184 cexpr_get_auname(cer->cer_cmdidx),
6185 cer->cer_forceit ? "!" : "",
6186 cer->cer_cmdline);
6187 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02006188#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006189 break;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006190 case ISN_INSTR:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006191 smsg("%s%4d INSTR", pfx, current);
6192 list_instructions(" ", iptr->isn_arg.instr, INT_MAX, NULL);
6193 msg(" -------------");
6194 break;
6195 case ISN_SOURCE:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006196 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006197 scriptitem_T *si = SCRIPT_ITEM(iptr->isn_arg.number);
6198
6199 smsg("%s%4d SOURCE %s", pfx, current, si->sn_name);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006200 }
6201 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006202 case ISN_SUBSTITUTE:
6203 {
6204 subs_T *subs = &iptr->isn_arg.subs;
6205
6206 smsg("%s%4d SUBSTITUTE %s", pfx, current, subs->subs_cmd);
6207 list_instructions(" ", subs->subs_instr, INT_MAX, NULL);
6208 msg(" -------------");
6209 }
6210 break;
6211 case ISN_EXECCONCAT:
6212 smsg("%s%4d EXECCONCAT %lld", pfx, current,
6213 (varnumber_T)iptr->isn_arg.number);
6214 break;
6215 case ISN_ECHO:
6216 {
6217 echo_T *echo = &iptr->isn_arg.echo;
6218
6219 smsg("%s%4d %s %d", pfx, current,
6220 echo->echo_with_white ? "ECHO" : "ECHON",
6221 echo->echo_count);
6222 }
6223 break;
6224 case ISN_EXECUTE:
6225 smsg("%s%4d EXECUTE %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02006226 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006227 break;
6228 case ISN_ECHOMSG:
6229 smsg("%s%4d ECHOMSG %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02006230 (varnumber_T)(iptr->isn_arg.number));
6231 break;
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01006232 case ISN_ECHOWINDOW:
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01006233 if (iptr->isn_arg.echowin.ewin_time > 0)
6234 smsg("%s%4d ECHOWINDOW %d (%ld sec)", pfx, current,
6235 iptr->isn_arg.echowin.ewin_count,
6236 iptr->isn_arg.echowin.ewin_time);
6237 else
6238 smsg("%s%4d ECHOWINDOW %d", pfx, current,
6239 iptr->isn_arg.echowin.ewin_count);
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01006240 break;
Bram Moolenaar7de62622021-08-07 15:05:47 +02006241 case ISN_ECHOCONSOLE:
6242 smsg("%s%4d ECHOCONSOLE %lld", pfx, current,
6243 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006244 break;
6245 case ISN_ECHOERR:
6246 smsg("%s%4d ECHOERR %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02006247 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006248 break;
6249 case ISN_LOAD:
6250 {
6251 if (iptr->isn_arg.number < 0)
6252 smsg("%s%4d LOAD arg[%lld]", pfx, current,
6253 (varnumber_T)(iptr->isn_arg.number
6254 + STACK_FRAME_SIZE));
6255 else
6256 smsg("%s%4d LOAD $%lld", pfx, current,
6257 (varnumber_T)(iptr->isn_arg.number));
6258 }
6259 break;
6260 case ISN_LOADOUTER:
6261 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006262 isn_outer_T *outer = &iptr->isn_arg.outer;
6263
6264 if (outer->outer_idx < 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02006265 smsg("%s%4d LOADOUTER level %d arg[%d]", pfx, current,
Bram Moolenaarcc341812022-09-19 15:54:34 +01006266 outer->outer_depth,
6267 outer->outer_idx + STACK_FRAME_SIZE);
6268 else if (outer->outer_depth < 0)
6269 smsg("%s%4d LOADOUTER $%d in loop level %d",
6270 pfx, current,
6271 outer->outer_idx,
6272 -outer->outer_depth);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006273 else
6274 smsg("%s%4d LOADOUTER level %d $%d", pfx, current,
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006275 outer->outer_depth,
6276 outer->outer_idx);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006277 }
6278 break;
6279 case ISN_LOADV:
6280 smsg("%s%4d LOADV v:%s", pfx, current,
6281 get_vim_var_name(iptr->isn_arg.number));
6282 break;
6283 case ISN_LOADSCRIPT:
6284 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006285 scriptref_T *sref = iptr->isn_arg.script.scriptref;
6286 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
6287 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006288
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006289 sv = get_script_svar(sref, -1);
6290 if (sv == NULL)
6291 smsg("%s%4d LOADSCRIPT [deleted] from %s",
6292 pfx, current, si->sn_name);
6293 else
6294 smsg("%s%4d LOADSCRIPT %s-%d from %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02006295 sv->sv_name,
6296 sref->sref_idx,
6297 si->sn_name);
6298 }
6299 break;
6300 case ISN_LOADS:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006301 case ISN_LOADEXPORT:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006302 {
6303 scriptitem_T *si = SCRIPT_ITEM(
6304 iptr->isn_arg.loadstore.ls_sid);
6305
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006306 smsg("%s%4d %s s:%s from %s", pfx, current,
6307 iptr->isn_type == ISN_LOADS ? "LOADS"
6308 : "LOADEXPORT",
6309 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006310 }
6311 break;
6312 case ISN_LOADAUTO:
6313 smsg("%s%4d LOADAUTO %s", pfx, current, iptr->isn_arg.string);
6314 break;
6315 case ISN_LOADG:
6316 smsg("%s%4d LOADG g:%s", pfx, current, iptr->isn_arg.string);
6317 break;
6318 case ISN_LOADB:
6319 smsg("%s%4d LOADB b:%s", pfx, current, iptr->isn_arg.string);
6320 break;
6321 case ISN_LOADW:
6322 smsg("%s%4d LOADW w:%s", pfx, current, iptr->isn_arg.string);
6323 break;
6324 case ISN_LOADT:
6325 smsg("%s%4d LOADT t:%s", pfx, current, iptr->isn_arg.string);
6326 break;
6327 case ISN_LOADGDICT:
6328 smsg("%s%4d LOAD g:", pfx, current);
6329 break;
6330 case ISN_LOADBDICT:
6331 smsg("%s%4d LOAD b:", pfx, current);
6332 break;
6333 case ISN_LOADWDICT:
6334 smsg("%s%4d LOAD w:", pfx, current);
6335 break;
6336 case ISN_LOADTDICT:
6337 smsg("%s%4d LOAD t:", pfx, current);
6338 break;
6339 case ISN_LOADOPT:
6340 smsg("%s%4d LOADOPT %s", pfx, current, iptr->isn_arg.string);
6341 break;
6342 case ISN_LOADENV:
6343 smsg("%s%4d LOADENV %s", pfx, current, iptr->isn_arg.string);
6344 break;
6345 case ISN_LOADREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006346 smsg("%s%4d LOADREG @%c", pfx, current,
6347 (int)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006348 break;
6349
6350 case ISN_STORE:
6351 if (iptr->isn_arg.number < 0)
6352 smsg("%s%4d STORE arg[%lld]", pfx, current,
6353 iptr->isn_arg.number + STACK_FRAME_SIZE);
6354 else
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006355 smsg("%s%4d STORE $%lld", pfx, current,
6356 iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006357 break;
6358 case ISN_STOREOUTER:
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006359 {
6360 isn_outer_T *outer = &iptr->isn_arg.outer;
6361
6362 if (outer->outer_depth == OUTER_LOOP_DEPTH)
6363 smsg("%s%4d STOREOUTER level 1 $%d in loop",
6364 pfx, current, outer->outer_idx);
6365 else
6366 smsg("%s%4d STOREOUTER level %d $%d", pfx, current,
6367 outer->outer_depth, outer->outer_idx);
6368 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006369 break;
6370 case ISN_STOREV:
6371 smsg("%s%4d STOREV v:%s", pfx, current,
6372 get_vim_var_name(iptr->isn_arg.number));
6373 break;
6374 case ISN_STOREAUTO:
6375 smsg("%s%4d STOREAUTO %s", pfx, current, iptr->isn_arg.string);
6376 break;
6377 case ISN_STOREG:
6378 smsg("%s%4d STOREG %s", pfx, current, iptr->isn_arg.string);
6379 break;
6380 case ISN_STOREB:
6381 smsg("%s%4d STOREB %s", pfx, current, iptr->isn_arg.string);
6382 break;
6383 case ISN_STOREW:
6384 smsg("%s%4d STOREW %s", pfx, current, iptr->isn_arg.string);
6385 break;
6386 case ISN_STORET:
6387 smsg("%s%4d STORET %s", pfx, current, iptr->isn_arg.string);
6388 break;
6389 case ISN_STORES:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006390 case ISN_STOREEXPORT:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006391 {
6392 scriptitem_T *si = SCRIPT_ITEM(
6393 iptr->isn_arg.loadstore.ls_sid);
6394
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006395 smsg("%s%4d %s %s in %s", pfx, current,
6396 iptr->isn_type == ISN_STORES
6397 ? "STORES" : "STOREEXPORT",
Bram Moolenaar4c137212021-04-19 16:48:48 +02006398 iptr->isn_arg.loadstore.ls_name, si->sn_name);
6399 }
6400 break;
6401 case ISN_STORESCRIPT:
6402 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006403 scriptref_T *sref = iptr->isn_arg.script.scriptref;
6404 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
6405 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006406
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006407 sv = get_script_svar(sref, -1);
6408 if (sv == NULL)
6409 smsg("%s%4d STORESCRIPT [deleted] in %s",
6410 pfx, current, si->sn_name);
6411 else
6412 smsg("%s%4d STORESCRIPT %s-%d in %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02006413 sv->sv_name,
6414 sref->sref_idx,
6415 si->sn_name);
6416 }
6417 break;
6418 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00006419 case ISN_STOREFUNCOPT:
6420 smsg("%s%4d %s &%s", pfx, current,
6421 iptr->isn_type == ISN_STOREOPT ? "STOREOPT" : "STOREFUNCOPT",
Bram Moolenaar4c137212021-04-19 16:48:48 +02006422 iptr->isn_arg.storeopt.so_name);
6423 break;
6424 case ISN_STOREENV:
6425 smsg("%s%4d STOREENV $%s", pfx, current, iptr->isn_arg.string);
6426 break;
6427 case ISN_STOREREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006428 smsg("%s%4d STOREREG @%c", pfx, current,
6429 (int)iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006430 break;
6431 case ISN_STORENR:
6432 smsg("%s%4d STORE %lld in $%d", pfx, current,
6433 iptr->isn_arg.storenr.stnr_val,
6434 iptr->isn_arg.storenr.stnr_idx);
6435 break;
6436
6437 case ISN_STOREINDEX:
6438 smsg("%s%4d STOREINDEX %s", pfx, current,
6439 vartype_name(iptr->isn_arg.vartype));
6440 break;
6441
6442 case ISN_STORERANGE:
6443 smsg("%s%4d STORERANGE", pfx, current);
6444 break;
6445
Bram Moolenaard505d172022-12-18 21:42:55 +00006446 case ISN_LOAD_CLASSMEMBER:
6447 case ISN_STORE_CLASSMEMBER:
6448 {
6449 class_T *cl = iptr->isn_arg.classmember.cm_class;
6450 int idx = iptr->isn_arg.classmember.cm_idx;
6451 ocmember_T *ocm = &cl->class_class_members[idx];
6452 smsg("%s%4d %s CLASSMEMBER %s.%s", pfx, current,
6453 iptr->isn_type == ISN_LOAD_CLASSMEMBER
6454 ? "LOAD" : "STORE",
6455 cl->class_name, ocm->ocm_name);
6456 }
6457 break;
6458
Bram Moolenaar4c137212021-04-19 16:48:48 +02006459 // constants
6460 case ISN_PUSHNR:
6461 smsg("%s%4d PUSHNR %lld", pfx, current,
6462 (varnumber_T)(iptr->isn_arg.number));
6463 break;
6464 case ISN_PUSHBOOL:
6465 case ISN_PUSHSPEC:
6466 smsg("%s%4d PUSH %s", pfx, current,
6467 get_var_special_name(iptr->isn_arg.number));
6468 break;
6469 case ISN_PUSHF:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006470 smsg("%s%4d PUSHF %g", pfx, current, iptr->isn_arg.fnumber);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006471 break;
6472 case ISN_PUSHS:
6473 smsg("%s%4d PUSHS \"%s\"", pfx, current, iptr->isn_arg.string);
6474 break;
6475 case ISN_PUSHBLOB:
6476 {
6477 char_u *r;
6478 char_u numbuf[NUMBUFLEN];
6479 char_u *tofree;
6480
6481 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
6482 smsg("%s%4d PUSHBLOB %s", pfx, current, r);
6483 vim_free(tofree);
6484 }
6485 break;
6486 case ISN_PUSHFUNC:
6487 {
6488 char *name = (char *)iptr->isn_arg.string;
6489
6490 smsg("%s%4d PUSHFUNC \"%s\"", pfx, current,
6491 name == NULL ? "[none]" : name);
6492 }
6493 break;
6494 case ISN_PUSHCHANNEL:
6495#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar397a87a2022-03-20 21:14:15 +00006496 smsg("%s%4d PUSHCHANNEL 0", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006497#endif
6498 break;
6499 case ISN_PUSHJOB:
6500#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar397a87a2022-03-20 21:14:15 +00006501 smsg("%s%4d PUSHJOB \"no process\"", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006502#endif
6503 break;
6504 case ISN_PUSHEXC:
6505 smsg("%s%4d PUSH v:exception", pfx, current);
6506 break;
Bram Moolenaar06b77222022-01-25 15:51:56 +00006507 case ISN_AUTOLOAD:
6508 smsg("%s%4d AUTOLOAD %s", pfx, current, iptr->isn_arg.string);
6509 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006510 case ISN_UNLET:
6511 smsg("%s%4d UNLET%s %s", pfx, current,
6512 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
6513 iptr->isn_arg.unlet.ul_name);
6514 break;
6515 case ISN_UNLETENV:
6516 smsg("%s%4d UNLETENV%s $%s", pfx, current,
6517 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
6518 iptr->isn_arg.unlet.ul_name);
6519 break;
6520 case ISN_UNLETINDEX:
6521 smsg("%s%4d UNLETINDEX", pfx, current);
6522 break;
6523 case ISN_UNLETRANGE:
6524 smsg("%s%4d UNLETRANGE", pfx, current);
6525 break;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02006526 case ISN_LOCKUNLOCK:
6527 smsg("%s%4d LOCKUNLOCK %s", pfx, current, iptr->isn_arg.string);
6528 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006529 case ISN_LOCKCONST:
6530 smsg("%s%4d LOCKCONST", pfx, current);
6531 break;
6532 case ISN_NEWLIST:
6533 smsg("%s%4d NEWLIST size %lld", pfx, current,
Bram Moolenaar1936c762022-09-28 15:19:10 +01006534 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006535 break;
6536 case ISN_NEWDICT:
6537 smsg("%s%4d NEWDICT size %lld", pfx, current,
Bram Moolenaar1936c762022-09-28 15:19:10 +01006538 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006539 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00006540 case ISN_NEWPARTIAL:
6541 smsg("%s%4d NEWPARTIAL", pfx, current);
6542 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006543
6544 // function call
6545 case ISN_BCALL:
6546 {
6547 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
6548
6549 smsg("%s%4d BCALL %s(argc %d)", pfx, current,
6550 internal_func_name(cbfunc->cbf_idx),
6551 cbfunc->cbf_argcount);
6552 }
6553 break;
6554 case ISN_DCALL:
6555 {
6556 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
6557 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
6558 + cdfunc->cdf_idx;
6559
6560 smsg("%s%4d DCALL %s(argc %d)", pfx, current,
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006561 printable_func_name(df->df_ufunc),
6562 cdfunc->cdf_argcount);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006563 }
6564 break;
6565 case ISN_UCALL:
6566 {
6567 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
6568
6569 smsg("%s%4d UCALL %s(argc %d)", pfx, current,
6570 cufunc->cuf_name, cufunc->cuf_argcount);
6571 }
6572 break;
6573 case ISN_PCALL:
6574 {
6575 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
6576
6577 smsg("%s%4d PCALL%s (argc %d)", pfx, current,
6578 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
6579 }
6580 break;
6581 case ISN_PCALL_END:
6582 smsg("%s%4d PCALL end", pfx, current);
6583 break;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006584 case ISN_DEFER:
6585 smsg("%s%4d DEFER %d args", pfx, current,
6586 (int)iptr->isn_arg.defer.defer_argcount);
6587 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006588 case ISN_RETURN:
6589 smsg("%s%4d RETURN", pfx, current);
6590 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02006591 case ISN_RETURN_VOID:
6592 smsg("%s%4d RETURN void", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006593 break;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006594 case ISN_RETURN_OBJECT:
6595 smsg("%s%4d RETURN object", pfx, current);
6596 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006597 case ISN_FUNCREF:
6598 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006599 funcref_T *funcref = &iptr->isn_arg.funcref;
6600 funcref_extra_T *extra = funcref->fr_extra;
6601 char_u *name;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006602
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006603 if (extra == NULL || extra->fre_func_name == NULL)
Bram Moolenaar38453522021-11-28 22:00:12 +00006604 {
6605 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
6606 + funcref->fr_dfunc_idx;
6607 name = df->df_ufunc->uf_name;
6608 }
6609 else
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006610 name = extra->fre_func_name;
Bram Moolenaarcc341812022-09-19 15:54:34 +01006611 if (extra == NULL || extra->fre_loopvar_info.lvi_depth == 0)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006612 smsg("%s%4d FUNCREF %s", pfx, current, name);
6613 else
Bram Moolenaarcc341812022-09-19 15:54:34 +01006614 {
6615 char_u *info = printable_loopvarinfo(
6616 &extra->fre_loopvar_info);
6617
6618 smsg("%s%4d FUNCREF %s vars %s", pfx, current,
6619 name, info);
6620 vim_free(info);
6621 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006622 }
6623 break;
6624
6625 case ISN_NEWFUNC:
6626 {
Bram Moolenaarcc341812022-09-19 15:54:34 +01006627 newfuncarg_T *arg = iptr->isn_arg.newfunc.nf_arg;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006628
Bram Moolenaarcc341812022-09-19 15:54:34 +01006629 if (arg->nfa_loopvar_info.lvi_depth == 0)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006630 smsg("%s%4d NEWFUNC %s %s", pfx, current,
6631 arg->nfa_lambda, arg->nfa_global);
6632 else
Bram Moolenaarcc341812022-09-19 15:54:34 +01006633 {
6634 char_u *info = printable_loopvarinfo(
6635 &arg->nfa_loopvar_info);
6636
6637 smsg("%s%4d NEWFUNC %s %s vars %s", pfx, current,
6638 arg->nfa_lambda, arg->nfa_global, info);
6639 vim_free(info);
6640 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006641 }
6642 break;
6643
6644 case ISN_DEF:
6645 {
6646 char_u *name = iptr->isn_arg.string;
6647
6648 smsg("%s%4d DEF %s", pfx, current,
6649 name == NULL ? (char_u *)"" : name);
6650 }
6651 break;
6652
6653 case ISN_JUMP:
6654 {
6655 char *when = "?";
6656
6657 switch (iptr->isn_arg.jump.jump_when)
6658 {
6659 case JUMP_ALWAYS:
6660 when = "JUMP";
6661 break;
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02006662 case JUMP_NEVER:
6663 iemsg("JUMP_NEVER should not be used");
6664 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006665 case JUMP_AND_KEEP_IF_TRUE:
6666 when = "JUMP_AND_KEEP_IF_TRUE";
6667 break;
6668 case JUMP_IF_FALSE:
6669 when = "JUMP_IF_FALSE";
6670 break;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01006671 case JUMP_WHILE_FALSE:
6672 when = "JUMP_WHILE_FALSE"; // unused
6673 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006674 case JUMP_IF_COND_FALSE:
6675 when = "JUMP_IF_COND_FALSE";
6676 break;
6677 case JUMP_IF_COND_TRUE:
6678 when = "JUMP_IF_COND_TRUE";
6679 break;
6680 }
6681 smsg("%s%4d %s -> %d", pfx, current, when,
6682 iptr->isn_arg.jump.jump_where);
6683 }
6684 break;
6685
6686 case ISN_JUMP_IF_ARG_SET:
6687 smsg("%s%4d JUMP_IF_ARG_SET arg[%d] -> %d", pfx, current,
6688 iptr->isn_arg.jumparg.jump_arg_off + STACK_FRAME_SIZE,
6689 iptr->isn_arg.jump.jump_where);
6690 break;
6691
Bram Moolenaar65b0d162022-12-13 18:43:22 +00006692 case ISN_JUMP_IF_ARG_NOT_SET:
6693 smsg("%s%4d JUMP_IF_ARG_NOT_SET arg[%d] -> %d", pfx, current,
6694 iptr->isn_arg.jumparg.jump_arg_off + STACK_FRAME_SIZE,
6695 iptr->isn_arg.jump.jump_where);
6696 break;
6697
Bram Moolenaar4c137212021-04-19 16:48:48 +02006698 case ISN_FOR:
6699 {
6700 forloop_T *forloop = &iptr->isn_arg.forloop;
6701
6702 smsg("%s%4d FOR $%d -> %d", pfx, current,
Bram Moolenaarcc341812022-09-19 15:54:34 +01006703 forloop->for_loop_idx, forloop->for_end);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006704 }
6705 break;
6706
Bram Moolenaarb46c0832022-09-15 17:19:37 +01006707 case ISN_ENDLOOP:
6708 {
6709 endloop_T *endloop = &iptr->isn_arg.endloop;
6710
Bram Moolenaarcc341812022-09-19 15:54:34 +01006711 smsg("%s%4d ENDLOOP ref $%d save $%d-$%d depth %d",
6712 pfx, current,
Bram Moolenaarb46c0832022-09-15 17:19:37 +01006713 endloop->end_funcref_idx,
6714 endloop->end_var_idx,
Bram Moolenaarcc341812022-09-19 15:54:34 +01006715 endloop->end_var_idx + endloop->end_var_count - 1,
6716 endloop->end_depth);
Bram Moolenaarb46c0832022-09-15 17:19:37 +01006717 }
6718 break;
6719
6720 case ISN_WHILE:
6721 {
6722 whileloop_T *whileloop = &iptr->isn_arg.whileloop;
6723
6724 smsg("%s%4d WHILE $%d -> %d", pfx, current,
6725 whileloop->while_funcref_idx,
6726 whileloop->while_end);
6727 }
6728 break;
6729
Bram Moolenaar4c137212021-04-19 16:48:48 +02006730 case ISN_TRY:
6731 {
Bram Moolenaar0d807102021-12-21 09:42:09 +00006732 try_T *try = &iptr->isn_arg.tryref;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006733
6734 if (try->try_ref->try_finally == 0)
6735 smsg("%s%4d TRY catch -> %d, endtry -> %d",
6736 pfx, current,
6737 try->try_ref->try_catch,
6738 try->try_ref->try_endtry);
6739 else
6740 smsg("%s%4d TRY catch -> %d, finally -> %d, endtry -> %d",
6741 pfx, current,
6742 try->try_ref->try_catch,
6743 try->try_ref->try_finally,
6744 try->try_ref->try_endtry);
6745 }
6746 break;
6747 case ISN_CATCH:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006748 smsg("%s%4d CATCH", pfx, current);
6749 break;
6750 case ISN_TRYCONT:
6751 {
6752 trycont_T *trycont = &iptr->isn_arg.trycont;
6753
6754 smsg("%s%4d TRY-CONTINUE %d level%s -> %d", pfx, current,
6755 trycont->tct_levels,
6756 trycont->tct_levels == 1 ? "" : "s",
6757 trycont->tct_where);
6758 }
6759 break;
6760 case ISN_FINALLY:
6761 smsg("%s%4d FINALLY", pfx, current);
6762 break;
6763 case ISN_ENDTRY:
6764 smsg("%s%4d ENDTRY", pfx, current);
6765 break;
6766 case ISN_THROW:
6767 smsg("%s%4d THROW", pfx, current);
6768 break;
6769
6770 // expression operations on number
6771 case ISN_OPNR:
6772 case ISN_OPFLOAT:
6773 case ISN_OPANY:
6774 {
6775 char *what;
6776 char *ins;
6777
6778 switch (iptr->isn_arg.op.op_type)
6779 {
6780 case EXPR_MULT: what = "*"; break;
6781 case EXPR_DIV: what = "/"; break;
6782 case EXPR_REM: what = "%"; break;
6783 case EXPR_SUB: what = "-"; break;
6784 case EXPR_ADD: what = "+"; break;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01006785 case EXPR_LSHIFT: what = "<<"; break;
6786 case EXPR_RSHIFT: what = ">>"; break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006787 default: what = "???"; break;
6788 }
6789 switch (iptr->isn_type)
6790 {
6791 case ISN_OPNR: ins = "OPNR"; break;
6792 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
6793 case ISN_OPANY: ins = "OPANY"; break;
6794 default: ins = "???"; break;
6795 }
6796 smsg("%s%4d %s %s", pfx, current, ins, what);
6797 }
6798 break;
6799
6800 case ISN_COMPAREBOOL:
6801 case ISN_COMPARESPECIAL:
Bram Moolenaar7a222242022-03-01 19:23:24 +00006802 case ISN_COMPARENULL:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006803 case ISN_COMPARENR:
6804 case ISN_COMPAREFLOAT:
6805 case ISN_COMPARESTRING:
6806 case ISN_COMPAREBLOB:
6807 case ISN_COMPARELIST:
6808 case ISN_COMPAREDICT:
6809 case ISN_COMPAREFUNC:
6810 case ISN_COMPAREANY:
6811 {
6812 char *p;
6813 char buf[10];
6814 char *type;
6815
6816 switch (iptr->isn_arg.op.op_type)
6817 {
6818 case EXPR_EQUAL: p = "=="; break;
6819 case EXPR_NEQUAL: p = "!="; break;
6820 case EXPR_GREATER: p = ">"; break;
6821 case EXPR_GEQUAL: p = ">="; break;
6822 case EXPR_SMALLER: p = "<"; break;
6823 case EXPR_SEQUAL: p = "<="; break;
6824 case EXPR_MATCH: p = "=~"; break;
6825 case EXPR_IS: p = "is"; break;
6826 case EXPR_ISNOT: p = "isnot"; break;
6827 case EXPR_NOMATCH: p = "!~"; break;
6828 default: p = "???"; break;
6829 }
6830 STRCPY(buf, p);
6831 if (iptr->isn_arg.op.op_ic == TRUE)
6832 strcat(buf, "?");
6833 switch(iptr->isn_type)
6834 {
6835 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
6836 case ISN_COMPARESPECIAL:
6837 type = "COMPARESPECIAL"; break;
Bram Moolenaar7a222242022-03-01 19:23:24 +00006838 case ISN_COMPARENULL: type = "COMPARENULL"; break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006839 case ISN_COMPARENR: type = "COMPARENR"; break;
6840 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
6841 case ISN_COMPARESTRING:
6842 type = "COMPARESTRING"; break;
6843 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
6844 case ISN_COMPARELIST: type = "COMPARELIST"; break;
6845 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
6846 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
6847 case ISN_COMPAREANY: type = "COMPAREANY"; break;
6848 default: type = "???"; break;
6849 }
6850
6851 smsg("%s%4d %s %s", pfx, current, type, buf);
6852 }
6853 break;
6854
6855 case ISN_ADDLIST: smsg("%s%4d ADDLIST", pfx, current); break;
6856 case ISN_ADDBLOB: smsg("%s%4d ADDBLOB", pfx, current); break;
6857
6858 // expression operations
LemonBoy372bcce2022-04-25 12:43:20 +01006859 case ISN_CONCAT:
6860 smsg("%s%4d CONCAT size %lld", pfx, current,
6861 (varnumber_T)(iptr->isn_arg.number));
6862 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006863 case ISN_STRINDEX: smsg("%s%4d STRINDEX", pfx, current); break;
6864 case ISN_STRSLICE: smsg("%s%4d STRSLICE", pfx, current); break;
6865 case ISN_BLOBINDEX: smsg("%s%4d BLOBINDEX", pfx, current); break;
6866 case ISN_BLOBSLICE: smsg("%s%4d BLOBSLICE", pfx, current); break;
6867 case ISN_LISTAPPEND: smsg("%s%4d LISTAPPEND", pfx, current); break;
6868 case ISN_BLOBAPPEND: smsg("%s%4d BLOBAPPEND", pfx, current); break;
6869 case ISN_LISTINDEX: smsg("%s%4d LISTINDEX", pfx, current); break;
6870 case ISN_LISTSLICE: smsg("%s%4d LISTSLICE", pfx, current); break;
6871 case ISN_ANYINDEX: smsg("%s%4d ANYINDEX", pfx, current); break;
6872 case ISN_ANYSLICE: smsg("%s%4d ANYSLICE", pfx, current); break;
6873 case ISN_SLICE: smsg("%s%4d SLICE %lld",
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02006874 pfx, current, iptr->isn_arg.number); break;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02006875 case ISN_GETITEM: smsg("%s%4d ITEM %lld%s", pfx, current,
6876 iptr->isn_arg.getitem.gi_index,
6877 iptr->isn_arg.getitem.gi_with_op ?
6878 " with op" : ""); break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006879 case ISN_MEMBER: smsg("%s%4d MEMBER", pfx, current); break;
6880 case ISN_STRINGMEMBER: smsg("%s%4d MEMBER %s", pfx, current,
6881 iptr->isn_arg.string); break;
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00006882 case ISN_GET_OBJ_MEMBER: smsg("%s%4d OBJ_MEMBER %d", pfx, current,
6883 (int)iptr->isn_arg.number); break;
6884 case ISN_STORE_THIS: smsg("%s%4d STORE_THIS %d", pfx, current,
Bram Moolenaarffdaca92022-12-09 21:41:48 +00006885 (int)iptr->isn_arg.number); break;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02006886 case ISN_CLEARDICT: smsg("%s%4d CLEARDICT", pfx, current); break;
6887 case ISN_USEDICT: smsg("%s%4d USEDICT", pfx, current); break;
6888
Bram Moolenaar4c137212021-04-19 16:48:48 +02006889 case ISN_NEGATENR: smsg("%s%4d NEGATENR", pfx, current); break;
6890
Bram Moolenaar4c137212021-04-19 16:48:48 +02006891 case ISN_CHECKTYPE:
6892 {
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01006893 checktype_T *ct = &iptr->isn_arg.type;
Bram Moolenaarc6951a72022-12-29 20:56:24 +00006894 char *tofree = NULL;
6895 char *typename;
6896
6897 if (ct->ct_type->tt_type == VAR_FLOAT
6898 && (ct->ct_type->tt_flags & TTFLAG_NUMBER_OK))
6899 typename = "float|number";
6900 else
6901 typename = type_name(ct->ct_type, &tofree);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006902
6903 if (ct->ct_arg_idx == 0)
6904 smsg("%s%4d CHECKTYPE %s stack[%d]", pfx, current,
Bram Moolenaarc6951a72022-12-29 20:56:24 +00006905 typename,
Bram Moolenaar4c137212021-04-19 16:48:48 +02006906 (int)ct->ct_off);
6907 else
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01006908 smsg("%s%4d CHECKTYPE %s stack[%d] %s %d",
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02006909 pfx, current,
Bram Moolenaarc6951a72022-12-29 20:56:24 +00006910 typename,
Bram Moolenaar4c137212021-04-19 16:48:48 +02006911 (int)ct->ct_off,
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01006912 ct->ct_is_var ? "var": "arg",
Bram Moolenaar4c137212021-04-19 16:48:48 +02006913 (int)ct->ct_arg_idx);
6914 vim_free(tofree);
6915 break;
6916 }
6917 case ISN_CHECKLEN: smsg("%s%4d CHECKLEN %s%d", pfx, current,
6918 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
6919 iptr->isn_arg.checklen.cl_min_len);
6920 break;
6921 case ISN_SETTYPE:
6922 {
6923 char *tofree;
6924
6925 smsg("%s%4d SETTYPE %s", pfx, current,
6926 type_name(iptr->isn_arg.type.ct_type, &tofree));
6927 vim_free(tofree);
6928 break;
6929 }
6930 case ISN_COND2BOOL: smsg("%s%4d COND2BOOL", pfx, current); break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006931 case ISN_2BOOL: if (iptr->isn_arg.tobool.invert)
6932 smsg("%s%4d INVERT %d (!val)", pfx, current,
6933 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006934 else
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006935 smsg("%s%4d 2BOOL %d (!!val)", pfx, current,
6936 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006937 break;
6938 case ISN_2STRING: smsg("%s%4d 2STRING stack[%lld]", pfx, current,
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006939 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006940 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006941 case ISN_2STRING_ANY: smsg("%s%4d 2STRING_ANY stack[%lld]",
6942 pfx, current,
6943 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006944 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006945 case ISN_RANGE: smsg("%s%4d RANGE %s", pfx, current,
6946 iptr->isn_arg.string);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006947 break;
6948 case ISN_PUT:
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01006949 if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE)
Bram Moolenaar4c137212021-04-19 16:48:48 +02006950 smsg("%s%4d PUT %c above range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006951 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006952 else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE)
6953 smsg("%s%4d PUT %c range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006954 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006955 else
6956 smsg("%s%4d PUT %c %ld", pfx, current,
6957 iptr->isn_arg.put.put_regname,
6958 (long)iptr->isn_arg.put.put_lnum);
6959 break;
6960
Bram Moolenaar4c137212021-04-19 16:48:48 +02006961 case ISN_CMDMOD:
6962 {
6963 char_u *buf;
6964 size_t len = produce_cmdmods(
6965 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
6966
6967 buf = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02006968 if (likely(buf != NULL))
Bram Moolenaar4c137212021-04-19 16:48:48 +02006969 {
6970 (void)produce_cmdmods(
6971 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
6972 smsg("%s%4d CMDMOD %s", pfx, current, buf);
6973 vim_free(buf);
6974 }
6975 break;
6976 }
6977 case ISN_CMDMOD_REV: smsg("%s%4d CMDMOD_REV", pfx, current); break;
6978
6979 case ISN_PROF_START:
Bram Moolenaare99d4222021-06-13 14:01:26 +02006980 smsg("%s%4d PROFILE START line %d", pfx, current,
6981 iptr->isn_lnum);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006982 break;
6983
6984 case ISN_PROF_END:
6985 smsg("%s%4d PROFILE END", pfx, current);
6986 break;
6987
Bram Moolenaare99d4222021-06-13 14:01:26 +02006988 case ISN_DEBUG:
Bram Moolenaar8cec9272021-06-23 20:20:53 +02006989 smsg("%s%4d DEBUG line %d-%d varcount %lld", pfx, current,
6990 iptr->isn_arg.debug.dbg_break_lnum + 1,
6991 iptr->isn_lnum,
6992 iptr->isn_arg.debug.dbg_var_names_len);
Bram Moolenaare99d4222021-06-13 14:01:26 +02006993 break;
6994
Bram Moolenaar4c137212021-04-19 16:48:48 +02006995 case ISN_UNPACK: smsg("%s%4d UNPACK %d%s", pfx, current,
6996 iptr->isn_arg.unpack.unp_count,
6997 iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : "");
6998 break;
6999 case ISN_SHUFFLE: smsg("%s%4d SHUFFLE %d up %d", pfx, current,
7000 iptr->isn_arg.shuffle.shfl_item,
7001 iptr->isn_arg.shuffle.shfl_up);
7002 break;
7003 case ISN_DROP: smsg("%s%4d DROP", pfx, current); break;
7004
7005 case ISN_FINISH: // End of list of instructions for ISN_SUBSTITUTE.
7006 return;
7007 }
7008
7009 out_flush(); // output one line at a time
7010 ui_breakcheck();
7011 if (got_int)
7012 break;
7013 }
7014}
7015
7016/*
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02007017 * Handle command line completion for the :disassemble command.
7018 */
7019 void
7020set_context_in_disassemble_cmd(expand_T *xp, char_u *arg)
7021{
7022 char_u *p;
7023
7024 // Default: expand user functions, "debug" and "profile"
7025 xp->xp_context = EXPAND_DISASSEMBLE;
7026 xp->xp_pattern = arg;
7027
7028 // first argument already typed: only user function names
7029 if (*arg != NUL && *(p = skiptowhite(arg)) != NUL)
7030 {
7031 xp->xp_context = EXPAND_USER_FUNC;
7032 xp->xp_pattern = skipwhite(p);
7033 }
7034}
7035
7036/*
7037 * Function given to ExpandGeneric() to obtain the list of :disassemble
7038 * arguments.
7039 */
7040 char_u *
7041get_disassemble_argument(expand_T *xp, int idx)
7042{
7043 if (idx == 0)
7044 return (char_u *)"debug";
7045 if (idx == 1)
7046 return (char_u *)"profile";
7047 return get_user_func_name(xp, idx - 2);
7048}
7049
7050/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01007051 * ":disassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01007052 * We don't really need this at runtime, but we do have tests that require it,
7053 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007054 */
7055 void
7056ex_disassemble(exarg_T *eap)
7057{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01007058 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01007059 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007060 dfunc_T *dfunc;
Bram Moolenaardafef512022-05-21 21:55:55 +01007061 isn_T *instr = NULL; // init to shut up gcc warning
7062 int instr_count = 0; // init to shut up gcc warning
Bram Moolenaar5a01caa2022-05-21 18:56:58 +01007063 compiletype_T compile_type = CT_NONE;
Bram Moolenaare99d4222021-06-13 14:01:26 +02007064
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01007065 ufunc = find_func_by_name(arg, &compile_type);
Bram Moolenaara26b9702020-04-18 19:53:28 +02007066 if (ufunc == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007067 return;
Bram Moolenaare99d4222021-06-13 14:01:26 +02007068 if (func_needs_compiling(ufunc, compile_type)
7069 && compile_def_function(ufunc, FALSE, compile_type, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02007070 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007071 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007072 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007073 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007074 return;
7075 }
Bram Moolenaar6db660b2021-08-01 14:08:54 +02007076 msg((char *)printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007077
7078 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
Bram Moolenaare99d4222021-06-13 14:01:26 +02007079 switch (compile_type)
7080 {
7081 case CT_PROFILE:
Bram Moolenaarf002a412021-01-24 13:34:18 +01007082#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02007083 instr = dfunc->df_instr_prof;
7084 instr_count = dfunc->df_instr_prof_count;
7085 break;
Bram Moolenaarf002a412021-01-24 13:34:18 +01007086#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02007087 // FALLTHROUGH
7088 case CT_NONE:
7089 instr = dfunc->df_instr;
7090 instr_count = dfunc->df_instr_count;
7091 break;
7092 case CT_DEBUG:
7093 instr = dfunc->df_instr_debug;
7094 instr_count = dfunc->df_instr_debug_count;
7095 break;
7096 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007097
Bram Moolenaar4c137212021-04-19 16:48:48 +02007098 list_instructions("", instr, instr_count, ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007099}
7100
7101/*
Bram Moolenaar13106602020-10-04 16:06:05 +02007102 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007103 * list, etc. Mostly like what JavaScript does, except that empty list and
7104 * empty dictionary are FALSE.
7105 */
7106 int
7107tv2bool(typval_T *tv)
7108{
7109 switch (tv->v_type)
7110 {
7111 case VAR_NUMBER:
7112 return tv->vval.v_number != 0;
7113 case VAR_FLOAT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007114 return tv->vval.v_float != 0.0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007115 case VAR_PARTIAL:
7116 return tv->vval.v_partial != NULL;
7117 case VAR_FUNC:
7118 case VAR_STRING:
7119 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
7120 case VAR_LIST:
7121 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
7122 case VAR_DICT:
7123 return tv->vval.v_dict != NULL
7124 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
7125 case VAR_BOOL:
7126 case VAR_SPECIAL:
7127 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
7128 case VAR_JOB:
7129#ifdef FEAT_JOB_CHANNEL
7130 return tv->vval.v_job != NULL;
7131#else
7132 break;
7133#endif
7134 case VAR_CHANNEL:
7135#ifdef FEAT_JOB_CHANNEL
7136 return tv->vval.v_channel != NULL;
7137#else
7138 break;
7139#endif
7140 case VAR_BLOB:
7141 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
7142 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02007143 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007144 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02007145 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00007146 case VAR_CLASS:
7147 case VAR_OBJECT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007148 break;
7149 }
7150 return FALSE;
7151}
7152
Bram Moolenaarea2d4072020-11-12 12:08:51 +01007153 void
7154emsg_using_string_as(typval_T *tv, int as_number)
7155{
7156 semsg(_(as_number ? e_using_string_as_number_str
7157 : e_using_string_as_bool_str),
7158 tv->vval.v_string == NULL
7159 ? (char_u *)"" : tv->vval.v_string);
7160}
7161
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007162/*
7163 * If "tv" is a string give an error and return FAIL.
7164 */
7165 int
7166check_not_string(typval_T *tv)
7167{
7168 if (tv->v_type == VAR_STRING)
7169 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +01007170 emsg_using_string_as(tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007171 clear_tv(tv);
7172 return FAIL;
7173 }
7174 return OK;
7175}
7176
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007177
7178#endif // FEAT_EVAL