blob: 22813e1597741844059bb286c68f18d43fb922a3 [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
67// 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)
74
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 Moolenaar8a7d6542020-01-26 15:56:19 +0100104// Get pointer to item relative to the bottom of the stack, -1 is the last one.
Bram Moolenaar11107ba2020-08-15 21:10:16 +0200105#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 +0100106
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200107 void
108to_string_error(vartype_T vartype)
109{
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200110 semsg(_(e_cannot_convert_str_to_string), vartype_name(vartype));
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200111}
112
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100113/*
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100114 * Return the number of arguments, including optional arguments and any vararg.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100115 */
116 static int
117ufunc_argcount(ufunc_T *ufunc)
118{
119 return ufunc->uf_args.ga_len + (ufunc->uf_va_name != NULL ? 1 : 0);
120}
121
122/*
LemonBoy372bcce2022-04-25 12:43:20 +0100123 * Create a new string from "count" items at the bottom of the stack.
124 * A trailing NUL is appended.
125 * When "count" is zero an empty string is added to the stack.
126 */
127 static int
128exe_concat(int count, ectx_T *ectx)
129{
130 int idx;
131 int len = 0;
132 typval_T *tv;
133 garray_T ga;
134
135 ga_init2(&ga, sizeof(char), 1);
136 // Preallocate enough space for the whole string to avoid having to grow
137 // and copy.
138 for (idx = 0; idx < count; ++idx)
139 {
140 tv = STACK_TV_BOT(idx - count);
141 if (tv->vval.v_string != NULL)
142 len += (int)STRLEN(tv->vval.v_string);
143 }
144
145 if (ga_grow(&ga, len + 1) == FAIL)
146 return FAIL;
147
148 for (idx = 0; idx < count; ++idx)
149 {
150 tv = STACK_TV_BOT(idx - count);
151 ga_concat(&ga, tv->vval.v_string);
152 clear_tv(tv);
153 }
154
155 // add a terminating NUL
156 ga_append(&ga, NUL);
157
158 ectx->ec_stack.ga_len -= count - 1;
159 STACK_TV_BOT(-1)->vval.v_string = ga.ga_data;
160
161 return OK;
162}
163
164/*
Bram Moolenaarfe270812020-04-11 22:31:27 +0200165 * Create a new list from "count" items at the bottom of the stack.
166 * When "count" is zero an empty list is added to the stack.
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100167 * When "count" is -1 a NULL list is added to the stack.
Bram Moolenaarfe270812020-04-11 22:31:27 +0200168 */
169 static int
170exe_newlist(int count, ectx_T *ectx)
171{
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100172 list_T *list = NULL;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200173 int idx;
174 typval_T *tv;
175
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100176 if (count >= 0)
177 {
178 list = list_alloc_with_items(count);
179 if (list == NULL)
180 return FAIL;
181 for (idx = 0; idx < count; ++idx)
182 list_set_item(list, idx, STACK_TV_BOT(idx - count));
183 }
Bram Moolenaarfe270812020-04-11 22:31:27 +0200184
185 if (count > 0)
186 ectx->ec_stack.ga_len -= count - 1;
Bram Moolenaar35578162021-08-02 19:10:38 +0200187 else if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100188 {
189 list_unref(list);
Bram Moolenaarfe270812020-04-11 22:31:27 +0200190 return FAIL;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100191 }
Bram Moolenaarfe270812020-04-11 22:31:27 +0200192 else
193 ++ectx->ec_stack.ga_len;
194 tv = STACK_TV_BOT(-1);
195 tv->v_type = VAR_LIST;
196 tv->vval.v_list = list;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100197 if (list != NULL)
198 ++list->lv_refcount;
199 return OK;
200}
201
202/*
203 * Implementation of ISN_NEWDICT.
204 * Returns FAIL on total failure, MAYBE on error.
205 */
206 static int
207exe_newdict(int count, ectx_T *ectx)
208{
209 dict_T *dict = NULL;
210 dictitem_T *item;
211 char_u *key;
212 int idx;
213 typval_T *tv;
214
215 if (count >= 0)
216 {
217 dict = dict_alloc();
218 if (unlikely(dict == NULL))
219 return FAIL;
220 for (idx = 0; idx < count; ++idx)
221 {
222 // have already checked key type is VAR_STRING
223 tv = STACK_TV_BOT(2 * (idx - count));
224 // check key is unique
225 key = tv->vval.v_string == NULL
226 ? (char_u *)"" : tv->vval.v_string;
227 item = dict_find(dict, key, -1);
228 if (item != NULL)
229 {
230 semsg(_(e_duplicate_key_in_dicitonary), key);
231 dict_unref(dict);
232 return MAYBE;
233 }
234 item = dictitem_alloc(key);
235 clear_tv(tv);
236 if (unlikely(item == NULL))
237 {
238 dict_unref(dict);
239 return FAIL;
240 }
Bram Moolenaar0d1f55c2022-04-05 17:30:29 +0100241 tv = STACK_TV_BOT(2 * (idx - count) + 1);
242 item->di_tv = *tv;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100243 item->di_tv.v_lock = 0;
Bram Moolenaar0d1f55c2022-04-05 17:30:29 +0100244 tv->v_type = VAR_UNKNOWN;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100245 if (dict_add(dict, item) == FAIL)
246 {
247 // can this ever happen?
248 dict_unref(dict);
249 return FAIL;
250 }
251 }
252 }
253
254 if (count > 0)
255 ectx->ec_stack.ga_len -= 2 * count - 1;
256 else if (GA_GROW_FAILS(&ectx->ec_stack, 1))
257 return FAIL;
258 else
259 ++ectx->ec_stack.ga_len;
260 tv = STACK_TV_BOT(-1);
261 tv->v_type = VAR_DICT;
262 tv->v_lock = 0;
263 tv->vval.v_dict = dict;
264 if (dict != NULL)
265 ++dict->dv_refcount;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200266 return OK;
267}
268
269/*
Bram Moolenaar4f8f5422021-06-20 19:28:14 +0200270 * If debug_tick changed check if "ufunc" has a breakpoint and update
271 * "uf_has_breakpoint".
272 */
Bram Moolenaar96923b72022-03-15 15:57:04 +0000273 void
Bram Moolenaar4f8f5422021-06-20 19:28:14 +0200274update_has_breakpoint(ufunc_T *ufunc)
275{
276 if (ufunc->uf_debug_tick != debug_tick)
277 {
278 linenr_T breakpoint;
279
280 ufunc->uf_debug_tick = debug_tick;
281 breakpoint = dbg_find_breakpoint(FALSE, ufunc->uf_name, 0);
282 ufunc->uf_has_breakpoint = breakpoint > 0;
283 }
284}
285
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +0200286static garray_T dict_stack = GA_EMPTY;
287
288/*
289 * Put a value on the dict stack. This consumes "tv".
290 */
291 static int
292dict_stack_save(typval_T *tv)
293{
294 if (dict_stack.ga_growsize == 0)
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000295 ga_init2(&dict_stack, sizeof(typval_T), 10);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +0200296 if (ga_grow(&dict_stack, 1) == FAIL)
297 return FAIL;
298 ((typval_T *)dict_stack.ga_data)[dict_stack.ga_len] = *tv;
299 ++dict_stack.ga_len;
300 return OK;
301}
302
303/*
304 * Get the typval at top of the dict stack.
305 */
306 static typval_T *
307dict_stack_get_tv(void)
308{
309 if (dict_stack.ga_len == 0)
310 return NULL;
311 return ((typval_T *)dict_stack.ga_data) + dict_stack.ga_len - 1;
312}
313
314/*
315 * Get the dict at top of the dict stack.
316 */
317 static dict_T *
318dict_stack_get_dict(void)
319{
320 typval_T *tv;
321
322 if (dict_stack.ga_len == 0)
323 return NULL;
324 tv = ((typval_T *)dict_stack.ga_data) + dict_stack.ga_len - 1;
325 if (tv->v_type == VAR_DICT)
326 return tv->vval.v_dict;
327 return NULL;
328}
329
330/*
331 * Drop an item from the dict stack.
332 */
333 static void
334dict_stack_drop(void)
335{
336 if (dict_stack.ga_len == 0)
337 {
338 iemsg("Dict stack underflow");
339 return;
340 }
341 --dict_stack.ga_len;
342 clear_tv(((typval_T *)dict_stack.ga_data) + dict_stack.ga_len);
343}
344
345/*
346 * Drop items from the dict stack until the length is equal to "len".
347 */
348 static void
349dict_stack_clear(int len)
350{
351 while (dict_stack.ga_len > len)
352 dict_stack_drop();
353}
354
Bram Moolenaar4f8f5422021-06-20 19:28:14 +0200355/*
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +0000356 * Get a pointer to useful "pt_outer" of "pt".
357 */
358 static outer_T *
359get_pt_outer(partial_T *pt)
360{
361 partial_T *ptref = pt->pt_outer_partial;
362
363 if (ptref == NULL)
364 return &pt->pt_outer;
365
366 // partial using partial (recursively)
367 while (ptref->pt_outer_partial != NULL)
368 ptref = ptref->pt_outer_partial;
369 return &ptref->pt_outer;
370}
371
372/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100373 * Call compiled function "cdf_idx" from compiled code.
Bram Moolenaarab360522021-01-10 14:02:28 +0100374 * This adds a stack frame and sets the instruction pointer to the start of the
375 * called function.
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200376 * If "pt" is not null use "pt->pt_outer" for ec_outer_ref->or_outer.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100377 *
378 * Stack has:
379 * - current arguments (already there)
380 * - omitted optional argument (default values) added here
381 * - stack frame:
382 * - pointer to calling function
383 * - Index of next instruction in calling function
384 * - previous frame pointer
385 * - reserved space for local variables
386 */
387 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100388call_dfunc(
389 int cdf_idx,
390 partial_T *pt,
391 int argcount_arg,
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100392 ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100393{
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100394 int argcount = argcount_arg;
395 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + cdf_idx;
396 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200397 int did_emsg_before = did_emsg_cumul + did_emsg;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100398 int arg_to_add;
399 int vararg_count = 0;
400 int varcount;
401 int idx;
402 estack_T *entry;
403 funclocal_T *floc = NULL;
Bram Moolenaar648594e2021-07-11 17:55:01 +0200404 int res = OK;
Bram Moolenaar139575d2022-03-15 19:29:30 +0000405 compiletype_T compile_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100406
407 if (dfunc->df_deleted)
408 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100409 // don't use ufunc->uf_name, it may have been freed
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000410 emsg_funcname(e_function_was_deleted_str,
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100411 dfunc->df_name == NULL ? (char_u *)"unknown" : dfunc->df_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100412 return FAIL;
413 }
414
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100415#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +0100416 if (do_profiling == PROF_YES)
417 {
Bram Moolenaar35578162021-08-02 19:10:38 +0200418 if (GA_GROW_OK(&profile_info_ga, 1))
Bram Moolenaar12d26532021-02-19 19:13:21 +0100419 {
420 profinfo_T *info = ((profinfo_T *)profile_info_ga.ga_data)
421 + profile_info_ga.ga_len;
422 ++profile_info_ga.ga_len;
423 CLEAR_POINTER(info);
424 profile_may_start_func(info, ufunc,
425 (((dfunc_T *)def_functions.ga_data)
426 + ectx->ec_dfunc_idx)->df_ufunc);
427 }
Bram Moolenaar12d26532021-02-19 19:13:21 +0100428 }
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100429#endif
430
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200431 // When debugging and using "cont" switches to the not-debugged
432 // instructions, may need to still compile them.
Bram Moolenaar139575d2022-03-15 19:29:30 +0000433 compile_type = get_compile_type(ufunc);
434 if (func_needs_compiling(ufunc, compile_type))
Bram Moolenaar648594e2021-07-11 17:55:01 +0200435 {
Bram Moolenaar139575d2022-03-15 19:29:30 +0000436 res = compile_def_function(ufunc, FALSE, compile_type, NULL);
Bram Moolenaar648594e2021-07-11 17:55:01 +0200437
438 // compile_def_function() may cause def_functions.ga_data to change
439 dfunc = ((dfunc_T *)def_functions.ga_data) + cdf_idx;
440 }
441 if (res == FAIL || INSTRUCTIONS(dfunc) == NULL)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200442 {
443 if (did_emsg_cumul + did_emsg == did_emsg_before)
444 semsg(_(e_function_is_not_compiled_str),
445 printable_func_name(ufunc));
446 return FAIL;
447 }
448
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200449 if (ufunc->uf_va_name != NULL)
450 {
Bram Moolenaarfe270812020-04-11 22:31:27 +0200451 // Need to make a list out of the vararg arguments.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200452 // Stack at time of call with 2 varargs:
453 // normal_arg
454 // optional_arg
455 // vararg_1
456 // vararg_2
Bram Moolenaarfe270812020-04-11 22:31:27 +0200457 // After creating the list:
458 // normal_arg
459 // optional_arg
460 // vararg-list
461 // With missing optional arguments we get:
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200462 // normal_arg
Bram Moolenaarfe270812020-04-11 22:31:27 +0200463 // After creating the list
464 // normal_arg
465 // (space for optional_arg)
466 // vararg-list
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200467 vararg_count = argcount - ufunc->uf_args.ga_len;
468 if (vararg_count < 0)
469 vararg_count = 0;
470 else
471 argcount -= vararg_count;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200472 if (exe_newlist(vararg_count, ectx) == FAIL)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200473 return FAIL;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200474
475 vararg_count = 1;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200476 }
477
Bram Moolenaarfe270812020-04-11 22:31:27 +0200478 arg_to_add = ufunc->uf_args.ga_len - argcount;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200479 if (arg_to_add < 0)
480 {
Bram Moolenaar79e8db92020-08-14 22:16:33 +0200481 if (arg_to_add == -1)
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200482 emsg(_(e_one_argument_too_many));
Bram Moolenaar79e8db92020-08-14 22:16:33 +0200483 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200484 semsg(_(e_nr_arguments_too_many), -arg_to_add);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200485 return FAIL;
486 }
Bram Moolenaarcd1cda22022-02-16 21:48:25 +0000487 else if (arg_to_add > ufunc->uf_def_args.ga_len)
488 {
489 int missing = arg_to_add - ufunc->uf_def_args.ga_len;
490
491 if (missing == 1)
492 emsg(_(e_one_argument_too_few));
493 else
494 semsg(_(e_nr_arguments_too_few), missing);
495 return FAIL;
496 }
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200497
498 // Reserve space for:
499 // - missing arguments
500 // - stack frame
501 // - local variables
502 // - if needed: a counter for number of closures created in
503 // ectx->ec_funcrefs.
504 varcount = dfunc->df_varcount + dfunc->df_has_closure;
Bram Moolenaar35578162021-08-02 19:10:38 +0200505 if (GA_GROW_FAILS(&ectx->ec_stack, arg_to_add + STACK_FRAME_SIZE + varcount))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100506 return FAIL;
507
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100508 // If depth of calling is getting too high, don't execute the function.
509 if (funcdepth_increment() == FAIL)
510 return FAIL;
Bram Moolenaare99d4222021-06-13 14:01:26 +0200511 ++ex_nesting_level;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100512
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100513 // Only make a copy of funclocal if it contains something to restore.
Bram Moolenaar4c137212021-04-19 16:48:48 +0200514 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100515 {
516 floc = ALLOC_ONE(funclocal_T);
517 if (floc == NULL)
518 return FAIL;
Bram Moolenaar4c137212021-04-19 16:48:48 +0200519 *floc = ectx->ec_funclocal;
520 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100521 }
522
Bram Moolenaarfe270812020-04-11 22:31:27 +0200523 // Move the vararg-list to below the missing optional arguments.
524 if (vararg_count > 0 && arg_to_add > 0)
525 *STACK_TV_BOT(arg_to_add - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100526
527 // Reserve space for omitted optional arguments, filled in soon.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200528 for (idx = 0; idx < arg_to_add; ++idx)
Bram Moolenaarfe270812020-04-11 22:31:27 +0200529 STACK_TV_BOT(idx - vararg_count)->v_type = VAR_UNKNOWN;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200530 ectx->ec_stack.ga_len += arg_to_add;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100531
532 // Store current execution state in stack frame for ISN_RETURN.
Bram Moolenaar0186e582021-01-10 18:33:11 +0100533 STACK_TV_BOT(STACK_FRAME_FUNC_OFF)->vval.v_number = ectx->ec_dfunc_idx;
534 STACK_TV_BOT(STACK_FRAME_IIDX_OFF)->vval.v_number = ectx->ec_iidx;
Bram Moolenaar5930ddc2021-04-26 20:32:59 +0200535 STACK_TV_BOT(STACK_FRAME_INSTR_OFF)->vval.v_string = (void *)ectx->ec_instr;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200536 STACK_TV_BOT(STACK_FRAME_OUTER_OFF)->vval.v_string =
537 (void *)ectx->ec_outer_ref;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100538 STACK_TV_BOT(STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string = (void *)floc;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100539 STACK_TV_BOT(STACK_FRAME_IDX_OFF)->vval.v_number = ectx->ec_frame_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200540 ectx->ec_frame_idx = ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100541
542 // Initialize local variables
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200543 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaar5cd64792021-12-25 18:23:24 +0000544 {
545 typval_T *tv = STACK_TV_BOT(STACK_FRAME_SIZE + idx);
546
547 tv->v_type = VAR_NUMBER;
548 tv->vval.v_number = 0;
549 }
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200550 if (dfunc->df_has_closure)
551 {
552 typval_T *tv = STACK_TV_BOT(STACK_FRAME_SIZE + dfunc->df_varcount);
553
554 tv->v_type = VAR_NUMBER;
555 tv->vval.v_number = 0;
556 }
557 ectx->ec_stack.ga_len += STACK_FRAME_SIZE + varcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100558
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +0100559 if (pt != NULL || ufunc->uf_partial != NULL
560 || (ufunc->uf_flags & FC_CLOSURE))
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100561 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200562 outer_ref_T *ref = ALLOC_CLEAR_ONE(outer_ref_T);
Bram Moolenaar0186e582021-01-10 18:33:11 +0100563
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200564 if (ref == NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +0100565 return FAIL;
566 if (pt != NULL)
567 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +0000568 ref->or_outer = get_pt_outer(pt);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200569 ++pt->pt_refcount;
570 ref->or_partial = pt;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100571 }
572 else if (ufunc->uf_partial != NULL)
573 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +0000574 ref->or_outer = get_pt_outer(ufunc->uf_partial);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200575 ++ufunc->uf_partial->pt_refcount;
576 ref->or_partial = ufunc->uf_partial;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100577 }
578 else
579 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200580 ref->or_outer = ALLOC_CLEAR_ONE(outer_T);
Dominique Pelle5a9e5842021-07-24 19:32:12 +0200581 if (unlikely(ref->or_outer == NULL))
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200582 {
583 vim_free(ref);
584 return FAIL;
585 }
586 ref->or_outer_allocated = TRUE;
587 ref->or_outer->out_stack = &ectx->ec_stack;
588 ref->or_outer->out_frame_idx = ectx->ec_frame_idx;
589 if (ectx->ec_outer_ref != NULL)
590 ref->or_outer->out_up = ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100591 }
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200592 ectx->ec_outer_ref = ref;
Bram Moolenaarab360522021-01-10 14:02:28 +0100593 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100594 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200595 ectx->ec_outer_ref = NULL;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100596
Bram Moolenaarc970e422021-03-17 15:03:04 +0100597 ++ufunc->uf_calls;
598
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100599 // Set execution state to the start of the called function.
600 ectx->ec_dfunc_idx = cdf_idx;
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100601 ectx->ec_instr = INSTRUCTIONS(dfunc);
Bram Moolenaarb2049902021-01-24 12:53:53 +0100602 entry = estack_push_ufunc(ufunc, 1);
Bram Moolenaarc620c052020-07-08 15:16:19 +0200603 if (entry != NULL)
604 {
605 // Set the script context to the script where the function was defined.
Bram Moolenaarc70fe462021-04-17 17:59:19 +0200606 // Save the current context so it can be restored on return.
607 entry->es_save_sctx = current_sctx;
608 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200609 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100610
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +0200611 // Start execution at the first instruction.
612 ectx->ec_iidx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100613
614 return OK;
615}
616
617// Get pointer to item in the stack.
618#define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
619
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000620// Double linked list of funcstack_T in use.
621static funcstack_T *first_funcstack = NULL;
622
623 static void
624add_funcstack_to_list(funcstack_T *funcstack)
625{
626 // Link in list of funcstacks.
627 if (first_funcstack != NULL)
628 first_funcstack->fs_prev = funcstack;
629 funcstack->fs_next = first_funcstack;
630 funcstack->fs_prev = NULL;
631 first_funcstack = funcstack;
632}
633
634 static void
635remove_funcstack_from_list(funcstack_T *funcstack)
636{
637 if (funcstack->fs_prev == NULL)
638 first_funcstack = funcstack->fs_next;
639 else
640 funcstack->fs_prev->fs_next = funcstack->fs_next;
641 if (funcstack->fs_next != NULL)
642 funcstack->fs_next->fs_prev = funcstack->fs_prev;
643}
644
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100645/*
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200646 * Used when returning from a function: Check if any closure is still
647 * referenced. If so then move the arguments and variables to a separate piece
648 * of stack to be used when the closure is called.
649 * When "free_arguments" is TRUE the arguments are to be freed.
650 * Returns FAIL when out of memory.
651 */
652 static int
653handle_closure_in_use(ectx_T *ectx, int free_arguments)
654{
655 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
656 + ectx->ec_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200657 int argcount;
658 int top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200659 int idx;
660 typval_T *tv;
661 int closure_in_use = FALSE;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200662 garray_T *gap = &ectx->ec_funcrefs;
663 varnumber_T closure_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200664
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200665 if (dfunc->df_ufunc == NULL)
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200666 return OK; // function was freed
667 if (dfunc->df_has_closure == 0)
668 return OK; // no closures
669 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + dfunc->df_varcount);
670 closure_count = tv->vval.v_number;
671 if (closure_count == 0)
672 return OK; // no funcrefs created
673
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200674 argcount = ufunc_argcount(dfunc->df_ufunc);
675 top = ectx->ec_frame_idx - argcount;
676
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200677 // Check if any created closure is still in use.
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200678 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200679 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200680 partial_T *pt;
681 int off = gap->ga_len - closure_count + idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200682
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200683 if (off < 0)
684 continue; // count is off or already done
685 pt = ((partial_T **)gap->ga_data)[off];
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200686 if (pt->pt_refcount > 1)
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200687 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200688 int refcount = pt->pt_refcount;
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200689 int i;
690
Dominique Pelleaf4a61a2021-12-27 17:21:41 +0000691 // A Reference in a local variable doesn't count, it gets
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200692 // unreferenced on return.
693 for (i = 0; i < dfunc->df_varcount; ++i)
694 {
695 typval_T *stv = STACK_TV(ectx->ec_frame_idx
696 + STACK_FRAME_SIZE + i);
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200697 if (stv->v_type == VAR_PARTIAL && pt == stv->vval.v_partial)
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200698 --refcount;
699 }
700 if (refcount > 1)
701 {
702 closure_in_use = TRUE;
703 break;
704 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200705 }
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200706 }
707
708 if (closure_in_use)
709 {
710 funcstack_T *funcstack = ALLOC_CLEAR_ONE(funcstack_T);
711 typval_T *stack;
712
713 // A closure is using the arguments and/or local variables.
714 // Move them to the called function.
715 if (funcstack == NULL)
716 return FAIL;
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000717
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200718 funcstack->fs_var_offset = argcount + STACK_FRAME_SIZE;
719 funcstack->fs_ga.ga_len = funcstack->fs_var_offset + dfunc->df_varcount;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200720 stack = ALLOC_CLEAR_MULT(typval_T, funcstack->fs_ga.ga_len);
721 funcstack->fs_ga.ga_data = stack;
722 if (stack == NULL)
723 {
724 vim_free(funcstack);
725 return FAIL;
726 }
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000727 add_funcstack_to_list(funcstack);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200728
729 // Move or copy the arguments.
730 for (idx = 0; idx < argcount; ++idx)
731 {
732 tv = STACK_TV(top + idx);
733 if (free_arguments)
734 {
735 *(stack + idx) = *tv;
736 tv->v_type = VAR_UNKNOWN;
737 }
738 else
739 copy_tv(tv, stack + idx);
740 }
741 // Move the local variables.
742 for (idx = 0; idx < dfunc->df_varcount; ++idx)
743 {
744 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + idx);
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200745
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200746 // A partial created for a local function, that is also used as a
747 // local variable, has a reference count for the variable, thus
748 // will never go down to zero. When all these refcounts are one
749 // then the funcstack is unused. We need to count how many we have
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000750 // so we know when to check.
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200751 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL)
752 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200753 int i;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200754
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200755 for (i = 0; i < closure_count; ++i)
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200756 if (tv->vval.v_partial == ((partial_T **)gap->ga_data)[
757 gap->ga_len - closure_count + i])
758 ++funcstack->fs_min_refcount;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200759 }
760
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200761 *(stack + funcstack->fs_var_offset + idx) = *tv;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200762 tv->v_type = VAR_UNKNOWN;
763 }
764
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200765 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200766 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200767 partial_T *pt = ((partial_T **)gap->ga_data)[gap->ga_len
768 - closure_count + idx];
769 if (pt->pt_refcount > 1)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200770 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200771 ++funcstack->fs_refcount;
772 pt->pt_funcstack = funcstack;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100773 pt->pt_outer.out_stack = &funcstack->fs_ga;
774 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx - top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200775 }
776 }
777 }
778
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200779 for (idx = 0; idx < closure_count; ++idx)
780 partial_unref(((partial_T **)gap->ga_data)[gap->ga_len
781 - closure_count + idx]);
782 gap->ga_len -= closure_count;
783 if (gap->ga_len == 0)
784 ga_clear(gap);
785
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200786 return OK;
787}
788
789/*
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200790 * Called when a partial is freed or its reference count goes down to one. The
791 * funcstack may be the only reference to the partials in the local variables.
792 * Go over all of them, the funcref and can be freed if all partials
793 * referencing the funcstack have a reference count of one.
794 */
795 void
796funcstack_check_refcount(funcstack_T *funcstack)
797{
798 int i;
799 garray_T *gap = &funcstack->fs_ga;
800 int done = 0;
801
802 if (funcstack->fs_refcount > funcstack->fs_min_refcount)
803 return;
804 for (i = funcstack->fs_var_offset; i < gap->ga_len; ++i)
805 {
806 typval_T *tv = ((typval_T *)gap->ga_data) + i;
807
808 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL
809 && tv->vval.v_partial->pt_funcstack == funcstack
810 && tv->vval.v_partial->pt_refcount == 1)
811 ++done;
812 }
813 if (done == funcstack->fs_min_refcount)
814 {
815 typval_T *stack = gap->ga_data;
816
817 // All partials referencing the funcstack have a reference count of
818 // one, thus the funcstack is no longer of use.
819 for (i = 0; i < gap->ga_len; ++i)
820 clear_tv(stack + i);
821 vim_free(stack);
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000822 remove_funcstack_from_list(funcstack);
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200823 vim_free(funcstack);
824 }
825}
826
827/*
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000828 * For garbage collecting: set references in all variables referenced by
829 * all funcstacks.
830 */
831 int
832set_ref_in_funcstacks(int copyID)
833{
834 funcstack_T *funcstack;
835
836 for (funcstack = first_funcstack; funcstack != NULL;
837 funcstack = funcstack->fs_next)
838 {
839 typval_T *stack = funcstack->fs_ga.ga_data;
840 int i;
841
842 for (i = 0; i < funcstack->fs_ga.ga_len; ++i)
843 if (set_ref_in_item(stack + i, copyID, NULL, NULL))
844 return TRUE; // abort
845 }
846 return FALSE;
847}
848
849/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100850 * Return from the current function.
851 */
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200852 static int
Bram Moolenaar4c137212021-04-19 16:48:48 +0200853func_return(ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100854{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100855 int idx;
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100856 int ret_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200857 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
858 + ectx->ec_dfunc_idx;
859 int argcount = ufunc_argcount(dfunc->df_ufunc);
860 int top = ectx->ec_frame_idx - argcount;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200861 estack_T *entry;
Bram Moolenaar12d26532021-02-19 19:13:21 +0100862 int prev_dfunc_idx = STACK_TV(ectx->ec_frame_idx
863 + STACK_FRAME_FUNC_OFF)->vval.v_number;
Bram Moolenaarb06b50d2021-04-26 21:39:25 +0200864 funclocal_T *floc;
865#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +0100866 dfunc_T *prev_dfunc = ((dfunc_T *)def_functions.ga_data)
867 + prev_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100868
Bram Moolenaar12d26532021-02-19 19:13:21 +0100869 if (do_profiling == PROF_YES)
870 {
871 ufunc_T *caller = prev_dfunc->df_ufunc;
872
873 if (dfunc->df_ufunc->uf_profiling
874 || (caller != NULL && caller->uf_profiling))
875 {
876 profile_may_end_func(((profinfo_T *)profile_info_ga.ga_data)
877 + profile_info_ga.ga_len - 1, dfunc->df_ufunc, caller);
878 --profile_info_ga.ga_len;
879 }
880 }
881#endif
Bram Moolenaar919c12c2021-12-14 14:29:16 +0000882
883 // No check for uf_refcount being zero, cannot think of a way that would
884 // happen.
Bram Moolenaarc970e422021-03-17 15:03:04 +0100885 --dfunc->df_ufunc->uf_calls;
886
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100887 // execution context goes one level up
Bram Moolenaarc620c052020-07-08 15:16:19 +0200888 entry = estack_pop();
889 if (entry != NULL)
Bram Moolenaarc70fe462021-04-17 17:59:19 +0200890 current_sctx = entry->es_save_sctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100891
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200892 if (handle_closure_in_use(ectx, TRUE) == FAIL)
893 return FAIL;
894
895 // Clear the arguments.
896 for (idx = top; idx < ectx->ec_frame_idx; ++idx)
897 clear_tv(STACK_TV(idx));
898
899 // Clear local variables and temp values, but not the return value.
900 for (idx = ectx->ec_frame_idx + STACK_FRAME_SIZE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100901 idx < ectx->ec_stack.ga_len - 1; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100902 clear_tv(STACK_TV(idx));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100903
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100904 // The return value should be on top of the stack. However, when aborting
905 // it may not be there and ec_frame_idx is the top of the stack.
906 ret_idx = ectx->ec_stack.ga_len - 1;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100907 if (ret_idx == ectx->ec_frame_idx + STACK_FRAME_IDX_OFF)
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100908 ret_idx = 0;
909
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200910 if (ectx->ec_outer_ref != NULL)
911 {
912 if (ectx->ec_outer_ref->or_outer_allocated)
913 vim_free(ectx->ec_outer_ref->or_outer);
914 partial_unref(ectx->ec_outer_ref->or_partial);
915 vim_free(ectx->ec_outer_ref);
916 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100917
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100918 // Restore the previous frame.
Bram Moolenaar12d26532021-02-19 19:13:21 +0100919 ectx->ec_dfunc_idx = prev_dfunc_idx;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100920 ectx->ec_iidx = STACK_TV(ectx->ec_frame_idx
921 + STACK_FRAME_IIDX_OFF)->vval.v_number;
Bram Moolenaar5930ddc2021-04-26 20:32:59 +0200922 ectx->ec_instr = (void *)STACK_TV(ectx->ec_frame_idx
923 + STACK_FRAME_INSTR_OFF)->vval.v_string;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200924 ectx->ec_outer_ref = (void *)STACK_TV(ectx->ec_frame_idx
Bram Moolenaar0186e582021-01-10 18:33:11 +0100925 + STACK_FRAME_OUTER_OFF)->vval.v_string;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100926 floc = (void *)STACK_TV(ectx->ec_frame_idx
927 + STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string;
Bram Moolenaar5366e1a2020-10-01 13:01:34 +0200928 // restoring ec_frame_idx must be last
Bram Moolenaar0186e582021-01-10 18:33:11 +0100929 ectx->ec_frame_idx = STACK_TV(ectx->ec_frame_idx
930 + STACK_FRAME_IDX_OFF)->vval.v_number;
Bram Moolenaard386e922021-04-25 14:48:49 +0200931
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100932 if (floc == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +0200933 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100934 else
935 {
Bram Moolenaar4c137212021-04-19 16:48:48 +0200936 ectx->ec_funclocal = *floc;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100937 vim_free(floc);
938 }
939
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100940 if (ret_idx > 0)
941 {
942 // Reset the stack to the position before the call, with a spot for the
943 // return value, moved there from above the frame.
944 ectx->ec_stack.ga_len = top + 1;
945 *STACK_TV_BOT(-1) = *STACK_TV(ret_idx);
946 }
947 else
948 // Reset the stack to the position before the call.
949 ectx->ec_stack.ga_len = top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200950
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100951 funcdepth_decrement();
Bram Moolenaare99d4222021-06-13 14:01:26 +0200952 --ex_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200953 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100954}
955
956#undef STACK_TV
957
958/*
959 * Prepare arguments and rettv for calling a builtin or user function.
960 */
961 static int
962call_prepare(int argcount, typval_T *argvars, ectx_T *ectx)
963{
964 int idx;
965 typval_T *tv;
966
967 // Move arguments from bottom of the stack to argvars[] and add terminator.
968 for (idx = 0; idx < argcount; ++idx)
969 argvars[idx] = *STACK_TV_BOT(idx - argcount);
970 argvars[argcount].v_type = VAR_UNKNOWN;
971
972 // Result replaces the arguments on the stack.
973 if (argcount > 0)
974 ectx->ec_stack.ga_len -= argcount - 1;
Bram Moolenaar35578162021-08-02 19:10:38 +0200975 else if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100976 return FAIL;
977 else
978 ++ectx->ec_stack.ga_len;
979
980 // Default return value is zero.
981 tv = STACK_TV_BOT(-1);
982 tv->v_type = VAR_NUMBER;
983 tv->vval.v_number = 0;
Bram Moolenaar24565cf2022-03-28 18:16:52 +0100984 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100985
986 return OK;
987}
988
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200989// Ugly global to avoid passing the execution context around through many
990// layers.
991static ectx_T *current_ectx = NULL;
992
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100993/*
994 * Call a builtin function by index.
995 */
996 static int
997call_bfunc(int func_idx, int argcount, ectx_T *ectx)
998{
999 typval_T argvars[MAX_FUNC_ARGS];
1000 int idx;
Bram Moolenaar171fb922020-10-28 16:54:47 +01001001 int did_emsg_before = did_emsg;
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001002 ectx_T *prev_ectx = current_ectx;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001003 char *save_func_name = ectx->ec_where.wt_func_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001004
1005 if (call_prepare(argcount, argvars, ectx) == FAIL)
1006 return FAIL;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001007 ectx->ec_where.wt_func_name = internal_func_name(func_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001008
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001009 // Call the builtin function. Set "current_ectx" so that when it
1010 // recursively invokes call_def_function() a closure context can be set.
1011 current_ectx = ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001012 call_internal_func_by_idx(func_idx, argvars, STACK_TV_BOT(-1));
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001013 current_ectx = prev_ectx;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001014 ectx->ec_where.wt_func_name = save_func_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001015
1016 // Clear the arguments.
1017 for (idx = 0; idx < argcount; ++idx)
1018 clear_tv(&argvars[idx]);
Bram Moolenaar015f4262020-05-05 21:25:22 +02001019
Bram Moolenaar57f799e2020-12-12 20:42:19 +01001020 if (did_emsg > did_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +02001021 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001022 return OK;
1023}
1024
1025/*
1026 * Execute a user defined function.
Bram Moolenaarab360522021-01-10 14:02:28 +01001027 * If the function is compiled this will add a stack frame and set the
1028 * instruction pointer at the start of the function.
1029 * Otherwise the function is called here.
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001030 * If "pt" is not null use "pt->pt_outer" for ec_outer_ref->or_outer.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001031 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001032 */
1033 static int
Bram Moolenaar0186e582021-01-10 18:33:11 +01001034call_ufunc(
1035 ufunc_T *ufunc,
1036 partial_T *pt,
1037 int argcount,
1038 ectx_T *ectx,
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001039 isn_T *iptr,
1040 dict_T *selfdict)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001041{
1042 typval_T argvars[MAX_FUNC_ARGS];
1043 funcexe_T funcexe;
1044 int error;
1045 int idx;
Bram Moolenaar28ee8922020-10-28 20:20:00 +01001046 int did_emsg_before = did_emsg;
Bram Moolenaar139575d2022-03-15 19:29:30 +00001047 compiletype_T compile_type = get_compile_type(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001048
Bram Moolenaare99d4222021-06-13 14:01:26 +02001049 if (func_needs_compiling(ufunc, compile_type)
1050 && compile_def_function(ufunc, FALSE, compile_type, NULL)
1051 == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02001052 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001053 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001054 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +01001055 error = check_user_func_argcount(ufunc, argcount);
Bram Moolenaar50824712020-12-20 21:10:17 +01001056 if (error != FCERR_UNKNOWN)
1057 {
1058 if (error == FCERR_TOOMANY)
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001059 semsg(_(e_too_many_arguments_for_function_str),
1060 printable_func_name(ufunc));
Bram Moolenaar50824712020-12-20 21:10:17 +01001061 else
Bram Moolenaare1242042021-12-16 20:56:57 +00001062 semsg(_(e_not_enough_arguments_for_function_str),
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001063 printable_func_name(ufunc));
Bram Moolenaar50824712020-12-20 21:10:17 +01001064 return FAIL;
1065 }
1066
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001067 // The function has been compiled, can call it quickly. For a function
1068 // that was defined later: we can call it directly next time.
1069 if (iptr != NULL)
1070 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001071 delete_instr(iptr);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001072 iptr->isn_type = ISN_DCALL;
1073 iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1074 iptr->isn_arg.dfunc.cdf_argcount = argcount;
1075 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001076 return call_dfunc(ufunc->uf_dfunc_idx, pt, argcount, ectx);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001077 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001078
1079 if (call_prepare(argcount, argvars, ectx) == FAIL)
1080 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +02001081 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00001082 funcexe.fe_evaluate = TRUE;
1083 funcexe.fe_selfdict = selfdict != NULL ? selfdict : dict_stack_get_dict();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001084
1085 // Call the user function. Result goes in last position on the stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001086 error = call_user_func_check(ufunc, argcount, argvars,
Bram Moolenaar851f86b2021-12-13 14:26:44 +00001087 STACK_TV_BOT(-1), &funcexe, funcexe.fe_selfdict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001088
1089 // Clear the arguments.
1090 for (idx = 0; idx < argcount; ++idx)
1091 clear_tv(&argvars[idx]);
1092
1093 if (error != FCERR_NONE)
1094 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001095 user_func_error(error, printable_func_name(ufunc), &funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001096 return FAIL;
1097 }
Bram Moolenaar28ee8922020-10-28 20:20:00 +01001098 if (did_emsg > did_emsg_before)
Bram Moolenaared677f52020-08-12 16:38:10 +02001099 // Error other than from calling the function itself.
1100 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001101 return OK;
1102}
1103
1104/*
Bram Moolenaara91a7132021-03-25 21:12:15 +01001105 * If command modifiers were applied restore them.
1106 */
1107 static void
1108may_restore_cmdmod(funclocal_T *funclocal)
1109{
1110 if (funclocal->floc_restore_cmdmod)
1111 {
1112 cmdmod.cmod_filter_regmatch.regprog = NULL;
1113 undo_cmdmod(&cmdmod);
1114 cmdmod = funclocal->floc_save_cmdmod;
1115 funclocal->floc_restore_cmdmod = FALSE;
1116 }
1117}
1118
1119/*
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001120 * Return TRUE if an error was given (not caught in try/catch) or CTRL-C was
1121 * pressed.
Bram Moolenaara1773442020-08-12 15:21:22 +02001122 */
1123 static int
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001124vim9_aborting(int prev_uncaught_emsg)
Bram Moolenaara1773442020-08-12 15:21:22 +02001125{
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001126 return uncaught_emsg > prev_uncaught_emsg || got_int || did_throw;
Bram Moolenaara1773442020-08-12 15:21:22 +02001127}
1128
1129/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001130 * Execute a function by "name".
1131 * This can be a builtin function or a user function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001132 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001133 * Returns FAIL if not found without an error message.
1134 */
1135 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001136call_by_name(
1137 char_u *name,
1138 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001139 ectx_T *ectx,
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001140 isn_T *iptr,
1141 dict_T *selfdict)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001142{
1143 ufunc_T *ufunc;
1144
1145 if (builtin_function(name, -1))
1146 {
1147 int func_idx = find_internal_func(name);
1148
Bram Moolenaar1983f1a2022-02-28 20:55:02 +00001149 if (func_idx < 0) // Impossible?
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001150 return FAIL;
Bram Moolenaar389df252020-07-09 21:20:47 +02001151 if (check_internal_func(func_idx, argcount) < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001152 return FAIL;
1153 return call_bfunc(func_idx, argcount, ectx);
1154 }
1155
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00001156 ufunc = find_func(name, FALSE);
Bram Moolenaara1773442020-08-12 15:21:22 +02001157
1158 if (ufunc == NULL)
1159 {
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001160 int prev_uncaught_emsg = uncaught_emsg;
Bram Moolenaara1773442020-08-12 15:21:22 +02001161
1162 if (script_autoload(name, TRUE))
1163 // loaded a package, search for the function again
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00001164 ufunc = find_func(name, FALSE);
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001165
1166 if (vim9_aborting(prev_uncaught_emsg))
Bram Moolenaara1773442020-08-12 15:21:22 +02001167 return FAIL; // bail out if loading the script caused an error
1168 }
1169
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001170 if (ufunc != NULL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001171 {
Bram Moolenaar6ce46b92021-08-07 15:35:36 +02001172 if (ufunc->uf_arg_types != NULL || ufunc->uf_va_type != NULL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001173 {
1174 int i;
1175 typval_T *argv = STACK_TV_BOT(0) - argcount;
1176
1177 // The function can change at runtime, check that the argument
1178 // types are correct.
1179 for (i = 0; i < argcount; ++i)
1180 {
Bram Moolenaare3ffcd92021-03-08 21:47:13 +01001181 type_T *type = NULL;
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001182
Bram Moolenaar6ce46b92021-08-07 15:35:36 +02001183 if (i < ufunc->uf_args.ga_len && ufunc->uf_arg_types != NULL)
Bram Moolenaare3ffcd92021-03-08 21:47:13 +01001184 type = ufunc->uf_arg_types[i];
1185 else if (ufunc->uf_va_type != NULL)
1186 type = ufunc->uf_va_type->tt_member;
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001187 if (type != NULL && check_typval_arg_type(type,
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001188 &argv[i], NULL, i + 1) == FAIL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001189 return FAIL;
1190 }
1191 }
1192
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001193 return call_ufunc(ufunc, NULL, argcount, ectx, iptr, selfdict);
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001194 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001195
1196 return FAIL;
1197}
1198
1199 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001200call_partial(
1201 typval_T *tv,
1202 int argcount_arg,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001203 ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001204{
Bram Moolenaara90afb92020-07-15 22:38:56 +02001205 int argcount = argcount_arg;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001206 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001207 int called_emsg_before = called_emsg;
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +01001208 int res = FAIL;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001209 dict_T *selfdict = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001210
1211 if (tv->v_type == VAR_PARTIAL)
1212 {
Bram Moolenaara90afb92020-07-15 22:38:56 +02001213 partial_T *pt = tv->vval.v_partial;
1214 int i;
1215
1216 if (pt->pt_argc > 0)
1217 {
1218 // Make space for arguments from the partial, shift the "argcount"
1219 // arguments up.
Bram Moolenaar35578162021-08-02 19:10:38 +02001220 if (GA_GROW_FAILS(&ectx->ec_stack, pt->pt_argc))
Bram Moolenaara90afb92020-07-15 22:38:56 +02001221 return FAIL;
1222 for (i = 1; i <= argcount; ++i)
1223 *STACK_TV_BOT(-i + pt->pt_argc) = *STACK_TV_BOT(-i);
1224 ectx->ec_stack.ga_len += pt->pt_argc;
1225 argcount += pt->pt_argc;
1226
1227 // copy the arguments from the partial onto the stack
1228 for (i = 0; i < pt->pt_argc; ++i)
1229 copy_tv(&pt->pt_argv[i], STACK_TV_BOT(-argcount + i));
1230 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001231 selfdict = pt->pt_dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001232
1233 if (pt->pt_func != NULL)
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001234 return call_ufunc(pt->pt_func, pt, argcount, ectx, NULL, selfdict);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001235
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001236 name = pt->pt_name;
1237 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001238 else if (tv->v_type == VAR_FUNC)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001239 name = tv->vval.v_string;
Bram Moolenaar95006e32020-08-29 17:47:08 +02001240 if (name != NULL)
1241 {
1242 char_u fname_buf[FLEN_FIXED + 1];
1243 char_u *tofree = NULL;
1244 int error = FCERR_NONE;
1245 char_u *fname;
1246
1247 // May need to translate <SNR>123_ to K_SNR.
1248 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
1249 if (error != FCERR_NONE)
1250 res = FAIL;
1251 else
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001252 res = call_by_name(fname, argcount, ectx, NULL, selfdict);
Bram Moolenaar95006e32020-08-29 17:47:08 +02001253 vim_free(tofree);
1254 }
1255
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +01001256 if (res == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001257 {
1258 if (called_emsg == called_emsg_before)
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001259 emsg_funcname(e_unknown_function_str,
Bram Moolenaar015f4262020-05-05 21:25:22 +02001260 name == NULL ? (char_u *)"[unknown]" : name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001261 return FAIL;
1262 }
1263 return OK;
1264}
1265
1266/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001267 * Check if "lock" is VAR_LOCKED or VAR_FIXED. If so give an error and return
1268 * TRUE.
1269 */
1270 static int
1271error_if_locked(int lock, char *error)
1272{
1273 if (lock & (VAR_LOCKED | VAR_FIXED))
1274 {
1275 emsg(_(error));
1276 return TRUE;
1277 }
1278 return FALSE;
1279}
1280
1281/*
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001282 * Give an error if "tv" is not a number and return FAIL.
1283 */
1284 static int
1285check_for_number(typval_T *tv)
1286{
1287 if (tv->v_type != VAR_NUMBER)
1288 {
1289 semsg(_(e_expected_str_but_got_str),
1290 vartype_name(VAR_NUMBER), vartype_name(tv->v_type));
1291 return FAIL;
1292 }
1293 return OK;
1294}
1295
1296/*
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001297 * Store "tv" in variable "name".
1298 * This is for s: and g: variables.
1299 */
1300 static void
1301store_var(char_u *name, typval_T *tv)
1302{
1303 funccal_entry_T entry;
Bram Moolenaard877a572021-04-01 19:42:48 +02001304 int flags = ASSIGN_DECL;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001305
Bram Moolenaard877a572021-04-01 19:42:48 +02001306 if (tv->v_lock)
1307 flags |= ASSIGN_CONST;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001308 save_funccal(&entry);
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001309 set_var_const(name, 0, NULL, tv, FALSE, flags, 0);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001310 restore_funccal();
1311}
1312
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001313/*
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001314 * Convert "tv" to a string.
1315 * Return FAIL if not allowed.
1316 */
1317 static int
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001318do_2string(typval_T *tv, int is_2string_any, int tolerant)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001319{
1320 if (tv->v_type != VAR_STRING)
1321 {
1322 char_u *str;
1323
1324 if (is_2string_any)
1325 {
1326 switch (tv->v_type)
1327 {
1328 case VAR_SPECIAL:
1329 case VAR_BOOL:
1330 case VAR_NUMBER:
1331 case VAR_FLOAT:
1332 case VAR_BLOB: break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001333
1334 case VAR_LIST:
1335 if (tolerant)
1336 {
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001337 char_u *s, *e, *p;
1338 garray_T ga;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001339
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001340 ga_init2(&ga, sizeof(char_u *), 1);
1341
1342 // Convert to NL separated items, then
1343 // escape the items and replace the NL with
1344 // a space.
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001345 str = typval2string(tv, TRUE);
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001346 if (str == NULL)
1347 return FAIL;
1348 s = str;
1349 while ((e = vim_strchr(s, '\n')) != NULL)
1350 {
1351 *e = NUL;
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +01001352 p = vim_strsave_fnameescape(s,
1353 VSE_NONE);
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001354 if (p != NULL)
1355 {
1356 ga_concat(&ga, p);
1357 ga_concat(&ga, (char_u *)" ");
1358 vim_free(p);
1359 }
1360 s = e + 1;
1361 }
1362 vim_free(str);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001363 clear_tv(tv);
1364 tv->v_type = VAR_STRING;
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001365 tv->vval.v_string = ga.ga_data;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001366 return OK;
1367 }
1368 // FALLTHROUGH
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001369 default: to_string_error(tv->v_type);
1370 return FAIL;
1371 }
1372 }
Bram Moolenaar34453202021-01-31 13:08:38 +01001373 str = typval_tostring(tv, TRUE);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001374 clear_tv(tv);
1375 tv->v_type = VAR_STRING;
1376 tv->vval.v_string = str;
1377 }
1378 return OK;
1379}
1380
1381/*
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001382 * When the value of "sv" is a null list of dict, allocate it.
1383 */
1384 static void
Bram Moolenaar859cc212022-03-28 15:22:35 +01001385allocate_if_null(svar_T *sv)
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001386{
Bram Moolenaar859cc212022-03-28 15:22:35 +01001387 typval_T *tv = sv->sv_tv;
1388
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001389 switch (tv->v_type)
1390 {
1391 case VAR_LIST:
Bram Moolenaar859cc212022-03-28 15:22:35 +01001392 if (tv->vval.v_list == NULL && sv->sv_type != &t_list_empty)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001393 (void)rettv_list_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001394 break;
1395 case VAR_DICT:
Bram Moolenaar859cc212022-03-28 15:22:35 +01001396 if (tv->vval.v_dict == NULL && sv->sv_type != &t_dict_empty)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001397 (void)rettv_dict_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001398 break;
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02001399 case VAR_BLOB:
Bram Moolenaar859cc212022-03-28 15:22:35 +01001400 if (tv->vval.v_blob == NULL && sv->sv_type != &t_blob_null)
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02001401 (void)rettv_blob_alloc(tv);
1402 break;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001403 default:
1404 break;
1405 }
1406}
Bram Moolenaard3aac292020-04-19 14:32:17 +02001407
Bram Moolenaare7525c52021-01-09 13:20:37 +01001408/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001409 * Return the character "str[index]" where "index" is the character index,
1410 * including composing characters.
1411 * If "index" is out of range NULL is returned.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001412 */
1413 char_u *
1414char_from_string(char_u *str, varnumber_T index)
1415{
1416 size_t nbyte = 0;
1417 varnumber_T nchar = index;
1418 size_t slen;
1419
1420 if (str == NULL)
1421 return NULL;
1422 slen = STRLEN(str);
1423
Bram Moolenaarff871402021-03-26 13:34:05 +01001424 // Do the same as for a list: a negative index counts from the end.
1425 // Optimization to check the first byte to be below 0x80 (and no composing
1426 // character follows) makes this a lot faster.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001427 if (index < 0)
1428 {
1429 int clen = 0;
1430
1431 for (nbyte = 0; nbyte < slen; ++clen)
Bram Moolenaarff871402021-03-26 13:34:05 +01001432 {
1433 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1434 ++nbyte;
1435 else if (enc_utf8)
1436 nbyte += utfc_ptr2len(str + nbyte);
1437 else
1438 nbyte += mb_ptr2len(str + nbyte);
1439 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001440 nchar = clen + index;
1441 if (nchar < 0)
1442 // unlike list: index out of range results in empty string
1443 return NULL;
1444 }
1445
1446 for (nbyte = 0; nchar > 0 && nbyte < slen; --nchar)
Bram Moolenaarff871402021-03-26 13:34:05 +01001447 {
1448 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1449 ++nbyte;
1450 else if (enc_utf8)
1451 nbyte += utfc_ptr2len(str + nbyte);
1452 else
1453 nbyte += mb_ptr2len(str + nbyte);
1454 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001455 if (nbyte >= slen)
1456 return NULL;
Bram Moolenaar0289a092021-03-14 18:40:19 +01001457 return vim_strnsave(str + nbyte, mb_ptr2len(str + nbyte));
Bram Moolenaare7525c52021-01-09 13:20:37 +01001458}
1459
1460/*
1461 * Get the byte index for character index "idx" in string "str" with length
Bram Moolenaar0289a092021-03-14 18:40:19 +01001462 * "str_len". Composing characters are included.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001463 * If going over the end return "str_len".
1464 * If "idx" is negative count from the end, -1 is the last character.
1465 * When going over the start return -1.
1466 */
1467 static long
1468char_idx2byte(char_u *str, size_t str_len, varnumber_T idx)
1469{
1470 varnumber_T nchar = idx;
1471 size_t nbyte = 0;
1472
1473 if (nchar >= 0)
1474 {
1475 while (nchar > 0 && nbyte < str_len)
1476 {
Bram Moolenaar0289a092021-03-14 18:40:19 +01001477 nbyte += mb_ptr2len(str + nbyte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001478 --nchar;
1479 }
1480 }
1481 else
1482 {
1483 nbyte = str_len;
1484 while (nchar < 0 && nbyte > 0)
1485 {
1486 --nbyte;
1487 nbyte -= mb_head_off(str, str + nbyte);
1488 ++nchar;
1489 }
1490 if (nchar < 0)
1491 return -1;
1492 }
1493 return (long)nbyte;
1494}
1495
1496/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001497 * Return the slice "str[first : last]" using character indexes. Composing
1498 * characters are included.
Bram Moolenaar6601b622021-01-13 21:47:15 +01001499 * "exclusive" is TRUE for slice().
Bram Moolenaare7525c52021-01-09 13:20:37 +01001500 * Return NULL when the result is empty.
1501 */
1502 char_u *
Bram Moolenaar6601b622021-01-13 21:47:15 +01001503string_slice(char_u *str, varnumber_T first, varnumber_T last, int exclusive)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001504{
1505 long start_byte, end_byte;
1506 size_t slen;
1507
1508 if (str == NULL)
1509 return NULL;
1510 slen = STRLEN(str);
1511 start_byte = char_idx2byte(str, slen, first);
1512 if (start_byte < 0)
1513 start_byte = 0; // first index very negative: use zero
Bram Moolenaar6601b622021-01-13 21:47:15 +01001514 if ((last == -1 && !exclusive) || last == VARNUM_MAX)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001515 end_byte = (long)slen;
1516 else
1517 {
1518 end_byte = char_idx2byte(str, slen, last);
Bram Moolenaar6601b622021-01-13 21:47:15 +01001519 if (!exclusive && end_byte >= 0 && end_byte < (long)slen)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001520 // end index is inclusive
Bram Moolenaar0289a092021-03-14 18:40:19 +01001521 end_byte += mb_ptr2len(str + end_byte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001522 }
1523
1524 if (start_byte >= (long)slen || end_byte <= start_byte)
1525 return NULL;
1526 return vim_strnsave(str + start_byte, end_byte - start_byte);
1527}
1528
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001529/*
1530 * Get a script variable for ISN_STORESCRIPT and ISN_LOADSCRIPT.
1531 * When "dfunc_idx" is negative don't give an error.
1532 * Returns NULL for an error.
1533 */
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001534 static svar_T *
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001535get_script_svar(scriptref_T *sref, int dfunc_idx)
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001536{
1537 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001538 dfunc_T *dfunc = dfunc_idx < 0 ? NULL
1539 : ((dfunc_T *)def_functions.ga_data) + dfunc_idx;
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001540 svar_T *sv;
1541
1542 if (sref->sref_seq != si->sn_script_seq)
1543 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001544 // The script was reloaded after the function was compiled, the
1545 // script_idx may not be valid.
1546 if (dfunc != NULL)
1547 semsg(_(e_script_variable_invalid_after_reload_in_function_str),
1548 printable_func_name(dfunc->df_ufunc));
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001549 return NULL;
1550 }
1551 sv = ((svar_T *)si->sn_var_vals.ga_data) + sref->sref_idx;
Bram Moolenaar60dc8272021-07-29 22:48:54 +02001552 if (!equal_type(sv->sv_type, sref->sref_type, 0))
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001553 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001554 if (dfunc != NULL)
1555 emsg(_(e_script_variable_type_changed));
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001556 return NULL;
1557 }
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001558
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01001559 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0
1560 && sref->sref_sid != current_sctx.sc_sid)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001561 {
1562 if (dfunc != NULL)
1563 semsg(_(e_item_not_exported_in_script_str), sv->sv_name);
1564 return NULL;
1565 }
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001566 return sv;
1567}
1568
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001569/*
Bram Moolenaar20677332021-06-06 17:02:53 +02001570 * Function passed to do_cmdline() for splitting a script joined by NL
1571 * characters.
1572 */
1573 static char_u *
1574get_split_sourceline(
1575 int c UNUSED,
1576 void *cookie,
1577 int indent UNUSED,
1578 getline_opt_T options UNUSED)
1579{
1580 source_cookie_T *sp = (source_cookie_T *)cookie;
1581 char_u *p;
1582 char_u *line;
1583
Bram Moolenaar20677332021-06-06 17:02:53 +02001584 p = vim_strchr(sp->nextline, '\n');
1585 if (p == NULL)
1586 {
1587 line = vim_strsave(sp->nextline);
1588 sp->nextline += STRLEN(sp->nextline);
1589 }
1590 else
1591 {
1592 line = vim_strnsave(sp->nextline, p - sp->nextline);
1593 sp->nextline = p + 1;
1594 }
1595 return line;
1596}
1597
1598/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001599 * Execute a function by "name".
1600 * This can be a builtin function, user function or a funcref.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001601 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001602 */
1603 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001604call_eval_func(
1605 char_u *name,
1606 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001607 ectx_T *ectx,
1608 isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001609{
Bram Moolenaared677f52020-08-12 16:38:10 +02001610 int called_emsg_before = called_emsg;
1611 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001612
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001613 res = call_by_name(name, argcount, ectx, iptr, NULL);
Bram Moolenaared677f52020-08-12 16:38:10 +02001614 if (res == FAIL && called_emsg == called_emsg_before)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001615 {
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001616 dictitem_T *v;
1617
1618 v = find_var(name, NULL, FALSE);
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001619 if (v == NULL || (v->di_tv.v_type != VAR_PARTIAL
1620 && v->di_tv.v_type != VAR_FUNC))
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001621 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001622 emsg_funcname(e_unknown_function_str, name);
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001623 return FAIL;
1624 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001625 return call_partial(&v->di_tv, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001626 }
Bram Moolenaared677f52020-08-12 16:38:10 +02001627 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001628}
1629
1630/*
Bram Moolenaarf112f302020-12-20 17:47:52 +01001631 * When a function reference is used, fill a partial with the information
1632 * needed, especially when it is used as a closure.
1633 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001634 int
Bram Moolenaarf112f302020-12-20 17:47:52 +01001635fill_partial_and_closure(partial_T *pt, ufunc_T *ufunc, ectx_T *ectx)
1636{
1637 pt->pt_func = ufunc;
1638 pt->pt_refcount = 1;
1639
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001640 if (ufunc->uf_flags & FC_CLOSURE)
Bram Moolenaarf112f302020-12-20 17:47:52 +01001641 {
1642 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1643 + ectx->ec_dfunc_idx;
1644
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001645 // The closure may need to find arguments and local variables in the
1646 // current stack.
Bram Moolenaar0186e582021-01-10 18:33:11 +01001647 pt->pt_outer.out_stack = &ectx->ec_stack;
1648 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001649 if (ectx->ec_outer_ref != NULL)
1650 {
1651 // The current context already has a context, link to that one.
1652 pt->pt_outer.out_up = ectx->ec_outer_ref->or_outer;
1653 if (ectx->ec_outer_ref->or_partial != NULL)
1654 {
1655 pt->pt_outer.out_up_partial = ectx->ec_outer_ref->or_partial;
1656 ++pt->pt_outer.out_up_partial->pt_refcount;
1657 }
1658 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01001659
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001660 // If this function returns and the closure is still being used, we
1661 // need to make a copy of the context (arguments and local variables).
1662 // Store a reference to the partial so we can handle that.
Bram Moolenaar35578162021-08-02 19:10:38 +02001663 if (GA_GROW_FAILS(&ectx->ec_funcrefs, 1))
Bram Moolenaarf112f302020-12-20 17:47:52 +01001664 {
1665 vim_free(pt);
1666 return FAIL;
1667 }
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001668 // Extra variable keeps the count of closures created in the current
1669 // function call.
Bram Moolenaarf112f302020-12-20 17:47:52 +01001670 ++(((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx
1671 + STACK_FRAME_SIZE + dfunc->df_varcount)->vval.v_number;
1672
1673 ((partial_T **)ectx->ec_funcrefs.ga_data)
1674 [ectx->ec_funcrefs.ga_len] = pt;
1675 ++pt->pt_refcount;
1676 ++ectx->ec_funcrefs.ga_len;
1677 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001678 ++ufunc->uf_refcount;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001679 return OK;
1680}
1681
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001682/*
1683 * Execute iptr->isn_arg.string as an Ex command.
1684 */
1685 static int
1686exec_command(isn_T *iptr)
1687{
1688 source_cookie_T cookie;
1689
1690 SOURCING_LNUM = iptr->isn_lnum;
1691 // Pass getsourceline to get an error for a missing ":end"
1692 // command.
1693 CLEAR_FIELD(cookie);
1694 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1695 if (do_cmdline(iptr->isn_arg.string,
1696 getsourceline, &cookie,
1697 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED) == FAIL
1698 || did_emsg)
1699 return FAIL;
1700 return OK;
1701}
1702
Bram Moolenaar89445512022-04-14 12:58:23 +01001703/*
1704 * If script "sid" is not loaded yet then load it now.
1705 * Caller must make sure "sid" is a valid script ID.
1706 * "loaded" is set to TRUE if the script had to be loaded.
1707 * Returns FAIL if loading fails, OK if already loaded or loaded now.
1708 */
1709 int
1710may_load_script(int sid, int *loaded)
1711{
1712 scriptitem_T *si = SCRIPT_ITEM(sid);
1713
1714 if (si->sn_state == SN_STATE_NOT_LOADED)
1715 {
1716 *loaded = TRUE;
1717 if (do_source(si->sn_name, FALSE, DOSO_NONE, NULL) == FAIL)
1718 {
1719 semsg(_(e_cant_open_file_str), si->sn_name);
1720 return FAIL;
1721 }
1722 }
1723 return OK;
1724}
1725
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001726// used for v_instr of typval of VAR_INSTR
1727struct instr_S {
1728 ectx_T *instr_ectx;
1729 isn_T *instr_instr;
1730};
1731
Bram Moolenaar4c137212021-04-19 16:48:48 +02001732// used for substitute_instr
1733typedef struct subs_expr_S {
1734 ectx_T *subs_ectx;
1735 isn_T *subs_instr;
1736 int subs_status;
1737} subs_expr_T;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001738
1739// Get pointer to item in the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001740#define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001741
1742// Get pointer to item at the bottom of the stack, -1 is the bottom.
1743#undef STACK_TV_BOT
Bram Moolenaar4c137212021-04-19 16:48:48 +02001744#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 +01001745
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001746// Get pointer to a local variable on the stack. Negative for arguments.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001747#define STACK_TV_VAR(idx) (((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx + STACK_FRAME_SIZE + idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001748
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001749// Set when calling do_debug().
1750static ectx_T *debug_context = NULL;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001751static int debug_var_count;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001752
1753/*
1754 * When debugging lookup "name" and return the typeval.
1755 * When not found return NULL.
1756 */
1757 typval_T *
1758lookup_debug_var(char_u *name)
1759{
1760 int idx;
1761 dfunc_T *dfunc;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001762 ufunc_T *ufunc;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001763 ectx_T *ectx = debug_context;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001764 int varargs_off;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001765
1766 if (ectx == NULL)
1767 return NULL;
1768 dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
1769
1770 // Go through the local variable names, from last to first.
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001771 for (idx = debug_var_count - 1; idx >= 0; --idx)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001772 {
Bram Moolenaare406ff82022-03-10 20:47:43 +00001773 char_u *varname = ((char_u **)dfunc->df_var_names.ga_data)[idx];
1774
1775 // the variable name may be NULL when not available in this block
1776 if (varname != NULL && STRCMP(varname, name) == 0)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001777 return STACK_TV_VAR(idx);
1778 }
1779
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001780 // Go through argument names.
1781 ufunc = dfunc->df_ufunc;
1782 varargs_off = ufunc->uf_va_name == NULL ? 0 : 1;
1783 for (idx = 0; idx < ufunc->uf_args.ga_len; ++idx)
1784 if (STRCMP(((char_u **)(ufunc->uf_args.ga_data))[idx], name) == 0)
1785 return STACK_TV(ectx->ec_frame_idx - ufunc->uf_args.ga_len
1786 - varargs_off + idx);
1787 if (ufunc->uf_va_name != NULL && STRCMP(ufunc->uf_va_name, name) == 0)
1788 return STACK_TV(ectx->ec_frame_idx - 1);
1789
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001790 return NULL;
1791}
1792
Bram Moolenaar26a44842021-09-02 18:49:06 +02001793/*
1794 * Return TRUE if there might be a breakpoint in "ufunc", which is when a
1795 * breakpoint was set in that function or when there is any expression.
1796 */
1797 int
1798may_break_in_function(ufunc_T *ufunc)
1799{
1800 return ufunc->uf_has_breakpoint || debug_has_expr_breakpoint();
1801}
1802
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001803 static void
1804handle_debug(isn_T *iptr, ectx_T *ectx)
1805{
1806 char_u *line;
1807 ufunc_T *ufunc = (((dfunc_T *)def_functions.ga_data)
1808 + ectx->ec_dfunc_idx)->df_ufunc;
1809 isn_T *ni;
1810 int end_lnum = iptr->isn_lnum;
1811 garray_T ga;
1812 int lnum;
1813
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02001814 if (ex_nesting_level > debug_break_level)
1815 {
1816 linenr_T breakpoint;
1817
Bram Moolenaar26a44842021-09-02 18:49:06 +02001818 if (!may_break_in_function(ufunc))
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02001819 return;
1820
1821 // check for the next breakpoint if needed
1822 breakpoint = dbg_find_breakpoint(FALSE, ufunc->uf_name,
Bram Moolenaar8cec9272021-06-23 20:20:53 +02001823 iptr->isn_arg.debug.dbg_break_lnum);
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02001824 if (breakpoint <= 0 || breakpoint > iptr->isn_lnum)
1825 return;
1826 }
1827
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001828 SOURCING_LNUM = iptr->isn_lnum;
1829 debug_context = ectx;
Bram Moolenaar8cec9272021-06-23 20:20:53 +02001830 debug_var_count = iptr->isn_arg.debug.dbg_var_names_len;
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001831
1832 for (ni = iptr + 1; ni->isn_type != ISN_FINISH; ++ni)
1833 if (ni->isn_type == ISN_DEBUG
1834 || ni->isn_type == ISN_RETURN
1835 || ni->isn_type == ISN_RETURN_VOID)
1836 {
Bram Moolenaar112bed02021-11-23 22:16:34 +00001837 end_lnum = ni->isn_lnum + (ni->isn_type == ISN_DEBUG ? 0 : 1);
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001838 break;
1839 }
1840
1841 if (end_lnum > iptr->isn_lnum)
1842 {
1843 ga_init2(&ga, sizeof(char_u *), 10);
Bram Moolenaar310091d2021-12-23 21:14:37 +00001844 for (lnum = iptr->isn_lnum; lnum < end_lnum
1845 && lnum <= ufunc->uf_lines.ga_len; ++lnum)
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001846 {
Bram Moolenaar303215d2021-07-07 20:10:43 +02001847 char_u *p = ((char_u **)ufunc->uf_lines.ga_data)[lnum - 1];
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001848
Bram Moolenaar303215d2021-07-07 20:10:43 +02001849 if (p == NULL)
1850 continue; // left over from continuation line
1851 p = skipwhite(p);
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001852 if (*p == '#')
1853 break;
Bram Moolenaar35578162021-08-02 19:10:38 +02001854 if (GA_GROW_OK(&ga, 1))
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001855 ((char_u **)(ga.ga_data))[ga.ga_len++] = p;
1856 if (STRNCMP(p, "def ", 4) == 0)
1857 break;
1858 }
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001859 line = ga_concat_strings(&ga, " ");
1860 vim_free(ga.ga_data);
1861 }
1862 else
1863 line = ((char_u **)ufunc->uf_lines.ga_data)[iptr->isn_lnum - 1];
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001864
Dominique Pelle6e969552021-06-17 13:53:41 +02001865 do_debug(line == NULL ? (char_u *)"[empty]" : line);
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001866 debug_context = NULL;
1867
1868 if (end_lnum > iptr->isn_lnum)
1869 vim_free(line);
1870}
1871
Bram Moolenaar4c137212021-04-19 16:48:48 +02001872/*
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00001873 * Store a value in a list, dict or blob variable.
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001874 * Returns OK, FAIL or NOTDONE (uncatchable error).
1875 */
1876 static int
1877execute_storeindex(isn_T *iptr, ectx_T *ectx)
1878{
1879 vartype_T dest_type = iptr->isn_arg.vartype;
1880 typval_T *tv;
1881 typval_T *tv_idx = STACK_TV_BOT(-2);
1882 typval_T *tv_dest = STACK_TV_BOT(-1);
1883 int status = OK;
1884
1885 // Stack contains:
1886 // -3 value to be stored
1887 // -2 index
1888 // -1 dict or list
1889 tv = STACK_TV_BOT(-3);
1890 SOURCING_LNUM = iptr->isn_lnum;
1891 if (dest_type == VAR_ANY)
1892 {
1893 dest_type = tv_dest->v_type;
1894 if (dest_type == VAR_DICT)
1895 status = do_2string(tv_idx, TRUE, FALSE);
1896 else if (dest_type == VAR_LIST && tv_idx->v_type != VAR_NUMBER)
1897 {
1898 emsg(_(e_number_expected));
1899 status = FAIL;
1900 }
1901 }
Bram Moolenaare08be092022-02-17 13:08:26 +00001902
1903 if (status == OK)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001904 {
Bram Moolenaare08be092022-02-17 13:08:26 +00001905 if (dest_type == VAR_LIST)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001906 {
Bram Moolenaare08be092022-02-17 13:08:26 +00001907 long lidx = (long)tv_idx->vval.v_number;
1908 list_T *list = tv_dest->vval.v_list;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001909
Bram Moolenaare08be092022-02-17 13:08:26 +00001910 if (list == NULL)
1911 {
1912 emsg(_(e_list_not_set));
1913 return FAIL;
1914 }
1915 if (lidx < 0 && list->lv_len + lidx >= 0)
1916 // negative index is relative to the end
1917 lidx = list->lv_len + lidx;
1918 if (lidx < 0 || lidx > list->lv_len)
1919 {
1920 semsg(_(e_list_index_out_of_range_nr), lidx);
1921 return FAIL;
1922 }
1923 if (lidx < list->lv_len)
1924 {
1925 listitem_T *li = list_find(list, lidx);
1926
1927 if (error_if_locked(li->li_tv.v_lock,
Bram Moolenaar70c43d82022-01-26 21:01:15 +00001928 e_cannot_change_locked_list_item))
Bram Moolenaare08be092022-02-17 13:08:26 +00001929 return FAIL;
1930 // overwrite existing list item
1931 clear_tv(&li->li_tv);
1932 li->li_tv = *tv;
1933 }
1934 else
1935 {
1936 if (error_if_locked(list->lv_lock, e_cannot_change_locked_list))
1937 return FAIL;
1938 // append to list, only fails when out of memory
1939 if (list_append_tv(list, tv) == FAIL)
1940 return NOTDONE;
1941 clear_tv(tv);
1942 }
1943 }
1944 else if (dest_type == VAR_DICT)
1945 {
1946 char_u *key = tv_idx->vval.v_string;
1947 dict_T *dict = tv_dest->vval.v_dict;
1948 dictitem_T *di;
1949
1950 SOURCING_LNUM = iptr->isn_lnum;
1951 if (dict == NULL)
1952 {
1953 emsg(_(e_dictionary_not_set));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001954 return FAIL;
Bram Moolenaare08be092022-02-17 13:08:26 +00001955 }
1956 if (key == NULL)
1957 key = (char_u *)"";
1958 di = dict_find(dict, key, -1);
1959 if (di != NULL)
1960 {
1961 if (error_if_locked(di->di_tv.v_lock,
1962 e_cannot_change_dict_item))
1963 return FAIL;
1964 // overwrite existing value
1965 clear_tv(&di->di_tv);
1966 di->di_tv = *tv;
1967 }
1968 else
1969 {
1970 if (error_if_locked(dict->dv_lock, e_cannot_change_dict))
1971 return FAIL;
1972 // add to dict, only fails when out of memory
1973 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
1974 return NOTDONE;
1975 clear_tv(tv);
1976 }
1977 }
1978 else if (dest_type == VAR_BLOB)
1979 {
1980 long lidx = (long)tv_idx->vval.v_number;
1981 blob_T *blob = tv_dest->vval.v_blob;
1982 varnumber_T nr;
1983 int error = FALSE;
1984 int len;
1985
1986 if (blob == NULL)
1987 {
1988 emsg(_(e_blob_not_set));
1989 return FAIL;
1990 }
1991 len = blob_len(blob);
1992 if (lidx < 0 && len + lidx >= 0)
1993 // negative index is relative to the end
1994 lidx = len + lidx;
1995
1996 // Can add one byte at the end.
1997 if (lidx < 0 || lidx > len)
1998 {
1999 semsg(_(e_blob_index_out_of_range_nr), lidx);
2000 return FAIL;
2001 }
2002 if (value_check_lock(blob->bv_lock, (char_u *)"blob", FALSE))
2003 return FAIL;
2004 nr = tv_get_number_chk(tv, &error);
2005 if (error)
2006 return FAIL;
2007 blob_set_append(blob, lidx, nr);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002008 }
2009 else
2010 {
Bram Moolenaare08be092022-02-17 13:08:26 +00002011 status = FAIL;
2012 semsg(_(e_cannot_index_str), vartype_name(dest_type));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002013 }
2014 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002015
2016 clear_tv(tv_idx);
2017 clear_tv(tv_dest);
2018 ectx->ec_stack.ga_len -= 3;
2019 if (status == FAIL)
2020 {
2021 clear_tv(tv);
2022 return FAIL;
2023 }
2024 return OK;
2025}
2026
2027/*
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002028 * Store a value in a list or blob range.
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002029 */
2030 static int
2031execute_storerange(isn_T *iptr, ectx_T *ectx)
2032{
2033 typval_T *tv;
2034 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2035 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2036 typval_T *tv_dest = STACK_TV_BOT(-1);
2037 int status = OK;
2038
2039 // Stack contains:
2040 // -4 value to be stored
2041 // -3 first index or "none"
2042 // -2 second index or "none"
2043 // -1 destination list or blob
2044 tv = STACK_TV_BOT(-4);
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002045 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002046 if (tv_dest->v_type == VAR_LIST)
2047 {
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002048 long n1;
2049 long n2;
2050 listitem_T *li1;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002051
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002052 n1 = (long)tv_get_number_chk(tv_idx1, NULL);
2053 if (tv_idx2->v_type == VAR_SPECIAL
2054 && tv_idx2->vval.v_number == VVAL_NONE)
2055 n2 = list_len(tv_dest->vval.v_list) - 1;
2056 else
2057 n2 = (long)tv_get_number_chk(tv_idx2, NULL);
2058
Bram Moolenaar22ebd172022-04-01 15:26:58 +01002059 li1 = check_range_index_one(tv_dest->vval.v_list, &n1, TRUE, FALSE);
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002060 if (li1 == NULL)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002061 status = FAIL;
2062 else
2063 {
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002064 status = check_range_index_two(tv_dest->vval.v_list,
2065 &n1, li1, &n2, FALSE);
2066 if (status != FAIL)
2067 status = list_assign_range(
2068 tv_dest->vval.v_list,
2069 tv->vval.v_list,
2070 n1,
2071 n2,
2072 tv_idx2->v_type == VAR_SPECIAL,
2073 (char_u *)"=",
2074 (char_u *)"[unknown]");
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002075 }
2076 }
2077 else if (tv_dest->v_type == VAR_BLOB)
2078 {
2079 varnumber_T n1;
2080 varnumber_T n2;
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002081 long bloblen;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002082
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002083 n1 = tv_get_number_chk(tv_idx1, NULL);
2084 if (tv_idx2->v_type == VAR_SPECIAL
2085 && tv_idx2->vval.v_number == VVAL_NONE)
2086 n2 = blob_len(tv_dest->vval.v_blob) - 1;
2087 else
2088 n2 = tv_get_number_chk(tv_idx2, NULL);
2089 bloblen = blob_len(tv_dest->vval.v_blob);
2090
2091 if (check_blob_index(bloblen, n1, FALSE) == FAIL
2092 || check_blob_range(bloblen, n1, n2, FALSE) == FAIL)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002093 status = FAIL;
2094 else
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002095 status = blob_set_range(tv_dest->vval.v_blob, n1, n2, tv);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002096 }
2097 else
2098 {
2099 status = FAIL;
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002100 emsg(_(e_list_or_blob_required));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002101 }
2102
2103 clear_tv(tv_idx1);
2104 clear_tv(tv_idx2);
2105 clear_tv(tv_dest);
2106 ectx->ec_stack.ga_len -= 4;
2107 clear_tv(tv);
2108
2109 return status;
2110}
2111
2112/*
2113 * Unlet item in list or dict variable.
2114 */
2115 static int
2116execute_unletindex(isn_T *iptr, ectx_T *ectx)
2117{
2118 typval_T *tv_idx = STACK_TV_BOT(-2);
2119 typval_T *tv_dest = STACK_TV_BOT(-1);
2120 int status = OK;
2121
2122 // Stack contains:
2123 // -2 index
2124 // -1 dict or list
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002125 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002126 if (tv_dest->v_type == VAR_DICT)
2127 {
2128 // unlet a dict item, index must be a string
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002129 if (tv_idx->v_type != VAR_STRING && tv_idx->v_type != VAR_NUMBER)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002130 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002131 semsg(_(e_expected_str_but_got_str),
2132 vartype_name(VAR_STRING),
2133 vartype_name(tv_idx->v_type));
2134 status = FAIL;
2135 }
2136 else
2137 {
2138 dict_T *d = tv_dest->vval.v_dict;
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002139 char_u *key;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002140 dictitem_T *di = NULL;
2141
2142 if (d != NULL && value_check_lock(
2143 d->dv_lock, NULL, FALSE))
2144 status = FAIL;
2145 else
2146 {
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002147 if (tv_idx->v_type == VAR_STRING)
2148 {
2149 key = tv_idx->vval.v_string;
2150 if (key == NULL)
2151 key = (char_u *)"";
2152 }
2153 else
2154 {
2155 key = tv_get_string(tv_idx);
2156 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002157 if (d != NULL)
2158 di = dict_find(d, key, (int)STRLEN(key));
2159 if (di == NULL)
2160 {
2161 // NULL dict is equivalent to empty dict
2162 semsg(_(e_key_not_present_in_dictionary),
2163 key);
2164 status = FAIL;
2165 }
2166 else if (var_check_fixed(di->di_flags,
2167 NULL, FALSE)
2168 || var_check_ro(di->di_flags,
2169 NULL, FALSE))
2170 status = FAIL;
2171 else
2172 dictitem_remove(d, di);
2173 }
2174 }
2175 }
2176 else if (tv_dest->v_type == VAR_LIST)
2177 {
2178 // unlet a List item, index must be a number
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002179 if (check_for_number(tv_idx) == FAIL)
2180 {
2181 status = FAIL;
2182 }
2183 else
2184 {
2185 list_T *l = tv_dest->vval.v_list;
2186 long n = (long)tv_idx->vval.v_number;
2187
2188 if (l != NULL && value_check_lock(
2189 l->lv_lock, NULL, FALSE))
2190 status = FAIL;
2191 else
2192 {
2193 listitem_T *li = list_find(l, n);
2194
2195 if (li == NULL)
2196 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002197 semsg(_(e_list_index_out_of_range_nr), n);
2198 status = FAIL;
2199 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002200 else
2201 listitem_remove(l, li);
2202 }
2203 }
2204 }
2205 else
2206 {
2207 status = FAIL;
2208 semsg(_(e_cannot_index_str),
2209 vartype_name(tv_dest->v_type));
2210 }
2211
2212 clear_tv(tv_idx);
2213 clear_tv(tv_dest);
2214 ectx->ec_stack.ga_len -= 2;
2215
2216 return status;
2217}
2218
2219/*
2220 * Unlet a range of items in a list variable.
2221 */
2222 static int
2223execute_unletrange(isn_T *iptr, ectx_T *ectx)
2224{
2225 // Stack contains:
2226 // -3 index1
2227 // -2 index2
2228 // -1 dict or list
2229 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2230 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2231 typval_T *tv_dest = STACK_TV_BOT(-1);
2232 int status = OK;
2233
2234 if (tv_dest->v_type == VAR_LIST)
2235 {
2236 // indexes must be a number
2237 SOURCING_LNUM = iptr->isn_lnum;
2238 if (check_for_number(tv_idx1) == FAIL
2239 || (tv_idx2->v_type != VAR_SPECIAL
2240 && check_for_number(tv_idx2) == FAIL))
2241 {
2242 status = FAIL;
2243 }
2244 else
2245 {
2246 list_T *l = tv_dest->vval.v_list;
2247 long n1 = (long)tv_idx1->vval.v_number;
2248 long n2 = tv_idx2->v_type == VAR_SPECIAL
2249 ? 0 : (long)tv_idx2->vval.v_number;
2250 listitem_T *li;
2251
2252 li = list_find_index(l, &n1);
2253 if (li == NULL)
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002254 {
2255 semsg(_(e_list_index_out_of_range_nr),
2256 (long)tv_idx1->vval.v_number);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002257 status = FAIL;
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002258 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002259 else
2260 {
2261 if (n1 < 0)
2262 n1 = list_idx_of_item(l, li);
2263 if (n2 < 0)
2264 {
2265 listitem_T *li2 = list_find(l, n2);
2266
2267 if (li2 == NULL)
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002268 {
2269 semsg(_(e_list_index_out_of_range_nr), n2);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002270 status = FAIL;
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002271 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002272 else
2273 n2 = list_idx_of_item(l, li2);
2274 }
2275 if (status != FAIL
2276 && tv_idx2->v_type != VAR_SPECIAL
2277 && n2 < n1)
2278 {
2279 semsg(_(e_list_index_out_of_range_nr), n2);
2280 status = FAIL;
2281 }
Bram Moolenaar6b8c7ba2022-03-20 17:46:06 +00002282 if (status != FAIL)
2283 list_unlet_range(l, li, n1,
2284 tv_idx2->v_type != VAR_SPECIAL, n2);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002285 }
2286 }
2287 }
2288 else
2289 {
2290 status = FAIL;
2291 SOURCING_LNUM = iptr->isn_lnum;
2292 semsg(_(e_cannot_index_str),
2293 vartype_name(tv_dest->v_type));
2294 }
2295
2296 clear_tv(tv_idx1);
2297 clear_tv(tv_idx2);
2298 clear_tv(tv_dest);
2299 ectx->ec_stack.ga_len -= 3;
2300
2301 return status;
2302}
2303
2304/*
2305 * Top of a for loop.
2306 */
2307 static int
2308execute_for(isn_T *iptr, ectx_T *ectx)
2309{
2310 typval_T *tv;
2311 typval_T *ltv = STACK_TV_BOT(-1);
2312 typval_T *idxtv =
2313 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
2314
2315 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
2316 return FAIL;
2317 if (ltv->v_type == VAR_LIST)
2318 {
2319 list_T *list = ltv->vval.v_list;
2320
2321 // push the next item from the list
2322 ++idxtv->vval.v_number;
2323 if (list == NULL
2324 || idxtv->vval.v_number >= list->lv_len)
2325 {
2326 // past the end of the list, jump to "endfor"
2327 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
2328 may_restore_cmdmod(&ectx->ec_funclocal);
2329 }
2330 else if (list->lv_first == &range_list_item)
2331 {
2332 // non-materialized range() list
2333 tv = STACK_TV_BOT(0);
2334 tv->v_type = VAR_NUMBER;
2335 tv->v_lock = 0;
2336 tv->vval.v_number = list_find_nr(
2337 list, idxtv->vval.v_number, NULL);
2338 ++ectx->ec_stack.ga_len;
2339 }
2340 else
2341 {
2342 listitem_T *li = list_find(list,
2343 idxtv->vval.v_number);
2344
2345 copy_tv(&li->li_tv, STACK_TV_BOT(0));
2346 ++ectx->ec_stack.ga_len;
2347 }
2348 }
2349 else if (ltv->v_type == VAR_STRING)
2350 {
2351 char_u *str = ltv->vval.v_string;
2352
2353 // The index is for the last byte of the previous
2354 // character.
2355 ++idxtv->vval.v_number;
2356 if (str == NULL || str[idxtv->vval.v_number] == NUL)
2357 {
2358 // past the end of the string, jump to "endfor"
2359 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
2360 may_restore_cmdmod(&ectx->ec_funclocal);
2361 }
2362 else
2363 {
2364 int clen = mb_ptr2len(str + idxtv->vval.v_number);
2365
2366 // Push the next character from the string.
2367 tv = STACK_TV_BOT(0);
2368 tv->v_type = VAR_STRING;
2369 tv->vval.v_string = vim_strnsave(
2370 str + idxtv->vval.v_number, clen);
2371 ++ectx->ec_stack.ga_len;
2372 idxtv->vval.v_number += clen - 1;
2373 }
2374 }
2375 else if (ltv->v_type == VAR_BLOB)
2376 {
2377 blob_T *blob = ltv->vval.v_blob;
2378
2379 // When we get here the first time make a copy of the
2380 // blob, so that the iteration still works when it is
2381 // changed.
2382 if (idxtv->vval.v_number == -1 && blob != NULL)
2383 {
2384 blob_copy(blob, ltv);
2385 blob_unref(blob);
2386 blob = ltv->vval.v_blob;
2387 }
2388
2389 // The index is for the previous byte.
2390 ++idxtv->vval.v_number;
2391 if (blob == NULL
2392 || idxtv->vval.v_number >= blob_len(blob))
2393 {
2394 // past the end of the blob, jump to "endfor"
2395 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
2396 may_restore_cmdmod(&ectx->ec_funclocal);
2397 }
2398 else
2399 {
2400 // Push the next byte from the blob.
2401 tv = STACK_TV_BOT(0);
2402 tv->v_type = VAR_NUMBER;
2403 tv->vval.v_number = blob_get(blob,
2404 idxtv->vval.v_number);
2405 ++ectx->ec_stack.ga_len;
2406 }
2407 }
2408 else
2409 {
2410 semsg(_(e_for_loop_on_str_not_supported),
2411 vartype_name(ltv->v_type));
2412 return FAIL;
2413 }
2414 return OK;
2415}
2416
2417/*
Bram Moolenaar06b77222022-01-25 15:51:56 +00002418 * Load instruction for w:/b:/g:/t: variable.
2419 * "isn_type" is used instead of "iptr->isn_type".
2420 */
2421 static int
2422load_namespace_var(ectx_T *ectx, isntype_T isn_type, isn_T *iptr)
2423{
2424 dictitem_T *di = NULL;
2425 hashtab_T *ht = NULL;
2426 char namespace;
2427
2428 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
2429 return NOTDONE;
2430 switch (isn_type)
2431 {
2432 case ISN_LOADG:
2433 ht = get_globvar_ht();
2434 namespace = 'g';
2435 break;
2436 case ISN_LOADB:
2437 ht = &curbuf->b_vars->dv_hashtab;
2438 namespace = 'b';
2439 break;
2440 case ISN_LOADW:
2441 ht = &curwin->w_vars->dv_hashtab;
2442 namespace = 'w';
2443 break;
2444 case ISN_LOADT:
2445 ht = &curtab->tp_vars->dv_hashtab;
2446 namespace = 't';
2447 break;
2448 default: // Cannot reach here
2449 return NOTDONE;
2450 }
2451 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
2452
Bram Moolenaar06b77222022-01-25 15:51:56 +00002453 if (di == NULL)
2454 {
Bram Moolenaarfe732552022-02-22 19:39:13 +00002455 if (isn_type == ISN_LOADG)
2456 {
2457 ufunc_T *ufunc = find_func(iptr->isn_arg.string, TRUE);
2458
2459 // g:Something could be a function
2460 if (ufunc != NULL)
2461 {
2462 typval_T *tv = STACK_TV_BOT(0);
2463
2464 ++ectx->ec_stack.ga_len;
2465 tv->v_type = VAR_FUNC;
2466 tv->vval.v_string = alloc(STRLEN(iptr->isn_arg.string) + 3);
2467 if (tv->vval.v_string == NULL)
2468 return FAIL;
2469 STRCPY(tv->vval.v_string, "g:");
2470 STRCPY(tv->vval.v_string + 2, iptr->isn_arg.string);
2471 return OK;
2472 }
2473 }
Bram Moolenaar06b77222022-01-25 15:51:56 +00002474 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarfe732552022-02-22 19:39:13 +00002475 if (vim_strchr(iptr->isn_arg.string, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar06b77222022-01-25 15:51:56 +00002476 // no check if the item exists in the script but
2477 // isn't exported, it is too complicated
Bram Moolenaarfe732552022-02-22 19:39:13 +00002478 semsg(_(e_item_not_found_in_script_str), iptr->isn_arg.string);
Bram Moolenaar06b77222022-01-25 15:51:56 +00002479 else
2480 semsg(_(e_undefined_variable_char_str),
Bram Moolenaarfe732552022-02-22 19:39:13 +00002481 namespace, iptr->isn_arg.string);
Bram Moolenaar06b77222022-01-25 15:51:56 +00002482 return FAIL;
2483 }
2484 else
2485 {
2486 copy_tv(&di->di_tv, STACK_TV_BOT(0));
2487 ++ectx->ec_stack.ga_len;
2488 }
2489 return OK;
2490}
2491
2492/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02002493 * Execute instructions in execution context "ectx".
2494 * Return OK or FAIL;
2495 */
2496 static int
2497exec_instructions(ectx_T *ectx)
2498{
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002499 int ret = FAIL;
2500 int save_trylevel_at_start = ectx->ec_trylevel_at_start;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02002501 int dict_stack_len_at_start = dict_stack.ga_len;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01002502
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02002503 // Start execution at the first instruction.
Bram Moolenaar4c137212021-04-19 16:48:48 +02002504 ectx->ec_iidx = 0;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01002505
Bram Moolenaarff652882021-05-16 15:24:49 +02002506 // Only catch exceptions in this instruction list.
2507 ectx->ec_trylevel_at_start = trylevel;
2508
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002509 for (;;)
2510 {
Bram Moolenaara7490192021-07-22 12:26:14 +02002511 static int breakcheck_count = 0; // using "static" makes it faster
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002512 isn_T *iptr;
Bram Moolenaara7490192021-07-22 12:26:14 +02002513 typval_T *tv;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002514
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002515 if (unlikely(++breakcheck_count >= 100))
Bram Moolenaar270d0382020-05-15 21:42:53 +02002516 {
2517 line_breakcheck();
2518 breakcheck_count = 0;
2519 }
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002520 if (unlikely(got_int))
Bram Moolenaar20431c92020-03-20 18:39:46 +01002521 {
2522 // Turn CTRL-C into an exception.
2523 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +01002524 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002525 goto theend;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002526 did_throw = TRUE;
2527 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002528
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002529 if (unlikely(did_emsg && msg_list != NULL && *msg_list != NULL))
Bram Moolenaara26b9702020-04-18 19:53:28 +02002530 {
2531 // Turn an error message into an exception.
2532 did_emsg = FALSE;
2533 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002534 goto theend;
Bram Moolenaara26b9702020-04-18 19:53:28 +02002535 did_throw = TRUE;
2536 *msg_list = NULL;
2537 }
2538
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002539 if (unlikely(did_throw))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002540 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002541 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002542 trycmd_T *trycmd = NULL;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002543 int index = trystack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002544
2545 // An exception jumps to the first catch, finally, or returns from
2546 // the current function.
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002547 while (index > 0)
2548 {
2549 trycmd = ((trycmd_T *)trystack->ga_data) + index - 1;
Bram Moolenaar834193a2021-06-30 20:39:15 +02002550 if (!trycmd->tcd_in_catch || trycmd->tcd_finally_idx != 0)
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002551 break;
2552 // In the catch and finally block of this try we have to go up
2553 // one level.
2554 --index;
2555 trycmd = NULL;
2556 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02002557 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002558 {
Bram Moolenaar834193a2021-06-30 20:39:15 +02002559 if (trycmd->tcd_in_catch)
2560 {
2561 // exception inside ":catch", jump to ":finally" once
2562 ectx->ec_iidx = trycmd->tcd_finally_idx;
2563 trycmd->tcd_finally_idx = 0;
2564 }
2565 else
2566 // jump to first ":catch"
2567 ectx->ec_iidx = trycmd->tcd_catch_idx;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002568 trycmd->tcd_in_catch = TRUE;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002569 did_throw = FALSE; // don't come back here until :endtry
2570 trycmd->tcd_did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002571 }
2572 else
2573 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02002574 // Not inside try or need to return from current functions.
2575 // Push a dummy return value.
Bram Moolenaar35578162021-08-02 19:10:38 +02002576 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002577 goto theend;
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02002578 tv = STACK_TV_BOT(0);
2579 tv->v_type = VAR_NUMBER;
2580 tv->vval.v_number = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002581 ++ectx->ec_stack.ga_len;
2582 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002583 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02002584 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +01002585 need_rethrow = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002586 if (handle_closure_in_use(ectx, FALSE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002587 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002588 goto done;
2589 }
2590
Bram Moolenaar4c137212021-04-19 16:48:48 +02002591 if (func_return(ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002592 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002593 }
2594 continue;
2595 }
2596
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002597 /*
2598 * Big switch on the instruction. Most compilers will be turning this
2599 * into an efficient lookup table, since the "case" values are an enum
2600 * with sequential numbers. It may look ugly, but it should be the
2601 * most efficient way.
2602 */
Bram Moolenaar4c137212021-04-19 16:48:48 +02002603 iptr = &ectx->ec_instr[ectx->ec_iidx++];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002604 switch (iptr->isn_type)
2605 {
2606 // execute Ex command line
2607 case ISN_EXEC:
Bram Moolenaaraacc9662021-08-13 19:40:51 +02002608 if (exec_command(iptr) == FAIL)
2609 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002610 break;
2611
Bram Moolenaar20677332021-06-06 17:02:53 +02002612 // execute Ex command line split at NL characters.
2613 case ISN_EXEC_SPLIT:
2614 {
2615 source_cookie_T cookie;
Bram Moolenaar518df272021-06-06 17:34:13 +02002616 char_u *line;
Bram Moolenaar20677332021-06-06 17:02:53 +02002617
2618 SOURCING_LNUM = iptr->isn_lnum;
2619 CLEAR_FIELD(cookie);
2620 cookie.sourcing_lnum = iptr->isn_lnum - 1;
2621 cookie.nextline = iptr->isn_arg.string;
Bram Moolenaar518df272021-06-06 17:34:13 +02002622 line = get_split_sourceline(0, &cookie, 0, 0);
2623 if (do_cmdline(line,
Bram Moolenaar20677332021-06-06 17:02:53 +02002624 get_split_sourceline, &cookie,
2625 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED)
2626 == FAIL
2627 || did_emsg)
Bram Moolenaar518df272021-06-06 17:34:13 +02002628 {
2629 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02002630 goto on_error;
Bram Moolenaar518df272021-06-06 17:34:13 +02002631 }
2632 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02002633 }
2634 break;
2635
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002636 // execute Ex command line that is only a range
2637 case ISN_EXECRANGE:
2638 {
2639 exarg_T ea;
2640 char *error = NULL;
2641
2642 CLEAR_FIELD(ea);
2643 ea.cmdidx = CMD_SIZE;
2644 ea.addr_type = ADDR_LINES;
2645 ea.cmd = iptr->isn_arg.string;
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00002646 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002647 parse_cmd_address(&ea, &error, FALSE);
Bram Moolenaar01a4dcb2021-12-04 13:15:10 +00002648 if (ea.cmd == NULL)
2649 goto on_error;
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00002650 // error is always NULL when using ADDR_LINES
2651 error = ex_range_without_command(&ea);
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002652 if (error != NULL)
2653 {
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002654 emsg(error);
2655 goto on_error;
2656 }
2657 }
2658 break;
2659
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02002660 // Evaluate an expression with legacy syntax, push it onto the
2661 // stack.
2662 case ISN_LEGACY_EVAL:
2663 {
2664 char_u *arg = iptr->isn_arg.string;
2665 int res;
2666 int save_flags = cmdmod.cmod_flags;
2667
Bram Moolenaar35578162021-08-02 19:10:38 +02002668 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002669 goto theend;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02002670 tv = STACK_TV_BOT(0);
2671 init_tv(tv);
2672 cmdmod.cmod_flags |= CMOD_LEGACY;
2673 res = eval0(arg, tv, NULL, &EVALARG_EVALUATE);
2674 cmdmod.cmod_flags = save_flags;
2675 if (res == FAIL)
2676 goto on_error;
2677 ++ectx->ec_stack.ga_len;
2678 }
2679 break;
2680
Bram Moolenaarf18332f2021-05-07 17:55:55 +02002681 // push typeval VAR_INSTR with instructions to be executed
2682 case ISN_INSTR:
2683 {
Bram Moolenaar35578162021-08-02 19:10:38 +02002684 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002685 goto theend;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02002686 tv = STACK_TV_BOT(0);
2687 tv->vval.v_instr = ALLOC_ONE(instr_T);
2688 if (tv->vval.v_instr == NULL)
2689 goto on_error;
2690 ++ectx->ec_stack.ga_len;
2691
2692 tv->v_type = VAR_INSTR;
2693 tv->vval.v_instr->instr_ectx = ectx;
2694 tv->vval.v_instr->instr_instr = iptr->isn_arg.instr;
2695 }
2696 break;
2697
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002698 case ISN_SOURCE:
2699 {
Bram Moolenaar89445512022-04-14 12:58:23 +01002700 int notused;
LemonBoy77142312022-04-15 20:50:46 +01002701
Bram Moolenaar89445512022-04-14 12:58:23 +01002702 SOURCING_LNUM = iptr->isn_lnum;
2703 if (may_load_script((int)iptr->isn_arg.number, &notused)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002704 == FAIL)
Bram Moolenaar89445512022-04-14 12:58:23 +01002705 goto on_error;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002706 }
2707 break;
2708
Bram Moolenaar4c137212021-04-19 16:48:48 +02002709 // execute :substitute with an expression
2710 case ISN_SUBSTITUTE:
2711 {
2712 subs_T *subs = &iptr->isn_arg.subs;
2713 source_cookie_T cookie;
2714 struct subs_expr_S *save_instr = substitute_instr;
2715 struct subs_expr_S subs_instr;
2716 int res;
2717
2718 subs_instr.subs_ectx = ectx;
2719 subs_instr.subs_instr = subs->subs_instr;
2720 subs_instr.subs_status = OK;
2721 substitute_instr = &subs_instr;
2722
2723 SOURCING_LNUM = iptr->isn_lnum;
2724 // This is very much like ISN_EXEC
2725 CLEAR_FIELD(cookie);
2726 cookie.sourcing_lnum = iptr->isn_lnum - 1;
2727 res = do_cmdline(subs->subs_cmd,
2728 getsourceline, &cookie,
2729 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
2730 substitute_instr = save_instr;
2731
2732 if (res == FAIL || did_emsg
2733 || subs_instr.subs_status == FAIL)
2734 goto on_error;
2735 }
2736 break;
2737
2738 case ISN_FINISH:
2739 goto done;
2740
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02002741 case ISN_REDIRSTART:
2742 // create a dummy entry for var_redir_str()
2743 if (alloc_redir_lval() == FAIL)
2744 goto on_error;
2745
2746 // The output is stored in growarray "redir_ga" until
2747 // redirection ends.
2748 init_redir_ga();
2749 redir_vname = 1;
2750 break;
2751
2752 case ISN_REDIREND:
2753 {
2754 char_u *res = get_clear_redir_ga();
2755
2756 // End redirection, put redirected text on the stack.
2757 clear_redir_lval();
2758 redir_vname = 0;
2759
Bram Moolenaar35578162021-08-02 19:10:38 +02002760 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02002761 {
2762 vim_free(res);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002763 goto theend;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02002764 }
2765 tv = STACK_TV_BOT(0);
2766 tv->v_type = VAR_STRING;
2767 tv->vval.v_string = res;
2768 ++ectx->ec_stack.ga_len;
2769 }
2770 break;
2771
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002772 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02002773#ifdef FEAT_QUICKFIX
Bram Moolenaar397a87a2022-03-20 21:14:15 +00002774 force_abort = TRUE;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002775 if (trigger_cexpr_autocmd(iptr->isn_arg.number) == FAIL)
2776 goto on_error;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00002777 force_abort = FALSE;
Bram Moolenaarb7c97812021-05-05 22:51:39 +02002778#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002779 break;
2780
2781 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02002782#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002783 {
2784 exarg_T ea;
2785 int res;
2786
2787 CLEAR_FIELD(ea);
2788 ea.cmdidx = iptr->isn_arg.cexpr.cexpr_ref->cer_cmdidx;
2789 ea.forceit = iptr->isn_arg.cexpr.cexpr_ref->cer_forceit;
2790 ea.cmdlinep = &iptr->isn_arg.cexpr.cexpr_ref->cer_cmdline;
2791 --ectx->ec_stack.ga_len;
2792 tv = STACK_TV_BOT(0);
2793 res = cexpr_core(&ea, tv);
2794 clear_tv(tv);
2795 if (res == FAIL)
2796 goto on_error;
2797 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02002798#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002799 break;
2800
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002801 // execute Ex command from pieces on the stack
2802 case ISN_EXECCONCAT:
2803 {
2804 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +02002805 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002806 int pass;
2807 int i;
2808 char_u *cmd = NULL;
2809 char_u *str;
2810
2811 for (pass = 1; pass <= 2; ++pass)
2812 {
2813 for (i = 0; i < count; ++i)
2814 {
2815 tv = STACK_TV_BOT(i - count);
2816 str = tv->vval.v_string;
2817 if (str != NULL && *str != NUL)
2818 {
2819 if (pass == 2)
2820 STRCPY(cmd + len, str);
2821 len += STRLEN(str);
2822 }
2823 if (pass == 2)
2824 clear_tv(tv);
2825 }
2826 if (pass == 1)
2827 {
2828 cmd = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002829 if (unlikely(cmd == NULL))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002830 goto theend;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002831 len = 0;
2832 }
2833 }
2834
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02002835 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002836 do_cmdline_cmd(cmd);
2837 vim_free(cmd);
2838 }
2839 break;
2840
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002841 // execute :echo {string} ...
2842 case ISN_ECHO:
2843 {
2844 int count = iptr->isn_arg.echo.echo_count;
2845 int atstart = TRUE;
2846 int needclr = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002847 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002848
2849 for (idx = 0; idx < count; ++idx)
2850 {
2851 tv = STACK_TV_BOT(idx - count);
2852 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
2853 &atstart, &needclr);
2854 clear_tv(tv);
2855 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01002856 if (needclr)
2857 msg_clr_eos();
Bram Moolenaar4c137212021-04-19 16:48:48 +02002858 ectx->ec_stack.ga_len -= count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002859 }
2860 break;
2861
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002862 // :execute {string} ...
2863 // :echomsg {string} ...
Bram Moolenaar7de62622021-08-07 15:05:47 +02002864 // :echoconsole {string} ...
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002865 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01002866 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002867 case ISN_ECHOMSG:
Bram Moolenaar7de62622021-08-07 15:05:47 +02002868 case ISN_ECHOCONSOLE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002869 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01002870 {
2871 int count = iptr->isn_arg.number;
2872 garray_T ga;
2873 char_u buf[NUMBUFLEN];
2874 char_u *p;
2875 int len;
2876 int failed = FALSE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002877 int idx;
Bram Moolenaarad39c092020-02-26 18:23:43 +01002878
2879 ga_init2(&ga, 1, 80);
2880 for (idx = 0; idx < count; ++idx)
2881 {
2882 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002883 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01002884 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002885 if (tv->v_type == VAR_CHANNEL
2886 || tv->v_type == VAR_JOB)
2887 {
2888 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar68db9962021-05-09 23:19:22 +02002889 semsg(_(e_using_invalid_value_as_string_str),
2890 vartype_name(tv->v_type));
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002891 break;
2892 }
2893 else
2894 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01002895 }
2896 else
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002897 p = tv_stringify(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01002898
2899 len = (int)STRLEN(p);
Bram Moolenaar35578162021-08-02 19:10:38 +02002900 if (GA_GROW_FAILS(&ga, len + 2))
Bram Moolenaarad39c092020-02-26 18:23:43 +01002901 failed = TRUE;
2902 else
2903 {
2904 if (ga.ga_len > 0)
2905 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
2906 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
2907 ga.ga_len += len;
2908 }
2909 clear_tv(tv);
2910 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02002911 ectx->ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002912 if (failed)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01002913 {
2914 ga_clear(&ga);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002915 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01002916 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01002917
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002918 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002919 {
2920 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02002921 {
2922 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002923 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01002924 if (did_emsg)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01002925 {
2926 ga_clear(&ga);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01002927 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01002928 }
Bram Moolenaar430deb12020-08-23 16:29:11 +02002929 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002930 else
2931 {
2932 msg_sb_eol();
2933 if (iptr->isn_type == ISN_ECHOMSG)
2934 {
2935 msg_attr(ga.ga_data, echo_attr);
2936 out_flush();
2937 }
Bram Moolenaar7de62622021-08-07 15:05:47 +02002938 else if (iptr->isn_type == ISN_ECHOCONSOLE)
2939 {
2940 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data),
2941 TRUE);
2942 ui_write((char_u *)"\r\n", 2, TRUE);
2943 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002944 else
2945 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002946 SOURCING_LNUM = iptr->isn_lnum;
2947 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002948 }
2949 }
2950 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01002951 ga_clear(&ga);
2952 }
2953 break;
2954
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002955 // load local variable or argument
2956 case ISN_LOAD:
Bram Moolenaar35578162021-08-02 19:10:38 +02002957 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002958 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002959 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002960 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002961 break;
2962
2963 // load v: variable
2964 case ISN_LOADV:
Bram Moolenaar35578162021-08-02 19:10:38 +02002965 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002966 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002967 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002968 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002969 break;
2970
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002971 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002972 case ISN_LOADSCRIPT:
2973 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01002974 scriptref_T *sref = iptr->isn_arg.script.scriptref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002975 svar_T *sv;
2976
Bram Moolenaar6db660b2021-08-01 14:08:54 +02002977 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01002978 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002979 goto theend;
Bram Moolenaar859cc212022-03-28 15:22:35 +01002980 allocate_if_null(sv);
Bram Moolenaar35578162021-08-02 19:10:38 +02002981 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002982 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002983 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002984 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002985 }
2986 break;
2987
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002988 // load s: variable in old script or autoload import
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002989 case ISN_LOADS:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002990 case ISN_LOADEXPORT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002991 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002992 int sid = iptr->isn_arg.loadstore.ls_sid;
2993 hashtab_T *ht = &SCRIPT_VARS(sid);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002994 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002995 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002996
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002997 if (di == NULL)
2998 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002999 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003000 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003001 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003002 }
3003 else
3004 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003005 if (iptr->isn_type == ISN_LOADEXPORT)
3006 {
3007 int idx = get_script_item_idx(sid, name, 0,
3008 NULL, NULL);
3009 svar_T *sv;
3010
3011 if (idx >= 0)
3012 {
3013 sv = ((svar_T *)SCRIPT_ITEM(sid)
3014 ->sn_var_vals.ga_data) + idx;
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003015 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003016 {
3017 SOURCING_LNUM = iptr->isn_lnum;
3018 semsg(_(e_item_not_exported_in_script_str),
3019 name);
3020 goto on_error;
3021 }
3022 }
3023 }
Bram Moolenaar35578162021-08-02 19:10:38 +02003024 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003025 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003026 copy_tv(&di->di_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003027 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003028 }
3029 }
3030 break;
3031
Bram Moolenaard3aac292020-04-19 14:32:17 +02003032 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003033 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02003034 case ISN_LOADB:
3035 case ISN_LOADW:
3036 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003037 {
Bram Moolenaar06b77222022-01-25 15:51:56 +00003038 int res = load_namespace_var(ectx, iptr->isn_type, iptr);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003039
Bram Moolenaar06b77222022-01-25 15:51:56 +00003040 if (res == NOTDONE)
Bram Moolenaarcbbc48f2022-01-18 12:58:28 +00003041 goto theend;
Bram Moolenaar06b77222022-01-25 15:51:56 +00003042 if (res == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003043 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003044 }
Bram Moolenaar06b77222022-01-25 15:51:56 +00003045
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003046 break;
3047
Bram Moolenaar03290b82020-12-19 16:30:44 +01003048 // load autoload variable
3049 case ISN_LOADAUTO:
3050 {
3051 char_u *name = iptr->isn_arg.string;
3052
Bram Moolenaar35578162021-08-02 19:10:38 +02003053 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003054 goto theend;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003055 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003056 if (eval_variable(name, (int)STRLEN(name), 0,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003057 STACK_TV_BOT(0), NULL, EVAL_VAR_VERBOSE) == FAIL)
Bram Moolenaar03290b82020-12-19 16:30:44 +01003058 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003059 ++ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003060 }
3061 break;
3062
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003063 // load g:/b:/w:/t: namespace
3064 case ISN_LOADGDICT:
3065 case ISN_LOADBDICT:
3066 case ISN_LOADWDICT:
3067 case ISN_LOADTDICT:
3068 {
3069 dict_T *d = NULL;
3070
3071 switch (iptr->isn_type)
3072 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02003073 case ISN_LOADGDICT: d = get_globvar_dict(); break;
3074 case ISN_LOADBDICT: d = curbuf->b_vars; break;
3075 case ISN_LOADWDICT: d = curwin->w_vars; break;
3076 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003077 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003078 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003079 }
Bram Moolenaar35578162021-08-02 19:10:38 +02003080 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003081 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003082 tv = STACK_TV_BOT(0);
3083 tv->v_type = VAR_DICT;
3084 tv->v_lock = 0;
3085 tv->vval.v_dict = d;
Bram Moolenaar1bd3cb22021-02-24 12:27:31 +01003086 ++d->dv_refcount;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003087 ++ectx->ec_stack.ga_len;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003088 }
3089 break;
3090
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003091 // load &option
3092 case ISN_LOADOPT:
3093 {
3094 typval_T optval;
3095 char_u *name = iptr->isn_arg.string;
3096
Bram Moolenaara8c17702020-04-01 21:17:24 +02003097 // This is not expected to fail, name is checked during
3098 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar35578162021-08-02 19:10:38 +02003099 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003100 goto theend;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003101 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003102 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003103 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003104 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003105 }
3106 break;
3107
3108 // load $ENV
3109 case ISN_LOADENV:
3110 {
3111 typval_T optval;
3112 char_u *name = iptr->isn_arg.string;
3113
Bram Moolenaar35578162021-08-02 19:10:38 +02003114 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003115 goto theend;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003116 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003117 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003118 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003119 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003120 }
3121 break;
3122
3123 // load @register
3124 case ISN_LOADREG:
Bram Moolenaar35578162021-08-02 19:10:38 +02003125 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003126 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003127 tv = STACK_TV_BOT(0);
3128 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003129 tv->v_lock = 0;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02003130 // This may result in NULL, which should be equivalent to an
3131 // empty string.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003132 tv->vval.v_string = get_reg_contents(
3133 iptr->isn_arg.number, GREG_EXPR_SRC);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003134 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003135 break;
3136
3137 // store local variable
3138 case ISN_STORE:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003139 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003140 tv = STACK_TV_VAR(iptr->isn_arg.number);
3141 clear_tv(tv);
3142 *tv = *STACK_TV_BOT(0);
3143 break;
3144
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003145 // store s: variable in old script or autoload import
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003146 case ISN_STORES:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003147 case ISN_STOREEXPORT:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003148 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003149 int sid = iptr->isn_arg.loadstore.ls_sid;
3150 hashtab_T *ht = &SCRIPT_VARS(sid);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003151 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003152 dictitem_T *di = find_var_in_ht(ht, 0,
3153 iptr->isn_type == ISN_STORES
3154 ? name + 2 : name, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003155
Bram Moolenaar4c137212021-04-19 16:48:48 +02003156 --ectx->ec_stack.ga_len;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003157 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003158 if (di == NULL)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003159 {
3160 if (iptr->isn_type == ISN_STOREEXPORT)
3161 {
3162 semsg(_(e_undefined_variable_str), name);
Bram Moolenaard1d26842022-03-31 10:13:47 +01003163 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003164 goto on_error;
3165 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003166 store_var(name, STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003167 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003168 else
3169 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003170 if (iptr->isn_type == ISN_STOREEXPORT)
3171 {
3172 int idx = get_script_item_idx(sid, name, 0,
3173 NULL, NULL);
3174
Bram Moolenaar06651632022-04-27 17:54:25 +01003175 // can this ever fail?
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003176 if (idx >= 0)
3177 {
3178 svar_T *sv = ((svar_T *)SCRIPT_ITEM(sid)
3179 ->sn_var_vals.ga_data) + idx;
3180
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003181 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003182 {
3183 semsg(_(e_item_not_exported_in_script_str),
3184 name);
Bram Moolenaard1d26842022-03-31 10:13:47 +01003185 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003186 goto on_error;
3187 }
3188 }
3189 }
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02003190 if (var_check_permission(di, name) == FAIL)
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003191 {
3192 clear_tv(STACK_TV_BOT(0));
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02003193 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003194 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003195 clear_tv(&di->di_tv);
3196 di->di_tv = *STACK_TV_BOT(0);
3197 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003198 }
3199 break;
3200
3201 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003202 case ISN_STORESCRIPT:
3203 {
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003204 scriptref_T *sref = iptr->isn_arg.script.scriptref;
3205 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003206
Bram Moolenaar6db660b2021-08-01 14:08:54 +02003207 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003208 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003209 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003210 --ectx->ec_stack.ga_len;
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003211
3212 // "const" and "final" are checked at compile time, locking
3213 // the value needs to be checked here.
3214 SOURCING_LNUM = iptr->isn_lnum;
3215 if (value_check_lock(sv->sv_tv->v_lock, sv->sv_name, FALSE))
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003216 {
3217 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003218 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003219 }
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003220
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003221 clear_tv(sv->sv_tv);
3222 *sv->sv_tv = *STACK_TV_BOT(0);
3223 }
3224 break;
3225
3226 // store option
3227 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003228 case ISN_STOREFUNCOPT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003229 {
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003230 char_u *opt_name = iptr->isn_arg.storeopt.so_name;
3231 int opt_flags = iptr->isn_arg.storeopt.so_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003232 long n = 0;
3233 char_u *s = NULL;
3234 char *msg;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003235 char_u numbuf[NUMBUFLEN];
3236 char_u *tofree = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003237
Bram Moolenaar4c137212021-04-19 16:48:48 +02003238 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003239 tv = STACK_TV_BOT(0);
3240 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01003241 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003242 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01003243 if (s == NULL)
3244 s = (char_u *)"";
3245 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003246 else if (iptr->isn_type == ISN_STOREFUNCOPT)
3247 {
3248 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003249 // If the option can be set to a function reference or
3250 // a lambda and the passed value is a function
3251 // reference, then convert it to the name (string) of
3252 // the function reference.
3253 s = tv2string(tv, &tofree, numbuf, 0);
3254 if (s == NULL || *s == NUL)
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003255 {
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003256 // cannot happen?
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003257 clear_tv(tv);
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003258 vim_free(tofree);
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003259 goto on_error;
3260 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003261 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003262 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003263 // must be VAR_NUMBER, CHECKTYPE makes sure
3264 n = tv->vval.v_number;
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003265 msg = set_option_value(opt_name, n, s, opt_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02003266 clear_tv(tv);
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003267 vim_free(tofree);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003268 if (msg != NULL)
3269 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003270 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003271 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02003272 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003273 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003274 }
3275 break;
3276
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003277 // store $ENV
3278 case ISN_STOREENV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003279 --ectx->ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003280 tv = STACK_TV_BOT(0);
3281 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
3282 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003283 break;
3284
3285 // store @r
3286 case ISN_STOREREG:
3287 {
3288 int reg = iptr->isn_arg.number;
3289
Bram Moolenaar4c137212021-04-19 16:48:48 +02003290 --ectx->ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01003291 tv = STACK_TV_BOT(0);
Bram Moolenaar74f4a962021-06-17 21:03:07 +02003292 write_reg_contents(reg, tv_get_string(tv), -1, FALSE);
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01003293 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003294 }
3295 break;
3296
3297 // store v: variable
3298 case ISN_STOREV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003299 --ectx->ec_stack.ga_len;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003300 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
3301 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003302 // should not happen, type is checked when compiling
3303 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003304 break;
3305
Bram Moolenaard3aac292020-04-19 14:32:17 +02003306 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003307 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02003308 case ISN_STOREB:
3309 case ISN_STOREW:
3310 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003311 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003312 dictitem_T *di;
3313 hashtab_T *ht;
3314 char_u *name = iptr->isn_arg.string + 2;
3315
Bram Moolenaard3aac292020-04-19 14:32:17 +02003316 switch (iptr->isn_type)
3317 {
3318 case ISN_STOREG:
3319 ht = get_globvar_ht();
3320 break;
3321 case ISN_STOREB:
3322 ht = &curbuf->b_vars->dv_hashtab;
3323 break;
3324 case ISN_STOREW:
3325 ht = &curwin->w_vars->dv_hashtab;
3326 break;
3327 case ISN_STORET:
3328 ht = &curtab->tp_vars->dv_hashtab;
3329 break;
3330 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003331 goto theend;
Bram Moolenaard3aac292020-04-19 14:32:17 +02003332 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003333
Bram Moolenaar4c137212021-04-19 16:48:48 +02003334 --ectx->ec_stack.ga_len;
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003335 di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003336 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003337 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003338 else
3339 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003340 SOURCING_LNUM = iptr->isn_lnum;
3341 if (var_check_permission(di, name) == FAIL)
3342 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003343 clear_tv(&di->di_tv);
3344 di->di_tv = *STACK_TV_BOT(0);
3345 }
3346 }
3347 break;
3348
Bram Moolenaar03290b82020-12-19 16:30:44 +01003349 // store an autoload variable
3350 case ISN_STOREAUTO:
3351 SOURCING_LNUM = iptr->isn_lnum;
3352 set_var(iptr->isn_arg.string, STACK_TV_BOT(-1), TRUE);
3353 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003354 --ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003355 break;
3356
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003357 // store number in local variable
3358 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01003359 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003360 clear_tv(tv);
3361 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01003362 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003363 break;
3364
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01003365 // store value in list or dict variable
3366 case ISN_STOREINDEX:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003367 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003368 int res = execute_storeindex(iptr, ectx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003369
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003370 if (res == FAIL)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02003371 goto on_error;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003372 if (res == NOTDONE)
3373 goto theend;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003374 }
3375 break;
3376
Bram Moolenaarea5c8982022-02-17 14:42:02 +00003377 // store value in list or blob range
Bram Moolenaar68452172021-04-12 21:21:02 +02003378 case ISN_STORERANGE:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003379 if (execute_storerange(iptr, ectx) == FAIL)
3380 goto on_error;
Bram Moolenaar68452172021-04-12 21:21:02 +02003381 break;
3382
Bram Moolenaar0186e582021-01-10 18:33:11 +01003383 // load or store variable or argument from outer scope
3384 case ISN_LOADOUTER:
3385 case ISN_STOREOUTER:
3386 {
3387 int depth = iptr->isn_arg.outer.outer_depth;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02003388 outer_T *outer = ectx->ec_outer_ref == NULL ? NULL
3389 : ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003390
3391 while (depth > 1 && outer != NULL)
3392 {
3393 outer = outer->out_up;
3394 --depth;
3395 }
3396 if (outer == NULL)
3397 {
3398 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar69c76172021-12-02 16:38:52 +00003399 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx
3400 || ectx->ec_outer_ref == NULL)
3401 // Possibly :def function called from legacy
3402 // context.
3403 emsg(_(e_closure_called_from_invalid_context));
3404 else
3405 iemsg("LOADOUTER depth more than scope levels");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003406 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003407 }
3408 tv = ((typval_T *)outer->out_stack->ga_data)
3409 + outer->out_frame_idx + STACK_FRAME_SIZE
3410 + iptr->isn_arg.outer.outer_idx;
3411 if (iptr->isn_type == ISN_LOADOUTER)
3412 {
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 Moolenaar0186e582021-01-10 18:33:11 +01003415 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003416 ++ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003417 }
3418 else
3419 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003420 --ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003421 clear_tv(tv);
3422 *tv = *STACK_TV_BOT(0);
3423 }
3424 }
3425 break;
3426
Bram Moolenaar752fc692021-01-04 21:57:11 +01003427 // unlet item in list or dict variable
3428 case ISN_UNLETINDEX:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003429 if (execute_unletindex(iptr, ectx) == FAIL)
3430 goto on_error;
Bram Moolenaar752fc692021-01-04 21:57:11 +01003431 break;
3432
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01003433 // unlet range of items in list variable
3434 case ISN_UNLETRANGE:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003435 if (execute_unletrange(iptr, ectx) == FAIL)
3436 goto on_error;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01003437 break;
3438
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003439 // push constant
3440 case ISN_PUSHNR:
3441 case ISN_PUSHBOOL:
3442 case ISN_PUSHSPEC:
3443 case ISN_PUSHF:
3444 case ISN_PUSHS:
3445 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003446 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003447 case ISN_PUSHCHANNEL:
3448 case ISN_PUSHJOB:
Bram Moolenaar35578162021-08-02 19:10:38 +02003449 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003450 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003451 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003452 tv->v_lock = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003453 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003454 switch (iptr->isn_type)
3455 {
3456 case ISN_PUSHNR:
3457 tv->v_type = VAR_NUMBER;
3458 tv->vval.v_number = iptr->isn_arg.number;
3459 break;
3460 case ISN_PUSHBOOL:
3461 tv->v_type = VAR_BOOL;
3462 tv->vval.v_number = iptr->isn_arg.number;
3463 break;
3464 case ISN_PUSHSPEC:
3465 tv->v_type = VAR_SPECIAL;
3466 tv->vval.v_number = iptr->isn_arg.number;
3467 break;
3468#ifdef FEAT_FLOAT
3469 case ISN_PUSHF:
3470 tv->v_type = VAR_FLOAT;
3471 tv->vval.v_float = iptr->isn_arg.fnumber;
3472 break;
3473#endif
3474 case ISN_PUSHBLOB:
3475 blob_copy(iptr->isn_arg.blob, tv);
3476 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003477 case ISN_PUSHFUNC:
3478 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01003479 if (iptr->isn_arg.string == NULL)
3480 tv->vval.v_string = NULL;
3481 else
3482 tv->vval.v_string =
3483 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003484 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003485 case ISN_PUSHCHANNEL:
3486#ifdef FEAT_JOB_CHANNEL
3487 tv->v_type = VAR_CHANNEL;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003488 tv->vval.v_channel = NULL;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003489#endif
3490 break;
3491 case ISN_PUSHJOB:
3492#ifdef FEAT_JOB_CHANNEL
3493 tv->v_type = VAR_JOB;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003494 tv->vval.v_job = NULL;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003495#endif
3496 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003497 default:
3498 tv->v_type = VAR_STRING;
Bram Moolenaarf8691002022-03-10 12:20:53 +00003499 tv->vval.v_string = iptr->isn_arg.string == NULL
3500 ? NULL : vim_strsave(iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003501 }
3502 break;
3503
Bram Moolenaar06b77222022-01-25 15:51:56 +00003504 case ISN_AUTOLOAD:
3505 {
3506 char_u *name = iptr->isn_arg.string;
3507
3508 (void)script_autoload(name, FALSE);
3509 if (find_func(name, TRUE))
3510 {
3511 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
3512 goto theend;
3513 tv = STACK_TV_BOT(0);
3514 tv->v_lock = 0;
3515 ++ectx->ec_stack.ga_len;
3516 tv->v_type = VAR_FUNC;
3517 tv->vval.v_string = vim_strsave(name);
3518 }
3519 else
3520 {
3521 int res = load_namespace_var(ectx, ISN_LOADG, iptr);
3522
3523 if (res == NOTDONE)
3524 goto theend;
3525 if (res == FAIL)
3526 goto on_error;
3527 }
3528 }
3529 break;
3530
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003531 case ISN_UNLET:
3532 if (do_unlet(iptr->isn_arg.unlet.ul_name,
3533 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003534 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003535 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02003536 case ISN_UNLETENV:
LemonBoy77142312022-04-15 20:50:46 +01003537 vim_unsetenv_ext(iptr->isn_arg.unlet.ul_name);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02003538 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003539
Bram Moolenaaraacc9662021-08-13 19:40:51 +02003540 case ISN_LOCKUNLOCK:
3541 {
3542 typval_T *lval_root_save = lval_root;
3543 int res;
3544
3545 // Stack has the local variable, argument the whole :lock
3546 // or :unlock command, like ISN_EXEC.
3547 --ectx->ec_stack.ga_len;
3548 lval_root = STACK_TV_BOT(0);
3549 res = exec_command(iptr);
3550 clear_tv(lval_root);
3551 lval_root = lval_root_save;
3552 if (res == FAIL)
3553 goto on_error;
3554 }
3555 break;
3556
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02003557 case ISN_LOCKCONST:
3558 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
3559 break;
3560
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003561 // create a list from items on the stack; uses a single allocation
3562 // for the list header and the items
3563 case ISN_NEWLIST:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003564 if (exe_newlist(iptr->isn_arg.number, ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003565 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003566 break;
3567
3568 // create a dict from items on the stack
3569 case ISN_NEWDICT:
3570 {
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01003571 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003572
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01003573 SOURCING_LNUM = iptr->isn_lnum;
3574 res = exe_newdict(iptr->isn_arg.number, ectx);
3575 if (res == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003576 goto theend;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01003577 if (res == MAYBE)
3578 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003579 }
3580 break;
3581
LemonBoy372bcce2022-04-25 12:43:20 +01003582 case ISN_CONCAT:
3583 if (exe_concat(iptr->isn_arg.number, ectx) == FAIL)
3584 goto theend;
3585 break;
3586
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003587 // create a partial with NULL value
3588 case ISN_NEWPARTIAL:
3589 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
3590 goto theend;
3591 ++ectx->ec_stack.ga_len;
3592 tv = STACK_TV_BOT(-1);
3593 tv->v_type = VAR_PARTIAL;
3594 tv->v_lock = 0;
3595 tv->vval.v_partial = NULL;
3596 break;
3597
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003598 // call a :def function
3599 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02003600 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003601 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
3602 NULL,
3603 iptr->isn_arg.dfunc.cdf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003604 ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003605 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003606 break;
3607
3608 // call a builtin function
3609 case ISN_BCALL:
3610 SOURCING_LNUM = iptr->isn_lnum;
3611 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
3612 iptr->isn_arg.bfunc.cbf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003613 ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02003614 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003615 break;
3616
3617 // call a funcref or partial
3618 case ISN_PCALL:
3619 {
3620 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
3621 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003622 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003623
3624 SOURCING_LNUM = iptr->isn_lnum;
3625 if (pfunc->cpf_top)
3626 {
3627 // funcref is above the arguments
3628 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
3629 }
3630 else
3631 {
3632 // Get the funcref from the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02003633 --ectx->ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003634 partial_tv = *STACK_TV_BOT(0);
3635 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003636 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003637 r = call_partial(tv, pfunc->cpf_argcount, ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003638 if (tv == &partial_tv)
3639 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003640 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02003641 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003642 }
3643 break;
3644
Bram Moolenaarbd5da372020-03-31 23:13:10 +02003645 case ISN_PCALL_END:
3646 // PCALL finished, arguments have been consumed and replaced by
3647 // the return value. Now clear the funcref from the stack,
3648 // and move the return value in its place.
Bram Moolenaar4c137212021-04-19 16:48:48 +02003649 --ectx->ec_stack.ga_len;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02003650 clear_tv(STACK_TV_BOT(-1));
3651 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
3652 break;
3653
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003654 // call a user defined function or funcref/partial
3655 case ISN_UCALL:
3656 {
3657 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
3658
3659 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003660 if (call_eval_func(cufunc->cuf_name, cufunc->cuf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003661 ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02003662 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003663 }
3664 break;
3665
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02003666 // return from a :def function call without a value
3667 case ISN_RETURN_VOID:
Bram Moolenaar35578162021-08-02 19:10:38 +02003668 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003669 goto theend;
Bram Moolenaar299f3032021-01-08 20:53:09 +01003670 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003671 ++ectx->ec_stack.ga_len;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02003672 tv->v_type = VAR_VOID;
Bram Moolenaar299f3032021-01-08 20:53:09 +01003673 tv->vval.v_number = 0;
3674 tv->v_lock = 0;
3675 // FALLTHROUGH
3676
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02003677 // return from a :def function call with what is on the stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003678 case ISN_RETURN:
3679 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003680 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003681 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01003682
3683 if (trystack->ga_len > 0)
3684 trycmd = ((trycmd_T *)trystack->ga_data)
3685 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003686 if (trycmd != NULL
Bram Moolenaar4c137212021-04-19 16:48:48 +02003687 && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003688 {
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01003689 // jump to ":finally" or ":endtry"
3690 if (trycmd->tcd_finally_idx != 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02003691 ectx->ec_iidx = trycmd->tcd_finally_idx;
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01003692 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02003693 ectx->ec_iidx = trycmd->tcd_endtry_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003694 trycmd->tcd_return = TRUE;
3695 }
3696 else
Bram Moolenaard032f342020-07-18 18:13:02 +02003697 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003698 }
3699 break;
3700
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02003701 // push a partial, a reference to a compiled function
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003702 case ISN_FUNCREF:
3703 {
Bram Moolenaarf112f302020-12-20 17:47:52 +01003704 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar38453522021-11-28 22:00:12 +00003705 ufunc_T *ufunc;
3706 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003707
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003708 if (pt == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003709 goto theend;
Bram Moolenaar35578162021-08-02 19:10:38 +02003710 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003711 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003712 vim_free(pt);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003713 goto theend;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003714 }
Bram Moolenaar38453522021-11-28 22:00:12 +00003715 if (funcref->fr_func_name == NULL)
3716 {
3717 dfunc_T *pt_dfunc = ((dfunc_T *)def_functions.ga_data)
3718 + funcref->fr_dfunc_idx;
3719
3720 ufunc = pt_dfunc->df_ufunc;
3721 }
3722 else
3723 {
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003724 ufunc = find_func(funcref->fr_func_name, FALSE);
Bram Moolenaar38453522021-11-28 22:00:12 +00003725 }
Bram Moolenaar56acd1f2022-02-18 13:24:52 +00003726 if (ufunc == NULL)
3727 {
3728 SOURCING_LNUM = iptr->isn_lnum;
3729 iemsg("ufunc unexpectedly NULL for FUNCREF");
3730 goto theend;
3731 }
Bram Moolenaar38453522021-11-28 22:00:12 +00003732 if (fill_partial_and_closure(pt, ufunc, ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003733 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003734 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003735 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003736 tv->vval.v_partial = pt;
3737 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003738 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003739 }
3740 break;
3741
Bram Moolenaar38ddf332020-07-31 22:05:04 +02003742 // Create a global function from a lambda.
3743 case ISN_NEWFUNC:
3744 {
3745 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
3746
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01003747 if (copy_func(newfunc->nf_lambda, newfunc->nf_global,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003748 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003749 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02003750 }
3751 break;
3752
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01003753 // List functions
3754 case ISN_DEF:
3755 if (iptr->isn_arg.string == NULL)
3756 list_functions(NULL);
3757 else
3758 {
Bram Moolenaar14336722022-01-08 16:02:59 +00003759 exarg_T ea;
3760 garray_T lines_to_free;
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01003761
3762 CLEAR_FIELD(ea);
3763 ea.cmd = ea.arg = iptr->isn_arg.string;
Bram Moolenaar14336722022-01-08 16:02:59 +00003764 ga_init2(&lines_to_free, sizeof(char_u *), 50);
3765 define_function(&ea, NULL, &lines_to_free);
3766 ga_clear_strings(&lines_to_free);
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01003767 }
3768 break;
3769
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003770 // jump if a condition is met
3771 case ISN_JUMP:
3772 {
3773 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003774 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003775 int jump = TRUE;
3776
3777 if (when != JUMP_ALWAYS)
3778 {
3779 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003780 if (when == JUMP_IF_COND_FALSE
Bram Moolenaar13106602020-10-04 16:06:05 +02003781 || when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003782 || when == JUMP_IF_COND_TRUE)
3783 {
3784 SOURCING_LNUM = iptr->isn_lnum;
3785 jump = tv_get_bool_chk(tv, &error);
3786 if (error)
3787 goto on_error;
3788 }
3789 else
3790 jump = tv2bool(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003791 if (when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003792 || when == JUMP_AND_KEEP_IF_FALSE
3793 || when == JUMP_IF_COND_FALSE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003794 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01003795 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003796 {
3797 // drop the value from the stack
3798 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003799 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003800 }
3801 }
3802 if (jump)
Bram Moolenaar4c137212021-04-19 16:48:48 +02003803 ectx->ec_iidx = iptr->isn_arg.jump.jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003804 }
3805 break;
3806
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02003807 // Jump if an argument with a default value was already set and not
3808 // v:none.
3809 case ISN_JUMP_IF_ARG_SET:
3810 tv = STACK_TV_VAR(iptr->isn_arg.jumparg.jump_arg_off);
3811 if (tv->v_type != VAR_UNKNOWN
3812 && !(tv->v_type == VAR_SPECIAL
3813 && tv->vval.v_number == VVAL_NONE))
Bram Moolenaar4c137212021-04-19 16:48:48 +02003814 ectx->ec_iidx = iptr->isn_arg.jumparg.jump_where;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02003815 break;
3816
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003817 // top of a for loop
3818 case ISN_FOR:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003819 if (execute_for(iptr, ectx) == FAIL)
3820 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003821 break;
3822
3823 // start of ":try" block
3824 case ISN_TRY:
3825 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01003826 trycmd_T *trycmd = NULL;
3827
Bram Moolenaar35578162021-08-02 19:10:38 +02003828 if (GA_GROW_FAILS(&ectx->ec_trystack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003829 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003830 trycmd = ((trycmd_T *)ectx->ec_trystack.ga_data)
3831 + ectx->ec_trystack.ga_len;
3832 ++ectx->ec_trystack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003833 ++trylevel;
Bram Moolenaar8d4be892021-02-13 18:33:02 +01003834 CLEAR_POINTER(trycmd);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003835 trycmd->tcd_frame_idx = ectx->ec_frame_idx;
3836 trycmd->tcd_stack_len = ectx->ec_stack.ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01003837 trycmd->tcd_catch_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00003838 iptr->isn_arg.tryref.try_ref->try_catch;
Bram Moolenaara91a7132021-03-25 21:12:15 +01003839 trycmd->tcd_finally_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00003840 iptr->isn_arg.tryref.try_ref->try_finally;
Bram Moolenaara91a7132021-03-25 21:12:15 +01003841 trycmd->tcd_endtry_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00003842 iptr->isn_arg.tryref.try_ref->try_endtry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003843 }
3844 break;
3845
3846 case ISN_PUSHEXC:
3847 if (current_exception == NULL)
3848 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003849 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003850 iemsg("Evaluating catch while current_exception is NULL");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003851 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003852 }
Bram Moolenaar35578162021-08-02 19:10:38 +02003853 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003854 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003855 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003856 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003857 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003858 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003859 tv->vval.v_string = vim_strsave(
3860 (char_u *)current_exception->value);
3861 break;
3862
3863 case ISN_CATCH:
3864 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003865 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar06651632022-04-27 17:54:25 +01003866 trycmd_T *trycmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003867
Bram Moolenaar4c137212021-04-19 16:48:48 +02003868 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar06651632022-04-27 17:54:25 +01003869 trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003870 + trystack->ga_len - 1;
Bram Moolenaar06651632022-04-27 17:54:25 +01003871 trycmd->tcd_caught = TRUE;
3872 trycmd->tcd_did_throw = FALSE;
3873
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003874 did_emsg = got_int = did_throw = FALSE;
Bram Moolenaar1430cee2021-01-17 19:20:32 +01003875 force_abort = need_rethrow = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003876 catch_exception(current_exception);
3877 }
3878 break;
3879
Bram Moolenaarc150c092021-02-13 15:02:46 +01003880 case ISN_TRYCONT:
3881 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003882 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003883 trycont_T *trycont = &iptr->isn_arg.trycont;
3884 int i;
3885 trycmd_T *trycmd;
3886 int iidx = trycont->tct_where;
3887
3888 if (trystack->ga_len < trycont->tct_levels)
3889 {
3890 siemsg("TRYCONT: expected %d levels, found %d",
3891 trycont->tct_levels, trystack->ga_len);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003892 goto theend;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003893 }
3894 // Make :endtry jump to any outer try block and the last
3895 // :endtry inside the loop to the loop start.
3896 for (i = trycont->tct_levels; i > 0; --i)
3897 {
3898 trycmd = ((trycmd_T *)trystack->ga_data)
3899 + trystack->ga_len - i;
Bram Moolenaar2e34c342021-03-14 12:13:33 +01003900 // Add one to tcd_cont to be able to jump to
3901 // instruction with index zero.
3902 trycmd->tcd_cont = iidx + 1;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003903 iidx = trycmd->tcd_finally_idx == 0
3904 ? trycmd->tcd_endtry_idx : trycmd->tcd_finally_idx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003905 }
3906 // jump to :finally or :endtry of current try statement
Bram Moolenaar4c137212021-04-19 16:48:48 +02003907 ectx->ec_iidx = iidx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003908 }
3909 break;
3910
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003911 case ISN_FINALLY:
3912 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003913 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003914 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
3915 + trystack->ga_len - 1;
3916
3917 // Reset the index to avoid a return statement jumps here
3918 // again.
3919 trycmd->tcd_finally_idx = 0;
3920 break;
3921 }
3922
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003923 // end of ":try" block
3924 case ISN_ENDTRY:
3925 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003926 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar06651632022-04-27 17:54:25 +01003927 trycmd_T *trycmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003928
Bram Moolenaar06651632022-04-27 17:54:25 +01003929 --trystack->ga_len;
3930 --trylevel;
3931 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len;
3932 if (trycmd->tcd_did_throw)
3933 did_throw = TRUE;
3934 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003935 {
Bram Moolenaar06651632022-04-27 17:54:25 +01003936 // discard the exception
3937 if (caught_stack == current_exception)
3938 caught_stack = caught_stack->caught;
3939 discard_current_exception();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003940 }
Bram Moolenaar06651632022-04-27 17:54:25 +01003941
3942 if (trycmd->tcd_return)
3943 goto func_return;
3944
3945 while (ectx->ec_stack.ga_len > trycmd->tcd_stack_len)
3946 {
3947 --ectx->ec_stack.ga_len;
3948 clear_tv(STACK_TV_BOT(0));
3949 }
3950 if (trycmd->tcd_cont != 0)
3951 // handling :continue: jump to outer try block or
3952 // start of the loop
3953 ectx->ec_iidx = trycmd->tcd_cont - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003954 }
3955 break;
3956
3957 case ISN_THROW:
Bram Moolenaar8f81b222021-01-14 21:47:06 +01003958 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003959 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02003960
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003961 if (trystack->ga_len == 0 && trylevel == 0 && emsg_silent)
3962 {
3963 // throwing an exception while using "silent!" causes
3964 // the function to abort but not display an error.
3965 tv = STACK_TV_BOT(-1);
3966 clear_tv(tv);
3967 tv->v_type = VAR_NUMBER;
3968 tv->vval.v_number = 0;
3969 goto done;
3970 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003971 --ectx->ec_stack.ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003972 tv = STACK_TV_BOT(0);
3973 if (tv->vval.v_string == NULL
3974 || *skipwhite(tv->vval.v_string) == NUL)
3975 {
3976 vim_free(tv->vval.v_string);
3977 SOURCING_LNUM = iptr->isn_lnum;
3978 emsg(_(e_throw_with_empty_string));
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003979 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003980 }
3981
3982 // Inside a "catch" we need to first discard the caught
3983 // exception.
3984 if (trystack->ga_len > 0)
3985 {
3986 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
3987 + trystack->ga_len - 1;
3988 if (trycmd->tcd_caught && current_exception != NULL)
3989 {
3990 // discard the exception
3991 if (caught_stack == current_exception)
3992 caught_stack = caught_stack->caught;
3993 discard_current_exception();
3994 trycmd->tcd_caught = FALSE;
3995 }
3996 }
3997
Bram Moolenaar90a57162022-02-12 14:23:17 +00003998 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003999 if (throw_exception(tv->vval.v_string, ET_USER, NULL)
4000 == FAIL)
4001 {
4002 vim_free(tv->vval.v_string);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004003 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004004 }
4005 did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004006 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004007 break;
4008
4009 // compare with special values
4010 case ISN_COMPAREBOOL:
4011 case ISN_COMPARESPECIAL:
4012 {
4013 typval_T *tv1 = STACK_TV_BOT(-2);
4014 typval_T *tv2 = STACK_TV_BOT(-1);
4015 varnumber_T arg1 = tv1->vval.v_number;
4016 varnumber_T arg2 = tv2->vval.v_number;
4017 int res;
4018
Bram Moolenaar06651632022-04-27 17:54:25 +01004019 if (iptr->isn_arg.op.op_type == EXPR_EQUAL)
4020 res = arg1 == arg2;
4021 else
4022 res = arg1 != arg2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004023
Bram Moolenaar4c137212021-04-19 16:48:48 +02004024 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004025 tv1->v_type = VAR_BOOL;
4026 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4027 }
4028 break;
4029
Bram Moolenaar7a222242022-03-01 19:23:24 +00004030 case ISN_COMPARENULL:
4031 {
4032 typval_T *tv1 = STACK_TV_BOT(-2);
4033 typval_T *tv2 = STACK_TV_BOT(-1);
4034 int res;
4035
4036 res = typval_compare_null(tv1, tv2);
4037 if (res == MAYBE)
4038 goto on_error;
4039 if (iptr->isn_arg.op.op_type == EXPR_NEQUAL)
4040 res = !res;
4041 clear_tv(tv1);
4042 clear_tv(tv2);
4043 --ectx->ec_stack.ga_len;
4044 tv1->v_type = VAR_BOOL;
4045 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4046 }
4047 break;
4048
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004049 // Operation with two number arguments
4050 case ISN_OPNR:
4051 case ISN_COMPARENR:
4052 {
4053 typval_T *tv1 = STACK_TV_BOT(-2);
4054 typval_T *tv2 = STACK_TV_BOT(-1);
4055 varnumber_T arg1 = tv1->vval.v_number;
4056 varnumber_T arg2 = tv2->vval.v_number;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004057 varnumber_T res = 0;
4058 int div_zero = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004059
4060 switch (iptr->isn_arg.op.op_type)
4061 {
4062 case EXPR_MULT: res = arg1 * arg2; break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004063 case EXPR_DIV: if (arg2 == 0)
4064 div_zero = TRUE;
4065 else
4066 res = arg1 / arg2;
4067 break;
4068 case EXPR_REM: if (arg2 == 0)
4069 div_zero = TRUE;
4070 else
4071 res = arg1 % arg2;
4072 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004073 case EXPR_SUB: res = arg1 - arg2; break;
4074 case EXPR_ADD: res = arg1 + arg2; break;
4075
4076 case EXPR_EQUAL: res = arg1 == arg2; break;
4077 case EXPR_NEQUAL: res = arg1 != arg2; break;
4078 case EXPR_GREATER: res = arg1 > arg2; break;
4079 case EXPR_GEQUAL: res = arg1 >= arg2; break;
4080 case EXPR_SMALLER: res = arg1 < arg2; break;
4081 case EXPR_SEQUAL: res = arg1 <= arg2; break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004082 default: break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004083 }
4084
Bram Moolenaar4c137212021-04-19 16:48:48 +02004085 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004086 if (iptr->isn_type == ISN_COMPARENR)
4087 {
4088 tv1->v_type = VAR_BOOL;
4089 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4090 }
4091 else
4092 tv1->vval.v_number = res;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004093 if (div_zero)
4094 {
4095 SOURCING_LNUM = iptr->isn_lnum;
4096 emsg(_(e_divide_by_zero));
4097 goto on_error;
4098 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004099 }
4100 break;
4101
4102 // Computation with two float arguments
4103 case ISN_OPFLOAT:
4104 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01004105#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004106 {
4107 typval_T *tv1 = STACK_TV_BOT(-2);
4108 typval_T *tv2 = STACK_TV_BOT(-1);
4109 float_T arg1 = tv1->vval.v_float;
4110 float_T arg2 = tv2->vval.v_float;
4111 float_T res = 0;
4112 int cmp = FALSE;
4113
4114 switch (iptr->isn_arg.op.op_type)
4115 {
4116 case EXPR_MULT: res = arg1 * arg2; break;
4117 case EXPR_DIV: res = arg1 / arg2; break;
4118 case EXPR_SUB: res = arg1 - arg2; break;
4119 case EXPR_ADD: res = arg1 + arg2; break;
4120
4121 case EXPR_EQUAL: cmp = arg1 == arg2; break;
4122 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
4123 case EXPR_GREATER: cmp = arg1 > arg2; break;
4124 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
4125 case EXPR_SMALLER: cmp = arg1 < arg2; break;
4126 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
4127 default: cmp = 0; break;
4128 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004129 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004130 if (iptr->isn_type == ISN_COMPAREFLOAT)
4131 {
4132 tv1->v_type = VAR_BOOL;
4133 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
4134 }
4135 else
4136 tv1->vval.v_float = res;
4137 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01004138#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004139 break;
4140
4141 case ISN_COMPARELIST:
Bram Moolenaar265f8112021-12-19 12:33:05 +00004142 case ISN_COMPAREDICT:
4143 case ISN_COMPAREFUNC:
4144 case ISN_COMPARESTRING:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004145 case ISN_COMPAREBLOB:
4146 {
4147 typval_T *tv1 = STACK_TV_BOT(-2);
4148 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar265f8112021-12-19 12:33:05 +00004149 exprtype_T exprtype = iptr->isn_arg.op.op_type;
4150 int ic = iptr->isn_arg.op.op_ic;
4151 int res = FALSE;
4152 int status = OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004153
Bram Moolenaar265f8112021-12-19 12:33:05 +00004154 SOURCING_LNUM = iptr->isn_lnum;
4155 if (iptr->isn_type == ISN_COMPARELIST)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004156 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00004157 status = typval_compare_list(tv1, tv2,
4158 exprtype, ic, &res);
4159 }
4160 else if (iptr->isn_type == ISN_COMPAREDICT)
4161 {
4162 status = typval_compare_dict(tv1, tv2,
4163 exprtype, ic, &res);
4164 }
4165 else if (iptr->isn_type == ISN_COMPAREFUNC)
4166 {
4167 status = typval_compare_func(tv1, tv2,
4168 exprtype, ic, &res);
4169 }
4170 else if (iptr->isn_type == ISN_COMPARESTRING)
4171 {
4172 status = typval_compare_string(tv1, tv2,
4173 exprtype, ic, &res);
4174 }
4175 else
4176 {
4177 status = typval_compare_blob(tv1, tv2, exprtype, &res);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004178 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004179 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004180 clear_tv(tv1);
4181 clear_tv(tv2);
4182 tv1->v_type = VAR_BOOL;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004183 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4184 if (status == FAIL)
4185 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004186 }
4187 break;
4188
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004189 case ISN_COMPAREANY:
4190 {
4191 typval_T *tv1 = STACK_TV_BOT(-2);
4192 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar657137c2021-01-09 15:45:23 +01004193 exprtype_T exprtype = iptr->isn_arg.op.op_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004194 int ic = iptr->isn_arg.op.op_ic;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004195 int status;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004196
Bram Moolenaareb26f432020-09-14 16:50:05 +02004197 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004198 status = typval_compare(tv1, tv2, exprtype, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004199 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004200 --ectx->ec_stack.ga_len;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004201 if (status == FAIL)
4202 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004203 }
4204 break;
4205
4206 case ISN_ADDLIST:
4207 case ISN_ADDBLOB:
4208 {
4209 typval_T *tv1 = STACK_TV_BOT(-2);
4210 typval_T *tv2 = STACK_TV_BOT(-1);
4211
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004212 // add two lists or blobs
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004213 if (iptr->isn_type == ISN_ADDLIST)
Bram Moolenaar07802042021-09-09 23:01:14 +02004214 {
4215 if (iptr->isn_arg.op.op_type == EXPR_APPEND
4216 && tv1->vval.v_list != NULL)
4217 list_extend(tv1->vval.v_list, tv2->vval.v_list,
4218 NULL);
4219 else
4220 eval_addlist(tv1, tv2);
4221 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004222 else
4223 eval_addblob(tv1, tv2);
4224 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004225 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004226 }
4227 break;
4228
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004229 case ISN_LISTAPPEND:
4230 {
4231 typval_T *tv1 = STACK_TV_BOT(-2);
4232 typval_T *tv2 = STACK_TV_BOT(-1);
4233 list_T *l = tv1->vval.v_list;
4234
4235 // add an item to a list
Bram Moolenaar1f4a3452022-01-01 18:29:21 +00004236 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004237 if (l == NULL)
4238 {
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004239 emsg(_(e_cannot_add_to_null_list));
4240 goto on_error;
4241 }
Bram Moolenaar1f4a3452022-01-01 18:29:21 +00004242 if (value_check_lock(l->lv_lock, NULL, FALSE))
4243 goto on_error;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004244 if (list_append_tv(l, tv2) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004245 goto theend;
Bram Moolenaar955347c2020-10-19 23:01:46 +02004246 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004247 --ectx->ec_stack.ga_len;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004248 }
4249 break;
4250
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02004251 case ISN_BLOBAPPEND:
4252 {
4253 typval_T *tv1 = STACK_TV_BOT(-2);
4254 typval_T *tv2 = STACK_TV_BOT(-1);
4255 blob_T *b = tv1->vval.v_blob;
4256 int error = FALSE;
4257 varnumber_T n;
4258
4259 // add a number to a blob
4260 if (b == NULL)
4261 {
4262 SOURCING_LNUM = iptr->isn_lnum;
4263 emsg(_(e_cannot_add_to_null_blob));
4264 goto on_error;
4265 }
4266 n = tv_get_number_chk(tv2, &error);
4267 if (error)
4268 goto on_error;
4269 ga_append(&b->bv_ga, (int)n);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004270 --ectx->ec_stack.ga_len;
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02004271 }
4272 break;
4273
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004274 // Computation with two arguments of unknown type
4275 case ISN_OPANY:
4276 {
4277 typval_T *tv1 = STACK_TV_BOT(-2);
4278 typval_T *tv2 = STACK_TV_BOT(-1);
4279 varnumber_T n1, n2;
4280#ifdef FEAT_FLOAT
4281 float_T f1 = 0, f2 = 0;
4282#endif
4283 int error = FALSE;
4284
4285 if (iptr->isn_arg.op.op_type == EXPR_ADD)
4286 {
4287 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
4288 {
4289 eval_addlist(tv1, tv2);
4290 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004291 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004292 break;
4293 }
4294 else if (tv1->v_type == VAR_BLOB
4295 && tv2->v_type == VAR_BLOB)
4296 {
4297 eval_addblob(tv1, tv2);
4298 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004299 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004300 break;
4301 }
4302 }
4303#ifdef FEAT_FLOAT
4304 if (tv1->v_type == VAR_FLOAT)
4305 {
4306 f1 = tv1->vval.v_float;
4307 n1 = 0;
4308 }
4309 else
4310#endif
4311 {
Bram Moolenaarf665e972020-12-05 19:17:16 +01004312 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004313 n1 = tv_get_number_chk(tv1, &error);
4314 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02004315 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004316#ifdef FEAT_FLOAT
4317 if (tv2->v_type == VAR_FLOAT)
4318 f1 = n1;
4319#endif
4320 }
4321#ifdef FEAT_FLOAT
4322 if (tv2->v_type == VAR_FLOAT)
4323 {
4324 f2 = tv2->vval.v_float;
4325 n2 = 0;
4326 }
4327 else
4328#endif
4329 {
4330 n2 = tv_get_number_chk(tv2, &error);
4331 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02004332 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004333#ifdef FEAT_FLOAT
4334 if (tv1->v_type == VAR_FLOAT)
4335 f2 = n2;
4336#endif
4337 }
4338#ifdef FEAT_FLOAT
4339 // if there is a float on either side the result is a float
4340 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
4341 {
4342 switch (iptr->isn_arg.op.op_type)
4343 {
4344 case EXPR_MULT: f1 = f1 * f2; break;
4345 case EXPR_DIV: f1 = f1 / f2; break;
4346 case EXPR_SUB: f1 = f1 - f2; break;
4347 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004348 default: SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004349 emsg(_(e_cannot_use_percent_with_float));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004350 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004351 }
4352 clear_tv(tv1);
4353 clear_tv(tv2);
4354 tv1->v_type = VAR_FLOAT;
4355 tv1->vval.v_float = f1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004356 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004357 }
4358 else
4359#endif
4360 {
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004361 int failed = FALSE;
4362
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004363 switch (iptr->isn_arg.op.op_type)
4364 {
4365 case EXPR_MULT: n1 = n1 * n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004366 case EXPR_DIV: n1 = num_divide(n1, n2, &failed);
4367 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01004368 goto on_error;
4369 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004370 case EXPR_SUB: n1 = n1 - n2; break;
4371 case EXPR_ADD: n1 = n1 + n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004372 default: n1 = num_modulus(n1, n2, &failed);
4373 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01004374 goto on_error;
4375 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004376 }
4377 clear_tv(tv1);
4378 clear_tv(tv2);
4379 tv1->v_type = VAR_NUMBER;
4380 tv1->vval.v_number = n1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004381 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004382 }
4383 }
4384 break;
4385
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004386 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004387 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004388 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004389 int is_slice = iptr->isn_type == ISN_STRSLICE;
4390 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004391 char_u *res;
4392
4393 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004394 // string slice: string is at stack-3, first index at
4395 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004396 if (is_slice)
4397 {
4398 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004399 n1 = tv->vval.v_number;
4400 }
4401
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004402 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004403 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004404
Bram Moolenaar4c137212021-04-19 16:48:48 +02004405 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004406 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004407 if (is_slice)
4408 // Slice: Select the characters from the string
Bram Moolenaar6601b622021-01-13 21:47:15 +01004409 res = string_slice(tv->vval.v_string, n1, n2, FALSE);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004410 else
4411 // Index: The resulting variable is a string of a
Bram Moolenaar0289a092021-03-14 18:40:19 +01004412 // single character (including composing characters).
4413 // If the index is too big or negative the result is
4414 // empty.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004415 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004416 vim_free(tv->vval.v_string);
4417 tv->vval.v_string = res;
4418 }
4419 break;
4420
4421 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02004422 case ISN_LISTSLICE:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004423 case ISN_BLOBINDEX:
4424 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004425 {
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004426 int is_slice = iptr->isn_type == ISN_LISTSLICE
4427 || iptr->isn_type == ISN_BLOBSLICE;
4428 int is_blob = iptr->isn_type == ISN_BLOBINDEX
4429 || iptr->isn_type == ISN_BLOBSLICE;
Bram Moolenaared591872020-08-15 22:14:53 +02004430 varnumber_T n1, n2;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004431 typval_T *val_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004432
4433 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02004434 // list slice: list is at stack-3, indexes at stack-2 and
4435 // stack-1
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004436 // Same for blob.
4437 val_tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004438
4439 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02004440 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004441 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02004442
4443 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004444 {
Bram Moolenaared591872020-08-15 22:14:53 +02004445 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02004446 n1 = tv->vval.v_number;
4447 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004448 }
Bram Moolenaared591872020-08-15 22:14:53 +02004449
Bram Moolenaar4c137212021-04-19 16:48:48 +02004450 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02004451 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02004452 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004453 if (is_blob)
4454 {
4455 if (blob_slice_or_index(val_tv->vval.v_blob, is_slice,
4456 n1, n2, FALSE, tv) == FAIL)
4457 goto on_error;
4458 }
4459 else
4460 {
4461 if (list_slice_or_index(val_tv->vval.v_list, is_slice,
4462 n1, n2, FALSE, tv, TRUE) == FAIL)
4463 goto on_error;
4464 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004465 }
4466 break;
4467
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004468 case ISN_ANYINDEX:
4469 case ISN_ANYSLICE:
4470 {
4471 int is_slice = iptr->isn_type == ISN_ANYSLICE;
4472 typval_T *var1, *var2;
4473 int res;
4474
4475 // index: composite is at stack-2, index at stack-1
4476 // slice: composite is at stack-3, indexes at stack-2 and
4477 // stack-1
4478 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02004479 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004480 if (check_can_index(tv, TRUE, TRUE) == FAIL)
4481 goto on_error;
4482 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
4483 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
Bram Moolenaar6601b622021-01-13 21:47:15 +01004484 res = eval_index_inner(tv, is_slice, var1, var2,
4485 FALSE, NULL, -1, TRUE);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004486 clear_tv(var1);
4487 if (is_slice)
4488 clear_tv(var2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004489 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004490 if (res == FAIL)
4491 goto on_error;
4492 }
4493 break;
4494
Bram Moolenaar9af78762020-06-16 11:34:42 +02004495 case ISN_SLICE:
4496 {
4497 list_T *list;
4498 int count = iptr->isn_arg.number;
4499
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02004500 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02004501 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02004502 list = tv->vval.v_list;
4503
4504 // no error for short list, expect it to be checked earlier
4505 if (list != NULL && list->lv_len >= count)
4506 {
4507 list_T *newlist = list_slice(list,
4508 count, list->lv_len - 1);
4509
4510 if (newlist != NULL)
4511 {
4512 list_unref(list);
4513 tv->vval.v_list = newlist;
4514 ++newlist->lv_refcount;
4515 }
4516 }
4517 }
4518 break;
4519
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004520 case ISN_GETITEM:
4521 {
4522 listitem_T *li;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02004523 getitem_T *gi = &iptr->isn_arg.getitem;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004524
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02004525 // Get list item: list is at stack-1, push item.
4526 // List type and length is checked for when compiling.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02004527 tv = STACK_TV_BOT(-1 - gi->gi_with_op);
4528 li = list_find(tv->vval.v_list, gi->gi_index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004529
Bram Moolenaar35578162021-08-02 19:10:38 +02004530 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004531 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004532 ++ectx->ec_stack.ga_len;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004533 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004534
4535 // Useful when used in unpack assignment. Reset at
4536 // ISN_DROP.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02004537 ectx->ec_where.wt_index = gi->gi_index + 1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004538 ectx->ec_where.wt_variable = TRUE;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004539 }
4540 break;
4541
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004542 case ISN_MEMBER:
4543 {
4544 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004545 char_u *key;
4546 dictitem_T *di;
4547
4548 // dict member: dict is at stack-2, key at stack-1
4549 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02004550 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004551 dict = tv->vval.v_dict;
4552
4553 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02004554 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004555 key = tv->vval.v_string;
Bram Moolenaar086fc9a2020-10-30 18:33:02 +01004556 if (key == NULL)
4557 key = (char_u *)"";
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02004558
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004559 if ((di = dict_find(dict, key, -1)) == NULL)
4560 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004561 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004562 semsg(_(e_key_not_present_in_dictionary), key);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004563
4564 // If :silent! is used we will continue, make sure the
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004565 // stack contents makes sense and the dict stack is
4566 // updated.
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004567 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004568 --ectx->ec_stack.ga_len;
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004569 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004570 (void) dict_stack_save(tv);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004571 tv->v_type = VAR_NUMBER;
4572 tv->vval.v_number = 0;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004573 goto on_fatal_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004574 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004575 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004576 --ectx->ec_stack.ga_len;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004577 // Put the dict used on the dict stack, it might be used by
4578 // a dict function later.
Bram Moolenaar50788ef2020-07-05 16:51:26 +02004579 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004580 if (dict_stack_save(tv) == FAIL)
4581 goto on_fatal_error;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02004582 copy_tv(&di->di_tv, tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004583 }
4584 break;
4585
4586 // dict member with string key
4587 case ISN_STRINGMEMBER:
4588 {
4589 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004590 dictitem_T *di;
4591
4592 tv = STACK_TV_BOT(-1);
4593 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
4594 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004595 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004596 emsg(_(e_dictionary_required));
Bram Moolenaard032f342020-07-18 18:13:02 +02004597 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004598 }
4599 dict = tv->vval.v_dict;
4600
4601 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
4602 == NULL)
4603 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004604 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar381692b2022-02-02 20:01:27 +00004605 semsg(_(e_key_not_present_in_dictionary),
4606 iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02004607 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004608 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004609 // Put the dict used on the dict stack, it might be used by
4610 // a dict function later.
4611 if (dict_stack_save(tv) == FAIL)
4612 goto on_fatal_error;
4613
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004614 copy_tv(&di->di_tv, tv);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004615 }
4616 break;
4617
4618 case ISN_CLEARDICT:
4619 dict_stack_drop();
4620 break;
4621
4622 case ISN_USEDICT:
4623 {
4624 typval_T *dict_tv = dict_stack_get_tv();
4625
4626 // Turn "dict.Func" into a partial for "Func" bound to
4627 // "dict". Don't do this when "Func" is already a partial
4628 // that was bound explicitly (pt_auto is FALSE).
4629 tv = STACK_TV_BOT(-1);
4630 if (dict_tv != NULL
4631 && dict_tv->v_type == VAR_DICT
4632 && dict_tv->vval.v_dict != NULL
4633 && (tv->v_type == VAR_FUNC
4634 || (tv->v_type == VAR_PARTIAL
4635 && (tv->vval.v_partial->pt_auto
4636 || tv->vval.v_partial->pt_dict == NULL))))
4637 dict_tv->vval.v_dict =
4638 make_partial(dict_tv->vval.v_dict, tv);
4639 dict_stack_drop();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004640 }
4641 break;
4642
4643 case ISN_NEGATENR:
4644 tv = STACK_TV_BOT(-1);
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00004645 // CHECKTYPE should have checked the variable type
Bram Moolenaarc58164c2020-03-29 18:40:30 +02004646#ifdef FEAT_FLOAT
4647 if (tv->v_type == VAR_FLOAT)
4648 tv->vval.v_float = -tv->vval.v_float;
4649 else
4650#endif
4651 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004652 break;
4653
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004654 case ISN_CHECKTYPE:
4655 {
4656 checktype_T *ct = &iptr->isn_arg.type;
4657
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01004658 tv = STACK_TV_BOT((int)ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02004659 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004660 if (!ectx->ec_where.wt_variable)
4661 ectx->ec_where.wt_index = ct->ct_arg_idx;
4662 if (check_typval_type(ct->ct_type, tv, ectx->ec_where)
4663 == FAIL)
Bram Moolenaar5e654232020-09-16 15:22:00 +02004664 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004665 if (!ectx->ec_where.wt_variable)
4666 ectx->ec_where.wt_index = 0;
Bram Moolenaar5e654232020-09-16 15:22:00 +02004667
4668 // number 0 is FALSE, number 1 is TRUE
4669 if (tv->v_type == VAR_NUMBER
4670 && ct->ct_type->tt_type == VAR_BOOL
4671 && (tv->vval.v_number == 0
4672 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004673 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02004674 tv->v_type = VAR_BOOL;
4675 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02004676 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004677 }
4678 }
4679 break;
4680
Bram Moolenaar9af78762020-06-16 11:34:42 +02004681 case ISN_CHECKLEN:
4682 {
4683 int min_len = iptr->isn_arg.checklen.cl_min_len;
4684 list_T *list = NULL;
4685
4686 tv = STACK_TV_BOT(-1);
4687 if (tv->v_type == VAR_LIST)
4688 list = tv->vval.v_list;
4689 if (list == NULL || list->lv_len < min_len
4690 || (list->lv_len > min_len
4691 && !iptr->isn_arg.checklen.cl_more_OK))
4692 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004693 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004694 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02004695 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004696 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02004697 }
4698 }
4699 break;
4700
Bram Moolenaaraa210a32021-01-02 15:41:03 +01004701 case ISN_SETTYPE:
Bram Moolenaar381692b2022-02-02 20:01:27 +00004702 set_tv_type(STACK_TV_BOT(-1), iptr->isn_arg.type.ct_type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01004703 break;
4704
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004705 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004706 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004707 {
4708 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004709 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004710
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004711 if (iptr->isn_type == ISN_2BOOL)
4712 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004713 tv = STACK_TV_BOT(iptr->isn_arg.tobool.offset);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004714 n = tv2bool(tv);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004715 if (iptr->isn_arg.tobool.invert)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004716 n = !n;
4717 }
4718 else
4719 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004720 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004721 SOURCING_LNUM = iptr->isn_lnum;
4722 n = tv_get_bool_chk(tv, &error);
4723 if (error)
4724 goto on_error;
4725 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004726 clear_tv(tv);
4727 tv->v_type = VAR_BOOL;
4728 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
4729 }
4730 break;
4731
4732 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02004733 case ISN_2STRING_ANY:
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01004734 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004735 if (do_2string(STACK_TV_BOT(iptr->isn_arg.tostring.offset),
4736 iptr->isn_type == ISN_2STRING_ANY,
4737 iptr->isn_arg.tostring.tolerant) == FAIL)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01004738 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004739 break;
4740
Bram Moolenaar08597872020-12-10 19:43:40 +01004741 case ISN_RANGE:
4742 {
4743 exarg_T ea;
4744 char *errormsg;
4745
Bram Moolenaarece0b872021-01-08 20:40:45 +01004746 ea.line2 = 0;
Bram Moolenaard1510ee2021-01-04 16:15:58 +01004747 ea.addr_count = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01004748 ea.addr_type = ADDR_LINES;
4749 ea.cmd = iptr->isn_arg.string;
Bram Moolenaarece0b872021-01-08 20:40:45 +01004750 ea.skip = FALSE;
Bram Moolenaar08597872020-12-10 19:43:40 +01004751 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaarece0b872021-01-08 20:40:45 +01004752 goto on_error;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004753
Bram Moolenaar35578162021-08-02 19:10:38 +02004754 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004755 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004756 ++ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004757 tv = STACK_TV_BOT(-1);
4758 tv->v_type = VAR_NUMBER;
4759 tv->v_lock = 0;
Bram Moolenaar06651632022-04-27 17:54:25 +01004760 tv->vval.v_number = ea.line2;
Bram Moolenaar08597872020-12-10 19:43:40 +01004761 }
4762 break;
4763
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004764 case ISN_PUT:
4765 {
4766 int regname = iptr->isn_arg.put.put_regname;
4767 linenr_T lnum = iptr->isn_arg.put.put_lnum;
4768 char_u *expr = NULL;
4769 int dir = FORWARD;
4770
Bram Moolenaar08597872020-12-10 19:43:40 +01004771 if (lnum < -2)
4772 {
4773 // line number was put on the stack by ISN_RANGE
4774 tv = STACK_TV_BOT(-1);
4775 curwin->w_cursor.lnum = tv->vval.v_number;
4776 if (lnum == LNUM_VARIABLE_RANGE_ABOVE)
4777 dir = BACKWARD;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004778 --ectx->ec_stack.ga_len;
Bram Moolenaar08597872020-12-10 19:43:40 +01004779 }
4780 else if (lnum == -2)
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004781 // :put! above cursor
4782 dir = BACKWARD;
4783 else if (lnum >= 0)
Bram Moolenaar4e713ba2022-02-07 15:31:37 +00004784 {
4785 curwin->w_cursor.lnum = lnum;
4786 if (lnum == 0)
4787 // check_cursor() below will move to line 1
4788 dir = BACKWARD;
4789 }
Bram Moolenaara28639e2021-01-19 22:48:09 +01004790
4791 if (regname == '=')
4792 {
4793 tv = STACK_TV_BOT(-1);
4794 if (tv->v_type == VAR_STRING)
4795 expr = tv->vval.v_string;
4796 else
4797 {
4798 expr = typval2string(tv, TRUE); // allocates value
4799 clear_tv(tv);
4800 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004801 --ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004802 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004803 check_cursor();
4804 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
4805 vim_free(expr);
4806 }
4807 break;
4808
Bram Moolenaar02194d22020-10-24 23:08:38 +02004809 case ISN_CMDMOD:
Bram Moolenaar4c137212021-04-19 16:48:48 +02004810 ectx->ec_funclocal.floc_save_cmdmod = cmdmod;
4811 ectx->ec_funclocal.floc_restore_cmdmod = TRUE;
4812 ectx->ec_funclocal.floc_restore_cmdmod_stacklen =
4813 ectx->ec_stack.ga_len;
Bram Moolenaar02194d22020-10-24 23:08:38 +02004814 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
4815 apply_cmdmod(&cmdmod);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004816 break;
4817
Bram Moolenaar02194d22020-10-24 23:08:38 +02004818 case ISN_CMDMOD_REV:
4819 // filter regprog is owned by the instruction, don't free it
4820 cmdmod.cmod_filter_regmatch.regprog = NULL;
4821 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004822 cmdmod = ectx->ec_funclocal.floc_save_cmdmod;
4823 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004824 break;
4825
Bram Moolenaar792f7862020-11-23 08:31:18 +01004826 case ISN_UNPACK:
4827 {
4828 int count = iptr->isn_arg.unpack.unp_count;
4829 int semicolon = iptr->isn_arg.unpack.unp_semicolon;
4830 list_T *l;
4831 listitem_T *li;
4832 int i;
4833
4834 // Check there is a valid list to unpack.
4835 tv = STACK_TV_BOT(-1);
4836 if (tv->v_type != VAR_LIST)
4837 {
4838 SOURCING_LNUM = iptr->isn_lnum;
4839 emsg(_(e_for_argument_must_be_sequence_of_lists));
4840 goto on_error;
4841 }
4842 l = tv->vval.v_list;
4843 if (l == NULL
4844 || l->lv_len < (semicolon ? count - 1 : count))
4845 {
4846 SOURCING_LNUM = iptr->isn_lnum;
4847 emsg(_(e_list_value_does_not_have_enough_items));
4848 goto on_error;
4849 }
4850 else if (!semicolon && l->lv_len > count)
4851 {
4852 SOURCING_LNUM = iptr->isn_lnum;
4853 emsg(_(e_list_value_has_more_items_than_targets));
4854 goto on_error;
4855 }
4856
4857 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar35578162021-08-02 19:10:38 +02004858 if (GA_GROW_FAILS(&ectx->ec_stack, count - 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004859 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004860 ectx->ec_stack.ga_len += count - 1;
Bram Moolenaar792f7862020-11-23 08:31:18 +01004861
4862 // Variable after semicolon gets a list with the remaining
4863 // items.
4864 if (semicolon)
4865 {
4866 list_T *rem_list =
4867 list_alloc_with_items(l->lv_len - count + 1);
4868
4869 if (rem_list == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004870 goto theend;
Bram Moolenaar792f7862020-11-23 08:31:18 +01004871 tv = STACK_TV_BOT(-count);
4872 tv->vval.v_list = rem_list;
4873 ++rem_list->lv_refcount;
4874 tv->v_lock = 0;
4875 li = l->lv_first;
4876 for (i = 0; i < count - 1; ++i)
4877 li = li->li_next;
4878 for (i = 0; li != NULL; ++i)
4879 {
Bram Moolenaar61efa162022-03-18 13:10:48 +00004880 typval_T tvcopy;
4881
4882 copy_tv(&li->li_tv, &tvcopy);
4883 list_set_item(rem_list, i, &tvcopy);
Bram Moolenaar792f7862020-11-23 08:31:18 +01004884 li = li->li_next;
4885 }
4886 --count;
4887 }
4888
4889 // Produce the values in reverse order, first item last.
4890 li = l->lv_first;
4891 for (i = 0; i < count; ++i)
4892 {
4893 tv = STACK_TV_BOT(-i - 1);
4894 copy_tv(&li->li_tv, tv);
4895 li = li->li_next;
4896 }
4897
4898 list_unref(l);
4899 }
4900 break;
4901
Bram Moolenaarb2049902021-01-24 12:53:53 +01004902 case ISN_PROF_START:
4903 case ISN_PROF_END:
4904 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01004905#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01004906 funccall_T cookie;
4907 ufunc_T *cur_ufunc =
4908 (((dfunc_T *)def_functions.ga_data)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02004909 + ectx->ec_dfunc_idx)->df_ufunc;
Bram Moolenaarb2049902021-01-24 12:53:53 +01004910
4911 cookie.func = cur_ufunc;
4912 if (iptr->isn_type == ISN_PROF_START)
4913 {
4914 func_line_start(&cookie, iptr->isn_lnum);
4915 // if we get here the instruction is executed
4916 func_line_exec(&cookie);
4917 }
4918 else
4919 func_line_end(&cookie);
Bram Moolenaarf002a412021-01-24 13:34:18 +01004920#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01004921 }
4922 break;
4923
Bram Moolenaare99d4222021-06-13 14:01:26 +02004924 case ISN_DEBUG:
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02004925 handle_debug(iptr, ectx);
Bram Moolenaare99d4222021-06-13 14:01:26 +02004926 break;
4927
Bram Moolenaar389df252020-07-09 21:20:47 +02004928 case ISN_SHUFFLE:
4929 {
Bram Moolenaar792f7862020-11-23 08:31:18 +01004930 typval_T tmp_tv;
4931 int item = iptr->isn_arg.shuffle.shfl_item;
4932 int up = iptr->isn_arg.shuffle.shfl_up;
Bram Moolenaar389df252020-07-09 21:20:47 +02004933
4934 tmp_tv = *STACK_TV_BOT(-item);
4935 for ( ; up > 0 && item > 1; --up)
4936 {
4937 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
4938 --item;
4939 }
4940 *STACK_TV_BOT(-item) = tmp_tv;
4941 }
4942 break;
4943
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004944 case ISN_DROP:
Bram Moolenaar4c137212021-04-19 16:48:48 +02004945 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004946 clear_tv(STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02004947 ectx->ec_where.wt_index = 0;
4948 ectx->ec_where.wt_variable = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004949 break;
4950 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004951 continue;
4952
Bram Moolenaard032f342020-07-18 18:13:02 +02004953func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02004954 // Restore previous function. If the frame pointer is where we started
4955 // then there is none and we are done.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004956 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02004957 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02004958
Bram Moolenaar4c137212021-04-19 16:48:48 +02004959 if (func_return(ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004960 // only fails when out of memory
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004961 goto theend;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02004962 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02004963
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004964on_error:
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004965 // Jump here for an error that does not require aborting execution.
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004966 // If "emsg_silent" is set then ignore the error, unless it was set
4967 // when calling the function.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004968 if (did_emsg_cumul + did_emsg == ectx->ec_did_emsg_before
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004969 && emsg_silent && did_emsg_def == 0)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004970 {
4971 // If a sequence of instructions causes an error while ":silent!"
4972 // was used, restore the stack length and jump ahead to restoring
4973 // the cmdmod.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004974 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004975 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004976 while (ectx->ec_stack.ga_len
4977 > ectx->ec_funclocal.floc_restore_cmdmod_stacklen)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004978 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004979 --ectx->ec_stack.ga_len;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004980 clear_tv(STACK_TV_BOT(0));
4981 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004982 while (ectx->ec_instr[ectx->ec_iidx].isn_type != ISN_CMDMOD_REV)
4983 ++ectx->ec_iidx;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004984 }
Bram Moolenaarcd030c42020-10-30 21:49:40 +01004985 continue;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004986 }
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004987on_fatal_error:
4988 // Jump here for an error that messes up the stack.
Bram Moolenaar171fb922020-10-28 16:54:47 +01004989 // If we are not inside a try-catch started here, abort execution.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004990 if (trylevel <= ectx->ec_trylevel_at_start)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004991 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004992 }
4993
4994done:
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004995 ret = OK;
4996theend:
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004997 dict_stack_clear(dict_stack_len_at_start);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004998 ectx->ec_trylevel_at_start = save_trylevel_at_start;
4999 return ret;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005000}
5001
5002/*
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005003 * Execute the instructions from a VAR_INSTR typeval and put the result in
5004 * "rettv".
5005 * Return OK or FAIL.
5006 */
5007 int
5008exe_typval_instr(typval_T *tv, typval_T *rettv)
5009{
5010 ectx_T *ectx = tv->vval.v_instr->instr_ectx;
5011 isn_T *save_instr = ectx->ec_instr;
5012 int save_iidx = ectx->ec_iidx;
5013 int res;
5014
5015 ectx->ec_instr = tv->vval.v_instr->instr_instr;
5016 res = exec_instructions(ectx);
5017 if (res == OK)
5018 {
5019 *rettv = *STACK_TV_BOT(-1);
5020 --ectx->ec_stack.ga_len;
5021 }
5022
5023 ectx->ec_instr = save_instr;
5024 ectx->ec_iidx = save_iidx;
5025
5026 return res;
5027}
5028
5029/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02005030 * Execute the instructions from an ISN_SUBSTITUTE command, which are in
5031 * "substitute_instr".
5032 */
5033 char_u *
5034exe_substitute_instr(void)
5035{
5036 ectx_T *ectx = substitute_instr->subs_ectx;
5037 isn_T *save_instr = ectx->ec_instr;
5038 int save_iidx = ectx->ec_iidx;
5039 char_u *res;
5040
5041 ectx->ec_instr = substitute_instr->subs_instr;
5042 if (exec_instructions(ectx) == OK)
Bram Moolenaar90193e62021-04-04 20:49:50 +02005043 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005044 typval_T *tv = STACK_TV_BOT(-1);
5045
Bram Moolenaar27523602021-06-05 21:36:19 +02005046 res = typval2string(tv, TRUE);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005047 --ectx->ec_stack.ga_len;
5048 clear_tv(tv);
Bram Moolenaar90193e62021-04-04 20:49:50 +02005049 }
5050 else
5051 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005052 substitute_instr->subs_status = FAIL;
5053 res = vim_strsave((char_u *)"");
Bram Moolenaar90193e62021-04-04 20:49:50 +02005054 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005055
Bram Moolenaar4c137212021-04-19 16:48:48 +02005056 ectx->ec_instr = save_instr;
5057 ectx->ec_iidx = save_iidx;
5058
5059 return res;
5060}
5061
5062/*
5063 * Call a "def" function from old Vim script.
5064 * Return OK or FAIL.
5065 */
5066 int
5067call_def_function(
5068 ufunc_T *ufunc,
5069 int argc_arg, // nr of arguments
5070 typval_T *argv, // arguments
5071 partial_T *partial, // optional partial for context
5072 typval_T *rettv) // return value
5073{
5074 ectx_T ectx; // execution context
5075 int argc = argc_arg;
5076 typval_T *tv;
5077 int idx;
5078 int ret = FAIL;
5079 int defcount = ufunc->uf_args.ga_len - argc;
5080 sctx_T save_current_sctx = current_sctx;
5081 int did_emsg_before = did_emsg_cumul + did_emsg;
5082 int save_suppress_errthrow = suppress_errthrow;
5083 msglist_T **saved_msg_list = NULL;
5084 msglist_T *private_msg_list = NULL;
5085 int save_emsg_silent_def = emsg_silent_def;
5086 int save_did_emsg_def = did_emsg_def;
5087 int orig_funcdepth;
Bram Moolenaare99d4222021-06-13 14:01:26 +02005088 int orig_nesting_level = ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005089
5090// Get pointer to item in the stack.
5091#undef STACK_TV
5092#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
5093
5094// Get pointer to item at the bottom of the stack, -1 is the bottom.
5095#undef STACK_TV_BOT
5096#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
5097
5098// Get pointer to a local variable on the stack. Negative for arguments.
5099#undef STACK_TV_VAR
5100#define STACK_TV_VAR(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_frame_idx + STACK_FRAME_SIZE + idx)
5101
5102 if (ufunc->uf_def_status == UF_NOT_COMPILED
5103 || ufunc->uf_def_status == UF_COMPILE_ERROR
Bram Moolenaar139575d2022-03-15 19:29:30 +00005104 || (func_needs_compiling(ufunc, get_compile_type(ufunc))
5105 && compile_def_function(ufunc, FALSE,
5106 get_compile_type(ufunc), NULL) == FAIL))
Bram Moolenaar4c137212021-04-19 16:48:48 +02005107 {
5108 if (did_emsg_cumul + did_emsg == did_emsg_before)
5109 semsg(_(e_function_is_not_compiled_str),
5110 printable_func_name(ufunc));
5111 return FAIL;
5112 }
5113
5114 {
5115 // Check the function was really compiled.
5116 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5117 + ufunc->uf_dfunc_idx;
5118 if (INSTRUCTIONS(dfunc) == NULL)
5119 {
5120 iemsg("using call_def_function() on not compiled function");
5121 return FAIL;
5122 }
5123 }
5124
5125 // If depth of calling is getting too high, don't execute the function.
5126 orig_funcdepth = funcdepth_get();
5127 if (funcdepth_increment() == FAIL)
5128 return FAIL;
5129
5130 CLEAR_FIELD(ectx);
5131 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
5132 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
Bram Moolenaar35578162021-08-02 19:10:38 +02005133 if (GA_GROW_FAILS(&ectx.ec_stack, 20))
Bram Moolenaar4c137212021-04-19 16:48:48 +02005134 {
5135 funcdepth_decrement();
5136 return FAIL;
5137 }
5138 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
5139 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
5140 ectx.ec_did_emsg_before = did_emsg_before;
Bram Moolenaare99d4222021-06-13 14:01:26 +02005141 ++ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005142
5143 idx = argc - ufunc->uf_args.ga_len;
5144 if (idx > 0 && ufunc->uf_va_name == NULL)
5145 {
5146 if (idx == 1)
5147 emsg(_(e_one_argument_too_many));
5148 else
5149 semsg(_(e_nr_arguments_too_many), idx);
5150 goto failed_early;
5151 }
Bram Moolenaarc6d71532021-06-05 18:49:38 +02005152 idx = argc - ufunc->uf_args.ga_len + ufunc->uf_def_args.ga_len;
5153 if (idx < 0)
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02005154 {
5155 if (idx == -1)
5156 emsg(_(e_one_argument_too_few));
5157 else
5158 semsg(_(e_nr_arguments_too_few), -idx);
5159 goto failed_early;
5160 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005161
5162 // Put arguments on the stack, but no more than what the function expects.
5163 // A lambda can be called with more arguments than it uses.
5164 for (idx = 0; idx < argc
5165 && (ufunc->uf_va_name != NULL || idx < ufunc->uf_args.ga_len);
5166 ++idx)
5167 {
5168 if (idx >= ufunc->uf_args.ga_len - ufunc->uf_def_args.ga_len
5169 && argv[idx].v_type == VAR_SPECIAL
5170 && argv[idx].vval.v_number == VVAL_NONE)
5171 {
5172 // Use the default value.
5173 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
5174 }
5175 else
5176 {
5177 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
5178 && check_typval_arg_type(
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02005179 ufunc->uf_arg_types[idx], &argv[idx],
5180 NULL, idx + 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005181 goto failed_early;
5182 copy_tv(&argv[idx], STACK_TV_BOT(0));
5183 }
5184 ++ectx.ec_stack.ga_len;
5185 }
5186
5187 // Turn varargs into a list. Empty list if no args.
5188 if (ufunc->uf_va_name != NULL)
5189 {
5190 int vararg_count = argc - ufunc->uf_args.ga_len;
5191
5192 if (vararg_count < 0)
5193 vararg_count = 0;
5194 else
5195 argc -= vararg_count;
5196 if (exe_newlist(vararg_count, &ectx) == FAIL)
5197 goto failed_early;
5198
5199 // Check the type of the list items.
5200 tv = STACK_TV_BOT(-1);
5201 if (ufunc->uf_va_type != NULL
5202 && ufunc->uf_va_type != &t_list_any
5203 && ufunc->uf_va_type->tt_member != &t_any
5204 && tv->vval.v_list != NULL)
5205 {
5206 type_T *expected = ufunc->uf_va_type->tt_member;
5207 listitem_T *li = tv->vval.v_list->lv_first;
5208
5209 for (idx = 0; idx < vararg_count; ++idx)
5210 {
5211 if (check_typval_arg_type(expected, &li->li_tv,
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02005212 NULL, argc + idx + 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005213 goto failed_early;
5214 li = li->li_next;
5215 }
5216 }
5217
5218 if (defcount > 0)
5219 // Move varargs list to below missing default arguments.
5220 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
5221 --ectx.ec_stack.ga_len;
5222 }
5223
5224 // Make space for omitted arguments, will store default value below.
5225 // Any varargs list goes after them.
5226 if (defcount > 0)
5227 for (idx = 0; idx < defcount; ++idx)
5228 {
5229 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
5230 ++ectx.ec_stack.ga_len;
5231 }
5232 if (ufunc->uf_va_name != NULL)
5233 ++ectx.ec_stack.ga_len;
5234
5235 // Frame pointer points to just after arguments.
5236 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
5237 ectx.ec_initial_frame_idx = ectx.ec_frame_idx;
5238
5239 {
5240 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5241 + ufunc->uf_dfunc_idx;
5242 ufunc_T *base_ufunc = dfunc->df_ufunc;
5243
5244 // "uf_partial" is on the ufunc that "df_ufunc" points to, as is done
5245 // by copy_func().
5246 if (partial != NULL || base_ufunc->uf_partial != NULL)
5247 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005248 ectx.ec_outer_ref = ALLOC_CLEAR_ONE(outer_ref_T);
5249 if (ectx.ec_outer_ref == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005250 goto failed_early;
5251 if (partial != NULL)
5252 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00005253 outer_T *outer = get_pt_outer(partial);
5254
5255 if (outer->out_stack == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005256 {
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00005257 if (current_ectx != NULL)
5258 {
5259 if (current_ectx->ec_outer_ref != NULL
5260 && current_ectx->ec_outer_ref->or_outer != NULL)
5261 ectx.ec_outer_ref->or_outer =
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005262 current_ectx->ec_outer_ref->or_outer;
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00005263 }
5264 // Should there be an error here?
Bram Moolenaar4c137212021-04-19 16:48:48 +02005265 }
5266 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005267 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00005268 ectx.ec_outer_ref->or_outer = outer;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005269 ++partial->pt_refcount;
5270 ectx.ec_outer_ref->or_partial = partial;
5271 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005272 }
5273 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005274 {
5275 ectx.ec_outer_ref->or_outer = &base_ufunc->uf_partial->pt_outer;
5276 ++base_ufunc->uf_partial->pt_refcount;
5277 ectx.ec_outer_ref->or_partial = base_ufunc->uf_partial;
5278 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005279 }
5280 }
5281
5282 // dummy frame entries
5283 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
5284 {
5285 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
5286 ++ectx.ec_stack.ga_len;
5287 }
5288
5289 {
5290 // Reserve space for local variables and any closure reference count.
5291 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5292 + ufunc->uf_dfunc_idx;
5293
Bram Moolenaar5cd64792021-12-25 18:23:24 +00005294 // Initialize variables to zero. That avoids having to generate
5295 // initializing instructions for "var nr: number", "var x: any", etc.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005296 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaar5cd64792021-12-25 18:23:24 +00005297 {
5298 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
5299 STACK_TV_VAR(idx)->vval.v_number = 0;
5300 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005301 ectx.ec_stack.ga_len += dfunc->df_varcount;
5302 if (dfunc->df_has_closure)
5303 {
5304 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
5305 STACK_TV_VAR(idx)->vval.v_number = 0;
5306 ++ectx.ec_stack.ga_len;
5307 }
5308
5309 ectx.ec_instr = INSTRUCTIONS(dfunc);
5310 }
5311
5312 // Following errors are in the function, not the caller.
5313 // Commands behave like vim9script.
5314 estack_push_ufunc(ufunc, 1);
5315 current_sctx = ufunc->uf_script_ctx;
5316 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
5317
5318 // Use a specific location for storing error messages to be converted to an
5319 // exception.
5320 saved_msg_list = msg_list;
5321 msg_list = &private_msg_list;
5322
5323 // Do turn errors into exceptions.
5324 suppress_errthrow = FALSE;
5325
5326 // Do not delete the function while executing it.
5327 ++ufunc->uf_calls;
5328
5329 // When ":silent!" was used before calling then we still abort the
5330 // function. If ":silent!" is used in the function then we don't.
5331 emsg_silent_def = emsg_silent;
5332 did_emsg_def = 0;
5333
5334 ectx.ec_where.wt_index = 0;
5335 ectx.ec_where.wt_variable = FALSE;
5336
5337 // Execute the instructions until done.
5338 ret = exec_instructions(&ectx);
5339 if (ret == OK)
5340 {
5341 // function finished, get result from the stack.
5342 if (ufunc->uf_ret_type == &t_void)
5343 {
5344 rettv->v_type = VAR_VOID;
5345 }
5346 else
5347 {
5348 tv = STACK_TV_BOT(-1);
5349 *rettv = *tv;
5350 tv->v_type = VAR_UNKNOWN;
5351 }
5352 }
5353
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01005354 // When failed need to unwind the call stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005355 while (ectx.ec_frame_idx != ectx.ec_initial_frame_idx)
5356 func_return(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02005357
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005358 // Deal with any remaining closures, they may be in use somewhere.
5359 if (ectx.ec_funcrefs.ga_len > 0)
Bram Moolenaarf112f302020-12-20 17:47:52 +01005360 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005361 handle_closure_in_use(&ectx, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00005362 ga_clear(&ectx.ec_funcrefs);
Bram Moolenaarf112f302020-12-20 17:47:52 +01005363 }
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005364
Bram Moolenaaree8580e2020-08-28 17:19:07 +02005365 estack_pop();
5366 current_sctx = save_current_sctx;
5367
Bram Moolenaar2336c372021-12-06 15:06:54 +00005368 if (--ufunc->uf_calls <= 0 && ufunc->uf_refcount <= 0)
5369 // Function was unreferenced while being used, free it now.
5370 func_clear_free(ufunc, FALSE);
Bram Moolenaarc970e422021-03-17 15:03:04 +01005371
Bram Moolenaar352134b2020-10-17 22:04:08 +02005372 if (*msg_list != NULL && saved_msg_list != NULL)
5373 {
5374 msglist_T **plist = saved_msg_list;
5375
5376 // Append entries from the current msg_list (uncaught exceptions) to
5377 // the saved msg_list.
5378 while (*plist != NULL)
5379 plist = &(*plist)->next;
5380
5381 *plist = *msg_list;
5382 }
5383 msg_list = saved_msg_list;
5384
Bram Moolenaar4c137212021-04-19 16:48:48 +02005385 if (ectx.ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar02194d22020-10-24 23:08:38 +02005386 {
5387 cmdmod.cmod_filter_regmatch.regprog = NULL;
5388 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005389 cmdmod = ectx.ec_funclocal.floc_save_cmdmod;
Bram Moolenaar02194d22020-10-24 23:08:38 +02005390 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +01005391 emsg_silent_def = save_emsg_silent_def;
5392 did_emsg_def += save_did_emsg_def;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02005393
Bram Moolenaaree8580e2020-08-28 17:19:07 +02005394failed_early:
Bram Moolenaar0d1f55c2022-04-05 17:30:29 +01005395 // Free all arguments and local variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005396 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
5397 clear_tv(STACK_TV(idx));
Bram Moolenaare99d4222021-06-13 14:01:26 +02005398 ex_nesting_level = orig_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02005399
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005400 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01005401 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005402 if (ectx.ec_outer_ref != NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +01005403 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005404 if (ectx.ec_outer_ref->or_outer_allocated)
5405 vim_free(ectx.ec_outer_ref->or_outer);
5406 partial_unref(ectx.ec_outer_ref->or_partial);
5407 vim_free(ectx.ec_outer_ref);
Bram Moolenaar0186e582021-01-10 18:33:11 +01005408 }
5409
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02005410 // Not sure if this is necessary.
5411 suppress_errthrow = save_suppress_errthrow;
5412
Bram Moolenaarb5841b92021-07-15 18:09:53 +02005413 if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before
5414 && !need_rethrow)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005415 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02005416 printable_func_name(ufunc));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01005417 funcdepth_restore(orig_funcdepth);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005418 return ret;
5419}
5420
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005421/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02005422 * List instructions "instr" up to "instr_count" or until ISN_FINISH.
5423 * "ufunc" has the source lines, NULL for the instructions of ISN_SUBSTITUTE.
5424 * "pfx" is prefixed to every line.
5425 */
5426 static void
5427list_instructions(char *pfx, isn_T *instr, int instr_count, ufunc_T *ufunc)
5428{
5429 int line_idx = 0;
5430 int prev_current = 0;
5431 int current;
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02005432 int def_arg_idx = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005433
5434 for (current = 0; current < instr_count; ++current)
5435 {
5436 isn_T *iptr = &instr[current];
5437 char *line;
5438
5439 if (ufunc != NULL)
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02005440 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005441 while (line_idx < iptr->isn_lnum
5442 && line_idx < ufunc->uf_lines.ga_len)
5443 {
5444 if (current > prev_current)
5445 {
5446 msg_puts("\n\n");
5447 prev_current = current;
5448 }
5449 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
5450 if (line != NULL)
5451 msg(line);
5452 }
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02005453 if (iptr->isn_type == ISN_JUMP_IF_ARG_SET)
5454 {
5455 int first_def_arg = ufunc->uf_args.ga_len
5456 - ufunc->uf_def_args.ga_len;
5457
5458 if (def_arg_idx > 0)
5459 msg_puts("\n\n");
5460 msg_start();
5461 msg_puts(" ");
5462 msg_puts(((char **)(ufunc->uf_args.ga_data))[
5463 first_def_arg + def_arg_idx]);
5464 msg_puts(" = ");
5465 msg_puts(((char **)(ufunc->uf_def_args.ga_data))[def_arg_idx++]);
5466 msg_clr_eos();
5467 msg_end();
5468 }
5469 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005470
5471 switch (iptr->isn_type)
5472 {
5473 case ISN_EXEC:
5474 smsg("%s%4d EXEC %s", pfx, current, iptr->isn_arg.string);
5475 break;
Bram Moolenaar20677332021-06-06 17:02:53 +02005476 case ISN_EXEC_SPLIT:
5477 smsg("%s%4d EXEC_SPLIT %s", pfx, current, iptr->isn_arg.string);
5478 break;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00005479 case ISN_EXECRANGE:
5480 smsg("%s%4d EXECRANGE %s", pfx, current, iptr->isn_arg.string);
5481 break;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005482 case ISN_LEGACY_EVAL:
5483 smsg("%s%4d EVAL legacy %s", pfx, current,
5484 iptr->isn_arg.string);
5485 break;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02005486 case ISN_REDIRSTART:
5487 smsg("%s%4d REDIR", pfx, current);
5488 break;
5489 case ISN_REDIREND:
5490 smsg("%s%4d REDIR END%s", pfx, current,
5491 iptr->isn_arg.number ? " append" : "");
5492 break;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005493 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005494#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005495 smsg("%s%4d CEXPR pre %s", pfx, current,
5496 cexpr_get_auname(iptr->isn_arg.number));
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005497#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005498 break;
5499 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005500#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005501 {
5502 cexprref_T *cer = iptr->isn_arg.cexpr.cexpr_ref;
5503
5504 smsg("%s%4d CEXPR core %s%s \"%s\"", pfx, current,
5505 cexpr_get_auname(cer->cer_cmdidx),
5506 cer->cer_forceit ? "!" : "",
5507 cer->cer_cmdline);
5508 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005509#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005510 break;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005511 case ISN_INSTR:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01005512 smsg("%s%4d INSTR", pfx, current);
5513 list_instructions(" ", iptr->isn_arg.instr, INT_MAX, NULL);
5514 msg(" -------------");
5515 break;
5516 case ISN_SOURCE:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005517 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01005518 scriptitem_T *si = SCRIPT_ITEM(iptr->isn_arg.number);
5519
5520 smsg("%s%4d SOURCE %s", pfx, current, si->sn_name);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005521 }
5522 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005523 case ISN_SUBSTITUTE:
5524 {
5525 subs_T *subs = &iptr->isn_arg.subs;
5526
5527 smsg("%s%4d SUBSTITUTE %s", pfx, current, subs->subs_cmd);
5528 list_instructions(" ", subs->subs_instr, INT_MAX, NULL);
5529 msg(" -------------");
5530 }
5531 break;
5532 case ISN_EXECCONCAT:
5533 smsg("%s%4d EXECCONCAT %lld", pfx, current,
5534 (varnumber_T)iptr->isn_arg.number);
5535 break;
5536 case ISN_ECHO:
5537 {
5538 echo_T *echo = &iptr->isn_arg.echo;
5539
5540 smsg("%s%4d %s %d", pfx, current,
5541 echo->echo_with_white ? "ECHO" : "ECHON",
5542 echo->echo_count);
5543 }
5544 break;
5545 case ISN_EXECUTE:
5546 smsg("%s%4d EXECUTE %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02005547 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005548 break;
5549 case ISN_ECHOMSG:
5550 smsg("%s%4d ECHOMSG %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02005551 (varnumber_T)(iptr->isn_arg.number));
5552 break;
5553 case ISN_ECHOCONSOLE:
5554 smsg("%s%4d ECHOCONSOLE %lld", pfx, current,
5555 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005556 break;
5557 case ISN_ECHOERR:
5558 smsg("%s%4d ECHOERR %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02005559 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005560 break;
5561 case ISN_LOAD:
5562 {
5563 if (iptr->isn_arg.number < 0)
5564 smsg("%s%4d LOAD arg[%lld]", pfx, current,
5565 (varnumber_T)(iptr->isn_arg.number
5566 + STACK_FRAME_SIZE));
5567 else
5568 smsg("%s%4d LOAD $%lld", pfx, current,
5569 (varnumber_T)(iptr->isn_arg.number));
5570 }
5571 break;
5572 case ISN_LOADOUTER:
5573 {
5574 if (iptr->isn_arg.number < 0)
5575 smsg("%s%4d LOADOUTER level %d arg[%d]", pfx, current,
5576 iptr->isn_arg.outer.outer_depth,
5577 iptr->isn_arg.outer.outer_idx
5578 + STACK_FRAME_SIZE);
5579 else
5580 smsg("%s%4d LOADOUTER level %d $%d", pfx, current,
5581 iptr->isn_arg.outer.outer_depth,
5582 iptr->isn_arg.outer.outer_idx);
5583 }
5584 break;
5585 case ISN_LOADV:
5586 smsg("%s%4d LOADV v:%s", pfx, current,
5587 get_vim_var_name(iptr->isn_arg.number));
5588 break;
5589 case ISN_LOADSCRIPT:
5590 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005591 scriptref_T *sref = iptr->isn_arg.script.scriptref;
5592 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
5593 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005594
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005595 sv = get_script_svar(sref, -1);
5596 if (sv == NULL)
5597 smsg("%s%4d LOADSCRIPT [deleted] from %s",
5598 pfx, current, si->sn_name);
5599 else
5600 smsg("%s%4d LOADSCRIPT %s-%d from %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02005601 sv->sv_name,
5602 sref->sref_idx,
5603 si->sn_name);
5604 }
5605 break;
5606 case ISN_LOADS:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01005607 case ISN_LOADEXPORT:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005608 {
5609 scriptitem_T *si = SCRIPT_ITEM(
5610 iptr->isn_arg.loadstore.ls_sid);
5611
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01005612 smsg("%s%4d %s s:%s from %s", pfx, current,
5613 iptr->isn_type == ISN_LOADS ? "LOADS"
5614 : "LOADEXPORT",
5615 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005616 }
5617 break;
5618 case ISN_LOADAUTO:
5619 smsg("%s%4d LOADAUTO %s", pfx, current, iptr->isn_arg.string);
5620 break;
5621 case ISN_LOADG:
5622 smsg("%s%4d LOADG g:%s", pfx, current, iptr->isn_arg.string);
5623 break;
5624 case ISN_LOADB:
5625 smsg("%s%4d LOADB b:%s", pfx, current, iptr->isn_arg.string);
5626 break;
5627 case ISN_LOADW:
5628 smsg("%s%4d LOADW w:%s", pfx, current, iptr->isn_arg.string);
5629 break;
5630 case ISN_LOADT:
5631 smsg("%s%4d LOADT t:%s", pfx, current, iptr->isn_arg.string);
5632 break;
5633 case ISN_LOADGDICT:
5634 smsg("%s%4d LOAD g:", pfx, current);
5635 break;
5636 case ISN_LOADBDICT:
5637 smsg("%s%4d LOAD b:", pfx, current);
5638 break;
5639 case ISN_LOADWDICT:
5640 smsg("%s%4d LOAD w:", pfx, current);
5641 break;
5642 case ISN_LOADTDICT:
5643 smsg("%s%4d LOAD t:", pfx, current);
5644 break;
5645 case ISN_LOADOPT:
5646 smsg("%s%4d LOADOPT %s", pfx, current, iptr->isn_arg.string);
5647 break;
5648 case ISN_LOADENV:
5649 smsg("%s%4d LOADENV %s", pfx, current, iptr->isn_arg.string);
5650 break;
5651 case ISN_LOADREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005652 smsg("%s%4d LOADREG @%c", pfx, current,
5653 (int)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005654 break;
5655
5656 case ISN_STORE:
5657 if (iptr->isn_arg.number < 0)
5658 smsg("%s%4d STORE arg[%lld]", pfx, current,
5659 iptr->isn_arg.number + STACK_FRAME_SIZE);
5660 else
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005661 smsg("%s%4d STORE $%lld", pfx, current,
5662 iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005663 break;
5664 case ISN_STOREOUTER:
5665 {
5666 if (iptr->isn_arg.number < 0)
5667 smsg("%s%4d STOREOUTEr level %d arg[%d]", pfx, current,
5668 iptr->isn_arg.outer.outer_depth,
5669 iptr->isn_arg.outer.outer_idx + STACK_FRAME_SIZE);
5670 else
5671 smsg("%s%4d STOREOUTER level %d $%d", pfx, current,
5672 iptr->isn_arg.outer.outer_depth,
5673 iptr->isn_arg.outer.outer_idx);
5674 }
5675 break;
5676 case ISN_STOREV:
5677 smsg("%s%4d STOREV v:%s", pfx, current,
5678 get_vim_var_name(iptr->isn_arg.number));
5679 break;
5680 case ISN_STOREAUTO:
5681 smsg("%s%4d STOREAUTO %s", pfx, current, iptr->isn_arg.string);
5682 break;
5683 case ISN_STOREG:
5684 smsg("%s%4d STOREG %s", pfx, current, iptr->isn_arg.string);
5685 break;
5686 case ISN_STOREB:
5687 smsg("%s%4d STOREB %s", pfx, current, iptr->isn_arg.string);
5688 break;
5689 case ISN_STOREW:
5690 smsg("%s%4d STOREW %s", pfx, current, iptr->isn_arg.string);
5691 break;
5692 case ISN_STORET:
5693 smsg("%s%4d STORET %s", pfx, current, iptr->isn_arg.string);
5694 break;
5695 case ISN_STORES:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01005696 case ISN_STOREEXPORT:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005697 {
5698 scriptitem_T *si = SCRIPT_ITEM(
5699 iptr->isn_arg.loadstore.ls_sid);
5700
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01005701 smsg("%s%4d %s %s in %s", pfx, current,
5702 iptr->isn_type == ISN_STORES
5703 ? "STORES" : "STOREEXPORT",
Bram Moolenaar4c137212021-04-19 16:48:48 +02005704 iptr->isn_arg.loadstore.ls_name, si->sn_name);
5705 }
5706 break;
5707 case ISN_STORESCRIPT:
5708 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005709 scriptref_T *sref = iptr->isn_arg.script.scriptref;
5710 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
5711 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005712
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005713 sv = get_script_svar(sref, -1);
5714 if (sv == NULL)
5715 smsg("%s%4d STORESCRIPT [deleted] in %s",
5716 pfx, current, si->sn_name);
5717 else
5718 smsg("%s%4d STORESCRIPT %s-%d in %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02005719 sv->sv_name,
5720 sref->sref_idx,
5721 si->sn_name);
5722 }
5723 break;
5724 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00005725 case ISN_STOREFUNCOPT:
5726 smsg("%s%4d %s &%s", pfx, current,
5727 iptr->isn_type == ISN_STOREOPT ? "STOREOPT" : "STOREFUNCOPT",
Bram Moolenaar4c137212021-04-19 16:48:48 +02005728 iptr->isn_arg.storeopt.so_name);
5729 break;
5730 case ISN_STOREENV:
5731 smsg("%s%4d STOREENV $%s", pfx, current, iptr->isn_arg.string);
5732 break;
5733 case ISN_STOREREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005734 smsg("%s%4d STOREREG @%c", pfx, current,
5735 (int)iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005736 break;
5737 case ISN_STORENR:
5738 smsg("%s%4d STORE %lld in $%d", pfx, current,
5739 iptr->isn_arg.storenr.stnr_val,
5740 iptr->isn_arg.storenr.stnr_idx);
5741 break;
5742
5743 case ISN_STOREINDEX:
5744 smsg("%s%4d STOREINDEX %s", pfx, current,
5745 vartype_name(iptr->isn_arg.vartype));
5746 break;
5747
5748 case ISN_STORERANGE:
5749 smsg("%s%4d STORERANGE", pfx, current);
5750 break;
5751
5752 // constants
5753 case ISN_PUSHNR:
5754 smsg("%s%4d PUSHNR %lld", pfx, current,
5755 (varnumber_T)(iptr->isn_arg.number));
5756 break;
5757 case ISN_PUSHBOOL:
5758 case ISN_PUSHSPEC:
5759 smsg("%s%4d PUSH %s", pfx, current,
5760 get_var_special_name(iptr->isn_arg.number));
5761 break;
5762 case ISN_PUSHF:
5763#ifdef FEAT_FLOAT
5764 smsg("%s%4d PUSHF %g", pfx, current, iptr->isn_arg.fnumber);
5765#endif
5766 break;
5767 case ISN_PUSHS:
5768 smsg("%s%4d PUSHS \"%s\"", pfx, current, iptr->isn_arg.string);
5769 break;
5770 case ISN_PUSHBLOB:
5771 {
5772 char_u *r;
5773 char_u numbuf[NUMBUFLEN];
5774 char_u *tofree;
5775
5776 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
5777 smsg("%s%4d PUSHBLOB %s", pfx, current, r);
5778 vim_free(tofree);
5779 }
5780 break;
5781 case ISN_PUSHFUNC:
5782 {
5783 char *name = (char *)iptr->isn_arg.string;
5784
5785 smsg("%s%4d PUSHFUNC \"%s\"", pfx, current,
5786 name == NULL ? "[none]" : name);
5787 }
5788 break;
5789 case ISN_PUSHCHANNEL:
5790#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar397a87a2022-03-20 21:14:15 +00005791 smsg("%s%4d PUSHCHANNEL 0", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005792#endif
5793 break;
5794 case ISN_PUSHJOB:
5795#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar397a87a2022-03-20 21:14:15 +00005796 smsg("%s%4d PUSHJOB \"no process\"", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005797#endif
5798 break;
5799 case ISN_PUSHEXC:
5800 smsg("%s%4d PUSH v:exception", pfx, current);
5801 break;
Bram Moolenaar06b77222022-01-25 15:51:56 +00005802 case ISN_AUTOLOAD:
5803 smsg("%s%4d AUTOLOAD %s", pfx, current, iptr->isn_arg.string);
5804 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005805 case ISN_UNLET:
5806 smsg("%s%4d UNLET%s %s", pfx, current,
5807 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
5808 iptr->isn_arg.unlet.ul_name);
5809 break;
5810 case ISN_UNLETENV:
5811 smsg("%s%4d UNLETENV%s $%s", pfx, current,
5812 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
5813 iptr->isn_arg.unlet.ul_name);
5814 break;
5815 case ISN_UNLETINDEX:
5816 smsg("%s%4d UNLETINDEX", pfx, current);
5817 break;
5818 case ISN_UNLETRANGE:
5819 smsg("%s%4d UNLETRANGE", pfx, current);
5820 break;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02005821 case ISN_LOCKUNLOCK:
5822 smsg("%s%4d LOCKUNLOCK %s", pfx, current, iptr->isn_arg.string);
5823 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005824 case ISN_LOCKCONST:
5825 smsg("%s%4d LOCKCONST", pfx, current);
5826 break;
5827 case ISN_NEWLIST:
5828 smsg("%s%4d NEWLIST size %lld", pfx, current,
5829 (varnumber_T)(iptr->isn_arg.number));
5830 break;
5831 case ISN_NEWDICT:
5832 smsg("%s%4d NEWDICT size %lld", pfx, current,
5833 (varnumber_T)(iptr->isn_arg.number));
5834 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00005835 case ISN_NEWPARTIAL:
5836 smsg("%s%4d NEWPARTIAL", pfx, current);
5837 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005838
5839 // function call
5840 case ISN_BCALL:
5841 {
5842 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
5843
5844 smsg("%s%4d BCALL %s(argc %d)", pfx, current,
5845 internal_func_name(cbfunc->cbf_idx),
5846 cbfunc->cbf_argcount);
5847 }
5848 break;
5849 case ISN_DCALL:
5850 {
5851 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
5852 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
5853 + cdfunc->cdf_idx;
5854
5855 smsg("%s%4d DCALL %s(argc %d)", pfx, current,
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005856 printable_func_name(df->df_ufunc),
5857 cdfunc->cdf_argcount);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005858 }
5859 break;
5860 case ISN_UCALL:
5861 {
5862 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
5863
5864 smsg("%s%4d UCALL %s(argc %d)", pfx, current,
5865 cufunc->cuf_name, cufunc->cuf_argcount);
5866 }
5867 break;
5868 case ISN_PCALL:
5869 {
5870 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
5871
5872 smsg("%s%4d PCALL%s (argc %d)", pfx, current,
5873 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
5874 }
5875 break;
5876 case ISN_PCALL_END:
5877 smsg("%s%4d PCALL end", pfx, current);
5878 break;
5879 case ISN_RETURN:
5880 smsg("%s%4d RETURN", pfx, current);
5881 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02005882 case ISN_RETURN_VOID:
5883 smsg("%s%4d RETURN void", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005884 break;
5885 case ISN_FUNCREF:
5886 {
5887 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar38453522021-11-28 22:00:12 +00005888 char_u *name;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005889
Bram Moolenaar38453522021-11-28 22:00:12 +00005890 if (funcref->fr_func_name == NULL)
5891 {
5892 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
5893 + funcref->fr_dfunc_idx;
5894 name = df->df_ufunc->uf_name;
5895 }
5896 else
5897 name = funcref->fr_func_name;
5898 smsg("%s%4d FUNCREF %s", pfx, current, name);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005899 }
5900 break;
5901
5902 case ISN_NEWFUNC:
5903 {
5904 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
5905
5906 smsg("%s%4d NEWFUNC %s %s", pfx, current,
5907 newfunc->nf_lambda, newfunc->nf_global);
5908 }
5909 break;
5910
5911 case ISN_DEF:
5912 {
5913 char_u *name = iptr->isn_arg.string;
5914
5915 smsg("%s%4d DEF %s", pfx, current,
5916 name == NULL ? (char_u *)"" : name);
5917 }
5918 break;
5919
5920 case ISN_JUMP:
5921 {
5922 char *when = "?";
5923
5924 switch (iptr->isn_arg.jump.jump_when)
5925 {
5926 case JUMP_ALWAYS:
5927 when = "JUMP";
5928 break;
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005929 case JUMP_NEVER:
5930 iemsg("JUMP_NEVER should not be used");
5931 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005932 case JUMP_AND_KEEP_IF_TRUE:
5933 when = "JUMP_AND_KEEP_IF_TRUE";
5934 break;
5935 case JUMP_IF_FALSE:
5936 when = "JUMP_IF_FALSE";
5937 break;
5938 case JUMP_AND_KEEP_IF_FALSE:
5939 when = "JUMP_AND_KEEP_IF_FALSE";
5940 break;
5941 case JUMP_IF_COND_FALSE:
5942 when = "JUMP_IF_COND_FALSE";
5943 break;
5944 case JUMP_IF_COND_TRUE:
5945 when = "JUMP_IF_COND_TRUE";
5946 break;
5947 }
5948 smsg("%s%4d %s -> %d", pfx, current, when,
5949 iptr->isn_arg.jump.jump_where);
5950 }
5951 break;
5952
5953 case ISN_JUMP_IF_ARG_SET:
5954 smsg("%s%4d JUMP_IF_ARG_SET arg[%d] -> %d", pfx, current,
5955 iptr->isn_arg.jumparg.jump_arg_off + STACK_FRAME_SIZE,
5956 iptr->isn_arg.jump.jump_where);
5957 break;
5958
5959 case ISN_FOR:
5960 {
5961 forloop_T *forloop = &iptr->isn_arg.forloop;
5962
5963 smsg("%s%4d FOR $%d -> %d", pfx, current,
5964 forloop->for_idx, forloop->for_end);
5965 }
5966 break;
5967
5968 case ISN_TRY:
5969 {
Bram Moolenaar0d807102021-12-21 09:42:09 +00005970 try_T *try = &iptr->isn_arg.tryref;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005971
5972 if (try->try_ref->try_finally == 0)
5973 smsg("%s%4d TRY catch -> %d, endtry -> %d",
5974 pfx, current,
5975 try->try_ref->try_catch,
5976 try->try_ref->try_endtry);
5977 else
5978 smsg("%s%4d TRY catch -> %d, finally -> %d, endtry -> %d",
5979 pfx, current,
5980 try->try_ref->try_catch,
5981 try->try_ref->try_finally,
5982 try->try_ref->try_endtry);
5983 }
5984 break;
5985 case ISN_CATCH:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005986 smsg("%s%4d CATCH", pfx, current);
5987 break;
5988 case ISN_TRYCONT:
5989 {
5990 trycont_T *trycont = &iptr->isn_arg.trycont;
5991
5992 smsg("%s%4d TRY-CONTINUE %d level%s -> %d", pfx, current,
5993 trycont->tct_levels,
5994 trycont->tct_levels == 1 ? "" : "s",
5995 trycont->tct_where);
5996 }
5997 break;
5998 case ISN_FINALLY:
5999 smsg("%s%4d FINALLY", pfx, current);
6000 break;
6001 case ISN_ENDTRY:
6002 smsg("%s%4d ENDTRY", pfx, current);
6003 break;
6004 case ISN_THROW:
6005 smsg("%s%4d THROW", pfx, current);
6006 break;
6007
6008 // expression operations on number
6009 case ISN_OPNR:
6010 case ISN_OPFLOAT:
6011 case ISN_OPANY:
6012 {
6013 char *what;
6014 char *ins;
6015
6016 switch (iptr->isn_arg.op.op_type)
6017 {
6018 case EXPR_MULT: what = "*"; break;
6019 case EXPR_DIV: what = "/"; break;
6020 case EXPR_REM: what = "%"; break;
6021 case EXPR_SUB: what = "-"; break;
6022 case EXPR_ADD: what = "+"; break;
6023 default: what = "???"; break;
6024 }
6025 switch (iptr->isn_type)
6026 {
6027 case ISN_OPNR: ins = "OPNR"; break;
6028 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
6029 case ISN_OPANY: ins = "OPANY"; break;
6030 default: ins = "???"; break;
6031 }
6032 smsg("%s%4d %s %s", pfx, current, ins, what);
6033 }
6034 break;
6035
6036 case ISN_COMPAREBOOL:
6037 case ISN_COMPARESPECIAL:
Bram Moolenaar7a222242022-03-01 19:23:24 +00006038 case ISN_COMPARENULL:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006039 case ISN_COMPARENR:
6040 case ISN_COMPAREFLOAT:
6041 case ISN_COMPARESTRING:
6042 case ISN_COMPAREBLOB:
6043 case ISN_COMPARELIST:
6044 case ISN_COMPAREDICT:
6045 case ISN_COMPAREFUNC:
6046 case ISN_COMPAREANY:
6047 {
6048 char *p;
6049 char buf[10];
6050 char *type;
6051
6052 switch (iptr->isn_arg.op.op_type)
6053 {
6054 case EXPR_EQUAL: p = "=="; break;
6055 case EXPR_NEQUAL: p = "!="; break;
6056 case EXPR_GREATER: p = ">"; break;
6057 case EXPR_GEQUAL: p = ">="; break;
6058 case EXPR_SMALLER: p = "<"; break;
6059 case EXPR_SEQUAL: p = "<="; break;
6060 case EXPR_MATCH: p = "=~"; break;
6061 case EXPR_IS: p = "is"; break;
6062 case EXPR_ISNOT: p = "isnot"; break;
6063 case EXPR_NOMATCH: p = "!~"; break;
6064 default: p = "???"; break;
6065 }
6066 STRCPY(buf, p);
6067 if (iptr->isn_arg.op.op_ic == TRUE)
6068 strcat(buf, "?");
6069 switch(iptr->isn_type)
6070 {
6071 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
6072 case ISN_COMPARESPECIAL:
6073 type = "COMPARESPECIAL"; break;
Bram Moolenaar7a222242022-03-01 19:23:24 +00006074 case ISN_COMPARENULL: type = "COMPARENULL"; break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006075 case ISN_COMPARENR: type = "COMPARENR"; break;
6076 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
6077 case ISN_COMPARESTRING:
6078 type = "COMPARESTRING"; break;
6079 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
6080 case ISN_COMPARELIST: type = "COMPARELIST"; break;
6081 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
6082 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
6083 case ISN_COMPAREANY: type = "COMPAREANY"; break;
6084 default: type = "???"; break;
6085 }
6086
6087 smsg("%s%4d %s %s", pfx, current, type, buf);
6088 }
6089 break;
6090
6091 case ISN_ADDLIST: smsg("%s%4d ADDLIST", pfx, current); break;
6092 case ISN_ADDBLOB: smsg("%s%4d ADDBLOB", pfx, current); break;
6093
6094 // expression operations
LemonBoy372bcce2022-04-25 12:43:20 +01006095 case ISN_CONCAT:
6096 smsg("%s%4d CONCAT size %lld", pfx, current,
6097 (varnumber_T)(iptr->isn_arg.number));
6098 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006099 case ISN_STRINDEX: smsg("%s%4d STRINDEX", pfx, current); break;
6100 case ISN_STRSLICE: smsg("%s%4d STRSLICE", pfx, current); break;
6101 case ISN_BLOBINDEX: smsg("%s%4d BLOBINDEX", pfx, current); break;
6102 case ISN_BLOBSLICE: smsg("%s%4d BLOBSLICE", pfx, current); break;
6103 case ISN_LISTAPPEND: smsg("%s%4d LISTAPPEND", pfx, current); break;
6104 case ISN_BLOBAPPEND: smsg("%s%4d BLOBAPPEND", pfx, current); break;
6105 case ISN_LISTINDEX: smsg("%s%4d LISTINDEX", pfx, current); break;
6106 case ISN_LISTSLICE: smsg("%s%4d LISTSLICE", pfx, current); break;
6107 case ISN_ANYINDEX: smsg("%s%4d ANYINDEX", pfx, current); break;
6108 case ISN_ANYSLICE: smsg("%s%4d ANYSLICE", pfx, current); break;
6109 case ISN_SLICE: smsg("%s%4d SLICE %lld",
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02006110 pfx, current, iptr->isn_arg.number); break;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02006111 case ISN_GETITEM: smsg("%s%4d ITEM %lld%s", pfx, current,
6112 iptr->isn_arg.getitem.gi_index,
6113 iptr->isn_arg.getitem.gi_with_op ?
6114 " with op" : ""); break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006115 case ISN_MEMBER: smsg("%s%4d MEMBER", pfx, current); break;
6116 case ISN_STRINGMEMBER: smsg("%s%4d MEMBER %s", pfx, current,
6117 iptr->isn_arg.string); break;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02006118 case ISN_CLEARDICT: smsg("%s%4d CLEARDICT", pfx, current); break;
6119 case ISN_USEDICT: smsg("%s%4d USEDICT", pfx, current); break;
6120
Bram Moolenaar4c137212021-04-19 16:48:48 +02006121 case ISN_NEGATENR: smsg("%s%4d NEGATENR", pfx, current); break;
6122
Bram Moolenaar4c137212021-04-19 16:48:48 +02006123 case ISN_CHECKTYPE:
6124 {
6125 checktype_T *ct = &iptr->isn_arg.type;
6126 char *tofree;
6127
6128 if (ct->ct_arg_idx == 0)
6129 smsg("%s%4d CHECKTYPE %s stack[%d]", pfx, current,
6130 type_name(ct->ct_type, &tofree),
6131 (int)ct->ct_off);
6132 else
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02006133 smsg("%s%4d CHECKTYPE %s stack[%d] arg %d",
6134 pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02006135 type_name(ct->ct_type, &tofree),
6136 (int)ct->ct_off,
6137 (int)ct->ct_arg_idx);
6138 vim_free(tofree);
6139 break;
6140 }
6141 case ISN_CHECKLEN: smsg("%s%4d CHECKLEN %s%d", pfx, current,
6142 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
6143 iptr->isn_arg.checklen.cl_min_len);
6144 break;
6145 case ISN_SETTYPE:
6146 {
6147 char *tofree;
6148
6149 smsg("%s%4d SETTYPE %s", pfx, current,
6150 type_name(iptr->isn_arg.type.ct_type, &tofree));
6151 vim_free(tofree);
6152 break;
6153 }
6154 case ISN_COND2BOOL: smsg("%s%4d COND2BOOL", pfx, current); break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006155 case ISN_2BOOL: if (iptr->isn_arg.tobool.invert)
6156 smsg("%s%4d INVERT %d (!val)", pfx, current,
6157 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006158 else
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006159 smsg("%s%4d 2BOOL %d (!!val)", pfx, current,
6160 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006161 break;
6162 case ISN_2STRING: smsg("%s%4d 2STRING stack[%lld]", pfx, current,
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006163 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006164 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006165 case ISN_2STRING_ANY: smsg("%s%4d 2STRING_ANY stack[%lld]",
6166 pfx, current,
6167 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006168 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006169 case ISN_RANGE: smsg("%s%4d RANGE %s", pfx, current,
6170 iptr->isn_arg.string);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006171 break;
6172 case ISN_PUT:
6173 if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE)
6174 smsg("%s%4d PUT %c above range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006175 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006176 else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE)
6177 smsg("%s%4d PUT %c range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006178 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006179 else
6180 smsg("%s%4d PUT %c %ld", pfx, current,
6181 iptr->isn_arg.put.put_regname,
6182 (long)iptr->isn_arg.put.put_lnum);
6183 break;
6184
Bram Moolenaar4c137212021-04-19 16:48:48 +02006185 case ISN_CMDMOD:
6186 {
6187 char_u *buf;
6188 size_t len = produce_cmdmods(
6189 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
6190
6191 buf = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02006192 if (likely(buf != NULL))
Bram Moolenaar4c137212021-04-19 16:48:48 +02006193 {
6194 (void)produce_cmdmods(
6195 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
6196 smsg("%s%4d CMDMOD %s", pfx, current, buf);
6197 vim_free(buf);
6198 }
6199 break;
6200 }
6201 case ISN_CMDMOD_REV: smsg("%s%4d CMDMOD_REV", pfx, current); break;
6202
6203 case ISN_PROF_START:
Bram Moolenaare99d4222021-06-13 14:01:26 +02006204 smsg("%s%4d PROFILE START line %d", pfx, current,
6205 iptr->isn_lnum);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006206 break;
6207
6208 case ISN_PROF_END:
6209 smsg("%s%4d PROFILE END", pfx, current);
6210 break;
6211
Bram Moolenaare99d4222021-06-13 14:01:26 +02006212 case ISN_DEBUG:
Bram Moolenaar8cec9272021-06-23 20:20:53 +02006213 smsg("%s%4d DEBUG line %d-%d varcount %lld", pfx, current,
6214 iptr->isn_arg.debug.dbg_break_lnum + 1,
6215 iptr->isn_lnum,
6216 iptr->isn_arg.debug.dbg_var_names_len);
Bram Moolenaare99d4222021-06-13 14:01:26 +02006217 break;
6218
Bram Moolenaar4c137212021-04-19 16:48:48 +02006219 case ISN_UNPACK: smsg("%s%4d UNPACK %d%s", pfx, current,
6220 iptr->isn_arg.unpack.unp_count,
6221 iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : "");
6222 break;
6223 case ISN_SHUFFLE: smsg("%s%4d SHUFFLE %d up %d", pfx, current,
6224 iptr->isn_arg.shuffle.shfl_item,
6225 iptr->isn_arg.shuffle.shfl_up);
6226 break;
6227 case ISN_DROP: smsg("%s%4d DROP", pfx, current); break;
6228
6229 case ISN_FINISH: // End of list of instructions for ISN_SUBSTITUTE.
6230 return;
6231 }
6232
6233 out_flush(); // output one line at a time
6234 ui_breakcheck();
6235 if (got_int)
6236 break;
6237 }
6238}
6239
6240/*
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02006241 * Handle command line completion for the :disassemble command.
6242 */
6243 void
6244set_context_in_disassemble_cmd(expand_T *xp, char_u *arg)
6245{
6246 char_u *p;
6247
6248 // Default: expand user functions, "debug" and "profile"
6249 xp->xp_context = EXPAND_DISASSEMBLE;
6250 xp->xp_pattern = arg;
6251
6252 // first argument already typed: only user function names
6253 if (*arg != NUL && *(p = skiptowhite(arg)) != NUL)
6254 {
6255 xp->xp_context = EXPAND_USER_FUNC;
6256 xp->xp_pattern = skipwhite(p);
6257 }
6258}
6259
6260/*
6261 * Function given to ExpandGeneric() to obtain the list of :disassemble
6262 * arguments.
6263 */
6264 char_u *
6265get_disassemble_argument(expand_T *xp, int idx)
6266{
6267 if (idx == 0)
6268 return (char_u *)"debug";
6269 if (idx == 1)
6270 return (char_u *)"profile";
6271 return get_user_func_name(xp, idx - 2);
6272}
6273
6274/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01006275 * ":disassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01006276 * We don't really need this at runtime, but we do have tests that require it,
6277 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006278 */
6279 void
6280ex_disassemble(exarg_T *eap)
6281{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01006282 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01006283 char_u *fname;
6284 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006285 dfunc_T *dfunc;
6286 isn_T *instr;
Bram Moolenaarb2049902021-01-24 12:53:53 +01006287 int instr_count;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02006288 int is_global = FALSE;
Bram Moolenaare99d4222021-06-13 14:01:26 +02006289 compiletype_T compile_type = CT_NONE;
6290
Bram Moolenaarc7d5fc82021-12-05 17:20:24 +00006291 if (STRNCMP(arg, "profile", 7) == 0 && VIM_ISWHITE(arg[7]))
Bram Moolenaare99d4222021-06-13 14:01:26 +02006292 {
6293 compile_type = CT_PROFILE;
6294 arg = skipwhite(arg + 7);
6295 }
Bram Moolenaarc7d5fc82021-12-05 17:20:24 +00006296 else if (STRNCMP(arg, "debug", 5) == 0 && VIM_ISWHITE(arg[5]))
Bram Moolenaare99d4222021-06-13 14:01:26 +02006297 {
6298 compile_type = CT_DEBUG;
6299 arg = skipwhite(arg + 5);
6300 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006301
Bram Moolenaarbfd65582020-07-13 18:18:00 +02006302 if (STRNCMP(arg, "<lambda>", 8) == 0)
6303 {
6304 arg += 8;
6305 (void)getdigits(&arg);
6306 fname = vim_strnsave(eap->arg, arg - eap->arg);
6307 }
6308 else
6309 fname = trans_function_name(&arg, &is_global, FALSE,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01006310 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL, NULL);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01006311 if (fname == NULL)
6312 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006313 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01006314 return;
6315 }
6316
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00006317 ufunc = find_func(fname, is_global);
Bram Moolenaara26b9702020-04-18 19:53:28 +02006318 if (ufunc == NULL)
6319 {
6320 char_u *p = untrans_function_name(fname);
6321
6322 if (p != NULL)
6323 // Try again without making it script-local.
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00006324 ufunc = find_func(p, FALSE);
Bram Moolenaara26b9702020-04-18 19:53:28 +02006325 }
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01006326 vim_free(fname);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006327 if (ufunc == NULL)
6328 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006329 semsg(_(e_cannot_find_function_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006330 return;
6331 }
Bram Moolenaare99d4222021-06-13 14:01:26 +02006332 if (func_needs_compiling(ufunc, compile_type)
6333 && compile_def_function(ufunc, FALSE, compile_type, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02006334 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006335 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006336 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006337 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006338 return;
6339 }
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006340 msg((char *)printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006341
6342 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
Bram Moolenaare99d4222021-06-13 14:01:26 +02006343 switch (compile_type)
6344 {
6345 case CT_PROFILE:
Bram Moolenaarf002a412021-01-24 13:34:18 +01006346#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02006347 instr = dfunc->df_instr_prof;
6348 instr_count = dfunc->df_instr_prof_count;
6349 break;
Bram Moolenaarf002a412021-01-24 13:34:18 +01006350#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02006351 // FALLTHROUGH
6352 case CT_NONE:
6353 instr = dfunc->df_instr;
6354 instr_count = dfunc->df_instr_count;
6355 break;
6356 case CT_DEBUG:
6357 instr = dfunc->df_instr_debug;
6358 instr_count = dfunc->df_instr_debug_count;
6359 break;
6360 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006361
Bram Moolenaar4c137212021-04-19 16:48:48 +02006362 list_instructions("", instr, instr_count, ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006363}
6364
6365/*
Bram Moolenaar13106602020-10-04 16:06:05 +02006366 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006367 * list, etc. Mostly like what JavaScript does, except that empty list and
6368 * empty dictionary are FALSE.
6369 */
6370 int
6371tv2bool(typval_T *tv)
6372{
6373 switch (tv->v_type)
6374 {
6375 case VAR_NUMBER:
6376 return tv->vval.v_number != 0;
6377 case VAR_FLOAT:
6378#ifdef FEAT_FLOAT
6379 return tv->vval.v_float != 0.0;
6380#else
6381 break;
6382#endif
6383 case VAR_PARTIAL:
6384 return tv->vval.v_partial != NULL;
6385 case VAR_FUNC:
6386 case VAR_STRING:
6387 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
6388 case VAR_LIST:
6389 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
6390 case VAR_DICT:
6391 return tv->vval.v_dict != NULL
6392 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
6393 case VAR_BOOL:
6394 case VAR_SPECIAL:
6395 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
6396 case VAR_JOB:
6397#ifdef FEAT_JOB_CHANNEL
6398 return tv->vval.v_job != NULL;
6399#else
6400 break;
6401#endif
6402 case VAR_CHANNEL:
6403#ifdef FEAT_JOB_CHANNEL
6404 return tv->vval.v_channel != NULL;
6405#else
6406 break;
6407#endif
6408 case VAR_BLOB:
6409 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
6410 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006411 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006412 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006413 case VAR_INSTR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006414 break;
6415 }
6416 return FALSE;
6417}
6418
Bram Moolenaarea2d4072020-11-12 12:08:51 +01006419 void
6420emsg_using_string_as(typval_T *tv, int as_number)
6421{
6422 semsg(_(as_number ? e_using_string_as_number_str
6423 : e_using_string_as_bool_str),
6424 tv->vval.v_string == NULL
6425 ? (char_u *)"" : tv->vval.v_string);
6426}
6427
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006428/*
6429 * If "tv" is a string give an error and return FAIL.
6430 */
6431 int
6432check_not_string(typval_T *tv)
6433{
6434 if (tv->v_type == VAR_STRING)
6435 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +01006436 emsg_using_string_as(tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006437 clear_tv(tv);
6438 return FAIL;
6439 }
6440 return OK;
6441}
6442
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006443
6444#endif // FEAT_EVAL