blob: 7ff2eaf09ae794210a8e1ee7aad9676226ab245b [file] [log] [blame]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * vim9execute.c: execute Vim9 script instructions
12 */
13
14#define USING_FLOAT_STUFF
15#include "vim.h"
16
17#if defined(FEAT_EVAL) || defined(PROTO)
18
Bram Moolenaardc7c3662021-12-20 15:04:29 +000019// When not generating protos this is included in proto.h
20#ifdef PROTO
21# include "vim9.h"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010022#endif
23
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010024
25// Structure put on ec_trystack when ISN_TRY is encountered.
26typedef struct {
Bram Moolenaard9d77892021-02-12 21:32:47 +010027 int tcd_frame_idx; // ec_frame_idx at ISN_TRY
28 int tcd_stack_len; // size of ectx.ec_stack at ISN_TRY
Bram Moolenaard3d8fee2021-06-30 19:54:43 +020029 int tcd_in_catch; // in catch or finally block
30 int tcd_did_throw; // set did_throw in :endtry
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +010031 int tcd_catch_idx; // instruction of the first :catch or :finally
32 int tcd_finally_idx; // instruction of the :finally block or zero
33 int tcd_endtry_idx; // instruction of the :endtry
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010034 int tcd_caught; // catch block entered
Bram Moolenaar2e34c342021-03-14 12:13:33 +010035 int tcd_cont; // :continue encountered, jump here (minus one)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010036 int tcd_return; // when TRUE return from end of :finally
37} trycmd_T;
38
Bram Moolenaar4c137212021-04-19 16:48:48 +020039// Data local to a function.
40// On a function call, if not empty, is saved on the stack and restored when
41// returning.
42typedef struct {
43 int floc_restore_cmdmod;
44 cmdmod_T floc_save_cmdmod;
45 int floc_restore_cmdmod_stacklen;
46} funclocal_T;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010047
Bram Moolenaarc04f2a42021-06-09 19:30:03 +020048// Structure to hold a reference to an outer_T, with information of whether it
49// was allocated.
50typedef struct {
51 outer_T *or_outer;
52 partial_T *or_partial; // decrement "or_partial->pt_refcount" later
53 int or_outer_allocated; // free "or_outer" later
54} outer_ref_T;
55
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010056// A stack is used to store:
57// - arguments passed to a :def function
58// - info about the calling function, to use when returning
59// - local variables
60// - temporary values
61//
62// In detail (FP == Frame Pointer):
63// arg1 first argument from caller (if present)
64// arg2 second argument from caller (if present)
65// extra_arg1 any missing optional argument default value
66// FP -> cur_func calling function
67// current previous instruction pointer
68// frame_ptr previous Frame Pointer
69// var1 space for local variable
70// var2 space for local variable
71// .... fixed space for max. number of local variables
72// temp temporary values
73// .... flexible space for temporary values (can grow big)
74
75/*
76 * Execution context.
77 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010078struct ectx_S {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010079 garray_T ec_stack; // stack of typval_T values
Bram Moolenaarbf67ea12020-05-02 17:52:42 +020080 int ec_frame_idx; // index in ec_stack: context of ec_dfunc_idx
Bram Moolenaar4c137212021-04-19 16:48:48 +020081 int ec_initial_frame_idx; // frame index when called
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010082
Bram Moolenaarc04f2a42021-06-09 19:30:03 +020083 outer_ref_T *ec_outer_ref; // outer scope used for closures, allocated
Bram Moolenaar4c137212021-04-19 16:48:48 +020084 funclocal_T ec_funclocal;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +020085
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010086 garray_T ec_trystack; // stack of trycmd_T values
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010087
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010088 isn_T *ec_instr; // array with instructions
Dominique Pelle3276f582021-08-07 12:44:41 +020089 int ec_dfunc_idx; // current function index
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010090 int ec_iidx; // index in ec_instr: instruction to execute
Bram Moolenaar148ce7a2020-09-23 21:57:23 +020091
92 garray_T ec_funcrefs; // partials that might be a closure
Bram Moolenaar4c137212021-04-19 16:48:48 +020093
94 int ec_did_emsg_before;
95 int ec_trylevel_at_start;
96 where_T ec_where;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010097};
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010098
Bram Moolenaar12d26532021-02-19 19:13:21 +010099#ifdef FEAT_PROFILE
100// stack of profinfo_T used when profiling.
101static garray_T profile_info_ga = {0, 0, sizeof(profinfo_T), 20, NULL};
102#endif
103
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100104// Get pointer to item relative to the bottom of the stack, -1 is the last one.
Bram Moolenaar11107ba2020-08-15 21:10:16 +0200105#define STACK_TV_BOT(idx) (((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_stack.ga_len + (idx))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100106
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200107 void
108to_string_error(vartype_T vartype)
109{
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200110 semsg(_(e_cannot_convert_str_to_string), vartype_name(vartype));
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200111}
112
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100113/*
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100114 * Return the number of arguments, including optional arguments and any vararg.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100115 */
116 static int
117ufunc_argcount(ufunc_T *ufunc)
118{
119 return ufunc->uf_args.ga_len + (ufunc->uf_va_name != NULL ? 1 : 0);
120}
121
122/*
Bram Moolenaarfe270812020-04-11 22:31:27 +0200123 * Create a new list from "count" items at the bottom of the stack.
124 * When "count" is zero an empty list is added to the stack.
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100125 * When "count" is -1 a NULL list is added to the stack.
Bram Moolenaarfe270812020-04-11 22:31:27 +0200126 */
127 static int
128exe_newlist(int count, ectx_T *ectx)
129{
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100130 list_T *list = NULL;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200131 int idx;
132 typval_T *tv;
133
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100134 if (count >= 0)
135 {
136 list = list_alloc_with_items(count);
137 if (list == NULL)
138 return FAIL;
139 for (idx = 0; idx < count; ++idx)
140 list_set_item(list, idx, STACK_TV_BOT(idx - count));
141 }
Bram Moolenaarfe270812020-04-11 22:31:27 +0200142
143 if (count > 0)
144 ectx->ec_stack.ga_len -= count - 1;
Bram Moolenaar35578162021-08-02 19:10:38 +0200145 else if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100146 {
147 list_unref(list);
Bram Moolenaarfe270812020-04-11 22:31:27 +0200148 return FAIL;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100149 }
Bram Moolenaarfe270812020-04-11 22:31:27 +0200150 else
151 ++ectx->ec_stack.ga_len;
152 tv = STACK_TV_BOT(-1);
153 tv->v_type = VAR_LIST;
154 tv->vval.v_list = list;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100155 if (list != NULL)
156 ++list->lv_refcount;
157 return OK;
158}
159
160/*
161 * Implementation of ISN_NEWDICT.
162 * Returns FAIL on total failure, MAYBE on error.
163 */
164 static int
165exe_newdict(int count, ectx_T *ectx)
166{
167 dict_T *dict = NULL;
168 dictitem_T *item;
169 char_u *key;
170 int idx;
171 typval_T *tv;
172
173 if (count >= 0)
174 {
175 dict = dict_alloc();
176 if (unlikely(dict == NULL))
177 return FAIL;
178 for (idx = 0; idx < count; ++idx)
179 {
180 // have already checked key type is VAR_STRING
181 tv = STACK_TV_BOT(2 * (idx - count));
182 // check key is unique
183 key = tv->vval.v_string == NULL
184 ? (char_u *)"" : tv->vval.v_string;
185 item = dict_find(dict, key, -1);
186 if (item != NULL)
187 {
188 semsg(_(e_duplicate_key_in_dicitonary), key);
189 dict_unref(dict);
190 return MAYBE;
191 }
192 item = dictitem_alloc(key);
193 clear_tv(tv);
194 if (unlikely(item == NULL))
195 {
196 dict_unref(dict);
197 return FAIL;
198 }
199 item->di_tv = *STACK_TV_BOT(2 * (idx - count) + 1);
200 item->di_tv.v_lock = 0;
201 if (dict_add(dict, item) == FAIL)
202 {
203 // can this ever happen?
204 dict_unref(dict);
205 return FAIL;
206 }
207 }
208 }
209
210 if (count > 0)
211 ectx->ec_stack.ga_len -= 2 * count - 1;
212 else if (GA_GROW_FAILS(&ectx->ec_stack, 1))
213 return FAIL;
214 else
215 ++ectx->ec_stack.ga_len;
216 tv = STACK_TV_BOT(-1);
217 tv->v_type = VAR_DICT;
218 tv->v_lock = 0;
219 tv->vval.v_dict = dict;
220 if (dict != NULL)
221 ++dict->dv_refcount;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200222 return OK;
223}
224
225/*
Bram Moolenaar4f8f5422021-06-20 19:28:14 +0200226 * If debug_tick changed check if "ufunc" has a breakpoint and update
227 * "uf_has_breakpoint".
228 */
Bram Moolenaar96923b72022-03-15 15:57:04 +0000229 void
Bram Moolenaar4f8f5422021-06-20 19:28:14 +0200230update_has_breakpoint(ufunc_T *ufunc)
231{
232 if (ufunc->uf_debug_tick != debug_tick)
233 {
234 linenr_T breakpoint;
235
236 ufunc->uf_debug_tick = debug_tick;
237 breakpoint = dbg_find_breakpoint(FALSE, ufunc->uf_name, 0);
238 ufunc->uf_has_breakpoint = breakpoint > 0;
239 }
240}
241
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +0200242static garray_T dict_stack = GA_EMPTY;
243
244/*
245 * Put a value on the dict stack. This consumes "tv".
246 */
247 static int
248dict_stack_save(typval_T *tv)
249{
250 if (dict_stack.ga_growsize == 0)
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000251 ga_init2(&dict_stack, sizeof(typval_T), 10);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +0200252 if (ga_grow(&dict_stack, 1) == FAIL)
253 return FAIL;
254 ((typval_T *)dict_stack.ga_data)[dict_stack.ga_len] = *tv;
255 ++dict_stack.ga_len;
256 return OK;
257}
258
259/*
260 * Get the typval at top of the dict stack.
261 */
262 static typval_T *
263dict_stack_get_tv(void)
264{
265 if (dict_stack.ga_len == 0)
266 return NULL;
267 return ((typval_T *)dict_stack.ga_data) + dict_stack.ga_len - 1;
268}
269
270/*
271 * Get the dict at top of the dict stack.
272 */
273 static dict_T *
274dict_stack_get_dict(void)
275{
276 typval_T *tv;
277
278 if (dict_stack.ga_len == 0)
279 return NULL;
280 tv = ((typval_T *)dict_stack.ga_data) + dict_stack.ga_len - 1;
281 if (tv->v_type == VAR_DICT)
282 return tv->vval.v_dict;
283 return NULL;
284}
285
286/*
287 * Drop an item from the dict stack.
288 */
289 static void
290dict_stack_drop(void)
291{
292 if (dict_stack.ga_len == 0)
293 {
294 iemsg("Dict stack underflow");
295 return;
296 }
297 --dict_stack.ga_len;
298 clear_tv(((typval_T *)dict_stack.ga_data) + dict_stack.ga_len);
299}
300
301/*
302 * Drop items from the dict stack until the length is equal to "len".
303 */
304 static void
305dict_stack_clear(int len)
306{
307 while (dict_stack.ga_len > len)
308 dict_stack_drop();
309}
310
Bram Moolenaar4f8f5422021-06-20 19:28:14 +0200311/*
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +0000312 * Get a pointer to useful "pt_outer" of "pt".
313 */
314 static outer_T *
315get_pt_outer(partial_T *pt)
316{
317 partial_T *ptref = pt->pt_outer_partial;
318
319 if (ptref == NULL)
320 return &pt->pt_outer;
321
322 // partial using partial (recursively)
323 while (ptref->pt_outer_partial != NULL)
324 ptref = ptref->pt_outer_partial;
325 return &ptref->pt_outer;
326}
327
328/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100329 * Call compiled function "cdf_idx" from compiled code.
Bram Moolenaarab360522021-01-10 14:02:28 +0100330 * This adds a stack frame and sets the instruction pointer to the start of the
331 * called function.
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200332 * If "pt" is not null use "pt->pt_outer" for ec_outer_ref->or_outer.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100333 *
334 * Stack has:
335 * - current arguments (already there)
336 * - omitted optional argument (default values) added here
337 * - stack frame:
338 * - pointer to calling function
339 * - Index of next instruction in calling function
340 * - previous frame pointer
341 * - reserved space for local variables
342 */
343 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100344call_dfunc(
345 int cdf_idx,
346 partial_T *pt,
347 int argcount_arg,
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100348 ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100349{
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100350 int argcount = argcount_arg;
351 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + cdf_idx;
352 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200353 int did_emsg_before = did_emsg_cumul + did_emsg;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100354 int arg_to_add;
355 int vararg_count = 0;
356 int varcount;
357 int idx;
358 estack_T *entry;
359 funclocal_T *floc = NULL;
Bram Moolenaar648594e2021-07-11 17:55:01 +0200360 int res = OK;
Bram Moolenaar139575d2022-03-15 19:29:30 +0000361 compiletype_T compile_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100362
363 if (dfunc->df_deleted)
364 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100365 // don't use ufunc->uf_name, it may have been freed
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000366 emsg_funcname(e_function_was_deleted_str,
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100367 dfunc->df_name == NULL ? (char_u *)"unknown" : dfunc->df_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100368 return FAIL;
369 }
370
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100371#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +0100372 if (do_profiling == PROF_YES)
373 {
Bram Moolenaar35578162021-08-02 19:10:38 +0200374 if (GA_GROW_OK(&profile_info_ga, 1))
Bram Moolenaar12d26532021-02-19 19:13:21 +0100375 {
376 profinfo_T *info = ((profinfo_T *)profile_info_ga.ga_data)
377 + profile_info_ga.ga_len;
378 ++profile_info_ga.ga_len;
379 CLEAR_POINTER(info);
380 profile_may_start_func(info, ufunc,
381 (((dfunc_T *)def_functions.ga_data)
382 + ectx->ec_dfunc_idx)->df_ufunc);
383 }
Bram Moolenaar12d26532021-02-19 19:13:21 +0100384 }
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100385#endif
386
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200387 // When debugging and using "cont" switches to the not-debugged
388 // instructions, may need to still compile them.
Bram Moolenaar139575d2022-03-15 19:29:30 +0000389 compile_type = get_compile_type(ufunc);
390 if (func_needs_compiling(ufunc, compile_type))
Bram Moolenaar648594e2021-07-11 17:55:01 +0200391 {
Bram Moolenaar139575d2022-03-15 19:29:30 +0000392 res = compile_def_function(ufunc, FALSE, compile_type, NULL);
Bram Moolenaar648594e2021-07-11 17:55:01 +0200393
394 // compile_def_function() may cause def_functions.ga_data to change
395 dfunc = ((dfunc_T *)def_functions.ga_data) + cdf_idx;
396 }
397 if (res == FAIL || INSTRUCTIONS(dfunc) == NULL)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200398 {
399 if (did_emsg_cumul + did_emsg == did_emsg_before)
400 semsg(_(e_function_is_not_compiled_str),
401 printable_func_name(ufunc));
402 return FAIL;
403 }
404
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200405 if (ufunc->uf_va_name != NULL)
406 {
Bram Moolenaarfe270812020-04-11 22:31:27 +0200407 // Need to make a list out of the vararg arguments.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200408 // Stack at time of call with 2 varargs:
409 // normal_arg
410 // optional_arg
411 // vararg_1
412 // vararg_2
Bram Moolenaarfe270812020-04-11 22:31:27 +0200413 // After creating the list:
414 // normal_arg
415 // optional_arg
416 // vararg-list
417 // With missing optional arguments we get:
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200418 // normal_arg
Bram Moolenaarfe270812020-04-11 22:31:27 +0200419 // After creating the list
420 // normal_arg
421 // (space for optional_arg)
422 // vararg-list
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200423 vararg_count = argcount - ufunc->uf_args.ga_len;
424 if (vararg_count < 0)
425 vararg_count = 0;
426 else
427 argcount -= vararg_count;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200428 if (exe_newlist(vararg_count, ectx) == FAIL)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200429 return FAIL;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200430
431 vararg_count = 1;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200432 }
433
Bram Moolenaarfe270812020-04-11 22:31:27 +0200434 arg_to_add = ufunc->uf_args.ga_len - argcount;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200435 if (arg_to_add < 0)
436 {
Bram Moolenaar79e8db92020-08-14 22:16:33 +0200437 if (arg_to_add == -1)
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200438 emsg(_(e_one_argument_too_many));
Bram Moolenaar79e8db92020-08-14 22:16:33 +0200439 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200440 semsg(_(e_nr_arguments_too_many), -arg_to_add);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200441 return FAIL;
442 }
Bram Moolenaarcd1cda22022-02-16 21:48:25 +0000443 else if (arg_to_add > ufunc->uf_def_args.ga_len)
444 {
445 int missing = arg_to_add - ufunc->uf_def_args.ga_len;
446
447 if (missing == 1)
448 emsg(_(e_one_argument_too_few));
449 else
450 semsg(_(e_nr_arguments_too_few), missing);
451 return FAIL;
452 }
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200453
454 // Reserve space for:
455 // - missing arguments
456 // - stack frame
457 // - local variables
458 // - if needed: a counter for number of closures created in
459 // ectx->ec_funcrefs.
460 varcount = dfunc->df_varcount + dfunc->df_has_closure;
Bram Moolenaar35578162021-08-02 19:10:38 +0200461 if (GA_GROW_FAILS(&ectx->ec_stack, arg_to_add + STACK_FRAME_SIZE + varcount))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100462 return FAIL;
463
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100464 // If depth of calling is getting too high, don't execute the function.
465 if (funcdepth_increment() == FAIL)
466 return FAIL;
Bram Moolenaare99d4222021-06-13 14:01:26 +0200467 ++ex_nesting_level;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100468
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100469 // Only make a copy of funclocal if it contains something to restore.
Bram Moolenaar4c137212021-04-19 16:48:48 +0200470 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100471 {
472 floc = ALLOC_ONE(funclocal_T);
473 if (floc == NULL)
474 return FAIL;
Bram Moolenaar4c137212021-04-19 16:48:48 +0200475 *floc = ectx->ec_funclocal;
476 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100477 }
478
Bram Moolenaarfe270812020-04-11 22:31:27 +0200479 // Move the vararg-list to below the missing optional arguments.
480 if (vararg_count > 0 && arg_to_add > 0)
481 *STACK_TV_BOT(arg_to_add - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100482
483 // Reserve space for omitted optional arguments, filled in soon.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200484 for (idx = 0; idx < arg_to_add; ++idx)
Bram Moolenaarfe270812020-04-11 22:31:27 +0200485 STACK_TV_BOT(idx - vararg_count)->v_type = VAR_UNKNOWN;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200486 ectx->ec_stack.ga_len += arg_to_add;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100487
488 // Store current execution state in stack frame for ISN_RETURN.
Bram Moolenaar0186e582021-01-10 18:33:11 +0100489 STACK_TV_BOT(STACK_FRAME_FUNC_OFF)->vval.v_number = ectx->ec_dfunc_idx;
490 STACK_TV_BOT(STACK_FRAME_IIDX_OFF)->vval.v_number = ectx->ec_iidx;
Bram Moolenaar5930ddc2021-04-26 20:32:59 +0200491 STACK_TV_BOT(STACK_FRAME_INSTR_OFF)->vval.v_string = (void *)ectx->ec_instr;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200492 STACK_TV_BOT(STACK_FRAME_OUTER_OFF)->vval.v_string =
493 (void *)ectx->ec_outer_ref;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100494 STACK_TV_BOT(STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string = (void *)floc;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100495 STACK_TV_BOT(STACK_FRAME_IDX_OFF)->vval.v_number = ectx->ec_frame_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200496 ectx->ec_frame_idx = ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100497
498 // Initialize local variables
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200499 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaar5cd64792021-12-25 18:23:24 +0000500 {
501 typval_T *tv = STACK_TV_BOT(STACK_FRAME_SIZE + idx);
502
503 tv->v_type = VAR_NUMBER;
504 tv->vval.v_number = 0;
505 }
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200506 if (dfunc->df_has_closure)
507 {
508 typval_T *tv = STACK_TV_BOT(STACK_FRAME_SIZE + dfunc->df_varcount);
509
510 tv->v_type = VAR_NUMBER;
511 tv->vval.v_number = 0;
512 }
513 ectx->ec_stack.ga_len += STACK_FRAME_SIZE + varcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100514
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +0100515 if (pt != NULL || ufunc->uf_partial != NULL
516 || (ufunc->uf_flags & FC_CLOSURE))
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100517 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200518 outer_ref_T *ref = ALLOC_CLEAR_ONE(outer_ref_T);
Bram Moolenaar0186e582021-01-10 18:33:11 +0100519
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200520 if (ref == NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +0100521 return FAIL;
522 if (pt != NULL)
523 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +0000524 ref->or_outer = get_pt_outer(pt);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200525 ++pt->pt_refcount;
526 ref->or_partial = pt;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100527 }
528 else if (ufunc->uf_partial != NULL)
529 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +0000530 ref->or_outer = get_pt_outer(ufunc->uf_partial);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200531 ++ufunc->uf_partial->pt_refcount;
532 ref->or_partial = ufunc->uf_partial;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100533 }
534 else
535 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200536 ref->or_outer = ALLOC_CLEAR_ONE(outer_T);
Dominique Pelle5a9e5842021-07-24 19:32:12 +0200537 if (unlikely(ref->or_outer == NULL))
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200538 {
539 vim_free(ref);
540 return FAIL;
541 }
542 ref->or_outer_allocated = TRUE;
543 ref->or_outer->out_stack = &ectx->ec_stack;
544 ref->or_outer->out_frame_idx = ectx->ec_frame_idx;
545 if (ectx->ec_outer_ref != NULL)
546 ref->or_outer->out_up = ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100547 }
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200548 ectx->ec_outer_ref = ref;
Bram Moolenaarab360522021-01-10 14:02:28 +0100549 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100550 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200551 ectx->ec_outer_ref = NULL;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100552
Bram Moolenaarc970e422021-03-17 15:03:04 +0100553 ++ufunc->uf_calls;
554
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100555 // Set execution state to the start of the called function.
556 ectx->ec_dfunc_idx = cdf_idx;
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100557 ectx->ec_instr = INSTRUCTIONS(dfunc);
Bram Moolenaarb2049902021-01-24 12:53:53 +0100558 entry = estack_push_ufunc(ufunc, 1);
Bram Moolenaarc620c052020-07-08 15:16:19 +0200559 if (entry != NULL)
560 {
561 // Set the script context to the script where the function was defined.
Bram Moolenaarc70fe462021-04-17 17:59:19 +0200562 // Save the current context so it can be restored on return.
563 entry->es_save_sctx = current_sctx;
564 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200565 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100566
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +0200567 // Start execution at the first instruction.
568 ectx->ec_iidx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100569
570 return OK;
571}
572
573// Get pointer to item in the stack.
574#define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
575
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000576// Double linked list of funcstack_T in use.
577static funcstack_T *first_funcstack = NULL;
578
579 static void
580add_funcstack_to_list(funcstack_T *funcstack)
581{
582 // Link in list of funcstacks.
583 if (first_funcstack != NULL)
584 first_funcstack->fs_prev = funcstack;
585 funcstack->fs_next = first_funcstack;
586 funcstack->fs_prev = NULL;
587 first_funcstack = funcstack;
588}
589
590 static void
591remove_funcstack_from_list(funcstack_T *funcstack)
592{
593 if (funcstack->fs_prev == NULL)
594 first_funcstack = funcstack->fs_next;
595 else
596 funcstack->fs_prev->fs_next = funcstack->fs_next;
597 if (funcstack->fs_next != NULL)
598 funcstack->fs_next->fs_prev = funcstack->fs_prev;
599}
600
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100601/*
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200602 * Used when returning from a function: Check if any closure is still
603 * referenced. If so then move the arguments and variables to a separate piece
604 * of stack to be used when the closure is called.
605 * When "free_arguments" is TRUE the arguments are to be freed.
606 * Returns FAIL when out of memory.
607 */
608 static int
609handle_closure_in_use(ectx_T *ectx, int free_arguments)
610{
611 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
612 + ectx->ec_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200613 int argcount;
614 int top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200615 int idx;
616 typval_T *tv;
617 int closure_in_use = FALSE;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200618 garray_T *gap = &ectx->ec_funcrefs;
619 varnumber_T closure_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200620
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200621 if (dfunc->df_ufunc == NULL)
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200622 return OK; // function was freed
623 if (dfunc->df_has_closure == 0)
624 return OK; // no closures
625 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + dfunc->df_varcount);
626 closure_count = tv->vval.v_number;
627 if (closure_count == 0)
628 return OK; // no funcrefs created
629
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200630 argcount = ufunc_argcount(dfunc->df_ufunc);
631 top = ectx->ec_frame_idx - argcount;
632
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200633 // Check if any created closure is still in use.
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200634 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200635 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200636 partial_T *pt;
637 int off = gap->ga_len - closure_count + idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200638
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200639 if (off < 0)
640 continue; // count is off or already done
641 pt = ((partial_T **)gap->ga_data)[off];
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200642 if (pt->pt_refcount > 1)
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200643 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200644 int refcount = pt->pt_refcount;
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200645 int i;
646
Dominique Pelleaf4a61a2021-12-27 17:21:41 +0000647 // A Reference in a local variable doesn't count, it gets
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200648 // unreferenced on return.
649 for (i = 0; i < dfunc->df_varcount; ++i)
650 {
651 typval_T *stv = STACK_TV(ectx->ec_frame_idx
652 + STACK_FRAME_SIZE + i);
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200653 if (stv->v_type == VAR_PARTIAL && pt == stv->vval.v_partial)
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200654 --refcount;
655 }
656 if (refcount > 1)
657 {
658 closure_in_use = TRUE;
659 break;
660 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200661 }
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200662 }
663
664 if (closure_in_use)
665 {
666 funcstack_T *funcstack = ALLOC_CLEAR_ONE(funcstack_T);
667 typval_T *stack;
668
669 // A closure is using the arguments and/or local variables.
670 // Move them to the called function.
671 if (funcstack == NULL)
672 return FAIL;
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000673
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200674 funcstack->fs_var_offset = argcount + STACK_FRAME_SIZE;
675 funcstack->fs_ga.ga_len = funcstack->fs_var_offset + dfunc->df_varcount;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200676 stack = ALLOC_CLEAR_MULT(typval_T, funcstack->fs_ga.ga_len);
677 funcstack->fs_ga.ga_data = stack;
678 if (stack == NULL)
679 {
680 vim_free(funcstack);
681 return FAIL;
682 }
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000683 add_funcstack_to_list(funcstack);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200684
685 // Move or copy the arguments.
686 for (idx = 0; idx < argcount; ++idx)
687 {
688 tv = STACK_TV(top + idx);
689 if (free_arguments)
690 {
691 *(stack + idx) = *tv;
692 tv->v_type = VAR_UNKNOWN;
693 }
694 else
695 copy_tv(tv, stack + idx);
696 }
697 // Move the local variables.
698 for (idx = 0; idx < dfunc->df_varcount; ++idx)
699 {
700 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + idx);
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200701
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200702 // A partial created for a local function, that is also used as a
703 // local variable, has a reference count for the variable, thus
704 // will never go down to zero. When all these refcounts are one
705 // then the funcstack is unused. We need to count how many we have
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000706 // so we know when to check.
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200707 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL)
708 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200709 int i;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200710
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200711 for (i = 0; i < closure_count; ++i)
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200712 if (tv->vval.v_partial == ((partial_T **)gap->ga_data)[
713 gap->ga_len - closure_count + i])
714 ++funcstack->fs_min_refcount;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200715 }
716
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200717 *(stack + funcstack->fs_var_offset + idx) = *tv;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200718 tv->v_type = VAR_UNKNOWN;
719 }
720
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200721 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200722 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200723 partial_T *pt = ((partial_T **)gap->ga_data)[gap->ga_len
724 - closure_count + idx];
725 if (pt->pt_refcount > 1)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200726 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200727 ++funcstack->fs_refcount;
728 pt->pt_funcstack = funcstack;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100729 pt->pt_outer.out_stack = &funcstack->fs_ga;
730 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx - top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200731 }
732 }
733 }
734
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200735 for (idx = 0; idx < closure_count; ++idx)
736 partial_unref(((partial_T **)gap->ga_data)[gap->ga_len
737 - closure_count + idx]);
738 gap->ga_len -= closure_count;
739 if (gap->ga_len == 0)
740 ga_clear(gap);
741
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200742 return OK;
743}
744
745/*
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200746 * Called when a partial is freed or its reference count goes down to one. The
747 * funcstack may be the only reference to the partials in the local variables.
748 * Go over all of them, the funcref and can be freed if all partials
749 * referencing the funcstack have a reference count of one.
750 */
751 void
752funcstack_check_refcount(funcstack_T *funcstack)
753{
754 int i;
755 garray_T *gap = &funcstack->fs_ga;
756 int done = 0;
757
758 if (funcstack->fs_refcount > funcstack->fs_min_refcount)
759 return;
760 for (i = funcstack->fs_var_offset; i < gap->ga_len; ++i)
761 {
762 typval_T *tv = ((typval_T *)gap->ga_data) + i;
763
764 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL
765 && tv->vval.v_partial->pt_funcstack == funcstack
766 && tv->vval.v_partial->pt_refcount == 1)
767 ++done;
768 }
769 if (done == funcstack->fs_min_refcount)
770 {
771 typval_T *stack = gap->ga_data;
772
773 // All partials referencing the funcstack have a reference count of
774 // one, thus the funcstack is no longer of use.
775 for (i = 0; i < gap->ga_len; ++i)
776 clear_tv(stack + i);
777 vim_free(stack);
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000778 remove_funcstack_from_list(funcstack);
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200779 vim_free(funcstack);
780 }
781}
782
783/*
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000784 * For garbage collecting: set references in all variables referenced by
785 * all funcstacks.
786 */
787 int
788set_ref_in_funcstacks(int copyID)
789{
790 funcstack_T *funcstack;
791
792 for (funcstack = first_funcstack; funcstack != NULL;
793 funcstack = funcstack->fs_next)
794 {
795 typval_T *stack = funcstack->fs_ga.ga_data;
796 int i;
797
798 for (i = 0; i < funcstack->fs_ga.ga_len; ++i)
799 if (set_ref_in_item(stack + i, copyID, NULL, NULL))
800 return TRUE; // abort
801 }
802 return FALSE;
803}
804
805/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100806 * Return from the current function.
807 */
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200808 static int
Bram Moolenaar4c137212021-04-19 16:48:48 +0200809func_return(ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100810{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100811 int idx;
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100812 int ret_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200813 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
814 + ectx->ec_dfunc_idx;
815 int argcount = ufunc_argcount(dfunc->df_ufunc);
816 int top = ectx->ec_frame_idx - argcount;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200817 estack_T *entry;
Bram Moolenaar12d26532021-02-19 19:13:21 +0100818 int prev_dfunc_idx = STACK_TV(ectx->ec_frame_idx
819 + STACK_FRAME_FUNC_OFF)->vval.v_number;
Bram Moolenaarb06b50d2021-04-26 21:39:25 +0200820 funclocal_T *floc;
821#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +0100822 dfunc_T *prev_dfunc = ((dfunc_T *)def_functions.ga_data)
823 + prev_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100824
Bram Moolenaar12d26532021-02-19 19:13:21 +0100825 if (do_profiling == PROF_YES)
826 {
827 ufunc_T *caller = prev_dfunc->df_ufunc;
828
829 if (dfunc->df_ufunc->uf_profiling
830 || (caller != NULL && caller->uf_profiling))
831 {
832 profile_may_end_func(((profinfo_T *)profile_info_ga.ga_data)
833 + profile_info_ga.ga_len - 1, dfunc->df_ufunc, caller);
834 --profile_info_ga.ga_len;
835 }
836 }
837#endif
Bram Moolenaar919c12c2021-12-14 14:29:16 +0000838
839 // No check for uf_refcount being zero, cannot think of a way that would
840 // happen.
Bram Moolenaarc970e422021-03-17 15:03:04 +0100841 --dfunc->df_ufunc->uf_calls;
842
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100843 // execution context goes one level up
Bram Moolenaarc620c052020-07-08 15:16:19 +0200844 entry = estack_pop();
845 if (entry != NULL)
Bram Moolenaarc70fe462021-04-17 17:59:19 +0200846 current_sctx = entry->es_save_sctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100847
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200848 if (handle_closure_in_use(ectx, TRUE) == FAIL)
849 return FAIL;
850
851 // Clear the arguments.
852 for (idx = top; idx < ectx->ec_frame_idx; ++idx)
853 clear_tv(STACK_TV(idx));
854
855 // Clear local variables and temp values, but not the return value.
856 for (idx = ectx->ec_frame_idx + STACK_FRAME_SIZE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100857 idx < ectx->ec_stack.ga_len - 1; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100858 clear_tv(STACK_TV(idx));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100859
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100860 // The return value should be on top of the stack. However, when aborting
861 // it may not be there and ec_frame_idx is the top of the stack.
862 ret_idx = ectx->ec_stack.ga_len - 1;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100863 if (ret_idx == ectx->ec_frame_idx + STACK_FRAME_IDX_OFF)
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100864 ret_idx = 0;
865
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200866 if (ectx->ec_outer_ref != NULL)
867 {
868 if (ectx->ec_outer_ref->or_outer_allocated)
869 vim_free(ectx->ec_outer_ref->or_outer);
870 partial_unref(ectx->ec_outer_ref->or_partial);
871 vim_free(ectx->ec_outer_ref);
872 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100873
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100874 // Restore the previous frame.
Bram Moolenaar12d26532021-02-19 19:13:21 +0100875 ectx->ec_dfunc_idx = prev_dfunc_idx;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100876 ectx->ec_iidx = STACK_TV(ectx->ec_frame_idx
877 + STACK_FRAME_IIDX_OFF)->vval.v_number;
Bram Moolenaar5930ddc2021-04-26 20:32:59 +0200878 ectx->ec_instr = (void *)STACK_TV(ectx->ec_frame_idx
879 + STACK_FRAME_INSTR_OFF)->vval.v_string;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200880 ectx->ec_outer_ref = (void *)STACK_TV(ectx->ec_frame_idx
Bram Moolenaar0186e582021-01-10 18:33:11 +0100881 + STACK_FRAME_OUTER_OFF)->vval.v_string;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100882 floc = (void *)STACK_TV(ectx->ec_frame_idx
883 + STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string;
Bram Moolenaar5366e1a2020-10-01 13:01:34 +0200884 // restoring ec_frame_idx must be last
Bram Moolenaar0186e582021-01-10 18:33:11 +0100885 ectx->ec_frame_idx = STACK_TV(ectx->ec_frame_idx
886 + STACK_FRAME_IDX_OFF)->vval.v_number;
Bram Moolenaard386e922021-04-25 14:48:49 +0200887
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100888 if (floc == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +0200889 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100890 else
891 {
Bram Moolenaar4c137212021-04-19 16:48:48 +0200892 ectx->ec_funclocal = *floc;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100893 vim_free(floc);
894 }
895
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100896 if (ret_idx > 0)
897 {
898 // Reset the stack to the position before the call, with a spot for the
899 // return value, moved there from above the frame.
900 ectx->ec_stack.ga_len = top + 1;
901 *STACK_TV_BOT(-1) = *STACK_TV(ret_idx);
902 }
903 else
904 // Reset the stack to the position before the call.
905 ectx->ec_stack.ga_len = top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200906
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100907 funcdepth_decrement();
Bram Moolenaare99d4222021-06-13 14:01:26 +0200908 --ex_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200909 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100910}
911
912#undef STACK_TV
913
914/*
915 * Prepare arguments and rettv for calling a builtin or user function.
916 */
917 static int
918call_prepare(int argcount, typval_T *argvars, ectx_T *ectx)
919{
920 int idx;
921 typval_T *tv;
922
923 // Move arguments from bottom of the stack to argvars[] and add terminator.
924 for (idx = 0; idx < argcount; ++idx)
925 argvars[idx] = *STACK_TV_BOT(idx - argcount);
926 argvars[argcount].v_type = VAR_UNKNOWN;
927
928 // Result replaces the arguments on the stack.
929 if (argcount > 0)
930 ectx->ec_stack.ga_len -= argcount - 1;
Bram Moolenaar35578162021-08-02 19:10:38 +0200931 else if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100932 return FAIL;
933 else
934 ++ectx->ec_stack.ga_len;
935
936 // Default return value is zero.
937 tv = STACK_TV_BOT(-1);
938 tv->v_type = VAR_NUMBER;
939 tv->vval.v_number = 0;
Bram Moolenaar24565cf2022-03-28 18:16:52 +0100940 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100941
942 return OK;
943}
944
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200945// Ugly global to avoid passing the execution context around through many
946// layers.
947static ectx_T *current_ectx = NULL;
948
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100949/*
950 * Call a builtin function by index.
951 */
952 static int
953call_bfunc(int func_idx, int argcount, ectx_T *ectx)
954{
955 typval_T argvars[MAX_FUNC_ARGS];
956 int idx;
Bram Moolenaar171fb922020-10-28 16:54:47 +0100957 int did_emsg_before = did_emsg;
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200958 ectx_T *prev_ectx = current_ectx;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +0200959 char *save_func_name = ectx->ec_where.wt_func_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100960
961 if (call_prepare(argcount, argvars, ectx) == FAIL)
962 return FAIL;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +0200963 ectx->ec_where.wt_func_name = internal_func_name(func_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100964
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200965 // Call the builtin function. Set "current_ectx" so that when it
966 // recursively invokes call_def_function() a closure context can be set.
967 current_ectx = ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100968 call_internal_func_by_idx(func_idx, argvars, STACK_TV_BOT(-1));
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200969 current_ectx = prev_ectx;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +0200970 ectx->ec_where.wt_func_name = save_func_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100971
972 // Clear the arguments.
973 for (idx = 0; idx < argcount; ++idx)
974 clear_tv(&argvars[idx]);
Bram Moolenaar015f4262020-05-05 21:25:22 +0200975
Bram Moolenaar57f799e2020-12-12 20:42:19 +0100976 if (did_emsg > did_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200977 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100978 return OK;
979}
980
981/*
982 * Execute a user defined function.
Bram Moolenaarab360522021-01-10 14:02:28 +0100983 * If the function is compiled this will add a stack frame and set the
984 * instruction pointer at the start of the function.
985 * Otherwise the function is called here.
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200986 * If "pt" is not null use "pt->pt_outer" for ec_outer_ref->or_outer.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100987 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100988 */
989 static int
Bram Moolenaar0186e582021-01-10 18:33:11 +0100990call_ufunc(
991 ufunc_T *ufunc,
992 partial_T *pt,
993 int argcount,
994 ectx_T *ectx,
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +0200995 isn_T *iptr,
996 dict_T *selfdict)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100997{
998 typval_T argvars[MAX_FUNC_ARGS];
999 funcexe_T funcexe;
1000 int error;
1001 int idx;
Bram Moolenaar28ee8922020-10-28 20:20:00 +01001002 int did_emsg_before = did_emsg;
Bram Moolenaar139575d2022-03-15 19:29:30 +00001003 compiletype_T compile_type = get_compile_type(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001004
Bram Moolenaare99d4222021-06-13 14:01:26 +02001005 if (func_needs_compiling(ufunc, compile_type)
1006 && compile_def_function(ufunc, FALSE, compile_type, NULL)
1007 == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02001008 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001009 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001010 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +01001011 error = check_user_func_argcount(ufunc, argcount);
Bram Moolenaar50824712020-12-20 21:10:17 +01001012 if (error != FCERR_UNKNOWN)
1013 {
1014 if (error == FCERR_TOOMANY)
Bram Moolenaare1242042021-12-16 20:56:57 +00001015 semsg(_(e_too_many_arguments_for_function_str), ufunc->uf_name);
Bram Moolenaar50824712020-12-20 21:10:17 +01001016 else
Bram Moolenaare1242042021-12-16 20:56:57 +00001017 semsg(_(e_not_enough_arguments_for_function_str),
1018 ufunc->uf_name);
Bram Moolenaar50824712020-12-20 21:10:17 +01001019 return FAIL;
1020 }
1021
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001022 // The function has been compiled, can call it quickly. For a function
1023 // that was defined later: we can call it directly next time.
1024 if (iptr != NULL)
1025 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001026 delete_instr(iptr);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001027 iptr->isn_type = ISN_DCALL;
1028 iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1029 iptr->isn_arg.dfunc.cdf_argcount = argcount;
1030 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001031 return call_dfunc(ufunc->uf_dfunc_idx, pt, argcount, ectx);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001032 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001033
1034 if (call_prepare(argcount, argvars, ectx) == FAIL)
1035 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +02001036 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00001037 funcexe.fe_evaluate = TRUE;
1038 funcexe.fe_selfdict = selfdict != NULL ? selfdict : dict_stack_get_dict();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001039
1040 // Call the user function. Result goes in last position on the stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001041 error = call_user_func_check(ufunc, argcount, argvars,
Bram Moolenaar851f86b2021-12-13 14:26:44 +00001042 STACK_TV_BOT(-1), &funcexe, funcexe.fe_selfdict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001043
1044 // Clear the arguments.
1045 for (idx = 0; idx < argcount; ++idx)
1046 clear_tv(&argvars[idx]);
1047
1048 if (error != FCERR_NONE)
1049 {
Bram Moolenaar2ef91562021-12-11 16:14:07 +00001050 user_func_error(error, ufunc->uf_name, &funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001051 return FAIL;
1052 }
Bram Moolenaar28ee8922020-10-28 20:20:00 +01001053 if (did_emsg > did_emsg_before)
Bram Moolenaared677f52020-08-12 16:38:10 +02001054 // Error other than from calling the function itself.
1055 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001056 return OK;
1057}
1058
1059/*
Bram Moolenaara91a7132021-03-25 21:12:15 +01001060 * If command modifiers were applied restore them.
1061 */
1062 static void
1063may_restore_cmdmod(funclocal_T *funclocal)
1064{
1065 if (funclocal->floc_restore_cmdmod)
1066 {
1067 cmdmod.cmod_filter_regmatch.regprog = NULL;
1068 undo_cmdmod(&cmdmod);
1069 cmdmod = funclocal->floc_save_cmdmod;
1070 funclocal->floc_restore_cmdmod = FALSE;
1071 }
1072}
1073
1074/*
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001075 * Return TRUE if an error was given (not caught in try/catch) or CTRL-C was
1076 * pressed.
Bram Moolenaara1773442020-08-12 15:21:22 +02001077 */
1078 static int
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001079vim9_aborting(int prev_uncaught_emsg)
Bram Moolenaara1773442020-08-12 15:21:22 +02001080{
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001081 return uncaught_emsg > prev_uncaught_emsg || got_int || did_throw;
Bram Moolenaara1773442020-08-12 15:21:22 +02001082}
1083
1084/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001085 * Execute a function by "name".
1086 * This can be a builtin function or a user function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001087 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001088 * Returns FAIL if not found without an error message.
1089 */
1090 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001091call_by_name(
1092 char_u *name,
1093 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001094 ectx_T *ectx,
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001095 isn_T *iptr,
1096 dict_T *selfdict)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001097{
1098 ufunc_T *ufunc;
1099
1100 if (builtin_function(name, -1))
1101 {
1102 int func_idx = find_internal_func(name);
1103
Bram Moolenaar1983f1a2022-02-28 20:55:02 +00001104 if (func_idx < 0) // Impossible?
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001105 return FAIL;
Bram Moolenaar389df252020-07-09 21:20:47 +02001106 if (check_internal_func(func_idx, argcount) < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001107 return FAIL;
1108 return call_bfunc(func_idx, argcount, ectx);
1109 }
1110
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00001111 ufunc = find_func(name, FALSE);
Bram Moolenaara1773442020-08-12 15:21:22 +02001112
1113 if (ufunc == NULL)
1114 {
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001115 int prev_uncaught_emsg = uncaught_emsg;
Bram Moolenaara1773442020-08-12 15:21:22 +02001116
1117 if (script_autoload(name, TRUE))
1118 // loaded a package, search for the function again
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00001119 ufunc = find_func(name, FALSE);
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001120
1121 if (vim9_aborting(prev_uncaught_emsg))
Bram Moolenaara1773442020-08-12 15:21:22 +02001122 return FAIL; // bail out if loading the script caused an error
1123 }
1124
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001125 if (ufunc != NULL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001126 {
Bram Moolenaar6ce46b92021-08-07 15:35:36 +02001127 if (ufunc->uf_arg_types != NULL || ufunc->uf_va_type != NULL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001128 {
1129 int i;
1130 typval_T *argv = STACK_TV_BOT(0) - argcount;
1131
1132 // The function can change at runtime, check that the argument
1133 // types are correct.
1134 for (i = 0; i < argcount; ++i)
1135 {
Bram Moolenaare3ffcd92021-03-08 21:47:13 +01001136 type_T *type = NULL;
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001137
Bram Moolenaar6ce46b92021-08-07 15:35:36 +02001138 if (i < ufunc->uf_args.ga_len && ufunc->uf_arg_types != NULL)
Bram Moolenaare3ffcd92021-03-08 21:47:13 +01001139 type = ufunc->uf_arg_types[i];
1140 else if (ufunc->uf_va_type != NULL)
1141 type = ufunc->uf_va_type->tt_member;
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001142 if (type != NULL && check_typval_arg_type(type,
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001143 &argv[i], NULL, i + 1) == FAIL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001144 return FAIL;
1145 }
1146 }
1147
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001148 return call_ufunc(ufunc, NULL, argcount, ectx, iptr, selfdict);
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001149 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001150
1151 return FAIL;
1152}
1153
1154 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001155call_partial(
1156 typval_T *tv,
1157 int argcount_arg,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001158 ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001159{
Bram Moolenaara90afb92020-07-15 22:38:56 +02001160 int argcount = argcount_arg;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001161 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001162 int called_emsg_before = called_emsg;
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +01001163 int res = FAIL;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001164 dict_T *selfdict = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001165
1166 if (tv->v_type == VAR_PARTIAL)
1167 {
Bram Moolenaara90afb92020-07-15 22:38:56 +02001168 partial_T *pt = tv->vval.v_partial;
1169 int i;
1170
1171 if (pt->pt_argc > 0)
1172 {
1173 // Make space for arguments from the partial, shift the "argcount"
1174 // arguments up.
Bram Moolenaar35578162021-08-02 19:10:38 +02001175 if (GA_GROW_FAILS(&ectx->ec_stack, pt->pt_argc))
Bram Moolenaara90afb92020-07-15 22:38:56 +02001176 return FAIL;
1177 for (i = 1; i <= argcount; ++i)
1178 *STACK_TV_BOT(-i + pt->pt_argc) = *STACK_TV_BOT(-i);
1179 ectx->ec_stack.ga_len += pt->pt_argc;
1180 argcount += pt->pt_argc;
1181
1182 // copy the arguments from the partial onto the stack
1183 for (i = 0; i < pt->pt_argc; ++i)
1184 copy_tv(&pt->pt_argv[i], STACK_TV_BOT(-argcount + i));
1185 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001186 selfdict = pt->pt_dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001187
1188 if (pt->pt_func != NULL)
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001189 return call_ufunc(pt->pt_func, pt, argcount, ectx, NULL, selfdict);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001190
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001191 name = pt->pt_name;
1192 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001193 else if (tv->v_type == VAR_FUNC)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001194 name = tv->vval.v_string;
Bram Moolenaar95006e32020-08-29 17:47:08 +02001195 if (name != NULL)
1196 {
1197 char_u fname_buf[FLEN_FIXED + 1];
1198 char_u *tofree = NULL;
1199 int error = FCERR_NONE;
1200 char_u *fname;
1201
1202 // May need to translate <SNR>123_ to K_SNR.
1203 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
1204 if (error != FCERR_NONE)
1205 res = FAIL;
1206 else
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001207 res = call_by_name(fname, argcount, ectx, NULL, selfdict);
Bram Moolenaar95006e32020-08-29 17:47:08 +02001208 vim_free(tofree);
1209 }
1210
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +01001211 if (res == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001212 {
1213 if (called_emsg == called_emsg_before)
Bram Moolenaare1242042021-12-16 20:56:57 +00001214 semsg(_(e_unknown_function_str),
Bram Moolenaar015f4262020-05-05 21:25:22 +02001215 name == NULL ? (char_u *)"[unknown]" : name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001216 return FAIL;
1217 }
1218 return OK;
1219}
1220
1221/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001222 * Check if "lock" is VAR_LOCKED or VAR_FIXED. If so give an error and return
1223 * TRUE.
1224 */
1225 static int
1226error_if_locked(int lock, char *error)
1227{
1228 if (lock & (VAR_LOCKED | VAR_FIXED))
1229 {
1230 emsg(_(error));
1231 return TRUE;
1232 }
1233 return FALSE;
1234}
1235
1236/*
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001237 * Give an error if "tv" is not a number and return FAIL.
1238 */
1239 static int
1240check_for_number(typval_T *tv)
1241{
1242 if (tv->v_type != VAR_NUMBER)
1243 {
1244 semsg(_(e_expected_str_but_got_str),
1245 vartype_name(VAR_NUMBER), vartype_name(tv->v_type));
1246 return FAIL;
1247 }
1248 return OK;
1249}
1250
1251/*
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001252 * Store "tv" in variable "name".
1253 * This is for s: and g: variables.
1254 */
1255 static void
1256store_var(char_u *name, typval_T *tv)
1257{
1258 funccal_entry_T entry;
Bram Moolenaard877a572021-04-01 19:42:48 +02001259 int flags = ASSIGN_DECL;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001260
Bram Moolenaard877a572021-04-01 19:42:48 +02001261 if (tv->v_lock)
1262 flags |= ASSIGN_CONST;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001263 save_funccal(&entry);
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001264 set_var_const(name, 0, NULL, tv, FALSE, flags, 0);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001265 restore_funccal();
1266}
1267
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001268/*
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001269 * Convert "tv" to a string.
1270 * Return FAIL if not allowed.
1271 */
1272 static int
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001273do_2string(typval_T *tv, int is_2string_any, int tolerant)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001274{
1275 if (tv->v_type != VAR_STRING)
1276 {
1277 char_u *str;
1278
1279 if (is_2string_any)
1280 {
1281 switch (tv->v_type)
1282 {
1283 case VAR_SPECIAL:
1284 case VAR_BOOL:
1285 case VAR_NUMBER:
1286 case VAR_FLOAT:
1287 case VAR_BLOB: break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001288
1289 case VAR_LIST:
1290 if (tolerant)
1291 {
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001292 char_u *s, *e, *p;
1293 garray_T ga;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001294
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001295 ga_init2(&ga, sizeof(char_u *), 1);
1296
1297 // Convert to NL separated items, then
1298 // escape the items and replace the NL with
1299 // a space.
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001300 str = typval2string(tv, TRUE);
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001301 if (str == NULL)
1302 return FAIL;
1303 s = str;
1304 while ((e = vim_strchr(s, '\n')) != NULL)
1305 {
1306 *e = NUL;
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +01001307 p = vim_strsave_fnameescape(s,
1308 VSE_NONE);
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001309 if (p != NULL)
1310 {
1311 ga_concat(&ga, p);
1312 ga_concat(&ga, (char_u *)" ");
1313 vim_free(p);
1314 }
1315 s = e + 1;
1316 }
1317 vim_free(str);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001318 clear_tv(tv);
1319 tv->v_type = VAR_STRING;
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001320 tv->vval.v_string = ga.ga_data;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001321 return OK;
1322 }
1323 // FALLTHROUGH
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001324 default: to_string_error(tv->v_type);
1325 return FAIL;
1326 }
1327 }
Bram Moolenaar34453202021-01-31 13:08:38 +01001328 str = typval_tostring(tv, TRUE);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001329 clear_tv(tv);
1330 tv->v_type = VAR_STRING;
1331 tv->vval.v_string = str;
1332 }
1333 return OK;
1334}
1335
1336/*
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001337 * When the value of "sv" is a null list of dict, allocate it.
1338 */
1339 static void
Bram Moolenaar859cc212022-03-28 15:22:35 +01001340allocate_if_null(svar_T *sv)
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001341{
Bram Moolenaar859cc212022-03-28 15:22:35 +01001342 typval_T *tv = sv->sv_tv;
1343
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001344 switch (tv->v_type)
1345 {
1346 case VAR_LIST:
Bram Moolenaar859cc212022-03-28 15:22:35 +01001347 if (tv->vval.v_list == NULL && sv->sv_type != &t_list_empty)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001348 (void)rettv_list_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001349 break;
1350 case VAR_DICT:
Bram Moolenaar859cc212022-03-28 15:22:35 +01001351 if (tv->vval.v_dict == NULL && sv->sv_type != &t_dict_empty)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001352 (void)rettv_dict_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001353 break;
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02001354 case VAR_BLOB:
Bram Moolenaar859cc212022-03-28 15:22:35 +01001355 if (tv->vval.v_blob == NULL && sv->sv_type != &t_blob_null)
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02001356 (void)rettv_blob_alloc(tv);
1357 break;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001358 default:
1359 break;
1360 }
1361}
Bram Moolenaard3aac292020-04-19 14:32:17 +02001362
Bram Moolenaare7525c52021-01-09 13:20:37 +01001363/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001364 * Return the character "str[index]" where "index" is the character index,
1365 * including composing characters.
1366 * If "index" is out of range NULL is returned.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001367 */
1368 char_u *
1369char_from_string(char_u *str, varnumber_T index)
1370{
1371 size_t nbyte = 0;
1372 varnumber_T nchar = index;
1373 size_t slen;
1374
1375 if (str == NULL)
1376 return NULL;
1377 slen = STRLEN(str);
1378
Bram Moolenaarff871402021-03-26 13:34:05 +01001379 // Do the same as for a list: a negative index counts from the end.
1380 // Optimization to check the first byte to be below 0x80 (and no composing
1381 // character follows) makes this a lot faster.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001382 if (index < 0)
1383 {
1384 int clen = 0;
1385
1386 for (nbyte = 0; nbyte < slen; ++clen)
Bram Moolenaarff871402021-03-26 13:34:05 +01001387 {
1388 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1389 ++nbyte;
1390 else if (enc_utf8)
1391 nbyte += utfc_ptr2len(str + nbyte);
1392 else
1393 nbyte += mb_ptr2len(str + nbyte);
1394 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001395 nchar = clen + index;
1396 if (nchar < 0)
1397 // unlike list: index out of range results in empty string
1398 return NULL;
1399 }
1400
1401 for (nbyte = 0; nchar > 0 && nbyte < slen; --nchar)
Bram Moolenaarff871402021-03-26 13:34:05 +01001402 {
1403 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1404 ++nbyte;
1405 else if (enc_utf8)
1406 nbyte += utfc_ptr2len(str + nbyte);
1407 else
1408 nbyte += mb_ptr2len(str + nbyte);
1409 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001410 if (nbyte >= slen)
1411 return NULL;
Bram Moolenaar0289a092021-03-14 18:40:19 +01001412 return vim_strnsave(str + nbyte, mb_ptr2len(str + nbyte));
Bram Moolenaare7525c52021-01-09 13:20:37 +01001413}
1414
1415/*
1416 * Get the byte index for character index "idx" in string "str" with length
Bram Moolenaar0289a092021-03-14 18:40:19 +01001417 * "str_len". Composing characters are included.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001418 * If going over the end return "str_len".
1419 * If "idx" is negative count from the end, -1 is the last character.
1420 * When going over the start return -1.
1421 */
1422 static long
1423char_idx2byte(char_u *str, size_t str_len, varnumber_T idx)
1424{
1425 varnumber_T nchar = idx;
1426 size_t nbyte = 0;
1427
1428 if (nchar >= 0)
1429 {
1430 while (nchar > 0 && nbyte < str_len)
1431 {
Bram Moolenaar0289a092021-03-14 18:40:19 +01001432 nbyte += mb_ptr2len(str + nbyte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001433 --nchar;
1434 }
1435 }
1436 else
1437 {
1438 nbyte = str_len;
1439 while (nchar < 0 && nbyte > 0)
1440 {
1441 --nbyte;
1442 nbyte -= mb_head_off(str, str + nbyte);
1443 ++nchar;
1444 }
1445 if (nchar < 0)
1446 return -1;
1447 }
1448 return (long)nbyte;
1449}
1450
1451/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001452 * Return the slice "str[first : last]" using character indexes. Composing
1453 * characters are included.
Bram Moolenaar6601b622021-01-13 21:47:15 +01001454 * "exclusive" is TRUE for slice().
Bram Moolenaare7525c52021-01-09 13:20:37 +01001455 * Return NULL when the result is empty.
1456 */
1457 char_u *
Bram Moolenaar6601b622021-01-13 21:47:15 +01001458string_slice(char_u *str, varnumber_T first, varnumber_T last, int exclusive)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001459{
1460 long start_byte, end_byte;
1461 size_t slen;
1462
1463 if (str == NULL)
1464 return NULL;
1465 slen = STRLEN(str);
1466 start_byte = char_idx2byte(str, slen, first);
1467 if (start_byte < 0)
1468 start_byte = 0; // first index very negative: use zero
Bram Moolenaar6601b622021-01-13 21:47:15 +01001469 if ((last == -1 && !exclusive) || last == VARNUM_MAX)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001470 end_byte = (long)slen;
1471 else
1472 {
1473 end_byte = char_idx2byte(str, slen, last);
Bram Moolenaar6601b622021-01-13 21:47:15 +01001474 if (!exclusive && end_byte >= 0 && end_byte < (long)slen)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001475 // end index is inclusive
Bram Moolenaar0289a092021-03-14 18:40:19 +01001476 end_byte += mb_ptr2len(str + end_byte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001477 }
1478
1479 if (start_byte >= (long)slen || end_byte <= start_byte)
1480 return NULL;
1481 return vim_strnsave(str + start_byte, end_byte - start_byte);
1482}
1483
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001484/*
1485 * Get a script variable for ISN_STORESCRIPT and ISN_LOADSCRIPT.
1486 * When "dfunc_idx" is negative don't give an error.
1487 * Returns NULL for an error.
1488 */
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001489 static svar_T *
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001490get_script_svar(scriptref_T *sref, int dfunc_idx)
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001491{
1492 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001493 dfunc_T *dfunc = dfunc_idx < 0 ? NULL
1494 : ((dfunc_T *)def_functions.ga_data) + dfunc_idx;
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001495 svar_T *sv;
1496
1497 if (sref->sref_seq != si->sn_script_seq)
1498 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001499 // The script was reloaded after the function was compiled, the
1500 // script_idx may not be valid.
1501 if (dfunc != NULL)
1502 semsg(_(e_script_variable_invalid_after_reload_in_function_str),
1503 printable_func_name(dfunc->df_ufunc));
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001504 return NULL;
1505 }
1506 sv = ((svar_T *)si->sn_var_vals.ga_data) + sref->sref_idx;
Bram Moolenaar60dc8272021-07-29 22:48:54 +02001507 if (!equal_type(sv->sv_type, sref->sref_type, 0))
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001508 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001509 if (dfunc != NULL)
1510 emsg(_(e_script_variable_type_changed));
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001511 return NULL;
1512 }
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001513
1514 if (!sv->sv_export && sref->sref_sid != current_sctx.sc_sid)
1515 {
1516 if (dfunc != NULL)
1517 semsg(_(e_item_not_exported_in_script_str), sv->sv_name);
1518 return NULL;
1519 }
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001520 return sv;
1521}
1522
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001523/*
Bram Moolenaar20677332021-06-06 17:02:53 +02001524 * Function passed to do_cmdline() for splitting a script joined by NL
1525 * characters.
1526 */
1527 static char_u *
1528get_split_sourceline(
1529 int c UNUSED,
1530 void *cookie,
1531 int indent UNUSED,
1532 getline_opt_T options UNUSED)
1533{
1534 source_cookie_T *sp = (source_cookie_T *)cookie;
1535 char_u *p;
1536 char_u *line;
1537
Bram Moolenaar20677332021-06-06 17:02:53 +02001538 p = vim_strchr(sp->nextline, '\n');
1539 if (p == NULL)
1540 {
1541 line = vim_strsave(sp->nextline);
1542 sp->nextline += STRLEN(sp->nextline);
1543 }
1544 else
1545 {
1546 line = vim_strnsave(sp->nextline, p - sp->nextline);
1547 sp->nextline = p + 1;
1548 }
1549 return line;
1550}
1551
1552/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001553 * Execute a function by "name".
1554 * This can be a builtin function, user function or a funcref.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001555 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001556 */
1557 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001558call_eval_func(
1559 char_u *name,
1560 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001561 ectx_T *ectx,
1562 isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001563{
Bram Moolenaared677f52020-08-12 16:38:10 +02001564 int called_emsg_before = called_emsg;
1565 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001566
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001567 res = call_by_name(name, argcount, ectx, iptr, NULL);
Bram Moolenaared677f52020-08-12 16:38:10 +02001568 if (res == FAIL && called_emsg == called_emsg_before)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001569 {
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001570 dictitem_T *v;
1571
1572 v = find_var(name, NULL, FALSE);
1573 if (v == NULL)
1574 {
Bram Moolenaare1242042021-12-16 20:56:57 +00001575 semsg(_(e_unknown_function_str), name);
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001576 return FAIL;
1577 }
1578 if (v->di_tv.v_type != VAR_PARTIAL && v->di_tv.v_type != VAR_FUNC)
1579 {
Bram Moolenaare1242042021-12-16 20:56:57 +00001580 semsg(_(e_unknown_function_str), name);
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001581 return FAIL;
1582 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001583 return call_partial(&v->di_tv, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001584 }
Bram Moolenaared677f52020-08-12 16:38:10 +02001585 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001586}
1587
1588/*
Bram Moolenaarf112f302020-12-20 17:47:52 +01001589 * When a function reference is used, fill a partial with the information
1590 * needed, especially when it is used as a closure.
1591 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001592 int
Bram Moolenaarf112f302020-12-20 17:47:52 +01001593fill_partial_and_closure(partial_T *pt, ufunc_T *ufunc, ectx_T *ectx)
1594{
1595 pt->pt_func = ufunc;
1596 pt->pt_refcount = 1;
1597
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001598 if (ufunc->uf_flags & FC_CLOSURE)
Bram Moolenaarf112f302020-12-20 17:47:52 +01001599 {
1600 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1601 + ectx->ec_dfunc_idx;
1602
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001603 // The closure may need to find arguments and local variables in the
1604 // current stack.
Bram Moolenaar0186e582021-01-10 18:33:11 +01001605 pt->pt_outer.out_stack = &ectx->ec_stack;
1606 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001607 if (ectx->ec_outer_ref != NULL)
1608 {
1609 // The current context already has a context, link to that one.
1610 pt->pt_outer.out_up = ectx->ec_outer_ref->or_outer;
1611 if (ectx->ec_outer_ref->or_partial != NULL)
1612 {
1613 pt->pt_outer.out_up_partial = ectx->ec_outer_ref->or_partial;
1614 ++pt->pt_outer.out_up_partial->pt_refcount;
1615 }
1616 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01001617
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001618 // If this function returns and the closure is still being used, we
1619 // need to make a copy of the context (arguments and local variables).
1620 // Store a reference to the partial so we can handle that.
Bram Moolenaar35578162021-08-02 19:10:38 +02001621 if (GA_GROW_FAILS(&ectx->ec_funcrefs, 1))
Bram Moolenaarf112f302020-12-20 17:47:52 +01001622 {
1623 vim_free(pt);
1624 return FAIL;
1625 }
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001626 // Extra variable keeps the count of closures created in the current
1627 // function call.
Bram Moolenaarf112f302020-12-20 17:47:52 +01001628 ++(((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx
1629 + STACK_FRAME_SIZE + dfunc->df_varcount)->vval.v_number;
1630
1631 ((partial_T **)ectx->ec_funcrefs.ga_data)
1632 [ectx->ec_funcrefs.ga_len] = pt;
1633 ++pt->pt_refcount;
1634 ++ectx->ec_funcrefs.ga_len;
1635 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001636 ++ufunc->uf_refcount;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001637 return OK;
1638}
1639
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001640/*
1641 * Execute iptr->isn_arg.string as an Ex command.
1642 */
1643 static int
1644exec_command(isn_T *iptr)
1645{
1646 source_cookie_T cookie;
1647
1648 SOURCING_LNUM = iptr->isn_lnum;
1649 // Pass getsourceline to get an error for a missing ":end"
1650 // command.
1651 CLEAR_FIELD(cookie);
1652 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1653 if (do_cmdline(iptr->isn_arg.string,
1654 getsourceline, &cookie,
1655 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED) == FAIL
1656 || did_emsg)
1657 return FAIL;
1658 return OK;
1659}
1660
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001661// used for v_instr of typval of VAR_INSTR
1662struct instr_S {
1663 ectx_T *instr_ectx;
1664 isn_T *instr_instr;
1665};
1666
Bram Moolenaar4c137212021-04-19 16:48:48 +02001667// used for substitute_instr
1668typedef struct subs_expr_S {
1669 ectx_T *subs_ectx;
1670 isn_T *subs_instr;
1671 int subs_status;
1672} subs_expr_T;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001673
1674// Get pointer to item in the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001675#define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001676
1677// Get pointer to item at the bottom of the stack, -1 is the bottom.
1678#undef STACK_TV_BOT
Bram Moolenaar4c137212021-04-19 16:48:48 +02001679#define STACK_TV_BOT(idx) (((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_stack.ga_len + idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001680
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001681// Get pointer to a local variable on the stack. Negative for arguments.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001682#define STACK_TV_VAR(idx) (((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx + STACK_FRAME_SIZE + idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001683
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001684// Set when calling do_debug().
1685static ectx_T *debug_context = NULL;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001686static int debug_var_count;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001687
1688/*
1689 * When debugging lookup "name" and return the typeval.
1690 * When not found return NULL.
1691 */
1692 typval_T *
1693lookup_debug_var(char_u *name)
1694{
1695 int idx;
1696 dfunc_T *dfunc;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001697 ufunc_T *ufunc;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001698 ectx_T *ectx = debug_context;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001699 int varargs_off;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001700
1701 if (ectx == NULL)
1702 return NULL;
1703 dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
1704
1705 // Go through the local variable names, from last to first.
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001706 for (idx = debug_var_count - 1; idx >= 0; --idx)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001707 {
Bram Moolenaare406ff82022-03-10 20:47:43 +00001708 char_u *varname = ((char_u **)dfunc->df_var_names.ga_data)[idx];
1709
1710 // the variable name may be NULL when not available in this block
1711 if (varname != NULL && STRCMP(varname, name) == 0)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001712 return STACK_TV_VAR(idx);
1713 }
1714
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001715 // Go through argument names.
1716 ufunc = dfunc->df_ufunc;
1717 varargs_off = ufunc->uf_va_name == NULL ? 0 : 1;
1718 for (idx = 0; idx < ufunc->uf_args.ga_len; ++idx)
1719 if (STRCMP(((char_u **)(ufunc->uf_args.ga_data))[idx], name) == 0)
1720 return STACK_TV(ectx->ec_frame_idx - ufunc->uf_args.ga_len
1721 - varargs_off + idx);
1722 if (ufunc->uf_va_name != NULL && STRCMP(ufunc->uf_va_name, name) == 0)
1723 return STACK_TV(ectx->ec_frame_idx - 1);
1724
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001725 return NULL;
1726}
1727
Bram Moolenaar26a44842021-09-02 18:49:06 +02001728/*
1729 * Return TRUE if there might be a breakpoint in "ufunc", which is when a
1730 * breakpoint was set in that function or when there is any expression.
1731 */
1732 int
1733may_break_in_function(ufunc_T *ufunc)
1734{
1735 return ufunc->uf_has_breakpoint || debug_has_expr_breakpoint();
1736}
1737
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001738 static void
1739handle_debug(isn_T *iptr, ectx_T *ectx)
1740{
1741 char_u *line;
1742 ufunc_T *ufunc = (((dfunc_T *)def_functions.ga_data)
1743 + ectx->ec_dfunc_idx)->df_ufunc;
1744 isn_T *ni;
1745 int end_lnum = iptr->isn_lnum;
1746 garray_T ga;
1747 int lnum;
1748
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02001749 if (ex_nesting_level > debug_break_level)
1750 {
1751 linenr_T breakpoint;
1752
Bram Moolenaar26a44842021-09-02 18:49:06 +02001753 if (!may_break_in_function(ufunc))
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02001754 return;
1755
1756 // check for the next breakpoint if needed
1757 breakpoint = dbg_find_breakpoint(FALSE, ufunc->uf_name,
Bram Moolenaar8cec9272021-06-23 20:20:53 +02001758 iptr->isn_arg.debug.dbg_break_lnum);
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02001759 if (breakpoint <= 0 || breakpoint > iptr->isn_lnum)
1760 return;
1761 }
1762
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001763 SOURCING_LNUM = iptr->isn_lnum;
1764 debug_context = ectx;
Bram Moolenaar8cec9272021-06-23 20:20:53 +02001765 debug_var_count = iptr->isn_arg.debug.dbg_var_names_len;
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001766
1767 for (ni = iptr + 1; ni->isn_type != ISN_FINISH; ++ni)
1768 if (ni->isn_type == ISN_DEBUG
1769 || ni->isn_type == ISN_RETURN
1770 || ni->isn_type == ISN_RETURN_VOID)
1771 {
Bram Moolenaar112bed02021-11-23 22:16:34 +00001772 end_lnum = ni->isn_lnum + (ni->isn_type == ISN_DEBUG ? 0 : 1);
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001773 break;
1774 }
1775
1776 if (end_lnum > iptr->isn_lnum)
1777 {
1778 ga_init2(&ga, sizeof(char_u *), 10);
Bram Moolenaar310091d2021-12-23 21:14:37 +00001779 for (lnum = iptr->isn_lnum; lnum < end_lnum
1780 && lnum <= ufunc->uf_lines.ga_len; ++lnum)
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001781 {
Bram Moolenaar303215d2021-07-07 20:10:43 +02001782 char_u *p = ((char_u **)ufunc->uf_lines.ga_data)[lnum - 1];
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001783
Bram Moolenaar303215d2021-07-07 20:10:43 +02001784 if (p == NULL)
1785 continue; // left over from continuation line
1786 p = skipwhite(p);
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001787 if (*p == '#')
1788 break;
Bram Moolenaar35578162021-08-02 19:10:38 +02001789 if (GA_GROW_OK(&ga, 1))
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001790 ((char_u **)(ga.ga_data))[ga.ga_len++] = p;
1791 if (STRNCMP(p, "def ", 4) == 0)
1792 break;
1793 }
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001794 line = ga_concat_strings(&ga, " ");
1795 vim_free(ga.ga_data);
1796 }
1797 else
1798 line = ((char_u **)ufunc->uf_lines.ga_data)[iptr->isn_lnum - 1];
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001799
Dominique Pelle6e969552021-06-17 13:53:41 +02001800 do_debug(line == NULL ? (char_u *)"[empty]" : line);
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001801 debug_context = NULL;
1802
1803 if (end_lnum > iptr->isn_lnum)
1804 vim_free(line);
1805}
1806
Bram Moolenaar4c137212021-04-19 16:48:48 +02001807/*
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00001808 * Store a value in a list, dict or blob variable.
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001809 * Returns OK, FAIL or NOTDONE (uncatchable error).
1810 */
1811 static int
1812execute_storeindex(isn_T *iptr, ectx_T *ectx)
1813{
1814 vartype_T dest_type = iptr->isn_arg.vartype;
1815 typval_T *tv;
1816 typval_T *tv_idx = STACK_TV_BOT(-2);
1817 typval_T *tv_dest = STACK_TV_BOT(-1);
1818 int status = OK;
1819
1820 // Stack contains:
1821 // -3 value to be stored
1822 // -2 index
1823 // -1 dict or list
1824 tv = STACK_TV_BOT(-3);
1825 SOURCING_LNUM = iptr->isn_lnum;
1826 if (dest_type == VAR_ANY)
1827 {
1828 dest_type = tv_dest->v_type;
1829 if (dest_type == VAR_DICT)
1830 status = do_2string(tv_idx, TRUE, FALSE);
1831 else if (dest_type == VAR_LIST && tv_idx->v_type != VAR_NUMBER)
1832 {
1833 emsg(_(e_number_expected));
1834 status = FAIL;
1835 }
1836 }
Bram Moolenaare08be092022-02-17 13:08:26 +00001837
1838 if (status == OK)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001839 {
Bram Moolenaare08be092022-02-17 13:08:26 +00001840 if (dest_type == VAR_LIST)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001841 {
Bram Moolenaare08be092022-02-17 13:08:26 +00001842 long lidx = (long)tv_idx->vval.v_number;
1843 list_T *list = tv_dest->vval.v_list;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001844
Bram Moolenaare08be092022-02-17 13:08:26 +00001845 if (list == NULL)
1846 {
1847 emsg(_(e_list_not_set));
1848 return FAIL;
1849 }
1850 if (lidx < 0 && list->lv_len + lidx >= 0)
1851 // negative index is relative to the end
1852 lidx = list->lv_len + lidx;
1853 if (lidx < 0 || lidx > list->lv_len)
1854 {
1855 semsg(_(e_list_index_out_of_range_nr), lidx);
1856 return FAIL;
1857 }
1858 if (lidx < list->lv_len)
1859 {
1860 listitem_T *li = list_find(list, lidx);
1861
1862 if (error_if_locked(li->li_tv.v_lock,
Bram Moolenaar70c43d82022-01-26 21:01:15 +00001863 e_cannot_change_locked_list_item))
Bram Moolenaare08be092022-02-17 13:08:26 +00001864 return FAIL;
1865 // overwrite existing list item
1866 clear_tv(&li->li_tv);
1867 li->li_tv = *tv;
1868 }
1869 else
1870 {
1871 if (error_if_locked(list->lv_lock, e_cannot_change_locked_list))
1872 return FAIL;
1873 // append to list, only fails when out of memory
1874 if (list_append_tv(list, tv) == FAIL)
1875 return NOTDONE;
1876 clear_tv(tv);
1877 }
1878 }
1879 else if (dest_type == VAR_DICT)
1880 {
1881 char_u *key = tv_idx->vval.v_string;
1882 dict_T *dict = tv_dest->vval.v_dict;
1883 dictitem_T *di;
1884
1885 SOURCING_LNUM = iptr->isn_lnum;
1886 if (dict == NULL)
1887 {
1888 emsg(_(e_dictionary_not_set));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001889 return FAIL;
Bram Moolenaare08be092022-02-17 13:08:26 +00001890 }
1891 if (key == NULL)
1892 key = (char_u *)"";
1893 di = dict_find(dict, key, -1);
1894 if (di != NULL)
1895 {
1896 if (error_if_locked(di->di_tv.v_lock,
1897 e_cannot_change_dict_item))
1898 return FAIL;
1899 // overwrite existing value
1900 clear_tv(&di->di_tv);
1901 di->di_tv = *tv;
1902 }
1903 else
1904 {
1905 if (error_if_locked(dict->dv_lock, e_cannot_change_dict))
1906 return FAIL;
1907 // add to dict, only fails when out of memory
1908 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
1909 return NOTDONE;
1910 clear_tv(tv);
1911 }
1912 }
1913 else if (dest_type == VAR_BLOB)
1914 {
1915 long lidx = (long)tv_idx->vval.v_number;
1916 blob_T *blob = tv_dest->vval.v_blob;
1917 varnumber_T nr;
1918 int error = FALSE;
1919 int len;
1920
1921 if (blob == NULL)
1922 {
1923 emsg(_(e_blob_not_set));
1924 return FAIL;
1925 }
1926 len = blob_len(blob);
1927 if (lidx < 0 && len + lidx >= 0)
1928 // negative index is relative to the end
1929 lidx = len + lidx;
1930
1931 // Can add one byte at the end.
1932 if (lidx < 0 || lidx > len)
1933 {
1934 semsg(_(e_blob_index_out_of_range_nr), lidx);
1935 return FAIL;
1936 }
1937 if (value_check_lock(blob->bv_lock, (char_u *)"blob", FALSE))
1938 return FAIL;
1939 nr = tv_get_number_chk(tv, &error);
1940 if (error)
1941 return FAIL;
1942 blob_set_append(blob, lidx, nr);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001943 }
1944 else
1945 {
Bram Moolenaare08be092022-02-17 13:08:26 +00001946 status = FAIL;
1947 semsg(_(e_cannot_index_str), vartype_name(dest_type));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001948 }
1949 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001950
1951 clear_tv(tv_idx);
1952 clear_tv(tv_dest);
1953 ectx->ec_stack.ga_len -= 3;
1954 if (status == FAIL)
1955 {
1956 clear_tv(tv);
1957 return FAIL;
1958 }
1959 return OK;
1960}
1961
1962/*
Bram Moolenaarea5c8982022-02-17 14:42:02 +00001963 * Store a value in a list or blob range.
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001964 */
1965 static int
1966execute_storerange(isn_T *iptr, ectx_T *ectx)
1967{
1968 typval_T *tv;
1969 typval_T *tv_idx1 = STACK_TV_BOT(-3);
1970 typval_T *tv_idx2 = STACK_TV_BOT(-2);
1971 typval_T *tv_dest = STACK_TV_BOT(-1);
1972 int status = OK;
1973
1974 // Stack contains:
1975 // -4 value to be stored
1976 // -3 first index or "none"
1977 // -2 second index or "none"
1978 // -1 destination list or blob
1979 tv = STACK_TV_BOT(-4);
Bram Moolenaarea5c8982022-02-17 14:42:02 +00001980 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001981 if (tv_dest->v_type == VAR_LIST)
1982 {
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00001983 long n1;
1984 long n2;
1985 listitem_T *li1;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001986
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00001987 n1 = (long)tv_get_number_chk(tv_idx1, NULL);
1988 if (tv_idx2->v_type == VAR_SPECIAL
1989 && tv_idx2->vval.v_number == VVAL_NONE)
1990 n2 = list_len(tv_dest->vval.v_list) - 1;
1991 else
1992 n2 = (long)tv_get_number_chk(tv_idx2, NULL);
1993
1994 li1 = check_range_index_one(tv_dest->vval.v_list, &n1, FALSE);
1995 if (li1 == NULL)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00001996 status = FAIL;
1997 else
1998 {
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00001999 status = check_range_index_two(tv_dest->vval.v_list,
2000 &n1, li1, &n2, FALSE);
2001 if (status != FAIL)
2002 status = list_assign_range(
2003 tv_dest->vval.v_list,
2004 tv->vval.v_list,
2005 n1,
2006 n2,
2007 tv_idx2->v_type == VAR_SPECIAL,
2008 (char_u *)"=",
2009 (char_u *)"[unknown]");
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002010 }
2011 }
2012 else if (tv_dest->v_type == VAR_BLOB)
2013 {
2014 varnumber_T n1;
2015 varnumber_T n2;
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002016 long bloblen;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002017
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002018 n1 = tv_get_number_chk(tv_idx1, NULL);
2019 if (tv_idx2->v_type == VAR_SPECIAL
2020 && tv_idx2->vval.v_number == VVAL_NONE)
2021 n2 = blob_len(tv_dest->vval.v_blob) - 1;
2022 else
2023 n2 = tv_get_number_chk(tv_idx2, NULL);
2024 bloblen = blob_len(tv_dest->vval.v_blob);
2025
2026 if (check_blob_index(bloblen, n1, FALSE) == FAIL
2027 || check_blob_range(bloblen, n1, n2, FALSE) == FAIL)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002028 status = FAIL;
2029 else
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002030 status = blob_set_range(tv_dest->vval.v_blob, n1, n2, tv);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002031 }
2032 else
2033 {
2034 status = FAIL;
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002035 emsg(_(e_list_or_blob_required));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002036 }
2037
2038 clear_tv(tv_idx1);
2039 clear_tv(tv_idx2);
2040 clear_tv(tv_dest);
2041 ectx->ec_stack.ga_len -= 4;
2042 clear_tv(tv);
2043
2044 return status;
2045}
2046
2047/*
2048 * Unlet item in list or dict variable.
2049 */
2050 static int
2051execute_unletindex(isn_T *iptr, ectx_T *ectx)
2052{
2053 typval_T *tv_idx = STACK_TV_BOT(-2);
2054 typval_T *tv_dest = STACK_TV_BOT(-1);
2055 int status = OK;
2056
2057 // Stack contains:
2058 // -2 index
2059 // -1 dict or list
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002060 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002061 if (tv_dest->v_type == VAR_DICT)
2062 {
2063 // unlet a dict item, index must be a string
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002064 if (tv_idx->v_type != VAR_STRING && tv_idx->v_type != VAR_NUMBER)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002065 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002066 semsg(_(e_expected_str_but_got_str),
2067 vartype_name(VAR_STRING),
2068 vartype_name(tv_idx->v_type));
2069 status = FAIL;
2070 }
2071 else
2072 {
2073 dict_T *d = tv_dest->vval.v_dict;
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002074 char_u *key;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002075 dictitem_T *di = NULL;
2076
2077 if (d != NULL && value_check_lock(
2078 d->dv_lock, NULL, FALSE))
2079 status = FAIL;
2080 else
2081 {
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002082 if (tv_idx->v_type == VAR_STRING)
2083 {
2084 key = tv_idx->vval.v_string;
2085 if (key == NULL)
2086 key = (char_u *)"";
2087 }
2088 else
2089 {
2090 key = tv_get_string(tv_idx);
2091 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002092 if (d != NULL)
2093 di = dict_find(d, key, (int)STRLEN(key));
2094 if (di == NULL)
2095 {
2096 // NULL dict is equivalent to empty dict
2097 semsg(_(e_key_not_present_in_dictionary),
2098 key);
2099 status = FAIL;
2100 }
2101 else if (var_check_fixed(di->di_flags,
2102 NULL, FALSE)
2103 || var_check_ro(di->di_flags,
2104 NULL, FALSE))
2105 status = FAIL;
2106 else
2107 dictitem_remove(d, di);
2108 }
2109 }
2110 }
2111 else if (tv_dest->v_type == VAR_LIST)
2112 {
2113 // unlet a List item, index must be a number
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002114 if (check_for_number(tv_idx) == FAIL)
2115 {
2116 status = FAIL;
2117 }
2118 else
2119 {
2120 list_T *l = tv_dest->vval.v_list;
2121 long n = (long)tv_idx->vval.v_number;
2122
2123 if (l != NULL && value_check_lock(
2124 l->lv_lock, NULL, FALSE))
2125 status = FAIL;
2126 else
2127 {
2128 listitem_T *li = list_find(l, n);
2129
2130 if (li == NULL)
2131 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002132 semsg(_(e_list_index_out_of_range_nr), n);
2133 status = FAIL;
2134 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002135 else
2136 listitem_remove(l, li);
2137 }
2138 }
2139 }
2140 else
2141 {
2142 status = FAIL;
2143 semsg(_(e_cannot_index_str),
2144 vartype_name(tv_dest->v_type));
2145 }
2146
2147 clear_tv(tv_idx);
2148 clear_tv(tv_dest);
2149 ectx->ec_stack.ga_len -= 2;
2150
2151 return status;
2152}
2153
2154/*
2155 * Unlet a range of items in a list variable.
2156 */
2157 static int
2158execute_unletrange(isn_T *iptr, ectx_T *ectx)
2159{
2160 // Stack contains:
2161 // -3 index1
2162 // -2 index2
2163 // -1 dict or list
2164 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2165 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2166 typval_T *tv_dest = STACK_TV_BOT(-1);
2167 int status = OK;
2168
2169 if (tv_dest->v_type == VAR_LIST)
2170 {
2171 // indexes must be a number
2172 SOURCING_LNUM = iptr->isn_lnum;
2173 if (check_for_number(tv_idx1) == FAIL
2174 || (tv_idx2->v_type != VAR_SPECIAL
2175 && check_for_number(tv_idx2) == FAIL))
2176 {
2177 status = FAIL;
2178 }
2179 else
2180 {
2181 list_T *l = tv_dest->vval.v_list;
2182 long n1 = (long)tv_idx1->vval.v_number;
2183 long n2 = tv_idx2->v_type == VAR_SPECIAL
2184 ? 0 : (long)tv_idx2->vval.v_number;
2185 listitem_T *li;
2186
2187 li = list_find_index(l, &n1);
2188 if (li == NULL)
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002189 {
2190 semsg(_(e_list_index_out_of_range_nr),
2191 (long)tv_idx1->vval.v_number);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002192 status = FAIL;
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002193 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002194 else
2195 {
2196 if (n1 < 0)
2197 n1 = list_idx_of_item(l, li);
2198 if (n2 < 0)
2199 {
2200 listitem_T *li2 = list_find(l, n2);
2201
2202 if (li2 == NULL)
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002203 {
2204 semsg(_(e_list_index_out_of_range_nr), n2);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002205 status = FAIL;
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002206 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002207 else
2208 n2 = list_idx_of_item(l, li2);
2209 }
2210 if (status != FAIL
2211 && tv_idx2->v_type != VAR_SPECIAL
2212 && n2 < n1)
2213 {
2214 semsg(_(e_list_index_out_of_range_nr), n2);
2215 status = FAIL;
2216 }
Bram Moolenaar6b8c7ba2022-03-20 17:46:06 +00002217 if (status != FAIL)
2218 list_unlet_range(l, li, n1,
2219 tv_idx2->v_type != VAR_SPECIAL, n2);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002220 }
2221 }
2222 }
2223 else
2224 {
2225 status = FAIL;
2226 SOURCING_LNUM = iptr->isn_lnum;
2227 semsg(_(e_cannot_index_str),
2228 vartype_name(tv_dest->v_type));
2229 }
2230
2231 clear_tv(tv_idx1);
2232 clear_tv(tv_idx2);
2233 clear_tv(tv_dest);
2234 ectx->ec_stack.ga_len -= 3;
2235
2236 return status;
2237}
2238
2239/*
2240 * Top of a for loop.
2241 */
2242 static int
2243execute_for(isn_T *iptr, ectx_T *ectx)
2244{
2245 typval_T *tv;
2246 typval_T *ltv = STACK_TV_BOT(-1);
2247 typval_T *idxtv =
2248 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
2249
2250 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
2251 return FAIL;
2252 if (ltv->v_type == VAR_LIST)
2253 {
2254 list_T *list = ltv->vval.v_list;
2255
2256 // push the next item from the list
2257 ++idxtv->vval.v_number;
2258 if (list == NULL
2259 || idxtv->vval.v_number >= list->lv_len)
2260 {
2261 // past the end of the list, jump to "endfor"
2262 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
2263 may_restore_cmdmod(&ectx->ec_funclocal);
2264 }
2265 else if (list->lv_first == &range_list_item)
2266 {
2267 // non-materialized range() list
2268 tv = STACK_TV_BOT(0);
2269 tv->v_type = VAR_NUMBER;
2270 tv->v_lock = 0;
2271 tv->vval.v_number = list_find_nr(
2272 list, idxtv->vval.v_number, NULL);
2273 ++ectx->ec_stack.ga_len;
2274 }
2275 else
2276 {
2277 listitem_T *li = list_find(list,
2278 idxtv->vval.v_number);
2279
2280 copy_tv(&li->li_tv, STACK_TV_BOT(0));
2281 ++ectx->ec_stack.ga_len;
2282 }
2283 }
2284 else if (ltv->v_type == VAR_STRING)
2285 {
2286 char_u *str = ltv->vval.v_string;
2287
2288 // The index is for the last byte of the previous
2289 // character.
2290 ++idxtv->vval.v_number;
2291 if (str == NULL || str[idxtv->vval.v_number] == NUL)
2292 {
2293 // past the end of the string, jump to "endfor"
2294 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
2295 may_restore_cmdmod(&ectx->ec_funclocal);
2296 }
2297 else
2298 {
2299 int clen = mb_ptr2len(str + idxtv->vval.v_number);
2300
2301 // Push the next character from the string.
2302 tv = STACK_TV_BOT(0);
2303 tv->v_type = VAR_STRING;
2304 tv->vval.v_string = vim_strnsave(
2305 str + idxtv->vval.v_number, clen);
2306 ++ectx->ec_stack.ga_len;
2307 idxtv->vval.v_number += clen - 1;
2308 }
2309 }
2310 else if (ltv->v_type == VAR_BLOB)
2311 {
2312 blob_T *blob = ltv->vval.v_blob;
2313
2314 // When we get here the first time make a copy of the
2315 // blob, so that the iteration still works when it is
2316 // changed.
2317 if (idxtv->vval.v_number == -1 && blob != NULL)
2318 {
2319 blob_copy(blob, ltv);
2320 blob_unref(blob);
2321 blob = ltv->vval.v_blob;
2322 }
2323
2324 // The index is for the previous byte.
2325 ++idxtv->vval.v_number;
2326 if (blob == NULL
2327 || idxtv->vval.v_number >= blob_len(blob))
2328 {
2329 // past the end of the blob, jump to "endfor"
2330 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
2331 may_restore_cmdmod(&ectx->ec_funclocal);
2332 }
2333 else
2334 {
2335 // Push the next byte from the blob.
2336 tv = STACK_TV_BOT(0);
2337 tv->v_type = VAR_NUMBER;
2338 tv->vval.v_number = blob_get(blob,
2339 idxtv->vval.v_number);
2340 ++ectx->ec_stack.ga_len;
2341 }
2342 }
2343 else
2344 {
2345 semsg(_(e_for_loop_on_str_not_supported),
2346 vartype_name(ltv->v_type));
2347 return FAIL;
2348 }
2349 return OK;
2350}
2351
2352/*
Bram Moolenaar06b77222022-01-25 15:51:56 +00002353 * Load instruction for w:/b:/g:/t: variable.
2354 * "isn_type" is used instead of "iptr->isn_type".
2355 */
2356 static int
2357load_namespace_var(ectx_T *ectx, isntype_T isn_type, isn_T *iptr)
2358{
2359 dictitem_T *di = NULL;
2360 hashtab_T *ht = NULL;
2361 char namespace;
2362
2363 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
2364 return NOTDONE;
2365 switch (isn_type)
2366 {
2367 case ISN_LOADG:
2368 ht = get_globvar_ht();
2369 namespace = 'g';
2370 break;
2371 case ISN_LOADB:
2372 ht = &curbuf->b_vars->dv_hashtab;
2373 namespace = 'b';
2374 break;
2375 case ISN_LOADW:
2376 ht = &curwin->w_vars->dv_hashtab;
2377 namespace = 'w';
2378 break;
2379 case ISN_LOADT:
2380 ht = &curtab->tp_vars->dv_hashtab;
2381 namespace = 't';
2382 break;
2383 default: // Cannot reach here
2384 return NOTDONE;
2385 }
2386 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
2387
Bram Moolenaar06b77222022-01-25 15:51:56 +00002388 if (di == NULL)
2389 {
Bram Moolenaarfe732552022-02-22 19:39:13 +00002390 if (isn_type == ISN_LOADG)
2391 {
2392 ufunc_T *ufunc = find_func(iptr->isn_arg.string, TRUE);
2393
2394 // g:Something could be a function
2395 if (ufunc != NULL)
2396 {
2397 typval_T *tv = STACK_TV_BOT(0);
2398
2399 ++ectx->ec_stack.ga_len;
2400 tv->v_type = VAR_FUNC;
2401 tv->vval.v_string = alloc(STRLEN(iptr->isn_arg.string) + 3);
2402 if (tv->vval.v_string == NULL)
2403 return FAIL;
2404 STRCPY(tv->vval.v_string, "g:");
2405 STRCPY(tv->vval.v_string + 2, iptr->isn_arg.string);
2406 return OK;
2407 }
2408 }
Bram Moolenaar06b77222022-01-25 15:51:56 +00002409 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarfe732552022-02-22 19:39:13 +00002410 if (vim_strchr(iptr->isn_arg.string, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar06b77222022-01-25 15:51:56 +00002411 // no check if the item exists in the script but
2412 // isn't exported, it is too complicated
Bram Moolenaarfe732552022-02-22 19:39:13 +00002413 semsg(_(e_item_not_found_in_script_str), iptr->isn_arg.string);
Bram Moolenaar06b77222022-01-25 15:51:56 +00002414 else
2415 semsg(_(e_undefined_variable_char_str),
Bram Moolenaarfe732552022-02-22 19:39:13 +00002416 namespace, iptr->isn_arg.string);
Bram Moolenaar06b77222022-01-25 15:51:56 +00002417 return FAIL;
2418 }
2419 else
2420 {
2421 copy_tv(&di->di_tv, STACK_TV_BOT(0));
2422 ++ectx->ec_stack.ga_len;
2423 }
2424 return OK;
2425}
2426
2427/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02002428 * Execute instructions in execution context "ectx".
2429 * Return OK or FAIL;
2430 */
2431 static int
2432exec_instructions(ectx_T *ectx)
2433{
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002434 int ret = FAIL;
2435 int save_trylevel_at_start = ectx->ec_trylevel_at_start;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02002436 int dict_stack_len_at_start = dict_stack.ga_len;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01002437
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02002438 // Start execution at the first instruction.
Bram Moolenaar4c137212021-04-19 16:48:48 +02002439 ectx->ec_iidx = 0;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01002440
Bram Moolenaarff652882021-05-16 15:24:49 +02002441 // Only catch exceptions in this instruction list.
2442 ectx->ec_trylevel_at_start = trylevel;
2443
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002444 for (;;)
2445 {
Bram Moolenaara7490192021-07-22 12:26:14 +02002446 static int breakcheck_count = 0; // using "static" makes it faster
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002447 isn_T *iptr;
Bram Moolenaara7490192021-07-22 12:26:14 +02002448 typval_T *tv;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002449
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002450 if (unlikely(++breakcheck_count >= 100))
Bram Moolenaar270d0382020-05-15 21:42:53 +02002451 {
2452 line_breakcheck();
2453 breakcheck_count = 0;
2454 }
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002455 if (unlikely(got_int))
Bram Moolenaar20431c92020-03-20 18:39:46 +01002456 {
2457 // Turn CTRL-C into an exception.
2458 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +01002459 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002460 goto theend;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002461 did_throw = TRUE;
2462 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002463
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002464 if (unlikely(did_emsg && msg_list != NULL && *msg_list != NULL))
Bram Moolenaara26b9702020-04-18 19:53:28 +02002465 {
2466 // Turn an error message into an exception.
2467 did_emsg = FALSE;
2468 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002469 goto theend;
Bram Moolenaara26b9702020-04-18 19:53:28 +02002470 did_throw = TRUE;
2471 *msg_list = NULL;
2472 }
2473
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002474 if (unlikely(did_throw))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002475 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002476 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002477 trycmd_T *trycmd = NULL;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002478 int index = trystack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002479
2480 // An exception jumps to the first catch, finally, or returns from
2481 // the current function.
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002482 while (index > 0)
2483 {
2484 trycmd = ((trycmd_T *)trystack->ga_data) + index - 1;
Bram Moolenaar834193a2021-06-30 20:39:15 +02002485 if (!trycmd->tcd_in_catch || trycmd->tcd_finally_idx != 0)
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002486 break;
2487 // In the catch and finally block of this try we have to go up
2488 // one level.
2489 --index;
2490 trycmd = NULL;
2491 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02002492 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002493 {
Bram Moolenaar834193a2021-06-30 20:39:15 +02002494 if (trycmd->tcd_in_catch)
2495 {
2496 // exception inside ":catch", jump to ":finally" once
2497 ectx->ec_iidx = trycmd->tcd_finally_idx;
2498 trycmd->tcd_finally_idx = 0;
2499 }
2500 else
2501 // jump to first ":catch"
2502 ectx->ec_iidx = trycmd->tcd_catch_idx;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002503 trycmd->tcd_in_catch = TRUE;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002504 did_throw = FALSE; // don't come back here until :endtry
2505 trycmd->tcd_did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002506 }
2507 else
2508 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02002509 // Not inside try or need to return from current functions.
2510 // Push a dummy return value.
Bram Moolenaar35578162021-08-02 19:10:38 +02002511 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002512 goto theend;
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02002513 tv = STACK_TV_BOT(0);
2514 tv->v_type = VAR_NUMBER;
2515 tv->vval.v_number = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002516 ++ectx->ec_stack.ga_len;
2517 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002518 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02002519 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +01002520 need_rethrow = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002521 if (handle_closure_in_use(ectx, FALSE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002522 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002523 goto done;
2524 }
2525
Bram Moolenaar4c137212021-04-19 16:48:48 +02002526 if (func_return(ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002527 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002528 }
2529 continue;
2530 }
2531
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002532 /*
2533 * Big switch on the instruction. Most compilers will be turning this
2534 * into an efficient lookup table, since the "case" values are an enum
2535 * with sequential numbers. It may look ugly, but it should be the
2536 * most efficient way.
2537 */
Bram Moolenaar4c137212021-04-19 16:48:48 +02002538 iptr = &ectx->ec_instr[ectx->ec_iidx++];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002539 switch (iptr->isn_type)
2540 {
2541 // execute Ex command line
2542 case ISN_EXEC:
Bram Moolenaaraacc9662021-08-13 19:40:51 +02002543 if (exec_command(iptr) == FAIL)
2544 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002545 break;
2546
Bram Moolenaar20677332021-06-06 17:02:53 +02002547 // execute Ex command line split at NL characters.
2548 case ISN_EXEC_SPLIT:
2549 {
2550 source_cookie_T cookie;
Bram Moolenaar518df272021-06-06 17:34:13 +02002551 char_u *line;
Bram Moolenaar20677332021-06-06 17:02:53 +02002552
2553 SOURCING_LNUM = iptr->isn_lnum;
2554 CLEAR_FIELD(cookie);
2555 cookie.sourcing_lnum = iptr->isn_lnum - 1;
2556 cookie.nextline = iptr->isn_arg.string;
Bram Moolenaar518df272021-06-06 17:34:13 +02002557 line = get_split_sourceline(0, &cookie, 0, 0);
2558 if (do_cmdline(line,
Bram Moolenaar20677332021-06-06 17:02:53 +02002559 get_split_sourceline, &cookie,
2560 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED)
2561 == FAIL
2562 || did_emsg)
Bram Moolenaar518df272021-06-06 17:34:13 +02002563 {
2564 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02002565 goto on_error;
Bram Moolenaar518df272021-06-06 17:34:13 +02002566 }
2567 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02002568 }
2569 break;
2570
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002571 // execute Ex command line that is only a range
2572 case ISN_EXECRANGE:
2573 {
2574 exarg_T ea;
2575 char *error = NULL;
2576
2577 CLEAR_FIELD(ea);
2578 ea.cmdidx = CMD_SIZE;
2579 ea.addr_type = ADDR_LINES;
2580 ea.cmd = iptr->isn_arg.string;
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00002581 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002582 parse_cmd_address(&ea, &error, FALSE);
Bram Moolenaar01a4dcb2021-12-04 13:15:10 +00002583 if (ea.cmd == NULL)
2584 goto on_error;
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00002585 // error is always NULL when using ADDR_LINES
2586 error = ex_range_without_command(&ea);
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002587 if (error != NULL)
2588 {
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00002589 emsg(error);
2590 goto on_error;
2591 }
2592 }
2593 break;
2594
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02002595 // Evaluate an expression with legacy syntax, push it onto the
2596 // stack.
2597 case ISN_LEGACY_EVAL:
2598 {
2599 char_u *arg = iptr->isn_arg.string;
2600 int res;
2601 int save_flags = cmdmod.cmod_flags;
2602
Bram Moolenaar35578162021-08-02 19:10:38 +02002603 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002604 goto theend;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02002605 tv = STACK_TV_BOT(0);
2606 init_tv(tv);
2607 cmdmod.cmod_flags |= CMOD_LEGACY;
2608 res = eval0(arg, tv, NULL, &EVALARG_EVALUATE);
2609 cmdmod.cmod_flags = save_flags;
2610 if (res == FAIL)
2611 goto on_error;
2612 ++ectx->ec_stack.ga_len;
2613 }
2614 break;
2615
Bram Moolenaarf18332f2021-05-07 17:55:55 +02002616 // push typeval VAR_INSTR with instructions to be executed
2617 case ISN_INSTR:
2618 {
Bram Moolenaar35578162021-08-02 19:10:38 +02002619 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002620 goto theend;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02002621 tv = STACK_TV_BOT(0);
2622 tv->vval.v_instr = ALLOC_ONE(instr_T);
2623 if (tv->vval.v_instr == NULL)
2624 goto on_error;
2625 ++ectx->ec_stack.ga_len;
2626
2627 tv->v_type = VAR_INSTR;
2628 tv->vval.v_instr->instr_ectx = ectx;
2629 tv->vval.v_instr->instr_instr = iptr->isn_arg.instr;
2630 }
2631 break;
2632
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002633 case ISN_SOURCE:
2634 {
2635 scriptitem_T *si = SCRIPT_ITEM(iptr->isn_arg.number);
2636
2637 if (si->sn_state == SN_STATE_NOT_LOADED)
2638 {
2639 SOURCING_LNUM = iptr->isn_lnum;
2640 if (do_source(si->sn_name, FALSE, DOSO_NONE, NULL)
2641 == FAIL)
2642 goto on_error;
2643 }
2644 }
2645 break;
2646
Bram Moolenaar4c137212021-04-19 16:48:48 +02002647 // execute :substitute with an expression
2648 case ISN_SUBSTITUTE:
2649 {
2650 subs_T *subs = &iptr->isn_arg.subs;
2651 source_cookie_T cookie;
2652 struct subs_expr_S *save_instr = substitute_instr;
2653 struct subs_expr_S subs_instr;
2654 int res;
2655
2656 subs_instr.subs_ectx = ectx;
2657 subs_instr.subs_instr = subs->subs_instr;
2658 subs_instr.subs_status = OK;
2659 substitute_instr = &subs_instr;
2660
2661 SOURCING_LNUM = iptr->isn_lnum;
2662 // This is very much like ISN_EXEC
2663 CLEAR_FIELD(cookie);
2664 cookie.sourcing_lnum = iptr->isn_lnum - 1;
2665 res = do_cmdline(subs->subs_cmd,
2666 getsourceline, &cookie,
2667 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
2668 substitute_instr = save_instr;
2669
2670 if (res == FAIL || did_emsg
2671 || subs_instr.subs_status == FAIL)
2672 goto on_error;
2673 }
2674 break;
2675
2676 case ISN_FINISH:
2677 goto done;
2678
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02002679 case ISN_REDIRSTART:
2680 // create a dummy entry for var_redir_str()
2681 if (alloc_redir_lval() == FAIL)
2682 goto on_error;
2683
2684 // The output is stored in growarray "redir_ga" until
2685 // redirection ends.
2686 init_redir_ga();
2687 redir_vname = 1;
2688 break;
2689
2690 case ISN_REDIREND:
2691 {
2692 char_u *res = get_clear_redir_ga();
2693
2694 // End redirection, put redirected text on the stack.
2695 clear_redir_lval();
2696 redir_vname = 0;
2697
Bram Moolenaar35578162021-08-02 19:10:38 +02002698 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02002699 {
2700 vim_free(res);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002701 goto theend;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02002702 }
2703 tv = STACK_TV_BOT(0);
2704 tv->v_type = VAR_STRING;
2705 tv->vval.v_string = res;
2706 ++ectx->ec_stack.ga_len;
2707 }
2708 break;
2709
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002710 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02002711#ifdef FEAT_QUICKFIX
Bram Moolenaar397a87a2022-03-20 21:14:15 +00002712 force_abort = TRUE;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002713 if (trigger_cexpr_autocmd(iptr->isn_arg.number) == FAIL)
2714 goto on_error;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00002715 force_abort = FALSE;
Bram Moolenaarb7c97812021-05-05 22:51:39 +02002716#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002717 break;
2718
2719 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02002720#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002721 {
2722 exarg_T ea;
2723 int res;
2724
2725 CLEAR_FIELD(ea);
2726 ea.cmdidx = iptr->isn_arg.cexpr.cexpr_ref->cer_cmdidx;
2727 ea.forceit = iptr->isn_arg.cexpr.cexpr_ref->cer_forceit;
2728 ea.cmdlinep = &iptr->isn_arg.cexpr.cexpr_ref->cer_cmdline;
2729 --ectx->ec_stack.ga_len;
2730 tv = STACK_TV_BOT(0);
2731 res = cexpr_core(&ea, tv);
2732 clear_tv(tv);
2733 if (res == FAIL)
2734 goto on_error;
2735 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02002736#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02002737 break;
2738
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002739 // execute Ex command from pieces on the stack
2740 case ISN_EXECCONCAT:
2741 {
2742 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +02002743 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002744 int pass;
2745 int i;
2746 char_u *cmd = NULL;
2747 char_u *str;
2748
2749 for (pass = 1; pass <= 2; ++pass)
2750 {
2751 for (i = 0; i < count; ++i)
2752 {
2753 tv = STACK_TV_BOT(i - count);
2754 str = tv->vval.v_string;
2755 if (str != NULL && *str != NUL)
2756 {
2757 if (pass == 2)
2758 STRCPY(cmd + len, str);
2759 len += STRLEN(str);
2760 }
2761 if (pass == 2)
2762 clear_tv(tv);
2763 }
2764 if (pass == 1)
2765 {
2766 cmd = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002767 if (unlikely(cmd == NULL))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002768 goto theend;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002769 len = 0;
2770 }
2771 }
2772
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02002773 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002774 do_cmdline_cmd(cmd);
2775 vim_free(cmd);
2776 }
2777 break;
2778
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002779 // execute :echo {string} ...
2780 case ISN_ECHO:
2781 {
2782 int count = iptr->isn_arg.echo.echo_count;
2783 int atstart = TRUE;
2784 int needclr = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002785 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002786
2787 for (idx = 0; idx < count; ++idx)
2788 {
2789 tv = STACK_TV_BOT(idx - count);
2790 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
2791 &atstart, &needclr);
2792 clear_tv(tv);
2793 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01002794 if (needclr)
2795 msg_clr_eos();
Bram Moolenaar4c137212021-04-19 16:48:48 +02002796 ectx->ec_stack.ga_len -= count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002797 }
2798 break;
2799
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002800 // :execute {string} ...
2801 // :echomsg {string} ...
Bram Moolenaar7de62622021-08-07 15:05:47 +02002802 // :echoconsole {string} ...
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002803 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01002804 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002805 case ISN_ECHOMSG:
Bram Moolenaar7de62622021-08-07 15:05:47 +02002806 case ISN_ECHOCONSOLE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002807 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01002808 {
2809 int count = iptr->isn_arg.number;
2810 garray_T ga;
2811 char_u buf[NUMBUFLEN];
2812 char_u *p;
2813 int len;
2814 int failed = FALSE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002815 int idx;
Bram Moolenaarad39c092020-02-26 18:23:43 +01002816
2817 ga_init2(&ga, 1, 80);
2818 for (idx = 0; idx < count; ++idx)
2819 {
2820 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002821 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01002822 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002823 if (tv->v_type == VAR_CHANNEL
2824 || tv->v_type == VAR_JOB)
2825 {
2826 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar68db9962021-05-09 23:19:22 +02002827 semsg(_(e_using_invalid_value_as_string_str),
2828 vartype_name(tv->v_type));
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002829 break;
2830 }
2831 else
2832 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01002833 }
2834 else
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002835 p = tv_stringify(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01002836
2837 len = (int)STRLEN(p);
Bram Moolenaar35578162021-08-02 19:10:38 +02002838 if (GA_GROW_FAILS(&ga, len + 2))
Bram Moolenaarad39c092020-02-26 18:23:43 +01002839 failed = TRUE;
2840 else
2841 {
2842 if (ga.ga_len > 0)
2843 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
2844 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
2845 ga.ga_len += len;
2846 }
2847 clear_tv(tv);
2848 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02002849 ectx->ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002850 if (failed)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01002851 {
2852 ga_clear(&ga);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002853 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01002854 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01002855
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002856 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002857 {
2858 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02002859 {
2860 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002861 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01002862 if (did_emsg)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01002863 {
2864 ga_clear(&ga);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01002865 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01002866 }
Bram Moolenaar430deb12020-08-23 16:29:11 +02002867 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002868 else
2869 {
2870 msg_sb_eol();
2871 if (iptr->isn_type == ISN_ECHOMSG)
2872 {
2873 msg_attr(ga.ga_data, echo_attr);
2874 out_flush();
2875 }
Bram Moolenaar7de62622021-08-07 15:05:47 +02002876 else if (iptr->isn_type == ISN_ECHOCONSOLE)
2877 {
2878 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data),
2879 TRUE);
2880 ui_write((char_u *)"\r\n", 2, TRUE);
2881 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002882 else
2883 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002884 SOURCING_LNUM = iptr->isn_lnum;
2885 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002886 }
2887 }
2888 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01002889 ga_clear(&ga);
2890 }
2891 break;
2892
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002893 // load local variable or argument
2894 case ISN_LOAD:
Bram Moolenaar35578162021-08-02 19:10:38 +02002895 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002896 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002897 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002898 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002899 break;
2900
2901 // load v: variable
2902 case ISN_LOADV:
Bram Moolenaar35578162021-08-02 19:10:38 +02002903 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002904 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002905 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002906 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002907 break;
2908
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002909 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002910 case ISN_LOADSCRIPT:
2911 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01002912 scriptref_T *sref = iptr->isn_arg.script.scriptref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002913 svar_T *sv;
2914
Bram Moolenaar6db660b2021-08-01 14:08:54 +02002915 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01002916 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002917 goto theend;
Bram Moolenaar859cc212022-03-28 15:22:35 +01002918 allocate_if_null(sv);
Bram Moolenaar35578162021-08-02 19:10:38 +02002919 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002920 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002921 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002922 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002923 }
2924 break;
2925
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002926 // load s: variable in old script or autoload import
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002927 case ISN_LOADS:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002928 case ISN_LOADEXPORT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002929 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002930 int sid = iptr->isn_arg.loadstore.ls_sid;
2931 hashtab_T *ht = &SCRIPT_VARS(sid);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002932 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002933 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002934
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002935 if (di == NULL)
2936 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002937 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002938 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002939 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002940 }
2941 else
2942 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01002943 if (iptr->isn_type == ISN_LOADEXPORT)
2944 {
2945 int idx = get_script_item_idx(sid, name, 0,
2946 NULL, NULL);
2947 svar_T *sv;
2948
2949 if (idx >= 0)
2950 {
2951 sv = ((svar_T *)SCRIPT_ITEM(sid)
2952 ->sn_var_vals.ga_data) + idx;
2953 if (!sv->sv_export)
2954 {
2955 SOURCING_LNUM = iptr->isn_lnum;
2956 semsg(_(e_item_not_exported_in_script_str),
2957 name);
2958 goto on_error;
2959 }
2960 }
2961 }
Bram Moolenaar35578162021-08-02 19:10:38 +02002962 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002963 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002964 copy_tv(&di->di_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002965 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002966 }
2967 }
2968 break;
2969
Bram Moolenaard3aac292020-04-19 14:32:17 +02002970 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002971 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02002972 case ISN_LOADB:
2973 case ISN_LOADW:
2974 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002975 {
Bram Moolenaar06b77222022-01-25 15:51:56 +00002976 int res = load_namespace_var(ectx, iptr->isn_type, iptr);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002977
Bram Moolenaar06b77222022-01-25 15:51:56 +00002978 if (res == NOTDONE)
Bram Moolenaarcbbc48f2022-01-18 12:58:28 +00002979 goto theend;
Bram Moolenaar06b77222022-01-25 15:51:56 +00002980 if (res == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002981 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002982 }
Bram Moolenaar06b77222022-01-25 15:51:56 +00002983
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002984 break;
2985
Bram Moolenaar03290b82020-12-19 16:30:44 +01002986 // load autoload variable
2987 case ISN_LOADAUTO:
2988 {
2989 char_u *name = iptr->isn_arg.string;
2990
Bram Moolenaar35578162021-08-02 19:10:38 +02002991 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002992 goto theend;
Bram Moolenaar03290b82020-12-19 16:30:44 +01002993 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002994 if (eval_variable(name, (int)STRLEN(name), 0,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002995 STACK_TV_BOT(0), NULL, EVAL_VAR_VERBOSE) == FAIL)
Bram Moolenaar03290b82020-12-19 16:30:44 +01002996 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002997 ++ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01002998 }
2999 break;
3000
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003001 // load g:/b:/w:/t: namespace
3002 case ISN_LOADGDICT:
3003 case ISN_LOADBDICT:
3004 case ISN_LOADWDICT:
3005 case ISN_LOADTDICT:
3006 {
3007 dict_T *d = NULL;
3008
3009 switch (iptr->isn_type)
3010 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02003011 case ISN_LOADGDICT: d = get_globvar_dict(); break;
3012 case ISN_LOADBDICT: d = curbuf->b_vars; break;
3013 case ISN_LOADWDICT: d = curwin->w_vars; break;
3014 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003015 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003016 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003017 }
Bram Moolenaar35578162021-08-02 19:10:38 +02003018 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003019 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003020 tv = STACK_TV_BOT(0);
3021 tv->v_type = VAR_DICT;
3022 tv->v_lock = 0;
3023 tv->vval.v_dict = d;
Bram Moolenaar1bd3cb22021-02-24 12:27:31 +01003024 ++d->dv_refcount;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003025 ++ectx->ec_stack.ga_len;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003026 }
3027 break;
3028
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003029 // load &option
3030 case ISN_LOADOPT:
3031 {
3032 typval_T optval;
3033 char_u *name = iptr->isn_arg.string;
3034
Bram Moolenaara8c17702020-04-01 21:17:24 +02003035 // This is not expected to fail, name is checked during
3036 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar35578162021-08-02 19:10:38 +02003037 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003038 goto theend;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003039 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003040 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003041 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003042 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003043 }
3044 break;
3045
3046 // load $ENV
3047 case ISN_LOADENV:
3048 {
3049 typval_T optval;
3050 char_u *name = iptr->isn_arg.string;
3051
Bram Moolenaar35578162021-08-02 19:10:38 +02003052 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003053 goto theend;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003054 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003055 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003056 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003057 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003058 }
3059 break;
3060
3061 // load @register
3062 case ISN_LOADREG:
Bram Moolenaar35578162021-08-02 19:10:38 +02003063 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003064 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003065 tv = STACK_TV_BOT(0);
3066 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003067 tv->v_lock = 0;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02003068 // This may result in NULL, which should be equivalent to an
3069 // empty string.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003070 tv->vval.v_string = get_reg_contents(
3071 iptr->isn_arg.number, GREG_EXPR_SRC);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003072 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003073 break;
3074
3075 // store local variable
3076 case ISN_STORE:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003077 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003078 tv = STACK_TV_VAR(iptr->isn_arg.number);
3079 clear_tv(tv);
3080 *tv = *STACK_TV_BOT(0);
3081 break;
3082
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003083 // store s: variable in old script or autoload import
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003084 case ISN_STORES:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003085 case ISN_STOREEXPORT:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003086 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003087 int sid = iptr->isn_arg.loadstore.ls_sid;
3088 hashtab_T *ht = &SCRIPT_VARS(sid);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003089 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003090 dictitem_T *di = find_var_in_ht(ht, 0,
3091 iptr->isn_type == ISN_STORES
3092 ? name + 2 : name, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003093
Bram Moolenaar4c137212021-04-19 16:48:48 +02003094 --ectx->ec_stack.ga_len;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003095 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003096 if (di == NULL)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003097 {
3098 if (iptr->isn_type == ISN_STOREEXPORT)
3099 {
3100 semsg(_(e_undefined_variable_str), name);
Bram Moolenaard1d26842022-03-31 10:13:47 +01003101 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003102 goto on_error;
3103 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003104 store_var(name, STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003105 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003106 else
3107 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003108 if (iptr->isn_type == ISN_STOREEXPORT)
3109 {
3110 int idx = get_script_item_idx(sid, name, 0,
3111 NULL, NULL);
3112
3113 if (idx >= 0)
3114 {
3115 svar_T *sv = ((svar_T *)SCRIPT_ITEM(sid)
3116 ->sn_var_vals.ga_data) + idx;
3117
3118 if (!sv->sv_export)
3119 {
3120 semsg(_(e_item_not_exported_in_script_str),
3121 name);
Bram Moolenaard1d26842022-03-31 10:13:47 +01003122 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003123 goto on_error;
3124 }
3125 }
3126 }
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02003127 if (var_check_permission(di, name) == FAIL)
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003128 {
3129 clear_tv(STACK_TV_BOT(0));
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02003130 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003131 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003132 clear_tv(&di->di_tv);
3133 di->di_tv = *STACK_TV_BOT(0);
3134 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003135 }
3136 break;
3137
3138 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003139 case ISN_STORESCRIPT:
3140 {
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003141 scriptref_T *sref = iptr->isn_arg.script.scriptref;
3142 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003143
Bram Moolenaar6db660b2021-08-01 14:08:54 +02003144 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003145 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003146 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003147 --ectx->ec_stack.ga_len;
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003148
3149 // "const" and "final" are checked at compile time, locking
3150 // the value needs to be checked here.
3151 SOURCING_LNUM = iptr->isn_lnum;
3152 if (value_check_lock(sv->sv_tv->v_lock, sv->sv_name, FALSE))
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003153 {
3154 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003155 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003156 }
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003157
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003158 clear_tv(sv->sv_tv);
3159 *sv->sv_tv = *STACK_TV_BOT(0);
3160 }
3161 break;
3162
3163 // store option
3164 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003165 case ISN_STOREFUNCOPT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003166 {
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003167 char_u *opt_name = iptr->isn_arg.storeopt.so_name;
3168 int opt_flags = iptr->isn_arg.storeopt.so_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003169 long n = 0;
3170 char_u *s = NULL;
3171 char *msg;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003172 char_u numbuf[NUMBUFLEN];
3173 char_u *tofree = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003174
Bram Moolenaar4c137212021-04-19 16:48:48 +02003175 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003176 tv = STACK_TV_BOT(0);
3177 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01003178 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003179 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01003180 if (s == NULL)
3181 s = (char_u *)"";
3182 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003183 else if (iptr->isn_type == ISN_STOREFUNCOPT)
3184 {
3185 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003186 // If the option can be set to a function reference or
3187 // a lambda and the passed value is a function
3188 // reference, then convert it to the name (string) of
3189 // the function reference.
3190 s = tv2string(tv, &tofree, numbuf, 0);
3191 if (s == NULL || *s == NUL)
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003192 {
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003193 // cannot happen?
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003194 clear_tv(tv);
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003195 vim_free(tofree);
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003196 goto on_error;
3197 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003198 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003199 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003200 // must be VAR_NUMBER, CHECKTYPE makes sure
3201 n = tv->vval.v_number;
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003202 msg = set_option_value(opt_name, n, s, opt_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02003203 clear_tv(tv);
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003204 vim_free(tofree);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003205 if (msg != NULL)
3206 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003207 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003208 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02003209 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003210 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003211 }
3212 break;
3213
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003214 // store $ENV
3215 case ISN_STOREENV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003216 --ectx->ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003217 tv = STACK_TV_BOT(0);
3218 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
3219 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003220 break;
3221
3222 // store @r
3223 case ISN_STOREREG:
3224 {
3225 int reg = iptr->isn_arg.number;
3226
Bram Moolenaar4c137212021-04-19 16:48:48 +02003227 --ectx->ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01003228 tv = STACK_TV_BOT(0);
Bram Moolenaar74f4a962021-06-17 21:03:07 +02003229 write_reg_contents(reg, tv_get_string(tv), -1, FALSE);
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01003230 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003231 }
3232 break;
3233
3234 // store v: variable
3235 case ISN_STOREV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003236 --ectx->ec_stack.ga_len;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003237 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
3238 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003239 // should not happen, type is checked when compiling
3240 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003241 break;
3242
Bram Moolenaard3aac292020-04-19 14:32:17 +02003243 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003244 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02003245 case ISN_STOREB:
3246 case ISN_STOREW:
3247 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003248 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003249 dictitem_T *di;
3250 hashtab_T *ht;
3251 char_u *name = iptr->isn_arg.string + 2;
3252
Bram Moolenaard3aac292020-04-19 14:32:17 +02003253 switch (iptr->isn_type)
3254 {
3255 case ISN_STOREG:
3256 ht = get_globvar_ht();
3257 break;
3258 case ISN_STOREB:
3259 ht = &curbuf->b_vars->dv_hashtab;
3260 break;
3261 case ISN_STOREW:
3262 ht = &curwin->w_vars->dv_hashtab;
3263 break;
3264 case ISN_STORET:
3265 ht = &curtab->tp_vars->dv_hashtab;
3266 break;
3267 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003268 goto theend;
Bram Moolenaard3aac292020-04-19 14:32:17 +02003269 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003270
Bram Moolenaar4c137212021-04-19 16:48:48 +02003271 --ectx->ec_stack.ga_len;
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003272 di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003273 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003274 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003275 else
3276 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003277 SOURCING_LNUM = iptr->isn_lnum;
3278 if (var_check_permission(di, name) == FAIL)
3279 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003280 clear_tv(&di->di_tv);
3281 di->di_tv = *STACK_TV_BOT(0);
3282 }
3283 }
3284 break;
3285
Bram Moolenaar03290b82020-12-19 16:30:44 +01003286 // store an autoload variable
3287 case ISN_STOREAUTO:
3288 SOURCING_LNUM = iptr->isn_lnum;
3289 set_var(iptr->isn_arg.string, STACK_TV_BOT(-1), TRUE);
3290 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003291 --ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003292 break;
3293
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003294 // store number in local variable
3295 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01003296 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003297 clear_tv(tv);
3298 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01003299 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003300 break;
3301
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01003302 // store value in list or dict variable
3303 case ISN_STOREINDEX:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003304 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003305 int res = execute_storeindex(iptr, ectx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003306
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003307 if (res == FAIL)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02003308 goto on_error;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003309 if (res == NOTDONE)
3310 goto theend;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003311 }
3312 break;
3313
Bram Moolenaarea5c8982022-02-17 14:42:02 +00003314 // store value in list or blob range
Bram Moolenaar68452172021-04-12 21:21:02 +02003315 case ISN_STORERANGE:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003316 if (execute_storerange(iptr, ectx) == FAIL)
3317 goto on_error;
Bram Moolenaar68452172021-04-12 21:21:02 +02003318 break;
3319
Bram Moolenaar0186e582021-01-10 18:33:11 +01003320 // load or store variable or argument from outer scope
3321 case ISN_LOADOUTER:
3322 case ISN_STOREOUTER:
3323 {
3324 int depth = iptr->isn_arg.outer.outer_depth;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02003325 outer_T *outer = ectx->ec_outer_ref == NULL ? NULL
3326 : ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003327
3328 while (depth > 1 && outer != NULL)
3329 {
3330 outer = outer->out_up;
3331 --depth;
3332 }
3333 if (outer == NULL)
3334 {
3335 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar69c76172021-12-02 16:38:52 +00003336 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx
3337 || ectx->ec_outer_ref == NULL)
3338 // Possibly :def function called from legacy
3339 // context.
3340 emsg(_(e_closure_called_from_invalid_context));
3341 else
3342 iemsg("LOADOUTER depth more than scope levels");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003343 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003344 }
3345 tv = ((typval_T *)outer->out_stack->ga_data)
3346 + outer->out_frame_idx + STACK_FRAME_SIZE
3347 + iptr->isn_arg.outer.outer_idx;
3348 if (iptr->isn_type == ISN_LOADOUTER)
3349 {
Bram Moolenaar35578162021-08-02 19:10:38 +02003350 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003351 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003352 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003353 ++ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003354 }
3355 else
3356 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003357 --ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003358 clear_tv(tv);
3359 *tv = *STACK_TV_BOT(0);
3360 }
3361 }
3362 break;
3363
Bram Moolenaar752fc692021-01-04 21:57:11 +01003364 // unlet item in list or dict variable
3365 case ISN_UNLETINDEX:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003366 if (execute_unletindex(iptr, ectx) == FAIL)
3367 goto on_error;
Bram Moolenaar752fc692021-01-04 21:57:11 +01003368 break;
3369
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01003370 // unlet range of items in list variable
3371 case ISN_UNLETRANGE:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003372 if (execute_unletrange(iptr, ectx) == FAIL)
3373 goto on_error;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01003374 break;
3375
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003376 // push constant
3377 case ISN_PUSHNR:
3378 case ISN_PUSHBOOL:
3379 case ISN_PUSHSPEC:
3380 case ISN_PUSHF:
3381 case ISN_PUSHS:
3382 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003383 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003384 case ISN_PUSHCHANNEL:
3385 case ISN_PUSHJOB:
Bram Moolenaar35578162021-08-02 19:10:38 +02003386 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003387 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003388 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003389 tv->v_lock = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003390 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003391 switch (iptr->isn_type)
3392 {
3393 case ISN_PUSHNR:
3394 tv->v_type = VAR_NUMBER;
3395 tv->vval.v_number = iptr->isn_arg.number;
3396 break;
3397 case ISN_PUSHBOOL:
3398 tv->v_type = VAR_BOOL;
3399 tv->vval.v_number = iptr->isn_arg.number;
3400 break;
3401 case ISN_PUSHSPEC:
3402 tv->v_type = VAR_SPECIAL;
3403 tv->vval.v_number = iptr->isn_arg.number;
3404 break;
3405#ifdef FEAT_FLOAT
3406 case ISN_PUSHF:
3407 tv->v_type = VAR_FLOAT;
3408 tv->vval.v_float = iptr->isn_arg.fnumber;
3409 break;
3410#endif
3411 case ISN_PUSHBLOB:
3412 blob_copy(iptr->isn_arg.blob, tv);
3413 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003414 case ISN_PUSHFUNC:
3415 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01003416 if (iptr->isn_arg.string == NULL)
3417 tv->vval.v_string = NULL;
3418 else
3419 tv->vval.v_string =
3420 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003421 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003422 case ISN_PUSHCHANNEL:
3423#ifdef FEAT_JOB_CHANNEL
3424 tv->v_type = VAR_CHANNEL;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003425 tv->vval.v_channel = NULL;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003426#endif
3427 break;
3428 case ISN_PUSHJOB:
3429#ifdef FEAT_JOB_CHANNEL
3430 tv->v_type = VAR_JOB;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003431 tv->vval.v_job = NULL;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003432#endif
3433 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003434 default:
3435 tv->v_type = VAR_STRING;
Bram Moolenaarf8691002022-03-10 12:20:53 +00003436 tv->vval.v_string = iptr->isn_arg.string == NULL
3437 ? NULL : vim_strsave(iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003438 }
3439 break;
3440
Bram Moolenaar06b77222022-01-25 15:51:56 +00003441 case ISN_AUTOLOAD:
3442 {
3443 char_u *name = iptr->isn_arg.string;
3444
3445 (void)script_autoload(name, FALSE);
3446 if (find_func(name, TRUE))
3447 {
3448 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
3449 goto theend;
3450 tv = STACK_TV_BOT(0);
3451 tv->v_lock = 0;
3452 ++ectx->ec_stack.ga_len;
3453 tv->v_type = VAR_FUNC;
3454 tv->vval.v_string = vim_strsave(name);
3455 }
3456 else
3457 {
3458 int res = load_namespace_var(ectx, ISN_LOADG, iptr);
3459
3460 if (res == NOTDONE)
3461 goto theend;
3462 if (res == FAIL)
3463 goto on_error;
3464 }
3465 }
3466 break;
3467
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003468 case ISN_UNLET:
3469 if (do_unlet(iptr->isn_arg.unlet.ul_name,
3470 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003471 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003472 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02003473 case ISN_UNLETENV:
3474 vim_unsetenv(iptr->isn_arg.unlet.ul_name);
3475 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003476
Bram Moolenaaraacc9662021-08-13 19:40:51 +02003477 case ISN_LOCKUNLOCK:
3478 {
3479 typval_T *lval_root_save = lval_root;
3480 int res;
3481
3482 // Stack has the local variable, argument the whole :lock
3483 // or :unlock command, like ISN_EXEC.
3484 --ectx->ec_stack.ga_len;
3485 lval_root = STACK_TV_BOT(0);
3486 res = exec_command(iptr);
3487 clear_tv(lval_root);
3488 lval_root = lval_root_save;
3489 if (res == FAIL)
3490 goto on_error;
3491 }
3492 break;
3493
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02003494 case ISN_LOCKCONST:
3495 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
3496 break;
3497
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003498 // create a list from items on the stack; uses a single allocation
3499 // for the list header and the items
3500 case ISN_NEWLIST:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003501 if (exe_newlist(iptr->isn_arg.number, ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003502 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003503 break;
3504
3505 // create a dict from items on the stack
3506 case ISN_NEWDICT:
3507 {
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01003508 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003509
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01003510 SOURCING_LNUM = iptr->isn_lnum;
3511 res = exe_newdict(iptr->isn_arg.number, ectx);
3512 if (res == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003513 goto theend;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01003514 if (res == MAYBE)
3515 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003516 }
3517 break;
3518
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003519 // create a partial with NULL value
3520 case ISN_NEWPARTIAL:
3521 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
3522 goto theend;
3523 ++ectx->ec_stack.ga_len;
3524 tv = STACK_TV_BOT(-1);
3525 tv->v_type = VAR_PARTIAL;
3526 tv->v_lock = 0;
3527 tv->vval.v_partial = NULL;
3528 break;
3529
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003530 // call a :def function
3531 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02003532 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003533 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
3534 NULL,
3535 iptr->isn_arg.dfunc.cdf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003536 ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003537 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003538 break;
3539
3540 // call a builtin function
3541 case ISN_BCALL:
3542 SOURCING_LNUM = iptr->isn_lnum;
3543 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
3544 iptr->isn_arg.bfunc.cbf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003545 ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02003546 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003547 break;
3548
3549 // call a funcref or partial
3550 case ISN_PCALL:
3551 {
3552 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
3553 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003554 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003555
3556 SOURCING_LNUM = iptr->isn_lnum;
3557 if (pfunc->cpf_top)
3558 {
3559 // funcref is above the arguments
3560 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
3561 }
3562 else
3563 {
3564 // Get the funcref from the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02003565 --ectx->ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003566 partial_tv = *STACK_TV_BOT(0);
3567 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003568 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003569 r = call_partial(tv, pfunc->cpf_argcount, ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003570 if (tv == &partial_tv)
3571 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003572 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02003573 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003574 }
3575 break;
3576
Bram Moolenaarbd5da372020-03-31 23:13:10 +02003577 case ISN_PCALL_END:
3578 // PCALL finished, arguments have been consumed and replaced by
3579 // the return value. Now clear the funcref from the stack,
3580 // and move the return value in its place.
Bram Moolenaar4c137212021-04-19 16:48:48 +02003581 --ectx->ec_stack.ga_len;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02003582 clear_tv(STACK_TV_BOT(-1));
3583 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
3584 break;
3585
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003586 // call a user defined function or funcref/partial
3587 case ISN_UCALL:
3588 {
3589 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
3590
3591 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003592 if (call_eval_func(cufunc->cuf_name, cufunc->cuf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003593 ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02003594 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003595 }
3596 break;
3597
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02003598 // return from a :def function call without a value
3599 case ISN_RETURN_VOID:
Bram Moolenaar35578162021-08-02 19:10:38 +02003600 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003601 goto theend;
Bram Moolenaar299f3032021-01-08 20:53:09 +01003602 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003603 ++ectx->ec_stack.ga_len;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02003604 tv->v_type = VAR_VOID;
Bram Moolenaar299f3032021-01-08 20:53:09 +01003605 tv->vval.v_number = 0;
3606 tv->v_lock = 0;
3607 // FALLTHROUGH
3608
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02003609 // return from a :def function call with what is on the stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003610 case ISN_RETURN:
3611 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003612 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003613 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01003614
3615 if (trystack->ga_len > 0)
3616 trycmd = ((trycmd_T *)trystack->ga_data)
3617 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003618 if (trycmd != NULL
Bram Moolenaar4c137212021-04-19 16:48:48 +02003619 && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003620 {
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01003621 // jump to ":finally" or ":endtry"
3622 if (trycmd->tcd_finally_idx != 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02003623 ectx->ec_iidx = trycmd->tcd_finally_idx;
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01003624 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02003625 ectx->ec_iidx = trycmd->tcd_endtry_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003626 trycmd->tcd_return = TRUE;
3627 }
3628 else
Bram Moolenaard032f342020-07-18 18:13:02 +02003629 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003630 }
3631 break;
3632
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02003633 // push a partial, a reference to a compiled function
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003634 case ISN_FUNCREF:
3635 {
Bram Moolenaarf112f302020-12-20 17:47:52 +01003636 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar38453522021-11-28 22:00:12 +00003637 ufunc_T *ufunc;
3638 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003639
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003640 if (pt == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003641 goto theend;
Bram Moolenaar35578162021-08-02 19:10:38 +02003642 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003643 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003644 vim_free(pt);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003645 goto theend;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003646 }
Bram Moolenaar38453522021-11-28 22:00:12 +00003647 if (funcref->fr_func_name == NULL)
3648 {
3649 dfunc_T *pt_dfunc = ((dfunc_T *)def_functions.ga_data)
3650 + funcref->fr_dfunc_idx;
3651
3652 ufunc = pt_dfunc->df_ufunc;
3653 }
3654 else
3655 {
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003656 ufunc = find_func(funcref->fr_func_name, FALSE);
Bram Moolenaar38453522021-11-28 22:00:12 +00003657 }
Bram Moolenaar56acd1f2022-02-18 13:24:52 +00003658 if (ufunc == NULL)
3659 {
3660 SOURCING_LNUM = iptr->isn_lnum;
3661 iemsg("ufunc unexpectedly NULL for FUNCREF");
3662 goto theend;
3663 }
Bram Moolenaar38453522021-11-28 22:00:12 +00003664 if (fill_partial_and_closure(pt, ufunc, ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003665 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003666 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003667 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003668 tv->vval.v_partial = pt;
3669 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003670 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003671 }
3672 break;
3673
Bram Moolenaar38ddf332020-07-31 22:05:04 +02003674 // Create a global function from a lambda.
3675 case ISN_NEWFUNC:
3676 {
3677 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
3678
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01003679 if (copy_func(newfunc->nf_lambda, newfunc->nf_global,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003680 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003681 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02003682 }
3683 break;
3684
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01003685 // List functions
3686 case ISN_DEF:
3687 if (iptr->isn_arg.string == NULL)
3688 list_functions(NULL);
3689 else
3690 {
Bram Moolenaar14336722022-01-08 16:02:59 +00003691 exarg_T ea;
3692 garray_T lines_to_free;
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01003693
3694 CLEAR_FIELD(ea);
3695 ea.cmd = ea.arg = iptr->isn_arg.string;
Bram Moolenaar14336722022-01-08 16:02:59 +00003696 ga_init2(&lines_to_free, sizeof(char_u *), 50);
3697 define_function(&ea, NULL, &lines_to_free);
3698 ga_clear_strings(&lines_to_free);
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01003699 }
3700 break;
3701
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003702 // jump if a condition is met
3703 case ISN_JUMP:
3704 {
3705 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003706 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003707 int jump = TRUE;
3708
3709 if (when != JUMP_ALWAYS)
3710 {
3711 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003712 if (when == JUMP_IF_COND_FALSE
Bram Moolenaar13106602020-10-04 16:06:05 +02003713 || when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003714 || when == JUMP_IF_COND_TRUE)
3715 {
3716 SOURCING_LNUM = iptr->isn_lnum;
3717 jump = tv_get_bool_chk(tv, &error);
3718 if (error)
3719 goto on_error;
3720 }
3721 else
3722 jump = tv2bool(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003723 if (when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003724 || when == JUMP_AND_KEEP_IF_FALSE
3725 || when == JUMP_IF_COND_FALSE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003726 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01003727 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003728 {
3729 // drop the value from the stack
3730 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003731 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003732 }
3733 }
3734 if (jump)
Bram Moolenaar4c137212021-04-19 16:48:48 +02003735 ectx->ec_iidx = iptr->isn_arg.jump.jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003736 }
3737 break;
3738
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02003739 // Jump if an argument with a default value was already set and not
3740 // v:none.
3741 case ISN_JUMP_IF_ARG_SET:
3742 tv = STACK_TV_VAR(iptr->isn_arg.jumparg.jump_arg_off);
3743 if (tv->v_type != VAR_UNKNOWN
3744 && !(tv->v_type == VAR_SPECIAL
3745 && tv->vval.v_number == VVAL_NONE))
Bram Moolenaar4c137212021-04-19 16:48:48 +02003746 ectx->ec_iidx = iptr->isn_arg.jumparg.jump_where;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02003747 break;
3748
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003749 // top of a for loop
3750 case ISN_FOR:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003751 if (execute_for(iptr, ectx) == FAIL)
3752 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003753 break;
3754
3755 // start of ":try" block
3756 case ISN_TRY:
3757 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01003758 trycmd_T *trycmd = NULL;
3759
Bram Moolenaar35578162021-08-02 19:10:38 +02003760 if (GA_GROW_FAILS(&ectx->ec_trystack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003761 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003762 trycmd = ((trycmd_T *)ectx->ec_trystack.ga_data)
3763 + ectx->ec_trystack.ga_len;
3764 ++ectx->ec_trystack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003765 ++trylevel;
Bram Moolenaar8d4be892021-02-13 18:33:02 +01003766 CLEAR_POINTER(trycmd);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003767 trycmd->tcd_frame_idx = ectx->ec_frame_idx;
3768 trycmd->tcd_stack_len = ectx->ec_stack.ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01003769 trycmd->tcd_catch_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00003770 iptr->isn_arg.tryref.try_ref->try_catch;
Bram Moolenaara91a7132021-03-25 21:12:15 +01003771 trycmd->tcd_finally_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00003772 iptr->isn_arg.tryref.try_ref->try_finally;
Bram Moolenaara91a7132021-03-25 21:12:15 +01003773 trycmd->tcd_endtry_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00003774 iptr->isn_arg.tryref.try_ref->try_endtry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003775 }
3776 break;
3777
3778 case ISN_PUSHEXC:
3779 if (current_exception == NULL)
3780 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003781 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003782 iemsg("Evaluating catch while current_exception is NULL");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003783 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003784 }
Bram Moolenaar35578162021-08-02 19:10:38 +02003785 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003786 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003787 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003788 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003789 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003790 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003791 tv->vval.v_string = vim_strsave(
3792 (char_u *)current_exception->value);
3793 break;
3794
3795 case ISN_CATCH:
3796 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003797 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003798
Bram Moolenaar4c137212021-04-19 16:48:48 +02003799 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003800 if (trystack->ga_len > 0)
3801 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01003802 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003803 + trystack->ga_len - 1;
3804 trycmd->tcd_caught = TRUE;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003805 trycmd->tcd_did_throw = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003806 }
3807 did_emsg = got_int = did_throw = FALSE;
Bram Moolenaar1430cee2021-01-17 19:20:32 +01003808 force_abort = need_rethrow = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003809 catch_exception(current_exception);
3810 }
3811 break;
3812
Bram Moolenaarc150c092021-02-13 15:02:46 +01003813 case ISN_TRYCONT:
3814 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003815 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003816 trycont_T *trycont = &iptr->isn_arg.trycont;
3817 int i;
3818 trycmd_T *trycmd;
3819 int iidx = trycont->tct_where;
3820
3821 if (trystack->ga_len < trycont->tct_levels)
3822 {
3823 siemsg("TRYCONT: expected %d levels, found %d",
3824 trycont->tct_levels, trystack->ga_len);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003825 goto theend;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003826 }
3827 // Make :endtry jump to any outer try block and the last
3828 // :endtry inside the loop to the loop start.
3829 for (i = trycont->tct_levels; i > 0; --i)
3830 {
3831 trycmd = ((trycmd_T *)trystack->ga_data)
3832 + trystack->ga_len - i;
Bram Moolenaar2e34c342021-03-14 12:13:33 +01003833 // Add one to tcd_cont to be able to jump to
3834 // instruction with index zero.
3835 trycmd->tcd_cont = iidx + 1;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003836 iidx = trycmd->tcd_finally_idx == 0
3837 ? trycmd->tcd_endtry_idx : trycmd->tcd_finally_idx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003838 }
3839 // jump to :finally or :endtry of current try statement
Bram Moolenaar4c137212021-04-19 16:48:48 +02003840 ectx->ec_iidx = iidx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003841 }
3842 break;
3843
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003844 case ISN_FINALLY:
3845 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003846 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003847 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
3848 + trystack->ga_len - 1;
3849
3850 // Reset the index to avoid a return statement jumps here
3851 // again.
3852 trycmd->tcd_finally_idx = 0;
3853 break;
3854 }
3855
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003856 // end of ":try" block
3857 case ISN_ENDTRY:
3858 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003859 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003860
3861 if (trystack->ga_len > 0)
3862 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003863 trycmd_T *trycmd;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003864
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003865 --trystack->ga_len;
3866 --trylevel;
3867 trycmd = ((trycmd_T *)trystack->ga_data)
3868 + trystack->ga_len;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003869 if (trycmd->tcd_did_throw)
3870 did_throw = TRUE;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01003871 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003872 {
3873 // discard the exception
3874 if (caught_stack == current_exception)
3875 caught_stack = caught_stack->caught;
3876 discard_current_exception();
3877 }
3878
3879 if (trycmd->tcd_return)
Bram Moolenaard032f342020-07-18 18:13:02 +02003880 goto func_return;
Bram Moolenaard9d77892021-02-12 21:32:47 +01003881
Bram Moolenaar4c137212021-04-19 16:48:48 +02003882 while (ectx->ec_stack.ga_len > trycmd->tcd_stack_len)
Bram Moolenaard9d77892021-02-12 21:32:47 +01003883 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003884 --ectx->ec_stack.ga_len;
Bram Moolenaard9d77892021-02-12 21:32:47 +01003885 clear_tv(STACK_TV_BOT(0));
3886 }
Bram Moolenaar8d4be892021-02-13 18:33:02 +01003887 if (trycmd->tcd_cont != 0)
Bram Moolenaarc150c092021-02-13 15:02:46 +01003888 // handling :continue: jump to outer try block or
3889 // start of the loop
Bram Moolenaar4c137212021-04-19 16:48:48 +02003890 ectx->ec_iidx = trycmd->tcd_cont - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003891 }
3892 }
3893 break;
3894
3895 case ISN_THROW:
Bram Moolenaar8f81b222021-01-14 21:47:06 +01003896 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003897 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02003898
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003899 if (trystack->ga_len == 0 && trylevel == 0 && emsg_silent)
3900 {
3901 // throwing an exception while using "silent!" causes
3902 // the function to abort but not display an error.
3903 tv = STACK_TV_BOT(-1);
3904 clear_tv(tv);
3905 tv->v_type = VAR_NUMBER;
3906 tv->vval.v_number = 0;
3907 goto done;
3908 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003909 --ectx->ec_stack.ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003910 tv = STACK_TV_BOT(0);
3911 if (tv->vval.v_string == NULL
3912 || *skipwhite(tv->vval.v_string) == NUL)
3913 {
3914 vim_free(tv->vval.v_string);
3915 SOURCING_LNUM = iptr->isn_lnum;
3916 emsg(_(e_throw_with_empty_string));
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003917 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003918 }
3919
3920 // Inside a "catch" we need to first discard the caught
3921 // exception.
3922 if (trystack->ga_len > 0)
3923 {
3924 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
3925 + trystack->ga_len - 1;
3926 if (trycmd->tcd_caught && current_exception != NULL)
3927 {
3928 // discard the exception
3929 if (caught_stack == current_exception)
3930 caught_stack = caught_stack->caught;
3931 discard_current_exception();
3932 trycmd->tcd_caught = FALSE;
3933 }
3934 }
3935
Bram Moolenaar90a57162022-02-12 14:23:17 +00003936 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003937 if (throw_exception(tv->vval.v_string, ET_USER, NULL)
3938 == FAIL)
3939 {
3940 vim_free(tv->vval.v_string);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003941 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003942 }
3943 did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003944 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003945 break;
3946
3947 // compare with special values
3948 case ISN_COMPAREBOOL:
3949 case ISN_COMPARESPECIAL:
3950 {
3951 typval_T *tv1 = STACK_TV_BOT(-2);
3952 typval_T *tv2 = STACK_TV_BOT(-1);
3953 varnumber_T arg1 = tv1->vval.v_number;
3954 varnumber_T arg2 = tv2->vval.v_number;
3955 int res;
3956
3957 switch (iptr->isn_arg.op.op_type)
3958 {
3959 case EXPR_EQUAL: res = arg1 == arg2; break;
3960 case EXPR_NEQUAL: res = arg1 != arg2; break;
3961 default: res = 0; break;
3962 }
3963
Bram Moolenaar4c137212021-04-19 16:48:48 +02003964 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003965 tv1->v_type = VAR_BOOL;
3966 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
3967 }
3968 break;
3969
Bram Moolenaar7a222242022-03-01 19:23:24 +00003970 case ISN_COMPARENULL:
3971 {
3972 typval_T *tv1 = STACK_TV_BOT(-2);
3973 typval_T *tv2 = STACK_TV_BOT(-1);
3974 int res;
3975
3976 res = typval_compare_null(tv1, tv2);
3977 if (res == MAYBE)
3978 goto on_error;
3979 if (iptr->isn_arg.op.op_type == EXPR_NEQUAL)
3980 res = !res;
3981 clear_tv(tv1);
3982 clear_tv(tv2);
3983 --ectx->ec_stack.ga_len;
3984 tv1->v_type = VAR_BOOL;
3985 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
3986 }
3987 break;
3988
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003989 // Operation with two number arguments
3990 case ISN_OPNR:
3991 case ISN_COMPARENR:
3992 {
3993 typval_T *tv1 = STACK_TV_BOT(-2);
3994 typval_T *tv2 = STACK_TV_BOT(-1);
3995 varnumber_T arg1 = tv1->vval.v_number;
3996 varnumber_T arg2 = tv2->vval.v_number;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02003997 varnumber_T res = 0;
3998 int div_zero = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003999
4000 switch (iptr->isn_arg.op.op_type)
4001 {
4002 case EXPR_MULT: res = arg1 * arg2; break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004003 case EXPR_DIV: if (arg2 == 0)
4004 div_zero = TRUE;
4005 else
4006 res = arg1 / arg2;
4007 break;
4008 case EXPR_REM: if (arg2 == 0)
4009 div_zero = TRUE;
4010 else
4011 res = arg1 % arg2;
4012 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004013 case EXPR_SUB: res = arg1 - arg2; break;
4014 case EXPR_ADD: res = arg1 + arg2; break;
4015
4016 case EXPR_EQUAL: res = arg1 == arg2; break;
4017 case EXPR_NEQUAL: res = arg1 != arg2; break;
4018 case EXPR_GREATER: res = arg1 > arg2; break;
4019 case EXPR_GEQUAL: res = arg1 >= arg2; break;
4020 case EXPR_SMALLER: res = arg1 < arg2; break;
4021 case EXPR_SEQUAL: res = arg1 <= arg2; break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004022 default: break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004023 }
4024
Bram Moolenaar4c137212021-04-19 16:48:48 +02004025 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004026 if (iptr->isn_type == ISN_COMPARENR)
4027 {
4028 tv1->v_type = VAR_BOOL;
4029 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4030 }
4031 else
4032 tv1->vval.v_number = res;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004033 if (div_zero)
4034 {
4035 SOURCING_LNUM = iptr->isn_lnum;
4036 emsg(_(e_divide_by_zero));
4037 goto on_error;
4038 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004039 }
4040 break;
4041
4042 // Computation with two float arguments
4043 case ISN_OPFLOAT:
4044 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01004045#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004046 {
4047 typval_T *tv1 = STACK_TV_BOT(-2);
4048 typval_T *tv2 = STACK_TV_BOT(-1);
4049 float_T arg1 = tv1->vval.v_float;
4050 float_T arg2 = tv2->vval.v_float;
4051 float_T res = 0;
4052 int cmp = FALSE;
4053
4054 switch (iptr->isn_arg.op.op_type)
4055 {
4056 case EXPR_MULT: res = arg1 * arg2; break;
4057 case EXPR_DIV: res = arg1 / arg2; break;
4058 case EXPR_SUB: res = arg1 - arg2; break;
4059 case EXPR_ADD: res = arg1 + arg2; break;
4060
4061 case EXPR_EQUAL: cmp = arg1 == arg2; break;
4062 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
4063 case EXPR_GREATER: cmp = arg1 > arg2; break;
4064 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
4065 case EXPR_SMALLER: cmp = arg1 < arg2; break;
4066 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
4067 default: cmp = 0; break;
4068 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004069 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004070 if (iptr->isn_type == ISN_COMPAREFLOAT)
4071 {
4072 tv1->v_type = VAR_BOOL;
4073 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
4074 }
4075 else
4076 tv1->vval.v_float = res;
4077 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01004078#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004079 break;
4080
4081 case ISN_COMPARELIST:
Bram Moolenaar265f8112021-12-19 12:33:05 +00004082 case ISN_COMPAREDICT:
4083 case ISN_COMPAREFUNC:
4084 case ISN_COMPARESTRING:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004085 case ISN_COMPAREBLOB:
4086 {
4087 typval_T *tv1 = STACK_TV_BOT(-2);
4088 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar265f8112021-12-19 12:33:05 +00004089 exprtype_T exprtype = iptr->isn_arg.op.op_type;
4090 int ic = iptr->isn_arg.op.op_ic;
4091 int res = FALSE;
4092 int status = OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004093
Bram Moolenaar265f8112021-12-19 12:33:05 +00004094 SOURCING_LNUM = iptr->isn_lnum;
4095 if (iptr->isn_type == ISN_COMPARELIST)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004096 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00004097 status = typval_compare_list(tv1, tv2,
4098 exprtype, ic, &res);
4099 }
4100 else if (iptr->isn_type == ISN_COMPAREDICT)
4101 {
4102 status = typval_compare_dict(tv1, tv2,
4103 exprtype, ic, &res);
4104 }
4105 else if (iptr->isn_type == ISN_COMPAREFUNC)
4106 {
4107 status = typval_compare_func(tv1, tv2,
4108 exprtype, ic, &res);
4109 }
4110 else if (iptr->isn_type == ISN_COMPARESTRING)
4111 {
4112 status = typval_compare_string(tv1, tv2,
4113 exprtype, ic, &res);
4114 }
4115 else
4116 {
4117 status = typval_compare_blob(tv1, tv2, exprtype, &res);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004118 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004119 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004120 clear_tv(tv1);
4121 clear_tv(tv2);
4122 tv1->v_type = VAR_BOOL;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004123 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4124 if (status == FAIL)
4125 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004126 }
4127 break;
4128
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004129 case ISN_COMPAREANY:
4130 {
4131 typval_T *tv1 = STACK_TV_BOT(-2);
4132 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar657137c2021-01-09 15:45:23 +01004133 exprtype_T exprtype = iptr->isn_arg.op.op_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004134 int ic = iptr->isn_arg.op.op_ic;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004135 int status;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004136
Bram Moolenaareb26f432020-09-14 16:50:05 +02004137 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004138 status = typval_compare(tv1, tv2, exprtype, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004139 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004140 --ectx->ec_stack.ga_len;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004141 if (status == FAIL)
4142 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004143 }
4144 break;
4145
4146 case ISN_ADDLIST:
4147 case ISN_ADDBLOB:
4148 {
4149 typval_T *tv1 = STACK_TV_BOT(-2);
4150 typval_T *tv2 = STACK_TV_BOT(-1);
4151
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004152 // add two lists or blobs
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004153 if (iptr->isn_type == ISN_ADDLIST)
Bram Moolenaar07802042021-09-09 23:01:14 +02004154 {
4155 if (iptr->isn_arg.op.op_type == EXPR_APPEND
4156 && tv1->vval.v_list != NULL)
4157 list_extend(tv1->vval.v_list, tv2->vval.v_list,
4158 NULL);
4159 else
4160 eval_addlist(tv1, tv2);
4161 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004162 else
4163 eval_addblob(tv1, tv2);
4164 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004165 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004166 }
4167 break;
4168
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004169 case ISN_LISTAPPEND:
4170 {
4171 typval_T *tv1 = STACK_TV_BOT(-2);
4172 typval_T *tv2 = STACK_TV_BOT(-1);
4173 list_T *l = tv1->vval.v_list;
4174
4175 // add an item to a list
Bram Moolenaar1f4a3452022-01-01 18:29:21 +00004176 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004177 if (l == NULL)
4178 {
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004179 emsg(_(e_cannot_add_to_null_list));
4180 goto on_error;
4181 }
Bram Moolenaar1f4a3452022-01-01 18:29:21 +00004182 if (value_check_lock(l->lv_lock, NULL, FALSE))
4183 goto on_error;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004184 if (list_append_tv(l, tv2) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004185 goto theend;
Bram Moolenaar955347c2020-10-19 23:01:46 +02004186 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004187 --ectx->ec_stack.ga_len;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004188 }
4189 break;
4190
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02004191 case ISN_BLOBAPPEND:
4192 {
4193 typval_T *tv1 = STACK_TV_BOT(-2);
4194 typval_T *tv2 = STACK_TV_BOT(-1);
4195 blob_T *b = tv1->vval.v_blob;
4196 int error = FALSE;
4197 varnumber_T n;
4198
4199 // add a number to a blob
4200 if (b == NULL)
4201 {
4202 SOURCING_LNUM = iptr->isn_lnum;
4203 emsg(_(e_cannot_add_to_null_blob));
4204 goto on_error;
4205 }
4206 n = tv_get_number_chk(tv2, &error);
4207 if (error)
4208 goto on_error;
4209 ga_append(&b->bv_ga, (int)n);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004210 --ectx->ec_stack.ga_len;
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02004211 }
4212 break;
4213
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004214 // Computation with two arguments of unknown type
4215 case ISN_OPANY:
4216 {
4217 typval_T *tv1 = STACK_TV_BOT(-2);
4218 typval_T *tv2 = STACK_TV_BOT(-1);
4219 varnumber_T n1, n2;
4220#ifdef FEAT_FLOAT
4221 float_T f1 = 0, f2 = 0;
4222#endif
4223 int error = FALSE;
4224
4225 if (iptr->isn_arg.op.op_type == EXPR_ADD)
4226 {
4227 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
4228 {
4229 eval_addlist(tv1, tv2);
4230 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004231 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004232 break;
4233 }
4234 else if (tv1->v_type == VAR_BLOB
4235 && tv2->v_type == VAR_BLOB)
4236 {
4237 eval_addblob(tv1, tv2);
4238 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004239 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004240 break;
4241 }
4242 }
4243#ifdef FEAT_FLOAT
4244 if (tv1->v_type == VAR_FLOAT)
4245 {
4246 f1 = tv1->vval.v_float;
4247 n1 = 0;
4248 }
4249 else
4250#endif
4251 {
Bram Moolenaarf665e972020-12-05 19:17:16 +01004252 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004253 n1 = tv_get_number_chk(tv1, &error);
4254 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02004255 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004256#ifdef FEAT_FLOAT
4257 if (tv2->v_type == VAR_FLOAT)
4258 f1 = n1;
4259#endif
4260 }
4261#ifdef FEAT_FLOAT
4262 if (tv2->v_type == VAR_FLOAT)
4263 {
4264 f2 = tv2->vval.v_float;
4265 n2 = 0;
4266 }
4267 else
4268#endif
4269 {
4270 n2 = tv_get_number_chk(tv2, &error);
4271 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02004272 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004273#ifdef FEAT_FLOAT
4274 if (tv1->v_type == VAR_FLOAT)
4275 f2 = n2;
4276#endif
4277 }
4278#ifdef FEAT_FLOAT
4279 // if there is a float on either side the result is a float
4280 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
4281 {
4282 switch (iptr->isn_arg.op.op_type)
4283 {
4284 case EXPR_MULT: f1 = f1 * f2; break;
4285 case EXPR_DIV: f1 = f1 / f2; break;
4286 case EXPR_SUB: f1 = f1 - f2; break;
4287 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004288 default: SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004289 emsg(_(e_cannot_use_percent_with_float));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004290 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004291 }
4292 clear_tv(tv1);
4293 clear_tv(tv2);
4294 tv1->v_type = VAR_FLOAT;
4295 tv1->vval.v_float = f1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004296 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004297 }
4298 else
4299#endif
4300 {
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004301 int failed = FALSE;
4302
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004303 switch (iptr->isn_arg.op.op_type)
4304 {
4305 case EXPR_MULT: n1 = n1 * n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004306 case EXPR_DIV: n1 = num_divide(n1, n2, &failed);
4307 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01004308 goto on_error;
4309 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004310 case EXPR_SUB: n1 = n1 - n2; break;
4311 case EXPR_ADD: n1 = n1 + n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004312 default: n1 = num_modulus(n1, n2, &failed);
4313 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01004314 goto on_error;
4315 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004316 }
4317 clear_tv(tv1);
4318 clear_tv(tv2);
4319 tv1->v_type = VAR_NUMBER;
4320 tv1->vval.v_number = n1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004321 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004322 }
4323 }
4324 break;
4325
4326 case ISN_CONCAT:
4327 {
4328 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string;
4329 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string;
4330 char_u *res;
4331
4332 res = concat_str(str1, str2);
4333 clear_tv(STACK_TV_BOT(-2));
4334 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02004335 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004336 STACK_TV_BOT(-1)->vval.v_string = res;
4337 }
4338 break;
4339
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004340 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004341 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004342 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004343 int is_slice = iptr->isn_type == ISN_STRSLICE;
4344 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004345 char_u *res;
4346
4347 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004348 // string slice: string is at stack-3, first index at
4349 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004350 if (is_slice)
4351 {
4352 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004353 n1 = tv->vval.v_number;
4354 }
4355
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004356 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004357 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004358
Bram Moolenaar4c137212021-04-19 16:48:48 +02004359 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004360 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004361 if (is_slice)
4362 // Slice: Select the characters from the string
Bram Moolenaar6601b622021-01-13 21:47:15 +01004363 res = string_slice(tv->vval.v_string, n1, n2, FALSE);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004364 else
4365 // Index: The resulting variable is a string of a
Bram Moolenaar0289a092021-03-14 18:40:19 +01004366 // single character (including composing characters).
4367 // If the index is too big or negative the result is
4368 // empty.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004369 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004370 vim_free(tv->vval.v_string);
4371 tv->vval.v_string = res;
4372 }
4373 break;
4374
4375 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02004376 case ISN_LISTSLICE:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004377 case ISN_BLOBINDEX:
4378 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004379 {
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004380 int is_slice = iptr->isn_type == ISN_LISTSLICE
4381 || iptr->isn_type == ISN_BLOBSLICE;
4382 int is_blob = iptr->isn_type == ISN_BLOBINDEX
4383 || iptr->isn_type == ISN_BLOBSLICE;
Bram Moolenaared591872020-08-15 22:14:53 +02004384 varnumber_T n1, n2;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004385 typval_T *val_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004386
4387 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02004388 // list slice: list is at stack-3, indexes at stack-2 and
4389 // stack-1
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004390 // Same for blob.
4391 val_tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004392
4393 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02004394 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004395 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02004396
4397 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004398 {
Bram Moolenaared591872020-08-15 22:14:53 +02004399 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02004400 n1 = tv->vval.v_number;
4401 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004402 }
Bram Moolenaared591872020-08-15 22:14:53 +02004403
Bram Moolenaar4c137212021-04-19 16:48:48 +02004404 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02004405 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02004406 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004407 if (is_blob)
4408 {
4409 if (blob_slice_or_index(val_tv->vval.v_blob, is_slice,
4410 n1, n2, FALSE, tv) == FAIL)
4411 goto on_error;
4412 }
4413 else
4414 {
4415 if (list_slice_or_index(val_tv->vval.v_list, is_slice,
4416 n1, n2, FALSE, tv, TRUE) == FAIL)
4417 goto on_error;
4418 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004419 }
4420 break;
4421
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004422 case ISN_ANYINDEX:
4423 case ISN_ANYSLICE:
4424 {
4425 int is_slice = iptr->isn_type == ISN_ANYSLICE;
4426 typval_T *var1, *var2;
4427 int res;
4428
4429 // index: composite is at stack-2, index at stack-1
4430 // slice: composite is at stack-3, indexes at stack-2 and
4431 // stack-1
4432 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02004433 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004434 if (check_can_index(tv, TRUE, TRUE) == FAIL)
4435 goto on_error;
4436 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
4437 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
Bram Moolenaar6601b622021-01-13 21:47:15 +01004438 res = eval_index_inner(tv, is_slice, var1, var2,
4439 FALSE, NULL, -1, TRUE);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004440 clear_tv(var1);
4441 if (is_slice)
4442 clear_tv(var2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004443 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004444 if (res == FAIL)
4445 goto on_error;
4446 }
4447 break;
4448
Bram Moolenaar9af78762020-06-16 11:34:42 +02004449 case ISN_SLICE:
4450 {
4451 list_T *list;
4452 int count = iptr->isn_arg.number;
4453
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02004454 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02004455 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02004456 list = tv->vval.v_list;
4457
4458 // no error for short list, expect it to be checked earlier
4459 if (list != NULL && list->lv_len >= count)
4460 {
4461 list_T *newlist = list_slice(list,
4462 count, list->lv_len - 1);
4463
4464 if (newlist != NULL)
4465 {
4466 list_unref(list);
4467 tv->vval.v_list = newlist;
4468 ++newlist->lv_refcount;
4469 }
4470 }
4471 }
4472 break;
4473
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004474 case ISN_GETITEM:
4475 {
4476 listitem_T *li;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02004477 getitem_T *gi = &iptr->isn_arg.getitem;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004478
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02004479 // Get list item: list is at stack-1, push item.
4480 // List type and length is checked for when compiling.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02004481 tv = STACK_TV_BOT(-1 - gi->gi_with_op);
4482 li = list_find(tv->vval.v_list, gi->gi_index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004483
Bram Moolenaar35578162021-08-02 19:10:38 +02004484 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004485 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004486 ++ectx->ec_stack.ga_len;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004487 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004488
4489 // Useful when used in unpack assignment. Reset at
4490 // ISN_DROP.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02004491 ectx->ec_where.wt_index = gi->gi_index + 1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004492 ectx->ec_where.wt_variable = TRUE;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004493 }
4494 break;
4495
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004496 case ISN_MEMBER:
4497 {
4498 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004499 char_u *key;
4500 dictitem_T *di;
4501
4502 // dict member: dict is at stack-2, key at stack-1
4503 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02004504 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004505 dict = tv->vval.v_dict;
4506
4507 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02004508 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004509 key = tv->vval.v_string;
Bram Moolenaar086fc9a2020-10-30 18:33:02 +01004510 if (key == NULL)
4511 key = (char_u *)"";
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02004512
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004513 if ((di = dict_find(dict, key, -1)) == NULL)
4514 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004515 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004516 semsg(_(e_key_not_present_in_dictionary), key);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004517
4518 // If :silent! is used we will continue, make sure the
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004519 // stack contents makes sense and the dict stack is
4520 // updated.
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004521 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004522 --ectx->ec_stack.ga_len;
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004523 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004524 (void) dict_stack_save(tv);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004525 tv->v_type = VAR_NUMBER;
4526 tv->vval.v_number = 0;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004527 goto on_fatal_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004528 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004529 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004530 --ectx->ec_stack.ga_len;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004531 // Put the dict used on the dict stack, it might be used by
4532 // a dict function later.
Bram Moolenaar50788ef2020-07-05 16:51:26 +02004533 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004534 if (dict_stack_save(tv) == FAIL)
4535 goto on_fatal_error;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02004536 copy_tv(&di->di_tv, tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004537 }
4538 break;
4539
4540 // dict member with string key
4541 case ISN_STRINGMEMBER:
4542 {
4543 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004544 dictitem_T *di;
4545
4546 tv = STACK_TV_BOT(-1);
4547 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
4548 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004549 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004550 emsg(_(e_dictionary_required));
Bram Moolenaard032f342020-07-18 18:13:02 +02004551 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004552 }
4553 dict = tv->vval.v_dict;
4554
4555 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
4556 == NULL)
4557 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004558 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar381692b2022-02-02 20:01:27 +00004559 semsg(_(e_key_not_present_in_dictionary),
4560 iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02004561 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004562 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004563 // Put the dict used on the dict stack, it might be used by
4564 // a dict function later.
4565 if (dict_stack_save(tv) == FAIL)
4566 goto on_fatal_error;
4567
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004568 copy_tv(&di->di_tv, tv);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004569 }
4570 break;
4571
4572 case ISN_CLEARDICT:
4573 dict_stack_drop();
4574 break;
4575
4576 case ISN_USEDICT:
4577 {
4578 typval_T *dict_tv = dict_stack_get_tv();
4579
4580 // Turn "dict.Func" into a partial for "Func" bound to
4581 // "dict". Don't do this when "Func" is already a partial
4582 // that was bound explicitly (pt_auto is FALSE).
4583 tv = STACK_TV_BOT(-1);
4584 if (dict_tv != NULL
4585 && dict_tv->v_type == VAR_DICT
4586 && dict_tv->vval.v_dict != NULL
4587 && (tv->v_type == VAR_FUNC
4588 || (tv->v_type == VAR_PARTIAL
4589 && (tv->vval.v_partial->pt_auto
4590 || tv->vval.v_partial->pt_dict == NULL))))
4591 dict_tv->vval.v_dict =
4592 make_partial(dict_tv->vval.v_dict, tv);
4593 dict_stack_drop();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004594 }
4595 break;
4596
4597 case ISN_NEGATENR:
4598 tv = STACK_TV_BOT(-1);
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00004599 // CHECKTYPE should have checked the variable type
Bram Moolenaarc58164c2020-03-29 18:40:30 +02004600#ifdef FEAT_FLOAT
4601 if (tv->v_type == VAR_FLOAT)
4602 tv->vval.v_float = -tv->vval.v_float;
4603 else
4604#endif
4605 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004606 break;
4607
4608 case ISN_CHECKNR:
4609 {
4610 int error = FALSE;
4611
4612 tv = STACK_TV_BOT(-1);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02004613 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004614 if (check_not_string(tv) == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004615 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004616 (void)tv_get_number_chk(tv, &error);
4617 if (error)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004618 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004619 }
4620 break;
4621
4622 case ISN_CHECKTYPE:
4623 {
4624 checktype_T *ct = &iptr->isn_arg.type;
4625
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01004626 tv = STACK_TV_BOT((int)ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02004627 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004628 if (!ectx->ec_where.wt_variable)
4629 ectx->ec_where.wt_index = ct->ct_arg_idx;
4630 if (check_typval_type(ct->ct_type, tv, ectx->ec_where)
4631 == FAIL)
Bram Moolenaar5e654232020-09-16 15:22:00 +02004632 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004633 if (!ectx->ec_where.wt_variable)
4634 ectx->ec_where.wt_index = 0;
Bram Moolenaar5e654232020-09-16 15:22:00 +02004635
4636 // number 0 is FALSE, number 1 is TRUE
4637 if (tv->v_type == VAR_NUMBER
4638 && ct->ct_type->tt_type == VAR_BOOL
4639 && (tv->vval.v_number == 0
4640 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004641 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02004642 tv->v_type = VAR_BOOL;
4643 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02004644 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004645 }
4646 }
4647 break;
4648
Bram Moolenaar9af78762020-06-16 11:34:42 +02004649 case ISN_CHECKLEN:
4650 {
4651 int min_len = iptr->isn_arg.checklen.cl_min_len;
4652 list_T *list = NULL;
4653
4654 tv = STACK_TV_BOT(-1);
4655 if (tv->v_type == VAR_LIST)
4656 list = tv->vval.v_list;
4657 if (list == NULL || list->lv_len < min_len
4658 || (list->lv_len > min_len
4659 && !iptr->isn_arg.checklen.cl_more_OK))
4660 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004661 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004662 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02004663 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004664 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02004665 }
4666 }
4667 break;
4668
Bram Moolenaaraa210a32021-01-02 15:41:03 +01004669 case ISN_SETTYPE:
Bram Moolenaar381692b2022-02-02 20:01:27 +00004670 set_tv_type(STACK_TV_BOT(-1), iptr->isn_arg.type.ct_type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01004671 break;
4672
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004673 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004674 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004675 {
4676 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004677 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004678
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004679 if (iptr->isn_type == ISN_2BOOL)
4680 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004681 tv = STACK_TV_BOT(iptr->isn_arg.tobool.offset);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004682 n = tv2bool(tv);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004683 if (iptr->isn_arg.tobool.invert)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004684 n = !n;
4685 }
4686 else
4687 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004688 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004689 SOURCING_LNUM = iptr->isn_lnum;
4690 n = tv_get_bool_chk(tv, &error);
4691 if (error)
4692 goto on_error;
4693 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004694 clear_tv(tv);
4695 tv->v_type = VAR_BOOL;
4696 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
4697 }
4698 break;
4699
4700 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02004701 case ISN_2STRING_ANY:
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01004702 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004703 if (do_2string(STACK_TV_BOT(iptr->isn_arg.tostring.offset),
4704 iptr->isn_type == ISN_2STRING_ANY,
4705 iptr->isn_arg.tostring.tolerant) == FAIL)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01004706 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004707 break;
4708
Bram Moolenaar08597872020-12-10 19:43:40 +01004709 case ISN_RANGE:
4710 {
4711 exarg_T ea;
4712 char *errormsg;
4713
Bram Moolenaarece0b872021-01-08 20:40:45 +01004714 ea.line2 = 0;
Bram Moolenaard1510ee2021-01-04 16:15:58 +01004715 ea.addr_count = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01004716 ea.addr_type = ADDR_LINES;
4717 ea.cmd = iptr->isn_arg.string;
Bram Moolenaarece0b872021-01-08 20:40:45 +01004718 ea.skip = FALSE;
Bram Moolenaar08597872020-12-10 19:43:40 +01004719 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaarece0b872021-01-08 20:40:45 +01004720 goto on_error;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004721
Bram Moolenaar35578162021-08-02 19:10:38 +02004722 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004723 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004724 ++ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004725 tv = STACK_TV_BOT(-1);
4726 tv->v_type = VAR_NUMBER;
4727 tv->v_lock = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01004728 if (ea.addr_count == 0)
4729 tv->vval.v_number = curwin->w_cursor.lnum;
4730 else
4731 tv->vval.v_number = ea.line2;
4732 }
4733 break;
4734
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004735 case ISN_PUT:
4736 {
4737 int regname = iptr->isn_arg.put.put_regname;
4738 linenr_T lnum = iptr->isn_arg.put.put_lnum;
4739 char_u *expr = NULL;
4740 int dir = FORWARD;
4741
Bram Moolenaar08597872020-12-10 19:43:40 +01004742 if (lnum < -2)
4743 {
4744 // line number was put on the stack by ISN_RANGE
4745 tv = STACK_TV_BOT(-1);
4746 curwin->w_cursor.lnum = tv->vval.v_number;
4747 if (lnum == LNUM_VARIABLE_RANGE_ABOVE)
4748 dir = BACKWARD;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004749 --ectx->ec_stack.ga_len;
Bram Moolenaar08597872020-12-10 19:43:40 +01004750 }
4751 else if (lnum == -2)
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004752 // :put! above cursor
4753 dir = BACKWARD;
4754 else if (lnum >= 0)
Bram Moolenaar4e713ba2022-02-07 15:31:37 +00004755 {
4756 curwin->w_cursor.lnum = lnum;
4757 if (lnum == 0)
4758 // check_cursor() below will move to line 1
4759 dir = BACKWARD;
4760 }
Bram Moolenaara28639e2021-01-19 22:48:09 +01004761
4762 if (regname == '=')
4763 {
4764 tv = STACK_TV_BOT(-1);
4765 if (tv->v_type == VAR_STRING)
4766 expr = tv->vval.v_string;
4767 else
4768 {
4769 expr = typval2string(tv, TRUE); // allocates value
4770 clear_tv(tv);
4771 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004772 --ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004773 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004774 check_cursor();
4775 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
4776 vim_free(expr);
4777 }
4778 break;
4779
Bram Moolenaar02194d22020-10-24 23:08:38 +02004780 case ISN_CMDMOD:
Bram Moolenaar4c137212021-04-19 16:48:48 +02004781 ectx->ec_funclocal.floc_save_cmdmod = cmdmod;
4782 ectx->ec_funclocal.floc_restore_cmdmod = TRUE;
4783 ectx->ec_funclocal.floc_restore_cmdmod_stacklen =
4784 ectx->ec_stack.ga_len;
Bram Moolenaar02194d22020-10-24 23:08:38 +02004785 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
4786 apply_cmdmod(&cmdmod);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004787 break;
4788
Bram Moolenaar02194d22020-10-24 23:08:38 +02004789 case ISN_CMDMOD_REV:
4790 // filter regprog is owned by the instruction, don't free it
4791 cmdmod.cmod_filter_regmatch.regprog = NULL;
4792 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004793 cmdmod = ectx->ec_funclocal.floc_save_cmdmod;
4794 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004795 break;
4796
Bram Moolenaar792f7862020-11-23 08:31:18 +01004797 case ISN_UNPACK:
4798 {
4799 int count = iptr->isn_arg.unpack.unp_count;
4800 int semicolon = iptr->isn_arg.unpack.unp_semicolon;
4801 list_T *l;
4802 listitem_T *li;
4803 int i;
4804
4805 // Check there is a valid list to unpack.
4806 tv = STACK_TV_BOT(-1);
4807 if (tv->v_type != VAR_LIST)
4808 {
4809 SOURCING_LNUM = iptr->isn_lnum;
4810 emsg(_(e_for_argument_must_be_sequence_of_lists));
4811 goto on_error;
4812 }
4813 l = tv->vval.v_list;
4814 if (l == NULL
4815 || l->lv_len < (semicolon ? count - 1 : count))
4816 {
4817 SOURCING_LNUM = iptr->isn_lnum;
4818 emsg(_(e_list_value_does_not_have_enough_items));
4819 goto on_error;
4820 }
4821 else if (!semicolon && l->lv_len > count)
4822 {
4823 SOURCING_LNUM = iptr->isn_lnum;
4824 emsg(_(e_list_value_has_more_items_than_targets));
4825 goto on_error;
4826 }
4827
4828 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar35578162021-08-02 19:10:38 +02004829 if (GA_GROW_FAILS(&ectx->ec_stack, count - 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004830 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004831 ectx->ec_stack.ga_len += count - 1;
Bram Moolenaar792f7862020-11-23 08:31:18 +01004832
4833 // Variable after semicolon gets a list with the remaining
4834 // items.
4835 if (semicolon)
4836 {
4837 list_T *rem_list =
4838 list_alloc_with_items(l->lv_len - count + 1);
4839
4840 if (rem_list == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004841 goto theend;
Bram Moolenaar792f7862020-11-23 08:31:18 +01004842 tv = STACK_TV_BOT(-count);
4843 tv->vval.v_list = rem_list;
4844 ++rem_list->lv_refcount;
4845 tv->v_lock = 0;
4846 li = l->lv_first;
4847 for (i = 0; i < count - 1; ++i)
4848 li = li->li_next;
4849 for (i = 0; li != NULL; ++i)
4850 {
Bram Moolenaar61efa162022-03-18 13:10:48 +00004851 typval_T tvcopy;
4852
4853 copy_tv(&li->li_tv, &tvcopy);
4854 list_set_item(rem_list, i, &tvcopy);
Bram Moolenaar792f7862020-11-23 08:31:18 +01004855 li = li->li_next;
4856 }
4857 --count;
4858 }
4859
4860 // Produce the values in reverse order, first item last.
4861 li = l->lv_first;
4862 for (i = 0; i < count; ++i)
4863 {
4864 tv = STACK_TV_BOT(-i - 1);
4865 copy_tv(&li->li_tv, tv);
4866 li = li->li_next;
4867 }
4868
4869 list_unref(l);
4870 }
4871 break;
4872
Bram Moolenaarb2049902021-01-24 12:53:53 +01004873 case ISN_PROF_START:
4874 case ISN_PROF_END:
4875 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01004876#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01004877 funccall_T cookie;
4878 ufunc_T *cur_ufunc =
4879 (((dfunc_T *)def_functions.ga_data)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02004880 + ectx->ec_dfunc_idx)->df_ufunc;
Bram Moolenaarb2049902021-01-24 12:53:53 +01004881
4882 cookie.func = cur_ufunc;
4883 if (iptr->isn_type == ISN_PROF_START)
4884 {
4885 func_line_start(&cookie, iptr->isn_lnum);
4886 // if we get here the instruction is executed
4887 func_line_exec(&cookie);
4888 }
4889 else
4890 func_line_end(&cookie);
Bram Moolenaarf002a412021-01-24 13:34:18 +01004891#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01004892 }
4893 break;
4894
Bram Moolenaare99d4222021-06-13 14:01:26 +02004895 case ISN_DEBUG:
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02004896 handle_debug(iptr, ectx);
Bram Moolenaare99d4222021-06-13 14:01:26 +02004897 break;
4898
Bram Moolenaar389df252020-07-09 21:20:47 +02004899 case ISN_SHUFFLE:
4900 {
Bram Moolenaar792f7862020-11-23 08:31:18 +01004901 typval_T tmp_tv;
4902 int item = iptr->isn_arg.shuffle.shfl_item;
4903 int up = iptr->isn_arg.shuffle.shfl_up;
Bram Moolenaar389df252020-07-09 21:20:47 +02004904
4905 tmp_tv = *STACK_TV_BOT(-item);
4906 for ( ; up > 0 && item > 1; --up)
4907 {
4908 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
4909 --item;
4910 }
4911 *STACK_TV_BOT(-item) = tmp_tv;
4912 }
4913 break;
4914
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004915 case ISN_DROP:
Bram Moolenaar4c137212021-04-19 16:48:48 +02004916 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004917 clear_tv(STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02004918 ectx->ec_where.wt_index = 0;
4919 ectx->ec_where.wt_variable = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004920 break;
4921 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004922 continue;
4923
Bram Moolenaard032f342020-07-18 18:13:02 +02004924func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02004925 // Restore previous function. If the frame pointer is where we started
4926 // then there is none and we are done.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004927 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02004928 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02004929
Bram Moolenaar4c137212021-04-19 16:48:48 +02004930 if (func_return(ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004931 // only fails when out of memory
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004932 goto theend;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02004933 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02004934
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004935on_error:
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004936 // Jump here for an error that does not require aborting execution.
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004937 // If "emsg_silent" is set then ignore the error, unless it was set
4938 // when calling the function.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004939 if (did_emsg_cumul + did_emsg == ectx->ec_did_emsg_before
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004940 && emsg_silent && did_emsg_def == 0)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004941 {
4942 // If a sequence of instructions causes an error while ":silent!"
4943 // was used, restore the stack length and jump ahead to restoring
4944 // the cmdmod.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004945 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004946 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004947 while (ectx->ec_stack.ga_len
4948 > ectx->ec_funclocal.floc_restore_cmdmod_stacklen)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004949 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004950 --ectx->ec_stack.ga_len;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004951 clear_tv(STACK_TV_BOT(0));
4952 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004953 while (ectx->ec_instr[ectx->ec_iidx].isn_type != ISN_CMDMOD_REV)
4954 ++ectx->ec_iidx;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004955 }
Bram Moolenaarcd030c42020-10-30 21:49:40 +01004956 continue;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004957 }
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004958on_fatal_error:
4959 // Jump here for an error that messes up the stack.
Bram Moolenaar171fb922020-10-28 16:54:47 +01004960 // If we are not inside a try-catch started here, abort execution.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004961 if (trylevel <= ectx->ec_trylevel_at_start)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004962 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004963 }
4964
4965done:
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004966 ret = OK;
4967theend:
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004968 dict_stack_clear(dict_stack_len_at_start);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004969 ectx->ec_trylevel_at_start = save_trylevel_at_start;
4970 return ret;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004971}
4972
4973/*
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004974 * Execute the instructions from a VAR_INSTR typeval and put the result in
4975 * "rettv".
4976 * Return OK or FAIL.
4977 */
4978 int
4979exe_typval_instr(typval_T *tv, typval_T *rettv)
4980{
4981 ectx_T *ectx = tv->vval.v_instr->instr_ectx;
4982 isn_T *save_instr = ectx->ec_instr;
4983 int save_iidx = ectx->ec_iidx;
4984 int res;
4985
4986 ectx->ec_instr = tv->vval.v_instr->instr_instr;
4987 res = exec_instructions(ectx);
4988 if (res == OK)
4989 {
4990 *rettv = *STACK_TV_BOT(-1);
4991 --ectx->ec_stack.ga_len;
4992 }
4993
4994 ectx->ec_instr = save_instr;
4995 ectx->ec_iidx = save_iidx;
4996
4997 return res;
4998}
4999
5000/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02005001 * Execute the instructions from an ISN_SUBSTITUTE command, which are in
5002 * "substitute_instr".
5003 */
5004 char_u *
5005exe_substitute_instr(void)
5006{
5007 ectx_T *ectx = substitute_instr->subs_ectx;
5008 isn_T *save_instr = ectx->ec_instr;
5009 int save_iidx = ectx->ec_iidx;
5010 char_u *res;
5011
5012 ectx->ec_instr = substitute_instr->subs_instr;
5013 if (exec_instructions(ectx) == OK)
Bram Moolenaar90193e62021-04-04 20:49:50 +02005014 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005015 typval_T *tv = STACK_TV_BOT(-1);
5016
Bram Moolenaar27523602021-06-05 21:36:19 +02005017 res = typval2string(tv, TRUE);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005018 --ectx->ec_stack.ga_len;
5019 clear_tv(tv);
Bram Moolenaar90193e62021-04-04 20:49:50 +02005020 }
5021 else
5022 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005023 substitute_instr->subs_status = FAIL;
5024 res = vim_strsave((char_u *)"");
Bram Moolenaar90193e62021-04-04 20:49:50 +02005025 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005026
Bram Moolenaar4c137212021-04-19 16:48:48 +02005027 ectx->ec_instr = save_instr;
5028 ectx->ec_iidx = save_iidx;
5029
5030 return res;
5031}
5032
5033/*
5034 * Call a "def" function from old Vim script.
5035 * Return OK or FAIL.
5036 */
5037 int
5038call_def_function(
5039 ufunc_T *ufunc,
5040 int argc_arg, // nr of arguments
5041 typval_T *argv, // arguments
5042 partial_T *partial, // optional partial for context
5043 typval_T *rettv) // return value
5044{
5045 ectx_T ectx; // execution context
5046 int argc = argc_arg;
5047 typval_T *tv;
5048 int idx;
5049 int ret = FAIL;
5050 int defcount = ufunc->uf_args.ga_len - argc;
5051 sctx_T save_current_sctx = current_sctx;
5052 int did_emsg_before = did_emsg_cumul + did_emsg;
5053 int save_suppress_errthrow = suppress_errthrow;
5054 msglist_T **saved_msg_list = NULL;
5055 msglist_T *private_msg_list = NULL;
5056 int save_emsg_silent_def = emsg_silent_def;
5057 int save_did_emsg_def = did_emsg_def;
5058 int orig_funcdepth;
Bram Moolenaare99d4222021-06-13 14:01:26 +02005059 int orig_nesting_level = ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005060
5061// Get pointer to item in the stack.
5062#undef STACK_TV
5063#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
5064
5065// Get pointer to item at the bottom of the stack, -1 is the bottom.
5066#undef STACK_TV_BOT
5067#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
5068
5069// Get pointer to a local variable on the stack. Negative for arguments.
5070#undef STACK_TV_VAR
5071#define STACK_TV_VAR(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_frame_idx + STACK_FRAME_SIZE + idx)
5072
5073 if (ufunc->uf_def_status == UF_NOT_COMPILED
5074 || ufunc->uf_def_status == UF_COMPILE_ERROR
Bram Moolenaar139575d2022-03-15 19:29:30 +00005075 || (func_needs_compiling(ufunc, get_compile_type(ufunc))
5076 && compile_def_function(ufunc, FALSE,
5077 get_compile_type(ufunc), NULL) == FAIL))
Bram Moolenaar4c137212021-04-19 16:48:48 +02005078 {
5079 if (did_emsg_cumul + did_emsg == did_emsg_before)
5080 semsg(_(e_function_is_not_compiled_str),
5081 printable_func_name(ufunc));
5082 return FAIL;
5083 }
5084
5085 {
5086 // Check the function was really compiled.
5087 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5088 + ufunc->uf_dfunc_idx;
5089 if (INSTRUCTIONS(dfunc) == NULL)
5090 {
5091 iemsg("using call_def_function() on not compiled function");
5092 return FAIL;
5093 }
5094 }
5095
5096 // If depth of calling is getting too high, don't execute the function.
5097 orig_funcdepth = funcdepth_get();
5098 if (funcdepth_increment() == FAIL)
5099 return FAIL;
5100
5101 CLEAR_FIELD(ectx);
5102 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
5103 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
Bram Moolenaar35578162021-08-02 19:10:38 +02005104 if (GA_GROW_FAILS(&ectx.ec_stack, 20))
Bram Moolenaar4c137212021-04-19 16:48:48 +02005105 {
5106 funcdepth_decrement();
5107 return FAIL;
5108 }
5109 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
5110 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
5111 ectx.ec_did_emsg_before = did_emsg_before;
Bram Moolenaare99d4222021-06-13 14:01:26 +02005112 ++ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005113
5114 idx = argc - ufunc->uf_args.ga_len;
5115 if (idx > 0 && ufunc->uf_va_name == NULL)
5116 {
5117 if (idx == 1)
5118 emsg(_(e_one_argument_too_many));
5119 else
5120 semsg(_(e_nr_arguments_too_many), idx);
5121 goto failed_early;
5122 }
Bram Moolenaarc6d71532021-06-05 18:49:38 +02005123 idx = argc - ufunc->uf_args.ga_len + ufunc->uf_def_args.ga_len;
5124 if (idx < 0)
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02005125 {
5126 if (idx == -1)
5127 emsg(_(e_one_argument_too_few));
5128 else
5129 semsg(_(e_nr_arguments_too_few), -idx);
5130 goto failed_early;
5131 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005132
5133 // Put arguments on the stack, but no more than what the function expects.
5134 // A lambda can be called with more arguments than it uses.
5135 for (idx = 0; idx < argc
5136 && (ufunc->uf_va_name != NULL || idx < ufunc->uf_args.ga_len);
5137 ++idx)
5138 {
5139 if (idx >= ufunc->uf_args.ga_len - ufunc->uf_def_args.ga_len
5140 && argv[idx].v_type == VAR_SPECIAL
5141 && argv[idx].vval.v_number == VVAL_NONE)
5142 {
5143 // Use the default value.
5144 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
5145 }
5146 else
5147 {
5148 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
5149 && check_typval_arg_type(
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02005150 ufunc->uf_arg_types[idx], &argv[idx],
5151 NULL, idx + 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005152 goto failed_early;
5153 copy_tv(&argv[idx], STACK_TV_BOT(0));
5154 }
5155 ++ectx.ec_stack.ga_len;
5156 }
5157
5158 // Turn varargs into a list. Empty list if no args.
5159 if (ufunc->uf_va_name != NULL)
5160 {
5161 int vararg_count = argc - ufunc->uf_args.ga_len;
5162
5163 if (vararg_count < 0)
5164 vararg_count = 0;
5165 else
5166 argc -= vararg_count;
5167 if (exe_newlist(vararg_count, &ectx) == FAIL)
5168 goto failed_early;
5169
5170 // Check the type of the list items.
5171 tv = STACK_TV_BOT(-1);
5172 if (ufunc->uf_va_type != NULL
5173 && ufunc->uf_va_type != &t_list_any
5174 && ufunc->uf_va_type->tt_member != &t_any
5175 && tv->vval.v_list != NULL)
5176 {
5177 type_T *expected = ufunc->uf_va_type->tt_member;
5178 listitem_T *li = tv->vval.v_list->lv_first;
5179
5180 for (idx = 0; idx < vararg_count; ++idx)
5181 {
5182 if (check_typval_arg_type(expected, &li->li_tv,
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02005183 NULL, argc + idx + 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005184 goto failed_early;
5185 li = li->li_next;
5186 }
5187 }
5188
5189 if (defcount > 0)
5190 // Move varargs list to below missing default arguments.
5191 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
5192 --ectx.ec_stack.ga_len;
5193 }
5194
5195 // Make space for omitted arguments, will store default value below.
5196 // Any varargs list goes after them.
5197 if (defcount > 0)
5198 for (idx = 0; idx < defcount; ++idx)
5199 {
5200 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
5201 ++ectx.ec_stack.ga_len;
5202 }
5203 if (ufunc->uf_va_name != NULL)
5204 ++ectx.ec_stack.ga_len;
5205
5206 // Frame pointer points to just after arguments.
5207 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
5208 ectx.ec_initial_frame_idx = ectx.ec_frame_idx;
5209
5210 {
5211 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5212 + ufunc->uf_dfunc_idx;
5213 ufunc_T *base_ufunc = dfunc->df_ufunc;
5214
5215 // "uf_partial" is on the ufunc that "df_ufunc" points to, as is done
5216 // by copy_func().
5217 if (partial != NULL || base_ufunc->uf_partial != NULL)
5218 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005219 ectx.ec_outer_ref = ALLOC_CLEAR_ONE(outer_ref_T);
5220 if (ectx.ec_outer_ref == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005221 goto failed_early;
5222 if (partial != NULL)
5223 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00005224 outer_T *outer = get_pt_outer(partial);
5225
5226 if (outer->out_stack == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005227 {
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00005228 if (current_ectx != NULL)
5229 {
5230 if (current_ectx->ec_outer_ref != NULL
5231 && current_ectx->ec_outer_ref->or_outer != NULL)
5232 ectx.ec_outer_ref->or_outer =
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005233 current_ectx->ec_outer_ref->or_outer;
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00005234 }
5235 // Should there be an error here?
Bram Moolenaar4c137212021-04-19 16:48:48 +02005236 }
5237 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005238 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00005239 ectx.ec_outer_ref->or_outer = outer;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005240 ++partial->pt_refcount;
5241 ectx.ec_outer_ref->or_partial = partial;
5242 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005243 }
5244 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005245 {
5246 ectx.ec_outer_ref->or_outer = &base_ufunc->uf_partial->pt_outer;
5247 ++base_ufunc->uf_partial->pt_refcount;
5248 ectx.ec_outer_ref->or_partial = base_ufunc->uf_partial;
5249 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005250 }
5251 }
5252
5253 // dummy frame entries
5254 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
5255 {
5256 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
5257 ++ectx.ec_stack.ga_len;
5258 }
5259
5260 {
5261 // Reserve space for local variables and any closure reference count.
5262 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5263 + ufunc->uf_dfunc_idx;
5264
Bram Moolenaar5cd64792021-12-25 18:23:24 +00005265 // Initialize variables to zero. That avoids having to generate
5266 // initializing instructions for "var nr: number", "var x: any", etc.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005267 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaar5cd64792021-12-25 18:23:24 +00005268 {
5269 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
5270 STACK_TV_VAR(idx)->vval.v_number = 0;
5271 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005272 ectx.ec_stack.ga_len += dfunc->df_varcount;
5273 if (dfunc->df_has_closure)
5274 {
5275 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
5276 STACK_TV_VAR(idx)->vval.v_number = 0;
5277 ++ectx.ec_stack.ga_len;
5278 }
5279
5280 ectx.ec_instr = INSTRUCTIONS(dfunc);
5281 }
5282
5283 // Following errors are in the function, not the caller.
5284 // Commands behave like vim9script.
5285 estack_push_ufunc(ufunc, 1);
5286 current_sctx = ufunc->uf_script_ctx;
5287 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
5288
5289 // Use a specific location for storing error messages to be converted to an
5290 // exception.
5291 saved_msg_list = msg_list;
5292 msg_list = &private_msg_list;
5293
5294 // Do turn errors into exceptions.
5295 suppress_errthrow = FALSE;
5296
5297 // Do not delete the function while executing it.
5298 ++ufunc->uf_calls;
5299
5300 // When ":silent!" was used before calling then we still abort the
5301 // function. If ":silent!" is used in the function then we don't.
5302 emsg_silent_def = emsg_silent;
5303 did_emsg_def = 0;
5304
5305 ectx.ec_where.wt_index = 0;
5306 ectx.ec_where.wt_variable = FALSE;
5307
5308 // Execute the instructions until done.
5309 ret = exec_instructions(&ectx);
5310 if (ret == OK)
5311 {
5312 // function finished, get result from the stack.
5313 if (ufunc->uf_ret_type == &t_void)
5314 {
5315 rettv->v_type = VAR_VOID;
5316 }
5317 else
5318 {
5319 tv = STACK_TV_BOT(-1);
5320 *rettv = *tv;
5321 tv->v_type = VAR_UNKNOWN;
5322 }
5323 }
5324
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01005325 // When failed need to unwind the call stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005326 while (ectx.ec_frame_idx != ectx.ec_initial_frame_idx)
5327 func_return(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02005328
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005329 // Deal with any remaining closures, they may be in use somewhere.
5330 if (ectx.ec_funcrefs.ga_len > 0)
Bram Moolenaarf112f302020-12-20 17:47:52 +01005331 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005332 handle_closure_in_use(&ectx, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00005333 ga_clear(&ectx.ec_funcrefs);
Bram Moolenaarf112f302020-12-20 17:47:52 +01005334 }
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005335
Bram Moolenaaree8580e2020-08-28 17:19:07 +02005336 estack_pop();
5337 current_sctx = save_current_sctx;
5338
Bram Moolenaar2336c372021-12-06 15:06:54 +00005339 if (--ufunc->uf_calls <= 0 && ufunc->uf_refcount <= 0)
5340 // Function was unreferenced while being used, free it now.
5341 func_clear_free(ufunc, FALSE);
Bram Moolenaarc970e422021-03-17 15:03:04 +01005342
Bram Moolenaar352134b2020-10-17 22:04:08 +02005343 if (*msg_list != NULL && saved_msg_list != NULL)
5344 {
5345 msglist_T **plist = saved_msg_list;
5346
5347 // Append entries from the current msg_list (uncaught exceptions) to
5348 // the saved msg_list.
5349 while (*plist != NULL)
5350 plist = &(*plist)->next;
5351
5352 *plist = *msg_list;
5353 }
5354 msg_list = saved_msg_list;
5355
Bram Moolenaar4c137212021-04-19 16:48:48 +02005356 if (ectx.ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar02194d22020-10-24 23:08:38 +02005357 {
5358 cmdmod.cmod_filter_regmatch.regprog = NULL;
5359 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005360 cmdmod = ectx.ec_funclocal.floc_save_cmdmod;
Bram Moolenaar02194d22020-10-24 23:08:38 +02005361 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +01005362 emsg_silent_def = save_emsg_silent_def;
5363 did_emsg_def += save_did_emsg_def;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02005364
Bram Moolenaaree8580e2020-08-28 17:19:07 +02005365failed_early:
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02005366 // Free all local variables, but not arguments.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005367 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
5368 clear_tv(STACK_TV(idx));
Bram Moolenaare99d4222021-06-13 14:01:26 +02005369 ex_nesting_level = orig_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02005370
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005371 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01005372 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005373 if (ectx.ec_outer_ref != NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +01005374 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005375 if (ectx.ec_outer_ref->or_outer_allocated)
5376 vim_free(ectx.ec_outer_ref->or_outer);
5377 partial_unref(ectx.ec_outer_ref->or_partial);
5378 vim_free(ectx.ec_outer_ref);
Bram Moolenaar0186e582021-01-10 18:33:11 +01005379 }
5380
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02005381 // Not sure if this is necessary.
5382 suppress_errthrow = save_suppress_errthrow;
5383
Bram Moolenaarb5841b92021-07-15 18:09:53 +02005384 if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before
5385 && !need_rethrow)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005386 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02005387 printable_func_name(ufunc));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01005388 funcdepth_restore(orig_funcdepth);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005389 return ret;
5390}
5391
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005392/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02005393 * List instructions "instr" up to "instr_count" or until ISN_FINISH.
5394 * "ufunc" has the source lines, NULL for the instructions of ISN_SUBSTITUTE.
5395 * "pfx" is prefixed to every line.
5396 */
5397 static void
5398list_instructions(char *pfx, isn_T *instr, int instr_count, ufunc_T *ufunc)
5399{
5400 int line_idx = 0;
5401 int prev_current = 0;
5402 int current;
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02005403 int def_arg_idx = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005404
5405 for (current = 0; current < instr_count; ++current)
5406 {
5407 isn_T *iptr = &instr[current];
5408 char *line;
5409
5410 if (ufunc != NULL)
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02005411 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005412 while (line_idx < iptr->isn_lnum
5413 && line_idx < ufunc->uf_lines.ga_len)
5414 {
5415 if (current > prev_current)
5416 {
5417 msg_puts("\n\n");
5418 prev_current = current;
5419 }
5420 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
5421 if (line != NULL)
5422 msg(line);
5423 }
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02005424 if (iptr->isn_type == ISN_JUMP_IF_ARG_SET)
5425 {
5426 int first_def_arg = ufunc->uf_args.ga_len
5427 - ufunc->uf_def_args.ga_len;
5428
5429 if (def_arg_idx > 0)
5430 msg_puts("\n\n");
5431 msg_start();
5432 msg_puts(" ");
5433 msg_puts(((char **)(ufunc->uf_args.ga_data))[
5434 first_def_arg + def_arg_idx]);
5435 msg_puts(" = ");
5436 msg_puts(((char **)(ufunc->uf_def_args.ga_data))[def_arg_idx++]);
5437 msg_clr_eos();
5438 msg_end();
5439 }
5440 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005441
5442 switch (iptr->isn_type)
5443 {
5444 case ISN_EXEC:
5445 smsg("%s%4d EXEC %s", pfx, current, iptr->isn_arg.string);
5446 break;
Bram Moolenaar20677332021-06-06 17:02:53 +02005447 case ISN_EXEC_SPLIT:
5448 smsg("%s%4d EXEC_SPLIT %s", pfx, current, iptr->isn_arg.string);
5449 break;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00005450 case ISN_EXECRANGE:
5451 smsg("%s%4d EXECRANGE %s", pfx, current, iptr->isn_arg.string);
5452 break;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005453 case ISN_LEGACY_EVAL:
5454 smsg("%s%4d EVAL legacy %s", pfx, current,
5455 iptr->isn_arg.string);
5456 break;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02005457 case ISN_REDIRSTART:
5458 smsg("%s%4d REDIR", pfx, current);
5459 break;
5460 case ISN_REDIREND:
5461 smsg("%s%4d REDIR END%s", pfx, current,
5462 iptr->isn_arg.number ? " append" : "");
5463 break;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005464 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005465#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005466 smsg("%s%4d CEXPR pre %s", pfx, current,
5467 cexpr_get_auname(iptr->isn_arg.number));
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005468#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005469 break;
5470 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005471#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005472 {
5473 cexprref_T *cer = iptr->isn_arg.cexpr.cexpr_ref;
5474
5475 smsg("%s%4d CEXPR core %s%s \"%s\"", pfx, current,
5476 cexpr_get_auname(cer->cer_cmdidx),
5477 cer->cer_forceit ? "!" : "",
5478 cer->cer_cmdline);
5479 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005480#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005481 break;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005482 case ISN_INSTR:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01005483 smsg("%s%4d INSTR", pfx, current);
5484 list_instructions(" ", iptr->isn_arg.instr, INT_MAX, NULL);
5485 msg(" -------------");
5486 break;
5487 case ISN_SOURCE:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005488 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01005489 scriptitem_T *si = SCRIPT_ITEM(iptr->isn_arg.number);
5490
5491 smsg("%s%4d SOURCE %s", pfx, current, si->sn_name);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005492 }
5493 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005494 case ISN_SUBSTITUTE:
5495 {
5496 subs_T *subs = &iptr->isn_arg.subs;
5497
5498 smsg("%s%4d SUBSTITUTE %s", pfx, current, subs->subs_cmd);
5499 list_instructions(" ", subs->subs_instr, INT_MAX, NULL);
5500 msg(" -------------");
5501 }
5502 break;
5503 case ISN_EXECCONCAT:
5504 smsg("%s%4d EXECCONCAT %lld", pfx, current,
5505 (varnumber_T)iptr->isn_arg.number);
5506 break;
5507 case ISN_ECHO:
5508 {
5509 echo_T *echo = &iptr->isn_arg.echo;
5510
5511 smsg("%s%4d %s %d", pfx, current,
5512 echo->echo_with_white ? "ECHO" : "ECHON",
5513 echo->echo_count);
5514 }
5515 break;
5516 case ISN_EXECUTE:
5517 smsg("%s%4d EXECUTE %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02005518 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005519 break;
5520 case ISN_ECHOMSG:
5521 smsg("%s%4d ECHOMSG %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02005522 (varnumber_T)(iptr->isn_arg.number));
5523 break;
5524 case ISN_ECHOCONSOLE:
5525 smsg("%s%4d ECHOCONSOLE %lld", pfx, current,
5526 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005527 break;
5528 case ISN_ECHOERR:
5529 smsg("%s%4d ECHOERR %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02005530 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005531 break;
5532 case ISN_LOAD:
5533 {
5534 if (iptr->isn_arg.number < 0)
5535 smsg("%s%4d LOAD arg[%lld]", pfx, current,
5536 (varnumber_T)(iptr->isn_arg.number
5537 + STACK_FRAME_SIZE));
5538 else
5539 smsg("%s%4d LOAD $%lld", pfx, current,
5540 (varnumber_T)(iptr->isn_arg.number));
5541 }
5542 break;
5543 case ISN_LOADOUTER:
5544 {
5545 if (iptr->isn_arg.number < 0)
5546 smsg("%s%4d LOADOUTER level %d arg[%d]", pfx, current,
5547 iptr->isn_arg.outer.outer_depth,
5548 iptr->isn_arg.outer.outer_idx
5549 + STACK_FRAME_SIZE);
5550 else
5551 smsg("%s%4d LOADOUTER level %d $%d", pfx, current,
5552 iptr->isn_arg.outer.outer_depth,
5553 iptr->isn_arg.outer.outer_idx);
5554 }
5555 break;
5556 case ISN_LOADV:
5557 smsg("%s%4d LOADV v:%s", pfx, current,
5558 get_vim_var_name(iptr->isn_arg.number));
5559 break;
5560 case ISN_LOADSCRIPT:
5561 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005562 scriptref_T *sref = iptr->isn_arg.script.scriptref;
5563 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
5564 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005565
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005566 sv = get_script_svar(sref, -1);
5567 if (sv == NULL)
5568 smsg("%s%4d LOADSCRIPT [deleted] from %s",
5569 pfx, current, si->sn_name);
5570 else
5571 smsg("%s%4d LOADSCRIPT %s-%d from %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02005572 sv->sv_name,
5573 sref->sref_idx,
5574 si->sn_name);
5575 }
5576 break;
5577 case ISN_LOADS:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01005578 case ISN_LOADEXPORT:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005579 {
5580 scriptitem_T *si = SCRIPT_ITEM(
5581 iptr->isn_arg.loadstore.ls_sid);
5582
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01005583 smsg("%s%4d %s s:%s from %s", pfx, current,
5584 iptr->isn_type == ISN_LOADS ? "LOADS"
5585 : "LOADEXPORT",
5586 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005587 }
5588 break;
5589 case ISN_LOADAUTO:
5590 smsg("%s%4d LOADAUTO %s", pfx, current, iptr->isn_arg.string);
5591 break;
5592 case ISN_LOADG:
5593 smsg("%s%4d LOADG g:%s", pfx, current, iptr->isn_arg.string);
5594 break;
5595 case ISN_LOADB:
5596 smsg("%s%4d LOADB b:%s", pfx, current, iptr->isn_arg.string);
5597 break;
5598 case ISN_LOADW:
5599 smsg("%s%4d LOADW w:%s", pfx, current, iptr->isn_arg.string);
5600 break;
5601 case ISN_LOADT:
5602 smsg("%s%4d LOADT t:%s", pfx, current, iptr->isn_arg.string);
5603 break;
5604 case ISN_LOADGDICT:
5605 smsg("%s%4d LOAD g:", pfx, current);
5606 break;
5607 case ISN_LOADBDICT:
5608 smsg("%s%4d LOAD b:", pfx, current);
5609 break;
5610 case ISN_LOADWDICT:
5611 smsg("%s%4d LOAD w:", pfx, current);
5612 break;
5613 case ISN_LOADTDICT:
5614 smsg("%s%4d LOAD t:", pfx, current);
5615 break;
5616 case ISN_LOADOPT:
5617 smsg("%s%4d LOADOPT %s", pfx, current, iptr->isn_arg.string);
5618 break;
5619 case ISN_LOADENV:
5620 smsg("%s%4d LOADENV %s", pfx, current, iptr->isn_arg.string);
5621 break;
5622 case ISN_LOADREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005623 smsg("%s%4d LOADREG @%c", pfx, current,
5624 (int)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005625 break;
5626
5627 case ISN_STORE:
5628 if (iptr->isn_arg.number < 0)
5629 smsg("%s%4d STORE arg[%lld]", pfx, current,
5630 iptr->isn_arg.number + STACK_FRAME_SIZE);
5631 else
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005632 smsg("%s%4d STORE $%lld", pfx, current,
5633 iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005634 break;
5635 case ISN_STOREOUTER:
5636 {
5637 if (iptr->isn_arg.number < 0)
5638 smsg("%s%4d STOREOUTEr level %d arg[%d]", pfx, current,
5639 iptr->isn_arg.outer.outer_depth,
5640 iptr->isn_arg.outer.outer_idx + STACK_FRAME_SIZE);
5641 else
5642 smsg("%s%4d STOREOUTER level %d $%d", pfx, current,
5643 iptr->isn_arg.outer.outer_depth,
5644 iptr->isn_arg.outer.outer_idx);
5645 }
5646 break;
5647 case ISN_STOREV:
5648 smsg("%s%4d STOREV v:%s", pfx, current,
5649 get_vim_var_name(iptr->isn_arg.number));
5650 break;
5651 case ISN_STOREAUTO:
5652 smsg("%s%4d STOREAUTO %s", pfx, current, iptr->isn_arg.string);
5653 break;
5654 case ISN_STOREG:
5655 smsg("%s%4d STOREG %s", pfx, current, iptr->isn_arg.string);
5656 break;
5657 case ISN_STOREB:
5658 smsg("%s%4d STOREB %s", pfx, current, iptr->isn_arg.string);
5659 break;
5660 case ISN_STOREW:
5661 smsg("%s%4d STOREW %s", pfx, current, iptr->isn_arg.string);
5662 break;
5663 case ISN_STORET:
5664 smsg("%s%4d STORET %s", pfx, current, iptr->isn_arg.string);
5665 break;
5666 case ISN_STORES:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01005667 case ISN_STOREEXPORT:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005668 {
5669 scriptitem_T *si = SCRIPT_ITEM(
5670 iptr->isn_arg.loadstore.ls_sid);
5671
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01005672 smsg("%s%4d %s %s in %s", pfx, current,
5673 iptr->isn_type == ISN_STORES
5674 ? "STORES" : "STOREEXPORT",
Bram Moolenaar4c137212021-04-19 16:48:48 +02005675 iptr->isn_arg.loadstore.ls_name, si->sn_name);
5676 }
5677 break;
5678 case ISN_STORESCRIPT:
5679 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005680 scriptref_T *sref = iptr->isn_arg.script.scriptref;
5681 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
5682 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005683
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005684 sv = get_script_svar(sref, -1);
5685 if (sv == NULL)
5686 smsg("%s%4d STORESCRIPT [deleted] in %s",
5687 pfx, current, si->sn_name);
5688 else
5689 smsg("%s%4d STORESCRIPT %s-%d in %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02005690 sv->sv_name,
5691 sref->sref_idx,
5692 si->sn_name);
5693 }
5694 break;
5695 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00005696 case ISN_STOREFUNCOPT:
5697 smsg("%s%4d %s &%s", pfx, current,
5698 iptr->isn_type == ISN_STOREOPT ? "STOREOPT" : "STOREFUNCOPT",
Bram Moolenaar4c137212021-04-19 16:48:48 +02005699 iptr->isn_arg.storeopt.so_name);
5700 break;
5701 case ISN_STOREENV:
5702 smsg("%s%4d STOREENV $%s", pfx, current, iptr->isn_arg.string);
5703 break;
5704 case ISN_STOREREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005705 smsg("%s%4d STOREREG @%c", pfx, current,
5706 (int)iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005707 break;
5708 case ISN_STORENR:
5709 smsg("%s%4d STORE %lld in $%d", pfx, current,
5710 iptr->isn_arg.storenr.stnr_val,
5711 iptr->isn_arg.storenr.stnr_idx);
5712 break;
5713
5714 case ISN_STOREINDEX:
5715 smsg("%s%4d STOREINDEX %s", pfx, current,
5716 vartype_name(iptr->isn_arg.vartype));
5717 break;
5718
5719 case ISN_STORERANGE:
5720 smsg("%s%4d STORERANGE", pfx, current);
5721 break;
5722
5723 // constants
5724 case ISN_PUSHNR:
5725 smsg("%s%4d PUSHNR %lld", pfx, current,
5726 (varnumber_T)(iptr->isn_arg.number));
5727 break;
5728 case ISN_PUSHBOOL:
5729 case ISN_PUSHSPEC:
5730 smsg("%s%4d PUSH %s", pfx, current,
5731 get_var_special_name(iptr->isn_arg.number));
5732 break;
5733 case ISN_PUSHF:
5734#ifdef FEAT_FLOAT
5735 smsg("%s%4d PUSHF %g", pfx, current, iptr->isn_arg.fnumber);
5736#endif
5737 break;
5738 case ISN_PUSHS:
5739 smsg("%s%4d PUSHS \"%s\"", pfx, current, iptr->isn_arg.string);
5740 break;
5741 case ISN_PUSHBLOB:
5742 {
5743 char_u *r;
5744 char_u numbuf[NUMBUFLEN];
5745 char_u *tofree;
5746
5747 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
5748 smsg("%s%4d PUSHBLOB %s", pfx, current, r);
5749 vim_free(tofree);
5750 }
5751 break;
5752 case ISN_PUSHFUNC:
5753 {
5754 char *name = (char *)iptr->isn_arg.string;
5755
5756 smsg("%s%4d PUSHFUNC \"%s\"", pfx, current,
5757 name == NULL ? "[none]" : name);
5758 }
5759 break;
5760 case ISN_PUSHCHANNEL:
5761#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar397a87a2022-03-20 21:14:15 +00005762 smsg("%s%4d PUSHCHANNEL 0", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005763#endif
5764 break;
5765 case ISN_PUSHJOB:
5766#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar397a87a2022-03-20 21:14:15 +00005767 smsg("%s%4d PUSHJOB \"no process\"", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005768#endif
5769 break;
5770 case ISN_PUSHEXC:
5771 smsg("%s%4d PUSH v:exception", pfx, current);
5772 break;
Bram Moolenaar06b77222022-01-25 15:51:56 +00005773 case ISN_AUTOLOAD:
5774 smsg("%s%4d AUTOLOAD %s", pfx, current, iptr->isn_arg.string);
5775 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005776 case ISN_UNLET:
5777 smsg("%s%4d UNLET%s %s", pfx, current,
5778 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
5779 iptr->isn_arg.unlet.ul_name);
5780 break;
5781 case ISN_UNLETENV:
5782 smsg("%s%4d UNLETENV%s $%s", pfx, current,
5783 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
5784 iptr->isn_arg.unlet.ul_name);
5785 break;
5786 case ISN_UNLETINDEX:
5787 smsg("%s%4d UNLETINDEX", pfx, current);
5788 break;
5789 case ISN_UNLETRANGE:
5790 smsg("%s%4d UNLETRANGE", pfx, current);
5791 break;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02005792 case ISN_LOCKUNLOCK:
5793 smsg("%s%4d LOCKUNLOCK %s", pfx, current, iptr->isn_arg.string);
5794 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005795 case ISN_LOCKCONST:
5796 smsg("%s%4d LOCKCONST", pfx, current);
5797 break;
5798 case ISN_NEWLIST:
5799 smsg("%s%4d NEWLIST size %lld", pfx, current,
5800 (varnumber_T)(iptr->isn_arg.number));
5801 break;
5802 case ISN_NEWDICT:
5803 smsg("%s%4d NEWDICT size %lld", pfx, current,
5804 (varnumber_T)(iptr->isn_arg.number));
5805 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00005806 case ISN_NEWPARTIAL:
5807 smsg("%s%4d NEWPARTIAL", pfx, current);
5808 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005809
5810 // function call
5811 case ISN_BCALL:
5812 {
5813 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
5814
5815 smsg("%s%4d BCALL %s(argc %d)", pfx, current,
5816 internal_func_name(cbfunc->cbf_idx),
5817 cbfunc->cbf_argcount);
5818 }
5819 break;
5820 case ISN_DCALL:
5821 {
5822 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
5823 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
5824 + cdfunc->cdf_idx;
5825
5826 smsg("%s%4d DCALL %s(argc %d)", pfx, current,
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005827 printable_func_name(df->df_ufunc),
5828 cdfunc->cdf_argcount);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005829 }
5830 break;
5831 case ISN_UCALL:
5832 {
5833 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
5834
5835 smsg("%s%4d UCALL %s(argc %d)", pfx, current,
5836 cufunc->cuf_name, cufunc->cuf_argcount);
5837 }
5838 break;
5839 case ISN_PCALL:
5840 {
5841 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
5842
5843 smsg("%s%4d PCALL%s (argc %d)", pfx, current,
5844 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
5845 }
5846 break;
5847 case ISN_PCALL_END:
5848 smsg("%s%4d PCALL end", pfx, current);
5849 break;
5850 case ISN_RETURN:
5851 smsg("%s%4d RETURN", pfx, current);
5852 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02005853 case ISN_RETURN_VOID:
5854 smsg("%s%4d RETURN void", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005855 break;
5856 case ISN_FUNCREF:
5857 {
5858 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar38453522021-11-28 22:00:12 +00005859 char_u *name;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005860
Bram Moolenaar38453522021-11-28 22:00:12 +00005861 if (funcref->fr_func_name == NULL)
5862 {
5863 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
5864 + funcref->fr_dfunc_idx;
5865 name = df->df_ufunc->uf_name;
5866 }
5867 else
5868 name = funcref->fr_func_name;
5869 smsg("%s%4d FUNCREF %s", pfx, current, name);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005870 }
5871 break;
5872
5873 case ISN_NEWFUNC:
5874 {
5875 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
5876
5877 smsg("%s%4d NEWFUNC %s %s", pfx, current,
5878 newfunc->nf_lambda, newfunc->nf_global);
5879 }
5880 break;
5881
5882 case ISN_DEF:
5883 {
5884 char_u *name = iptr->isn_arg.string;
5885
5886 smsg("%s%4d DEF %s", pfx, current,
5887 name == NULL ? (char_u *)"" : name);
5888 }
5889 break;
5890
5891 case ISN_JUMP:
5892 {
5893 char *when = "?";
5894
5895 switch (iptr->isn_arg.jump.jump_when)
5896 {
5897 case JUMP_ALWAYS:
5898 when = "JUMP";
5899 break;
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005900 case JUMP_NEVER:
5901 iemsg("JUMP_NEVER should not be used");
5902 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005903 case JUMP_AND_KEEP_IF_TRUE:
5904 when = "JUMP_AND_KEEP_IF_TRUE";
5905 break;
5906 case JUMP_IF_FALSE:
5907 when = "JUMP_IF_FALSE";
5908 break;
5909 case JUMP_AND_KEEP_IF_FALSE:
5910 when = "JUMP_AND_KEEP_IF_FALSE";
5911 break;
5912 case JUMP_IF_COND_FALSE:
5913 when = "JUMP_IF_COND_FALSE";
5914 break;
5915 case JUMP_IF_COND_TRUE:
5916 when = "JUMP_IF_COND_TRUE";
5917 break;
5918 }
5919 smsg("%s%4d %s -> %d", pfx, current, when,
5920 iptr->isn_arg.jump.jump_where);
5921 }
5922 break;
5923
5924 case ISN_JUMP_IF_ARG_SET:
5925 smsg("%s%4d JUMP_IF_ARG_SET arg[%d] -> %d", pfx, current,
5926 iptr->isn_arg.jumparg.jump_arg_off + STACK_FRAME_SIZE,
5927 iptr->isn_arg.jump.jump_where);
5928 break;
5929
5930 case ISN_FOR:
5931 {
5932 forloop_T *forloop = &iptr->isn_arg.forloop;
5933
5934 smsg("%s%4d FOR $%d -> %d", pfx, current,
5935 forloop->for_idx, forloop->for_end);
5936 }
5937 break;
5938
5939 case ISN_TRY:
5940 {
Bram Moolenaar0d807102021-12-21 09:42:09 +00005941 try_T *try = &iptr->isn_arg.tryref;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005942
5943 if (try->try_ref->try_finally == 0)
5944 smsg("%s%4d TRY catch -> %d, endtry -> %d",
5945 pfx, current,
5946 try->try_ref->try_catch,
5947 try->try_ref->try_endtry);
5948 else
5949 smsg("%s%4d TRY catch -> %d, finally -> %d, endtry -> %d",
5950 pfx, current,
5951 try->try_ref->try_catch,
5952 try->try_ref->try_finally,
5953 try->try_ref->try_endtry);
5954 }
5955 break;
5956 case ISN_CATCH:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005957 smsg("%s%4d CATCH", pfx, current);
5958 break;
5959 case ISN_TRYCONT:
5960 {
5961 trycont_T *trycont = &iptr->isn_arg.trycont;
5962
5963 smsg("%s%4d TRY-CONTINUE %d level%s -> %d", pfx, current,
5964 trycont->tct_levels,
5965 trycont->tct_levels == 1 ? "" : "s",
5966 trycont->tct_where);
5967 }
5968 break;
5969 case ISN_FINALLY:
5970 smsg("%s%4d FINALLY", pfx, current);
5971 break;
5972 case ISN_ENDTRY:
5973 smsg("%s%4d ENDTRY", pfx, current);
5974 break;
5975 case ISN_THROW:
5976 smsg("%s%4d THROW", pfx, current);
5977 break;
5978
5979 // expression operations on number
5980 case ISN_OPNR:
5981 case ISN_OPFLOAT:
5982 case ISN_OPANY:
5983 {
5984 char *what;
5985 char *ins;
5986
5987 switch (iptr->isn_arg.op.op_type)
5988 {
5989 case EXPR_MULT: what = "*"; break;
5990 case EXPR_DIV: what = "/"; break;
5991 case EXPR_REM: what = "%"; break;
5992 case EXPR_SUB: what = "-"; break;
5993 case EXPR_ADD: what = "+"; break;
5994 default: what = "???"; break;
5995 }
5996 switch (iptr->isn_type)
5997 {
5998 case ISN_OPNR: ins = "OPNR"; break;
5999 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
6000 case ISN_OPANY: ins = "OPANY"; break;
6001 default: ins = "???"; break;
6002 }
6003 smsg("%s%4d %s %s", pfx, current, ins, what);
6004 }
6005 break;
6006
6007 case ISN_COMPAREBOOL:
6008 case ISN_COMPARESPECIAL:
Bram Moolenaar7a222242022-03-01 19:23:24 +00006009 case ISN_COMPARENULL:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006010 case ISN_COMPARENR:
6011 case ISN_COMPAREFLOAT:
6012 case ISN_COMPARESTRING:
6013 case ISN_COMPAREBLOB:
6014 case ISN_COMPARELIST:
6015 case ISN_COMPAREDICT:
6016 case ISN_COMPAREFUNC:
6017 case ISN_COMPAREANY:
6018 {
6019 char *p;
6020 char buf[10];
6021 char *type;
6022
6023 switch (iptr->isn_arg.op.op_type)
6024 {
6025 case EXPR_EQUAL: p = "=="; break;
6026 case EXPR_NEQUAL: p = "!="; break;
6027 case EXPR_GREATER: p = ">"; break;
6028 case EXPR_GEQUAL: p = ">="; break;
6029 case EXPR_SMALLER: p = "<"; break;
6030 case EXPR_SEQUAL: p = "<="; break;
6031 case EXPR_MATCH: p = "=~"; break;
6032 case EXPR_IS: p = "is"; break;
6033 case EXPR_ISNOT: p = "isnot"; break;
6034 case EXPR_NOMATCH: p = "!~"; break;
6035 default: p = "???"; break;
6036 }
6037 STRCPY(buf, p);
6038 if (iptr->isn_arg.op.op_ic == TRUE)
6039 strcat(buf, "?");
6040 switch(iptr->isn_type)
6041 {
6042 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
6043 case ISN_COMPARESPECIAL:
6044 type = "COMPARESPECIAL"; break;
Bram Moolenaar7a222242022-03-01 19:23:24 +00006045 case ISN_COMPARENULL: type = "COMPARENULL"; break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006046 case ISN_COMPARENR: type = "COMPARENR"; break;
6047 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
6048 case ISN_COMPARESTRING:
6049 type = "COMPARESTRING"; break;
6050 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
6051 case ISN_COMPARELIST: type = "COMPARELIST"; break;
6052 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
6053 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
6054 case ISN_COMPAREANY: type = "COMPAREANY"; break;
6055 default: type = "???"; break;
6056 }
6057
6058 smsg("%s%4d %s %s", pfx, current, type, buf);
6059 }
6060 break;
6061
6062 case ISN_ADDLIST: smsg("%s%4d ADDLIST", pfx, current); break;
6063 case ISN_ADDBLOB: smsg("%s%4d ADDBLOB", pfx, current); break;
6064
6065 // expression operations
6066 case ISN_CONCAT: smsg("%s%4d CONCAT", pfx, current); break;
6067 case ISN_STRINDEX: smsg("%s%4d STRINDEX", pfx, current); break;
6068 case ISN_STRSLICE: smsg("%s%4d STRSLICE", pfx, current); break;
6069 case ISN_BLOBINDEX: smsg("%s%4d BLOBINDEX", pfx, current); break;
6070 case ISN_BLOBSLICE: smsg("%s%4d BLOBSLICE", pfx, current); break;
6071 case ISN_LISTAPPEND: smsg("%s%4d LISTAPPEND", pfx, current); break;
6072 case ISN_BLOBAPPEND: smsg("%s%4d BLOBAPPEND", pfx, current); break;
6073 case ISN_LISTINDEX: smsg("%s%4d LISTINDEX", pfx, current); break;
6074 case ISN_LISTSLICE: smsg("%s%4d LISTSLICE", pfx, current); break;
6075 case ISN_ANYINDEX: smsg("%s%4d ANYINDEX", pfx, current); break;
6076 case ISN_ANYSLICE: smsg("%s%4d ANYSLICE", pfx, current); break;
6077 case ISN_SLICE: smsg("%s%4d SLICE %lld",
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02006078 pfx, current, iptr->isn_arg.number); break;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02006079 case ISN_GETITEM: smsg("%s%4d ITEM %lld%s", pfx, current,
6080 iptr->isn_arg.getitem.gi_index,
6081 iptr->isn_arg.getitem.gi_with_op ?
6082 " with op" : ""); break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006083 case ISN_MEMBER: smsg("%s%4d MEMBER", pfx, current); break;
6084 case ISN_STRINGMEMBER: smsg("%s%4d MEMBER %s", pfx, current,
6085 iptr->isn_arg.string); break;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02006086 case ISN_CLEARDICT: smsg("%s%4d CLEARDICT", pfx, current); break;
6087 case ISN_USEDICT: smsg("%s%4d USEDICT", pfx, current); break;
6088
Bram Moolenaar4c137212021-04-19 16:48:48 +02006089 case ISN_NEGATENR: smsg("%s%4d NEGATENR", pfx, current); break;
6090
6091 case ISN_CHECKNR: smsg("%s%4d CHECKNR", pfx, current); break;
6092 case ISN_CHECKTYPE:
6093 {
6094 checktype_T *ct = &iptr->isn_arg.type;
6095 char *tofree;
6096
6097 if (ct->ct_arg_idx == 0)
6098 smsg("%s%4d CHECKTYPE %s stack[%d]", pfx, current,
6099 type_name(ct->ct_type, &tofree),
6100 (int)ct->ct_off);
6101 else
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02006102 smsg("%s%4d CHECKTYPE %s stack[%d] arg %d",
6103 pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02006104 type_name(ct->ct_type, &tofree),
6105 (int)ct->ct_off,
6106 (int)ct->ct_arg_idx);
6107 vim_free(tofree);
6108 break;
6109 }
6110 case ISN_CHECKLEN: smsg("%s%4d CHECKLEN %s%d", pfx, current,
6111 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
6112 iptr->isn_arg.checklen.cl_min_len);
6113 break;
6114 case ISN_SETTYPE:
6115 {
6116 char *tofree;
6117
6118 smsg("%s%4d SETTYPE %s", pfx, current,
6119 type_name(iptr->isn_arg.type.ct_type, &tofree));
6120 vim_free(tofree);
6121 break;
6122 }
6123 case ISN_COND2BOOL: smsg("%s%4d COND2BOOL", pfx, current); break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006124 case ISN_2BOOL: if (iptr->isn_arg.tobool.invert)
6125 smsg("%s%4d INVERT %d (!val)", pfx, current,
6126 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006127 else
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006128 smsg("%s%4d 2BOOL %d (!!val)", pfx, current,
6129 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006130 break;
6131 case ISN_2STRING: smsg("%s%4d 2STRING stack[%lld]", pfx, current,
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006132 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006133 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006134 case ISN_2STRING_ANY: smsg("%s%4d 2STRING_ANY stack[%lld]",
6135 pfx, current,
6136 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006137 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006138 case ISN_RANGE: smsg("%s%4d RANGE %s", pfx, current,
6139 iptr->isn_arg.string);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006140 break;
6141 case ISN_PUT:
6142 if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE)
6143 smsg("%s%4d PUT %c above range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006144 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006145 else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE)
6146 smsg("%s%4d PUT %c range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006147 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006148 else
6149 smsg("%s%4d PUT %c %ld", pfx, current,
6150 iptr->isn_arg.put.put_regname,
6151 (long)iptr->isn_arg.put.put_lnum);
6152 break;
6153
Bram Moolenaar4c137212021-04-19 16:48:48 +02006154 case ISN_CMDMOD:
6155 {
6156 char_u *buf;
6157 size_t len = produce_cmdmods(
6158 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
6159
6160 buf = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02006161 if (likely(buf != NULL))
Bram Moolenaar4c137212021-04-19 16:48:48 +02006162 {
6163 (void)produce_cmdmods(
6164 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
6165 smsg("%s%4d CMDMOD %s", pfx, current, buf);
6166 vim_free(buf);
6167 }
6168 break;
6169 }
6170 case ISN_CMDMOD_REV: smsg("%s%4d CMDMOD_REV", pfx, current); break;
6171
6172 case ISN_PROF_START:
Bram Moolenaare99d4222021-06-13 14:01:26 +02006173 smsg("%s%4d PROFILE START line %d", pfx, current,
6174 iptr->isn_lnum);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006175 break;
6176
6177 case ISN_PROF_END:
6178 smsg("%s%4d PROFILE END", pfx, current);
6179 break;
6180
Bram Moolenaare99d4222021-06-13 14:01:26 +02006181 case ISN_DEBUG:
Bram Moolenaar8cec9272021-06-23 20:20:53 +02006182 smsg("%s%4d DEBUG line %d-%d varcount %lld", pfx, current,
6183 iptr->isn_arg.debug.dbg_break_lnum + 1,
6184 iptr->isn_lnum,
6185 iptr->isn_arg.debug.dbg_var_names_len);
Bram Moolenaare99d4222021-06-13 14:01:26 +02006186 break;
6187
Bram Moolenaar4c137212021-04-19 16:48:48 +02006188 case ISN_UNPACK: smsg("%s%4d UNPACK %d%s", pfx, current,
6189 iptr->isn_arg.unpack.unp_count,
6190 iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : "");
6191 break;
6192 case ISN_SHUFFLE: smsg("%s%4d SHUFFLE %d up %d", pfx, current,
6193 iptr->isn_arg.shuffle.shfl_item,
6194 iptr->isn_arg.shuffle.shfl_up);
6195 break;
6196 case ISN_DROP: smsg("%s%4d DROP", pfx, current); break;
6197
6198 case ISN_FINISH: // End of list of instructions for ISN_SUBSTITUTE.
6199 return;
6200 }
6201
6202 out_flush(); // output one line at a time
6203 ui_breakcheck();
6204 if (got_int)
6205 break;
6206 }
6207}
6208
6209/*
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02006210 * Handle command line completion for the :disassemble command.
6211 */
6212 void
6213set_context_in_disassemble_cmd(expand_T *xp, char_u *arg)
6214{
6215 char_u *p;
6216
6217 // Default: expand user functions, "debug" and "profile"
6218 xp->xp_context = EXPAND_DISASSEMBLE;
6219 xp->xp_pattern = arg;
6220
6221 // first argument already typed: only user function names
6222 if (*arg != NUL && *(p = skiptowhite(arg)) != NUL)
6223 {
6224 xp->xp_context = EXPAND_USER_FUNC;
6225 xp->xp_pattern = skipwhite(p);
6226 }
6227}
6228
6229/*
6230 * Function given to ExpandGeneric() to obtain the list of :disassemble
6231 * arguments.
6232 */
6233 char_u *
6234get_disassemble_argument(expand_T *xp, int idx)
6235{
6236 if (idx == 0)
6237 return (char_u *)"debug";
6238 if (idx == 1)
6239 return (char_u *)"profile";
6240 return get_user_func_name(xp, idx - 2);
6241}
6242
6243/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01006244 * ":disassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01006245 * We don't really need this at runtime, but we do have tests that require it,
6246 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006247 */
6248 void
6249ex_disassemble(exarg_T *eap)
6250{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01006251 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01006252 char_u *fname;
6253 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006254 dfunc_T *dfunc;
6255 isn_T *instr;
Bram Moolenaarb2049902021-01-24 12:53:53 +01006256 int instr_count;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02006257 int is_global = FALSE;
Bram Moolenaare99d4222021-06-13 14:01:26 +02006258 compiletype_T compile_type = CT_NONE;
6259
Bram Moolenaarc7d5fc82021-12-05 17:20:24 +00006260 if (STRNCMP(arg, "profile", 7) == 0 && VIM_ISWHITE(arg[7]))
Bram Moolenaare99d4222021-06-13 14:01:26 +02006261 {
6262 compile_type = CT_PROFILE;
6263 arg = skipwhite(arg + 7);
6264 }
Bram Moolenaarc7d5fc82021-12-05 17:20:24 +00006265 else if (STRNCMP(arg, "debug", 5) == 0 && VIM_ISWHITE(arg[5]))
Bram Moolenaare99d4222021-06-13 14:01:26 +02006266 {
6267 compile_type = CT_DEBUG;
6268 arg = skipwhite(arg + 5);
6269 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006270
Bram Moolenaarbfd65582020-07-13 18:18:00 +02006271 if (STRNCMP(arg, "<lambda>", 8) == 0)
6272 {
6273 arg += 8;
6274 (void)getdigits(&arg);
6275 fname = vim_strnsave(eap->arg, arg - eap->arg);
6276 }
6277 else
6278 fname = trans_function_name(&arg, &is_global, FALSE,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01006279 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL, NULL);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01006280 if (fname == NULL)
6281 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006282 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01006283 return;
6284 }
6285
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00006286 ufunc = find_func(fname, is_global);
Bram Moolenaara26b9702020-04-18 19:53:28 +02006287 if (ufunc == NULL)
6288 {
6289 char_u *p = untrans_function_name(fname);
6290
6291 if (p != NULL)
6292 // Try again without making it script-local.
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00006293 ufunc = find_func(p, FALSE);
Bram Moolenaara26b9702020-04-18 19:53:28 +02006294 }
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01006295 vim_free(fname);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006296 if (ufunc == NULL)
6297 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006298 semsg(_(e_cannot_find_function_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006299 return;
6300 }
Bram Moolenaare99d4222021-06-13 14:01:26 +02006301 if (func_needs_compiling(ufunc, compile_type)
6302 && compile_def_function(ufunc, FALSE, compile_type, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02006303 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006304 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006305 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006306 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006307 return;
6308 }
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006309 msg((char *)printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006310
6311 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
Bram Moolenaare99d4222021-06-13 14:01:26 +02006312 switch (compile_type)
6313 {
6314 case CT_PROFILE:
Bram Moolenaarf002a412021-01-24 13:34:18 +01006315#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02006316 instr = dfunc->df_instr_prof;
6317 instr_count = dfunc->df_instr_prof_count;
6318 break;
Bram Moolenaarf002a412021-01-24 13:34:18 +01006319#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02006320 // FALLTHROUGH
6321 case CT_NONE:
6322 instr = dfunc->df_instr;
6323 instr_count = dfunc->df_instr_count;
6324 break;
6325 case CT_DEBUG:
6326 instr = dfunc->df_instr_debug;
6327 instr_count = dfunc->df_instr_debug_count;
6328 break;
6329 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006330
Bram Moolenaar4c137212021-04-19 16:48:48 +02006331 list_instructions("", instr, instr_count, ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006332}
6333
6334/*
Bram Moolenaar13106602020-10-04 16:06:05 +02006335 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006336 * list, etc. Mostly like what JavaScript does, except that empty list and
6337 * empty dictionary are FALSE.
6338 */
6339 int
6340tv2bool(typval_T *tv)
6341{
6342 switch (tv->v_type)
6343 {
6344 case VAR_NUMBER:
6345 return tv->vval.v_number != 0;
6346 case VAR_FLOAT:
6347#ifdef FEAT_FLOAT
6348 return tv->vval.v_float != 0.0;
6349#else
6350 break;
6351#endif
6352 case VAR_PARTIAL:
6353 return tv->vval.v_partial != NULL;
6354 case VAR_FUNC:
6355 case VAR_STRING:
6356 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
6357 case VAR_LIST:
6358 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
6359 case VAR_DICT:
6360 return tv->vval.v_dict != NULL
6361 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
6362 case VAR_BOOL:
6363 case VAR_SPECIAL:
6364 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
6365 case VAR_JOB:
6366#ifdef FEAT_JOB_CHANNEL
6367 return tv->vval.v_job != NULL;
6368#else
6369 break;
6370#endif
6371 case VAR_CHANNEL:
6372#ifdef FEAT_JOB_CHANNEL
6373 return tv->vval.v_channel != NULL;
6374#else
6375 break;
6376#endif
6377 case VAR_BLOB:
6378 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
6379 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006380 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006381 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006382 case VAR_INSTR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006383 break;
6384 }
6385 return FALSE;
6386}
6387
Bram Moolenaarea2d4072020-11-12 12:08:51 +01006388 void
6389emsg_using_string_as(typval_T *tv, int as_number)
6390{
6391 semsg(_(as_number ? e_using_string_as_number_str
6392 : e_using_string_as_bool_str),
6393 tv->vval.v_string == NULL
6394 ? (char_u *)"" : tv->vval.v_string);
6395}
6396
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006397/*
6398 * If "tv" is a string give an error and return FAIL.
6399 */
6400 int
6401check_not_string(typval_T *tv)
6402{
6403 if (tv->v_type == VAR_STRING)
6404 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +01006405 emsg_using_string_as(tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006406 clear_tv(tv);
6407 return FAIL;
6408 }
6409 return OK;
6410}
6411
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006412
6413#endif // FEAT_EVAL