blob: c5dc30da04322ef202c2482409d1aac093551aa1 [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 Moolenaare1242042021-12-16 20:56:57 +00001015 semsg(_(e_too_many_arguments_for_function_str), ufunc->uf_name);
Bram Moolenaar50824712020-12-20 21:10:17 +01001016 else
Bram Moolenaare1242042021-12-16 20:56:57 +00001017 semsg(_(e_not_enough_arguments_for_function_str),
1018 ufunc->uf_name);
Bram Moolenaar50824712020-12-20 21:10:17 +01001019 return FAIL;
1020 }
1021
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001022 // The function has been compiled, can call it quickly. For a function
1023 // that was defined later: we can call it directly next time.
1024 if (iptr != NULL)
1025 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001026 delete_instr(iptr);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001027 iptr->isn_type = ISN_DCALL;
1028 iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1029 iptr->isn_arg.dfunc.cdf_argcount = argcount;
1030 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001031 return call_dfunc(ufunc->uf_dfunc_idx, pt, argcount, ectx);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001032 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001033
1034 if (call_prepare(argcount, argvars, ectx) == FAIL)
1035 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +02001036 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00001037 funcexe.fe_evaluate = TRUE;
1038 funcexe.fe_selfdict = selfdict != NULL ? selfdict : dict_stack_get_dict();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001039
1040 // Call the user function. Result goes in last position on the stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001041 error = call_user_func_check(ufunc, argcount, argvars,
Bram Moolenaar851f86b2021-12-13 14:26:44 +00001042 STACK_TV_BOT(-1), &funcexe, funcexe.fe_selfdict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001043
1044 // Clear the arguments.
1045 for (idx = 0; idx < argcount; ++idx)
1046 clear_tv(&argvars[idx]);
1047
1048 if (error != FCERR_NONE)
1049 {
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001050 user_func_error(error, ufunc->uf_name, &funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001051 return FAIL;
1052 }
Bram Moolenaar28ee8922020-10-28 20:20:00 +01001053 if (did_emsg > did_emsg_before)
Bram Moolenaared677f52020-08-12 16:38:10 +02001054 // Error other than from calling the function itself.
1055 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001056 return OK;
1057}
1058
1059/*
Bram Moolenaara91a7132021-03-25 21:12:15 +01001060 * If command modifiers were applied restore them.
1061 */
1062 static void
1063may_restore_cmdmod(funclocal_T *funclocal)
1064{
1065 if (funclocal->floc_restore_cmdmod)
1066 {
1067 cmdmod.cmod_filter_regmatch.regprog = NULL;
1068 undo_cmdmod(&cmdmod);
1069 cmdmod = funclocal->floc_save_cmdmod;
1070 funclocal->floc_restore_cmdmod = FALSE;
1071 }
1072}
1073
1074/*
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001075 * Return TRUE if an error was given (not caught in try/catch) or CTRL-C was
1076 * pressed.
Bram Moolenaara1773442020-08-12 15:21:22 +02001077 */
1078 static int
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001079vim9_aborting(int prev_uncaught_emsg)
Bram Moolenaara1773442020-08-12 15:21:22 +02001080{
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001081 return uncaught_emsg > prev_uncaught_emsg || got_int || did_throw;
Bram Moolenaara1773442020-08-12 15:21:22 +02001082}
1083
1084/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001085 * Execute a function by "name".
1086 * This can be a builtin function or a user function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001087 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001088 * Returns FAIL if not found without an error message.
1089 */
1090 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001091call_by_name(
1092 char_u *name,
1093 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001094 ectx_T *ectx,
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001095 isn_T *iptr,
1096 dict_T *selfdict)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001097{
1098 ufunc_T *ufunc;
1099
1100 if (builtin_function(name, -1))
1101 {
1102 int func_idx = find_internal_func(name);
1103
Bram Moolenaar1983f1a2022-02-28 20:55:02 +00001104 if (func_idx < 0) // Impossible?
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001105 return FAIL;
Bram Moolenaar389df252020-07-09 21:20:47 +02001106 if (check_internal_func(func_idx, argcount) < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001107 return FAIL;
1108 return call_bfunc(func_idx, argcount, ectx);
1109 }
1110
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00001111 ufunc = find_func(name, FALSE);
Bram Moolenaara1773442020-08-12 15:21:22 +02001112
1113 if (ufunc == NULL)
1114 {
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001115 int prev_uncaught_emsg = uncaught_emsg;
Bram Moolenaara1773442020-08-12 15:21:22 +02001116
1117 if (script_autoload(name, TRUE))
1118 // loaded a package, search for the function again
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00001119 ufunc = find_func(name, FALSE);
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001120
1121 if (vim9_aborting(prev_uncaught_emsg))
Bram Moolenaara1773442020-08-12 15:21:22 +02001122 return FAIL; // bail out if loading the script caused an error
1123 }
1124
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001125 if (ufunc != NULL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001126 {
Bram Moolenaar6ce46b92021-08-07 15:35:36 +02001127 if (ufunc->uf_arg_types != NULL || ufunc->uf_va_type != NULL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001128 {
1129 int i;
1130 typval_T *argv = STACK_TV_BOT(0) - argcount;
1131
1132 // The function can change at runtime, check that the argument
1133 // types are correct.
1134 for (i = 0; i < argcount; ++i)
1135 {
Bram Moolenaare3ffcd92021-03-08 21:47:13 +01001136 type_T *type = NULL;
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001137
Bram Moolenaar6ce46b92021-08-07 15:35:36 +02001138 if (i < ufunc->uf_args.ga_len && ufunc->uf_arg_types != NULL)
Bram Moolenaare3ffcd92021-03-08 21:47:13 +01001139 type = ufunc->uf_arg_types[i];
1140 else if (ufunc->uf_va_type != NULL)
1141 type = ufunc->uf_va_type->tt_member;
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001142 if (type != NULL && check_typval_arg_type(type,
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001143 &argv[i], NULL, i + 1) == FAIL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001144 return FAIL;
1145 }
1146 }
1147
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001148 return call_ufunc(ufunc, NULL, argcount, ectx, iptr, selfdict);
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001149 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001150
1151 return FAIL;
1152}
1153
1154 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001155call_partial(
1156 typval_T *tv,
1157 int argcount_arg,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001158 ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001159{
Bram Moolenaara90afb92020-07-15 22:38:56 +02001160 int argcount = argcount_arg;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001161 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001162 int called_emsg_before = called_emsg;
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +01001163 int res = FAIL;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001164 dict_T *selfdict = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001165
1166 if (tv->v_type == VAR_PARTIAL)
1167 {
Bram Moolenaara90afb92020-07-15 22:38:56 +02001168 partial_T *pt = tv->vval.v_partial;
1169 int i;
1170
1171 if (pt->pt_argc > 0)
1172 {
1173 // Make space for arguments from the partial, shift the "argcount"
1174 // arguments up.
Bram Moolenaar35578162021-08-02 19:10:38 +02001175 if (GA_GROW_FAILS(&ectx->ec_stack, pt->pt_argc))
Bram Moolenaara90afb92020-07-15 22:38:56 +02001176 return FAIL;
1177 for (i = 1; i <= argcount; ++i)
1178 *STACK_TV_BOT(-i + pt->pt_argc) = *STACK_TV_BOT(-i);
1179 ectx->ec_stack.ga_len += pt->pt_argc;
1180 argcount += pt->pt_argc;
1181
1182 // copy the arguments from the partial onto the stack
1183 for (i = 0; i < pt->pt_argc; ++i)
1184 copy_tv(&pt->pt_argv[i], STACK_TV_BOT(-argcount + i));
1185 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001186 selfdict = pt->pt_dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001187
1188 if (pt->pt_func != NULL)
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001189 return call_ufunc(pt->pt_func, pt, argcount, ectx, NULL, selfdict);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001190
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001191 name = pt->pt_name;
1192 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001193 else if (tv->v_type == VAR_FUNC)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001194 name = tv->vval.v_string;
Bram Moolenaar95006e32020-08-29 17:47:08 +02001195 if (name != NULL)
1196 {
1197 char_u fname_buf[FLEN_FIXED + 1];
1198 char_u *tofree = NULL;
1199 int error = FCERR_NONE;
1200 char_u *fname;
1201
1202 // May need to translate <SNR>123_ to K_SNR.
1203 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
1204 if (error != FCERR_NONE)
1205 res = FAIL;
1206 else
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001207 res = call_by_name(fname, argcount, ectx, NULL, selfdict);
Bram Moolenaar95006e32020-08-29 17:47:08 +02001208 vim_free(tofree);
1209 }
1210
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +01001211 if (res == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001212 {
1213 if (called_emsg == called_emsg_before)
Bram Moolenaare1242042021-12-16 20:56:57 +00001214 semsg(_(e_unknown_function_str),
Bram Moolenaar015f4262020-05-05 21:25:22 +02001215 name == NULL ? (char_u *)"[unknown]" : name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001216 return FAIL;
1217 }
1218 return OK;
1219}
1220
1221/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001222 * Check if "lock" is VAR_LOCKED or VAR_FIXED. If so give an error and return
1223 * TRUE.
1224 */
1225 static int
1226error_if_locked(int lock, char *error)
1227{
1228 if (lock & (VAR_LOCKED | VAR_FIXED))
1229 {
1230 emsg(_(error));
1231 return TRUE;
1232 }
1233 return FALSE;
1234}
1235
1236/*
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001237 * Give an error if "tv" is not a number and return FAIL.
1238 */
1239 static int
1240check_for_number(typval_T *tv)
1241{
1242 if (tv->v_type != VAR_NUMBER)
1243 {
1244 semsg(_(e_expected_str_but_got_str),
1245 vartype_name(VAR_NUMBER), vartype_name(tv->v_type));
1246 return FAIL;
1247 }
1248 return OK;
1249}
1250
1251/*
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001252 * Store "tv" in variable "name".
1253 * This is for s: and g: variables.
1254 */
1255 static void
1256store_var(char_u *name, typval_T *tv)
1257{
1258 funccal_entry_T entry;
Bram Moolenaard877a572021-04-01 19:42:48 +02001259 int flags = ASSIGN_DECL;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001260
Bram Moolenaard877a572021-04-01 19:42:48 +02001261 if (tv->v_lock)
1262 flags |= ASSIGN_CONST;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001263 save_funccal(&entry);
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001264 set_var_const(name, 0, NULL, tv, FALSE, flags, 0);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001265 restore_funccal();
1266}
1267
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001268/*
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001269 * Convert "tv" to a string.
1270 * Return FAIL if not allowed.
1271 */
1272 static int
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001273do_2string(typval_T *tv, int is_2string_any, int tolerant)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001274{
1275 if (tv->v_type != VAR_STRING)
1276 {
1277 char_u *str;
1278
1279 if (is_2string_any)
1280 {
1281 switch (tv->v_type)
1282 {
1283 case VAR_SPECIAL:
1284 case VAR_BOOL:
1285 case VAR_NUMBER:
1286 case VAR_FLOAT:
1287 case VAR_BLOB: break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001288
1289 case VAR_LIST:
1290 if (tolerant)
1291 {
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001292 char_u *s, *e, *p;
1293 garray_T ga;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001294
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001295 ga_init2(&ga, sizeof(char_u *), 1);
1296
1297 // Convert to NL separated items, then
1298 // escape the items and replace the NL with
1299 // a space.
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001300 str = typval2string(tv, TRUE);
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001301 if (str == NULL)
1302 return FAIL;
1303 s = str;
1304 while ((e = vim_strchr(s, '\n')) != NULL)
1305 {
1306 *e = NUL;
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +01001307 p = vim_strsave_fnameescape(s,
1308 VSE_NONE);
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001309 if (p != NULL)
1310 {
1311 ga_concat(&ga, p);
1312 ga_concat(&ga, (char_u *)" ");
1313 vim_free(p);
1314 }
1315 s = e + 1;
1316 }
1317 vim_free(str);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001318 clear_tv(tv);
1319 tv->v_type = VAR_STRING;
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001320 tv->vval.v_string = ga.ga_data;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001321 return OK;
1322 }
1323 // FALLTHROUGH
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001324 default: to_string_error(tv->v_type);
1325 return FAIL;
1326 }
1327 }
Bram Moolenaar34453202021-01-31 13:08:38 +01001328 str = typval_tostring(tv, TRUE);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001329 clear_tv(tv);
1330 tv->v_type = VAR_STRING;
1331 tv->vval.v_string = str;
1332 }
1333 return OK;
1334}
1335
1336/*
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001337 * When the value of "sv" is a null list of dict, allocate it.
1338 */
1339 static void
Bram Moolenaar859cc212022-03-28 15:22:35 +01001340allocate_if_null(svar_T *sv)
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001341{
Bram Moolenaar859cc212022-03-28 15:22:35 +01001342 typval_T *tv = sv->sv_tv;
1343
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001344 switch (tv->v_type)
1345 {
1346 case VAR_LIST:
Bram Moolenaar859cc212022-03-28 15:22:35 +01001347 if (tv->vval.v_list == NULL && sv->sv_type != &t_list_empty)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001348 (void)rettv_list_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001349 break;
1350 case VAR_DICT:
Bram Moolenaar859cc212022-03-28 15:22:35 +01001351 if (tv->vval.v_dict == NULL && sv->sv_type != &t_dict_empty)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001352 (void)rettv_dict_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001353 break;
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02001354 case VAR_BLOB:
Bram Moolenaar859cc212022-03-28 15:22:35 +01001355 if (tv->vval.v_blob == NULL && sv->sv_type != &t_blob_null)
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02001356 (void)rettv_blob_alloc(tv);
1357 break;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001358 default:
1359 break;
1360 }
1361}
Bram Moolenaard3aac292020-04-19 14:32:17 +02001362
Bram Moolenaare7525c52021-01-09 13:20:37 +01001363/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001364 * Return the character "str[index]" where "index" is the character index,
1365 * including composing characters.
1366 * If "index" is out of range NULL is returned.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001367 */
1368 char_u *
1369char_from_string(char_u *str, varnumber_T index)
1370{
1371 size_t nbyte = 0;
1372 varnumber_T nchar = index;
1373 size_t slen;
1374
1375 if (str == NULL)
1376 return NULL;
1377 slen = STRLEN(str);
1378
Bram Moolenaarff871402021-03-26 13:34:05 +01001379 // Do the same as for a list: a negative index counts from the end.
1380 // Optimization to check the first byte to be below 0x80 (and no composing
1381 // character follows) makes this a lot faster.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001382 if (index < 0)
1383 {
1384 int clen = 0;
1385
1386 for (nbyte = 0; nbyte < slen; ++clen)
Bram Moolenaarff871402021-03-26 13:34:05 +01001387 {
1388 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1389 ++nbyte;
1390 else if (enc_utf8)
1391 nbyte += utfc_ptr2len(str + nbyte);
1392 else
1393 nbyte += mb_ptr2len(str + nbyte);
1394 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001395 nchar = clen + index;
1396 if (nchar < 0)
1397 // unlike list: index out of range results in empty string
1398 return NULL;
1399 }
1400
1401 for (nbyte = 0; nchar > 0 && nbyte < slen; --nchar)
Bram Moolenaarff871402021-03-26 13:34:05 +01001402 {
1403 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1404 ++nbyte;
1405 else if (enc_utf8)
1406 nbyte += utfc_ptr2len(str + nbyte);
1407 else
1408 nbyte += mb_ptr2len(str + nbyte);
1409 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001410 if (nbyte >= slen)
1411 return NULL;
Bram Moolenaar0289a092021-03-14 18:40:19 +01001412 return vim_strnsave(str + nbyte, mb_ptr2len(str + nbyte));
Bram Moolenaare7525c52021-01-09 13:20:37 +01001413}
1414
1415/*
1416 * Get the byte index for character index "idx" in string "str" with length
Bram Moolenaar0289a092021-03-14 18:40:19 +01001417 * "str_len". Composing characters are included.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001418 * If going over the end return "str_len".
1419 * If "idx" is negative count from the end, -1 is the last character.
1420 * When going over the start return -1.
1421 */
1422 static long
1423char_idx2byte(char_u *str, size_t str_len, varnumber_T idx)
1424{
1425 varnumber_T nchar = idx;
1426 size_t nbyte = 0;
1427
1428 if (nchar >= 0)
1429 {
1430 while (nchar > 0 && nbyte < str_len)
1431 {
Bram Moolenaar0289a092021-03-14 18:40:19 +01001432 nbyte += mb_ptr2len(str + nbyte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001433 --nchar;
1434 }
1435 }
1436 else
1437 {
1438 nbyte = str_len;
1439 while (nchar < 0 && nbyte > 0)
1440 {
1441 --nbyte;
1442 nbyte -= mb_head_off(str, str + nbyte);
1443 ++nchar;
1444 }
1445 if (nchar < 0)
1446 return -1;
1447 }
1448 return (long)nbyte;
1449}
1450
1451/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001452 * Return the slice "str[first : last]" using character indexes. Composing
1453 * characters are included.
Bram Moolenaar6601b622021-01-13 21:47:15 +01001454 * "exclusive" is TRUE for slice().
Bram Moolenaare7525c52021-01-09 13:20:37 +01001455 * Return NULL when the result is empty.
1456 */
1457 char_u *
Bram Moolenaar6601b622021-01-13 21:47:15 +01001458string_slice(char_u *str, varnumber_T first, varnumber_T last, int exclusive)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001459{
1460 long start_byte, end_byte;
1461 size_t slen;
1462
1463 if (str == NULL)
1464 return NULL;
1465 slen = STRLEN(str);
1466 start_byte = char_idx2byte(str, slen, first);
1467 if (start_byte < 0)
1468 start_byte = 0; // first index very negative: use zero
Bram Moolenaar6601b622021-01-13 21:47:15 +01001469 if ((last == -1 && !exclusive) || last == VARNUM_MAX)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001470 end_byte = (long)slen;
1471 else
1472 {
1473 end_byte = char_idx2byte(str, slen, last);
Bram Moolenaar6601b622021-01-13 21:47:15 +01001474 if (!exclusive && end_byte >= 0 && end_byte < (long)slen)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001475 // end index is inclusive
Bram Moolenaar0289a092021-03-14 18:40:19 +01001476 end_byte += mb_ptr2len(str + end_byte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001477 }
1478
1479 if (start_byte >= (long)slen || end_byte <= start_byte)
1480 return NULL;
1481 return vim_strnsave(str + start_byte, end_byte - start_byte);
1482}
1483
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001484/*
1485 * Get a script variable for ISN_STORESCRIPT and ISN_LOADSCRIPT.
1486 * When "dfunc_idx" is negative don't give an error.
1487 * Returns NULL for an error.
1488 */
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001489 static svar_T *
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001490get_script_svar(scriptref_T *sref, int dfunc_idx)
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001491{
1492 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001493 dfunc_T *dfunc = dfunc_idx < 0 ? NULL
1494 : ((dfunc_T *)def_functions.ga_data) + dfunc_idx;
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001495 svar_T *sv;
1496
1497 if (sref->sref_seq != si->sn_script_seq)
1498 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001499 // The script was reloaded after the function was compiled, the
1500 // script_idx may not be valid.
1501 if (dfunc != NULL)
1502 semsg(_(e_script_variable_invalid_after_reload_in_function_str),
1503 printable_func_name(dfunc->df_ufunc));
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001504 return NULL;
1505 }
1506 sv = ((svar_T *)si->sn_var_vals.ga_data) + sref->sref_idx;
Bram Moolenaar60dc8272021-07-29 22:48:54 +02001507 if (!equal_type(sv->sv_type, sref->sref_type, 0))
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001508 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001509 if (dfunc != NULL)
1510 emsg(_(e_script_variable_type_changed));
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001511 return NULL;
1512 }
1513 return sv;
1514}
1515
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001516/*
Bram Moolenaar20677332021-06-06 17:02:53 +02001517 * Function passed to do_cmdline() for splitting a script joined by NL
1518 * characters.
1519 */
1520 static char_u *
1521get_split_sourceline(
1522 int c UNUSED,
1523 void *cookie,
1524 int indent UNUSED,
1525 getline_opt_T options UNUSED)
1526{
1527 source_cookie_T *sp = (source_cookie_T *)cookie;
1528 char_u *p;
1529 char_u *line;
1530
Bram Moolenaar20677332021-06-06 17:02:53 +02001531 p = vim_strchr(sp->nextline, '\n');
1532 if (p == NULL)
1533 {
1534 line = vim_strsave(sp->nextline);
1535 sp->nextline += STRLEN(sp->nextline);
1536 }
1537 else
1538 {
1539 line = vim_strnsave(sp->nextline, p - sp->nextline);
1540 sp->nextline = p + 1;
1541 }
1542 return line;
1543}
1544
1545/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001546 * Execute a function by "name".
1547 * This can be a builtin function, user function or a funcref.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001548 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001549 */
1550 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001551call_eval_func(
1552 char_u *name,
1553 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001554 ectx_T *ectx,
1555 isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001556{
Bram Moolenaared677f52020-08-12 16:38:10 +02001557 int called_emsg_before = called_emsg;
1558 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001559
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001560 res = call_by_name(name, argcount, ectx, iptr, NULL);
Bram Moolenaared677f52020-08-12 16:38:10 +02001561 if (res == FAIL && called_emsg == called_emsg_before)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001562 {
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001563 dictitem_T *v;
1564
1565 v = find_var(name, NULL, FALSE);
1566 if (v == NULL)
1567 {
Bram Moolenaare1242042021-12-16 20:56:57 +00001568 semsg(_(e_unknown_function_str), name);
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001569 return FAIL;
1570 }
1571 if (v->di_tv.v_type != VAR_PARTIAL && v->di_tv.v_type != VAR_FUNC)
1572 {
Bram Moolenaare1242042021-12-16 20:56:57 +00001573 semsg(_(e_unknown_function_str), name);
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001574 return FAIL;
1575 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001576 return call_partial(&v->di_tv, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001577 }
Bram Moolenaared677f52020-08-12 16:38:10 +02001578 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001579}
1580
1581/*
Bram Moolenaarf112f302020-12-20 17:47:52 +01001582 * When a function reference is used, fill a partial with the information
1583 * needed, especially when it is used as a closure.
1584 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001585 int
Bram Moolenaarf112f302020-12-20 17:47:52 +01001586fill_partial_and_closure(partial_T *pt, ufunc_T *ufunc, ectx_T *ectx)
1587{
1588 pt->pt_func = ufunc;
1589 pt->pt_refcount = 1;
1590
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001591 if (ufunc->uf_flags & FC_CLOSURE)
Bram Moolenaarf112f302020-12-20 17:47:52 +01001592 {
1593 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1594 + ectx->ec_dfunc_idx;
1595
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001596 // The closure may need to find arguments and local variables in the
1597 // current stack.
Bram Moolenaar0186e582021-01-10 18:33:11 +01001598 pt->pt_outer.out_stack = &ectx->ec_stack;
1599 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001600 if (ectx->ec_outer_ref != NULL)
1601 {
1602 // The current context already has a context, link to that one.
1603 pt->pt_outer.out_up = ectx->ec_outer_ref->or_outer;
1604 if (ectx->ec_outer_ref->or_partial != NULL)
1605 {
1606 pt->pt_outer.out_up_partial = ectx->ec_outer_ref->or_partial;
1607 ++pt->pt_outer.out_up_partial->pt_refcount;
1608 }
1609 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01001610
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001611 // If this function returns and the closure is still being used, we
1612 // need to make a copy of the context (arguments and local variables).
1613 // Store a reference to the partial so we can handle that.
Bram Moolenaar35578162021-08-02 19:10:38 +02001614 if (GA_GROW_FAILS(&ectx->ec_funcrefs, 1))
Bram Moolenaarf112f302020-12-20 17:47:52 +01001615 {
1616 vim_free(pt);
1617 return FAIL;
1618 }
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001619 // Extra variable keeps the count of closures created in the current
1620 // function call.
Bram Moolenaarf112f302020-12-20 17:47:52 +01001621 ++(((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx
1622 + STACK_FRAME_SIZE + dfunc->df_varcount)->vval.v_number;
1623
1624 ((partial_T **)ectx->ec_funcrefs.ga_data)
1625 [ectx->ec_funcrefs.ga_len] = pt;
1626 ++pt->pt_refcount;
1627 ++ectx->ec_funcrefs.ga_len;
1628 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001629 ++ufunc->uf_refcount;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001630 return OK;
1631}
1632
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001633/*
1634 * Execute iptr->isn_arg.string as an Ex command.
1635 */
1636 static int
1637exec_command(isn_T *iptr)
1638{
1639 source_cookie_T cookie;
1640
1641 SOURCING_LNUM = iptr->isn_lnum;
1642 // Pass getsourceline to get an error for a missing ":end"
1643 // command.
1644 CLEAR_FIELD(cookie);
1645 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1646 if (do_cmdline(iptr->isn_arg.string,
1647 getsourceline, &cookie,
1648 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED) == FAIL
1649 || did_emsg)
1650 return FAIL;
1651 return OK;
1652}
1653
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001654// used for v_instr of typval of VAR_INSTR
1655struct instr_S {
1656 ectx_T *instr_ectx;
1657 isn_T *instr_instr;
1658};
1659
Bram Moolenaar4c137212021-04-19 16:48:48 +02001660// used for substitute_instr
1661typedef struct subs_expr_S {
1662 ectx_T *subs_ectx;
1663 isn_T *subs_instr;
1664 int subs_status;
1665} subs_expr_T;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001666
1667// Get pointer to item in the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001668#define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001669
1670// Get pointer to item at the bottom of the stack, -1 is the bottom.
1671#undef STACK_TV_BOT
Bram Moolenaar4c137212021-04-19 16:48:48 +02001672#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 +01001673
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001674// Get pointer to a local variable on the stack. Negative for arguments.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001675#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 +01001676
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001677// Set when calling do_debug().
1678static ectx_T *debug_context = NULL;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001679static int debug_var_count;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001680
1681/*
1682 * When debugging lookup "name" and return the typeval.
1683 * When not found return NULL.
1684 */
1685 typval_T *
1686lookup_debug_var(char_u *name)
1687{
1688 int idx;
1689 dfunc_T *dfunc;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001690 ufunc_T *ufunc;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001691 ectx_T *ectx = debug_context;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001692 int varargs_off;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001693
1694 if (ectx == NULL)
1695 return NULL;
1696 dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
1697
1698 // Go through the local variable names, from last to first.
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001699 for (idx = debug_var_count - 1; idx >= 0; --idx)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001700 {
Bram Moolenaare406ff82022-03-10 20:47:43 +00001701 char_u *varname = ((char_u **)dfunc->df_var_names.ga_data)[idx];
1702
1703 // the variable name may be NULL when not available in this block
1704 if (varname != NULL && STRCMP(varname, name) == 0)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001705 return STACK_TV_VAR(idx);
1706 }
1707
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001708 // Go through argument names.
1709 ufunc = dfunc->df_ufunc;
1710 varargs_off = ufunc->uf_va_name == NULL ? 0 : 1;
1711 for (idx = 0; idx < ufunc->uf_args.ga_len; ++idx)
1712 if (STRCMP(((char_u **)(ufunc->uf_args.ga_data))[idx], name) == 0)
1713 return STACK_TV(ectx->ec_frame_idx - ufunc->uf_args.ga_len
1714 - varargs_off + idx);
1715 if (ufunc->uf_va_name != NULL && STRCMP(ufunc->uf_va_name, name) == 0)
1716 return STACK_TV(ectx->ec_frame_idx - 1);
1717
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001718 return NULL;
1719}
1720
Bram Moolenaar26a44842021-09-02 18:49:06 +02001721/*
1722 * Return TRUE if there might be a breakpoint in "ufunc", which is when a
1723 * breakpoint was set in that function or when there is any expression.
1724 */
1725 int
1726may_break_in_function(ufunc_T *ufunc)
1727{
1728 return ufunc->uf_has_breakpoint || debug_has_expr_breakpoint();
1729}
1730
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001731 static void
1732handle_debug(isn_T *iptr, ectx_T *ectx)
1733{
1734 char_u *line;
1735 ufunc_T *ufunc = (((dfunc_T *)def_functions.ga_data)
1736 + ectx->ec_dfunc_idx)->df_ufunc;
1737 isn_T *ni;
1738 int end_lnum = iptr->isn_lnum;
1739 garray_T ga;
1740 int lnum;
1741
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02001742 if (ex_nesting_level > debug_break_level)
1743 {
1744 linenr_T breakpoint;
1745
Bram Moolenaar26a44842021-09-02 18:49:06 +02001746 if (!may_break_in_function(ufunc))
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02001747 return;
1748
1749 // check for the next breakpoint if needed
1750 breakpoint = dbg_find_breakpoint(FALSE, ufunc->uf_name,
Bram Moolenaar8cec9272021-06-23 20:20:53 +02001751 iptr->isn_arg.debug.dbg_break_lnum);
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02001752 if (breakpoint <= 0 || breakpoint > iptr->isn_lnum)
1753 return;
1754 }
1755
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001756 SOURCING_LNUM = iptr->isn_lnum;
1757 debug_context = ectx;
Bram Moolenaar8cec9272021-06-23 20:20:53 +02001758 debug_var_count = iptr->isn_arg.debug.dbg_var_names_len;
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001759
1760 for (ni = iptr + 1; ni->isn_type != ISN_FINISH; ++ni)
1761 if (ni->isn_type == ISN_DEBUG
1762 || ni->isn_type == ISN_RETURN
1763 || ni->isn_type == ISN_RETURN_VOID)
1764 {
Bram Moolenaar112bed02021-11-23 22:16:34 +00001765 end_lnum = ni->isn_lnum + (ni->isn_type == ISN_DEBUG ? 0 : 1);
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001766 break;
1767 }
1768
1769 if (end_lnum > iptr->isn_lnum)
1770 {
1771 ga_init2(&ga, sizeof(char_u *), 10);
Bram Moolenaar310091d2021-12-23 21:14:37 +00001772 for (lnum = iptr->isn_lnum; lnum < end_lnum
1773 && lnum <= ufunc->uf_lines.ga_len; ++lnum)
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001774 {
Bram Moolenaar303215d2021-07-07 20:10:43 +02001775 char_u *p = ((char_u **)ufunc->uf_lines.ga_data)[lnum - 1];
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001776
Bram Moolenaar303215d2021-07-07 20:10:43 +02001777 if (p == NULL)
1778 continue; // left over from continuation line
1779 p = skipwhite(p);
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001780 if (*p == '#')
1781 break;
Bram Moolenaar35578162021-08-02 19:10:38 +02001782 if (GA_GROW_OK(&ga, 1))
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001783 ((char_u **)(ga.ga_data))[ga.ga_len++] = p;
1784 if (STRNCMP(p, "def ", 4) == 0)
1785 break;
1786 }
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001787 line = ga_concat_strings(&ga, " ");
1788 vim_free(ga.ga_data);
1789 }
1790 else
1791 line = ((char_u **)ufunc->uf_lines.ga_data)[iptr->isn_lnum - 1];
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001792
Dominique Pelle6e969552021-06-17 13:53:41 +02001793 do_debug(line == NULL ? (char_u *)"[empty]" : line);
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001794 debug_context = NULL;
1795
1796 if (end_lnum > iptr->isn_lnum)
1797 vim_free(line);
1798}
1799
Bram Moolenaar4c137212021-04-19 16:48:48 +02001800/*
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00001801 * Store a value in a list, dict or blob variable.
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001802 * Returns OK, FAIL or NOTDONE (uncatchable error).
1803 */
1804 static int
1805execute_storeindex(isn_T *iptr, ectx_T *ectx)
1806{
1807 vartype_T dest_type = iptr->isn_arg.vartype;
1808 typval_T *tv;
1809 typval_T *tv_idx = STACK_TV_BOT(-2);
1810 typval_T *tv_dest = STACK_TV_BOT(-1);
1811 int status = OK;
1812
1813 // Stack contains:
1814 // -3 value to be stored
1815 // -2 index
1816 // -1 dict or list
1817 tv = STACK_TV_BOT(-3);
1818 SOURCING_LNUM = iptr->isn_lnum;
1819 if (dest_type == VAR_ANY)
1820 {
1821 dest_type = tv_dest->v_type;
1822 if (dest_type == VAR_DICT)
1823 status = do_2string(tv_idx, TRUE, FALSE);
1824 else if (dest_type == VAR_LIST && tv_idx->v_type != VAR_NUMBER)
1825 {
1826 emsg(_(e_number_expected));
1827 status = FAIL;
1828 }
1829 }
Bram Moolenaare08be092022-02-17 13:08:26 +00001830
1831 if (status == OK)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001832 {
Bram Moolenaare08be092022-02-17 13:08:26 +00001833 if (dest_type == VAR_LIST)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001834 {
Bram Moolenaare08be092022-02-17 13:08:26 +00001835 long lidx = (long)tv_idx->vval.v_number;
1836 list_T *list = tv_dest->vval.v_list;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001837
Bram Moolenaare08be092022-02-17 13:08:26 +00001838 if (list == NULL)
1839 {
1840 emsg(_(e_list_not_set));
1841 return FAIL;
1842 }
1843 if (lidx < 0 && list->lv_len + lidx >= 0)
1844 // negative index is relative to the end
1845 lidx = list->lv_len + lidx;
1846 if (lidx < 0 || lidx > list->lv_len)
1847 {
1848 semsg(_(e_list_index_out_of_range_nr), lidx);
1849 return FAIL;
1850 }
1851 if (lidx < list->lv_len)
1852 {
1853 listitem_T *li = list_find(list, lidx);
1854
1855 if (error_if_locked(li->li_tv.v_lock,
Bram Moolenaar70c43d82022-01-26 21:01:15 +00001856 e_cannot_change_locked_list_item))
Bram Moolenaare08be092022-02-17 13:08:26 +00001857 return FAIL;
1858 // overwrite existing list item
1859 clear_tv(&li->li_tv);
1860 li->li_tv = *tv;
1861 }
1862 else
1863 {
1864 if (error_if_locked(list->lv_lock, e_cannot_change_locked_list))
1865 return FAIL;
1866 // append to list, only fails when out of memory
1867 if (list_append_tv(list, tv) == FAIL)
1868 return NOTDONE;
1869 clear_tv(tv);
1870 }
1871 }
1872 else if (dest_type == VAR_DICT)
1873 {
1874 char_u *key = tv_idx->vval.v_string;
1875 dict_T *dict = tv_dest->vval.v_dict;
1876 dictitem_T *di;
1877
1878 SOURCING_LNUM = iptr->isn_lnum;
1879 if (dict == NULL)
1880 {
1881 emsg(_(e_dictionary_not_set));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001882 return FAIL;
Bram Moolenaare08be092022-02-17 13:08:26 +00001883 }
1884 if (key == NULL)
1885 key = (char_u *)"";
1886 di = dict_find(dict, key, -1);
1887 if (di != NULL)
1888 {
1889 if (error_if_locked(di->di_tv.v_lock,
1890 e_cannot_change_dict_item))
1891 return FAIL;
1892 // overwrite existing value
1893 clear_tv(&di->di_tv);
1894 di->di_tv = *tv;
1895 }
1896 else
1897 {
1898 if (error_if_locked(dict->dv_lock, e_cannot_change_dict))
1899 return FAIL;
1900 // add to dict, only fails when out of memory
1901 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
1902 return NOTDONE;
1903 clear_tv(tv);
1904 }
1905 }
1906 else if (dest_type == VAR_BLOB)
1907 {
1908 long lidx = (long)tv_idx->vval.v_number;
1909 blob_T *blob = tv_dest->vval.v_blob;
1910 varnumber_T nr;
1911 int error = FALSE;
1912 int len;
1913
1914 if (blob == NULL)
1915 {
1916 emsg(_(e_blob_not_set));
1917 return FAIL;
1918 }
1919 len = blob_len(blob);
1920 if (lidx < 0 && len + lidx >= 0)
1921 // negative index is relative to the end
1922 lidx = len + lidx;
1923
1924 // Can add one byte at the end.
1925 if (lidx < 0 || lidx > len)
1926 {
1927 semsg(_(e_blob_index_out_of_range_nr), lidx);
1928 return FAIL;
1929 }
1930 if (value_check_lock(blob->bv_lock, (char_u *)"blob", FALSE))
1931 return FAIL;
1932 nr = tv_get_number_chk(tv, &error);
1933 if (error)
1934 return FAIL;
1935 blob_set_append(blob, lidx, nr);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001936 }
1937 else
1938 {
Bram Moolenaare08be092022-02-17 13:08:26 +00001939 status = FAIL;
1940 semsg(_(e_cannot_index_str), vartype_name(dest_type));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001941 }
1942 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001943
1944 clear_tv(tv_idx);
1945 clear_tv(tv_dest);
1946 ectx->ec_stack.ga_len -= 3;
1947 if (status == FAIL)
1948 {
1949 clear_tv(tv);
1950 return FAIL;
1951 }
1952 return OK;
1953}
1954
1955/*
Bram Moolenaarea5c8982022-02-17 14:42:02 +00001956 * Store a value in a list or blob range.
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001957 */
1958 static int
1959execute_storerange(isn_T *iptr, ectx_T *ectx)
1960{
1961 typval_T *tv;
1962 typval_T *tv_idx1 = STACK_TV_BOT(-3);
1963 typval_T *tv_idx2 = STACK_TV_BOT(-2);
1964 typval_T *tv_dest = STACK_TV_BOT(-1);
1965 int status = OK;
1966
1967 // Stack contains:
1968 // -4 value to be stored
1969 // -3 first index or "none"
1970 // -2 second index or "none"
1971 // -1 destination list or blob
1972 tv = STACK_TV_BOT(-4);
Bram Moolenaarea5c8982022-02-17 14:42:02 +00001973 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001974 if (tv_dest->v_type == VAR_LIST)
1975 {
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00001976 long n1;
1977 long n2;
1978 listitem_T *li1;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001979
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00001980 n1 = (long)tv_get_number_chk(tv_idx1, NULL);
1981 if (tv_idx2->v_type == VAR_SPECIAL
1982 && tv_idx2->vval.v_number == VVAL_NONE)
1983 n2 = list_len(tv_dest->vval.v_list) - 1;
1984 else
1985 n2 = (long)tv_get_number_chk(tv_idx2, NULL);
1986
1987 li1 = check_range_index_one(tv_dest->vval.v_list, &n1, FALSE);
1988 if (li1 == NULL)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001989 status = FAIL;
1990 else
1991 {
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00001992 status = check_range_index_two(tv_dest->vval.v_list,
1993 &n1, li1, &n2, FALSE);
1994 if (status != FAIL)
1995 status = list_assign_range(
1996 tv_dest->vval.v_list,
1997 tv->vval.v_list,
1998 n1,
1999 n2,
2000 tv_idx2->v_type == VAR_SPECIAL,
2001 (char_u *)"=",
2002 (char_u *)"[unknown]");
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002003 }
2004 }
2005 else if (tv_dest->v_type == VAR_BLOB)
2006 {
2007 varnumber_T n1;
2008 varnumber_T n2;
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002009 long bloblen;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002010
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002011 n1 = tv_get_number_chk(tv_idx1, NULL);
2012 if (tv_idx2->v_type == VAR_SPECIAL
2013 && tv_idx2->vval.v_number == VVAL_NONE)
2014 n2 = blob_len(tv_dest->vval.v_blob) - 1;
2015 else
2016 n2 = tv_get_number_chk(tv_idx2, NULL);
2017 bloblen = blob_len(tv_dest->vval.v_blob);
2018
2019 if (check_blob_index(bloblen, n1, FALSE) == FAIL
2020 || check_blob_range(bloblen, n1, n2, FALSE) == FAIL)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002021 status = FAIL;
2022 else
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002023 status = blob_set_range(tv_dest->vval.v_blob, n1, n2, tv);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002024 }
2025 else
2026 {
2027 status = FAIL;
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002028 emsg(_(e_list_or_blob_required));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002029 }
2030
2031 clear_tv(tv_idx1);
2032 clear_tv(tv_idx2);
2033 clear_tv(tv_dest);
2034 ectx->ec_stack.ga_len -= 4;
2035 clear_tv(tv);
2036
2037 return status;
2038}
2039
2040/*
2041 * Unlet item in list or dict variable.
2042 */
2043 static int
2044execute_unletindex(isn_T *iptr, ectx_T *ectx)
2045{
2046 typval_T *tv_idx = STACK_TV_BOT(-2);
2047 typval_T *tv_dest = STACK_TV_BOT(-1);
2048 int status = OK;
2049
2050 // Stack contains:
2051 // -2 index
2052 // -1 dict or list
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002053 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002054 if (tv_dest->v_type == VAR_DICT)
2055 {
2056 // unlet a dict item, index must be a string
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002057 if (tv_idx->v_type != VAR_STRING && tv_idx->v_type != VAR_NUMBER)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002058 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002059 semsg(_(e_expected_str_but_got_str),
2060 vartype_name(VAR_STRING),
2061 vartype_name(tv_idx->v_type));
2062 status = FAIL;
2063 }
2064 else
2065 {
2066 dict_T *d = tv_dest->vval.v_dict;
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002067 char_u *key;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002068 dictitem_T *di = NULL;
2069
2070 if (d != NULL && value_check_lock(
2071 d->dv_lock, NULL, FALSE))
2072 status = FAIL;
2073 else
2074 {
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002075 if (tv_idx->v_type == VAR_STRING)
2076 {
2077 key = tv_idx->vval.v_string;
2078 if (key == NULL)
2079 key = (char_u *)"";
2080 }
2081 else
2082 {
2083 key = tv_get_string(tv_idx);
2084 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002085 if (d != NULL)
2086 di = dict_find(d, key, (int)STRLEN(key));
2087 if (di == NULL)
2088 {
2089 // NULL dict is equivalent to empty dict
2090 semsg(_(e_key_not_present_in_dictionary),
2091 key);
2092 status = FAIL;
2093 }
2094 else if (var_check_fixed(di->di_flags,
2095 NULL, FALSE)
2096 || var_check_ro(di->di_flags,
2097 NULL, FALSE))
2098 status = FAIL;
2099 else
2100 dictitem_remove(d, di);
2101 }
2102 }
2103 }
2104 else if (tv_dest->v_type == VAR_LIST)
2105 {
2106 // unlet a List item, index must be a number
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002107 if (check_for_number(tv_idx) == FAIL)
2108 {
2109 status = FAIL;
2110 }
2111 else
2112 {
2113 list_T *l = tv_dest->vval.v_list;
2114 long n = (long)tv_idx->vval.v_number;
2115
2116 if (l != NULL && value_check_lock(
2117 l->lv_lock, NULL, FALSE))
2118 status = FAIL;
2119 else
2120 {
2121 listitem_T *li = list_find(l, n);
2122
2123 if (li == NULL)
2124 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002125 semsg(_(e_list_index_out_of_range_nr), n);
2126 status = FAIL;
2127 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002128 else
2129 listitem_remove(l, li);
2130 }
2131 }
2132 }
2133 else
2134 {
2135 status = FAIL;
2136 semsg(_(e_cannot_index_str),
2137 vartype_name(tv_dest->v_type));
2138 }
2139
2140 clear_tv(tv_idx);
2141 clear_tv(tv_dest);
2142 ectx->ec_stack.ga_len -= 2;
2143
2144 return status;
2145}
2146
2147/*
2148 * Unlet a range of items in a list variable.
2149 */
2150 static int
2151execute_unletrange(isn_T *iptr, ectx_T *ectx)
2152{
2153 // Stack contains:
2154 // -3 index1
2155 // -2 index2
2156 // -1 dict or list
2157 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2158 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2159 typval_T *tv_dest = STACK_TV_BOT(-1);
2160 int status = OK;
2161
2162 if (tv_dest->v_type == VAR_LIST)
2163 {
2164 // indexes must be a number
2165 SOURCING_LNUM = iptr->isn_lnum;
2166 if (check_for_number(tv_idx1) == FAIL
2167 || (tv_idx2->v_type != VAR_SPECIAL
2168 && check_for_number(tv_idx2) == FAIL))
2169 {
2170 status = FAIL;
2171 }
2172 else
2173 {
2174 list_T *l = tv_dest->vval.v_list;
2175 long n1 = (long)tv_idx1->vval.v_number;
2176 long n2 = tv_idx2->v_type == VAR_SPECIAL
2177 ? 0 : (long)tv_idx2->vval.v_number;
2178 listitem_T *li;
2179
2180 li = list_find_index(l, &n1);
2181 if (li == NULL)
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002182 {
2183 semsg(_(e_list_index_out_of_range_nr),
2184 (long)tv_idx1->vval.v_number);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002185 status = FAIL;
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002186 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002187 else
2188 {
2189 if (n1 < 0)
2190 n1 = list_idx_of_item(l, li);
2191 if (n2 < 0)
2192 {
2193 listitem_T *li2 = list_find(l, n2);
2194
2195 if (li2 == NULL)
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002196 {
2197 semsg(_(e_list_index_out_of_range_nr), n2);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002198 status = FAIL;
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002199 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002200 else
2201 n2 = list_idx_of_item(l, li2);
2202 }
2203 if (status != FAIL
2204 && tv_idx2->v_type != VAR_SPECIAL
2205 && n2 < n1)
2206 {
2207 semsg(_(e_list_index_out_of_range_nr), n2);
2208 status = FAIL;
2209 }
Bram Moolenaar6b8c7ba2022-03-20 17:46:06 +00002210 if (status != FAIL)
2211 list_unlet_range(l, li, n1,
2212 tv_idx2->v_type != VAR_SPECIAL, n2);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002213 }
2214 }
2215 }
2216 else
2217 {
2218 status = FAIL;
2219 SOURCING_LNUM = iptr->isn_lnum;
2220 semsg(_(e_cannot_index_str),
2221 vartype_name(tv_dest->v_type));
2222 }
2223
2224 clear_tv(tv_idx1);
2225 clear_tv(tv_idx2);
2226 clear_tv(tv_dest);
2227 ectx->ec_stack.ga_len -= 3;
2228
2229 return status;
2230}
2231
2232/*
2233 * Top of a for loop.
2234 */
2235 static int
2236execute_for(isn_T *iptr, ectx_T *ectx)
2237{
2238 typval_T *tv;
2239 typval_T *ltv = STACK_TV_BOT(-1);
2240 typval_T *idxtv =
2241 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
2242
2243 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
2244 return FAIL;
2245 if (ltv->v_type == VAR_LIST)
2246 {
2247 list_T *list = ltv->vval.v_list;
2248
2249 // push the next item from the list
2250 ++idxtv->vval.v_number;
2251 if (list == NULL
2252 || idxtv->vval.v_number >= list->lv_len)
2253 {
2254 // past the end of the list, jump to "endfor"
2255 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
2256 may_restore_cmdmod(&ectx->ec_funclocal);
2257 }
2258 else if (list->lv_first == &range_list_item)
2259 {
2260 // non-materialized range() list
2261 tv = STACK_TV_BOT(0);
2262 tv->v_type = VAR_NUMBER;
2263 tv->v_lock = 0;
2264 tv->vval.v_number = list_find_nr(
2265 list, idxtv->vval.v_number, NULL);
2266 ++ectx->ec_stack.ga_len;
2267 }
2268 else
2269 {
2270 listitem_T *li = list_find(list,
2271 idxtv->vval.v_number);
2272
2273 copy_tv(&li->li_tv, STACK_TV_BOT(0));
2274 ++ectx->ec_stack.ga_len;
2275 }
2276 }
2277 else if (ltv->v_type == VAR_STRING)
2278 {
2279 char_u *str = ltv->vval.v_string;
2280
2281 // The index is for the last byte of the previous
2282 // character.
2283 ++idxtv->vval.v_number;
2284 if (str == NULL || str[idxtv->vval.v_number] == NUL)
2285 {
2286 // past the end of the string, jump to "endfor"
2287 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
2288 may_restore_cmdmod(&ectx->ec_funclocal);
2289 }
2290 else
2291 {
2292 int clen = mb_ptr2len(str + idxtv->vval.v_number);
2293
2294 // Push the next character from the string.
2295 tv = STACK_TV_BOT(0);
2296 tv->v_type = VAR_STRING;
2297 tv->vval.v_string = vim_strnsave(
2298 str + idxtv->vval.v_number, clen);
2299 ++ectx->ec_stack.ga_len;
2300 idxtv->vval.v_number += clen - 1;
2301 }
2302 }
2303 else if (ltv->v_type == VAR_BLOB)
2304 {
2305 blob_T *blob = ltv->vval.v_blob;
2306
2307 // When we get here the first time make a copy of the
2308 // blob, so that the iteration still works when it is
2309 // changed.
2310 if (idxtv->vval.v_number == -1 && blob != NULL)
2311 {
2312 blob_copy(blob, ltv);
2313 blob_unref(blob);
2314 blob = ltv->vval.v_blob;
2315 }
2316
2317 // The index is for the previous byte.
2318 ++idxtv->vval.v_number;
2319 if (blob == NULL
2320 || idxtv->vval.v_number >= blob_len(blob))
2321 {
2322 // past the end of the blob, jump to "endfor"
2323 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
2324 may_restore_cmdmod(&ectx->ec_funclocal);
2325 }
2326 else
2327 {
2328 // Push the next byte from the blob.
2329 tv = STACK_TV_BOT(0);
2330 tv->v_type = VAR_NUMBER;
2331 tv->vval.v_number = blob_get(blob,
2332 idxtv->vval.v_number);
2333 ++ectx->ec_stack.ga_len;
2334 }
2335 }
2336 else
2337 {
2338 semsg(_(e_for_loop_on_str_not_supported),
2339 vartype_name(ltv->v_type));
2340 return FAIL;
2341 }
2342 return OK;
2343}
2344
2345/*
Bram Moolenaar06b77222022-01-25 15:51:56 +00002346 * Load instruction for w:/b:/g:/t: variable.
2347 * "isn_type" is used instead of "iptr->isn_type".
2348 */
2349 static int
2350load_namespace_var(ectx_T *ectx, isntype_T isn_type, isn_T *iptr)
2351{
2352 dictitem_T *di = NULL;
2353 hashtab_T *ht = NULL;
2354 char namespace;
2355
2356 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
2357 return NOTDONE;
2358 switch (isn_type)
2359 {
2360 case ISN_LOADG:
2361 ht = get_globvar_ht();
2362 namespace = 'g';
2363 break;
2364 case ISN_LOADB:
2365 ht = &curbuf->b_vars->dv_hashtab;
2366 namespace = 'b';
2367 break;
2368 case ISN_LOADW:
2369 ht = &curwin->w_vars->dv_hashtab;
2370 namespace = 'w';
2371 break;
2372 case ISN_LOADT:
2373 ht = &curtab->tp_vars->dv_hashtab;
2374 namespace = 't';
2375 break;
2376 default: // Cannot reach here
2377 return NOTDONE;
2378 }
2379 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
2380
Bram Moolenaar06b77222022-01-25 15:51:56 +00002381 if (di == NULL)
2382 {
Bram Moolenaarfe732552022-02-22 19:39:13 +00002383 if (isn_type == ISN_LOADG)
2384 {
2385 ufunc_T *ufunc = find_func(iptr->isn_arg.string, TRUE);
2386
2387 // g:Something could be a function
2388 if (ufunc != NULL)
2389 {
2390 typval_T *tv = STACK_TV_BOT(0);
2391
2392 ++ectx->ec_stack.ga_len;
2393 tv->v_type = VAR_FUNC;
2394 tv->vval.v_string = alloc(STRLEN(iptr->isn_arg.string) + 3);
2395 if (tv->vval.v_string == NULL)
2396 return FAIL;
2397 STRCPY(tv->vval.v_string, "g:");
2398 STRCPY(tv->vval.v_string + 2, iptr->isn_arg.string);
2399 return OK;
2400 }
2401 }
Bram Moolenaar06b77222022-01-25 15:51:56 +00002402 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarfe732552022-02-22 19:39:13 +00002403 if (vim_strchr(iptr->isn_arg.string, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar06b77222022-01-25 15:51:56 +00002404 // no check if the item exists in the script but
2405 // isn't exported, it is too complicated
Bram Moolenaarfe732552022-02-22 19:39:13 +00002406 semsg(_(e_item_not_found_in_script_str), iptr->isn_arg.string);
Bram Moolenaar06b77222022-01-25 15:51:56 +00002407 else
2408 semsg(_(e_undefined_variable_char_str),
Bram Moolenaarfe732552022-02-22 19:39:13 +00002409 namespace, iptr->isn_arg.string);
Bram Moolenaar06b77222022-01-25 15:51:56 +00002410 return FAIL;
2411 }
2412 else
2413 {
2414 copy_tv(&di->di_tv, STACK_TV_BOT(0));
2415 ++ectx->ec_stack.ga_len;
2416 }
2417 return OK;
2418}
2419
2420/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02002421 * Execute instructions in execution context "ectx".
2422 * Return OK or FAIL;
2423 */
2424 static int
2425exec_instructions(ectx_T *ectx)
2426{
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002427 int ret = FAIL;
2428 int save_trylevel_at_start = ectx->ec_trylevel_at_start;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02002429 int dict_stack_len_at_start = dict_stack.ga_len;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01002430
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02002431 // Start execution at the first instruction.
Bram Moolenaar4c137212021-04-19 16:48:48 +02002432 ectx->ec_iidx = 0;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01002433
Bram Moolenaarff652882021-05-16 15:24:49 +02002434 // Only catch exceptions in this instruction list.
2435 ectx->ec_trylevel_at_start = trylevel;
2436
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002437 for (;;)
2438 {
Bram Moolenaara7490192021-07-22 12:26:14 +02002439 static int breakcheck_count = 0; // using "static" makes it faster
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002440 isn_T *iptr;
Bram Moolenaara7490192021-07-22 12:26:14 +02002441 typval_T *tv;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002442
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002443 if (unlikely(++breakcheck_count >= 100))
Bram Moolenaar270d0382020-05-15 21:42:53 +02002444 {
2445 line_breakcheck();
2446 breakcheck_count = 0;
2447 }
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002448 if (unlikely(got_int))
Bram Moolenaar20431c92020-03-20 18:39:46 +01002449 {
2450 // Turn CTRL-C into an exception.
2451 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +01002452 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002453 goto theend;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002454 did_throw = TRUE;
2455 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002456
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002457 if (unlikely(did_emsg && msg_list != NULL && *msg_list != NULL))
Bram Moolenaara26b9702020-04-18 19:53:28 +02002458 {
2459 // Turn an error message into an exception.
2460 did_emsg = FALSE;
2461 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002462 goto theend;
Bram Moolenaara26b9702020-04-18 19:53:28 +02002463 did_throw = TRUE;
2464 *msg_list = NULL;
2465 }
2466
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002467 if (unlikely(did_throw))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002468 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002469 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002470 trycmd_T *trycmd = NULL;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002471 int index = trystack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002472
2473 // An exception jumps to the first catch, finally, or returns from
2474 // the current function.
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002475 while (index > 0)
2476 {
2477 trycmd = ((trycmd_T *)trystack->ga_data) + index - 1;
Bram Moolenaar834193a2021-06-30 20:39:15 +02002478 if (!trycmd->tcd_in_catch || trycmd->tcd_finally_idx != 0)
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002479 break;
2480 // In the catch and finally block of this try we have to go up
2481 // one level.
2482 --index;
2483 trycmd = NULL;
2484 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02002485 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002486 {
Bram Moolenaar834193a2021-06-30 20:39:15 +02002487 if (trycmd->tcd_in_catch)
2488 {
2489 // exception inside ":catch", jump to ":finally" once
2490 ectx->ec_iidx = trycmd->tcd_finally_idx;
2491 trycmd->tcd_finally_idx = 0;
2492 }
2493 else
2494 // jump to first ":catch"
2495 ectx->ec_iidx = trycmd->tcd_catch_idx;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002496 trycmd->tcd_in_catch = TRUE;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002497 did_throw = FALSE; // don't come back here until :endtry
2498 trycmd->tcd_did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002499 }
2500 else
2501 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02002502 // Not inside try or need to return from current functions.
2503 // Push a dummy return value.
Bram Moolenaar35578162021-08-02 19:10:38 +02002504 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002505 goto theend;
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02002506 tv = STACK_TV_BOT(0);
2507 tv->v_type = VAR_NUMBER;
2508 tv->vval.v_number = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002509 ++ectx->ec_stack.ga_len;
2510 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002511 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02002512 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +01002513 need_rethrow = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002514 if (handle_closure_in_use(ectx, FALSE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002515 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002516 goto done;
2517 }
2518
Bram Moolenaar4c137212021-04-19 16:48:48 +02002519 if (func_return(ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002520 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002521 }
2522 continue;
2523 }
2524
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002525 /*
2526 * Big switch on the instruction. Most compilers will be turning this
2527 * into an efficient lookup table, since the "case" values are an enum
2528 * with sequential numbers. It may look ugly, but it should be the
2529 * most efficient way.
2530 */
Bram Moolenaar4c137212021-04-19 16:48:48 +02002531 iptr = &ectx->ec_instr[ectx->ec_iidx++];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002532 switch (iptr->isn_type)
2533 {
2534 // execute Ex command line
2535 case ISN_EXEC:
Bram Moolenaaraacc9662021-08-13 19:40:51 +02002536 if (exec_command(iptr) == FAIL)
2537 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002538 break;
2539
Bram Moolenaar20677332021-06-06 17:02:53 +02002540 // execute Ex command line split at NL characters.
2541 case ISN_EXEC_SPLIT:
2542 {
2543 source_cookie_T cookie;
Bram Moolenaar518df272021-06-06 17:34:13 +02002544 char_u *line;
Bram Moolenaar20677332021-06-06 17:02:53 +02002545
2546 SOURCING_LNUM = iptr->isn_lnum;
2547 CLEAR_FIELD(cookie);
2548 cookie.sourcing_lnum = iptr->isn_lnum - 1;
2549 cookie.nextline = iptr->isn_arg.string;
Bram Moolenaar518df272021-06-06 17:34:13 +02002550 line = get_split_sourceline(0, &cookie, 0, 0);
2551 if (do_cmdline(line,
Bram Moolenaar20677332021-06-06 17:02:53 +02002552 get_split_sourceline, &cookie,
2553 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED)
2554 == FAIL
2555 || did_emsg)
Bram Moolenaar518df272021-06-06 17:34:13 +02002556 {
2557 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02002558 goto on_error;
Bram Moolenaar518df272021-06-06 17:34:13 +02002559 }
2560 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02002561 }
2562 break;
2563
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002564 // execute Ex command line that is only a range
2565 case ISN_EXECRANGE:
2566 {
2567 exarg_T ea;
2568 char *error = NULL;
2569
2570 CLEAR_FIELD(ea);
2571 ea.cmdidx = CMD_SIZE;
2572 ea.addr_type = ADDR_LINES;
2573 ea.cmd = iptr->isn_arg.string;
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00002574 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002575 parse_cmd_address(&ea, &error, FALSE);
Bram Moolenaar01a4dcb2021-12-04 13:15:10 +00002576 if (ea.cmd == NULL)
2577 goto on_error;
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00002578 // error is always NULL when using ADDR_LINES
2579 error = ex_range_without_command(&ea);
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002580 if (error != NULL)
2581 {
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002582 emsg(error);
2583 goto on_error;
2584 }
2585 }
2586 break;
2587
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02002588 // Evaluate an expression with legacy syntax, push it onto the
2589 // stack.
2590 case ISN_LEGACY_EVAL:
2591 {
2592 char_u *arg = iptr->isn_arg.string;
2593 int res;
2594 int save_flags = cmdmod.cmod_flags;
2595
Bram Moolenaar35578162021-08-02 19:10:38 +02002596 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002597 goto theend;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02002598 tv = STACK_TV_BOT(0);
2599 init_tv(tv);
2600 cmdmod.cmod_flags |= CMOD_LEGACY;
2601 res = eval0(arg, tv, NULL, &EVALARG_EVALUATE);
2602 cmdmod.cmod_flags = save_flags;
2603 if (res == FAIL)
2604 goto on_error;
2605 ++ectx->ec_stack.ga_len;
2606 }
2607 break;
2608
Bram Moolenaarf18332f2021-05-07 17:55:55 +02002609 // push typeval VAR_INSTR with instructions to be executed
2610 case ISN_INSTR:
2611 {
Bram Moolenaar35578162021-08-02 19:10:38 +02002612 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002613 goto theend;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02002614 tv = STACK_TV_BOT(0);
2615 tv->vval.v_instr = ALLOC_ONE(instr_T);
2616 if (tv->vval.v_instr == NULL)
2617 goto on_error;
2618 ++ectx->ec_stack.ga_len;
2619
2620 tv->v_type = VAR_INSTR;
2621 tv->vval.v_instr->instr_ectx = ectx;
2622 tv->vval.v_instr->instr_instr = iptr->isn_arg.instr;
2623 }
2624 break;
2625
Bram Moolenaar4c137212021-04-19 16:48:48 +02002626 // execute :substitute with an expression
2627 case ISN_SUBSTITUTE:
2628 {
2629 subs_T *subs = &iptr->isn_arg.subs;
2630 source_cookie_T cookie;
2631 struct subs_expr_S *save_instr = substitute_instr;
2632 struct subs_expr_S subs_instr;
2633 int res;
2634
2635 subs_instr.subs_ectx = ectx;
2636 subs_instr.subs_instr = subs->subs_instr;
2637 subs_instr.subs_status = OK;
2638 substitute_instr = &subs_instr;
2639
2640 SOURCING_LNUM = iptr->isn_lnum;
2641 // This is very much like ISN_EXEC
2642 CLEAR_FIELD(cookie);
2643 cookie.sourcing_lnum = iptr->isn_lnum - 1;
2644 res = do_cmdline(subs->subs_cmd,
2645 getsourceline, &cookie,
2646 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
2647 substitute_instr = save_instr;
2648
2649 if (res == FAIL || did_emsg
2650 || subs_instr.subs_status == FAIL)
2651 goto on_error;
2652 }
2653 break;
2654
2655 case ISN_FINISH:
2656 goto done;
2657
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02002658 case ISN_REDIRSTART:
2659 // create a dummy entry for var_redir_str()
2660 if (alloc_redir_lval() == FAIL)
2661 goto on_error;
2662
2663 // The output is stored in growarray "redir_ga" until
2664 // redirection ends.
2665 init_redir_ga();
2666 redir_vname = 1;
2667 break;
2668
2669 case ISN_REDIREND:
2670 {
2671 char_u *res = get_clear_redir_ga();
2672
2673 // End redirection, put redirected text on the stack.
2674 clear_redir_lval();
2675 redir_vname = 0;
2676
Bram Moolenaar35578162021-08-02 19:10:38 +02002677 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02002678 {
2679 vim_free(res);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002680 goto theend;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02002681 }
2682 tv = STACK_TV_BOT(0);
2683 tv->v_type = VAR_STRING;
2684 tv->vval.v_string = res;
2685 ++ectx->ec_stack.ga_len;
2686 }
2687 break;
2688
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002689 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02002690#ifdef FEAT_QUICKFIX
Bram Moolenaar397a87a2022-03-20 21:14:15 +00002691 force_abort = TRUE;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002692 if (trigger_cexpr_autocmd(iptr->isn_arg.number) == FAIL)
2693 goto on_error;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00002694 force_abort = FALSE;
Bram Moolenaarb7c97812021-05-05 22:51:39 +02002695#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002696 break;
2697
2698 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02002699#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002700 {
2701 exarg_T ea;
2702 int res;
2703
2704 CLEAR_FIELD(ea);
2705 ea.cmdidx = iptr->isn_arg.cexpr.cexpr_ref->cer_cmdidx;
2706 ea.forceit = iptr->isn_arg.cexpr.cexpr_ref->cer_forceit;
2707 ea.cmdlinep = &iptr->isn_arg.cexpr.cexpr_ref->cer_cmdline;
2708 --ectx->ec_stack.ga_len;
2709 tv = STACK_TV_BOT(0);
2710 res = cexpr_core(&ea, tv);
2711 clear_tv(tv);
2712 if (res == FAIL)
2713 goto on_error;
2714 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02002715#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002716 break;
2717
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002718 // execute Ex command from pieces on the stack
2719 case ISN_EXECCONCAT:
2720 {
2721 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +02002722 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002723 int pass;
2724 int i;
2725 char_u *cmd = NULL;
2726 char_u *str;
2727
2728 for (pass = 1; pass <= 2; ++pass)
2729 {
2730 for (i = 0; i < count; ++i)
2731 {
2732 tv = STACK_TV_BOT(i - count);
2733 str = tv->vval.v_string;
2734 if (str != NULL && *str != NUL)
2735 {
2736 if (pass == 2)
2737 STRCPY(cmd + len, str);
2738 len += STRLEN(str);
2739 }
2740 if (pass == 2)
2741 clear_tv(tv);
2742 }
2743 if (pass == 1)
2744 {
2745 cmd = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002746 if (unlikely(cmd == NULL))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002747 goto theend;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002748 len = 0;
2749 }
2750 }
2751
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02002752 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002753 do_cmdline_cmd(cmd);
2754 vim_free(cmd);
2755 }
2756 break;
2757
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002758 // execute :echo {string} ...
2759 case ISN_ECHO:
2760 {
2761 int count = iptr->isn_arg.echo.echo_count;
2762 int atstart = TRUE;
2763 int needclr = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002764 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002765
2766 for (idx = 0; idx < count; ++idx)
2767 {
2768 tv = STACK_TV_BOT(idx - count);
2769 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
2770 &atstart, &needclr);
2771 clear_tv(tv);
2772 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01002773 if (needclr)
2774 msg_clr_eos();
Bram Moolenaar4c137212021-04-19 16:48:48 +02002775 ectx->ec_stack.ga_len -= count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002776 }
2777 break;
2778
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002779 // :execute {string} ...
2780 // :echomsg {string} ...
Bram Moolenaar7de62622021-08-07 15:05:47 +02002781 // :echoconsole {string} ...
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002782 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01002783 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002784 case ISN_ECHOMSG:
Bram Moolenaar7de62622021-08-07 15:05:47 +02002785 case ISN_ECHOCONSOLE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002786 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01002787 {
2788 int count = iptr->isn_arg.number;
2789 garray_T ga;
2790 char_u buf[NUMBUFLEN];
2791 char_u *p;
2792 int len;
2793 int failed = FALSE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002794 int idx;
Bram Moolenaarad39c092020-02-26 18:23:43 +01002795
2796 ga_init2(&ga, 1, 80);
2797 for (idx = 0; idx < count; ++idx)
2798 {
2799 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002800 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01002801 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002802 if (tv->v_type == VAR_CHANNEL
2803 || tv->v_type == VAR_JOB)
2804 {
2805 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar68db9962021-05-09 23:19:22 +02002806 semsg(_(e_using_invalid_value_as_string_str),
2807 vartype_name(tv->v_type));
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002808 break;
2809 }
2810 else
2811 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01002812 }
2813 else
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002814 p = tv_stringify(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01002815
2816 len = (int)STRLEN(p);
Bram Moolenaar35578162021-08-02 19:10:38 +02002817 if (GA_GROW_FAILS(&ga, len + 2))
Bram Moolenaarad39c092020-02-26 18:23:43 +01002818 failed = TRUE;
2819 else
2820 {
2821 if (ga.ga_len > 0)
2822 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
2823 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
2824 ga.ga_len += len;
2825 }
2826 clear_tv(tv);
2827 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02002828 ectx->ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002829 if (failed)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01002830 {
2831 ga_clear(&ga);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002832 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01002833 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01002834
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002835 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002836 {
2837 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02002838 {
2839 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002840 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01002841 if (did_emsg)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01002842 {
2843 ga_clear(&ga);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01002844 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01002845 }
Bram Moolenaar430deb12020-08-23 16:29:11 +02002846 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002847 else
2848 {
2849 msg_sb_eol();
2850 if (iptr->isn_type == ISN_ECHOMSG)
2851 {
2852 msg_attr(ga.ga_data, echo_attr);
2853 out_flush();
2854 }
Bram Moolenaar7de62622021-08-07 15:05:47 +02002855 else if (iptr->isn_type == ISN_ECHOCONSOLE)
2856 {
2857 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data),
2858 TRUE);
2859 ui_write((char_u *)"\r\n", 2, TRUE);
2860 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002861 else
2862 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002863 SOURCING_LNUM = iptr->isn_lnum;
2864 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002865 }
2866 }
2867 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01002868 ga_clear(&ga);
2869 }
2870 break;
2871
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002872 // load local variable or argument
2873 case ISN_LOAD:
Bram Moolenaar35578162021-08-02 19:10:38 +02002874 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002875 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002876 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002877 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002878 break;
2879
2880 // load v: variable
2881 case ISN_LOADV:
Bram Moolenaar35578162021-08-02 19:10:38 +02002882 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002883 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002884 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002885 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002886 break;
2887
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002888 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002889 case ISN_LOADSCRIPT:
2890 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01002891 scriptref_T *sref = iptr->isn_arg.script.scriptref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002892 svar_T *sv;
2893
Bram Moolenaar6db660b2021-08-01 14:08:54 +02002894 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01002895 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002896 goto theend;
Bram Moolenaar859cc212022-03-28 15:22:35 +01002897 allocate_if_null(sv);
Bram Moolenaar35578162021-08-02 19:10:38 +02002898 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002899 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002900 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002901 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002902 }
2903 break;
2904
2905 // load s: variable in old script
2906 case ISN_LOADS:
2907 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002908 hashtab_T *ht = &SCRIPT_VARS(
2909 iptr->isn_arg.loadstore.ls_sid);
2910 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002911 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002912
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002913 if (di == NULL)
2914 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002915 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002916 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002917 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002918 }
2919 else
2920 {
Bram Moolenaar35578162021-08-02 19:10:38 +02002921 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002922 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002923 copy_tv(&di->di_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002924 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002925 }
2926 }
2927 break;
2928
Bram Moolenaard3aac292020-04-19 14:32:17 +02002929 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002930 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02002931 case ISN_LOADB:
2932 case ISN_LOADW:
2933 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002934 {
Bram Moolenaar06b77222022-01-25 15:51:56 +00002935 int res = load_namespace_var(ectx, iptr->isn_type, iptr);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002936
Bram Moolenaar06b77222022-01-25 15:51:56 +00002937 if (res == NOTDONE)
Bram Moolenaarcbbc48f2022-01-18 12:58:28 +00002938 goto theend;
Bram Moolenaar06b77222022-01-25 15:51:56 +00002939 if (res == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002940 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002941 }
Bram Moolenaar06b77222022-01-25 15:51:56 +00002942
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002943 break;
2944
Bram Moolenaar03290b82020-12-19 16:30:44 +01002945 // load autoload variable
2946 case ISN_LOADAUTO:
2947 {
2948 char_u *name = iptr->isn_arg.string;
2949
Bram Moolenaar35578162021-08-02 19:10:38 +02002950 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002951 goto theend;
Bram Moolenaar03290b82020-12-19 16:30:44 +01002952 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002953 if (eval_variable(name, (int)STRLEN(name), 0,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002954 STACK_TV_BOT(0), NULL, EVAL_VAR_VERBOSE) == FAIL)
Bram Moolenaar03290b82020-12-19 16:30:44 +01002955 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002956 ++ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01002957 }
2958 break;
2959
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002960 // load g:/b:/w:/t: namespace
2961 case ISN_LOADGDICT:
2962 case ISN_LOADBDICT:
2963 case ISN_LOADWDICT:
2964 case ISN_LOADTDICT:
2965 {
2966 dict_T *d = NULL;
2967
2968 switch (iptr->isn_type)
2969 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02002970 case ISN_LOADGDICT: d = get_globvar_dict(); break;
2971 case ISN_LOADBDICT: d = curbuf->b_vars; break;
2972 case ISN_LOADWDICT: d = curwin->w_vars; break;
2973 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002974 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002975 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002976 }
Bram Moolenaar35578162021-08-02 19:10:38 +02002977 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002978 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002979 tv = STACK_TV_BOT(0);
2980 tv->v_type = VAR_DICT;
2981 tv->v_lock = 0;
2982 tv->vval.v_dict = d;
Bram Moolenaar1bd3cb22021-02-24 12:27:31 +01002983 ++d->dv_refcount;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002984 ++ectx->ec_stack.ga_len;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002985 }
2986 break;
2987
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002988 // load &option
2989 case ISN_LOADOPT:
2990 {
2991 typval_T optval;
2992 char_u *name = iptr->isn_arg.string;
2993
Bram Moolenaara8c17702020-04-01 21:17:24 +02002994 // This is not expected to fail, name is checked during
2995 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar35578162021-08-02 19:10:38 +02002996 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002997 goto theend;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002998 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002999 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003000 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003001 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003002 }
3003 break;
3004
3005 // load $ENV
3006 case ISN_LOADENV:
3007 {
3008 typval_T optval;
3009 char_u *name = iptr->isn_arg.string;
3010
Bram Moolenaar35578162021-08-02 19:10:38 +02003011 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003012 goto theend;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003013 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003014 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003015 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003016 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003017 }
3018 break;
3019
3020 // load @register
3021 case ISN_LOADREG:
Bram Moolenaar35578162021-08-02 19:10:38 +02003022 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003023 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003024 tv = STACK_TV_BOT(0);
3025 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003026 tv->v_lock = 0;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02003027 // This may result in NULL, which should be equivalent to an
3028 // empty string.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003029 tv->vval.v_string = get_reg_contents(
3030 iptr->isn_arg.number, GREG_EXPR_SRC);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003031 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003032 break;
3033
3034 // store local variable
3035 case ISN_STORE:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003036 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003037 tv = STACK_TV_VAR(iptr->isn_arg.number);
3038 clear_tv(tv);
3039 *tv = *STACK_TV_BOT(0);
3040 break;
3041
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003042 // store s: variable in old script
3043 case ISN_STORES:
3044 {
3045 hashtab_T *ht = &SCRIPT_VARS(
3046 iptr->isn_arg.loadstore.ls_sid);
3047 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003048 dictitem_T *di = find_var_in_ht(ht, 0, name + 2, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003049
Bram Moolenaar4c137212021-04-19 16:48:48 +02003050 --ectx->ec_stack.ga_len;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003051 if (di == NULL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003052 store_var(name, STACK_TV_BOT(0));
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003053 else
3054 {
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02003055 SOURCING_LNUM = iptr->isn_lnum;
3056 if (var_check_permission(di, name) == FAIL)
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003057 {
3058 clear_tv(STACK_TV_BOT(0));
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02003059 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003060 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003061 clear_tv(&di->di_tv);
3062 di->di_tv = *STACK_TV_BOT(0);
3063 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003064 }
3065 break;
3066
3067 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003068 case ISN_STORESCRIPT:
3069 {
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003070 scriptref_T *sref = iptr->isn_arg.script.scriptref;
3071 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003072
Bram Moolenaar6db660b2021-08-01 14:08:54 +02003073 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003074 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003075 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003076 --ectx->ec_stack.ga_len;
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003077
3078 // "const" and "final" are checked at compile time, locking
3079 // the value needs to be checked here.
3080 SOURCING_LNUM = iptr->isn_lnum;
3081 if (value_check_lock(sv->sv_tv->v_lock, sv->sv_name, FALSE))
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003082 {
3083 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003084 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003085 }
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003086
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003087 clear_tv(sv->sv_tv);
3088 *sv->sv_tv = *STACK_TV_BOT(0);
3089 }
3090 break;
3091
3092 // store option
3093 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003094 case ISN_STOREFUNCOPT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003095 {
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003096 char_u *opt_name = iptr->isn_arg.storeopt.so_name;
3097 int opt_flags = iptr->isn_arg.storeopt.so_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003098 long n = 0;
3099 char_u *s = NULL;
3100 char *msg;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003101 char_u numbuf[NUMBUFLEN];
3102 char_u *tofree = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003103
Bram Moolenaar4c137212021-04-19 16:48:48 +02003104 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003105 tv = STACK_TV_BOT(0);
3106 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01003107 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003108 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01003109 if (s == NULL)
3110 s = (char_u *)"";
3111 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003112 else if (iptr->isn_type == ISN_STOREFUNCOPT)
3113 {
3114 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003115 // If the option can be set to a function reference or
3116 // a lambda and the passed value is a function
3117 // reference, then convert it to the name (string) of
3118 // the function reference.
3119 s = tv2string(tv, &tofree, numbuf, 0);
3120 if (s == NULL || *s == NUL)
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003121 {
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003122 // cannot happen?
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003123 clear_tv(tv);
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003124 vim_free(tofree);
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003125 goto on_error;
3126 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003127 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003128 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003129 // must be VAR_NUMBER, CHECKTYPE makes sure
3130 n = tv->vval.v_number;
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003131 msg = set_option_value(opt_name, n, s, opt_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02003132 clear_tv(tv);
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003133 vim_free(tofree);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003134 if (msg != NULL)
3135 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003136 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003137 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02003138 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003139 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003140 }
3141 break;
3142
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003143 // store $ENV
3144 case ISN_STOREENV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003145 --ectx->ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003146 tv = STACK_TV_BOT(0);
3147 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
3148 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003149 break;
3150
3151 // store @r
3152 case ISN_STOREREG:
3153 {
3154 int reg = iptr->isn_arg.number;
3155
Bram Moolenaar4c137212021-04-19 16:48:48 +02003156 --ectx->ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01003157 tv = STACK_TV_BOT(0);
Bram Moolenaar74f4a962021-06-17 21:03:07 +02003158 write_reg_contents(reg, tv_get_string(tv), -1, FALSE);
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01003159 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003160 }
3161 break;
3162
3163 // store v: variable
3164 case ISN_STOREV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003165 --ectx->ec_stack.ga_len;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003166 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
3167 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003168 // should not happen, type is checked when compiling
3169 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003170 break;
3171
Bram Moolenaard3aac292020-04-19 14:32:17 +02003172 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003173 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02003174 case ISN_STOREB:
3175 case ISN_STOREW:
3176 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003177 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003178 dictitem_T *di;
3179 hashtab_T *ht;
3180 char_u *name = iptr->isn_arg.string + 2;
3181
Bram Moolenaard3aac292020-04-19 14:32:17 +02003182 switch (iptr->isn_type)
3183 {
3184 case ISN_STOREG:
3185 ht = get_globvar_ht();
3186 break;
3187 case ISN_STOREB:
3188 ht = &curbuf->b_vars->dv_hashtab;
3189 break;
3190 case ISN_STOREW:
3191 ht = &curwin->w_vars->dv_hashtab;
3192 break;
3193 case ISN_STORET:
3194 ht = &curtab->tp_vars->dv_hashtab;
3195 break;
3196 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003197 goto theend;
Bram Moolenaard3aac292020-04-19 14:32:17 +02003198 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003199
Bram Moolenaar4c137212021-04-19 16:48:48 +02003200 --ectx->ec_stack.ga_len;
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003201 di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003202 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003203 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003204 else
3205 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003206 SOURCING_LNUM = iptr->isn_lnum;
3207 if (var_check_permission(di, name) == FAIL)
3208 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003209 clear_tv(&di->di_tv);
3210 di->di_tv = *STACK_TV_BOT(0);
3211 }
3212 }
3213 break;
3214
Bram Moolenaar03290b82020-12-19 16:30:44 +01003215 // store an autoload variable
3216 case ISN_STOREAUTO:
3217 SOURCING_LNUM = iptr->isn_lnum;
3218 set_var(iptr->isn_arg.string, STACK_TV_BOT(-1), TRUE);
3219 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003220 --ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003221 break;
3222
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003223 // store number in local variable
3224 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01003225 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003226 clear_tv(tv);
3227 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01003228 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003229 break;
3230
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01003231 // store value in list or dict variable
3232 case ISN_STOREINDEX:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003233 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003234 int res = execute_storeindex(iptr, ectx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003235
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003236 if (res == FAIL)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02003237 goto on_error;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003238 if (res == NOTDONE)
3239 goto theend;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003240 }
3241 break;
3242
Bram Moolenaarea5c8982022-02-17 14:42:02 +00003243 // store value in list or blob range
Bram Moolenaar68452172021-04-12 21:21:02 +02003244 case ISN_STORERANGE:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003245 if (execute_storerange(iptr, ectx) == FAIL)
3246 goto on_error;
Bram Moolenaar68452172021-04-12 21:21:02 +02003247 break;
3248
Bram Moolenaar0186e582021-01-10 18:33:11 +01003249 // load or store variable or argument from outer scope
3250 case ISN_LOADOUTER:
3251 case ISN_STOREOUTER:
3252 {
3253 int depth = iptr->isn_arg.outer.outer_depth;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02003254 outer_T *outer = ectx->ec_outer_ref == NULL ? NULL
3255 : ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003256
3257 while (depth > 1 && outer != NULL)
3258 {
3259 outer = outer->out_up;
3260 --depth;
3261 }
3262 if (outer == NULL)
3263 {
3264 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar69c76172021-12-02 16:38:52 +00003265 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx
3266 || ectx->ec_outer_ref == NULL)
3267 // Possibly :def function called from legacy
3268 // context.
3269 emsg(_(e_closure_called_from_invalid_context));
3270 else
3271 iemsg("LOADOUTER depth more than scope levels");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003272 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003273 }
3274 tv = ((typval_T *)outer->out_stack->ga_data)
3275 + outer->out_frame_idx + STACK_FRAME_SIZE
3276 + iptr->isn_arg.outer.outer_idx;
3277 if (iptr->isn_type == ISN_LOADOUTER)
3278 {
Bram Moolenaar35578162021-08-02 19:10:38 +02003279 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003280 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003281 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003282 ++ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003283 }
3284 else
3285 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003286 --ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003287 clear_tv(tv);
3288 *tv = *STACK_TV_BOT(0);
3289 }
3290 }
3291 break;
3292
Bram Moolenaar752fc692021-01-04 21:57:11 +01003293 // unlet item in list or dict variable
3294 case ISN_UNLETINDEX:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003295 if (execute_unletindex(iptr, ectx) == FAIL)
3296 goto on_error;
Bram Moolenaar752fc692021-01-04 21:57:11 +01003297 break;
3298
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01003299 // unlet range of items in list variable
3300 case ISN_UNLETRANGE:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003301 if (execute_unletrange(iptr, ectx) == FAIL)
3302 goto on_error;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01003303 break;
3304
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003305 // push constant
3306 case ISN_PUSHNR:
3307 case ISN_PUSHBOOL:
3308 case ISN_PUSHSPEC:
3309 case ISN_PUSHF:
3310 case ISN_PUSHS:
3311 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003312 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003313 case ISN_PUSHCHANNEL:
3314 case ISN_PUSHJOB:
Bram Moolenaar35578162021-08-02 19:10:38 +02003315 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003316 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003317 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003318 tv->v_lock = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003319 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003320 switch (iptr->isn_type)
3321 {
3322 case ISN_PUSHNR:
3323 tv->v_type = VAR_NUMBER;
3324 tv->vval.v_number = iptr->isn_arg.number;
3325 break;
3326 case ISN_PUSHBOOL:
3327 tv->v_type = VAR_BOOL;
3328 tv->vval.v_number = iptr->isn_arg.number;
3329 break;
3330 case ISN_PUSHSPEC:
3331 tv->v_type = VAR_SPECIAL;
3332 tv->vval.v_number = iptr->isn_arg.number;
3333 break;
3334#ifdef FEAT_FLOAT
3335 case ISN_PUSHF:
3336 tv->v_type = VAR_FLOAT;
3337 tv->vval.v_float = iptr->isn_arg.fnumber;
3338 break;
3339#endif
3340 case ISN_PUSHBLOB:
3341 blob_copy(iptr->isn_arg.blob, tv);
3342 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003343 case ISN_PUSHFUNC:
3344 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01003345 if (iptr->isn_arg.string == NULL)
3346 tv->vval.v_string = NULL;
3347 else
3348 tv->vval.v_string =
3349 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003350 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003351 case ISN_PUSHCHANNEL:
3352#ifdef FEAT_JOB_CHANNEL
3353 tv->v_type = VAR_CHANNEL;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003354 tv->vval.v_channel = NULL;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003355#endif
3356 break;
3357 case ISN_PUSHJOB:
3358#ifdef FEAT_JOB_CHANNEL
3359 tv->v_type = VAR_JOB;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003360 tv->vval.v_job = NULL;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003361#endif
3362 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003363 default:
3364 tv->v_type = VAR_STRING;
Bram Moolenaarf8691002022-03-10 12:20:53 +00003365 tv->vval.v_string = iptr->isn_arg.string == NULL
3366 ? NULL : vim_strsave(iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003367 }
3368 break;
3369
Bram Moolenaar06b77222022-01-25 15:51:56 +00003370 case ISN_AUTOLOAD:
3371 {
3372 char_u *name = iptr->isn_arg.string;
3373
3374 (void)script_autoload(name, FALSE);
3375 if (find_func(name, TRUE))
3376 {
3377 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
3378 goto theend;
3379 tv = STACK_TV_BOT(0);
3380 tv->v_lock = 0;
3381 ++ectx->ec_stack.ga_len;
3382 tv->v_type = VAR_FUNC;
3383 tv->vval.v_string = vim_strsave(name);
3384 }
3385 else
3386 {
3387 int res = load_namespace_var(ectx, ISN_LOADG, iptr);
3388
3389 if (res == NOTDONE)
3390 goto theend;
3391 if (res == FAIL)
3392 goto on_error;
3393 }
3394 }
3395 break;
3396
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003397 case ISN_UNLET:
3398 if (do_unlet(iptr->isn_arg.unlet.ul_name,
3399 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003400 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003401 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02003402 case ISN_UNLETENV:
3403 vim_unsetenv(iptr->isn_arg.unlet.ul_name);
3404 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003405
Bram Moolenaaraacc9662021-08-13 19:40:51 +02003406 case ISN_LOCKUNLOCK:
3407 {
3408 typval_T *lval_root_save = lval_root;
3409 int res;
3410
3411 // Stack has the local variable, argument the whole :lock
3412 // or :unlock command, like ISN_EXEC.
3413 --ectx->ec_stack.ga_len;
3414 lval_root = STACK_TV_BOT(0);
3415 res = exec_command(iptr);
3416 clear_tv(lval_root);
3417 lval_root = lval_root_save;
3418 if (res == FAIL)
3419 goto on_error;
3420 }
3421 break;
3422
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02003423 case ISN_LOCKCONST:
3424 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
3425 break;
3426
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003427 // create a list from items on the stack; uses a single allocation
3428 // for the list header and the items
3429 case ISN_NEWLIST:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003430 if (exe_newlist(iptr->isn_arg.number, ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003431 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003432 break;
3433
3434 // create a dict from items on the stack
3435 case ISN_NEWDICT:
3436 {
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01003437 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003438
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01003439 SOURCING_LNUM = iptr->isn_lnum;
3440 res = exe_newdict(iptr->isn_arg.number, ectx);
3441 if (res == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003442 goto theend;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01003443 if (res == MAYBE)
3444 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003445 }
3446 break;
3447
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003448 // create a partial with NULL value
3449 case ISN_NEWPARTIAL:
3450 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
3451 goto theend;
3452 ++ectx->ec_stack.ga_len;
3453 tv = STACK_TV_BOT(-1);
3454 tv->v_type = VAR_PARTIAL;
3455 tv->v_lock = 0;
3456 tv->vval.v_partial = NULL;
3457 break;
3458
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003459 // call a :def function
3460 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02003461 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003462 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
3463 NULL,
3464 iptr->isn_arg.dfunc.cdf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003465 ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003466 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003467 break;
3468
3469 // call a builtin function
3470 case ISN_BCALL:
3471 SOURCING_LNUM = iptr->isn_lnum;
3472 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
3473 iptr->isn_arg.bfunc.cbf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003474 ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02003475 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003476 break;
3477
3478 // call a funcref or partial
3479 case ISN_PCALL:
3480 {
3481 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
3482 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003483 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003484
3485 SOURCING_LNUM = iptr->isn_lnum;
3486 if (pfunc->cpf_top)
3487 {
3488 // funcref is above the arguments
3489 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
3490 }
3491 else
3492 {
3493 // Get the funcref from the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02003494 --ectx->ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003495 partial_tv = *STACK_TV_BOT(0);
3496 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003497 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003498 r = call_partial(tv, pfunc->cpf_argcount, ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003499 if (tv == &partial_tv)
3500 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003501 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02003502 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003503 }
3504 break;
3505
Bram Moolenaarbd5da372020-03-31 23:13:10 +02003506 case ISN_PCALL_END:
3507 // PCALL finished, arguments have been consumed and replaced by
3508 // the return value. Now clear the funcref from the stack,
3509 // and move the return value in its place.
Bram Moolenaar4c137212021-04-19 16:48:48 +02003510 --ectx->ec_stack.ga_len;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02003511 clear_tv(STACK_TV_BOT(-1));
3512 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
3513 break;
3514
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003515 // call a user defined function or funcref/partial
3516 case ISN_UCALL:
3517 {
3518 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
3519
3520 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003521 if (call_eval_func(cufunc->cuf_name, cufunc->cuf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003522 ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02003523 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003524 }
3525 break;
3526
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02003527 // return from a :def function call without a value
3528 case ISN_RETURN_VOID:
Bram Moolenaar35578162021-08-02 19:10:38 +02003529 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003530 goto theend;
Bram Moolenaar299f3032021-01-08 20:53:09 +01003531 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003532 ++ectx->ec_stack.ga_len;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02003533 tv->v_type = VAR_VOID;
Bram Moolenaar299f3032021-01-08 20:53:09 +01003534 tv->vval.v_number = 0;
3535 tv->v_lock = 0;
3536 // FALLTHROUGH
3537
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02003538 // return from a :def function call with what is on the stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003539 case ISN_RETURN:
3540 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003541 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003542 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01003543
3544 if (trystack->ga_len > 0)
3545 trycmd = ((trycmd_T *)trystack->ga_data)
3546 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003547 if (trycmd != NULL
Bram Moolenaar4c137212021-04-19 16:48:48 +02003548 && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003549 {
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01003550 // jump to ":finally" or ":endtry"
3551 if (trycmd->tcd_finally_idx != 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02003552 ectx->ec_iidx = trycmd->tcd_finally_idx;
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01003553 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02003554 ectx->ec_iidx = trycmd->tcd_endtry_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003555 trycmd->tcd_return = TRUE;
3556 }
3557 else
Bram Moolenaard032f342020-07-18 18:13:02 +02003558 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003559 }
3560 break;
3561
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02003562 // push a partial, a reference to a compiled function
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003563 case ISN_FUNCREF:
3564 {
Bram Moolenaarf112f302020-12-20 17:47:52 +01003565 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar38453522021-11-28 22:00:12 +00003566 ufunc_T *ufunc;
3567 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003568
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003569 if (pt == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003570 goto theend;
Bram Moolenaar35578162021-08-02 19:10:38 +02003571 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003572 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003573 vim_free(pt);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003574 goto theend;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003575 }
Bram Moolenaar38453522021-11-28 22:00:12 +00003576 if (funcref->fr_func_name == NULL)
3577 {
3578 dfunc_T *pt_dfunc = ((dfunc_T *)def_functions.ga_data)
3579 + funcref->fr_dfunc_idx;
3580
3581 ufunc = pt_dfunc->df_ufunc;
3582 }
3583 else
3584 {
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003585 ufunc = find_func(funcref->fr_func_name, FALSE);
Bram Moolenaar38453522021-11-28 22:00:12 +00003586 }
Bram Moolenaar56acd1f2022-02-18 13:24:52 +00003587 if (ufunc == NULL)
3588 {
3589 SOURCING_LNUM = iptr->isn_lnum;
3590 iemsg("ufunc unexpectedly NULL for FUNCREF");
3591 goto theend;
3592 }
Bram Moolenaar38453522021-11-28 22:00:12 +00003593 if (fill_partial_and_closure(pt, ufunc, ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003594 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003595 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003596 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003597 tv->vval.v_partial = pt;
3598 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003599 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003600 }
3601 break;
3602
Bram Moolenaar38ddf332020-07-31 22:05:04 +02003603 // Create a global function from a lambda.
3604 case ISN_NEWFUNC:
3605 {
3606 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
3607
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01003608 if (copy_func(newfunc->nf_lambda, newfunc->nf_global,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003609 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003610 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02003611 }
3612 break;
3613
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01003614 // List functions
3615 case ISN_DEF:
3616 if (iptr->isn_arg.string == NULL)
3617 list_functions(NULL);
3618 else
3619 {
Bram Moolenaar14336722022-01-08 16:02:59 +00003620 exarg_T ea;
3621 garray_T lines_to_free;
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01003622
3623 CLEAR_FIELD(ea);
3624 ea.cmd = ea.arg = iptr->isn_arg.string;
Bram Moolenaar14336722022-01-08 16:02:59 +00003625 ga_init2(&lines_to_free, sizeof(char_u *), 50);
3626 define_function(&ea, NULL, &lines_to_free);
3627 ga_clear_strings(&lines_to_free);
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01003628 }
3629 break;
3630
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003631 // jump if a condition is met
3632 case ISN_JUMP:
3633 {
3634 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003635 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003636 int jump = TRUE;
3637
3638 if (when != JUMP_ALWAYS)
3639 {
3640 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003641 if (when == JUMP_IF_COND_FALSE
Bram Moolenaar13106602020-10-04 16:06:05 +02003642 || when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003643 || when == JUMP_IF_COND_TRUE)
3644 {
3645 SOURCING_LNUM = iptr->isn_lnum;
3646 jump = tv_get_bool_chk(tv, &error);
3647 if (error)
3648 goto on_error;
3649 }
3650 else
3651 jump = tv2bool(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003652 if (when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003653 || when == JUMP_AND_KEEP_IF_FALSE
3654 || when == JUMP_IF_COND_FALSE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003655 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01003656 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003657 {
3658 // drop the value from the stack
3659 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003660 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003661 }
3662 }
3663 if (jump)
Bram Moolenaar4c137212021-04-19 16:48:48 +02003664 ectx->ec_iidx = iptr->isn_arg.jump.jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003665 }
3666 break;
3667
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02003668 // Jump if an argument with a default value was already set and not
3669 // v:none.
3670 case ISN_JUMP_IF_ARG_SET:
3671 tv = STACK_TV_VAR(iptr->isn_arg.jumparg.jump_arg_off);
3672 if (tv->v_type != VAR_UNKNOWN
3673 && !(tv->v_type == VAR_SPECIAL
3674 && tv->vval.v_number == VVAL_NONE))
Bram Moolenaar4c137212021-04-19 16:48:48 +02003675 ectx->ec_iidx = iptr->isn_arg.jumparg.jump_where;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02003676 break;
3677
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003678 // top of a for loop
3679 case ISN_FOR:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003680 if (execute_for(iptr, ectx) == FAIL)
3681 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003682 break;
3683
3684 // start of ":try" block
3685 case ISN_TRY:
3686 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01003687 trycmd_T *trycmd = NULL;
3688
Bram Moolenaar35578162021-08-02 19:10:38 +02003689 if (GA_GROW_FAILS(&ectx->ec_trystack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003690 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003691 trycmd = ((trycmd_T *)ectx->ec_trystack.ga_data)
3692 + ectx->ec_trystack.ga_len;
3693 ++ectx->ec_trystack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003694 ++trylevel;
Bram Moolenaar8d4be892021-02-13 18:33:02 +01003695 CLEAR_POINTER(trycmd);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003696 trycmd->tcd_frame_idx = ectx->ec_frame_idx;
3697 trycmd->tcd_stack_len = ectx->ec_stack.ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01003698 trycmd->tcd_catch_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00003699 iptr->isn_arg.tryref.try_ref->try_catch;
Bram Moolenaara91a7132021-03-25 21:12:15 +01003700 trycmd->tcd_finally_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00003701 iptr->isn_arg.tryref.try_ref->try_finally;
Bram Moolenaara91a7132021-03-25 21:12:15 +01003702 trycmd->tcd_endtry_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00003703 iptr->isn_arg.tryref.try_ref->try_endtry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003704 }
3705 break;
3706
3707 case ISN_PUSHEXC:
3708 if (current_exception == NULL)
3709 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003710 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003711 iemsg("Evaluating catch while current_exception is NULL");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003712 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003713 }
Bram Moolenaar35578162021-08-02 19:10:38 +02003714 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003715 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003716 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003717 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003718 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003719 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003720 tv->vval.v_string = vim_strsave(
3721 (char_u *)current_exception->value);
3722 break;
3723
3724 case ISN_CATCH:
3725 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003726 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003727
Bram Moolenaar4c137212021-04-19 16:48:48 +02003728 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003729 if (trystack->ga_len > 0)
3730 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01003731 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003732 + trystack->ga_len - 1;
3733 trycmd->tcd_caught = TRUE;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003734 trycmd->tcd_did_throw = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003735 }
3736 did_emsg = got_int = did_throw = FALSE;
Bram Moolenaar1430cee2021-01-17 19:20:32 +01003737 force_abort = need_rethrow = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003738 catch_exception(current_exception);
3739 }
3740 break;
3741
Bram Moolenaarc150c092021-02-13 15:02:46 +01003742 case ISN_TRYCONT:
3743 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003744 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003745 trycont_T *trycont = &iptr->isn_arg.trycont;
3746 int i;
3747 trycmd_T *trycmd;
3748 int iidx = trycont->tct_where;
3749
3750 if (trystack->ga_len < trycont->tct_levels)
3751 {
3752 siemsg("TRYCONT: expected %d levels, found %d",
3753 trycont->tct_levels, trystack->ga_len);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003754 goto theend;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003755 }
3756 // Make :endtry jump to any outer try block and the last
3757 // :endtry inside the loop to the loop start.
3758 for (i = trycont->tct_levels; i > 0; --i)
3759 {
3760 trycmd = ((trycmd_T *)trystack->ga_data)
3761 + trystack->ga_len - i;
Bram Moolenaar2e34c342021-03-14 12:13:33 +01003762 // Add one to tcd_cont to be able to jump to
3763 // instruction with index zero.
3764 trycmd->tcd_cont = iidx + 1;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003765 iidx = trycmd->tcd_finally_idx == 0
3766 ? trycmd->tcd_endtry_idx : trycmd->tcd_finally_idx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003767 }
3768 // jump to :finally or :endtry of current try statement
Bram Moolenaar4c137212021-04-19 16:48:48 +02003769 ectx->ec_iidx = iidx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003770 }
3771 break;
3772
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003773 case ISN_FINALLY:
3774 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003775 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003776 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
3777 + trystack->ga_len - 1;
3778
3779 // Reset the index to avoid a return statement jumps here
3780 // again.
3781 trycmd->tcd_finally_idx = 0;
3782 break;
3783 }
3784
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003785 // end of ":try" block
3786 case ISN_ENDTRY:
3787 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003788 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003789
3790 if (trystack->ga_len > 0)
3791 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003792 trycmd_T *trycmd;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003793
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003794 --trystack->ga_len;
3795 --trylevel;
3796 trycmd = ((trycmd_T *)trystack->ga_data)
3797 + trystack->ga_len;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003798 if (trycmd->tcd_did_throw)
3799 did_throw = TRUE;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01003800 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003801 {
3802 // discard the exception
3803 if (caught_stack == current_exception)
3804 caught_stack = caught_stack->caught;
3805 discard_current_exception();
3806 }
3807
3808 if (trycmd->tcd_return)
Bram Moolenaard032f342020-07-18 18:13:02 +02003809 goto func_return;
Bram Moolenaard9d77892021-02-12 21:32:47 +01003810
Bram Moolenaar4c137212021-04-19 16:48:48 +02003811 while (ectx->ec_stack.ga_len > trycmd->tcd_stack_len)
Bram Moolenaard9d77892021-02-12 21:32:47 +01003812 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003813 --ectx->ec_stack.ga_len;
Bram Moolenaard9d77892021-02-12 21:32:47 +01003814 clear_tv(STACK_TV_BOT(0));
3815 }
Bram Moolenaar8d4be892021-02-13 18:33:02 +01003816 if (trycmd->tcd_cont != 0)
Bram Moolenaarc150c092021-02-13 15:02:46 +01003817 // handling :continue: jump to outer try block or
3818 // start of the loop
Bram Moolenaar4c137212021-04-19 16:48:48 +02003819 ectx->ec_iidx = trycmd->tcd_cont - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003820 }
3821 }
3822 break;
3823
3824 case ISN_THROW:
Bram Moolenaar8f81b222021-01-14 21:47:06 +01003825 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003826 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02003827
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003828 if (trystack->ga_len == 0 && trylevel == 0 && emsg_silent)
3829 {
3830 // throwing an exception while using "silent!" causes
3831 // the function to abort but not display an error.
3832 tv = STACK_TV_BOT(-1);
3833 clear_tv(tv);
3834 tv->v_type = VAR_NUMBER;
3835 tv->vval.v_number = 0;
3836 goto done;
3837 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003838 --ectx->ec_stack.ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003839 tv = STACK_TV_BOT(0);
3840 if (tv->vval.v_string == NULL
3841 || *skipwhite(tv->vval.v_string) == NUL)
3842 {
3843 vim_free(tv->vval.v_string);
3844 SOURCING_LNUM = iptr->isn_lnum;
3845 emsg(_(e_throw_with_empty_string));
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003846 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003847 }
3848
3849 // Inside a "catch" we need to first discard the caught
3850 // exception.
3851 if (trystack->ga_len > 0)
3852 {
3853 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
3854 + trystack->ga_len - 1;
3855 if (trycmd->tcd_caught && current_exception != NULL)
3856 {
3857 // discard the exception
3858 if (caught_stack == current_exception)
3859 caught_stack = caught_stack->caught;
3860 discard_current_exception();
3861 trycmd->tcd_caught = FALSE;
3862 }
3863 }
3864
Bram Moolenaar90a57162022-02-12 14:23:17 +00003865 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003866 if (throw_exception(tv->vval.v_string, ET_USER, NULL)
3867 == FAIL)
3868 {
3869 vim_free(tv->vval.v_string);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003870 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003871 }
3872 did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003873 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003874 break;
3875
3876 // compare with special values
3877 case ISN_COMPAREBOOL:
3878 case ISN_COMPARESPECIAL:
3879 {
3880 typval_T *tv1 = STACK_TV_BOT(-2);
3881 typval_T *tv2 = STACK_TV_BOT(-1);
3882 varnumber_T arg1 = tv1->vval.v_number;
3883 varnumber_T arg2 = tv2->vval.v_number;
3884 int res;
3885
3886 switch (iptr->isn_arg.op.op_type)
3887 {
3888 case EXPR_EQUAL: res = arg1 == arg2; break;
3889 case EXPR_NEQUAL: res = arg1 != arg2; break;
3890 default: res = 0; break;
3891 }
3892
Bram Moolenaar4c137212021-04-19 16:48:48 +02003893 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003894 tv1->v_type = VAR_BOOL;
3895 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
3896 }
3897 break;
3898
Bram Moolenaar7a222242022-03-01 19:23:24 +00003899 case ISN_COMPARENULL:
3900 {
3901 typval_T *tv1 = STACK_TV_BOT(-2);
3902 typval_T *tv2 = STACK_TV_BOT(-1);
3903 int res;
3904
3905 res = typval_compare_null(tv1, tv2);
3906 if (res == MAYBE)
3907 goto on_error;
3908 if (iptr->isn_arg.op.op_type == EXPR_NEQUAL)
3909 res = !res;
3910 clear_tv(tv1);
3911 clear_tv(tv2);
3912 --ectx->ec_stack.ga_len;
3913 tv1->v_type = VAR_BOOL;
3914 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
3915 }
3916 break;
3917
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003918 // Operation with two number arguments
3919 case ISN_OPNR:
3920 case ISN_COMPARENR:
3921 {
3922 typval_T *tv1 = STACK_TV_BOT(-2);
3923 typval_T *tv2 = STACK_TV_BOT(-1);
3924 varnumber_T arg1 = tv1->vval.v_number;
3925 varnumber_T arg2 = tv2->vval.v_number;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02003926 varnumber_T res = 0;
3927 int div_zero = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003928
3929 switch (iptr->isn_arg.op.op_type)
3930 {
3931 case EXPR_MULT: res = arg1 * arg2; break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02003932 case EXPR_DIV: if (arg2 == 0)
3933 div_zero = TRUE;
3934 else
3935 res = arg1 / arg2;
3936 break;
3937 case EXPR_REM: if (arg2 == 0)
3938 div_zero = TRUE;
3939 else
3940 res = arg1 % arg2;
3941 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003942 case EXPR_SUB: res = arg1 - arg2; break;
3943 case EXPR_ADD: res = arg1 + arg2; break;
3944
3945 case EXPR_EQUAL: res = arg1 == arg2; break;
3946 case EXPR_NEQUAL: res = arg1 != arg2; break;
3947 case EXPR_GREATER: res = arg1 > arg2; break;
3948 case EXPR_GEQUAL: res = arg1 >= arg2; break;
3949 case EXPR_SMALLER: res = arg1 < arg2; break;
3950 case EXPR_SEQUAL: res = arg1 <= arg2; break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02003951 default: break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003952 }
3953
Bram Moolenaar4c137212021-04-19 16:48:48 +02003954 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003955 if (iptr->isn_type == ISN_COMPARENR)
3956 {
3957 tv1->v_type = VAR_BOOL;
3958 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
3959 }
3960 else
3961 tv1->vval.v_number = res;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02003962 if (div_zero)
3963 {
3964 SOURCING_LNUM = iptr->isn_lnum;
3965 emsg(_(e_divide_by_zero));
3966 goto on_error;
3967 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003968 }
3969 break;
3970
3971 // Computation with two float arguments
3972 case ISN_OPFLOAT:
3973 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01003974#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003975 {
3976 typval_T *tv1 = STACK_TV_BOT(-2);
3977 typval_T *tv2 = STACK_TV_BOT(-1);
3978 float_T arg1 = tv1->vval.v_float;
3979 float_T arg2 = tv2->vval.v_float;
3980 float_T res = 0;
3981 int cmp = FALSE;
3982
3983 switch (iptr->isn_arg.op.op_type)
3984 {
3985 case EXPR_MULT: res = arg1 * arg2; break;
3986 case EXPR_DIV: res = arg1 / arg2; break;
3987 case EXPR_SUB: res = arg1 - arg2; break;
3988 case EXPR_ADD: res = arg1 + arg2; break;
3989
3990 case EXPR_EQUAL: cmp = arg1 == arg2; break;
3991 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
3992 case EXPR_GREATER: cmp = arg1 > arg2; break;
3993 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
3994 case EXPR_SMALLER: cmp = arg1 < arg2; break;
3995 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
3996 default: cmp = 0; break;
3997 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003998 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003999 if (iptr->isn_type == ISN_COMPAREFLOAT)
4000 {
4001 tv1->v_type = VAR_BOOL;
4002 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
4003 }
4004 else
4005 tv1->vval.v_float = res;
4006 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01004007#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004008 break;
4009
4010 case ISN_COMPARELIST:
Bram Moolenaar265f8112021-12-19 12:33:05 +00004011 case ISN_COMPAREDICT:
4012 case ISN_COMPAREFUNC:
4013 case ISN_COMPARESTRING:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004014 case ISN_COMPAREBLOB:
4015 {
4016 typval_T *tv1 = STACK_TV_BOT(-2);
4017 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar265f8112021-12-19 12:33:05 +00004018 exprtype_T exprtype = iptr->isn_arg.op.op_type;
4019 int ic = iptr->isn_arg.op.op_ic;
4020 int res = FALSE;
4021 int status = OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004022
Bram Moolenaar265f8112021-12-19 12:33:05 +00004023 SOURCING_LNUM = iptr->isn_lnum;
4024 if (iptr->isn_type == ISN_COMPARELIST)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004025 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00004026 status = typval_compare_list(tv1, tv2,
4027 exprtype, ic, &res);
4028 }
4029 else if (iptr->isn_type == ISN_COMPAREDICT)
4030 {
4031 status = typval_compare_dict(tv1, tv2,
4032 exprtype, ic, &res);
4033 }
4034 else if (iptr->isn_type == ISN_COMPAREFUNC)
4035 {
4036 status = typval_compare_func(tv1, tv2,
4037 exprtype, ic, &res);
4038 }
4039 else if (iptr->isn_type == ISN_COMPARESTRING)
4040 {
4041 status = typval_compare_string(tv1, tv2,
4042 exprtype, ic, &res);
4043 }
4044 else
4045 {
4046 status = typval_compare_blob(tv1, tv2, exprtype, &res);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004047 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004048 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004049 clear_tv(tv1);
4050 clear_tv(tv2);
4051 tv1->v_type = VAR_BOOL;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004052 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4053 if (status == FAIL)
4054 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004055 }
4056 break;
4057
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004058 case ISN_COMPAREANY:
4059 {
4060 typval_T *tv1 = STACK_TV_BOT(-2);
4061 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar657137c2021-01-09 15:45:23 +01004062 exprtype_T exprtype = iptr->isn_arg.op.op_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004063 int ic = iptr->isn_arg.op.op_ic;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004064 int status;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004065
Bram Moolenaareb26f432020-09-14 16:50:05 +02004066 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004067 status = typval_compare(tv1, tv2, exprtype, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004068 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004069 --ectx->ec_stack.ga_len;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004070 if (status == FAIL)
4071 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004072 }
4073 break;
4074
4075 case ISN_ADDLIST:
4076 case ISN_ADDBLOB:
4077 {
4078 typval_T *tv1 = STACK_TV_BOT(-2);
4079 typval_T *tv2 = STACK_TV_BOT(-1);
4080
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004081 // add two lists or blobs
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004082 if (iptr->isn_type == ISN_ADDLIST)
Bram Moolenaar07802042021-09-09 23:01:14 +02004083 {
4084 if (iptr->isn_arg.op.op_type == EXPR_APPEND
4085 && tv1->vval.v_list != NULL)
4086 list_extend(tv1->vval.v_list, tv2->vval.v_list,
4087 NULL);
4088 else
4089 eval_addlist(tv1, tv2);
4090 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004091 else
4092 eval_addblob(tv1, tv2);
4093 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004094 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004095 }
4096 break;
4097
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004098 case ISN_LISTAPPEND:
4099 {
4100 typval_T *tv1 = STACK_TV_BOT(-2);
4101 typval_T *tv2 = STACK_TV_BOT(-1);
4102 list_T *l = tv1->vval.v_list;
4103
4104 // add an item to a list
Bram Moolenaar1f4a3452022-01-01 18:29:21 +00004105 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004106 if (l == NULL)
4107 {
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004108 emsg(_(e_cannot_add_to_null_list));
4109 goto on_error;
4110 }
Bram Moolenaar1f4a3452022-01-01 18:29:21 +00004111 if (value_check_lock(l->lv_lock, NULL, FALSE))
4112 goto on_error;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004113 if (list_append_tv(l, tv2) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004114 goto theend;
Bram Moolenaar955347c2020-10-19 23:01:46 +02004115 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004116 --ectx->ec_stack.ga_len;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004117 }
4118 break;
4119
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02004120 case ISN_BLOBAPPEND:
4121 {
4122 typval_T *tv1 = STACK_TV_BOT(-2);
4123 typval_T *tv2 = STACK_TV_BOT(-1);
4124 blob_T *b = tv1->vval.v_blob;
4125 int error = FALSE;
4126 varnumber_T n;
4127
4128 // add a number to a blob
4129 if (b == NULL)
4130 {
4131 SOURCING_LNUM = iptr->isn_lnum;
4132 emsg(_(e_cannot_add_to_null_blob));
4133 goto on_error;
4134 }
4135 n = tv_get_number_chk(tv2, &error);
4136 if (error)
4137 goto on_error;
4138 ga_append(&b->bv_ga, (int)n);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004139 --ectx->ec_stack.ga_len;
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02004140 }
4141 break;
4142
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004143 // Computation with two arguments of unknown type
4144 case ISN_OPANY:
4145 {
4146 typval_T *tv1 = STACK_TV_BOT(-2);
4147 typval_T *tv2 = STACK_TV_BOT(-1);
4148 varnumber_T n1, n2;
4149#ifdef FEAT_FLOAT
4150 float_T f1 = 0, f2 = 0;
4151#endif
4152 int error = FALSE;
4153
4154 if (iptr->isn_arg.op.op_type == EXPR_ADD)
4155 {
4156 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
4157 {
4158 eval_addlist(tv1, tv2);
4159 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004160 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004161 break;
4162 }
4163 else if (tv1->v_type == VAR_BLOB
4164 && tv2->v_type == VAR_BLOB)
4165 {
4166 eval_addblob(tv1, tv2);
4167 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004168 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004169 break;
4170 }
4171 }
4172#ifdef FEAT_FLOAT
4173 if (tv1->v_type == VAR_FLOAT)
4174 {
4175 f1 = tv1->vval.v_float;
4176 n1 = 0;
4177 }
4178 else
4179#endif
4180 {
Bram Moolenaarf665e972020-12-05 19:17:16 +01004181 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004182 n1 = tv_get_number_chk(tv1, &error);
4183 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02004184 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004185#ifdef FEAT_FLOAT
4186 if (tv2->v_type == VAR_FLOAT)
4187 f1 = n1;
4188#endif
4189 }
4190#ifdef FEAT_FLOAT
4191 if (tv2->v_type == VAR_FLOAT)
4192 {
4193 f2 = tv2->vval.v_float;
4194 n2 = 0;
4195 }
4196 else
4197#endif
4198 {
4199 n2 = tv_get_number_chk(tv2, &error);
4200 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02004201 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004202#ifdef FEAT_FLOAT
4203 if (tv1->v_type == VAR_FLOAT)
4204 f2 = n2;
4205#endif
4206 }
4207#ifdef FEAT_FLOAT
4208 // if there is a float on either side the result is a float
4209 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
4210 {
4211 switch (iptr->isn_arg.op.op_type)
4212 {
4213 case EXPR_MULT: f1 = f1 * f2; break;
4214 case EXPR_DIV: f1 = f1 / f2; break;
4215 case EXPR_SUB: f1 = f1 - f2; break;
4216 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004217 default: SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004218 emsg(_(e_cannot_use_percent_with_float));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004219 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004220 }
4221 clear_tv(tv1);
4222 clear_tv(tv2);
4223 tv1->v_type = VAR_FLOAT;
4224 tv1->vval.v_float = f1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004225 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004226 }
4227 else
4228#endif
4229 {
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004230 int failed = FALSE;
4231
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004232 switch (iptr->isn_arg.op.op_type)
4233 {
4234 case EXPR_MULT: n1 = n1 * n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004235 case EXPR_DIV: n1 = num_divide(n1, n2, &failed);
4236 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01004237 goto on_error;
4238 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004239 case EXPR_SUB: n1 = n1 - n2; break;
4240 case EXPR_ADD: n1 = n1 + n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004241 default: n1 = num_modulus(n1, n2, &failed);
4242 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01004243 goto on_error;
4244 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004245 }
4246 clear_tv(tv1);
4247 clear_tv(tv2);
4248 tv1->v_type = VAR_NUMBER;
4249 tv1->vval.v_number = n1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004250 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004251 }
4252 }
4253 break;
4254
4255 case ISN_CONCAT:
4256 {
4257 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string;
4258 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string;
4259 char_u *res;
4260
4261 res = concat_str(str1, str2);
4262 clear_tv(STACK_TV_BOT(-2));
4263 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02004264 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004265 STACK_TV_BOT(-1)->vval.v_string = res;
4266 }
4267 break;
4268
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004269 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004270 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004271 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004272 int is_slice = iptr->isn_type == ISN_STRSLICE;
4273 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004274 char_u *res;
4275
4276 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004277 // string slice: string is at stack-3, first index at
4278 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004279 if (is_slice)
4280 {
4281 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004282 n1 = tv->vval.v_number;
4283 }
4284
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004285 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004286 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004287
Bram Moolenaar4c137212021-04-19 16:48:48 +02004288 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004289 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004290 if (is_slice)
4291 // Slice: Select the characters from the string
Bram Moolenaar6601b622021-01-13 21:47:15 +01004292 res = string_slice(tv->vval.v_string, n1, n2, FALSE);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004293 else
4294 // Index: The resulting variable is a string of a
Bram Moolenaar0289a092021-03-14 18:40:19 +01004295 // single character (including composing characters).
4296 // If the index is too big or negative the result is
4297 // empty.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004298 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004299 vim_free(tv->vval.v_string);
4300 tv->vval.v_string = res;
4301 }
4302 break;
4303
4304 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02004305 case ISN_LISTSLICE:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004306 case ISN_BLOBINDEX:
4307 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004308 {
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004309 int is_slice = iptr->isn_type == ISN_LISTSLICE
4310 || iptr->isn_type == ISN_BLOBSLICE;
4311 int is_blob = iptr->isn_type == ISN_BLOBINDEX
4312 || iptr->isn_type == ISN_BLOBSLICE;
Bram Moolenaared591872020-08-15 22:14:53 +02004313 varnumber_T n1, n2;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004314 typval_T *val_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004315
4316 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02004317 // list slice: list is at stack-3, indexes at stack-2 and
4318 // stack-1
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004319 // Same for blob.
4320 val_tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004321
4322 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02004323 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004324 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02004325
4326 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004327 {
Bram Moolenaared591872020-08-15 22:14:53 +02004328 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02004329 n1 = tv->vval.v_number;
4330 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004331 }
Bram Moolenaared591872020-08-15 22:14:53 +02004332
Bram Moolenaar4c137212021-04-19 16:48:48 +02004333 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02004334 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02004335 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004336 if (is_blob)
4337 {
4338 if (blob_slice_or_index(val_tv->vval.v_blob, is_slice,
4339 n1, n2, FALSE, tv) == FAIL)
4340 goto on_error;
4341 }
4342 else
4343 {
4344 if (list_slice_or_index(val_tv->vval.v_list, is_slice,
4345 n1, n2, FALSE, tv, TRUE) == FAIL)
4346 goto on_error;
4347 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004348 }
4349 break;
4350
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004351 case ISN_ANYINDEX:
4352 case ISN_ANYSLICE:
4353 {
4354 int is_slice = iptr->isn_type == ISN_ANYSLICE;
4355 typval_T *var1, *var2;
4356 int res;
4357
4358 // index: composite is at stack-2, index at stack-1
4359 // slice: composite is at stack-3, indexes at stack-2 and
4360 // stack-1
4361 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02004362 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004363 if (check_can_index(tv, TRUE, TRUE) == FAIL)
4364 goto on_error;
4365 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
4366 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
Bram Moolenaar6601b622021-01-13 21:47:15 +01004367 res = eval_index_inner(tv, is_slice, var1, var2,
4368 FALSE, NULL, -1, TRUE);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004369 clear_tv(var1);
4370 if (is_slice)
4371 clear_tv(var2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004372 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004373 if (res == FAIL)
4374 goto on_error;
4375 }
4376 break;
4377
Bram Moolenaar9af78762020-06-16 11:34:42 +02004378 case ISN_SLICE:
4379 {
4380 list_T *list;
4381 int count = iptr->isn_arg.number;
4382
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02004383 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02004384 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02004385 list = tv->vval.v_list;
4386
4387 // no error for short list, expect it to be checked earlier
4388 if (list != NULL && list->lv_len >= count)
4389 {
4390 list_T *newlist = list_slice(list,
4391 count, list->lv_len - 1);
4392
4393 if (newlist != NULL)
4394 {
4395 list_unref(list);
4396 tv->vval.v_list = newlist;
4397 ++newlist->lv_refcount;
4398 }
4399 }
4400 }
4401 break;
4402
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004403 case ISN_GETITEM:
4404 {
4405 listitem_T *li;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02004406 getitem_T *gi = &iptr->isn_arg.getitem;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004407
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02004408 // Get list item: list is at stack-1, push item.
4409 // List type and length is checked for when compiling.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02004410 tv = STACK_TV_BOT(-1 - gi->gi_with_op);
4411 li = list_find(tv->vval.v_list, gi->gi_index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004412
Bram Moolenaar35578162021-08-02 19:10:38 +02004413 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004414 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004415 ++ectx->ec_stack.ga_len;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004416 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004417
4418 // Useful when used in unpack assignment. Reset at
4419 // ISN_DROP.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02004420 ectx->ec_where.wt_index = gi->gi_index + 1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004421 ectx->ec_where.wt_variable = TRUE;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004422 }
4423 break;
4424
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004425 case ISN_MEMBER:
4426 {
4427 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004428 char_u *key;
4429 dictitem_T *di;
4430
4431 // dict member: dict is at stack-2, key at stack-1
4432 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02004433 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004434 dict = tv->vval.v_dict;
4435
4436 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02004437 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004438 key = tv->vval.v_string;
Bram Moolenaar086fc9a2020-10-30 18:33:02 +01004439 if (key == NULL)
4440 key = (char_u *)"";
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02004441
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004442 if ((di = dict_find(dict, key, -1)) == NULL)
4443 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004444 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004445 semsg(_(e_key_not_present_in_dictionary), key);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004446
4447 // If :silent! is used we will continue, make sure the
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004448 // stack contents makes sense and the dict stack is
4449 // updated.
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004450 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004451 --ectx->ec_stack.ga_len;
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004452 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004453 (void) dict_stack_save(tv);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004454 tv->v_type = VAR_NUMBER;
4455 tv->vval.v_number = 0;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004456 goto on_fatal_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004457 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004458 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004459 --ectx->ec_stack.ga_len;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004460 // Put the dict used on the dict stack, it might be used by
4461 // a dict function later.
Bram Moolenaar50788ef2020-07-05 16:51:26 +02004462 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004463 if (dict_stack_save(tv) == FAIL)
4464 goto on_fatal_error;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02004465 copy_tv(&di->di_tv, tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004466 }
4467 break;
4468
4469 // dict member with string key
4470 case ISN_STRINGMEMBER:
4471 {
4472 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004473 dictitem_T *di;
4474
4475 tv = STACK_TV_BOT(-1);
4476 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
4477 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004478 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004479 emsg(_(e_dictionary_required));
Bram Moolenaard032f342020-07-18 18:13:02 +02004480 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004481 }
4482 dict = tv->vval.v_dict;
4483
4484 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
4485 == NULL)
4486 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004487 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar381692b2022-02-02 20:01:27 +00004488 semsg(_(e_key_not_present_in_dictionary),
4489 iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02004490 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004491 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004492 // Put the dict used on the dict stack, it might be used by
4493 // a dict function later.
4494 if (dict_stack_save(tv) == FAIL)
4495 goto on_fatal_error;
4496
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004497 copy_tv(&di->di_tv, tv);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004498 }
4499 break;
4500
4501 case ISN_CLEARDICT:
4502 dict_stack_drop();
4503 break;
4504
4505 case ISN_USEDICT:
4506 {
4507 typval_T *dict_tv = dict_stack_get_tv();
4508
4509 // Turn "dict.Func" into a partial for "Func" bound to
4510 // "dict". Don't do this when "Func" is already a partial
4511 // that was bound explicitly (pt_auto is FALSE).
4512 tv = STACK_TV_BOT(-1);
4513 if (dict_tv != NULL
4514 && dict_tv->v_type == VAR_DICT
4515 && dict_tv->vval.v_dict != NULL
4516 && (tv->v_type == VAR_FUNC
4517 || (tv->v_type == VAR_PARTIAL
4518 && (tv->vval.v_partial->pt_auto
4519 || tv->vval.v_partial->pt_dict == NULL))))
4520 dict_tv->vval.v_dict =
4521 make_partial(dict_tv->vval.v_dict, tv);
4522 dict_stack_drop();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004523 }
4524 break;
4525
4526 case ISN_NEGATENR:
4527 tv = STACK_TV_BOT(-1);
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00004528 // CHECKTYPE should have checked the variable type
Bram Moolenaarc58164c2020-03-29 18:40:30 +02004529#ifdef FEAT_FLOAT
4530 if (tv->v_type == VAR_FLOAT)
4531 tv->vval.v_float = -tv->vval.v_float;
4532 else
4533#endif
4534 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004535 break;
4536
4537 case ISN_CHECKNR:
4538 {
4539 int error = FALSE;
4540
4541 tv = STACK_TV_BOT(-1);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02004542 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004543 if (check_not_string(tv) == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004544 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004545 (void)tv_get_number_chk(tv, &error);
4546 if (error)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004547 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004548 }
4549 break;
4550
4551 case ISN_CHECKTYPE:
4552 {
4553 checktype_T *ct = &iptr->isn_arg.type;
4554
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01004555 tv = STACK_TV_BOT((int)ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02004556 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004557 if (!ectx->ec_where.wt_variable)
4558 ectx->ec_where.wt_index = ct->ct_arg_idx;
4559 if (check_typval_type(ct->ct_type, tv, ectx->ec_where)
4560 == FAIL)
Bram Moolenaar5e654232020-09-16 15:22:00 +02004561 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004562 if (!ectx->ec_where.wt_variable)
4563 ectx->ec_where.wt_index = 0;
Bram Moolenaar5e654232020-09-16 15:22:00 +02004564
4565 // number 0 is FALSE, number 1 is TRUE
4566 if (tv->v_type == VAR_NUMBER
4567 && ct->ct_type->tt_type == VAR_BOOL
4568 && (tv->vval.v_number == 0
4569 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004570 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02004571 tv->v_type = VAR_BOOL;
4572 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02004573 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004574 }
4575 }
4576 break;
4577
Bram Moolenaar9af78762020-06-16 11:34:42 +02004578 case ISN_CHECKLEN:
4579 {
4580 int min_len = iptr->isn_arg.checklen.cl_min_len;
4581 list_T *list = NULL;
4582
4583 tv = STACK_TV_BOT(-1);
4584 if (tv->v_type == VAR_LIST)
4585 list = tv->vval.v_list;
4586 if (list == NULL || list->lv_len < min_len
4587 || (list->lv_len > min_len
4588 && !iptr->isn_arg.checklen.cl_more_OK))
4589 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004590 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004591 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02004592 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004593 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02004594 }
4595 }
4596 break;
4597
Bram Moolenaaraa210a32021-01-02 15:41:03 +01004598 case ISN_SETTYPE:
Bram Moolenaar381692b2022-02-02 20:01:27 +00004599 set_tv_type(STACK_TV_BOT(-1), iptr->isn_arg.type.ct_type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01004600 break;
4601
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004602 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004603 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004604 {
4605 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004606 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004607
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004608 if (iptr->isn_type == ISN_2BOOL)
4609 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004610 tv = STACK_TV_BOT(iptr->isn_arg.tobool.offset);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004611 n = tv2bool(tv);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004612 if (iptr->isn_arg.tobool.invert)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004613 n = !n;
4614 }
4615 else
4616 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004617 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004618 SOURCING_LNUM = iptr->isn_lnum;
4619 n = tv_get_bool_chk(tv, &error);
4620 if (error)
4621 goto on_error;
4622 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004623 clear_tv(tv);
4624 tv->v_type = VAR_BOOL;
4625 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
4626 }
4627 break;
4628
4629 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02004630 case ISN_2STRING_ANY:
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01004631 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004632 if (do_2string(STACK_TV_BOT(iptr->isn_arg.tostring.offset),
4633 iptr->isn_type == ISN_2STRING_ANY,
4634 iptr->isn_arg.tostring.tolerant) == FAIL)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01004635 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004636 break;
4637
Bram Moolenaar08597872020-12-10 19:43:40 +01004638 case ISN_RANGE:
4639 {
4640 exarg_T ea;
4641 char *errormsg;
4642
Bram Moolenaarece0b872021-01-08 20:40:45 +01004643 ea.line2 = 0;
Bram Moolenaard1510ee2021-01-04 16:15:58 +01004644 ea.addr_count = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01004645 ea.addr_type = ADDR_LINES;
4646 ea.cmd = iptr->isn_arg.string;
Bram Moolenaarece0b872021-01-08 20:40:45 +01004647 ea.skip = FALSE;
Bram Moolenaar08597872020-12-10 19:43:40 +01004648 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaarece0b872021-01-08 20:40:45 +01004649 goto on_error;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004650
Bram Moolenaar35578162021-08-02 19:10:38 +02004651 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004652 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004653 ++ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004654 tv = STACK_TV_BOT(-1);
4655 tv->v_type = VAR_NUMBER;
4656 tv->v_lock = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01004657 if (ea.addr_count == 0)
4658 tv->vval.v_number = curwin->w_cursor.lnum;
4659 else
4660 tv->vval.v_number = ea.line2;
4661 }
4662 break;
4663
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004664 case ISN_PUT:
4665 {
4666 int regname = iptr->isn_arg.put.put_regname;
4667 linenr_T lnum = iptr->isn_arg.put.put_lnum;
4668 char_u *expr = NULL;
4669 int dir = FORWARD;
4670
Bram Moolenaar08597872020-12-10 19:43:40 +01004671 if (lnum < -2)
4672 {
4673 // line number was put on the stack by ISN_RANGE
4674 tv = STACK_TV_BOT(-1);
4675 curwin->w_cursor.lnum = tv->vval.v_number;
4676 if (lnum == LNUM_VARIABLE_RANGE_ABOVE)
4677 dir = BACKWARD;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004678 --ectx->ec_stack.ga_len;
Bram Moolenaar08597872020-12-10 19:43:40 +01004679 }
4680 else if (lnum == -2)
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004681 // :put! above cursor
4682 dir = BACKWARD;
4683 else if (lnum >= 0)
Bram Moolenaar4e713ba2022-02-07 15:31:37 +00004684 {
4685 curwin->w_cursor.lnum = lnum;
4686 if (lnum == 0)
4687 // check_cursor() below will move to line 1
4688 dir = BACKWARD;
4689 }
Bram Moolenaara28639e2021-01-19 22:48:09 +01004690
4691 if (regname == '=')
4692 {
4693 tv = STACK_TV_BOT(-1);
4694 if (tv->v_type == VAR_STRING)
4695 expr = tv->vval.v_string;
4696 else
4697 {
4698 expr = typval2string(tv, TRUE); // allocates value
4699 clear_tv(tv);
4700 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004701 --ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004702 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004703 check_cursor();
4704 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
4705 vim_free(expr);
4706 }
4707 break;
4708
Bram Moolenaar02194d22020-10-24 23:08:38 +02004709 case ISN_CMDMOD:
Bram Moolenaar4c137212021-04-19 16:48:48 +02004710 ectx->ec_funclocal.floc_save_cmdmod = cmdmod;
4711 ectx->ec_funclocal.floc_restore_cmdmod = TRUE;
4712 ectx->ec_funclocal.floc_restore_cmdmod_stacklen =
4713 ectx->ec_stack.ga_len;
Bram Moolenaar02194d22020-10-24 23:08:38 +02004714 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
4715 apply_cmdmod(&cmdmod);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004716 break;
4717
Bram Moolenaar02194d22020-10-24 23:08:38 +02004718 case ISN_CMDMOD_REV:
4719 // filter regprog is owned by the instruction, don't free it
4720 cmdmod.cmod_filter_regmatch.regprog = NULL;
4721 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004722 cmdmod = ectx->ec_funclocal.floc_save_cmdmod;
4723 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004724 break;
4725
Bram Moolenaar792f7862020-11-23 08:31:18 +01004726 case ISN_UNPACK:
4727 {
4728 int count = iptr->isn_arg.unpack.unp_count;
4729 int semicolon = iptr->isn_arg.unpack.unp_semicolon;
4730 list_T *l;
4731 listitem_T *li;
4732 int i;
4733
4734 // Check there is a valid list to unpack.
4735 tv = STACK_TV_BOT(-1);
4736 if (tv->v_type != VAR_LIST)
4737 {
4738 SOURCING_LNUM = iptr->isn_lnum;
4739 emsg(_(e_for_argument_must_be_sequence_of_lists));
4740 goto on_error;
4741 }
4742 l = tv->vval.v_list;
4743 if (l == NULL
4744 || l->lv_len < (semicolon ? count - 1 : count))
4745 {
4746 SOURCING_LNUM = iptr->isn_lnum;
4747 emsg(_(e_list_value_does_not_have_enough_items));
4748 goto on_error;
4749 }
4750 else if (!semicolon && l->lv_len > count)
4751 {
4752 SOURCING_LNUM = iptr->isn_lnum;
4753 emsg(_(e_list_value_has_more_items_than_targets));
4754 goto on_error;
4755 }
4756
4757 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar35578162021-08-02 19:10:38 +02004758 if (GA_GROW_FAILS(&ectx->ec_stack, count - 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004759 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004760 ectx->ec_stack.ga_len += count - 1;
Bram Moolenaar792f7862020-11-23 08:31:18 +01004761
4762 // Variable after semicolon gets a list with the remaining
4763 // items.
4764 if (semicolon)
4765 {
4766 list_T *rem_list =
4767 list_alloc_with_items(l->lv_len - count + 1);
4768
4769 if (rem_list == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004770 goto theend;
Bram Moolenaar792f7862020-11-23 08:31:18 +01004771 tv = STACK_TV_BOT(-count);
4772 tv->vval.v_list = rem_list;
4773 ++rem_list->lv_refcount;
4774 tv->v_lock = 0;
4775 li = l->lv_first;
4776 for (i = 0; i < count - 1; ++i)
4777 li = li->li_next;
4778 for (i = 0; li != NULL; ++i)
4779 {
Bram Moolenaar61efa162022-03-18 13:10:48 +00004780 typval_T tvcopy;
4781
4782 copy_tv(&li->li_tv, &tvcopy);
4783 list_set_item(rem_list, i, &tvcopy);
Bram Moolenaar792f7862020-11-23 08:31:18 +01004784 li = li->li_next;
4785 }
4786 --count;
4787 }
4788
4789 // Produce the values in reverse order, first item last.
4790 li = l->lv_first;
4791 for (i = 0; i < count; ++i)
4792 {
4793 tv = STACK_TV_BOT(-i - 1);
4794 copy_tv(&li->li_tv, tv);
4795 li = li->li_next;
4796 }
4797
4798 list_unref(l);
4799 }
4800 break;
4801
Bram Moolenaarb2049902021-01-24 12:53:53 +01004802 case ISN_PROF_START:
4803 case ISN_PROF_END:
4804 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01004805#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01004806 funccall_T cookie;
4807 ufunc_T *cur_ufunc =
4808 (((dfunc_T *)def_functions.ga_data)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02004809 + ectx->ec_dfunc_idx)->df_ufunc;
Bram Moolenaarb2049902021-01-24 12:53:53 +01004810
4811 cookie.func = cur_ufunc;
4812 if (iptr->isn_type == ISN_PROF_START)
4813 {
4814 func_line_start(&cookie, iptr->isn_lnum);
4815 // if we get here the instruction is executed
4816 func_line_exec(&cookie);
4817 }
4818 else
4819 func_line_end(&cookie);
Bram Moolenaarf002a412021-01-24 13:34:18 +01004820#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01004821 }
4822 break;
4823
Bram Moolenaare99d4222021-06-13 14:01:26 +02004824 case ISN_DEBUG:
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02004825 handle_debug(iptr, ectx);
Bram Moolenaare99d4222021-06-13 14:01:26 +02004826 break;
4827
Bram Moolenaar389df252020-07-09 21:20:47 +02004828 case ISN_SHUFFLE:
4829 {
Bram Moolenaar792f7862020-11-23 08:31:18 +01004830 typval_T tmp_tv;
4831 int item = iptr->isn_arg.shuffle.shfl_item;
4832 int up = iptr->isn_arg.shuffle.shfl_up;
Bram Moolenaar389df252020-07-09 21:20:47 +02004833
4834 tmp_tv = *STACK_TV_BOT(-item);
4835 for ( ; up > 0 && item > 1; --up)
4836 {
4837 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
4838 --item;
4839 }
4840 *STACK_TV_BOT(-item) = tmp_tv;
4841 }
4842 break;
4843
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004844 case ISN_DROP:
Bram Moolenaar4c137212021-04-19 16:48:48 +02004845 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004846 clear_tv(STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02004847 ectx->ec_where.wt_index = 0;
4848 ectx->ec_where.wt_variable = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004849 break;
4850 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004851 continue;
4852
Bram Moolenaard032f342020-07-18 18:13:02 +02004853func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02004854 // Restore previous function. If the frame pointer is where we started
4855 // then there is none and we are done.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004856 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02004857 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02004858
Bram Moolenaar4c137212021-04-19 16:48:48 +02004859 if (func_return(ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004860 // only fails when out of memory
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004861 goto theend;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02004862 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02004863
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004864on_error:
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004865 // Jump here for an error that does not require aborting execution.
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004866 // If "emsg_silent" is set then ignore the error, unless it was set
4867 // when calling the function.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004868 if (did_emsg_cumul + did_emsg == ectx->ec_did_emsg_before
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004869 && emsg_silent && did_emsg_def == 0)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004870 {
4871 // If a sequence of instructions causes an error while ":silent!"
4872 // was used, restore the stack length and jump ahead to restoring
4873 // the cmdmod.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004874 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004875 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004876 while (ectx->ec_stack.ga_len
4877 > ectx->ec_funclocal.floc_restore_cmdmod_stacklen)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004878 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004879 --ectx->ec_stack.ga_len;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004880 clear_tv(STACK_TV_BOT(0));
4881 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004882 while (ectx->ec_instr[ectx->ec_iidx].isn_type != ISN_CMDMOD_REV)
4883 ++ectx->ec_iidx;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004884 }
Bram Moolenaarcd030c42020-10-30 21:49:40 +01004885 continue;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004886 }
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004887on_fatal_error:
4888 // Jump here for an error that messes up the stack.
Bram Moolenaar171fb922020-10-28 16:54:47 +01004889 // If we are not inside a try-catch started here, abort execution.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004890 if (trylevel <= ectx->ec_trylevel_at_start)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004891 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004892 }
4893
4894done:
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004895 ret = OK;
4896theend:
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004897 dict_stack_clear(dict_stack_len_at_start);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004898 ectx->ec_trylevel_at_start = save_trylevel_at_start;
4899 return ret;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004900}
4901
4902/*
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004903 * Execute the instructions from a VAR_INSTR typeval and put the result in
4904 * "rettv".
4905 * Return OK or FAIL.
4906 */
4907 int
4908exe_typval_instr(typval_T *tv, typval_T *rettv)
4909{
4910 ectx_T *ectx = tv->vval.v_instr->instr_ectx;
4911 isn_T *save_instr = ectx->ec_instr;
4912 int save_iidx = ectx->ec_iidx;
4913 int res;
4914
4915 ectx->ec_instr = tv->vval.v_instr->instr_instr;
4916 res = exec_instructions(ectx);
4917 if (res == OK)
4918 {
4919 *rettv = *STACK_TV_BOT(-1);
4920 --ectx->ec_stack.ga_len;
4921 }
4922
4923 ectx->ec_instr = save_instr;
4924 ectx->ec_iidx = save_iidx;
4925
4926 return res;
4927}
4928
4929/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02004930 * Execute the instructions from an ISN_SUBSTITUTE command, which are in
4931 * "substitute_instr".
4932 */
4933 char_u *
4934exe_substitute_instr(void)
4935{
4936 ectx_T *ectx = substitute_instr->subs_ectx;
4937 isn_T *save_instr = ectx->ec_instr;
4938 int save_iidx = ectx->ec_iidx;
4939 char_u *res;
4940
4941 ectx->ec_instr = substitute_instr->subs_instr;
4942 if (exec_instructions(ectx) == OK)
Bram Moolenaar90193e62021-04-04 20:49:50 +02004943 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004944 typval_T *tv = STACK_TV_BOT(-1);
4945
Bram Moolenaar27523602021-06-05 21:36:19 +02004946 res = typval2string(tv, TRUE);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004947 --ectx->ec_stack.ga_len;
4948 clear_tv(tv);
Bram Moolenaar90193e62021-04-04 20:49:50 +02004949 }
4950 else
4951 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004952 substitute_instr->subs_status = FAIL;
4953 res = vim_strsave((char_u *)"");
Bram Moolenaar90193e62021-04-04 20:49:50 +02004954 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004955
Bram Moolenaar4c137212021-04-19 16:48:48 +02004956 ectx->ec_instr = save_instr;
4957 ectx->ec_iidx = save_iidx;
4958
4959 return res;
4960}
4961
4962/*
4963 * Call a "def" function from old Vim script.
4964 * Return OK or FAIL.
4965 */
4966 int
4967call_def_function(
4968 ufunc_T *ufunc,
4969 int argc_arg, // nr of arguments
4970 typval_T *argv, // arguments
4971 partial_T *partial, // optional partial for context
4972 typval_T *rettv) // return value
4973{
4974 ectx_T ectx; // execution context
4975 int argc = argc_arg;
4976 typval_T *tv;
4977 int idx;
4978 int ret = FAIL;
4979 int defcount = ufunc->uf_args.ga_len - argc;
4980 sctx_T save_current_sctx = current_sctx;
4981 int did_emsg_before = did_emsg_cumul + did_emsg;
4982 int save_suppress_errthrow = suppress_errthrow;
4983 msglist_T **saved_msg_list = NULL;
4984 msglist_T *private_msg_list = NULL;
4985 int save_emsg_silent_def = emsg_silent_def;
4986 int save_did_emsg_def = did_emsg_def;
4987 int orig_funcdepth;
Bram Moolenaare99d4222021-06-13 14:01:26 +02004988 int orig_nesting_level = ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004989
4990// Get pointer to item in the stack.
4991#undef STACK_TV
4992#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
4993
4994// Get pointer to item at the bottom of the stack, -1 is the bottom.
4995#undef STACK_TV_BOT
4996#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
4997
4998// Get pointer to a local variable on the stack. Negative for arguments.
4999#undef STACK_TV_VAR
5000#define STACK_TV_VAR(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_frame_idx + STACK_FRAME_SIZE + idx)
5001
5002 if (ufunc->uf_def_status == UF_NOT_COMPILED
5003 || ufunc->uf_def_status == UF_COMPILE_ERROR
Bram Moolenaar139575d2022-03-15 19:29:30 +00005004 || (func_needs_compiling(ufunc, get_compile_type(ufunc))
5005 && compile_def_function(ufunc, FALSE,
5006 get_compile_type(ufunc), NULL) == FAIL))
Bram Moolenaar4c137212021-04-19 16:48:48 +02005007 {
5008 if (did_emsg_cumul + did_emsg == did_emsg_before)
5009 semsg(_(e_function_is_not_compiled_str),
5010 printable_func_name(ufunc));
5011 return FAIL;
5012 }
5013
5014 {
5015 // Check the function was really compiled.
5016 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5017 + ufunc->uf_dfunc_idx;
5018 if (INSTRUCTIONS(dfunc) == NULL)
5019 {
5020 iemsg("using call_def_function() on not compiled function");
5021 return FAIL;
5022 }
5023 }
5024
5025 // If depth of calling is getting too high, don't execute the function.
5026 orig_funcdepth = funcdepth_get();
5027 if (funcdepth_increment() == FAIL)
5028 return FAIL;
5029
5030 CLEAR_FIELD(ectx);
5031 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
5032 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
Bram Moolenaar35578162021-08-02 19:10:38 +02005033 if (GA_GROW_FAILS(&ectx.ec_stack, 20))
Bram Moolenaar4c137212021-04-19 16:48:48 +02005034 {
5035 funcdepth_decrement();
5036 return FAIL;
5037 }
5038 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
5039 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
5040 ectx.ec_did_emsg_before = did_emsg_before;
Bram Moolenaare99d4222021-06-13 14:01:26 +02005041 ++ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005042
5043 idx = argc - ufunc->uf_args.ga_len;
5044 if (idx > 0 && ufunc->uf_va_name == NULL)
5045 {
5046 if (idx == 1)
5047 emsg(_(e_one_argument_too_many));
5048 else
5049 semsg(_(e_nr_arguments_too_many), idx);
5050 goto failed_early;
5051 }
Bram Moolenaarc6d71532021-06-05 18:49:38 +02005052 idx = argc - ufunc->uf_args.ga_len + ufunc->uf_def_args.ga_len;
5053 if (idx < 0)
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02005054 {
5055 if (idx == -1)
5056 emsg(_(e_one_argument_too_few));
5057 else
5058 semsg(_(e_nr_arguments_too_few), -idx);
5059 goto failed_early;
5060 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005061
5062 // Put arguments on the stack, but no more than what the function expects.
5063 // A lambda can be called with more arguments than it uses.
5064 for (idx = 0; idx < argc
5065 && (ufunc->uf_va_name != NULL || idx < ufunc->uf_args.ga_len);
5066 ++idx)
5067 {
5068 if (idx >= ufunc->uf_args.ga_len - ufunc->uf_def_args.ga_len
5069 && argv[idx].v_type == VAR_SPECIAL
5070 && argv[idx].vval.v_number == VVAL_NONE)
5071 {
5072 // Use the default value.
5073 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
5074 }
5075 else
5076 {
5077 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
5078 && check_typval_arg_type(
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02005079 ufunc->uf_arg_types[idx], &argv[idx],
5080 NULL, idx + 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005081 goto failed_early;
5082 copy_tv(&argv[idx], STACK_TV_BOT(0));
5083 }
5084 ++ectx.ec_stack.ga_len;
5085 }
5086
5087 // Turn varargs into a list. Empty list if no args.
5088 if (ufunc->uf_va_name != NULL)
5089 {
5090 int vararg_count = argc - ufunc->uf_args.ga_len;
5091
5092 if (vararg_count < 0)
5093 vararg_count = 0;
5094 else
5095 argc -= vararg_count;
5096 if (exe_newlist(vararg_count, &ectx) == FAIL)
5097 goto failed_early;
5098
5099 // Check the type of the list items.
5100 tv = STACK_TV_BOT(-1);
5101 if (ufunc->uf_va_type != NULL
5102 && ufunc->uf_va_type != &t_list_any
5103 && ufunc->uf_va_type->tt_member != &t_any
5104 && tv->vval.v_list != NULL)
5105 {
5106 type_T *expected = ufunc->uf_va_type->tt_member;
5107 listitem_T *li = tv->vval.v_list->lv_first;
5108
5109 for (idx = 0; idx < vararg_count; ++idx)
5110 {
5111 if (check_typval_arg_type(expected, &li->li_tv,
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02005112 NULL, argc + idx + 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005113 goto failed_early;
5114 li = li->li_next;
5115 }
5116 }
5117
5118 if (defcount > 0)
5119 // Move varargs list to below missing default arguments.
5120 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
5121 --ectx.ec_stack.ga_len;
5122 }
5123
5124 // Make space for omitted arguments, will store default value below.
5125 // Any varargs list goes after them.
5126 if (defcount > 0)
5127 for (idx = 0; idx < defcount; ++idx)
5128 {
5129 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
5130 ++ectx.ec_stack.ga_len;
5131 }
5132 if (ufunc->uf_va_name != NULL)
5133 ++ectx.ec_stack.ga_len;
5134
5135 // Frame pointer points to just after arguments.
5136 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
5137 ectx.ec_initial_frame_idx = ectx.ec_frame_idx;
5138
5139 {
5140 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5141 + ufunc->uf_dfunc_idx;
5142 ufunc_T *base_ufunc = dfunc->df_ufunc;
5143
5144 // "uf_partial" is on the ufunc that "df_ufunc" points to, as is done
5145 // by copy_func().
5146 if (partial != NULL || base_ufunc->uf_partial != NULL)
5147 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005148 ectx.ec_outer_ref = ALLOC_CLEAR_ONE(outer_ref_T);
5149 if (ectx.ec_outer_ref == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005150 goto failed_early;
5151 if (partial != NULL)
5152 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00005153 outer_T *outer = get_pt_outer(partial);
5154
5155 if (outer->out_stack == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005156 {
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00005157 if (current_ectx != NULL)
5158 {
5159 if (current_ectx->ec_outer_ref != NULL
5160 && current_ectx->ec_outer_ref->or_outer != NULL)
5161 ectx.ec_outer_ref->or_outer =
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005162 current_ectx->ec_outer_ref->or_outer;
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00005163 }
5164 // Should there be an error here?
Bram Moolenaar4c137212021-04-19 16:48:48 +02005165 }
5166 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005167 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00005168 ectx.ec_outer_ref->or_outer = outer;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005169 ++partial->pt_refcount;
5170 ectx.ec_outer_ref->or_partial = partial;
5171 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005172 }
5173 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005174 {
5175 ectx.ec_outer_ref->or_outer = &base_ufunc->uf_partial->pt_outer;
5176 ++base_ufunc->uf_partial->pt_refcount;
5177 ectx.ec_outer_ref->or_partial = base_ufunc->uf_partial;
5178 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005179 }
5180 }
5181
5182 // dummy frame entries
5183 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
5184 {
5185 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
5186 ++ectx.ec_stack.ga_len;
5187 }
5188
5189 {
5190 // Reserve space for local variables and any closure reference count.
5191 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5192 + ufunc->uf_dfunc_idx;
5193
Bram Moolenaar5cd64792021-12-25 18:23:24 +00005194 // Initialize variables to zero. That avoids having to generate
5195 // initializing instructions for "var nr: number", "var x: any", etc.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005196 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaar5cd64792021-12-25 18:23:24 +00005197 {
5198 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
5199 STACK_TV_VAR(idx)->vval.v_number = 0;
5200 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005201 ectx.ec_stack.ga_len += dfunc->df_varcount;
5202 if (dfunc->df_has_closure)
5203 {
5204 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
5205 STACK_TV_VAR(idx)->vval.v_number = 0;
5206 ++ectx.ec_stack.ga_len;
5207 }
5208
5209 ectx.ec_instr = INSTRUCTIONS(dfunc);
5210 }
5211
5212 // Following errors are in the function, not the caller.
5213 // Commands behave like vim9script.
5214 estack_push_ufunc(ufunc, 1);
5215 current_sctx = ufunc->uf_script_ctx;
5216 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
5217
5218 // Use a specific location for storing error messages to be converted to an
5219 // exception.
5220 saved_msg_list = msg_list;
5221 msg_list = &private_msg_list;
5222
5223 // Do turn errors into exceptions.
5224 suppress_errthrow = FALSE;
5225
5226 // Do not delete the function while executing it.
5227 ++ufunc->uf_calls;
5228
5229 // When ":silent!" was used before calling then we still abort the
5230 // function. If ":silent!" is used in the function then we don't.
5231 emsg_silent_def = emsg_silent;
5232 did_emsg_def = 0;
5233
5234 ectx.ec_where.wt_index = 0;
5235 ectx.ec_where.wt_variable = FALSE;
5236
5237 // Execute the instructions until done.
5238 ret = exec_instructions(&ectx);
5239 if (ret == OK)
5240 {
5241 // function finished, get result from the stack.
5242 if (ufunc->uf_ret_type == &t_void)
5243 {
5244 rettv->v_type = VAR_VOID;
5245 }
5246 else
5247 {
5248 tv = STACK_TV_BOT(-1);
5249 *rettv = *tv;
5250 tv->v_type = VAR_UNKNOWN;
5251 }
5252 }
5253
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01005254 // When failed need to unwind the call stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005255 while (ectx.ec_frame_idx != ectx.ec_initial_frame_idx)
5256 func_return(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02005257
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005258 // Deal with any remaining closures, they may be in use somewhere.
5259 if (ectx.ec_funcrefs.ga_len > 0)
Bram Moolenaarf112f302020-12-20 17:47:52 +01005260 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005261 handle_closure_in_use(&ectx, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00005262 ga_clear(&ectx.ec_funcrefs);
Bram Moolenaarf112f302020-12-20 17:47:52 +01005263 }
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005264
Bram Moolenaaree8580e2020-08-28 17:19:07 +02005265 estack_pop();
5266 current_sctx = save_current_sctx;
5267
Bram Moolenaar2336c372021-12-06 15:06:54 +00005268 if (--ufunc->uf_calls <= 0 && ufunc->uf_refcount <= 0)
5269 // Function was unreferenced while being used, free it now.
5270 func_clear_free(ufunc, FALSE);
Bram Moolenaarc970e422021-03-17 15:03:04 +01005271
Bram Moolenaar352134b2020-10-17 22:04:08 +02005272 if (*msg_list != NULL && saved_msg_list != NULL)
5273 {
5274 msglist_T **plist = saved_msg_list;
5275
5276 // Append entries from the current msg_list (uncaught exceptions) to
5277 // the saved msg_list.
5278 while (*plist != NULL)
5279 plist = &(*plist)->next;
5280
5281 *plist = *msg_list;
5282 }
5283 msg_list = saved_msg_list;
5284
Bram Moolenaar4c137212021-04-19 16:48:48 +02005285 if (ectx.ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar02194d22020-10-24 23:08:38 +02005286 {
5287 cmdmod.cmod_filter_regmatch.regprog = NULL;
5288 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005289 cmdmod = ectx.ec_funclocal.floc_save_cmdmod;
Bram Moolenaar02194d22020-10-24 23:08:38 +02005290 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +01005291 emsg_silent_def = save_emsg_silent_def;
5292 did_emsg_def += save_did_emsg_def;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02005293
Bram Moolenaaree8580e2020-08-28 17:19:07 +02005294failed_early:
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02005295 // Free all local variables, but not arguments.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005296 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
5297 clear_tv(STACK_TV(idx));
Bram Moolenaare99d4222021-06-13 14:01:26 +02005298 ex_nesting_level = orig_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02005299
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005300 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01005301 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005302 if (ectx.ec_outer_ref != NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +01005303 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005304 if (ectx.ec_outer_ref->or_outer_allocated)
5305 vim_free(ectx.ec_outer_ref->or_outer);
5306 partial_unref(ectx.ec_outer_ref->or_partial);
5307 vim_free(ectx.ec_outer_ref);
Bram Moolenaar0186e582021-01-10 18:33:11 +01005308 }
5309
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02005310 // Not sure if this is necessary.
5311 suppress_errthrow = save_suppress_errthrow;
5312
Bram Moolenaarb5841b92021-07-15 18:09:53 +02005313 if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before
5314 && !need_rethrow)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005315 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02005316 printable_func_name(ufunc));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01005317 funcdepth_restore(orig_funcdepth);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005318 return ret;
5319}
5320
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005321/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02005322 * List instructions "instr" up to "instr_count" or until ISN_FINISH.
5323 * "ufunc" has the source lines, NULL for the instructions of ISN_SUBSTITUTE.
5324 * "pfx" is prefixed to every line.
5325 */
5326 static void
5327list_instructions(char *pfx, isn_T *instr, int instr_count, ufunc_T *ufunc)
5328{
5329 int line_idx = 0;
5330 int prev_current = 0;
5331 int current;
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02005332 int def_arg_idx = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005333
5334 for (current = 0; current < instr_count; ++current)
5335 {
5336 isn_T *iptr = &instr[current];
5337 char *line;
5338
5339 if (ufunc != NULL)
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02005340 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005341 while (line_idx < iptr->isn_lnum
5342 && line_idx < ufunc->uf_lines.ga_len)
5343 {
5344 if (current > prev_current)
5345 {
5346 msg_puts("\n\n");
5347 prev_current = current;
5348 }
5349 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
5350 if (line != NULL)
5351 msg(line);
5352 }
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02005353 if (iptr->isn_type == ISN_JUMP_IF_ARG_SET)
5354 {
5355 int first_def_arg = ufunc->uf_args.ga_len
5356 - ufunc->uf_def_args.ga_len;
5357
5358 if (def_arg_idx > 0)
5359 msg_puts("\n\n");
5360 msg_start();
5361 msg_puts(" ");
5362 msg_puts(((char **)(ufunc->uf_args.ga_data))[
5363 first_def_arg + def_arg_idx]);
5364 msg_puts(" = ");
5365 msg_puts(((char **)(ufunc->uf_def_args.ga_data))[def_arg_idx++]);
5366 msg_clr_eos();
5367 msg_end();
5368 }
5369 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005370
5371 switch (iptr->isn_type)
5372 {
5373 case ISN_EXEC:
5374 smsg("%s%4d EXEC %s", pfx, current, iptr->isn_arg.string);
5375 break;
Bram Moolenaar20677332021-06-06 17:02:53 +02005376 case ISN_EXEC_SPLIT:
5377 smsg("%s%4d EXEC_SPLIT %s", pfx, current, iptr->isn_arg.string);
5378 break;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00005379 case ISN_EXECRANGE:
5380 smsg("%s%4d EXECRANGE %s", pfx, current, iptr->isn_arg.string);
5381 break;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005382 case ISN_LEGACY_EVAL:
5383 smsg("%s%4d EVAL legacy %s", pfx, current,
5384 iptr->isn_arg.string);
5385 break;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02005386 case ISN_REDIRSTART:
5387 smsg("%s%4d REDIR", pfx, current);
5388 break;
5389 case ISN_REDIREND:
5390 smsg("%s%4d REDIR END%s", pfx, current,
5391 iptr->isn_arg.number ? " append" : "");
5392 break;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005393 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005394#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005395 smsg("%s%4d CEXPR pre %s", pfx, current,
5396 cexpr_get_auname(iptr->isn_arg.number));
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005397#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005398 break;
5399 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005400#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005401 {
5402 cexprref_T *cer = iptr->isn_arg.cexpr.cexpr_ref;
5403
5404 smsg("%s%4d CEXPR core %s%s \"%s\"", pfx, current,
5405 cexpr_get_auname(cer->cer_cmdidx),
5406 cer->cer_forceit ? "!" : "",
5407 cer->cer_cmdline);
5408 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005409#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005410 break;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005411 case ISN_INSTR:
5412 {
5413 smsg("%s%4d INSTR", pfx, current);
5414 list_instructions(" ", iptr->isn_arg.instr,
5415 INT_MAX, NULL);
5416 msg(" -------------");
5417 }
5418 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005419 case ISN_SUBSTITUTE:
5420 {
5421 subs_T *subs = &iptr->isn_arg.subs;
5422
5423 smsg("%s%4d SUBSTITUTE %s", pfx, current, subs->subs_cmd);
5424 list_instructions(" ", subs->subs_instr, INT_MAX, NULL);
5425 msg(" -------------");
5426 }
5427 break;
5428 case ISN_EXECCONCAT:
5429 smsg("%s%4d EXECCONCAT %lld", pfx, current,
5430 (varnumber_T)iptr->isn_arg.number);
5431 break;
5432 case ISN_ECHO:
5433 {
5434 echo_T *echo = &iptr->isn_arg.echo;
5435
5436 smsg("%s%4d %s %d", pfx, current,
5437 echo->echo_with_white ? "ECHO" : "ECHON",
5438 echo->echo_count);
5439 }
5440 break;
5441 case ISN_EXECUTE:
5442 smsg("%s%4d EXECUTE %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02005443 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005444 break;
5445 case ISN_ECHOMSG:
5446 smsg("%s%4d ECHOMSG %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02005447 (varnumber_T)(iptr->isn_arg.number));
5448 break;
5449 case ISN_ECHOCONSOLE:
5450 smsg("%s%4d ECHOCONSOLE %lld", pfx, current,
5451 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005452 break;
5453 case ISN_ECHOERR:
5454 smsg("%s%4d ECHOERR %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02005455 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005456 break;
5457 case ISN_LOAD:
5458 {
5459 if (iptr->isn_arg.number < 0)
5460 smsg("%s%4d LOAD arg[%lld]", pfx, current,
5461 (varnumber_T)(iptr->isn_arg.number
5462 + STACK_FRAME_SIZE));
5463 else
5464 smsg("%s%4d LOAD $%lld", pfx, current,
5465 (varnumber_T)(iptr->isn_arg.number));
5466 }
5467 break;
5468 case ISN_LOADOUTER:
5469 {
5470 if (iptr->isn_arg.number < 0)
5471 smsg("%s%4d LOADOUTER level %d arg[%d]", pfx, current,
5472 iptr->isn_arg.outer.outer_depth,
5473 iptr->isn_arg.outer.outer_idx
5474 + STACK_FRAME_SIZE);
5475 else
5476 smsg("%s%4d LOADOUTER level %d $%d", pfx, current,
5477 iptr->isn_arg.outer.outer_depth,
5478 iptr->isn_arg.outer.outer_idx);
5479 }
5480 break;
5481 case ISN_LOADV:
5482 smsg("%s%4d LOADV v:%s", pfx, current,
5483 get_vim_var_name(iptr->isn_arg.number));
5484 break;
5485 case ISN_LOADSCRIPT:
5486 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005487 scriptref_T *sref = iptr->isn_arg.script.scriptref;
5488 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
5489 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005490
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005491 sv = get_script_svar(sref, -1);
5492 if (sv == NULL)
5493 smsg("%s%4d LOADSCRIPT [deleted] from %s",
5494 pfx, current, si->sn_name);
5495 else
5496 smsg("%s%4d LOADSCRIPT %s-%d from %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02005497 sv->sv_name,
5498 sref->sref_idx,
5499 si->sn_name);
5500 }
5501 break;
5502 case ISN_LOADS:
5503 {
5504 scriptitem_T *si = SCRIPT_ITEM(
5505 iptr->isn_arg.loadstore.ls_sid);
5506
5507 smsg("%s%4d LOADS s:%s from %s", pfx, current,
5508 iptr->isn_arg.loadstore.ls_name, si->sn_name);
5509 }
5510 break;
5511 case ISN_LOADAUTO:
5512 smsg("%s%4d LOADAUTO %s", pfx, current, iptr->isn_arg.string);
5513 break;
5514 case ISN_LOADG:
5515 smsg("%s%4d LOADG g:%s", pfx, current, iptr->isn_arg.string);
5516 break;
5517 case ISN_LOADB:
5518 smsg("%s%4d LOADB b:%s", pfx, current, iptr->isn_arg.string);
5519 break;
5520 case ISN_LOADW:
5521 smsg("%s%4d LOADW w:%s", pfx, current, iptr->isn_arg.string);
5522 break;
5523 case ISN_LOADT:
5524 smsg("%s%4d LOADT t:%s", pfx, current, iptr->isn_arg.string);
5525 break;
5526 case ISN_LOADGDICT:
5527 smsg("%s%4d LOAD g:", pfx, current);
5528 break;
5529 case ISN_LOADBDICT:
5530 smsg("%s%4d LOAD b:", pfx, current);
5531 break;
5532 case ISN_LOADWDICT:
5533 smsg("%s%4d LOAD w:", pfx, current);
5534 break;
5535 case ISN_LOADTDICT:
5536 smsg("%s%4d LOAD t:", pfx, current);
5537 break;
5538 case ISN_LOADOPT:
5539 smsg("%s%4d LOADOPT %s", pfx, current, iptr->isn_arg.string);
5540 break;
5541 case ISN_LOADENV:
5542 smsg("%s%4d LOADENV %s", pfx, current, iptr->isn_arg.string);
5543 break;
5544 case ISN_LOADREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005545 smsg("%s%4d LOADREG @%c", pfx, current,
5546 (int)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005547 break;
5548
5549 case ISN_STORE:
5550 if (iptr->isn_arg.number < 0)
5551 smsg("%s%4d STORE arg[%lld]", pfx, current,
5552 iptr->isn_arg.number + STACK_FRAME_SIZE);
5553 else
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005554 smsg("%s%4d STORE $%lld", pfx, current,
5555 iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005556 break;
5557 case ISN_STOREOUTER:
5558 {
5559 if (iptr->isn_arg.number < 0)
5560 smsg("%s%4d STOREOUTEr level %d arg[%d]", pfx, current,
5561 iptr->isn_arg.outer.outer_depth,
5562 iptr->isn_arg.outer.outer_idx + STACK_FRAME_SIZE);
5563 else
5564 smsg("%s%4d STOREOUTER level %d $%d", pfx, current,
5565 iptr->isn_arg.outer.outer_depth,
5566 iptr->isn_arg.outer.outer_idx);
5567 }
5568 break;
5569 case ISN_STOREV:
5570 smsg("%s%4d STOREV v:%s", pfx, current,
5571 get_vim_var_name(iptr->isn_arg.number));
5572 break;
5573 case ISN_STOREAUTO:
5574 smsg("%s%4d STOREAUTO %s", pfx, current, iptr->isn_arg.string);
5575 break;
5576 case ISN_STOREG:
5577 smsg("%s%4d STOREG %s", pfx, current, iptr->isn_arg.string);
5578 break;
5579 case ISN_STOREB:
5580 smsg("%s%4d STOREB %s", pfx, current, iptr->isn_arg.string);
5581 break;
5582 case ISN_STOREW:
5583 smsg("%s%4d STOREW %s", pfx, current, iptr->isn_arg.string);
5584 break;
5585 case ISN_STORET:
5586 smsg("%s%4d STORET %s", pfx, current, iptr->isn_arg.string);
5587 break;
5588 case ISN_STORES:
5589 {
5590 scriptitem_T *si = SCRIPT_ITEM(
5591 iptr->isn_arg.loadstore.ls_sid);
5592
5593 smsg("%s%4d STORES %s in %s", pfx, current,
5594 iptr->isn_arg.loadstore.ls_name, si->sn_name);
5595 }
5596 break;
5597 case ISN_STORESCRIPT:
5598 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005599 scriptref_T *sref = iptr->isn_arg.script.scriptref;
5600 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
5601 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005602
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005603 sv = get_script_svar(sref, -1);
5604 if (sv == NULL)
5605 smsg("%s%4d STORESCRIPT [deleted] in %s",
5606 pfx, current, si->sn_name);
5607 else
5608 smsg("%s%4d STORESCRIPT %s-%d in %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02005609 sv->sv_name,
5610 sref->sref_idx,
5611 si->sn_name);
5612 }
5613 break;
5614 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00005615 case ISN_STOREFUNCOPT:
5616 smsg("%s%4d %s &%s", pfx, current,
5617 iptr->isn_type == ISN_STOREOPT ? "STOREOPT" : "STOREFUNCOPT",
Bram Moolenaar4c137212021-04-19 16:48:48 +02005618 iptr->isn_arg.storeopt.so_name);
5619 break;
5620 case ISN_STOREENV:
5621 smsg("%s%4d STOREENV $%s", pfx, current, iptr->isn_arg.string);
5622 break;
5623 case ISN_STOREREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005624 smsg("%s%4d STOREREG @%c", pfx, current,
5625 (int)iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005626 break;
5627 case ISN_STORENR:
5628 smsg("%s%4d STORE %lld in $%d", pfx, current,
5629 iptr->isn_arg.storenr.stnr_val,
5630 iptr->isn_arg.storenr.stnr_idx);
5631 break;
5632
5633 case ISN_STOREINDEX:
5634 smsg("%s%4d STOREINDEX %s", pfx, current,
5635 vartype_name(iptr->isn_arg.vartype));
5636 break;
5637
5638 case ISN_STORERANGE:
5639 smsg("%s%4d STORERANGE", pfx, current);
5640 break;
5641
5642 // constants
5643 case ISN_PUSHNR:
5644 smsg("%s%4d PUSHNR %lld", pfx, current,
5645 (varnumber_T)(iptr->isn_arg.number));
5646 break;
5647 case ISN_PUSHBOOL:
5648 case ISN_PUSHSPEC:
5649 smsg("%s%4d PUSH %s", pfx, current,
5650 get_var_special_name(iptr->isn_arg.number));
5651 break;
5652 case ISN_PUSHF:
5653#ifdef FEAT_FLOAT
5654 smsg("%s%4d PUSHF %g", pfx, current, iptr->isn_arg.fnumber);
5655#endif
5656 break;
5657 case ISN_PUSHS:
5658 smsg("%s%4d PUSHS \"%s\"", pfx, current, iptr->isn_arg.string);
5659 break;
5660 case ISN_PUSHBLOB:
5661 {
5662 char_u *r;
5663 char_u numbuf[NUMBUFLEN];
5664 char_u *tofree;
5665
5666 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
5667 smsg("%s%4d PUSHBLOB %s", pfx, current, r);
5668 vim_free(tofree);
5669 }
5670 break;
5671 case ISN_PUSHFUNC:
5672 {
5673 char *name = (char *)iptr->isn_arg.string;
5674
5675 smsg("%s%4d PUSHFUNC \"%s\"", pfx, current,
5676 name == NULL ? "[none]" : name);
5677 }
5678 break;
5679 case ISN_PUSHCHANNEL:
5680#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar397a87a2022-03-20 21:14:15 +00005681 smsg("%s%4d PUSHCHANNEL 0", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005682#endif
5683 break;
5684 case ISN_PUSHJOB:
5685#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar397a87a2022-03-20 21:14:15 +00005686 smsg("%s%4d PUSHJOB \"no process\"", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005687#endif
5688 break;
5689 case ISN_PUSHEXC:
5690 smsg("%s%4d PUSH v:exception", pfx, current);
5691 break;
Bram Moolenaar06b77222022-01-25 15:51:56 +00005692 case ISN_AUTOLOAD:
5693 smsg("%s%4d AUTOLOAD %s", pfx, current, iptr->isn_arg.string);
5694 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005695 case ISN_UNLET:
5696 smsg("%s%4d UNLET%s %s", pfx, current,
5697 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
5698 iptr->isn_arg.unlet.ul_name);
5699 break;
5700 case ISN_UNLETENV:
5701 smsg("%s%4d UNLETENV%s $%s", pfx, current,
5702 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
5703 iptr->isn_arg.unlet.ul_name);
5704 break;
5705 case ISN_UNLETINDEX:
5706 smsg("%s%4d UNLETINDEX", pfx, current);
5707 break;
5708 case ISN_UNLETRANGE:
5709 smsg("%s%4d UNLETRANGE", pfx, current);
5710 break;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02005711 case ISN_LOCKUNLOCK:
5712 smsg("%s%4d LOCKUNLOCK %s", pfx, current, iptr->isn_arg.string);
5713 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005714 case ISN_LOCKCONST:
5715 smsg("%s%4d LOCKCONST", pfx, current);
5716 break;
5717 case ISN_NEWLIST:
5718 smsg("%s%4d NEWLIST size %lld", pfx, current,
5719 (varnumber_T)(iptr->isn_arg.number));
5720 break;
5721 case ISN_NEWDICT:
5722 smsg("%s%4d NEWDICT size %lld", pfx, current,
5723 (varnumber_T)(iptr->isn_arg.number));
5724 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00005725 case ISN_NEWPARTIAL:
5726 smsg("%s%4d NEWPARTIAL", pfx, current);
5727 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005728
5729 // function call
5730 case ISN_BCALL:
5731 {
5732 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
5733
5734 smsg("%s%4d BCALL %s(argc %d)", pfx, current,
5735 internal_func_name(cbfunc->cbf_idx),
5736 cbfunc->cbf_argcount);
5737 }
5738 break;
5739 case ISN_DCALL:
5740 {
5741 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
5742 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
5743 + cdfunc->cdf_idx;
5744
5745 smsg("%s%4d DCALL %s(argc %d)", pfx, current,
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005746 printable_func_name(df->df_ufunc),
5747 cdfunc->cdf_argcount);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005748 }
5749 break;
5750 case ISN_UCALL:
5751 {
5752 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
5753
5754 smsg("%s%4d UCALL %s(argc %d)", pfx, current,
5755 cufunc->cuf_name, cufunc->cuf_argcount);
5756 }
5757 break;
5758 case ISN_PCALL:
5759 {
5760 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
5761
5762 smsg("%s%4d PCALL%s (argc %d)", pfx, current,
5763 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
5764 }
5765 break;
5766 case ISN_PCALL_END:
5767 smsg("%s%4d PCALL end", pfx, current);
5768 break;
5769 case ISN_RETURN:
5770 smsg("%s%4d RETURN", pfx, current);
5771 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02005772 case ISN_RETURN_VOID:
5773 smsg("%s%4d RETURN void", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005774 break;
5775 case ISN_FUNCREF:
5776 {
5777 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar38453522021-11-28 22:00:12 +00005778 char_u *name;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005779
Bram Moolenaar38453522021-11-28 22:00:12 +00005780 if (funcref->fr_func_name == NULL)
5781 {
5782 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
5783 + funcref->fr_dfunc_idx;
5784 name = df->df_ufunc->uf_name;
5785 }
5786 else
5787 name = funcref->fr_func_name;
5788 smsg("%s%4d FUNCREF %s", pfx, current, name);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005789 }
5790 break;
5791
5792 case ISN_NEWFUNC:
5793 {
5794 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
5795
5796 smsg("%s%4d NEWFUNC %s %s", pfx, current,
5797 newfunc->nf_lambda, newfunc->nf_global);
5798 }
5799 break;
5800
5801 case ISN_DEF:
5802 {
5803 char_u *name = iptr->isn_arg.string;
5804
5805 smsg("%s%4d DEF %s", pfx, current,
5806 name == NULL ? (char_u *)"" : name);
5807 }
5808 break;
5809
5810 case ISN_JUMP:
5811 {
5812 char *when = "?";
5813
5814 switch (iptr->isn_arg.jump.jump_when)
5815 {
5816 case JUMP_ALWAYS:
5817 when = "JUMP";
5818 break;
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005819 case JUMP_NEVER:
5820 iemsg("JUMP_NEVER should not be used");
5821 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005822 case JUMP_AND_KEEP_IF_TRUE:
5823 when = "JUMP_AND_KEEP_IF_TRUE";
5824 break;
5825 case JUMP_IF_FALSE:
5826 when = "JUMP_IF_FALSE";
5827 break;
5828 case JUMP_AND_KEEP_IF_FALSE:
5829 when = "JUMP_AND_KEEP_IF_FALSE";
5830 break;
5831 case JUMP_IF_COND_FALSE:
5832 when = "JUMP_IF_COND_FALSE";
5833 break;
5834 case JUMP_IF_COND_TRUE:
5835 when = "JUMP_IF_COND_TRUE";
5836 break;
5837 }
5838 smsg("%s%4d %s -> %d", pfx, current, when,
5839 iptr->isn_arg.jump.jump_where);
5840 }
5841 break;
5842
5843 case ISN_JUMP_IF_ARG_SET:
5844 smsg("%s%4d JUMP_IF_ARG_SET arg[%d] -> %d", pfx, current,
5845 iptr->isn_arg.jumparg.jump_arg_off + STACK_FRAME_SIZE,
5846 iptr->isn_arg.jump.jump_where);
5847 break;
5848
5849 case ISN_FOR:
5850 {
5851 forloop_T *forloop = &iptr->isn_arg.forloop;
5852
5853 smsg("%s%4d FOR $%d -> %d", pfx, current,
5854 forloop->for_idx, forloop->for_end);
5855 }
5856 break;
5857
5858 case ISN_TRY:
5859 {
Bram Moolenaar0d807102021-12-21 09:42:09 +00005860 try_T *try = &iptr->isn_arg.tryref;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005861
5862 if (try->try_ref->try_finally == 0)
5863 smsg("%s%4d TRY catch -> %d, endtry -> %d",
5864 pfx, current,
5865 try->try_ref->try_catch,
5866 try->try_ref->try_endtry);
5867 else
5868 smsg("%s%4d TRY catch -> %d, finally -> %d, endtry -> %d",
5869 pfx, current,
5870 try->try_ref->try_catch,
5871 try->try_ref->try_finally,
5872 try->try_ref->try_endtry);
5873 }
5874 break;
5875 case ISN_CATCH:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005876 smsg("%s%4d CATCH", pfx, current);
5877 break;
5878 case ISN_TRYCONT:
5879 {
5880 trycont_T *trycont = &iptr->isn_arg.trycont;
5881
5882 smsg("%s%4d TRY-CONTINUE %d level%s -> %d", pfx, current,
5883 trycont->tct_levels,
5884 trycont->tct_levels == 1 ? "" : "s",
5885 trycont->tct_where);
5886 }
5887 break;
5888 case ISN_FINALLY:
5889 smsg("%s%4d FINALLY", pfx, current);
5890 break;
5891 case ISN_ENDTRY:
5892 smsg("%s%4d ENDTRY", pfx, current);
5893 break;
5894 case ISN_THROW:
5895 smsg("%s%4d THROW", pfx, current);
5896 break;
5897
5898 // expression operations on number
5899 case ISN_OPNR:
5900 case ISN_OPFLOAT:
5901 case ISN_OPANY:
5902 {
5903 char *what;
5904 char *ins;
5905
5906 switch (iptr->isn_arg.op.op_type)
5907 {
5908 case EXPR_MULT: what = "*"; break;
5909 case EXPR_DIV: what = "/"; break;
5910 case EXPR_REM: what = "%"; break;
5911 case EXPR_SUB: what = "-"; break;
5912 case EXPR_ADD: what = "+"; break;
5913 default: what = "???"; break;
5914 }
5915 switch (iptr->isn_type)
5916 {
5917 case ISN_OPNR: ins = "OPNR"; break;
5918 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
5919 case ISN_OPANY: ins = "OPANY"; break;
5920 default: ins = "???"; break;
5921 }
5922 smsg("%s%4d %s %s", pfx, current, ins, what);
5923 }
5924 break;
5925
5926 case ISN_COMPAREBOOL:
5927 case ISN_COMPARESPECIAL:
Bram Moolenaar7a222242022-03-01 19:23:24 +00005928 case ISN_COMPARENULL:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005929 case ISN_COMPARENR:
5930 case ISN_COMPAREFLOAT:
5931 case ISN_COMPARESTRING:
5932 case ISN_COMPAREBLOB:
5933 case ISN_COMPARELIST:
5934 case ISN_COMPAREDICT:
5935 case ISN_COMPAREFUNC:
5936 case ISN_COMPAREANY:
5937 {
5938 char *p;
5939 char buf[10];
5940 char *type;
5941
5942 switch (iptr->isn_arg.op.op_type)
5943 {
5944 case EXPR_EQUAL: p = "=="; break;
5945 case EXPR_NEQUAL: p = "!="; break;
5946 case EXPR_GREATER: p = ">"; break;
5947 case EXPR_GEQUAL: p = ">="; break;
5948 case EXPR_SMALLER: p = "<"; break;
5949 case EXPR_SEQUAL: p = "<="; break;
5950 case EXPR_MATCH: p = "=~"; break;
5951 case EXPR_IS: p = "is"; break;
5952 case EXPR_ISNOT: p = "isnot"; break;
5953 case EXPR_NOMATCH: p = "!~"; break;
5954 default: p = "???"; break;
5955 }
5956 STRCPY(buf, p);
5957 if (iptr->isn_arg.op.op_ic == TRUE)
5958 strcat(buf, "?");
5959 switch(iptr->isn_type)
5960 {
5961 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
5962 case ISN_COMPARESPECIAL:
5963 type = "COMPARESPECIAL"; break;
Bram Moolenaar7a222242022-03-01 19:23:24 +00005964 case ISN_COMPARENULL: type = "COMPARENULL"; break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005965 case ISN_COMPARENR: type = "COMPARENR"; break;
5966 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
5967 case ISN_COMPARESTRING:
5968 type = "COMPARESTRING"; break;
5969 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
5970 case ISN_COMPARELIST: type = "COMPARELIST"; break;
5971 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
5972 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
5973 case ISN_COMPAREANY: type = "COMPAREANY"; break;
5974 default: type = "???"; break;
5975 }
5976
5977 smsg("%s%4d %s %s", pfx, current, type, buf);
5978 }
5979 break;
5980
5981 case ISN_ADDLIST: smsg("%s%4d ADDLIST", pfx, current); break;
5982 case ISN_ADDBLOB: smsg("%s%4d ADDBLOB", pfx, current); break;
5983
5984 // expression operations
5985 case ISN_CONCAT: smsg("%s%4d CONCAT", pfx, current); break;
5986 case ISN_STRINDEX: smsg("%s%4d STRINDEX", pfx, current); break;
5987 case ISN_STRSLICE: smsg("%s%4d STRSLICE", pfx, current); break;
5988 case ISN_BLOBINDEX: smsg("%s%4d BLOBINDEX", pfx, current); break;
5989 case ISN_BLOBSLICE: smsg("%s%4d BLOBSLICE", pfx, current); break;
5990 case ISN_LISTAPPEND: smsg("%s%4d LISTAPPEND", pfx, current); break;
5991 case ISN_BLOBAPPEND: smsg("%s%4d BLOBAPPEND", pfx, current); break;
5992 case ISN_LISTINDEX: smsg("%s%4d LISTINDEX", pfx, current); break;
5993 case ISN_LISTSLICE: smsg("%s%4d LISTSLICE", pfx, current); break;
5994 case ISN_ANYINDEX: smsg("%s%4d ANYINDEX", pfx, current); break;
5995 case ISN_ANYSLICE: smsg("%s%4d ANYSLICE", pfx, current); break;
5996 case ISN_SLICE: smsg("%s%4d SLICE %lld",
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02005997 pfx, current, iptr->isn_arg.number); break;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02005998 case ISN_GETITEM: smsg("%s%4d ITEM %lld%s", pfx, current,
5999 iptr->isn_arg.getitem.gi_index,
6000 iptr->isn_arg.getitem.gi_with_op ?
6001 " with op" : ""); break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006002 case ISN_MEMBER: smsg("%s%4d MEMBER", pfx, current); break;
6003 case ISN_STRINGMEMBER: smsg("%s%4d MEMBER %s", pfx, current,
6004 iptr->isn_arg.string); break;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02006005 case ISN_CLEARDICT: smsg("%s%4d CLEARDICT", pfx, current); break;
6006 case ISN_USEDICT: smsg("%s%4d USEDICT", pfx, current); break;
6007
Bram Moolenaar4c137212021-04-19 16:48:48 +02006008 case ISN_NEGATENR: smsg("%s%4d NEGATENR", pfx, current); break;
6009
6010 case ISN_CHECKNR: smsg("%s%4d CHECKNR", pfx, current); break;
6011 case ISN_CHECKTYPE:
6012 {
6013 checktype_T *ct = &iptr->isn_arg.type;
6014 char *tofree;
6015
6016 if (ct->ct_arg_idx == 0)
6017 smsg("%s%4d CHECKTYPE %s stack[%d]", pfx, current,
6018 type_name(ct->ct_type, &tofree),
6019 (int)ct->ct_off);
6020 else
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02006021 smsg("%s%4d CHECKTYPE %s stack[%d] arg %d",
6022 pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02006023 type_name(ct->ct_type, &tofree),
6024 (int)ct->ct_off,
6025 (int)ct->ct_arg_idx);
6026 vim_free(tofree);
6027 break;
6028 }
6029 case ISN_CHECKLEN: smsg("%s%4d CHECKLEN %s%d", pfx, current,
6030 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
6031 iptr->isn_arg.checklen.cl_min_len);
6032 break;
6033 case ISN_SETTYPE:
6034 {
6035 char *tofree;
6036
6037 smsg("%s%4d SETTYPE %s", pfx, current,
6038 type_name(iptr->isn_arg.type.ct_type, &tofree));
6039 vim_free(tofree);
6040 break;
6041 }
6042 case ISN_COND2BOOL: smsg("%s%4d COND2BOOL", pfx, current); break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006043 case ISN_2BOOL: if (iptr->isn_arg.tobool.invert)
6044 smsg("%s%4d INVERT %d (!val)", pfx, current,
6045 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006046 else
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006047 smsg("%s%4d 2BOOL %d (!!val)", pfx, current,
6048 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006049 break;
6050 case ISN_2STRING: smsg("%s%4d 2STRING stack[%lld]", pfx, current,
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006051 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006052 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006053 case ISN_2STRING_ANY: smsg("%s%4d 2STRING_ANY stack[%lld]",
6054 pfx, current,
6055 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006056 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006057 case ISN_RANGE: smsg("%s%4d RANGE %s", pfx, current,
6058 iptr->isn_arg.string);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006059 break;
6060 case ISN_PUT:
6061 if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE)
6062 smsg("%s%4d PUT %c above range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006063 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006064 else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE)
6065 smsg("%s%4d PUT %c range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006066 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006067 else
6068 smsg("%s%4d PUT %c %ld", pfx, current,
6069 iptr->isn_arg.put.put_regname,
6070 (long)iptr->isn_arg.put.put_lnum);
6071 break;
6072
Bram Moolenaar4c137212021-04-19 16:48:48 +02006073 case ISN_CMDMOD:
6074 {
6075 char_u *buf;
6076 size_t len = produce_cmdmods(
6077 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
6078
6079 buf = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02006080 if (likely(buf != NULL))
Bram Moolenaar4c137212021-04-19 16:48:48 +02006081 {
6082 (void)produce_cmdmods(
6083 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
6084 smsg("%s%4d CMDMOD %s", pfx, current, buf);
6085 vim_free(buf);
6086 }
6087 break;
6088 }
6089 case ISN_CMDMOD_REV: smsg("%s%4d CMDMOD_REV", pfx, current); break;
6090
6091 case ISN_PROF_START:
Bram Moolenaare99d4222021-06-13 14:01:26 +02006092 smsg("%s%4d PROFILE START line %d", pfx, current,
6093 iptr->isn_lnum);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006094 break;
6095
6096 case ISN_PROF_END:
6097 smsg("%s%4d PROFILE END", pfx, current);
6098 break;
6099
Bram Moolenaare99d4222021-06-13 14:01:26 +02006100 case ISN_DEBUG:
Bram Moolenaar8cec9272021-06-23 20:20:53 +02006101 smsg("%s%4d DEBUG line %d-%d varcount %lld", pfx, current,
6102 iptr->isn_arg.debug.dbg_break_lnum + 1,
6103 iptr->isn_lnum,
6104 iptr->isn_arg.debug.dbg_var_names_len);
Bram Moolenaare99d4222021-06-13 14:01:26 +02006105 break;
6106
Bram Moolenaar4c137212021-04-19 16:48:48 +02006107 case ISN_UNPACK: smsg("%s%4d UNPACK %d%s", pfx, current,
6108 iptr->isn_arg.unpack.unp_count,
6109 iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : "");
6110 break;
6111 case ISN_SHUFFLE: smsg("%s%4d SHUFFLE %d up %d", pfx, current,
6112 iptr->isn_arg.shuffle.shfl_item,
6113 iptr->isn_arg.shuffle.shfl_up);
6114 break;
6115 case ISN_DROP: smsg("%s%4d DROP", pfx, current); break;
6116
6117 case ISN_FINISH: // End of list of instructions for ISN_SUBSTITUTE.
6118 return;
6119 }
6120
6121 out_flush(); // output one line at a time
6122 ui_breakcheck();
6123 if (got_int)
6124 break;
6125 }
6126}
6127
6128/*
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02006129 * Handle command line completion for the :disassemble command.
6130 */
6131 void
6132set_context_in_disassemble_cmd(expand_T *xp, char_u *arg)
6133{
6134 char_u *p;
6135
6136 // Default: expand user functions, "debug" and "profile"
6137 xp->xp_context = EXPAND_DISASSEMBLE;
6138 xp->xp_pattern = arg;
6139
6140 // first argument already typed: only user function names
6141 if (*arg != NUL && *(p = skiptowhite(arg)) != NUL)
6142 {
6143 xp->xp_context = EXPAND_USER_FUNC;
6144 xp->xp_pattern = skipwhite(p);
6145 }
6146}
6147
6148/*
6149 * Function given to ExpandGeneric() to obtain the list of :disassemble
6150 * arguments.
6151 */
6152 char_u *
6153get_disassemble_argument(expand_T *xp, int idx)
6154{
6155 if (idx == 0)
6156 return (char_u *)"debug";
6157 if (idx == 1)
6158 return (char_u *)"profile";
6159 return get_user_func_name(xp, idx - 2);
6160}
6161
6162/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01006163 * ":disassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01006164 * We don't really need this at runtime, but we do have tests that require it,
6165 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006166 */
6167 void
6168ex_disassemble(exarg_T *eap)
6169{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01006170 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01006171 char_u *fname;
6172 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006173 dfunc_T *dfunc;
6174 isn_T *instr;
Bram Moolenaarb2049902021-01-24 12:53:53 +01006175 int instr_count;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02006176 int is_global = FALSE;
Bram Moolenaare99d4222021-06-13 14:01:26 +02006177 compiletype_T compile_type = CT_NONE;
6178
Bram Moolenaarc7d5fc82021-12-05 17:20:24 +00006179 if (STRNCMP(arg, "profile", 7) == 0 && VIM_ISWHITE(arg[7]))
Bram Moolenaare99d4222021-06-13 14:01:26 +02006180 {
6181 compile_type = CT_PROFILE;
6182 arg = skipwhite(arg + 7);
6183 }
Bram Moolenaarc7d5fc82021-12-05 17:20:24 +00006184 else if (STRNCMP(arg, "debug", 5) == 0 && VIM_ISWHITE(arg[5]))
Bram Moolenaare99d4222021-06-13 14:01:26 +02006185 {
6186 compile_type = CT_DEBUG;
6187 arg = skipwhite(arg + 5);
6188 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006189
Bram Moolenaarbfd65582020-07-13 18:18:00 +02006190 if (STRNCMP(arg, "<lambda>", 8) == 0)
6191 {
6192 arg += 8;
6193 (void)getdigits(&arg);
6194 fname = vim_strnsave(eap->arg, arg - eap->arg);
6195 }
6196 else
6197 fname = trans_function_name(&arg, &is_global, FALSE,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01006198 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL, NULL);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01006199 if (fname == NULL)
6200 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006201 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01006202 return;
6203 }
6204
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00006205 ufunc = find_func(fname, is_global);
Bram Moolenaara26b9702020-04-18 19:53:28 +02006206 if (ufunc == NULL)
6207 {
6208 char_u *p = untrans_function_name(fname);
6209
6210 if (p != NULL)
6211 // Try again without making it script-local.
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00006212 ufunc = find_func(p, FALSE);
Bram Moolenaara26b9702020-04-18 19:53:28 +02006213 }
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01006214 vim_free(fname);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006215 if (ufunc == NULL)
6216 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006217 semsg(_(e_cannot_find_function_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006218 return;
6219 }
Bram Moolenaare99d4222021-06-13 14:01:26 +02006220 if (func_needs_compiling(ufunc, compile_type)
6221 && compile_def_function(ufunc, FALSE, compile_type, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02006222 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006223 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006224 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006225 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006226 return;
6227 }
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006228 msg((char *)printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006229
6230 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
Bram Moolenaare99d4222021-06-13 14:01:26 +02006231 switch (compile_type)
6232 {
6233 case CT_PROFILE:
Bram Moolenaarf002a412021-01-24 13:34:18 +01006234#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02006235 instr = dfunc->df_instr_prof;
6236 instr_count = dfunc->df_instr_prof_count;
6237 break;
Bram Moolenaarf002a412021-01-24 13:34:18 +01006238#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02006239 // FALLTHROUGH
6240 case CT_NONE:
6241 instr = dfunc->df_instr;
6242 instr_count = dfunc->df_instr_count;
6243 break;
6244 case CT_DEBUG:
6245 instr = dfunc->df_instr_debug;
6246 instr_count = dfunc->df_instr_debug_count;
6247 break;
6248 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006249
Bram Moolenaar4c137212021-04-19 16:48:48 +02006250 list_instructions("", instr, instr_count, ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006251}
6252
6253/*
Bram Moolenaar13106602020-10-04 16:06:05 +02006254 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006255 * list, etc. Mostly like what JavaScript does, except that empty list and
6256 * empty dictionary are FALSE.
6257 */
6258 int
6259tv2bool(typval_T *tv)
6260{
6261 switch (tv->v_type)
6262 {
6263 case VAR_NUMBER:
6264 return tv->vval.v_number != 0;
6265 case VAR_FLOAT:
6266#ifdef FEAT_FLOAT
6267 return tv->vval.v_float != 0.0;
6268#else
6269 break;
6270#endif
6271 case VAR_PARTIAL:
6272 return tv->vval.v_partial != NULL;
6273 case VAR_FUNC:
6274 case VAR_STRING:
6275 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
6276 case VAR_LIST:
6277 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
6278 case VAR_DICT:
6279 return tv->vval.v_dict != NULL
6280 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
6281 case VAR_BOOL:
6282 case VAR_SPECIAL:
6283 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
6284 case VAR_JOB:
6285#ifdef FEAT_JOB_CHANNEL
6286 return tv->vval.v_job != NULL;
6287#else
6288 break;
6289#endif
6290 case VAR_CHANNEL:
6291#ifdef FEAT_JOB_CHANNEL
6292 return tv->vval.v_channel != NULL;
6293#else
6294 break;
6295#endif
6296 case VAR_BLOB:
6297 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
6298 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006299 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006300 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006301 case VAR_INSTR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006302 break;
6303 }
6304 return FALSE;
6305}
6306
Bram Moolenaarea2d4072020-11-12 12:08:51 +01006307 void
6308emsg_using_string_as(typval_T *tv, int as_number)
6309{
6310 semsg(_(as_number ? e_using_string_as_number_str
6311 : e_using_string_as_bool_str),
6312 tv->vval.v_string == NULL
6313 ? (char_u *)"" : tv->vval.v_string);
6314}
6315
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006316/*
6317 * If "tv" is a string give an error and return FAIL.
6318 */
6319 int
6320check_not_string(typval_T *tv)
6321{
6322 if (tv->v_type == VAR_STRING)
6323 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +01006324 emsg_using_string_as(tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006325 clear_tv(tv);
6326 return FAIL;
6327 }
6328 return OK;
6329}
6330
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006331
6332#endif // FEAT_EVAL