blob: 19f5bdc7628fe2051cd348a1fe2af1e6721db176 [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);
3101 goto on_error;
3102 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003103 store_var(name, STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003104 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003105 else
3106 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003107 if (iptr->isn_type == ISN_STOREEXPORT)
3108 {
3109 int idx = get_script_item_idx(sid, name, 0,
3110 NULL, NULL);
3111
3112 if (idx >= 0)
3113 {
3114 svar_T *sv = ((svar_T *)SCRIPT_ITEM(sid)
3115 ->sn_var_vals.ga_data) + idx;
3116
3117 if (!sv->sv_export)
3118 {
3119 semsg(_(e_item_not_exported_in_script_str),
3120 name);
3121 goto on_error;
3122 }
3123 }
3124 }
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02003125 if (var_check_permission(di, name) == FAIL)
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003126 {
3127 clear_tv(STACK_TV_BOT(0));
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02003128 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003129 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003130 clear_tv(&di->di_tv);
3131 di->di_tv = *STACK_TV_BOT(0);
3132 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003133 }
3134 break;
3135
3136 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003137 case ISN_STORESCRIPT:
3138 {
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003139 scriptref_T *sref = iptr->isn_arg.script.scriptref;
3140 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003141
Bram Moolenaar6db660b2021-08-01 14:08:54 +02003142 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003143 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003144 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003145 --ectx->ec_stack.ga_len;
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003146
3147 // "const" and "final" are checked at compile time, locking
3148 // the value needs to be checked here.
3149 SOURCING_LNUM = iptr->isn_lnum;
3150 if (value_check_lock(sv->sv_tv->v_lock, sv->sv_name, FALSE))
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003151 {
3152 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003153 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003154 }
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003155
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003156 clear_tv(sv->sv_tv);
3157 *sv->sv_tv = *STACK_TV_BOT(0);
3158 }
3159 break;
3160
3161 // store option
3162 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003163 case ISN_STOREFUNCOPT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003164 {
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003165 char_u *opt_name = iptr->isn_arg.storeopt.so_name;
3166 int opt_flags = iptr->isn_arg.storeopt.so_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003167 long n = 0;
3168 char_u *s = NULL;
3169 char *msg;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003170 char_u numbuf[NUMBUFLEN];
3171 char_u *tofree = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003172
Bram Moolenaar4c137212021-04-19 16:48:48 +02003173 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003174 tv = STACK_TV_BOT(0);
3175 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01003176 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003177 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01003178 if (s == NULL)
3179 s = (char_u *)"";
3180 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003181 else if (iptr->isn_type == ISN_STOREFUNCOPT)
3182 {
3183 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003184 // If the option can be set to a function reference or
3185 // a lambda and the passed value is a function
3186 // reference, then convert it to the name (string) of
3187 // the function reference.
3188 s = tv2string(tv, &tofree, numbuf, 0);
3189 if (s == NULL || *s == NUL)
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003190 {
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003191 // cannot happen?
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003192 clear_tv(tv);
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003193 vim_free(tofree);
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003194 goto on_error;
3195 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003196 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003197 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003198 // must be VAR_NUMBER, CHECKTYPE makes sure
3199 n = tv->vval.v_number;
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003200 msg = set_option_value(opt_name, n, s, opt_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02003201 clear_tv(tv);
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003202 vim_free(tofree);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003203 if (msg != NULL)
3204 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003205 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003206 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02003207 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003208 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003209 }
3210 break;
3211
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003212 // store $ENV
3213 case ISN_STOREENV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003214 --ectx->ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003215 tv = STACK_TV_BOT(0);
3216 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
3217 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003218 break;
3219
3220 // store @r
3221 case ISN_STOREREG:
3222 {
3223 int reg = iptr->isn_arg.number;
3224
Bram Moolenaar4c137212021-04-19 16:48:48 +02003225 --ectx->ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01003226 tv = STACK_TV_BOT(0);
Bram Moolenaar74f4a962021-06-17 21:03:07 +02003227 write_reg_contents(reg, tv_get_string(tv), -1, FALSE);
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01003228 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003229 }
3230 break;
3231
3232 // store v: variable
3233 case ISN_STOREV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003234 --ectx->ec_stack.ga_len;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003235 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
3236 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003237 // should not happen, type is checked when compiling
3238 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003239 break;
3240
Bram Moolenaard3aac292020-04-19 14:32:17 +02003241 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003242 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02003243 case ISN_STOREB:
3244 case ISN_STOREW:
3245 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003246 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003247 dictitem_T *di;
3248 hashtab_T *ht;
3249 char_u *name = iptr->isn_arg.string + 2;
3250
Bram Moolenaard3aac292020-04-19 14:32:17 +02003251 switch (iptr->isn_type)
3252 {
3253 case ISN_STOREG:
3254 ht = get_globvar_ht();
3255 break;
3256 case ISN_STOREB:
3257 ht = &curbuf->b_vars->dv_hashtab;
3258 break;
3259 case ISN_STOREW:
3260 ht = &curwin->w_vars->dv_hashtab;
3261 break;
3262 case ISN_STORET:
3263 ht = &curtab->tp_vars->dv_hashtab;
3264 break;
3265 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003266 goto theend;
Bram Moolenaard3aac292020-04-19 14:32:17 +02003267 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003268
Bram Moolenaar4c137212021-04-19 16:48:48 +02003269 --ectx->ec_stack.ga_len;
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003270 di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003271 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003272 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003273 else
3274 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003275 SOURCING_LNUM = iptr->isn_lnum;
3276 if (var_check_permission(di, name) == FAIL)
3277 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003278 clear_tv(&di->di_tv);
3279 di->di_tv = *STACK_TV_BOT(0);
3280 }
3281 }
3282 break;
3283
Bram Moolenaar03290b82020-12-19 16:30:44 +01003284 // store an autoload variable
3285 case ISN_STOREAUTO:
3286 SOURCING_LNUM = iptr->isn_lnum;
3287 set_var(iptr->isn_arg.string, STACK_TV_BOT(-1), TRUE);
3288 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003289 --ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003290 break;
3291
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003292 // store number in local variable
3293 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01003294 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003295 clear_tv(tv);
3296 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01003297 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003298 break;
3299
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01003300 // store value in list or dict variable
3301 case ISN_STOREINDEX:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003302 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003303 int res = execute_storeindex(iptr, ectx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003304
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003305 if (res == FAIL)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02003306 goto on_error;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003307 if (res == NOTDONE)
3308 goto theend;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003309 }
3310 break;
3311
Bram Moolenaarea5c8982022-02-17 14:42:02 +00003312 // store value in list or blob range
Bram Moolenaar68452172021-04-12 21:21:02 +02003313 case ISN_STORERANGE:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003314 if (execute_storerange(iptr, ectx) == FAIL)
3315 goto on_error;
Bram Moolenaar68452172021-04-12 21:21:02 +02003316 break;
3317
Bram Moolenaar0186e582021-01-10 18:33:11 +01003318 // load or store variable or argument from outer scope
3319 case ISN_LOADOUTER:
3320 case ISN_STOREOUTER:
3321 {
3322 int depth = iptr->isn_arg.outer.outer_depth;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02003323 outer_T *outer = ectx->ec_outer_ref == NULL ? NULL
3324 : ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003325
3326 while (depth > 1 && outer != NULL)
3327 {
3328 outer = outer->out_up;
3329 --depth;
3330 }
3331 if (outer == NULL)
3332 {
3333 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar69c76172021-12-02 16:38:52 +00003334 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx
3335 || ectx->ec_outer_ref == NULL)
3336 // Possibly :def function called from legacy
3337 // context.
3338 emsg(_(e_closure_called_from_invalid_context));
3339 else
3340 iemsg("LOADOUTER depth more than scope levels");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003341 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003342 }
3343 tv = ((typval_T *)outer->out_stack->ga_data)
3344 + outer->out_frame_idx + STACK_FRAME_SIZE
3345 + iptr->isn_arg.outer.outer_idx;
3346 if (iptr->isn_type == ISN_LOADOUTER)
3347 {
Bram Moolenaar35578162021-08-02 19:10:38 +02003348 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003349 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003350 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003351 ++ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003352 }
3353 else
3354 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003355 --ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003356 clear_tv(tv);
3357 *tv = *STACK_TV_BOT(0);
3358 }
3359 }
3360 break;
3361
Bram Moolenaar752fc692021-01-04 21:57:11 +01003362 // unlet item in list or dict variable
3363 case ISN_UNLETINDEX:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003364 if (execute_unletindex(iptr, ectx) == FAIL)
3365 goto on_error;
Bram Moolenaar752fc692021-01-04 21:57:11 +01003366 break;
3367
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01003368 // unlet range of items in list variable
3369 case ISN_UNLETRANGE:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003370 if (execute_unletrange(iptr, ectx) == FAIL)
3371 goto on_error;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01003372 break;
3373
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003374 // push constant
3375 case ISN_PUSHNR:
3376 case ISN_PUSHBOOL:
3377 case ISN_PUSHSPEC:
3378 case ISN_PUSHF:
3379 case ISN_PUSHS:
3380 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003381 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003382 case ISN_PUSHCHANNEL:
3383 case ISN_PUSHJOB:
Bram Moolenaar35578162021-08-02 19:10:38 +02003384 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003385 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003386 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003387 tv->v_lock = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003388 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003389 switch (iptr->isn_type)
3390 {
3391 case ISN_PUSHNR:
3392 tv->v_type = VAR_NUMBER;
3393 tv->vval.v_number = iptr->isn_arg.number;
3394 break;
3395 case ISN_PUSHBOOL:
3396 tv->v_type = VAR_BOOL;
3397 tv->vval.v_number = iptr->isn_arg.number;
3398 break;
3399 case ISN_PUSHSPEC:
3400 tv->v_type = VAR_SPECIAL;
3401 tv->vval.v_number = iptr->isn_arg.number;
3402 break;
3403#ifdef FEAT_FLOAT
3404 case ISN_PUSHF:
3405 tv->v_type = VAR_FLOAT;
3406 tv->vval.v_float = iptr->isn_arg.fnumber;
3407 break;
3408#endif
3409 case ISN_PUSHBLOB:
3410 blob_copy(iptr->isn_arg.blob, tv);
3411 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003412 case ISN_PUSHFUNC:
3413 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01003414 if (iptr->isn_arg.string == NULL)
3415 tv->vval.v_string = NULL;
3416 else
3417 tv->vval.v_string =
3418 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003419 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003420 case ISN_PUSHCHANNEL:
3421#ifdef FEAT_JOB_CHANNEL
3422 tv->v_type = VAR_CHANNEL;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003423 tv->vval.v_channel = NULL;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003424#endif
3425 break;
3426 case ISN_PUSHJOB:
3427#ifdef FEAT_JOB_CHANNEL
3428 tv->v_type = VAR_JOB;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003429 tv->vval.v_job = NULL;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003430#endif
3431 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003432 default:
3433 tv->v_type = VAR_STRING;
Bram Moolenaarf8691002022-03-10 12:20:53 +00003434 tv->vval.v_string = iptr->isn_arg.string == NULL
3435 ? NULL : vim_strsave(iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003436 }
3437 break;
3438
Bram Moolenaar06b77222022-01-25 15:51:56 +00003439 case ISN_AUTOLOAD:
3440 {
3441 char_u *name = iptr->isn_arg.string;
3442
3443 (void)script_autoload(name, FALSE);
3444 if (find_func(name, TRUE))
3445 {
3446 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
3447 goto theend;
3448 tv = STACK_TV_BOT(0);
3449 tv->v_lock = 0;
3450 ++ectx->ec_stack.ga_len;
3451 tv->v_type = VAR_FUNC;
3452 tv->vval.v_string = vim_strsave(name);
3453 }
3454 else
3455 {
3456 int res = load_namespace_var(ectx, ISN_LOADG, iptr);
3457
3458 if (res == NOTDONE)
3459 goto theend;
3460 if (res == FAIL)
3461 goto on_error;
3462 }
3463 }
3464 break;
3465
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003466 case ISN_UNLET:
3467 if (do_unlet(iptr->isn_arg.unlet.ul_name,
3468 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003469 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003470 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02003471 case ISN_UNLETENV:
3472 vim_unsetenv(iptr->isn_arg.unlet.ul_name);
3473 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003474
Bram Moolenaaraacc9662021-08-13 19:40:51 +02003475 case ISN_LOCKUNLOCK:
3476 {
3477 typval_T *lval_root_save = lval_root;
3478 int res;
3479
3480 // Stack has the local variable, argument the whole :lock
3481 // or :unlock command, like ISN_EXEC.
3482 --ectx->ec_stack.ga_len;
3483 lval_root = STACK_TV_BOT(0);
3484 res = exec_command(iptr);
3485 clear_tv(lval_root);
3486 lval_root = lval_root_save;
3487 if (res == FAIL)
3488 goto on_error;
3489 }
3490 break;
3491
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02003492 case ISN_LOCKCONST:
3493 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
3494 break;
3495
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003496 // create a list from items on the stack; uses a single allocation
3497 // for the list header and the items
3498 case ISN_NEWLIST:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003499 if (exe_newlist(iptr->isn_arg.number, ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003500 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003501 break;
3502
3503 // create a dict from items on the stack
3504 case ISN_NEWDICT:
3505 {
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01003506 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003507
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01003508 SOURCING_LNUM = iptr->isn_lnum;
3509 res = exe_newdict(iptr->isn_arg.number, ectx);
3510 if (res == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003511 goto theend;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01003512 if (res == MAYBE)
3513 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003514 }
3515 break;
3516
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003517 // create a partial with NULL value
3518 case ISN_NEWPARTIAL:
3519 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
3520 goto theend;
3521 ++ectx->ec_stack.ga_len;
3522 tv = STACK_TV_BOT(-1);
3523 tv->v_type = VAR_PARTIAL;
3524 tv->v_lock = 0;
3525 tv->vval.v_partial = NULL;
3526 break;
3527
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003528 // call a :def function
3529 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02003530 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003531 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
3532 NULL,
3533 iptr->isn_arg.dfunc.cdf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003534 ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003535 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003536 break;
3537
3538 // call a builtin function
3539 case ISN_BCALL:
3540 SOURCING_LNUM = iptr->isn_lnum;
3541 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
3542 iptr->isn_arg.bfunc.cbf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003543 ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02003544 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003545 break;
3546
3547 // call a funcref or partial
3548 case ISN_PCALL:
3549 {
3550 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
3551 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003552 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003553
3554 SOURCING_LNUM = iptr->isn_lnum;
3555 if (pfunc->cpf_top)
3556 {
3557 // funcref is above the arguments
3558 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
3559 }
3560 else
3561 {
3562 // Get the funcref from the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02003563 --ectx->ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003564 partial_tv = *STACK_TV_BOT(0);
3565 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003566 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003567 r = call_partial(tv, pfunc->cpf_argcount, ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003568 if (tv == &partial_tv)
3569 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003570 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02003571 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003572 }
3573 break;
3574
Bram Moolenaarbd5da372020-03-31 23:13:10 +02003575 case ISN_PCALL_END:
3576 // PCALL finished, arguments have been consumed and replaced by
3577 // the return value. Now clear the funcref from the stack,
3578 // and move the return value in its place.
Bram Moolenaar4c137212021-04-19 16:48:48 +02003579 --ectx->ec_stack.ga_len;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02003580 clear_tv(STACK_TV_BOT(-1));
3581 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
3582 break;
3583
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003584 // call a user defined function or funcref/partial
3585 case ISN_UCALL:
3586 {
3587 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
3588
3589 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003590 if (call_eval_func(cufunc->cuf_name, cufunc->cuf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003591 ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02003592 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003593 }
3594 break;
3595
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02003596 // return from a :def function call without a value
3597 case ISN_RETURN_VOID:
Bram Moolenaar35578162021-08-02 19:10:38 +02003598 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003599 goto theend;
Bram Moolenaar299f3032021-01-08 20:53:09 +01003600 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003601 ++ectx->ec_stack.ga_len;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02003602 tv->v_type = VAR_VOID;
Bram Moolenaar299f3032021-01-08 20:53:09 +01003603 tv->vval.v_number = 0;
3604 tv->v_lock = 0;
3605 // FALLTHROUGH
3606
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02003607 // return from a :def function call with what is on the stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003608 case ISN_RETURN:
3609 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003610 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003611 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01003612
3613 if (trystack->ga_len > 0)
3614 trycmd = ((trycmd_T *)trystack->ga_data)
3615 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003616 if (trycmd != NULL
Bram Moolenaar4c137212021-04-19 16:48:48 +02003617 && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003618 {
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01003619 // jump to ":finally" or ":endtry"
3620 if (trycmd->tcd_finally_idx != 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02003621 ectx->ec_iidx = trycmd->tcd_finally_idx;
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01003622 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02003623 ectx->ec_iidx = trycmd->tcd_endtry_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003624 trycmd->tcd_return = TRUE;
3625 }
3626 else
Bram Moolenaard032f342020-07-18 18:13:02 +02003627 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003628 }
3629 break;
3630
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02003631 // push a partial, a reference to a compiled function
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003632 case ISN_FUNCREF:
3633 {
Bram Moolenaarf112f302020-12-20 17:47:52 +01003634 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar38453522021-11-28 22:00:12 +00003635 ufunc_T *ufunc;
3636 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003637
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003638 if (pt == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003639 goto theend;
Bram Moolenaar35578162021-08-02 19:10:38 +02003640 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003641 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003642 vim_free(pt);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003643 goto theend;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003644 }
Bram Moolenaar38453522021-11-28 22:00:12 +00003645 if (funcref->fr_func_name == NULL)
3646 {
3647 dfunc_T *pt_dfunc = ((dfunc_T *)def_functions.ga_data)
3648 + funcref->fr_dfunc_idx;
3649
3650 ufunc = pt_dfunc->df_ufunc;
3651 }
3652 else
3653 {
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003654 ufunc = find_func(funcref->fr_func_name, FALSE);
Bram Moolenaar38453522021-11-28 22:00:12 +00003655 }
Bram Moolenaar56acd1f2022-02-18 13:24:52 +00003656 if (ufunc == NULL)
3657 {
3658 SOURCING_LNUM = iptr->isn_lnum;
3659 iemsg("ufunc unexpectedly NULL for FUNCREF");
3660 goto theend;
3661 }
Bram Moolenaar38453522021-11-28 22:00:12 +00003662 if (fill_partial_and_closure(pt, ufunc, ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003663 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003664 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003665 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003666 tv->vval.v_partial = pt;
3667 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003668 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003669 }
3670 break;
3671
Bram Moolenaar38ddf332020-07-31 22:05:04 +02003672 // Create a global function from a lambda.
3673 case ISN_NEWFUNC:
3674 {
3675 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
3676
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01003677 if (copy_func(newfunc->nf_lambda, newfunc->nf_global,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003678 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003679 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02003680 }
3681 break;
3682
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01003683 // List functions
3684 case ISN_DEF:
3685 if (iptr->isn_arg.string == NULL)
3686 list_functions(NULL);
3687 else
3688 {
Bram Moolenaar14336722022-01-08 16:02:59 +00003689 exarg_T ea;
3690 garray_T lines_to_free;
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01003691
3692 CLEAR_FIELD(ea);
3693 ea.cmd = ea.arg = iptr->isn_arg.string;
Bram Moolenaar14336722022-01-08 16:02:59 +00003694 ga_init2(&lines_to_free, sizeof(char_u *), 50);
3695 define_function(&ea, NULL, &lines_to_free);
3696 ga_clear_strings(&lines_to_free);
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01003697 }
3698 break;
3699
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003700 // jump if a condition is met
3701 case ISN_JUMP:
3702 {
3703 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003704 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003705 int jump = TRUE;
3706
3707 if (when != JUMP_ALWAYS)
3708 {
3709 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003710 if (when == JUMP_IF_COND_FALSE
Bram Moolenaar13106602020-10-04 16:06:05 +02003711 || when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003712 || when == JUMP_IF_COND_TRUE)
3713 {
3714 SOURCING_LNUM = iptr->isn_lnum;
3715 jump = tv_get_bool_chk(tv, &error);
3716 if (error)
3717 goto on_error;
3718 }
3719 else
3720 jump = tv2bool(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003721 if (when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003722 || when == JUMP_AND_KEEP_IF_FALSE
3723 || when == JUMP_IF_COND_FALSE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003724 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01003725 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003726 {
3727 // drop the value from the stack
3728 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003729 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003730 }
3731 }
3732 if (jump)
Bram Moolenaar4c137212021-04-19 16:48:48 +02003733 ectx->ec_iidx = iptr->isn_arg.jump.jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003734 }
3735 break;
3736
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02003737 // Jump if an argument with a default value was already set and not
3738 // v:none.
3739 case ISN_JUMP_IF_ARG_SET:
3740 tv = STACK_TV_VAR(iptr->isn_arg.jumparg.jump_arg_off);
3741 if (tv->v_type != VAR_UNKNOWN
3742 && !(tv->v_type == VAR_SPECIAL
3743 && tv->vval.v_number == VVAL_NONE))
Bram Moolenaar4c137212021-04-19 16:48:48 +02003744 ectx->ec_iidx = iptr->isn_arg.jumparg.jump_where;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02003745 break;
3746
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003747 // top of a for loop
3748 case ISN_FOR:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003749 if (execute_for(iptr, ectx) == FAIL)
3750 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003751 break;
3752
3753 // start of ":try" block
3754 case ISN_TRY:
3755 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01003756 trycmd_T *trycmd = NULL;
3757
Bram Moolenaar35578162021-08-02 19:10:38 +02003758 if (GA_GROW_FAILS(&ectx->ec_trystack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003759 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003760 trycmd = ((trycmd_T *)ectx->ec_trystack.ga_data)
3761 + ectx->ec_trystack.ga_len;
3762 ++ectx->ec_trystack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003763 ++trylevel;
Bram Moolenaar8d4be892021-02-13 18:33:02 +01003764 CLEAR_POINTER(trycmd);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003765 trycmd->tcd_frame_idx = ectx->ec_frame_idx;
3766 trycmd->tcd_stack_len = ectx->ec_stack.ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01003767 trycmd->tcd_catch_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00003768 iptr->isn_arg.tryref.try_ref->try_catch;
Bram Moolenaara91a7132021-03-25 21:12:15 +01003769 trycmd->tcd_finally_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00003770 iptr->isn_arg.tryref.try_ref->try_finally;
Bram Moolenaara91a7132021-03-25 21:12:15 +01003771 trycmd->tcd_endtry_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00003772 iptr->isn_arg.tryref.try_ref->try_endtry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003773 }
3774 break;
3775
3776 case ISN_PUSHEXC:
3777 if (current_exception == NULL)
3778 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003779 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003780 iemsg("Evaluating catch while current_exception is NULL");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003781 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003782 }
Bram Moolenaar35578162021-08-02 19:10:38 +02003783 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003784 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003785 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003786 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003787 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003788 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003789 tv->vval.v_string = vim_strsave(
3790 (char_u *)current_exception->value);
3791 break;
3792
3793 case ISN_CATCH:
3794 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003795 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003796
Bram Moolenaar4c137212021-04-19 16:48:48 +02003797 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003798 if (trystack->ga_len > 0)
3799 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01003800 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003801 + trystack->ga_len - 1;
3802 trycmd->tcd_caught = TRUE;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003803 trycmd->tcd_did_throw = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003804 }
3805 did_emsg = got_int = did_throw = FALSE;
Bram Moolenaar1430cee2021-01-17 19:20:32 +01003806 force_abort = need_rethrow = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003807 catch_exception(current_exception);
3808 }
3809 break;
3810
Bram Moolenaarc150c092021-02-13 15:02:46 +01003811 case ISN_TRYCONT:
3812 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003813 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003814 trycont_T *trycont = &iptr->isn_arg.trycont;
3815 int i;
3816 trycmd_T *trycmd;
3817 int iidx = trycont->tct_where;
3818
3819 if (trystack->ga_len < trycont->tct_levels)
3820 {
3821 siemsg("TRYCONT: expected %d levels, found %d",
3822 trycont->tct_levels, trystack->ga_len);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003823 goto theend;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003824 }
3825 // Make :endtry jump to any outer try block and the last
3826 // :endtry inside the loop to the loop start.
3827 for (i = trycont->tct_levels; i > 0; --i)
3828 {
3829 trycmd = ((trycmd_T *)trystack->ga_data)
3830 + trystack->ga_len - i;
Bram Moolenaar2e34c342021-03-14 12:13:33 +01003831 // Add one to tcd_cont to be able to jump to
3832 // instruction with index zero.
3833 trycmd->tcd_cont = iidx + 1;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003834 iidx = trycmd->tcd_finally_idx == 0
3835 ? trycmd->tcd_endtry_idx : trycmd->tcd_finally_idx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003836 }
3837 // jump to :finally or :endtry of current try statement
Bram Moolenaar4c137212021-04-19 16:48:48 +02003838 ectx->ec_iidx = iidx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003839 }
3840 break;
3841
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003842 case ISN_FINALLY:
3843 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003844 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003845 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
3846 + trystack->ga_len - 1;
3847
3848 // Reset the index to avoid a return statement jumps here
3849 // again.
3850 trycmd->tcd_finally_idx = 0;
3851 break;
3852 }
3853
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003854 // end of ":try" block
3855 case ISN_ENDTRY:
3856 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003857 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003858
3859 if (trystack->ga_len > 0)
3860 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003861 trycmd_T *trycmd;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003862
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003863 --trystack->ga_len;
3864 --trylevel;
3865 trycmd = ((trycmd_T *)trystack->ga_data)
3866 + trystack->ga_len;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003867 if (trycmd->tcd_did_throw)
3868 did_throw = TRUE;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01003869 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003870 {
3871 // discard the exception
3872 if (caught_stack == current_exception)
3873 caught_stack = caught_stack->caught;
3874 discard_current_exception();
3875 }
3876
3877 if (trycmd->tcd_return)
Bram Moolenaard032f342020-07-18 18:13:02 +02003878 goto func_return;
Bram Moolenaard9d77892021-02-12 21:32:47 +01003879
Bram Moolenaar4c137212021-04-19 16:48:48 +02003880 while (ectx->ec_stack.ga_len > trycmd->tcd_stack_len)
Bram Moolenaard9d77892021-02-12 21:32:47 +01003881 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003882 --ectx->ec_stack.ga_len;
Bram Moolenaard9d77892021-02-12 21:32:47 +01003883 clear_tv(STACK_TV_BOT(0));
3884 }
Bram Moolenaar8d4be892021-02-13 18:33:02 +01003885 if (trycmd->tcd_cont != 0)
Bram Moolenaarc150c092021-02-13 15:02:46 +01003886 // handling :continue: jump to outer try block or
3887 // start of the loop
Bram Moolenaar4c137212021-04-19 16:48:48 +02003888 ectx->ec_iidx = trycmd->tcd_cont - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003889 }
3890 }
3891 break;
3892
3893 case ISN_THROW:
Bram Moolenaar8f81b222021-01-14 21:47:06 +01003894 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003895 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02003896
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003897 if (trystack->ga_len == 0 && trylevel == 0 && emsg_silent)
3898 {
3899 // throwing an exception while using "silent!" causes
3900 // the function to abort but not display an error.
3901 tv = STACK_TV_BOT(-1);
3902 clear_tv(tv);
3903 tv->v_type = VAR_NUMBER;
3904 tv->vval.v_number = 0;
3905 goto done;
3906 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003907 --ectx->ec_stack.ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003908 tv = STACK_TV_BOT(0);
3909 if (tv->vval.v_string == NULL
3910 || *skipwhite(tv->vval.v_string) == NUL)
3911 {
3912 vim_free(tv->vval.v_string);
3913 SOURCING_LNUM = iptr->isn_lnum;
3914 emsg(_(e_throw_with_empty_string));
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003915 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003916 }
3917
3918 // Inside a "catch" we need to first discard the caught
3919 // exception.
3920 if (trystack->ga_len > 0)
3921 {
3922 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
3923 + trystack->ga_len - 1;
3924 if (trycmd->tcd_caught && current_exception != NULL)
3925 {
3926 // discard the exception
3927 if (caught_stack == current_exception)
3928 caught_stack = caught_stack->caught;
3929 discard_current_exception();
3930 trycmd->tcd_caught = FALSE;
3931 }
3932 }
3933
Bram Moolenaar90a57162022-02-12 14:23:17 +00003934 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003935 if (throw_exception(tv->vval.v_string, ET_USER, NULL)
3936 == FAIL)
3937 {
3938 vim_free(tv->vval.v_string);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003939 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003940 }
3941 did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003942 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003943 break;
3944
3945 // compare with special values
3946 case ISN_COMPAREBOOL:
3947 case ISN_COMPARESPECIAL:
3948 {
3949 typval_T *tv1 = STACK_TV_BOT(-2);
3950 typval_T *tv2 = STACK_TV_BOT(-1);
3951 varnumber_T arg1 = tv1->vval.v_number;
3952 varnumber_T arg2 = tv2->vval.v_number;
3953 int res;
3954
3955 switch (iptr->isn_arg.op.op_type)
3956 {
3957 case EXPR_EQUAL: res = arg1 == arg2; break;
3958 case EXPR_NEQUAL: res = arg1 != arg2; break;
3959 default: res = 0; break;
3960 }
3961
Bram Moolenaar4c137212021-04-19 16:48:48 +02003962 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003963 tv1->v_type = VAR_BOOL;
3964 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
3965 }
3966 break;
3967
Bram Moolenaar7a222242022-03-01 19:23:24 +00003968 case ISN_COMPARENULL:
3969 {
3970 typval_T *tv1 = STACK_TV_BOT(-2);
3971 typval_T *tv2 = STACK_TV_BOT(-1);
3972 int res;
3973
3974 res = typval_compare_null(tv1, tv2);
3975 if (res == MAYBE)
3976 goto on_error;
3977 if (iptr->isn_arg.op.op_type == EXPR_NEQUAL)
3978 res = !res;
3979 clear_tv(tv1);
3980 clear_tv(tv2);
3981 --ectx->ec_stack.ga_len;
3982 tv1->v_type = VAR_BOOL;
3983 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
3984 }
3985 break;
3986
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003987 // Operation with two number arguments
3988 case ISN_OPNR:
3989 case ISN_COMPARENR:
3990 {
3991 typval_T *tv1 = STACK_TV_BOT(-2);
3992 typval_T *tv2 = STACK_TV_BOT(-1);
3993 varnumber_T arg1 = tv1->vval.v_number;
3994 varnumber_T arg2 = tv2->vval.v_number;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02003995 varnumber_T res = 0;
3996 int div_zero = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003997
3998 switch (iptr->isn_arg.op.op_type)
3999 {
4000 case EXPR_MULT: res = arg1 * arg2; break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004001 case EXPR_DIV: if (arg2 == 0)
4002 div_zero = TRUE;
4003 else
4004 res = arg1 / arg2;
4005 break;
4006 case EXPR_REM: if (arg2 == 0)
4007 div_zero = TRUE;
4008 else
4009 res = arg1 % arg2;
4010 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004011 case EXPR_SUB: res = arg1 - arg2; break;
4012 case EXPR_ADD: res = arg1 + arg2; break;
4013
4014 case EXPR_EQUAL: res = arg1 == arg2; break;
4015 case EXPR_NEQUAL: res = arg1 != arg2; break;
4016 case EXPR_GREATER: res = arg1 > arg2; break;
4017 case EXPR_GEQUAL: res = arg1 >= arg2; break;
4018 case EXPR_SMALLER: res = arg1 < arg2; break;
4019 case EXPR_SEQUAL: res = arg1 <= arg2; break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004020 default: break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004021 }
4022
Bram Moolenaar4c137212021-04-19 16:48:48 +02004023 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004024 if (iptr->isn_type == ISN_COMPARENR)
4025 {
4026 tv1->v_type = VAR_BOOL;
4027 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4028 }
4029 else
4030 tv1->vval.v_number = res;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004031 if (div_zero)
4032 {
4033 SOURCING_LNUM = iptr->isn_lnum;
4034 emsg(_(e_divide_by_zero));
4035 goto on_error;
4036 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004037 }
4038 break;
4039
4040 // Computation with two float arguments
4041 case ISN_OPFLOAT:
4042 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01004043#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004044 {
4045 typval_T *tv1 = STACK_TV_BOT(-2);
4046 typval_T *tv2 = STACK_TV_BOT(-1);
4047 float_T arg1 = tv1->vval.v_float;
4048 float_T arg2 = tv2->vval.v_float;
4049 float_T res = 0;
4050 int cmp = FALSE;
4051
4052 switch (iptr->isn_arg.op.op_type)
4053 {
4054 case EXPR_MULT: res = arg1 * arg2; break;
4055 case EXPR_DIV: res = arg1 / arg2; break;
4056 case EXPR_SUB: res = arg1 - arg2; break;
4057 case EXPR_ADD: res = arg1 + arg2; break;
4058
4059 case EXPR_EQUAL: cmp = arg1 == arg2; break;
4060 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
4061 case EXPR_GREATER: cmp = arg1 > arg2; break;
4062 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
4063 case EXPR_SMALLER: cmp = arg1 < arg2; break;
4064 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
4065 default: cmp = 0; break;
4066 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004067 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004068 if (iptr->isn_type == ISN_COMPAREFLOAT)
4069 {
4070 tv1->v_type = VAR_BOOL;
4071 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
4072 }
4073 else
4074 tv1->vval.v_float = res;
4075 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01004076#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004077 break;
4078
4079 case ISN_COMPARELIST:
Bram Moolenaar265f8112021-12-19 12:33:05 +00004080 case ISN_COMPAREDICT:
4081 case ISN_COMPAREFUNC:
4082 case ISN_COMPARESTRING:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004083 case ISN_COMPAREBLOB:
4084 {
4085 typval_T *tv1 = STACK_TV_BOT(-2);
4086 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar265f8112021-12-19 12:33:05 +00004087 exprtype_T exprtype = iptr->isn_arg.op.op_type;
4088 int ic = iptr->isn_arg.op.op_ic;
4089 int res = FALSE;
4090 int status = OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004091
Bram Moolenaar265f8112021-12-19 12:33:05 +00004092 SOURCING_LNUM = iptr->isn_lnum;
4093 if (iptr->isn_type == ISN_COMPARELIST)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004094 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00004095 status = typval_compare_list(tv1, tv2,
4096 exprtype, ic, &res);
4097 }
4098 else if (iptr->isn_type == ISN_COMPAREDICT)
4099 {
4100 status = typval_compare_dict(tv1, tv2,
4101 exprtype, ic, &res);
4102 }
4103 else if (iptr->isn_type == ISN_COMPAREFUNC)
4104 {
4105 status = typval_compare_func(tv1, tv2,
4106 exprtype, ic, &res);
4107 }
4108 else if (iptr->isn_type == ISN_COMPARESTRING)
4109 {
4110 status = typval_compare_string(tv1, tv2,
4111 exprtype, ic, &res);
4112 }
4113 else
4114 {
4115 status = typval_compare_blob(tv1, tv2, exprtype, &res);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004116 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004117 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004118 clear_tv(tv1);
4119 clear_tv(tv2);
4120 tv1->v_type = VAR_BOOL;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004121 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4122 if (status == FAIL)
4123 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004124 }
4125 break;
4126
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004127 case ISN_COMPAREANY:
4128 {
4129 typval_T *tv1 = STACK_TV_BOT(-2);
4130 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar657137c2021-01-09 15:45:23 +01004131 exprtype_T exprtype = iptr->isn_arg.op.op_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004132 int ic = iptr->isn_arg.op.op_ic;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004133 int status;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004134
Bram Moolenaareb26f432020-09-14 16:50:05 +02004135 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004136 status = typval_compare(tv1, tv2, exprtype, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004137 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004138 --ectx->ec_stack.ga_len;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004139 if (status == FAIL)
4140 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004141 }
4142 break;
4143
4144 case ISN_ADDLIST:
4145 case ISN_ADDBLOB:
4146 {
4147 typval_T *tv1 = STACK_TV_BOT(-2);
4148 typval_T *tv2 = STACK_TV_BOT(-1);
4149
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004150 // add two lists or blobs
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004151 if (iptr->isn_type == ISN_ADDLIST)
Bram Moolenaar07802042021-09-09 23:01:14 +02004152 {
4153 if (iptr->isn_arg.op.op_type == EXPR_APPEND
4154 && tv1->vval.v_list != NULL)
4155 list_extend(tv1->vval.v_list, tv2->vval.v_list,
4156 NULL);
4157 else
4158 eval_addlist(tv1, tv2);
4159 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004160 else
4161 eval_addblob(tv1, tv2);
4162 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004163 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004164 }
4165 break;
4166
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004167 case ISN_LISTAPPEND:
4168 {
4169 typval_T *tv1 = STACK_TV_BOT(-2);
4170 typval_T *tv2 = STACK_TV_BOT(-1);
4171 list_T *l = tv1->vval.v_list;
4172
4173 // add an item to a list
Bram Moolenaar1f4a3452022-01-01 18:29:21 +00004174 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004175 if (l == NULL)
4176 {
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004177 emsg(_(e_cannot_add_to_null_list));
4178 goto on_error;
4179 }
Bram Moolenaar1f4a3452022-01-01 18:29:21 +00004180 if (value_check_lock(l->lv_lock, NULL, FALSE))
4181 goto on_error;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004182 if (list_append_tv(l, tv2) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004183 goto theend;
Bram Moolenaar955347c2020-10-19 23:01:46 +02004184 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004185 --ectx->ec_stack.ga_len;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004186 }
4187 break;
4188
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02004189 case ISN_BLOBAPPEND:
4190 {
4191 typval_T *tv1 = STACK_TV_BOT(-2);
4192 typval_T *tv2 = STACK_TV_BOT(-1);
4193 blob_T *b = tv1->vval.v_blob;
4194 int error = FALSE;
4195 varnumber_T n;
4196
4197 // add a number to a blob
4198 if (b == NULL)
4199 {
4200 SOURCING_LNUM = iptr->isn_lnum;
4201 emsg(_(e_cannot_add_to_null_blob));
4202 goto on_error;
4203 }
4204 n = tv_get_number_chk(tv2, &error);
4205 if (error)
4206 goto on_error;
4207 ga_append(&b->bv_ga, (int)n);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004208 --ectx->ec_stack.ga_len;
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02004209 }
4210 break;
4211
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004212 // Computation with two arguments of unknown type
4213 case ISN_OPANY:
4214 {
4215 typval_T *tv1 = STACK_TV_BOT(-2);
4216 typval_T *tv2 = STACK_TV_BOT(-1);
4217 varnumber_T n1, n2;
4218#ifdef FEAT_FLOAT
4219 float_T f1 = 0, f2 = 0;
4220#endif
4221 int error = FALSE;
4222
4223 if (iptr->isn_arg.op.op_type == EXPR_ADD)
4224 {
4225 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
4226 {
4227 eval_addlist(tv1, tv2);
4228 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004229 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004230 break;
4231 }
4232 else if (tv1->v_type == VAR_BLOB
4233 && tv2->v_type == VAR_BLOB)
4234 {
4235 eval_addblob(tv1, tv2);
4236 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004237 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004238 break;
4239 }
4240 }
4241#ifdef FEAT_FLOAT
4242 if (tv1->v_type == VAR_FLOAT)
4243 {
4244 f1 = tv1->vval.v_float;
4245 n1 = 0;
4246 }
4247 else
4248#endif
4249 {
Bram Moolenaarf665e972020-12-05 19:17:16 +01004250 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004251 n1 = tv_get_number_chk(tv1, &error);
4252 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02004253 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004254#ifdef FEAT_FLOAT
4255 if (tv2->v_type == VAR_FLOAT)
4256 f1 = n1;
4257#endif
4258 }
4259#ifdef FEAT_FLOAT
4260 if (tv2->v_type == VAR_FLOAT)
4261 {
4262 f2 = tv2->vval.v_float;
4263 n2 = 0;
4264 }
4265 else
4266#endif
4267 {
4268 n2 = tv_get_number_chk(tv2, &error);
4269 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02004270 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004271#ifdef FEAT_FLOAT
4272 if (tv1->v_type == VAR_FLOAT)
4273 f2 = n2;
4274#endif
4275 }
4276#ifdef FEAT_FLOAT
4277 // if there is a float on either side the result is a float
4278 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
4279 {
4280 switch (iptr->isn_arg.op.op_type)
4281 {
4282 case EXPR_MULT: f1 = f1 * f2; break;
4283 case EXPR_DIV: f1 = f1 / f2; break;
4284 case EXPR_SUB: f1 = f1 - f2; break;
4285 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004286 default: SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004287 emsg(_(e_cannot_use_percent_with_float));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004288 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004289 }
4290 clear_tv(tv1);
4291 clear_tv(tv2);
4292 tv1->v_type = VAR_FLOAT;
4293 tv1->vval.v_float = f1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004294 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004295 }
4296 else
4297#endif
4298 {
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004299 int failed = FALSE;
4300
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004301 switch (iptr->isn_arg.op.op_type)
4302 {
4303 case EXPR_MULT: n1 = n1 * n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004304 case EXPR_DIV: n1 = num_divide(n1, n2, &failed);
4305 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01004306 goto on_error;
4307 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004308 case EXPR_SUB: n1 = n1 - n2; break;
4309 case EXPR_ADD: n1 = n1 + n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004310 default: n1 = num_modulus(n1, n2, &failed);
4311 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01004312 goto on_error;
4313 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004314 }
4315 clear_tv(tv1);
4316 clear_tv(tv2);
4317 tv1->v_type = VAR_NUMBER;
4318 tv1->vval.v_number = n1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004319 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004320 }
4321 }
4322 break;
4323
4324 case ISN_CONCAT:
4325 {
4326 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string;
4327 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string;
4328 char_u *res;
4329
4330 res = concat_str(str1, str2);
4331 clear_tv(STACK_TV_BOT(-2));
4332 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02004333 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004334 STACK_TV_BOT(-1)->vval.v_string = res;
4335 }
4336 break;
4337
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004338 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004339 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004340 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004341 int is_slice = iptr->isn_type == ISN_STRSLICE;
4342 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004343 char_u *res;
4344
4345 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004346 // string slice: string is at stack-3, first index at
4347 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004348 if (is_slice)
4349 {
4350 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004351 n1 = tv->vval.v_number;
4352 }
4353
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004354 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004355 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004356
Bram Moolenaar4c137212021-04-19 16:48:48 +02004357 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004358 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004359 if (is_slice)
4360 // Slice: Select the characters from the string
Bram Moolenaar6601b622021-01-13 21:47:15 +01004361 res = string_slice(tv->vval.v_string, n1, n2, FALSE);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004362 else
4363 // Index: The resulting variable is a string of a
Bram Moolenaar0289a092021-03-14 18:40:19 +01004364 // single character (including composing characters).
4365 // If the index is too big or negative the result is
4366 // empty.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004367 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004368 vim_free(tv->vval.v_string);
4369 tv->vval.v_string = res;
4370 }
4371 break;
4372
4373 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02004374 case ISN_LISTSLICE:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004375 case ISN_BLOBINDEX:
4376 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004377 {
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004378 int is_slice = iptr->isn_type == ISN_LISTSLICE
4379 || iptr->isn_type == ISN_BLOBSLICE;
4380 int is_blob = iptr->isn_type == ISN_BLOBINDEX
4381 || iptr->isn_type == ISN_BLOBSLICE;
Bram Moolenaared591872020-08-15 22:14:53 +02004382 varnumber_T n1, n2;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004383 typval_T *val_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004384
4385 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02004386 // list slice: list is at stack-3, indexes at stack-2 and
4387 // stack-1
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004388 // Same for blob.
4389 val_tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004390
4391 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02004392 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004393 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02004394
4395 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004396 {
Bram Moolenaared591872020-08-15 22:14:53 +02004397 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02004398 n1 = tv->vval.v_number;
4399 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004400 }
Bram Moolenaared591872020-08-15 22:14:53 +02004401
Bram Moolenaar4c137212021-04-19 16:48:48 +02004402 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02004403 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02004404 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004405 if (is_blob)
4406 {
4407 if (blob_slice_or_index(val_tv->vval.v_blob, is_slice,
4408 n1, n2, FALSE, tv) == FAIL)
4409 goto on_error;
4410 }
4411 else
4412 {
4413 if (list_slice_or_index(val_tv->vval.v_list, is_slice,
4414 n1, n2, FALSE, tv, TRUE) == FAIL)
4415 goto on_error;
4416 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004417 }
4418 break;
4419
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004420 case ISN_ANYINDEX:
4421 case ISN_ANYSLICE:
4422 {
4423 int is_slice = iptr->isn_type == ISN_ANYSLICE;
4424 typval_T *var1, *var2;
4425 int res;
4426
4427 // index: composite is at stack-2, index at stack-1
4428 // slice: composite is at stack-3, indexes at stack-2 and
4429 // stack-1
4430 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02004431 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004432 if (check_can_index(tv, TRUE, TRUE) == FAIL)
4433 goto on_error;
4434 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
4435 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
Bram Moolenaar6601b622021-01-13 21:47:15 +01004436 res = eval_index_inner(tv, is_slice, var1, var2,
4437 FALSE, NULL, -1, TRUE);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004438 clear_tv(var1);
4439 if (is_slice)
4440 clear_tv(var2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004441 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004442 if (res == FAIL)
4443 goto on_error;
4444 }
4445 break;
4446
Bram Moolenaar9af78762020-06-16 11:34:42 +02004447 case ISN_SLICE:
4448 {
4449 list_T *list;
4450 int count = iptr->isn_arg.number;
4451
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02004452 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02004453 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02004454 list = tv->vval.v_list;
4455
4456 // no error for short list, expect it to be checked earlier
4457 if (list != NULL && list->lv_len >= count)
4458 {
4459 list_T *newlist = list_slice(list,
4460 count, list->lv_len - 1);
4461
4462 if (newlist != NULL)
4463 {
4464 list_unref(list);
4465 tv->vval.v_list = newlist;
4466 ++newlist->lv_refcount;
4467 }
4468 }
4469 }
4470 break;
4471
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004472 case ISN_GETITEM:
4473 {
4474 listitem_T *li;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02004475 getitem_T *gi = &iptr->isn_arg.getitem;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004476
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02004477 // Get list item: list is at stack-1, push item.
4478 // List type and length is checked for when compiling.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02004479 tv = STACK_TV_BOT(-1 - gi->gi_with_op);
4480 li = list_find(tv->vval.v_list, gi->gi_index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004481
Bram Moolenaar35578162021-08-02 19:10:38 +02004482 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004483 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004484 ++ectx->ec_stack.ga_len;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004485 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004486
4487 // Useful when used in unpack assignment. Reset at
4488 // ISN_DROP.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02004489 ectx->ec_where.wt_index = gi->gi_index + 1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004490 ectx->ec_where.wt_variable = TRUE;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004491 }
4492 break;
4493
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004494 case ISN_MEMBER:
4495 {
4496 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004497 char_u *key;
4498 dictitem_T *di;
4499
4500 // dict member: dict is at stack-2, key at stack-1
4501 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02004502 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004503 dict = tv->vval.v_dict;
4504
4505 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02004506 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004507 key = tv->vval.v_string;
Bram Moolenaar086fc9a2020-10-30 18:33:02 +01004508 if (key == NULL)
4509 key = (char_u *)"";
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02004510
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004511 if ((di = dict_find(dict, key, -1)) == NULL)
4512 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004513 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004514 semsg(_(e_key_not_present_in_dictionary), key);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004515
4516 // If :silent! is used we will continue, make sure the
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004517 // stack contents makes sense and the dict stack is
4518 // updated.
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004519 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004520 --ectx->ec_stack.ga_len;
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004521 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004522 (void) dict_stack_save(tv);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004523 tv->v_type = VAR_NUMBER;
4524 tv->vval.v_number = 0;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004525 goto on_fatal_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004526 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004527 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004528 --ectx->ec_stack.ga_len;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004529 // Put the dict used on the dict stack, it might be used by
4530 // a dict function later.
Bram Moolenaar50788ef2020-07-05 16:51:26 +02004531 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004532 if (dict_stack_save(tv) == FAIL)
4533 goto on_fatal_error;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02004534 copy_tv(&di->di_tv, tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004535 }
4536 break;
4537
4538 // dict member with string key
4539 case ISN_STRINGMEMBER:
4540 {
4541 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004542 dictitem_T *di;
4543
4544 tv = STACK_TV_BOT(-1);
4545 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
4546 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004547 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004548 emsg(_(e_dictionary_required));
Bram Moolenaard032f342020-07-18 18:13:02 +02004549 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004550 }
4551 dict = tv->vval.v_dict;
4552
4553 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
4554 == NULL)
4555 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004556 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar381692b2022-02-02 20:01:27 +00004557 semsg(_(e_key_not_present_in_dictionary),
4558 iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02004559 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004560 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004561 // Put the dict used on the dict stack, it might be used by
4562 // a dict function later.
4563 if (dict_stack_save(tv) == FAIL)
4564 goto on_fatal_error;
4565
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004566 copy_tv(&di->di_tv, tv);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004567 }
4568 break;
4569
4570 case ISN_CLEARDICT:
4571 dict_stack_drop();
4572 break;
4573
4574 case ISN_USEDICT:
4575 {
4576 typval_T *dict_tv = dict_stack_get_tv();
4577
4578 // Turn "dict.Func" into a partial for "Func" bound to
4579 // "dict". Don't do this when "Func" is already a partial
4580 // that was bound explicitly (pt_auto is FALSE).
4581 tv = STACK_TV_BOT(-1);
4582 if (dict_tv != NULL
4583 && dict_tv->v_type == VAR_DICT
4584 && dict_tv->vval.v_dict != NULL
4585 && (tv->v_type == VAR_FUNC
4586 || (tv->v_type == VAR_PARTIAL
4587 && (tv->vval.v_partial->pt_auto
4588 || tv->vval.v_partial->pt_dict == NULL))))
4589 dict_tv->vval.v_dict =
4590 make_partial(dict_tv->vval.v_dict, tv);
4591 dict_stack_drop();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004592 }
4593 break;
4594
4595 case ISN_NEGATENR:
4596 tv = STACK_TV_BOT(-1);
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00004597 // CHECKTYPE should have checked the variable type
Bram Moolenaarc58164c2020-03-29 18:40:30 +02004598#ifdef FEAT_FLOAT
4599 if (tv->v_type == VAR_FLOAT)
4600 tv->vval.v_float = -tv->vval.v_float;
4601 else
4602#endif
4603 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004604 break;
4605
4606 case ISN_CHECKNR:
4607 {
4608 int error = FALSE;
4609
4610 tv = STACK_TV_BOT(-1);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02004611 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004612 if (check_not_string(tv) == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004613 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004614 (void)tv_get_number_chk(tv, &error);
4615 if (error)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004616 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004617 }
4618 break;
4619
4620 case ISN_CHECKTYPE:
4621 {
4622 checktype_T *ct = &iptr->isn_arg.type;
4623
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01004624 tv = STACK_TV_BOT((int)ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02004625 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004626 if (!ectx->ec_where.wt_variable)
4627 ectx->ec_where.wt_index = ct->ct_arg_idx;
4628 if (check_typval_type(ct->ct_type, tv, ectx->ec_where)
4629 == FAIL)
Bram Moolenaar5e654232020-09-16 15:22:00 +02004630 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004631 if (!ectx->ec_where.wt_variable)
4632 ectx->ec_where.wt_index = 0;
Bram Moolenaar5e654232020-09-16 15:22:00 +02004633
4634 // number 0 is FALSE, number 1 is TRUE
4635 if (tv->v_type == VAR_NUMBER
4636 && ct->ct_type->tt_type == VAR_BOOL
4637 && (tv->vval.v_number == 0
4638 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004639 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02004640 tv->v_type = VAR_BOOL;
4641 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02004642 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004643 }
4644 }
4645 break;
4646
Bram Moolenaar9af78762020-06-16 11:34:42 +02004647 case ISN_CHECKLEN:
4648 {
4649 int min_len = iptr->isn_arg.checklen.cl_min_len;
4650 list_T *list = NULL;
4651
4652 tv = STACK_TV_BOT(-1);
4653 if (tv->v_type == VAR_LIST)
4654 list = tv->vval.v_list;
4655 if (list == NULL || list->lv_len < min_len
4656 || (list->lv_len > min_len
4657 && !iptr->isn_arg.checklen.cl_more_OK))
4658 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004659 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004660 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02004661 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004662 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02004663 }
4664 }
4665 break;
4666
Bram Moolenaaraa210a32021-01-02 15:41:03 +01004667 case ISN_SETTYPE:
Bram Moolenaar381692b2022-02-02 20:01:27 +00004668 set_tv_type(STACK_TV_BOT(-1), iptr->isn_arg.type.ct_type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01004669 break;
4670
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004671 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004672 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004673 {
4674 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004675 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004676
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004677 if (iptr->isn_type == ISN_2BOOL)
4678 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004679 tv = STACK_TV_BOT(iptr->isn_arg.tobool.offset);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004680 n = tv2bool(tv);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004681 if (iptr->isn_arg.tobool.invert)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004682 n = !n;
4683 }
4684 else
4685 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004686 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004687 SOURCING_LNUM = iptr->isn_lnum;
4688 n = tv_get_bool_chk(tv, &error);
4689 if (error)
4690 goto on_error;
4691 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004692 clear_tv(tv);
4693 tv->v_type = VAR_BOOL;
4694 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
4695 }
4696 break;
4697
4698 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02004699 case ISN_2STRING_ANY:
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01004700 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004701 if (do_2string(STACK_TV_BOT(iptr->isn_arg.tostring.offset),
4702 iptr->isn_type == ISN_2STRING_ANY,
4703 iptr->isn_arg.tostring.tolerant) == FAIL)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01004704 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004705 break;
4706
Bram Moolenaar08597872020-12-10 19:43:40 +01004707 case ISN_RANGE:
4708 {
4709 exarg_T ea;
4710 char *errormsg;
4711
Bram Moolenaarece0b872021-01-08 20:40:45 +01004712 ea.line2 = 0;
Bram Moolenaard1510ee2021-01-04 16:15:58 +01004713 ea.addr_count = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01004714 ea.addr_type = ADDR_LINES;
4715 ea.cmd = iptr->isn_arg.string;
Bram Moolenaarece0b872021-01-08 20:40:45 +01004716 ea.skip = FALSE;
Bram Moolenaar08597872020-12-10 19:43:40 +01004717 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaarece0b872021-01-08 20:40:45 +01004718 goto on_error;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004719
Bram Moolenaar35578162021-08-02 19:10:38 +02004720 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004721 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004722 ++ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004723 tv = STACK_TV_BOT(-1);
4724 tv->v_type = VAR_NUMBER;
4725 tv->v_lock = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01004726 if (ea.addr_count == 0)
4727 tv->vval.v_number = curwin->w_cursor.lnum;
4728 else
4729 tv->vval.v_number = ea.line2;
4730 }
4731 break;
4732
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004733 case ISN_PUT:
4734 {
4735 int regname = iptr->isn_arg.put.put_regname;
4736 linenr_T lnum = iptr->isn_arg.put.put_lnum;
4737 char_u *expr = NULL;
4738 int dir = FORWARD;
4739
Bram Moolenaar08597872020-12-10 19:43:40 +01004740 if (lnum < -2)
4741 {
4742 // line number was put on the stack by ISN_RANGE
4743 tv = STACK_TV_BOT(-1);
4744 curwin->w_cursor.lnum = tv->vval.v_number;
4745 if (lnum == LNUM_VARIABLE_RANGE_ABOVE)
4746 dir = BACKWARD;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004747 --ectx->ec_stack.ga_len;
Bram Moolenaar08597872020-12-10 19:43:40 +01004748 }
4749 else if (lnum == -2)
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004750 // :put! above cursor
4751 dir = BACKWARD;
4752 else if (lnum >= 0)
Bram Moolenaar4e713ba2022-02-07 15:31:37 +00004753 {
4754 curwin->w_cursor.lnum = lnum;
4755 if (lnum == 0)
4756 // check_cursor() below will move to line 1
4757 dir = BACKWARD;
4758 }
Bram Moolenaara28639e2021-01-19 22:48:09 +01004759
4760 if (regname == '=')
4761 {
4762 tv = STACK_TV_BOT(-1);
4763 if (tv->v_type == VAR_STRING)
4764 expr = tv->vval.v_string;
4765 else
4766 {
4767 expr = typval2string(tv, TRUE); // allocates value
4768 clear_tv(tv);
4769 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004770 --ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004771 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004772 check_cursor();
4773 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
4774 vim_free(expr);
4775 }
4776 break;
4777
Bram Moolenaar02194d22020-10-24 23:08:38 +02004778 case ISN_CMDMOD:
Bram Moolenaar4c137212021-04-19 16:48:48 +02004779 ectx->ec_funclocal.floc_save_cmdmod = cmdmod;
4780 ectx->ec_funclocal.floc_restore_cmdmod = TRUE;
4781 ectx->ec_funclocal.floc_restore_cmdmod_stacklen =
4782 ectx->ec_stack.ga_len;
Bram Moolenaar02194d22020-10-24 23:08:38 +02004783 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
4784 apply_cmdmod(&cmdmod);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004785 break;
4786
Bram Moolenaar02194d22020-10-24 23:08:38 +02004787 case ISN_CMDMOD_REV:
4788 // filter regprog is owned by the instruction, don't free it
4789 cmdmod.cmod_filter_regmatch.regprog = NULL;
4790 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004791 cmdmod = ectx->ec_funclocal.floc_save_cmdmod;
4792 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004793 break;
4794
Bram Moolenaar792f7862020-11-23 08:31:18 +01004795 case ISN_UNPACK:
4796 {
4797 int count = iptr->isn_arg.unpack.unp_count;
4798 int semicolon = iptr->isn_arg.unpack.unp_semicolon;
4799 list_T *l;
4800 listitem_T *li;
4801 int i;
4802
4803 // Check there is a valid list to unpack.
4804 tv = STACK_TV_BOT(-1);
4805 if (tv->v_type != VAR_LIST)
4806 {
4807 SOURCING_LNUM = iptr->isn_lnum;
4808 emsg(_(e_for_argument_must_be_sequence_of_lists));
4809 goto on_error;
4810 }
4811 l = tv->vval.v_list;
4812 if (l == NULL
4813 || l->lv_len < (semicolon ? count - 1 : count))
4814 {
4815 SOURCING_LNUM = iptr->isn_lnum;
4816 emsg(_(e_list_value_does_not_have_enough_items));
4817 goto on_error;
4818 }
4819 else if (!semicolon && l->lv_len > count)
4820 {
4821 SOURCING_LNUM = iptr->isn_lnum;
4822 emsg(_(e_list_value_has_more_items_than_targets));
4823 goto on_error;
4824 }
4825
4826 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar35578162021-08-02 19:10:38 +02004827 if (GA_GROW_FAILS(&ectx->ec_stack, count - 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004828 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004829 ectx->ec_stack.ga_len += count - 1;
Bram Moolenaar792f7862020-11-23 08:31:18 +01004830
4831 // Variable after semicolon gets a list with the remaining
4832 // items.
4833 if (semicolon)
4834 {
4835 list_T *rem_list =
4836 list_alloc_with_items(l->lv_len - count + 1);
4837
4838 if (rem_list == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004839 goto theend;
Bram Moolenaar792f7862020-11-23 08:31:18 +01004840 tv = STACK_TV_BOT(-count);
4841 tv->vval.v_list = rem_list;
4842 ++rem_list->lv_refcount;
4843 tv->v_lock = 0;
4844 li = l->lv_first;
4845 for (i = 0; i < count - 1; ++i)
4846 li = li->li_next;
4847 for (i = 0; li != NULL; ++i)
4848 {
Bram Moolenaar61efa162022-03-18 13:10:48 +00004849 typval_T tvcopy;
4850
4851 copy_tv(&li->li_tv, &tvcopy);
4852 list_set_item(rem_list, i, &tvcopy);
Bram Moolenaar792f7862020-11-23 08:31:18 +01004853 li = li->li_next;
4854 }
4855 --count;
4856 }
4857
4858 // Produce the values in reverse order, first item last.
4859 li = l->lv_first;
4860 for (i = 0; i < count; ++i)
4861 {
4862 tv = STACK_TV_BOT(-i - 1);
4863 copy_tv(&li->li_tv, tv);
4864 li = li->li_next;
4865 }
4866
4867 list_unref(l);
4868 }
4869 break;
4870
Bram Moolenaarb2049902021-01-24 12:53:53 +01004871 case ISN_PROF_START:
4872 case ISN_PROF_END:
4873 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01004874#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01004875 funccall_T cookie;
4876 ufunc_T *cur_ufunc =
4877 (((dfunc_T *)def_functions.ga_data)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02004878 + ectx->ec_dfunc_idx)->df_ufunc;
Bram Moolenaarb2049902021-01-24 12:53:53 +01004879
4880 cookie.func = cur_ufunc;
4881 if (iptr->isn_type == ISN_PROF_START)
4882 {
4883 func_line_start(&cookie, iptr->isn_lnum);
4884 // if we get here the instruction is executed
4885 func_line_exec(&cookie);
4886 }
4887 else
4888 func_line_end(&cookie);
Bram Moolenaarf002a412021-01-24 13:34:18 +01004889#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01004890 }
4891 break;
4892
Bram Moolenaare99d4222021-06-13 14:01:26 +02004893 case ISN_DEBUG:
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02004894 handle_debug(iptr, ectx);
Bram Moolenaare99d4222021-06-13 14:01:26 +02004895 break;
4896
Bram Moolenaar389df252020-07-09 21:20:47 +02004897 case ISN_SHUFFLE:
4898 {
Bram Moolenaar792f7862020-11-23 08:31:18 +01004899 typval_T tmp_tv;
4900 int item = iptr->isn_arg.shuffle.shfl_item;
4901 int up = iptr->isn_arg.shuffle.shfl_up;
Bram Moolenaar389df252020-07-09 21:20:47 +02004902
4903 tmp_tv = *STACK_TV_BOT(-item);
4904 for ( ; up > 0 && item > 1; --up)
4905 {
4906 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
4907 --item;
4908 }
4909 *STACK_TV_BOT(-item) = tmp_tv;
4910 }
4911 break;
4912
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004913 case ISN_DROP:
Bram Moolenaar4c137212021-04-19 16:48:48 +02004914 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004915 clear_tv(STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02004916 ectx->ec_where.wt_index = 0;
4917 ectx->ec_where.wt_variable = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004918 break;
4919 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004920 continue;
4921
Bram Moolenaard032f342020-07-18 18:13:02 +02004922func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02004923 // Restore previous function. If the frame pointer is where we started
4924 // then there is none and we are done.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004925 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02004926 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02004927
Bram Moolenaar4c137212021-04-19 16:48:48 +02004928 if (func_return(ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004929 // only fails when out of memory
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004930 goto theend;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02004931 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02004932
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004933on_error:
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004934 // Jump here for an error that does not require aborting execution.
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004935 // If "emsg_silent" is set then ignore the error, unless it was set
4936 // when calling the function.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004937 if (did_emsg_cumul + did_emsg == ectx->ec_did_emsg_before
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004938 && emsg_silent && did_emsg_def == 0)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004939 {
4940 // If a sequence of instructions causes an error while ":silent!"
4941 // was used, restore the stack length and jump ahead to restoring
4942 // the cmdmod.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004943 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004944 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004945 while (ectx->ec_stack.ga_len
4946 > ectx->ec_funclocal.floc_restore_cmdmod_stacklen)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004947 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004948 --ectx->ec_stack.ga_len;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004949 clear_tv(STACK_TV_BOT(0));
4950 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004951 while (ectx->ec_instr[ectx->ec_iidx].isn_type != ISN_CMDMOD_REV)
4952 ++ectx->ec_iidx;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004953 }
Bram Moolenaarcd030c42020-10-30 21:49:40 +01004954 continue;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004955 }
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004956on_fatal_error:
4957 // Jump here for an error that messes up the stack.
Bram Moolenaar171fb922020-10-28 16:54:47 +01004958 // If we are not inside a try-catch started here, abort execution.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004959 if (trylevel <= ectx->ec_trylevel_at_start)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004960 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004961 }
4962
4963done:
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004964 ret = OK;
4965theend:
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004966 dict_stack_clear(dict_stack_len_at_start);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004967 ectx->ec_trylevel_at_start = save_trylevel_at_start;
4968 return ret;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004969}
4970
4971/*
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004972 * Execute the instructions from a VAR_INSTR typeval and put the result in
4973 * "rettv".
4974 * Return OK or FAIL.
4975 */
4976 int
4977exe_typval_instr(typval_T *tv, typval_T *rettv)
4978{
4979 ectx_T *ectx = tv->vval.v_instr->instr_ectx;
4980 isn_T *save_instr = ectx->ec_instr;
4981 int save_iidx = ectx->ec_iidx;
4982 int res;
4983
4984 ectx->ec_instr = tv->vval.v_instr->instr_instr;
4985 res = exec_instructions(ectx);
4986 if (res == OK)
4987 {
4988 *rettv = *STACK_TV_BOT(-1);
4989 --ectx->ec_stack.ga_len;
4990 }
4991
4992 ectx->ec_instr = save_instr;
4993 ectx->ec_iidx = save_iidx;
4994
4995 return res;
4996}
4997
4998/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02004999 * Execute the instructions from an ISN_SUBSTITUTE command, which are in
5000 * "substitute_instr".
5001 */
5002 char_u *
5003exe_substitute_instr(void)
5004{
5005 ectx_T *ectx = substitute_instr->subs_ectx;
5006 isn_T *save_instr = ectx->ec_instr;
5007 int save_iidx = ectx->ec_iidx;
5008 char_u *res;
5009
5010 ectx->ec_instr = substitute_instr->subs_instr;
5011 if (exec_instructions(ectx) == OK)
Bram Moolenaar90193e62021-04-04 20:49:50 +02005012 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005013 typval_T *tv = STACK_TV_BOT(-1);
5014
Bram Moolenaar27523602021-06-05 21:36:19 +02005015 res = typval2string(tv, TRUE);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005016 --ectx->ec_stack.ga_len;
5017 clear_tv(tv);
Bram Moolenaar90193e62021-04-04 20:49:50 +02005018 }
5019 else
5020 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005021 substitute_instr->subs_status = FAIL;
5022 res = vim_strsave((char_u *)"");
Bram Moolenaar90193e62021-04-04 20:49:50 +02005023 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005024
Bram Moolenaar4c137212021-04-19 16:48:48 +02005025 ectx->ec_instr = save_instr;
5026 ectx->ec_iidx = save_iidx;
5027
5028 return res;
5029}
5030
5031/*
5032 * Call a "def" function from old Vim script.
5033 * Return OK or FAIL.
5034 */
5035 int
5036call_def_function(
5037 ufunc_T *ufunc,
5038 int argc_arg, // nr of arguments
5039 typval_T *argv, // arguments
5040 partial_T *partial, // optional partial for context
5041 typval_T *rettv) // return value
5042{
5043 ectx_T ectx; // execution context
5044 int argc = argc_arg;
5045 typval_T *tv;
5046 int idx;
5047 int ret = FAIL;
5048 int defcount = ufunc->uf_args.ga_len - argc;
5049 sctx_T save_current_sctx = current_sctx;
5050 int did_emsg_before = did_emsg_cumul + did_emsg;
5051 int save_suppress_errthrow = suppress_errthrow;
5052 msglist_T **saved_msg_list = NULL;
5053 msglist_T *private_msg_list = NULL;
5054 int save_emsg_silent_def = emsg_silent_def;
5055 int save_did_emsg_def = did_emsg_def;
5056 int orig_funcdepth;
Bram Moolenaare99d4222021-06-13 14:01:26 +02005057 int orig_nesting_level = ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005058
5059// Get pointer to item in the stack.
5060#undef STACK_TV
5061#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
5062
5063// Get pointer to item at the bottom of the stack, -1 is the bottom.
5064#undef STACK_TV_BOT
5065#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
5066
5067// Get pointer to a local variable on the stack. Negative for arguments.
5068#undef STACK_TV_VAR
5069#define STACK_TV_VAR(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_frame_idx + STACK_FRAME_SIZE + idx)
5070
5071 if (ufunc->uf_def_status == UF_NOT_COMPILED
5072 || ufunc->uf_def_status == UF_COMPILE_ERROR
Bram Moolenaar139575d2022-03-15 19:29:30 +00005073 || (func_needs_compiling(ufunc, get_compile_type(ufunc))
5074 && compile_def_function(ufunc, FALSE,
5075 get_compile_type(ufunc), NULL) == FAIL))
Bram Moolenaar4c137212021-04-19 16:48:48 +02005076 {
5077 if (did_emsg_cumul + did_emsg == did_emsg_before)
5078 semsg(_(e_function_is_not_compiled_str),
5079 printable_func_name(ufunc));
5080 return FAIL;
5081 }
5082
5083 {
5084 // Check the function was really compiled.
5085 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5086 + ufunc->uf_dfunc_idx;
5087 if (INSTRUCTIONS(dfunc) == NULL)
5088 {
5089 iemsg("using call_def_function() on not compiled function");
5090 return FAIL;
5091 }
5092 }
5093
5094 // If depth of calling is getting too high, don't execute the function.
5095 orig_funcdepth = funcdepth_get();
5096 if (funcdepth_increment() == FAIL)
5097 return FAIL;
5098
5099 CLEAR_FIELD(ectx);
5100 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
5101 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
Bram Moolenaar35578162021-08-02 19:10:38 +02005102 if (GA_GROW_FAILS(&ectx.ec_stack, 20))
Bram Moolenaar4c137212021-04-19 16:48:48 +02005103 {
5104 funcdepth_decrement();
5105 return FAIL;
5106 }
5107 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
5108 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
5109 ectx.ec_did_emsg_before = did_emsg_before;
Bram Moolenaare99d4222021-06-13 14:01:26 +02005110 ++ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005111
5112 idx = argc - ufunc->uf_args.ga_len;
5113 if (idx > 0 && ufunc->uf_va_name == NULL)
5114 {
5115 if (idx == 1)
5116 emsg(_(e_one_argument_too_many));
5117 else
5118 semsg(_(e_nr_arguments_too_many), idx);
5119 goto failed_early;
5120 }
Bram Moolenaarc6d71532021-06-05 18:49:38 +02005121 idx = argc - ufunc->uf_args.ga_len + ufunc->uf_def_args.ga_len;
5122 if (idx < 0)
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02005123 {
5124 if (idx == -1)
5125 emsg(_(e_one_argument_too_few));
5126 else
5127 semsg(_(e_nr_arguments_too_few), -idx);
5128 goto failed_early;
5129 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005130
5131 // Put arguments on the stack, but no more than what the function expects.
5132 // A lambda can be called with more arguments than it uses.
5133 for (idx = 0; idx < argc
5134 && (ufunc->uf_va_name != NULL || idx < ufunc->uf_args.ga_len);
5135 ++idx)
5136 {
5137 if (idx >= ufunc->uf_args.ga_len - ufunc->uf_def_args.ga_len
5138 && argv[idx].v_type == VAR_SPECIAL
5139 && argv[idx].vval.v_number == VVAL_NONE)
5140 {
5141 // Use the default value.
5142 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
5143 }
5144 else
5145 {
5146 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
5147 && check_typval_arg_type(
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02005148 ufunc->uf_arg_types[idx], &argv[idx],
5149 NULL, idx + 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005150 goto failed_early;
5151 copy_tv(&argv[idx], STACK_TV_BOT(0));
5152 }
5153 ++ectx.ec_stack.ga_len;
5154 }
5155
5156 // Turn varargs into a list. Empty list if no args.
5157 if (ufunc->uf_va_name != NULL)
5158 {
5159 int vararg_count = argc - ufunc->uf_args.ga_len;
5160
5161 if (vararg_count < 0)
5162 vararg_count = 0;
5163 else
5164 argc -= vararg_count;
5165 if (exe_newlist(vararg_count, &ectx) == FAIL)
5166 goto failed_early;
5167
5168 // Check the type of the list items.
5169 tv = STACK_TV_BOT(-1);
5170 if (ufunc->uf_va_type != NULL
5171 && ufunc->uf_va_type != &t_list_any
5172 && ufunc->uf_va_type->tt_member != &t_any
5173 && tv->vval.v_list != NULL)
5174 {
5175 type_T *expected = ufunc->uf_va_type->tt_member;
5176 listitem_T *li = tv->vval.v_list->lv_first;
5177
5178 for (idx = 0; idx < vararg_count; ++idx)
5179 {
5180 if (check_typval_arg_type(expected, &li->li_tv,
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02005181 NULL, argc + idx + 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005182 goto failed_early;
5183 li = li->li_next;
5184 }
5185 }
5186
5187 if (defcount > 0)
5188 // Move varargs list to below missing default arguments.
5189 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
5190 --ectx.ec_stack.ga_len;
5191 }
5192
5193 // Make space for omitted arguments, will store default value below.
5194 // Any varargs list goes after them.
5195 if (defcount > 0)
5196 for (idx = 0; idx < defcount; ++idx)
5197 {
5198 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
5199 ++ectx.ec_stack.ga_len;
5200 }
5201 if (ufunc->uf_va_name != NULL)
5202 ++ectx.ec_stack.ga_len;
5203
5204 // Frame pointer points to just after arguments.
5205 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
5206 ectx.ec_initial_frame_idx = ectx.ec_frame_idx;
5207
5208 {
5209 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5210 + ufunc->uf_dfunc_idx;
5211 ufunc_T *base_ufunc = dfunc->df_ufunc;
5212
5213 // "uf_partial" is on the ufunc that "df_ufunc" points to, as is done
5214 // by copy_func().
5215 if (partial != NULL || base_ufunc->uf_partial != NULL)
5216 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005217 ectx.ec_outer_ref = ALLOC_CLEAR_ONE(outer_ref_T);
5218 if (ectx.ec_outer_ref == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005219 goto failed_early;
5220 if (partial != NULL)
5221 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00005222 outer_T *outer = get_pt_outer(partial);
5223
5224 if (outer->out_stack == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005225 {
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00005226 if (current_ectx != NULL)
5227 {
5228 if (current_ectx->ec_outer_ref != NULL
5229 && current_ectx->ec_outer_ref->or_outer != NULL)
5230 ectx.ec_outer_ref->or_outer =
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005231 current_ectx->ec_outer_ref->or_outer;
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00005232 }
5233 // Should there be an error here?
Bram Moolenaar4c137212021-04-19 16:48:48 +02005234 }
5235 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005236 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00005237 ectx.ec_outer_ref->or_outer = outer;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005238 ++partial->pt_refcount;
5239 ectx.ec_outer_ref->or_partial = partial;
5240 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005241 }
5242 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005243 {
5244 ectx.ec_outer_ref->or_outer = &base_ufunc->uf_partial->pt_outer;
5245 ++base_ufunc->uf_partial->pt_refcount;
5246 ectx.ec_outer_ref->or_partial = base_ufunc->uf_partial;
5247 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005248 }
5249 }
5250
5251 // dummy frame entries
5252 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
5253 {
5254 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
5255 ++ectx.ec_stack.ga_len;
5256 }
5257
5258 {
5259 // Reserve space for local variables and any closure reference count.
5260 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5261 + ufunc->uf_dfunc_idx;
5262
Bram Moolenaar5cd64792021-12-25 18:23:24 +00005263 // Initialize variables to zero. That avoids having to generate
5264 // initializing instructions for "var nr: number", "var x: any", etc.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005265 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaar5cd64792021-12-25 18:23:24 +00005266 {
5267 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
5268 STACK_TV_VAR(idx)->vval.v_number = 0;
5269 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005270 ectx.ec_stack.ga_len += dfunc->df_varcount;
5271 if (dfunc->df_has_closure)
5272 {
5273 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
5274 STACK_TV_VAR(idx)->vval.v_number = 0;
5275 ++ectx.ec_stack.ga_len;
5276 }
5277
5278 ectx.ec_instr = INSTRUCTIONS(dfunc);
5279 }
5280
5281 // Following errors are in the function, not the caller.
5282 // Commands behave like vim9script.
5283 estack_push_ufunc(ufunc, 1);
5284 current_sctx = ufunc->uf_script_ctx;
5285 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
5286
5287 // Use a specific location for storing error messages to be converted to an
5288 // exception.
5289 saved_msg_list = msg_list;
5290 msg_list = &private_msg_list;
5291
5292 // Do turn errors into exceptions.
5293 suppress_errthrow = FALSE;
5294
5295 // Do not delete the function while executing it.
5296 ++ufunc->uf_calls;
5297
5298 // When ":silent!" was used before calling then we still abort the
5299 // function. If ":silent!" is used in the function then we don't.
5300 emsg_silent_def = emsg_silent;
5301 did_emsg_def = 0;
5302
5303 ectx.ec_where.wt_index = 0;
5304 ectx.ec_where.wt_variable = FALSE;
5305
5306 // Execute the instructions until done.
5307 ret = exec_instructions(&ectx);
5308 if (ret == OK)
5309 {
5310 // function finished, get result from the stack.
5311 if (ufunc->uf_ret_type == &t_void)
5312 {
5313 rettv->v_type = VAR_VOID;
5314 }
5315 else
5316 {
5317 tv = STACK_TV_BOT(-1);
5318 *rettv = *tv;
5319 tv->v_type = VAR_UNKNOWN;
5320 }
5321 }
5322
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01005323 // When failed need to unwind the call stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005324 while (ectx.ec_frame_idx != ectx.ec_initial_frame_idx)
5325 func_return(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02005326
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005327 // Deal with any remaining closures, they may be in use somewhere.
5328 if (ectx.ec_funcrefs.ga_len > 0)
Bram Moolenaarf112f302020-12-20 17:47:52 +01005329 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005330 handle_closure_in_use(&ectx, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00005331 ga_clear(&ectx.ec_funcrefs);
Bram Moolenaarf112f302020-12-20 17:47:52 +01005332 }
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005333
Bram Moolenaaree8580e2020-08-28 17:19:07 +02005334 estack_pop();
5335 current_sctx = save_current_sctx;
5336
Bram Moolenaar2336c372021-12-06 15:06:54 +00005337 if (--ufunc->uf_calls <= 0 && ufunc->uf_refcount <= 0)
5338 // Function was unreferenced while being used, free it now.
5339 func_clear_free(ufunc, FALSE);
Bram Moolenaarc970e422021-03-17 15:03:04 +01005340
Bram Moolenaar352134b2020-10-17 22:04:08 +02005341 if (*msg_list != NULL && saved_msg_list != NULL)
5342 {
5343 msglist_T **plist = saved_msg_list;
5344
5345 // Append entries from the current msg_list (uncaught exceptions) to
5346 // the saved msg_list.
5347 while (*plist != NULL)
5348 plist = &(*plist)->next;
5349
5350 *plist = *msg_list;
5351 }
5352 msg_list = saved_msg_list;
5353
Bram Moolenaar4c137212021-04-19 16:48:48 +02005354 if (ectx.ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar02194d22020-10-24 23:08:38 +02005355 {
5356 cmdmod.cmod_filter_regmatch.regprog = NULL;
5357 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005358 cmdmod = ectx.ec_funclocal.floc_save_cmdmod;
Bram Moolenaar02194d22020-10-24 23:08:38 +02005359 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +01005360 emsg_silent_def = save_emsg_silent_def;
5361 did_emsg_def += save_did_emsg_def;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02005362
Bram Moolenaaree8580e2020-08-28 17:19:07 +02005363failed_early:
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02005364 // Free all local variables, but not arguments.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005365 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
5366 clear_tv(STACK_TV(idx));
Bram Moolenaare99d4222021-06-13 14:01:26 +02005367 ex_nesting_level = orig_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02005368
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005369 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01005370 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005371 if (ectx.ec_outer_ref != NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +01005372 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005373 if (ectx.ec_outer_ref->or_outer_allocated)
5374 vim_free(ectx.ec_outer_ref->or_outer);
5375 partial_unref(ectx.ec_outer_ref->or_partial);
5376 vim_free(ectx.ec_outer_ref);
Bram Moolenaar0186e582021-01-10 18:33:11 +01005377 }
5378
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02005379 // Not sure if this is necessary.
5380 suppress_errthrow = save_suppress_errthrow;
5381
Bram Moolenaarb5841b92021-07-15 18:09:53 +02005382 if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before
5383 && !need_rethrow)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005384 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02005385 printable_func_name(ufunc));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01005386 funcdepth_restore(orig_funcdepth);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005387 return ret;
5388}
5389
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005390/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02005391 * List instructions "instr" up to "instr_count" or until ISN_FINISH.
5392 * "ufunc" has the source lines, NULL for the instructions of ISN_SUBSTITUTE.
5393 * "pfx" is prefixed to every line.
5394 */
5395 static void
5396list_instructions(char *pfx, isn_T *instr, int instr_count, ufunc_T *ufunc)
5397{
5398 int line_idx = 0;
5399 int prev_current = 0;
5400 int current;
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02005401 int def_arg_idx = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005402
5403 for (current = 0; current < instr_count; ++current)
5404 {
5405 isn_T *iptr = &instr[current];
5406 char *line;
5407
5408 if (ufunc != NULL)
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02005409 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005410 while (line_idx < iptr->isn_lnum
5411 && line_idx < ufunc->uf_lines.ga_len)
5412 {
5413 if (current > prev_current)
5414 {
5415 msg_puts("\n\n");
5416 prev_current = current;
5417 }
5418 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
5419 if (line != NULL)
5420 msg(line);
5421 }
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02005422 if (iptr->isn_type == ISN_JUMP_IF_ARG_SET)
5423 {
5424 int first_def_arg = ufunc->uf_args.ga_len
5425 - ufunc->uf_def_args.ga_len;
5426
5427 if (def_arg_idx > 0)
5428 msg_puts("\n\n");
5429 msg_start();
5430 msg_puts(" ");
5431 msg_puts(((char **)(ufunc->uf_args.ga_data))[
5432 first_def_arg + def_arg_idx]);
5433 msg_puts(" = ");
5434 msg_puts(((char **)(ufunc->uf_def_args.ga_data))[def_arg_idx++]);
5435 msg_clr_eos();
5436 msg_end();
5437 }
5438 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005439
5440 switch (iptr->isn_type)
5441 {
5442 case ISN_EXEC:
5443 smsg("%s%4d EXEC %s", pfx, current, iptr->isn_arg.string);
5444 break;
Bram Moolenaar20677332021-06-06 17:02:53 +02005445 case ISN_EXEC_SPLIT:
5446 smsg("%s%4d EXEC_SPLIT %s", pfx, current, iptr->isn_arg.string);
5447 break;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00005448 case ISN_EXECRANGE:
5449 smsg("%s%4d EXECRANGE %s", pfx, current, iptr->isn_arg.string);
5450 break;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005451 case ISN_LEGACY_EVAL:
5452 smsg("%s%4d EVAL legacy %s", pfx, current,
5453 iptr->isn_arg.string);
5454 break;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02005455 case ISN_REDIRSTART:
5456 smsg("%s%4d REDIR", pfx, current);
5457 break;
5458 case ISN_REDIREND:
5459 smsg("%s%4d REDIR END%s", pfx, current,
5460 iptr->isn_arg.number ? " append" : "");
5461 break;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005462 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005463#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005464 smsg("%s%4d CEXPR pre %s", pfx, current,
5465 cexpr_get_auname(iptr->isn_arg.number));
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005466#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005467 break;
5468 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005469#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005470 {
5471 cexprref_T *cer = iptr->isn_arg.cexpr.cexpr_ref;
5472
5473 smsg("%s%4d CEXPR core %s%s \"%s\"", pfx, current,
5474 cexpr_get_auname(cer->cer_cmdidx),
5475 cer->cer_forceit ? "!" : "",
5476 cer->cer_cmdline);
5477 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005478#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005479 break;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005480 case ISN_INSTR:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01005481 smsg("%s%4d INSTR", pfx, current);
5482 list_instructions(" ", iptr->isn_arg.instr, INT_MAX, NULL);
5483 msg(" -------------");
5484 break;
5485 case ISN_SOURCE:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005486 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01005487 scriptitem_T *si = SCRIPT_ITEM(iptr->isn_arg.number);
5488
5489 smsg("%s%4d SOURCE %s", pfx, current, si->sn_name);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005490 }
5491 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005492 case ISN_SUBSTITUTE:
5493 {
5494 subs_T *subs = &iptr->isn_arg.subs;
5495
5496 smsg("%s%4d SUBSTITUTE %s", pfx, current, subs->subs_cmd);
5497 list_instructions(" ", subs->subs_instr, INT_MAX, NULL);
5498 msg(" -------------");
5499 }
5500 break;
5501 case ISN_EXECCONCAT:
5502 smsg("%s%4d EXECCONCAT %lld", pfx, current,
5503 (varnumber_T)iptr->isn_arg.number);
5504 break;
5505 case ISN_ECHO:
5506 {
5507 echo_T *echo = &iptr->isn_arg.echo;
5508
5509 smsg("%s%4d %s %d", pfx, current,
5510 echo->echo_with_white ? "ECHO" : "ECHON",
5511 echo->echo_count);
5512 }
5513 break;
5514 case ISN_EXECUTE:
5515 smsg("%s%4d EXECUTE %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02005516 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005517 break;
5518 case ISN_ECHOMSG:
5519 smsg("%s%4d ECHOMSG %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02005520 (varnumber_T)(iptr->isn_arg.number));
5521 break;
5522 case ISN_ECHOCONSOLE:
5523 smsg("%s%4d ECHOCONSOLE %lld", pfx, current,
5524 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005525 break;
5526 case ISN_ECHOERR:
5527 smsg("%s%4d ECHOERR %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02005528 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005529 break;
5530 case ISN_LOAD:
5531 {
5532 if (iptr->isn_arg.number < 0)
5533 smsg("%s%4d LOAD arg[%lld]", pfx, current,
5534 (varnumber_T)(iptr->isn_arg.number
5535 + STACK_FRAME_SIZE));
5536 else
5537 smsg("%s%4d LOAD $%lld", pfx, current,
5538 (varnumber_T)(iptr->isn_arg.number));
5539 }
5540 break;
5541 case ISN_LOADOUTER:
5542 {
5543 if (iptr->isn_arg.number < 0)
5544 smsg("%s%4d LOADOUTER level %d arg[%d]", pfx, current,
5545 iptr->isn_arg.outer.outer_depth,
5546 iptr->isn_arg.outer.outer_idx
5547 + STACK_FRAME_SIZE);
5548 else
5549 smsg("%s%4d LOADOUTER level %d $%d", pfx, current,
5550 iptr->isn_arg.outer.outer_depth,
5551 iptr->isn_arg.outer.outer_idx);
5552 }
5553 break;
5554 case ISN_LOADV:
5555 smsg("%s%4d LOADV v:%s", pfx, current,
5556 get_vim_var_name(iptr->isn_arg.number));
5557 break;
5558 case ISN_LOADSCRIPT:
5559 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005560 scriptref_T *sref = iptr->isn_arg.script.scriptref;
5561 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
5562 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005563
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005564 sv = get_script_svar(sref, -1);
5565 if (sv == NULL)
5566 smsg("%s%4d LOADSCRIPT [deleted] from %s",
5567 pfx, current, si->sn_name);
5568 else
5569 smsg("%s%4d LOADSCRIPT %s-%d from %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02005570 sv->sv_name,
5571 sref->sref_idx,
5572 si->sn_name);
5573 }
5574 break;
5575 case ISN_LOADS:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01005576 case ISN_LOADEXPORT:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005577 {
5578 scriptitem_T *si = SCRIPT_ITEM(
5579 iptr->isn_arg.loadstore.ls_sid);
5580
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01005581 smsg("%s%4d %s s:%s from %s", pfx, current,
5582 iptr->isn_type == ISN_LOADS ? "LOADS"
5583 : "LOADEXPORT",
5584 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005585 }
5586 break;
5587 case ISN_LOADAUTO:
5588 smsg("%s%4d LOADAUTO %s", pfx, current, iptr->isn_arg.string);
5589 break;
5590 case ISN_LOADG:
5591 smsg("%s%4d LOADG g:%s", pfx, current, iptr->isn_arg.string);
5592 break;
5593 case ISN_LOADB:
5594 smsg("%s%4d LOADB b:%s", pfx, current, iptr->isn_arg.string);
5595 break;
5596 case ISN_LOADW:
5597 smsg("%s%4d LOADW w:%s", pfx, current, iptr->isn_arg.string);
5598 break;
5599 case ISN_LOADT:
5600 smsg("%s%4d LOADT t:%s", pfx, current, iptr->isn_arg.string);
5601 break;
5602 case ISN_LOADGDICT:
5603 smsg("%s%4d LOAD g:", pfx, current);
5604 break;
5605 case ISN_LOADBDICT:
5606 smsg("%s%4d LOAD b:", pfx, current);
5607 break;
5608 case ISN_LOADWDICT:
5609 smsg("%s%4d LOAD w:", pfx, current);
5610 break;
5611 case ISN_LOADTDICT:
5612 smsg("%s%4d LOAD t:", pfx, current);
5613 break;
5614 case ISN_LOADOPT:
5615 smsg("%s%4d LOADOPT %s", pfx, current, iptr->isn_arg.string);
5616 break;
5617 case ISN_LOADENV:
5618 smsg("%s%4d LOADENV %s", pfx, current, iptr->isn_arg.string);
5619 break;
5620 case ISN_LOADREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005621 smsg("%s%4d LOADREG @%c", pfx, current,
5622 (int)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005623 break;
5624
5625 case ISN_STORE:
5626 if (iptr->isn_arg.number < 0)
5627 smsg("%s%4d STORE arg[%lld]", pfx, current,
5628 iptr->isn_arg.number + STACK_FRAME_SIZE);
5629 else
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005630 smsg("%s%4d STORE $%lld", pfx, current,
5631 iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005632 break;
5633 case ISN_STOREOUTER:
5634 {
5635 if (iptr->isn_arg.number < 0)
5636 smsg("%s%4d STOREOUTEr level %d arg[%d]", pfx, current,
5637 iptr->isn_arg.outer.outer_depth,
5638 iptr->isn_arg.outer.outer_idx + STACK_FRAME_SIZE);
5639 else
5640 smsg("%s%4d STOREOUTER level %d $%d", pfx, current,
5641 iptr->isn_arg.outer.outer_depth,
5642 iptr->isn_arg.outer.outer_idx);
5643 }
5644 break;
5645 case ISN_STOREV:
5646 smsg("%s%4d STOREV v:%s", pfx, current,
5647 get_vim_var_name(iptr->isn_arg.number));
5648 break;
5649 case ISN_STOREAUTO:
5650 smsg("%s%4d STOREAUTO %s", pfx, current, iptr->isn_arg.string);
5651 break;
5652 case ISN_STOREG:
5653 smsg("%s%4d STOREG %s", pfx, current, iptr->isn_arg.string);
5654 break;
5655 case ISN_STOREB:
5656 smsg("%s%4d STOREB %s", pfx, current, iptr->isn_arg.string);
5657 break;
5658 case ISN_STOREW:
5659 smsg("%s%4d STOREW %s", pfx, current, iptr->isn_arg.string);
5660 break;
5661 case ISN_STORET:
5662 smsg("%s%4d STORET %s", pfx, current, iptr->isn_arg.string);
5663 break;
5664 case ISN_STORES:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01005665 case ISN_STOREEXPORT:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005666 {
5667 scriptitem_T *si = SCRIPT_ITEM(
5668 iptr->isn_arg.loadstore.ls_sid);
5669
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01005670 smsg("%s%4d %s %s in %s", pfx, current,
5671 iptr->isn_type == ISN_STORES
5672 ? "STORES" : "STOREEXPORT",
Bram Moolenaar4c137212021-04-19 16:48:48 +02005673 iptr->isn_arg.loadstore.ls_name, si->sn_name);
5674 }
5675 break;
5676 case ISN_STORESCRIPT:
5677 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005678 scriptref_T *sref = iptr->isn_arg.script.scriptref;
5679 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
5680 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005681
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005682 sv = get_script_svar(sref, -1);
5683 if (sv == NULL)
5684 smsg("%s%4d STORESCRIPT [deleted] in %s",
5685 pfx, current, si->sn_name);
5686 else
5687 smsg("%s%4d STORESCRIPT %s-%d in %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02005688 sv->sv_name,
5689 sref->sref_idx,
5690 si->sn_name);
5691 }
5692 break;
5693 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00005694 case ISN_STOREFUNCOPT:
5695 smsg("%s%4d %s &%s", pfx, current,
5696 iptr->isn_type == ISN_STOREOPT ? "STOREOPT" : "STOREFUNCOPT",
Bram Moolenaar4c137212021-04-19 16:48:48 +02005697 iptr->isn_arg.storeopt.so_name);
5698 break;
5699 case ISN_STOREENV:
5700 smsg("%s%4d STOREENV $%s", pfx, current, iptr->isn_arg.string);
5701 break;
5702 case ISN_STOREREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005703 smsg("%s%4d STOREREG @%c", pfx, current,
5704 (int)iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005705 break;
5706 case ISN_STORENR:
5707 smsg("%s%4d STORE %lld in $%d", pfx, current,
5708 iptr->isn_arg.storenr.stnr_val,
5709 iptr->isn_arg.storenr.stnr_idx);
5710 break;
5711
5712 case ISN_STOREINDEX:
5713 smsg("%s%4d STOREINDEX %s", pfx, current,
5714 vartype_name(iptr->isn_arg.vartype));
5715 break;
5716
5717 case ISN_STORERANGE:
5718 smsg("%s%4d STORERANGE", pfx, current);
5719 break;
5720
5721 // constants
5722 case ISN_PUSHNR:
5723 smsg("%s%4d PUSHNR %lld", pfx, current,
5724 (varnumber_T)(iptr->isn_arg.number));
5725 break;
5726 case ISN_PUSHBOOL:
5727 case ISN_PUSHSPEC:
5728 smsg("%s%4d PUSH %s", pfx, current,
5729 get_var_special_name(iptr->isn_arg.number));
5730 break;
5731 case ISN_PUSHF:
5732#ifdef FEAT_FLOAT
5733 smsg("%s%4d PUSHF %g", pfx, current, iptr->isn_arg.fnumber);
5734#endif
5735 break;
5736 case ISN_PUSHS:
5737 smsg("%s%4d PUSHS \"%s\"", pfx, current, iptr->isn_arg.string);
5738 break;
5739 case ISN_PUSHBLOB:
5740 {
5741 char_u *r;
5742 char_u numbuf[NUMBUFLEN];
5743 char_u *tofree;
5744
5745 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
5746 smsg("%s%4d PUSHBLOB %s", pfx, current, r);
5747 vim_free(tofree);
5748 }
5749 break;
5750 case ISN_PUSHFUNC:
5751 {
5752 char *name = (char *)iptr->isn_arg.string;
5753
5754 smsg("%s%4d PUSHFUNC \"%s\"", pfx, current,
5755 name == NULL ? "[none]" : name);
5756 }
5757 break;
5758 case ISN_PUSHCHANNEL:
5759#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar397a87a2022-03-20 21:14:15 +00005760 smsg("%s%4d PUSHCHANNEL 0", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005761#endif
5762 break;
5763 case ISN_PUSHJOB:
5764#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar397a87a2022-03-20 21:14:15 +00005765 smsg("%s%4d PUSHJOB \"no process\"", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005766#endif
5767 break;
5768 case ISN_PUSHEXC:
5769 smsg("%s%4d PUSH v:exception", pfx, current);
5770 break;
Bram Moolenaar06b77222022-01-25 15:51:56 +00005771 case ISN_AUTOLOAD:
5772 smsg("%s%4d AUTOLOAD %s", pfx, current, iptr->isn_arg.string);
5773 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005774 case ISN_UNLET:
5775 smsg("%s%4d UNLET%s %s", pfx, current,
5776 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
5777 iptr->isn_arg.unlet.ul_name);
5778 break;
5779 case ISN_UNLETENV:
5780 smsg("%s%4d UNLETENV%s $%s", pfx, current,
5781 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
5782 iptr->isn_arg.unlet.ul_name);
5783 break;
5784 case ISN_UNLETINDEX:
5785 smsg("%s%4d UNLETINDEX", pfx, current);
5786 break;
5787 case ISN_UNLETRANGE:
5788 smsg("%s%4d UNLETRANGE", pfx, current);
5789 break;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02005790 case ISN_LOCKUNLOCK:
5791 smsg("%s%4d LOCKUNLOCK %s", pfx, current, iptr->isn_arg.string);
5792 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005793 case ISN_LOCKCONST:
5794 smsg("%s%4d LOCKCONST", pfx, current);
5795 break;
5796 case ISN_NEWLIST:
5797 smsg("%s%4d NEWLIST size %lld", pfx, current,
5798 (varnumber_T)(iptr->isn_arg.number));
5799 break;
5800 case ISN_NEWDICT:
5801 smsg("%s%4d NEWDICT size %lld", pfx, current,
5802 (varnumber_T)(iptr->isn_arg.number));
5803 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00005804 case ISN_NEWPARTIAL:
5805 smsg("%s%4d NEWPARTIAL", pfx, current);
5806 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005807
5808 // function call
5809 case ISN_BCALL:
5810 {
5811 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
5812
5813 smsg("%s%4d BCALL %s(argc %d)", pfx, current,
5814 internal_func_name(cbfunc->cbf_idx),
5815 cbfunc->cbf_argcount);
5816 }
5817 break;
5818 case ISN_DCALL:
5819 {
5820 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
5821 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
5822 + cdfunc->cdf_idx;
5823
5824 smsg("%s%4d DCALL %s(argc %d)", pfx, current,
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005825 printable_func_name(df->df_ufunc),
5826 cdfunc->cdf_argcount);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005827 }
5828 break;
5829 case ISN_UCALL:
5830 {
5831 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
5832
5833 smsg("%s%4d UCALL %s(argc %d)", pfx, current,
5834 cufunc->cuf_name, cufunc->cuf_argcount);
5835 }
5836 break;
5837 case ISN_PCALL:
5838 {
5839 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
5840
5841 smsg("%s%4d PCALL%s (argc %d)", pfx, current,
5842 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
5843 }
5844 break;
5845 case ISN_PCALL_END:
5846 smsg("%s%4d PCALL end", pfx, current);
5847 break;
5848 case ISN_RETURN:
5849 smsg("%s%4d RETURN", pfx, current);
5850 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02005851 case ISN_RETURN_VOID:
5852 smsg("%s%4d RETURN void", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005853 break;
5854 case ISN_FUNCREF:
5855 {
5856 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar38453522021-11-28 22:00:12 +00005857 char_u *name;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005858
Bram Moolenaar38453522021-11-28 22:00:12 +00005859 if (funcref->fr_func_name == NULL)
5860 {
5861 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
5862 + funcref->fr_dfunc_idx;
5863 name = df->df_ufunc->uf_name;
5864 }
5865 else
5866 name = funcref->fr_func_name;
5867 smsg("%s%4d FUNCREF %s", pfx, current, name);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005868 }
5869 break;
5870
5871 case ISN_NEWFUNC:
5872 {
5873 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
5874
5875 smsg("%s%4d NEWFUNC %s %s", pfx, current,
5876 newfunc->nf_lambda, newfunc->nf_global);
5877 }
5878 break;
5879
5880 case ISN_DEF:
5881 {
5882 char_u *name = iptr->isn_arg.string;
5883
5884 smsg("%s%4d DEF %s", pfx, current,
5885 name == NULL ? (char_u *)"" : name);
5886 }
5887 break;
5888
5889 case ISN_JUMP:
5890 {
5891 char *when = "?";
5892
5893 switch (iptr->isn_arg.jump.jump_when)
5894 {
5895 case JUMP_ALWAYS:
5896 when = "JUMP";
5897 break;
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005898 case JUMP_NEVER:
5899 iemsg("JUMP_NEVER should not be used");
5900 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005901 case JUMP_AND_KEEP_IF_TRUE:
5902 when = "JUMP_AND_KEEP_IF_TRUE";
5903 break;
5904 case JUMP_IF_FALSE:
5905 when = "JUMP_IF_FALSE";
5906 break;
5907 case JUMP_AND_KEEP_IF_FALSE:
5908 when = "JUMP_AND_KEEP_IF_FALSE";
5909 break;
5910 case JUMP_IF_COND_FALSE:
5911 when = "JUMP_IF_COND_FALSE";
5912 break;
5913 case JUMP_IF_COND_TRUE:
5914 when = "JUMP_IF_COND_TRUE";
5915 break;
5916 }
5917 smsg("%s%4d %s -> %d", pfx, current, when,
5918 iptr->isn_arg.jump.jump_where);
5919 }
5920 break;
5921
5922 case ISN_JUMP_IF_ARG_SET:
5923 smsg("%s%4d JUMP_IF_ARG_SET arg[%d] -> %d", pfx, current,
5924 iptr->isn_arg.jumparg.jump_arg_off + STACK_FRAME_SIZE,
5925 iptr->isn_arg.jump.jump_where);
5926 break;
5927
5928 case ISN_FOR:
5929 {
5930 forloop_T *forloop = &iptr->isn_arg.forloop;
5931
5932 smsg("%s%4d FOR $%d -> %d", pfx, current,
5933 forloop->for_idx, forloop->for_end);
5934 }
5935 break;
5936
5937 case ISN_TRY:
5938 {
Bram Moolenaar0d807102021-12-21 09:42:09 +00005939 try_T *try = &iptr->isn_arg.tryref;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005940
5941 if (try->try_ref->try_finally == 0)
5942 smsg("%s%4d TRY catch -> %d, endtry -> %d",
5943 pfx, current,
5944 try->try_ref->try_catch,
5945 try->try_ref->try_endtry);
5946 else
5947 smsg("%s%4d TRY catch -> %d, finally -> %d, endtry -> %d",
5948 pfx, current,
5949 try->try_ref->try_catch,
5950 try->try_ref->try_finally,
5951 try->try_ref->try_endtry);
5952 }
5953 break;
5954 case ISN_CATCH:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005955 smsg("%s%4d CATCH", pfx, current);
5956 break;
5957 case ISN_TRYCONT:
5958 {
5959 trycont_T *trycont = &iptr->isn_arg.trycont;
5960
5961 smsg("%s%4d TRY-CONTINUE %d level%s -> %d", pfx, current,
5962 trycont->tct_levels,
5963 trycont->tct_levels == 1 ? "" : "s",
5964 trycont->tct_where);
5965 }
5966 break;
5967 case ISN_FINALLY:
5968 smsg("%s%4d FINALLY", pfx, current);
5969 break;
5970 case ISN_ENDTRY:
5971 smsg("%s%4d ENDTRY", pfx, current);
5972 break;
5973 case ISN_THROW:
5974 smsg("%s%4d THROW", pfx, current);
5975 break;
5976
5977 // expression operations on number
5978 case ISN_OPNR:
5979 case ISN_OPFLOAT:
5980 case ISN_OPANY:
5981 {
5982 char *what;
5983 char *ins;
5984
5985 switch (iptr->isn_arg.op.op_type)
5986 {
5987 case EXPR_MULT: what = "*"; break;
5988 case EXPR_DIV: what = "/"; break;
5989 case EXPR_REM: what = "%"; break;
5990 case EXPR_SUB: what = "-"; break;
5991 case EXPR_ADD: what = "+"; break;
5992 default: what = "???"; break;
5993 }
5994 switch (iptr->isn_type)
5995 {
5996 case ISN_OPNR: ins = "OPNR"; break;
5997 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
5998 case ISN_OPANY: ins = "OPANY"; break;
5999 default: ins = "???"; break;
6000 }
6001 smsg("%s%4d %s %s", pfx, current, ins, what);
6002 }
6003 break;
6004
6005 case ISN_COMPAREBOOL:
6006 case ISN_COMPARESPECIAL:
Bram Moolenaar7a222242022-03-01 19:23:24 +00006007 case ISN_COMPARENULL:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006008 case ISN_COMPARENR:
6009 case ISN_COMPAREFLOAT:
6010 case ISN_COMPARESTRING:
6011 case ISN_COMPAREBLOB:
6012 case ISN_COMPARELIST:
6013 case ISN_COMPAREDICT:
6014 case ISN_COMPAREFUNC:
6015 case ISN_COMPAREANY:
6016 {
6017 char *p;
6018 char buf[10];
6019 char *type;
6020
6021 switch (iptr->isn_arg.op.op_type)
6022 {
6023 case EXPR_EQUAL: p = "=="; break;
6024 case EXPR_NEQUAL: p = "!="; break;
6025 case EXPR_GREATER: p = ">"; break;
6026 case EXPR_GEQUAL: p = ">="; break;
6027 case EXPR_SMALLER: p = "<"; break;
6028 case EXPR_SEQUAL: p = "<="; break;
6029 case EXPR_MATCH: p = "=~"; break;
6030 case EXPR_IS: p = "is"; break;
6031 case EXPR_ISNOT: p = "isnot"; break;
6032 case EXPR_NOMATCH: p = "!~"; break;
6033 default: p = "???"; break;
6034 }
6035 STRCPY(buf, p);
6036 if (iptr->isn_arg.op.op_ic == TRUE)
6037 strcat(buf, "?");
6038 switch(iptr->isn_type)
6039 {
6040 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
6041 case ISN_COMPARESPECIAL:
6042 type = "COMPARESPECIAL"; break;
Bram Moolenaar7a222242022-03-01 19:23:24 +00006043 case ISN_COMPARENULL: type = "COMPARENULL"; break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006044 case ISN_COMPARENR: type = "COMPARENR"; break;
6045 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
6046 case ISN_COMPARESTRING:
6047 type = "COMPARESTRING"; break;
6048 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
6049 case ISN_COMPARELIST: type = "COMPARELIST"; break;
6050 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
6051 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
6052 case ISN_COMPAREANY: type = "COMPAREANY"; break;
6053 default: type = "???"; break;
6054 }
6055
6056 smsg("%s%4d %s %s", pfx, current, type, buf);
6057 }
6058 break;
6059
6060 case ISN_ADDLIST: smsg("%s%4d ADDLIST", pfx, current); break;
6061 case ISN_ADDBLOB: smsg("%s%4d ADDBLOB", pfx, current); break;
6062
6063 // expression operations
6064 case ISN_CONCAT: smsg("%s%4d CONCAT", pfx, current); break;
6065 case ISN_STRINDEX: smsg("%s%4d STRINDEX", pfx, current); break;
6066 case ISN_STRSLICE: smsg("%s%4d STRSLICE", pfx, current); break;
6067 case ISN_BLOBINDEX: smsg("%s%4d BLOBINDEX", pfx, current); break;
6068 case ISN_BLOBSLICE: smsg("%s%4d BLOBSLICE", pfx, current); break;
6069 case ISN_LISTAPPEND: smsg("%s%4d LISTAPPEND", pfx, current); break;
6070 case ISN_BLOBAPPEND: smsg("%s%4d BLOBAPPEND", pfx, current); break;
6071 case ISN_LISTINDEX: smsg("%s%4d LISTINDEX", pfx, current); break;
6072 case ISN_LISTSLICE: smsg("%s%4d LISTSLICE", pfx, current); break;
6073 case ISN_ANYINDEX: smsg("%s%4d ANYINDEX", pfx, current); break;
6074 case ISN_ANYSLICE: smsg("%s%4d ANYSLICE", pfx, current); break;
6075 case ISN_SLICE: smsg("%s%4d SLICE %lld",
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02006076 pfx, current, iptr->isn_arg.number); break;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02006077 case ISN_GETITEM: smsg("%s%4d ITEM %lld%s", pfx, current,
6078 iptr->isn_arg.getitem.gi_index,
6079 iptr->isn_arg.getitem.gi_with_op ?
6080 " with op" : ""); break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006081 case ISN_MEMBER: smsg("%s%4d MEMBER", pfx, current); break;
6082 case ISN_STRINGMEMBER: smsg("%s%4d MEMBER %s", pfx, current,
6083 iptr->isn_arg.string); break;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02006084 case ISN_CLEARDICT: smsg("%s%4d CLEARDICT", pfx, current); break;
6085 case ISN_USEDICT: smsg("%s%4d USEDICT", pfx, current); break;
6086
Bram Moolenaar4c137212021-04-19 16:48:48 +02006087 case ISN_NEGATENR: smsg("%s%4d NEGATENR", pfx, current); break;
6088
6089 case ISN_CHECKNR: smsg("%s%4d CHECKNR", pfx, current); break;
6090 case ISN_CHECKTYPE:
6091 {
6092 checktype_T *ct = &iptr->isn_arg.type;
6093 char *tofree;
6094
6095 if (ct->ct_arg_idx == 0)
6096 smsg("%s%4d CHECKTYPE %s stack[%d]", pfx, current,
6097 type_name(ct->ct_type, &tofree),
6098 (int)ct->ct_off);
6099 else
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02006100 smsg("%s%4d CHECKTYPE %s stack[%d] arg %d",
6101 pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02006102 type_name(ct->ct_type, &tofree),
6103 (int)ct->ct_off,
6104 (int)ct->ct_arg_idx);
6105 vim_free(tofree);
6106 break;
6107 }
6108 case ISN_CHECKLEN: smsg("%s%4d CHECKLEN %s%d", pfx, current,
6109 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
6110 iptr->isn_arg.checklen.cl_min_len);
6111 break;
6112 case ISN_SETTYPE:
6113 {
6114 char *tofree;
6115
6116 smsg("%s%4d SETTYPE %s", pfx, current,
6117 type_name(iptr->isn_arg.type.ct_type, &tofree));
6118 vim_free(tofree);
6119 break;
6120 }
6121 case ISN_COND2BOOL: smsg("%s%4d COND2BOOL", pfx, current); break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006122 case ISN_2BOOL: if (iptr->isn_arg.tobool.invert)
6123 smsg("%s%4d INVERT %d (!val)", pfx, current,
6124 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006125 else
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006126 smsg("%s%4d 2BOOL %d (!!val)", pfx, current,
6127 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006128 break;
6129 case ISN_2STRING: smsg("%s%4d 2STRING stack[%lld]", pfx, current,
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006130 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006131 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006132 case ISN_2STRING_ANY: smsg("%s%4d 2STRING_ANY stack[%lld]",
6133 pfx, current,
6134 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006135 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006136 case ISN_RANGE: smsg("%s%4d RANGE %s", pfx, current,
6137 iptr->isn_arg.string);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006138 break;
6139 case ISN_PUT:
6140 if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE)
6141 smsg("%s%4d PUT %c above range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006142 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006143 else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE)
6144 smsg("%s%4d PUT %c range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006145 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006146 else
6147 smsg("%s%4d PUT %c %ld", pfx, current,
6148 iptr->isn_arg.put.put_regname,
6149 (long)iptr->isn_arg.put.put_lnum);
6150 break;
6151
Bram Moolenaar4c137212021-04-19 16:48:48 +02006152 case ISN_CMDMOD:
6153 {
6154 char_u *buf;
6155 size_t len = produce_cmdmods(
6156 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
6157
6158 buf = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02006159 if (likely(buf != NULL))
Bram Moolenaar4c137212021-04-19 16:48:48 +02006160 {
6161 (void)produce_cmdmods(
6162 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
6163 smsg("%s%4d CMDMOD %s", pfx, current, buf);
6164 vim_free(buf);
6165 }
6166 break;
6167 }
6168 case ISN_CMDMOD_REV: smsg("%s%4d CMDMOD_REV", pfx, current); break;
6169
6170 case ISN_PROF_START:
Bram Moolenaare99d4222021-06-13 14:01:26 +02006171 smsg("%s%4d PROFILE START line %d", pfx, current,
6172 iptr->isn_lnum);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006173 break;
6174
6175 case ISN_PROF_END:
6176 smsg("%s%4d PROFILE END", pfx, current);
6177 break;
6178
Bram Moolenaare99d4222021-06-13 14:01:26 +02006179 case ISN_DEBUG:
Bram Moolenaar8cec9272021-06-23 20:20:53 +02006180 smsg("%s%4d DEBUG line %d-%d varcount %lld", pfx, current,
6181 iptr->isn_arg.debug.dbg_break_lnum + 1,
6182 iptr->isn_lnum,
6183 iptr->isn_arg.debug.dbg_var_names_len);
Bram Moolenaare99d4222021-06-13 14:01:26 +02006184 break;
6185
Bram Moolenaar4c137212021-04-19 16:48:48 +02006186 case ISN_UNPACK: smsg("%s%4d UNPACK %d%s", pfx, current,
6187 iptr->isn_arg.unpack.unp_count,
6188 iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : "");
6189 break;
6190 case ISN_SHUFFLE: smsg("%s%4d SHUFFLE %d up %d", pfx, current,
6191 iptr->isn_arg.shuffle.shfl_item,
6192 iptr->isn_arg.shuffle.shfl_up);
6193 break;
6194 case ISN_DROP: smsg("%s%4d DROP", pfx, current); break;
6195
6196 case ISN_FINISH: // End of list of instructions for ISN_SUBSTITUTE.
6197 return;
6198 }
6199
6200 out_flush(); // output one line at a time
6201 ui_breakcheck();
6202 if (got_int)
6203 break;
6204 }
6205}
6206
6207/*
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02006208 * Handle command line completion for the :disassemble command.
6209 */
6210 void
6211set_context_in_disassemble_cmd(expand_T *xp, char_u *arg)
6212{
6213 char_u *p;
6214
6215 // Default: expand user functions, "debug" and "profile"
6216 xp->xp_context = EXPAND_DISASSEMBLE;
6217 xp->xp_pattern = arg;
6218
6219 // first argument already typed: only user function names
6220 if (*arg != NUL && *(p = skiptowhite(arg)) != NUL)
6221 {
6222 xp->xp_context = EXPAND_USER_FUNC;
6223 xp->xp_pattern = skipwhite(p);
6224 }
6225}
6226
6227/*
6228 * Function given to ExpandGeneric() to obtain the list of :disassemble
6229 * arguments.
6230 */
6231 char_u *
6232get_disassemble_argument(expand_T *xp, int idx)
6233{
6234 if (idx == 0)
6235 return (char_u *)"debug";
6236 if (idx == 1)
6237 return (char_u *)"profile";
6238 return get_user_func_name(xp, idx - 2);
6239}
6240
6241/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01006242 * ":disassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01006243 * We don't really need this at runtime, but we do have tests that require it,
6244 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006245 */
6246 void
6247ex_disassemble(exarg_T *eap)
6248{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01006249 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01006250 char_u *fname;
6251 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006252 dfunc_T *dfunc;
6253 isn_T *instr;
Bram Moolenaarb2049902021-01-24 12:53:53 +01006254 int instr_count;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02006255 int is_global = FALSE;
Bram Moolenaare99d4222021-06-13 14:01:26 +02006256 compiletype_T compile_type = CT_NONE;
6257
Bram Moolenaarc7d5fc82021-12-05 17:20:24 +00006258 if (STRNCMP(arg, "profile", 7) == 0 && VIM_ISWHITE(arg[7]))
Bram Moolenaare99d4222021-06-13 14:01:26 +02006259 {
6260 compile_type = CT_PROFILE;
6261 arg = skipwhite(arg + 7);
6262 }
Bram Moolenaarc7d5fc82021-12-05 17:20:24 +00006263 else if (STRNCMP(arg, "debug", 5) == 0 && VIM_ISWHITE(arg[5]))
Bram Moolenaare99d4222021-06-13 14:01:26 +02006264 {
6265 compile_type = CT_DEBUG;
6266 arg = skipwhite(arg + 5);
6267 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006268
Bram Moolenaarbfd65582020-07-13 18:18:00 +02006269 if (STRNCMP(arg, "<lambda>", 8) == 0)
6270 {
6271 arg += 8;
6272 (void)getdigits(&arg);
6273 fname = vim_strnsave(eap->arg, arg - eap->arg);
6274 }
6275 else
6276 fname = trans_function_name(&arg, &is_global, FALSE,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01006277 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL, NULL);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01006278 if (fname == NULL)
6279 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00006280 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01006281 return;
6282 }
6283
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00006284 ufunc = find_func(fname, is_global);
Bram Moolenaara26b9702020-04-18 19:53:28 +02006285 if (ufunc == NULL)
6286 {
6287 char_u *p = untrans_function_name(fname);
6288
6289 if (p != NULL)
6290 // Try again without making it script-local.
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00006291 ufunc = find_func(p, FALSE);
Bram Moolenaara26b9702020-04-18 19:53:28 +02006292 }
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01006293 vim_free(fname);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006294 if (ufunc == NULL)
6295 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006296 semsg(_(e_cannot_find_function_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006297 return;
6298 }
Bram Moolenaare99d4222021-06-13 14:01:26 +02006299 if (func_needs_compiling(ufunc, compile_type)
6300 && compile_def_function(ufunc, FALSE, compile_type, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02006301 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006302 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006303 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006304 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006305 return;
6306 }
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006307 msg((char *)printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006308
6309 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
Bram Moolenaare99d4222021-06-13 14:01:26 +02006310 switch (compile_type)
6311 {
6312 case CT_PROFILE:
Bram Moolenaarf002a412021-01-24 13:34:18 +01006313#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02006314 instr = dfunc->df_instr_prof;
6315 instr_count = dfunc->df_instr_prof_count;
6316 break;
Bram Moolenaarf002a412021-01-24 13:34:18 +01006317#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02006318 // FALLTHROUGH
6319 case CT_NONE:
6320 instr = dfunc->df_instr;
6321 instr_count = dfunc->df_instr_count;
6322 break;
6323 case CT_DEBUG:
6324 instr = dfunc->df_instr_debug;
6325 instr_count = dfunc->df_instr_debug_count;
6326 break;
6327 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006328
Bram Moolenaar4c137212021-04-19 16:48:48 +02006329 list_instructions("", instr, instr_count, ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006330}
6331
6332/*
Bram Moolenaar13106602020-10-04 16:06:05 +02006333 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006334 * list, etc. Mostly like what JavaScript does, except that empty list and
6335 * empty dictionary are FALSE.
6336 */
6337 int
6338tv2bool(typval_T *tv)
6339{
6340 switch (tv->v_type)
6341 {
6342 case VAR_NUMBER:
6343 return tv->vval.v_number != 0;
6344 case VAR_FLOAT:
6345#ifdef FEAT_FLOAT
6346 return tv->vval.v_float != 0.0;
6347#else
6348 break;
6349#endif
6350 case VAR_PARTIAL:
6351 return tv->vval.v_partial != NULL;
6352 case VAR_FUNC:
6353 case VAR_STRING:
6354 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
6355 case VAR_LIST:
6356 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
6357 case VAR_DICT:
6358 return tv->vval.v_dict != NULL
6359 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
6360 case VAR_BOOL:
6361 case VAR_SPECIAL:
6362 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
6363 case VAR_JOB:
6364#ifdef FEAT_JOB_CHANNEL
6365 return tv->vval.v_job != NULL;
6366#else
6367 break;
6368#endif
6369 case VAR_CHANNEL:
6370#ifdef FEAT_JOB_CHANNEL
6371 return tv->vval.v_channel != NULL;
6372#else
6373 break;
6374#endif
6375 case VAR_BLOB:
6376 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
6377 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006378 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006379 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006380 case VAR_INSTR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006381 break;
6382 }
6383 return FALSE;
6384}
6385
Bram Moolenaarea2d4072020-11-12 12:08:51 +01006386 void
6387emsg_using_string_as(typval_T *tv, int as_number)
6388{
6389 semsg(_(as_number ? e_using_string_as_number_str
6390 : e_using_string_as_bool_str),
6391 tv->vval.v_string == NULL
6392 ? (char_u *)"" : tv->vval.v_string);
6393}
6394
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006395/*
6396 * If "tv" is a string give an error and return FAIL.
6397 */
6398 int
6399check_not_string(typval_T *tv)
6400{
6401 if (tv->v_type == VAR_STRING)
6402 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +01006403 emsg_using_string_as(tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006404 clear_tv(tv);
6405 return FAIL;
6406 }
6407 return OK;
6408}
6409
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006410
6411#endif // FEAT_EVAL