blob: a95a488068b87563215efc2a7a1493bdf1141560 [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;
940
941 return OK;
942}
943
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200944// Ugly global to avoid passing the execution context around through many
945// layers.
946static ectx_T *current_ectx = NULL;
947
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100948/*
949 * Call a builtin function by index.
950 */
951 static int
952call_bfunc(int func_idx, int argcount, ectx_T *ectx)
953{
954 typval_T argvars[MAX_FUNC_ARGS];
955 int idx;
Bram Moolenaar171fb922020-10-28 16:54:47 +0100956 int did_emsg_before = did_emsg;
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200957 ectx_T *prev_ectx = current_ectx;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +0200958 char *save_func_name = ectx->ec_where.wt_func_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100959
960 if (call_prepare(argcount, argvars, ectx) == FAIL)
961 return FAIL;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +0200962 ectx->ec_where.wt_func_name = internal_func_name(func_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100963
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200964 // Call the builtin function. Set "current_ectx" so that when it
965 // recursively invokes call_def_function() a closure context can be set.
966 current_ectx = ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100967 call_internal_func_by_idx(func_idx, argvars, STACK_TV_BOT(-1));
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200968 current_ectx = prev_ectx;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +0200969 ectx->ec_where.wt_func_name = save_func_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100970
971 // Clear the arguments.
972 for (idx = 0; idx < argcount; ++idx)
973 clear_tv(&argvars[idx]);
Bram Moolenaar015f4262020-05-05 21:25:22 +0200974
Bram Moolenaar57f799e2020-12-12 20:42:19 +0100975 if (did_emsg > did_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200976 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100977 return OK;
978}
979
980/*
981 * Execute a user defined function.
Bram Moolenaarab360522021-01-10 14:02:28 +0100982 * If the function is compiled this will add a stack frame and set the
983 * instruction pointer at the start of the function.
984 * Otherwise the function is called here.
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200985 * If "pt" is not null use "pt->pt_outer" for ec_outer_ref->or_outer.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100986 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100987 */
988 static int
Bram Moolenaar0186e582021-01-10 18:33:11 +0100989call_ufunc(
990 ufunc_T *ufunc,
991 partial_T *pt,
992 int argcount,
993 ectx_T *ectx,
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +0200994 isn_T *iptr,
995 dict_T *selfdict)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100996{
997 typval_T argvars[MAX_FUNC_ARGS];
998 funcexe_T funcexe;
999 int error;
1000 int idx;
Bram Moolenaar28ee8922020-10-28 20:20:00 +01001001 int did_emsg_before = did_emsg;
Bram Moolenaar139575d2022-03-15 19:29:30 +00001002 compiletype_T compile_type = get_compile_type(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001003
Bram Moolenaare99d4222021-06-13 14:01:26 +02001004 if (func_needs_compiling(ufunc, compile_type)
1005 && compile_def_function(ufunc, FALSE, compile_type, NULL)
1006 == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02001007 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001008 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001009 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +01001010 error = check_user_func_argcount(ufunc, argcount);
Bram Moolenaar50824712020-12-20 21:10:17 +01001011 if (error != FCERR_UNKNOWN)
1012 {
1013 if (error == FCERR_TOOMANY)
Bram Moolenaare1242042021-12-16 20:56:57 +00001014 semsg(_(e_too_many_arguments_for_function_str), ufunc->uf_name);
Bram Moolenaar50824712020-12-20 21:10:17 +01001015 else
Bram Moolenaare1242042021-12-16 20:56:57 +00001016 semsg(_(e_not_enough_arguments_for_function_str),
1017 ufunc->uf_name);
Bram Moolenaar50824712020-12-20 21:10:17 +01001018 return FAIL;
1019 }
1020
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001021 // The function has been compiled, can call it quickly. For a function
1022 // that was defined later: we can call it directly next time.
1023 if (iptr != NULL)
1024 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001025 delete_instr(iptr);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001026 iptr->isn_type = ISN_DCALL;
1027 iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1028 iptr->isn_arg.dfunc.cdf_argcount = argcount;
1029 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001030 return call_dfunc(ufunc->uf_dfunc_idx, pt, argcount, ectx);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001031 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001032
1033 if (call_prepare(argcount, argvars, ectx) == FAIL)
1034 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +02001035 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00001036 funcexe.fe_evaluate = TRUE;
1037 funcexe.fe_selfdict = selfdict != NULL ? selfdict : dict_stack_get_dict();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001038
1039 // Call the user function. Result goes in last position on the stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001040 error = call_user_func_check(ufunc, argcount, argvars,
Bram Moolenaar851f86b2021-12-13 14:26:44 +00001041 STACK_TV_BOT(-1), &funcexe, funcexe.fe_selfdict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001042
1043 // Clear the arguments.
1044 for (idx = 0; idx < argcount; ++idx)
1045 clear_tv(&argvars[idx]);
1046
1047 if (error != FCERR_NONE)
1048 {
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001049 user_func_error(error, ufunc->uf_name, &funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001050 return FAIL;
1051 }
Bram Moolenaar28ee8922020-10-28 20:20:00 +01001052 if (did_emsg > did_emsg_before)
Bram Moolenaared677f52020-08-12 16:38:10 +02001053 // Error other than from calling the function itself.
1054 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001055 return OK;
1056}
1057
1058/*
Bram Moolenaara91a7132021-03-25 21:12:15 +01001059 * If command modifiers were applied restore them.
1060 */
1061 static void
1062may_restore_cmdmod(funclocal_T *funclocal)
1063{
1064 if (funclocal->floc_restore_cmdmod)
1065 {
1066 cmdmod.cmod_filter_regmatch.regprog = NULL;
1067 undo_cmdmod(&cmdmod);
1068 cmdmod = funclocal->floc_save_cmdmod;
1069 funclocal->floc_restore_cmdmod = FALSE;
1070 }
1071}
1072
1073/*
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001074 * Return TRUE if an error was given (not caught in try/catch) or CTRL-C was
1075 * pressed.
Bram Moolenaara1773442020-08-12 15:21:22 +02001076 */
1077 static int
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001078vim9_aborting(int prev_uncaught_emsg)
Bram Moolenaara1773442020-08-12 15:21:22 +02001079{
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001080 return uncaught_emsg > prev_uncaught_emsg || got_int || did_throw;
Bram Moolenaara1773442020-08-12 15:21:22 +02001081}
1082
1083/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001084 * Execute a function by "name".
1085 * This can be a builtin function or a user function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001086 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001087 * Returns FAIL if not found without an error message.
1088 */
1089 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001090call_by_name(
1091 char_u *name,
1092 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001093 ectx_T *ectx,
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001094 isn_T *iptr,
1095 dict_T *selfdict)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001096{
1097 ufunc_T *ufunc;
1098
1099 if (builtin_function(name, -1))
1100 {
1101 int func_idx = find_internal_func(name);
1102
Bram Moolenaar1983f1a2022-02-28 20:55:02 +00001103 if (func_idx < 0) // Impossible?
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001104 return FAIL;
Bram Moolenaar389df252020-07-09 21:20:47 +02001105 if (check_internal_func(func_idx, argcount) < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001106 return FAIL;
1107 return call_bfunc(func_idx, argcount, ectx);
1108 }
1109
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00001110 ufunc = find_func(name, FALSE);
Bram Moolenaara1773442020-08-12 15:21:22 +02001111
1112 if (ufunc == NULL)
1113 {
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001114 int prev_uncaught_emsg = uncaught_emsg;
Bram Moolenaara1773442020-08-12 15:21:22 +02001115
1116 if (script_autoload(name, TRUE))
1117 // loaded a package, search for the function again
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00001118 ufunc = find_func(name, FALSE);
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001119
1120 if (vim9_aborting(prev_uncaught_emsg))
Bram Moolenaara1773442020-08-12 15:21:22 +02001121 return FAIL; // bail out if loading the script caused an error
1122 }
1123
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001124 if (ufunc != NULL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001125 {
Bram Moolenaar6ce46b92021-08-07 15:35:36 +02001126 if (ufunc->uf_arg_types != NULL || ufunc->uf_va_type != NULL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001127 {
1128 int i;
1129 typval_T *argv = STACK_TV_BOT(0) - argcount;
1130
1131 // The function can change at runtime, check that the argument
1132 // types are correct.
1133 for (i = 0; i < argcount; ++i)
1134 {
Bram Moolenaare3ffcd92021-03-08 21:47:13 +01001135 type_T *type = NULL;
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001136
Bram Moolenaar6ce46b92021-08-07 15:35:36 +02001137 if (i < ufunc->uf_args.ga_len && ufunc->uf_arg_types != NULL)
Bram Moolenaare3ffcd92021-03-08 21:47:13 +01001138 type = ufunc->uf_arg_types[i];
1139 else if (ufunc->uf_va_type != NULL)
1140 type = ufunc->uf_va_type->tt_member;
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001141 if (type != NULL && check_typval_arg_type(type,
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001142 &argv[i], NULL, i + 1) == FAIL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001143 return FAIL;
1144 }
1145 }
1146
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001147 return call_ufunc(ufunc, NULL, argcount, ectx, iptr, selfdict);
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001148 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001149
1150 return FAIL;
1151}
1152
1153 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001154call_partial(
1155 typval_T *tv,
1156 int argcount_arg,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001157 ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001158{
Bram Moolenaara90afb92020-07-15 22:38:56 +02001159 int argcount = argcount_arg;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001160 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001161 int called_emsg_before = called_emsg;
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +01001162 int res = FAIL;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001163 dict_T *selfdict = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001164
1165 if (tv->v_type == VAR_PARTIAL)
1166 {
Bram Moolenaara90afb92020-07-15 22:38:56 +02001167 partial_T *pt = tv->vval.v_partial;
1168 int i;
1169
1170 if (pt->pt_argc > 0)
1171 {
1172 // Make space for arguments from the partial, shift the "argcount"
1173 // arguments up.
Bram Moolenaar35578162021-08-02 19:10:38 +02001174 if (GA_GROW_FAILS(&ectx->ec_stack, pt->pt_argc))
Bram Moolenaara90afb92020-07-15 22:38:56 +02001175 return FAIL;
1176 for (i = 1; i <= argcount; ++i)
1177 *STACK_TV_BOT(-i + pt->pt_argc) = *STACK_TV_BOT(-i);
1178 ectx->ec_stack.ga_len += pt->pt_argc;
1179 argcount += pt->pt_argc;
1180
1181 // copy the arguments from the partial onto the stack
1182 for (i = 0; i < pt->pt_argc; ++i)
1183 copy_tv(&pt->pt_argv[i], STACK_TV_BOT(-argcount + i));
1184 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001185 selfdict = pt->pt_dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001186
1187 if (pt->pt_func != NULL)
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001188 return call_ufunc(pt->pt_func, pt, argcount, ectx, NULL, selfdict);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001189
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001190 name = pt->pt_name;
1191 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001192 else if (tv->v_type == VAR_FUNC)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001193 name = tv->vval.v_string;
Bram Moolenaar95006e32020-08-29 17:47:08 +02001194 if (name != NULL)
1195 {
1196 char_u fname_buf[FLEN_FIXED + 1];
1197 char_u *tofree = NULL;
1198 int error = FCERR_NONE;
1199 char_u *fname;
1200
1201 // May need to translate <SNR>123_ to K_SNR.
1202 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
1203 if (error != FCERR_NONE)
1204 res = FAIL;
1205 else
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001206 res = call_by_name(fname, argcount, ectx, NULL, selfdict);
Bram Moolenaar95006e32020-08-29 17:47:08 +02001207 vim_free(tofree);
1208 }
1209
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +01001210 if (res == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001211 {
1212 if (called_emsg == called_emsg_before)
Bram Moolenaare1242042021-12-16 20:56:57 +00001213 semsg(_(e_unknown_function_str),
Bram Moolenaar015f4262020-05-05 21:25:22 +02001214 name == NULL ? (char_u *)"[unknown]" : name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001215 return FAIL;
1216 }
1217 return OK;
1218}
1219
1220/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001221 * Check if "lock" is VAR_LOCKED or VAR_FIXED. If so give an error and return
1222 * TRUE.
1223 */
1224 static int
1225error_if_locked(int lock, char *error)
1226{
1227 if (lock & (VAR_LOCKED | VAR_FIXED))
1228 {
1229 emsg(_(error));
1230 return TRUE;
1231 }
1232 return FALSE;
1233}
1234
1235/*
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001236 * Give an error if "tv" is not a number and return FAIL.
1237 */
1238 static int
1239check_for_number(typval_T *tv)
1240{
1241 if (tv->v_type != VAR_NUMBER)
1242 {
1243 semsg(_(e_expected_str_but_got_str),
1244 vartype_name(VAR_NUMBER), vartype_name(tv->v_type));
1245 return FAIL;
1246 }
1247 return OK;
1248}
1249
1250/*
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001251 * Store "tv" in variable "name".
1252 * This is for s: and g: variables.
1253 */
1254 static void
1255store_var(char_u *name, typval_T *tv)
1256{
1257 funccal_entry_T entry;
Bram Moolenaard877a572021-04-01 19:42:48 +02001258 int flags = ASSIGN_DECL;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001259
Bram Moolenaard877a572021-04-01 19:42:48 +02001260 if (tv->v_lock)
1261 flags |= ASSIGN_CONST;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001262 save_funccal(&entry);
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001263 set_var_const(name, 0, NULL, tv, FALSE, flags, 0);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001264 restore_funccal();
1265}
1266
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001267/*
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001268 * Convert "tv" to a string.
1269 * Return FAIL if not allowed.
1270 */
1271 static int
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001272do_2string(typval_T *tv, int is_2string_any, int tolerant)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001273{
1274 if (tv->v_type != VAR_STRING)
1275 {
1276 char_u *str;
1277
1278 if (is_2string_any)
1279 {
1280 switch (tv->v_type)
1281 {
1282 case VAR_SPECIAL:
1283 case VAR_BOOL:
1284 case VAR_NUMBER:
1285 case VAR_FLOAT:
1286 case VAR_BLOB: break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001287
1288 case VAR_LIST:
1289 if (tolerant)
1290 {
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001291 char_u *s, *e, *p;
1292 garray_T ga;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001293
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001294 ga_init2(&ga, sizeof(char_u *), 1);
1295
1296 // Convert to NL separated items, then
1297 // escape the items and replace the NL with
1298 // a space.
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001299 str = typval2string(tv, TRUE);
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001300 if (str == NULL)
1301 return FAIL;
1302 s = str;
1303 while ((e = vim_strchr(s, '\n')) != NULL)
1304 {
1305 *e = NUL;
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +01001306 p = vim_strsave_fnameescape(s,
1307 VSE_NONE);
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001308 if (p != NULL)
1309 {
1310 ga_concat(&ga, p);
1311 ga_concat(&ga, (char_u *)" ");
1312 vim_free(p);
1313 }
1314 s = e + 1;
1315 }
1316 vim_free(str);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001317 clear_tv(tv);
1318 tv->v_type = VAR_STRING;
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001319 tv->vval.v_string = ga.ga_data;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001320 return OK;
1321 }
1322 // FALLTHROUGH
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001323 default: to_string_error(tv->v_type);
1324 return FAIL;
1325 }
1326 }
Bram Moolenaar34453202021-01-31 13:08:38 +01001327 str = typval_tostring(tv, TRUE);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001328 clear_tv(tv);
1329 tv->v_type = VAR_STRING;
1330 tv->vval.v_string = str;
1331 }
1332 return OK;
1333}
1334
1335/*
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001336 * When the value of "sv" is a null list of dict, allocate it.
1337 */
1338 static void
Bram Moolenaar859cc212022-03-28 15:22:35 +01001339allocate_if_null(svar_T *sv)
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001340{
Bram Moolenaar859cc212022-03-28 15:22:35 +01001341 typval_T *tv = sv->sv_tv;
1342
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001343 switch (tv->v_type)
1344 {
1345 case VAR_LIST:
Bram Moolenaar859cc212022-03-28 15:22:35 +01001346 if (tv->vval.v_list == NULL && sv->sv_type != &t_list_empty)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001347 (void)rettv_list_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001348 break;
1349 case VAR_DICT:
Bram Moolenaar859cc212022-03-28 15:22:35 +01001350 if (tv->vval.v_dict == NULL && sv->sv_type != &t_dict_empty)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001351 (void)rettv_dict_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001352 break;
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02001353 case VAR_BLOB:
Bram Moolenaar859cc212022-03-28 15:22:35 +01001354 if (tv->vval.v_blob == NULL && sv->sv_type != &t_blob_null)
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02001355 (void)rettv_blob_alloc(tv);
1356 break;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001357 default:
1358 break;
1359 }
1360}
Bram Moolenaard3aac292020-04-19 14:32:17 +02001361
Bram Moolenaare7525c52021-01-09 13:20:37 +01001362/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001363 * Return the character "str[index]" where "index" is the character index,
1364 * including composing characters.
1365 * If "index" is out of range NULL is returned.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001366 */
1367 char_u *
1368char_from_string(char_u *str, varnumber_T index)
1369{
1370 size_t nbyte = 0;
1371 varnumber_T nchar = index;
1372 size_t slen;
1373
1374 if (str == NULL)
1375 return NULL;
1376 slen = STRLEN(str);
1377
Bram Moolenaarff871402021-03-26 13:34:05 +01001378 // Do the same as for a list: a negative index counts from the end.
1379 // Optimization to check the first byte to be below 0x80 (and no composing
1380 // character follows) makes this a lot faster.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001381 if (index < 0)
1382 {
1383 int clen = 0;
1384
1385 for (nbyte = 0; nbyte < slen; ++clen)
Bram Moolenaarff871402021-03-26 13:34:05 +01001386 {
1387 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1388 ++nbyte;
1389 else if (enc_utf8)
1390 nbyte += utfc_ptr2len(str + nbyte);
1391 else
1392 nbyte += mb_ptr2len(str + nbyte);
1393 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001394 nchar = clen + index;
1395 if (nchar < 0)
1396 // unlike list: index out of range results in empty string
1397 return NULL;
1398 }
1399
1400 for (nbyte = 0; nchar > 0 && nbyte < slen; --nchar)
Bram Moolenaarff871402021-03-26 13:34:05 +01001401 {
1402 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1403 ++nbyte;
1404 else if (enc_utf8)
1405 nbyte += utfc_ptr2len(str + nbyte);
1406 else
1407 nbyte += mb_ptr2len(str + nbyte);
1408 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001409 if (nbyte >= slen)
1410 return NULL;
Bram Moolenaar0289a092021-03-14 18:40:19 +01001411 return vim_strnsave(str + nbyte, mb_ptr2len(str + nbyte));
Bram Moolenaare7525c52021-01-09 13:20:37 +01001412}
1413
1414/*
1415 * Get the byte index for character index "idx" in string "str" with length
Bram Moolenaar0289a092021-03-14 18:40:19 +01001416 * "str_len". Composing characters are included.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001417 * If going over the end return "str_len".
1418 * If "idx" is negative count from the end, -1 is the last character.
1419 * When going over the start return -1.
1420 */
1421 static long
1422char_idx2byte(char_u *str, size_t str_len, varnumber_T idx)
1423{
1424 varnumber_T nchar = idx;
1425 size_t nbyte = 0;
1426
1427 if (nchar >= 0)
1428 {
1429 while (nchar > 0 && nbyte < str_len)
1430 {
Bram Moolenaar0289a092021-03-14 18:40:19 +01001431 nbyte += mb_ptr2len(str + nbyte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001432 --nchar;
1433 }
1434 }
1435 else
1436 {
1437 nbyte = str_len;
1438 while (nchar < 0 && nbyte > 0)
1439 {
1440 --nbyte;
1441 nbyte -= mb_head_off(str, str + nbyte);
1442 ++nchar;
1443 }
1444 if (nchar < 0)
1445 return -1;
1446 }
1447 return (long)nbyte;
1448}
1449
1450/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001451 * Return the slice "str[first : last]" using character indexes. Composing
1452 * characters are included.
Bram Moolenaar6601b622021-01-13 21:47:15 +01001453 * "exclusive" is TRUE for slice().
Bram Moolenaare7525c52021-01-09 13:20:37 +01001454 * Return NULL when the result is empty.
1455 */
1456 char_u *
Bram Moolenaar6601b622021-01-13 21:47:15 +01001457string_slice(char_u *str, varnumber_T first, varnumber_T last, int exclusive)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001458{
1459 long start_byte, end_byte;
1460 size_t slen;
1461
1462 if (str == NULL)
1463 return NULL;
1464 slen = STRLEN(str);
1465 start_byte = char_idx2byte(str, slen, first);
1466 if (start_byte < 0)
1467 start_byte = 0; // first index very negative: use zero
Bram Moolenaar6601b622021-01-13 21:47:15 +01001468 if ((last == -1 && !exclusive) || last == VARNUM_MAX)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001469 end_byte = (long)slen;
1470 else
1471 {
1472 end_byte = char_idx2byte(str, slen, last);
Bram Moolenaar6601b622021-01-13 21:47:15 +01001473 if (!exclusive && end_byte >= 0 && end_byte < (long)slen)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001474 // end index is inclusive
Bram Moolenaar0289a092021-03-14 18:40:19 +01001475 end_byte += mb_ptr2len(str + end_byte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001476 }
1477
1478 if (start_byte >= (long)slen || end_byte <= start_byte)
1479 return NULL;
1480 return vim_strnsave(str + start_byte, end_byte - start_byte);
1481}
1482
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001483/*
1484 * Get a script variable for ISN_STORESCRIPT and ISN_LOADSCRIPT.
1485 * When "dfunc_idx" is negative don't give an error.
1486 * Returns NULL for an error.
1487 */
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001488 static svar_T *
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001489get_script_svar(scriptref_T *sref, int dfunc_idx)
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001490{
1491 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001492 dfunc_T *dfunc = dfunc_idx < 0 ? NULL
1493 : ((dfunc_T *)def_functions.ga_data) + dfunc_idx;
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001494 svar_T *sv;
1495
1496 if (sref->sref_seq != si->sn_script_seq)
1497 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001498 // The script was reloaded after the function was compiled, the
1499 // script_idx may not be valid.
1500 if (dfunc != NULL)
1501 semsg(_(e_script_variable_invalid_after_reload_in_function_str),
1502 printable_func_name(dfunc->df_ufunc));
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001503 return NULL;
1504 }
1505 sv = ((svar_T *)si->sn_var_vals.ga_data) + sref->sref_idx;
Bram Moolenaar60dc8272021-07-29 22:48:54 +02001506 if (!equal_type(sv->sv_type, sref->sref_type, 0))
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001507 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001508 if (dfunc != NULL)
1509 emsg(_(e_script_variable_type_changed));
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001510 return NULL;
1511 }
1512 return sv;
1513}
1514
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001515/*
Bram Moolenaar20677332021-06-06 17:02:53 +02001516 * Function passed to do_cmdline() for splitting a script joined by NL
1517 * characters.
1518 */
1519 static char_u *
1520get_split_sourceline(
1521 int c UNUSED,
1522 void *cookie,
1523 int indent UNUSED,
1524 getline_opt_T options UNUSED)
1525{
1526 source_cookie_T *sp = (source_cookie_T *)cookie;
1527 char_u *p;
1528 char_u *line;
1529
Bram Moolenaar20677332021-06-06 17:02:53 +02001530 p = vim_strchr(sp->nextline, '\n');
1531 if (p == NULL)
1532 {
1533 line = vim_strsave(sp->nextline);
1534 sp->nextline += STRLEN(sp->nextline);
1535 }
1536 else
1537 {
1538 line = vim_strnsave(sp->nextline, p - sp->nextline);
1539 sp->nextline = p + 1;
1540 }
1541 return line;
1542}
1543
1544/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001545 * Execute a function by "name".
1546 * This can be a builtin function, user function or a funcref.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001547 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001548 */
1549 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001550call_eval_func(
1551 char_u *name,
1552 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001553 ectx_T *ectx,
1554 isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001555{
Bram Moolenaared677f52020-08-12 16:38:10 +02001556 int called_emsg_before = called_emsg;
1557 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001558
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001559 res = call_by_name(name, argcount, ectx, iptr, NULL);
Bram Moolenaared677f52020-08-12 16:38:10 +02001560 if (res == FAIL && called_emsg == called_emsg_before)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001561 {
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001562 dictitem_T *v;
1563
1564 v = find_var(name, NULL, FALSE);
1565 if (v == NULL)
1566 {
Bram Moolenaare1242042021-12-16 20:56:57 +00001567 semsg(_(e_unknown_function_str), name);
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001568 return FAIL;
1569 }
1570 if (v->di_tv.v_type != VAR_PARTIAL && v->di_tv.v_type != VAR_FUNC)
1571 {
Bram Moolenaare1242042021-12-16 20:56:57 +00001572 semsg(_(e_unknown_function_str), name);
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001573 return FAIL;
1574 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001575 return call_partial(&v->di_tv, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001576 }
Bram Moolenaared677f52020-08-12 16:38:10 +02001577 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001578}
1579
1580/*
Bram Moolenaarf112f302020-12-20 17:47:52 +01001581 * When a function reference is used, fill a partial with the information
1582 * needed, especially when it is used as a closure.
1583 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001584 int
Bram Moolenaarf112f302020-12-20 17:47:52 +01001585fill_partial_and_closure(partial_T *pt, ufunc_T *ufunc, ectx_T *ectx)
1586{
1587 pt->pt_func = ufunc;
1588 pt->pt_refcount = 1;
1589
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001590 if (ufunc->uf_flags & FC_CLOSURE)
Bram Moolenaarf112f302020-12-20 17:47:52 +01001591 {
1592 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1593 + ectx->ec_dfunc_idx;
1594
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001595 // The closure may need to find arguments and local variables in the
1596 // current stack.
Bram Moolenaar0186e582021-01-10 18:33:11 +01001597 pt->pt_outer.out_stack = &ectx->ec_stack;
1598 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001599 if (ectx->ec_outer_ref != NULL)
1600 {
1601 // The current context already has a context, link to that one.
1602 pt->pt_outer.out_up = ectx->ec_outer_ref->or_outer;
1603 if (ectx->ec_outer_ref->or_partial != NULL)
1604 {
1605 pt->pt_outer.out_up_partial = ectx->ec_outer_ref->or_partial;
1606 ++pt->pt_outer.out_up_partial->pt_refcount;
1607 }
1608 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01001609
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001610 // If this function returns and the closure is still being used, we
1611 // need to make a copy of the context (arguments and local variables).
1612 // Store a reference to the partial so we can handle that.
Bram Moolenaar35578162021-08-02 19:10:38 +02001613 if (GA_GROW_FAILS(&ectx->ec_funcrefs, 1))
Bram Moolenaarf112f302020-12-20 17:47:52 +01001614 {
1615 vim_free(pt);
1616 return FAIL;
1617 }
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001618 // Extra variable keeps the count of closures created in the current
1619 // function call.
Bram Moolenaarf112f302020-12-20 17:47:52 +01001620 ++(((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx
1621 + STACK_FRAME_SIZE + dfunc->df_varcount)->vval.v_number;
1622
1623 ((partial_T **)ectx->ec_funcrefs.ga_data)
1624 [ectx->ec_funcrefs.ga_len] = pt;
1625 ++pt->pt_refcount;
1626 ++ectx->ec_funcrefs.ga_len;
1627 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001628 ++ufunc->uf_refcount;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001629 return OK;
1630}
1631
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001632/*
1633 * Execute iptr->isn_arg.string as an Ex command.
1634 */
1635 static int
1636exec_command(isn_T *iptr)
1637{
1638 source_cookie_T cookie;
1639
1640 SOURCING_LNUM = iptr->isn_lnum;
1641 // Pass getsourceline to get an error for a missing ":end"
1642 // command.
1643 CLEAR_FIELD(cookie);
1644 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1645 if (do_cmdline(iptr->isn_arg.string,
1646 getsourceline, &cookie,
1647 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED) == FAIL
1648 || did_emsg)
1649 return FAIL;
1650 return OK;
1651}
1652
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001653// used for v_instr of typval of VAR_INSTR
1654struct instr_S {
1655 ectx_T *instr_ectx;
1656 isn_T *instr_instr;
1657};
1658
Bram Moolenaar4c137212021-04-19 16:48:48 +02001659// used for substitute_instr
1660typedef struct subs_expr_S {
1661 ectx_T *subs_ectx;
1662 isn_T *subs_instr;
1663 int subs_status;
1664} subs_expr_T;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001665
1666// Get pointer to item in the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001667#define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001668
1669// Get pointer to item at the bottom of the stack, -1 is the bottom.
1670#undef STACK_TV_BOT
Bram Moolenaar4c137212021-04-19 16:48:48 +02001671#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 +01001672
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001673// Get pointer to a local variable on the stack. Negative for arguments.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001674#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 +01001675
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001676// Set when calling do_debug().
1677static ectx_T *debug_context = NULL;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001678static int debug_var_count;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001679
1680/*
1681 * When debugging lookup "name" and return the typeval.
1682 * When not found return NULL.
1683 */
1684 typval_T *
1685lookup_debug_var(char_u *name)
1686{
1687 int idx;
1688 dfunc_T *dfunc;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001689 ufunc_T *ufunc;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001690 ectx_T *ectx = debug_context;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001691 int varargs_off;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001692
1693 if (ectx == NULL)
1694 return NULL;
1695 dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
1696
1697 // Go through the local variable names, from last to first.
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001698 for (idx = debug_var_count - 1; idx >= 0; --idx)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001699 {
Bram Moolenaare406ff82022-03-10 20:47:43 +00001700 char_u *varname = ((char_u **)dfunc->df_var_names.ga_data)[idx];
1701
1702 // the variable name may be NULL when not available in this block
1703 if (varname != NULL && STRCMP(varname, name) == 0)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001704 return STACK_TV_VAR(idx);
1705 }
1706
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001707 // Go through argument names.
1708 ufunc = dfunc->df_ufunc;
1709 varargs_off = ufunc->uf_va_name == NULL ? 0 : 1;
1710 for (idx = 0; idx < ufunc->uf_args.ga_len; ++idx)
1711 if (STRCMP(((char_u **)(ufunc->uf_args.ga_data))[idx], name) == 0)
1712 return STACK_TV(ectx->ec_frame_idx - ufunc->uf_args.ga_len
1713 - varargs_off + idx);
1714 if (ufunc->uf_va_name != NULL && STRCMP(ufunc->uf_va_name, name) == 0)
1715 return STACK_TV(ectx->ec_frame_idx - 1);
1716
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001717 return NULL;
1718}
1719
Bram Moolenaar26a44842021-09-02 18:49:06 +02001720/*
1721 * Return TRUE if there might be a breakpoint in "ufunc", which is when a
1722 * breakpoint was set in that function or when there is any expression.
1723 */
1724 int
1725may_break_in_function(ufunc_T *ufunc)
1726{
1727 return ufunc->uf_has_breakpoint || debug_has_expr_breakpoint();
1728}
1729
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001730 static void
1731handle_debug(isn_T *iptr, ectx_T *ectx)
1732{
1733 char_u *line;
1734 ufunc_T *ufunc = (((dfunc_T *)def_functions.ga_data)
1735 + ectx->ec_dfunc_idx)->df_ufunc;
1736 isn_T *ni;
1737 int end_lnum = iptr->isn_lnum;
1738 garray_T ga;
1739 int lnum;
1740
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02001741 if (ex_nesting_level > debug_break_level)
1742 {
1743 linenr_T breakpoint;
1744
Bram Moolenaar26a44842021-09-02 18:49:06 +02001745 if (!may_break_in_function(ufunc))
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02001746 return;
1747
1748 // check for the next breakpoint if needed
1749 breakpoint = dbg_find_breakpoint(FALSE, ufunc->uf_name,
Bram Moolenaar8cec9272021-06-23 20:20:53 +02001750 iptr->isn_arg.debug.dbg_break_lnum);
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02001751 if (breakpoint <= 0 || breakpoint > iptr->isn_lnum)
1752 return;
1753 }
1754
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001755 SOURCING_LNUM = iptr->isn_lnum;
1756 debug_context = ectx;
Bram Moolenaar8cec9272021-06-23 20:20:53 +02001757 debug_var_count = iptr->isn_arg.debug.dbg_var_names_len;
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001758
1759 for (ni = iptr + 1; ni->isn_type != ISN_FINISH; ++ni)
1760 if (ni->isn_type == ISN_DEBUG
1761 || ni->isn_type == ISN_RETURN
1762 || ni->isn_type == ISN_RETURN_VOID)
1763 {
Bram Moolenaar112bed02021-11-23 22:16:34 +00001764 end_lnum = ni->isn_lnum + (ni->isn_type == ISN_DEBUG ? 0 : 1);
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001765 break;
1766 }
1767
1768 if (end_lnum > iptr->isn_lnum)
1769 {
1770 ga_init2(&ga, sizeof(char_u *), 10);
Bram Moolenaar310091d2021-12-23 21:14:37 +00001771 for (lnum = iptr->isn_lnum; lnum < end_lnum
1772 && lnum <= ufunc->uf_lines.ga_len; ++lnum)
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001773 {
Bram Moolenaar303215d2021-07-07 20:10:43 +02001774 char_u *p = ((char_u **)ufunc->uf_lines.ga_data)[lnum - 1];
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001775
Bram Moolenaar303215d2021-07-07 20:10:43 +02001776 if (p == NULL)
1777 continue; // left over from continuation line
1778 p = skipwhite(p);
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001779 if (*p == '#')
1780 break;
Bram Moolenaar35578162021-08-02 19:10:38 +02001781 if (GA_GROW_OK(&ga, 1))
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001782 ((char_u **)(ga.ga_data))[ga.ga_len++] = p;
1783 if (STRNCMP(p, "def ", 4) == 0)
1784 break;
1785 }
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001786 line = ga_concat_strings(&ga, " ");
1787 vim_free(ga.ga_data);
1788 }
1789 else
1790 line = ((char_u **)ufunc->uf_lines.ga_data)[iptr->isn_lnum - 1];
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001791
Dominique Pelle6e969552021-06-17 13:53:41 +02001792 do_debug(line == NULL ? (char_u *)"[empty]" : line);
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001793 debug_context = NULL;
1794
1795 if (end_lnum > iptr->isn_lnum)
1796 vim_free(line);
1797}
1798
Bram Moolenaar4c137212021-04-19 16:48:48 +02001799/*
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00001800 * Store a value in a list, dict or blob variable.
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001801 * Returns OK, FAIL or NOTDONE (uncatchable error).
1802 */
1803 static int
1804execute_storeindex(isn_T *iptr, ectx_T *ectx)
1805{
1806 vartype_T dest_type = iptr->isn_arg.vartype;
1807 typval_T *tv;
1808 typval_T *tv_idx = STACK_TV_BOT(-2);
1809 typval_T *tv_dest = STACK_TV_BOT(-1);
1810 int status = OK;
1811
1812 // Stack contains:
1813 // -3 value to be stored
1814 // -2 index
1815 // -1 dict or list
1816 tv = STACK_TV_BOT(-3);
1817 SOURCING_LNUM = iptr->isn_lnum;
1818 if (dest_type == VAR_ANY)
1819 {
1820 dest_type = tv_dest->v_type;
1821 if (dest_type == VAR_DICT)
1822 status = do_2string(tv_idx, TRUE, FALSE);
1823 else if (dest_type == VAR_LIST && tv_idx->v_type != VAR_NUMBER)
1824 {
1825 emsg(_(e_number_expected));
1826 status = FAIL;
1827 }
1828 }
Bram Moolenaare08be092022-02-17 13:08:26 +00001829
1830 if (status == OK)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001831 {
Bram Moolenaare08be092022-02-17 13:08:26 +00001832 if (dest_type == VAR_LIST)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001833 {
Bram Moolenaare08be092022-02-17 13:08:26 +00001834 long lidx = (long)tv_idx->vval.v_number;
1835 list_T *list = tv_dest->vval.v_list;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001836
Bram Moolenaare08be092022-02-17 13:08:26 +00001837 if (list == NULL)
1838 {
1839 emsg(_(e_list_not_set));
1840 return FAIL;
1841 }
1842 if (lidx < 0 && list->lv_len + lidx >= 0)
1843 // negative index is relative to the end
1844 lidx = list->lv_len + lidx;
1845 if (lidx < 0 || lidx > list->lv_len)
1846 {
1847 semsg(_(e_list_index_out_of_range_nr), lidx);
1848 return FAIL;
1849 }
1850 if (lidx < list->lv_len)
1851 {
1852 listitem_T *li = list_find(list, lidx);
1853
1854 if (error_if_locked(li->li_tv.v_lock,
Bram Moolenaar70c43d82022-01-26 21:01:15 +00001855 e_cannot_change_locked_list_item))
Bram Moolenaare08be092022-02-17 13:08:26 +00001856 return FAIL;
1857 // overwrite existing list item
1858 clear_tv(&li->li_tv);
1859 li->li_tv = *tv;
1860 }
1861 else
1862 {
1863 if (error_if_locked(list->lv_lock, e_cannot_change_locked_list))
1864 return FAIL;
1865 // append to list, only fails when out of memory
1866 if (list_append_tv(list, tv) == FAIL)
1867 return NOTDONE;
1868 clear_tv(tv);
1869 }
1870 }
1871 else if (dest_type == VAR_DICT)
1872 {
1873 char_u *key = tv_idx->vval.v_string;
1874 dict_T *dict = tv_dest->vval.v_dict;
1875 dictitem_T *di;
1876
1877 SOURCING_LNUM = iptr->isn_lnum;
1878 if (dict == NULL)
1879 {
1880 emsg(_(e_dictionary_not_set));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001881 return FAIL;
Bram Moolenaare08be092022-02-17 13:08:26 +00001882 }
1883 if (key == NULL)
1884 key = (char_u *)"";
1885 di = dict_find(dict, key, -1);
1886 if (di != NULL)
1887 {
1888 if (error_if_locked(di->di_tv.v_lock,
1889 e_cannot_change_dict_item))
1890 return FAIL;
1891 // overwrite existing value
1892 clear_tv(&di->di_tv);
1893 di->di_tv = *tv;
1894 }
1895 else
1896 {
1897 if (error_if_locked(dict->dv_lock, e_cannot_change_dict))
1898 return FAIL;
1899 // add to dict, only fails when out of memory
1900 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
1901 return NOTDONE;
1902 clear_tv(tv);
1903 }
1904 }
1905 else if (dest_type == VAR_BLOB)
1906 {
1907 long lidx = (long)tv_idx->vval.v_number;
1908 blob_T *blob = tv_dest->vval.v_blob;
1909 varnumber_T nr;
1910 int error = FALSE;
1911 int len;
1912
1913 if (blob == NULL)
1914 {
1915 emsg(_(e_blob_not_set));
1916 return FAIL;
1917 }
1918 len = blob_len(blob);
1919 if (lidx < 0 && len + lidx >= 0)
1920 // negative index is relative to the end
1921 lidx = len + lidx;
1922
1923 // Can add one byte at the end.
1924 if (lidx < 0 || lidx > len)
1925 {
1926 semsg(_(e_blob_index_out_of_range_nr), lidx);
1927 return FAIL;
1928 }
1929 if (value_check_lock(blob->bv_lock, (char_u *)"blob", FALSE))
1930 return FAIL;
1931 nr = tv_get_number_chk(tv, &error);
1932 if (error)
1933 return FAIL;
1934 blob_set_append(blob, lidx, nr);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001935 }
1936 else
1937 {
Bram Moolenaare08be092022-02-17 13:08:26 +00001938 status = FAIL;
1939 semsg(_(e_cannot_index_str), vartype_name(dest_type));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001940 }
1941 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001942
1943 clear_tv(tv_idx);
1944 clear_tv(tv_dest);
1945 ectx->ec_stack.ga_len -= 3;
1946 if (status == FAIL)
1947 {
1948 clear_tv(tv);
1949 return FAIL;
1950 }
1951 return OK;
1952}
1953
1954/*
Bram Moolenaarea5c8982022-02-17 14:42:02 +00001955 * Store a value in a list or blob range.
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001956 */
1957 static int
1958execute_storerange(isn_T *iptr, ectx_T *ectx)
1959{
1960 typval_T *tv;
1961 typval_T *tv_idx1 = STACK_TV_BOT(-3);
1962 typval_T *tv_idx2 = STACK_TV_BOT(-2);
1963 typval_T *tv_dest = STACK_TV_BOT(-1);
1964 int status = OK;
1965
1966 // Stack contains:
1967 // -4 value to be stored
1968 // -3 first index or "none"
1969 // -2 second index or "none"
1970 // -1 destination list or blob
1971 tv = STACK_TV_BOT(-4);
Bram Moolenaarea5c8982022-02-17 14:42:02 +00001972 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001973 if (tv_dest->v_type == VAR_LIST)
1974 {
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00001975 long n1;
1976 long n2;
1977 listitem_T *li1;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001978
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00001979 n1 = (long)tv_get_number_chk(tv_idx1, NULL);
1980 if (tv_idx2->v_type == VAR_SPECIAL
1981 && tv_idx2->vval.v_number == VVAL_NONE)
1982 n2 = list_len(tv_dest->vval.v_list) - 1;
1983 else
1984 n2 = (long)tv_get_number_chk(tv_idx2, NULL);
1985
1986 li1 = check_range_index_one(tv_dest->vval.v_list, &n1, FALSE);
1987 if (li1 == NULL)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001988 status = FAIL;
1989 else
1990 {
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00001991 status = check_range_index_two(tv_dest->vval.v_list,
1992 &n1, li1, &n2, FALSE);
1993 if (status != FAIL)
1994 status = list_assign_range(
1995 tv_dest->vval.v_list,
1996 tv->vval.v_list,
1997 n1,
1998 n2,
1999 tv_idx2->v_type == VAR_SPECIAL,
2000 (char_u *)"=",
2001 (char_u *)"[unknown]");
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002002 }
2003 }
2004 else if (tv_dest->v_type == VAR_BLOB)
2005 {
2006 varnumber_T n1;
2007 varnumber_T n2;
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002008 long bloblen;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002009
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002010 n1 = tv_get_number_chk(tv_idx1, NULL);
2011 if (tv_idx2->v_type == VAR_SPECIAL
2012 && tv_idx2->vval.v_number == VVAL_NONE)
2013 n2 = blob_len(tv_dest->vval.v_blob) - 1;
2014 else
2015 n2 = tv_get_number_chk(tv_idx2, NULL);
2016 bloblen = blob_len(tv_dest->vval.v_blob);
2017
2018 if (check_blob_index(bloblen, n1, FALSE) == FAIL
2019 || check_blob_range(bloblen, n1, n2, FALSE) == FAIL)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002020 status = FAIL;
2021 else
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002022 status = blob_set_range(tv_dest->vval.v_blob, n1, n2, tv);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002023 }
2024 else
2025 {
2026 status = FAIL;
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002027 emsg(_(e_list_or_blob_required));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002028 }
2029
2030 clear_tv(tv_idx1);
2031 clear_tv(tv_idx2);
2032 clear_tv(tv_dest);
2033 ectx->ec_stack.ga_len -= 4;
2034 clear_tv(tv);
2035
2036 return status;
2037}
2038
2039/*
2040 * Unlet item in list or dict variable.
2041 */
2042 static int
2043execute_unletindex(isn_T *iptr, ectx_T *ectx)
2044{
2045 typval_T *tv_idx = STACK_TV_BOT(-2);
2046 typval_T *tv_dest = STACK_TV_BOT(-1);
2047 int status = OK;
2048
2049 // Stack contains:
2050 // -2 index
2051 // -1 dict or list
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002052 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002053 if (tv_dest->v_type == VAR_DICT)
2054 {
2055 // unlet a dict item, index must be a string
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002056 if (tv_idx->v_type != VAR_STRING && tv_idx->v_type != VAR_NUMBER)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002057 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002058 semsg(_(e_expected_str_but_got_str),
2059 vartype_name(VAR_STRING),
2060 vartype_name(tv_idx->v_type));
2061 status = FAIL;
2062 }
2063 else
2064 {
2065 dict_T *d = tv_dest->vval.v_dict;
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002066 char_u *key;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002067 dictitem_T *di = NULL;
2068
2069 if (d != NULL && value_check_lock(
2070 d->dv_lock, NULL, FALSE))
2071 status = FAIL;
2072 else
2073 {
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002074 if (tv_idx->v_type == VAR_STRING)
2075 {
2076 key = tv_idx->vval.v_string;
2077 if (key == NULL)
2078 key = (char_u *)"";
2079 }
2080 else
2081 {
2082 key = tv_get_string(tv_idx);
2083 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002084 if (d != NULL)
2085 di = dict_find(d, key, (int)STRLEN(key));
2086 if (di == NULL)
2087 {
2088 // NULL dict is equivalent to empty dict
2089 semsg(_(e_key_not_present_in_dictionary),
2090 key);
2091 status = FAIL;
2092 }
2093 else if (var_check_fixed(di->di_flags,
2094 NULL, FALSE)
2095 || var_check_ro(di->di_flags,
2096 NULL, FALSE))
2097 status = FAIL;
2098 else
2099 dictitem_remove(d, di);
2100 }
2101 }
2102 }
2103 else if (tv_dest->v_type == VAR_LIST)
2104 {
2105 // unlet a List item, index must be a number
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002106 if (check_for_number(tv_idx) == FAIL)
2107 {
2108 status = FAIL;
2109 }
2110 else
2111 {
2112 list_T *l = tv_dest->vval.v_list;
2113 long n = (long)tv_idx->vval.v_number;
2114
2115 if (l != NULL && value_check_lock(
2116 l->lv_lock, NULL, FALSE))
2117 status = FAIL;
2118 else
2119 {
2120 listitem_T *li = list_find(l, n);
2121
2122 if (li == NULL)
2123 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002124 semsg(_(e_list_index_out_of_range_nr), n);
2125 status = FAIL;
2126 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002127 else
2128 listitem_remove(l, li);
2129 }
2130 }
2131 }
2132 else
2133 {
2134 status = FAIL;
2135 semsg(_(e_cannot_index_str),
2136 vartype_name(tv_dest->v_type));
2137 }
2138
2139 clear_tv(tv_idx);
2140 clear_tv(tv_dest);
2141 ectx->ec_stack.ga_len -= 2;
2142
2143 return status;
2144}
2145
2146/*
2147 * Unlet a range of items in a list variable.
2148 */
2149 static int
2150execute_unletrange(isn_T *iptr, ectx_T *ectx)
2151{
2152 // Stack contains:
2153 // -3 index1
2154 // -2 index2
2155 // -1 dict or list
2156 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2157 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2158 typval_T *tv_dest = STACK_TV_BOT(-1);
2159 int status = OK;
2160
2161 if (tv_dest->v_type == VAR_LIST)
2162 {
2163 // indexes must be a number
2164 SOURCING_LNUM = iptr->isn_lnum;
2165 if (check_for_number(tv_idx1) == FAIL
2166 || (tv_idx2->v_type != VAR_SPECIAL
2167 && check_for_number(tv_idx2) == FAIL))
2168 {
2169 status = FAIL;
2170 }
2171 else
2172 {
2173 list_T *l = tv_dest->vval.v_list;
2174 long n1 = (long)tv_idx1->vval.v_number;
2175 long n2 = tv_idx2->v_type == VAR_SPECIAL
2176 ? 0 : (long)tv_idx2->vval.v_number;
2177 listitem_T *li;
2178
2179 li = list_find_index(l, &n1);
2180 if (li == NULL)
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002181 {
2182 semsg(_(e_list_index_out_of_range_nr),
2183 (long)tv_idx1->vval.v_number);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002184 status = FAIL;
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002185 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002186 else
2187 {
2188 if (n1 < 0)
2189 n1 = list_idx_of_item(l, li);
2190 if (n2 < 0)
2191 {
2192 listitem_T *li2 = list_find(l, n2);
2193
2194 if (li2 == NULL)
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002195 {
2196 semsg(_(e_list_index_out_of_range_nr), n2);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002197 status = FAIL;
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002198 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002199 else
2200 n2 = list_idx_of_item(l, li2);
2201 }
2202 if (status != FAIL
2203 && tv_idx2->v_type != VAR_SPECIAL
2204 && n2 < n1)
2205 {
2206 semsg(_(e_list_index_out_of_range_nr), n2);
2207 status = FAIL;
2208 }
Bram Moolenaar6b8c7ba2022-03-20 17:46:06 +00002209 if (status != FAIL)
2210 list_unlet_range(l, li, n1,
2211 tv_idx2->v_type != VAR_SPECIAL, n2);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002212 }
2213 }
2214 }
2215 else
2216 {
2217 status = FAIL;
2218 SOURCING_LNUM = iptr->isn_lnum;
2219 semsg(_(e_cannot_index_str),
2220 vartype_name(tv_dest->v_type));
2221 }
2222
2223 clear_tv(tv_idx1);
2224 clear_tv(tv_idx2);
2225 clear_tv(tv_dest);
2226 ectx->ec_stack.ga_len -= 3;
2227
2228 return status;
2229}
2230
2231/*
2232 * Top of a for loop.
2233 */
2234 static int
2235execute_for(isn_T *iptr, ectx_T *ectx)
2236{
2237 typval_T *tv;
2238 typval_T *ltv = STACK_TV_BOT(-1);
2239 typval_T *idxtv =
2240 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
2241
2242 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
2243 return FAIL;
2244 if (ltv->v_type == VAR_LIST)
2245 {
2246 list_T *list = ltv->vval.v_list;
2247
2248 // push the next item from the list
2249 ++idxtv->vval.v_number;
2250 if (list == NULL
2251 || idxtv->vval.v_number >= list->lv_len)
2252 {
2253 // past the end of the list, jump to "endfor"
2254 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
2255 may_restore_cmdmod(&ectx->ec_funclocal);
2256 }
2257 else if (list->lv_first == &range_list_item)
2258 {
2259 // non-materialized range() list
2260 tv = STACK_TV_BOT(0);
2261 tv->v_type = VAR_NUMBER;
2262 tv->v_lock = 0;
2263 tv->vval.v_number = list_find_nr(
2264 list, idxtv->vval.v_number, NULL);
2265 ++ectx->ec_stack.ga_len;
2266 }
2267 else
2268 {
2269 listitem_T *li = list_find(list,
2270 idxtv->vval.v_number);
2271
2272 copy_tv(&li->li_tv, STACK_TV_BOT(0));
2273 ++ectx->ec_stack.ga_len;
2274 }
2275 }
2276 else if (ltv->v_type == VAR_STRING)
2277 {
2278 char_u *str = ltv->vval.v_string;
2279
2280 // The index is for the last byte of the previous
2281 // character.
2282 ++idxtv->vval.v_number;
2283 if (str == NULL || str[idxtv->vval.v_number] == NUL)
2284 {
2285 // past the end of the string, jump to "endfor"
2286 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
2287 may_restore_cmdmod(&ectx->ec_funclocal);
2288 }
2289 else
2290 {
2291 int clen = mb_ptr2len(str + idxtv->vval.v_number);
2292
2293 // Push the next character from the string.
2294 tv = STACK_TV_BOT(0);
2295 tv->v_type = VAR_STRING;
2296 tv->vval.v_string = vim_strnsave(
2297 str + idxtv->vval.v_number, clen);
2298 ++ectx->ec_stack.ga_len;
2299 idxtv->vval.v_number += clen - 1;
2300 }
2301 }
2302 else if (ltv->v_type == VAR_BLOB)
2303 {
2304 blob_T *blob = ltv->vval.v_blob;
2305
2306 // When we get here the first time make a copy of the
2307 // blob, so that the iteration still works when it is
2308 // changed.
2309 if (idxtv->vval.v_number == -1 && blob != NULL)
2310 {
2311 blob_copy(blob, ltv);
2312 blob_unref(blob);
2313 blob = ltv->vval.v_blob;
2314 }
2315
2316 // The index is for the previous byte.
2317 ++idxtv->vval.v_number;
2318 if (blob == NULL
2319 || idxtv->vval.v_number >= blob_len(blob))
2320 {
2321 // past the end of the blob, jump to "endfor"
2322 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
2323 may_restore_cmdmod(&ectx->ec_funclocal);
2324 }
2325 else
2326 {
2327 // Push the next byte from the blob.
2328 tv = STACK_TV_BOT(0);
2329 tv->v_type = VAR_NUMBER;
2330 tv->vval.v_number = blob_get(blob,
2331 idxtv->vval.v_number);
2332 ++ectx->ec_stack.ga_len;
2333 }
2334 }
2335 else
2336 {
2337 semsg(_(e_for_loop_on_str_not_supported),
2338 vartype_name(ltv->v_type));
2339 return FAIL;
2340 }
2341 return OK;
2342}
2343
2344/*
Bram Moolenaar06b77222022-01-25 15:51:56 +00002345 * Load instruction for w:/b:/g:/t: variable.
2346 * "isn_type" is used instead of "iptr->isn_type".
2347 */
2348 static int
2349load_namespace_var(ectx_T *ectx, isntype_T isn_type, isn_T *iptr)
2350{
2351 dictitem_T *di = NULL;
2352 hashtab_T *ht = NULL;
2353 char namespace;
2354
2355 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
2356 return NOTDONE;
2357 switch (isn_type)
2358 {
2359 case ISN_LOADG:
2360 ht = get_globvar_ht();
2361 namespace = 'g';
2362 break;
2363 case ISN_LOADB:
2364 ht = &curbuf->b_vars->dv_hashtab;
2365 namespace = 'b';
2366 break;
2367 case ISN_LOADW:
2368 ht = &curwin->w_vars->dv_hashtab;
2369 namespace = 'w';
2370 break;
2371 case ISN_LOADT:
2372 ht = &curtab->tp_vars->dv_hashtab;
2373 namespace = 't';
2374 break;
2375 default: // Cannot reach here
2376 return NOTDONE;
2377 }
2378 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
2379
Bram Moolenaar06b77222022-01-25 15:51:56 +00002380 if (di == NULL)
2381 {
Bram Moolenaarfe732552022-02-22 19:39:13 +00002382 if (isn_type == ISN_LOADG)
2383 {
2384 ufunc_T *ufunc = find_func(iptr->isn_arg.string, TRUE);
2385
2386 // g:Something could be a function
2387 if (ufunc != NULL)
2388 {
2389 typval_T *tv = STACK_TV_BOT(0);
2390
2391 ++ectx->ec_stack.ga_len;
2392 tv->v_type = VAR_FUNC;
2393 tv->vval.v_string = alloc(STRLEN(iptr->isn_arg.string) + 3);
2394 if (tv->vval.v_string == NULL)
2395 return FAIL;
2396 STRCPY(tv->vval.v_string, "g:");
2397 STRCPY(tv->vval.v_string + 2, iptr->isn_arg.string);
2398 return OK;
2399 }
2400 }
Bram Moolenaar06b77222022-01-25 15:51:56 +00002401 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarfe732552022-02-22 19:39:13 +00002402 if (vim_strchr(iptr->isn_arg.string, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar06b77222022-01-25 15:51:56 +00002403 // no check if the item exists in the script but
2404 // isn't exported, it is too complicated
Bram Moolenaarfe732552022-02-22 19:39:13 +00002405 semsg(_(e_item_not_found_in_script_str), iptr->isn_arg.string);
Bram Moolenaar06b77222022-01-25 15:51:56 +00002406 else
2407 semsg(_(e_undefined_variable_char_str),
Bram Moolenaarfe732552022-02-22 19:39:13 +00002408 namespace, iptr->isn_arg.string);
Bram Moolenaar06b77222022-01-25 15:51:56 +00002409 return FAIL;
2410 }
2411 else
2412 {
2413 copy_tv(&di->di_tv, STACK_TV_BOT(0));
2414 ++ectx->ec_stack.ga_len;
2415 }
2416 return OK;
2417}
2418
2419/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02002420 * Execute instructions in execution context "ectx".
2421 * Return OK or FAIL;
2422 */
2423 static int
2424exec_instructions(ectx_T *ectx)
2425{
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002426 int ret = FAIL;
2427 int save_trylevel_at_start = ectx->ec_trylevel_at_start;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02002428 int dict_stack_len_at_start = dict_stack.ga_len;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01002429
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02002430 // Start execution at the first instruction.
Bram Moolenaar4c137212021-04-19 16:48:48 +02002431 ectx->ec_iidx = 0;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01002432
Bram Moolenaarff652882021-05-16 15:24:49 +02002433 // Only catch exceptions in this instruction list.
2434 ectx->ec_trylevel_at_start = trylevel;
2435
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002436 for (;;)
2437 {
Bram Moolenaara7490192021-07-22 12:26:14 +02002438 static int breakcheck_count = 0; // using "static" makes it faster
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002439 isn_T *iptr;
Bram Moolenaara7490192021-07-22 12:26:14 +02002440 typval_T *tv;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002441
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002442 if (unlikely(++breakcheck_count >= 100))
Bram Moolenaar270d0382020-05-15 21:42:53 +02002443 {
2444 line_breakcheck();
2445 breakcheck_count = 0;
2446 }
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002447 if (unlikely(got_int))
Bram Moolenaar20431c92020-03-20 18:39:46 +01002448 {
2449 // Turn CTRL-C into an exception.
2450 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +01002451 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002452 goto theend;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002453 did_throw = TRUE;
2454 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002455
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002456 if (unlikely(did_emsg && msg_list != NULL && *msg_list != NULL))
Bram Moolenaara26b9702020-04-18 19:53:28 +02002457 {
2458 // Turn an error message into an exception.
2459 did_emsg = FALSE;
2460 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002461 goto theend;
Bram Moolenaara26b9702020-04-18 19:53:28 +02002462 did_throw = TRUE;
2463 *msg_list = NULL;
2464 }
2465
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002466 if (unlikely(did_throw))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002467 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002468 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002469 trycmd_T *trycmd = NULL;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002470 int index = trystack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002471
2472 // An exception jumps to the first catch, finally, or returns from
2473 // the current function.
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002474 while (index > 0)
2475 {
2476 trycmd = ((trycmd_T *)trystack->ga_data) + index - 1;
Bram Moolenaar834193a2021-06-30 20:39:15 +02002477 if (!trycmd->tcd_in_catch || trycmd->tcd_finally_idx != 0)
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002478 break;
2479 // In the catch and finally block of this try we have to go up
2480 // one level.
2481 --index;
2482 trycmd = NULL;
2483 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02002484 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002485 {
Bram Moolenaar834193a2021-06-30 20:39:15 +02002486 if (trycmd->tcd_in_catch)
2487 {
2488 // exception inside ":catch", jump to ":finally" once
2489 ectx->ec_iidx = trycmd->tcd_finally_idx;
2490 trycmd->tcd_finally_idx = 0;
2491 }
2492 else
2493 // jump to first ":catch"
2494 ectx->ec_iidx = trycmd->tcd_catch_idx;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002495 trycmd->tcd_in_catch = TRUE;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002496 did_throw = FALSE; // don't come back here until :endtry
2497 trycmd->tcd_did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002498 }
2499 else
2500 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02002501 // Not inside try or need to return from current functions.
2502 // Push a dummy return value.
Bram Moolenaar35578162021-08-02 19:10:38 +02002503 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002504 goto theend;
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02002505 tv = STACK_TV_BOT(0);
2506 tv->v_type = VAR_NUMBER;
2507 tv->vval.v_number = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002508 ++ectx->ec_stack.ga_len;
2509 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002510 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02002511 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +01002512 need_rethrow = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002513 if (handle_closure_in_use(ectx, FALSE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002514 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002515 goto done;
2516 }
2517
Bram Moolenaar4c137212021-04-19 16:48:48 +02002518 if (func_return(ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002519 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002520 }
2521 continue;
2522 }
2523
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002524 /*
2525 * Big switch on the instruction. Most compilers will be turning this
2526 * into an efficient lookup table, since the "case" values are an enum
2527 * with sequential numbers. It may look ugly, but it should be the
2528 * most efficient way.
2529 */
Bram Moolenaar4c137212021-04-19 16:48:48 +02002530 iptr = &ectx->ec_instr[ectx->ec_iidx++];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002531 switch (iptr->isn_type)
2532 {
2533 // execute Ex command line
2534 case ISN_EXEC:
Bram Moolenaaraacc9662021-08-13 19:40:51 +02002535 if (exec_command(iptr) == FAIL)
2536 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002537 break;
2538
Bram Moolenaar20677332021-06-06 17:02:53 +02002539 // execute Ex command line split at NL characters.
2540 case ISN_EXEC_SPLIT:
2541 {
2542 source_cookie_T cookie;
Bram Moolenaar518df272021-06-06 17:34:13 +02002543 char_u *line;
Bram Moolenaar20677332021-06-06 17:02:53 +02002544
2545 SOURCING_LNUM = iptr->isn_lnum;
2546 CLEAR_FIELD(cookie);
2547 cookie.sourcing_lnum = iptr->isn_lnum - 1;
2548 cookie.nextline = iptr->isn_arg.string;
Bram Moolenaar518df272021-06-06 17:34:13 +02002549 line = get_split_sourceline(0, &cookie, 0, 0);
2550 if (do_cmdline(line,
Bram Moolenaar20677332021-06-06 17:02:53 +02002551 get_split_sourceline, &cookie,
2552 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED)
2553 == FAIL
2554 || did_emsg)
Bram Moolenaar518df272021-06-06 17:34:13 +02002555 {
2556 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02002557 goto on_error;
Bram Moolenaar518df272021-06-06 17:34:13 +02002558 }
2559 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02002560 }
2561 break;
2562
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002563 // execute Ex command line that is only a range
2564 case ISN_EXECRANGE:
2565 {
2566 exarg_T ea;
2567 char *error = NULL;
2568
2569 CLEAR_FIELD(ea);
2570 ea.cmdidx = CMD_SIZE;
2571 ea.addr_type = ADDR_LINES;
2572 ea.cmd = iptr->isn_arg.string;
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00002573 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002574 parse_cmd_address(&ea, &error, FALSE);
Bram Moolenaar01a4dcb2021-12-04 13:15:10 +00002575 if (ea.cmd == NULL)
2576 goto on_error;
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00002577 // error is always NULL when using ADDR_LINES
2578 error = ex_range_without_command(&ea);
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002579 if (error != NULL)
2580 {
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002581 emsg(error);
2582 goto on_error;
2583 }
2584 }
2585 break;
2586
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02002587 // Evaluate an expression with legacy syntax, push it onto the
2588 // stack.
2589 case ISN_LEGACY_EVAL:
2590 {
2591 char_u *arg = iptr->isn_arg.string;
2592 int res;
2593 int save_flags = cmdmod.cmod_flags;
2594
Bram Moolenaar35578162021-08-02 19:10:38 +02002595 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002596 goto theend;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02002597 tv = STACK_TV_BOT(0);
2598 init_tv(tv);
2599 cmdmod.cmod_flags |= CMOD_LEGACY;
2600 res = eval0(arg, tv, NULL, &EVALARG_EVALUATE);
2601 cmdmod.cmod_flags = save_flags;
2602 if (res == FAIL)
2603 goto on_error;
2604 ++ectx->ec_stack.ga_len;
2605 }
2606 break;
2607
Bram Moolenaarf18332f2021-05-07 17:55:55 +02002608 // push typeval VAR_INSTR with instructions to be executed
2609 case ISN_INSTR:
2610 {
Bram Moolenaar35578162021-08-02 19:10:38 +02002611 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002612 goto theend;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02002613 tv = STACK_TV_BOT(0);
2614 tv->vval.v_instr = ALLOC_ONE(instr_T);
2615 if (tv->vval.v_instr == NULL)
2616 goto on_error;
2617 ++ectx->ec_stack.ga_len;
2618
2619 tv->v_type = VAR_INSTR;
2620 tv->vval.v_instr->instr_ectx = ectx;
2621 tv->vval.v_instr->instr_instr = iptr->isn_arg.instr;
2622 }
2623 break;
2624
Bram Moolenaar4c137212021-04-19 16:48:48 +02002625 // execute :substitute with an expression
2626 case ISN_SUBSTITUTE:
2627 {
2628 subs_T *subs = &iptr->isn_arg.subs;
2629 source_cookie_T cookie;
2630 struct subs_expr_S *save_instr = substitute_instr;
2631 struct subs_expr_S subs_instr;
2632 int res;
2633
2634 subs_instr.subs_ectx = ectx;
2635 subs_instr.subs_instr = subs->subs_instr;
2636 subs_instr.subs_status = OK;
2637 substitute_instr = &subs_instr;
2638
2639 SOURCING_LNUM = iptr->isn_lnum;
2640 // This is very much like ISN_EXEC
2641 CLEAR_FIELD(cookie);
2642 cookie.sourcing_lnum = iptr->isn_lnum - 1;
2643 res = do_cmdline(subs->subs_cmd,
2644 getsourceline, &cookie,
2645 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
2646 substitute_instr = save_instr;
2647
2648 if (res == FAIL || did_emsg
2649 || subs_instr.subs_status == FAIL)
2650 goto on_error;
2651 }
2652 break;
2653
2654 case ISN_FINISH:
2655 goto done;
2656
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02002657 case ISN_REDIRSTART:
2658 // create a dummy entry for var_redir_str()
2659 if (alloc_redir_lval() == FAIL)
2660 goto on_error;
2661
2662 // The output is stored in growarray "redir_ga" until
2663 // redirection ends.
2664 init_redir_ga();
2665 redir_vname = 1;
2666 break;
2667
2668 case ISN_REDIREND:
2669 {
2670 char_u *res = get_clear_redir_ga();
2671
2672 // End redirection, put redirected text on the stack.
2673 clear_redir_lval();
2674 redir_vname = 0;
2675
Bram Moolenaar35578162021-08-02 19:10:38 +02002676 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02002677 {
2678 vim_free(res);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002679 goto theend;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02002680 }
2681 tv = STACK_TV_BOT(0);
2682 tv->v_type = VAR_STRING;
2683 tv->vval.v_string = res;
2684 ++ectx->ec_stack.ga_len;
2685 }
2686 break;
2687
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002688 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02002689#ifdef FEAT_QUICKFIX
Bram Moolenaar397a87a2022-03-20 21:14:15 +00002690 force_abort = TRUE;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002691 if (trigger_cexpr_autocmd(iptr->isn_arg.number) == FAIL)
2692 goto on_error;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00002693 force_abort = FALSE;
Bram Moolenaarb7c97812021-05-05 22:51:39 +02002694#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002695 break;
2696
2697 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02002698#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002699 {
2700 exarg_T ea;
2701 int res;
2702
2703 CLEAR_FIELD(ea);
2704 ea.cmdidx = iptr->isn_arg.cexpr.cexpr_ref->cer_cmdidx;
2705 ea.forceit = iptr->isn_arg.cexpr.cexpr_ref->cer_forceit;
2706 ea.cmdlinep = &iptr->isn_arg.cexpr.cexpr_ref->cer_cmdline;
2707 --ectx->ec_stack.ga_len;
2708 tv = STACK_TV_BOT(0);
2709 res = cexpr_core(&ea, tv);
2710 clear_tv(tv);
2711 if (res == FAIL)
2712 goto on_error;
2713 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02002714#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002715 break;
2716
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002717 // execute Ex command from pieces on the stack
2718 case ISN_EXECCONCAT:
2719 {
2720 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +02002721 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002722 int pass;
2723 int i;
2724 char_u *cmd = NULL;
2725 char_u *str;
2726
2727 for (pass = 1; pass <= 2; ++pass)
2728 {
2729 for (i = 0; i < count; ++i)
2730 {
2731 tv = STACK_TV_BOT(i - count);
2732 str = tv->vval.v_string;
2733 if (str != NULL && *str != NUL)
2734 {
2735 if (pass == 2)
2736 STRCPY(cmd + len, str);
2737 len += STRLEN(str);
2738 }
2739 if (pass == 2)
2740 clear_tv(tv);
2741 }
2742 if (pass == 1)
2743 {
2744 cmd = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002745 if (unlikely(cmd == NULL))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002746 goto theend;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002747 len = 0;
2748 }
2749 }
2750
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02002751 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002752 do_cmdline_cmd(cmd);
2753 vim_free(cmd);
2754 }
2755 break;
2756
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002757 // execute :echo {string} ...
2758 case ISN_ECHO:
2759 {
2760 int count = iptr->isn_arg.echo.echo_count;
2761 int atstart = TRUE;
2762 int needclr = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002763 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002764
2765 for (idx = 0; idx < count; ++idx)
2766 {
2767 tv = STACK_TV_BOT(idx - count);
2768 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
2769 &atstart, &needclr);
2770 clear_tv(tv);
2771 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01002772 if (needclr)
2773 msg_clr_eos();
Bram Moolenaar4c137212021-04-19 16:48:48 +02002774 ectx->ec_stack.ga_len -= count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002775 }
2776 break;
2777
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002778 // :execute {string} ...
2779 // :echomsg {string} ...
Bram Moolenaar7de62622021-08-07 15:05:47 +02002780 // :echoconsole {string} ...
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002781 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01002782 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002783 case ISN_ECHOMSG:
Bram Moolenaar7de62622021-08-07 15:05:47 +02002784 case ISN_ECHOCONSOLE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002785 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01002786 {
2787 int count = iptr->isn_arg.number;
2788 garray_T ga;
2789 char_u buf[NUMBUFLEN];
2790 char_u *p;
2791 int len;
2792 int failed = FALSE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002793 int idx;
Bram Moolenaarad39c092020-02-26 18:23:43 +01002794
2795 ga_init2(&ga, 1, 80);
2796 for (idx = 0; idx < count; ++idx)
2797 {
2798 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002799 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01002800 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002801 if (tv->v_type == VAR_CHANNEL
2802 || tv->v_type == VAR_JOB)
2803 {
2804 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar68db9962021-05-09 23:19:22 +02002805 semsg(_(e_using_invalid_value_as_string_str),
2806 vartype_name(tv->v_type));
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002807 break;
2808 }
2809 else
2810 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01002811 }
2812 else
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002813 p = tv_stringify(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01002814
2815 len = (int)STRLEN(p);
Bram Moolenaar35578162021-08-02 19:10:38 +02002816 if (GA_GROW_FAILS(&ga, len + 2))
Bram Moolenaarad39c092020-02-26 18:23:43 +01002817 failed = TRUE;
2818 else
2819 {
2820 if (ga.ga_len > 0)
2821 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
2822 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
2823 ga.ga_len += len;
2824 }
2825 clear_tv(tv);
2826 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02002827 ectx->ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002828 if (failed)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01002829 {
2830 ga_clear(&ga);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002831 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01002832 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01002833
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002834 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002835 {
2836 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02002837 {
2838 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002839 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01002840 if (did_emsg)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01002841 {
2842 ga_clear(&ga);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01002843 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01002844 }
Bram Moolenaar430deb12020-08-23 16:29:11 +02002845 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002846 else
2847 {
2848 msg_sb_eol();
2849 if (iptr->isn_type == ISN_ECHOMSG)
2850 {
2851 msg_attr(ga.ga_data, echo_attr);
2852 out_flush();
2853 }
Bram Moolenaar7de62622021-08-07 15:05:47 +02002854 else if (iptr->isn_type == ISN_ECHOCONSOLE)
2855 {
2856 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data),
2857 TRUE);
2858 ui_write((char_u *)"\r\n", 2, TRUE);
2859 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002860 else
2861 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002862 SOURCING_LNUM = iptr->isn_lnum;
2863 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002864 }
2865 }
2866 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01002867 ga_clear(&ga);
2868 }
2869 break;
2870
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002871 // load local variable or argument
2872 case ISN_LOAD:
Bram Moolenaar35578162021-08-02 19:10:38 +02002873 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002874 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002875 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002876 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002877 break;
2878
2879 // load v: variable
2880 case ISN_LOADV:
Bram Moolenaar35578162021-08-02 19:10:38 +02002881 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002882 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002883 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002884 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002885 break;
2886
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002887 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002888 case ISN_LOADSCRIPT:
2889 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01002890 scriptref_T *sref = iptr->isn_arg.script.scriptref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002891 svar_T *sv;
2892
Bram Moolenaar6db660b2021-08-01 14:08:54 +02002893 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01002894 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002895 goto theend;
Bram Moolenaar859cc212022-03-28 15:22:35 +01002896 allocate_if_null(sv);
Bram Moolenaar35578162021-08-02 19:10:38 +02002897 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002898 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002899 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002900 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002901 }
2902 break;
2903
2904 // load s: variable in old script
2905 case ISN_LOADS:
2906 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002907 hashtab_T *ht = &SCRIPT_VARS(
2908 iptr->isn_arg.loadstore.ls_sid);
2909 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002910 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002911
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002912 if (di == NULL)
2913 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002914 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002915 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002916 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002917 }
2918 else
2919 {
Bram Moolenaar35578162021-08-02 19:10:38 +02002920 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002921 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002922 copy_tv(&di->di_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002923 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002924 }
2925 }
2926 break;
2927
Bram Moolenaard3aac292020-04-19 14:32:17 +02002928 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002929 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02002930 case ISN_LOADB:
2931 case ISN_LOADW:
2932 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002933 {
Bram Moolenaar06b77222022-01-25 15:51:56 +00002934 int res = load_namespace_var(ectx, iptr->isn_type, iptr);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002935
Bram Moolenaar06b77222022-01-25 15:51:56 +00002936 if (res == NOTDONE)
Bram Moolenaarcbbc48f2022-01-18 12:58:28 +00002937 goto theend;
Bram Moolenaar06b77222022-01-25 15:51:56 +00002938 if (res == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002939 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002940 }
Bram Moolenaar06b77222022-01-25 15:51:56 +00002941
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002942 break;
2943
Bram Moolenaar03290b82020-12-19 16:30:44 +01002944 // load autoload variable
2945 case ISN_LOADAUTO:
2946 {
2947 char_u *name = iptr->isn_arg.string;
2948
Bram Moolenaar35578162021-08-02 19:10:38 +02002949 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002950 goto theend;
Bram Moolenaar03290b82020-12-19 16:30:44 +01002951 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002952 if (eval_variable(name, (int)STRLEN(name), 0,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002953 STACK_TV_BOT(0), NULL, EVAL_VAR_VERBOSE) == FAIL)
Bram Moolenaar03290b82020-12-19 16:30:44 +01002954 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002955 ++ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01002956 }
2957 break;
2958
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002959 // load g:/b:/w:/t: namespace
2960 case ISN_LOADGDICT:
2961 case ISN_LOADBDICT:
2962 case ISN_LOADWDICT:
2963 case ISN_LOADTDICT:
2964 {
2965 dict_T *d = NULL;
2966
2967 switch (iptr->isn_type)
2968 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02002969 case ISN_LOADGDICT: d = get_globvar_dict(); break;
2970 case ISN_LOADBDICT: d = curbuf->b_vars; break;
2971 case ISN_LOADWDICT: d = curwin->w_vars; break;
2972 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002973 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002974 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002975 }
Bram Moolenaar35578162021-08-02 19:10:38 +02002976 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002977 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002978 tv = STACK_TV_BOT(0);
2979 tv->v_type = VAR_DICT;
2980 tv->v_lock = 0;
2981 tv->vval.v_dict = d;
Bram Moolenaar1bd3cb22021-02-24 12:27:31 +01002982 ++d->dv_refcount;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002983 ++ectx->ec_stack.ga_len;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002984 }
2985 break;
2986
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002987 // load &option
2988 case ISN_LOADOPT:
2989 {
2990 typval_T optval;
2991 char_u *name = iptr->isn_arg.string;
2992
Bram Moolenaara8c17702020-04-01 21:17:24 +02002993 // This is not expected to fail, name is checked during
2994 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar35578162021-08-02 19:10:38 +02002995 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002996 goto theend;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002997 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002998 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002999 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003000 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003001 }
3002 break;
3003
3004 // load $ENV
3005 case ISN_LOADENV:
3006 {
3007 typval_T optval;
3008 char_u *name = iptr->isn_arg.string;
3009
Bram Moolenaar35578162021-08-02 19:10:38 +02003010 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003011 goto theend;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003012 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003013 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003014 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003015 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003016 }
3017 break;
3018
3019 // load @register
3020 case ISN_LOADREG:
Bram Moolenaar35578162021-08-02 19:10:38 +02003021 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003022 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003023 tv = STACK_TV_BOT(0);
3024 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003025 tv->v_lock = 0;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02003026 // This may result in NULL, which should be equivalent to an
3027 // empty string.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003028 tv->vval.v_string = get_reg_contents(
3029 iptr->isn_arg.number, GREG_EXPR_SRC);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003030 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003031 break;
3032
3033 // store local variable
3034 case ISN_STORE:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003035 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003036 tv = STACK_TV_VAR(iptr->isn_arg.number);
3037 clear_tv(tv);
3038 *tv = *STACK_TV_BOT(0);
3039 break;
3040
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003041 // store s: variable in old script
3042 case ISN_STORES:
3043 {
3044 hashtab_T *ht = &SCRIPT_VARS(
3045 iptr->isn_arg.loadstore.ls_sid);
3046 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003047 dictitem_T *di = find_var_in_ht(ht, 0, name + 2, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003048
Bram Moolenaar4c137212021-04-19 16:48:48 +02003049 --ectx->ec_stack.ga_len;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003050 if (di == NULL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003051 store_var(name, STACK_TV_BOT(0));
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003052 else
3053 {
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02003054 SOURCING_LNUM = iptr->isn_lnum;
3055 if (var_check_permission(di, name) == FAIL)
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003056 {
3057 clear_tv(STACK_TV_BOT(0));
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02003058 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003059 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003060 clear_tv(&di->di_tv);
3061 di->di_tv = *STACK_TV_BOT(0);
3062 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003063 }
3064 break;
3065
3066 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003067 case ISN_STORESCRIPT:
3068 {
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003069 scriptref_T *sref = iptr->isn_arg.script.scriptref;
3070 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003071
Bram Moolenaar6db660b2021-08-01 14:08:54 +02003072 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003073 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003074 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003075 --ectx->ec_stack.ga_len;
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003076
3077 // "const" and "final" are checked at compile time, locking
3078 // the value needs to be checked here.
3079 SOURCING_LNUM = iptr->isn_lnum;
3080 if (value_check_lock(sv->sv_tv->v_lock, sv->sv_name, FALSE))
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003081 {
3082 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003083 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003084 }
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003085
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003086 clear_tv(sv->sv_tv);
3087 *sv->sv_tv = *STACK_TV_BOT(0);
3088 }
3089 break;
3090
3091 // store option
3092 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003093 case ISN_STOREFUNCOPT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003094 {
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003095 char_u *opt_name = iptr->isn_arg.storeopt.so_name;
3096 int opt_flags = iptr->isn_arg.storeopt.so_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003097 long n = 0;
3098 char_u *s = NULL;
3099 char *msg;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003100 char_u numbuf[NUMBUFLEN];
3101 char_u *tofree = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003102
Bram Moolenaar4c137212021-04-19 16:48:48 +02003103 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003104 tv = STACK_TV_BOT(0);
3105 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01003106 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003107 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01003108 if (s == NULL)
3109 s = (char_u *)"";
3110 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003111 else if (iptr->isn_type == ISN_STOREFUNCOPT)
3112 {
3113 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003114 // If the option can be set to a function reference or
3115 // a lambda and the passed value is a function
3116 // reference, then convert it to the name (string) of
3117 // the function reference.
3118 s = tv2string(tv, &tofree, numbuf, 0);
3119 if (s == NULL || *s == NUL)
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003120 {
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003121 // cannot happen?
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003122 clear_tv(tv);
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003123 vim_free(tofree);
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003124 goto on_error;
3125 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003126 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003127 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003128 // must be VAR_NUMBER, CHECKTYPE makes sure
3129 n = tv->vval.v_number;
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003130 msg = set_option_value(opt_name, n, s, opt_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02003131 clear_tv(tv);
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003132 vim_free(tofree);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003133 if (msg != NULL)
3134 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003135 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003136 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02003137 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003138 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003139 }
3140 break;
3141
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003142 // store $ENV
3143 case ISN_STOREENV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003144 --ectx->ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003145 tv = STACK_TV_BOT(0);
3146 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
3147 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003148 break;
3149
3150 // store @r
3151 case ISN_STOREREG:
3152 {
3153 int reg = iptr->isn_arg.number;
3154
Bram Moolenaar4c137212021-04-19 16:48:48 +02003155 --ectx->ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01003156 tv = STACK_TV_BOT(0);
Bram Moolenaar74f4a962021-06-17 21:03:07 +02003157 write_reg_contents(reg, tv_get_string(tv), -1, FALSE);
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01003158 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003159 }
3160 break;
3161
3162 // store v: variable
3163 case ISN_STOREV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003164 --ectx->ec_stack.ga_len;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003165 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
3166 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003167 // should not happen, type is checked when compiling
3168 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003169 break;
3170
Bram Moolenaard3aac292020-04-19 14:32:17 +02003171 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003172 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02003173 case ISN_STOREB:
3174 case ISN_STOREW:
3175 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003176 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003177 dictitem_T *di;
3178 hashtab_T *ht;
3179 char_u *name = iptr->isn_arg.string + 2;
3180
Bram Moolenaard3aac292020-04-19 14:32:17 +02003181 switch (iptr->isn_type)
3182 {
3183 case ISN_STOREG:
3184 ht = get_globvar_ht();
3185 break;
3186 case ISN_STOREB:
3187 ht = &curbuf->b_vars->dv_hashtab;
3188 break;
3189 case ISN_STOREW:
3190 ht = &curwin->w_vars->dv_hashtab;
3191 break;
3192 case ISN_STORET:
3193 ht = &curtab->tp_vars->dv_hashtab;
3194 break;
3195 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003196 goto theend;
Bram Moolenaard3aac292020-04-19 14:32:17 +02003197 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003198
Bram Moolenaar4c137212021-04-19 16:48:48 +02003199 --ectx->ec_stack.ga_len;
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003200 di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003201 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003202 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003203 else
3204 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003205 SOURCING_LNUM = iptr->isn_lnum;
3206 if (var_check_permission(di, name) == FAIL)
3207 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003208 clear_tv(&di->di_tv);
3209 di->di_tv = *STACK_TV_BOT(0);
3210 }
3211 }
3212 break;
3213
Bram Moolenaar03290b82020-12-19 16:30:44 +01003214 // store an autoload variable
3215 case ISN_STOREAUTO:
3216 SOURCING_LNUM = iptr->isn_lnum;
3217 set_var(iptr->isn_arg.string, STACK_TV_BOT(-1), TRUE);
3218 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003219 --ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003220 break;
3221
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003222 // store number in local variable
3223 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01003224 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003225 clear_tv(tv);
3226 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01003227 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003228 break;
3229
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01003230 // store value in list or dict variable
3231 case ISN_STOREINDEX:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003232 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003233 int res = execute_storeindex(iptr, ectx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003234
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003235 if (res == FAIL)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02003236 goto on_error;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003237 if (res == NOTDONE)
3238 goto theend;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003239 }
3240 break;
3241
Bram Moolenaarea5c8982022-02-17 14:42:02 +00003242 // store value in list or blob range
Bram Moolenaar68452172021-04-12 21:21:02 +02003243 case ISN_STORERANGE:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003244 if (execute_storerange(iptr, ectx) == FAIL)
3245 goto on_error;
Bram Moolenaar68452172021-04-12 21:21:02 +02003246 break;
3247
Bram Moolenaar0186e582021-01-10 18:33:11 +01003248 // load or store variable or argument from outer scope
3249 case ISN_LOADOUTER:
3250 case ISN_STOREOUTER:
3251 {
3252 int depth = iptr->isn_arg.outer.outer_depth;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02003253 outer_T *outer = ectx->ec_outer_ref == NULL ? NULL
3254 : ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003255
3256 while (depth > 1 && outer != NULL)
3257 {
3258 outer = outer->out_up;
3259 --depth;
3260 }
3261 if (outer == NULL)
3262 {
3263 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar69c76172021-12-02 16:38:52 +00003264 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx
3265 || ectx->ec_outer_ref == NULL)
3266 // Possibly :def function called from legacy
3267 // context.
3268 emsg(_(e_closure_called_from_invalid_context));
3269 else
3270 iemsg("LOADOUTER depth more than scope levels");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003271 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003272 }
3273 tv = ((typval_T *)outer->out_stack->ga_data)
3274 + outer->out_frame_idx + STACK_FRAME_SIZE
3275 + iptr->isn_arg.outer.outer_idx;
3276 if (iptr->isn_type == ISN_LOADOUTER)
3277 {
Bram Moolenaar35578162021-08-02 19:10:38 +02003278 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003279 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003280 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003281 ++ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003282 }
3283 else
3284 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003285 --ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003286 clear_tv(tv);
3287 *tv = *STACK_TV_BOT(0);
3288 }
3289 }
3290 break;
3291
Bram Moolenaar752fc692021-01-04 21:57:11 +01003292 // unlet item in list or dict variable
3293 case ISN_UNLETINDEX:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003294 if (execute_unletindex(iptr, ectx) == FAIL)
3295 goto on_error;
Bram Moolenaar752fc692021-01-04 21:57:11 +01003296 break;
3297
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01003298 // unlet range of items in list variable
3299 case ISN_UNLETRANGE:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003300 if (execute_unletrange(iptr, ectx) == FAIL)
3301 goto on_error;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01003302 break;
3303
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003304 // push constant
3305 case ISN_PUSHNR:
3306 case ISN_PUSHBOOL:
3307 case ISN_PUSHSPEC:
3308 case ISN_PUSHF:
3309 case ISN_PUSHS:
3310 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003311 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003312 case ISN_PUSHCHANNEL:
3313 case ISN_PUSHJOB:
Bram Moolenaar35578162021-08-02 19:10:38 +02003314 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003315 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003316 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003317 tv->v_lock = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003318 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003319 switch (iptr->isn_type)
3320 {
3321 case ISN_PUSHNR:
3322 tv->v_type = VAR_NUMBER;
3323 tv->vval.v_number = iptr->isn_arg.number;
3324 break;
3325 case ISN_PUSHBOOL:
3326 tv->v_type = VAR_BOOL;
3327 tv->vval.v_number = iptr->isn_arg.number;
3328 break;
3329 case ISN_PUSHSPEC:
3330 tv->v_type = VAR_SPECIAL;
3331 tv->vval.v_number = iptr->isn_arg.number;
3332 break;
3333#ifdef FEAT_FLOAT
3334 case ISN_PUSHF:
3335 tv->v_type = VAR_FLOAT;
3336 tv->vval.v_float = iptr->isn_arg.fnumber;
3337 break;
3338#endif
3339 case ISN_PUSHBLOB:
3340 blob_copy(iptr->isn_arg.blob, tv);
3341 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003342 case ISN_PUSHFUNC:
3343 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01003344 if (iptr->isn_arg.string == NULL)
3345 tv->vval.v_string = NULL;
3346 else
3347 tv->vval.v_string =
3348 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003349 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003350 case ISN_PUSHCHANNEL:
3351#ifdef FEAT_JOB_CHANNEL
3352 tv->v_type = VAR_CHANNEL;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003353 tv->vval.v_channel = NULL;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003354#endif
3355 break;
3356 case ISN_PUSHJOB:
3357#ifdef FEAT_JOB_CHANNEL
3358 tv->v_type = VAR_JOB;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003359 tv->vval.v_job = NULL;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003360#endif
3361 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003362 default:
3363 tv->v_type = VAR_STRING;
Bram Moolenaarf8691002022-03-10 12:20:53 +00003364 tv->vval.v_string = iptr->isn_arg.string == NULL
3365 ? NULL : vim_strsave(iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003366 }
3367 break;
3368
Bram Moolenaar06b77222022-01-25 15:51:56 +00003369 case ISN_AUTOLOAD:
3370 {
3371 char_u *name = iptr->isn_arg.string;
3372
3373 (void)script_autoload(name, FALSE);
3374 if (find_func(name, TRUE))
3375 {
3376 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
3377 goto theend;
3378 tv = STACK_TV_BOT(0);
3379 tv->v_lock = 0;
3380 ++ectx->ec_stack.ga_len;
3381 tv->v_type = VAR_FUNC;
3382 tv->vval.v_string = vim_strsave(name);
3383 }
3384 else
3385 {
3386 int res = load_namespace_var(ectx, ISN_LOADG, iptr);
3387
3388 if (res == NOTDONE)
3389 goto theend;
3390 if (res == FAIL)
3391 goto on_error;
3392 }
3393 }
3394 break;
3395
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003396 case ISN_UNLET:
3397 if (do_unlet(iptr->isn_arg.unlet.ul_name,
3398 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003399 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003400 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02003401 case ISN_UNLETENV:
3402 vim_unsetenv(iptr->isn_arg.unlet.ul_name);
3403 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003404
Bram Moolenaaraacc9662021-08-13 19:40:51 +02003405 case ISN_LOCKUNLOCK:
3406 {
3407 typval_T *lval_root_save = lval_root;
3408 int res;
3409
3410 // Stack has the local variable, argument the whole :lock
3411 // or :unlock command, like ISN_EXEC.
3412 --ectx->ec_stack.ga_len;
3413 lval_root = STACK_TV_BOT(0);
3414 res = exec_command(iptr);
3415 clear_tv(lval_root);
3416 lval_root = lval_root_save;
3417 if (res == FAIL)
3418 goto on_error;
3419 }
3420 break;
3421
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02003422 case ISN_LOCKCONST:
3423 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
3424 break;
3425
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003426 // create a list from items on the stack; uses a single allocation
3427 // for the list header and the items
3428 case ISN_NEWLIST:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003429 if (exe_newlist(iptr->isn_arg.number, ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003430 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003431 break;
3432
3433 // create a dict from items on the stack
3434 case ISN_NEWDICT:
3435 {
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01003436 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003437
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01003438 SOURCING_LNUM = iptr->isn_lnum;
3439 res = exe_newdict(iptr->isn_arg.number, ectx);
3440 if (res == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003441 goto theend;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01003442 if (res == MAYBE)
3443 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003444 }
3445 break;
3446
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003447 // create a partial with NULL value
3448 case ISN_NEWPARTIAL:
3449 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
3450 goto theend;
3451 ++ectx->ec_stack.ga_len;
3452 tv = STACK_TV_BOT(-1);
3453 tv->v_type = VAR_PARTIAL;
3454 tv->v_lock = 0;
3455 tv->vval.v_partial = NULL;
3456 break;
3457
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003458 // call a :def function
3459 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02003460 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003461 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
3462 NULL,
3463 iptr->isn_arg.dfunc.cdf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003464 ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003465 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003466 break;
3467
3468 // call a builtin function
3469 case ISN_BCALL:
3470 SOURCING_LNUM = iptr->isn_lnum;
3471 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
3472 iptr->isn_arg.bfunc.cbf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003473 ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02003474 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003475 break;
3476
3477 // call a funcref or partial
3478 case ISN_PCALL:
3479 {
3480 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
3481 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003482 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003483
3484 SOURCING_LNUM = iptr->isn_lnum;
3485 if (pfunc->cpf_top)
3486 {
3487 // funcref is above the arguments
3488 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
3489 }
3490 else
3491 {
3492 // Get the funcref from the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02003493 --ectx->ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003494 partial_tv = *STACK_TV_BOT(0);
3495 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003496 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003497 r = call_partial(tv, pfunc->cpf_argcount, ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003498 if (tv == &partial_tv)
3499 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003500 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02003501 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003502 }
3503 break;
3504
Bram Moolenaarbd5da372020-03-31 23:13:10 +02003505 case ISN_PCALL_END:
3506 // PCALL finished, arguments have been consumed and replaced by
3507 // the return value. Now clear the funcref from the stack,
3508 // and move the return value in its place.
Bram Moolenaar4c137212021-04-19 16:48:48 +02003509 --ectx->ec_stack.ga_len;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02003510 clear_tv(STACK_TV_BOT(-1));
3511 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
3512 break;
3513
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003514 // call a user defined function or funcref/partial
3515 case ISN_UCALL:
3516 {
3517 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
3518
3519 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003520 if (call_eval_func(cufunc->cuf_name, cufunc->cuf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003521 ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02003522 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003523 }
3524 break;
3525
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02003526 // return from a :def function call without a value
3527 case ISN_RETURN_VOID:
Bram Moolenaar35578162021-08-02 19:10:38 +02003528 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003529 goto theend;
Bram Moolenaar299f3032021-01-08 20:53:09 +01003530 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003531 ++ectx->ec_stack.ga_len;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02003532 tv->v_type = VAR_VOID;
Bram Moolenaar299f3032021-01-08 20:53:09 +01003533 tv->vval.v_number = 0;
3534 tv->v_lock = 0;
3535 // FALLTHROUGH
3536
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02003537 // return from a :def function call with what is on the stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003538 case ISN_RETURN:
3539 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003540 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003541 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01003542
3543 if (trystack->ga_len > 0)
3544 trycmd = ((trycmd_T *)trystack->ga_data)
3545 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003546 if (trycmd != NULL
Bram Moolenaar4c137212021-04-19 16:48:48 +02003547 && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003548 {
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01003549 // jump to ":finally" or ":endtry"
3550 if (trycmd->tcd_finally_idx != 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02003551 ectx->ec_iidx = trycmd->tcd_finally_idx;
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01003552 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02003553 ectx->ec_iidx = trycmd->tcd_endtry_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003554 trycmd->tcd_return = TRUE;
3555 }
3556 else
Bram Moolenaard032f342020-07-18 18:13:02 +02003557 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003558 }
3559 break;
3560
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02003561 // push a partial, a reference to a compiled function
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003562 case ISN_FUNCREF:
3563 {
Bram Moolenaarf112f302020-12-20 17:47:52 +01003564 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar38453522021-11-28 22:00:12 +00003565 ufunc_T *ufunc;
3566 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003567
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003568 if (pt == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003569 goto theend;
Bram Moolenaar35578162021-08-02 19:10:38 +02003570 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003571 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003572 vim_free(pt);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003573 goto theend;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003574 }
Bram Moolenaar38453522021-11-28 22:00:12 +00003575 if (funcref->fr_func_name == NULL)
3576 {
3577 dfunc_T *pt_dfunc = ((dfunc_T *)def_functions.ga_data)
3578 + funcref->fr_dfunc_idx;
3579
3580 ufunc = pt_dfunc->df_ufunc;
3581 }
3582 else
3583 {
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003584 ufunc = find_func(funcref->fr_func_name, FALSE);
Bram Moolenaar38453522021-11-28 22:00:12 +00003585 }
Bram Moolenaar56acd1f2022-02-18 13:24:52 +00003586 if (ufunc == NULL)
3587 {
3588 SOURCING_LNUM = iptr->isn_lnum;
3589 iemsg("ufunc unexpectedly NULL for FUNCREF");
3590 goto theend;
3591 }
Bram Moolenaar38453522021-11-28 22:00:12 +00003592 if (fill_partial_and_closure(pt, ufunc, ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003593 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003594 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003595 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003596 tv->vval.v_partial = pt;
3597 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003598 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003599 }
3600 break;
3601
Bram Moolenaar38ddf332020-07-31 22:05:04 +02003602 // Create a global function from a lambda.
3603 case ISN_NEWFUNC:
3604 {
3605 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
3606
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01003607 if (copy_func(newfunc->nf_lambda, newfunc->nf_global,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003608 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003609 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02003610 }
3611 break;
3612
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01003613 // List functions
3614 case ISN_DEF:
3615 if (iptr->isn_arg.string == NULL)
3616 list_functions(NULL);
3617 else
3618 {
Bram Moolenaar14336722022-01-08 16:02:59 +00003619 exarg_T ea;
3620 garray_T lines_to_free;
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01003621
3622 CLEAR_FIELD(ea);
3623 ea.cmd = ea.arg = iptr->isn_arg.string;
Bram Moolenaar14336722022-01-08 16:02:59 +00003624 ga_init2(&lines_to_free, sizeof(char_u *), 50);
3625 define_function(&ea, NULL, &lines_to_free);
3626 ga_clear_strings(&lines_to_free);
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01003627 }
3628 break;
3629
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003630 // jump if a condition is met
3631 case ISN_JUMP:
3632 {
3633 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003634 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003635 int jump = TRUE;
3636
3637 if (when != JUMP_ALWAYS)
3638 {
3639 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003640 if (when == JUMP_IF_COND_FALSE
Bram Moolenaar13106602020-10-04 16:06:05 +02003641 || when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003642 || when == JUMP_IF_COND_TRUE)
3643 {
3644 SOURCING_LNUM = iptr->isn_lnum;
3645 jump = tv_get_bool_chk(tv, &error);
3646 if (error)
3647 goto on_error;
3648 }
3649 else
3650 jump = tv2bool(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003651 if (when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003652 || when == JUMP_AND_KEEP_IF_FALSE
3653 || when == JUMP_IF_COND_FALSE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003654 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01003655 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003656 {
3657 // drop the value from the stack
3658 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003659 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003660 }
3661 }
3662 if (jump)
Bram Moolenaar4c137212021-04-19 16:48:48 +02003663 ectx->ec_iidx = iptr->isn_arg.jump.jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003664 }
3665 break;
3666
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02003667 // Jump if an argument with a default value was already set and not
3668 // v:none.
3669 case ISN_JUMP_IF_ARG_SET:
3670 tv = STACK_TV_VAR(iptr->isn_arg.jumparg.jump_arg_off);
3671 if (tv->v_type != VAR_UNKNOWN
3672 && !(tv->v_type == VAR_SPECIAL
3673 && tv->vval.v_number == VVAL_NONE))
Bram Moolenaar4c137212021-04-19 16:48:48 +02003674 ectx->ec_iidx = iptr->isn_arg.jumparg.jump_where;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02003675 break;
3676
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003677 // top of a for loop
3678 case ISN_FOR:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003679 if (execute_for(iptr, ectx) == FAIL)
3680 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003681 break;
3682
3683 // start of ":try" block
3684 case ISN_TRY:
3685 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01003686 trycmd_T *trycmd = NULL;
3687
Bram Moolenaar35578162021-08-02 19:10:38 +02003688 if (GA_GROW_FAILS(&ectx->ec_trystack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003689 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003690 trycmd = ((trycmd_T *)ectx->ec_trystack.ga_data)
3691 + ectx->ec_trystack.ga_len;
3692 ++ectx->ec_trystack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003693 ++trylevel;
Bram Moolenaar8d4be892021-02-13 18:33:02 +01003694 CLEAR_POINTER(trycmd);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003695 trycmd->tcd_frame_idx = ectx->ec_frame_idx;
3696 trycmd->tcd_stack_len = ectx->ec_stack.ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01003697 trycmd->tcd_catch_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00003698 iptr->isn_arg.tryref.try_ref->try_catch;
Bram Moolenaara91a7132021-03-25 21:12:15 +01003699 trycmd->tcd_finally_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00003700 iptr->isn_arg.tryref.try_ref->try_finally;
Bram Moolenaara91a7132021-03-25 21:12:15 +01003701 trycmd->tcd_endtry_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00003702 iptr->isn_arg.tryref.try_ref->try_endtry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003703 }
3704 break;
3705
3706 case ISN_PUSHEXC:
3707 if (current_exception == NULL)
3708 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003709 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003710 iemsg("Evaluating catch while current_exception is NULL");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003711 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003712 }
Bram Moolenaar35578162021-08-02 19:10:38 +02003713 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003714 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003715 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003716 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003717 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003718 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003719 tv->vval.v_string = vim_strsave(
3720 (char_u *)current_exception->value);
3721 break;
3722
3723 case ISN_CATCH:
3724 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003725 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003726
Bram Moolenaar4c137212021-04-19 16:48:48 +02003727 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003728 if (trystack->ga_len > 0)
3729 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01003730 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003731 + trystack->ga_len - 1;
3732 trycmd->tcd_caught = TRUE;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003733 trycmd->tcd_did_throw = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003734 }
3735 did_emsg = got_int = did_throw = FALSE;
Bram Moolenaar1430cee2021-01-17 19:20:32 +01003736 force_abort = need_rethrow = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003737 catch_exception(current_exception);
3738 }
3739 break;
3740
Bram Moolenaarc150c092021-02-13 15:02:46 +01003741 case ISN_TRYCONT:
3742 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003743 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003744 trycont_T *trycont = &iptr->isn_arg.trycont;
3745 int i;
3746 trycmd_T *trycmd;
3747 int iidx = trycont->tct_where;
3748
3749 if (trystack->ga_len < trycont->tct_levels)
3750 {
3751 siemsg("TRYCONT: expected %d levels, found %d",
3752 trycont->tct_levels, trystack->ga_len);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003753 goto theend;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003754 }
3755 // Make :endtry jump to any outer try block and the last
3756 // :endtry inside the loop to the loop start.
3757 for (i = trycont->tct_levels; i > 0; --i)
3758 {
3759 trycmd = ((trycmd_T *)trystack->ga_data)
3760 + trystack->ga_len - i;
Bram Moolenaar2e34c342021-03-14 12:13:33 +01003761 // Add one to tcd_cont to be able to jump to
3762 // instruction with index zero.
3763 trycmd->tcd_cont = iidx + 1;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003764 iidx = trycmd->tcd_finally_idx == 0
3765 ? trycmd->tcd_endtry_idx : trycmd->tcd_finally_idx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003766 }
3767 // jump to :finally or :endtry of current try statement
Bram Moolenaar4c137212021-04-19 16:48:48 +02003768 ectx->ec_iidx = iidx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003769 }
3770 break;
3771
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003772 case ISN_FINALLY:
3773 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003774 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003775 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
3776 + trystack->ga_len - 1;
3777
3778 // Reset the index to avoid a return statement jumps here
3779 // again.
3780 trycmd->tcd_finally_idx = 0;
3781 break;
3782 }
3783
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003784 // end of ":try" block
3785 case ISN_ENDTRY:
3786 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003787 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003788
3789 if (trystack->ga_len > 0)
3790 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003791 trycmd_T *trycmd;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003792
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003793 --trystack->ga_len;
3794 --trylevel;
3795 trycmd = ((trycmd_T *)trystack->ga_data)
3796 + trystack->ga_len;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003797 if (trycmd->tcd_did_throw)
3798 did_throw = TRUE;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01003799 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003800 {
3801 // discard the exception
3802 if (caught_stack == current_exception)
3803 caught_stack = caught_stack->caught;
3804 discard_current_exception();
3805 }
3806
3807 if (trycmd->tcd_return)
Bram Moolenaard032f342020-07-18 18:13:02 +02003808 goto func_return;
Bram Moolenaard9d77892021-02-12 21:32:47 +01003809
Bram Moolenaar4c137212021-04-19 16:48:48 +02003810 while (ectx->ec_stack.ga_len > trycmd->tcd_stack_len)
Bram Moolenaard9d77892021-02-12 21:32:47 +01003811 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003812 --ectx->ec_stack.ga_len;
Bram Moolenaard9d77892021-02-12 21:32:47 +01003813 clear_tv(STACK_TV_BOT(0));
3814 }
Bram Moolenaar8d4be892021-02-13 18:33:02 +01003815 if (trycmd->tcd_cont != 0)
Bram Moolenaarc150c092021-02-13 15:02:46 +01003816 // handling :continue: jump to outer try block or
3817 // start of the loop
Bram Moolenaar4c137212021-04-19 16:48:48 +02003818 ectx->ec_iidx = trycmd->tcd_cont - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003819 }
3820 }
3821 break;
3822
3823 case ISN_THROW:
Bram Moolenaar8f81b222021-01-14 21:47:06 +01003824 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003825 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02003826
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003827 if (trystack->ga_len == 0 && trylevel == 0 && emsg_silent)
3828 {
3829 // throwing an exception while using "silent!" causes
3830 // the function to abort but not display an error.
3831 tv = STACK_TV_BOT(-1);
3832 clear_tv(tv);
3833 tv->v_type = VAR_NUMBER;
3834 tv->vval.v_number = 0;
3835 goto done;
3836 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003837 --ectx->ec_stack.ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003838 tv = STACK_TV_BOT(0);
3839 if (tv->vval.v_string == NULL
3840 || *skipwhite(tv->vval.v_string) == NUL)
3841 {
3842 vim_free(tv->vval.v_string);
3843 SOURCING_LNUM = iptr->isn_lnum;
3844 emsg(_(e_throw_with_empty_string));
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003845 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003846 }
3847
3848 // Inside a "catch" we need to first discard the caught
3849 // exception.
3850 if (trystack->ga_len > 0)
3851 {
3852 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
3853 + trystack->ga_len - 1;
3854 if (trycmd->tcd_caught && current_exception != NULL)
3855 {
3856 // discard the exception
3857 if (caught_stack == current_exception)
3858 caught_stack = caught_stack->caught;
3859 discard_current_exception();
3860 trycmd->tcd_caught = FALSE;
3861 }
3862 }
3863
Bram Moolenaar90a57162022-02-12 14:23:17 +00003864 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003865 if (throw_exception(tv->vval.v_string, ET_USER, NULL)
3866 == FAIL)
3867 {
3868 vim_free(tv->vval.v_string);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003869 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003870 }
3871 did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003872 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003873 break;
3874
3875 // compare with special values
3876 case ISN_COMPAREBOOL:
3877 case ISN_COMPARESPECIAL:
3878 {
3879 typval_T *tv1 = STACK_TV_BOT(-2);
3880 typval_T *tv2 = STACK_TV_BOT(-1);
3881 varnumber_T arg1 = tv1->vval.v_number;
3882 varnumber_T arg2 = tv2->vval.v_number;
3883 int res;
3884
3885 switch (iptr->isn_arg.op.op_type)
3886 {
3887 case EXPR_EQUAL: res = arg1 == arg2; break;
3888 case EXPR_NEQUAL: res = arg1 != arg2; break;
3889 default: res = 0; break;
3890 }
3891
Bram Moolenaar4c137212021-04-19 16:48:48 +02003892 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003893 tv1->v_type = VAR_BOOL;
3894 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
3895 }
3896 break;
3897
Bram Moolenaar7a222242022-03-01 19:23:24 +00003898 case ISN_COMPARENULL:
3899 {
3900 typval_T *tv1 = STACK_TV_BOT(-2);
3901 typval_T *tv2 = STACK_TV_BOT(-1);
3902 int res;
3903
3904 res = typval_compare_null(tv1, tv2);
3905 if (res == MAYBE)
3906 goto on_error;
3907 if (iptr->isn_arg.op.op_type == EXPR_NEQUAL)
3908 res = !res;
3909 clear_tv(tv1);
3910 clear_tv(tv2);
3911 --ectx->ec_stack.ga_len;
3912 tv1->v_type = VAR_BOOL;
3913 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
3914 }
3915 break;
3916
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003917 // Operation with two number arguments
3918 case ISN_OPNR:
3919 case ISN_COMPARENR:
3920 {
3921 typval_T *tv1 = STACK_TV_BOT(-2);
3922 typval_T *tv2 = STACK_TV_BOT(-1);
3923 varnumber_T arg1 = tv1->vval.v_number;
3924 varnumber_T arg2 = tv2->vval.v_number;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02003925 varnumber_T res = 0;
3926 int div_zero = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003927
3928 switch (iptr->isn_arg.op.op_type)
3929 {
3930 case EXPR_MULT: res = arg1 * arg2; break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02003931 case EXPR_DIV: if (arg2 == 0)
3932 div_zero = TRUE;
3933 else
3934 res = arg1 / arg2;
3935 break;
3936 case EXPR_REM: if (arg2 == 0)
3937 div_zero = TRUE;
3938 else
3939 res = arg1 % arg2;
3940 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003941 case EXPR_SUB: res = arg1 - arg2; break;
3942 case EXPR_ADD: res = arg1 + arg2; break;
3943
3944 case EXPR_EQUAL: res = arg1 == arg2; break;
3945 case EXPR_NEQUAL: res = arg1 != arg2; break;
3946 case EXPR_GREATER: res = arg1 > arg2; break;
3947 case EXPR_GEQUAL: res = arg1 >= arg2; break;
3948 case EXPR_SMALLER: res = arg1 < arg2; break;
3949 case EXPR_SEQUAL: res = arg1 <= arg2; break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02003950 default: break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003951 }
3952
Bram Moolenaar4c137212021-04-19 16:48:48 +02003953 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003954 if (iptr->isn_type == ISN_COMPARENR)
3955 {
3956 tv1->v_type = VAR_BOOL;
3957 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
3958 }
3959 else
3960 tv1->vval.v_number = res;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02003961 if (div_zero)
3962 {
3963 SOURCING_LNUM = iptr->isn_lnum;
3964 emsg(_(e_divide_by_zero));
3965 goto on_error;
3966 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003967 }
3968 break;
3969
3970 // Computation with two float arguments
3971 case ISN_OPFLOAT:
3972 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01003973#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003974 {
3975 typval_T *tv1 = STACK_TV_BOT(-2);
3976 typval_T *tv2 = STACK_TV_BOT(-1);
3977 float_T arg1 = tv1->vval.v_float;
3978 float_T arg2 = tv2->vval.v_float;
3979 float_T res = 0;
3980 int cmp = FALSE;
3981
3982 switch (iptr->isn_arg.op.op_type)
3983 {
3984 case EXPR_MULT: res = arg1 * arg2; break;
3985 case EXPR_DIV: res = arg1 / arg2; break;
3986 case EXPR_SUB: res = arg1 - arg2; break;
3987 case EXPR_ADD: res = arg1 + arg2; break;
3988
3989 case EXPR_EQUAL: cmp = arg1 == arg2; break;
3990 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
3991 case EXPR_GREATER: cmp = arg1 > arg2; break;
3992 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
3993 case EXPR_SMALLER: cmp = arg1 < arg2; break;
3994 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
3995 default: cmp = 0; break;
3996 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003997 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003998 if (iptr->isn_type == ISN_COMPAREFLOAT)
3999 {
4000 tv1->v_type = VAR_BOOL;
4001 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
4002 }
4003 else
4004 tv1->vval.v_float = res;
4005 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01004006#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004007 break;
4008
4009 case ISN_COMPARELIST:
Bram Moolenaar265f8112021-12-19 12:33:05 +00004010 case ISN_COMPAREDICT:
4011 case ISN_COMPAREFUNC:
4012 case ISN_COMPARESTRING:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004013 case ISN_COMPAREBLOB:
4014 {
4015 typval_T *tv1 = STACK_TV_BOT(-2);
4016 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar265f8112021-12-19 12:33:05 +00004017 exprtype_T exprtype = iptr->isn_arg.op.op_type;
4018 int ic = iptr->isn_arg.op.op_ic;
4019 int res = FALSE;
4020 int status = OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004021
Bram Moolenaar265f8112021-12-19 12:33:05 +00004022 SOURCING_LNUM = iptr->isn_lnum;
4023 if (iptr->isn_type == ISN_COMPARELIST)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004024 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00004025 status = typval_compare_list(tv1, tv2,
4026 exprtype, ic, &res);
4027 }
4028 else if (iptr->isn_type == ISN_COMPAREDICT)
4029 {
4030 status = typval_compare_dict(tv1, tv2,
4031 exprtype, ic, &res);
4032 }
4033 else if (iptr->isn_type == ISN_COMPAREFUNC)
4034 {
4035 status = typval_compare_func(tv1, tv2,
4036 exprtype, ic, &res);
4037 }
4038 else if (iptr->isn_type == ISN_COMPARESTRING)
4039 {
4040 status = typval_compare_string(tv1, tv2,
4041 exprtype, ic, &res);
4042 }
4043 else
4044 {
4045 status = typval_compare_blob(tv1, tv2, exprtype, &res);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004046 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004047 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004048 clear_tv(tv1);
4049 clear_tv(tv2);
4050 tv1->v_type = VAR_BOOL;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004051 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4052 if (status == FAIL)
4053 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004054 }
4055 break;
4056
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004057 case ISN_COMPAREANY:
4058 {
4059 typval_T *tv1 = STACK_TV_BOT(-2);
4060 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar657137c2021-01-09 15:45:23 +01004061 exprtype_T exprtype = iptr->isn_arg.op.op_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004062 int ic = iptr->isn_arg.op.op_ic;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004063 int status;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004064
Bram Moolenaareb26f432020-09-14 16:50:05 +02004065 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004066 status = typval_compare(tv1, tv2, exprtype, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004067 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004068 --ectx->ec_stack.ga_len;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004069 if (status == FAIL)
4070 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004071 }
4072 break;
4073
4074 case ISN_ADDLIST:
4075 case ISN_ADDBLOB:
4076 {
4077 typval_T *tv1 = STACK_TV_BOT(-2);
4078 typval_T *tv2 = STACK_TV_BOT(-1);
4079
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004080 // add two lists or blobs
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004081 if (iptr->isn_type == ISN_ADDLIST)
Bram Moolenaar07802042021-09-09 23:01:14 +02004082 {
4083 if (iptr->isn_arg.op.op_type == EXPR_APPEND
4084 && tv1->vval.v_list != NULL)
4085 list_extend(tv1->vval.v_list, tv2->vval.v_list,
4086 NULL);
4087 else
4088 eval_addlist(tv1, tv2);
4089 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004090 else
4091 eval_addblob(tv1, tv2);
4092 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004093 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004094 }
4095 break;
4096
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004097 case ISN_LISTAPPEND:
4098 {
4099 typval_T *tv1 = STACK_TV_BOT(-2);
4100 typval_T *tv2 = STACK_TV_BOT(-1);
4101 list_T *l = tv1->vval.v_list;
4102
4103 // add an item to a list
Bram Moolenaar1f4a3452022-01-01 18:29:21 +00004104 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004105 if (l == NULL)
4106 {
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004107 emsg(_(e_cannot_add_to_null_list));
4108 goto on_error;
4109 }
Bram Moolenaar1f4a3452022-01-01 18:29:21 +00004110 if (value_check_lock(l->lv_lock, NULL, FALSE))
4111 goto on_error;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004112 if (list_append_tv(l, tv2) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004113 goto theend;
Bram Moolenaar955347c2020-10-19 23:01:46 +02004114 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004115 --ectx->ec_stack.ga_len;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004116 }
4117 break;
4118
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02004119 case ISN_BLOBAPPEND:
4120 {
4121 typval_T *tv1 = STACK_TV_BOT(-2);
4122 typval_T *tv2 = STACK_TV_BOT(-1);
4123 blob_T *b = tv1->vval.v_blob;
4124 int error = FALSE;
4125 varnumber_T n;
4126
4127 // add a number to a blob
4128 if (b == NULL)
4129 {
4130 SOURCING_LNUM = iptr->isn_lnum;
4131 emsg(_(e_cannot_add_to_null_blob));
4132 goto on_error;
4133 }
4134 n = tv_get_number_chk(tv2, &error);
4135 if (error)
4136 goto on_error;
4137 ga_append(&b->bv_ga, (int)n);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004138 --ectx->ec_stack.ga_len;
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02004139 }
4140 break;
4141
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004142 // Computation with two arguments of unknown type
4143 case ISN_OPANY:
4144 {
4145 typval_T *tv1 = STACK_TV_BOT(-2);
4146 typval_T *tv2 = STACK_TV_BOT(-1);
4147 varnumber_T n1, n2;
4148#ifdef FEAT_FLOAT
4149 float_T f1 = 0, f2 = 0;
4150#endif
4151 int error = FALSE;
4152
4153 if (iptr->isn_arg.op.op_type == EXPR_ADD)
4154 {
4155 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
4156 {
4157 eval_addlist(tv1, tv2);
4158 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004159 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004160 break;
4161 }
4162 else if (tv1->v_type == VAR_BLOB
4163 && tv2->v_type == VAR_BLOB)
4164 {
4165 eval_addblob(tv1, tv2);
4166 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004167 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004168 break;
4169 }
4170 }
4171#ifdef FEAT_FLOAT
4172 if (tv1->v_type == VAR_FLOAT)
4173 {
4174 f1 = tv1->vval.v_float;
4175 n1 = 0;
4176 }
4177 else
4178#endif
4179 {
Bram Moolenaarf665e972020-12-05 19:17:16 +01004180 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004181 n1 = tv_get_number_chk(tv1, &error);
4182 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02004183 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004184#ifdef FEAT_FLOAT
4185 if (tv2->v_type == VAR_FLOAT)
4186 f1 = n1;
4187#endif
4188 }
4189#ifdef FEAT_FLOAT
4190 if (tv2->v_type == VAR_FLOAT)
4191 {
4192 f2 = tv2->vval.v_float;
4193 n2 = 0;
4194 }
4195 else
4196#endif
4197 {
4198 n2 = tv_get_number_chk(tv2, &error);
4199 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02004200 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004201#ifdef FEAT_FLOAT
4202 if (tv1->v_type == VAR_FLOAT)
4203 f2 = n2;
4204#endif
4205 }
4206#ifdef FEAT_FLOAT
4207 // if there is a float on either side the result is a float
4208 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
4209 {
4210 switch (iptr->isn_arg.op.op_type)
4211 {
4212 case EXPR_MULT: f1 = f1 * f2; break;
4213 case EXPR_DIV: f1 = f1 / f2; break;
4214 case EXPR_SUB: f1 = f1 - f2; break;
4215 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004216 default: SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004217 emsg(_(e_cannot_use_percent_with_float));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004218 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004219 }
4220 clear_tv(tv1);
4221 clear_tv(tv2);
4222 tv1->v_type = VAR_FLOAT;
4223 tv1->vval.v_float = f1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004224 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004225 }
4226 else
4227#endif
4228 {
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004229 int failed = FALSE;
4230
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004231 switch (iptr->isn_arg.op.op_type)
4232 {
4233 case EXPR_MULT: n1 = n1 * n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004234 case EXPR_DIV: n1 = num_divide(n1, n2, &failed);
4235 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01004236 goto on_error;
4237 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004238 case EXPR_SUB: n1 = n1 - n2; break;
4239 case EXPR_ADD: n1 = n1 + n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004240 default: n1 = num_modulus(n1, n2, &failed);
4241 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01004242 goto on_error;
4243 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004244 }
4245 clear_tv(tv1);
4246 clear_tv(tv2);
4247 tv1->v_type = VAR_NUMBER;
4248 tv1->vval.v_number = n1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004249 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004250 }
4251 }
4252 break;
4253
4254 case ISN_CONCAT:
4255 {
4256 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string;
4257 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string;
4258 char_u *res;
4259
4260 res = concat_str(str1, str2);
4261 clear_tv(STACK_TV_BOT(-2));
4262 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02004263 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004264 STACK_TV_BOT(-1)->vval.v_string = res;
4265 }
4266 break;
4267
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004268 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004269 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004270 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004271 int is_slice = iptr->isn_type == ISN_STRSLICE;
4272 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004273 char_u *res;
4274
4275 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004276 // string slice: string is at stack-3, first index at
4277 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004278 if (is_slice)
4279 {
4280 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004281 n1 = tv->vval.v_number;
4282 }
4283
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004284 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004285 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004286
Bram Moolenaar4c137212021-04-19 16:48:48 +02004287 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004288 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004289 if (is_slice)
4290 // Slice: Select the characters from the string
Bram Moolenaar6601b622021-01-13 21:47:15 +01004291 res = string_slice(tv->vval.v_string, n1, n2, FALSE);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004292 else
4293 // Index: The resulting variable is a string of a
Bram Moolenaar0289a092021-03-14 18:40:19 +01004294 // single character (including composing characters).
4295 // If the index is too big or negative the result is
4296 // empty.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004297 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004298 vim_free(tv->vval.v_string);
4299 tv->vval.v_string = res;
4300 }
4301 break;
4302
4303 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02004304 case ISN_LISTSLICE:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004305 case ISN_BLOBINDEX:
4306 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004307 {
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004308 int is_slice = iptr->isn_type == ISN_LISTSLICE
4309 || iptr->isn_type == ISN_BLOBSLICE;
4310 int is_blob = iptr->isn_type == ISN_BLOBINDEX
4311 || iptr->isn_type == ISN_BLOBSLICE;
Bram Moolenaared591872020-08-15 22:14:53 +02004312 varnumber_T n1, n2;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004313 typval_T *val_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004314
4315 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02004316 // list slice: list is at stack-3, indexes at stack-2 and
4317 // stack-1
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004318 // Same for blob.
4319 val_tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004320
4321 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02004322 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004323 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02004324
4325 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004326 {
Bram Moolenaared591872020-08-15 22:14:53 +02004327 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02004328 n1 = tv->vval.v_number;
4329 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004330 }
Bram Moolenaared591872020-08-15 22:14:53 +02004331
Bram Moolenaar4c137212021-04-19 16:48:48 +02004332 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02004333 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02004334 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004335 if (is_blob)
4336 {
4337 if (blob_slice_or_index(val_tv->vval.v_blob, is_slice,
4338 n1, n2, FALSE, tv) == FAIL)
4339 goto on_error;
4340 }
4341 else
4342 {
4343 if (list_slice_or_index(val_tv->vval.v_list, is_slice,
4344 n1, n2, FALSE, tv, TRUE) == FAIL)
4345 goto on_error;
4346 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004347 }
4348 break;
4349
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004350 case ISN_ANYINDEX:
4351 case ISN_ANYSLICE:
4352 {
4353 int is_slice = iptr->isn_type == ISN_ANYSLICE;
4354 typval_T *var1, *var2;
4355 int res;
4356
4357 // index: composite is at stack-2, index at stack-1
4358 // slice: composite is at stack-3, indexes at stack-2 and
4359 // stack-1
4360 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02004361 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004362 if (check_can_index(tv, TRUE, TRUE) == FAIL)
4363 goto on_error;
4364 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
4365 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
Bram Moolenaar6601b622021-01-13 21:47:15 +01004366 res = eval_index_inner(tv, is_slice, var1, var2,
4367 FALSE, NULL, -1, TRUE);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004368 clear_tv(var1);
4369 if (is_slice)
4370 clear_tv(var2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004371 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004372 if (res == FAIL)
4373 goto on_error;
4374 }
4375 break;
4376
Bram Moolenaar9af78762020-06-16 11:34:42 +02004377 case ISN_SLICE:
4378 {
4379 list_T *list;
4380 int count = iptr->isn_arg.number;
4381
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02004382 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02004383 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02004384 list = tv->vval.v_list;
4385
4386 // no error for short list, expect it to be checked earlier
4387 if (list != NULL && list->lv_len >= count)
4388 {
4389 list_T *newlist = list_slice(list,
4390 count, list->lv_len - 1);
4391
4392 if (newlist != NULL)
4393 {
4394 list_unref(list);
4395 tv->vval.v_list = newlist;
4396 ++newlist->lv_refcount;
4397 }
4398 }
4399 }
4400 break;
4401
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004402 case ISN_GETITEM:
4403 {
4404 listitem_T *li;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02004405 getitem_T *gi = &iptr->isn_arg.getitem;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004406
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02004407 // Get list item: list is at stack-1, push item.
4408 // List type and length is checked for when compiling.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02004409 tv = STACK_TV_BOT(-1 - gi->gi_with_op);
4410 li = list_find(tv->vval.v_list, gi->gi_index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004411
Bram Moolenaar35578162021-08-02 19:10:38 +02004412 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004413 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004414 ++ectx->ec_stack.ga_len;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004415 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004416
4417 // Useful when used in unpack assignment. Reset at
4418 // ISN_DROP.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02004419 ectx->ec_where.wt_index = gi->gi_index + 1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004420 ectx->ec_where.wt_variable = TRUE;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004421 }
4422 break;
4423
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004424 case ISN_MEMBER:
4425 {
4426 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004427 char_u *key;
4428 dictitem_T *di;
4429
4430 // dict member: dict is at stack-2, key at stack-1
4431 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02004432 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004433 dict = tv->vval.v_dict;
4434
4435 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02004436 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004437 key = tv->vval.v_string;
Bram Moolenaar086fc9a2020-10-30 18:33:02 +01004438 if (key == NULL)
4439 key = (char_u *)"";
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02004440
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004441 if ((di = dict_find(dict, key, -1)) == NULL)
4442 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004443 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004444 semsg(_(e_key_not_present_in_dictionary), key);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004445
4446 // If :silent! is used we will continue, make sure the
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004447 // stack contents makes sense and the dict stack is
4448 // updated.
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004449 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004450 --ectx->ec_stack.ga_len;
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004451 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004452 (void) dict_stack_save(tv);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004453 tv->v_type = VAR_NUMBER;
4454 tv->vval.v_number = 0;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004455 goto on_fatal_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004456 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004457 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004458 --ectx->ec_stack.ga_len;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004459 // Put the dict used on the dict stack, it might be used by
4460 // a dict function later.
Bram Moolenaar50788ef2020-07-05 16:51:26 +02004461 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004462 if (dict_stack_save(tv) == FAIL)
4463 goto on_fatal_error;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02004464 copy_tv(&di->di_tv, tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004465 }
4466 break;
4467
4468 // dict member with string key
4469 case ISN_STRINGMEMBER:
4470 {
4471 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004472 dictitem_T *di;
4473
4474 tv = STACK_TV_BOT(-1);
4475 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
4476 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004477 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004478 emsg(_(e_dictionary_required));
Bram Moolenaard032f342020-07-18 18:13:02 +02004479 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004480 }
4481 dict = tv->vval.v_dict;
4482
4483 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
4484 == NULL)
4485 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004486 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar381692b2022-02-02 20:01:27 +00004487 semsg(_(e_key_not_present_in_dictionary),
4488 iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02004489 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004490 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004491 // Put the dict used on the dict stack, it might be used by
4492 // a dict function later.
4493 if (dict_stack_save(tv) == FAIL)
4494 goto on_fatal_error;
4495
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004496 copy_tv(&di->di_tv, tv);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004497 }
4498 break;
4499
4500 case ISN_CLEARDICT:
4501 dict_stack_drop();
4502 break;
4503
4504 case ISN_USEDICT:
4505 {
4506 typval_T *dict_tv = dict_stack_get_tv();
4507
4508 // Turn "dict.Func" into a partial for "Func" bound to
4509 // "dict". Don't do this when "Func" is already a partial
4510 // that was bound explicitly (pt_auto is FALSE).
4511 tv = STACK_TV_BOT(-1);
4512 if (dict_tv != NULL
4513 && dict_tv->v_type == VAR_DICT
4514 && dict_tv->vval.v_dict != NULL
4515 && (tv->v_type == VAR_FUNC
4516 || (tv->v_type == VAR_PARTIAL
4517 && (tv->vval.v_partial->pt_auto
4518 || tv->vval.v_partial->pt_dict == NULL))))
4519 dict_tv->vval.v_dict =
4520 make_partial(dict_tv->vval.v_dict, tv);
4521 dict_stack_drop();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004522 }
4523 break;
4524
4525 case ISN_NEGATENR:
4526 tv = STACK_TV_BOT(-1);
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00004527 // CHECKTYPE should have checked the variable type
Bram Moolenaarc58164c2020-03-29 18:40:30 +02004528#ifdef FEAT_FLOAT
4529 if (tv->v_type == VAR_FLOAT)
4530 tv->vval.v_float = -tv->vval.v_float;
4531 else
4532#endif
4533 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004534 break;
4535
4536 case ISN_CHECKNR:
4537 {
4538 int error = FALSE;
4539
4540 tv = STACK_TV_BOT(-1);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02004541 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004542 if (check_not_string(tv) == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004543 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004544 (void)tv_get_number_chk(tv, &error);
4545 if (error)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004546 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004547 }
4548 break;
4549
4550 case ISN_CHECKTYPE:
4551 {
4552 checktype_T *ct = &iptr->isn_arg.type;
4553
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01004554 tv = STACK_TV_BOT((int)ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02004555 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004556 if (!ectx->ec_where.wt_variable)
4557 ectx->ec_where.wt_index = ct->ct_arg_idx;
4558 if (check_typval_type(ct->ct_type, tv, ectx->ec_where)
4559 == FAIL)
Bram Moolenaar5e654232020-09-16 15:22:00 +02004560 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004561 if (!ectx->ec_where.wt_variable)
4562 ectx->ec_where.wt_index = 0;
Bram Moolenaar5e654232020-09-16 15:22:00 +02004563
4564 // number 0 is FALSE, number 1 is TRUE
4565 if (tv->v_type == VAR_NUMBER
4566 && ct->ct_type->tt_type == VAR_BOOL
4567 && (tv->vval.v_number == 0
4568 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004569 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02004570 tv->v_type = VAR_BOOL;
4571 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02004572 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004573 }
4574 }
4575 break;
4576
Bram Moolenaar9af78762020-06-16 11:34:42 +02004577 case ISN_CHECKLEN:
4578 {
4579 int min_len = iptr->isn_arg.checklen.cl_min_len;
4580 list_T *list = NULL;
4581
4582 tv = STACK_TV_BOT(-1);
4583 if (tv->v_type == VAR_LIST)
4584 list = tv->vval.v_list;
4585 if (list == NULL || list->lv_len < min_len
4586 || (list->lv_len > min_len
4587 && !iptr->isn_arg.checklen.cl_more_OK))
4588 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004589 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004590 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02004591 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004592 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02004593 }
4594 }
4595 break;
4596
Bram Moolenaaraa210a32021-01-02 15:41:03 +01004597 case ISN_SETTYPE:
Bram Moolenaar381692b2022-02-02 20:01:27 +00004598 set_tv_type(STACK_TV_BOT(-1), iptr->isn_arg.type.ct_type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01004599 break;
4600
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004601 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004602 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004603 {
4604 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004605 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004606
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004607 if (iptr->isn_type == ISN_2BOOL)
4608 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004609 tv = STACK_TV_BOT(iptr->isn_arg.tobool.offset);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004610 n = tv2bool(tv);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004611 if (iptr->isn_arg.tobool.invert)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004612 n = !n;
4613 }
4614 else
4615 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004616 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004617 SOURCING_LNUM = iptr->isn_lnum;
4618 n = tv_get_bool_chk(tv, &error);
4619 if (error)
4620 goto on_error;
4621 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004622 clear_tv(tv);
4623 tv->v_type = VAR_BOOL;
4624 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
4625 }
4626 break;
4627
4628 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02004629 case ISN_2STRING_ANY:
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01004630 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004631 if (do_2string(STACK_TV_BOT(iptr->isn_arg.tostring.offset),
4632 iptr->isn_type == ISN_2STRING_ANY,
4633 iptr->isn_arg.tostring.tolerant) == FAIL)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01004634 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004635 break;
4636
Bram Moolenaar08597872020-12-10 19:43:40 +01004637 case ISN_RANGE:
4638 {
4639 exarg_T ea;
4640 char *errormsg;
4641
Bram Moolenaarece0b872021-01-08 20:40:45 +01004642 ea.line2 = 0;
Bram Moolenaard1510ee2021-01-04 16:15:58 +01004643 ea.addr_count = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01004644 ea.addr_type = ADDR_LINES;
4645 ea.cmd = iptr->isn_arg.string;
Bram Moolenaarece0b872021-01-08 20:40:45 +01004646 ea.skip = FALSE;
Bram Moolenaar08597872020-12-10 19:43:40 +01004647 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaarece0b872021-01-08 20:40:45 +01004648 goto on_error;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004649
Bram Moolenaar35578162021-08-02 19:10:38 +02004650 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004651 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004652 ++ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004653 tv = STACK_TV_BOT(-1);
4654 tv->v_type = VAR_NUMBER;
4655 tv->v_lock = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01004656 if (ea.addr_count == 0)
4657 tv->vval.v_number = curwin->w_cursor.lnum;
4658 else
4659 tv->vval.v_number = ea.line2;
4660 }
4661 break;
4662
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004663 case ISN_PUT:
4664 {
4665 int regname = iptr->isn_arg.put.put_regname;
4666 linenr_T lnum = iptr->isn_arg.put.put_lnum;
4667 char_u *expr = NULL;
4668 int dir = FORWARD;
4669
Bram Moolenaar08597872020-12-10 19:43:40 +01004670 if (lnum < -2)
4671 {
4672 // line number was put on the stack by ISN_RANGE
4673 tv = STACK_TV_BOT(-1);
4674 curwin->w_cursor.lnum = tv->vval.v_number;
4675 if (lnum == LNUM_VARIABLE_RANGE_ABOVE)
4676 dir = BACKWARD;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004677 --ectx->ec_stack.ga_len;
Bram Moolenaar08597872020-12-10 19:43:40 +01004678 }
4679 else if (lnum == -2)
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004680 // :put! above cursor
4681 dir = BACKWARD;
4682 else if (lnum >= 0)
Bram Moolenaar4e713ba2022-02-07 15:31:37 +00004683 {
4684 curwin->w_cursor.lnum = lnum;
4685 if (lnum == 0)
4686 // check_cursor() below will move to line 1
4687 dir = BACKWARD;
4688 }
Bram Moolenaara28639e2021-01-19 22:48:09 +01004689
4690 if (regname == '=')
4691 {
4692 tv = STACK_TV_BOT(-1);
4693 if (tv->v_type == VAR_STRING)
4694 expr = tv->vval.v_string;
4695 else
4696 {
4697 expr = typval2string(tv, TRUE); // allocates value
4698 clear_tv(tv);
4699 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004700 --ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004701 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004702 check_cursor();
4703 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
4704 vim_free(expr);
4705 }
4706 break;
4707
Bram Moolenaar02194d22020-10-24 23:08:38 +02004708 case ISN_CMDMOD:
Bram Moolenaar4c137212021-04-19 16:48:48 +02004709 ectx->ec_funclocal.floc_save_cmdmod = cmdmod;
4710 ectx->ec_funclocal.floc_restore_cmdmod = TRUE;
4711 ectx->ec_funclocal.floc_restore_cmdmod_stacklen =
4712 ectx->ec_stack.ga_len;
Bram Moolenaar02194d22020-10-24 23:08:38 +02004713 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
4714 apply_cmdmod(&cmdmod);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004715 break;
4716
Bram Moolenaar02194d22020-10-24 23:08:38 +02004717 case ISN_CMDMOD_REV:
4718 // filter regprog is owned by the instruction, don't free it
4719 cmdmod.cmod_filter_regmatch.regprog = NULL;
4720 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004721 cmdmod = ectx->ec_funclocal.floc_save_cmdmod;
4722 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004723 break;
4724
Bram Moolenaar792f7862020-11-23 08:31:18 +01004725 case ISN_UNPACK:
4726 {
4727 int count = iptr->isn_arg.unpack.unp_count;
4728 int semicolon = iptr->isn_arg.unpack.unp_semicolon;
4729 list_T *l;
4730 listitem_T *li;
4731 int i;
4732
4733 // Check there is a valid list to unpack.
4734 tv = STACK_TV_BOT(-1);
4735 if (tv->v_type != VAR_LIST)
4736 {
4737 SOURCING_LNUM = iptr->isn_lnum;
4738 emsg(_(e_for_argument_must_be_sequence_of_lists));
4739 goto on_error;
4740 }
4741 l = tv->vval.v_list;
4742 if (l == NULL
4743 || l->lv_len < (semicolon ? count - 1 : count))
4744 {
4745 SOURCING_LNUM = iptr->isn_lnum;
4746 emsg(_(e_list_value_does_not_have_enough_items));
4747 goto on_error;
4748 }
4749 else if (!semicolon && l->lv_len > count)
4750 {
4751 SOURCING_LNUM = iptr->isn_lnum;
4752 emsg(_(e_list_value_has_more_items_than_targets));
4753 goto on_error;
4754 }
4755
4756 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar35578162021-08-02 19:10:38 +02004757 if (GA_GROW_FAILS(&ectx->ec_stack, count - 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004758 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004759 ectx->ec_stack.ga_len += count - 1;
Bram Moolenaar792f7862020-11-23 08:31:18 +01004760
4761 // Variable after semicolon gets a list with the remaining
4762 // items.
4763 if (semicolon)
4764 {
4765 list_T *rem_list =
4766 list_alloc_with_items(l->lv_len - count + 1);
4767
4768 if (rem_list == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004769 goto theend;
Bram Moolenaar792f7862020-11-23 08:31:18 +01004770 tv = STACK_TV_BOT(-count);
4771 tv->vval.v_list = rem_list;
4772 ++rem_list->lv_refcount;
4773 tv->v_lock = 0;
4774 li = l->lv_first;
4775 for (i = 0; i < count - 1; ++i)
4776 li = li->li_next;
4777 for (i = 0; li != NULL; ++i)
4778 {
Bram Moolenaar61efa162022-03-18 13:10:48 +00004779 typval_T tvcopy;
4780
4781 copy_tv(&li->li_tv, &tvcopy);
4782 list_set_item(rem_list, i, &tvcopy);
Bram Moolenaar792f7862020-11-23 08:31:18 +01004783 li = li->li_next;
4784 }
4785 --count;
4786 }
4787
4788 // Produce the values in reverse order, first item last.
4789 li = l->lv_first;
4790 for (i = 0; i < count; ++i)
4791 {
4792 tv = STACK_TV_BOT(-i - 1);
4793 copy_tv(&li->li_tv, tv);
4794 li = li->li_next;
4795 }
4796
4797 list_unref(l);
4798 }
4799 break;
4800
Bram Moolenaarb2049902021-01-24 12:53:53 +01004801 case ISN_PROF_START:
4802 case ISN_PROF_END:
4803 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01004804#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01004805 funccall_T cookie;
4806 ufunc_T *cur_ufunc =
4807 (((dfunc_T *)def_functions.ga_data)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02004808 + ectx->ec_dfunc_idx)->df_ufunc;
Bram Moolenaarb2049902021-01-24 12:53:53 +01004809
4810 cookie.func = cur_ufunc;
4811 if (iptr->isn_type == ISN_PROF_START)
4812 {
4813 func_line_start(&cookie, iptr->isn_lnum);
4814 // if we get here the instruction is executed
4815 func_line_exec(&cookie);
4816 }
4817 else
4818 func_line_end(&cookie);
Bram Moolenaarf002a412021-01-24 13:34:18 +01004819#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01004820 }
4821 break;
4822
Bram Moolenaare99d4222021-06-13 14:01:26 +02004823 case ISN_DEBUG:
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02004824 handle_debug(iptr, ectx);
Bram Moolenaare99d4222021-06-13 14:01:26 +02004825 break;
4826
Bram Moolenaar389df252020-07-09 21:20:47 +02004827 case ISN_SHUFFLE:
4828 {
Bram Moolenaar792f7862020-11-23 08:31:18 +01004829 typval_T tmp_tv;
4830 int item = iptr->isn_arg.shuffle.shfl_item;
4831 int up = iptr->isn_arg.shuffle.shfl_up;
Bram Moolenaar389df252020-07-09 21:20:47 +02004832
4833 tmp_tv = *STACK_TV_BOT(-item);
4834 for ( ; up > 0 && item > 1; --up)
4835 {
4836 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
4837 --item;
4838 }
4839 *STACK_TV_BOT(-item) = tmp_tv;
4840 }
4841 break;
4842
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004843 case ISN_DROP:
Bram Moolenaar4c137212021-04-19 16:48:48 +02004844 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004845 clear_tv(STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02004846 ectx->ec_where.wt_index = 0;
4847 ectx->ec_where.wt_variable = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004848 break;
4849 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004850 continue;
4851
Bram Moolenaard032f342020-07-18 18:13:02 +02004852func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02004853 // Restore previous function. If the frame pointer is where we started
4854 // then there is none and we are done.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004855 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02004856 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02004857
Bram Moolenaar4c137212021-04-19 16:48:48 +02004858 if (func_return(ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004859 // only fails when out of memory
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004860 goto theend;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02004861 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02004862
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004863on_error:
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004864 // Jump here for an error that does not require aborting execution.
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004865 // If "emsg_silent" is set then ignore the error, unless it was set
4866 // when calling the function.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004867 if (did_emsg_cumul + did_emsg == ectx->ec_did_emsg_before
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004868 && emsg_silent && did_emsg_def == 0)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004869 {
4870 // If a sequence of instructions causes an error while ":silent!"
4871 // was used, restore the stack length and jump ahead to restoring
4872 // the cmdmod.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004873 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004874 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004875 while (ectx->ec_stack.ga_len
4876 > ectx->ec_funclocal.floc_restore_cmdmod_stacklen)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004877 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004878 --ectx->ec_stack.ga_len;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004879 clear_tv(STACK_TV_BOT(0));
4880 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004881 while (ectx->ec_instr[ectx->ec_iidx].isn_type != ISN_CMDMOD_REV)
4882 ++ectx->ec_iidx;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004883 }
Bram Moolenaarcd030c42020-10-30 21:49:40 +01004884 continue;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004885 }
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004886on_fatal_error:
4887 // Jump here for an error that messes up the stack.
Bram Moolenaar171fb922020-10-28 16:54:47 +01004888 // If we are not inside a try-catch started here, abort execution.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004889 if (trylevel <= ectx->ec_trylevel_at_start)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004890 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004891 }
4892
4893done:
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004894 ret = OK;
4895theend:
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004896 dict_stack_clear(dict_stack_len_at_start);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004897 ectx->ec_trylevel_at_start = save_trylevel_at_start;
4898 return ret;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004899}
4900
4901/*
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004902 * Execute the instructions from a VAR_INSTR typeval and put the result in
4903 * "rettv".
4904 * Return OK or FAIL.
4905 */
4906 int
4907exe_typval_instr(typval_T *tv, typval_T *rettv)
4908{
4909 ectx_T *ectx = tv->vval.v_instr->instr_ectx;
4910 isn_T *save_instr = ectx->ec_instr;
4911 int save_iidx = ectx->ec_iidx;
4912 int res;
4913
4914 ectx->ec_instr = tv->vval.v_instr->instr_instr;
4915 res = exec_instructions(ectx);
4916 if (res == OK)
4917 {
4918 *rettv = *STACK_TV_BOT(-1);
4919 --ectx->ec_stack.ga_len;
4920 }
4921
4922 ectx->ec_instr = save_instr;
4923 ectx->ec_iidx = save_iidx;
4924
4925 return res;
4926}
4927
4928/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02004929 * Execute the instructions from an ISN_SUBSTITUTE command, which are in
4930 * "substitute_instr".
4931 */
4932 char_u *
4933exe_substitute_instr(void)
4934{
4935 ectx_T *ectx = substitute_instr->subs_ectx;
4936 isn_T *save_instr = ectx->ec_instr;
4937 int save_iidx = ectx->ec_iidx;
4938 char_u *res;
4939
4940 ectx->ec_instr = substitute_instr->subs_instr;
4941 if (exec_instructions(ectx) == OK)
Bram Moolenaar90193e62021-04-04 20:49:50 +02004942 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004943 typval_T *tv = STACK_TV_BOT(-1);
4944
Bram Moolenaar27523602021-06-05 21:36:19 +02004945 res = typval2string(tv, TRUE);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004946 --ectx->ec_stack.ga_len;
4947 clear_tv(tv);
Bram Moolenaar90193e62021-04-04 20:49:50 +02004948 }
4949 else
4950 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004951 substitute_instr->subs_status = FAIL;
4952 res = vim_strsave((char_u *)"");
Bram Moolenaar90193e62021-04-04 20:49:50 +02004953 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004954
Bram Moolenaar4c137212021-04-19 16:48:48 +02004955 ectx->ec_instr = save_instr;
4956 ectx->ec_iidx = save_iidx;
4957
4958 return res;
4959}
4960
4961/*
4962 * Call a "def" function from old Vim script.
4963 * Return OK or FAIL.
4964 */
4965 int
4966call_def_function(
4967 ufunc_T *ufunc,
4968 int argc_arg, // nr of arguments
4969 typval_T *argv, // arguments
4970 partial_T *partial, // optional partial for context
4971 typval_T *rettv) // return value
4972{
4973 ectx_T ectx; // execution context
4974 int argc = argc_arg;
4975 typval_T *tv;
4976 int idx;
4977 int ret = FAIL;
4978 int defcount = ufunc->uf_args.ga_len - argc;
4979 sctx_T save_current_sctx = current_sctx;
4980 int did_emsg_before = did_emsg_cumul + did_emsg;
4981 int save_suppress_errthrow = suppress_errthrow;
4982 msglist_T **saved_msg_list = NULL;
4983 msglist_T *private_msg_list = NULL;
4984 int save_emsg_silent_def = emsg_silent_def;
4985 int save_did_emsg_def = did_emsg_def;
4986 int orig_funcdepth;
Bram Moolenaare99d4222021-06-13 14:01:26 +02004987 int orig_nesting_level = ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004988
4989// Get pointer to item in the stack.
4990#undef STACK_TV
4991#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
4992
4993// Get pointer to item at the bottom of the stack, -1 is the bottom.
4994#undef STACK_TV_BOT
4995#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
4996
4997// Get pointer to a local variable on the stack. Negative for arguments.
4998#undef STACK_TV_VAR
4999#define STACK_TV_VAR(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_frame_idx + STACK_FRAME_SIZE + idx)
5000
5001 if (ufunc->uf_def_status == UF_NOT_COMPILED
5002 || ufunc->uf_def_status == UF_COMPILE_ERROR
Bram Moolenaar139575d2022-03-15 19:29:30 +00005003 || (func_needs_compiling(ufunc, get_compile_type(ufunc))
5004 && compile_def_function(ufunc, FALSE,
5005 get_compile_type(ufunc), NULL) == FAIL))
Bram Moolenaar4c137212021-04-19 16:48:48 +02005006 {
5007 if (did_emsg_cumul + did_emsg == did_emsg_before)
5008 semsg(_(e_function_is_not_compiled_str),
5009 printable_func_name(ufunc));
5010 return FAIL;
5011 }
5012
5013 {
5014 // Check the function was really compiled.
5015 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5016 + ufunc->uf_dfunc_idx;
5017 if (INSTRUCTIONS(dfunc) == NULL)
5018 {
5019 iemsg("using call_def_function() on not compiled function");
5020 return FAIL;
5021 }
5022 }
5023
5024 // If depth of calling is getting too high, don't execute the function.
5025 orig_funcdepth = funcdepth_get();
5026 if (funcdepth_increment() == FAIL)
5027 return FAIL;
5028
5029 CLEAR_FIELD(ectx);
5030 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
5031 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
Bram Moolenaar35578162021-08-02 19:10:38 +02005032 if (GA_GROW_FAILS(&ectx.ec_stack, 20))
Bram Moolenaar4c137212021-04-19 16:48:48 +02005033 {
5034 funcdepth_decrement();
5035 return FAIL;
5036 }
5037 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
5038 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
5039 ectx.ec_did_emsg_before = did_emsg_before;
Bram Moolenaare99d4222021-06-13 14:01:26 +02005040 ++ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005041
5042 idx = argc - ufunc->uf_args.ga_len;
5043 if (idx > 0 && ufunc->uf_va_name == NULL)
5044 {
5045 if (idx == 1)
5046 emsg(_(e_one_argument_too_many));
5047 else
5048 semsg(_(e_nr_arguments_too_many), idx);
5049 goto failed_early;
5050 }
Bram Moolenaarc6d71532021-06-05 18:49:38 +02005051 idx = argc - ufunc->uf_args.ga_len + ufunc->uf_def_args.ga_len;
5052 if (idx < 0)
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02005053 {
5054 if (idx == -1)
5055 emsg(_(e_one_argument_too_few));
5056 else
5057 semsg(_(e_nr_arguments_too_few), -idx);
5058 goto failed_early;
5059 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005060
5061 // Put arguments on the stack, but no more than what the function expects.
5062 // A lambda can be called with more arguments than it uses.
5063 for (idx = 0; idx < argc
5064 && (ufunc->uf_va_name != NULL || idx < ufunc->uf_args.ga_len);
5065 ++idx)
5066 {
5067 if (idx >= ufunc->uf_args.ga_len - ufunc->uf_def_args.ga_len
5068 && argv[idx].v_type == VAR_SPECIAL
5069 && argv[idx].vval.v_number == VVAL_NONE)
5070 {
5071 // Use the default value.
5072 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
5073 }
5074 else
5075 {
5076 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
5077 && check_typval_arg_type(
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02005078 ufunc->uf_arg_types[idx], &argv[idx],
5079 NULL, idx + 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005080 goto failed_early;
5081 copy_tv(&argv[idx], STACK_TV_BOT(0));
5082 }
5083 ++ectx.ec_stack.ga_len;
5084 }
5085
5086 // Turn varargs into a list. Empty list if no args.
5087 if (ufunc->uf_va_name != NULL)
5088 {
5089 int vararg_count = argc - ufunc->uf_args.ga_len;
5090
5091 if (vararg_count < 0)
5092 vararg_count = 0;
5093 else
5094 argc -= vararg_count;
5095 if (exe_newlist(vararg_count, &ectx) == FAIL)
5096 goto failed_early;
5097
5098 // Check the type of the list items.
5099 tv = STACK_TV_BOT(-1);
5100 if (ufunc->uf_va_type != NULL
5101 && ufunc->uf_va_type != &t_list_any
5102 && ufunc->uf_va_type->tt_member != &t_any
5103 && tv->vval.v_list != NULL)
5104 {
5105 type_T *expected = ufunc->uf_va_type->tt_member;
5106 listitem_T *li = tv->vval.v_list->lv_first;
5107
5108 for (idx = 0; idx < vararg_count; ++idx)
5109 {
5110 if (check_typval_arg_type(expected, &li->li_tv,
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02005111 NULL, argc + idx + 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005112 goto failed_early;
5113 li = li->li_next;
5114 }
5115 }
5116
5117 if (defcount > 0)
5118 // Move varargs list to below missing default arguments.
5119 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
5120 --ectx.ec_stack.ga_len;
5121 }
5122
5123 // Make space for omitted arguments, will store default value below.
5124 // Any varargs list goes after them.
5125 if (defcount > 0)
5126 for (idx = 0; idx < defcount; ++idx)
5127 {
5128 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
5129 ++ectx.ec_stack.ga_len;
5130 }
5131 if (ufunc->uf_va_name != NULL)
5132 ++ectx.ec_stack.ga_len;
5133
5134 // Frame pointer points to just after arguments.
5135 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
5136 ectx.ec_initial_frame_idx = ectx.ec_frame_idx;
5137
5138 {
5139 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5140 + ufunc->uf_dfunc_idx;
5141 ufunc_T *base_ufunc = dfunc->df_ufunc;
5142
5143 // "uf_partial" is on the ufunc that "df_ufunc" points to, as is done
5144 // by copy_func().
5145 if (partial != NULL || base_ufunc->uf_partial != NULL)
5146 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005147 ectx.ec_outer_ref = ALLOC_CLEAR_ONE(outer_ref_T);
5148 if (ectx.ec_outer_ref == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005149 goto failed_early;
5150 if (partial != NULL)
5151 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00005152 outer_T *outer = get_pt_outer(partial);
5153
5154 if (outer->out_stack == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005155 {
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00005156 if (current_ectx != NULL)
5157 {
5158 if (current_ectx->ec_outer_ref != NULL
5159 && current_ectx->ec_outer_ref->or_outer != NULL)
5160 ectx.ec_outer_ref->or_outer =
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005161 current_ectx->ec_outer_ref->or_outer;
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00005162 }
5163 // Should there be an error here?
Bram Moolenaar4c137212021-04-19 16:48:48 +02005164 }
5165 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005166 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00005167 ectx.ec_outer_ref->or_outer = outer;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005168 ++partial->pt_refcount;
5169 ectx.ec_outer_ref->or_partial = partial;
5170 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005171 }
5172 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005173 {
5174 ectx.ec_outer_ref->or_outer = &base_ufunc->uf_partial->pt_outer;
5175 ++base_ufunc->uf_partial->pt_refcount;
5176 ectx.ec_outer_ref->or_partial = base_ufunc->uf_partial;
5177 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005178 }
5179 }
5180
5181 // dummy frame entries
5182 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
5183 {
5184 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
5185 ++ectx.ec_stack.ga_len;
5186 }
5187
5188 {
5189 // Reserve space for local variables and any closure reference count.
5190 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5191 + ufunc->uf_dfunc_idx;
5192
Bram Moolenaar5cd64792021-12-25 18:23:24 +00005193 // Initialize variables to zero. That avoids having to generate
5194 // initializing instructions for "var nr: number", "var x: any", etc.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005195 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaar5cd64792021-12-25 18:23:24 +00005196 {
5197 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
5198 STACK_TV_VAR(idx)->vval.v_number = 0;
5199 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005200 ectx.ec_stack.ga_len += dfunc->df_varcount;
5201 if (dfunc->df_has_closure)
5202 {
5203 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
5204 STACK_TV_VAR(idx)->vval.v_number = 0;
5205 ++ectx.ec_stack.ga_len;
5206 }
5207
5208 ectx.ec_instr = INSTRUCTIONS(dfunc);
5209 }
5210
5211 // Following errors are in the function, not the caller.
5212 // Commands behave like vim9script.
5213 estack_push_ufunc(ufunc, 1);
5214 current_sctx = ufunc->uf_script_ctx;
5215 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
5216
5217 // Use a specific location for storing error messages to be converted to an
5218 // exception.
5219 saved_msg_list = msg_list;
5220 msg_list = &private_msg_list;
5221
5222 // Do turn errors into exceptions.
5223 suppress_errthrow = FALSE;
5224
5225 // Do not delete the function while executing it.
5226 ++ufunc->uf_calls;
5227
5228 // When ":silent!" was used before calling then we still abort the
5229 // function. If ":silent!" is used in the function then we don't.
5230 emsg_silent_def = emsg_silent;
5231 did_emsg_def = 0;
5232
5233 ectx.ec_where.wt_index = 0;
5234 ectx.ec_where.wt_variable = FALSE;
5235
5236 // Execute the instructions until done.
5237 ret = exec_instructions(&ectx);
5238 if (ret == OK)
5239 {
5240 // function finished, get result from the stack.
5241 if (ufunc->uf_ret_type == &t_void)
5242 {
5243 rettv->v_type = VAR_VOID;
5244 }
5245 else
5246 {
5247 tv = STACK_TV_BOT(-1);
5248 *rettv = *tv;
5249 tv->v_type = VAR_UNKNOWN;
5250 }
5251 }
5252
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01005253 // When failed need to unwind the call stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005254 while (ectx.ec_frame_idx != ectx.ec_initial_frame_idx)
5255 func_return(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02005256
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005257 // Deal with any remaining closures, they may be in use somewhere.
5258 if (ectx.ec_funcrefs.ga_len > 0)
Bram Moolenaarf112f302020-12-20 17:47:52 +01005259 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005260 handle_closure_in_use(&ectx, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00005261 ga_clear(&ectx.ec_funcrefs);
Bram Moolenaarf112f302020-12-20 17:47:52 +01005262 }
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005263
Bram Moolenaaree8580e2020-08-28 17:19:07 +02005264 estack_pop();
5265 current_sctx = save_current_sctx;
5266
Bram Moolenaar2336c372021-12-06 15:06:54 +00005267 if (--ufunc->uf_calls <= 0 && ufunc->uf_refcount <= 0)
5268 // Function was unreferenced while being used, free it now.
5269 func_clear_free(ufunc, FALSE);
Bram Moolenaarc970e422021-03-17 15:03:04 +01005270
Bram Moolenaar352134b2020-10-17 22:04:08 +02005271 if (*msg_list != NULL && saved_msg_list != NULL)
5272 {
5273 msglist_T **plist = saved_msg_list;
5274
5275 // Append entries from the current msg_list (uncaught exceptions) to
5276 // the saved msg_list.
5277 while (*plist != NULL)
5278 plist = &(*plist)->next;
5279
5280 *plist = *msg_list;
5281 }
5282 msg_list = saved_msg_list;
5283
Bram Moolenaar4c137212021-04-19 16:48:48 +02005284 if (ectx.ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar02194d22020-10-24 23:08:38 +02005285 {
5286 cmdmod.cmod_filter_regmatch.regprog = NULL;
5287 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005288 cmdmod = ectx.ec_funclocal.floc_save_cmdmod;
Bram Moolenaar02194d22020-10-24 23:08:38 +02005289 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +01005290 emsg_silent_def = save_emsg_silent_def;
5291 did_emsg_def += save_did_emsg_def;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02005292
Bram Moolenaaree8580e2020-08-28 17:19:07 +02005293failed_early:
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02005294 // Free all local variables, but not arguments.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005295 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
5296 clear_tv(STACK_TV(idx));
Bram Moolenaare99d4222021-06-13 14:01:26 +02005297 ex_nesting_level = orig_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02005298
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005299 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01005300 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005301 if (ectx.ec_outer_ref != NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +01005302 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005303 if (ectx.ec_outer_ref->or_outer_allocated)
5304 vim_free(ectx.ec_outer_ref->or_outer);
5305 partial_unref(ectx.ec_outer_ref->or_partial);
5306 vim_free(ectx.ec_outer_ref);
Bram Moolenaar0186e582021-01-10 18:33:11 +01005307 }
5308
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02005309 // Not sure if this is necessary.
5310 suppress_errthrow = save_suppress_errthrow;
5311
Bram Moolenaarb5841b92021-07-15 18:09:53 +02005312 if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before
5313 && !need_rethrow)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005314 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02005315 printable_func_name(ufunc));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01005316 funcdepth_restore(orig_funcdepth);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005317 return ret;
5318}
5319
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005320/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02005321 * List instructions "instr" up to "instr_count" or until ISN_FINISH.
5322 * "ufunc" has the source lines, NULL for the instructions of ISN_SUBSTITUTE.
5323 * "pfx" is prefixed to every line.
5324 */
5325 static void
5326list_instructions(char *pfx, isn_T *instr, int instr_count, ufunc_T *ufunc)
5327{
5328 int line_idx = 0;
5329 int prev_current = 0;
5330 int current;
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02005331 int def_arg_idx = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005332
5333 for (current = 0; current < instr_count; ++current)
5334 {
5335 isn_T *iptr = &instr[current];
5336 char *line;
5337
5338 if (ufunc != NULL)
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02005339 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005340 while (line_idx < iptr->isn_lnum
5341 && line_idx < ufunc->uf_lines.ga_len)
5342 {
5343 if (current > prev_current)
5344 {
5345 msg_puts("\n\n");
5346 prev_current = current;
5347 }
5348 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
5349 if (line != NULL)
5350 msg(line);
5351 }
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02005352 if (iptr->isn_type == ISN_JUMP_IF_ARG_SET)
5353 {
5354 int first_def_arg = ufunc->uf_args.ga_len
5355 - ufunc->uf_def_args.ga_len;
5356
5357 if (def_arg_idx > 0)
5358 msg_puts("\n\n");
5359 msg_start();
5360 msg_puts(" ");
5361 msg_puts(((char **)(ufunc->uf_args.ga_data))[
5362 first_def_arg + def_arg_idx]);
5363 msg_puts(" = ");
5364 msg_puts(((char **)(ufunc->uf_def_args.ga_data))[def_arg_idx++]);
5365 msg_clr_eos();
5366 msg_end();
5367 }
5368 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005369
5370 switch (iptr->isn_type)
5371 {
5372 case ISN_EXEC:
5373 smsg("%s%4d EXEC %s", pfx, current, iptr->isn_arg.string);
5374 break;
Bram Moolenaar20677332021-06-06 17:02:53 +02005375 case ISN_EXEC_SPLIT:
5376 smsg("%s%4d EXEC_SPLIT %s", pfx, current, iptr->isn_arg.string);
5377 break;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00005378 case ISN_EXECRANGE:
5379 smsg("%s%4d EXECRANGE %s", pfx, current, iptr->isn_arg.string);
5380 break;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005381 case ISN_LEGACY_EVAL:
5382 smsg("%s%4d EVAL legacy %s", pfx, current,
5383 iptr->isn_arg.string);
5384 break;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02005385 case ISN_REDIRSTART:
5386 smsg("%s%4d REDIR", pfx, current);
5387 break;
5388 case ISN_REDIREND:
5389 smsg("%s%4d REDIR END%s", pfx, current,
5390 iptr->isn_arg.number ? " append" : "");
5391 break;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005392 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005393#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005394 smsg("%s%4d CEXPR pre %s", pfx, current,
5395 cexpr_get_auname(iptr->isn_arg.number));
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005396#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005397 break;
5398 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005399#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005400 {
5401 cexprref_T *cer = iptr->isn_arg.cexpr.cexpr_ref;
5402
5403 smsg("%s%4d CEXPR core %s%s \"%s\"", pfx, current,
5404 cexpr_get_auname(cer->cer_cmdidx),
5405 cer->cer_forceit ? "!" : "",
5406 cer->cer_cmdline);
5407 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005408#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005409 break;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005410 case ISN_INSTR:
5411 {
5412 smsg("%s%4d INSTR", pfx, current);
5413 list_instructions(" ", iptr->isn_arg.instr,
5414 INT_MAX, NULL);
5415 msg(" -------------");
5416 }
5417 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005418 case ISN_SUBSTITUTE:
5419 {
5420 subs_T *subs = &iptr->isn_arg.subs;
5421
5422 smsg("%s%4d SUBSTITUTE %s", pfx, current, subs->subs_cmd);
5423 list_instructions(" ", subs->subs_instr, INT_MAX, NULL);
5424 msg(" -------------");
5425 }
5426 break;
5427 case ISN_EXECCONCAT:
5428 smsg("%s%4d EXECCONCAT %lld", pfx, current,
5429 (varnumber_T)iptr->isn_arg.number);
5430 break;
5431 case ISN_ECHO:
5432 {
5433 echo_T *echo = &iptr->isn_arg.echo;
5434
5435 smsg("%s%4d %s %d", pfx, current,
5436 echo->echo_with_white ? "ECHO" : "ECHON",
5437 echo->echo_count);
5438 }
5439 break;
5440 case ISN_EXECUTE:
5441 smsg("%s%4d EXECUTE %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02005442 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005443 break;
5444 case ISN_ECHOMSG:
5445 smsg("%s%4d ECHOMSG %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02005446 (varnumber_T)(iptr->isn_arg.number));
5447 break;
5448 case ISN_ECHOCONSOLE:
5449 smsg("%s%4d ECHOCONSOLE %lld", pfx, current,
5450 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005451 break;
5452 case ISN_ECHOERR:
5453 smsg("%s%4d ECHOERR %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02005454 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005455 break;
5456 case ISN_LOAD:
5457 {
5458 if (iptr->isn_arg.number < 0)
5459 smsg("%s%4d LOAD arg[%lld]", pfx, current,
5460 (varnumber_T)(iptr->isn_arg.number
5461 + STACK_FRAME_SIZE));
5462 else
5463 smsg("%s%4d LOAD $%lld", pfx, current,
5464 (varnumber_T)(iptr->isn_arg.number));
5465 }
5466 break;
5467 case ISN_LOADOUTER:
5468 {
5469 if (iptr->isn_arg.number < 0)
5470 smsg("%s%4d LOADOUTER level %d arg[%d]", pfx, current,
5471 iptr->isn_arg.outer.outer_depth,
5472 iptr->isn_arg.outer.outer_idx
5473 + STACK_FRAME_SIZE);
5474 else
5475 smsg("%s%4d LOADOUTER level %d $%d", pfx, current,
5476 iptr->isn_arg.outer.outer_depth,
5477 iptr->isn_arg.outer.outer_idx);
5478 }
5479 break;
5480 case ISN_LOADV:
5481 smsg("%s%4d LOADV v:%s", pfx, current,
5482 get_vim_var_name(iptr->isn_arg.number));
5483 break;
5484 case ISN_LOADSCRIPT:
5485 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005486 scriptref_T *sref = iptr->isn_arg.script.scriptref;
5487 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
5488 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005489
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005490 sv = get_script_svar(sref, -1);
5491 if (sv == NULL)
5492 smsg("%s%4d LOADSCRIPT [deleted] from %s",
5493 pfx, current, si->sn_name);
5494 else
5495 smsg("%s%4d LOADSCRIPT %s-%d from %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02005496 sv->sv_name,
5497 sref->sref_idx,
5498 si->sn_name);
5499 }
5500 break;
5501 case ISN_LOADS:
5502 {
5503 scriptitem_T *si = SCRIPT_ITEM(
5504 iptr->isn_arg.loadstore.ls_sid);
5505
5506 smsg("%s%4d LOADS s:%s from %s", pfx, current,
5507 iptr->isn_arg.loadstore.ls_name, si->sn_name);
5508 }
5509 break;
5510 case ISN_LOADAUTO:
5511 smsg("%s%4d LOADAUTO %s", pfx, current, iptr->isn_arg.string);
5512 break;
5513 case ISN_LOADG:
5514 smsg("%s%4d LOADG g:%s", pfx, current, iptr->isn_arg.string);
5515 break;
5516 case ISN_LOADB:
5517 smsg("%s%4d LOADB b:%s", pfx, current, iptr->isn_arg.string);
5518 break;
5519 case ISN_LOADW:
5520 smsg("%s%4d LOADW w:%s", pfx, current, iptr->isn_arg.string);
5521 break;
5522 case ISN_LOADT:
5523 smsg("%s%4d LOADT t:%s", pfx, current, iptr->isn_arg.string);
5524 break;
5525 case ISN_LOADGDICT:
5526 smsg("%s%4d LOAD g:", pfx, current);
5527 break;
5528 case ISN_LOADBDICT:
5529 smsg("%s%4d LOAD b:", pfx, current);
5530 break;
5531 case ISN_LOADWDICT:
5532 smsg("%s%4d LOAD w:", pfx, current);
5533 break;
5534 case ISN_LOADTDICT:
5535 smsg("%s%4d LOAD t:", pfx, current);
5536 break;
5537 case ISN_LOADOPT:
5538 smsg("%s%4d LOADOPT %s", pfx, current, iptr->isn_arg.string);
5539 break;
5540 case ISN_LOADENV:
5541 smsg("%s%4d LOADENV %s", pfx, current, iptr->isn_arg.string);
5542 break;
5543 case ISN_LOADREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005544 smsg("%s%4d LOADREG @%c", pfx, current,
5545 (int)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005546 break;
5547
5548 case ISN_STORE:
5549 if (iptr->isn_arg.number < 0)
5550 smsg("%s%4d STORE arg[%lld]", pfx, current,
5551 iptr->isn_arg.number + STACK_FRAME_SIZE);
5552 else
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005553 smsg("%s%4d STORE $%lld", pfx, current,
5554 iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005555 break;
5556 case ISN_STOREOUTER:
5557 {
5558 if (iptr->isn_arg.number < 0)
5559 smsg("%s%4d STOREOUTEr level %d arg[%d]", pfx, current,
5560 iptr->isn_arg.outer.outer_depth,
5561 iptr->isn_arg.outer.outer_idx + STACK_FRAME_SIZE);
5562 else
5563 smsg("%s%4d STOREOUTER level %d $%d", pfx, current,
5564 iptr->isn_arg.outer.outer_depth,
5565 iptr->isn_arg.outer.outer_idx);
5566 }
5567 break;
5568 case ISN_STOREV:
5569 smsg("%s%4d STOREV v:%s", pfx, current,
5570 get_vim_var_name(iptr->isn_arg.number));
5571 break;
5572 case ISN_STOREAUTO:
5573 smsg("%s%4d STOREAUTO %s", pfx, current, iptr->isn_arg.string);
5574 break;
5575 case ISN_STOREG:
5576 smsg("%s%4d STOREG %s", pfx, current, iptr->isn_arg.string);
5577 break;
5578 case ISN_STOREB:
5579 smsg("%s%4d STOREB %s", pfx, current, iptr->isn_arg.string);
5580 break;
5581 case ISN_STOREW:
5582 smsg("%s%4d STOREW %s", pfx, current, iptr->isn_arg.string);
5583 break;
5584 case ISN_STORET:
5585 smsg("%s%4d STORET %s", pfx, current, iptr->isn_arg.string);
5586 break;
5587 case ISN_STORES:
5588 {
5589 scriptitem_T *si = SCRIPT_ITEM(
5590 iptr->isn_arg.loadstore.ls_sid);
5591
5592 smsg("%s%4d STORES %s in %s", pfx, current,
5593 iptr->isn_arg.loadstore.ls_name, si->sn_name);
5594 }
5595 break;
5596 case ISN_STORESCRIPT:
5597 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005598 scriptref_T *sref = iptr->isn_arg.script.scriptref;
5599 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
5600 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005601
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005602 sv = get_script_svar(sref, -1);
5603 if (sv == NULL)
5604 smsg("%s%4d STORESCRIPT [deleted] in %s",
5605 pfx, current, si->sn_name);
5606 else
5607 smsg("%s%4d STORESCRIPT %s-%d in %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02005608 sv->sv_name,
5609 sref->sref_idx,
5610 si->sn_name);
5611 }
5612 break;
5613 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00005614 case ISN_STOREFUNCOPT:
5615 smsg("%s%4d %s &%s", pfx, current,
5616 iptr->isn_type == ISN_STOREOPT ? "STOREOPT" : "STOREFUNCOPT",
Bram Moolenaar4c137212021-04-19 16:48:48 +02005617 iptr->isn_arg.storeopt.so_name);
5618 break;
5619 case ISN_STOREENV:
5620 smsg("%s%4d STOREENV $%s", pfx, current, iptr->isn_arg.string);
5621 break;
5622 case ISN_STOREREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005623 smsg("%s%4d STOREREG @%c", pfx, current,
5624 (int)iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005625 break;
5626 case ISN_STORENR:
5627 smsg("%s%4d STORE %lld in $%d", pfx, current,
5628 iptr->isn_arg.storenr.stnr_val,
5629 iptr->isn_arg.storenr.stnr_idx);
5630 break;
5631
5632 case ISN_STOREINDEX:
5633 smsg("%s%4d STOREINDEX %s", pfx, current,
5634 vartype_name(iptr->isn_arg.vartype));
5635 break;
5636
5637 case ISN_STORERANGE:
5638 smsg("%s%4d STORERANGE", pfx, current);
5639 break;
5640
5641 // constants
5642 case ISN_PUSHNR:
5643 smsg("%s%4d PUSHNR %lld", pfx, current,
5644 (varnumber_T)(iptr->isn_arg.number));
5645 break;
5646 case ISN_PUSHBOOL:
5647 case ISN_PUSHSPEC:
5648 smsg("%s%4d PUSH %s", pfx, current,
5649 get_var_special_name(iptr->isn_arg.number));
5650 break;
5651 case ISN_PUSHF:
5652#ifdef FEAT_FLOAT
5653 smsg("%s%4d PUSHF %g", pfx, current, iptr->isn_arg.fnumber);
5654#endif
5655 break;
5656 case ISN_PUSHS:
5657 smsg("%s%4d PUSHS \"%s\"", pfx, current, iptr->isn_arg.string);
5658 break;
5659 case ISN_PUSHBLOB:
5660 {
5661 char_u *r;
5662 char_u numbuf[NUMBUFLEN];
5663 char_u *tofree;
5664
5665 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
5666 smsg("%s%4d PUSHBLOB %s", pfx, current, r);
5667 vim_free(tofree);
5668 }
5669 break;
5670 case ISN_PUSHFUNC:
5671 {
5672 char *name = (char *)iptr->isn_arg.string;
5673
5674 smsg("%s%4d PUSHFUNC \"%s\"", pfx, current,
5675 name == NULL ? "[none]" : name);
5676 }
5677 break;
5678 case ISN_PUSHCHANNEL:
5679#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar397a87a2022-03-20 21:14:15 +00005680 smsg("%s%4d PUSHCHANNEL 0", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005681#endif
5682 break;
5683 case ISN_PUSHJOB:
5684#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar397a87a2022-03-20 21:14:15 +00005685 smsg("%s%4d PUSHJOB \"no process\"", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005686#endif
5687 break;
5688 case ISN_PUSHEXC:
5689 smsg("%s%4d PUSH v:exception", pfx, current);
5690 break;
Bram Moolenaar06b77222022-01-25 15:51:56 +00005691 case ISN_AUTOLOAD:
5692 smsg("%s%4d AUTOLOAD %s", pfx, current, iptr->isn_arg.string);
5693 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005694 case ISN_UNLET:
5695 smsg("%s%4d UNLET%s %s", pfx, current,
5696 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
5697 iptr->isn_arg.unlet.ul_name);
5698 break;
5699 case ISN_UNLETENV:
5700 smsg("%s%4d UNLETENV%s $%s", pfx, current,
5701 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
5702 iptr->isn_arg.unlet.ul_name);
5703 break;
5704 case ISN_UNLETINDEX:
5705 smsg("%s%4d UNLETINDEX", pfx, current);
5706 break;
5707 case ISN_UNLETRANGE:
5708 smsg("%s%4d UNLETRANGE", pfx, current);
5709 break;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02005710 case ISN_LOCKUNLOCK:
5711 smsg("%s%4d LOCKUNLOCK %s", pfx, current, iptr->isn_arg.string);
5712 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005713 case ISN_LOCKCONST:
5714 smsg("%s%4d LOCKCONST", pfx, current);
5715 break;
5716 case ISN_NEWLIST:
5717 smsg("%s%4d NEWLIST size %lld", pfx, current,
5718 (varnumber_T)(iptr->isn_arg.number));
5719 break;
5720 case ISN_NEWDICT:
5721 smsg("%s%4d NEWDICT size %lld", pfx, current,
5722 (varnumber_T)(iptr->isn_arg.number));
5723 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00005724 case ISN_NEWPARTIAL:
5725 smsg("%s%4d NEWPARTIAL", pfx, current);
5726 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005727
5728 // function call
5729 case ISN_BCALL:
5730 {
5731 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
5732
5733 smsg("%s%4d BCALL %s(argc %d)", pfx, current,
5734 internal_func_name(cbfunc->cbf_idx),
5735 cbfunc->cbf_argcount);
5736 }
5737 break;
5738 case ISN_DCALL:
5739 {
5740 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
5741 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
5742 + cdfunc->cdf_idx;
5743
5744 smsg("%s%4d DCALL %s(argc %d)", pfx, current,
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005745 printable_func_name(df->df_ufunc),
5746 cdfunc->cdf_argcount);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005747 }
5748 break;
5749 case ISN_UCALL:
5750 {
5751 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
5752
5753 smsg("%s%4d UCALL %s(argc %d)", pfx, current,
5754 cufunc->cuf_name, cufunc->cuf_argcount);
5755 }
5756 break;
5757 case ISN_PCALL:
5758 {
5759 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
5760
5761 smsg("%s%4d PCALL%s (argc %d)", pfx, current,
5762 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
5763 }
5764 break;
5765 case ISN_PCALL_END:
5766 smsg("%s%4d PCALL end", pfx, current);
5767 break;
5768 case ISN_RETURN:
5769 smsg("%s%4d RETURN", pfx, current);
5770 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02005771 case ISN_RETURN_VOID:
5772 smsg("%s%4d RETURN void", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005773 break;
5774 case ISN_FUNCREF:
5775 {
5776 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar38453522021-11-28 22:00:12 +00005777 char_u *name;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005778
Bram Moolenaar38453522021-11-28 22:00:12 +00005779 if (funcref->fr_func_name == NULL)
5780 {
5781 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
5782 + funcref->fr_dfunc_idx;
5783 name = df->df_ufunc->uf_name;
5784 }
5785 else
5786 name = funcref->fr_func_name;
5787 smsg("%s%4d FUNCREF %s", pfx, current, name);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005788 }
5789 break;
5790
5791 case ISN_NEWFUNC:
5792 {
5793 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
5794
5795 smsg("%s%4d NEWFUNC %s %s", pfx, current,
5796 newfunc->nf_lambda, newfunc->nf_global);
5797 }
5798 break;
5799
5800 case ISN_DEF:
5801 {
5802 char_u *name = iptr->isn_arg.string;
5803
5804 smsg("%s%4d DEF %s", pfx, current,
5805 name == NULL ? (char_u *)"" : name);
5806 }
5807 break;
5808
5809 case ISN_JUMP:
5810 {
5811 char *when = "?";
5812
5813 switch (iptr->isn_arg.jump.jump_when)
5814 {
5815 case JUMP_ALWAYS:
5816 when = "JUMP";
5817 break;
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005818 case JUMP_NEVER:
5819 iemsg("JUMP_NEVER should not be used");
5820 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005821 case JUMP_AND_KEEP_IF_TRUE:
5822 when = "JUMP_AND_KEEP_IF_TRUE";
5823 break;
5824 case JUMP_IF_FALSE:
5825 when = "JUMP_IF_FALSE";
5826 break;
5827 case JUMP_AND_KEEP_IF_FALSE:
5828 when = "JUMP_AND_KEEP_IF_FALSE";
5829 break;
5830 case JUMP_IF_COND_FALSE:
5831 when = "JUMP_IF_COND_FALSE";
5832 break;
5833 case JUMP_IF_COND_TRUE:
5834 when = "JUMP_IF_COND_TRUE";
5835 break;
5836 }
5837 smsg("%s%4d %s -> %d", pfx, current, when,
5838 iptr->isn_arg.jump.jump_where);
5839 }
5840 break;
5841
5842 case ISN_JUMP_IF_ARG_SET:
5843 smsg("%s%4d JUMP_IF_ARG_SET arg[%d] -> %d", pfx, current,
5844 iptr->isn_arg.jumparg.jump_arg_off + STACK_FRAME_SIZE,
5845 iptr->isn_arg.jump.jump_where);
5846 break;
5847
5848 case ISN_FOR:
5849 {
5850 forloop_T *forloop = &iptr->isn_arg.forloop;
5851
5852 smsg("%s%4d FOR $%d -> %d", pfx, current,
5853 forloop->for_idx, forloop->for_end);
5854 }
5855 break;
5856
5857 case ISN_TRY:
5858 {
Bram Moolenaar0d807102021-12-21 09:42:09 +00005859 try_T *try = &iptr->isn_arg.tryref;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005860
5861 if (try->try_ref->try_finally == 0)
5862 smsg("%s%4d TRY catch -> %d, endtry -> %d",
5863 pfx, current,
5864 try->try_ref->try_catch,
5865 try->try_ref->try_endtry);
5866 else
5867 smsg("%s%4d TRY catch -> %d, finally -> %d, endtry -> %d",
5868 pfx, current,
5869 try->try_ref->try_catch,
5870 try->try_ref->try_finally,
5871 try->try_ref->try_endtry);
5872 }
5873 break;
5874 case ISN_CATCH:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005875 smsg("%s%4d CATCH", pfx, current);
5876 break;
5877 case ISN_TRYCONT:
5878 {
5879 trycont_T *trycont = &iptr->isn_arg.trycont;
5880
5881 smsg("%s%4d TRY-CONTINUE %d level%s -> %d", pfx, current,
5882 trycont->tct_levels,
5883 trycont->tct_levels == 1 ? "" : "s",
5884 trycont->tct_where);
5885 }
5886 break;
5887 case ISN_FINALLY:
5888 smsg("%s%4d FINALLY", pfx, current);
5889 break;
5890 case ISN_ENDTRY:
5891 smsg("%s%4d ENDTRY", pfx, current);
5892 break;
5893 case ISN_THROW:
5894 smsg("%s%4d THROW", pfx, current);
5895 break;
5896
5897 // expression operations on number
5898 case ISN_OPNR:
5899 case ISN_OPFLOAT:
5900 case ISN_OPANY:
5901 {
5902 char *what;
5903 char *ins;
5904
5905 switch (iptr->isn_arg.op.op_type)
5906 {
5907 case EXPR_MULT: what = "*"; break;
5908 case EXPR_DIV: what = "/"; break;
5909 case EXPR_REM: what = "%"; break;
5910 case EXPR_SUB: what = "-"; break;
5911 case EXPR_ADD: what = "+"; break;
5912 default: what = "???"; break;
5913 }
5914 switch (iptr->isn_type)
5915 {
5916 case ISN_OPNR: ins = "OPNR"; break;
5917 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
5918 case ISN_OPANY: ins = "OPANY"; break;
5919 default: ins = "???"; break;
5920 }
5921 smsg("%s%4d %s %s", pfx, current, ins, what);
5922 }
5923 break;
5924
5925 case ISN_COMPAREBOOL:
5926 case ISN_COMPARESPECIAL:
Bram Moolenaar7a222242022-03-01 19:23:24 +00005927 case ISN_COMPARENULL:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005928 case ISN_COMPARENR:
5929 case ISN_COMPAREFLOAT:
5930 case ISN_COMPARESTRING:
5931 case ISN_COMPAREBLOB:
5932 case ISN_COMPARELIST:
5933 case ISN_COMPAREDICT:
5934 case ISN_COMPAREFUNC:
5935 case ISN_COMPAREANY:
5936 {
5937 char *p;
5938 char buf[10];
5939 char *type;
5940
5941 switch (iptr->isn_arg.op.op_type)
5942 {
5943 case EXPR_EQUAL: p = "=="; break;
5944 case EXPR_NEQUAL: p = "!="; break;
5945 case EXPR_GREATER: p = ">"; break;
5946 case EXPR_GEQUAL: p = ">="; break;
5947 case EXPR_SMALLER: p = "<"; break;
5948 case EXPR_SEQUAL: p = "<="; break;
5949 case EXPR_MATCH: p = "=~"; break;
5950 case EXPR_IS: p = "is"; break;
5951 case EXPR_ISNOT: p = "isnot"; break;
5952 case EXPR_NOMATCH: p = "!~"; break;
5953 default: p = "???"; break;
5954 }
5955 STRCPY(buf, p);
5956 if (iptr->isn_arg.op.op_ic == TRUE)
5957 strcat(buf, "?");
5958 switch(iptr->isn_type)
5959 {
5960 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
5961 case ISN_COMPARESPECIAL:
5962 type = "COMPARESPECIAL"; break;
Bram Moolenaar7a222242022-03-01 19:23:24 +00005963 case ISN_COMPARENULL: type = "COMPARENULL"; break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005964 case ISN_COMPARENR: type = "COMPARENR"; break;
5965 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
5966 case ISN_COMPARESTRING:
5967 type = "COMPARESTRING"; break;
5968 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
5969 case ISN_COMPARELIST: type = "COMPARELIST"; break;
5970 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
5971 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
5972 case ISN_COMPAREANY: type = "COMPAREANY"; break;
5973 default: type = "???"; break;
5974 }
5975
5976 smsg("%s%4d %s %s", pfx, current, type, buf);
5977 }
5978 break;
5979
5980 case ISN_ADDLIST: smsg("%s%4d ADDLIST", pfx, current); break;
5981 case ISN_ADDBLOB: smsg("%s%4d ADDBLOB", pfx, current); break;
5982
5983 // expression operations
5984 case ISN_CONCAT: smsg("%s%4d CONCAT", pfx, current); break;
5985 case ISN_STRINDEX: smsg("%s%4d STRINDEX", pfx, current); break;
5986 case ISN_STRSLICE: smsg("%s%4d STRSLICE", pfx, current); break;
5987 case ISN_BLOBINDEX: smsg("%s%4d BLOBINDEX", pfx, current); break;
5988 case ISN_BLOBSLICE: smsg("%s%4d BLOBSLICE", pfx, current); break;
5989 case ISN_LISTAPPEND: smsg("%s%4d LISTAPPEND", pfx, current); break;
5990 case ISN_BLOBAPPEND: smsg("%s%4d BLOBAPPEND", pfx, current); break;
5991 case ISN_LISTINDEX: smsg("%s%4d LISTINDEX", pfx, current); break;
5992 case ISN_LISTSLICE: smsg("%s%4d LISTSLICE", pfx, current); break;
5993 case ISN_ANYINDEX: smsg("%s%4d ANYINDEX", pfx, current); break;
5994 case ISN_ANYSLICE: smsg("%s%4d ANYSLICE", pfx, current); break;
5995 case ISN_SLICE: smsg("%s%4d SLICE %lld",
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02005996 pfx, current, iptr->isn_arg.number); break;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02005997 case ISN_GETITEM: smsg("%s%4d ITEM %lld%s", pfx, current,
5998 iptr->isn_arg.getitem.gi_index,
5999 iptr->isn_arg.getitem.gi_with_op ?
6000 " with op" : ""); break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006001 case ISN_MEMBER: smsg("%s%4d MEMBER", pfx, current); break;
6002 case ISN_STRINGMEMBER: smsg("%s%4d MEMBER %s", pfx, current,
6003 iptr->isn_arg.string); break;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02006004 case ISN_CLEARDICT: smsg("%s%4d CLEARDICT", pfx, current); break;
6005 case ISN_USEDICT: smsg("%s%4d USEDICT", pfx, current); break;
6006
Bram Moolenaar4c137212021-04-19 16:48:48 +02006007 case ISN_NEGATENR: smsg("%s%4d NEGATENR", pfx, current); break;
6008
6009 case ISN_CHECKNR: smsg("%s%4d CHECKNR", pfx, current); break;
6010 case ISN_CHECKTYPE:
6011 {
6012 checktype_T *ct = &iptr->isn_arg.type;
6013 char *tofree;
6014
6015 if (ct->ct_arg_idx == 0)
6016 smsg("%s%4d CHECKTYPE %s stack[%d]", pfx, current,
6017 type_name(ct->ct_type, &tofree),
6018 (int)ct->ct_off);
6019 else
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02006020 smsg("%s%4d CHECKTYPE %s stack[%d] arg %d",
6021 pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02006022 type_name(ct->ct_type, &tofree),
6023 (int)ct->ct_off,
6024 (int)ct->ct_arg_idx);
6025 vim_free(tofree);
6026 break;
6027 }
6028 case ISN_CHECKLEN: smsg("%s%4d CHECKLEN %s%d", pfx, current,
6029 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
6030 iptr->isn_arg.checklen.cl_min_len);
6031 break;
6032 case ISN_SETTYPE:
6033 {
6034 char *tofree;
6035
6036 smsg("%s%4d SETTYPE %s", pfx, current,
6037 type_name(iptr->isn_arg.type.ct_type, &tofree));
6038 vim_free(tofree);
6039 break;
6040 }
6041 case ISN_COND2BOOL: smsg("%s%4d COND2BOOL", pfx, current); break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006042 case ISN_2BOOL: if (iptr->isn_arg.tobool.invert)
6043 smsg("%s%4d INVERT %d (!val)", pfx, current,
6044 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006045 else
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006046 smsg("%s%4d 2BOOL %d (!!val)", pfx, current,
6047 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006048 break;
6049 case ISN_2STRING: smsg("%s%4d 2STRING stack[%lld]", pfx, current,
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006050 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006051 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006052 case ISN_2STRING_ANY: smsg("%s%4d 2STRING_ANY stack[%lld]",
6053 pfx, current,
6054 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006055 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006056 case ISN_RANGE: smsg("%s%4d RANGE %s", pfx, current,
6057 iptr->isn_arg.string);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006058 break;
6059 case ISN_PUT:
6060 if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE)
6061 smsg("%s%4d PUT %c above range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006062 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006063 else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE)
6064 smsg("%s%4d PUT %c range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006065 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006066 else
6067 smsg("%s%4d PUT %c %ld", pfx, current,
6068 iptr->isn_arg.put.put_regname,
6069 (long)iptr->isn_arg.put.put_lnum);
6070 break;
6071
Bram Moolenaar4c137212021-04-19 16:48:48 +02006072 case ISN_CMDMOD:
6073 {
6074 char_u *buf;
6075 size_t len = produce_cmdmods(
6076 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
6077
6078 buf = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02006079 if (likely(buf != NULL))
Bram Moolenaar4c137212021-04-19 16:48:48 +02006080 {
6081 (void)produce_cmdmods(
6082 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
6083 smsg("%s%4d CMDMOD %s", pfx, current, buf);
6084 vim_free(buf);
6085 }
6086 break;
6087 }
6088 case ISN_CMDMOD_REV: smsg("%s%4d CMDMOD_REV", pfx, current); break;
6089
6090 case ISN_PROF_START:
Bram Moolenaare99d4222021-06-13 14:01:26 +02006091 smsg("%s%4d PROFILE START line %d", pfx, current,
6092 iptr->isn_lnum);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006093 break;
6094
6095 case ISN_PROF_END:
6096 smsg("%s%4d PROFILE END", pfx, current);
6097 break;
6098
Bram Moolenaare99d4222021-06-13 14:01:26 +02006099 case ISN_DEBUG:
Bram Moolenaar8cec9272021-06-23 20:20:53 +02006100 smsg("%s%4d DEBUG line %d-%d varcount %lld", pfx, current,
6101 iptr->isn_arg.debug.dbg_break_lnum + 1,
6102 iptr->isn_lnum,
6103 iptr->isn_arg.debug.dbg_var_names_len);
Bram Moolenaare99d4222021-06-13 14:01:26 +02006104 break;
6105
Bram Moolenaar4c137212021-04-19 16:48:48 +02006106 case ISN_UNPACK: smsg("%s%4d UNPACK %d%s", pfx, current,
6107 iptr->isn_arg.unpack.unp_count,
6108 iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : "");
6109 break;
6110 case ISN_SHUFFLE: smsg("%s%4d SHUFFLE %d up %d", pfx, current,
6111 iptr->isn_arg.shuffle.shfl_item,
6112 iptr->isn_arg.shuffle.shfl_up);
6113 break;
6114 case ISN_DROP: smsg("%s%4d DROP", pfx, current); break;
6115
6116 case ISN_FINISH: // End of list of instructions for ISN_SUBSTITUTE.
6117 return;
6118 }
6119
6120 out_flush(); // output one line at a time
6121 ui_breakcheck();
6122 if (got_int)
6123 break;
6124 }
6125}
6126
6127/*
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02006128 * Handle command line completion for the :disassemble command.
6129 */
6130 void
6131set_context_in_disassemble_cmd(expand_T *xp, char_u *arg)
6132{
6133 char_u *p;
6134
6135 // Default: expand user functions, "debug" and "profile"
6136 xp->xp_context = EXPAND_DISASSEMBLE;
6137 xp->xp_pattern = arg;
6138
6139 // first argument already typed: only user function names
6140 if (*arg != NUL && *(p = skiptowhite(arg)) != NUL)
6141 {
6142 xp->xp_context = EXPAND_USER_FUNC;
6143 xp->xp_pattern = skipwhite(p);
6144 }
6145}
6146
6147/*
6148 * Function given to ExpandGeneric() to obtain the list of :disassemble
6149 * arguments.
6150 */
6151 char_u *
6152get_disassemble_argument(expand_T *xp, int idx)
6153{
6154 if (idx == 0)
6155 return (char_u *)"debug";
6156 if (idx == 1)
6157 return (char_u *)"profile";
6158 return get_user_func_name(xp, idx - 2);
6159}
6160
6161/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01006162 * ":disassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01006163 * We don't really need this at runtime, but we do have tests that require it,
6164 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006165 */
6166 void
6167ex_disassemble(exarg_T *eap)
6168{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01006169 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01006170 char_u *fname;
6171 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006172 dfunc_T *dfunc;
6173 isn_T *instr;
Bram Moolenaarb2049902021-01-24 12:53:53 +01006174 int instr_count;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02006175 int is_global = FALSE;
Bram Moolenaare99d4222021-06-13 14:01:26 +02006176 compiletype_T compile_type = CT_NONE;
6177
Bram Moolenaarc7d5fc82021-12-05 17:20:24 +00006178 if (STRNCMP(arg, "profile", 7) == 0 && VIM_ISWHITE(arg[7]))
Bram Moolenaare99d4222021-06-13 14:01:26 +02006179 {
6180 compile_type = CT_PROFILE;
6181 arg = skipwhite(arg + 7);
6182 }
Bram Moolenaarc7d5fc82021-12-05 17:20:24 +00006183 else if (STRNCMP(arg, "debug", 5) == 0 && VIM_ISWHITE(arg[5]))
Bram Moolenaare99d4222021-06-13 14:01:26 +02006184 {
6185 compile_type = CT_DEBUG;
6186 arg = skipwhite(arg + 5);
6187 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006188
Bram Moolenaarbfd65582020-07-13 18:18:00 +02006189 if (STRNCMP(arg, "<lambda>", 8) == 0)
6190 {
6191 arg += 8;
6192 (void)getdigits(&arg);
6193 fname = vim_strnsave(eap->arg, arg - eap->arg);
6194 }
6195 else
6196 fname = trans_function_name(&arg, &is_global, FALSE,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01006197 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL, NULL);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01006198 if (fname == NULL)
6199 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006200 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01006201 return;
6202 }
6203
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00006204 ufunc = find_func(fname, is_global);
Bram Moolenaara26b9702020-04-18 19:53:28 +02006205 if (ufunc == NULL)
6206 {
6207 char_u *p = untrans_function_name(fname);
6208
6209 if (p != NULL)
6210 // Try again without making it script-local.
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00006211 ufunc = find_func(p, FALSE);
Bram Moolenaara26b9702020-04-18 19:53:28 +02006212 }
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01006213 vim_free(fname);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006214 if (ufunc == NULL)
6215 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006216 semsg(_(e_cannot_find_function_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006217 return;
6218 }
Bram Moolenaare99d4222021-06-13 14:01:26 +02006219 if (func_needs_compiling(ufunc, compile_type)
6220 && compile_def_function(ufunc, FALSE, compile_type, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02006221 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006222 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006223 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006224 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006225 return;
6226 }
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006227 msg((char *)printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006228
6229 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
Bram Moolenaare99d4222021-06-13 14:01:26 +02006230 switch (compile_type)
6231 {
6232 case CT_PROFILE:
Bram Moolenaarf002a412021-01-24 13:34:18 +01006233#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02006234 instr = dfunc->df_instr_prof;
6235 instr_count = dfunc->df_instr_prof_count;
6236 break;
Bram Moolenaarf002a412021-01-24 13:34:18 +01006237#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02006238 // FALLTHROUGH
6239 case CT_NONE:
6240 instr = dfunc->df_instr;
6241 instr_count = dfunc->df_instr_count;
6242 break;
6243 case CT_DEBUG:
6244 instr = dfunc->df_instr_debug;
6245 instr_count = dfunc->df_instr_debug_count;
6246 break;
6247 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006248
Bram Moolenaar4c137212021-04-19 16:48:48 +02006249 list_instructions("", instr, instr_count, ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006250}
6251
6252/*
Bram Moolenaar13106602020-10-04 16:06:05 +02006253 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006254 * list, etc. Mostly like what JavaScript does, except that empty list and
6255 * empty dictionary are FALSE.
6256 */
6257 int
6258tv2bool(typval_T *tv)
6259{
6260 switch (tv->v_type)
6261 {
6262 case VAR_NUMBER:
6263 return tv->vval.v_number != 0;
6264 case VAR_FLOAT:
6265#ifdef FEAT_FLOAT
6266 return tv->vval.v_float != 0.0;
6267#else
6268 break;
6269#endif
6270 case VAR_PARTIAL:
6271 return tv->vval.v_partial != NULL;
6272 case VAR_FUNC:
6273 case VAR_STRING:
6274 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
6275 case VAR_LIST:
6276 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
6277 case VAR_DICT:
6278 return tv->vval.v_dict != NULL
6279 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
6280 case VAR_BOOL:
6281 case VAR_SPECIAL:
6282 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
6283 case VAR_JOB:
6284#ifdef FEAT_JOB_CHANNEL
6285 return tv->vval.v_job != NULL;
6286#else
6287 break;
6288#endif
6289 case VAR_CHANNEL:
6290#ifdef FEAT_JOB_CHANNEL
6291 return tv->vval.v_channel != NULL;
6292#else
6293 break;
6294#endif
6295 case VAR_BLOB:
6296 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
6297 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006298 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006299 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006300 case VAR_INSTR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006301 break;
6302 }
6303 return FALSE;
6304}
6305
Bram Moolenaarea2d4072020-11-12 12:08:51 +01006306 void
6307emsg_using_string_as(typval_T *tv, int as_number)
6308{
6309 semsg(_(as_number ? e_using_string_as_number_str
6310 : e_using_string_as_bool_str),
6311 tv->vval.v_string == NULL
6312 ? (char_u *)"" : tv->vval.v_string);
6313}
6314
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006315/*
6316 * If "tv" is a string give an error and return FAIL.
6317 */
6318 int
6319check_not_string(typval_T *tv)
6320{
6321 if (tv->v_type == VAR_STRING)
6322 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +01006323 emsg_using_string_as(tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006324 clear_tv(tv);
6325 return FAIL;
6326 }
6327 return OK;
6328}
6329
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006330
6331#endif // FEAT_EVAL