blob: a1710407a83d66c2b979c01396ea3774e5d95757 [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 }
199 item->di_tv = *STACK_TV_BOT(2 * (idx - count) + 1);
200 item->di_tv.v_lock = 0;
201 if (dict_add(dict, item) == FAIL)
202 {
203 // can this ever happen?
204 dict_unref(dict);
205 return FAIL;
206 }
207 }
208 }
209
210 if (count > 0)
211 ectx->ec_stack.ga_len -= 2 * count - 1;
212 else if (GA_GROW_FAILS(&ectx->ec_stack, 1))
213 return FAIL;
214 else
215 ++ectx->ec_stack.ga_len;
216 tv = STACK_TV_BOT(-1);
217 tv->v_type = VAR_DICT;
218 tv->v_lock = 0;
219 tv->vval.v_dict = dict;
220 if (dict != NULL)
221 ++dict->dv_refcount;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200222 return OK;
223}
224
225/*
Bram Moolenaar4f8f5422021-06-20 19:28:14 +0200226 * If debug_tick changed check if "ufunc" has a breakpoint and update
227 * "uf_has_breakpoint".
228 */
Bram Moolenaar96923b72022-03-15 15:57:04 +0000229 void
Bram Moolenaar4f8f5422021-06-20 19:28:14 +0200230update_has_breakpoint(ufunc_T *ufunc)
231{
232 if (ufunc->uf_debug_tick != debug_tick)
233 {
234 linenr_T breakpoint;
235
236 ufunc->uf_debug_tick = debug_tick;
237 breakpoint = dbg_find_breakpoint(FALSE, ufunc->uf_name, 0);
238 ufunc->uf_has_breakpoint = breakpoint > 0;
239 }
240}
241
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +0200242static garray_T dict_stack = GA_EMPTY;
243
244/*
245 * Put a value on the dict stack. This consumes "tv".
246 */
247 static int
248dict_stack_save(typval_T *tv)
249{
250 if (dict_stack.ga_growsize == 0)
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000251 ga_init2(&dict_stack, sizeof(typval_T), 10);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +0200252 if (ga_grow(&dict_stack, 1) == FAIL)
253 return FAIL;
254 ((typval_T *)dict_stack.ga_data)[dict_stack.ga_len] = *tv;
255 ++dict_stack.ga_len;
256 return OK;
257}
258
259/*
260 * Get the typval at top of the dict stack.
261 */
262 static typval_T *
263dict_stack_get_tv(void)
264{
265 if (dict_stack.ga_len == 0)
266 return NULL;
267 return ((typval_T *)dict_stack.ga_data) + dict_stack.ga_len - 1;
268}
269
270/*
271 * Get the dict at top of the dict stack.
272 */
273 static dict_T *
274dict_stack_get_dict(void)
275{
276 typval_T *tv;
277
278 if (dict_stack.ga_len == 0)
279 return NULL;
280 tv = ((typval_T *)dict_stack.ga_data) + dict_stack.ga_len - 1;
281 if (tv->v_type == VAR_DICT)
282 return tv->vval.v_dict;
283 return NULL;
284}
285
286/*
287 * Drop an item from the dict stack.
288 */
289 static void
290dict_stack_drop(void)
291{
292 if (dict_stack.ga_len == 0)
293 {
294 iemsg("Dict stack underflow");
295 return;
296 }
297 --dict_stack.ga_len;
298 clear_tv(((typval_T *)dict_stack.ga_data) + dict_stack.ga_len);
299}
300
301/*
302 * Drop items from the dict stack until the length is equal to "len".
303 */
304 static void
305dict_stack_clear(int len)
306{
307 while (dict_stack.ga_len > len)
308 dict_stack_drop();
309}
310
Bram Moolenaar4f8f5422021-06-20 19:28:14 +0200311/*
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +0000312 * Get a pointer to useful "pt_outer" of "pt".
313 */
314 static outer_T *
315get_pt_outer(partial_T *pt)
316{
317 partial_T *ptref = pt->pt_outer_partial;
318
319 if (ptref == NULL)
320 return &pt->pt_outer;
321
322 // partial using partial (recursively)
323 while (ptref->pt_outer_partial != NULL)
324 ptref = ptref->pt_outer_partial;
325 return &ptref->pt_outer;
326}
327
328/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100329 * Call compiled function "cdf_idx" from compiled code.
Bram Moolenaarab360522021-01-10 14:02:28 +0100330 * This adds a stack frame and sets the instruction pointer to the start of the
331 * called function.
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200332 * If "pt" is not null use "pt->pt_outer" for ec_outer_ref->or_outer.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100333 *
334 * Stack has:
335 * - current arguments (already there)
336 * - omitted optional argument (default values) added here
337 * - stack frame:
338 * - pointer to calling function
339 * - Index of next instruction in calling function
340 * - previous frame pointer
341 * - reserved space for local variables
342 */
343 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100344call_dfunc(
345 int cdf_idx,
346 partial_T *pt,
347 int argcount_arg,
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100348 ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100349{
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100350 int argcount = argcount_arg;
351 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + cdf_idx;
352 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200353 int did_emsg_before = did_emsg_cumul + did_emsg;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100354 int arg_to_add;
355 int vararg_count = 0;
356 int varcount;
357 int idx;
358 estack_T *entry;
359 funclocal_T *floc = NULL;
Bram Moolenaar648594e2021-07-11 17:55:01 +0200360 int res = OK;
Bram Moolenaar139575d2022-03-15 19:29:30 +0000361 compiletype_T compile_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100362
363 if (dfunc->df_deleted)
364 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100365 // don't use ufunc->uf_name, it may have been freed
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000366 emsg_funcname(e_function_was_deleted_str,
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100367 dfunc->df_name == NULL ? (char_u *)"unknown" : dfunc->df_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100368 return FAIL;
369 }
370
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100371#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +0100372 if (do_profiling == PROF_YES)
373 {
Bram Moolenaar35578162021-08-02 19:10:38 +0200374 if (GA_GROW_OK(&profile_info_ga, 1))
Bram Moolenaar12d26532021-02-19 19:13:21 +0100375 {
376 profinfo_T *info = ((profinfo_T *)profile_info_ga.ga_data)
377 + profile_info_ga.ga_len;
378 ++profile_info_ga.ga_len;
379 CLEAR_POINTER(info);
380 profile_may_start_func(info, ufunc,
381 (((dfunc_T *)def_functions.ga_data)
382 + ectx->ec_dfunc_idx)->df_ufunc);
383 }
Bram Moolenaar12d26532021-02-19 19:13:21 +0100384 }
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100385#endif
386
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200387 // When debugging and using "cont" switches to the not-debugged
388 // instructions, may need to still compile them.
Bram Moolenaar139575d2022-03-15 19:29:30 +0000389 compile_type = get_compile_type(ufunc);
390 if (func_needs_compiling(ufunc, compile_type))
Bram Moolenaar648594e2021-07-11 17:55:01 +0200391 {
Bram Moolenaar139575d2022-03-15 19:29:30 +0000392 res = compile_def_function(ufunc, FALSE, compile_type, NULL);
Bram Moolenaar648594e2021-07-11 17:55:01 +0200393
394 // compile_def_function() may cause def_functions.ga_data to change
395 dfunc = ((dfunc_T *)def_functions.ga_data) + cdf_idx;
396 }
397 if (res == FAIL || INSTRUCTIONS(dfunc) == NULL)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200398 {
399 if (did_emsg_cumul + did_emsg == did_emsg_before)
400 semsg(_(e_function_is_not_compiled_str),
401 printable_func_name(ufunc));
402 return FAIL;
403 }
404
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200405 if (ufunc->uf_va_name != NULL)
406 {
Bram Moolenaarfe270812020-04-11 22:31:27 +0200407 // Need to make a list out of the vararg arguments.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200408 // Stack at time of call with 2 varargs:
409 // normal_arg
410 // optional_arg
411 // vararg_1
412 // vararg_2
Bram Moolenaarfe270812020-04-11 22:31:27 +0200413 // After creating the list:
414 // normal_arg
415 // optional_arg
416 // vararg-list
417 // With missing optional arguments we get:
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200418 // normal_arg
Bram Moolenaarfe270812020-04-11 22:31:27 +0200419 // After creating the list
420 // normal_arg
421 // (space for optional_arg)
422 // vararg-list
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200423 vararg_count = argcount - ufunc->uf_args.ga_len;
424 if (vararg_count < 0)
425 vararg_count = 0;
426 else
427 argcount -= vararg_count;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200428 if (exe_newlist(vararg_count, ectx) == FAIL)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200429 return FAIL;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200430
431 vararg_count = 1;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200432 }
433
Bram Moolenaarfe270812020-04-11 22:31:27 +0200434 arg_to_add = ufunc->uf_args.ga_len - argcount;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200435 if (arg_to_add < 0)
436 {
Bram Moolenaar79e8db92020-08-14 22:16:33 +0200437 if (arg_to_add == -1)
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200438 emsg(_(e_one_argument_too_many));
Bram Moolenaar79e8db92020-08-14 22:16:33 +0200439 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200440 semsg(_(e_nr_arguments_too_many), -arg_to_add);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200441 return FAIL;
442 }
Bram Moolenaarcd1cda22022-02-16 21:48:25 +0000443 else if (arg_to_add > ufunc->uf_def_args.ga_len)
444 {
445 int missing = arg_to_add - ufunc->uf_def_args.ga_len;
446
447 if (missing == 1)
448 emsg(_(e_one_argument_too_few));
449 else
450 semsg(_(e_nr_arguments_too_few), missing);
451 return FAIL;
452 }
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200453
454 // Reserve space for:
455 // - missing arguments
456 // - stack frame
457 // - local variables
458 // - if needed: a counter for number of closures created in
459 // ectx->ec_funcrefs.
460 varcount = dfunc->df_varcount + dfunc->df_has_closure;
Bram Moolenaar35578162021-08-02 19:10:38 +0200461 if (GA_GROW_FAILS(&ectx->ec_stack, arg_to_add + STACK_FRAME_SIZE + varcount))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100462 return FAIL;
463
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100464 // If depth of calling is getting too high, don't execute the function.
465 if (funcdepth_increment() == FAIL)
466 return FAIL;
Bram Moolenaare99d4222021-06-13 14:01:26 +0200467 ++ex_nesting_level;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100468
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100469 // Only make a copy of funclocal if it contains something to restore.
Bram Moolenaar4c137212021-04-19 16:48:48 +0200470 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100471 {
472 floc = ALLOC_ONE(funclocal_T);
473 if (floc == NULL)
474 return FAIL;
Bram Moolenaar4c137212021-04-19 16:48:48 +0200475 *floc = ectx->ec_funclocal;
476 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100477 }
478
Bram Moolenaarfe270812020-04-11 22:31:27 +0200479 // Move the vararg-list to below the missing optional arguments.
480 if (vararg_count > 0 && arg_to_add > 0)
481 *STACK_TV_BOT(arg_to_add - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100482
483 // Reserve space for omitted optional arguments, filled in soon.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200484 for (idx = 0; idx < arg_to_add; ++idx)
Bram Moolenaarfe270812020-04-11 22:31:27 +0200485 STACK_TV_BOT(idx - vararg_count)->v_type = VAR_UNKNOWN;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200486 ectx->ec_stack.ga_len += arg_to_add;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100487
488 // Store current execution state in stack frame for ISN_RETURN.
Bram Moolenaar0186e582021-01-10 18:33:11 +0100489 STACK_TV_BOT(STACK_FRAME_FUNC_OFF)->vval.v_number = ectx->ec_dfunc_idx;
490 STACK_TV_BOT(STACK_FRAME_IIDX_OFF)->vval.v_number = ectx->ec_iidx;
Bram Moolenaar5930ddc2021-04-26 20:32:59 +0200491 STACK_TV_BOT(STACK_FRAME_INSTR_OFF)->vval.v_string = (void *)ectx->ec_instr;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200492 STACK_TV_BOT(STACK_FRAME_OUTER_OFF)->vval.v_string =
493 (void *)ectx->ec_outer_ref;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100494 STACK_TV_BOT(STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string = (void *)floc;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100495 STACK_TV_BOT(STACK_FRAME_IDX_OFF)->vval.v_number = ectx->ec_frame_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200496 ectx->ec_frame_idx = ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100497
498 // Initialize local variables
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200499 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaar5cd64792021-12-25 18:23:24 +0000500 {
501 typval_T *tv = STACK_TV_BOT(STACK_FRAME_SIZE + idx);
502
503 tv->v_type = VAR_NUMBER;
504 tv->vval.v_number = 0;
505 }
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200506 if (dfunc->df_has_closure)
507 {
508 typval_T *tv = STACK_TV_BOT(STACK_FRAME_SIZE + dfunc->df_varcount);
509
510 tv->v_type = VAR_NUMBER;
511 tv->vval.v_number = 0;
512 }
513 ectx->ec_stack.ga_len += STACK_FRAME_SIZE + varcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100514
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +0100515 if (pt != NULL || ufunc->uf_partial != NULL
516 || (ufunc->uf_flags & FC_CLOSURE))
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100517 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200518 outer_ref_T *ref = ALLOC_CLEAR_ONE(outer_ref_T);
Bram Moolenaar0186e582021-01-10 18:33:11 +0100519
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200520 if (ref == NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +0100521 return FAIL;
522 if (pt != NULL)
523 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +0000524 ref->or_outer = get_pt_outer(pt);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200525 ++pt->pt_refcount;
526 ref->or_partial = pt;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100527 }
528 else if (ufunc->uf_partial != NULL)
529 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +0000530 ref->or_outer = get_pt_outer(ufunc->uf_partial);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200531 ++ufunc->uf_partial->pt_refcount;
532 ref->or_partial = ufunc->uf_partial;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100533 }
534 else
535 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200536 ref->or_outer = ALLOC_CLEAR_ONE(outer_T);
Dominique Pelle5a9e5842021-07-24 19:32:12 +0200537 if (unlikely(ref->or_outer == NULL))
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200538 {
539 vim_free(ref);
540 return FAIL;
541 }
542 ref->or_outer_allocated = TRUE;
543 ref->or_outer->out_stack = &ectx->ec_stack;
544 ref->or_outer->out_frame_idx = ectx->ec_frame_idx;
545 if (ectx->ec_outer_ref != NULL)
546 ref->or_outer->out_up = ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100547 }
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200548 ectx->ec_outer_ref = ref;
Bram Moolenaarab360522021-01-10 14:02:28 +0100549 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100550 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200551 ectx->ec_outer_ref = NULL;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100552
Bram Moolenaarc970e422021-03-17 15:03:04 +0100553 ++ufunc->uf_calls;
554
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100555 // Set execution state to the start of the called function.
556 ectx->ec_dfunc_idx = cdf_idx;
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100557 ectx->ec_instr = INSTRUCTIONS(dfunc);
Bram Moolenaarb2049902021-01-24 12:53:53 +0100558 entry = estack_push_ufunc(ufunc, 1);
Bram Moolenaarc620c052020-07-08 15:16:19 +0200559 if (entry != NULL)
560 {
561 // Set the script context to the script where the function was defined.
Bram Moolenaarc70fe462021-04-17 17:59:19 +0200562 // Save the current context so it can be restored on return.
563 entry->es_save_sctx = current_sctx;
564 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200565 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100566
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +0200567 // Start execution at the first instruction.
568 ectx->ec_iidx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100569
570 return OK;
571}
572
573// Get pointer to item in the stack.
574#define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
575
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000576// Double linked list of funcstack_T in use.
577static funcstack_T *first_funcstack = NULL;
578
579 static void
580add_funcstack_to_list(funcstack_T *funcstack)
581{
582 // Link in list of funcstacks.
583 if (first_funcstack != NULL)
584 first_funcstack->fs_prev = funcstack;
585 funcstack->fs_next = first_funcstack;
586 funcstack->fs_prev = NULL;
587 first_funcstack = funcstack;
588}
589
590 static void
591remove_funcstack_from_list(funcstack_T *funcstack)
592{
593 if (funcstack->fs_prev == NULL)
594 first_funcstack = funcstack->fs_next;
595 else
596 funcstack->fs_prev->fs_next = funcstack->fs_next;
597 if (funcstack->fs_next != NULL)
598 funcstack->fs_next->fs_prev = funcstack->fs_prev;
599}
600
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100601/*
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200602 * Used when returning from a function: Check if any closure is still
603 * referenced. If so then move the arguments and variables to a separate piece
604 * of stack to be used when the closure is called.
605 * When "free_arguments" is TRUE the arguments are to be freed.
606 * Returns FAIL when out of memory.
607 */
608 static int
609handle_closure_in_use(ectx_T *ectx, int free_arguments)
610{
611 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
612 + ectx->ec_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200613 int argcount;
614 int top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200615 int idx;
616 typval_T *tv;
617 int closure_in_use = FALSE;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200618 garray_T *gap = &ectx->ec_funcrefs;
619 varnumber_T closure_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200620
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200621 if (dfunc->df_ufunc == NULL)
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200622 return OK; // function was freed
623 if (dfunc->df_has_closure == 0)
624 return OK; // no closures
625 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + dfunc->df_varcount);
626 closure_count = tv->vval.v_number;
627 if (closure_count == 0)
628 return OK; // no funcrefs created
629
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200630 argcount = ufunc_argcount(dfunc->df_ufunc);
631 top = ectx->ec_frame_idx - argcount;
632
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200633 // Check if any created closure is still in use.
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200634 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200635 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200636 partial_T *pt;
637 int off = gap->ga_len - closure_count + idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200638
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200639 if (off < 0)
640 continue; // count is off or already done
641 pt = ((partial_T **)gap->ga_data)[off];
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200642 if (pt->pt_refcount > 1)
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200643 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200644 int refcount = pt->pt_refcount;
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200645 int i;
646
Dominique Pelleaf4a61a2021-12-27 17:21:41 +0000647 // A Reference in a local variable doesn't count, it gets
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200648 // unreferenced on return.
649 for (i = 0; i < dfunc->df_varcount; ++i)
650 {
651 typval_T *stv = STACK_TV(ectx->ec_frame_idx
652 + STACK_FRAME_SIZE + i);
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200653 if (stv->v_type == VAR_PARTIAL && pt == stv->vval.v_partial)
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200654 --refcount;
655 }
656 if (refcount > 1)
657 {
658 closure_in_use = TRUE;
659 break;
660 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200661 }
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200662 }
663
664 if (closure_in_use)
665 {
666 funcstack_T *funcstack = ALLOC_CLEAR_ONE(funcstack_T);
667 typval_T *stack;
668
669 // A closure is using the arguments and/or local variables.
670 // Move them to the called function.
671 if (funcstack == NULL)
672 return FAIL;
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000673
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200674 funcstack->fs_var_offset = argcount + STACK_FRAME_SIZE;
675 funcstack->fs_ga.ga_len = funcstack->fs_var_offset + dfunc->df_varcount;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200676 stack = ALLOC_CLEAR_MULT(typval_T, funcstack->fs_ga.ga_len);
677 funcstack->fs_ga.ga_data = stack;
678 if (stack == NULL)
679 {
680 vim_free(funcstack);
681 return FAIL;
682 }
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000683 add_funcstack_to_list(funcstack);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200684
685 // Move or copy the arguments.
686 for (idx = 0; idx < argcount; ++idx)
687 {
688 tv = STACK_TV(top + idx);
689 if (free_arguments)
690 {
691 *(stack + idx) = *tv;
692 tv->v_type = VAR_UNKNOWN;
693 }
694 else
695 copy_tv(tv, stack + idx);
696 }
697 // Move the local variables.
698 for (idx = 0; idx < dfunc->df_varcount; ++idx)
699 {
700 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + idx);
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200701
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200702 // A partial created for a local function, that is also used as a
703 // local variable, has a reference count for the variable, thus
704 // will never go down to zero. When all these refcounts are one
705 // then the funcstack is unused. We need to count how many we have
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000706 // so we know when to check.
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200707 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL)
708 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200709 int i;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200710
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200711 for (i = 0; i < closure_count; ++i)
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200712 if (tv->vval.v_partial == ((partial_T **)gap->ga_data)[
713 gap->ga_len - closure_count + i])
714 ++funcstack->fs_min_refcount;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200715 }
716
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200717 *(stack + funcstack->fs_var_offset + idx) = *tv;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200718 tv->v_type = VAR_UNKNOWN;
719 }
720
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200721 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200722 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200723 partial_T *pt = ((partial_T **)gap->ga_data)[gap->ga_len
724 - closure_count + idx];
725 if (pt->pt_refcount > 1)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200726 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200727 ++funcstack->fs_refcount;
728 pt->pt_funcstack = funcstack;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100729 pt->pt_outer.out_stack = &funcstack->fs_ga;
730 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx - top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200731 }
732 }
733 }
734
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200735 for (idx = 0; idx < closure_count; ++idx)
736 partial_unref(((partial_T **)gap->ga_data)[gap->ga_len
737 - closure_count + idx]);
738 gap->ga_len -= closure_count;
739 if (gap->ga_len == 0)
740 ga_clear(gap);
741
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200742 return OK;
743}
744
745/*
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200746 * Called when a partial is freed or its reference count goes down to one. The
747 * funcstack may be the only reference to the partials in the local variables.
748 * Go over all of them, the funcref and can be freed if all partials
749 * referencing the funcstack have a reference count of one.
750 */
751 void
752funcstack_check_refcount(funcstack_T *funcstack)
753{
754 int i;
755 garray_T *gap = &funcstack->fs_ga;
756 int done = 0;
757
758 if (funcstack->fs_refcount > funcstack->fs_min_refcount)
759 return;
760 for (i = funcstack->fs_var_offset; i < gap->ga_len; ++i)
761 {
762 typval_T *tv = ((typval_T *)gap->ga_data) + i;
763
764 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL
765 && tv->vval.v_partial->pt_funcstack == funcstack
766 && tv->vval.v_partial->pt_refcount == 1)
767 ++done;
768 }
769 if (done == funcstack->fs_min_refcount)
770 {
771 typval_T *stack = gap->ga_data;
772
773 // All partials referencing the funcstack have a reference count of
774 // one, thus the funcstack is no longer of use.
775 for (i = 0; i < gap->ga_len; ++i)
776 clear_tv(stack + i);
777 vim_free(stack);
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000778 remove_funcstack_from_list(funcstack);
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200779 vim_free(funcstack);
780 }
781}
782
783/*
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000784 * For garbage collecting: set references in all variables referenced by
785 * all funcstacks.
786 */
787 int
788set_ref_in_funcstacks(int copyID)
789{
790 funcstack_T *funcstack;
791
792 for (funcstack = first_funcstack; funcstack != NULL;
793 funcstack = funcstack->fs_next)
794 {
795 typval_T *stack = funcstack->fs_ga.ga_data;
796 int i;
797
798 for (i = 0; i < funcstack->fs_ga.ga_len; ++i)
799 if (set_ref_in_item(stack + i, copyID, NULL, NULL))
800 return TRUE; // abort
801 }
802 return FALSE;
803}
804
805/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100806 * Return from the current function.
807 */
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200808 static int
Bram Moolenaar4c137212021-04-19 16:48:48 +0200809func_return(ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100810{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100811 int idx;
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100812 int ret_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200813 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
814 + ectx->ec_dfunc_idx;
815 int argcount = ufunc_argcount(dfunc->df_ufunc);
816 int top = ectx->ec_frame_idx - argcount;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200817 estack_T *entry;
Bram Moolenaar12d26532021-02-19 19:13:21 +0100818 int prev_dfunc_idx = STACK_TV(ectx->ec_frame_idx
819 + STACK_FRAME_FUNC_OFF)->vval.v_number;
Bram Moolenaarb06b50d2021-04-26 21:39:25 +0200820 funclocal_T *floc;
821#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +0100822 dfunc_T *prev_dfunc = ((dfunc_T *)def_functions.ga_data)
823 + prev_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100824
Bram Moolenaar12d26532021-02-19 19:13:21 +0100825 if (do_profiling == PROF_YES)
826 {
827 ufunc_T *caller = prev_dfunc->df_ufunc;
828
829 if (dfunc->df_ufunc->uf_profiling
830 || (caller != NULL && caller->uf_profiling))
831 {
832 profile_may_end_func(((profinfo_T *)profile_info_ga.ga_data)
833 + profile_info_ga.ga_len - 1, dfunc->df_ufunc, caller);
834 --profile_info_ga.ga_len;
835 }
836 }
837#endif
Bram Moolenaar919c12c2021-12-14 14:29:16 +0000838
839 // No check for uf_refcount being zero, cannot think of a way that would
840 // happen.
Bram Moolenaarc970e422021-03-17 15:03:04 +0100841 --dfunc->df_ufunc->uf_calls;
842
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100843 // execution context goes one level up
Bram Moolenaarc620c052020-07-08 15:16:19 +0200844 entry = estack_pop();
845 if (entry != NULL)
Bram Moolenaarc70fe462021-04-17 17:59:19 +0200846 current_sctx = entry->es_save_sctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100847
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200848 if (handle_closure_in_use(ectx, TRUE) == FAIL)
849 return FAIL;
850
851 // Clear the arguments.
852 for (idx = top; idx < ectx->ec_frame_idx; ++idx)
853 clear_tv(STACK_TV(idx));
854
855 // Clear local variables and temp values, but not the return value.
856 for (idx = ectx->ec_frame_idx + STACK_FRAME_SIZE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100857 idx < ectx->ec_stack.ga_len - 1; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100858 clear_tv(STACK_TV(idx));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100859
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100860 // The return value should be on top of the stack. However, when aborting
861 // it may not be there and ec_frame_idx is the top of the stack.
862 ret_idx = ectx->ec_stack.ga_len - 1;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100863 if (ret_idx == ectx->ec_frame_idx + STACK_FRAME_IDX_OFF)
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100864 ret_idx = 0;
865
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200866 if (ectx->ec_outer_ref != NULL)
867 {
868 if (ectx->ec_outer_ref->or_outer_allocated)
869 vim_free(ectx->ec_outer_ref->or_outer);
870 partial_unref(ectx->ec_outer_ref->or_partial);
871 vim_free(ectx->ec_outer_ref);
872 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100873
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100874 // Restore the previous frame.
Bram Moolenaar12d26532021-02-19 19:13:21 +0100875 ectx->ec_dfunc_idx = prev_dfunc_idx;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100876 ectx->ec_iidx = STACK_TV(ectx->ec_frame_idx
877 + STACK_FRAME_IIDX_OFF)->vval.v_number;
Bram Moolenaar5930ddc2021-04-26 20:32:59 +0200878 ectx->ec_instr = (void *)STACK_TV(ectx->ec_frame_idx
879 + STACK_FRAME_INSTR_OFF)->vval.v_string;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200880 ectx->ec_outer_ref = (void *)STACK_TV(ectx->ec_frame_idx
Bram Moolenaar0186e582021-01-10 18:33:11 +0100881 + STACK_FRAME_OUTER_OFF)->vval.v_string;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100882 floc = (void *)STACK_TV(ectx->ec_frame_idx
883 + STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string;
Bram Moolenaar5366e1a2020-10-01 13:01:34 +0200884 // restoring ec_frame_idx must be last
Bram Moolenaar0186e582021-01-10 18:33:11 +0100885 ectx->ec_frame_idx = STACK_TV(ectx->ec_frame_idx
886 + STACK_FRAME_IDX_OFF)->vval.v_number;
Bram Moolenaard386e922021-04-25 14:48:49 +0200887
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100888 if (floc == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +0200889 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100890 else
891 {
Bram Moolenaar4c137212021-04-19 16:48:48 +0200892 ectx->ec_funclocal = *floc;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100893 vim_free(floc);
894 }
895
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100896 if (ret_idx > 0)
897 {
898 // Reset the stack to the position before the call, with a spot for the
899 // return value, moved there from above the frame.
900 ectx->ec_stack.ga_len = top + 1;
901 *STACK_TV_BOT(-1) = *STACK_TV(ret_idx);
902 }
903 else
904 // Reset the stack to the position before the call.
905 ectx->ec_stack.ga_len = top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200906
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100907 funcdepth_decrement();
Bram Moolenaare99d4222021-06-13 14:01:26 +0200908 --ex_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200909 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100910}
911
912#undef STACK_TV
913
914/*
915 * Prepare arguments and rettv for calling a builtin or user function.
916 */
917 static int
918call_prepare(int argcount, typval_T *argvars, ectx_T *ectx)
919{
920 int idx;
921 typval_T *tv;
922
923 // Move arguments from bottom of the stack to argvars[] and add terminator.
924 for (idx = 0; idx < argcount; ++idx)
925 argvars[idx] = *STACK_TV_BOT(idx - argcount);
926 argvars[argcount].v_type = VAR_UNKNOWN;
927
928 // Result replaces the arguments on the stack.
929 if (argcount > 0)
930 ectx->ec_stack.ga_len -= argcount - 1;
Bram Moolenaar35578162021-08-02 19:10:38 +0200931 else if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100932 return FAIL;
933 else
934 ++ectx->ec_stack.ga_len;
935
936 // Default return value is zero.
937 tv = STACK_TV_BOT(-1);
938 tv->v_type = VAR_NUMBER;
939 tv->vval.v_number = 0;
Bram Moolenaar24565cf2022-03-28 18:16:52 +0100940 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100941
942 return OK;
943}
944
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200945// Ugly global to avoid passing the execution context around through many
946// layers.
947static ectx_T *current_ectx = NULL;
948
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100949/*
950 * Call a builtin function by index.
951 */
952 static int
953call_bfunc(int func_idx, int argcount, ectx_T *ectx)
954{
955 typval_T argvars[MAX_FUNC_ARGS];
956 int idx;
Bram Moolenaar171fb922020-10-28 16:54:47 +0100957 int did_emsg_before = did_emsg;
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200958 ectx_T *prev_ectx = current_ectx;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +0200959 char *save_func_name = ectx->ec_where.wt_func_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100960
961 if (call_prepare(argcount, argvars, ectx) == FAIL)
962 return FAIL;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +0200963 ectx->ec_where.wt_func_name = internal_func_name(func_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100964
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200965 // Call the builtin function. Set "current_ectx" so that when it
966 // recursively invokes call_def_function() a closure context can be set.
967 current_ectx = ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100968 call_internal_func_by_idx(func_idx, argvars, STACK_TV_BOT(-1));
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200969 current_ectx = prev_ectx;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +0200970 ectx->ec_where.wt_func_name = save_func_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100971
972 // Clear the arguments.
973 for (idx = 0; idx < argcount; ++idx)
974 clear_tv(&argvars[idx]);
Bram Moolenaar015f4262020-05-05 21:25:22 +0200975
Bram Moolenaar57f799e2020-12-12 20:42:19 +0100976 if (did_emsg > did_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200977 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100978 return OK;
979}
980
981/*
982 * Execute a user defined function.
Bram Moolenaarab360522021-01-10 14:02:28 +0100983 * If the function is compiled this will add a stack frame and set the
984 * instruction pointer at the start of the function.
985 * Otherwise the function is called here.
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200986 * If "pt" is not null use "pt->pt_outer" for ec_outer_ref->or_outer.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100987 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100988 */
989 static int
Bram Moolenaar0186e582021-01-10 18:33:11 +0100990call_ufunc(
991 ufunc_T *ufunc,
992 partial_T *pt,
993 int argcount,
994 ectx_T *ectx,
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +0200995 isn_T *iptr,
996 dict_T *selfdict)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100997{
998 typval_T argvars[MAX_FUNC_ARGS];
999 funcexe_T funcexe;
1000 int error;
1001 int idx;
Bram Moolenaar28ee8922020-10-28 20:20:00 +01001002 int did_emsg_before = did_emsg;
Bram Moolenaar139575d2022-03-15 19:29:30 +00001003 compiletype_T compile_type = get_compile_type(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001004
Bram Moolenaare99d4222021-06-13 14:01:26 +02001005 if (func_needs_compiling(ufunc, compile_type)
1006 && compile_def_function(ufunc, FALSE, compile_type, NULL)
1007 == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02001008 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001009 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001010 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +01001011 error = check_user_func_argcount(ufunc, argcount);
Bram Moolenaar50824712020-12-20 21:10:17 +01001012 if (error != FCERR_UNKNOWN)
1013 {
1014 if (error == FCERR_TOOMANY)
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001015 semsg(_(e_too_many_arguments_for_function_str),
1016 printable_func_name(ufunc));
Bram Moolenaar50824712020-12-20 21:10:17 +01001017 else
Bram Moolenaare1242042021-12-16 20:56:57 +00001018 semsg(_(e_not_enough_arguments_for_function_str),
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001019 printable_func_name(ufunc));
Bram Moolenaar50824712020-12-20 21:10:17 +01001020 return FAIL;
1021 }
1022
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001023 // The function has been compiled, can call it quickly. For a function
1024 // that was defined later: we can call it directly next time.
1025 if (iptr != NULL)
1026 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001027 delete_instr(iptr);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001028 iptr->isn_type = ISN_DCALL;
1029 iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1030 iptr->isn_arg.dfunc.cdf_argcount = argcount;
1031 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001032 return call_dfunc(ufunc->uf_dfunc_idx, pt, argcount, ectx);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001033 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001034
1035 if (call_prepare(argcount, argvars, ectx) == FAIL)
1036 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +02001037 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00001038 funcexe.fe_evaluate = TRUE;
1039 funcexe.fe_selfdict = selfdict != NULL ? selfdict : dict_stack_get_dict();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001040
1041 // Call the user function. Result goes in last position on the stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001042 error = call_user_func_check(ufunc, argcount, argvars,
Bram Moolenaar851f86b2021-12-13 14:26:44 +00001043 STACK_TV_BOT(-1), &funcexe, funcexe.fe_selfdict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001044
1045 // Clear the arguments.
1046 for (idx = 0; idx < argcount; ++idx)
1047 clear_tv(&argvars[idx]);
1048
1049 if (error != FCERR_NONE)
1050 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001051 user_func_error(error, printable_func_name(ufunc), &funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001052 return FAIL;
1053 }
Bram Moolenaar28ee8922020-10-28 20:20:00 +01001054 if (did_emsg > did_emsg_before)
Bram Moolenaared677f52020-08-12 16:38:10 +02001055 // Error other than from calling the function itself.
1056 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001057 return OK;
1058}
1059
1060/*
Bram Moolenaara91a7132021-03-25 21:12:15 +01001061 * If command modifiers were applied restore them.
1062 */
1063 static void
1064may_restore_cmdmod(funclocal_T *funclocal)
1065{
1066 if (funclocal->floc_restore_cmdmod)
1067 {
1068 cmdmod.cmod_filter_regmatch.regprog = NULL;
1069 undo_cmdmod(&cmdmod);
1070 cmdmod = funclocal->floc_save_cmdmod;
1071 funclocal->floc_restore_cmdmod = FALSE;
1072 }
1073}
1074
1075/*
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001076 * Return TRUE if an error was given (not caught in try/catch) or CTRL-C was
1077 * pressed.
Bram Moolenaara1773442020-08-12 15:21:22 +02001078 */
1079 static int
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001080vim9_aborting(int prev_uncaught_emsg)
Bram Moolenaara1773442020-08-12 15:21:22 +02001081{
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001082 return uncaught_emsg > prev_uncaught_emsg || got_int || did_throw;
Bram Moolenaara1773442020-08-12 15:21:22 +02001083}
1084
1085/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001086 * Execute a function by "name".
1087 * This can be a builtin function or a user function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001088 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001089 * Returns FAIL if not found without an error message.
1090 */
1091 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001092call_by_name(
1093 char_u *name,
1094 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001095 ectx_T *ectx,
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001096 isn_T *iptr,
1097 dict_T *selfdict)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001098{
1099 ufunc_T *ufunc;
1100
1101 if (builtin_function(name, -1))
1102 {
1103 int func_idx = find_internal_func(name);
1104
Bram Moolenaar1983f1a2022-02-28 20:55:02 +00001105 if (func_idx < 0) // Impossible?
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001106 return FAIL;
Bram Moolenaar389df252020-07-09 21:20:47 +02001107 if (check_internal_func(func_idx, argcount) < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001108 return FAIL;
1109 return call_bfunc(func_idx, argcount, ectx);
1110 }
1111
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00001112 ufunc = find_func(name, FALSE);
Bram Moolenaara1773442020-08-12 15:21:22 +02001113
1114 if (ufunc == NULL)
1115 {
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001116 int prev_uncaught_emsg = uncaught_emsg;
Bram Moolenaara1773442020-08-12 15:21:22 +02001117
1118 if (script_autoload(name, TRUE))
1119 // loaded a package, search for the function again
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00001120 ufunc = find_func(name, FALSE);
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001121
1122 if (vim9_aborting(prev_uncaught_emsg))
Bram Moolenaara1773442020-08-12 15:21:22 +02001123 return FAIL; // bail out if loading the script caused an error
1124 }
1125
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001126 if (ufunc != NULL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001127 {
Bram Moolenaar6ce46b92021-08-07 15:35:36 +02001128 if (ufunc->uf_arg_types != NULL || ufunc->uf_va_type != NULL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001129 {
1130 int i;
1131 typval_T *argv = STACK_TV_BOT(0) - argcount;
1132
1133 // The function can change at runtime, check that the argument
1134 // types are correct.
1135 for (i = 0; i < argcount; ++i)
1136 {
Bram Moolenaare3ffcd92021-03-08 21:47:13 +01001137 type_T *type = NULL;
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001138
Bram Moolenaar6ce46b92021-08-07 15:35:36 +02001139 if (i < ufunc->uf_args.ga_len && ufunc->uf_arg_types != NULL)
Bram Moolenaare3ffcd92021-03-08 21:47:13 +01001140 type = ufunc->uf_arg_types[i];
1141 else if (ufunc->uf_va_type != NULL)
1142 type = ufunc->uf_va_type->tt_member;
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001143 if (type != NULL && check_typval_arg_type(type,
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001144 &argv[i], NULL, i + 1) == FAIL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001145 return FAIL;
1146 }
1147 }
1148
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001149 return call_ufunc(ufunc, NULL, argcount, ectx, iptr, selfdict);
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001150 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001151
1152 return FAIL;
1153}
1154
1155 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001156call_partial(
1157 typval_T *tv,
1158 int argcount_arg,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001159 ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001160{
Bram Moolenaara90afb92020-07-15 22:38:56 +02001161 int argcount = argcount_arg;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001162 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001163 int called_emsg_before = called_emsg;
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +01001164 int res = FAIL;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001165 dict_T *selfdict = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001166
1167 if (tv->v_type == VAR_PARTIAL)
1168 {
Bram Moolenaara90afb92020-07-15 22:38:56 +02001169 partial_T *pt = tv->vval.v_partial;
1170 int i;
1171
1172 if (pt->pt_argc > 0)
1173 {
1174 // Make space for arguments from the partial, shift the "argcount"
1175 // arguments up.
Bram Moolenaar35578162021-08-02 19:10:38 +02001176 if (GA_GROW_FAILS(&ectx->ec_stack, pt->pt_argc))
Bram Moolenaara90afb92020-07-15 22:38:56 +02001177 return FAIL;
1178 for (i = 1; i <= argcount; ++i)
1179 *STACK_TV_BOT(-i + pt->pt_argc) = *STACK_TV_BOT(-i);
1180 ectx->ec_stack.ga_len += pt->pt_argc;
1181 argcount += pt->pt_argc;
1182
1183 // copy the arguments from the partial onto the stack
1184 for (i = 0; i < pt->pt_argc; ++i)
1185 copy_tv(&pt->pt_argv[i], STACK_TV_BOT(-argcount + i));
1186 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001187 selfdict = pt->pt_dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001188
1189 if (pt->pt_func != NULL)
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001190 return call_ufunc(pt->pt_func, pt, argcount, ectx, NULL, selfdict);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001191
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001192 name = pt->pt_name;
1193 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001194 else if (tv->v_type == VAR_FUNC)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001195 name = tv->vval.v_string;
Bram Moolenaar95006e32020-08-29 17:47:08 +02001196 if (name != NULL)
1197 {
1198 char_u fname_buf[FLEN_FIXED + 1];
1199 char_u *tofree = NULL;
1200 int error = FCERR_NONE;
1201 char_u *fname;
1202
1203 // May need to translate <SNR>123_ to K_SNR.
1204 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
1205 if (error != FCERR_NONE)
1206 res = FAIL;
1207 else
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001208 res = call_by_name(fname, argcount, ectx, NULL, selfdict);
Bram Moolenaar95006e32020-08-29 17:47:08 +02001209 vim_free(tofree);
1210 }
1211
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +01001212 if (res == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001213 {
1214 if (called_emsg == called_emsg_before)
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001215 emsg_funcname(e_unknown_function_str,
Bram Moolenaar015f4262020-05-05 21:25:22 +02001216 name == NULL ? (char_u *)"[unknown]" : name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001217 return FAIL;
1218 }
1219 return OK;
1220}
1221
1222/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001223 * Check if "lock" is VAR_LOCKED or VAR_FIXED. If so give an error and return
1224 * TRUE.
1225 */
1226 static int
1227error_if_locked(int lock, char *error)
1228{
1229 if (lock & (VAR_LOCKED | VAR_FIXED))
1230 {
1231 emsg(_(error));
1232 return TRUE;
1233 }
1234 return FALSE;
1235}
1236
1237/*
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001238 * Give an error if "tv" is not a number and return FAIL.
1239 */
1240 static int
1241check_for_number(typval_T *tv)
1242{
1243 if (tv->v_type != VAR_NUMBER)
1244 {
1245 semsg(_(e_expected_str_but_got_str),
1246 vartype_name(VAR_NUMBER), vartype_name(tv->v_type));
1247 return FAIL;
1248 }
1249 return OK;
1250}
1251
1252/*
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001253 * Store "tv" in variable "name".
1254 * This is for s: and g: variables.
1255 */
1256 static void
1257store_var(char_u *name, typval_T *tv)
1258{
1259 funccal_entry_T entry;
Bram Moolenaard877a572021-04-01 19:42:48 +02001260 int flags = ASSIGN_DECL;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001261
Bram Moolenaard877a572021-04-01 19:42:48 +02001262 if (tv->v_lock)
1263 flags |= ASSIGN_CONST;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001264 save_funccal(&entry);
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001265 set_var_const(name, 0, NULL, tv, FALSE, flags, 0);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001266 restore_funccal();
1267}
1268
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001269/*
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001270 * Convert "tv" to a string.
1271 * Return FAIL if not allowed.
1272 */
1273 static int
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001274do_2string(typval_T *tv, int is_2string_any, int tolerant)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001275{
1276 if (tv->v_type != VAR_STRING)
1277 {
1278 char_u *str;
1279
1280 if (is_2string_any)
1281 {
1282 switch (tv->v_type)
1283 {
1284 case VAR_SPECIAL:
1285 case VAR_BOOL:
1286 case VAR_NUMBER:
1287 case VAR_FLOAT:
1288 case VAR_BLOB: break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001289
1290 case VAR_LIST:
1291 if (tolerant)
1292 {
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001293 char_u *s, *e, *p;
1294 garray_T ga;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001295
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001296 ga_init2(&ga, sizeof(char_u *), 1);
1297
1298 // Convert to NL separated items, then
1299 // escape the items and replace the NL with
1300 // a space.
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001301 str = typval2string(tv, TRUE);
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001302 if (str == NULL)
1303 return FAIL;
1304 s = str;
1305 while ((e = vim_strchr(s, '\n')) != NULL)
1306 {
1307 *e = NUL;
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +01001308 p = vim_strsave_fnameescape(s,
1309 VSE_NONE);
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001310 if (p != NULL)
1311 {
1312 ga_concat(&ga, p);
1313 ga_concat(&ga, (char_u *)" ");
1314 vim_free(p);
1315 }
1316 s = e + 1;
1317 }
1318 vim_free(str);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001319 clear_tv(tv);
1320 tv->v_type = VAR_STRING;
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001321 tv->vval.v_string = ga.ga_data;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001322 return OK;
1323 }
1324 // FALLTHROUGH
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001325 default: to_string_error(tv->v_type);
1326 return FAIL;
1327 }
1328 }
Bram Moolenaar34453202021-01-31 13:08:38 +01001329 str = typval_tostring(tv, TRUE);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001330 clear_tv(tv);
1331 tv->v_type = VAR_STRING;
1332 tv->vval.v_string = str;
1333 }
1334 return OK;
1335}
1336
1337/*
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001338 * When the value of "sv" is a null list of dict, allocate it.
1339 */
1340 static void
Bram Moolenaar859cc212022-03-28 15:22:35 +01001341allocate_if_null(svar_T *sv)
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001342{
Bram Moolenaar859cc212022-03-28 15:22:35 +01001343 typval_T *tv = sv->sv_tv;
1344
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001345 switch (tv->v_type)
1346 {
1347 case VAR_LIST:
Bram Moolenaar859cc212022-03-28 15:22:35 +01001348 if (tv->vval.v_list == NULL && sv->sv_type != &t_list_empty)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001349 (void)rettv_list_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001350 break;
1351 case VAR_DICT:
Bram Moolenaar859cc212022-03-28 15:22:35 +01001352 if (tv->vval.v_dict == NULL && sv->sv_type != &t_dict_empty)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001353 (void)rettv_dict_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001354 break;
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02001355 case VAR_BLOB:
Bram Moolenaar859cc212022-03-28 15:22:35 +01001356 if (tv->vval.v_blob == NULL && sv->sv_type != &t_blob_null)
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02001357 (void)rettv_blob_alloc(tv);
1358 break;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001359 default:
1360 break;
1361 }
1362}
Bram Moolenaard3aac292020-04-19 14:32:17 +02001363
Bram Moolenaare7525c52021-01-09 13:20:37 +01001364/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001365 * Return the character "str[index]" where "index" is the character index,
1366 * including composing characters.
1367 * If "index" is out of range NULL is returned.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001368 */
1369 char_u *
1370char_from_string(char_u *str, varnumber_T index)
1371{
1372 size_t nbyte = 0;
1373 varnumber_T nchar = index;
1374 size_t slen;
1375
1376 if (str == NULL)
1377 return NULL;
1378 slen = STRLEN(str);
1379
Bram Moolenaarff871402021-03-26 13:34:05 +01001380 // Do the same as for a list: a negative index counts from the end.
1381 // Optimization to check the first byte to be below 0x80 (and no composing
1382 // character follows) makes this a lot faster.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001383 if (index < 0)
1384 {
1385 int clen = 0;
1386
1387 for (nbyte = 0; nbyte < slen; ++clen)
Bram Moolenaarff871402021-03-26 13:34:05 +01001388 {
1389 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1390 ++nbyte;
1391 else if (enc_utf8)
1392 nbyte += utfc_ptr2len(str + nbyte);
1393 else
1394 nbyte += mb_ptr2len(str + nbyte);
1395 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001396 nchar = clen + index;
1397 if (nchar < 0)
1398 // unlike list: index out of range results in empty string
1399 return NULL;
1400 }
1401
1402 for (nbyte = 0; nchar > 0 && nbyte < slen; --nchar)
Bram Moolenaarff871402021-03-26 13:34:05 +01001403 {
1404 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1405 ++nbyte;
1406 else if (enc_utf8)
1407 nbyte += utfc_ptr2len(str + nbyte);
1408 else
1409 nbyte += mb_ptr2len(str + nbyte);
1410 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001411 if (nbyte >= slen)
1412 return NULL;
Bram Moolenaar0289a092021-03-14 18:40:19 +01001413 return vim_strnsave(str + nbyte, mb_ptr2len(str + nbyte));
Bram Moolenaare7525c52021-01-09 13:20:37 +01001414}
1415
1416/*
1417 * Get the byte index for character index "idx" in string "str" with length
Bram Moolenaar0289a092021-03-14 18:40:19 +01001418 * "str_len". Composing characters are included.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001419 * If going over the end return "str_len".
1420 * If "idx" is negative count from the end, -1 is the last character.
1421 * When going over the start return -1.
1422 */
1423 static long
1424char_idx2byte(char_u *str, size_t str_len, varnumber_T idx)
1425{
1426 varnumber_T nchar = idx;
1427 size_t nbyte = 0;
1428
1429 if (nchar >= 0)
1430 {
1431 while (nchar > 0 && nbyte < str_len)
1432 {
Bram Moolenaar0289a092021-03-14 18:40:19 +01001433 nbyte += mb_ptr2len(str + nbyte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001434 --nchar;
1435 }
1436 }
1437 else
1438 {
1439 nbyte = str_len;
1440 while (nchar < 0 && nbyte > 0)
1441 {
1442 --nbyte;
1443 nbyte -= mb_head_off(str, str + nbyte);
1444 ++nchar;
1445 }
1446 if (nchar < 0)
1447 return -1;
1448 }
1449 return (long)nbyte;
1450}
1451
1452/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001453 * Return the slice "str[first : last]" using character indexes. Composing
1454 * characters are included.
Bram Moolenaar6601b622021-01-13 21:47:15 +01001455 * "exclusive" is TRUE for slice().
Bram Moolenaare7525c52021-01-09 13:20:37 +01001456 * Return NULL when the result is empty.
1457 */
1458 char_u *
Bram Moolenaar6601b622021-01-13 21:47:15 +01001459string_slice(char_u *str, varnumber_T first, varnumber_T last, int exclusive)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001460{
1461 long start_byte, end_byte;
1462 size_t slen;
1463
1464 if (str == NULL)
1465 return NULL;
1466 slen = STRLEN(str);
1467 start_byte = char_idx2byte(str, slen, first);
1468 if (start_byte < 0)
1469 start_byte = 0; // first index very negative: use zero
Bram Moolenaar6601b622021-01-13 21:47:15 +01001470 if ((last == -1 && !exclusive) || last == VARNUM_MAX)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001471 end_byte = (long)slen;
1472 else
1473 {
1474 end_byte = char_idx2byte(str, slen, last);
Bram Moolenaar6601b622021-01-13 21:47:15 +01001475 if (!exclusive && end_byte >= 0 && end_byte < (long)slen)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001476 // end index is inclusive
Bram Moolenaar0289a092021-03-14 18:40:19 +01001477 end_byte += mb_ptr2len(str + end_byte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001478 }
1479
1480 if (start_byte >= (long)slen || end_byte <= start_byte)
1481 return NULL;
1482 return vim_strnsave(str + start_byte, end_byte - start_byte);
1483}
1484
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001485/*
1486 * Get a script variable for ISN_STORESCRIPT and ISN_LOADSCRIPT.
1487 * When "dfunc_idx" is negative don't give an error.
1488 * Returns NULL for an error.
1489 */
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001490 static svar_T *
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001491get_script_svar(scriptref_T *sref, int dfunc_idx)
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001492{
1493 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001494 dfunc_T *dfunc = dfunc_idx < 0 ? NULL
1495 : ((dfunc_T *)def_functions.ga_data) + dfunc_idx;
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001496 svar_T *sv;
1497
1498 if (sref->sref_seq != si->sn_script_seq)
1499 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001500 // The script was reloaded after the function was compiled, the
1501 // script_idx may not be valid.
1502 if (dfunc != NULL)
1503 semsg(_(e_script_variable_invalid_after_reload_in_function_str),
1504 printable_func_name(dfunc->df_ufunc));
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001505 return NULL;
1506 }
1507 sv = ((svar_T *)si->sn_var_vals.ga_data) + sref->sref_idx;
Bram Moolenaar60dc8272021-07-29 22:48:54 +02001508 if (!equal_type(sv->sv_type, sref->sref_type, 0))
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001509 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001510 if (dfunc != NULL)
1511 emsg(_(e_script_variable_type_changed));
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001512 return NULL;
1513 }
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001514
1515 if (!sv->sv_export && sref->sref_sid != current_sctx.sc_sid)
1516 {
1517 if (dfunc != NULL)
1518 semsg(_(e_item_not_exported_in_script_str), sv->sv_name);
1519 return NULL;
1520 }
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001521 return sv;
1522}
1523
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001524/*
Bram Moolenaar20677332021-06-06 17:02:53 +02001525 * Function passed to do_cmdline() for splitting a script joined by NL
1526 * characters.
1527 */
1528 static char_u *
1529get_split_sourceline(
1530 int c UNUSED,
1531 void *cookie,
1532 int indent UNUSED,
1533 getline_opt_T options UNUSED)
1534{
1535 source_cookie_T *sp = (source_cookie_T *)cookie;
1536 char_u *p;
1537 char_u *line;
1538
Bram Moolenaar20677332021-06-06 17:02:53 +02001539 p = vim_strchr(sp->nextline, '\n');
1540 if (p == NULL)
1541 {
1542 line = vim_strsave(sp->nextline);
1543 sp->nextline += STRLEN(sp->nextline);
1544 }
1545 else
1546 {
1547 line = vim_strnsave(sp->nextline, p - sp->nextline);
1548 sp->nextline = p + 1;
1549 }
1550 return line;
1551}
1552
1553/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001554 * Execute a function by "name".
1555 * This can be a builtin function, user function or a funcref.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001556 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001557 */
1558 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001559call_eval_func(
1560 char_u *name,
1561 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001562 ectx_T *ectx,
1563 isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001564{
Bram Moolenaared677f52020-08-12 16:38:10 +02001565 int called_emsg_before = called_emsg;
1566 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001567
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001568 res = call_by_name(name, argcount, ectx, iptr, NULL);
Bram Moolenaared677f52020-08-12 16:38:10 +02001569 if (res == FAIL && called_emsg == called_emsg_before)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001570 {
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001571 dictitem_T *v;
1572
1573 v = find_var(name, NULL, FALSE);
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001574 if (v == NULL || (v->di_tv.v_type != VAR_PARTIAL
1575 && v->di_tv.v_type != VAR_FUNC))
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001576 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001577 emsg_funcname(e_unknown_function_str, name);
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001578 return FAIL;
1579 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001580 return call_partial(&v->di_tv, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001581 }
Bram Moolenaared677f52020-08-12 16:38:10 +02001582 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001583}
1584
1585/*
Bram Moolenaarf112f302020-12-20 17:47:52 +01001586 * When a function reference is used, fill a partial with the information
1587 * needed, especially when it is used as a closure.
1588 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001589 int
Bram Moolenaarf112f302020-12-20 17:47:52 +01001590fill_partial_and_closure(partial_T *pt, ufunc_T *ufunc, ectx_T *ectx)
1591{
1592 pt->pt_func = ufunc;
1593 pt->pt_refcount = 1;
1594
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001595 if (ufunc->uf_flags & FC_CLOSURE)
Bram Moolenaarf112f302020-12-20 17:47:52 +01001596 {
1597 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1598 + ectx->ec_dfunc_idx;
1599
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001600 // The closure may need to find arguments and local variables in the
1601 // current stack.
Bram Moolenaar0186e582021-01-10 18:33:11 +01001602 pt->pt_outer.out_stack = &ectx->ec_stack;
1603 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001604 if (ectx->ec_outer_ref != NULL)
1605 {
1606 // The current context already has a context, link to that one.
1607 pt->pt_outer.out_up = ectx->ec_outer_ref->or_outer;
1608 if (ectx->ec_outer_ref->or_partial != NULL)
1609 {
1610 pt->pt_outer.out_up_partial = ectx->ec_outer_ref->or_partial;
1611 ++pt->pt_outer.out_up_partial->pt_refcount;
1612 }
1613 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01001614
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001615 // If this function returns and the closure is still being used, we
1616 // need to make a copy of the context (arguments and local variables).
1617 // Store a reference to the partial so we can handle that.
Bram Moolenaar35578162021-08-02 19:10:38 +02001618 if (GA_GROW_FAILS(&ectx->ec_funcrefs, 1))
Bram Moolenaarf112f302020-12-20 17:47:52 +01001619 {
1620 vim_free(pt);
1621 return FAIL;
1622 }
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001623 // Extra variable keeps the count of closures created in the current
1624 // function call.
Bram Moolenaarf112f302020-12-20 17:47:52 +01001625 ++(((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx
1626 + STACK_FRAME_SIZE + dfunc->df_varcount)->vval.v_number;
1627
1628 ((partial_T **)ectx->ec_funcrefs.ga_data)
1629 [ectx->ec_funcrefs.ga_len] = pt;
1630 ++pt->pt_refcount;
1631 ++ectx->ec_funcrefs.ga_len;
1632 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001633 ++ufunc->uf_refcount;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001634 return OK;
1635}
1636
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001637/*
1638 * Execute iptr->isn_arg.string as an Ex command.
1639 */
1640 static int
1641exec_command(isn_T *iptr)
1642{
1643 source_cookie_T cookie;
1644
1645 SOURCING_LNUM = iptr->isn_lnum;
1646 // Pass getsourceline to get an error for a missing ":end"
1647 // command.
1648 CLEAR_FIELD(cookie);
1649 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1650 if (do_cmdline(iptr->isn_arg.string,
1651 getsourceline, &cookie,
1652 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED) == FAIL
1653 || did_emsg)
1654 return FAIL;
1655 return OK;
1656}
1657
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001658// used for v_instr of typval of VAR_INSTR
1659struct instr_S {
1660 ectx_T *instr_ectx;
1661 isn_T *instr_instr;
1662};
1663
Bram Moolenaar4c137212021-04-19 16:48:48 +02001664// used for substitute_instr
1665typedef struct subs_expr_S {
1666 ectx_T *subs_ectx;
1667 isn_T *subs_instr;
1668 int subs_status;
1669} subs_expr_T;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001670
1671// Get pointer to item in the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001672#define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001673
1674// Get pointer to item at the bottom of the stack, -1 is the bottom.
1675#undef STACK_TV_BOT
Bram Moolenaar4c137212021-04-19 16:48:48 +02001676#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 +01001677
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001678// Get pointer to a local variable on the stack. Negative for arguments.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001679#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 +01001680
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001681// Set when calling do_debug().
1682static ectx_T *debug_context = NULL;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001683static int debug_var_count;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001684
1685/*
1686 * When debugging lookup "name" and return the typeval.
1687 * When not found return NULL.
1688 */
1689 typval_T *
1690lookup_debug_var(char_u *name)
1691{
1692 int idx;
1693 dfunc_T *dfunc;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001694 ufunc_T *ufunc;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001695 ectx_T *ectx = debug_context;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001696 int varargs_off;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001697
1698 if (ectx == NULL)
1699 return NULL;
1700 dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
1701
1702 // Go through the local variable names, from last to first.
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001703 for (idx = debug_var_count - 1; idx >= 0; --idx)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001704 {
Bram Moolenaare406ff82022-03-10 20:47:43 +00001705 char_u *varname = ((char_u **)dfunc->df_var_names.ga_data)[idx];
1706
1707 // the variable name may be NULL when not available in this block
1708 if (varname != NULL && STRCMP(varname, name) == 0)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001709 return STACK_TV_VAR(idx);
1710 }
1711
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001712 // Go through argument names.
1713 ufunc = dfunc->df_ufunc;
1714 varargs_off = ufunc->uf_va_name == NULL ? 0 : 1;
1715 for (idx = 0; idx < ufunc->uf_args.ga_len; ++idx)
1716 if (STRCMP(((char_u **)(ufunc->uf_args.ga_data))[idx], name) == 0)
1717 return STACK_TV(ectx->ec_frame_idx - ufunc->uf_args.ga_len
1718 - varargs_off + idx);
1719 if (ufunc->uf_va_name != NULL && STRCMP(ufunc->uf_va_name, name) == 0)
1720 return STACK_TV(ectx->ec_frame_idx - 1);
1721
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001722 return NULL;
1723}
1724
Bram Moolenaar26a44842021-09-02 18:49:06 +02001725/*
1726 * Return TRUE if there might be a breakpoint in "ufunc", which is when a
1727 * breakpoint was set in that function or when there is any expression.
1728 */
1729 int
1730may_break_in_function(ufunc_T *ufunc)
1731{
1732 return ufunc->uf_has_breakpoint || debug_has_expr_breakpoint();
1733}
1734
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001735 static void
1736handle_debug(isn_T *iptr, ectx_T *ectx)
1737{
1738 char_u *line;
1739 ufunc_T *ufunc = (((dfunc_T *)def_functions.ga_data)
1740 + ectx->ec_dfunc_idx)->df_ufunc;
1741 isn_T *ni;
1742 int end_lnum = iptr->isn_lnum;
1743 garray_T ga;
1744 int lnum;
1745
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02001746 if (ex_nesting_level > debug_break_level)
1747 {
1748 linenr_T breakpoint;
1749
Bram Moolenaar26a44842021-09-02 18:49:06 +02001750 if (!may_break_in_function(ufunc))
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02001751 return;
1752
1753 // check for the next breakpoint if needed
1754 breakpoint = dbg_find_breakpoint(FALSE, ufunc->uf_name,
Bram Moolenaar8cec9272021-06-23 20:20:53 +02001755 iptr->isn_arg.debug.dbg_break_lnum);
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02001756 if (breakpoint <= 0 || breakpoint > iptr->isn_lnum)
1757 return;
1758 }
1759
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001760 SOURCING_LNUM = iptr->isn_lnum;
1761 debug_context = ectx;
Bram Moolenaar8cec9272021-06-23 20:20:53 +02001762 debug_var_count = iptr->isn_arg.debug.dbg_var_names_len;
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001763
1764 for (ni = iptr + 1; ni->isn_type != ISN_FINISH; ++ni)
1765 if (ni->isn_type == ISN_DEBUG
1766 || ni->isn_type == ISN_RETURN
1767 || ni->isn_type == ISN_RETURN_VOID)
1768 {
Bram Moolenaar112bed02021-11-23 22:16:34 +00001769 end_lnum = ni->isn_lnum + (ni->isn_type == ISN_DEBUG ? 0 : 1);
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001770 break;
1771 }
1772
1773 if (end_lnum > iptr->isn_lnum)
1774 {
1775 ga_init2(&ga, sizeof(char_u *), 10);
Bram Moolenaar310091d2021-12-23 21:14:37 +00001776 for (lnum = iptr->isn_lnum; lnum < end_lnum
1777 && lnum <= ufunc->uf_lines.ga_len; ++lnum)
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001778 {
Bram Moolenaar303215d2021-07-07 20:10:43 +02001779 char_u *p = ((char_u **)ufunc->uf_lines.ga_data)[lnum - 1];
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001780
Bram Moolenaar303215d2021-07-07 20:10:43 +02001781 if (p == NULL)
1782 continue; // left over from continuation line
1783 p = skipwhite(p);
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001784 if (*p == '#')
1785 break;
Bram Moolenaar35578162021-08-02 19:10:38 +02001786 if (GA_GROW_OK(&ga, 1))
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001787 ((char_u **)(ga.ga_data))[ga.ga_len++] = p;
1788 if (STRNCMP(p, "def ", 4) == 0)
1789 break;
1790 }
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001791 line = ga_concat_strings(&ga, " ");
1792 vim_free(ga.ga_data);
1793 }
1794 else
1795 line = ((char_u **)ufunc->uf_lines.ga_data)[iptr->isn_lnum - 1];
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001796
Dominique Pelle6e969552021-06-17 13:53:41 +02001797 do_debug(line == NULL ? (char_u *)"[empty]" : line);
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001798 debug_context = NULL;
1799
1800 if (end_lnum > iptr->isn_lnum)
1801 vim_free(line);
1802}
1803
Bram Moolenaar4c137212021-04-19 16:48:48 +02001804/*
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00001805 * Store a value in a list, dict or blob variable.
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001806 * Returns OK, FAIL or NOTDONE (uncatchable error).
1807 */
1808 static int
1809execute_storeindex(isn_T *iptr, ectx_T *ectx)
1810{
1811 vartype_T dest_type = iptr->isn_arg.vartype;
1812 typval_T *tv;
1813 typval_T *tv_idx = STACK_TV_BOT(-2);
1814 typval_T *tv_dest = STACK_TV_BOT(-1);
1815 int status = OK;
1816
1817 // Stack contains:
1818 // -3 value to be stored
1819 // -2 index
1820 // -1 dict or list
1821 tv = STACK_TV_BOT(-3);
1822 SOURCING_LNUM = iptr->isn_lnum;
1823 if (dest_type == VAR_ANY)
1824 {
1825 dest_type = tv_dest->v_type;
1826 if (dest_type == VAR_DICT)
1827 status = do_2string(tv_idx, TRUE, FALSE);
1828 else if (dest_type == VAR_LIST && tv_idx->v_type != VAR_NUMBER)
1829 {
1830 emsg(_(e_number_expected));
1831 status = FAIL;
1832 }
1833 }
Bram Moolenaare08be092022-02-17 13:08:26 +00001834
1835 if (status == OK)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001836 {
Bram Moolenaare08be092022-02-17 13:08:26 +00001837 if (dest_type == VAR_LIST)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001838 {
Bram Moolenaare08be092022-02-17 13:08:26 +00001839 long lidx = (long)tv_idx->vval.v_number;
1840 list_T *list = tv_dest->vval.v_list;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001841
Bram Moolenaare08be092022-02-17 13:08:26 +00001842 if (list == NULL)
1843 {
1844 emsg(_(e_list_not_set));
1845 return FAIL;
1846 }
1847 if (lidx < 0 && list->lv_len + lidx >= 0)
1848 // negative index is relative to the end
1849 lidx = list->lv_len + lidx;
1850 if (lidx < 0 || lidx > list->lv_len)
1851 {
1852 semsg(_(e_list_index_out_of_range_nr), lidx);
1853 return FAIL;
1854 }
1855 if (lidx < list->lv_len)
1856 {
1857 listitem_T *li = list_find(list, lidx);
1858
1859 if (error_if_locked(li->li_tv.v_lock,
Bram Moolenaar70c43d82022-01-26 21:01:15 +00001860 e_cannot_change_locked_list_item))
Bram Moolenaare08be092022-02-17 13:08:26 +00001861 return FAIL;
1862 // overwrite existing list item
1863 clear_tv(&li->li_tv);
1864 li->li_tv = *tv;
1865 }
1866 else
1867 {
1868 if (error_if_locked(list->lv_lock, e_cannot_change_locked_list))
1869 return FAIL;
1870 // append to list, only fails when out of memory
1871 if (list_append_tv(list, tv) == FAIL)
1872 return NOTDONE;
1873 clear_tv(tv);
1874 }
1875 }
1876 else if (dest_type == VAR_DICT)
1877 {
1878 char_u *key = tv_idx->vval.v_string;
1879 dict_T *dict = tv_dest->vval.v_dict;
1880 dictitem_T *di;
1881
1882 SOURCING_LNUM = iptr->isn_lnum;
1883 if (dict == NULL)
1884 {
1885 emsg(_(e_dictionary_not_set));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001886 return FAIL;
Bram Moolenaare08be092022-02-17 13:08:26 +00001887 }
1888 if (key == NULL)
1889 key = (char_u *)"";
1890 di = dict_find(dict, key, -1);
1891 if (di != NULL)
1892 {
1893 if (error_if_locked(di->di_tv.v_lock,
1894 e_cannot_change_dict_item))
1895 return FAIL;
1896 // overwrite existing value
1897 clear_tv(&di->di_tv);
1898 di->di_tv = *tv;
1899 }
1900 else
1901 {
1902 if (error_if_locked(dict->dv_lock, e_cannot_change_dict))
1903 return FAIL;
1904 // add to dict, only fails when out of memory
1905 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
1906 return NOTDONE;
1907 clear_tv(tv);
1908 }
1909 }
1910 else if (dest_type == VAR_BLOB)
1911 {
1912 long lidx = (long)tv_idx->vval.v_number;
1913 blob_T *blob = tv_dest->vval.v_blob;
1914 varnumber_T nr;
1915 int error = FALSE;
1916 int len;
1917
1918 if (blob == NULL)
1919 {
1920 emsg(_(e_blob_not_set));
1921 return FAIL;
1922 }
1923 len = blob_len(blob);
1924 if (lidx < 0 && len + lidx >= 0)
1925 // negative index is relative to the end
1926 lidx = len + lidx;
1927
1928 // Can add one byte at the end.
1929 if (lidx < 0 || lidx > len)
1930 {
1931 semsg(_(e_blob_index_out_of_range_nr), lidx);
1932 return FAIL;
1933 }
1934 if (value_check_lock(blob->bv_lock, (char_u *)"blob", FALSE))
1935 return FAIL;
1936 nr = tv_get_number_chk(tv, &error);
1937 if (error)
1938 return FAIL;
1939 blob_set_append(blob, lidx, nr);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001940 }
1941 else
1942 {
Bram Moolenaare08be092022-02-17 13:08:26 +00001943 status = FAIL;
1944 semsg(_(e_cannot_index_str), vartype_name(dest_type));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001945 }
1946 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001947
1948 clear_tv(tv_idx);
1949 clear_tv(tv_dest);
1950 ectx->ec_stack.ga_len -= 3;
1951 if (status == FAIL)
1952 {
1953 clear_tv(tv);
1954 return FAIL;
1955 }
1956 return OK;
1957}
1958
1959/*
Bram Moolenaarea5c8982022-02-17 14:42:02 +00001960 * Store a value in a list or blob range.
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001961 */
1962 static int
1963execute_storerange(isn_T *iptr, ectx_T *ectx)
1964{
1965 typval_T *tv;
1966 typval_T *tv_idx1 = STACK_TV_BOT(-3);
1967 typval_T *tv_idx2 = STACK_TV_BOT(-2);
1968 typval_T *tv_dest = STACK_TV_BOT(-1);
1969 int status = OK;
1970
1971 // Stack contains:
1972 // -4 value to be stored
1973 // -3 first index or "none"
1974 // -2 second index or "none"
1975 // -1 destination list or blob
1976 tv = STACK_TV_BOT(-4);
Bram Moolenaarea5c8982022-02-17 14:42:02 +00001977 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001978 if (tv_dest->v_type == VAR_LIST)
1979 {
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00001980 long n1;
1981 long n2;
1982 listitem_T *li1;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001983
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00001984 n1 = (long)tv_get_number_chk(tv_idx1, NULL);
1985 if (tv_idx2->v_type == VAR_SPECIAL
1986 && tv_idx2->vval.v_number == VVAL_NONE)
1987 n2 = list_len(tv_dest->vval.v_list) - 1;
1988 else
1989 n2 = (long)tv_get_number_chk(tv_idx2, NULL);
1990
1991 li1 = check_range_index_one(tv_dest->vval.v_list, &n1, FALSE);
1992 if (li1 == NULL)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001993 status = FAIL;
1994 else
1995 {
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00001996 status = check_range_index_two(tv_dest->vval.v_list,
1997 &n1, li1, &n2, FALSE);
1998 if (status != FAIL)
1999 status = list_assign_range(
2000 tv_dest->vval.v_list,
2001 tv->vval.v_list,
2002 n1,
2003 n2,
2004 tv_idx2->v_type == VAR_SPECIAL,
2005 (char_u *)"=",
2006 (char_u *)"[unknown]");
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002007 }
2008 }
2009 else if (tv_dest->v_type == VAR_BLOB)
2010 {
2011 varnumber_T n1;
2012 varnumber_T n2;
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002013 long bloblen;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002014
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002015 n1 = tv_get_number_chk(tv_idx1, NULL);
2016 if (tv_idx2->v_type == VAR_SPECIAL
2017 && tv_idx2->vval.v_number == VVAL_NONE)
2018 n2 = blob_len(tv_dest->vval.v_blob) - 1;
2019 else
2020 n2 = tv_get_number_chk(tv_idx2, NULL);
2021 bloblen = blob_len(tv_dest->vval.v_blob);
2022
2023 if (check_blob_index(bloblen, n1, FALSE) == FAIL
2024 || check_blob_range(bloblen, n1, n2, FALSE) == FAIL)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002025 status = FAIL;
2026 else
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002027 status = blob_set_range(tv_dest->vval.v_blob, n1, n2, tv);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002028 }
2029 else
2030 {
2031 status = FAIL;
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002032 emsg(_(e_list_or_blob_required));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002033 }
2034
2035 clear_tv(tv_idx1);
2036 clear_tv(tv_idx2);
2037 clear_tv(tv_dest);
2038 ectx->ec_stack.ga_len -= 4;
2039 clear_tv(tv);
2040
2041 return status;
2042}
2043
2044/*
2045 * Unlet item in list or dict variable.
2046 */
2047 static int
2048execute_unletindex(isn_T *iptr, ectx_T *ectx)
2049{
2050 typval_T *tv_idx = STACK_TV_BOT(-2);
2051 typval_T *tv_dest = STACK_TV_BOT(-1);
2052 int status = OK;
2053
2054 // Stack contains:
2055 // -2 index
2056 // -1 dict or list
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002057 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002058 if (tv_dest->v_type == VAR_DICT)
2059 {
2060 // unlet a dict item, index must be a string
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002061 if (tv_idx->v_type != VAR_STRING && tv_idx->v_type != VAR_NUMBER)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002062 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002063 semsg(_(e_expected_str_but_got_str),
2064 vartype_name(VAR_STRING),
2065 vartype_name(tv_idx->v_type));
2066 status = FAIL;
2067 }
2068 else
2069 {
2070 dict_T *d = tv_dest->vval.v_dict;
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002071 char_u *key;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002072 dictitem_T *di = NULL;
2073
2074 if (d != NULL && value_check_lock(
2075 d->dv_lock, NULL, FALSE))
2076 status = FAIL;
2077 else
2078 {
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002079 if (tv_idx->v_type == VAR_STRING)
2080 {
2081 key = tv_idx->vval.v_string;
2082 if (key == NULL)
2083 key = (char_u *)"";
2084 }
2085 else
2086 {
2087 key = tv_get_string(tv_idx);
2088 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002089 if (d != NULL)
2090 di = dict_find(d, key, (int)STRLEN(key));
2091 if (di == NULL)
2092 {
2093 // NULL dict is equivalent to empty dict
2094 semsg(_(e_key_not_present_in_dictionary),
2095 key);
2096 status = FAIL;
2097 }
2098 else if (var_check_fixed(di->di_flags,
2099 NULL, FALSE)
2100 || var_check_ro(di->di_flags,
2101 NULL, FALSE))
2102 status = FAIL;
2103 else
2104 dictitem_remove(d, di);
2105 }
2106 }
2107 }
2108 else if (tv_dest->v_type == VAR_LIST)
2109 {
2110 // unlet a List item, index must be a number
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002111 if (check_for_number(tv_idx) == FAIL)
2112 {
2113 status = FAIL;
2114 }
2115 else
2116 {
2117 list_T *l = tv_dest->vval.v_list;
2118 long n = (long)tv_idx->vval.v_number;
2119
2120 if (l != NULL && value_check_lock(
2121 l->lv_lock, NULL, FALSE))
2122 status = FAIL;
2123 else
2124 {
2125 listitem_T *li = list_find(l, n);
2126
2127 if (li == NULL)
2128 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002129 semsg(_(e_list_index_out_of_range_nr), n);
2130 status = FAIL;
2131 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002132 else
2133 listitem_remove(l, li);
2134 }
2135 }
2136 }
2137 else
2138 {
2139 status = FAIL;
2140 semsg(_(e_cannot_index_str),
2141 vartype_name(tv_dest->v_type));
2142 }
2143
2144 clear_tv(tv_idx);
2145 clear_tv(tv_dest);
2146 ectx->ec_stack.ga_len -= 2;
2147
2148 return status;
2149}
2150
2151/*
2152 * Unlet a range of items in a list variable.
2153 */
2154 static int
2155execute_unletrange(isn_T *iptr, ectx_T *ectx)
2156{
2157 // Stack contains:
2158 // -3 index1
2159 // -2 index2
2160 // -1 dict or list
2161 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2162 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2163 typval_T *tv_dest = STACK_TV_BOT(-1);
2164 int status = OK;
2165
2166 if (tv_dest->v_type == VAR_LIST)
2167 {
2168 // indexes must be a number
2169 SOURCING_LNUM = iptr->isn_lnum;
2170 if (check_for_number(tv_idx1) == FAIL
2171 || (tv_idx2->v_type != VAR_SPECIAL
2172 && check_for_number(tv_idx2) == FAIL))
2173 {
2174 status = FAIL;
2175 }
2176 else
2177 {
2178 list_T *l = tv_dest->vval.v_list;
2179 long n1 = (long)tv_idx1->vval.v_number;
2180 long n2 = tv_idx2->v_type == VAR_SPECIAL
2181 ? 0 : (long)tv_idx2->vval.v_number;
2182 listitem_T *li;
2183
2184 li = list_find_index(l, &n1);
2185 if (li == NULL)
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002186 {
2187 semsg(_(e_list_index_out_of_range_nr),
2188 (long)tv_idx1->vval.v_number);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002189 status = FAIL;
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002190 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002191 else
2192 {
2193 if (n1 < 0)
2194 n1 = list_idx_of_item(l, li);
2195 if (n2 < 0)
2196 {
2197 listitem_T *li2 = list_find(l, n2);
2198
2199 if (li2 == NULL)
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002200 {
2201 semsg(_(e_list_index_out_of_range_nr), n2);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002202 status = FAIL;
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002203 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002204 else
2205 n2 = list_idx_of_item(l, li2);
2206 }
2207 if (status != FAIL
2208 && tv_idx2->v_type != VAR_SPECIAL
2209 && n2 < n1)
2210 {
2211 semsg(_(e_list_index_out_of_range_nr), n2);
2212 status = FAIL;
2213 }
Bram Moolenaar6b8c7ba2022-03-20 17:46:06 +00002214 if (status != FAIL)
2215 list_unlet_range(l, li, n1,
2216 tv_idx2->v_type != VAR_SPECIAL, n2);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002217 }
2218 }
2219 }
2220 else
2221 {
2222 status = FAIL;
2223 SOURCING_LNUM = iptr->isn_lnum;
2224 semsg(_(e_cannot_index_str),
2225 vartype_name(tv_dest->v_type));
2226 }
2227
2228 clear_tv(tv_idx1);
2229 clear_tv(tv_idx2);
2230 clear_tv(tv_dest);
2231 ectx->ec_stack.ga_len -= 3;
2232
2233 return status;
2234}
2235
2236/*
2237 * Top of a for loop.
2238 */
2239 static int
2240execute_for(isn_T *iptr, ectx_T *ectx)
2241{
2242 typval_T *tv;
2243 typval_T *ltv = STACK_TV_BOT(-1);
2244 typval_T *idxtv =
2245 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
2246
2247 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
2248 return FAIL;
2249 if (ltv->v_type == VAR_LIST)
2250 {
2251 list_T *list = ltv->vval.v_list;
2252
2253 // push the next item from the list
2254 ++idxtv->vval.v_number;
2255 if (list == NULL
2256 || idxtv->vval.v_number >= list->lv_len)
2257 {
2258 // past the end of the list, jump to "endfor"
2259 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
2260 may_restore_cmdmod(&ectx->ec_funclocal);
2261 }
2262 else if (list->lv_first == &range_list_item)
2263 {
2264 // non-materialized range() list
2265 tv = STACK_TV_BOT(0);
2266 tv->v_type = VAR_NUMBER;
2267 tv->v_lock = 0;
2268 tv->vval.v_number = list_find_nr(
2269 list, idxtv->vval.v_number, NULL);
2270 ++ectx->ec_stack.ga_len;
2271 }
2272 else
2273 {
2274 listitem_T *li = list_find(list,
2275 idxtv->vval.v_number);
2276
2277 copy_tv(&li->li_tv, STACK_TV_BOT(0));
2278 ++ectx->ec_stack.ga_len;
2279 }
2280 }
2281 else if (ltv->v_type == VAR_STRING)
2282 {
2283 char_u *str = ltv->vval.v_string;
2284
2285 // The index is for the last byte of the previous
2286 // character.
2287 ++idxtv->vval.v_number;
2288 if (str == NULL || str[idxtv->vval.v_number] == NUL)
2289 {
2290 // past the end of the string, jump to "endfor"
2291 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
2292 may_restore_cmdmod(&ectx->ec_funclocal);
2293 }
2294 else
2295 {
2296 int clen = mb_ptr2len(str + idxtv->vval.v_number);
2297
2298 // Push the next character from the string.
2299 tv = STACK_TV_BOT(0);
2300 tv->v_type = VAR_STRING;
2301 tv->vval.v_string = vim_strnsave(
2302 str + idxtv->vval.v_number, clen);
2303 ++ectx->ec_stack.ga_len;
2304 idxtv->vval.v_number += clen - 1;
2305 }
2306 }
2307 else if (ltv->v_type == VAR_BLOB)
2308 {
2309 blob_T *blob = ltv->vval.v_blob;
2310
2311 // When we get here the first time make a copy of the
2312 // blob, so that the iteration still works when it is
2313 // changed.
2314 if (idxtv->vval.v_number == -1 && blob != NULL)
2315 {
2316 blob_copy(blob, ltv);
2317 blob_unref(blob);
2318 blob = ltv->vval.v_blob;
2319 }
2320
2321 // The index is for the previous byte.
2322 ++idxtv->vval.v_number;
2323 if (blob == NULL
2324 || idxtv->vval.v_number >= blob_len(blob))
2325 {
2326 // past the end of the blob, jump to "endfor"
2327 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
2328 may_restore_cmdmod(&ectx->ec_funclocal);
2329 }
2330 else
2331 {
2332 // Push the next byte from the blob.
2333 tv = STACK_TV_BOT(0);
2334 tv->v_type = VAR_NUMBER;
2335 tv->vval.v_number = blob_get(blob,
2336 idxtv->vval.v_number);
2337 ++ectx->ec_stack.ga_len;
2338 }
2339 }
2340 else
2341 {
2342 semsg(_(e_for_loop_on_str_not_supported),
2343 vartype_name(ltv->v_type));
2344 return FAIL;
2345 }
2346 return OK;
2347}
2348
2349/*
Bram Moolenaar06b77222022-01-25 15:51:56 +00002350 * Load instruction for w:/b:/g:/t: variable.
2351 * "isn_type" is used instead of "iptr->isn_type".
2352 */
2353 static int
2354load_namespace_var(ectx_T *ectx, isntype_T isn_type, isn_T *iptr)
2355{
2356 dictitem_T *di = NULL;
2357 hashtab_T *ht = NULL;
2358 char namespace;
2359
2360 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
2361 return NOTDONE;
2362 switch (isn_type)
2363 {
2364 case ISN_LOADG:
2365 ht = get_globvar_ht();
2366 namespace = 'g';
2367 break;
2368 case ISN_LOADB:
2369 ht = &curbuf->b_vars->dv_hashtab;
2370 namespace = 'b';
2371 break;
2372 case ISN_LOADW:
2373 ht = &curwin->w_vars->dv_hashtab;
2374 namespace = 'w';
2375 break;
2376 case ISN_LOADT:
2377 ht = &curtab->tp_vars->dv_hashtab;
2378 namespace = 't';
2379 break;
2380 default: // Cannot reach here
2381 return NOTDONE;
2382 }
2383 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
2384
Bram Moolenaar06b77222022-01-25 15:51:56 +00002385 if (di == NULL)
2386 {
Bram Moolenaarfe732552022-02-22 19:39:13 +00002387 if (isn_type == ISN_LOADG)
2388 {
2389 ufunc_T *ufunc = find_func(iptr->isn_arg.string, TRUE);
2390
2391 // g:Something could be a function
2392 if (ufunc != NULL)
2393 {
2394 typval_T *tv = STACK_TV_BOT(0);
2395
2396 ++ectx->ec_stack.ga_len;
2397 tv->v_type = VAR_FUNC;
2398 tv->vval.v_string = alloc(STRLEN(iptr->isn_arg.string) + 3);
2399 if (tv->vval.v_string == NULL)
2400 return FAIL;
2401 STRCPY(tv->vval.v_string, "g:");
2402 STRCPY(tv->vval.v_string + 2, iptr->isn_arg.string);
2403 return OK;
2404 }
2405 }
Bram Moolenaar06b77222022-01-25 15:51:56 +00002406 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarfe732552022-02-22 19:39:13 +00002407 if (vim_strchr(iptr->isn_arg.string, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar06b77222022-01-25 15:51:56 +00002408 // no check if the item exists in the script but
2409 // isn't exported, it is too complicated
Bram Moolenaarfe732552022-02-22 19:39:13 +00002410 semsg(_(e_item_not_found_in_script_str), iptr->isn_arg.string);
Bram Moolenaar06b77222022-01-25 15:51:56 +00002411 else
2412 semsg(_(e_undefined_variable_char_str),
Bram Moolenaarfe732552022-02-22 19:39:13 +00002413 namespace, iptr->isn_arg.string);
Bram Moolenaar06b77222022-01-25 15:51:56 +00002414 return FAIL;
2415 }
2416 else
2417 {
2418 copy_tv(&di->di_tv, STACK_TV_BOT(0));
2419 ++ectx->ec_stack.ga_len;
2420 }
2421 return OK;
2422}
2423
2424/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02002425 * Execute instructions in execution context "ectx".
2426 * Return OK or FAIL;
2427 */
2428 static int
2429exec_instructions(ectx_T *ectx)
2430{
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002431 int ret = FAIL;
2432 int save_trylevel_at_start = ectx->ec_trylevel_at_start;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02002433 int dict_stack_len_at_start = dict_stack.ga_len;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01002434
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02002435 // Start execution at the first instruction.
Bram Moolenaar4c137212021-04-19 16:48:48 +02002436 ectx->ec_iidx = 0;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01002437
Bram Moolenaarff652882021-05-16 15:24:49 +02002438 // Only catch exceptions in this instruction list.
2439 ectx->ec_trylevel_at_start = trylevel;
2440
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002441 for (;;)
2442 {
Bram Moolenaara7490192021-07-22 12:26:14 +02002443 static int breakcheck_count = 0; // using "static" makes it faster
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002444 isn_T *iptr;
Bram Moolenaara7490192021-07-22 12:26:14 +02002445 typval_T *tv;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002446
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002447 if (unlikely(++breakcheck_count >= 100))
Bram Moolenaar270d0382020-05-15 21:42:53 +02002448 {
2449 line_breakcheck();
2450 breakcheck_count = 0;
2451 }
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002452 if (unlikely(got_int))
Bram Moolenaar20431c92020-03-20 18:39:46 +01002453 {
2454 // Turn CTRL-C into an exception.
2455 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +01002456 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002457 goto theend;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002458 did_throw = TRUE;
2459 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002460
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002461 if (unlikely(did_emsg && msg_list != NULL && *msg_list != NULL))
Bram Moolenaara26b9702020-04-18 19:53:28 +02002462 {
2463 // Turn an error message into an exception.
2464 did_emsg = FALSE;
2465 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002466 goto theend;
Bram Moolenaara26b9702020-04-18 19:53:28 +02002467 did_throw = TRUE;
2468 *msg_list = NULL;
2469 }
2470
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002471 if (unlikely(did_throw))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002472 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002473 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002474 trycmd_T *trycmd = NULL;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002475 int index = trystack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002476
2477 // An exception jumps to the first catch, finally, or returns from
2478 // the current function.
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002479 while (index > 0)
2480 {
2481 trycmd = ((trycmd_T *)trystack->ga_data) + index - 1;
Bram Moolenaar834193a2021-06-30 20:39:15 +02002482 if (!trycmd->tcd_in_catch || trycmd->tcd_finally_idx != 0)
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002483 break;
2484 // In the catch and finally block of this try we have to go up
2485 // one level.
2486 --index;
2487 trycmd = NULL;
2488 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02002489 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002490 {
Bram Moolenaar834193a2021-06-30 20:39:15 +02002491 if (trycmd->tcd_in_catch)
2492 {
2493 // exception inside ":catch", jump to ":finally" once
2494 ectx->ec_iidx = trycmd->tcd_finally_idx;
2495 trycmd->tcd_finally_idx = 0;
2496 }
2497 else
2498 // jump to first ":catch"
2499 ectx->ec_iidx = trycmd->tcd_catch_idx;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002500 trycmd->tcd_in_catch = TRUE;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002501 did_throw = FALSE; // don't come back here until :endtry
2502 trycmd->tcd_did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002503 }
2504 else
2505 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02002506 // Not inside try or need to return from current functions.
2507 // Push a dummy return value.
Bram Moolenaar35578162021-08-02 19:10:38 +02002508 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002509 goto theend;
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02002510 tv = STACK_TV_BOT(0);
2511 tv->v_type = VAR_NUMBER;
2512 tv->vval.v_number = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002513 ++ectx->ec_stack.ga_len;
2514 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002515 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02002516 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +01002517 need_rethrow = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002518 if (handle_closure_in_use(ectx, FALSE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002519 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002520 goto done;
2521 }
2522
Bram Moolenaar4c137212021-04-19 16:48:48 +02002523 if (func_return(ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002524 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002525 }
2526 continue;
2527 }
2528
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002529 /*
2530 * Big switch on the instruction. Most compilers will be turning this
2531 * into an efficient lookup table, since the "case" values are an enum
2532 * with sequential numbers. It may look ugly, but it should be the
2533 * most efficient way.
2534 */
Bram Moolenaar4c137212021-04-19 16:48:48 +02002535 iptr = &ectx->ec_instr[ectx->ec_iidx++];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002536 switch (iptr->isn_type)
2537 {
2538 // execute Ex command line
2539 case ISN_EXEC:
Bram Moolenaaraacc9662021-08-13 19:40:51 +02002540 if (exec_command(iptr) == FAIL)
2541 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002542 break;
2543
Bram Moolenaar20677332021-06-06 17:02:53 +02002544 // execute Ex command line split at NL characters.
2545 case ISN_EXEC_SPLIT:
2546 {
2547 source_cookie_T cookie;
Bram Moolenaar518df272021-06-06 17:34:13 +02002548 char_u *line;
Bram Moolenaar20677332021-06-06 17:02:53 +02002549
2550 SOURCING_LNUM = iptr->isn_lnum;
2551 CLEAR_FIELD(cookie);
2552 cookie.sourcing_lnum = iptr->isn_lnum - 1;
2553 cookie.nextline = iptr->isn_arg.string;
Bram Moolenaar518df272021-06-06 17:34:13 +02002554 line = get_split_sourceline(0, &cookie, 0, 0);
2555 if (do_cmdline(line,
Bram Moolenaar20677332021-06-06 17:02:53 +02002556 get_split_sourceline, &cookie,
2557 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED)
2558 == FAIL
2559 || did_emsg)
Bram Moolenaar518df272021-06-06 17:34:13 +02002560 {
2561 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02002562 goto on_error;
Bram Moolenaar518df272021-06-06 17:34:13 +02002563 }
2564 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02002565 }
2566 break;
2567
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002568 // execute Ex command line that is only a range
2569 case ISN_EXECRANGE:
2570 {
2571 exarg_T ea;
2572 char *error = NULL;
2573
2574 CLEAR_FIELD(ea);
2575 ea.cmdidx = CMD_SIZE;
2576 ea.addr_type = ADDR_LINES;
2577 ea.cmd = iptr->isn_arg.string;
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00002578 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002579 parse_cmd_address(&ea, &error, FALSE);
Bram Moolenaar01a4dcb2021-12-04 13:15:10 +00002580 if (ea.cmd == NULL)
2581 goto on_error;
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00002582 // error is always NULL when using ADDR_LINES
2583 error = ex_range_without_command(&ea);
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002584 if (error != NULL)
2585 {
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002586 emsg(error);
2587 goto on_error;
2588 }
2589 }
2590 break;
2591
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02002592 // Evaluate an expression with legacy syntax, push it onto the
2593 // stack.
2594 case ISN_LEGACY_EVAL:
2595 {
2596 char_u *arg = iptr->isn_arg.string;
2597 int res;
2598 int save_flags = cmdmod.cmod_flags;
2599
Bram Moolenaar35578162021-08-02 19:10:38 +02002600 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002601 goto theend;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02002602 tv = STACK_TV_BOT(0);
2603 init_tv(tv);
2604 cmdmod.cmod_flags |= CMOD_LEGACY;
2605 res = eval0(arg, tv, NULL, &EVALARG_EVALUATE);
2606 cmdmod.cmod_flags = save_flags;
2607 if (res == FAIL)
2608 goto on_error;
2609 ++ectx->ec_stack.ga_len;
2610 }
2611 break;
2612
Bram Moolenaarf18332f2021-05-07 17:55:55 +02002613 // push typeval VAR_INSTR with instructions to be executed
2614 case ISN_INSTR:
2615 {
Bram Moolenaar35578162021-08-02 19:10:38 +02002616 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002617 goto theend;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02002618 tv = STACK_TV_BOT(0);
2619 tv->vval.v_instr = ALLOC_ONE(instr_T);
2620 if (tv->vval.v_instr == NULL)
2621 goto on_error;
2622 ++ectx->ec_stack.ga_len;
2623
2624 tv->v_type = VAR_INSTR;
2625 tv->vval.v_instr->instr_ectx = ectx;
2626 tv->vval.v_instr->instr_instr = iptr->isn_arg.instr;
2627 }
2628 break;
2629
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002630 case ISN_SOURCE:
2631 {
2632 scriptitem_T *si = SCRIPT_ITEM(iptr->isn_arg.number);
2633
2634 if (si->sn_state == SN_STATE_NOT_LOADED)
2635 {
2636 SOURCING_LNUM = iptr->isn_lnum;
2637 if (do_source(si->sn_name, FALSE, DOSO_NONE, NULL)
2638 == FAIL)
2639 goto on_error;
2640 }
2641 }
2642 break;
2643
Bram Moolenaar4c137212021-04-19 16:48:48 +02002644 // execute :substitute with an expression
2645 case ISN_SUBSTITUTE:
2646 {
2647 subs_T *subs = &iptr->isn_arg.subs;
2648 source_cookie_T cookie;
2649 struct subs_expr_S *save_instr = substitute_instr;
2650 struct subs_expr_S subs_instr;
2651 int res;
2652
2653 subs_instr.subs_ectx = ectx;
2654 subs_instr.subs_instr = subs->subs_instr;
2655 subs_instr.subs_status = OK;
2656 substitute_instr = &subs_instr;
2657
2658 SOURCING_LNUM = iptr->isn_lnum;
2659 // This is very much like ISN_EXEC
2660 CLEAR_FIELD(cookie);
2661 cookie.sourcing_lnum = iptr->isn_lnum - 1;
2662 res = do_cmdline(subs->subs_cmd,
2663 getsourceline, &cookie,
2664 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
2665 substitute_instr = save_instr;
2666
2667 if (res == FAIL || did_emsg
2668 || subs_instr.subs_status == FAIL)
2669 goto on_error;
2670 }
2671 break;
2672
2673 case ISN_FINISH:
2674 goto done;
2675
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02002676 case ISN_REDIRSTART:
2677 // create a dummy entry for var_redir_str()
2678 if (alloc_redir_lval() == FAIL)
2679 goto on_error;
2680
2681 // The output is stored in growarray "redir_ga" until
2682 // redirection ends.
2683 init_redir_ga();
2684 redir_vname = 1;
2685 break;
2686
2687 case ISN_REDIREND:
2688 {
2689 char_u *res = get_clear_redir_ga();
2690
2691 // End redirection, put redirected text on the stack.
2692 clear_redir_lval();
2693 redir_vname = 0;
2694
Bram Moolenaar35578162021-08-02 19:10:38 +02002695 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02002696 {
2697 vim_free(res);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002698 goto theend;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02002699 }
2700 tv = STACK_TV_BOT(0);
2701 tv->v_type = VAR_STRING;
2702 tv->vval.v_string = res;
2703 ++ectx->ec_stack.ga_len;
2704 }
2705 break;
2706
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002707 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02002708#ifdef FEAT_QUICKFIX
Bram Moolenaar397a87a2022-03-20 21:14:15 +00002709 force_abort = TRUE;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002710 if (trigger_cexpr_autocmd(iptr->isn_arg.number) == FAIL)
2711 goto on_error;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00002712 force_abort = FALSE;
Bram Moolenaarb7c97812021-05-05 22:51:39 +02002713#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002714 break;
2715
2716 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02002717#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002718 {
2719 exarg_T ea;
2720 int res;
2721
2722 CLEAR_FIELD(ea);
2723 ea.cmdidx = iptr->isn_arg.cexpr.cexpr_ref->cer_cmdidx;
2724 ea.forceit = iptr->isn_arg.cexpr.cexpr_ref->cer_forceit;
2725 ea.cmdlinep = &iptr->isn_arg.cexpr.cexpr_ref->cer_cmdline;
2726 --ectx->ec_stack.ga_len;
2727 tv = STACK_TV_BOT(0);
2728 res = cexpr_core(&ea, tv);
2729 clear_tv(tv);
2730 if (res == FAIL)
2731 goto on_error;
2732 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02002733#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002734 break;
2735
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002736 // execute Ex command from pieces on the stack
2737 case ISN_EXECCONCAT:
2738 {
2739 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +02002740 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002741 int pass;
2742 int i;
2743 char_u *cmd = NULL;
2744 char_u *str;
2745
2746 for (pass = 1; pass <= 2; ++pass)
2747 {
2748 for (i = 0; i < count; ++i)
2749 {
2750 tv = STACK_TV_BOT(i - count);
2751 str = tv->vval.v_string;
2752 if (str != NULL && *str != NUL)
2753 {
2754 if (pass == 2)
2755 STRCPY(cmd + len, str);
2756 len += STRLEN(str);
2757 }
2758 if (pass == 2)
2759 clear_tv(tv);
2760 }
2761 if (pass == 1)
2762 {
2763 cmd = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002764 if (unlikely(cmd == NULL))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002765 goto theend;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002766 len = 0;
2767 }
2768 }
2769
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02002770 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002771 do_cmdline_cmd(cmd);
2772 vim_free(cmd);
2773 }
2774 break;
2775
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002776 // execute :echo {string} ...
2777 case ISN_ECHO:
2778 {
2779 int count = iptr->isn_arg.echo.echo_count;
2780 int atstart = TRUE;
2781 int needclr = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002782 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002783
2784 for (idx = 0; idx < count; ++idx)
2785 {
2786 tv = STACK_TV_BOT(idx - count);
2787 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
2788 &atstart, &needclr);
2789 clear_tv(tv);
2790 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01002791 if (needclr)
2792 msg_clr_eos();
Bram Moolenaar4c137212021-04-19 16:48:48 +02002793 ectx->ec_stack.ga_len -= count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002794 }
2795 break;
2796
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002797 // :execute {string} ...
2798 // :echomsg {string} ...
Bram Moolenaar7de62622021-08-07 15:05:47 +02002799 // :echoconsole {string} ...
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002800 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01002801 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002802 case ISN_ECHOMSG:
Bram Moolenaar7de62622021-08-07 15:05:47 +02002803 case ISN_ECHOCONSOLE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002804 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01002805 {
2806 int count = iptr->isn_arg.number;
2807 garray_T ga;
2808 char_u buf[NUMBUFLEN];
2809 char_u *p;
2810 int len;
2811 int failed = FALSE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002812 int idx;
Bram Moolenaarad39c092020-02-26 18:23:43 +01002813
2814 ga_init2(&ga, 1, 80);
2815 for (idx = 0; idx < count; ++idx)
2816 {
2817 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002818 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01002819 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002820 if (tv->v_type == VAR_CHANNEL
2821 || tv->v_type == VAR_JOB)
2822 {
2823 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar68db9962021-05-09 23:19:22 +02002824 semsg(_(e_using_invalid_value_as_string_str),
2825 vartype_name(tv->v_type));
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002826 break;
2827 }
2828 else
2829 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01002830 }
2831 else
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002832 p = tv_stringify(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01002833
2834 len = (int)STRLEN(p);
Bram Moolenaar35578162021-08-02 19:10:38 +02002835 if (GA_GROW_FAILS(&ga, len + 2))
Bram Moolenaarad39c092020-02-26 18:23:43 +01002836 failed = TRUE;
2837 else
2838 {
2839 if (ga.ga_len > 0)
2840 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
2841 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
2842 ga.ga_len += len;
2843 }
2844 clear_tv(tv);
2845 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02002846 ectx->ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002847 if (failed)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01002848 {
2849 ga_clear(&ga);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002850 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01002851 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01002852
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002853 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002854 {
2855 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02002856 {
2857 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002858 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01002859 if (did_emsg)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01002860 {
2861 ga_clear(&ga);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01002862 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01002863 }
Bram Moolenaar430deb12020-08-23 16:29:11 +02002864 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002865 else
2866 {
2867 msg_sb_eol();
2868 if (iptr->isn_type == ISN_ECHOMSG)
2869 {
2870 msg_attr(ga.ga_data, echo_attr);
2871 out_flush();
2872 }
Bram Moolenaar7de62622021-08-07 15:05:47 +02002873 else if (iptr->isn_type == ISN_ECHOCONSOLE)
2874 {
2875 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data),
2876 TRUE);
2877 ui_write((char_u *)"\r\n", 2, TRUE);
2878 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002879 else
2880 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002881 SOURCING_LNUM = iptr->isn_lnum;
2882 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002883 }
2884 }
2885 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01002886 ga_clear(&ga);
2887 }
2888 break;
2889
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002890 // load local variable or argument
2891 case ISN_LOAD:
Bram Moolenaar35578162021-08-02 19:10:38 +02002892 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002893 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002894 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002895 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002896 break;
2897
2898 // load v: variable
2899 case ISN_LOADV:
Bram Moolenaar35578162021-08-02 19:10:38 +02002900 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002901 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002902 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002903 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002904 break;
2905
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002906 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002907 case ISN_LOADSCRIPT:
2908 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01002909 scriptref_T *sref = iptr->isn_arg.script.scriptref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002910 svar_T *sv;
2911
Bram Moolenaar6db660b2021-08-01 14:08:54 +02002912 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01002913 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002914 goto theend;
Bram Moolenaar859cc212022-03-28 15:22:35 +01002915 allocate_if_null(sv);
Bram Moolenaar35578162021-08-02 19:10:38 +02002916 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002917 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002918 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002919 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002920 }
2921 break;
2922
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002923 // load s: variable in old script or autoload import
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002924 case ISN_LOADS:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002925 case ISN_LOADEXPORT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002926 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002927 int sid = iptr->isn_arg.loadstore.ls_sid;
2928 hashtab_T *ht = &SCRIPT_VARS(sid);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002929 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002930 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002931
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002932 if (di == NULL)
2933 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002934 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002935 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002936 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002937 }
2938 else
2939 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002940 if (iptr->isn_type == ISN_LOADEXPORT)
2941 {
2942 int idx = get_script_item_idx(sid, name, 0,
2943 NULL, NULL);
2944 svar_T *sv;
2945
2946 if (idx >= 0)
2947 {
2948 sv = ((svar_T *)SCRIPT_ITEM(sid)
2949 ->sn_var_vals.ga_data) + idx;
2950 if (!sv->sv_export)
2951 {
2952 SOURCING_LNUM = iptr->isn_lnum;
2953 semsg(_(e_item_not_exported_in_script_str),
2954 name);
2955 goto on_error;
2956 }
2957 }
2958 }
Bram Moolenaar35578162021-08-02 19:10:38 +02002959 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002960 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002961 copy_tv(&di->di_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002962 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002963 }
2964 }
2965 break;
2966
Bram Moolenaard3aac292020-04-19 14:32:17 +02002967 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002968 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02002969 case ISN_LOADB:
2970 case ISN_LOADW:
2971 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002972 {
Bram Moolenaar06b77222022-01-25 15:51:56 +00002973 int res = load_namespace_var(ectx, iptr->isn_type, iptr);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002974
Bram Moolenaar06b77222022-01-25 15:51:56 +00002975 if (res == NOTDONE)
Bram Moolenaarcbbc48f2022-01-18 12:58:28 +00002976 goto theend;
Bram Moolenaar06b77222022-01-25 15:51:56 +00002977 if (res == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002978 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002979 }
Bram Moolenaar06b77222022-01-25 15:51:56 +00002980
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002981 break;
2982
Bram Moolenaar03290b82020-12-19 16:30:44 +01002983 // load autoload variable
2984 case ISN_LOADAUTO:
2985 {
2986 char_u *name = iptr->isn_arg.string;
2987
Bram Moolenaar35578162021-08-02 19:10:38 +02002988 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002989 goto theend;
Bram Moolenaar03290b82020-12-19 16:30:44 +01002990 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002991 if (eval_variable(name, (int)STRLEN(name), 0,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002992 STACK_TV_BOT(0), NULL, EVAL_VAR_VERBOSE) == FAIL)
Bram Moolenaar03290b82020-12-19 16:30:44 +01002993 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002994 ++ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01002995 }
2996 break;
2997
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002998 // load g:/b:/w:/t: namespace
2999 case ISN_LOADGDICT:
3000 case ISN_LOADBDICT:
3001 case ISN_LOADWDICT:
3002 case ISN_LOADTDICT:
3003 {
3004 dict_T *d = NULL;
3005
3006 switch (iptr->isn_type)
3007 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02003008 case ISN_LOADGDICT: d = get_globvar_dict(); break;
3009 case ISN_LOADBDICT: d = curbuf->b_vars; break;
3010 case ISN_LOADWDICT: d = curwin->w_vars; break;
3011 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003012 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003013 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003014 }
Bram Moolenaar35578162021-08-02 19:10:38 +02003015 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003016 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003017 tv = STACK_TV_BOT(0);
3018 tv->v_type = VAR_DICT;
3019 tv->v_lock = 0;
3020 tv->vval.v_dict = d;
Bram Moolenaar1bd3cb22021-02-24 12:27:31 +01003021 ++d->dv_refcount;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003022 ++ectx->ec_stack.ga_len;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003023 }
3024 break;
3025
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003026 // load &option
3027 case ISN_LOADOPT:
3028 {
3029 typval_T optval;
3030 char_u *name = iptr->isn_arg.string;
3031
Bram Moolenaara8c17702020-04-01 21:17:24 +02003032 // This is not expected to fail, name is checked during
3033 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar35578162021-08-02 19:10:38 +02003034 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003035 goto theend;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003036 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003037 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003038 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003039 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003040 }
3041 break;
3042
3043 // load $ENV
3044 case ISN_LOADENV:
3045 {
3046 typval_T optval;
3047 char_u *name = iptr->isn_arg.string;
3048
Bram Moolenaar35578162021-08-02 19:10:38 +02003049 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003050 goto theend;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003051 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003052 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003053 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003054 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003055 }
3056 break;
3057
3058 // load @register
3059 case ISN_LOADREG:
Bram Moolenaar35578162021-08-02 19:10:38 +02003060 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003061 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003062 tv = STACK_TV_BOT(0);
3063 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003064 tv->v_lock = 0;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02003065 // This may result in NULL, which should be equivalent to an
3066 // empty string.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003067 tv->vval.v_string = get_reg_contents(
3068 iptr->isn_arg.number, GREG_EXPR_SRC);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003069 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003070 break;
3071
3072 // store local variable
3073 case ISN_STORE:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003074 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003075 tv = STACK_TV_VAR(iptr->isn_arg.number);
3076 clear_tv(tv);
3077 *tv = *STACK_TV_BOT(0);
3078 break;
3079
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003080 // store s: variable in old script or autoload import
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003081 case ISN_STORES:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003082 case ISN_STOREEXPORT:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003083 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003084 int sid = iptr->isn_arg.loadstore.ls_sid;
3085 hashtab_T *ht = &SCRIPT_VARS(sid);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003086 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003087 dictitem_T *di = find_var_in_ht(ht, 0,
3088 iptr->isn_type == ISN_STORES
3089 ? name + 2 : name, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003090
Bram Moolenaar4c137212021-04-19 16:48:48 +02003091 --ectx->ec_stack.ga_len;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003092 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003093 if (di == NULL)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003094 {
3095 if (iptr->isn_type == ISN_STOREEXPORT)
3096 {
3097 semsg(_(e_undefined_variable_str), name);
Bram Moolenaard1d26842022-03-31 10:13:47 +01003098 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003099 goto on_error;
3100 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003101 store_var(name, STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003102 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003103 else
3104 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003105 if (iptr->isn_type == ISN_STOREEXPORT)
3106 {
3107 int idx = get_script_item_idx(sid, name, 0,
3108 NULL, NULL);
3109
3110 if (idx >= 0)
3111 {
3112 svar_T *sv = ((svar_T *)SCRIPT_ITEM(sid)
3113 ->sn_var_vals.ga_data) + idx;
3114
3115 if (!sv->sv_export)
3116 {
3117 semsg(_(e_item_not_exported_in_script_str),
3118 name);
Bram Moolenaard1d26842022-03-31 10:13:47 +01003119 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003120 goto on_error;
3121 }
3122 }
3123 }
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02003124 if (var_check_permission(di, name) == FAIL)
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003125 {
3126 clear_tv(STACK_TV_BOT(0));
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02003127 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003128 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003129 clear_tv(&di->di_tv);
3130 di->di_tv = *STACK_TV_BOT(0);
3131 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003132 }
3133 break;
3134
3135 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003136 case ISN_STORESCRIPT:
3137 {
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003138 scriptref_T *sref = iptr->isn_arg.script.scriptref;
3139 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003140
Bram Moolenaar6db660b2021-08-01 14:08:54 +02003141 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003142 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003143 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003144 --ectx->ec_stack.ga_len;
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003145
3146 // "const" and "final" are checked at compile time, locking
3147 // the value needs to be checked here.
3148 SOURCING_LNUM = iptr->isn_lnum;
3149 if (value_check_lock(sv->sv_tv->v_lock, sv->sv_name, FALSE))
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003150 {
3151 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003152 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003153 }
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003154
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003155 clear_tv(sv->sv_tv);
3156 *sv->sv_tv = *STACK_TV_BOT(0);
3157 }
3158 break;
3159
3160 // store option
3161 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003162 case ISN_STOREFUNCOPT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003163 {
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003164 char_u *opt_name = iptr->isn_arg.storeopt.so_name;
3165 int opt_flags = iptr->isn_arg.storeopt.so_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003166 long n = 0;
3167 char_u *s = NULL;
3168 char *msg;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003169 char_u numbuf[NUMBUFLEN];
3170 char_u *tofree = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003171
Bram Moolenaar4c137212021-04-19 16:48:48 +02003172 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003173 tv = STACK_TV_BOT(0);
3174 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01003175 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003176 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01003177 if (s == NULL)
3178 s = (char_u *)"";
3179 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003180 else if (iptr->isn_type == ISN_STOREFUNCOPT)
3181 {
3182 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003183 // If the option can be set to a function reference or
3184 // a lambda and the passed value is a function
3185 // reference, then convert it to the name (string) of
3186 // the function reference.
3187 s = tv2string(tv, &tofree, numbuf, 0);
3188 if (s == NULL || *s == NUL)
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003189 {
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003190 // cannot happen?
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003191 clear_tv(tv);
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003192 vim_free(tofree);
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003193 goto on_error;
3194 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003195 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003196 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003197 // must be VAR_NUMBER, CHECKTYPE makes sure
3198 n = tv->vval.v_number;
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003199 msg = set_option_value(opt_name, n, s, opt_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02003200 clear_tv(tv);
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003201 vim_free(tofree);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003202 if (msg != NULL)
3203 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003204 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003205 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02003206 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003207 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003208 }
3209 break;
3210
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003211 // store $ENV
3212 case ISN_STOREENV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003213 --ectx->ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003214 tv = STACK_TV_BOT(0);
3215 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
3216 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003217 break;
3218
3219 // store @r
3220 case ISN_STOREREG:
3221 {
3222 int reg = iptr->isn_arg.number;
3223
Bram Moolenaar4c137212021-04-19 16:48:48 +02003224 --ectx->ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01003225 tv = STACK_TV_BOT(0);
Bram Moolenaar74f4a962021-06-17 21:03:07 +02003226 write_reg_contents(reg, tv_get_string(tv), -1, FALSE);
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01003227 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003228 }
3229 break;
3230
3231 // store v: variable
3232 case ISN_STOREV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003233 --ectx->ec_stack.ga_len;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003234 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
3235 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003236 // should not happen, type is checked when compiling
3237 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003238 break;
3239
Bram Moolenaard3aac292020-04-19 14:32:17 +02003240 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003241 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02003242 case ISN_STOREB:
3243 case ISN_STOREW:
3244 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003245 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003246 dictitem_T *di;
3247 hashtab_T *ht;
3248 char_u *name = iptr->isn_arg.string + 2;
3249
Bram Moolenaard3aac292020-04-19 14:32:17 +02003250 switch (iptr->isn_type)
3251 {
3252 case ISN_STOREG:
3253 ht = get_globvar_ht();
3254 break;
3255 case ISN_STOREB:
3256 ht = &curbuf->b_vars->dv_hashtab;
3257 break;
3258 case ISN_STOREW:
3259 ht = &curwin->w_vars->dv_hashtab;
3260 break;
3261 case ISN_STORET:
3262 ht = &curtab->tp_vars->dv_hashtab;
3263 break;
3264 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003265 goto theend;
Bram Moolenaard3aac292020-04-19 14:32:17 +02003266 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003267
Bram Moolenaar4c137212021-04-19 16:48:48 +02003268 --ectx->ec_stack.ga_len;
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003269 di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003270 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003271 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003272 else
3273 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003274 SOURCING_LNUM = iptr->isn_lnum;
3275 if (var_check_permission(di, name) == FAIL)
3276 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003277 clear_tv(&di->di_tv);
3278 di->di_tv = *STACK_TV_BOT(0);
3279 }
3280 }
3281 break;
3282
Bram Moolenaar03290b82020-12-19 16:30:44 +01003283 // store an autoload variable
3284 case ISN_STOREAUTO:
3285 SOURCING_LNUM = iptr->isn_lnum;
3286 set_var(iptr->isn_arg.string, STACK_TV_BOT(-1), TRUE);
3287 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003288 --ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003289 break;
3290
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003291 // store number in local variable
3292 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01003293 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003294 clear_tv(tv);
3295 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01003296 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003297 break;
3298
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01003299 // store value in list or dict variable
3300 case ISN_STOREINDEX:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003301 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003302 int res = execute_storeindex(iptr, ectx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003303
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003304 if (res == FAIL)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02003305 goto on_error;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003306 if (res == NOTDONE)
3307 goto theend;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003308 }
3309 break;
3310
Bram Moolenaarea5c8982022-02-17 14:42:02 +00003311 // store value in list or blob range
Bram Moolenaar68452172021-04-12 21:21:02 +02003312 case ISN_STORERANGE:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003313 if (execute_storerange(iptr, ectx) == FAIL)
3314 goto on_error;
Bram Moolenaar68452172021-04-12 21:21:02 +02003315 break;
3316
Bram Moolenaar0186e582021-01-10 18:33:11 +01003317 // load or store variable or argument from outer scope
3318 case ISN_LOADOUTER:
3319 case ISN_STOREOUTER:
3320 {
3321 int depth = iptr->isn_arg.outer.outer_depth;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02003322 outer_T *outer = ectx->ec_outer_ref == NULL ? NULL
3323 : ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003324
3325 while (depth > 1 && outer != NULL)
3326 {
3327 outer = outer->out_up;
3328 --depth;
3329 }
3330 if (outer == NULL)
3331 {
3332 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar69c76172021-12-02 16:38:52 +00003333 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx
3334 || ectx->ec_outer_ref == NULL)
3335 // Possibly :def function called from legacy
3336 // context.
3337 emsg(_(e_closure_called_from_invalid_context));
3338 else
3339 iemsg("LOADOUTER depth more than scope levels");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003340 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003341 }
3342 tv = ((typval_T *)outer->out_stack->ga_data)
3343 + outer->out_frame_idx + STACK_FRAME_SIZE
3344 + iptr->isn_arg.outer.outer_idx;
3345 if (iptr->isn_type == ISN_LOADOUTER)
3346 {
Bram Moolenaar35578162021-08-02 19:10:38 +02003347 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003348 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003349 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003350 ++ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003351 }
3352 else
3353 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003354 --ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003355 clear_tv(tv);
3356 *tv = *STACK_TV_BOT(0);
3357 }
3358 }
3359 break;
3360
Bram Moolenaar752fc692021-01-04 21:57:11 +01003361 // unlet item in list or dict variable
3362 case ISN_UNLETINDEX:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003363 if (execute_unletindex(iptr, ectx) == FAIL)
3364 goto on_error;
Bram Moolenaar752fc692021-01-04 21:57:11 +01003365 break;
3366
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01003367 // unlet range of items in list variable
3368 case ISN_UNLETRANGE:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003369 if (execute_unletrange(iptr, ectx) == FAIL)
3370 goto on_error;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01003371 break;
3372
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003373 // push constant
3374 case ISN_PUSHNR:
3375 case ISN_PUSHBOOL:
3376 case ISN_PUSHSPEC:
3377 case ISN_PUSHF:
3378 case ISN_PUSHS:
3379 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003380 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003381 case ISN_PUSHCHANNEL:
3382 case ISN_PUSHJOB:
Bram Moolenaar35578162021-08-02 19:10:38 +02003383 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003384 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003385 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003386 tv->v_lock = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003387 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003388 switch (iptr->isn_type)
3389 {
3390 case ISN_PUSHNR:
3391 tv->v_type = VAR_NUMBER;
3392 tv->vval.v_number = iptr->isn_arg.number;
3393 break;
3394 case ISN_PUSHBOOL:
3395 tv->v_type = VAR_BOOL;
3396 tv->vval.v_number = iptr->isn_arg.number;
3397 break;
3398 case ISN_PUSHSPEC:
3399 tv->v_type = VAR_SPECIAL;
3400 tv->vval.v_number = iptr->isn_arg.number;
3401 break;
3402#ifdef FEAT_FLOAT
3403 case ISN_PUSHF:
3404 tv->v_type = VAR_FLOAT;
3405 tv->vval.v_float = iptr->isn_arg.fnumber;
3406 break;
3407#endif
3408 case ISN_PUSHBLOB:
3409 blob_copy(iptr->isn_arg.blob, tv);
3410 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003411 case ISN_PUSHFUNC:
3412 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01003413 if (iptr->isn_arg.string == NULL)
3414 tv->vval.v_string = NULL;
3415 else
3416 tv->vval.v_string =
3417 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003418 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003419 case ISN_PUSHCHANNEL:
3420#ifdef FEAT_JOB_CHANNEL
3421 tv->v_type = VAR_CHANNEL;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003422 tv->vval.v_channel = NULL;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003423#endif
3424 break;
3425 case ISN_PUSHJOB:
3426#ifdef FEAT_JOB_CHANNEL
3427 tv->v_type = VAR_JOB;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003428 tv->vval.v_job = NULL;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003429#endif
3430 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003431 default:
3432 tv->v_type = VAR_STRING;
Bram Moolenaarf8691002022-03-10 12:20:53 +00003433 tv->vval.v_string = iptr->isn_arg.string == NULL
3434 ? NULL : vim_strsave(iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003435 }
3436 break;
3437
Bram Moolenaar06b77222022-01-25 15:51:56 +00003438 case ISN_AUTOLOAD:
3439 {
3440 char_u *name = iptr->isn_arg.string;
3441
3442 (void)script_autoload(name, FALSE);
3443 if (find_func(name, TRUE))
3444 {
3445 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
3446 goto theend;
3447 tv = STACK_TV_BOT(0);
3448 tv->v_lock = 0;
3449 ++ectx->ec_stack.ga_len;
3450 tv->v_type = VAR_FUNC;
3451 tv->vval.v_string = vim_strsave(name);
3452 }
3453 else
3454 {
3455 int res = load_namespace_var(ectx, ISN_LOADG, iptr);
3456
3457 if (res == NOTDONE)
3458 goto theend;
3459 if (res == FAIL)
3460 goto on_error;
3461 }
3462 }
3463 break;
3464
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003465 case ISN_UNLET:
3466 if (do_unlet(iptr->isn_arg.unlet.ul_name,
3467 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003468 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003469 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02003470 case ISN_UNLETENV:
3471 vim_unsetenv(iptr->isn_arg.unlet.ul_name);
3472 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003473
Bram Moolenaaraacc9662021-08-13 19:40:51 +02003474 case ISN_LOCKUNLOCK:
3475 {
3476 typval_T *lval_root_save = lval_root;
3477 int res;
3478
3479 // Stack has the local variable, argument the whole :lock
3480 // or :unlock command, like ISN_EXEC.
3481 --ectx->ec_stack.ga_len;
3482 lval_root = STACK_TV_BOT(0);
3483 res = exec_command(iptr);
3484 clear_tv(lval_root);
3485 lval_root = lval_root_save;
3486 if (res == FAIL)
3487 goto on_error;
3488 }
3489 break;
3490
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02003491 case ISN_LOCKCONST:
3492 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
3493 break;
3494
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003495 // create a list from items on the stack; uses a single allocation
3496 // for the list header and the items
3497 case ISN_NEWLIST:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003498 if (exe_newlist(iptr->isn_arg.number, ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003499 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003500 break;
3501
3502 // create a dict from items on the stack
3503 case ISN_NEWDICT:
3504 {
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01003505 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003506
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01003507 SOURCING_LNUM = iptr->isn_lnum;
3508 res = exe_newdict(iptr->isn_arg.number, ectx);
3509 if (res == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003510 goto theend;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01003511 if (res == MAYBE)
3512 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003513 }
3514 break;
3515
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003516 // create a partial with NULL value
3517 case ISN_NEWPARTIAL:
3518 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
3519 goto theend;
3520 ++ectx->ec_stack.ga_len;
3521 tv = STACK_TV_BOT(-1);
3522 tv->v_type = VAR_PARTIAL;
3523 tv->v_lock = 0;
3524 tv->vval.v_partial = NULL;
3525 break;
3526
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003527 // call a :def function
3528 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02003529 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003530 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
3531 NULL,
3532 iptr->isn_arg.dfunc.cdf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003533 ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003534 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003535 break;
3536
3537 // call a builtin function
3538 case ISN_BCALL:
3539 SOURCING_LNUM = iptr->isn_lnum;
3540 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
3541 iptr->isn_arg.bfunc.cbf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003542 ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02003543 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003544 break;
3545
3546 // call a funcref or partial
3547 case ISN_PCALL:
3548 {
3549 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
3550 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003551 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003552
3553 SOURCING_LNUM = iptr->isn_lnum;
3554 if (pfunc->cpf_top)
3555 {
3556 // funcref is above the arguments
3557 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
3558 }
3559 else
3560 {
3561 // Get the funcref from the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02003562 --ectx->ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003563 partial_tv = *STACK_TV_BOT(0);
3564 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003565 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003566 r = call_partial(tv, pfunc->cpf_argcount, ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003567 if (tv == &partial_tv)
3568 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003569 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02003570 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003571 }
3572 break;
3573
Bram Moolenaarbd5da372020-03-31 23:13:10 +02003574 case ISN_PCALL_END:
3575 // PCALL finished, arguments have been consumed and replaced by
3576 // the return value. Now clear the funcref from the stack,
3577 // and move the return value in its place.
Bram Moolenaar4c137212021-04-19 16:48:48 +02003578 --ectx->ec_stack.ga_len;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02003579 clear_tv(STACK_TV_BOT(-1));
3580 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
3581 break;
3582
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003583 // call a user defined function or funcref/partial
3584 case ISN_UCALL:
3585 {
3586 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
3587
3588 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003589 if (call_eval_func(cufunc->cuf_name, cufunc->cuf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003590 ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02003591 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003592 }
3593 break;
3594
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02003595 // return from a :def function call without a value
3596 case ISN_RETURN_VOID:
Bram Moolenaar35578162021-08-02 19:10:38 +02003597 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003598 goto theend;
Bram Moolenaar299f3032021-01-08 20:53:09 +01003599 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003600 ++ectx->ec_stack.ga_len;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02003601 tv->v_type = VAR_VOID;
Bram Moolenaar299f3032021-01-08 20:53:09 +01003602 tv->vval.v_number = 0;
3603 tv->v_lock = 0;
3604 // FALLTHROUGH
3605
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02003606 // return from a :def function call with what is on the stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003607 case ISN_RETURN:
3608 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003609 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003610 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01003611
3612 if (trystack->ga_len > 0)
3613 trycmd = ((trycmd_T *)trystack->ga_data)
3614 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003615 if (trycmd != NULL
Bram Moolenaar4c137212021-04-19 16:48:48 +02003616 && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003617 {
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01003618 // jump to ":finally" or ":endtry"
3619 if (trycmd->tcd_finally_idx != 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02003620 ectx->ec_iidx = trycmd->tcd_finally_idx;
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01003621 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02003622 ectx->ec_iidx = trycmd->tcd_endtry_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003623 trycmd->tcd_return = TRUE;
3624 }
3625 else
Bram Moolenaard032f342020-07-18 18:13:02 +02003626 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003627 }
3628 break;
3629
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02003630 // push a partial, a reference to a compiled function
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003631 case ISN_FUNCREF:
3632 {
Bram Moolenaarf112f302020-12-20 17:47:52 +01003633 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar38453522021-11-28 22:00:12 +00003634 ufunc_T *ufunc;
3635 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003636
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003637 if (pt == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003638 goto theend;
Bram Moolenaar35578162021-08-02 19:10:38 +02003639 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003640 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003641 vim_free(pt);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003642 goto theend;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003643 }
Bram Moolenaar38453522021-11-28 22:00:12 +00003644 if (funcref->fr_func_name == NULL)
3645 {
3646 dfunc_T *pt_dfunc = ((dfunc_T *)def_functions.ga_data)
3647 + funcref->fr_dfunc_idx;
3648
3649 ufunc = pt_dfunc->df_ufunc;
3650 }
3651 else
3652 {
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003653 ufunc = find_func(funcref->fr_func_name, FALSE);
Bram Moolenaar38453522021-11-28 22:00:12 +00003654 }
Bram Moolenaar56acd1f2022-02-18 13:24:52 +00003655 if (ufunc == NULL)
3656 {
3657 SOURCING_LNUM = iptr->isn_lnum;
3658 iemsg("ufunc unexpectedly NULL for FUNCREF");
3659 goto theend;
3660 }
Bram Moolenaar38453522021-11-28 22:00:12 +00003661 if (fill_partial_and_closure(pt, ufunc, ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003662 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003663 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003664 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003665 tv->vval.v_partial = pt;
3666 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003667 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003668 }
3669 break;
3670
Bram Moolenaar38ddf332020-07-31 22:05:04 +02003671 // Create a global function from a lambda.
3672 case ISN_NEWFUNC:
3673 {
3674 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
3675
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01003676 if (copy_func(newfunc->nf_lambda, newfunc->nf_global,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003677 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003678 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02003679 }
3680 break;
3681
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01003682 // List functions
3683 case ISN_DEF:
3684 if (iptr->isn_arg.string == NULL)
3685 list_functions(NULL);
3686 else
3687 {
Bram Moolenaar14336722022-01-08 16:02:59 +00003688 exarg_T ea;
3689 garray_T lines_to_free;
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01003690
3691 CLEAR_FIELD(ea);
3692 ea.cmd = ea.arg = iptr->isn_arg.string;
Bram Moolenaar14336722022-01-08 16:02:59 +00003693 ga_init2(&lines_to_free, sizeof(char_u *), 50);
3694 define_function(&ea, NULL, &lines_to_free);
3695 ga_clear_strings(&lines_to_free);
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01003696 }
3697 break;
3698
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003699 // jump if a condition is met
3700 case ISN_JUMP:
3701 {
3702 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003703 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003704 int jump = TRUE;
3705
3706 if (when != JUMP_ALWAYS)
3707 {
3708 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003709 if (when == JUMP_IF_COND_FALSE
Bram Moolenaar13106602020-10-04 16:06:05 +02003710 || when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003711 || when == JUMP_IF_COND_TRUE)
3712 {
3713 SOURCING_LNUM = iptr->isn_lnum;
3714 jump = tv_get_bool_chk(tv, &error);
3715 if (error)
3716 goto on_error;
3717 }
3718 else
3719 jump = tv2bool(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003720 if (when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003721 || when == JUMP_AND_KEEP_IF_FALSE
3722 || when == JUMP_IF_COND_FALSE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003723 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01003724 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003725 {
3726 // drop the value from the stack
3727 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003728 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003729 }
3730 }
3731 if (jump)
Bram Moolenaar4c137212021-04-19 16:48:48 +02003732 ectx->ec_iidx = iptr->isn_arg.jump.jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003733 }
3734 break;
3735
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02003736 // Jump if an argument with a default value was already set and not
3737 // v:none.
3738 case ISN_JUMP_IF_ARG_SET:
3739 tv = STACK_TV_VAR(iptr->isn_arg.jumparg.jump_arg_off);
3740 if (tv->v_type != VAR_UNKNOWN
3741 && !(tv->v_type == VAR_SPECIAL
3742 && tv->vval.v_number == VVAL_NONE))
Bram Moolenaar4c137212021-04-19 16:48:48 +02003743 ectx->ec_iidx = iptr->isn_arg.jumparg.jump_where;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02003744 break;
3745
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003746 // top of a for loop
3747 case ISN_FOR:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003748 if (execute_for(iptr, ectx) == FAIL)
3749 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003750 break;
3751
3752 // start of ":try" block
3753 case ISN_TRY:
3754 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01003755 trycmd_T *trycmd = NULL;
3756
Bram Moolenaar35578162021-08-02 19:10:38 +02003757 if (GA_GROW_FAILS(&ectx->ec_trystack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003758 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003759 trycmd = ((trycmd_T *)ectx->ec_trystack.ga_data)
3760 + ectx->ec_trystack.ga_len;
3761 ++ectx->ec_trystack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003762 ++trylevel;
Bram Moolenaar8d4be892021-02-13 18:33:02 +01003763 CLEAR_POINTER(trycmd);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003764 trycmd->tcd_frame_idx = ectx->ec_frame_idx;
3765 trycmd->tcd_stack_len = ectx->ec_stack.ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01003766 trycmd->tcd_catch_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00003767 iptr->isn_arg.tryref.try_ref->try_catch;
Bram Moolenaara91a7132021-03-25 21:12:15 +01003768 trycmd->tcd_finally_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00003769 iptr->isn_arg.tryref.try_ref->try_finally;
Bram Moolenaara91a7132021-03-25 21:12:15 +01003770 trycmd->tcd_endtry_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00003771 iptr->isn_arg.tryref.try_ref->try_endtry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003772 }
3773 break;
3774
3775 case ISN_PUSHEXC:
3776 if (current_exception == NULL)
3777 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003778 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003779 iemsg("Evaluating catch while current_exception is NULL");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003780 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003781 }
Bram Moolenaar35578162021-08-02 19:10:38 +02003782 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003783 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003784 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003785 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003786 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003787 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003788 tv->vval.v_string = vim_strsave(
3789 (char_u *)current_exception->value);
3790 break;
3791
3792 case ISN_CATCH:
3793 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003794 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003795
Bram Moolenaar4c137212021-04-19 16:48:48 +02003796 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003797 if (trystack->ga_len > 0)
3798 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01003799 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003800 + trystack->ga_len - 1;
3801 trycmd->tcd_caught = TRUE;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003802 trycmd->tcd_did_throw = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003803 }
3804 did_emsg = got_int = did_throw = FALSE;
Bram Moolenaar1430cee2021-01-17 19:20:32 +01003805 force_abort = need_rethrow = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003806 catch_exception(current_exception);
3807 }
3808 break;
3809
Bram Moolenaarc150c092021-02-13 15:02:46 +01003810 case ISN_TRYCONT:
3811 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003812 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003813 trycont_T *trycont = &iptr->isn_arg.trycont;
3814 int i;
3815 trycmd_T *trycmd;
3816 int iidx = trycont->tct_where;
3817
3818 if (trystack->ga_len < trycont->tct_levels)
3819 {
3820 siemsg("TRYCONT: expected %d levels, found %d",
3821 trycont->tct_levels, trystack->ga_len);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003822 goto theend;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003823 }
3824 // Make :endtry jump to any outer try block and the last
3825 // :endtry inside the loop to the loop start.
3826 for (i = trycont->tct_levels; i > 0; --i)
3827 {
3828 trycmd = ((trycmd_T *)trystack->ga_data)
3829 + trystack->ga_len - i;
Bram Moolenaar2e34c342021-03-14 12:13:33 +01003830 // Add one to tcd_cont to be able to jump to
3831 // instruction with index zero.
3832 trycmd->tcd_cont = iidx + 1;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003833 iidx = trycmd->tcd_finally_idx == 0
3834 ? trycmd->tcd_endtry_idx : trycmd->tcd_finally_idx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003835 }
3836 // jump to :finally or :endtry of current try statement
Bram Moolenaar4c137212021-04-19 16:48:48 +02003837 ectx->ec_iidx = iidx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003838 }
3839 break;
3840
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003841 case ISN_FINALLY:
3842 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003843 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003844 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
3845 + trystack->ga_len - 1;
3846
3847 // Reset the index to avoid a return statement jumps here
3848 // again.
3849 trycmd->tcd_finally_idx = 0;
3850 break;
3851 }
3852
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003853 // end of ":try" block
3854 case ISN_ENDTRY:
3855 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003856 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003857
3858 if (trystack->ga_len > 0)
3859 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003860 trycmd_T *trycmd;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003861
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003862 --trystack->ga_len;
3863 --trylevel;
3864 trycmd = ((trycmd_T *)trystack->ga_data)
3865 + trystack->ga_len;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003866 if (trycmd->tcd_did_throw)
3867 did_throw = TRUE;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01003868 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003869 {
3870 // discard the exception
3871 if (caught_stack == current_exception)
3872 caught_stack = caught_stack->caught;
3873 discard_current_exception();
3874 }
3875
3876 if (trycmd->tcd_return)
Bram Moolenaard032f342020-07-18 18:13:02 +02003877 goto func_return;
Bram Moolenaard9d77892021-02-12 21:32:47 +01003878
Bram Moolenaar4c137212021-04-19 16:48:48 +02003879 while (ectx->ec_stack.ga_len > trycmd->tcd_stack_len)
Bram Moolenaard9d77892021-02-12 21:32:47 +01003880 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003881 --ectx->ec_stack.ga_len;
Bram Moolenaard9d77892021-02-12 21:32:47 +01003882 clear_tv(STACK_TV_BOT(0));
3883 }
Bram Moolenaar8d4be892021-02-13 18:33:02 +01003884 if (trycmd->tcd_cont != 0)
Bram Moolenaarc150c092021-02-13 15:02:46 +01003885 // handling :continue: jump to outer try block or
3886 // start of the loop
Bram Moolenaar4c137212021-04-19 16:48:48 +02003887 ectx->ec_iidx = trycmd->tcd_cont - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003888 }
3889 }
3890 break;
3891
3892 case ISN_THROW:
Bram Moolenaar8f81b222021-01-14 21:47:06 +01003893 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003894 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02003895
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003896 if (trystack->ga_len == 0 && trylevel == 0 && emsg_silent)
3897 {
3898 // throwing an exception while using "silent!" causes
3899 // the function to abort but not display an error.
3900 tv = STACK_TV_BOT(-1);
3901 clear_tv(tv);
3902 tv->v_type = VAR_NUMBER;
3903 tv->vval.v_number = 0;
3904 goto done;
3905 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003906 --ectx->ec_stack.ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003907 tv = STACK_TV_BOT(0);
3908 if (tv->vval.v_string == NULL
3909 || *skipwhite(tv->vval.v_string) == NUL)
3910 {
3911 vim_free(tv->vval.v_string);
3912 SOURCING_LNUM = iptr->isn_lnum;
3913 emsg(_(e_throw_with_empty_string));
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003914 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003915 }
3916
3917 // Inside a "catch" we need to first discard the caught
3918 // exception.
3919 if (trystack->ga_len > 0)
3920 {
3921 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
3922 + trystack->ga_len - 1;
3923 if (trycmd->tcd_caught && current_exception != NULL)
3924 {
3925 // discard the exception
3926 if (caught_stack == current_exception)
3927 caught_stack = caught_stack->caught;
3928 discard_current_exception();
3929 trycmd->tcd_caught = FALSE;
3930 }
3931 }
3932
Bram Moolenaar90a57162022-02-12 14:23:17 +00003933 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003934 if (throw_exception(tv->vval.v_string, ET_USER, NULL)
3935 == FAIL)
3936 {
3937 vim_free(tv->vval.v_string);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003938 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003939 }
3940 did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003941 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003942 break;
3943
3944 // compare with special values
3945 case ISN_COMPAREBOOL:
3946 case ISN_COMPARESPECIAL:
3947 {
3948 typval_T *tv1 = STACK_TV_BOT(-2);
3949 typval_T *tv2 = STACK_TV_BOT(-1);
3950 varnumber_T arg1 = tv1->vval.v_number;
3951 varnumber_T arg2 = tv2->vval.v_number;
3952 int res;
3953
3954 switch (iptr->isn_arg.op.op_type)
3955 {
3956 case EXPR_EQUAL: res = arg1 == arg2; break;
3957 case EXPR_NEQUAL: res = arg1 != arg2; break;
3958 default: res = 0; break;
3959 }
3960
Bram Moolenaar4c137212021-04-19 16:48:48 +02003961 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003962 tv1->v_type = VAR_BOOL;
3963 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
3964 }
3965 break;
3966
Bram Moolenaar7a222242022-03-01 19:23:24 +00003967 case ISN_COMPARENULL:
3968 {
3969 typval_T *tv1 = STACK_TV_BOT(-2);
3970 typval_T *tv2 = STACK_TV_BOT(-1);
3971 int res;
3972
3973 res = typval_compare_null(tv1, tv2);
3974 if (res == MAYBE)
3975 goto on_error;
3976 if (iptr->isn_arg.op.op_type == EXPR_NEQUAL)
3977 res = !res;
3978 clear_tv(tv1);
3979 clear_tv(tv2);
3980 --ectx->ec_stack.ga_len;
3981 tv1->v_type = VAR_BOOL;
3982 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
3983 }
3984 break;
3985
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003986 // Operation with two number arguments
3987 case ISN_OPNR:
3988 case ISN_COMPARENR:
3989 {
3990 typval_T *tv1 = STACK_TV_BOT(-2);
3991 typval_T *tv2 = STACK_TV_BOT(-1);
3992 varnumber_T arg1 = tv1->vval.v_number;
3993 varnumber_T arg2 = tv2->vval.v_number;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02003994 varnumber_T res = 0;
3995 int div_zero = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003996
3997 switch (iptr->isn_arg.op.op_type)
3998 {
3999 case EXPR_MULT: res = arg1 * arg2; break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004000 case EXPR_DIV: if (arg2 == 0)
4001 div_zero = TRUE;
4002 else
4003 res = arg1 / arg2;
4004 break;
4005 case EXPR_REM: if (arg2 == 0)
4006 div_zero = TRUE;
4007 else
4008 res = arg1 % arg2;
4009 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004010 case EXPR_SUB: res = arg1 - arg2; break;
4011 case EXPR_ADD: res = arg1 + arg2; break;
4012
4013 case EXPR_EQUAL: res = arg1 == arg2; break;
4014 case EXPR_NEQUAL: res = arg1 != arg2; break;
4015 case EXPR_GREATER: res = arg1 > arg2; break;
4016 case EXPR_GEQUAL: res = arg1 >= arg2; break;
4017 case EXPR_SMALLER: res = arg1 < arg2; break;
4018 case EXPR_SEQUAL: res = arg1 <= arg2; break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004019 default: break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004020 }
4021
Bram Moolenaar4c137212021-04-19 16:48:48 +02004022 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004023 if (iptr->isn_type == ISN_COMPARENR)
4024 {
4025 tv1->v_type = VAR_BOOL;
4026 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4027 }
4028 else
4029 tv1->vval.v_number = res;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004030 if (div_zero)
4031 {
4032 SOURCING_LNUM = iptr->isn_lnum;
4033 emsg(_(e_divide_by_zero));
4034 goto on_error;
4035 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004036 }
4037 break;
4038
4039 // Computation with two float arguments
4040 case ISN_OPFLOAT:
4041 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01004042#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004043 {
4044 typval_T *tv1 = STACK_TV_BOT(-2);
4045 typval_T *tv2 = STACK_TV_BOT(-1);
4046 float_T arg1 = tv1->vval.v_float;
4047 float_T arg2 = tv2->vval.v_float;
4048 float_T res = 0;
4049 int cmp = FALSE;
4050
4051 switch (iptr->isn_arg.op.op_type)
4052 {
4053 case EXPR_MULT: res = arg1 * arg2; break;
4054 case EXPR_DIV: res = arg1 / arg2; break;
4055 case EXPR_SUB: res = arg1 - arg2; break;
4056 case EXPR_ADD: res = arg1 + arg2; break;
4057
4058 case EXPR_EQUAL: cmp = arg1 == arg2; break;
4059 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
4060 case EXPR_GREATER: cmp = arg1 > arg2; break;
4061 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
4062 case EXPR_SMALLER: cmp = arg1 < arg2; break;
4063 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
4064 default: cmp = 0; break;
4065 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004066 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004067 if (iptr->isn_type == ISN_COMPAREFLOAT)
4068 {
4069 tv1->v_type = VAR_BOOL;
4070 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
4071 }
4072 else
4073 tv1->vval.v_float = res;
4074 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01004075#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004076 break;
4077
4078 case ISN_COMPARELIST:
Bram Moolenaar265f8112021-12-19 12:33:05 +00004079 case ISN_COMPAREDICT:
4080 case ISN_COMPAREFUNC:
4081 case ISN_COMPARESTRING:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004082 case ISN_COMPAREBLOB:
4083 {
4084 typval_T *tv1 = STACK_TV_BOT(-2);
4085 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar265f8112021-12-19 12:33:05 +00004086 exprtype_T exprtype = iptr->isn_arg.op.op_type;
4087 int ic = iptr->isn_arg.op.op_ic;
4088 int res = FALSE;
4089 int status = OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004090
Bram Moolenaar265f8112021-12-19 12:33:05 +00004091 SOURCING_LNUM = iptr->isn_lnum;
4092 if (iptr->isn_type == ISN_COMPARELIST)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004093 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00004094 status = typval_compare_list(tv1, tv2,
4095 exprtype, ic, &res);
4096 }
4097 else if (iptr->isn_type == ISN_COMPAREDICT)
4098 {
4099 status = typval_compare_dict(tv1, tv2,
4100 exprtype, ic, &res);
4101 }
4102 else if (iptr->isn_type == ISN_COMPAREFUNC)
4103 {
4104 status = typval_compare_func(tv1, tv2,
4105 exprtype, ic, &res);
4106 }
4107 else if (iptr->isn_type == ISN_COMPARESTRING)
4108 {
4109 status = typval_compare_string(tv1, tv2,
4110 exprtype, ic, &res);
4111 }
4112 else
4113 {
4114 status = typval_compare_blob(tv1, tv2, exprtype, &res);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004115 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004116 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004117 clear_tv(tv1);
4118 clear_tv(tv2);
4119 tv1->v_type = VAR_BOOL;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004120 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4121 if (status == FAIL)
4122 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004123 }
4124 break;
4125
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004126 case ISN_COMPAREANY:
4127 {
4128 typval_T *tv1 = STACK_TV_BOT(-2);
4129 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar657137c2021-01-09 15:45:23 +01004130 exprtype_T exprtype = iptr->isn_arg.op.op_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004131 int ic = iptr->isn_arg.op.op_ic;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004132 int status;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004133
Bram Moolenaareb26f432020-09-14 16:50:05 +02004134 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004135 status = typval_compare(tv1, tv2, exprtype, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004136 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004137 --ectx->ec_stack.ga_len;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004138 if (status == FAIL)
4139 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004140 }
4141 break;
4142
4143 case ISN_ADDLIST:
4144 case ISN_ADDBLOB:
4145 {
4146 typval_T *tv1 = STACK_TV_BOT(-2);
4147 typval_T *tv2 = STACK_TV_BOT(-1);
4148
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004149 // add two lists or blobs
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004150 if (iptr->isn_type == ISN_ADDLIST)
Bram Moolenaar07802042021-09-09 23:01:14 +02004151 {
4152 if (iptr->isn_arg.op.op_type == EXPR_APPEND
4153 && tv1->vval.v_list != NULL)
4154 list_extend(tv1->vval.v_list, tv2->vval.v_list,
4155 NULL);
4156 else
4157 eval_addlist(tv1, tv2);
4158 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004159 else
4160 eval_addblob(tv1, tv2);
4161 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004162 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004163 }
4164 break;
4165
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004166 case ISN_LISTAPPEND:
4167 {
4168 typval_T *tv1 = STACK_TV_BOT(-2);
4169 typval_T *tv2 = STACK_TV_BOT(-1);
4170 list_T *l = tv1->vval.v_list;
4171
4172 // add an item to a list
Bram Moolenaar1f4a3452022-01-01 18:29:21 +00004173 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004174 if (l == NULL)
4175 {
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004176 emsg(_(e_cannot_add_to_null_list));
4177 goto on_error;
4178 }
Bram Moolenaar1f4a3452022-01-01 18:29:21 +00004179 if (value_check_lock(l->lv_lock, NULL, FALSE))
4180 goto on_error;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004181 if (list_append_tv(l, tv2) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004182 goto theend;
Bram Moolenaar955347c2020-10-19 23:01:46 +02004183 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004184 --ectx->ec_stack.ga_len;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004185 }
4186 break;
4187
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02004188 case ISN_BLOBAPPEND:
4189 {
4190 typval_T *tv1 = STACK_TV_BOT(-2);
4191 typval_T *tv2 = STACK_TV_BOT(-1);
4192 blob_T *b = tv1->vval.v_blob;
4193 int error = FALSE;
4194 varnumber_T n;
4195
4196 // add a number to a blob
4197 if (b == NULL)
4198 {
4199 SOURCING_LNUM = iptr->isn_lnum;
4200 emsg(_(e_cannot_add_to_null_blob));
4201 goto on_error;
4202 }
4203 n = tv_get_number_chk(tv2, &error);
4204 if (error)
4205 goto on_error;
4206 ga_append(&b->bv_ga, (int)n);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004207 --ectx->ec_stack.ga_len;
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02004208 }
4209 break;
4210
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004211 // Computation with two arguments of unknown type
4212 case ISN_OPANY:
4213 {
4214 typval_T *tv1 = STACK_TV_BOT(-2);
4215 typval_T *tv2 = STACK_TV_BOT(-1);
4216 varnumber_T n1, n2;
4217#ifdef FEAT_FLOAT
4218 float_T f1 = 0, f2 = 0;
4219#endif
4220 int error = FALSE;
4221
4222 if (iptr->isn_arg.op.op_type == EXPR_ADD)
4223 {
4224 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
4225 {
4226 eval_addlist(tv1, tv2);
4227 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004228 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004229 break;
4230 }
4231 else if (tv1->v_type == VAR_BLOB
4232 && tv2->v_type == VAR_BLOB)
4233 {
4234 eval_addblob(tv1, tv2);
4235 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004236 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004237 break;
4238 }
4239 }
4240#ifdef FEAT_FLOAT
4241 if (tv1->v_type == VAR_FLOAT)
4242 {
4243 f1 = tv1->vval.v_float;
4244 n1 = 0;
4245 }
4246 else
4247#endif
4248 {
Bram Moolenaarf665e972020-12-05 19:17:16 +01004249 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004250 n1 = tv_get_number_chk(tv1, &error);
4251 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02004252 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004253#ifdef FEAT_FLOAT
4254 if (tv2->v_type == VAR_FLOAT)
4255 f1 = n1;
4256#endif
4257 }
4258#ifdef FEAT_FLOAT
4259 if (tv2->v_type == VAR_FLOAT)
4260 {
4261 f2 = tv2->vval.v_float;
4262 n2 = 0;
4263 }
4264 else
4265#endif
4266 {
4267 n2 = tv_get_number_chk(tv2, &error);
4268 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02004269 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004270#ifdef FEAT_FLOAT
4271 if (tv1->v_type == VAR_FLOAT)
4272 f2 = n2;
4273#endif
4274 }
4275#ifdef FEAT_FLOAT
4276 // if there is a float on either side the result is a float
4277 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
4278 {
4279 switch (iptr->isn_arg.op.op_type)
4280 {
4281 case EXPR_MULT: f1 = f1 * f2; break;
4282 case EXPR_DIV: f1 = f1 / f2; break;
4283 case EXPR_SUB: f1 = f1 - f2; break;
4284 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004285 default: SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004286 emsg(_(e_cannot_use_percent_with_float));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004287 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004288 }
4289 clear_tv(tv1);
4290 clear_tv(tv2);
4291 tv1->v_type = VAR_FLOAT;
4292 tv1->vval.v_float = f1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004293 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004294 }
4295 else
4296#endif
4297 {
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004298 int failed = FALSE;
4299
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004300 switch (iptr->isn_arg.op.op_type)
4301 {
4302 case EXPR_MULT: n1 = n1 * n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004303 case EXPR_DIV: n1 = num_divide(n1, n2, &failed);
4304 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01004305 goto on_error;
4306 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004307 case EXPR_SUB: n1 = n1 - n2; break;
4308 case EXPR_ADD: n1 = n1 + n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004309 default: n1 = num_modulus(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 }
4314 clear_tv(tv1);
4315 clear_tv(tv2);
4316 tv1->v_type = VAR_NUMBER;
4317 tv1->vval.v_number = n1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004318 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004319 }
4320 }
4321 break;
4322
4323 case ISN_CONCAT:
4324 {
4325 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string;
4326 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string;
4327 char_u *res;
4328
4329 res = concat_str(str1, str2);
4330 clear_tv(STACK_TV_BOT(-2));
4331 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02004332 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004333 STACK_TV_BOT(-1)->vval.v_string = res;
4334 }
4335 break;
4336
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004337 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004338 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004339 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004340 int is_slice = iptr->isn_type == ISN_STRSLICE;
4341 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004342 char_u *res;
4343
4344 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004345 // string slice: string is at stack-3, first index at
4346 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004347 if (is_slice)
4348 {
4349 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004350 n1 = tv->vval.v_number;
4351 }
4352
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004353 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004354 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004355
Bram Moolenaar4c137212021-04-19 16:48:48 +02004356 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004357 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004358 if (is_slice)
4359 // Slice: Select the characters from the string
Bram Moolenaar6601b622021-01-13 21:47:15 +01004360 res = string_slice(tv->vval.v_string, n1, n2, FALSE);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004361 else
4362 // Index: The resulting variable is a string of a
Bram Moolenaar0289a092021-03-14 18:40:19 +01004363 // single character (including composing characters).
4364 // If the index is too big or negative the result is
4365 // empty.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004366 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004367 vim_free(tv->vval.v_string);
4368 tv->vval.v_string = res;
4369 }
4370 break;
4371
4372 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02004373 case ISN_LISTSLICE:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004374 case ISN_BLOBINDEX:
4375 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004376 {
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004377 int is_slice = iptr->isn_type == ISN_LISTSLICE
4378 || iptr->isn_type == ISN_BLOBSLICE;
4379 int is_blob = iptr->isn_type == ISN_BLOBINDEX
4380 || iptr->isn_type == ISN_BLOBSLICE;
Bram Moolenaared591872020-08-15 22:14:53 +02004381 varnumber_T n1, n2;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004382 typval_T *val_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004383
4384 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02004385 // list slice: list is at stack-3, indexes at stack-2 and
4386 // stack-1
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004387 // Same for blob.
4388 val_tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004389
4390 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02004391 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004392 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02004393
4394 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004395 {
Bram Moolenaared591872020-08-15 22:14:53 +02004396 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02004397 n1 = tv->vval.v_number;
4398 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004399 }
Bram Moolenaared591872020-08-15 22:14:53 +02004400
Bram Moolenaar4c137212021-04-19 16:48:48 +02004401 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02004402 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02004403 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004404 if (is_blob)
4405 {
4406 if (blob_slice_or_index(val_tv->vval.v_blob, is_slice,
4407 n1, n2, FALSE, tv) == FAIL)
4408 goto on_error;
4409 }
4410 else
4411 {
4412 if (list_slice_or_index(val_tv->vval.v_list, is_slice,
4413 n1, n2, FALSE, tv, TRUE) == FAIL)
4414 goto on_error;
4415 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004416 }
4417 break;
4418
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004419 case ISN_ANYINDEX:
4420 case ISN_ANYSLICE:
4421 {
4422 int is_slice = iptr->isn_type == ISN_ANYSLICE;
4423 typval_T *var1, *var2;
4424 int res;
4425
4426 // index: composite is at stack-2, index at stack-1
4427 // slice: composite is at stack-3, indexes at stack-2 and
4428 // stack-1
4429 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02004430 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004431 if (check_can_index(tv, TRUE, TRUE) == FAIL)
4432 goto on_error;
4433 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
4434 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
Bram Moolenaar6601b622021-01-13 21:47:15 +01004435 res = eval_index_inner(tv, is_slice, var1, var2,
4436 FALSE, NULL, -1, TRUE);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004437 clear_tv(var1);
4438 if (is_slice)
4439 clear_tv(var2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004440 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004441 if (res == FAIL)
4442 goto on_error;
4443 }
4444 break;
4445
Bram Moolenaar9af78762020-06-16 11:34:42 +02004446 case ISN_SLICE:
4447 {
4448 list_T *list;
4449 int count = iptr->isn_arg.number;
4450
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02004451 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02004452 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02004453 list = tv->vval.v_list;
4454
4455 // no error for short list, expect it to be checked earlier
4456 if (list != NULL && list->lv_len >= count)
4457 {
4458 list_T *newlist = list_slice(list,
4459 count, list->lv_len - 1);
4460
4461 if (newlist != NULL)
4462 {
4463 list_unref(list);
4464 tv->vval.v_list = newlist;
4465 ++newlist->lv_refcount;
4466 }
4467 }
4468 }
4469 break;
4470
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004471 case ISN_GETITEM:
4472 {
4473 listitem_T *li;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02004474 getitem_T *gi = &iptr->isn_arg.getitem;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004475
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02004476 // Get list item: list is at stack-1, push item.
4477 // List type and length is checked for when compiling.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02004478 tv = STACK_TV_BOT(-1 - gi->gi_with_op);
4479 li = list_find(tv->vval.v_list, gi->gi_index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004480
Bram Moolenaar35578162021-08-02 19:10:38 +02004481 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004482 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004483 ++ectx->ec_stack.ga_len;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004484 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004485
4486 // Useful when used in unpack assignment. Reset at
4487 // ISN_DROP.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02004488 ectx->ec_where.wt_index = gi->gi_index + 1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004489 ectx->ec_where.wt_variable = TRUE;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004490 }
4491 break;
4492
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004493 case ISN_MEMBER:
4494 {
4495 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004496 char_u *key;
4497 dictitem_T *di;
4498
4499 // dict member: dict is at stack-2, key at stack-1
4500 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02004501 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004502 dict = tv->vval.v_dict;
4503
4504 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02004505 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004506 key = tv->vval.v_string;
Bram Moolenaar086fc9a2020-10-30 18:33:02 +01004507 if (key == NULL)
4508 key = (char_u *)"";
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02004509
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004510 if ((di = dict_find(dict, key, -1)) == NULL)
4511 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004512 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004513 semsg(_(e_key_not_present_in_dictionary), key);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004514
4515 // If :silent! is used we will continue, make sure the
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004516 // stack contents makes sense and the dict stack is
4517 // updated.
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004518 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004519 --ectx->ec_stack.ga_len;
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004520 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004521 (void) dict_stack_save(tv);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004522 tv->v_type = VAR_NUMBER;
4523 tv->vval.v_number = 0;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004524 goto on_fatal_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004525 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004526 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004527 --ectx->ec_stack.ga_len;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004528 // Put the dict used on the dict stack, it might be used by
4529 // a dict function later.
Bram Moolenaar50788ef2020-07-05 16:51:26 +02004530 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004531 if (dict_stack_save(tv) == FAIL)
4532 goto on_fatal_error;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02004533 copy_tv(&di->di_tv, tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004534 }
4535 break;
4536
4537 // dict member with string key
4538 case ISN_STRINGMEMBER:
4539 {
4540 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004541 dictitem_T *di;
4542
4543 tv = STACK_TV_BOT(-1);
4544 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
4545 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004546 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004547 emsg(_(e_dictionary_required));
Bram Moolenaard032f342020-07-18 18:13:02 +02004548 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004549 }
4550 dict = tv->vval.v_dict;
4551
4552 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
4553 == NULL)
4554 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004555 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar381692b2022-02-02 20:01:27 +00004556 semsg(_(e_key_not_present_in_dictionary),
4557 iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02004558 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004559 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004560 // Put the dict used on the dict stack, it might be used by
4561 // a dict function later.
4562 if (dict_stack_save(tv) == FAIL)
4563 goto on_fatal_error;
4564
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004565 copy_tv(&di->di_tv, tv);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004566 }
4567 break;
4568
4569 case ISN_CLEARDICT:
4570 dict_stack_drop();
4571 break;
4572
4573 case ISN_USEDICT:
4574 {
4575 typval_T *dict_tv = dict_stack_get_tv();
4576
4577 // Turn "dict.Func" into a partial for "Func" bound to
4578 // "dict". Don't do this when "Func" is already a partial
4579 // that was bound explicitly (pt_auto is FALSE).
4580 tv = STACK_TV_BOT(-1);
4581 if (dict_tv != NULL
4582 && dict_tv->v_type == VAR_DICT
4583 && dict_tv->vval.v_dict != NULL
4584 && (tv->v_type == VAR_FUNC
4585 || (tv->v_type == VAR_PARTIAL
4586 && (tv->vval.v_partial->pt_auto
4587 || tv->vval.v_partial->pt_dict == NULL))))
4588 dict_tv->vval.v_dict =
4589 make_partial(dict_tv->vval.v_dict, tv);
4590 dict_stack_drop();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004591 }
4592 break;
4593
4594 case ISN_NEGATENR:
4595 tv = STACK_TV_BOT(-1);
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00004596 // CHECKTYPE should have checked the variable type
Bram Moolenaarc58164c2020-03-29 18:40:30 +02004597#ifdef FEAT_FLOAT
4598 if (tv->v_type == VAR_FLOAT)
4599 tv->vval.v_float = -tv->vval.v_float;
4600 else
4601#endif
4602 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004603 break;
4604
4605 case ISN_CHECKNR:
4606 {
4607 int error = FALSE;
4608
4609 tv = STACK_TV_BOT(-1);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02004610 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004611 if (check_not_string(tv) == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004612 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004613 (void)tv_get_number_chk(tv, &error);
4614 if (error)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004615 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004616 }
4617 break;
4618
4619 case ISN_CHECKTYPE:
4620 {
4621 checktype_T *ct = &iptr->isn_arg.type;
4622
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01004623 tv = STACK_TV_BOT((int)ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02004624 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004625 if (!ectx->ec_where.wt_variable)
4626 ectx->ec_where.wt_index = ct->ct_arg_idx;
4627 if (check_typval_type(ct->ct_type, tv, ectx->ec_where)
4628 == FAIL)
Bram Moolenaar5e654232020-09-16 15:22:00 +02004629 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004630 if (!ectx->ec_where.wt_variable)
4631 ectx->ec_where.wt_index = 0;
Bram Moolenaar5e654232020-09-16 15:22:00 +02004632
4633 // number 0 is FALSE, number 1 is TRUE
4634 if (tv->v_type == VAR_NUMBER
4635 && ct->ct_type->tt_type == VAR_BOOL
4636 && (tv->vval.v_number == 0
4637 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004638 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02004639 tv->v_type = VAR_BOOL;
4640 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02004641 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004642 }
4643 }
4644 break;
4645
Bram Moolenaar9af78762020-06-16 11:34:42 +02004646 case ISN_CHECKLEN:
4647 {
4648 int min_len = iptr->isn_arg.checklen.cl_min_len;
4649 list_T *list = NULL;
4650
4651 tv = STACK_TV_BOT(-1);
4652 if (tv->v_type == VAR_LIST)
4653 list = tv->vval.v_list;
4654 if (list == NULL || list->lv_len < min_len
4655 || (list->lv_len > min_len
4656 && !iptr->isn_arg.checklen.cl_more_OK))
4657 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004658 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004659 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02004660 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004661 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02004662 }
4663 }
4664 break;
4665
Bram Moolenaaraa210a32021-01-02 15:41:03 +01004666 case ISN_SETTYPE:
Bram Moolenaar381692b2022-02-02 20:01:27 +00004667 set_tv_type(STACK_TV_BOT(-1), iptr->isn_arg.type.ct_type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01004668 break;
4669
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004670 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004671 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004672 {
4673 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004674 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004675
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004676 if (iptr->isn_type == ISN_2BOOL)
4677 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004678 tv = STACK_TV_BOT(iptr->isn_arg.tobool.offset);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004679 n = tv2bool(tv);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004680 if (iptr->isn_arg.tobool.invert)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004681 n = !n;
4682 }
4683 else
4684 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004685 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004686 SOURCING_LNUM = iptr->isn_lnum;
4687 n = tv_get_bool_chk(tv, &error);
4688 if (error)
4689 goto on_error;
4690 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004691 clear_tv(tv);
4692 tv->v_type = VAR_BOOL;
4693 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
4694 }
4695 break;
4696
4697 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02004698 case ISN_2STRING_ANY:
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01004699 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004700 if (do_2string(STACK_TV_BOT(iptr->isn_arg.tostring.offset),
4701 iptr->isn_type == ISN_2STRING_ANY,
4702 iptr->isn_arg.tostring.tolerant) == FAIL)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01004703 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004704 break;
4705
Bram Moolenaar08597872020-12-10 19:43:40 +01004706 case ISN_RANGE:
4707 {
4708 exarg_T ea;
4709 char *errormsg;
4710
Bram Moolenaarece0b872021-01-08 20:40:45 +01004711 ea.line2 = 0;
Bram Moolenaard1510ee2021-01-04 16:15:58 +01004712 ea.addr_count = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01004713 ea.addr_type = ADDR_LINES;
4714 ea.cmd = iptr->isn_arg.string;
Bram Moolenaarece0b872021-01-08 20:40:45 +01004715 ea.skip = FALSE;
Bram Moolenaar08597872020-12-10 19:43:40 +01004716 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaarece0b872021-01-08 20:40:45 +01004717 goto on_error;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004718
Bram Moolenaar35578162021-08-02 19:10:38 +02004719 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004720 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004721 ++ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004722 tv = STACK_TV_BOT(-1);
4723 tv->v_type = VAR_NUMBER;
4724 tv->v_lock = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01004725 if (ea.addr_count == 0)
4726 tv->vval.v_number = curwin->w_cursor.lnum;
4727 else
4728 tv->vval.v_number = ea.line2;
4729 }
4730 break;
4731
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004732 case ISN_PUT:
4733 {
4734 int regname = iptr->isn_arg.put.put_regname;
4735 linenr_T lnum = iptr->isn_arg.put.put_lnum;
4736 char_u *expr = NULL;
4737 int dir = FORWARD;
4738
Bram Moolenaar08597872020-12-10 19:43:40 +01004739 if (lnum < -2)
4740 {
4741 // line number was put on the stack by ISN_RANGE
4742 tv = STACK_TV_BOT(-1);
4743 curwin->w_cursor.lnum = tv->vval.v_number;
4744 if (lnum == LNUM_VARIABLE_RANGE_ABOVE)
4745 dir = BACKWARD;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004746 --ectx->ec_stack.ga_len;
Bram Moolenaar08597872020-12-10 19:43:40 +01004747 }
4748 else if (lnum == -2)
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004749 // :put! above cursor
4750 dir = BACKWARD;
4751 else if (lnum >= 0)
Bram Moolenaar4e713ba2022-02-07 15:31:37 +00004752 {
4753 curwin->w_cursor.lnum = lnum;
4754 if (lnum == 0)
4755 // check_cursor() below will move to line 1
4756 dir = BACKWARD;
4757 }
Bram Moolenaara28639e2021-01-19 22:48:09 +01004758
4759 if (regname == '=')
4760 {
4761 tv = STACK_TV_BOT(-1);
4762 if (tv->v_type == VAR_STRING)
4763 expr = tv->vval.v_string;
4764 else
4765 {
4766 expr = typval2string(tv, TRUE); // allocates value
4767 clear_tv(tv);
4768 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004769 --ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004770 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004771 check_cursor();
4772 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
4773 vim_free(expr);
4774 }
4775 break;
4776
Bram Moolenaar02194d22020-10-24 23:08:38 +02004777 case ISN_CMDMOD:
Bram Moolenaar4c137212021-04-19 16:48:48 +02004778 ectx->ec_funclocal.floc_save_cmdmod = cmdmod;
4779 ectx->ec_funclocal.floc_restore_cmdmod = TRUE;
4780 ectx->ec_funclocal.floc_restore_cmdmod_stacklen =
4781 ectx->ec_stack.ga_len;
Bram Moolenaar02194d22020-10-24 23:08:38 +02004782 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
4783 apply_cmdmod(&cmdmod);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004784 break;
4785
Bram Moolenaar02194d22020-10-24 23:08:38 +02004786 case ISN_CMDMOD_REV:
4787 // filter regprog is owned by the instruction, don't free it
4788 cmdmod.cmod_filter_regmatch.regprog = NULL;
4789 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004790 cmdmod = ectx->ec_funclocal.floc_save_cmdmod;
4791 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004792 break;
4793
Bram Moolenaar792f7862020-11-23 08:31:18 +01004794 case ISN_UNPACK:
4795 {
4796 int count = iptr->isn_arg.unpack.unp_count;
4797 int semicolon = iptr->isn_arg.unpack.unp_semicolon;
4798 list_T *l;
4799 listitem_T *li;
4800 int i;
4801
4802 // Check there is a valid list to unpack.
4803 tv = STACK_TV_BOT(-1);
4804 if (tv->v_type != VAR_LIST)
4805 {
4806 SOURCING_LNUM = iptr->isn_lnum;
4807 emsg(_(e_for_argument_must_be_sequence_of_lists));
4808 goto on_error;
4809 }
4810 l = tv->vval.v_list;
4811 if (l == NULL
4812 || l->lv_len < (semicolon ? count - 1 : count))
4813 {
4814 SOURCING_LNUM = iptr->isn_lnum;
4815 emsg(_(e_list_value_does_not_have_enough_items));
4816 goto on_error;
4817 }
4818 else if (!semicolon && l->lv_len > count)
4819 {
4820 SOURCING_LNUM = iptr->isn_lnum;
4821 emsg(_(e_list_value_has_more_items_than_targets));
4822 goto on_error;
4823 }
4824
4825 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar35578162021-08-02 19:10:38 +02004826 if (GA_GROW_FAILS(&ectx->ec_stack, count - 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004827 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004828 ectx->ec_stack.ga_len += count - 1;
Bram Moolenaar792f7862020-11-23 08:31:18 +01004829
4830 // Variable after semicolon gets a list with the remaining
4831 // items.
4832 if (semicolon)
4833 {
4834 list_T *rem_list =
4835 list_alloc_with_items(l->lv_len - count + 1);
4836
4837 if (rem_list == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004838 goto theend;
Bram Moolenaar792f7862020-11-23 08:31:18 +01004839 tv = STACK_TV_BOT(-count);
4840 tv->vval.v_list = rem_list;
4841 ++rem_list->lv_refcount;
4842 tv->v_lock = 0;
4843 li = l->lv_first;
4844 for (i = 0; i < count - 1; ++i)
4845 li = li->li_next;
4846 for (i = 0; li != NULL; ++i)
4847 {
Bram Moolenaar61efa162022-03-18 13:10:48 +00004848 typval_T tvcopy;
4849
4850 copy_tv(&li->li_tv, &tvcopy);
4851 list_set_item(rem_list, i, &tvcopy);
Bram Moolenaar792f7862020-11-23 08:31:18 +01004852 li = li->li_next;
4853 }
4854 --count;
4855 }
4856
4857 // Produce the values in reverse order, first item last.
4858 li = l->lv_first;
4859 for (i = 0; i < count; ++i)
4860 {
4861 tv = STACK_TV_BOT(-i - 1);
4862 copy_tv(&li->li_tv, tv);
4863 li = li->li_next;
4864 }
4865
4866 list_unref(l);
4867 }
4868 break;
4869
Bram Moolenaarb2049902021-01-24 12:53:53 +01004870 case ISN_PROF_START:
4871 case ISN_PROF_END:
4872 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01004873#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01004874 funccall_T cookie;
4875 ufunc_T *cur_ufunc =
4876 (((dfunc_T *)def_functions.ga_data)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02004877 + ectx->ec_dfunc_idx)->df_ufunc;
Bram Moolenaarb2049902021-01-24 12:53:53 +01004878
4879 cookie.func = cur_ufunc;
4880 if (iptr->isn_type == ISN_PROF_START)
4881 {
4882 func_line_start(&cookie, iptr->isn_lnum);
4883 // if we get here the instruction is executed
4884 func_line_exec(&cookie);
4885 }
4886 else
4887 func_line_end(&cookie);
Bram Moolenaarf002a412021-01-24 13:34:18 +01004888#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01004889 }
4890 break;
4891
Bram Moolenaare99d4222021-06-13 14:01:26 +02004892 case ISN_DEBUG:
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02004893 handle_debug(iptr, ectx);
Bram Moolenaare99d4222021-06-13 14:01:26 +02004894 break;
4895
Bram Moolenaar389df252020-07-09 21:20:47 +02004896 case ISN_SHUFFLE:
4897 {
Bram Moolenaar792f7862020-11-23 08:31:18 +01004898 typval_T tmp_tv;
4899 int item = iptr->isn_arg.shuffle.shfl_item;
4900 int up = iptr->isn_arg.shuffle.shfl_up;
Bram Moolenaar389df252020-07-09 21:20:47 +02004901
4902 tmp_tv = *STACK_TV_BOT(-item);
4903 for ( ; up > 0 && item > 1; --up)
4904 {
4905 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
4906 --item;
4907 }
4908 *STACK_TV_BOT(-item) = tmp_tv;
4909 }
4910 break;
4911
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004912 case ISN_DROP:
Bram Moolenaar4c137212021-04-19 16:48:48 +02004913 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004914 clear_tv(STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02004915 ectx->ec_where.wt_index = 0;
4916 ectx->ec_where.wt_variable = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004917 break;
4918 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004919 continue;
4920
Bram Moolenaard032f342020-07-18 18:13:02 +02004921func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02004922 // Restore previous function. If the frame pointer is where we started
4923 // then there is none and we are done.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004924 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02004925 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02004926
Bram Moolenaar4c137212021-04-19 16:48:48 +02004927 if (func_return(ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004928 // only fails when out of memory
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004929 goto theend;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02004930 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02004931
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004932on_error:
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004933 // Jump here for an error that does not require aborting execution.
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004934 // If "emsg_silent" is set then ignore the error, unless it was set
4935 // when calling the function.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004936 if (did_emsg_cumul + did_emsg == ectx->ec_did_emsg_before
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004937 && emsg_silent && did_emsg_def == 0)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004938 {
4939 // If a sequence of instructions causes an error while ":silent!"
4940 // was used, restore the stack length and jump ahead to restoring
4941 // the cmdmod.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004942 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004943 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004944 while (ectx->ec_stack.ga_len
4945 > ectx->ec_funclocal.floc_restore_cmdmod_stacklen)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004946 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004947 --ectx->ec_stack.ga_len;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004948 clear_tv(STACK_TV_BOT(0));
4949 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004950 while (ectx->ec_instr[ectx->ec_iidx].isn_type != ISN_CMDMOD_REV)
4951 ++ectx->ec_iidx;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004952 }
Bram Moolenaarcd030c42020-10-30 21:49:40 +01004953 continue;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004954 }
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004955on_fatal_error:
4956 // Jump here for an error that messes up the stack.
Bram Moolenaar171fb922020-10-28 16:54:47 +01004957 // If we are not inside a try-catch started here, abort execution.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004958 if (trylevel <= ectx->ec_trylevel_at_start)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004959 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004960 }
4961
4962done:
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004963 ret = OK;
4964theend:
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004965 dict_stack_clear(dict_stack_len_at_start);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004966 ectx->ec_trylevel_at_start = save_trylevel_at_start;
4967 return ret;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004968}
4969
4970/*
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004971 * Execute the instructions from a VAR_INSTR typeval and put the result in
4972 * "rettv".
4973 * Return OK or FAIL.
4974 */
4975 int
4976exe_typval_instr(typval_T *tv, typval_T *rettv)
4977{
4978 ectx_T *ectx = tv->vval.v_instr->instr_ectx;
4979 isn_T *save_instr = ectx->ec_instr;
4980 int save_iidx = ectx->ec_iidx;
4981 int res;
4982
4983 ectx->ec_instr = tv->vval.v_instr->instr_instr;
4984 res = exec_instructions(ectx);
4985 if (res == OK)
4986 {
4987 *rettv = *STACK_TV_BOT(-1);
4988 --ectx->ec_stack.ga_len;
4989 }
4990
4991 ectx->ec_instr = save_instr;
4992 ectx->ec_iidx = save_iidx;
4993
4994 return res;
4995}
4996
4997/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02004998 * Execute the instructions from an ISN_SUBSTITUTE command, which are in
4999 * "substitute_instr".
5000 */
5001 char_u *
5002exe_substitute_instr(void)
5003{
5004 ectx_T *ectx = substitute_instr->subs_ectx;
5005 isn_T *save_instr = ectx->ec_instr;
5006 int save_iidx = ectx->ec_iidx;
5007 char_u *res;
5008
5009 ectx->ec_instr = substitute_instr->subs_instr;
5010 if (exec_instructions(ectx) == OK)
Bram Moolenaar90193e62021-04-04 20:49:50 +02005011 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005012 typval_T *tv = STACK_TV_BOT(-1);
5013
Bram Moolenaar27523602021-06-05 21:36:19 +02005014 res = typval2string(tv, TRUE);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005015 --ectx->ec_stack.ga_len;
5016 clear_tv(tv);
Bram Moolenaar90193e62021-04-04 20:49:50 +02005017 }
5018 else
5019 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005020 substitute_instr->subs_status = FAIL;
5021 res = vim_strsave((char_u *)"");
Bram Moolenaar90193e62021-04-04 20:49:50 +02005022 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005023
Bram Moolenaar4c137212021-04-19 16:48:48 +02005024 ectx->ec_instr = save_instr;
5025 ectx->ec_iidx = save_iidx;
5026
5027 return res;
5028}
5029
5030/*
5031 * Call a "def" function from old Vim script.
5032 * Return OK or FAIL.
5033 */
5034 int
5035call_def_function(
5036 ufunc_T *ufunc,
5037 int argc_arg, // nr of arguments
5038 typval_T *argv, // arguments
5039 partial_T *partial, // optional partial for context
5040 typval_T *rettv) // return value
5041{
5042 ectx_T ectx; // execution context
5043 int argc = argc_arg;
5044 typval_T *tv;
5045 int idx;
5046 int ret = FAIL;
5047 int defcount = ufunc->uf_args.ga_len - argc;
5048 sctx_T save_current_sctx = current_sctx;
5049 int did_emsg_before = did_emsg_cumul + did_emsg;
5050 int save_suppress_errthrow = suppress_errthrow;
5051 msglist_T **saved_msg_list = NULL;
5052 msglist_T *private_msg_list = NULL;
5053 int save_emsg_silent_def = emsg_silent_def;
5054 int save_did_emsg_def = did_emsg_def;
5055 int orig_funcdepth;
Bram Moolenaare99d4222021-06-13 14:01:26 +02005056 int orig_nesting_level = ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005057
5058// Get pointer to item in the stack.
5059#undef STACK_TV
5060#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
5061
5062// Get pointer to item at the bottom of the stack, -1 is the bottom.
5063#undef STACK_TV_BOT
5064#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
5065
5066// Get pointer to a local variable on the stack. Negative for arguments.
5067#undef STACK_TV_VAR
5068#define STACK_TV_VAR(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_frame_idx + STACK_FRAME_SIZE + idx)
5069
5070 if (ufunc->uf_def_status == UF_NOT_COMPILED
5071 || ufunc->uf_def_status == UF_COMPILE_ERROR
Bram Moolenaar139575d2022-03-15 19:29:30 +00005072 || (func_needs_compiling(ufunc, get_compile_type(ufunc))
5073 && compile_def_function(ufunc, FALSE,
5074 get_compile_type(ufunc), NULL) == FAIL))
Bram Moolenaar4c137212021-04-19 16:48:48 +02005075 {
5076 if (did_emsg_cumul + did_emsg == did_emsg_before)
5077 semsg(_(e_function_is_not_compiled_str),
5078 printable_func_name(ufunc));
5079 return FAIL;
5080 }
5081
5082 {
5083 // Check the function was really compiled.
5084 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5085 + ufunc->uf_dfunc_idx;
5086 if (INSTRUCTIONS(dfunc) == NULL)
5087 {
5088 iemsg("using call_def_function() on not compiled function");
5089 return FAIL;
5090 }
5091 }
5092
5093 // If depth of calling is getting too high, don't execute the function.
5094 orig_funcdepth = funcdepth_get();
5095 if (funcdepth_increment() == FAIL)
5096 return FAIL;
5097
5098 CLEAR_FIELD(ectx);
5099 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
5100 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
Bram Moolenaar35578162021-08-02 19:10:38 +02005101 if (GA_GROW_FAILS(&ectx.ec_stack, 20))
Bram Moolenaar4c137212021-04-19 16:48:48 +02005102 {
5103 funcdepth_decrement();
5104 return FAIL;
5105 }
5106 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
5107 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
5108 ectx.ec_did_emsg_before = did_emsg_before;
Bram Moolenaare99d4222021-06-13 14:01:26 +02005109 ++ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005110
5111 idx = argc - ufunc->uf_args.ga_len;
5112 if (idx > 0 && ufunc->uf_va_name == NULL)
5113 {
5114 if (idx == 1)
5115 emsg(_(e_one_argument_too_many));
5116 else
5117 semsg(_(e_nr_arguments_too_many), idx);
5118 goto failed_early;
5119 }
Bram Moolenaarc6d71532021-06-05 18:49:38 +02005120 idx = argc - ufunc->uf_args.ga_len + ufunc->uf_def_args.ga_len;
5121 if (idx < 0)
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02005122 {
5123 if (idx == -1)
5124 emsg(_(e_one_argument_too_few));
5125 else
5126 semsg(_(e_nr_arguments_too_few), -idx);
5127 goto failed_early;
5128 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005129
5130 // Put arguments on the stack, but no more than what the function expects.
5131 // A lambda can be called with more arguments than it uses.
5132 for (idx = 0; idx < argc
5133 && (ufunc->uf_va_name != NULL || idx < ufunc->uf_args.ga_len);
5134 ++idx)
5135 {
5136 if (idx >= ufunc->uf_args.ga_len - ufunc->uf_def_args.ga_len
5137 && argv[idx].v_type == VAR_SPECIAL
5138 && argv[idx].vval.v_number == VVAL_NONE)
5139 {
5140 // Use the default value.
5141 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
5142 }
5143 else
5144 {
5145 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
5146 && check_typval_arg_type(
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02005147 ufunc->uf_arg_types[idx], &argv[idx],
5148 NULL, idx + 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005149 goto failed_early;
5150 copy_tv(&argv[idx], STACK_TV_BOT(0));
5151 }
5152 ++ectx.ec_stack.ga_len;
5153 }
5154
5155 // Turn varargs into a list. Empty list if no args.
5156 if (ufunc->uf_va_name != NULL)
5157 {
5158 int vararg_count = argc - ufunc->uf_args.ga_len;
5159
5160 if (vararg_count < 0)
5161 vararg_count = 0;
5162 else
5163 argc -= vararg_count;
5164 if (exe_newlist(vararg_count, &ectx) == FAIL)
5165 goto failed_early;
5166
5167 // Check the type of the list items.
5168 tv = STACK_TV_BOT(-1);
5169 if (ufunc->uf_va_type != NULL
5170 && ufunc->uf_va_type != &t_list_any
5171 && ufunc->uf_va_type->tt_member != &t_any
5172 && tv->vval.v_list != NULL)
5173 {
5174 type_T *expected = ufunc->uf_va_type->tt_member;
5175 listitem_T *li = tv->vval.v_list->lv_first;
5176
5177 for (idx = 0; idx < vararg_count; ++idx)
5178 {
5179 if (check_typval_arg_type(expected, &li->li_tv,
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02005180 NULL, argc + idx + 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005181 goto failed_early;
5182 li = li->li_next;
5183 }
5184 }
5185
5186 if (defcount > 0)
5187 // Move varargs list to below missing default arguments.
5188 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
5189 --ectx.ec_stack.ga_len;
5190 }
5191
5192 // Make space for omitted arguments, will store default value below.
5193 // Any varargs list goes after them.
5194 if (defcount > 0)
5195 for (idx = 0; idx < defcount; ++idx)
5196 {
5197 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
5198 ++ectx.ec_stack.ga_len;
5199 }
5200 if (ufunc->uf_va_name != NULL)
5201 ++ectx.ec_stack.ga_len;
5202
5203 // Frame pointer points to just after arguments.
5204 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
5205 ectx.ec_initial_frame_idx = ectx.ec_frame_idx;
5206
5207 {
5208 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5209 + ufunc->uf_dfunc_idx;
5210 ufunc_T *base_ufunc = dfunc->df_ufunc;
5211
5212 // "uf_partial" is on the ufunc that "df_ufunc" points to, as is done
5213 // by copy_func().
5214 if (partial != NULL || base_ufunc->uf_partial != NULL)
5215 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005216 ectx.ec_outer_ref = ALLOC_CLEAR_ONE(outer_ref_T);
5217 if (ectx.ec_outer_ref == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005218 goto failed_early;
5219 if (partial != NULL)
5220 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00005221 outer_T *outer = get_pt_outer(partial);
5222
5223 if (outer->out_stack == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005224 {
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00005225 if (current_ectx != NULL)
5226 {
5227 if (current_ectx->ec_outer_ref != NULL
5228 && current_ectx->ec_outer_ref->or_outer != NULL)
5229 ectx.ec_outer_ref->or_outer =
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005230 current_ectx->ec_outer_ref->or_outer;
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00005231 }
5232 // Should there be an error here?
Bram Moolenaar4c137212021-04-19 16:48:48 +02005233 }
5234 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005235 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00005236 ectx.ec_outer_ref->or_outer = outer;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005237 ++partial->pt_refcount;
5238 ectx.ec_outer_ref->or_partial = partial;
5239 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005240 }
5241 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005242 {
5243 ectx.ec_outer_ref->or_outer = &base_ufunc->uf_partial->pt_outer;
5244 ++base_ufunc->uf_partial->pt_refcount;
5245 ectx.ec_outer_ref->or_partial = base_ufunc->uf_partial;
5246 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005247 }
5248 }
5249
5250 // dummy frame entries
5251 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
5252 {
5253 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
5254 ++ectx.ec_stack.ga_len;
5255 }
5256
5257 {
5258 // Reserve space for local variables and any closure reference count.
5259 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5260 + ufunc->uf_dfunc_idx;
5261
Bram Moolenaar5cd64792021-12-25 18:23:24 +00005262 // Initialize variables to zero. That avoids having to generate
5263 // initializing instructions for "var nr: number", "var x: any", etc.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005264 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaar5cd64792021-12-25 18:23:24 +00005265 {
5266 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
5267 STACK_TV_VAR(idx)->vval.v_number = 0;
5268 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005269 ectx.ec_stack.ga_len += dfunc->df_varcount;
5270 if (dfunc->df_has_closure)
5271 {
5272 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
5273 STACK_TV_VAR(idx)->vval.v_number = 0;
5274 ++ectx.ec_stack.ga_len;
5275 }
5276
5277 ectx.ec_instr = INSTRUCTIONS(dfunc);
5278 }
5279
5280 // Following errors are in the function, not the caller.
5281 // Commands behave like vim9script.
5282 estack_push_ufunc(ufunc, 1);
5283 current_sctx = ufunc->uf_script_ctx;
5284 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
5285
5286 // Use a specific location for storing error messages to be converted to an
5287 // exception.
5288 saved_msg_list = msg_list;
5289 msg_list = &private_msg_list;
5290
5291 // Do turn errors into exceptions.
5292 suppress_errthrow = FALSE;
5293
5294 // Do not delete the function while executing it.
5295 ++ufunc->uf_calls;
5296
5297 // When ":silent!" was used before calling then we still abort the
5298 // function. If ":silent!" is used in the function then we don't.
5299 emsg_silent_def = emsg_silent;
5300 did_emsg_def = 0;
5301
5302 ectx.ec_where.wt_index = 0;
5303 ectx.ec_where.wt_variable = FALSE;
5304
5305 // Execute the instructions until done.
5306 ret = exec_instructions(&ectx);
5307 if (ret == OK)
5308 {
5309 // function finished, get result from the stack.
5310 if (ufunc->uf_ret_type == &t_void)
5311 {
5312 rettv->v_type = VAR_VOID;
5313 }
5314 else
5315 {
5316 tv = STACK_TV_BOT(-1);
5317 *rettv = *tv;
5318 tv->v_type = VAR_UNKNOWN;
5319 }
5320 }
5321
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01005322 // When failed need to unwind the call stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005323 while (ectx.ec_frame_idx != ectx.ec_initial_frame_idx)
5324 func_return(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02005325
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005326 // Deal with any remaining closures, they may be in use somewhere.
5327 if (ectx.ec_funcrefs.ga_len > 0)
Bram Moolenaarf112f302020-12-20 17:47:52 +01005328 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005329 handle_closure_in_use(&ectx, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00005330 ga_clear(&ectx.ec_funcrefs);
Bram Moolenaarf112f302020-12-20 17:47:52 +01005331 }
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005332
Bram Moolenaaree8580e2020-08-28 17:19:07 +02005333 estack_pop();
5334 current_sctx = save_current_sctx;
5335
Bram Moolenaar2336c372021-12-06 15:06:54 +00005336 if (--ufunc->uf_calls <= 0 && ufunc->uf_refcount <= 0)
5337 // Function was unreferenced while being used, free it now.
5338 func_clear_free(ufunc, FALSE);
Bram Moolenaarc970e422021-03-17 15:03:04 +01005339
Bram Moolenaar352134b2020-10-17 22:04:08 +02005340 if (*msg_list != NULL && saved_msg_list != NULL)
5341 {
5342 msglist_T **plist = saved_msg_list;
5343
5344 // Append entries from the current msg_list (uncaught exceptions) to
5345 // the saved msg_list.
5346 while (*plist != NULL)
5347 plist = &(*plist)->next;
5348
5349 *plist = *msg_list;
5350 }
5351 msg_list = saved_msg_list;
5352
Bram Moolenaar4c137212021-04-19 16:48:48 +02005353 if (ectx.ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar02194d22020-10-24 23:08:38 +02005354 {
5355 cmdmod.cmod_filter_regmatch.regprog = NULL;
5356 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005357 cmdmod = ectx.ec_funclocal.floc_save_cmdmod;
Bram Moolenaar02194d22020-10-24 23:08:38 +02005358 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +01005359 emsg_silent_def = save_emsg_silent_def;
5360 did_emsg_def += save_did_emsg_def;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02005361
Bram Moolenaaree8580e2020-08-28 17:19:07 +02005362failed_early:
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02005363 // Free all local variables, but not arguments.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005364 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
5365 clear_tv(STACK_TV(idx));
Bram Moolenaare99d4222021-06-13 14:01:26 +02005366 ex_nesting_level = orig_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02005367
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005368 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01005369 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005370 if (ectx.ec_outer_ref != NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +01005371 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005372 if (ectx.ec_outer_ref->or_outer_allocated)
5373 vim_free(ectx.ec_outer_ref->or_outer);
5374 partial_unref(ectx.ec_outer_ref->or_partial);
5375 vim_free(ectx.ec_outer_ref);
Bram Moolenaar0186e582021-01-10 18:33:11 +01005376 }
5377
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02005378 // Not sure if this is necessary.
5379 suppress_errthrow = save_suppress_errthrow;
5380
Bram Moolenaarb5841b92021-07-15 18:09:53 +02005381 if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before
5382 && !need_rethrow)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005383 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02005384 printable_func_name(ufunc));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01005385 funcdepth_restore(orig_funcdepth);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005386 return ret;
5387}
5388
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005389/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02005390 * List instructions "instr" up to "instr_count" or until ISN_FINISH.
5391 * "ufunc" has the source lines, NULL for the instructions of ISN_SUBSTITUTE.
5392 * "pfx" is prefixed to every line.
5393 */
5394 static void
5395list_instructions(char *pfx, isn_T *instr, int instr_count, ufunc_T *ufunc)
5396{
5397 int line_idx = 0;
5398 int prev_current = 0;
5399 int current;
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02005400 int def_arg_idx = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005401
5402 for (current = 0; current < instr_count; ++current)
5403 {
5404 isn_T *iptr = &instr[current];
5405 char *line;
5406
5407 if (ufunc != NULL)
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02005408 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005409 while (line_idx < iptr->isn_lnum
5410 && line_idx < ufunc->uf_lines.ga_len)
5411 {
5412 if (current > prev_current)
5413 {
5414 msg_puts("\n\n");
5415 prev_current = current;
5416 }
5417 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
5418 if (line != NULL)
5419 msg(line);
5420 }
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02005421 if (iptr->isn_type == ISN_JUMP_IF_ARG_SET)
5422 {
5423 int first_def_arg = ufunc->uf_args.ga_len
5424 - ufunc->uf_def_args.ga_len;
5425
5426 if (def_arg_idx > 0)
5427 msg_puts("\n\n");
5428 msg_start();
5429 msg_puts(" ");
5430 msg_puts(((char **)(ufunc->uf_args.ga_data))[
5431 first_def_arg + def_arg_idx]);
5432 msg_puts(" = ");
5433 msg_puts(((char **)(ufunc->uf_def_args.ga_data))[def_arg_idx++]);
5434 msg_clr_eos();
5435 msg_end();
5436 }
5437 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005438
5439 switch (iptr->isn_type)
5440 {
5441 case ISN_EXEC:
5442 smsg("%s%4d EXEC %s", pfx, current, iptr->isn_arg.string);
5443 break;
Bram Moolenaar20677332021-06-06 17:02:53 +02005444 case ISN_EXEC_SPLIT:
5445 smsg("%s%4d EXEC_SPLIT %s", pfx, current, iptr->isn_arg.string);
5446 break;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00005447 case ISN_EXECRANGE:
5448 smsg("%s%4d EXECRANGE %s", pfx, current, iptr->isn_arg.string);
5449 break;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005450 case ISN_LEGACY_EVAL:
5451 smsg("%s%4d EVAL legacy %s", pfx, current,
5452 iptr->isn_arg.string);
5453 break;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02005454 case ISN_REDIRSTART:
5455 smsg("%s%4d REDIR", pfx, current);
5456 break;
5457 case ISN_REDIREND:
5458 smsg("%s%4d REDIR END%s", pfx, current,
5459 iptr->isn_arg.number ? " append" : "");
5460 break;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005461 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005462#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005463 smsg("%s%4d CEXPR pre %s", pfx, current,
5464 cexpr_get_auname(iptr->isn_arg.number));
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005465#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005466 break;
5467 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005468#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005469 {
5470 cexprref_T *cer = iptr->isn_arg.cexpr.cexpr_ref;
5471
5472 smsg("%s%4d CEXPR core %s%s \"%s\"", pfx, current,
5473 cexpr_get_auname(cer->cer_cmdidx),
5474 cer->cer_forceit ? "!" : "",
5475 cer->cer_cmdline);
5476 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005477#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005478 break;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005479 case ISN_INSTR:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01005480 smsg("%s%4d INSTR", pfx, current);
5481 list_instructions(" ", iptr->isn_arg.instr, INT_MAX, NULL);
5482 msg(" -------------");
5483 break;
5484 case ISN_SOURCE:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005485 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01005486 scriptitem_T *si = SCRIPT_ITEM(iptr->isn_arg.number);
5487
5488 smsg("%s%4d SOURCE %s", pfx, current, si->sn_name);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005489 }
5490 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005491 case ISN_SUBSTITUTE:
5492 {
5493 subs_T *subs = &iptr->isn_arg.subs;
5494
5495 smsg("%s%4d SUBSTITUTE %s", pfx, current, subs->subs_cmd);
5496 list_instructions(" ", subs->subs_instr, INT_MAX, NULL);
5497 msg(" -------------");
5498 }
5499 break;
5500 case ISN_EXECCONCAT:
5501 smsg("%s%4d EXECCONCAT %lld", pfx, current,
5502 (varnumber_T)iptr->isn_arg.number);
5503 break;
5504 case ISN_ECHO:
5505 {
5506 echo_T *echo = &iptr->isn_arg.echo;
5507
5508 smsg("%s%4d %s %d", pfx, current,
5509 echo->echo_with_white ? "ECHO" : "ECHON",
5510 echo->echo_count);
5511 }
5512 break;
5513 case ISN_EXECUTE:
5514 smsg("%s%4d EXECUTE %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02005515 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005516 break;
5517 case ISN_ECHOMSG:
5518 smsg("%s%4d ECHOMSG %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02005519 (varnumber_T)(iptr->isn_arg.number));
5520 break;
5521 case ISN_ECHOCONSOLE:
5522 smsg("%s%4d ECHOCONSOLE %lld", pfx, current,
5523 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005524 break;
5525 case ISN_ECHOERR:
5526 smsg("%s%4d ECHOERR %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02005527 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005528 break;
5529 case ISN_LOAD:
5530 {
5531 if (iptr->isn_arg.number < 0)
5532 smsg("%s%4d LOAD arg[%lld]", pfx, current,
5533 (varnumber_T)(iptr->isn_arg.number
5534 + STACK_FRAME_SIZE));
5535 else
5536 smsg("%s%4d LOAD $%lld", pfx, current,
5537 (varnumber_T)(iptr->isn_arg.number));
5538 }
5539 break;
5540 case ISN_LOADOUTER:
5541 {
5542 if (iptr->isn_arg.number < 0)
5543 smsg("%s%4d LOADOUTER level %d arg[%d]", pfx, current,
5544 iptr->isn_arg.outer.outer_depth,
5545 iptr->isn_arg.outer.outer_idx
5546 + STACK_FRAME_SIZE);
5547 else
5548 smsg("%s%4d LOADOUTER level %d $%d", pfx, current,
5549 iptr->isn_arg.outer.outer_depth,
5550 iptr->isn_arg.outer.outer_idx);
5551 }
5552 break;
5553 case ISN_LOADV:
5554 smsg("%s%4d LOADV v:%s", pfx, current,
5555 get_vim_var_name(iptr->isn_arg.number));
5556 break;
5557 case ISN_LOADSCRIPT:
5558 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005559 scriptref_T *sref = iptr->isn_arg.script.scriptref;
5560 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
5561 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005562
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005563 sv = get_script_svar(sref, -1);
5564 if (sv == NULL)
5565 smsg("%s%4d LOADSCRIPT [deleted] from %s",
5566 pfx, current, si->sn_name);
5567 else
5568 smsg("%s%4d LOADSCRIPT %s-%d from %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02005569 sv->sv_name,
5570 sref->sref_idx,
5571 si->sn_name);
5572 }
5573 break;
5574 case ISN_LOADS:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01005575 case ISN_LOADEXPORT:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005576 {
5577 scriptitem_T *si = SCRIPT_ITEM(
5578 iptr->isn_arg.loadstore.ls_sid);
5579
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01005580 smsg("%s%4d %s s:%s from %s", pfx, current,
5581 iptr->isn_type == ISN_LOADS ? "LOADS"
5582 : "LOADEXPORT",
5583 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005584 }
5585 break;
5586 case ISN_LOADAUTO:
5587 smsg("%s%4d LOADAUTO %s", pfx, current, iptr->isn_arg.string);
5588 break;
5589 case ISN_LOADG:
5590 smsg("%s%4d LOADG g:%s", pfx, current, iptr->isn_arg.string);
5591 break;
5592 case ISN_LOADB:
5593 smsg("%s%4d LOADB b:%s", pfx, current, iptr->isn_arg.string);
5594 break;
5595 case ISN_LOADW:
5596 smsg("%s%4d LOADW w:%s", pfx, current, iptr->isn_arg.string);
5597 break;
5598 case ISN_LOADT:
5599 smsg("%s%4d LOADT t:%s", pfx, current, iptr->isn_arg.string);
5600 break;
5601 case ISN_LOADGDICT:
5602 smsg("%s%4d LOAD g:", pfx, current);
5603 break;
5604 case ISN_LOADBDICT:
5605 smsg("%s%4d LOAD b:", pfx, current);
5606 break;
5607 case ISN_LOADWDICT:
5608 smsg("%s%4d LOAD w:", pfx, current);
5609 break;
5610 case ISN_LOADTDICT:
5611 smsg("%s%4d LOAD t:", pfx, current);
5612 break;
5613 case ISN_LOADOPT:
5614 smsg("%s%4d LOADOPT %s", pfx, current, iptr->isn_arg.string);
5615 break;
5616 case ISN_LOADENV:
5617 smsg("%s%4d LOADENV %s", pfx, current, iptr->isn_arg.string);
5618 break;
5619 case ISN_LOADREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005620 smsg("%s%4d LOADREG @%c", pfx, current,
5621 (int)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005622 break;
5623
5624 case ISN_STORE:
5625 if (iptr->isn_arg.number < 0)
5626 smsg("%s%4d STORE arg[%lld]", pfx, current,
5627 iptr->isn_arg.number + STACK_FRAME_SIZE);
5628 else
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005629 smsg("%s%4d STORE $%lld", pfx, current,
5630 iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005631 break;
5632 case ISN_STOREOUTER:
5633 {
5634 if (iptr->isn_arg.number < 0)
5635 smsg("%s%4d STOREOUTEr level %d arg[%d]", pfx, current,
5636 iptr->isn_arg.outer.outer_depth,
5637 iptr->isn_arg.outer.outer_idx + STACK_FRAME_SIZE);
5638 else
5639 smsg("%s%4d STOREOUTER level %d $%d", pfx, current,
5640 iptr->isn_arg.outer.outer_depth,
5641 iptr->isn_arg.outer.outer_idx);
5642 }
5643 break;
5644 case ISN_STOREV:
5645 smsg("%s%4d STOREV v:%s", pfx, current,
5646 get_vim_var_name(iptr->isn_arg.number));
5647 break;
5648 case ISN_STOREAUTO:
5649 smsg("%s%4d STOREAUTO %s", pfx, current, iptr->isn_arg.string);
5650 break;
5651 case ISN_STOREG:
5652 smsg("%s%4d STOREG %s", pfx, current, iptr->isn_arg.string);
5653 break;
5654 case ISN_STOREB:
5655 smsg("%s%4d STOREB %s", pfx, current, iptr->isn_arg.string);
5656 break;
5657 case ISN_STOREW:
5658 smsg("%s%4d STOREW %s", pfx, current, iptr->isn_arg.string);
5659 break;
5660 case ISN_STORET:
5661 smsg("%s%4d STORET %s", pfx, current, iptr->isn_arg.string);
5662 break;
5663 case ISN_STORES:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01005664 case ISN_STOREEXPORT:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005665 {
5666 scriptitem_T *si = SCRIPT_ITEM(
5667 iptr->isn_arg.loadstore.ls_sid);
5668
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01005669 smsg("%s%4d %s %s in %s", pfx, current,
5670 iptr->isn_type == ISN_STORES
5671 ? "STORES" : "STOREEXPORT",
Bram Moolenaar4c137212021-04-19 16:48:48 +02005672 iptr->isn_arg.loadstore.ls_name, si->sn_name);
5673 }
5674 break;
5675 case ISN_STORESCRIPT:
5676 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005677 scriptref_T *sref = iptr->isn_arg.script.scriptref;
5678 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
5679 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005680
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005681 sv = get_script_svar(sref, -1);
5682 if (sv == NULL)
5683 smsg("%s%4d STORESCRIPT [deleted] in %s",
5684 pfx, current, si->sn_name);
5685 else
5686 smsg("%s%4d STORESCRIPT %s-%d in %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02005687 sv->sv_name,
5688 sref->sref_idx,
5689 si->sn_name);
5690 }
5691 break;
5692 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00005693 case ISN_STOREFUNCOPT:
5694 smsg("%s%4d %s &%s", pfx, current,
5695 iptr->isn_type == ISN_STOREOPT ? "STOREOPT" : "STOREFUNCOPT",
Bram Moolenaar4c137212021-04-19 16:48:48 +02005696 iptr->isn_arg.storeopt.so_name);
5697 break;
5698 case ISN_STOREENV:
5699 smsg("%s%4d STOREENV $%s", pfx, current, iptr->isn_arg.string);
5700 break;
5701 case ISN_STOREREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005702 smsg("%s%4d STOREREG @%c", pfx, current,
5703 (int)iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005704 break;
5705 case ISN_STORENR:
5706 smsg("%s%4d STORE %lld in $%d", pfx, current,
5707 iptr->isn_arg.storenr.stnr_val,
5708 iptr->isn_arg.storenr.stnr_idx);
5709 break;
5710
5711 case ISN_STOREINDEX:
5712 smsg("%s%4d STOREINDEX %s", pfx, current,
5713 vartype_name(iptr->isn_arg.vartype));
5714 break;
5715
5716 case ISN_STORERANGE:
5717 smsg("%s%4d STORERANGE", pfx, current);
5718 break;
5719
5720 // constants
5721 case ISN_PUSHNR:
5722 smsg("%s%4d PUSHNR %lld", pfx, current,
5723 (varnumber_T)(iptr->isn_arg.number));
5724 break;
5725 case ISN_PUSHBOOL:
5726 case ISN_PUSHSPEC:
5727 smsg("%s%4d PUSH %s", pfx, current,
5728 get_var_special_name(iptr->isn_arg.number));
5729 break;
5730 case ISN_PUSHF:
5731#ifdef FEAT_FLOAT
5732 smsg("%s%4d PUSHF %g", pfx, current, iptr->isn_arg.fnumber);
5733#endif
5734 break;
5735 case ISN_PUSHS:
5736 smsg("%s%4d PUSHS \"%s\"", pfx, current, iptr->isn_arg.string);
5737 break;
5738 case ISN_PUSHBLOB:
5739 {
5740 char_u *r;
5741 char_u numbuf[NUMBUFLEN];
5742 char_u *tofree;
5743
5744 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
5745 smsg("%s%4d PUSHBLOB %s", pfx, current, r);
5746 vim_free(tofree);
5747 }
5748 break;
5749 case ISN_PUSHFUNC:
5750 {
5751 char *name = (char *)iptr->isn_arg.string;
5752
5753 smsg("%s%4d PUSHFUNC \"%s\"", pfx, current,
5754 name == NULL ? "[none]" : name);
5755 }
5756 break;
5757 case ISN_PUSHCHANNEL:
5758#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar397a87a2022-03-20 21:14:15 +00005759 smsg("%s%4d PUSHCHANNEL 0", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005760#endif
5761 break;
5762 case ISN_PUSHJOB:
5763#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar397a87a2022-03-20 21:14:15 +00005764 smsg("%s%4d PUSHJOB \"no process\"", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005765#endif
5766 break;
5767 case ISN_PUSHEXC:
5768 smsg("%s%4d PUSH v:exception", pfx, current);
5769 break;
Bram Moolenaar06b77222022-01-25 15:51:56 +00005770 case ISN_AUTOLOAD:
5771 smsg("%s%4d AUTOLOAD %s", pfx, current, iptr->isn_arg.string);
5772 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005773 case ISN_UNLET:
5774 smsg("%s%4d UNLET%s %s", pfx, current,
5775 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
5776 iptr->isn_arg.unlet.ul_name);
5777 break;
5778 case ISN_UNLETENV:
5779 smsg("%s%4d UNLETENV%s $%s", pfx, current,
5780 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
5781 iptr->isn_arg.unlet.ul_name);
5782 break;
5783 case ISN_UNLETINDEX:
5784 smsg("%s%4d UNLETINDEX", pfx, current);
5785 break;
5786 case ISN_UNLETRANGE:
5787 smsg("%s%4d UNLETRANGE", pfx, current);
5788 break;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02005789 case ISN_LOCKUNLOCK:
5790 smsg("%s%4d LOCKUNLOCK %s", pfx, current, iptr->isn_arg.string);
5791 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005792 case ISN_LOCKCONST:
5793 smsg("%s%4d LOCKCONST", pfx, current);
5794 break;
5795 case ISN_NEWLIST:
5796 smsg("%s%4d NEWLIST size %lld", pfx, current,
5797 (varnumber_T)(iptr->isn_arg.number));
5798 break;
5799 case ISN_NEWDICT:
5800 smsg("%s%4d NEWDICT size %lld", pfx, current,
5801 (varnumber_T)(iptr->isn_arg.number));
5802 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00005803 case ISN_NEWPARTIAL:
5804 smsg("%s%4d NEWPARTIAL", pfx, current);
5805 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005806
5807 // function call
5808 case ISN_BCALL:
5809 {
5810 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
5811
5812 smsg("%s%4d BCALL %s(argc %d)", pfx, current,
5813 internal_func_name(cbfunc->cbf_idx),
5814 cbfunc->cbf_argcount);
5815 }
5816 break;
5817 case ISN_DCALL:
5818 {
5819 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
5820 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
5821 + cdfunc->cdf_idx;
5822
5823 smsg("%s%4d DCALL %s(argc %d)", pfx, current,
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005824 printable_func_name(df->df_ufunc),
5825 cdfunc->cdf_argcount);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005826 }
5827 break;
5828 case ISN_UCALL:
5829 {
5830 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
5831
5832 smsg("%s%4d UCALL %s(argc %d)", pfx, current,
5833 cufunc->cuf_name, cufunc->cuf_argcount);
5834 }
5835 break;
5836 case ISN_PCALL:
5837 {
5838 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
5839
5840 smsg("%s%4d PCALL%s (argc %d)", pfx, current,
5841 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
5842 }
5843 break;
5844 case ISN_PCALL_END:
5845 smsg("%s%4d PCALL end", pfx, current);
5846 break;
5847 case ISN_RETURN:
5848 smsg("%s%4d RETURN", pfx, current);
5849 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02005850 case ISN_RETURN_VOID:
5851 smsg("%s%4d RETURN void", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005852 break;
5853 case ISN_FUNCREF:
5854 {
5855 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar38453522021-11-28 22:00:12 +00005856 char_u *name;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005857
Bram Moolenaar38453522021-11-28 22:00:12 +00005858 if (funcref->fr_func_name == NULL)
5859 {
5860 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
5861 + funcref->fr_dfunc_idx;
5862 name = df->df_ufunc->uf_name;
5863 }
5864 else
5865 name = funcref->fr_func_name;
5866 smsg("%s%4d FUNCREF %s", pfx, current, name);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005867 }
5868 break;
5869
5870 case ISN_NEWFUNC:
5871 {
5872 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
5873
5874 smsg("%s%4d NEWFUNC %s %s", pfx, current,
5875 newfunc->nf_lambda, newfunc->nf_global);
5876 }
5877 break;
5878
5879 case ISN_DEF:
5880 {
5881 char_u *name = iptr->isn_arg.string;
5882
5883 smsg("%s%4d DEF %s", pfx, current,
5884 name == NULL ? (char_u *)"" : name);
5885 }
5886 break;
5887
5888 case ISN_JUMP:
5889 {
5890 char *when = "?";
5891
5892 switch (iptr->isn_arg.jump.jump_when)
5893 {
5894 case JUMP_ALWAYS:
5895 when = "JUMP";
5896 break;
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005897 case JUMP_NEVER:
5898 iemsg("JUMP_NEVER should not be used");
5899 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005900 case JUMP_AND_KEEP_IF_TRUE:
5901 when = "JUMP_AND_KEEP_IF_TRUE";
5902 break;
5903 case JUMP_IF_FALSE:
5904 when = "JUMP_IF_FALSE";
5905 break;
5906 case JUMP_AND_KEEP_IF_FALSE:
5907 when = "JUMP_AND_KEEP_IF_FALSE";
5908 break;
5909 case JUMP_IF_COND_FALSE:
5910 when = "JUMP_IF_COND_FALSE";
5911 break;
5912 case JUMP_IF_COND_TRUE:
5913 when = "JUMP_IF_COND_TRUE";
5914 break;
5915 }
5916 smsg("%s%4d %s -> %d", pfx, current, when,
5917 iptr->isn_arg.jump.jump_where);
5918 }
5919 break;
5920
5921 case ISN_JUMP_IF_ARG_SET:
5922 smsg("%s%4d JUMP_IF_ARG_SET arg[%d] -> %d", pfx, current,
5923 iptr->isn_arg.jumparg.jump_arg_off + STACK_FRAME_SIZE,
5924 iptr->isn_arg.jump.jump_where);
5925 break;
5926
5927 case ISN_FOR:
5928 {
5929 forloop_T *forloop = &iptr->isn_arg.forloop;
5930
5931 smsg("%s%4d FOR $%d -> %d", pfx, current,
5932 forloop->for_idx, forloop->for_end);
5933 }
5934 break;
5935
5936 case ISN_TRY:
5937 {
Bram Moolenaar0d807102021-12-21 09:42:09 +00005938 try_T *try = &iptr->isn_arg.tryref;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005939
5940 if (try->try_ref->try_finally == 0)
5941 smsg("%s%4d TRY catch -> %d, endtry -> %d",
5942 pfx, current,
5943 try->try_ref->try_catch,
5944 try->try_ref->try_endtry);
5945 else
5946 smsg("%s%4d TRY catch -> %d, finally -> %d, endtry -> %d",
5947 pfx, current,
5948 try->try_ref->try_catch,
5949 try->try_ref->try_finally,
5950 try->try_ref->try_endtry);
5951 }
5952 break;
5953 case ISN_CATCH:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005954 smsg("%s%4d CATCH", pfx, current);
5955 break;
5956 case ISN_TRYCONT:
5957 {
5958 trycont_T *trycont = &iptr->isn_arg.trycont;
5959
5960 smsg("%s%4d TRY-CONTINUE %d level%s -> %d", pfx, current,
5961 trycont->tct_levels,
5962 trycont->tct_levels == 1 ? "" : "s",
5963 trycont->tct_where);
5964 }
5965 break;
5966 case ISN_FINALLY:
5967 smsg("%s%4d FINALLY", pfx, current);
5968 break;
5969 case ISN_ENDTRY:
5970 smsg("%s%4d ENDTRY", pfx, current);
5971 break;
5972 case ISN_THROW:
5973 smsg("%s%4d THROW", pfx, current);
5974 break;
5975
5976 // expression operations on number
5977 case ISN_OPNR:
5978 case ISN_OPFLOAT:
5979 case ISN_OPANY:
5980 {
5981 char *what;
5982 char *ins;
5983
5984 switch (iptr->isn_arg.op.op_type)
5985 {
5986 case EXPR_MULT: what = "*"; break;
5987 case EXPR_DIV: what = "/"; break;
5988 case EXPR_REM: what = "%"; break;
5989 case EXPR_SUB: what = "-"; break;
5990 case EXPR_ADD: what = "+"; break;
5991 default: what = "???"; break;
5992 }
5993 switch (iptr->isn_type)
5994 {
5995 case ISN_OPNR: ins = "OPNR"; break;
5996 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
5997 case ISN_OPANY: ins = "OPANY"; break;
5998 default: ins = "???"; break;
5999 }
6000 smsg("%s%4d %s %s", pfx, current, ins, what);
6001 }
6002 break;
6003
6004 case ISN_COMPAREBOOL:
6005 case ISN_COMPARESPECIAL:
Bram Moolenaar7a222242022-03-01 19:23:24 +00006006 case ISN_COMPARENULL:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006007 case ISN_COMPARENR:
6008 case ISN_COMPAREFLOAT:
6009 case ISN_COMPARESTRING:
6010 case ISN_COMPAREBLOB:
6011 case ISN_COMPARELIST:
6012 case ISN_COMPAREDICT:
6013 case ISN_COMPAREFUNC:
6014 case ISN_COMPAREANY:
6015 {
6016 char *p;
6017 char buf[10];
6018 char *type;
6019
6020 switch (iptr->isn_arg.op.op_type)
6021 {
6022 case EXPR_EQUAL: p = "=="; break;
6023 case EXPR_NEQUAL: p = "!="; break;
6024 case EXPR_GREATER: p = ">"; break;
6025 case EXPR_GEQUAL: p = ">="; break;
6026 case EXPR_SMALLER: p = "<"; break;
6027 case EXPR_SEQUAL: p = "<="; break;
6028 case EXPR_MATCH: p = "=~"; break;
6029 case EXPR_IS: p = "is"; break;
6030 case EXPR_ISNOT: p = "isnot"; break;
6031 case EXPR_NOMATCH: p = "!~"; break;
6032 default: p = "???"; break;
6033 }
6034 STRCPY(buf, p);
6035 if (iptr->isn_arg.op.op_ic == TRUE)
6036 strcat(buf, "?");
6037 switch(iptr->isn_type)
6038 {
6039 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
6040 case ISN_COMPARESPECIAL:
6041 type = "COMPARESPECIAL"; break;
Bram Moolenaar7a222242022-03-01 19:23:24 +00006042 case ISN_COMPARENULL: type = "COMPARENULL"; break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006043 case ISN_COMPARENR: type = "COMPARENR"; break;
6044 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
6045 case ISN_COMPARESTRING:
6046 type = "COMPARESTRING"; break;
6047 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
6048 case ISN_COMPARELIST: type = "COMPARELIST"; break;
6049 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
6050 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
6051 case ISN_COMPAREANY: type = "COMPAREANY"; break;
6052 default: type = "???"; break;
6053 }
6054
6055 smsg("%s%4d %s %s", pfx, current, type, buf);
6056 }
6057 break;
6058
6059 case ISN_ADDLIST: smsg("%s%4d ADDLIST", pfx, current); break;
6060 case ISN_ADDBLOB: smsg("%s%4d ADDBLOB", pfx, current); break;
6061
6062 // expression operations
6063 case ISN_CONCAT: smsg("%s%4d CONCAT", pfx, current); break;
6064 case ISN_STRINDEX: smsg("%s%4d STRINDEX", pfx, current); break;
6065 case ISN_STRSLICE: smsg("%s%4d STRSLICE", pfx, current); break;
6066 case ISN_BLOBINDEX: smsg("%s%4d BLOBINDEX", pfx, current); break;
6067 case ISN_BLOBSLICE: smsg("%s%4d BLOBSLICE", pfx, current); break;
6068 case ISN_LISTAPPEND: smsg("%s%4d LISTAPPEND", pfx, current); break;
6069 case ISN_BLOBAPPEND: smsg("%s%4d BLOBAPPEND", pfx, current); break;
6070 case ISN_LISTINDEX: smsg("%s%4d LISTINDEX", pfx, current); break;
6071 case ISN_LISTSLICE: smsg("%s%4d LISTSLICE", pfx, current); break;
6072 case ISN_ANYINDEX: smsg("%s%4d ANYINDEX", pfx, current); break;
6073 case ISN_ANYSLICE: smsg("%s%4d ANYSLICE", pfx, current); break;
6074 case ISN_SLICE: smsg("%s%4d SLICE %lld",
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02006075 pfx, current, iptr->isn_arg.number); break;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02006076 case ISN_GETITEM: smsg("%s%4d ITEM %lld%s", pfx, current,
6077 iptr->isn_arg.getitem.gi_index,
6078 iptr->isn_arg.getitem.gi_with_op ?
6079 " with op" : ""); break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006080 case ISN_MEMBER: smsg("%s%4d MEMBER", pfx, current); break;
6081 case ISN_STRINGMEMBER: smsg("%s%4d MEMBER %s", pfx, current,
6082 iptr->isn_arg.string); break;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02006083 case ISN_CLEARDICT: smsg("%s%4d CLEARDICT", pfx, current); break;
6084 case ISN_USEDICT: smsg("%s%4d USEDICT", pfx, current); break;
6085
Bram Moolenaar4c137212021-04-19 16:48:48 +02006086 case ISN_NEGATENR: smsg("%s%4d NEGATENR", pfx, current); break;
6087
6088 case ISN_CHECKNR: smsg("%s%4d CHECKNR", pfx, current); break;
6089 case ISN_CHECKTYPE:
6090 {
6091 checktype_T *ct = &iptr->isn_arg.type;
6092 char *tofree;
6093
6094 if (ct->ct_arg_idx == 0)
6095 smsg("%s%4d CHECKTYPE %s stack[%d]", pfx, current,
6096 type_name(ct->ct_type, &tofree),
6097 (int)ct->ct_off);
6098 else
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02006099 smsg("%s%4d CHECKTYPE %s stack[%d] arg %d",
6100 pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02006101 type_name(ct->ct_type, &tofree),
6102 (int)ct->ct_off,
6103 (int)ct->ct_arg_idx);
6104 vim_free(tofree);
6105 break;
6106 }
6107 case ISN_CHECKLEN: smsg("%s%4d CHECKLEN %s%d", pfx, current,
6108 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
6109 iptr->isn_arg.checklen.cl_min_len);
6110 break;
6111 case ISN_SETTYPE:
6112 {
6113 char *tofree;
6114
6115 smsg("%s%4d SETTYPE %s", pfx, current,
6116 type_name(iptr->isn_arg.type.ct_type, &tofree));
6117 vim_free(tofree);
6118 break;
6119 }
6120 case ISN_COND2BOOL: smsg("%s%4d COND2BOOL", pfx, current); break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006121 case ISN_2BOOL: if (iptr->isn_arg.tobool.invert)
6122 smsg("%s%4d INVERT %d (!val)", pfx, current,
6123 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006124 else
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006125 smsg("%s%4d 2BOOL %d (!!val)", pfx, current,
6126 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006127 break;
6128 case ISN_2STRING: smsg("%s%4d 2STRING stack[%lld]", pfx, current,
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006129 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006130 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006131 case ISN_2STRING_ANY: smsg("%s%4d 2STRING_ANY stack[%lld]",
6132 pfx, current,
6133 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006134 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006135 case ISN_RANGE: smsg("%s%4d RANGE %s", pfx, current,
6136 iptr->isn_arg.string);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006137 break;
6138 case ISN_PUT:
6139 if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE)
6140 smsg("%s%4d PUT %c above range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006141 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006142 else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE)
6143 smsg("%s%4d PUT %c range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006144 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006145 else
6146 smsg("%s%4d PUT %c %ld", pfx, current,
6147 iptr->isn_arg.put.put_regname,
6148 (long)iptr->isn_arg.put.put_lnum);
6149 break;
6150
Bram Moolenaar4c137212021-04-19 16:48:48 +02006151 case ISN_CMDMOD:
6152 {
6153 char_u *buf;
6154 size_t len = produce_cmdmods(
6155 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
6156
6157 buf = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02006158 if (likely(buf != NULL))
Bram Moolenaar4c137212021-04-19 16:48:48 +02006159 {
6160 (void)produce_cmdmods(
6161 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
6162 smsg("%s%4d CMDMOD %s", pfx, current, buf);
6163 vim_free(buf);
6164 }
6165 break;
6166 }
6167 case ISN_CMDMOD_REV: smsg("%s%4d CMDMOD_REV", pfx, current); break;
6168
6169 case ISN_PROF_START:
Bram Moolenaare99d4222021-06-13 14:01:26 +02006170 smsg("%s%4d PROFILE START line %d", pfx, current,
6171 iptr->isn_lnum);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006172 break;
6173
6174 case ISN_PROF_END:
6175 smsg("%s%4d PROFILE END", pfx, current);
6176 break;
6177
Bram Moolenaare99d4222021-06-13 14:01:26 +02006178 case ISN_DEBUG:
Bram Moolenaar8cec9272021-06-23 20:20:53 +02006179 smsg("%s%4d DEBUG line %d-%d varcount %lld", pfx, current,
6180 iptr->isn_arg.debug.dbg_break_lnum + 1,
6181 iptr->isn_lnum,
6182 iptr->isn_arg.debug.dbg_var_names_len);
Bram Moolenaare99d4222021-06-13 14:01:26 +02006183 break;
6184
Bram Moolenaar4c137212021-04-19 16:48:48 +02006185 case ISN_UNPACK: smsg("%s%4d UNPACK %d%s", pfx, current,
6186 iptr->isn_arg.unpack.unp_count,
6187 iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : "");
6188 break;
6189 case ISN_SHUFFLE: smsg("%s%4d SHUFFLE %d up %d", pfx, current,
6190 iptr->isn_arg.shuffle.shfl_item,
6191 iptr->isn_arg.shuffle.shfl_up);
6192 break;
6193 case ISN_DROP: smsg("%s%4d DROP", pfx, current); break;
6194
6195 case ISN_FINISH: // End of list of instructions for ISN_SUBSTITUTE.
6196 return;
6197 }
6198
6199 out_flush(); // output one line at a time
6200 ui_breakcheck();
6201 if (got_int)
6202 break;
6203 }
6204}
6205
6206/*
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02006207 * Handle command line completion for the :disassemble command.
6208 */
6209 void
6210set_context_in_disassemble_cmd(expand_T *xp, char_u *arg)
6211{
6212 char_u *p;
6213
6214 // Default: expand user functions, "debug" and "profile"
6215 xp->xp_context = EXPAND_DISASSEMBLE;
6216 xp->xp_pattern = arg;
6217
6218 // first argument already typed: only user function names
6219 if (*arg != NUL && *(p = skiptowhite(arg)) != NUL)
6220 {
6221 xp->xp_context = EXPAND_USER_FUNC;
6222 xp->xp_pattern = skipwhite(p);
6223 }
6224}
6225
6226/*
6227 * Function given to ExpandGeneric() to obtain the list of :disassemble
6228 * arguments.
6229 */
6230 char_u *
6231get_disassemble_argument(expand_T *xp, int idx)
6232{
6233 if (idx == 0)
6234 return (char_u *)"debug";
6235 if (idx == 1)
6236 return (char_u *)"profile";
6237 return get_user_func_name(xp, idx - 2);
6238}
6239
6240/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01006241 * ":disassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01006242 * We don't really need this at runtime, but we do have tests that require it,
6243 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006244 */
6245 void
6246ex_disassemble(exarg_T *eap)
6247{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01006248 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01006249 char_u *fname;
6250 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006251 dfunc_T *dfunc;
6252 isn_T *instr;
Bram Moolenaarb2049902021-01-24 12:53:53 +01006253 int instr_count;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02006254 int is_global = FALSE;
Bram Moolenaare99d4222021-06-13 14:01:26 +02006255 compiletype_T compile_type = CT_NONE;
6256
Bram Moolenaarc7d5fc82021-12-05 17:20:24 +00006257 if (STRNCMP(arg, "profile", 7) == 0 && VIM_ISWHITE(arg[7]))
Bram Moolenaare99d4222021-06-13 14:01:26 +02006258 {
6259 compile_type = CT_PROFILE;
6260 arg = skipwhite(arg + 7);
6261 }
Bram Moolenaarc7d5fc82021-12-05 17:20:24 +00006262 else if (STRNCMP(arg, "debug", 5) == 0 && VIM_ISWHITE(arg[5]))
Bram Moolenaare99d4222021-06-13 14:01:26 +02006263 {
6264 compile_type = CT_DEBUG;
6265 arg = skipwhite(arg + 5);
6266 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006267
Bram Moolenaarbfd65582020-07-13 18:18:00 +02006268 if (STRNCMP(arg, "<lambda>", 8) == 0)
6269 {
6270 arg += 8;
6271 (void)getdigits(&arg);
6272 fname = vim_strnsave(eap->arg, arg - eap->arg);
6273 }
6274 else
6275 fname = trans_function_name(&arg, &is_global, FALSE,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01006276 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL, NULL);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01006277 if (fname == NULL)
6278 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006279 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01006280 return;
6281 }
6282
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00006283 ufunc = find_func(fname, is_global);
Bram Moolenaara26b9702020-04-18 19:53:28 +02006284 if (ufunc == NULL)
6285 {
6286 char_u *p = untrans_function_name(fname);
6287
6288 if (p != NULL)
6289 // Try again without making it script-local.
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00006290 ufunc = find_func(p, FALSE);
Bram Moolenaara26b9702020-04-18 19:53:28 +02006291 }
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01006292 vim_free(fname);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006293 if (ufunc == NULL)
6294 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006295 semsg(_(e_cannot_find_function_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006296 return;
6297 }
Bram Moolenaare99d4222021-06-13 14:01:26 +02006298 if (func_needs_compiling(ufunc, compile_type)
6299 && compile_def_function(ufunc, FALSE, compile_type, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02006300 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006301 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006302 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006303 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006304 return;
6305 }
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006306 msg((char *)printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006307
6308 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
Bram Moolenaare99d4222021-06-13 14:01:26 +02006309 switch (compile_type)
6310 {
6311 case CT_PROFILE:
Bram Moolenaarf002a412021-01-24 13:34:18 +01006312#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02006313 instr = dfunc->df_instr_prof;
6314 instr_count = dfunc->df_instr_prof_count;
6315 break;
Bram Moolenaarf002a412021-01-24 13:34:18 +01006316#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02006317 // FALLTHROUGH
6318 case CT_NONE:
6319 instr = dfunc->df_instr;
6320 instr_count = dfunc->df_instr_count;
6321 break;
6322 case CT_DEBUG:
6323 instr = dfunc->df_instr_debug;
6324 instr_count = dfunc->df_instr_debug_count;
6325 break;
6326 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006327
Bram Moolenaar4c137212021-04-19 16:48:48 +02006328 list_instructions("", instr, instr_count, ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006329}
6330
6331/*
Bram Moolenaar13106602020-10-04 16:06:05 +02006332 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006333 * list, etc. Mostly like what JavaScript does, except that empty list and
6334 * empty dictionary are FALSE.
6335 */
6336 int
6337tv2bool(typval_T *tv)
6338{
6339 switch (tv->v_type)
6340 {
6341 case VAR_NUMBER:
6342 return tv->vval.v_number != 0;
6343 case VAR_FLOAT:
6344#ifdef FEAT_FLOAT
6345 return tv->vval.v_float != 0.0;
6346#else
6347 break;
6348#endif
6349 case VAR_PARTIAL:
6350 return tv->vval.v_partial != NULL;
6351 case VAR_FUNC:
6352 case VAR_STRING:
6353 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
6354 case VAR_LIST:
6355 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
6356 case VAR_DICT:
6357 return tv->vval.v_dict != NULL
6358 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
6359 case VAR_BOOL:
6360 case VAR_SPECIAL:
6361 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
6362 case VAR_JOB:
6363#ifdef FEAT_JOB_CHANNEL
6364 return tv->vval.v_job != NULL;
6365#else
6366 break;
6367#endif
6368 case VAR_CHANNEL:
6369#ifdef FEAT_JOB_CHANNEL
6370 return tv->vval.v_channel != NULL;
6371#else
6372 break;
6373#endif
6374 case VAR_BLOB:
6375 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
6376 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006377 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006378 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006379 case VAR_INSTR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006380 break;
6381 }
6382 return FALSE;
6383}
6384
Bram Moolenaarea2d4072020-11-12 12:08:51 +01006385 void
6386emsg_using_string_as(typval_T *tv, int as_number)
6387{
6388 semsg(_(as_number ? e_using_string_as_number_str
6389 : e_using_string_as_bool_str),
6390 tv->vval.v_string == NULL
6391 ? (char_u *)"" : tv->vval.v_string);
6392}
6393
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006394/*
6395 * If "tv" is a string give an error and return FAIL.
6396 */
6397 int
6398check_not_string(typval_T *tv)
6399{
6400 if (tv->v_type == VAR_STRING)
6401 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +01006402 emsg_using_string_as(tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006403 clear_tv(tv);
6404 return FAIL;
6405 }
6406 return OK;
6407}
6408
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006409
6410#endif // FEAT_EVAL