blob: 0e8a1dd951a2b0c2384bc47a5f0de5b168c1fe59 [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/*
Bram Moolenaarfe270812020-04-11 22:31:27 +0200123 * Create a new list from "count" items at the bottom of the stack.
124 * When "count" is zero an empty list is added to the stack.
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100125 * When "count" is -1 a NULL list is added to the stack.
Bram Moolenaarfe270812020-04-11 22:31:27 +0200126 */
127 static int
128exe_newlist(int count, ectx_T *ectx)
129{
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100130 list_T *list = NULL;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200131 int idx;
132 typval_T *tv;
133
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100134 if (count >= 0)
135 {
136 list = list_alloc_with_items(count);
137 if (list == NULL)
138 return FAIL;
139 for (idx = 0; idx < count; ++idx)
140 list_set_item(list, idx, STACK_TV_BOT(idx - count));
141 }
Bram Moolenaarfe270812020-04-11 22:31:27 +0200142
143 if (count > 0)
144 ectx->ec_stack.ga_len -= count - 1;
Bram Moolenaar35578162021-08-02 19:10:38 +0200145 else if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100146 {
147 list_unref(list);
Bram Moolenaarfe270812020-04-11 22:31:27 +0200148 return FAIL;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100149 }
Bram Moolenaarfe270812020-04-11 22:31:27 +0200150 else
151 ++ectx->ec_stack.ga_len;
152 tv = STACK_TV_BOT(-1);
153 tv->v_type = VAR_LIST;
154 tv->vval.v_list = list;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100155 if (list != NULL)
156 ++list->lv_refcount;
157 return OK;
158}
159
160/*
161 * Implementation of ISN_NEWDICT.
162 * Returns FAIL on total failure, MAYBE on error.
163 */
164 static int
165exe_newdict(int count, ectx_T *ectx)
166{
167 dict_T *dict = NULL;
168 dictitem_T *item;
169 char_u *key;
170 int idx;
171 typval_T *tv;
172
173 if (count >= 0)
174 {
175 dict = dict_alloc();
176 if (unlikely(dict == NULL))
177 return FAIL;
178 for (idx = 0; idx < count; ++idx)
179 {
180 // have already checked key type is VAR_STRING
181 tv = STACK_TV_BOT(2 * (idx - count));
182 // check key is unique
183 key = tv->vval.v_string == NULL
184 ? (char_u *)"" : tv->vval.v_string;
185 item = dict_find(dict, key, -1);
186 if (item != NULL)
187 {
188 semsg(_(e_duplicate_key_in_dicitonary), key);
189 dict_unref(dict);
190 return MAYBE;
191 }
192 item = dictitem_alloc(key);
193 clear_tv(tv);
194 if (unlikely(item == NULL))
195 {
196 dict_unref(dict);
197 return FAIL;
198 }
Bram Moolenaar0d1f55c2022-04-05 17:30:29 +0100199 tv = STACK_TV_BOT(2 * (idx - count) + 1);
200 item->di_tv = *tv;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100201 item->di_tv.v_lock = 0;
Bram Moolenaar0d1f55c2022-04-05 17:30:29 +0100202 tv->v_type = VAR_UNKNOWN;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100203 if (dict_add(dict, item) == FAIL)
204 {
205 // can this ever happen?
206 dict_unref(dict);
207 return FAIL;
208 }
209 }
210 }
211
212 if (count > 0)
213 ectx->ec_stack.ga_len -= 2 * count - 1;
214 else if (GA_GROW_FAILS(&ectx->ec_stack, 1))
215 return FAIL;
216 else
217 ++ectx->ec_stack.ga_len;
218 tv = STACK_TV_BOT(-1);
219 tv->v_type = VAR_DICT;
220 tv->v_lock = 0;
221 tv->vval.v_dict = dict;
222 if (dict != NULL)
223 ++dict->dv_refcount;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200224 return OK;
225}
226
227/*
Bram Moolenaar4f8f5422021-06-20 19:28:14 +0200228 * If debug_tick changed check if "ufunc" has a breakpoint and update
229 * "uf_has_breakpoint".
230 */
Bram Moolenaar96923b72022-03-15 15:57:04 +0000231 void
Bram Moolenaar4f8f5422021-06-20 19:28:14 +0200232update_has_breakpoint(ufunc_T *ufunc)
233{
234 if (ufunc->uf_debug_tick != debug_tick)
235 {
236 linenr_T breakpoint;
237
238 ufunc->uf_debug_tick = debug_tick;
239 breakpoint = dbg_find_breakpoint(FALSE, ufunc->uf_name, 0);
240 ufunc->uf_has_breakpoint = breakpoint > 0;
241 }
242}
243
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +0200244static garray_T dict_stack = GA_EMPTY;
245
246/*
247 * Put a value on the dict stack. This consumes "tv".
248 */
249 static int
250dict_stack_save(typval_T *tv)
251{
252 if (dict_stack.ga_growsize == 0)
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000253 ga_init2(&dict_stack, sizeof(typval_T), 10);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +0200254 if (ga_grow(&dict_stack, 1) == FAIL)
255 return FAIL;
256 ((typval_T *)dict_stack.ga_data)[dict_stack.ga_len] = *tv;
257 ++dict_stack.ga_len;
258 return OK;
259}
260
261/*
262 * Get the typval at top of the dict stack.
263 */
264 static typval_T *
265dict_stack_get_tv(void)
266{
267 if (dict_stack.ga_len == 0)
268 return NULL;
269 return ((typval_T *)dict_stack.ga_data) + dict_stack.ga_len - 1;
270}
271
272/*
273 * Get the dict at top of the dict stack.
274 */
275 static dict_T *
276dict_stack_get_dict(void)
277{
278 typval_T *tv;
279
280 if (dict_stack.ga_len == 0)
281 return NULL;
282 tv = ((typval_T *)dict_stack.ga_data) + dict_stack.ga_len - 1;
283 if (tv->v_type == VAR_DICT)
284 return tv->vval.v_dict;
285 return NULL;
286}
287
288/*
289 * Drop an item from the dict stack.
290 */
291 static void
292dict_stack_drop(void)
293{
294 if (dict_stack.ga_len == 0)
295 {
296 iemsg("Dict stack underflow");
297 return;
298 }
299 --dict_stack.ga_len;
300 clear_tv(((typval_T *)dict_stack.ga_data) + dict_stack.ga_len);
301}
302
303/*
304 * Drop items from the dict stack until the length is equal to "len".
305 */
306 static void
307dict_stack_clear(int len)
308{
309 while (dict_stack.ga_len > len)
310 dict_stack_drop();
311}
312
Bram Moolenaar4f8f5422021-06-20 19:28:14 +0200313/*
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +0000314 * Get a pointer to useful "pt_outer" of "pt".
315 */
316 static outer_T *
317get_pt_outer(partial_T *pt)
318{
319 partial_T *ptref = pt->pt_outer_partial;
320
321 if (ptref == NULL)
322 return &pt->pt_outer;
323
324 // partial using partial (recursively)
325 while (ptref->pt_outer_partial != NULL)
326 ptref = ptref->pt_outer_partial;
327 return &ptref->pt_outer;
328}
329
330/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100331 * Call compiled function "cdf_idx" from compiled code.
Bram Moolenaarab360522021-01-10 14:02:28 +0100332 * This adds a stack frame and sets the instruction pointer to the start of the
333 * called function.
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200334 * If "pt" is not null use "pt->pt_outer" for ec_outer_ref->or_outer.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100335 *
336 * Stack has:
337 * - current arguments (already there)
338 * - omitted optional argument (default values) added here
339 * - stack frame:
340 * - pointer to calling function
341 * - Index of next instruction in calling function
342 * - previous frame pointer
343 * - reserved space for local variables
344 */
345 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100346call_dfunc(
347 int cdf_idx,
348 partial_T *pt,
349 int argcount_arg,
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100350 ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100351{
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100352 int argcount = argcount_arg;
353 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + cdf_idx;
354 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200355 int did_emsg_before = did_emsg_cumul + did_emsg;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100356 int arg_to_add;
357 int vararg_count = 0;
358 int varcount;
359 int idx;
360 estack_T *entry;
361 funclocal_T *floc = NULL;
Bram Moolenaar648594e2021-07-11 17:55:01 +0200362 int res = OK;
Bram Moolenaar139575d2022-03-15 19:29:30 +0000363 compiletype_T compile_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100364
365 if (dfunc->df_deleted)
366 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100367 // don't use ufunc->uf_name, it may have been freed
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000368 emsg_funcname(e_function_was_deleted_str,
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100369 dfunc->df_name == NULL ? (char_u *)"unknown" : dfunc->df_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100370 return FAIL;
371 }
372
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100373#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +0100374 if (do_profiling == PROF_YES)
375 {
Bram Moolenaar35578162021-08-02 19:10:38 +0200376 if (GA_GROW_OK(&profile_info_ga, 1))
Bram Moolenaar12d26532021-02-19 19:13:21 +0100377 {
378 profinfo_T *info = ((profinfo_T *)profile_info_ga.ga_data)
379 + profile_info_ga.ga_len;
380 ++profile_info_ga.ga_len;
381 CLEAR_POINTER(info);
382 profile_may_start_func(info, ufunc,
383 (((dfunc_T *)def_functions.ga_data)
384 + ectx->ec_dfunc_idx)->df_ufunc);
385 }
Bram Moolenaar12d26532021-02-19 19:13:21 +0100386 }
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100387#endif
388
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200389 // When debugging and using "cont" switches to the not-debugged
390 // instructions, may need to still compile them.
Bram Moolenaar139575d2022-03-15 19:29:30 +0000391 compile_type = get_compile_type(ufunc);
392 if (func_needs_compiling(ufunc, compile_type))
Bram Moolenaar648594e2021-07-11 17:55:01 +0200393 {
Bram Moolenaar139575d2022-03-15 19:29:30 +0000394 res = compile_def_function(ufunc, FALSE, compile_type, NULL);
Bram Moolenaar648594e2021-07-11 17:55:01 +0200395
396 // compile_def_function() may cause def_functions.ga_data to change
397 dfunc = ((dfunc_T *)def_functions.ga_data) + cdf_idx;
398 }
399 if (res == FAIL || INSTRUCTIONS(dfunc) == NULL)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200400 {
401 if (did_emsg_cumul + did_emsg == did_emsg_before)
402 semsg(_(e_function_is_not_compiled_str),
403 printable_func_name(ufunc));
404 return FAIL;
405 }
406
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200407 if (ufunc->uf_va_name != NULL)
408 {
Bram Moolenaarfe270812020-04-11 22:31:27 +0200409 // Need to make a list out of the vararg arguments.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200410 // Stack at time of call with 2 varargs:
411 // normal_arg
412 // optional_arg
413 // vararg_1
414 // vararg_2
Bram Moolenaarfe270812020-04-11 22:31:27 +0200415 // After creating the list:
416 // normal_arg
417 // optional_arg
418 // vararg-list
419 // With missing optional arguments we get:
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200420 // normal_arg
Bram Moolenaarfe270812020-04-11 22:31:27 +0200421 // After creating the list
422 // normal_arg
423 // (space for optional_arg)
424 // vararg-list
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200425 vararg_count = argcount - ufunc->uf_args.ga_len;
426 if (vararg_count < 0)
427 vararg_count = 0;
428 else
429 argcount -= vararg_count;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200430 if (exe_newlist(vararg_count, ectx) == FAIL)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200431 return FAIL;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200432
433 vararg_count = 1;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200434 }
435
Bram Moolenaarfe270812020-04-11 22:31:27 +0200436 arg_to_add = ufunc->uf_args.ga_len - argcount;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200437 if (arg_to_add < 0)
438 {
Bram Moolenaar79e8db92020-08-14 22:16:33 +0200439 if (arg_to_add == -1)
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200440 emsg(_(e_one_argument_too_many));
Bram Moolenaar79e8db92020-08-14 22:16:33 +0200441 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200442 semsg(_(e_nr_arguments_too_many), -arg_to_add);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200443 return FAIL;
444 }
Bram Moolenaarcd1cda22022-02-16 21:48:25 +0000445 else if (arg_to_add > ufunc->uf_def_args.ga_len)
446 {
447 int missing = arg_to_add - ufunc->uf_def_args.ga_len;
448
449 if (missing == 1)
450 emsg(_(e_one_argument_too_few));
451 else
452 semsg(_(e_nr_arguments_too_few), missing);
453 return FAIL;
454 }
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200455
456 // Reserve space for:
457 // - missing arguments
458 // - stack frame
459 // - local variables
460 // - if needed: a counter for number of closures created in
461 // ectx->ec_funcrefs.
462 varcount = dfunc->df_varcount + dfunc->df_has_closure;
Bram Moolenaar35578162021-08-02 19:10:38 +0200463 if (GA_GROW_FAILS(&ectx->ec_stack, arg_to_add + STACK_FRAME_SIZE + varcount))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100464 return FAIL;
465
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100466 // If depth of calling is getting too high, don't execute the function.
467 if (funcdepth_increment() == FAIL)
468 return FAIL;
Bram Moolenaare99d4222021-06-13 14:01:26 +0200469 ++ex_nesting_level;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100470
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100471 // Only make a copy of funclocal if it contains something to restore.
Bram Moolenaar4c137212021-04-19 16:48:48 +0200472 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100473 {
474 floc = ALLOC_ONE(funclocal_T);
475 if (floc == NULL)
476 return FAIL;
Bram Moolenaar4c137212021-04-19 16:48:48 +0200477 *floc = ectx->ec_funclocal;
478 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100479 }
480
Bram Moolenaarfe270812020-04-11 22:31:27 +0200481 // Move the vararg-list to below the missing optional arguments.
482 if (vararg_count > 0 && arg_to_add > 0)
483 *STACK_TV_BOT(arg_to_add - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100484
485 // Reserve space for omitted optional arguments, filled in soon.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200486 for (idx = 0; idx < arg_to_add; ++idx)
Bram Moolenaarfe270812020-04-11 22:31:27 +0200487 STACK_TV_BOT(idx - vararg_count)->v_type = VAR_UNKNOWN;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200488 ectx->ec_stack.ga_len += arg_to_add;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100489
490 // Store current execution state in stack frame for ISN_RETURN.
Bram Moolenaar0186e582021-01-10 18:33:11 +0100491 STACK_TV_BOT(STACK_FRAME_FUNC_OFF)->vval.v_number = ectx->ec_dfunc_idx;
492 STACK_TV_BOT(STACK_FRAME_IIDX_OFF)->vval.v_number = ectx->ec_iidx;
Bram Moolenaar5930ddc2021-04-26 20:32:59 +0200493 STACK_TV_BOT(STACK_FRAME_INSTR_OFF)->vval.v_string = (void *)ectx->ec_instr;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200494 STACK_TV_BOT(STACK_FRAME_OUTER_OFF)->vval.v_string =
495 (void *)ectx->ec_outer_ref;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100496 STACK_TV_BOT(STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string = (void *)floc;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100497 STACK_TV_BOT(STACK_FRAME_IDX_OFF)->vval.v_number = ectx->ec_frame_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200498 ectx->ec_frame_idx = ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100499
500 // Initialize local variables
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200501 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaar5cd64792021-12-25 18:23:24 +0000502 {
503 typval_T *tv = STACK_TV_BOT(STACK_FRAME_SIZE + idx);
504
505 tv->v_type = VAR_NUMBER;
506 tv->vval.v_number = 0;
507 }
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200508 if (dfunc->df_has_closure)
509 {
510 typval_T *tv = STACK_TV_BOT(STACK_FRAME_SIZE + dfunc->df_varcount);
511
512 tv->v_type = VAR_NUMBER;
513 tv->vval.v_number = 0;
514 }
515 ectx->ec_stack.ga_len += STACK_FRAME_SIZE + varcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100516
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +0100517 if (pt != NULL || ufunc->uf_partial != NULL
518 || (ufunc->uf_flags & FC_CLOSURE))
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100519 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200520 outer_ref_T *ref = ALLOC_CLEAR_ONE(outer_ref_T);
Bram Moolenaar0186e582021-01-10 18:33:11 +0100521
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200522 if (ref == NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +0100523 return FAIL;
524 if (pt != NULL)
525 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +0000526 ref->or_outer = get_pt_outer(pt);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200527 ++pt->pt_refcount;
528 ref->or_partial = pt;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100529 }
530 else if (ufunc->uf_partial != NULL)
531 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +0000532 ref->or_outer = get_pt_outer(ufunc->uf_partial);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200533 ++ufunc->uf_partial->pt_refcount;
534 ref->or_partial = ufunc->uf_partial;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100535 }
536 else
537 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200538 ref->or_outer = ALLOC_CLEAR_ONE(outer_T);
Dominique Pelle5a9e5842021-07-24 19:32:12 +0200539 if (unlikely(ref->or_outer == NULL))
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200540 {
541 vim_free(ref);
542 return FAIL;
543 }
544 ref->or_outer_allocated = TRUE;
545 ref->or_outer->out_stack = &ectx->ec_stack;
546 ref->or_outer->out_frame_idx = ectx->ec_frame_idx;
547 if (ectx->ec_outer_ref != NULL)
548 ref->or_outer->out_up = ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100549 }
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200550 ectx->ec_outer_ref = ref;
Bram Moolenaarab360522021-01-10 14:02:28 +0100551 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100552 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200553 ectx->ec_outer_ref = NULL;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100554
Bram Moolenaarc970e422021-03-17 15:03:04 +0100555 ++ufunc->uf_calls;
556
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100557 // Set execution state to the start of the called function.
558 ectx->ec_dfunc_idx = cdf_idx;
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100559 ectx->ec_instr = INSTRUCTIONS(dfunc);
Bram Moolenaarb2049902021-01-24 12:53:53 +0100560 entry = estack_push_ufunc(ufunc, 1);
Bram Moolenaarc620c052020-07-08 15:16:19 +0200561 if (entry != NULL)
562 {
563 // Set the script context to the script where the function was defined.
Bram Moolenaarc70fe462021-04-17 17:59:19 +0200564 // Save the current context so it can be restored on return.
565 entry->es_save_sctx = current_sctx;
566 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200567 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100568
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +0200569 // Start execution at the first instruction.
570 ectx->ec_iidx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100571
572 return OK;
573}
574
575// Get pointer to item in the stack.
576#define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
577
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000578// Double linked list of funcstack_T in use.
579static funcstack_T *first_funcstack = NULL;
580
581 static void
582add_funcstack_to_list(funcstack_T *funcstack)
583{
584 // Link in list of funcstacks.
585 if (first_funcstack != NULL)
586 first_funcstack->fs_prev = funcstack;
587 funcstack->fs_next = first_funcstack;
588 funcstack->fs_prev = NULL;
589 first_funcstack = funcstack;
590}
591
592 static void
593remove_funcstack_from_list(funcstack_T *funcstack)
594{
595 if (funcstack->fs_prev == NULL)
596 first_funcstack = funcstack->fs_next;
597 else
598 funcstack->fs_prev->fs_next = funcstack->fs_next;
599 if (funcstack->fs_next != NULL)
600 funcstack->fs_next->fs_prev = funcstack->fs_prev;
601}
602
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100603/*
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200604 * Used when returning from a function: Check if any closure is still
605 * referenced. If so then move the arguments and variables to a separate piece
606 * of stack to be used when the closure is called.
607 * When "free_arguments" is TRUE the arguments are to be freed.
608 * Returns FAIL when out of memory.
609 */
610 static int
611handle_closure_in_use(ectx_T *ectx, int free_arguments)
612{
613 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
614 + ectx->ec_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200615 int argcount;
616 int top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200617 int idx;
618 typval_T *tv;
619 int closure_in_use = FALSE;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200620 garray_T *gap = &ectx->ec_funcrefs;
621 varnumber_T closure_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200622
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200623 if (dfunc->df_ufunc == NULL)
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200624 return OK; // function was freed
625 if (dfunc->df_has_closure == 0)
626 return OK; // no closures
627 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + dfunc->df_varcount);
628 closure_count = tv->vval.v_number;
629 if (closure_count == 0)
630 return OK; // no funcrefs created
631
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200632 argcount = ufunc_argcount(dfunc->df_ufunc);
633 top = ectx->ec_frame_idx - argcount;
634
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200635 // Check if any created closure is still in use.
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200636 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200637 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200638 partial_T *pt;
639 int off = gap->ga_len - closure_count + idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200640
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200641 if (off < 0)
642 continue; // count is off or already done
643 pt = ((partial_T **)gap->ga_data)[off];
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200644 if (pt->pt_refcount > 1)
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200645 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200646 int refcount = pt->pt_refcount;
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200647 int i;
648
Dominique Pelleaf4a61a2021-12-27 17:21:41 +0000649 // A Reference in a local variable doesn't count, it gets
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200650 // unreferenced on return.
651 for (i = 0; i < dfunc->df_varcount; ++i)
652 {
653 typval_T *stv = STACK_TV(ectx->ec_frame_idx
654 + STACK_FRAME_SIZE + i);
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200655 if (stv->v_type == VAR_PARTIAL && pt == stv->vval.v_partial)
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200656 --refcount;
657 }
658 if (refcount > 1)
659 {
660 closure_in_use = TRUE;
661 break;
662 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200663 }
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200664 }
665
666 if (closure_in_use)
667 {
668 funcstack_T *funcstack = ALLOC_CLEAR_ONE(funcstack_T);
669 typval_T *stack;
670
671 // A closure is using the arguments and/or local variables.
672 // Move them to the called function.
673 if (funcstack == NULL)
674 return FAIL;
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000675
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200676 funcstack->fs_var_offset = argcount + STACK_FRAME_SIZE;
677 funcstack->fs_ga.ga_len = funcstack->fs_var_offset + dfunc->df_varcount;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200678 stack = ALLOC_CLEAR_MULT(typval_T, funcstack->fs_ga.ga_len);
679 funcstack->fs_ga.ga_data = stack;
680 if (stack == NULL)
681 {
682 vim_free(funcstack);
683 return FAIL;
684 }
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000685 add_funcstack_to_list(funcstack);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200686
687 // Move or copy the arguments.
688 for (idx = 0; idx < argcount; ++idx)
689 {
690 tv = STACK_TV(top + idx);
691 if (free_arguments)
692 {
693 *(stack + idx) = *tv;
694 tv->v_type = VAR_UNKNOWN;
695 }
696 else
697 copy_tv(tv, stack + idx);
698 }
699 // Move the local variables.
700 for (idx = 0; idx < dfunc->df_varcount; ++idx)
701 {
702 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + idx);
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200703
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200704 // A partial created for a local function, that is also used as a
705 // local variable, has a reference count for the variable, thus
706 // will never go down to zero. When all these refcounts are one
707 // then the funcstack is unused. We need to count how many we have
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000708 // so we know when to check.
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200709 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL)
710 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200711 int i;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200712
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200713 for (i = 0; i < closure_count; ++i)
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200714 if (tv->vval.v_partial == ((partial_T **)gap->ga_data)[
715 gap->ga_len - closure_count + i])
716 ++funcstack->fs_min_refcount;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200717 }
718
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200719 *(stack + funcstack->fs_var_offset + idx) = *tv;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200720 tv->v_type = VAR_UNKNOWN;
721 }
722
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200723 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200724 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200725 partial_T *pt = ((partial_T **)gap->ga_data)[gap->ga_len
726 - closure_count + idx];
727 if (pt->pt_refcount > 1)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200728 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200729 ++funcstack->fs_refcount;
730 pt->pt_funcstack = funcstack;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100731 pt->pt_outer.out_stack = &funcstack->fs_ga;
732 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx - top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200733 }
734 }
735 }
736
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200737 for (idx = 0; idx < closure_count; ++idx)
738 partial_unref(((partial_T **)gap->ga_data)[gap->ga_len
739 - closure_count + idx]);
740 gap->ga_len -= closure_count;
741 if (gap->ga_len == 0)
742 ga_clear(gap);
743
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200744 return OK;
745}
746
747/*
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200748 * Called when a partial is freed or its reference count goes down to one. The
749 * funcstack may be the only reference to the partials in the local variables.
750 * Go over all of them, the funcref and can be freed if all partials
751 * referencing the funcstack have a reference count of one.
752 */
753 void
754funcstack_check_refcount(funcstack_T *funcstack)
755{
756 int i;
757 garray_T *gap = &funcstack->fs_ga;
758 int done = 0;
759
760 if (funcstack->fs_refcount > funcstack->fs_min_refcount)
761 return;
762 for (i = funcstack->fs_var_offset; i < gap->ga_len; ++i)
763 {
764 typval_T *tv = ((typval_T *)gap->ga_data) + i;
765
766 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL
767 && tv->vval.v_partial->pt_funcstack == funcstack
768 && tv->vval.v_partial->pt_refcount == 1)
769 ++done;
770 }
771 if (done == funcstack->fs_min_refcount)
772 {
773 typval_T *stack = gap->ga_data;
774
775 // All partials referencing the funcstack have a reference count of
776 // one, thus the funcstack is no longer of use.
777 for (i = 0; i < gap->ga_len; ++i)
778 clear_tv(stack + i);
779 vim_free(stack);
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000780 remove_funcstack_from_list(funcstack);
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200781 vim_free(funcstack);
782 }
783}
784
785/*
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000786 * For garbage collecting: set references in all variables referenced by
787 * all funcstacks.
788 */
789 int
790set_ref_in_funcstacks(int copyID)
791{
792 funcstack_T *funcstack;
793
794 for (funcstack = first_funcstack; funcstack != NULL;
795 funcstack = funcstack->fs_next)
796 {
797 typval_T *stack = funcstack->fs_ga.ga_data;
798 int i;
799
800 for (i = 0; i < funcstack->fs_ga.ga_len; ++i)
801 if (set_ref_in_item(stack + i, copyID, NULL, NULL))
802 return TRUE; // abort
803 }
804 return FALSE;
805}
806
807/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100808 * Return from the current function.
809 */
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200810 static int
Bram Moolenaar4c137212021-04-19 16:48:48 +0200811func_return(ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100812{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100813 int idx;
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100814 int ret_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200815 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
816 + ectx->ec_dfunc_idx;
817 int argcount = ufunc_argcount(dfunc->df_ufunc);
818 int top = ectx->ec_frame_idx - argcount;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200819 estack_T *entry;
Bram Moolenaar12d26532021-02-19 19:13:21 +0100820 int prev_dfunc_idx = STACK_TV(ectx->ec_frame_idx
821 + STACK_FRAME_FUNC_OFF)->vval.v_number;
Bram Moolenaarb06b50d2021-04-26 21:39:25 +0200822 funclocal_T *floc;
823#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +0100824 dfunc_T *prev_dfunc = ((dfunc_T *)def_functions.ga_data)
825 + prev_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100826
Bram Moolenaar12d26532021-02-19 19:13:21 +0100827 if (do_profiling == PROF_YES)
828 {
829 ufunc_T *caller = prev_dfunc->df_ufunc;
830
831 if (dfunc->df_ufunc->uf_profiling
832 || (caller != NULL && caller->uf_profiling))
833 {
834 profile_may_end_func(((profinfo_T *)profile_info_ga.ga_data)
835 + profile_info_ga.ga_len - 1, dfunc->df_ufunc, caller);
836 --profile_info_ga.ga_len;
837 }
838 }
839#endif
Bram Moolenaar919c12c2021-12-14 14:29:16 +0000840
841 // No check for uf_refcount being zero, cannot think of a way that would
842 // happen.
Bram Moolenaarc970e422021-03-17 15:03:04 +0100843 --dfunc->df_ufunc->uf_calls;
844
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100845 // execution context goes one level up
Bram Moolenaarc620c052020-07-08 15:16:19 +0200846 entry = estack_pop();
847 if (entry != NULL)
Bram Moolenaarc70fe462021-04-17 17:59:19 +0200848 current_sctx = entry->es_save_sctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100849
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200850 if (handle_closure_in_use(ectx, TRUE) == FAIL)
851 return FAIL;
852
853 // Clear the arguments.
854 for (idx = top; idx < ectx->ec_frame_idx; ++idx)
855 clear_tv(STACK_TV(idx));
856
857 // Clear local variables and temp values, but not the return value.
858 for (idx = ectx->ec_frame_idx + STACK_FRAME_SIZE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100859 idx < ectx->ec_stack.ga_len - 1; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100860 clear_tv(STACK_TV(idx));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100861
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100862 // The return value should be on top of the stack. However, when aborting
863 // it may not be there and ec_frame_idx is the top of the stack.
864 ret_idx = ectx->ec_stack.ga_len - 1;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100865 if (ret_idx == ectx->ec_frame_idx + STACK_FRAME_IDX_OFF)
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100866 ret_idx = 0;
867
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200868 if (ectx->ec_outer_ref != NULL)
869 {
870 if (ectx->ec_outer_ref->or_outer_allocated)
871 vim_free(ectx->ec_outer_ref->or_outer);
872 partial_unref(ectx->ec_outer_ref->or_partial);
873 vim_free(ectx->ec_outer_ref);
874 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100875
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100876 // Restore the previous frame.
Bram Moolenaar12d26532021-02-19 19:13:21 +0100877 ectx->ec_dfunc_idx = prev_dfunc_idx;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100878 ectx->ec_iidx = STACK_TV(ectx->ec_frame_idx
879 + STACK_FRAME_IIDX_OFF)->vval.v_number;
Bram Moolenaar5930ddc2021-04-26 20:32:59 +0200880 ectx->ec_instr = (void *)STACK_TV(ectx->ec_frame_idx
881 + STACK_FRAME_INSTR_OFF)->vval.v_string;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200882 ectx->ec_outer_ref = (void *)STACK_TV(ectx->ec_frame_idx
Bram Moolenaar0186e582021-01-10 18:33:11 +0100883 + STACK_FRAME_OUTER_OFF)->vval.v_string;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100884 floc = (void *)STACK_TV(ectx->ec_frame_idx
885 + STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string;
Bram Moolenaar5366e1a2020-10-01 13:01:34 +0200886 // restoring ec_frame_idx must be last
Bram Moolenaar0186e582021-01-10 18:33:11 +0100887 ectx->ec_frame_idx = STACK_TV(ectx->ec_frame_idx
888 + STACK_FRAME_IDX_OFF)->vval.v_number;
Bram Moolenaard386e922021-04-25 14:48:49 +0200889
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100890 if (floc == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +0200891 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100892 else
893 {
Bram Moolenaar4c137212021-04-19 16:48:48 +0200894 ectx->ec_funclocal = *floc;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100895 vim_free(floc);
896 }
897
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100898 if (ret_idx > 0)
899 {
900 // Reset the stack to the position before the call, with a spot for the
901 // return value, moved there from above the frame.
902 ectx->ec_stack.ga_len = top + 1;
903 *STACK_TV_BOT(-1) = *STACK_TV(ret_idx);
904 }
905 else
906 // Reset the stack to the position before the call.
907 ectx->ec_stack.ga_len = top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200908
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100909 funcdepth_decrement();
Bram Moolenaare99d4222021-06-13 14:01:26 +0200910 --ex_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200911 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100912}
913
914#undef STACK_TV
915
916/*
917 * Prepare arguments and rettv for calling a builtin or user function.
918 */
919 static int
920call_prepare(int argcount, typval_T *argvars, ectx_T *ectx)
921{
922 int idx;
923 typval_T *tv;
924
925 // Move arguments from bottom of the stack to argvars[] and add terminator.
926 for (idx = 0; idx < argcount; ++idx)
927 argvars[idx] = *STACK_TV_BOT(idx - argcount);
928 argvars[argcount].v_type = VAR_UNKNOWN;
929
930 // Result replaces the arguments on the stack.
931 if (argcount > 0)
932 ectx->ec_stack.ga_len -= argcount - 1;
Bram Moolenaar35578162021-08-02 19:10:38 +0200933 else if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100934 return FAIL;
935 else
936 ++ectx->ec_stack.ga_len;
937
938 // Default return value is zero.
939 tv = STACK_TV_BOT(-1);
940 tv->v_type = VAR_NUMBER;
941 tv->vval.v_number = 0;
Bram Moolenaar24565cf2022-03-28 18:16:52 +0100942 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100943
944 return OK;
945}
946
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200947// Ugly global to avoid passing the execution context around through many
948// layers.
949static ectx_T *current_ectx = NULL;
950
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100951/*
952 * Call a builtin function by index.
953 */
954 static int
955call_bfunc(int func_idx, int argcount, ectx_T *ectx)
956{
957 typval_T argvars[MAX_FUNC_ARGS];
958 int idx;
Bram Moolenaar171fb922020-10-28 16:54:47 +0100959 int did_emsg_before = did_emsg;
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200960 ectx_T *prev_ectx = current_ectx;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +0200961 char *save_func_name = ectx->ec_where.wt_func_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100962
963 if (call_prepare(argcount, argvars, ectx) == FAIL)
964 return FAIL;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +0200965 ectx->ec_where.wt_func_name = internal_func_name(func_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100966
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200967 // Call the builtin function. Set "current_ectx" so that when it
968 // recursively invokes call_def_function() a closure context can be set.
969 current_ectx = ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100970 call_internal_func_by_idx(func_idx, argvars, STACK_TV_BOT(-1));
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200971 current_ectx = prev_ectx;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +0200972 ectx->ec_where.wt_func_name = save_func_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100973
974 // Clear the arguments.
975 for (idx = 0; idx < argcount; ++idx)
976 clear_tv(&argvars[idx]);
Bram Moolenaar015f4262020-05-05 21:25:22 +0200977
Bram Moolenaar57f799e2020-12-12 20:42:19 +0100978 if (did_emsg > did_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200979 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100980 return OK;
981}
982
983/*
984 * Execute a user defined function.
Bram Moolenaarab360522021-01-10 14:02:28 +0100985 * If the function is compiled this will add a stack frame and set the
986 * instruction pointer at the start of the function.
987 * Otherwise the function is called here.
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200988 * If "pt" is not null use "pt->pt_outer" for ec_outer_ref->or_outer.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100989 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100990 */
991 static int
Bram Moolenaar0186e582021-01-10 18:33:11 +0100992call_ufunc(
993 ufunc_T *ufunc,
994 partial_T *pt,
995 int argcount,
996 ectx_T *ectx,
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +0200997 isn_T *iptr,
998 dict_T *selfdict)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100999{
1000 typval_T argvars[MAX_FUNC_ARGS];
1001 funcexe_T funcexe;
1002 int error;
1003 int idx;
Bram Moolenaar28ee8922020-10-28 20:20:00 +01001004 int did_emsg_before = did_emsg;
Bram Moolenaar139575d2022-03-15 19:29:30 +00001005 compiletype_T compile_type = get_compile_type(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001006
Bram Moolenaare99d4222021-06-13 14:01:26 +02001007 if (func_needs_compiling(ufunc, compile_type)
1008 && compile_def_function(ufunc, FALSE, compile_type, NULL)
1009 == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02001010 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001011 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001012 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +01001013 error = check_user_func_argcount(ufunc, argcount);
Bram Moolenaar50824712020-12-20 21:10:17 +01001014 if (error != FCERR_UNKNOWN)
1015 {
1016 if (error == FCERR_TOOMANY)
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001017 semsg(_(e_too_many_arguments_for_function_str),
1018 printable_func_name(ufunc));
Bram Moolenaar50824712020-12-20 21:10:17 +01001019 else
Bram Moolenaare1242042021-12-16 20:56:57 +00001020 semsg(_(e_not_enough_arguments_for_function_str),
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001021 printable_func_name(ufunc));
Bram Moolenaar50824712020-12-20 21:10:17 +01001022 return FAIL;
1023 }
1024
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001025 // The function has been compiled, can call it quickly. For a function
1026 // that was defined later: we can call it directly next time.
1027 if (iptr != NULL)
1028 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001029 delete_instr(iptr);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001030 iptr->isn_type = ISN_DCALL;
1031 iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1032 iptr->isn_arg.dfunc.cdf_argcount = argcount;
1033 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001034 return call_dfunc(ufunc->uf_dfunc_idx, pt, argcount, ectx);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001035 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001036
1037 if (call_prepare(argcount, argvars, ectx) == FAIL)
1038 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +02001039 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00001040 funcexe.fe_evaluate = TRUE;
1041 funcexe.fe_selfdict = selfdict != NULL ? selfdict : dict_stack_get_dict();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001042
1043 // Call the user function. Result goes in last position on the stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001044 error = call_user_func_check(ufunc, argcount, argvars,
Bram Moolenaar851f86b2021-12-13 14:26:44 +00001045 STACK_TV_BOT(-1), &funcexe, funcexe.fe_selfdict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001046
1047 // Clear the arguments.
1048 for (idx = 0; idx < argcount; ++idx)
1049 clear_tv(&argvars[idx]);
1050
1051 if (error != FCERR_NONE)
1052 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001053 user_func_error(error, printable_func_name(ufunc), &funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001054 return FAIL;
1055 }
Bram Moolenaar28ee8922020-10-28 20:20:00 +01001056 if (did_emsg > did_emsg_before)
Bram Moolenaared677f52020-08-12 16:38:10 +02001057 // Error other than from calling the function itself.
1058 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001059 return OK;
1060}
1061
1062/*
Bram Moolenaara91a7132021-03-25 21:12:15 +01001063 * If command modifiers were applied restore them.
1064 */
1065 static void
1066may_restore_cmdmod(funclocal_T *funclocal)
1067{
1068 if (funclocal->floc_restore_cmdmod)
1069 {
1070 cmdmod.cmod_filter_regmatch.regprog = NULL;
1071 undo_cmdmod(&cmdmod);
1072 cmdmod = funclocal->floc_save_cmdmod;
1073 funclocal->floc_restore_cmdmod = FALSE;
1074 }
1075}
1076
1077/*
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001078 * Return TRUE if an error was given (not caught in try/catch) or CTRL-C was
1079 * pressed.
Bram Moolenaara1773442020-08-12 15:21:22 +02001080 */
1081 static int
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001082vim9_aborting(int prev_uncaught_emsg)
Bram Moolenaara1773442020-08-12 15:21:22 +02001083{
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001084 return uncaught_emsg > prev_uncaught_emsg || got_int || did_throw;
Bram Moolenaara1773442020-08-12 15:21:22 +02001085}
1086
1087/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001088 * Execute a function by "name".
1089 * This can be a builtin function or a user function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001090 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001091 * Returns FAIL if not found without an error message.
1092 */
1093 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001094call_by_name(
1095 char_u *name,
1096 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001097 ectx_T *ectx,
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001098 isn_T *iptr,
1099 dict_T *selfdict)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001100{
1101 ufunc_T *ufunc;
1102
1103 if (builtin_function(name, -1))
1104 {
1105 int func_idx = find_internal_func(name);
1106
Bram Moolenaar1983f1a2022-02-28 20:55:02 +00001107 if (func_idx < 0) // Impossible?
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001108 return FAIL;
Bram Moolenaar389df252020-07-09 21:20:47 +02001109 if (check_internal_func(func_idx, argcount) < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001110 return FAIL;
1111 return call_bfunc(func_idx, argcount, ectx);
1112 }
1113
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00001114 ufunc = find_func(name, FALSE);
Bram Moolenaara1773442020-08-12 15:21:22 +02001115
1116 if (ufunc == NULL)
1117 {
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001118 int prev_uncaught_emsg = uncaught_emsg;
Bram Moolenaara1773442020-08-12 15:21:22 +02001119
1120 if (script_autoload(name, TRUE))
1121 // loaded a package, search for the function again
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00001122 ufunc = find_func(name, FALSE);
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001123
1124 if (vim9_aborting(prev_uncaught_emsg))
Bram Moolenaara1773442020-08-12 15:21:22 +02001125 return FAIL; // bail out if loading the script caused an error
1126 }
1127
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001128 if (ufunc != NULL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001129 {
Bram Moolenaar6ce46b92021-08-07 15:35:36 +02001130 if (ufunc->uf_arg_types != NULL || ufunc->uf_va_type != NULL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001131 {
1132 int i;
1133 typval_T *argv = STACK_TV_BOT(0) - argcount;
1134
1135 // The function can change at runtime, check that the argument
1136 // types are correct.
1137 for (i = 0; i < argcount; ++i)
1138 {
Bram Moolenaare3ffcd92021-03-08 21:47:13 +01001139 type_T *type = NULL;
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001140
Bram Moolenaar6ce46b92021-08-07 15:35:36 +02001141 if (i < ufunc->uf_args.ga_len && ufunc->uf_arg_types != NULL)
Bram Moolenaare3ffcd92021-03-08 21:47:13 +01001142 type = ufunc->uf_arg_types[i];
1143 else if (ufunc->uf_va_type != NULL)
1144 type = ufunc->uf_va_type->tt_member;
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001145 if (type != NULL && check_typval_arg_type(type,
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001146 &argv[i], NULL, i + 1) == FAIL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001147 return FAIL;
1148 }
1149 }
1150
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001151 return call_ufunc(ufunc, NULL, argcount, ectx, iptr, selfdict);
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001152 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001153
1154 return FAIL;
1155}
1156
1157 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001158call_partial(
1159 typval_T *tv,
1160 int argcount_arg,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001161 ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001162{
Bram Moolenaara90afb92020-07-15 22:38:56 +02001163 int argcount = argcount_arg;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001164 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001165 int called_emsg_before = called_emsg;
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +01001166 int res = FAIL;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001167 dict_T *selfdict = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001168
1169 if (tv->v_type == VAR_PARTIAL)
1170 {
Bram Moolenaara90afb92020-07-15 22:38:56 +02001171 partial_T *pt = tv->vval.v_partial;
1172 int i;
1173
1174 if (pt->pt_argc > 0)
1175 {
1176 // Make space for arguments from the partial, shift the "argcount"
1177 // arguments up.
Bram Moolenaar35578162021-08-02 19:10:38 +02001178 if (GA_GROW_FAILS(&ectx->ec_stack, pt->pt_argc))
Bram Moolenaara90afb92020-07-15 22:38:56 +02001179 return FAIL;
1180 for (i = 1; i <= argcount; ++i)
1181 *STACK_TV_BOT(-i + pt->pt_argc) = *STACK_TV_BOT(-i);
1182 ectx->ec_stack.ga_len += pt->pt_argc;
1183 argcount += pt->pt_argc;
1184
1185 // copy the arguments from the partial onto the stack
1186 for (i = 0; i < pt->pt_argc; ++i)
1187 copy_tv(&pt->pt_argv[i], STACK_TV_BOT(-argcount + i));
1188 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001189 selfdict = pt->pt_dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001190
1191 if (pt->pt_func != NULL)
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001192 return call_ufunc(pt->pt_func, pt, argcount, ectx, NULL, selfdict);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001193
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001194 name = pt->pt_name;
1195 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001196 else if (tv->v_type == VAR_FUNC)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001197 name = tv->vval.v_string;
Bram Moolenaar95006e32020-08-29 17:47:08 +02001198 if (name != NULL)
1199 {
1200 char_u fname_buf[FLEN_FIXED + 1];
1201 char_u *tofree = NULL;
1202 int error = FCERR_NONE;
1203 char_u *fname;
1204
1205 // May need to translate <SNR>123_ to K_SNR.
1206 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
1207 if (error != FCERR_NONE)
1208 res = FAIL;
1209 else
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001210 res = call_by_name(fname, argcount, ectx, NULL, selfdict);
Bram Moolenaar95006e32020-08-29 17:47:08 +02001211 vim_free(tofree);
1212 }
1213
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +01001214 if (res == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001215 {
1216 if (called_emsg == called_emsg_before)
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001217 emsg_funcname(e_unknown_function_str,
Bram Moolenaar015f4262020-05-05 21:25:22 +02001218 name == NULL ? (char_u *)"[unknown]" : name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001219 return FAIL;
1220 }
1221 return OK;
1222}
1223
1224/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001225 * Check if "lock" is VAR_LOCKED or VAR_FIXED. If so give an error and return
1226 * TRUE.
1227 */
1228 static int
1229error_if_locked(int lock, char *error)
1230{
1231 if (lock & (VAR_LOCKED | VAR_FIXED))
1232 {
1233 emsg(_(error));
1234 return TRUE;
1235 }
1236 return FALSE;
1237}
1238
1239/*
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001240 * Give an error if "tv" is not a number and return FAIL.
1241 */
1242 static int
1243check_for_number(typval_T *tv)
1244{
1245 if (tv->v_type != VAR_NUMBER)
1246 {
1247 semsg(_(e_expected_str_but_got_str),
1248 vartype_name(VAR_NUMBER), vartype_name(tv->v_type));
1249 return FAIL;
1250 }
1251 return OK;
1252}
1253
1254/*
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001255 * Store "tv" in variable "name".
1256 * This is for s: and g: variables.
1257 */
1258 static void
1259store_var(char_u *name, typval_T *tv)
1260{
1261 funccal_entry_T entry;
Bram Moolenaard877a572021-04-01 19:42:48 +02001262 int flags = ASSIGN_DECL;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001263
Bram Moolenaard877a572021-04-01 19:42:48 +02001264 if (tv->v_lock)
1265 flags |= ASSIGN_CONST;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001266 save_funccal(&entry);
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001267 set_var_const(name, 0, NULL, tv, FALSE, flags, 0);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001268 restore_funccal();
1269}
1270
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001271/*
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001272 * Convert "tv" to a string.
1273 * Return FAIL if not allowed.
1274 */
1275 static int
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001276do_2string(typval_T *tv, int is_2string_any, int tolerant)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001277{
1278 if (tv->v_type != VAR_STRING)
1279 {
1280 char_u *str;
1281
1282 if (is_2string_any)
1283 {
1284 switch (tv->v_type)
1285 {
1286 case VAR_SPECIAL:
1287 case VAR_BOOL:
1288 case VAR_NUMBER:
1289 case VAR_FLOAT:
1290 case VAR_BLOB: break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001291
1292 case VAR_LIST:
1293 if (tolerant)
1294 {
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001295 char_u *s, *e, *p;
1296 garray_T ga;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001297
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001298 ga_init2(&ga, sizeof(char_u *), 1);
1299
1300 // Convert to NL separated items, then
1301 // escape the items and replace the NL with
1302 // a space.
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001303 str = typval2string(tv, TRUE);
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001304 if (str == NULL)
1305 return FAIL;
1306 s = str;
1307 while ((e = vim_strchr(s, '\n')) != NULL)
1308 {
1309 *e = NUL;
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +01001310 p = vim_strsave_fnameescape(s,
1311 VSE_NONE);
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001312 if (p != NULL)
1313 {
1314 ga_concat(&ga, p);
1315 ga_concat(&ga, (char_u *)" ");
1316 vim_free(p);
1317 }
1318 s = e + 1;
1319 }
1320 vim_free(str);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001321 clear_tv(tv);
1322 tv->v_type = VAR_STRING;
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001323 tv->vval.v_string = ga.ga_data;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001324 return OK;
1325 }
1326 // FALLTHROUGH
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001327 default: to_string_error(tv->v_type);
1328 return FAIL;
1329 }
1330 }
Bram Moolenaar34453202021-01-31 13:08:38 +01001331 str = typval_tostring(tv, TRUE);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001332 clear_tv(tv);
1333 tv->v_type = VAR_STRING;
1334 tv->vval.v_string = str;
1335 }
1336 return OK;
1337}
1338
1339/*
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001340 * When the value of "sv" is a null list of dict, allocate it.
1341 */
1342 static void
Bram Moolenaar859cc212022-03-28 15:22:35 +01001343allocate_if_null(svar_T *sv)
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001344{
Bram Moolenaar859cc212022-03-28 15:22:35 +01001345 typval_T *tv = sv->sv_tv;
1346
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001347 switch (tv->v_type)
1348 {
1349 case VAR_LIST:
Bram Moolenaar859cc212022-03-28 15:22:35 +01001350 if (tv->vval.v_list == NULL && sv->sv_type != &t_list_empty)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001351 (void)rettv_list_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001352 break;
1353 case VAR_DICT:
Bram Moolenaar859cc212022-03-28 15:22:35 +01001354 if (tv->vval.v_dict == NULL && sv->sv_type != &t_dict_empty)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001355 (void)rettv_dict_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001356 break;
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02001357 case VAR_BLOB:
Bram Moolenaar859cc212022-03-28 15:22:35 +01001358 if (tv->vval.v_blob == NULL && sv->sv_type != &t_blob_null)
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02001359 (void)rettv_blob_alloc(tv);
1360 break;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001361 default:
1362 break;
1363 }
1364}
Bram Moolenaard3aac292020-04-19 14:32:17 +02001365
Bram Moolenaare7525c52021-01-09 13:20:37 +01001366/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001367 * Return the character "str[index]" where "index" is the character index,
1368 * including composing characters.
1369 * If "index" is out of range NULL is returned.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001370 */
1371 char_u *
1372char_from_string(char_u *str, varnumber_T index)
1373{
1374 size_t nbyte = 0;
1375 varnumber_T nchar = index;
1376 size_t slen;
1377
1378 if (str == NULL)
1379 return NULL;
1380 slen = STRLEN(str);
1381
Bram Moolenaarff871402021-03-26 13:34:05 +01001382 // Do the same as for a list: a negative index counts from the end.
1383 // Optimization to check the first byte to be below 0x80 (and no composing
1384 // character follows) makes this a lot faster.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001385 if (index < 0)
1386 {
1387 int clen = 0;
1388
1389 for (nbyte = 0; nbyte < slen; ++clen)
Bram Moolenaarff871402021-03-26 13:34:05 +01001390 {
1391 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1392 ++nbyte;
1393 else if (enc_utf8)
1394 nbyte += utfc_ptr2len(str + nbyte);
1395 else
1396 nbyte += mb_ptr2len(str + nbyte);
1397 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001398 nchar = clen + index;
1399 if (nchar < 0)
1400 // unlike list: index out of range results in empty string
1401 return NULL;
1402 }
1403
1404 for (nbyte = 0; nchar > 0 && nbyte < slen; --nchar)
Bram Moolenaarff871402021-03-26 13:34:05 +01001405 {
1406 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1407 ++nbyte;
1408 else if (enc_utf8)
1409 nbyte += utfc_ptr2len(str + nbyte);
1410 else
1411 nbyte += mb_ptr2len(str + nbyte);
1412 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001413 if (nbyte >= slen)
1414 return NULL;
Bram Moolenaar0289a092021-03-14 18:40:19 +01001415 return vim_strnsave(str + nbyte, mb_ptr2len(str + nbyte));
Bram Moolenaare7525c52021-01-09 13:20:37 +01001416}
1417
1418/*
1419 * Get the byte index for character index "idx" in string "str" with length
Bram Moolenaar0289a092021-03-14 18:40:19 +01001420 * "str_len". Composing characters are included.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001421 * If going over the end return "str_len".
1422 * If "idx" is negative count from the end, -1 is the last character.
1423 * When going over the start return -1.
1424 */
1425 static long
1426char_idx2byte(char_u *str, size_t str_len, varnumber_T idx)
1427{
1428 varnumber_T nchar = idx;
1429 size_t nbyte = 0;
1430
1431 if (nchar >= 0)
1432 {
1433 while (nchar > 0 && nbyte < str_len)
1434 {
Bram Moolenaar0289a092021-03-14 18:40:19 +01001435 nbyte += mb_ptr2len(str + nbyte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001436 --nchar;
1437 }
1438 }
1439 else
1440 {
1441 nbyte = str_len;
1442 while (nchar < 0 && nbyte > 0)
1443 {
1444 --nbyte;
1445 nbyte -= mb_head_off(str, str + nbyte);
1446 ++nchar;
1447 }
1448 if (nchar < 0)
1449 return -1;
1450 }
1451 return (long)nbyte;
1452}
1453
1454/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001455 * Return the slice "str[first : last]" using character indexes. Composing
1456 * characters are included.
Bram Moolenaar6601b622021-01-13 21:47:15 +01001457 * "exclusive" is TRUE for slice().
Bram Moolenaare7525c52021-01-09 13:20:37 +01001458 * Return NULL when the result is empty.
1459 */
1460 char_u *
Bram Moolenaar6601b622021-01-13 21:47:15 +01001461string_slice(char_u *str, varnumber_T first, varnumber_T last, int exclusive)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001462{
1463 long start_byte, end_byte;
1464 size_t slen;
1465
1466 if (str == NULL)
1467 return NULL;
1468 slen = STRLEN(str);
1469 start_byte = char_idx2byte(str, slen, first);
1470 if (start_byte < 0)
1471 start_byte = 0; // first index very negative: use zero
Bram Moolenaar6601b622021-01-13 21:47:15 +01001472 if ((last == -1 && !exclusive) || last == VARNUM_MAX)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001473 end_byte = (long)slen;
1474 else
1475 {
1476 end_byte = char_idx2byte(str, slen, last);
Bram Moolenaar6601b622021-01-13 21:47:15 +01001477 if (!exclusive && end_byte >= 0 && end_byte < (long)slen)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001478 // end index is inclusive
Bram Moolenaar0289a092021-03-14 18:40:19 +01001479 end_byte += mb_ptr2len(str + end_byte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001480 }
1481
1482 if (start_byte >= (long)slen || end_byte <= start_byte)
1483 return NULL;
1484 return vim_strnsave(str + start_byte, end_byte - start_byte);
1485}
1486
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001487/*
1488 * Get a script variable for ISN_STORESCRIPT and ISN_LOADSCRIPT.
1489 * When "dfunc_idx" is negative don't give an error.
1490 * Returns NULL for an error.
1491 */
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001492 static svar_T *
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001493get_script_svar(scriptref_T *sref, int dfunc_idx)
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001494{
1495 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001496 dfunc_T *dfunc = dfunc_idx < 0 ? NULL
1497 : ((dfunc_T *)def_functions.ga_data) + dfunc_idx;
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001498 svar_T *sv;
1499
1500 if (sref->sref_seq != si->sn_script_seq)
1501 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001502 // The script was reloaded after the function was compiled, the
1503 // script_idx may not be valid.
1504 if (dfunc != NULL)
1505 semsg(_(e_script_variable_invalid_after_reload_in_function_str),
1506 printable_func_name(dfunc->df_ufunc));
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001507 return NULL;
1508 }
1509 sv = ((svar_T *)si->sn_var_vals.ga_data) + sref->sref_idx;
Bram Moolenaar60dc8272021-07-29 22:48:54 +02001510 if (!equal_type(sv->sv_type, sref->sref_type, 0))
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001511 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001512 if (dfunc != NULL)
1513 emsg(_(e_script_variable_type_changed));
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001514 return NULL;
1515 }
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001516
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01001517 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0
1518 && sref->sref_sid != current_sctx.sc_sid)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001519 {
1520 if (dfunc != NULL)
1521 semsg(_(e_item_not_exported_in_script_str), sv->sv_name);
1522 return NULL;
1523 }
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001524 return sv;
1525}
1526
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001527/*
Bram Moolenaar20677332021-06-06 17:02:53 +02001528 * Function passed to do_cmdline() for splitting a script joined by NL
1529 * characters.
1530 */
1531 static char_u *
1532get_split_sourceline(
1533 int c UNUSED,
1534 void *cookie,
1535 int indent UNUSED,
1536 getline_opt_T options UNUSED)
1537{
1538 source_cookie_T *sp = (source_cookie_T *)cookie;
1539 char_u *p;
1540 char_u *line;
1541
Bram Moolenaar20677332021-06-06 17:02:53 +02001542 p = vim_strchr(sp->nextline, '\n');
1543 if (p == NULL)
1544 {
1545 line = vim_strsave(sp->nextline);
1546 sp->nextline += STRLEN(sp->nextline);
1547 }
1548 else
1549 {
1550 line = vim_strnsave(sp->nextline, p - sp->nextline);
1551 sp->nextline = p + 1;
1552 }
1553 return line;
1554}
1555
1556/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001557 * Execute a function by "name".
1558 * This can be a builtin function, user function or a funcref.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001559 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001560 */
1561 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001562call_eval_func(
1563 char_u *name,
1564 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001565 ectx_T *ectx,
1566 isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001567{
Bram Moolenaared677f52020-08-12 16:38:10 +02001568 int called_emsg_before = called_emsg;
1569 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001570
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001571 res = call_by_name(name, argcount, ectx, iptr, NULL);
Bram Moolenaared677f52020-08-12 16:38:10 +02001572 if (res == FAIL && called_emsg == called_emsg_before)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001573 {
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001574 dictitem_T *v;
1575
1576 v = find_var(name, NULL, FALSE);
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001577 if (v == NULL || (v->di_tv.v_type != VAR_PARTIAL
1578 && v->di_tv.v_type != VAR_FUNC))
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001579 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001580 emsg_funcname(e_unknown_function_str, name);
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001581 return FAIL;
1582 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001583 return call_partial(&v->di_tv, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001584 }
Bram Moolenaared677f52020-08-12 16:38:10 +02001585 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001586}
1587
1588/*
Bram Moolenaarf112f302020-12-20 17:47:52 +01001589 * When a function reference is used, fill a partial with the information
1590 * needed, especially when it is used as a closure.
1591 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001592 int
Bram Moolenaarf112f302020-12-20 17:47:52 +01001593fill_partial_and_closure(partial_T *pt, ufunc_T *ufunc, ectx_T *ectx)
1594{
1595 pt->pt_func = ufunc;
1596 pt->pt_refcount = 1;
1597
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001598 if (ufunc->uf_flags & FC_CLOSURE)
Bram Moolenaarf112f302020-12-20 17:47:52 +01001599 {
1600 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1601 + ectx->ec_dfunc_idx;
1602
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001603 // The closure may need to find arguments and local variables in the
1604 // current stack.
Bram Moolenaar0186e582021-01-10 18:33:11 +01001605 pt->pt_outer.out_stack = &ectx->ec_stack;
1606 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001607 if (ectx->ec_outer_ref != NULL)
1608 {
1609 // The current context already has a context, link to that one.
1610 pt->pt_outer.out_up = ectx->ec_outer_ref->or_outer;
1611 if (ectx->ec_outer_ref->or_partial != NULL)
1612 {
1613 pt->pt_outer.out_up_partial = ectx->ec_outer_ref->or_partial;
1614 ++pt->pt_outer.out_up_partial->pt_refcount;
1615 }
1616 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01001617
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001618 // If this function returns and the closure is still being used, we
1619 // need to make a copy of the context (arguments and local variables).
1620 // Store a reference to the partial so we can handle that.
Bram Moolenaar35578162021-08-02 19:10:38 +02001621 if (GA_GROW_FAILS(&ectx->ec_funcrefs, 1))
Bram Moolenaarf112f302020-12-20 17:47:52 +01001622 {
1623 vim_free(pt);
1624 return FAIL;
1625 }
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001626 // Extra variable keeps the count of closures created in the current
1627 // function call.
Bram Moolenaarf112f302020-12-20 17:47:52 +01001628 ++(((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx
1629 + STACK_FRAME_SIZE + dfunc->df_varcount)->vval.v_number;
1630
1631 ((partial_T **)ectx->ec_funcrefs.ga_data)
1632 [ectx->ec_funcrefs.ga_len] = pt;
1633 ++pt->pt_refcount;
1634 ++ectx->ec_funcrefs.ga_len;
1635 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001636 ++ufunc->uf_refcount;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001637 return OK;
1638}
1639
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001640/*
1641 * Execute iptr->isn_arg.string as an Ex command.
1642 */
1643 static int
1644exec_command(isn_T *iptr)
1645{
1646 source_cookie_T cookie;
1647
1648 SOURCING_LNUM = iptr->isn_lnum;
1649 // Pass getsourceline to get an error for a missing ":end"
1650 // command.
1651 CLEAR_FIELD(cookie);
1652 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1653 if (do_cmdline(iptr->isn_arg.string,
1654 getsourceline, &cookie,
1655 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED) == FAIL
1656 || did_emsg)
1657 return FAIL;
1658 return OK;
1659}
1660
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001661// used for v_instr of typval of VAR_INSTR
1662struct instr_S {
1663 ectx_T *instr_ectx;
1664 isn_T *instr_instr;
1665};
1666
Bram Moolenaar4c137212021-04-19 16:48:48 +02001667// used for substitute_instr
1668typedef struct subs_expr_S {
1669 ectx_T *subs_ectx;
1670 isn_T *subs_instr;
1671 int subs_status;
1672} subs_expr_T;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001673
1674// Get pointer to item in the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001675#define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001676
1677// Get pointer to item at the bottom of the stack, -1 is the bottom.
1678#undef STACK_TV_BOT
Bram Moolenaar4c137212021-04-19 16:48:48 +02001679#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 +01001680
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001681// Get pointer to a local variable on the stack. Negative for arguments.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001682#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 +01001683
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001684// Set when calling do_debug().
1685static ectx_T *debug_context = NULL;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001686static int debug_var_count;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001687
1688/*
1689 * When debugging lookup "name" and return the typeval.
1690 * When not found return NULL.
1691 */
1692 typval_T *
1693lookup_debug_var(char_u *name)
1694{
1695 int idx;
1696 dfunc_T *dfunc;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001697 ufunc_T *ufunc;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001698 ectx_T *ectx = debug_context;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001699 int varargs_off;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001700
1701 if (ectx == NULL)
1702 return NULL;
1703 dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
1704
1705 // Go through the local variable names, from last to first.
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001706 for (idx = debug_var_count - 1; idx >= 0; --idx)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001707 {
Bram Moolenaare406ff82022-03-10 20:47:43 +00001708 char_u *varname = ((char_u **)dfunc->df_var_names.ga_data)[idx];
1709
1710 // the variable name may be NULL when not available in this block
1711 if (varname != NULL && STRCMP(varname, name) == 0)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001712 return STACK_TV_VAR(idx);
1713 }
1714
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001715 // Go through argument names.
1716 ufunc = dfunc->df_ufunc;
1717 varargs_off = ufunc->uf_va_name == NULL ? 0 : 1;
1718 for (idx = 0; idx < ufunc->uf_args.ga_len; ++idx)
1719 if (STRCMP(((char_u **)(ufunc->uf_args.ga_data))[idx], name) == 0)
1720 return STACK_TV(ectx->ec_frame_idx - ufunc->uf_args.ga_len
1721 - varargs_off + idx);
1722 if (ufunc->uf_va_name != NULL && STRCMP(ufunc->uf_va_name, name) == 0)
1723 return STACK_TV(ectx->ec_frame_idx - 1);
1724
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001725 return NULL;
1726}
1727
Bram Moolenaar26a44842021-09-02 18:49:06 +02001728/*
1729 * Return TRUE if there might be a breakpoint in "ufunc", which is when a
1730 * breakpoint was set in that function or when there is any expression.
1731 */
1732 int
1733may_break_in_function(ufunc_T *ufunc)
1734{
1735 return ufunc->uf_has_breakpoint || debug_has_expr_breakpoint();
1736}
1737
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001738 static void
1739handle_debug(isn_T *iptr, ectx_T *ectx)
1740{
1741 char_u *line;
1742 ufunc_T *ufunc = (((dfunc_T *)def_functions.ga_data)
1743 + ectx->ec_dfunc_idx)->df_ufunc;
1744 isn_T *ni;
1745 int end_lnum = iptr->isn_lnum;
1746 garray_T ga;
1747 int lnum;
1748
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02001749 if (ex_nesting_level > debug_break_level)
1750 {
1751 linenr_T breakpoint;
1752
Bram Moolenaar26a44842021-09-02 18:49:06 +02001753 if (!may_break_in_function(ufunc))
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02001754 return;
1755
1756 // check for the next breakpoint if needed
1757 breakpoint = dbg_find_breakpoint(FALSE, ufunc->uf_name,
Bram Moolenaar8cec9272021-06-23 20:20:53 +02001758 iptr->isn_arg.debug.dbg_break_lnum);
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02001759 if (breakpoint <= 0 || breakpoint > iptr->isn_lnum)
1760 return;
1761 }
1762
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001763 SOURCING_LNUM = iptr->isn_lnum;
1764 debug_context = ectx;
Bram Moolenaar8cec9272021-06-23 20:20:53 +02001765 debug_var_count = iptr->isn_arg.debug.dbg_var_names_len;
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001766
1767 for (ni = iptr + 1; ni->isn_type != ISN_FINISH; ++ni)
1768 if (ni->isn_type == ISN_DEBUG
1769 || ni->isn_type == ISN_RETURN
1770 || ni->isn_type == ISN_RETURN_VOID)
1771 {
Bram Moolenaar112bed02021-11-23 22:16:34 +00001772 end_lnum = ni->isn_lnum + (ni->isn_type == ISN_DEBUG ? 0 : 1);
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001773 break;
1774 }
1775
1776 if (end_lnum > iptr->isn_lnum)
1777 {
1778 ga_init2(&ga, sizeof(char_u *), 10);
Bram Moolenaar310091d2021-12-23 21:14:37 +00001779 for (lnum = iptr->isn_lnum; lnum < end_lnum
1780 && lnum <= ufunc->uf_lines.ga_len; ++lnum)
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001781 {
Bram Moolenaar303215d2021-07-07 20:10:43 +02001782 char_u *p = ((char_u **)ufunc->uf_lines.ga_data)[lnum - 1];
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001783
Bram Moolenaar303215d2021-07-07 20:10:43 +02001784 if (p == NULL)
1785 continue; // left over from continuation line
1786 p = skipwhite(p);
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001787 if (*p == '#')
1788 break;
Bram Moolenaar35578162021-08-02 19:10:38 +02001789 if (GA_GROW_OK(&ga, 1))
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001790 ((char_u **)(ga.ga_data))[ga.ga_len++] = p;
1791 if (STRNCMP(p, "def ", 4) == 0)
1792 break;
1793 }
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001794 line = ga_concat_strings(&ga, " ");
1795 vim_free(ga.ga_data);
1796 }
1797 else
1798 line = ((char_u **)ufunc->uf_lines.ga_data)[iptr->isn_lnum - 1];
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001799
Dominique Pelle6e969552021-06-17 13:53:41 +02001800 do_debug(line == NULL ? (char_u *)"[empty]" : line);
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001801 debug_context = NULL;
1802
1803 if (end_lnum > iptr->isn_lnum)
1804 vim_free(line);
1805}
1806
Bram Moolenaar4c137212021-04-19 16:48:48 +02001807/*
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00001808 * Store a value in a list, dict or blob variable.
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001809 * Returns OK, FAIL or NOTDONE (uncatchable error).
1810 */
1811 static int
1812execute_storeindex(isn_T *iptr, ectx_T *ectx)
1813{
1814 vartype_T dest_type = iptr->isn_arg.vartype;
1815 typval_T *tv;
1816 typval_T *tv_idx = STACK_TV_BOT(-2);
1817 typval_T *tv_dest = STACK_TV_BOT(-1);
1818 int status = OK;
1819
1820 // Stack contains:
1821 // -3 value to be stored
1822 // -2 index
1823 // -1 dict or list
1824 tv = STACK_TV_BOT(-3);
1825 SOURCING_LNUM = iptr->isn_lnum;
1826 if (dest_type == VAR_ANY)
1827 {
1828 dest_type = tv_dest->v_type;
1829 if (dest_type == VAR_DICT)
1830 status = do_2string(tv_idx, TRUE, FALSE);
1831 else if (dest_type == VAR_LIST && tv_idx->v_type != VAR_NUMBER)
1832 {
1833 emsg(_(e_number_expected));
1834 status = FAIL;
1835 }
1836 }
Bram Moolenaare08be092022-02-17 13:08:26 +00001837
1838 if (status == OK)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001839 {
Bram Moolenaare08be092022-02-17 13:08:26 +00001840 if (dest_type == VAR_LIST)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001841 {
Bram Moolenaare08be092022-02-17 13:08:26 +00001842 long lidx = (long)tv_idx->vval.v_number;
1843 list_T *list = tv_dest->vval.v_list;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001844
Bram Moolenaare08be092022-02-17 13:08:26 +00001845 if (list == NULL)
1846 {
1847 emsg(_(e_list_not_set));
1848 return FAIL;
1849 }
1850 if (lidx < 0 && list->lv_len + lidx >= 0)
1851 // negative index is relative to the end
1852 lidx = list->lv_len + lidx;
1853 if (lidx < 0 || lidx > list->lv_len)
1854 {
1855 semsg(_(e_list_index_out_of_range_nr), lidx);
1856 return FAIL;
1857 }
1858 if (lidx < list->lv_len)
1859 {
1860 listitem_T *li = list_find(list, lidx);
1861
1862 if (error_if_locked(li->li_tv.v_lock,
Bram Moolenaar70c43d82022-01-26 21:01:15 +00001863 e_cannot_change_locked_list_item))
Bram Moolenaare08be092022-02-17 13:08:26 +00001864 return FAIL;
1865 // overwrite existing list item
1866 clear_tv(&li->li_tv);
1867 li->li_tv = *tv;
1868 }
1869 else
1870 {
1871 if (error_if_locked(list->lv_lock, e_cannot_change_locked_list))
1872 return FAIL;
1873 // append to list, only fails when out of memory
1874 if (list_append_tv(list, tv) == FAIL)
1875 return NOTDONE;
1876 clear_tv(tv);
1877 }
1878 }
1879 else if (dest_type == VAR_DICT)
1880 {
1881 char_u *key = tv_idx->vval.v_string;
1882 dict_T *dict = tv_dest->vval.v_dict;
1883 dictitem_T *di;
1884
1885 SOURCING_LNUM = iptr->isn_lnum;
1886 if (dict == NULL)
1887 {
1888 emsg(_(e_dictionary_not_set));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001889 return FAIL;
Bram Moolenaare08be092022-02-17 13:08:26 +00001890 }
1891 if (key == NULL)
1892 key = (char_u *)"";
1893 di = dict_find(dict, key, -1);
1894 if (di != NULL)
1895 {
1896 if (error_if_locked(di->di_tv.v_lock,
1897 e_cannot_change_dict_item))
1898 return FAIL;
1899 // overwrite existing value
1900 clear_tv(&di->di_tv);
1901 di->di_tv = *tv;
1902 }
1903 else
1904 {
1905 if (error_if_locked(dict->dv_lock, e_cannot_change_dict))
1906 return FAIL;
1907 // add to dict, only fails when out of memory
1908 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
1909 return NOTDONE;
1910 clear_tv(tv);
1911 }
1912 }
1913 else if (dest_type == VAR_BLOB)
1914 {
1915 long lidx = (long)tv_idx->vval.v_number;
1916 blob_T *blob = tv_dest->vval.v_blob;
1917 varnumber_T nr;
1918 int error = FALSE;
1919 int len;
1920
1921 if (blob == NULL)
1922 {
1923 emsg(_(e_blob_not_set));
1924 return FAIL;
1925 }
1926 len = blob_len(blob);
1927 if (lidx < 0 && len + lidx >= 0)
1928 // negative index is relative to the end
1929 lidx = len + lidx;
1930
1931 // Can add one byte at the end.
1932 if (lidx < 0 || lidx > len)
1933 {
1934 semsg(_(e_blob_index_out_of_range_nr), lidx);
1935 return FAIL;
1936 }
1937 if (value_check_lock(blob->bv_lock, (char_u *)"blob", FALSE))
1938 return FAIL;
1939 nr = tv_get_number_chk(tv, &error);
1940 if (error)
1941 return FAIL;
1942 blob_set_append(blob, lidx, nr);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001943 }
1944 else
1945 {
Bram Moolenaare08be092022-02-17 13:08:26 +00001946 status = FAIL;
1947 semsg(_(e_cannot_index_str), vartype_name(dest_type));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001948 }
1949 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001950
1951 clear_tv(tv_idx);
1952 clear_tv(tv_dest);
1953 ectx->ec_stack.ga_len -= 3;
1954 if (status == FAIL)
1955 {
1956 clear_tv(tv);
1957 return FAIL;
1958 }
1959 return OK;
1960}
1961
1962/*
Bram Moolenaarea5c8982022-02-17 14:42:02 +00001963 * Store a value in a list or blob range.
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001964 */
1965 static int
1966execute_storerange(isn_T *iptr, ectx_T *ectx)
1967{
1968 typval_T *tv;
1969 typval_T *tv_idx1 = STACK_TV_BOT(-3);
1970 typval_T *tv_idx2 = STACK_TV_BOT(-2);
1971 typval_T *tv_dest = STACK_TV_BOT(-1);
1972 int status = OK;
1973
1974 // Stack contains:
1975 // -4 value to be stored
1976 // -3 first index or "none"
1977 // -2 second index or "none"
1978 // -1 destination list or blob
1979 tv = STACK_TV_BOT(-4);
Bram Moolenaarea5c8982022-02-17 14:42:02 +00001980 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001981 if (tv_dest->v_type == VAR_LIST)
1982 {
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00001983 long n1;
1984 long n2;
1985 listitem_T *li1;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001986
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00001987 n1 = (long)tv_get_number_chk(tv_idx1, NULL);
1988 if (tv_idx2->v_type == VAR_SPECIAL
1989 && tv_idx2->vval.v_number == VVAL_NONE)
1990 n2 = list_len(tv_dest->vval.v_list) - 1;
1991 else
1992 n2 = (long)tv_get_number_chk(tv_idx2, NULL);
1993
Bram Moolenaar22ebd172022-04-01 15:26:58 +01001994 li1 = check_range_index_one(tv_dest->vval.v_list, &n1, TRUE, FALSE);
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00001995 if (li1 == NULL)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001996 status = FAIL;
1997 else
1998 {
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00001999 status = check_range_index_two(tv_dest->vval.v_list,
2000 &n1, li1, &n2, FALSE);
2001 if (status != FAIL)
2002 status = list_assign_range(
2003 tv_dest->vval.v_list,
2004 tv->vval.v_list,
2005 n1,
2006 n2,
2007 tv_idx2->v_type == VAR_SPECIAL,
2008 (char_u *)"=",
2009 (char_u *)"[unknown]");
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002010 }
2011 }
2012 else if (tv_dest->v_type == VAR_BLOB)
2013 {
2014 varnumber_T n1;
2015 varnumber_T n2;
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002016 long bloblen;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002017
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002018 n1 = tv_get_number_chk(tv_idx1, NULL);
2019 if (tv_idx2->v_type == VAR_SPECIAL
2020 && tv_idx2->vval.v_number == VVAL_NONE)
2021 n2 = blob_len(tv_dest->vval.v_blob) - 1;
2022 else
2023 n2 = tv_get_number_chk(tv_idx2, NULL);
2024 bloblen = blob_len(tv_dest->vval.v_blob);
2025
2026 if (check_blob_index(bloblen, n1, FALSE) == FAIL
2027 || check_blob_range(bloblen, n1, n2, FALSE) == FAIL)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002028 status = FAIL;
2029 else
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002030 status = blob_set_range(tv_dest->vval.v_blob, n1, n2, tv);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002031 }
2032 else
2033 {
2034 status = FAIL;
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002035 emsg(_(e_list_or_blob_required));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002036 }
2037
2038 clear_tv(tv_idx1);
2039 clear_tv(tv_idx2);
2040 clear_tv(tv_dest);
2041 ectx->ec_stack.ga_len -= 4;
2042 clear_tv(tv);
2043
2044 return status;
2045}
2046
2047/*
2048 * Unlet item in list or dict variable.
2049 */
2050 static int
2051execute_unletindex(isn_T *iptr, ectx_T *ectx)
2052{
2053 typval_T *tv_idx = STACK_TV_BOT(-2);
2054 typval_T *tv_dest = STACK_TV_BOT(-1);
2055 int status = OK;
2056
2057 // Stack contains:
2058 // -2 index
2059 // -1 dict or list
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002060 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002061 if (tv_dest->v_type == VAR_DICT)
2062 {
2063 // unlet a dict item, index must be a string
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002064 if (tv_idx->v_type != VAR_STRING && tv_idx->v_type != VAR_NUMBER)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002065 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002066 semsg(_(e_expected_str_but_got_str),
2067 vartype_name(VAR_STRING),
2068 vartype_name(tv_idx->v_type));
2069 status = FAIL;
2070 }
2071 else
2072 {
2073 dict_T *d = tv_dest->vval.v_dict;
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002074 char_u *key;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002075 dictitem_T *di = NULL;
2076
2077 if (d != NULL && value_check_lock(
2078 d->dv_lock, NULL, FALSE))
2079 status = FAIL;
2080 else
2081 {
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002082 if (tv_idx->v_type == VAR_STRING)
2083 {
2084 key = tv_idx->vval.v_string;
2085 if (key == NULL)
2086 key = (char_u *)"";
2087 }
2088 else
2089 {
2090 key = tv_get_string(tv_idx);
2091 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002092 if (d != NULL)
2093 di = dict_find(d, key, (int)STRLEN(key));
2094 if (di == NULL)
2095 {
2096 // NULL dict is equivalent to empty dict
2097 semsg(_(e_key_not_present_in_dictionary),
2098 key);
2099 status = FAIL;
2100 }
2101 else if (var_check_fixed(di->di_flags,
2102 NULL, FALSE)
2103 || var_check_ro(di->di_flags,
2104 NULL, FALSE))
2105 status = FAIL;
2106 else
2107 dictitem_remove(d, di);
2108 }
2109 }
2110 }
2111 else if (tv_dest->v_type == VAR_LIST)
2112 {
2113 // unlet a List item, index must be a number
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002114 if (check_for_number(tv_idx) == FAIL)
2115 {
2116 status = FAIL;
2117 }
2118 else
2119 {
2120 list_T *l = tv_dest->vval.v_list;
2121 long n = (long)tv_idx->vval.v_number;
2122
2123 if (l != NULL && value_check_lock(
2124 l->lv_lock, NULL, FALSE))
2125 status = FAIL;
2126 else
2127 {
2128 listitem_T *li = list_find(l, n);
2129
2130 if (li == NULL)
2131 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002132 semsg(_(e_list_index_out_of_range_nr), n);
2133 status = FAIL;
2134 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002135 else
2136 listitem_remove(l, li);
2137 }
2138 }
2139 }
2140 else
2141 {
2142 status = FAIL;
2143 semsg(_(e_cannot_index_str),
2144 vartype_name(tv_dest->v_type));
2145 }
2146
2147 clear_tv(tv_idx);
2148 clear_tv(tv_dest);
2149 ectx->ec_stack.ga_len -= 2;
2150
2151 return status;
2152}
2153
2154/*
2155 * Unlet a range of items in a list variable.
2156 */
2157 static int
2158execute_unletrange(isn_T *iptr, ectx_T *ectx)
2159{
2160 // Stack contains:
2161 // -3 index1
2162 // -2 index2
2163 // -1 dict or list
2164 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2165 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2166 typval_T *tv_dest = STACK_TV_BOT(-1);
2167 int status = OK;
2168
2169 if (tv_dest->v_type == VAR_LIST)
2170 {
2171 // indexes must be a number
2172 SOURCING_LNUM = iptr->isn_lnum;
2173 if (check_for_number(tv_idx1) == FAIL
2174 || (tv_idx2->v_type != VAR_SPECIAL
2175 && check_for_number(tv_idx2) == FAIL))
2176 {
2177 status = FAIL;
2178 }
2179 else
2180 {
2181 list_T *l = tv_dest->vval.v_list;
2182 long n1 = (long)tv_idx1->vval.v_number;
2183 long n2 = tv_idx2->v_type == VAR_SPECIAL
2184 ? 0 : (long)tv_idx2->vval.v_number;
2185 listitem_T *li;
2186
2187 li = list_find_index(l, &n1);
2188 if (li == NULL)
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002189 {
2190 semsg(_(e_list_index_out_of_range_nr),
2191 (long)tv_idx1->vval.v_number);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002192 status = FAIL;
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002193 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002194 else
2195 {
2196 if (n1 < 0)
2197 n1 = list_idx_of_item(l, li);
2198 if (n2 < 0)
2199 {
2200 listitem_T *li2 = list_find(l, n2);
2201
2202 if (li2 == NULL)
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002203 {
2204 semsg(_(e_list_index_out_of_range_nr), n2);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002205 status = FAIL;
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002206 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002207 else
2208 n2 = list_idx_of_item(l, li2);
2209 }
2210 if (status != FAIL
2211 && tv_idx2->v_type != VAR_SPECIAL
2212 && n2 < n1)
2213 {
2214 semsg(_(e_list_index_out_of_range_nr), n2);
2215 status = FAIL;
2216 }
Bram Moolenaar6b8c7ba2022-03-20 17:46:06 +00002217 if (status != FAIL)
2218 list_unlet_range(l, li, n1,
2219 tv_idx2->v_type != VAR_SPECIAL, n2);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002220 }
2221 }
2222 }
2223 else
2224 {
2225 status = FAIL;
2226 SOURCING_LNUM = iptr->isn_lnum;
2227 semsg(_(e_cannot_index_str),
2228 vartype_name(tv_dest->v_type));
2229 }
2230
2231 clear_tv(tv_idx1);
2232 clear_tv(tv_idx2);
2233 clear_tv(tv_dest);
2234 ectx->ec_stack.ga_len -= 3;
2235
2236 return status;
2237}
2238
2239/*
2240 * Top of a for loop.
2241 */
2242 static int
2243execute_for(isn_T *iptr, ectx_T *ectx)
2244{
2245 typval_T *tv;
2246 typval_T *ltv = STACK_TV_BOT(-1);
2247 typval_T *idxtv =
2248 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
2249
2250 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
2251 return FAIL;
2252 if (ltv->v_type == VAR_LIST)
2253 {
2254 list_T *list = ltv->vval.v_list;
2255
2256 // push the next item from the list
2257 ++idxtv->vval.v_number;
2258 if (list == NULL
2259 || idxtv->vval.v_number >= list->lv_len)
2260 {
2261 // past the end of the list, jump to "endfor"
2262 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
2263 may_restore_cmdmod(&ectx->ec_funclocal);
2264 }
2265 else if (list->lv_first == &range_list_item)
2266 {
2267 // non-materialized range() list
2268 tv = STACK_TV_BOT(0);
2269 tv->v_type = VAR_NUMBER;
2270 tv->v_lock = 0;
2271 tv->vval.v_number = list_find_nr(
2272 list, idxtv->vval.v_number, NULL);
2273 ++ectx->ec_stack.ga_len;
2274 }
2275 else
2276 {
2277 listitem_T *li = list_find(list,
2278 idxtv->vval.v_number);
2279
2280 copy_tv(&li->li_tv, STACK_TV_BOT(0));
2281 ++ectx->ec_stack.ga_len;
2282 }
2283 }
2284 else if (ltv->v_type == VAR_STRING)
2285 {
2286 char_u *str = ltv->vval.v_string;
2287
2288 // The index is for the last byte of the previous
2289 // character.
2290 ++idxtv->vval.v_number;
2291 if (str == NULL || str[idxtv->vval.v_number] == NUL)
2292 {
2293 // past the end of the string, jump to "endfor"
2294 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
2295 may_restore_cmdmod(&ectx->ec_funclocal);
2296 }
2297 else
2298 {
2299 int clen = mb_ptr2len(str + idxtv->vval.v_number);
2300
2301 // Push the next character from the string.
2302 tv = STACK_TV_BOT(0);
2303 tv->v_type = VAR_STRING;
2304 tv->vval.v_string = vim_strnsave(
2305 str + idxtv->vval.v_number, clen);
2306 ++ectx->ec_stack.ga_len;
2307 idxtv->vval.v_number += clen - 1;
2308 }
2309 }
2310 else if (ltv->v_type == VAR_BLOB)
2311 {
2312 blob_T *blob = ltv->vval.v_blob;
2313
2314 // When we get here the first time make a copy of the
2315 // blob, so that the iteration still works when it is
2316 // changed.
2317 if (idxtv->vval.v_number == -1 && blob != NULL)
2318 {
2319 blob_copy(blob, ltv);
2320 blob_unref(blob);
2321 blob = ltv->vval.v_blob;
2322 }
2323
2324 // The index is for the previous byte.
2325 ++idxtv->vval.v_number;
2326 if (blob == NULL
2327 || idxtv->vval.v_number >= blob_len(blob))
2328 {
2329 // past the end of the blob, jump to "endfor"
2330 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
2331 may_restore_cmdmod(&ectx->ec_funclocal);
2332 }
2333 else
2334 {
2335 // Push the next byte from the blob.
2336 tv = STACK_TV_BOT(0);
2337 tv->v_type = VAR_NUMBER;
2338 tv->vval.v_number = blob_get(blob,
2339 idxtv->vval.v_number);
2340 ++ectx->ec_stack.ga_len;
2341 }
2342 }
2343 else
2344 {
2345 semsg(_(e_for_loop_on_str_not_supported),
2346 vartype_name(ltv->v_type));
2347 return FAIL;
2348 }
2349 return OK;
2350}
2351
2352/*
Bram Moolenaar06b77222022-01-25 15:51:56 +00002353 * Load instruction for w:/b:/g:/t: variable.
2354 * "isn_type" is used instead of "iptr->isn_type".
2355 */
2356 static int
2357load_namespace_var(ectx_T *ectx, isntype_T isn_type, isn_T *iptr)
2358{
2359 dictitem_T *di = NULL;
2360 hashtab_T *ht = NULL;
2361 char namespace;
2362
2363 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
2364 return NOTDONE;
2365 switch (isn_type)
2366 {
2367 case ISN_LOADG:
2368 ht = get_globvar_ht();
2369 namespace = 'g';
2370 break;
2371 case ISN_LOADB:
2372 ht = &curbuf->b_vars->dv_hashtab;
2373 namespace = 'b';
2374 break;
2375 case ISN_LOADW:
2376 ht = &curwin->w_vars->dv_hashtab;
2377 namespace = 'w';
2378 break;
2379 case ISN_LOADT:
2380 ht = &curtab->tp_vars->dv_hashtab;
2381 namespace = 't';
2382 break;
2383 default: // Cannot reach here
2384 return NOTDONE;
2385 }
2386 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
2387
Bram Moolenaar06b77222022-01-25 15:51:56 +00002388 if (di == NULL)
2389 {
Bram Moolenaarfe732552022-02-22 19:39:13 +00002390 if (isn_type == ISN_LOADG)
2391 {
2392 ufunc_T *ufunc = find_func(iptr->isn_arg.string, TRUE);
2393
2394 // g:Something could be a function
2395 if (ufunc != NULL)
2396 {
2397 typval_T *tv = STACK_TV_BOT(0);
2398
2399 ++ectx->ec_stack.ga_len;
2400 tv->v_type = VAR_FUNC;
2401 tv->vval.v_string = alloc(STRLEN(iptr->isn_arg.string) + 3);
2402 if (tv->vval.v_string == NULL)
2403 return FAIL;
2404 STRCPY(tv->vval.v_string, "g:");
2405 STRCPY(tv->vval.v_string + 2, iptr->isn_arg.string);
2406 return OK;
2407 }
2408 }
Bram Moolenaar06b77222022-01-25 15:51:56 +00002409 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarfe732552022-02-22 19:39:13 +00002410 if (vim_strchr(iptr->isn_arg.string, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar06b77222022-01-25 15:51:56 +00002411 // no check if the item exists in the script but
2412 // isn't exported, it is too complicated
Bram Moolenaarfe732552022-02-22 19:39:13 +00002413 semsg(_(e_item_not_found_in_script_str), iptr->isn_arg.string);
Bram Moolenaar06b77222022-01-25 15:51:56 +00002414 else
2415 semsg(_(e_undefined_variable_char_str),
Bram Moolenaarfe732552022-02-22 19:39:13 +00002416 namespace, iptr->isn_arg.string);
Bram Moolenaar06b77222022-01-25 15:51:56 +00002417 return FAIL;
2418 }
2419 else
2420 {
2421 copy_tv(&di->di_tv, STACK_TV_BOT(0));
2422 ++ectx->ec_stack.ga_len;
2423 }
2424 return OK;
2425}
2426
2427/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02002428 * Execute instructions in execution context "ectx".
2429 * Return OK or FAIL;
2430 */
2431 static int
2432exec_instructions(ectx_T *ectx)
2433{
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002434 int ret = FAIL;
2435 int save_trylevel_at_start = ectx->ec_trylevel_at_start;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02002436 int dict_stack_len_at_start = dict_stack.ga_len;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01002437
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02002438 // Start execution at the first instruction.
Bram Moolenaar4c137212021-04-19 16:48:48 +02002439 ectx->ec_iidx = 0;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01002440
Bram Moolenaarff652882021-05-16 15:24:49 +02002441 // Only catch exceptions in this instruction list.
2442 ectx->ec_trylevel_at_start = trylevel;
2443
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002444 for (;;)
2445 {
Bram Moolenaara7490192021-07-22 12:26:14 +02002446 static int breakcheck_count = 0; // using "static" makes it faster
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002447 isn_T *iptr;
Bram Moolenaara7490192021-07-22 12:26:14 +02002448 typval_T *tv;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002449
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002450 if (unlikely(++breakcheck_count >= 100))
Bram Moolenaar270d0382020-05-15 21:42:53 +02002451 {
2452 line_breakcheck();
2453 breakcheck_count = 0;
2454 }
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002455 if (unlikely(got_int))
Bram Moolenaar20431c92020-03-20 18:39:46 +01002456 {
2457 // Turn CTRL-C into an exception.
2458 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +01002459 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002460 goto theend;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002461 did_throw = TRUE;
2462 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002463
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002464 if (unlikely(did_emsg && msg_list != NULL && *msg_list != NULL))
Bram Moolenaara26b9702020-04-18 19:53:28 +02002465 {
2466 // Turn an error message into an exception.
2467 did_emsg = FALSE;
2468 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002469 goto theend;
Bram Moolenaara26b9702020-04-18 19:53:28 +02002470 did_throw = TRUE;
2471 *msg_list = NULL;
2472 }
2473
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002474 if (unlikely(did_throw))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002475 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002476 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002477 trycmd_T *trycmd = NULL;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002478 int index = trystack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002479
2480 // An exception jumps to the first catch, finally, or returns from
2481 // the current function.
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002482 while (index > 0)
2483 {
2484 trycmd = ((trycmd_T *)trystack->ga_data) + index - 1;
Bram Moolenaar834193a2021-06-30 20:39:15 +02002485 if (!trycmd->tcd_in_catch || trycmd->tcd_finally_idx != 0)
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002486 break;
2487 // In the catch and finally block of this try we have to go up
2488 // one level.
2489 --index;
2490 trycmd = NULL;
2491 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02002492 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002493 {
Bram Moolenaar834193a2021-06-30 20:39:15 +02002494 if (trycmd->tcd_in_catch)
2495 {
2496 // exception inside ":catch", jump to ":finally" once
2497 ectx->ec_iidx = trycmd->tcd_finally_idx;
2498 trycmd->tcd_finally_idx = 0;
2499 }
2500 else
2501 // jump to first ":catch"
2502 ectx->ec_iidx = trycmd->tcd_catch_idx;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002503 trycmd->tcd_in_catch = TRUE;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002504 did_throw = FALSE; // don't come back here until :endtry
2505 trycmd->tcd_did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002506 }
2507 else
2508 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02002509 // Not inside try or need to return from current functions.
2510 // Push a dummy return value.
Bram Moolenaar35578162021-08-02 19:10:38 +02002511 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002512 goto theend;
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02002513 tv = STACK_TV_BOT(0);
2514 tv->v_type = VAR_NUMBER;
2515 tv->vval.v_number = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002516 ++ectx->ec_stack.ga_len;
2517 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002518 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02002519 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +01002520 need_rethrow = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002521 if (handle_closure_in_use(ectx, FALSE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002522 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002523 goto done;
2524 }
2525
Bram Moolenaar4c137212021-04-19 16:48:48 +02002526 if (func_return(ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002527 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002528 }
2529 continue;
2530 }
2531
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002532 /*
2533 * Big switch on the instruction. Most compilers will be turning this
2534 * into an efficient lookup table, since the "case" values are an enum
2535 * with sequential numbers. It may look ugly, but it should be the
2536 * most efficient way.
2537 */
Bram Moolenaar4c137212021-04-19 16:48:48 +02002538 iptr = &ectx->ec_instr[ectx->ec_iidx++];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002539 switch (iptr->isn_type)
2540 {
2541 // execute Ex command line
2542 case ISN_EXEC:
Bram Moolenaaraacc9662021-08-13 19:40:51 +02002543 if (exec_command(iptr) == FAIL)
2544 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002545 break;
2546
Bram Moolenaar20677332021-06-06 17:02:53 +02002547 // execute Ex command line split at NL characters.
2548 case ISN_EXEC_SPLIT:
2549 {
2550 source_cookie_T cookie;
Bram Moolenaar518df272021-06-06 17:34:13 +02002551 char_u *line;
Bram Moolenaar20677332021-06-06 17:02:53 +02002552
2553 SOURCING_LNUM = iptr->isn_lnum;
2554 CLEAR_FIELD(cookie);
2555 cookie.sourcing_lnum = iptr->isn_lnum - 1;
2556 cookie.nextline = iptr->isn_arg.string;
Bram Moolenaar518df272021-06-06 17:34:13 +02002557 line = get_split_sourceline(0, &cookie, 0, 0);
2558 if (do_cmdline(line,
Bram Moolenaar20677332021-06-06 17:02:53 +02002559 get_split_sourceline, &cookie,
2560 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED)
2561 == FAIL
2562 || did_emsg)
Bram Moolenaar518df272021-06-06 17:34:13 +02002563 {
2564 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02002565 goto on_error;
Bram Moolenaar518df272021-06-06 17:34:13 +02002566 }
2567 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02002568 }
2569 break;
2570
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002571 // execute Ex command line that is only a range
2572 case ISN_EXECRANGE:
2573 {
2574 exarg_T ea;
2575 char *error = NULL;
2576
2577 CLEAR_FIELD(ea);
2578 ea.cmdidx = CMD_SIZE;
2579 ea.addr_type = ADDR_LINES;
2580 ea.cmd = iptr->isn_arg.string;
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00002581 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002582 parse_cmd_address(&ea, &error, FALSE);
Bram Moolenaar01a4dcb2021-12-04 13:15:10 +00002583 if (ea.cmd == NULL)
2584 goto on_error;
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00002585 // error is always NULL when using ADDR_LINES
2586 error = ex_range_without_command(&ea);
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002587 if (error != NULL)
2588 {
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002589 emsg(error);
2590 goto on_error;
2591 }
2592 }
2593 break;
2594
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02002595 // Evaluate an expression with legacy syntax, push it onto the
2596 // stack.
2597 case ISN_LEGACY_EVAL:
2598 {
2599 char_u *arg = iptr->isn_arg.string;
2600 int res;
2601 int save_flags = cmdmod.cmod_flags;
2602
Bram Moolenaar35578162021-08-02 19:10:38 +02002603 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002604 goto theend;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02002605 tv = STACK_TV_BOT(0);
2606 init_tv(tv);
2607 cmdmod.cmod_flags |= CMOD_LEGACY;
2608 res = eval0(arg, tv, NULL, &EVALARG_EVALUATE);
2609 cmdmod.cmod_flags = save_flags;
2610 if (res == FAIL)
2611 goto on_error;
2612 ++ectx->ec_stack.ga_len;
2613 }
2614 break;
2615
Bram Moolenaarf18332f2021-05-07 17:55:55 +02002616 // push typeval VAR_INSTR with instructions to be executed
2617 case ISN_INSTR:
2618 {
Bram Moolenaar35578162021-08-02 19:10:38 +02002619 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002620 goto theend;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02002621 tv = STACK_TV_BOT(0);
2622 tv->vval.v_instr = ALLOC_ONE(instr_T);
2623 if (tv->vval.v_instr == NULL)
2624 goto on_error;
2625 ++ectx->ec_stack.ga_len;
2626
2627 tv->v_type = VAR_INSTR;
2628 tv->vval.v_instr->instr_ectx = ectx;
2629 tv->vval.v_instr->instr_instr = iptr->isn_arg.instr;
2630 }
2631 break;
2632
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002633 case ISN_SOURCE:
2634 {
2635 scriptitem_T *si = SCRIPT_ITEM(iptr->isn_arg.number);
2636
2637 if (si->sn_state == SN_STATE_NOT_LOADED)
2638 {
2639 SOURCING_LNUM = iptr->isn_lnum;
2640 if (do_source(si->sn_name, FALSE, DOSO_NONE, NULL)
2641 == FAIL)
Bram Moolenaar10611952022-04-03 21:11:34 +01002642 {
Bram Moolenaar242c1522022-04-03 21:52:51 +01002643 semsg(_(e_cant_open_file_str), si->sn_name);
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002644 goto on_error;
Bram Moolenaar10611952022-04-03 21:11:34 +01002645 }
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002646 }
2647 }
2648 break;
2649
Bram Moolenaar4c137212021-04-19 16:48:48 +02002650 // execute :substitute with an expression
2651 case ISN_SUBSTITUTE:
2652 {
2653 subs_T *subs = &iptr->isn_arg.subs;
2654 source_cookie_T cookie;
2655 struct subs_expr_S *save_instr = substitute_instr;
2656 struct subs_expr_S subs_instr;
2657 int res;
2658
2659 subs_instr.subs_ectx = ectx;
2660 subs_instr.subs_instr = subs->subs_instr;
2661 subs_instr.subs_status = OK;
2662 substitute_instr = &subs_instr;
2663
2664 SOURCING_LNUM = iptr->isn_lnum;
2665 // This is very much like ISN_EXEC
2666 CLEAR_FIELD(cookie);
2667 cookie.sourcing_lnum = iptr->isn_lnum - 1;
2668 res = do_cmdline(subs->subs_cmd,
2669 getsourceline, &cookie,
2670 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
2671 substitute_instr = save_instr;
2672
2673 if (res == FAIL || did_emsg
2674 || subs_instr.subs_status == FAIL)
2675 goto on_error;
2676 }
2677 break;
2678
2679 case ISN_FINISH:
2680 goto done;
2681
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02002682 case ISN_REDIRSTART:
2683 // create a dummy entry for var_redir_str()
2684 if (alloc_redir_lval() == FAIL)
2685 goto on_error;
2686
2687 // The output is stored in growarray "redir_ga" until
2688 // redirection ends.
2689 init_redir_ga();
2690 redir_vname = 1;
2691 break;
2692
2693 case ISN_REDIREND:
2694 {
2695 char_u *res = get_clear_redir_ga();
2696
2697 // End redirection, put redirected text on the stack.
2698 clear_redir_lval();
2699 redir_vname = 0;
2700
Bram Moolenaar35578162021-08-02 19:10:38 +02002701 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02002702 {
2703 vim_free(res);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002704 goto theend;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02002705 }
2706 tv = STACK_TV_BOT(0);
2707 tv->v_type = VAR_STRING;
2708 tv->vval.v_string = res;
2709 ++ectx->ec_stack.ga_len;
2710 }
2711 break;
2712
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002713 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02002714#ifdef FEAT_QUICKFIX
Bram Moolenaar397a87a2022-03-20 21:14:15 +00002715 force_abort = TRUE;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002716 if (trigger_cexpr_autocmd(iptr->isn_arg.number) == FAIL)
2717 goto on_error;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00002718 force_abort = FALSE;
Bram Moolenaarb7c97812021-05-05 22:51:39 +02002719#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002720 break;
2721
2722 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02002723#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002724 {
2725 exarg_T ea;
2726 int res;
2727
2728 CLEAR_FIELD(ea);
2729 ea.cmdidx = iptr->isn_arg.cexpr.cexpr_ref->cer_cmdidx;
2730 ea.forceit = iptr->isn_arg.cexpr.cexpr_ref->cer_forceit;
2731 ea.cmdlinep = &iptr->isn_arg.cexpr.cexpr_ref->cer_cmdline;
2732 --ectx->ec_stack.ga_len;
2733 tv = STACK_TV_BOT(0);
2734 res = cexpr_core(&ea, tv);
2735 clear_tv(tv);
2736 if (res == FAIL)
2737 goto on_error;
2738 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02002739#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002740 break;
2741
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002742 // execute Ex command from pieces on the stack
2743 case ISN_EXECCONCAT:
2744 {
2745 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +02002746 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002747 int pass;
2748 int i;
2749 char_u *cmd = NULL;
2750 char_u *str;
2751
2752 for (pass = 1; pass <= 2; ++pass)
2753 {
2754 for (i = 0; i < count; ++i)
2755 {
2756 tv = STACK_TV_BOT(i - count);
2757 str = tv->vval.v_string;
2758 if (str != NULL && *str != NUL)
2759 {
2760 if (pass == 2)
2761 STRCPY(cmd + len, str);
2762 len += STRLEN(str);
2763 }
2764 if (pass == 2)
2765 clear_tv(tv);
2766 }
2767 if (pass == 1)
2768 {
2769 cmd = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002770 if (unlikely(cmd == NULL))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002771 goto theend;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002772 len = 0;
2773 }
2774 }
2775
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02002776 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002777 do_cmdline_cmd(cmd);
2778 vim_free(cmd);
2779 }
2780 break;
2781
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002782 // execute :echo {string} ...
2783 case ISN_ECHO:
2784 {
2785 int count = iptr->isn_arg.echo.echo_count;
2786 int atstart = TRUE;
2787 int needclr = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002788 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002789
2790 for (idx = 0; idx < count; ++idx)
2791 {
2792 tv = STACK_TV_BOT(idx - count);
2793 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
2794 &atstart, &needclr);
2795 clear_tv(tv);
2796 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01002797 if (needclr)
2798 msg_clr_eos();
Bram Moolenaar4c137212021-04-19 16:48:48 +02002799 ectx->ec_stack.ga_len -= count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002800 }
2801 break;
2802
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002803 // :execute {string} ...
2804 // :echomsg {string} ...
Bram Moolenaar7de62622021-08-07 15:05:47 +02002805 // :echoconsole {string} ...
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002806 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01002807 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002808 case ISN_ECHOMSG:
Bram Moolenaar7de62622021-08-07 15:05:47 +02002809 case ISN_ECHOCONSOLE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002810 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01002811 {
2812 int count = iptr->isn_arg.number;
2813 garray_T ga;
2814 char_u buf[NUMBUFLEN];
2815 char_u *p;
2816 int len;
2817 int failed = FALSE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002818 int idx;
Bram Moolenaarad39c092020-02-26 18:23:43 +01002819
2820 ga_init2(&ga, 1, 80);
2821 for (idx = 0; idx < count; ++idx)
2822 {
2823 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002824 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01002825 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002826 if (tv->v_type == VAR_CHANNEL
2827 || tv->v_type == VAR_JOB)
2828 {
2829 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar68db9962021-05-09 23:19:22 +02002830 semsg(_(e_using_invalid_value_as_string_str),
2831 vartype_name(tv->v_type));
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002832 break;
2833 }
2834 else
2835 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01002836 }
2837 else
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002838 p = tv_stringify(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01002839
2840 len = (int)STRLEN(p);
Bram Moolenaar35578162021-08-02 19:10:38 +02002841 if (GA_GROW_FAILS(&ga, len + 2))
Bram Moolenaarad39c092020-02-26 18:23:43 +01002842 failed = TRUE;
2843 else
2844 {
2845 if (ga.ga_len > 0)
2846 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
2847 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
2848 ga.ga_len += len;
2849 }
2850 clear_tv(tv);
2851 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02002852 ectx->ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002853 if (failed)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01002854 {
2855 ga_clear(&ga);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002856 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01002857 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01002858
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002859 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002860 {
2861 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02002862 {
2863 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002864 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01002865 if (did_emsg)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01002866 {
2867 ga_clear(&ga);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01002868 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01002869 }
Bram Moolenaar430deb12020-08-23 16:29:11 +02002870 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002871 else
2872 {
2873 msg_sb_eol();
2874 if (iptr->isn_type == ISN_ECHOMSG)
2875 {
2876 msg_attr(ga.ga_data, echo_attr);
2877 out_flush();
2878 }
Bram Moolenaar7de62622021-08-07 15:05:47 +02002879 else if (iptr->isn_type == ISN_ECHOCONSOLE)
2880 {
2881 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data),
2882 TRUE);
2883 ui_write((char_u *)"\r\n", 2, TRUE);
2884 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002885 else
2886 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002887 SOURCING_LNUM = iptr->isn_lnum;
2888 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002889 }
2890 }
2891 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01002892 ga_clear(&ga);
2893 }
2894 break;
2895
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002896 // load local variable or argument
2897 case ISN_LOAD:
Bram Moolenaar35578162021-08-02 19:10:38 +02002898 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002899 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002900 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002901 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002902 break;
2903
2904 // load v: variable
2905 case ISN_LOADV:
Bram Moolenaar35578162021-08-02 19:10:38 +02002906 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002907 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002908 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002909 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002910 break;
2911
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002912 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002913 case ISN_LOADSCRIPT:
2914 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01002915 scriptref_T *sref = iptr->isn_arg.script.scriptref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002916 svar_T *sv;
2917
Bram Moolenaar6db660b2021-08-01 14:08:54 +02002918 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01002919 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002920 goto theend;
Bram Moolenaar859cc212022-03-28 15:22:35 +01002921 allocate_if_null(sv);
Bram Moolenaar35578162021-08-02 19:10:38 +02002922 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002923 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002924 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002925 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002926 }
2927 break;
2928
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002929 // load s: variable in old script or autoload import
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002930 case ISN_LOADS:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002931 case ISN_LOADEXPORT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002932 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002933 int sid = iptr->isn_arg.loadstore.ls_sid;
2934 hashtab_T *ht = &SCRIPT_VARS(sid);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002935 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002936 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002937
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002938 if (di == NULL)
2939 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002940 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002941 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002942 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002943 }
2944 else
2945 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002946 if (iptr->isn_type == ISN_LOADEXPORT)
2947 {
2948 int idx = get_script_item_idx(sid, name, 0,
2949 NULL, NULL);
2950 svar_T *sv;
2951
2952 if (idx >= 0)
2953 {
2954 sv = ((svar_T *)SCRIPT_ITEM(sid)
2955 ->sn_var_vals.ga_data) + idx;
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01002956 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002957 {
2958 SOURCING_LNUM = iptr->isn_lnum;
2959 semsg(_(e_item_not_exported_in_script_str),
2960 name);
2961 goto on_error;
2962 }
2963 }
2964 }
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(&di->di_tv, 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 }
2970 }
2971 break;
2972
Bram Moolenaard3aac292020-04-19 14:32:17 +02002973 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002974 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02002975 case ISN_LOADB:
2976 case ISN_LOADW:
2977 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002978 {
Bram Moolenaar06b77222022-01-25 15:51:56 +00002979 int res = load_namespace_var(ectx, iptr->isn_type, iptr);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002980
Bram Moolenaar06b77222022-01-25 15:51:56 +00002981 if (res == NOTDONE)
Bram Moolenaarcbbc48f2022-01-18 12:58:28 +00002982 goto theend;
Bram Moolenaar06b77222022-01-25 15:51:56 +00002983 if (res == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002984 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002985 }
Bram Moolenaar06b77222022-01-25 15:51:56 +00002986
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002987 break;
2988
Bram Moolenaar03290b82020-12-19 16:30:44 +01002989 // load autoload variable
2990 case ISN_LOADAUTO:
2991 {
2992 char_u *name = iptr->isn_arg.string;
2993
Bram Moolenaar35578162021-08-02 19:10:38 +02002994 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002995 goto theend;
Bram Moolenaar03290b82020-12-19 16:30:44 +01002996 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002997 if (eval_variable(name, (int)STRLEN(name), 0,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002998 STACK_TV_BOT(0), NULL, EVAL_VAR_VERBOSE) == FAIL)
Bram Moolenaar03290b82020-12-19 16:30:44 +01002999 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003000 ++ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003001 }
3002 break;
3003
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003004 // load g:/b:/w:/t: namespace
3005 case ISN_LOADGDICT:
3006 case ISN_LOADBDICT:
3007 case ISN_LOADWDICT:
3008 case ISN_LOADTDICT:
3009 {
3010 dict_T *d = NULL;
3011
3012 switch (iptr->isn_type)
3013 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02003014 case ISN_LOADGDICT: d = get_globvar_dict(); break;
3015 case ISN_LOADBDICT: d = curbuf->b_vars; break;
3016 case ISN_LOADWDICT: d = curwin->w_vars; break;
3017 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003018 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003019 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003020 }
Bram Moolenaar35578162021-08-02 19:10:38 +02003021 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003022 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003023 tv = STACK_TV_BOT(0);
3024 tv->v_type = VAR_DICT;
3025 tv->v_lock = 0;
3026 tv->vval.v_dict = d;
Bram Moolenaar1bd3cb22021-02-24 12:27:31 +01003027 ++d->dv_refcount;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003028 ++ectx->ec_stack.ga_len;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003029 }
3030 break;
3031
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003032 // load &option
3033 case ISN_LOADOPT:
3034 {
3035 typval_T optval;
3036 char_u *name = iptr->isn_arg.string;
3037
Bram Moolenaara8c17702020-04-01 21:17:24 +02003038 // This is not expected to fail, name is checked during
3039 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar35578162021-08-02 19:10:38 +02003040 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003041 goto theend;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003042 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003043 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003044 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003045 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003046 }
3047 break;
3048
3049 // load $ENV
3050 case ISN_LOADENV:
3051 {
3052 typval_T optval;
3053 char_u *name = iptr->isn_arg.string;
3054
Bram Moolenaar35578162021-08-02 19:10:38 +02003055 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003056 goto theend;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003057 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003058 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003059 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003060 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003061 }
3062 break;
3063
3064 // load @register
3065 case ISN_LOADREG:
Bram Moolenaar35578162021-08-02 19:10:38 +02003066 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003067 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003068 tv = STACK_TV_BOT(0);
3069 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003070 tv->v_lock = 0;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02003071 // This may result in NULL, which should be equivalent to an
3072 // empty string.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003073 tv->vval.v_string = get_reg_contents(
3074 iptr->isn_arg.number, GREG_EXPR_SRC);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003075 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003076 break;
3077
3078 // store local variable
3079 case ISN_STORE:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003080 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003081 tv = STACK_TV_VAR(iptr->isn_arg.number);
3082 clear_tv(tv);
3083 *tv = *STACK_TV_BOT(0);
3084 break;
3085
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003086 // store s: variable in old script or autoload import
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003087 case ISN_STORES:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003088 case ISN_STOREEXPORT:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003089 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003090 int sid = iptr->isn_arg.loadstore.ls_sid;
3091 hashtab_T *ht = &SCRIPT_VARS(sid);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003092 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003093 dictitem_T *di = find_var_in_ht(ht, 0,
3094 iptr->isn_type == ISN_STORES
3095 ? name + 2 : name, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003096
Bram Moolenaar4c137212021-04-19 16:48:48 +02003097 --ectx->ec_stack.ga_len;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003098 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003099 if (di == NULL)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003100 {
3101 if (iptr->isn_type == ISN_STOREEXPORT)
3102 {
3103 semsg(_(e_undefined_variable_str), name);
Bram Moolenaard1d26842022-03-31 10:13:47 +01003104 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003105 goto on_error;
3106 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003107 store_var(name, STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003108 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003109 else
3110 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003111 if (iptr->isn_type == ISN_STOREEXPORT)
3112 {
3113 int idx = get_script_item_idx(sid, name, 0,
3114 NULL, NULL);
3115
3116 if (idx >= 0)
3117 {
3118 svar_T *sv = ((svar_T *)SCRIPT_ITEM(sid)
3119 ->sn_var_vals.ga_data) + idx;
3120
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003121 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003122 {
3123 semsg(_(e_item_not_exported_in_script_str),
3124 name);
Bram Moolenaard1d26842022-03-31 10:13:47 +01003125 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003126 goto on_error;
3127 }
3128 }
3129 }
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02003130 if (var_check_permission(di, name) == FAIL)
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003131 {
3132 clear_tv(STACK_TV_BOT(0));
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02003133 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003134 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003135 clear_tv(&di->di_tv);
3136 di->di_tv = *STACK_TV_BOT(0);
3137 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003138 }
3139 break;
3140
3141 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003142 case ISN_STORESCRIPT:
3143 {
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003144 scriptref_T *sref = iptr->isn_arg.script.scriptref;
3145 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003146
Bram Moolenaar6db660b2021-08-01 14:08:54 +02003147 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003148 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003149 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003150 --ectx->ec_stack.ga_len;
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003151
3152 // "const" and "final" are checked at compile time, locking
3153 // the value needs to be checked here.
3154 SOURCING_LNUM = iptr->isn_lnum;
3155 if (value_check_lock(sv->sv_tv->v_lock, sv->sv_name, FALSE))
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003156 {
3157 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003158 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003159 }
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003160
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003161 clear_tv(sv->sv_tv);
3162 *sv->sv_tv = *STACK_TV_BOT(0);
3163 }
3164 break;
3165
3166 // store option
3167 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003168 case ISN_STOREFUNCOPT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003169 {
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003170 char_u *opt_name = iptr->isn_arg.storeopt.so_name;
3171 int opt_flags = iptr->isn_arg.storeopt.so_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003172 long n = 0;
3173 char_u *s = NULL;
3174 char *msg;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003175 char_u numbuf[NUMBUFLEN];
3176 char_u *tofree = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003177
Bram Moolenaar4c137212021-04-19 16:48:48 +02003178 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003179 tv = STACK_TV_BOT(0);
3180 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01003181 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003182 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01003183 if (s == NULL)
3184 s = (char_u *)"";
3185 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003186 else if (iptr->isn_type == ISN_STOREFUNCOPT)
3187 {
3188 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003189 // If the option can be set to a function reference or
3190 // a lambda and the passed value is a function
3191 // reference, then convert it to the name (string) of
3192 // the function reference.
3193 s = tv2string(tv, &tofree, numbuf, 0);
3194 if (s == NULL || *s == NUL)
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003195 {
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003196 // cannot happen?
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003197 clear_tv(tv);
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003198 vim_free(tofree);
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003199 goto on_error;
3200 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003201 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003202 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003203 // must be VAR_NUMBER, CHECKTYPE makes sure
3204 n = tv->vval.v_number;
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003205 msg = set_option_value(opt_name, n, s, opt_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02003206 clear_tv(tv);
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003207 vim_free(tofree);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003208 if (msg != NULL)
3209 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003210 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003211 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02003212 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003213 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003214 }
3215 break;
3216
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003217 // store $ENV
3218 case ISN_STOREENV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003219 --ectx->ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003220 tv = STACK_TV_BOT(0);
3221 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
3222 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003223 break;
3224
3225 // store @r
3226 case ISN_STOREREG:
3227 {
3228 int reg = iptr->isn_arg.number;
3229
Bram Moolenaar4c137212021-04-19 16:48:48 +02003230 --ectx->ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01003231 tv = STACK_TV_BOT(0);
Bram Moolenaar74f4a962021-06-17 21:03:07 +02003232 write_reg_contents(reg, tv_get_string(tv), -1, FALSE);
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01003233 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003234 }
3235 break;
3236
3237 // store v: variable
3238 case ISN_STOREV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003239 --ectx->ec_stack.ga_len;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003240 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
3241 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003242 // should not happen, type is checked when compiling
3243 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003244 break;
3245
Bram Moolenaard3aac292020-04-19 14:32:17 +02003246 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003247 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02003248 case ISN_STOREB:
3249 case ISN_STOREW:
3250 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003251 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003252 dictitem_T *di;
3253 hashtab_T *ht;
3254 char_u *name = iptr->isn_arg.string + 2;
3255
Bram Moolenaard3aac292020-04-19 14:32:17 +02003256 switch (iptr->isn_type)
3257 {
3258 case ISN_STOREG:
3259 ht = get_globvar_ht();
3260 break;
3261 case ISN_STOREB:
3262 ht = &curbuf->b_vars->dv_hashtab;
3263 break;
3264 case ISN_STOREW:
3265 ht = &curwin->w_vars->dv_hashtab;
3266 break;
3267 case ISN_STORET:
3268 ht = &curtab->tp_vars->dv_hashtab;
3269 break;
3270 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003271 goto theend;
Bram Moolenaard3aac292020-04-19 14:32:17 +02003272 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003273
Bram Moolenaar4c137212021-04-19 16:48:48 +02003274 --ectx->ec_stack.ga_len;
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003275 di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003276 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003277 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003278 else
3279 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003280 SOURCING_LNUM = iptr->isn_lnum;
3281 if (var_check_permission(di, name) == FAIL)
3282 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003283 clear_tv(&di->di_tv);
3284 di->di_tv = *STACK_TV_BOT(0);
3285 }
3286 }
3287 break;
3288
Bram Moolenaar03290b82020-12-19 16:30:44 +01003289 // store an autoload variable
3290 case ISN_STOREAUTO:
3291 SOURCING_LNUM = iptr->isn_lnum;
3292 set_var(iptr->isn_arg.string, STACK_TV_BOT(-1), TRUE);
3293 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003294 --ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003295 break;
3296
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003297 // store number in local variable
3298 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01003299 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003300 clear_tv(tv);
3301 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01003302 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003303 break;
3304
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01003305 // store value in list or dict variable
3306 case ISN_STOREINDEX:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003307 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003308 int res = execute_storeindex(iptr, ectx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003309
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003310 if (res == FAIL)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02003311 goto on_error;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003312 if (res == NOTDONE)
3313 goto theend;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003314 }
3315 break;
3316
Bram Moolenaarea5c8982022-02-17 14:42:02 +00003317 // store value in list or blob range
Bram Moolenaar68452172021-04-12 21:21:02 +02003318 case ISN_STORERANGE:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003319 if (execute_storerange(iptr, ectx) == FAIL)
3320 goto on_error;
Bram Moolenaar68452172021-04-12 21:21:02 +02003321 break;
3322
Bram Moolenaar0186e582021-01-10 18:33:11 +01003323 // load or store variable or argument from outer scope
3324 case ISN_LOADOUTER:
3325 case ISN_STOREOUTER:
3326 {
3327 int depth = iptr->isn_arg.outer.outer_depth;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02003328 outer_T *outer = ectx->ec_outer_ref == NULL ? NULL
3329 : ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003330
3331 while (depth > 1 && outer != NULL)
3332 {
3333 outer = outer->out_up;
3334 --depth;
3335 }
3336 if (outer == NULL)
3337 {
3338 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar69c76172021-12-02 16:38:52 +00003339 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx
3340 || ectx->ec_outer_ref == NULL)
3341 // Possibly :def function called from legacy
3342 // context.
3343 emsg(_(e_closure_called_from_invalid_context));
3344 else
3345 iemsg("LOADOUTER depth more than scope levels");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003346 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003347 }
3348 tv = ((typval_T *)outer->out_stack->ga_data)
3349 + outer->out_frame_idx + STACK_FRAME_SIZE
3350 + iptr->isn_arg.outer.outer_idx;
3351 if (iptr->isn_type == ISN_LOADOUTER)
3352 {
Bram Moolenaar35578162021-08-02 19:10:38 +02003353 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003354 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003355 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003356 ++ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003357 }
3358 else
3359 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003360 --ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003361 clear_tv(tv);
3362 *tv = *STACK_TV_BOT(0);
3363 }
3364 }
3365 break;
3366
Bram Moolenaar752fc692021-01-04 21:57:11 +01003367 // unlet item in list or dict variable
3368 case ISN_UNLETINDEX:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003369 if (execute_unletindex(iptr, ectx) == FAIL)
3370 goto on_error;
Bram Moolenaar752fc692021-01-04 21:57:11 +01003371 break;
3372
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01003373 // unlet range of items in list variable
3374 case ISN_UNLETRANGE:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003375 if (execute_unletrange(iptr, ectx) == FAIL)
3376 goto on_error;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01003377 break;
3378
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003379 // push constant
3380 case ISN_PUSHNR:
3381 case ISN_PUSHBOOL:
3382 case ISN_PUSHSPEC:
3383 case ISN_PUSHF:
3384 case ISN_PUSHS:
3385 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003386 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003387 case ISN_PUSHCHANNEL:
3388 case ISN_PUSHJOB:
Bram Moolenaar35578162021-08-02 19:10:38 +02003389 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003390 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003391 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003392 tv->v_lock = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003393 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003394 switch (iptr->isn_type)
3395 {
3396 case ISN_PUSHNR:
3397 tv->v_type = VAR_NUMBER;
3398 tv->vval.v_number = iptr->isn_arg.number;
3399 break;
3400 case ISN_PUSHBOOL:
3401 tv->v_type = VAR_BOOL;
3402 tv->vval.v_number = iptr->isn_arg.number;
3403 break;
3404 case ISN_PUSHSPEC:
3405 tv->v_type = VAR_SPECIAL;
3406 tv->vval.v_number = iptr->isn_arg.number;
3407 break;
3408#ifdef FEAT_FLOAT
3409 case ISN_PUSHF:
3410 tv->v_type = VAR_FLOAT;
3411 tv->vval.v_float = iptr->isn_arg.fnumber;
3412 break;
3413#endif
3414 case ISN_PUSHBLOB:
3415 blob_copy(iptr->isn_arg.blob, tv);
3416 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003417 case ISN_PUSHFUNC:
3418 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01003419 if (iptr->isn_arg.string == NULL)
3420 tv->vval.v_string = NULL;
3421 else
3422 tv->vval.v_string =
3423 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003424 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003425 case ISN_PUSHCHANNEL:
3426#ifdef FEAT_JOB_CHANNEL
3427 tv->v_type = VAR_CHANNEL;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003428 tv->vval.v_channel = NULL;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003429#endif
3430 break;
3431 case ISN_PUSHJOB:
3432#ifdef FEAT_JOB_CHANNEL
3433 tv->v_type = VAR_JOB;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003434 tv->vval.v_job = NULL;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003435#endif
3436 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003437 default:
3438 tv->v_type = VAR_STRING;
Bram Moolenaarf8691002022-03-10 12:20:53 +00003439 tv->vval.v_string = iptr->isn_arg.string == NULL
3440 ? NULL : vim_strsave(iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003441 }
3442 break;
3443
Bram Moolenaar06b77222022-01-25 15:51:56 +00003444 case ISN_AUTOLOAD:
3445 {
3446 char_u *name = iptr->isn_arg.string;
3447
3448 (void)script_autoload(name, FALSE);
3449 if (find_func(name, TRUE))
3450 {
3451 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
3452 goto theend;
3453 tv = STACK_TV_BOT(0);
3454 tv->v_lock = 0;
3455 ++ectx->ec_stack.ga_len;
3456 tv->v_type = VAR_FUNC;
3457 tv->vval.v_string = vim_strsave(name);
3458 }
3459 else
3460 {
3461 int res = load_namespace_var(ectx, ISN_LOADG, iptr);
3462
3463 if (res == NOTDONE)
3464 goto theend;
3465 if (res == FAIL)
3466 goto on_error;
3467 }
3468 }
3469 break;
3470
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003471 case ISN_UNLET:
3472 if (do_unlet(iptr->isn_arg.unlet.ul_name,
3473 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003474 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003475 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02003476 case ISN_UNLETENV:
3477 vim_unsetenv(iptr->isn_arg.unlet.ul_name);
3478 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003479
Bram Moolenaaraacc9662021-08-13 19:40:51 +02003480 case ISN_LOCKUNLOCK:
3481 {
3482 typval_T *lval_root_save = lval_root;
3483 int res;
3484
3485 // Stack has the local variable, argument the whole :lock
3486 // or :unlock command, like ISN_EXEC.
3487 --ectx->ec_stack.ga_len;
3488 lval_root = STACK_TV_BOT(0);
3489 res = exec_command(iptr);
3490 clear_tv(lval_root);
3491 lval_root = lval_root_save;
3492 if (res == FAIL)
3493 goto on_error;
3494 }
3495 break;
3496
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02003497 case ISN_LOCKCONST:
3498 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
3499 break;
3500
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003501 // create a list from items on the stack; uses a single allocation
3502 // for the list header and the items
3503 case ISN_NEWLIST:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003504 if (exe_newlist(iptr->isn_arg.number, ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003505 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003506 break;
3507
3508 // create a dict from items on the stack
3509 case ISN_NEWDICT:
3510 {
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01003511 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003512
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01003513 SOURCING_LNUM = iptr->isn_lnum;
3514 res = exe_newdict(iptr->isn_arg.number, ectx);
3515 if (res == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003516 goto theend;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01003517 if (res == MAYBE)
3518 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003519 }
3520 break;
3521
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003522 // create a partial with NULL value
3523 case ISN_NEWPARTIAL:
3524 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
3525 goto theend;
3526 ++ectx->ec_stack.ga_len;
3527 tv = STACK_TV_BOT(-1);
3528 tv->v_type = VAR_PARTIAL;
3529 tv->v_lock = 0;
3530 tv->vval.v_partial = NULL;
3531 break;
3532
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003533 // call a :def function
3534 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02003535 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003536 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
3537 NULL,
3538 iptr->isn_arg.dfunc.cdf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003539 ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003540 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003541 break;
3542
3543 // call a builtin function
3544 case ISN_BCALL:
3545 SOURCING_LNUM = iptr->isn_lnum;
3546 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
3547 iptr->isn_arg.bfunc.cbf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003548 ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02003549 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003550 break;
3551
3552 // call a funcref or partial
3553 case ISN_PCALL:
3554 {
3555 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
3556 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003557 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003558
3559 SOURCING_LNUM = iptr->isn_lnum;
3560 if (pfunc->cpf_top)
3561 {
3562 // funcref is above the arguments
3563 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
3564 }
3565 else
3566 {
3567 // Get the funcref from the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02003568 --ectx->ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003569 partial_tv = *STACK_TV_BOT(0);
3570 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003571 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003572 r = call_partial(tv, pfunc->cpf_argcount, ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003573 if (tv == &partial_tv)
3574 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003575 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02003576 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003577 }
3578 break;
3579
Bram Moolenaarbd5da372020-03-31 23:13:10 +02003580 case ISN_PCALL_END:
3581 // PCALL finished, arguments have been consumed and replaced by
3582 // the return value. Now clear the funcref from the stack,
3583 // and move the return value in its place.
Bram Moolenaar4c137212021-04-19 16:48:48 +02003584 --ectx->ec_stack.ga_len;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02003585 clear_tv(STACK_TV_BOT(-1));
3586 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
3587 break;
3588
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003589 // call a user defined function or funcref/partial
3590 case ISN_UCALL:
3591 {
3592 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
3593
3594 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003595 if (call_eval_func(cufunc->cuf_name, cufunc->cuf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003596 ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02003597 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003598 }
3599 break;
3600
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02003601 // return from a :def function call without a value
3602 case ISN_RETURN_VOID:
Bram Moolenaar35578162021-08-02 19:10:38 +02003603 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003604 goto theend;
Bram Moolenaar299f3032021-01-08 20:53:09 +01003605 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003606 ++ectx->ec_stack.ga_len;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02003607 tv->v_type = VAR_VOID;
Bram Moolenaar299f3032021-01-08 20:53:09 +01003608 tv->vval.v_number = 0;
3609 tv->v_lock = 0;
3610 // FALLTHROUGH
3611
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02003612 // return from a :def function call with what is on the stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003613 case ISN_RETURN:
3614 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003615 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003616 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01003617
3618 if (trystack->ga_len > 0)
3619 trycmd = ((trycmd_T *)trystack->ga_data)
3620 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003621 if (trycmd != NULL
Bram Moolenaar4c137212021-04-19 16:48:48 +02003622 && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003623 {
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01003624 // jump to ":finally" or ":endtry"
3625 if (trycmd->tcd_finally_idx != 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02003626 ectx->ec_iidx = trycmd->tcd_finally_idx;
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01003627 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02003628 ectx->ec_iidx = trycmd->tcd_endtry_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003629 trycmd->tcd_return = TRUE;
3630 }
3631 else
Bram Moolenaard032f342020-07-18 18:13:02 +02003632 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003633 }
3634 break;
3635
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02003636 // push a partial, a reference to a compiled function
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003637 case ISN_FUNCREF:
3638 {
Bram Moolenaarf112f302020-12-20 17:47:52 +01003639 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar38453522021-11-28 22:00:12 +00003640 ufunc_T *ufunc;
3641 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003642
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003643 if (pt == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003644 goto theend;
Bram Moolenaar35578162021-08-02 19:10:38 +02003645 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003646 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003647 vim_free(pt);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003648 goto theend;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003649 }
Bram Moolenaar38453522021-11-28 22:00:12 +00003650 if (funcref->fr_func_name == NULL)
3651 {
3652 dfunc_T *pt_dfunc = ((dfunc_T *)def_functions.ga_data)
3653 + funcref->fr_dfunc_idx;
3654
3655 ufunc = pt_dfunc->df_ufunc;
3656 }
3657 else
3658 {
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003659 ufunc = find_func(funcref->fr_func_name, FALSE);
Bram Moolenaar38453522021-11-28 22:00:12 +00003660 }
Bram Moolenaar56acd1f2022-02-18 13:24:52 +00003661 if (ufunc == NULL)
3662 {
3663 SOURCING_LNUM = iptr->isn_lnum;
3664 iemsg("ufunc unexpectedly NULL for FUNCREF");
3665 goto theend;
3666 }
Bram Moolenaar38453522021-11-28 22:00:12 +00003667 if (fill_partial_and_closure(pt, ufunc, ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003668 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003669 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003670 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003671 tv->vval.v_partial = pt;
3672 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003673 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003674 }
3675 break;
3676
Bram Moolenaar38ddf332020-07-31 22:05:04 +02003677 // Create a global function from a lambda.
3678 case ISN_NEWFUNC:
3679 {
3680 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
3681
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01003682 if (copy_func(newfunc->nf_lambda, newfunc->nf_global,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003683 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003684 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02003685 }
3686 break;
3687
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01003688 // List functions
3689 case ISN_DEF:
3690 if (iptr->isn_arg.string == NULL)
3691 list_functions(NULL);
3692 else
3693 {
Bram Moolenaar14336722022-01-08 16:02:59 +00003694 exarg_T ea;
3695 garray_T lines_to_free;
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01003696
3697 CLEAR_FIELD(ea);
3698 ea.cmd = ea.arg = iptr->isn_arg.string;
Bram Moolenaar14336722022-01-08 16:02:59 +00003699 ga_init2(&lines_to_free, sizeof(char_u *), 50);
3700 define_function(&ea, NULL, &lines_to_free);
3701 ga_clear_strings(&lines_to_free);
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01003702 }
3703 break;
3704
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003705 // jump if a condition is met
3706 case ISN_JUMP:
3707 {
3708 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003709 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003710 int jump = TRUE;
3711
3712 if (when != JUMP_ALWAYS)
3713 {
3714 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003715 if (when == JUMP_IF_COND_FALSE
Bram Moolenaar13106602020-10-04 16:06:05 +02003716 || when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003717 || when == JUMP_IF_COND_TRUE)
3718 {
3719 SOURCING_LNUM = iptr->isn_lnum;
3720 jump = tv_get_bool_chk(tv, &error);
3721 if (error)
3722 goto on_error;
3723 }
3724 else
3725 jump = tv2bool(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003726 if (when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003727 || when == JUMP_AND_KEEP_IF_FALSE
3728 || when == JUMP_IF_COND_FALSE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003729 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01003730 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003731 {
3732 // drop the value from the stack
3733 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003734 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003735 }
3736 }
3737 if (jump)
Bram Moolenaar4c137212021-04-19 16:48:48 +02003738 ectx->ec_iidx = iptr->isn_arg.jump.jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003739 }
3740 break;
3741
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02003742 // Jump if an argument with a default value was already set and not
3743 // v:none.
3744 case ISN_JUMP_IF_ARG_SET:
3745 tv = STACK_TV_VAR(iptr->isn_arg.jumparg.jump_arg_off);
3746 if (tv->v_type != VAR_UNKNOWN
3747 && !(tv->v_type == VAR_SPECIAL
3748 && tv->vval.v_number == VVAL_NONE))
Bram Moolenaar4c137212021-04-19 16:48:48 +02003749 ectx->ec_iidx = iptr->isn_arg.jumparg.jump_where;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02003750 break;
3751
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003752 // top of a for loop
3753 case ISN_FOR:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003754 if (execute_for(iptr, ectx) == FAIL)
3755 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003756 break;
3757
3758 // start of ":try" block
3759 case ISN_TRY:
3760 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01003761 trycmd_T *trycmd = NULL;
3762
Bram Moolenaar35578162021-08-02 19:10:38 +02003763 if (GA_GROW_FAILS(&ectx->ec_trystack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003764 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003765 trycmd = ((trycmd_T *)ectx->ec_trystack.ga_data)
3766 + ectx->ec_trystack.ga_len;
3767 ++ectx->ec_trystack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003768 ++trylevel;
Bram Moolenaar8d4be892021-02-13 18:33:02 +01003769 CLEAR_POINTER(trycmd);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003770 trycmd->tcd_frame_idx = ectx->ec_frame_idx;
3771 trycmd->tcd_stack_len = ectx->ec_stack.ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01003772 trycmd->tcd_catch_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00003773 iptr->isn_arg.tryref.try_ref->try_catch;
Bram Moolenaara91a7132021-03-25 21:12:15 +01003774 trycmd->tcd_finally_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00003775 iptr->isn_arg.tryref.try_ref->try_finally;
Bram Moolenaara91a7132021-03-25 21:12:15 +01003776 trycmd->tcd_endtry_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00003777 iptr->isn_arg.tryref.try_ref->try_endtry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003778 }
3779 break;
3780
3781 case ISN_PUSHEXC:
3782 if (current_exception == NULL)
3783 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003784 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003785 iemsg("Evaluating catch while current_exception is NULL");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003786 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003787 }
Bram Moolenaar35578162021-08-02 19:10:38 +02003788 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003789 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003790 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003791 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003792 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003793 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003794 tv->vval.v_string = vim_strsave(
3795 (char_u *)current_exception->value);
3796 break;
3797
3798 case ISN_CATCH:
3799 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003800 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003801
Bram Moolenaar4c137212021-04-19 16:48:48 +02003802 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003803 if (trystack->ga_len > 0)
3804 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01003805 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003806 + trystack->ga_len - 1;
3807 trycmd->tcd_caught = TRUE;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003808 trycmd->tcd_did_throw = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003809 }
3810 did_emsg = got_int = did_throw = FALSE;
Bram Moolenaar1430cee2021-01-17 19:20:32 +01003811 force_abort = need_rethrow = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003812 catch_exception(current_exception);
3813 }
3814 break;
3815
Bram Moolenaarc150c092021-02-13 15:02:46 +01003816 case ISN_TRYCONT:
3817 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003818 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003819 trycont_T *trycont = &iptr->isn_arg.trycont;
3820 int i;
3821 trycmd_T *trycmd;
3822 int iidx = trycont->tct_where;
3823
3824 if (trystack->ga_len < trycont->tct_levels)
3825 {
3826 siemsg("TRYCONT: expected %d levels, found %d",
3827 trycont->tct_levels, trystack->ga_len);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003828 goto theend;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003829 }
3830 // Make :endtry jump to any outer try block and the last
3831 // :endtry inside the loop to the loop start.
3832 for (i = trycont->tct_levels; i > 0; --i)
3833 {
3834 trycmd = ((trycmd_T *)trystack->ga_data)
3835 + trystack->ga_len - i;
Bram Moolenaar2e34c342021-03-14 12:13:33 +01003836 // Add one to tcd_cont to be able to jump to
3837 // instruction with index zero.
3838 trycmd->tcd_cont = iidx + 1;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003839 iidx = trycmd->tcd_finally_idx == 0
3840 ? trycmd->tcd_endtry_idx : trycmd->tcd_finally_idx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003841 }
3842 // jump to :finally or :endtry of current try statement
Bram Moolenaar4c137212021-04-19 16:48:48 +02003843 ectx->ec_iidx = iidx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003844 }
3845 break;
3846
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003847 case ISN_FINALLY:
3848 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003849 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003850 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
3851 + trystack->ga_len - 1;
3852
3853 // Reset the index to avoid a return statement jumps here
3854 // again.
3855 trycmd->tcd_finally_idx = 0;
3856 break;
3857 }
3858
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003859 // end of ":try" block
3860 case ISN_ENDTRY:
3861 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003862 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003863
3864 if (trystack->ga_len > 0)
3865 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003866 trycmd_T *trycmd;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003867
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003868 --trystack->ga_len;
3869 --trylevel;
3870 trycmd = ((trycmd_T *)trystack->ga_data)
3871 + trystack->ga_len;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003872 if (trycmd->tcd_did_throw)
3873 did_throw = TRUE;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01003874 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003875 {
3876 // discard the exception
3877 if (caught_stack == current_exception)
3878 caught_stack = caught_stack->caught;
3879 discard_current_exception();
3880 }
3881
3882 if (trycmd->tcd_return)
Bram Moolenaard032f342020-07-18 18:13:02 +02003883 goto func_return;
Bram Moolenaard9d77892021-02-12 21:32:47 +01003884
Bram Moolenaar4c137212021-04-19 16:48:48 +02003885 while (ectx->ec_stack.ga_len > trycmd->tcd_stack_len)
Bram Moolenaard9d77892021-02-12 21:32:47 +01003886 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003887 --ectx->ec_stack.ga_len;
Bram Moolenaard9d77892021-02-12 21:32:47 +01003888 clear_tv(STACK_TV_BOT(0));
3889 }
Bram Moolenaar8d4be892021-02-13 18:33:02 +01003890 if (trycmd->tcd_cont != 0)
Bram Moolenaarc150c092021-02-13 15:02:46 +01003891 // handling :continue: jump to outer try block or
3892 // start of the loop
Bram Moolenaar4c137212021-04-19 16:48:48 +02003893 ectx->ec_iidx = trycmd->tcd_cont - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003894 }
3895 }
3896 break;
3897
3898 case ISN_THROW:
Bram Moolenaar8f81b222021-01-14 21:47:06 +01003899 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003900 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02003901
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003902 if (trystack->ga_len == 0 && trylevel == 0 && emsg_silent)
3903 {
3904 // throwing an exception while using "silent!" causes
3905 // the function to abort but not display an error.
3906 tv = STACK_TV_BOT(-1);
3907 clear_tv(tv);
3908 tv->v_type = VAR_NUMBER;
3909 tv->vval.v_number = 0;
3910 goto done;
3911 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003912 --ectx->ec_stack.ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003913 tv = STACK_TV_BOT(0);
3914 if (tv->vval.v_string == NULL
3915 || *skipwhite(tv->vval.v_string) == NUL)
3916 {
3917 vim_free(tv->vval.v_string);
3918 SOURCING_LNUM = iptr->isn_lnum;
3919 emsg(_(e_throw_with_empty_string));
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003920 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003921 }
3922
3923 // Inside a "catch" we need to first discard the caught
3924 // exception.
3925 if (trystack->ga_len > 0)
3926 {
3927 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
3928 + trystack->ga_len - 1;
3929 if (trycmd->tcd_caught && current_exception != NULL)
3930 {
3931 // discard the exception
3932 if (caught_stack == current_exception)
3933 caught_stack = caught_stack->caught;
3934 discard_current_exception();
3935 trycmd->tcd_caught = FALSE;
3936 }
3937 }
3938
Bram Moolenaar90a57162022-02-12 14:23:17 +00003939 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003940 if (throw_exception(tv->vval.v_string, ET_USER, NULL)
3941 == FAIL)
3942 {
3943 vim_free(tv->vval.v_string);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003944 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003945 }
3946 did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003947 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003948 break;
3949
3950 // compare with special values
3951 case ISN_COMPAREBOOL:
3952 case ISN_COMPARESPECIAL:
3953 {
3954 typval_T *tv1 = STACK_TV_BOT(-2);
3955 typval_T *tv2 = STACK_TV_BOT(-1);
3956 varnumber_T arg1 = tv1->vval.v_number;
3957 varnumber_T arg2 = tv2->vval.v_number;
3958 int res;
3959
3960 switch (iptr->isn_arg.op.op_type)
3961 {
3962 case EXPR_EQUAL: res = arg1 == arg2; break;
3963 case EXPR_NEQUAL: res = arg1 != arg2; break;
3964 default: res = 0; break;
3965 }
3966
Bram Moolenaar4c137212021-04-19 16:48:48 +02003967 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003968 tv1->v_type = VAR_BOOL;
3969 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
3970 }
3971 break;
3972
Bram Moolenaar7a222242022-03-01 19:23:24 +00003973 case ISN_COMPARENULL:
3974 {
3975 typval_T *tv1 = STACK_TV_BOT(-2);
3976 typval_T *tv2 = STACK_TV_BOT(-1);
3977 int res;
3978
3979 res = typval_compare_null(tv1, tv2);
3980 if (res == MAYBE)
3981 goto on_error;
3982 if (iptr->isn_arg.op.op_type == EXPR_NEQUAL)
3983 res = !res;
3984 clear_tv(tv1);
3985 clear_tv(tv2);
3986 --ectx->ec_stack.ga_len;
3987 tv1->v_type = VAR_BOOL;
3988 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
3989 }
3990 break;
3991
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003992 // Operation with two number arguments
3993 case ISN_OPNR:
3994 case ISN_COMPARENR:
3995 {
3996 typval_T *tv1 = STACK_TV_BOT(-2);
3997 typval_T *tv2 = STACK_TV_BOT(-1);
3998 varnumber_T arg1 = tv1->vval.v_number;
3999 varnumber_T arg2 = tv2->vval.v_number;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004000 varnumber_T res = 0;
4001 int div_zero = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004002
4003 switch (iptr->isn_arg.op.op_type)
4004 {
4005 case EXPR_MULT: res = arg1 * arg2; break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004006 case EXPR_DIV: if (arg2 == 0)
4007 div_zero = TRUE;
4008 else
4009 res = arg1 / arg2;
4010 break;
4011 case EXPR_REM: if (arg2 == 0)
4012 div_zero = TRUE;
4013 else
4014 res = arg1 % arg2;
4015 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004016 case EXPR_SUB: res = arg1 - arg2; break;
4017 case EXPR_ADD: res = arg1 + arg2; break;
4018
4019 case EXPR_EQUAL: res = arg1 == arg2; break;
4020 case EXPR_NEQUAL: res = arg1 != arg2; break;
4021 case EXPR_GREATER: res = arg1 > arg2; break;
4022 case EXPR_GEQUAL: res = arg1 >= arg2; break;
4023 case EXPR_SMALLER: res = arg1 < arg2; break;
4024 case EXPR_SEQUAL: res = arg1 <= arg2; break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004025 default: break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004026 }
4027
Bram Moolenaar4c137212021-04-19 16:48:48 +02004028 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004029 if (iptr->isn_type == ISN_COMPARENR)
4030 {
4031 tv1->v_type = VAR_BOOL;
4032 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4033 }
4034 else
4035 tv1->vval.v_number = res;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004036 if (div_zero)
4037 {
4038 SOURCING_LNUM = iptr->isn_lnum;
4039 emsg(_(e_divide_by_zero));
4040 goto on_error;
4041 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004042 }
4043 break;
4044
4045 // Computation with two float arguments
4046 case ISN_OPFLOAT:
4047 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01004048#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004049 {
4050 typval_T *tv1 = STACK_TV_BOT(-2);
4051 typval_T *tv2 = STACK_TV_BOT(-1);
4052 float_T arg1 = tv1->vval.v_float;
4053 float_T arg2 = tv2->vval.v_float;
4054 float_T res = 0;
4055 int cmp = FALSE;
4056
4057 switch (iptr->isn_arg.op.op_type)
4058 {
4059 case EXPR_MULT: res = arg1 * arg2; break;
4060 case EXPR_DIV: res = arg1 / arg2; break;
4061 case EXPR_SUB: res = arg1 - arg2; break;
4062 case EXPR_ADD: res = arg1 + arg2; break;
4063
4064 case EXPR_EQUAL: cmp = arg1 == arg2; break;
4065 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
4066 case EXPR_GREATER: cmp = arg1 > arg2; break;
4067 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
4068 case EXPR_SMALLER: cmp = arg1 < arg2; break;
4069 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
4070 default: cmp = 0; break;
4071 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004072 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004073 if (iptr->isn_type == ISN_COMPAREFLOAT)
4074 {
4075 tv1->v_type = VAR_BOOL;
4076 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
4077 }
4078 else
4079 tv1->vval.v_float = res;
4080 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01004081#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004082 break;
4083
4084 case ISN_COMPARELIST:
Bram Moolenaar265f8112021-12-19 12:33:05 +00004085 case ISN_COMPAREDICT:
4086 case ISN_COMPAREFUNC:
4087 case ISN_COMPARESTRING:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004088 case ISN_COMPAREBLOB:
4089 {
4090 typval_T *tv1 = STACK_TV_BOT(-2);
4091 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar265f8112021-12-19 12:33:05 +00004092 exprtype_T exprtype = iptr->isn_arg.op.op_type;
4093 int ic = iptr->isn_arg.op.op_ic;
4094 int res = FALSE;
4095 int status = OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004096
Bram Moolenaar265f8112021-12-19 12:33:05 +00004097 SOURCING_LNUM = iptr->isn_lnum;
4098 if (iptr->isn_type == ISN_COMPARELIST)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004099 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00004100 status = typval_compare_list(tv1, tv2,
4101 exprtype, ic, &res);
4102 }
4103 else if (iptr->isn_type == ISN_COMPAREDICT)
4104 {
4105 status = typval_compare_dict(tv1, tv2,
4106 exprtype, ic, &res);
4107 }
4108 else if (iptr->isn_type == ISN_COMPAREFUNC)
4109 {
4110 status = typval_compare_func(tv1, tv2,
4111 exprtype, ic, &res);
4112 }
4113 else if (iptr->isn_type == ISN_COMPARESTRING)
4114 {
4115 status = typval_compare_string(tv1, tv2,
4116 exprtype, ic, &res);
4117 }
4118 else
4119 {
4120 status = typval_compare_blob(tv1, tv2, exprtype, &res);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004121 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004122 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004123 clear_tv(tv1);
4124 clear_tv(tv2);
4125 tv1->v_type = VAR_BOOL;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004126 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4127 if (status == FAIL)
4128 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004129 }
4130 break;
4131
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004132 case ISN_COMPAREANY:
4133 {
4134 typval_T *tv1 = STACK_TV_BOT(-2);
4135 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar657137c2021-01-09 15:45:23 +01004136 exprtype_T exprtype = iptr->isn_arg.op.op_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004137 int ic = iptr->isn_arg.op.op_ic;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004138 int status;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004139
Bram Moolenaareb26f432020-09-14 16:50:05 +02004140 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004141 status = typval_compare(tv1, tv2, exprtype, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004142 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004143 --ectx->ec_stack.ga_len;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004144 if (status == FAIL)
4145 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004146 }
4147 break;
4148
4149 case ISN_ADDLIST:
4150 case ISN_ADDBLOB:
4151 {
4152 typval_T *tv1 = STACK_TV_BOT(-2);
4153 typval_T *tv2 = STACK_TV_BOT(-1);
4154
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004155 // add two lists or blobs
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004156 if (iptr->isn_type == ISN_ADDLIST)
Bram Moolenaar07802042021-09-09 23:01:14 +02004157 {
4158 if (iptr->isn_arg.op.op_type == EXPR_APPEND
4159 && tv1->vval.v_list != NULL)
4160 list_extend(tv1->vval.v_list, tv2->vval.v_list,
4161 NULL);
4162 else
4163 eval_addlist(tv1, tv2);
4164 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004165 else
4166 eval_addblob(tv1, tv2);
4167 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004168 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004169 }
4170 break;
4171
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004172 case ISN_LISTAPPEND:
4173 {
4174 typval_T *tv1 = STACK_TV_BOT(-2);
4175 typval_T *tv2 = STACK_TV_BOT(-1);
4176 list_T *l = tv1->vval.v_list;
4177
4178 // add an item to a list
Bram Moolenaar1f4a3452022-01-01 18:29:21 +00004179 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004180 if (l == NULL)
4181 {
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004182 emsg(_(e_cannot_add_to_null_list));
4183 goto on_error;
4184 }
Bram Moolenaar1f4a3452022-01-01 18:29:21 +00004185 if (value_check_lock(l->lv_lock, NULL, FALSE))
4186 goto on_error;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004187 if (list_append_tv(l, tv2) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004188 goto theend;
Bram Moolenaar955347c2020-10-19 23:01:46 +02004189 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004190 --ectx->ec_stack.ga_len;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004191 }
4192 break;
4193
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02004194 case ISN_BLOBAPPEND:
4195 {
4196 typval_T *tv1 = STACK_TV_BOT(-2);
4197 typval_T *tv2 = STACK_TV_BOT(-1);
4198 blob_T *b = tv1->vval.v_blob;
4199 int error = FALSE;
4200 varnumber_T n;
4201
4202 // add a number to a blob
4203 if (b == NULL)
4204 {
4205 SOURCING_LNUM = iptr->isn_lnum;
4206 emsg(_(e_cannot_add_to_null_blob));
4207 goto on_error;
4208 }
4209 n = tv_get_number_chk(tv2, &error);
4210 if (error)
4211 goto on_error;
4212 ga_append(&b->bv_ga, (int)n);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004213 --ectx->ec_stack.ga_len;
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02004214 }
4215 break;
4216
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004217 // Computation with two arguments of unknown type
4218 case ISN_OPANY:
4219 {
4220 typval_T *tv1 = STACK_TV_BOT(-2);
4221 typval_T *tv2 = STACK_TV_BOT(-1);
4222 varnumber_T n1, n2;
4223#ifdef FEAT_FLOAT
4224 float_T f1 = 0, f2 = 0;
4225#endif
4226 int error = FALSE;
4227
4228 if (iptr->isn_arg.op.op_type == EXPR_ADD)
4229 {
4230 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
4231 {
4232 eval_addlist(tv1, tv2);
4233 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004234 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004235 break;
4236 }
4237 else if (tv1->v_type == VAR_BLOB
4238 && tv2->v_type == VAR_BLOB)
4239 {
4240 eval_addblob(tv1, tv2);
4241 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004242 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004243 break;
4244 }
4245 }
4246#ifdef FEAT_FLOAT
4247 if (tv1->v_type == VAR_FLOAT)
4248 {
4249 f1 = tv1->vval.v_float;
4250 n1 = 0;
4251 }
4252 else
4253#endif
4254 {
Bram Moolenaarf665e972020-12-05 19:17:16 +01004255 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004256 n1 = tv_get_number_chk(tv1, &error);
4257 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02004258 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004259#ifdef FEAT_FLOAT
4260 if (tv2->v_type == VAR_FLOAT)
4261 f1 = n1;
4262#endif
4263 }
4264#ifdef FEAT_FLOAT
4265 if (tv2->v_type == VAR_FLOAT)
4266 {
4267 f2 = tv2->vval.v_float;
4268 n2 = 0;
4269 }
4270 else
4271#endif
4272 {
4273 n2 = tv_get_number_chk(tv2, &error);
4274 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02004275 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004276#ifdef FEAT_FLOAT
4277 if (tv1->v_type == VAR_FLOAT)
4278 f2 = n2;
4279#endif
4280 }
4281#ifdef FEAT_FLOAT
4282 // if there is a float on either side the result is a float
4283 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
4284 {
4285 switch (iptr->isn_arg.op.op_type)
4286 {
4287 case EXPR_MULT: f1 = f1 * f2; break;
4288 case EXPR_DIV: f1 = f1 / f2; break;
4289 case EXPR_SUB: f1 = f1 - f2; break;
4290 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004291 default: SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004292 emsg(_(e_cannot_use_percent_with_float));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004293 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004294 }
4295 clear_tv(tv1);
4296 clear_tv(tv2);
4297 tv1->v_type = VAR_FLOAT;
4298 tv1->vval.v_float = f1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004299 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004300 }
4301 else
4302#endif
4303 {
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004304 int failed = FALSE;
4305
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004306 switch (iptr->isn_arg.op.op_type)
4307 {
4308 case EXPR_MULT: n1 = n1 * n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004309 case EXPR_DIV: n1 = num_divide(n1, n2, &failed);
4310 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01004311 goto on_error;
4312 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004313 case EXPR_SUB: n1 = n1 - n2; break;
4314 case EXPR_ADD: n1 = n1 + n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004315 default: n1 = num_modulus(n1, n2, &failed);
4316 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01004317 goto on_error;
4318 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004319 }
4320 clear_tv(tv1);
4321 clear_tv(tv2);
4322 tv1->v_type = VAR_NUMBER;
4323 tv1->vval.v_number = n1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004324 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004325 }
4326 }
4327 break;
4328
4329 case ISN_CONCAT:
4330 {
4331 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string;
4332 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string;
4333 char_u *res;
4334
4335 res = concat_str(str1, str2);
4336 clear_tv(STACK_TV_BOT(-2));
4337 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02004338 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004339 STACK_TV_BOT(-1)->vval.v_string = res;
4340 }
4341 break;
4342
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004343 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004344 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004345 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004346 int is_slice = iptr->isn_type == ISN_STRSLICE;
4347 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004348 char_u *res;
4349
4350 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004351 // string slice: string is at stack-3, first index at
4352 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004353 if (is_slice)
4354 {
4355 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004356 n1 = tv->vval.v_number;
4357 }
4358
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004359 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004360 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004361
Bram Moolenaar4c137212021-04-19 16:48:48 +02004362 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004363 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004364 if (is_slice)
4365 // Slice: Select the characters from the string
Bram Moolenaar6601b622021-01-13 21:47:15 +01004366 res = string_slice(tv->vval.v_string, n1, n2, FALSE);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004367 else
4368 // Index: The resulting variable is a string of a
Bram Moolenaar0289a092021-03-14 18:40:19 +01004369 // single character (including composing characters).
4370 // If the index is too big or negative the result is
4371 // empty.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004372 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004373 vim_free(tv->vval.v_string);
4374 tv->vval.v_string = res;
4375 }
4376 break;
4377
4378 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02004379 case ISN_LISTSLICE:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004380 case ISN_BLOBINDEX:
4381 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004382 {
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004383 int is_slice = iptr->isn_type == ISN_LISTSLICE
4384 || iptr->isn_type == ISN_BLOBSLICE;
4385 int is_blob = iptr->isn_type == ISN_BLOBINDEX
4386 || iptr->isn_type == ISN_BLOBSLICE;
Bram Moolenaared591872020-08-15 22:14:53 +02004387 varnumber_T n1, n2;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004388 typval_T *val_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004389
4390 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02004391 // list slice: list is at stack-3, indexes at stack-2 and
4392 // stack-1
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004393 // Same for blob.
4394 val_tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004395
4396 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02004397 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004398 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02004399
4400 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004401 {
Bram Moolenaared591872020-08-15 22:14:53 +02004402 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02004403 n1 = tv->vval.v_number;
4404 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004405 }
Bram Moolenaared591872020-08-15 22:14:53 +02004406
Bram Moolenaar4c137212021-04-19 16:48:48 +02004407 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02004408 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02004409 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004410 if (is_blob)
4411 {
4412 if (blob_slice_or_index(val_tv->vval.v_blob, is_slice,
4413 n1, n2, FALSE, tv) == FAIL)
4414 goto on_error;
4415 }
4416 else
4417 {
4418 if (list_slice_or_index(val_tv->vval.v_list, is_slice,
4419 n1, n2, FALSE, tv, TRUE) == FAIL)
4420 goto on_error;
4421 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004422 }
4423 break;
4424
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004425 case ISN_ANYINDEX:
4426 case ISN_ANYSLICE:
4427 {
4428 int is_slice = iptr->isn_type == ISN_ANYSLICE;
4429 typval_T *var1, *var2;
4430 int res;
4431
4432 // index: composite is at stack-2, index at stack-1
4433 // slice: composite is at stack-3, indexes at stack-2 and
4434 // stack-1
4435 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02004436 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004437 if (check_can_index(tv, TRUE, TRUE) == FAIL)
4438 goto on_error;
4439 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
4440 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
Bram Moolenaar6601b622021-01-13 21:47:15 +01004441 res = eval_index_inner(tv, is_slice, var1, var2,
4442 FALSE, NULL, -1, TRUE);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004443 clear_tv(var1);
4444 if (is_slice)
4445 clear_tv(var2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004446 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004447 if (res == FAIL)
4448 goto on_error;
4449 }
4450 break;
4451
Bram Moolenaar9af78762020-06-16 11:34:42 +02004452 case ISN_SLICE:
4453 {
4454 list_T *list;
4455 int count = iptr->isn_arg.number;
4456
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02004457 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02004458 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02004459 list = tv->vval.v_list;
4460
4461 // no error for short list, expect it to be checked earlier
4462 if (list != NULL && list->lv_len >= count)
4463 {
4464 list_T *newlist = list_slice(list,
4465 count, list->lv_len - 1);
4466
4467 if (newlist != NULL)
4468 {
4469 list_unref(list);
4470 tv->vval.v_list = newlist;
4471 ++newlist->lv_refcount;
4472 }
4473 }
4474 }
4475 break;
4476
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004477 case ISN_GETITEM:
4478 {
4479 listitem_T *li;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02004480 getitem_T *gi = &iptr->isn_arg.getitem;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004481
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02004482 // Get list item: list is at stack-1, push item.
4483 // List type and length is checked for when compiling.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02004484 tv = STACK_TV_BOT(-1 - gi->gi_with_op);
4485 li = list_find(tv->vval.v_list, gi->gi_index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004486
Bram Moolenaar35578162021-08-02 19:10:38 +02004487 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004488 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004489 ++ectx->ec_stack.ga_len;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004490 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004491
4492 // Useful when used in unpack assignment. Reset at
4493 // ISN_DROP.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02004494 ectx->ec_where.wt_index = gi->gi_index + 1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004495 ectx->ec_where.wt_variable = TRUE;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004496 }
4497 break;
4498
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004499 case ISN_MEMBER:
4500 {
4501 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004502 char_u *key;
4503 dictitem_T *di;
4504
4505 // dict member: dict is at stack-2, key at stack-1
4506 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02004507 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004508 dict = tv->vval.v_dict;
4509
4510 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02004511 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004512 key = tv->vval.v_string;
Bram Moolenaar086fc9a2020-10-30 18:33:02 +01004513 if (key == NULL)
4514 key = (char_u *)"";
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02004515
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004516 if ((di = dict_find(dict, key, -1)) == NULL)
4517 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004518 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004519 semsg(_(e_key_not_present_in_dictionary), key);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004520
4521 // If :silent! is used we will continue, make sure the
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004522 // stack contents makes sense and the dict stack is
4523 // updated.
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004524 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004525 --ectx->ec_stack.ga_len;
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004526 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004527 (void) dict_stack_save(tv);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004528 tv->v_type = VAR_NUMBER;
4529 tv->vval.v_number = 0;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004530 goto on_fatal_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004531 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004532 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004533 --ectx->ec_stack.ga_len;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004534 // Put the dict used on the dict stack, it might be used by
4535 // a dict function later.
Bram Moolenaar50788ef2020-07-05 16:51:26 +02004536 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004537 if (dict_stack_save(tv) == FAIL)
4538 goto on_fatal_error;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02004539 copy_tv(&di->di_tv, tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004540 }
4541 break;
4542
4543 // dict member with string key
4544 case ISN_STRINGMEMBER:
4545 {
4546 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004547 dictitem_T *di;
4548
4549 tv = STACK_TV_BOT(-1);
4550 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
4551 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004552 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004553 emsg(_(e_dictionary_required));
Bram Moolenaard032f342020-07-18 18:13:02 +02004554 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004555 }
4556 dict = tv->vval.v_dict;
4557
4558 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
4559 == NULL)
4560 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004561 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar381692b2022-02-02 20:01:27 +00004562 semsg(_(e_key_not_present_in_dictionary),
4563 iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02004564 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004565 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004566 // Put the dict used on the dict stack, it might be used by
4567 // a dict function later.
4568 if (dict_stack_save(tv) == FAIL)
4569 goto on_fatal_error;
4570
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004571 copy_tv(&di->di_tv, tv);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004572 }
4573 break;
4574
4575 case ISN_CLEARDICT:
4576 dict_stack_drop();
4577 break;
4578
4579 case ISN_USEDICT:
4580 {
4581 typval_T *dict_tv = dict_stack_get_tv();
4582
4583 // Turn "dict.Func" into a partial for "Func" bound to
4584 // "dict". Don't do this when "Func" is already a partial
4585 // that was bound explicitly (pt_auto is FALSE).
4586 tv = STACK_TV_BOT(-1);
4587 if (dict_tv != NULL
4588 && dict_tv->v_type == VAR_DICT
4589 && dict_tv->vval.v_dict != NULL
4590 && (tv->v_type == VAR_FUNC
4591 || (tv->v_type == VAR_PARTIAL
4592 && (tv->vval.v_partial->pt_auto
4593 || tv->vval.v_partial->pt_dict == NULL))))
4594 dict_tv->vval.v_dict =
4595 make_partial(dict_tv->vval.v_dict, tv);
4596 dict_stack_drop();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004597 }
4598 break;
4599
4600 case ISN_NEGATENR:
4601 tv = STACK_TV_BOT(-1);
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00004602 // CHECKTYPE should have checked the variable type
Bram Moolenaarc58164c2020-03-29 18:40:30 +02004603#ifdef FEAT_FLOAT
4604 if (tv->v_type == VAR_FLOAT)
4605 tv->vval.v_float = -tv->vval.v_float;
4606 else
4607#endif
4608 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004609 break;
4610
4611 case ISN_CHECKNR:
4612 {
4613 int error = FALSE;
4614
4615 tv = STACK_TV_BOT(-1);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02004616 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004617 if (check_not_string(tv) == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004618 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004619 (void)tv_get_number_chk(tv, &error);
4620 if (error)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004621 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004622 }
4623 break;
4624
4625 case ISN_CHECKTYPE:
4626 {
4627 checktype_T *ct = &iptr->isn_arg.type;
4628
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01004629 tv = STACK_TV_BOT((int)ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02004630 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004631 if (!ectx->ec_where.wt_variable)
4632 ectx->ec_where.wt_index = ct->ct_arg_idx;
4633 if (check_typval_type(ct->ct_type, tv, ectx->ec_where)
4634 == FAIL)
Bram Moolenaar5e654232020-09-16 15:22:00 +02004635 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004636 if (!ectx->ec_where.wt_variable)
4637 ectx->ec_where.wt_index = 0;
Bram Moolenaar5e654232020-09-16 15:22:00 +02004638
4639 // number 0 is FALSE, number 1 is TRUE
4640 if (tv->v_type == VAR_NUMBER
4641 && ct->ct_type->tt_type == VAR_BOOL
4642 && (tv->vval.v_number == 0
4643 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004644 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02004645 tv->v_type = VAR_BOOL;
4646 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02004647 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004648 }
4649 }
4650 break;
4651
Bram Moolenaar9af78762020-06-16 11:34:42 +02004652 case ISN_CHECKLEN:
4653 {
4654 int min_len = iptr->isn_arg.checklen.cl_min_len;
4655 list_T *list = NULL;
4656
4657 tv = STACK_TV_BOT(-1);
4658 if (tv->v_type == VAR_LIST)
4659 list = tv->vval.v_list;
4660 if (list == NULL || list->lv_len < min_len
4661 || (list->lv_len > min_len
4662 && !iptr->isn_arg.checklen.cl_more_OK))
4663 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004664 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004665 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02004666 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004667 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02004668 }
4669 }
4670 break;
4671
Bram Moolenaaraa210a32021-01-02 15:41:03 +01004672 case ISN_SETTYPE:
Bram Moolenaar381692b2022-02-02 20:01:27 +00004673 set_tv_type(STACK_TV_BOT(-1), iptr->isn_arg.type.ct_type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01004674 break;
4675
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004676 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004677 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004678 {
4679 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004680 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004681
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004682 if (iptr->isn_type == ISN_2BOOL)
4683 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004684 tv = STACK_TV_BOT(iptr->isn_arg.tobool.offset);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004685 n = tv2bool(tv);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004686 if (iptr->isn_arg.tobool.invert)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004687 n = !n;
4688 }
4689 else
4690 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004691 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004692 SOURCING_LNUM = iptr->isn_lnum;
4693 n = tv_get_bool_chk(tv, &error);
4694 if (error)
4695 goto on_error;
4696 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004697 clear_tv(tv);
4698 tv->v_type = VAR_BOOL;
4699 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
4700 }
4701 break;
4702
4703 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02004704 case ISN_2STRING_ANY:
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01004705 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004706 if (do_2string(STACK_TV_BOT(iptr->isn_arg.tostring.offset),
4707 iptr->isn_type == ISN_2STRING_ANY,
4708 iptr->isn_arg.tostring.tolerant) == FAIL)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01004709 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004710 break;
4711
Bram Moolenaar08597872020-12-10 19:43:40 +01004712 case ISN_RANGE:
4713 {
4714 exarg_T ea;
4715 char *errormsg;
4716
Bram Moolenaarece0b872021-01-08 20:40:45 +01004717 ea.line2 = 0;
Bram Moolenaard1510ee2021-01-04 16:15:58 +01004718 ea.addr_count = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01004719 ea.addr_type = ADDR_LINES;
4720 ea.cmd = iptr->isn_arg.string;
Bram Moolenaarece0b872021-01-08 20:40:45 +01004721 ea.skip = FALSE;
Bram Moolenaar08597872020-12-10 19:43:40 +01004722 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaarece0b872021-01-08 20:40:45 +01004723 goto on_error;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004724
Bram Moolenaar35578162021-08-02 19:10:38 +02004725 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004726 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004727 ++ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004728 tv = STACK_TV_BOT(-1);
4729 tv->v_type = VAR_NUMBER;
4730 tv->v_lock = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01004731 if (ea.addr_count == 0)
4732 tv->vval.v_number = curwin->w_cursor.lnum;
4733 else
4734 tv->vval.v_number = ea.line2;
4735 }
4736 break;
4737
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004738 case ISN_PUT:
4739 {
4740 int regname = iptr->isn_arg.put.put_regname;
4741 linenr_T lnum = iptr->isn_arg.put.put_lnum;
4742 char_u *expr = NULL;
4743 int dir = FORWARD;
4744
Bram Moolenaar08597872020-12-10 19:43:40 +01004745 if (lnum < -2)
4746 {
4747 // line number was put on the stack by ISN_RANGE
4748 tv = STACK_TV_BOT(-1);
4749 curwin->w_cursor.lnum = tv->vval.v_number;
4750 if (lnum == LNUM_VARIABLE_RANGE_ABOVE)
4751 dir = BACKWARD;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004752 --ectx->ec_stack.ga_len;
Bram Moolenaar08597872020-12-10 19:43:40 +01004753 }
4754 else if (lnum == -2)
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004755 // :put! above cursor
4756 dir = BACKWARD;
4757 else if (lnum >= 0)
Bram Moolenaar4e713ba2022-02-07 15:31:37 +00004758 {
4759 curwin->w_cursor.lnum = lnum;
4760 if (lnum == 0)
4761 // check_cursor() below will move to line 1
4762 dir = BACKWARD;
4763 }
Bram Moolenaara28639e2021-01-19 22:48:09 +01004764
4765 if (regname == '=')
4766 {
4767 tv = STACK_TV_BOT(-1);
4768 if (tv->v_type == VAR_STRING)
4769 expr = tv->vval.v_string;
4770 else
4771 {
4772 expr = typval2string(tv, TRUE); // allocates value
4773 clear_tv(tv);
4774 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004775 --ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004776 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004777 check_cursor();
4778 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
4779 vim_free(expr);
4780 }
4781 break;
4782
Bram Moolenaar02194d22020-10-24 23:08:38 +02004783 case ISN_CMDMOD:
Bram Moolenaar4c137212021-04-19 16:48:48 +02004784 ectx->ec_funclocal.floc_save_cmdmod = cmdmod;
4785 ectx->ec_funclocal.floc_restore_cmdmod = TRUE;
4786 ectx->ec_funclocal.floc_restore_cmdmod_stacklen =
4787 ectx->ec_stack.ga_len;
Bram Moolenaar02194d22020-10-24 23:08:38 +02004788 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
4789 apply_cmdmod(&cmdmod);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004790 break;
4791
Bram Moolenaar02194d22020-10-24 23:08:38 +02004792 case ISN_CMDMOD_REV:
4793 // filter regprog is owned by the instruction, don't free it
4794 cmdmod.cmod_filter_regmatch.regprog = NULL;
4795 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004796 cmdmod = ectx->ec_funclocal.floc_save_cmdmod;
4797 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004798 break;
4799
Bram Moolenaar792f7862020-11-23 08:31:18 +01004800 case ISN_UNPACK:
4801 {
4802 int count = iptr->isn_arg.unpack.unp_count;
4803 int semicolon = iptr->isn_arg.unpack.unp_semicolon;
4804 list_T *l;
4805 listitem_T *li;
4806 int i;
4807
4808 // Check there is a valid list to unpack.
4809 tv = STACK_TV_BOT(-1);
4810 if (tv->v_type != VAR_LIST)
4811 {
4812 SOURCING_LNUM = iptr->isn_lnum;
4813 emsg(_(e_for_argument_must_be_sequence_of_lists));
4814 goto on_error;
4815 }
4816 l = tv->vval.v_list;
4817 if (l == NULL
4818 || l->lv_len < (semicolon ? count - 1 : count))
4819 {
4820 SOURCING_LNUM = iptr->isn_lnum;
4821 emsg(_(e_list_value_does_not_have_enough_items));
4822 goto on_error;
4823 }
4824 else if (!semicolon && l->lv_len > count)
4825 {
4826 SOURCING_LNUM = iptr->isn_lnum;
4827 emsg(_(e_list_value_has_more_items_than_targets));
4828 goto on_error;
4829 }
4830
4831 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar35578162021-08-02 19:10:38 +02004832 if (GA_GROW_FAILS(&ectx->ec_stack, count - 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004833 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004834 ectx->ec_stack.ga_len += count - 1;
Bram Moolenaar792f7862020-11-23 08:31:18 +01004835
4836 // Variable after semicolon gets a list with the remaining
4837 // items.
4838 if (semicolon)
4839 {
4840 list_T *rem_list =
4841 list_alloc_with_items(l->lv_len - count + 1);
4842
4843 if (rem_list == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004844 goto theend;
Bram Moolenaar792f7862020-11-23 08:31:18 +01004845 tv = STACK_TV_BOT(-count);
4846 tv->vval.v_list = rem_list;
4847 ++rem_list->lv_refcount;
4848 tv->v_lock = 0;
4849 li = l->lv_first;
4850 for (i = 0; i < count - 1; ++i)
4851 li = li->li_next;
4852 for (i = 0; li != NULL; ++i)
4853 {
Bram Moolenaar61efa162022-03-18 13:10:48 +00004854 typval_T tvcopy;
4855
4856 copy_tv(&li->li_tv, &tvcopy);
4857 list_set_item(rem_list, i, &tvcopy);
Bram Moolenaar792f7862020-11-23 08:31:18 +01004858 li = li->li_next;
4859 }
4860 --count;
4861 }
4862
4863 // Produce the values in reverse order, first item last.
4864 li = l->lv_first;
4865 for (i = 0; i < count; ++i)
4866 {
4867 tv = STACK_TV_BOT(-i - 1);
4868 copy_tv(&li->li_tv, tv);
4869 li = li->li_next;
4870 }
4871
4872 list_unref(l);
4873 }
4874 break;
4875
Bram Moolenaarb2049902021-01-24 12:53:53 +01004876 case ISN_PROF_START:
4877 case ISN_PROF_END:
4878 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01004879#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01004880 funccall_T cookie;
4881 ufunc_T *cur_ufunc =
4882 (((dfunc_T *)def_functions.ga_data)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02004883 + ectx->ec_dfunc_idx)->df_ufunc;
Bram Moolenaarb2049902021-01-24 12:53:53 +01004884
4885 cookie.func = cur_ufunc;
4886 if (iptr->isn_type == ISN_PROF_START)
4887 {
4888 func_line_start(&cookie, iptr->isn_lnum);
4889 // if we get here the instruction is executed
4890 func_line_exec(&cookie);
4891 }
4892 else
4893 func_line_end(&cookie);
Bram Moolenaarf002a412021-01-24 13:34:18 +01004894#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01004895 }
4896 break;
4897
Bram Moolenaare99d4222021-06-13 14:01:26 +02004898 case ISN_DEBUG:
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02004899 handle_debug(iptr, ectx);
Bram Moolenaare99d4222021-06-13 14:01:26 +02004900 break;
4901
Bram Moolenaar389df252020-07-09 21:20:47 +02004902 case ISN_SHUFFLE:
4903 {
Bram Moolenaar792f7862020-11-23 08:31:18 +01004904 typval_T tmp_tv;
4905 int item = iptr->isn_arg.shuffle.shfl_item;
4906 int up = iptr->isn_arg.shuffle.shfl_up;
Bram Moolenaar389df252020-07-09 21:20:47 +02004907
4908 tmp_tv = *STACK_TV_BOT(-item);
4909 for ( ; up > 0 && item > 1; --up)
4910 {
4911 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
4912 --item;
4913 }
4914 *STACK_TV_BOT(-item) = tmp_tv;
4915 }
4916 break;
4917
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004918 case ISN_DROP:
Bram Moolenaar4c137212021-04-19 16:48:48 +02004919 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004920 clear_tv(STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02004921 ectx->ec_where.wt_index = 0;
4922 ectx->ec_where.wt_variable = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004923 break;
4924 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004925 continue;
4926
Bram Moolenaard032f342020-07-18 18:13:02 +02004927func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02004928 // Restore previous function. If the frame pointer is where we started
4929 // then there is none and we are done.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004930 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02004931 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02004932
Bram Moolenaar4c137212021-04-19 16:48:48 +02004933 if (func_return(ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004934 // only fails when out of memory
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004935 goto theend;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02004936 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02004937
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004938on_error:
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004939 // Jump here for an error that does not require aborting execution.
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004940 // If "emsg_silent" is set then ignore the error, unless it was set
4941 // when calling the function.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004942 if (did_emsg_cumul + did_emsg == ectx->ec_did_emsg_before
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004943 && emsg_silent && did_emsg_def == 0)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004944 {
4945 // If a sequence of instructions causes an error while ":silent!"
4946 // was used, restore the stack length and jump ahead to restoring
4947 // the cmdmod.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004948 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004949 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004950 while (ectx->ec_stack.ga_len
4951 > ectx->ec_funclocal.floc_restore_cmdmod_stacklen)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004952 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004953 --ectx->ec_stack.ga_len;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004954 clear_tv(STACK_TV_BOT(0));
4955 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004956 while (ectx->ec_instr[ectx->ec_iidx].isn_type != ISN_CMDMOD_REV)
4957 ++ectx->ec_iidx;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004958 }
Bram Moolenaarcd030c42020-10-30 21:49:40 +01004959 continue;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004960 }
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004961on_fatal_error:
4962 // Jump here for an error that messes up the stack.
Bram Moolenaar171fb922020-10-28 16:54:47 +01004963 // If we are not inside a try-catch started here, abort execution.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004964 if (trylevel <= ectx->ec_trylevel_at_start)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004965 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004966 }
4967
4968done:
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004969 ret = OK;
4970theend:
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004971 dict_stack_clear(dict_stack_len_at_start);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004972 ectx->ec_trylevel_at_start = save_trylevel_at_start;
4973 return ret;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004974}
4975
4976/*
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004977 * Execute the instructions from a VAR_INSTR typeval and put the result in
4978 * "rettv".
4979 * Return OK or FAIL.
4980 */
4981 int
4982exe_typval_instr(typval_T *tv, typval_T *rettv)
4983{
4984 ectx_T *ectx = tv->vval.v_instr->instr_ectx;
4985 isn_T *save_instr = ectx->ec_instr;
4986 int save_iidx = ectx->ec_iidx;
4987 int res;
4988
4989 ectx->ec_instr = tv->vval.v_instr->instr_instr;
4990 res = exec_instructions(ectx);
4991 if (res == OK)
4992 {
4993 *rettv = *STACK_TV_BOT(-1);
4994 --ectx->ec_stack.ga_len;
4995 }
4996
4997 ectx->ec_instr = save_instr;
4998 ectx->ec_iidx = save_iidx;
4999
5000 return res;
5001}
5002
5003/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02005004 * Execute the instructions from an ISN_SUBSTITUTE command, which are in
5005 * "substitute_instr".
5006 */
5007 char_u *
5008exe_substitute_instr(void)
5009{
5010 ectx_T *ectx = substitute_instr->subs_ectx;
5011 isn_T *save_instr = ectx->ec_instr;
5012 int save_iidx = ectx->ec_iidx;
5013 char_u *res;
5014
5015 ectx->ec_instr = substitute_instr->subs_instr;
5016 if (exec_instructions(ectx) == OK)
Bram Moolenaar90193e62021-04-04 20:49:50 +02005017 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005018 typval_T *tv = STACK_TV_BOT(-1);
5019
Bram Moolenaar27523602021-06-05 21:36:19 +02005020 res = typval2string(tv, TRUE);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005021 --ectx->ec_stack.ga_len;
5022 clear_tv(tv);
Bram Moolenaar90193e62021-04-04 20:49:50 +02005023 }
5024 else
5025 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005026 substitute_instr->subs_status = FAIL;
5027 res = vim_strsave((char_u *)"");
Bram Moolenaar90193e62021-04-04 20:49:50 +02005028 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005029
Bram Moolenaar4c137212021-04-19 16:48:48 +02005030 ectx->ec_instr = save_instr;
5031 ectx->ec_iidx = save_iidx;
5032
5033 return res;
5034}
5035
5036/*
5037 * Call a "def" function from old Vim script.
5038 * Return OK or FAIL.
5039 */
5040 int
5041call_def_function(
5042 ufunc_T *ufunc,
5043 int argc_arg, // nr of arguments
5044 typval_T *argv, // arguments
5045 partial_T *partial, // optional partial for context
5046 typval_T *rettv) // return value
5047{
5048 ectx_T ectx; // execution context
5049 int argc = argc_arg;
5050 typval_T *tv;
5051 int idx;
5052 int ret = FAIL;
5053 int defcount = ufunc->uf_args.ga_len - argc;
5054 sctx_T save_current_sctx = current_sctx;
5055 int did_emsg_before = did_emsg_cumul + did_emsg;
5056 int save_suppress_errthrow = suppress_errthrow;
5057 msglist_T **saved_msg_list = NULL;
5058 msglist_T *private_msg_list = NULL;
5059 int save_emsg_silent_def = emsg_silent_def;
5060 int save_did_emsg_def = did_emsg_def;
5061 int orig_funcdepth;
Bram Moolenaare99d4222021-06-13 14:01:26 +02005062 int orig_nesting_level = ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005063
5064// Get pointer to item in the stack.
5065#undef STACK_TV
5066#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
5067
5068// Get pointer to item at the bottom of the stack, -1 is the bottom.
5069#undef STACK_TV_BOT
5070#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
5071
5072// Get pointer to a local variable on the stack. Negative for arguments.
5073#undef STACK_TV_VAR
5074#define STACK_TV_VAR(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_frame_idx + STACK_FRAME_SIZE + idx)
5075
5076 if (ufunc->uf_def_status == UF_NOT_COMPILED
5077 || ufunc->uf_def_status == UF_COMPILE_ERROR
Bram Moolenaar139575d2022-03-15 19:29:30 +00005078 || (func_needs_compiling(ufunc, get_compile_type(ufunc))
5079 && compile_def_function(ufunc, FALSE,
5080 get_compile_type(ufunc), NULL) == FAIL))
Bram Moolenaar4c137212021-04-19 16:48:48 +02005081 {
5082 if (did_emsg_cumul + did_emsg == did_emsg_before)
5083 semsg(_(e_function_is_not_compiled_str),
5084 printable_func_name(ufunc));
5085 return FAIL;
5086 }
5087
5088 {
5089 // Check the function was really compiled.
5090 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5091 + ufunc->uf_dfunc_idx;
5092 if (INSTRUCTIONS(dfunc) == NULL)
5093 {
5094 iemsg("using call_def_function() on not compiled function");
5095 return FAIL;
5096 }
5097 }
5098
5099 // If depth of calling is getting too high, don't execute the function.
5100 orig_funcdepth = funcdepth_get();
5101 if (funcdepth_increment() == FAIL)
5102 return FAIL;
5103
5104 CLEAR_FIELD(ectx);
5105 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
5106 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
Bram Moolenaar35578162021-08-02 19:10:38 +02005107 if (GA_GROW_FAILS(&ectx.ec_stack, 20))
Bram Moolenaar4c137212021-04-19 16:48:48 +02005108 {
5109 funcdepth_decrement();
5110 return FAIL;
5111 }
5112 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
5113 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
5114 ectx.ec_did_emsg_before = did_emsg_before;
Bram Moolenaare99d4222021-06-13 14:01:26 +02005115 ++ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005116
5117 idx = argc - ufunc->uf_args.ga_len;
5118 if (idx > 0 && ufunc->uf_va_name == NULL)
5119 {
5120 if (idx == 1)
5121 emsg(_(e_one_argument_too_many));
5122 else
5123 semsg(_(e_nr_arguments_too_many), idx);
5124 goto failed_early;
5125 }
Bram Moolenaarc6d71532021-06-05 18:49:38 +02005126 idx = argc - ufunc->uf_args.ga_len + ufunc->uf_def_args.ga_len;
5127 if (idx < 0)
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02005128 {
5129 if (idx == -1)
5130 emsg(_(e_one_argument_too_few));
5131 else
5132 semsg(_(e_nr_arguments_too_few), -idx);
5133 goto failed_early;
5134 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005135
5136 // Put arguments on the stack, but no more than what the function expects.
5137 // A lambda can be called with more arguments than it uses.
5138 for (idx = 0; idx < argc
5139 && (ufunc->uf_va_name != NULL || idx < ufunc->uf_args.ga_len);
5140 ++idx)
5141 {
5142 if (idx >= ufunc->uf_args.ga_len - ufunc->uf_def_args.ga_len
5143 && argv[idx].v_type == VAR_SPECIAL
5144 && argv[idx].vval.v_number == VVAL_NONE)
5145 {
5146 // Use the default value.
5147 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
5148 }
5149 else
5150 {
5151 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
5152 && check_typval_arg_type(
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02005153 ufunc->uf_arg_types[idx], &argv[idx],
5154 NULL, idx + 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005155 goto failed_early;
5156 copy_tv(&argv[idx], STACK_TV_BOT(0));
5157 }
5158 ++ectx.ec_stack.ga_len;
5159 }
5160
5161 // Turn varargs into a list. Empty list if no args.
5162 if (ufunc->uf_va_name != NULL)
5163 {
5164 int vararg_count = argc - ufunc->uf_args.ga_len;
5165
5166 if (vararg_count < 0)
5167 vararg_count = 0;
5168 else
5169 argc -= vararg_count;
5170 if (exe_newlist(vararg_count, &ectx) == FAIL)
5171 goto failed_early;
5172
5173 // Check the type of the list items.
5174 tv = STACK_TV_BOT(-1);
5175 if (ufunc->uf_va_type != NULL
5176 && ufunc->uf_va_type != &t_list_any
5177 && ufunc->uf_va_type->tt_member != &t_any
5178 && tv->vval.v_list != NULL)
5179 {
5180 type_T *expected = ufunc->uf_va_type->tt_member;
5181 listitem_T *li = tv->vval.v_list->lv_first;
5182
5183 for (idx = 0; idx < vararg_count; ++idx)
5184 {
5185 if (check_typval_arg_type(expected, &li->li_tv,
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02005186 NULL, argc + idx + 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005187 goto failed_early;
5188 li = li->li_next;
5189 }
5190 }
5191
5192 if (defcount > 0)
5193 // Move varargs list to below missing default arguments.
5194 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
5195 --ectx.ec_stack.ga_len;
5196 }
5197
5198 // Make space for omitted arguments, will store default value below.
5199 // Any varargs list goes after them.
5200 if (defcount > 0)
5201 for (idx = 0; idx < defcount; ++idx)
5202 {
5203 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
5204 ++ectx.ec_stack.ga_len;
5205 }
5206 if (ufunc->uf_va_name != NULL)
5207 ++ectx.ec_stack.ga_len;
5208
5209 // Frame pointer points to just after arguments.
5210 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
5211 ectx.ec_initial_frame_idx = ectx.ec_frame_idx;
5212
5213 {
5214 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5215 + ufunc->uf_dfunc_idx;
5216 ufunc_T *base_ufunc = dfunc->df_ufunc;
5217
5218 // "uf_partial" is on the ufunc that "df_ufunc" points to, as is done
5219 // by copy_func().
5220 if (partial != NULL || base_ufunc->uf_partial != NULL)
5221 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005222 ectx.ec_outer_ref = ALLOC_CLEAR_ONE(outer_ref_T);
5223 if (ectx.ec_outer_ref == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005224 goto failed_early;
5225 if (partial != NULL)
5226 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00005227 outer_T *outer = get_pt_outer(partial);
5228
5229 if (outer->out_stack == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005230 {
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00005231 if (current_ectx != NULL)
5232 {
5233 if (current_ectx->ec_outer_ref != NULL
5234 && current_ectx->ec_outer_ref->or_outer != NULL)
5235 ectx.ec_outer_ref->or_outer =
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005236 current_ectx->ec_outer_ref->or_outer;
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00005237 }
5238 // Should there be an error here?
Bram Moolenaar4c137212021-04-19 16:48:48 +02005239 }
5240 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005241 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00005242 ectx.ec_outer_ref->or_outer = outer;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005243 ++partial->pt_refcount;
5244 ectx.ec_outer_ref->or_partial = partial;
5245 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005246 }
5247 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005248 {
5249 ectx.ec_outer_ref->or_outer = &base_ufunc->uf_partial->pt_outer;
5250 ++base_ufunc->uf_partial->pt_refcount;
5251 ectx.ec_outer_ref->or_partial = base_ufunc->uf_partial;
5252 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005253 }
5254 }
5255
5256 // dummy frame entries
5257 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
5258 {
5259 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
5260 ++ectx.ec_stack.ga_len;
5261 }
5262
5263 {
5264 // Reserve space for local variables and any closure reference count.
5265 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5266 + ufunc->uf_dfunc_idx;
5267
Bram Moolenaar5cd64792021-12-25 18:23:24 +00005268 // Initialize variables to zero. That avoids having to generate
5269 // initializing instructions for "var nr: number", "var x: any", etc.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005270 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaar5cd64792021-12-25 18:23:24 +00005271 {
5272 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
5273 STACK_TV_VAR(idx)->vval.v_number = 0;
5274 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005275 ectx.ec_stack.ga_len += dfunc->df_varcount;
5276 if (dfunc->df_has_closure)
5277 {
5278 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
5279 STACK_TV_VAR(idx)->vval.v_number = 0;
5280 ++ectx.ec_stack.ga_len;
5281 }
5282
5283 ectx.ec_instr = INSTRUCTIONS(dfunc);
5284 }
5285
5286 // Following errors are in the function, not the caller.
5287 // Commands behave like vim9script.
5288 estack_push_ufunc(ufunc, 1);
5289 current_sctx = ufunc->uf_script_ctx;
5290 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
5291
5292 // Use a specific location for storing error messages to be converted to an
5293 // exception.
5294 saved_msg_list = msg_list;
5295 msg_list = &private_msg_list;
5296
5297 // Do turn errors into exceptions.
5298 suppress_errthrow = FALSE;
5299
5300 // Do not delete the function while executing it.
5301 ++ufunc->uf_calls;
5302
5303 // When ":silent!" was used before calling then we still abort the
5304 // function. If ":silent!" is used in the function then we don't.
5305 emsg_silent_def = emsg_silent;
5306 did_emsg_def = 0;
5307
5308 ectx.ec_where.wt_index = 0;
5309 ectx.ec_where.wt_variable = FALSE;
5310
5311 // Execute the instructions until done.
5312 ret = exec_instructions(&ectx);
5313 if (ret == OK)
5314 {
5315 // function finished, get result from the stack.
5316 if (ufunc->uf_ret_type == &t_void)
5317 {
5318 rettv->v_type = VAR_VOID;
5319 }
5320 else
5321 {
5322 tv = STACK_TV_BOT(-1);
5323 *rettv = *tv;
5324 tv->v_type = VAR_UNKNOWN;
5325 }
5326 }
5327
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01005328 // When failed need to unwind the call stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005329 while (ectx.ec_frame_idx != ectx.ec_initial_frame_idx)
5330 func_return(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02005331
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005332 // Deal with any remaining closures, they may be in use somewhere.
5333 if (ectx.ec_funcrefs.ga_len > 0)
Bram Moolenaarf112f302020-12-20 17:47:52 +01005334 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005335 handle_closure_in_use(&ectx, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00005336 ga_clear(&ectx.ec_funcrefs);
Bram Moolenaarf112f302020-12-20 17:47:52 +01005337 }
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005338
Bram Moolenaaree8580e2020-08-28 17:19:07 +02005339 estack_pop();
5340 current_sctx = save_current_sctx;
5341
Bram Moolenaar2336c372021-12-06 15:06:54 +00005342 if (--ufunc->uf_calls <= 0 && ufunc->uf_refcount <= 0)
5343 // Function was unreferenced while being used, free it now.
5344 func_clear_free(ufunc, FALSE);
Bram Moolenaarc970e422021-03-17 15:03:04 +01005345
Bram Moolenaar352134b2020-10-17 22:04:08 +02005346 if (*msg_list != NULL && saved_msg_list != NULL)
5347 {
5348 msglist_T **plist = saved_msg_list;
5349
5350 // Append entries from the current msg_list (uncaught exceptions) to
5351 // the saved msg_list.
5352 while (*plist != NULL)
5353 plist = &(*plist)->next;
5354
5355 *plist = *msg_list;
5356 }
5357 msg_list = saved_msg_list;
5358
Bram Moolenaar4c137212021-04-19 16:48:48 +02005359 if (ectx.ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar02194d22020-10-24 23:08:38 +02005360 {
5361 cmdmod.cmod_filter_regmatch.regprog = NULL;
5362 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005363 cmdmod = ectx.ec_funclocal.floc_save_cmdmod;
Bram Moolenaar02194d22020-10-24 23:08:38 +02005364 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +01005365 emsg_silent_def = save_emsg_silent_def;
5366 did_emsg_def += save_did_emsg_def;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02005367
Bram Moolenaaree8580e2020-08-28 17:19:07 +02005368failed_early:
Bram Moolenaar0d1f55c2022-04-05 17:30:29 +01005369 // Free all arguments and local variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005370 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
5371 clear_tv(STACK_TV(idx));
Bram Moolenaare99d4222021-06-13 14:01:26 +02005372 ex_nesting_level = orig_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02005373
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005374 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01005375 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005376 if (ectx.ec_outer_ref != NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +01005377 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005378 if (ectx.ec_outer_ref->or_outer_allocated)
5379 vim_free(ectx.ec_outer_ref->or_outer);
5380 partial_unref(ectx.ec_outer_ref->or_partial);
5381 vim_free(ectx.ec_outer_ref);
Bram Moolenaar0186e582021-01-10 18:33:11 +01005382 }
5383
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02005384 // Not sure if this is necessary.
5385 suppress_errthrow = save_suppress_errthrow;
5386
Bram Moolenaarb5841b92021-07-15 18:09:53 +02005387 if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before
5388 && !need_rethrow)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005389 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02005390 printable_func_name(ufunc));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01005391 funcdepth_restore(orig_funcdepth);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005392 return ret;
5393}
5394
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005395/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02005396 * List instructions "instr" up to "instr_count" or until ISN_FINISH.
5397 * "ufunc" has the source lines, NULL for the instructions of ISN_SUBSTITUTE.
5398 * "pfx" is prefixed to every line.
5399 */
5400 static void
5401list_instructions(char *pfx, isn_T *instr, int instr_count, ufunc_T *ufunc)
5402{
5403 int line_idx = 0;
5404 int prev_current = 0;
5405 int current;
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02005406 int def_arg_idx = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005407
5408 for (current = 0; current < instr_count; ++current)
5409 {
5410 isn_T *iptr = &instr[current];
5411 char *line;
5412
5413 if (ufunc != NULL)
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02005414 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005415 while (line_idx < iptr->isn_lnum
5416 && line_idx < ufunc->uf_lines.ga_len)
5417 {
5418 if (current > prev_current)
5419 {
5420 msg_puts("\n\n");
5421 prev_current = current;
5422 }
5423 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
5424 if (line != NULL)
5425 msg(line);
5426 }
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02005427 if (iptr->isn_type == ISN_JUMP_IF_ARG_SET)
5428 {
5429 int first_def_arg = ufunc->uf_args.ga_len
5430 - ufunc->uf_def_args.ga_len;
5431
5432 if (def_arg_idx > 0)
5433 msg_puts("\n\n");
5434 msg_start();
5435 msg_puts(" ");
5436 msg_puts(((char **)(ufunc->uf_args.ga_data))[
5437 first_def_arg + def_arg_idx]);
5438 msg_puts(" = ");
5439 msg_puts(((char **)(ufunc->uf_def_args.ga_data))[def_arg_idx++]);
5440 msg_clr_eos();
5441 msg_end();
5442 }
5443 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005444
5445 switch (iptr->isn_type)
5446 {
5447 case ISN_EXEC:
5448 smsg("%s%4d EXEC %s", pfx, current, iptr->isn_arg.string);
5449 break;
Bram Moolenaar20677332021-06-06 17:02:53 +02005450 case ISN_EXEC_SPLIT:
5451 smsg("%s%4d EXEC_SPLIT %s", pfx, current, iptr->isn_arg.string);
5452 break;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00005453 case ISN_EXECRANGE:
5454 smsg("%s%4d EXECRANGE %s", pfx, current, iptr->isn_arg.string);
5455 break;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005456 case ISN_LEGACY_EVAL:
5457 smsg("%s%4d EVAL legacy %s", pfx, current,
5458 iptr->isn_arg.string);
5459 break;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02005460 case ISN_REDIRSTART:
5461 smsg("%s%4d REDIR", pfx, current);
5462 break;
5463 case ISN_REDIREND:
5464 smsg("%s%4d REDIR END%s", pfx, current,
5465 iptr->isn_arg.number ? " append" : "");
5466 break;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005467 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005468#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005469 smsg("%s%4d CEXPR pre %s", pfx, current,
5470 cexpr_get_auname(iptr->isn_arg.number));
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005471#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005472 break;
5473 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005474#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005475 {
5476 cexprref_T *cer = iptr->isn_arg.cexpr.cexpr_ref;
5477
5478 smsg("%s%4d CEXPR core %s%s \"%s\"", pfx, current,
5479 cexpr_get_auname(cer->cer_cmdidx),
5480 cer->cer_forceit ? "!" : "",
5481 cer->cer_cmdline);
5482 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005483#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005484 break;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005485 case ISN_INSTR:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01005486 smsg("%s%4d INSTR", pfx, current);
5487 list_instructions(" ", iptr->isn_arg.instr, INT_MAX, NULL);
5488 msg(" -------------");
5489 break;
5490 case ISN_SOURCE:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005491 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01005492 scriptitem_T *si = SCRIPT_ITEM(iptr->isn_arg.number);
5493
5494 smsg("%s%4d SOURCE %s", pfx, current, si->sn_name);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005495 }
5496 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005497 case ISN_SUBSTITUTE:
5498 {
5499 subs_T *subs = &iptr->isn_arg.subs;
5500
5501 smsg("%s%4d SUBSTITUTE %s", pfx, current, subs->subs_cmd);
5502 list_instructions(" ", subs->subs_instr, INT_MAX, NULL);
5503 msg(" -------------");
5504 }
5505 break;
5506 case ISN_EXECCONCAT:
5507 smsg("%s%4d EXECCONCAT %lld", pfx, current,
5508 (varnumber_T)iptr->isn_arg.number);
5509 break;
5510 case ISN_ECHO:
5511 {
5512 echo_T *echo = &iptr->isn_arg.echo;
5513
5514 smsg("%s%4d %s %d", pfx, current,
5515 echo->echo_with_white ? "ECHO" : "ECHON",
5516 echo->echo_count);
5517 }
5518 break;
5519 case ISN_EXECUTE:
5520 smsg("%s%4d EXECUTE %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02005521 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005522 break;
5523 case ISN_ECHOMSG:
5524 smsg("%s%4d ECHOMSG %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02005525 (varnumber_T)(iptr->isn_arg.number));
5526 break;
5527 case ISN_ECHOCONSOLE:
5528 smsg("%s%4d ECHOCONSOLE %lld", pfx, current,
5529 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005530 break;
5531 case ISN_ECHOERR:
5532 smsg("%s%4d ECHOERR %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02005533 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005534 break;
5535 case ISN_LOAD:
5536 {
5537 if (iptr->isn_arg.number < 0)
5538 smsg("%s%4d LOAD arg[%lld]", pfx, current,
5539 (varnumber_T)(iptr->isn_arg.number
5540 + STACK_FRAME_SIZE));
5541 else
5542 smsg("%s%4d LOAD $%lld", pfx, current,
5543 (varnumber_T)(iptr->isn_arg.number));
5544 }
5545 break;
5546 case ISN_LOADOUTER:
5547 {
5548 if (iptr->isn_arg.number < 0)
5549 smsg("%s%4d LOADOUTER level %d arg[%d]", pfx, current,
5550 iptr->isn_arg.outer.outer_depth,
5551 iptr->isn_arg.outer.outer_idx
5552 + STACK_FRAME_SIZE);
5553 else
5554 smsg("%s%4d LOADOUTER level %d $%d", pfx, current,
5555 iptr->isn_arg.outer.outer_depth,
5556 iptr->isn_arg.outer.outer_idx);
5557 }
5558 break;
5559 case ISN_LOADV:
5560 smsg("%s%4d LOADV v:%s", pfx, current,
5561 get_vim_var_name(iptr->isn_arg.number));
5562 break;
5563 case ISN_LOADSCRIPT:
5564 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005565 scriptref_T *sref = iptr->isn_arg.script.scriptref;
5566 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
5567 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005568
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005569 sv = get_script_svar(sref, -1);
5570 if (sv == NULL)
5571 smsg("%s%4d LOADSCRIPT [deleted] from %s",
5572 pfx, current, si->sn_name);
5573 else
5574 smsg("%s%4d LOADSCRIPT %s-%d from %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02005575 sv->sv_name,
5576 sref->sref_idx,
5577 si->sn_name);
5578 }
5579 break;
5580 case ISN_LOADS:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01005581 case ISN_LOADEXPORT:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005582 {
5583 scriptitem_T *si = SCRIPT_ITEM(
5584 iptr->isn_arg.loadstore.ls_sid);
5585
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01005586 smsg("%s%4d %s s:%s from %s", pfx, current,
5587 iptr->isn_type == ISN_LOADS ? "LOADS"
5588 : "LOADEXPORT",
5589 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005590 }
5591 break;
5592 case ISN_LOADAUTO:
5593 smsg("%s%4d LOADAUTO %s", pfx, current, iptr->isn_arg.string);
5594 break;
5595 case ISN_LOADG:
5596 smsg("%s%4d LOADG g:%s", pfx, current, iptr->isn_arg.string);
5597 break;
5598 case ISN_LOADB:
5599 smsg("%s%4d LOADB b:%s", pfx, current, iptr->isn_arg.string);
5600 break;
5601 case ISN_LOADW:
5602 smsg("%s%4d LOADW w:%s", pfx, current, iptr->isn_arg.string);
5603 break;
5604 case ISN_LOADT:
5605 smsg("%s%4d LOADT t:%s", pfx, current, iptr->isn_arg.string);
5606 break;
5607 case ISN_LOADGDICT:
5608 smsg("%s%4d LOAD g:", pfx, current);
5609 break;
5610 case ISN_LOADBDICT:
5611 smsg("%s%4d LOAD b:", pfx, current);
5612 break;
5613 case ISN_LOADWDICT:
5614 smsg("%s%4d LOAD w:", pfx, current);
5615 break;
5616 case ISN_LOADTDICT:
5617 smsg("%s%4d LOAD t:", pfx, current);
5618 break;
5619 case ISN_LOADOPT:
5620 smsg("%s%4d LOADOPT %s", pfx, current, iptr->isn_arg.string);
5621 break;
5622 case ISN_LOADENV:
5623 smsg("%s%4d LOADENV %s", pfx, current, iptr->isn_arg.string);
5624 break;
5625 case ISN_LOADREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005626 smsg("%s%4d LOADREG @%c", pfx, current,
5627 (int)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005628 break;
5629
5630 case ISN_STORE:
5631 if (iptr->isn_arg.number < 0)
5632 smsg("%s%4d STORE arg[%lld]", pfx, current,
5633 iptr->isn_arg.number + STACK_FRAME_SIZE);
5634 else
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005635 smsg("%s%4d STORE $%lld", pfx, current,
5636 iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005637 break;
5638 case ISN_STOREOUTER:
5639 {
5640 if (iptr->isn_arg.number < 0)
5641 smsg("%s%4d STOREOUTEr level %d arg[%d]", pfx, current,
5642 iptr->isn_arg.outer.outer_depth,
5643 iptr->isn_arg.outer.outer_idx + STACK_FRAME_SIZE);
5644 else
5645 smsg("%s%4d STOREOUTER level %d $%d", pfx, current,
5646 iptr->isn_arg.outer.outer_depth,
5647 iptr->isn_arg.outer.outer_idx);
5648 }
5649 break;
5650 case ISN_STOREV:
5651 smsg("%s%4d STOREV v:%s", pfx, current,
5652 get_vim_var_name(iptr->isn_arg.number));
5653 break;
5654 case ISN_STOREAUTO:
5655 smsg("%s%4d STOREAUTO %s", pfx, current, iptr->isn_arg.string);
5656 break;
5657 case ISN_STOREG:
5658 smsg("%s%4d STOREG %s", pfx, current, iptr->isn_arg.string);
5659 break;
5660 case ISN_STOREB:
5661 smsg("%s%4d STOREB %s", pfx, current, iptr->isn_arg.string);
5662 break;
5663 case ISN_STOREW:
5664 smsg("%s%4d STOREW %s", pfx, current, iptr->isn_arg.string);
5665 break;
5666 case ISN_STORET:
5667 smsg("%s%4d STORET %s", pfx, current, iptr->isn_arg.string);
5668 break;
5669 case ISN_STORES:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01005670 case ISN_STOREEXPORT:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005671 {
5672 scriptitem_T *si = SCRIPT_ITEM(
5673 iptr->isn_arg.loadstore.ls_sid);
5674
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01005675 smsg("%s%4d %s %s in %s", pfx, current,
5676 iptr->isn_type == ISN_STORES
5677 ? "STORES" : "STOREEXPORT",
Bram Moolenaar4c137212021-04-19 16:48:48 +02005678 iptr->isn_arg.loadstore.ls_name, si->sn_name);
5679 }
5680 break;
5681 case ISN_STORESCRIPT:
5682 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005683 scriptref_T *sref = iptr->isn_arg.script.scriptref;
5684 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
5685 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005686
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005687 sv = get_script_svar(sref, -1);
5688 if (sv == NULL)
5689 smsg("%s%4d STORESCRIPT [deleted] in %s",
5690 pfx, current, si->sn_name);
5691 else
5692 smsg("%s%4d STORESCRIPT %s-%d in %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02005693 sv->sv_name,
5694 sref->sref_idx,
5695 si->sn_name);
5696 }
5697 break;
5698 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00005699 case ISN_STOREFUNCOPT:
5700 smsg("%s%4d %s &%s", pfx, current,
5701 iptr->isn_type == ISN_STOREOPT ? "STOREOPT" : "STOREFUNCOPT",
Bram Moolenaar4c137212021-04-19 16:48:48 +02005702 iptr->isn_arg.storeopt.so_name);
5703 break;
5704 case ISN_STOREENV:
5705 smsg("%s%4d STOREENV $%s", pfx, current, iptr->isn_arg.string);
5706 break;
5707 case ISN_STOREREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005708 smsg("%s%4d STOREREG @%c", pfx, current,
5709 (int)iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005710 break;
5711 case ISN_STORENR:
5712 smsg("%s%4d STORE %lld in $%d", pfx, current,
5713 iptr->isn_arg.storenr.stnr_val,
5714 iptr->isn_arg.storenr.stnr_idx);
5715 break;
5716
5717 case ISN_STOREINDEX:
5718 smsg("%s%4d STOREINDEX %s", pfx, current,
5719 vartype_name(iptr->isn_arg.vartype));
5720 break;
5721
5722 case ISN_STORERANGE:
5723 smsg("%s%4d STORERANGE", pfx, current);
5724 break;
5725
5726 // constants
5727 case ISN_PUSHNR:
5728 smsg("%s%4d PUSHNR %lld", pfx, current,
5729 (varnumber_T)(iptr->isn_arg.number));
5730 break;
5731 case ISN_PUSHBOOL:
5732 case ISN_PUSHSPEC:
5733 smsg("%s%4d PUSH %s", pfx, current,
5734 get_var_special_name(iptr->isn_arg.number));
5735 break;
5736 case ISN_PUSHF:
5737#ifdef FEAT_FLOAT
5738 smsg("%s%4d PUSHF %g", pfx, current, iptr->isn_arg.fnumber);
5739#endif
5740 break;
5741 case ISN_PUSHS:
5742 smsg("%s%4d PUSHS \"%s\"", pfx, current, iptr->isn_arg.string);
5743 break;
5744 case ISN_PUSHBLOB:
5745 {
5746 char_u *r;
5747 char_u numbuf[NUMBUFLEN];
5748 char_u *tofree;
5749
5750 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
5751 smsg("%s%4d PUSHBLOB %s", pfx, current, r);
5752 vim_free(tofree);
5753 }
5754 break;
5755 case ISN_PUSHFUNC:
5756 {
5757 char *name = (char *)iptr->isn_arg.string;
5758
5759 smsg("%s%4d PUSHFUNC \"%s\"", pfx, current,
5760 name == NULL ? "[none]" : name);
5761 }
5762 break;
5763 case ISN_PUSHCHANNEL:
5764#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar397a87a2022-03-20 21:14:15 +00005765 smsg("%s%4d PUSHCHANNEL 0", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005766#endif
5767 break;
5768 case ISN_PUSHJOB:
5769#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar397a87a2022-03-20 21:14:15 +00005770 smsg("%s%4d PUSHJOB \"no process\"", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005771#endif
5772 break;
5773 case ISN_PUSHEXC:
5774 smsg("%s%4d PUSH v:exception", pfx, current);
5775 break;
Bram Moolenaar06b77222022-01-25 15:51:56 +00005776 case ISN_AUTOLOAD:
5777 smsg("%s%4d AUTOLOAD %s", pfx, current, iptr->isn_arg.string);
5778 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005779 case ISN_UNLET:
5780 smsg("%s%4d UNLET%s %s", pfx, current,
5781 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
5782 iptr->isn_arg.unlet.ul_name);
5783 break;
5784 case ISN_UNLETENV:
5785 smsg("%s%4d UNLETENV%s $%s", pfx, current,
5786 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
5787 iptr->isn_arg.unlet.ul_name);
5788 break;
5789 case ISN_UNLETINDEX:
5790 smsg("%s%4d UNLETINDEX", pfx, current);
5791 break;
5792 case ISN_UNLETRANGE:
5793 smsg("%s%4d UNLETRANGE", pfx, current);
5794 break;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02005795 case ISN_LOCKUNLOCK:
5796 smsg("%s%4d LOCKUNLOCK %s", pfx, current, iptr->isn_arg.string);
5797 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005798 case ISN_LOCKCONST:
5799 smsg("%s%4d LOCKCONST", pfx, current);
5800 break;
5801 case ISN_NEWLIST:
5802 smsg("%s%4d NEWLIST size %lld", pfx, current,
5803 (varnumber_T)(iptr->isn_arg.number));
5804 break;
5805 case ISN_NEWDICT:
5806 smsg("%s%4d NEWDICT size %lld", pfx, current,
5807 (varnumber_T)(iptr->isn_arg.number));
5808 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00005809 case ISN_NEWPARTIAL:
5810 smsg("%s%4d NEWPARTIAL", pfx, current);
5811 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005812
5813 // function call
5814 case ISN_BCALL:
5815 {
5816 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
5817
5818 smsg("%s%4d BCALL %s(argc %d)", pfx, current,
5819 internal_func_name(cbfunc->cbf_idx),
5820 cbfunc->cbf_argcount);
5821 }
5822 break;
5823 case ISN_DCALL:
5824 {
5825 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
5826 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
5827 + cdfunc->cdf_idx;
5828
5829 smsg("%s%4d DCALL %s(argc %d)", pfx, current,
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005830 printable_func_name(df->df_ufunc),
5831 cdfunc->cdf_argcount);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005832 }
5833 break;
5834 case ISN_UCALL:
5835 {
5836 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
5837
5838 smsg("%s%4d UCALL %s(argc %d)", pfx, current,
5839 cufunc->cuf_name, cufunc->cuf_argcount);
5840 }
5841 break;
5842 case ISN_PCALL:
5843 {
5844 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
5845
5846 smsg("%s%4d PCALL%s (argc %d)", pfx, current,
5847 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
5848 }
5849 break;
5850 case ISN_PCALL_END:
5851 smsg("%s%4d PCALL end", pfx, current);
5852 break;
5853 case ISN_RETURN:
5854 smsg("%s%4d RETURN", pfx, current);
5855 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02005856 case ISN_RETURN_VOID:
5857 smsg("%s%4d RETURN void", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005858 break;
5859 case ISN_FUNCREF:
5860 {
5861 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar38453522021-11-28 22:00:12 +00005862 char_u *name;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005863
Bram Moolenaar38453522021-11-28 22:00:12 +00005864 if (funcref->fr_func_name == NULL)
5865 {
5866 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
5867 + funcref->fr_dfunc_idx;
5868 name = df->df_ufunc->uf_name;
5869 }
5870 else
5871 name = funcref->fr_func_name;
5872 smsg("%s%4d FUNCREF %s", pfx, current, name);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005873 }
5874 break;
5875
5876 case ISN_NEWFUNC:
5877 {
5878 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
5879
5880 smsg("%s%4d NEWFUNC %s %s", pfx, current,
5881 newfunc->nf_lambda, newfunc->nf_global);
5882 }
5883 break;
5884
5885 case ISN_DEF:
5886 {
5887 char_u *name = iptr->isn_arg.string;
5888
5889 smsg("%s%4d DEF %s", pfx, current,
5890 name == NULL ? (char_u *)"" : name);
5891 }
5892 break;
5893
5894 case ISN_JUMP:
5895 {
5896 char *when = "?";
5897
5898 switch (iptr->isn_arg.jump.jump_when)
5899 {
5900 case JUMP_ALWAYS:
5901 when = "JUMP";
5902 break;
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005903 case JUMP_NEVER:
5904 iemsg("JUMP_NEVER should not be used");
5905 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005906 case JUMP_AND_KEEP_IF_TRUE:
5907 when = "JUMP_AND_KEEP_IF_TRUE";
5908 break;
5909 case JUMP_IF_FALSE:
5910 when = "JUMP_IF_FALSE";
5911 break;
5912 case JUMP_AND_KEEP_IF_FALSE:
5913 when = "JUMP_AND_KEEP_IF_FALSE";
5914 break;
5915 case JUMP_IF_COND_FALSE:
5916 when = "JUMP_IF_COND_FALSE";
5917 break;
5918 case JUMP_IF_COND_TRUE:
5919 when = "JUMP_IF_COND_TRUE";
5920 break;
5921 }
5922 smsg("%s%4d %s -> %d", pfx, current, when,
5923 iptr->isn_arg.jump.jump_where);
5924 }
5925 break;
5926
5927 case ISN_JUMP_IF_ARG_SET:
5928 smsg("%s%4d JUMP_IF_ARG_SET arg[%d] -> %d", pfx, current,
5929 iptr->isn_arg.jumparg.jump_arg_off + STACK_FRAME_SIZE,
5930 iptr->isn_arg.jump.jump_where);
5931 break;
5932
5933 case ISN_FOR:
5934 {
5935 forloop_T *forloop = &iptr->isn_arg.forloop;
5936
5937 smsg("%s%4d FOR $%d -> %d", pfx, current,
5938 forloop->for_idx, forloop->for_end);
5939 }
5940 break;
5941
5942 case ISN_TRY:
5943 {
Bram Moolenaar0d807102021-12-21 09:42:09 +00005944 try_T *try = &iptr->isn_arg.tryref;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005945
5946 if (try->try_ref->try_finally == 0)
5947 smsg("%s%4d TRY catch -> %d, endtry -> %d",
5948 pfx, current,
5949 try->try_ref->try_catch,
5950 try->try_ref->try_endtry);
5951 else
5952 smsg("%s%4d TRY catch -> %d, finally -> %d, endtry -> %d",
5953 pfx, current,
5954 try->try_ref->try_catch,
5955 try->try_ref->try_finally,
5956 try->try_ref->try_endtry);
5957 }
5958 break;
5959 case ISN_CATCH:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005960 smsg("%s%4d CATCH", pfx, current);
5961 break;
5962 case ISN_TRYCONT:
5963 {
5964 trycont_T *trycont = &iptr->isn_arg.trycont;
5965
5966 smsg("%s%4d TRY-CONTINUE %d level%s -> %d", pfx, current,
5967 trycont->tct_levels,
5968 trycont->tct_levels == 1 ? "" : "s",
5969 trycont->tct_where);
5970 }
5971 break;
5972 case ISN_FINALLY:
5973 smsg("%s%4d FINALLY", pfx, current);
5974 break;
5975 case ISN_ENDTRY:
5976 smsg("%s%4d ENDTRY", pfx, current);
5977 break;
5978 case ISN_THROW:
5979 smsg("%s%4d THROW", pfx, current);
5980 break;
5981
5982 // expression operations on number
5983 case ISN_OPNR:
5984 case ISN_OPFLOAT:
5985 case ISN_OPANY:
5986 {
5987 char *what;
5988 char *ins;
5989
5990 switch (iptr->isn_arg.op.op_type)
5991 {
5992 case EXPR_MULT: what = "*"; break;
5993 case EXPR_DIV: what = "/"; break;
5994 case EXPR_REM: what = "%"; break;
5995 case EXPR_SUB: what = "-"; break;
5996 case EXPR_ADD: what = "+"; break;
5997 default: what = "???"; break;
5998 }
5999 switch (iptr->isn_type)
6000 {
6001 case ISN_OPNR: ins = "OPNR"; break;
6002 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
6003 case ISN_OPANY: ins = "OPANY"; break;
6004 default: ins = "???"; break;
6005 }
6006 smsg("%s%4d %s %s", pfx, current, ins, what);
6007 }
6008 break;
6009
6010 case ISN_COMPAREBOOL:
6011 case ISN_COMPARESPECIAL:
Bram Moolenaar7a222242022-03-01 19:23:24 +00006012 case ISN_COMPARENULL:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006013 case ISN_COMPARENR:
6014 case ISN_COMPAREFLOAT:
6015 case ISN_COMPARESTRING:
6016 case ISN_COMPAREBLOB:
6017 case ISN_COMPARELIST:
6018 case ISN_COMPAREDICT:
6019 case ISN_COMPAREFUNC:
6020 case ISN_COMPAREANY:
6021 {
6022 char *p;
6023 char buf[10];
6024 char *type;
6025
6026 switch (iptr->isn_arg.op.op_type)
6027 {
6028 case EXPR_EQUAL: p = "=="; break;
6029 case EXPR_NEQUAL: p = "!="; break;
6030 case EXPR_GREATER: p = ">"; break;
6031 case EXPR_GEQUAL: p = ">="; break;
6032 case EXPR_SMALLER: p = "<"; break;
6033 case EXPR_SEQUAL: p = "<="; break;
6034 case EXPR_MATCH: p = "=~"; break;
6035 case EXPR_IS: p = "is"; break;
6036 case EXPR_ISNOT: p = "isnot"; break;
6037 case EXPR_NOMATCH: p = "!~"; break;
6038 default: p = "???"; break;
6039 }
6040 STRCPY(buf, p);
6041 if (iptr->isn_arg.op.op_ic == TRUE)
6042 strcat(buf, "?");
6043 switch(iptr->isn_type)
6044 {
6045 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
6046 case ISN_COMPARESPECIAL:
6047 type = "COMPARESPECIAL"; break;
Bram Moolenaar7a222242022-03-01 19:23:24 +00006048 case ISN_COMPARENULL: type = "COMPARENULL"; break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006049 case ISN_COMPARENR: type = "COMPARENR"; break;
6050 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
6051 case ISN_COMPARESTRING:
6052 type = "COMPARESTRING"; break;
6053 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
6054 case ISN_COMPARELIST: type = "COMPARELIST"; break;
6055 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
6056 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
6057 case ISN_COMPAREANY: type = "COMPAREANY"; break;
6058 default: type = "???"; break;
6059 }
6060
6061 smsg("%s%4d %s %s", pfx, current, type, buf);
6062 }
6063 break;
6064
6065 case ISN_ADDLIST: smsg("%s%4d ADDLIST", pfx, current); break;
6066 case ISN_ADDBLOB: smsg("%s%4d ADDBLOB", pfx, current); break;
6067
6068 // expression operations
6069 case ISN_CONCAT: smsg("%s%4d CONCAT", pfx, current); break;
6070 case ISN_STRINDEX: smsg("%s%4d STRINDEX", pfx, current); break;
6071 case ISN_STRSLICE: smsg("%s%4d STRSLICE", pfx, current); break;
6072 case ISN_BLOBINDEX: smsg("%s%4d BLOBINDEX", pfx, current); break;
6073 case ISN_BLOBSLICE: smsg("%s%4d BLOBSLICE", pfx, current); break;
6074 case ISN_LISTAPPEND: smsg("%s%4d LISTAPPEND", pfx, current); break;
6075 case ISN_BLOBAPPEND: smsg("%s%4d BLOBAPPEND", pfx, current); break;
6076 case ISN_LISTINDEX: smsg("%s%4d LISTINDEX", pfx, current); break;
6077 case ISN_LISTSLICE: smsg("%s%4d LISTSLICE", pfx, current); break;
6078 case ISN_ANYINDEX: smsg("%s%4d ANYINDEX", pfx, current); break;
6079 case ISN_ANYSLICE: smsg("%s%4d ANYSLICE", pfx, current); break;
6080 case ISN_SLICE: smsg("%s%4d SLICE %lld",
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02006081 pfx, current, iptr->isn_arg.number); break;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02006082 case ISN_GETITEM: smsg("%s%4d ITEM %lld%s", pfx, current,
6083 iptr->isn_arg.getitem.gi_index,
6084 iptr->isn_arg.getitem.gi_with_op ?
6085 " with op" : ""); break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006086 case ISN_MEMBER: smsg("%s%4d MEMBER", pfx, current); break;
6087 case ISN_STRINGMEMBER: smsg("%s%4d MEMBER %s", pfx, current,
6088 iptr->isn_arg.string); break;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02006089 case ISN_CLEARDICT: smsg("%s%4d CLEARDICT", pfx, current); break;
6090 case ISN_USEDICT: smsg("%s%4d USEDICT", pfx, current); break;
6091
Bram Moolenaar4c137212021-04-19 16:48:48 +02006092 case ISN_NEGATENR: smsg("%s%4d NEGATENR", pfx, current); break;
6093
6094 case ISN_CHECKNR: smsg("%s%4d CHECKNR", pfx, current); break;
6095 case ISN_CHECKTYPE:
6096 {
6097 checktype_T *ct = &iptr->isn_arg.type;
6098 char *tofree;
6099
6100 if (ct->ct_arg_idx == 0)
6101 smsg("%s%4d CHECKTYPE %s stack[%d]", pfx, current,
6102 type_name(ct->ct_type, &tofree),
6103 (int)ct->ct_off);
6104 else
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02006105 smsg("%s%4d CHECKTYPE %s stack[%d] arg %d",
6106 pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02006107 type_name(ct->ct_type, &tofree),
6108 (int)ct->ct_off,
6109 (int)ct->ct_arg_idx);
6110 vim_free(tofree);
6111 break;
6112 }
6113 case ISN_CHECKLEN: smsg("%s%4d CHECKLEN %s%d", pfx, current,
6114 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
6115 iptr->isn_arg.checklen.cl_min_len);
6116 break;
6117 case ISN_SETTYPE:
6118 {
6119 char *tofree;
6120
6121 smsg("%s%4d SETTYPE %s", pfx, current,
6122 type_name(iptr->isn_arg.type.ct_type, &tofree));
6123 vim_free(tofree);
6124 break;
6125 }
6126 case ISN_COND2BOOL: smsg("%s%4d COND2BOOL", pfx, current); break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006127 case ISN_2BOOL: if (iptr->isn_arg.tobool.invert)
6128 smsg("%s%4d INVERT %d (!val)", pfx, current,
6129 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006130 else
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006131 smsg("%s%4d 2BOOL %d (!!val)", pfx, current,
6132 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006133 break;
6134 case ISN_2STRING: smsg("%s%4d 2STRING stack[%lld]", pfx, current,
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006135 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006136 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006137 case ISN_2STRING_ANY: smsg("%s%4d 2STRING_ANY stack[%lld]",
6138 pfx, current,
6139 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006140 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006141 case ISN_RANGE: smsg("%s%4d RANGE %s", pfx, current,
6142 iptr->isn_arg.string);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006143 break;
6144 case ISN_PUT:
6145 if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE)
6146 smsg("%s%4d PUT %c above range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006147 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006148 else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE)
6149 smsg("%s%4d PUT %c range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006150 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006151 else
6152 smsg("%s%4d PUT %c %ld", pfx, current,
6153 iptr->isn_arg.put.put_regname,
6154 (long)iptr->isn_arg.put.put_lnum);
6155 break;
6156
Bram Moolenaar4c137212021-04-19 16:48:48 +02006157 case ISN_CMDMOD:
6158 {
6159 char_u *buf;
6160 size_t len = produce_cmdmods(
6161 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
6162
6163 buf = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02006164 if (likely(buf != NULL))
Bram Moolenaar4c137212021-04-19 16:48:48 +02006165 {
6166 (void)produce_cmdmods(
6167 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
6168 smsg("%s%4d CMDMOD %s", pfx, current, buf);
6169 vim_free(buf);
6170 }
6171 break;
6172 }
6173 case ISN_CMDMOD_REV: smsg("%s%4d CMDMOD_REV", pfx, current); break;
6174
6175 case ISN_PROF_START:
Bram Moolenaare99d4222021-06-13 14:01:26 +02006176 smsg("%s%4d PROFILE START line %d", pfx, current,
6177 iptr->isn_lnum);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006178 break;
6179
6180 case ISN_PROF_END:
6181 smsg("%s%4d PROFILE END", pfx, current);
6182 break;
6183
Bram Moolenaare99d4222021-06-13 14:01:26 +02006184 case ISN_DEBUG:
Bram Moolenaar8cec9272021-06-23 20:20:53 +02006185 smsg("%s%4d DEBUG line %d-%d varcount %lld", pfx, current,
6186 iptr->isn_arg.debug.dbg_break_lnum + 1,
6187 iptr->isn_lnum,
6188 iptr->isn_arg.debug.dbg_var_names_len);
Bram Moolenaare99d4222021-06-13 14:01:26 +02006189 break;
6190
Bram Moolenaar4c137212021-04-19 16:48:48 +02006191 case ISN_UNPACK: smsg("%s%4d UNPACK %d%s", pfx, current,
6192 iptr->isn_arg.unpack.unp_count,
6193 iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : "");
6194 break;
6195 case ISN_SHUFFLE: smsg("%s%4d SHUFFLE %d up %d", pfx, current,
6196 iptr->isn_arg.shuffle.shfl_item,
6197 iptr->isn_arg.shuffle.shfl_up);
6198 break;
6199 case ISN_DROP: smsg("%s%4d DROP", pfx, current); break;
6200
6201 case ISN_FINISH: // End of list of instructions for ISN_SUBSTITUTE.
6202 return;
6203 }
6204
6205 out_flush(); // output one line at a time
6206 ui_breakcheck();
6207 if (got_int)
6208 break;
6209 }
6210}
6211
6212/*
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02006213 * Handle command line completion for the :disassemble command.
6214 */
6215 void
6216set_context_in_disassemble_cmd(expand_T *xp, char_u *arg)
6217{
6218 char_u *p;
6219
6220 // Default: expand user functions, "debug" and "profile"
6221 xp->xp_context = EXPAND_DISASSEMBLE;
6222 xp->xp_pattern = arg;
6223
6224 // first argument already typed: only user function names
6225 if (*arg != NUL && *(p = skiptowhite(arg)) != NUL)
6226 {
6227 xp->xp_context = EXPAND_USER_FUNC;
6228 xp->xp_pattern = skipwhite(p);
6229 }
6230}
6231
6232/*
6233 * Function given to ExpandGeneric() to obtain the list of :disassemble
6234 * arguments.
6235 */
6236 char_u *
6237get_disassemble_argument(expand_T *xp, int idx)
6238{
6239 if (idx == 0)
6240 return (char_u *)"debug";
6241 if (idx == 1)
6242 return (char_u *)"profile";
6243 return get_user_func_name(xp, idx - 2);
6244}
6245
6246/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01006247 * ":disassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01006248 * We don't really need this at runtime, but we do have tests that require it,
6249 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006250 */
6251 void
6252ex_disassemble(exarg_T *eap)
6253{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01006254 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01006255 char_u *fname;
6256 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006257 dfunc_T *dfunc;
6258 isn_T *instr;
Bram Moolenaarb2049902021-01-24 12:53:53 +01006259 int instr_count;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02006260 int is_global = FALSE;
Bram Moolenaare99d4222021-06-13 14:01:26 +02006261 compiletype_T compile_type = CT_NONE;
6262
Bram Moolenaarc7d5fc82021-12-05 17:20:24 +00006263 if (STRNCMP(arg, "profile", 7) == 0 && VIM_ISWHITE(arg[7]))
Bram Moolenaare99d4222021-06-13 14:01:26 +02006264 {
6265 compile_type = CT_PROFILE;
6266 arg = skipwhite(arg + 7);
6267 }
Bram Moolenaarc7d5fc82021-12-05 17:20:24 +00006268 else if (STRNCMP(arg, "debug", 5) == 0 && VIM_ISWHITE(arg[5]))
Bram Moolenaare99d4222021-06-13 14:01:26 +02006269 {
6270 compile_type = CT_DEBUG;
6271 arg = skipwhite(arg + 5);
6272 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006273
Bram Moolenaarbfd65582020-07-13 18:18:00 +02006274 if (STRNCMP(arg, "<lambda>", 8) == 0)
6275 {
6276 arg += 8;
6277 (void)getdigits(&arg);
6278 fname = vim_strnsave(eap->arg, arg - eap->arg);
6279 }
6280 else
6281 fname = trans_function_name(&arg, &is_global, FALSE,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01006282 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL, NULL);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01006283 if (fname == NULL)
6284 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006285 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01006286 return;
6287 }
6288
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00006289 ufunc = find_func(fname, is_global);
Bram Moolenaara26b9702020-04-18 19:53:28 +02006290 if (ufunc == NULL)
6291 {
6292 char_u *p = untrans_function_name(fname);
6293
6294 if (p != NULL)
6295 // Try again without making it script-local.
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00006296 ufunc = find_func(p, FALSE);
Bram Moolenaara26b9702020-04-18 19:53:28 +02006297 }
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01006298 vim_free(fname);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006299 if (ufunc == NULL)
6300 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006301 semsg(_(e_cannot_find_function_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006302 return;
6303 }
Bram Moolenaare99d4222021-06-13 14:01:26 +02006304 if (func_needs_compiling(ufunc, compile_type)
6305 && compile_def_function(ufunc, FALSE, compile_type, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02006306 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006307 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006308 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006309 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006310 return;
6311 }
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006312 msg((char *)printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006313
6314 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
Bram Moolenaare99d4222021-06-13 14:01:26 +02006315 switch (compile_type)
6316 {
6317 case CT_PROFILE:
Bram Moolenaarf002a412021-01-24 13:34:18 +01006318#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02006319 instr = dfunc->df_instr_prof;
6320 instr_count = dfunc->df_instr_prof_count;
6321 break;
Bram Moolenaarf002a412021-01-24 13:34:18 +01006322#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02006323 // FALLTHROUGH
6324 case CT_NONE:
6325 instr = dfunc->df_instr;
6326 instr_count = dfunc->df_instr_count;
6327 break;
6328 case CT_DEBUG:
6329 instr = dfunc->df_instr_debug;
6330 instr_count = dfunc->df_instr_debug_count;
6331 break;
6332 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006333
Bram Moolenaar4c137212021-04-19 16:48:48 +02006334 list_instructions("", instr, instr_count, ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006335}
6336
6337/*
Bram Moolenaar13106602020-10-04 16:06:05 +02006338 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006339 * list, etc. Mostly like what JavaScript does, except that empty list and
6340 * empty dictionary are FALSE.
6341 */
6342 int
6343tv2bool(typval_T *tv)
6344{
6345 switch (tv->v_type)
6346 {
6347 case VAR_NUMBER:
6348 return tv->vval.v_number != 0;
6349 case VAR_FLOAT:
6350#ifdef FEAT_FLOAT
6351 return tv->vval.v_float != 0.0;
6352#else
6353 break;
6354#endif
6355 case VAR_PARTIAL:
6356 return tv->vval.v_partial != NULL;
6357 case VAR_FUNC:
6358 case VAR_STRING:
6359 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
6360 case VAR_LIST:
6361 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
6362 case VAR_DICT:
6363 return tv->vval.v_dict != NULL
6364 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
6365 case VAR_BOOL:
6366 case VAR_SPECIAL:
6367 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
6368 case VAR_JOB:
6369#ifdef FEAT_JOB_CHANNEL
6370 return tv->vval.v_job != NULL;
6371#else
6372 break;
6373#endif
6374 case VAR_CHANNEL:
6375#ifdef FEAT_JOB_CHANNEL
6376 return tv->vval.v_channel != NULL;
6377#else
6378 break;
6379#endif
6380 case VAR_BLOB:
6381 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
6382 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006383 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006384 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006385 case VAR_INSTR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006386 break;
6387 }
6388 return FALSE;
6389}
6390
Bram Moolenaarea2d4072020-11-12 12:08:51 +01006391 void
6392emsg_using_string_as(typval_T *tv, int as_number)
6393{
6394 semsg(_(as_number ? e_using_string_as_number_str
6395 : e_using_string_as_bool_str),
6396 tv->vval.v_string == NULL
6397 ? (char_u *)"" : tv->vval.v_string);
6398}
6399
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006400/*
6401 * If "tv" is a string give an error and return FAIL.
6402 */
6403 int
6404check_not_string(typval_T *tv)
6405{
6406 if (tv->v_type == VAR_STRING)
6407 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +01006408 emsg_using_string_as(tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006409 clear_tv(tv);
6410 return FAIL;
6411 }
6412 return OK;
6413}
6414
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006415
6416#endif // FEAT_EVAL