blob: 06229b884a35e91f5967d38b83095d27dc2ddef6 [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
Bram Moolenaar6ed545e2022-05-09 20:09:23 +010067// 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)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010074
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 Moolenaar1d84f762022-09-03 21:35:53 +0100104// Get pointer to item in the stack.
105#define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
106
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100107// Get pointer to item relative to the bottom of the stack, -1 is the last one.
Bram Moolenaar11107ba2020-08-15 21:10:16 +0200108#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 +0100109
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100110// Get pointer to a local variable on the stack. Negative for arguments.
111#define STACK_TV_VAR(idx) (((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx + STACK_FRAME_SIZE + idx)
112
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200113 void
114to_string_error(vartype_T vartype)
115{
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200116 semsg(_(e_cannot_convert_str_to_string), vartype_name(vartype));
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200117}
118
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100119/*
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100120 * Return the number of arguments, including optional arguments and any vararg.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100121 */
122 static int
123ufunc_argcount(ufunc_T *ufunc)
124{
125 return ufunc->uf_args.ga_len + (ufunc->uf_va_name != NULL ? 1 : 0);
126}
127
128/*
LemonBoy372bcce2022-04-25 12:43:20 +0100129 * Create a new string from "count" items at the bottom of the stack.
130 * A trailing NUL is appended.
131 * When "count" is zero an empty string is added to the stack.
132 */
133 static int
134exe_concat(int count, ectx_T *ectx)
135{
136 int idx;
137 int len = 0;
138 typval_T *tv;
139 garray_T ga;
140
141 ga_init2(&ga, sizeof(char), 1);
142 // Preallocate enough space for the whole string to avoid having to grow
143 // and copy.
144 for (idx = 0; idx < count; ++idx)
145 {
146 tv = STACK_TV_BOT(idx - count);
147 if (tv->vval.v_string != NULL)
148 len += (int)STRLEN(tv->vval.v_string);
149 }
150
151 if (ga_grow(&ga, len + 1) == FAIL)
152 return FAIL;
153
154 for (idx = 0; idx < count; ++idx)
155 {
156 tv = STACK_TV_BOT(idx - count);
157 ga_concat(&ga, tv->vval.v_string);
158 clear_tv(tv);
159 }
160
161 // add a terminating NUL
162 ga_append(&ga, NUL);
163
164 ectx->ec_stack.ga_len -= count - 1;
165 STACK_TV_BOT(-1)->vval.v_string = ga.ga_data;
166
167 return OK;
168}
169
170/*
Bram Moolenaarfe270812020-04-11 22:31:27 +0200171 * Create a new list from "count" items at the bottom of the stack.
172 * When "count" is zero an empty list is added to the stack.
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100173 * When "count" is -1 a NULL list is added to the stack.
Bram Moolenaarfe270812020-04-11 22:31:27 +0200174 */
175 static int
176exe_newlist(int count, ectx_T *ectx)
177{
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100178 list_T *list = NULL;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200179 int idx;
180 typval_T *tv;
181
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100182 if (count >= 0)
183 {
184 list = list_alloc_with_items(count);
185 if (list == NULL)
186 return FAIL;
187 for (idx = 0; idx < count; ++idx)
188 list_set_item(list, idx, STACK_TV_BOT(idx - count));
189 }
Bram Moolenaarfe270812020-04-11 22:31:27 +0200190
191 if (count > 0)
192 ectx->ec_stack.ga_len -= count - 1;
Bram Moolenaar35578162021-08-02 19:10:38 +0200193 else if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100194 {
195 list_unref(list);
Bram Moolenaarfe270812020-04-11 22:31:27 +0200196 return FAIL;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100197 }
Bram Moolenaarfe270812020-04-11 22:31:27 +0200198 else
199 ++ectx->ec_stack.ga_len;
200 tv = STACK_TV_BOT(-1);
201 tv->v_type = VAR_LIST;
202 tv->vval.v_list = list;
Bram Moolenaar566badc2022-09-18 13:46:08 +0100203 tv->v_lock = 0;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100204 if (list != NULL)
205 ++list->lv_refcount;
206 return OK;
207}
208
209/*
210 * Implementation of ISN_NEWDICT.
211 * Returns FAIL on total failure, MAYBE on error.
212 */
213 static int
214exe_newdict(int count, ectx_T *ectx)
215{
216 dict_T *dict = NULL;
217 dictitem_T *item;
218 char_u *key;
219 int idx;
220 typval_T *tv;
221
222 if (count >= 0)
223 {
224 dict = dict_alloc();
225 if (unlikely(dict == NULL))
226 return FAIL;
227 for (idx = 0; idx < count; ++idx)
228 {
229 // have already checked key type is VAR_STRING
230 tv = STACK_TV_BOT(2 * (idx - count));
231 // check key is unique
232 key = tv->vval.v_string == NULL
233 ? (char_u *)"" : tv->vval.v_string;
234 item = dict_find(dict, key, -1);
235 if (item != NULL)
236 {
dundargocc57b5bc2022-11-02 13:30:51 +0000237 semsg(_(e_duplicate_key_in_dictionary), key);
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100238 dict_unref(dict);
239 return MAYBE;
240 }
241 item = dictitem_alloc(key);
242 clear_tv(tv);
243 if (unlikely(item == NULL))
244 {
245 dict_unref(dict);
246 return FAIL;
247 }
Bram Moolenaar0d1f55c2022-04-05 17:30:29 +0100248 tv = STACK_TV_BOT(2 * (idx - count) + 1);
249 item->di_tv = *tv;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100250 item->di_tv.v_lock = 0;
Bram Moolenaar0d1f55c2022-04-05 17:30:29 +0100251 tv->v_type = VAR_UNKNOWN;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +0100252 if (dict_add(dict, item) == FAIL)
253 {
254 // can this ever happen?
255 dict_unref(dict);
256 return FAIL;
257 }
258 }
259 }
260
261 if (count > 0)
262 ectx->ec_stack.ga_len -= 2 * count - 1;
263 else if (GA_GROW_FAILS(&ectx->ec_stack, 1))
264 return FAIL;
265 else
266 ++ectx->ec_stack.ga_len;
267 tv = STACK_TV_BOT(-1);
268 tv->v_type = VAR_DICT;
269 tv->v_lock = 0;
270 tv->vval.v_dict = dict;
271 if (dict != NULL)
272 ++dict->dv_refcount;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200273 return OK;
274}
275
276/*
Bram Moolenaar4f8f5422021-06-20 19:28:14 +0200277 * If debug_tick changed check if "ufunc" has a breakpoint and update
278 * "uf_has_breakpoint".
279 */
Bram Moolenaar96923b72022-03-15 15:57:04 +0000280 void
Bram Moolenaar4f8f5422021-06-20 19:28:14 +0200281update_has_breakpoint(ufunc_T *ufunc)
282{
283 if (ufunc->uf_debug_tick != debug_tick)
284 {
285 linenr_T breakpoint;
286
287 ufunc->uf_debug_tick = debug_tick;
288 breakpoint = dbg_find_breakpoint(FALSE, ufunc->uf_name, 0);
289 ufunc->uf_has_breakpoint = breakpoint > 0;
290 }
291}
292
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +0200293static garray_T dict_stack = GA_EMPTY;
294
295/*
296 * Put a value on the dict stack. This consumes "tv".
297 */
298 static int
299dict_stack_save(typval_T *tv)
300{
301 if (dict_stack.ga_growsize == 0)
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000302 ga_init2(&dict_stack, sizeof(typval_T), 10);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +0200303 if (ga_grow(&dict_stack, 1) == FAIL)
304 return FAIL;
305 ((typval_T *)dict_stack.ga_data)[dict_stack.ga_len] = *tv;
306 ++dict_stack.ga_len;
307 return OK;
308}
309
310/*
311 * Get the typval at top of the dict stack.
312 */
313 static typval_T *
314dict_stack_get_tv(void)
315{
316 if (dict_stack.ga_len == 0)
317 return NULL;
318 return ((typval_T *)dict_stack.ga_data) + dict_stack.ga_len - 1;
319}
320
321/*
322 * Get the dict at top of the dict stack.
323 */
324 static dict_T *
325dict_stack_get_dict(void)
326{
327 typval_T *tv;
328
329 if (dict_stack.ga_len == 0)
330 return NULL;
331 tv = ((typval_T *)dict_stack.ga_data) + dict_stack.ga_len - 1;
332 if (tv->v_type == VAR_DICT)
333 return tv->vval.v_dict;
334 return NULL;
335}
336
337/*
338 * Drop an item from the dict stack.
339 */
340 static void
341dict_stack_drop(void)
342{
343 if (dict_stack.ga_len == 0)
344 {
345 iemsg("Dict stack underflow");
346 return;
347 }
348 --dict_stack.ga_len;
349 clear_tv(((typval_T *)dict_stack.ga_data) + dict_stack.ga_len);
350}
351
352/*
353 * Drop items from the dict stack until the length is equal to "len".
354 */
355 static void
356dict_stack_clear(int len)
357{
358 while (dict_stack.ga_len > len)
359 dict_stack_drop();
360}
361
Bram Moolenaar4f8f5422021-06-20 19:28:14 +0200362/*
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +0000363 * Get a pointer to useful "pt_outer" of "pt".
364 */
365 static outer_T *
366get_pt_outer(partial_T *pt)
367{
368 partial_T *ptref = pt->pt_outer_partial;
369
370 if (ptref == NULL)
371 return &pt->pt_outer;
372
373 // partial using partial (recursively)
374 while (ptref->pt_outer_partial != NULL)
375 ptref = ptref->pt_outer_partial;
376 return &ptref->pt_outer;
377}
378
379/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100380 * Call compiled function "cdf_idx" from compiled code.
Bram Moolenaarab360522021-01-10 14:02:28 +0100381 * This adds a stack frame and sets the instruction pointer to the start of the
382 * called function.
Bram Moolenaar5800c792022-09-22 17:34:01 +0100383 * If "pt_arg" is not NULL use "pt_arg->pt_outer" for ec_outer_ref->or_outer.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100384 *
385 * Stack has:
386 * - current arguments (already there)
387 * - omitted optional argument (default values) added here
388 * - stack frame:
389 * - pointer to calling function
390 * - Index of next instruction in calling function
391 * - previous frame pointer
392 * - reserved space for local variables
393 */
394 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100395call_dfunc(
396 int cdf_idx,
Bram Moolenaar5800c792022-09-22 17:34:01 +0100397 partial_T *pt_arg,
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100398 int argcount_arg,
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100399 ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100400{
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100401 int argcount = argcount_arg;
402 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + cdf_idx;
403 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200404 int did_emsg_before = did_emsg_cumul + did_emsg;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100405 int arg_to_add;
406 int vararg_count = 0;
407 int varcount;
408 int idx;
409 estack_T *entry;
410 funclocal_T *floc = NULL;
Bram Moolenaar648594e2021-07-11 17:55:01 +0200411 int res = OK;
Bram Moolenaar139575d2022-03-15 19:29:30 +0000412 compiletype_T compile_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100413
414 if (dfunc->df_deleted)
415 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100416 // don't use ufunc->uf_name, it may have been freed
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000417 emsg_funcname(e_function_was_deleted_str,
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100418 dfunc->df_name == NULL ? (char_u *)"unknown" : dfunc->df_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100419 return FAIL;
420 }
421
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100422#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +0100423 if (do_profiling == PROF_YES)
424 {
Bram Moolenaar35578162021-08-02 19:10:38 +0200425 if (GA_GROW_OK(&profile_info_ga, 1))
Bram Moolenaar12d26532021-02-19 19:13:21 +0100426 {
427 profinfo_T *info = ((profinfo_T *)profile_info_ga.ga_data)
428 + profile_info_ga.ga_len;
429 ++profile_info_ga.ga_len;
430 CLEAR_POINTER(info);
431 profile_may_start_func(info, ufunc,
432 (((dfunc_T *)def_functions.ga_data)
433 + ectx->ec_dfunc_idx)->df_ufunc);
434 }
Bram Moolenaar12d26532021-02-19 19:13:21 +0100435 }
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100436#endif
437
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200438 // When debugging and using "cont" switches to the not-debugged
439 // instructions, may need to still compile them.
Bram Moolenaar139575d2022-03-15 19:29:30 +0000440 compile_type = get_compile_type(ufunc);
441 if (func_needs_compiling(ufunc, compile_type))
Bram Moolenaar648594e2021-07-11 17:55:01 +0200442 {
Bram Moolenaar139575d2022-03-15 19:29:30 +0000443 res = compile_def_function(ufunc, FALSE, compile_type, NULL);
Bram Moolenaar648594e2021-07-11 17:55:01 +0200444
445 // compile_def_function() may cause def_functions.ga_data to change
446 dfunc = ((dfunc_T *)def_functions.ga_data) + cdf_idx;
447 }
448 if (res == FAIL || INSTRUCTIONS(dfunc) == NULL)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200449 {
450 if (did_emsg_cumul + did_emsg == did_emsg_before)
451 semsg(_(e_function_is_not_compiled_str),
452 printable_func_name(ufunc));
453 return FAIL;
454 }
455
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200456 if (ufunc->uf_va_name != NULL)
457 {
Bram Moolenaarfe270812020-04-11 22:31:27 +0200458 // Need to make a list out of the vararg arguments.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200459 // Stack at time of call with 2 varargs:
460 // normal_arg
461 // optional_arg
462 // vararg_1
463 // vararg_2
Bram Moolenaarfe270812020-04-11 22:31:27 +0200464 // After creating the list:
465 // normal_arg
466 // optional_arg
467 // vararg-list
468 // With missing optional arguments we get:
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200469 // normal_arg
Bram Moolenaarfe270812020-04-11 22:31:27 +0200470 // After creating the list
471 // normal_arg
472 // (space for optional_arg)
473 // vararg-list
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200474 vararg_count = argcount - ufunc->uf_args.ga_len;
475 if (vararg_count < 0)
476 vararg_count = 0;
477 else
478 argcount -= vararg_count;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200479 if (exe_newlist(vararg_count, ectx) == FAIL)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200480 return FAIL;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200481
482 vararg_count = 1;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200483 }
484
Bram Moolenaarfe270812020-04-11 22:31:27 +0200485 arg_to_add = ufunc->uf_args.ga_len - argcount;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200486 if (arg_to_add < 0)
487 {
Matvey Tarasovd14bb1a2022-06-29 13:18:27 +0100488 semsg(NGETTEXT(e_one_argument_too_many, e_nr_arguments_too_many,
489 -arg_to_add), -arg_to_add);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200490 return FAIL;
491 }
Bram Moolenaarcd1cda22022-02-16 21:48:25 +0000492 else if (arg_to_add > ufunc->uf_def_args.ga_len)
493 {
494 int missing = arg_to_add - ufunc->uf_def_args.ga_len;
495
Matvey Tarasovd14bb1a2022-06-29 13:18:27 +0100496 semsg(NGETTEXT(e_one_argument_too_few, e_nr_arguments_too_few,
497 missing), missing);
Bram Moolenaarcd1cda22022-02-16 21:48:25 +0000498 return FAIL;
499 }
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200500
501 // Reserve space for:
502 // - missing arguments
503 // - stack frame
504 // - local variables
505 // - if needed: a counter for number of closures created in
506 // ectx->ec_funcrefs.
507 varcount = dfunc->df_varcount + dfunc->df_has_closure;
Bram Moolenaarb46c0832022-09-15 17:19:37 +0100508 if (GA_GROW_FAILS(&ectx->ec_stack,
509 arg_to_add + STACK_FRAME_SIZE + varcount))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100510 return FAIL;
511
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100512 // If depth of calling is getting too high, don't execute the function.
513 if (funcdepth_increment() == FAIL)
514 return FAIL;
Bram Moolenaare99d4222021-06-13 14:01:26 +0200515 ++ex_nesting_level;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100516
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100517 // Only make a copy of funclocal if it contains something to restore.
Bram Moolenaar4c137212021-04-19 16:48:48 +0200518 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100519 {
520 floc = ALLOC_ONE(funclocal_T);
521 if (floc == NULL)
522 return FAIL;
Bram Moolenaar4c137212021-04-19 16:48:48 +0200523 *floc = ectx->ec_funclocal;
524 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100525 }
526
Bram Moolenaarfe270812020-04-11 22:31:27 +0200527 // Move the vararg-list to below the missing optional arguments.
528 if (vararg_count > 0 && arg_to_add > 0)
529 *STACK_TV_BOT(arg_to_add - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100530
531 // Reserve space for omitted optional arguments, filled in soon.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200532 for (idx = 0; idx < arg_to_add; ++idx)
Bram Moolenaarfe270812020-04-11 22:31:27 +0200533 STACK_TV_BOT(idx - vararg_count)->v_type = VAR_UNKNOWN;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200534 ectx->ec_stack.ga_len += arg_to_add;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100535
536 // Store current execution state in stack frame for ISN_RETURN.
Bram Moolenaar0186e582021-01-10 18:33:11 +0100537 STACK_TV_BOT(STACK_FRAME_FUNC_OFF)->vval.v_number = ectx->ec_dfunc_idx;
538 STACK_TV_BOT(STACK_FRAME_IIDX_OFF)->vval.v_number = ectx->ec_iidx;
Bram Moolenaar5930ddc2021-04-26 20:32:59 +0200539 STACK_TV_BOT(STACK_FRAME_INSTR_OFF)->vval.v_string = (void *)ectx->ec_instr;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200540 STACK_TV_BOT(STACK_FRAME_OUTER_OFF)->vval.v_string =
541 (void *)ectx->ec_outer_ref;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100542 STACK_TV_BOT(STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string = (void *)floc;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100543 STACK_TV_BOT(STACK_FRAME_IDX_OFF)->vval.v_number = ectx->ec_frame_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200544 ectx->ec_frame_idx = ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100545
Bram Moolenaar5800c792022-09-22 17:34:01 +0100546 // Initialize all local variables to number zero. Also initialize the
547 // variable that counts how many closures were created. This is used in
548 // handle_closure_in_use().
549 int initcount = dfunc->df_varcount + (dfunc->df_has_closure ? 1 : 0);
550 for (idx = 0; idx < initcount; ++idx)
Bram Moolenaar5cd64792021-12-25 18:23:24 +0000551 {
552 typval_T *tv = STACK_TV_BOT(STACK_FRAME_SIZE + idx);
553
554 tv->v_type = VAR_NUMBER;
555 tv->vval.v_number = 0;
556 }
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200557 ectx->ec_stack.ga_len += STACK_FRAME_SIZE + varcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100558
Bram Moolenaar5800c792022-09-22 17:34:01 +0100559 partial_T *pt = pt_arg != NULL ? pt_arg : ufunc->uf_partial;
560 if (pt != NULL || (ufunc->uf_flags & FC_CLOSURE))
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100561 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200562 outer_ref_T *ref = ALLOC_CLEAR_ONE(outer_ref_T);
Bram Moolenaar0186e582021-01-10 18:33:11 +0100563
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200564 if (ref == NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +0100565 return FAIL;
566 if (pt != NULL)
567 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +0000568 ref->or_outer = get_pt_outer(pt);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200569 ++pt->pt_refcount;
570 ref->or_partial = pt;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100571 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100572 else
573 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200574 ref->or_outer = ALLOC_CLEAR_ONE(outer_T);
Dominique Pelle5a9e5842021-07-24 19:32:12 +0200575 if (unlikely(ref->or_outer == NULL))
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200576 {
577 vim_free(ref);
578 return FAIL;
579 }
580 ref->or_outer_allocated = TRUE;
581 ref->or_outer->out_stack = &ectx->ec_stack;
582 ref->or_outer->out_frame_idx = ectx->ec_frame_idx;
583 if (ectx->ec_outer_ref != NULL)
584 ref->or_outer->out_up = ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100585 }
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200586 ectx->ec_outer_ref = ref;
Bram Moolenaarab360522021-01-10 14:02:28 +0100587 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100588 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200589 ectx->ec_outer_ref = NULL;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100590
Bram Moolenaarc970e422021-03-17 15:03:04 +0100591 ++ufunc->uf_calls;
592
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100593 // Set execution state to the start of the called function.
594 ectx->ec_dfunc_idx = cdf_idx;
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100595 ectx->ec_instr = INSTRUCTIONS(dfunc);
Bram Moolenaarb2049902021-01-24 12:53:53 +0100596 entry = estack_push_ufunc(ufunc, 1);
Bram Moolenaarc620c052020-07-08 15:16:19 +0200597 if (entry != NULL)
598 {
599 // Set the script context to the script where the function was defined.
Bram Moolenaarc70fe462021-04-17 17:59:19 +0200600 // Save the current context so it can be restored on return.
601 entry->es_save_sctx = current_sctx;
602 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200603 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100604
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +0200605 // Start execution at the first instruction.
606 ectx->ec_iidx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100607
608 return OK;
609}
610
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000611// Double linked list of funcstack_T in use.
612static funcstack_T *first_funcstack = NULL;
613
614 static void
615add_funcstack_to_list(funcstack_T *funcstack)
616{
617 // Link in list of funcstacks.
618 if (first_funcstack != NULL)
619 first_funcstack->fs_prev = funcstack;
620 funcstack->fs_next = first_funcstack;
621 funcstack->fs_prev = NULL;
622 first_funcstack = funcstack;
623}
624
625 static void
626remove_funcstack_from_list(funcstack_T *funcstack)
627{
628 if (funcstack->fs_prev == NULL)
629 first_funcstack = funcstack->fs_next;
630 else
631 funcstack->fs_prev->fs_next = funcstack->fs_next;
632 if (funcstack->fs_next != NULL)
633 funcstack->fs_next->fs_prev = funcstack->fs_prev;
634}
635
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100636/*
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200637 * Used when returning from a function: Check if any closure is still
638 * referenced. If so then move the arguments and variables to a separate piece
639 * of stack to be used when the closure is called.
640 * When "free_arguments" is TRUE the arguments are to be freed.
641 * Returns FAIL when out of memory.
642 */
643 static int
644handle_closure_in_use(ectx_T *ectx, int free_arguments)
645{
646 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
647 + ectx->ec_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200648 int argcount;
649 int top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200650 int idx;
651 typval_T *tv;
652 int closure_in_use = FALSE;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200653 garray_T *gap = &ectx->ec_funcrefs;
654 varnumber_T closure_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200655
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200656 if (dfunc->df_ufunc == NULL)
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200657 return OK; // function was freed
658 if (dfunc->df_has_closure == 0)
659 return OK; // no closures
660 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + dfunc->df_varcount);
661 closure_count = tv->vval.v_number;
662 if (closure_count == 0)
663 return OK; // no funcrefs created
664
Bram Moolenaar8fa745e2022-09-16 19:04:24 +0100665 // Compute "top": the first entry in the stack used by the function.
666 // This is the first argument (after that comes the stack frame and then
667 // the local variables).
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200668 argcount = ufunc_argcount(dfunc->df_ufunc);
669 top = ectx->ec_frame_idx - argcount;
670
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200671 // Check if any created closure is still in use.
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200672 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200673 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200674 partial_T *pt;
675 int off = gap->ga_len - closure_count + idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200676
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200677 if (off < 0)
678 continue; // count is off or already done
679 pt = ((partial_T **)gap->ga_data)[off];
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200680 if (pt->pt_refcount > 1)
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200681 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200682 int refcount = pt->pt_refcount;
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200683 int i;
684
Dominique Pelleaf4a61a2021-12-27 17:21:41 +0000685 // A Reference in a local variable doesn't count, it gets
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200686 // unreferenced on return.
687 for (i = 0; i < dfunc->df_varcount; ++i)
688 {
689 typval_T *stv = STACK_TV(ectx->ec_frame_idx
690 + STACK_FRAME_SIZE + i);
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200691 if (stv->v_type == VAR_PARTIAL && pt == stv->vval.v_partial)
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200692 --refcount;
693 }
694 if (refcount > 1)
695 {
696 closure_in_use = TRUE;
697 break;
698 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200699 }
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200700 }
701
702 if (closure_in_use)
703 {
704 funcstack_T *funcstack = ALLOC_CLEAR_ONE(funcstack_T);
705 typval_T *stack;
706
707 // A closure is using the arguments and/or local variables.
708 // Move them to the called function.
709 if (funcstack == NULL)
710 return FAIL;
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000711
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200712 funcstack->fs_var_offset = argcount + STACK_FRAME_SIZE;
Bram Moolenaar1936c762022-09-28 15:19:10 +0100713 funcstack->fs_ga.ga_len = funcstack->fs_var_offset
714 + dfunc->df_varcount;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200715 stack = ALLOC_CLEAR_MULT(typval_T, funcstack->fs_ga.ga_len);
716 funcstack->fs_ga.ga_data = stack;
717 if (stack == NULL)
718 {
719 vim_free(funcstack);
720 return FAIL;
721 }
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000722 add_funcstack_to_list(funcstack);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200723
724 // Move or copy the arguments.
725 for (idx = 0; idx < argcount; ++idx)
726 {
727 tv = STACK_TV(top + idx);
728 if (free_arguments)
729 {
730 *(stack + idx) = *tv;
731 tv->v_type = VAR_UNKNOWN;
732 }
733 else
734 copy_tv(tv, stack + idx);
735 }
Bram Moolenaar8fa745e2022-09-16 19:04:24 +0100736 // Skip the stack frame.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200737 // Move the local variables.
738 for (idx = 0; idx < dfunc->df_varcount; ++idx)
739 {
740 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + idx);
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200741
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200742 // A partial created for a local function, that is also used as a
743 // local variable, has a reference count for the variable, thus
744 // will never go down to zero. When all these refcounts are one
745 // then the funcstack is unused. We need to count how many we have
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000746 // so we know when to check.
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200747 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL)
748 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200749 int i;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200750
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200751 for (i = 0; i < closure_count; ++i)
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200752 if (tv->vval.v_partial == ((partial_T **)gap->ga_data)[
753 gap->ga_len - closure_count + i])
754 ++funcstack->fs_min_refcount;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200755 }
756
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200757 *(stack + funcstack->fs_var_offset + idx) = *tv;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200758 tv->v_type = VAR_UNKNOWN;
759 }
760
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200761 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200762 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200763 partial_T *pt = ((partial_T **)gap->ga_data)[gap->ga_len
764 - closure_count + idx];
765 if (pt->pt_refcount > 1)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200766 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200767 ++funcstack->fs_refcount;
768 pt->pt_funcstack = funcstack;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100769 pt->pt_outer.out_stack = &funcstack->fs_ga;
770 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx - top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200771 }
772 }
773 }
774
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200775 for (idx = 0; idx < closure_count; ++idx)
776 partial_unref(((partial_T **)gap->ga_data)[gap->ga_len
777 - closure_count + idx]);
778 gap->ga_len -= closure_count;
779 if (gap->ga_len == 0)
780 ga_clear(gap);
781
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200782 return OK;
783}
784
785/*
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200786 * Called when a partial is freed or its reference count goes down to one. The
787 * funcstack may be the only reference to the partials in the local variables.
788 * Go over all of them, the funcref and can be freed if all partials
789 * referencing the funcstack have a reference count of one.
Bram Moolenaaracd6b992022-09-17 16:27:39 +0100790 * Returns TRUE if the funcstack is freed, the partial referencing it will then
791 * also have been freed.
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200792 */
Bram Moolenaaracd6b992022-09-17 16:27:39 +0100793 int
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200794funcstack_check_refcount(funcstack_T *funcstack)
795{
Bram Moolenaaracd6b992022-09-17 16:27:39 +0100796 int i;
797 garray_T *gap = &funcstack->fs_ga;
798 int done = 0;
799 typval_T *stack;
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200800
801 if (funcstack->fs_refcount > funcstack->fs_min_refcount)
Bram Moolenaaracd6b992022-09-17 16:27:39 +0100802 return FALSE;
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200803 for (i = funcstack->fs_var_offset; i < gap->ga_len; ++i)
804 {
805 typval_T *tv = ((typval_T *)gap->ga_data) + i;
806
807 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL
808 && tv->vval.v_partial->pt_funcstack == funcstack
809 && tv->vval.v_partial->pt_refcount == 1)
810 ++done;
811 }
Bram Moolenaaracd6b992022-09-17 16:27:39 +0100812 if (done != funcstack->fs_min_refcount)
813 return FALSE;
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200814
Bram Moolenaaracd6b992022-09-17 16:27:39 +0100815 stack = gap->ga_data;
816
817 // All partials referencing the funcstack have a reference count of
818 // one, thus the funcstack is no longer of use.
819 for (i = 0; i < gap->ga_len; ++i)
820 clear_tv(stack + i);
821 vim_free(stack);
822 remove_funcstack_from_list(funcstack);
823 vim_free(funcstack);
824
825 return TRUE;
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200826}
827
828/*
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000829 * For garbage collecting: set references in all variables referenced by
830 * all funcstacks.
831 */
832 int
833set_ref_in_funcstacks(int copyID)
834{
835 funcstack_T *funcstack;
836
837 for (funcstack = first_funcstack; funcstack != NULL;
838 funcstack = funcstack->fs_next)
839 {
840 typval_T *stack = funcstack->fs_ga.ga_data;
841 int i;
842
843 for (i = 0; i < funcstack->fs_ga.ga_len; ++i)
844 if (set_ref_in_item(stack + i, copyID, NULL, NULL))
845 return TRUE; // abort
846 }
847 return FALSE;
848}
849
Bram Moolenaar806a2732022-09-04 15:40:36 +0100850// Ugly static to avoid passing the execution context around through many
851// layers.
852static ectx_T *current_ectx = NULL;
853
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000854/*
Bram Moolenaar806a2732022-09-04 15:40:36 +0100855 * Return TRUE if currently executing a :def function.
856 * Can be used by builtin functions only.
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100857 */
Bram Moolenaar806a2732022-09-04 15:40:36 +0100858 int
859in_def_function(void)
860{
861 return current_ectx != NULL;
862}
863
864/*
Bram Moolenaar98aff652022-09-06 21:02:35 +0100865 * Clear "current_ectx" and return the previous value. To be used when calling
866 * a user function.
867 */
868 ectx_T *
dundargocc57b5bc2022-11-02 13:30:51 +0000869clear_current_ectx(void)
Bram Moolenaar98aff652022-09-06 21:02:35 +0100870{
871 ectx_T *r = current_ectx;
872
873 current_ectx = NULL;
874 return r;
875}
876
877 void
878restore_current_ectx(ectx_T *ectx)
879{
880 if (current_ectx != NULL)
881 iemsg("Restoring current_ectx while it is not NULL");
882 current_ectx = ectx;
883}
884
885/*
Bram Moolenaar806a2732022-09-04 15:40:36 +0100886 * Add an entry for a deferred function call to the currently executing
887 * function.
888 * Return the list or NULL when failed.
889 */
890 static list_T *
891add_defer_item(int var_idx, int argcount, ectx_T *ectx)
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100892{
893 typval_T *defer_tv = STACK_TV_VAR(var_idx);
894 list_T *defer_l;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100895 list_T *l;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100896 typval_T listval;
897
898 if (defer_tv->v_type != VAR_LIST)
899 {
Bram Moolenaara5348f22022-09-04 11:42:22 +0100900 // first time, allocate the list
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100901 if (rettv_list_alloc(defer_tv) == FAIL)
Bram Moolenaar806a2732022-09-04 15:40:36 +0100902 return NULL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100903 }
904 defer_l = defer_tv->vval.v_list;
905
906 l = list_alloc_with_items(argcount + 1);
907 if (l == NULL)
Bram Moolenaar806a2732022-09-04 15:40:36 +0100908 return NULL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100909 listval.v_type = VAR_LIST;
910 listval.vval.v_list = l;
911 listval.v_lock = 0;
Bram Moolenaara5348f22022-09-04 11:42:22 +0100912 if (list_insert_tv(defer_l, &listval, defer_l->lv_first) == FAIL)
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100913 {
914 vim_free(l);
Bram Moolenaar806a2732022-09-04 15:40:36 +0100915 return NULL;
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100916 }
917
Bram Moolenaar806a2732022-09-04 15:40:36 +0100918 return l;
919}
920
921/*
922 * Handle ISN_DEFER. Stack has a function reference and "argcount" arguments.
923 * The local variable that lists deferred functions is "var_idx".
924 * Returns OK or FAIL.
925 */
926 static int
927defer_command(int var_idx, int argcount, ectx_T *ectx)
928{
929 list_T *l = add_defer_item(var_idx, argcount, ectx);
930 int i;
931 typval_T *func_tv;
932
933 if (l == NULL)
934 return FAIL;
935
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100936 func_tv = STACK_TV_BOT(-argcount - 1);
Bram Moolenaarf5fec052022-09-11 11:49:22 +0100937 if (func_tv->v_type != VAR_FUNC && func_tv->v_type != VAR_PARTIAL)
938 {
939 semsg(_(e_expected_str_but_got_str),
940 "function or partial",
941 vartype_name(func_tv->v_type));
942 return FAIL;
943 }
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100944 list_set_item(l, 0, func_tv);
945
Bram Moolenaarf5fec052022-09-11 11:49:22 +0100946 for (i = 0; i < argcount; ++i)
947 list_set_item(l, i + 1, STACK_TV_BOT(-argcount + i));
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100948 ectx->ec_stack.ga_len -= argcount + 1;
949 return OK;
950}
951
952/*
Bram Moolenaar806a2732022-09-04 15:40:36 +0100953 * Add a deferred function "name" with one argument "arg_tv".
954 * Consumes "name", also on failure.
955 * Only to be called when in_def_function() returns TRUE.
956 */
957 int
958add_defer_function(char_u *name, int argcount, typval_T *argvars)
959{
960 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
961 + current_ectx->ec_dfunc_idx;
962 list_T *l;
963 typval_T func_tv;
964 int i;
965
966 if (dfunc->df_defer_var_idx == 0)
967 {
968 iemsg("df_defer_var_idx is zero");
Bram Moolenaar31ea6bf2022-09-05 10:47:13 +0100969 vim_free(name);
Bram Moolenaar806a2732022-09-04 15:40:36 +0100970 return FAIL;
971 }
Bram Moolenaar806a2732022-09-04 15:40:36 +0100972
Bram Moolenaarf5fec052022-09-11 11:49:22 +0100973 l = add_defer_item(dfunc->df_defer_var_idx - 1, argcount, current_ectx);
Bram Moolenaar806a2732022-09-04 15:40:36 +0100974 if (l == NULL)
975 {
Bram Moolenaar31ea6bf2022-09-05 10:47:13 +0100976 vim_free(name);
Bram Moolenaar806a2732022-09-04 15:40:36 +0100977 return FAIL;
978 }
979
Bram Moolenaar31ea6bf2022-09-05 10:47:13 +0100980 func_tv.v_type = VAR_FUNC;
981 func_tv.v_lock = 0;
982 func_tv.vval.v_string = name;
Bram Moolenaar806a2732022-09-04 15:40:36 +0100983 list_set_item(l, 0, &func_tv);
Bram Moolenaar31ea6bf2022-09-05 10:47:13 +0100984
Bram Moolenaar806a2732022-09-04 15:40:36 +0100985 for (i = 0; i < argcount; ++i)
986 list_set_item(l, i + 1, argvars + i);
987 return OK;
988}
989
990/*
Bram Moolenaar1d84f762022-09-03 21:35:53 +0100991 * Invoked when returning from a function: Invoke any deferred calls.
992 */
993 static void
994invoke_defer_funcs(ectx_T *ectx)
995{
996 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
997 + ectx->ec_dfunc_idx;
998 typval_T *defer_tv = STACK_TV_VAR(dfunc->df_defer_var_idx - 1);
999 listitem_T *li;
1000
1001 if (defer_tv->v_type != VAR_LIST)
1002 return; // no function added
1003 for (li = defer_tv->vval.v_list->lv_first; li != NULL; li = li->li_next)
1004 {
1005 list_T *l = li->li_tv.vval.v_list;
1006 typval_T rettv;
1007 typval_T argvars[MAX_FUNC_ARGS];
1008 int i;
1009 listitem_T *arg_li = l->lv_first;
1010 funcexe_T funcexe;
1011
1012 for (i = 0; i < l->lv_len - 1; ++i)
1013 {
1014 arg_li = arg_li->li_next;
1015 argvars[i] = arg_li->li_tv;
1016 }
1017
1018 CLEAR_FIELD(funcexe);
1019 funcexe.fe_evaluate = TRUE;
1020 rettv.v_type = VAR_UNKNOWN;
1021 (void)call_func(l->lv_first->li_tv.vval.v_string, -1,
1022 &rettv, l->lv_len - 1, argvars, &funcexe);
1023 clear_tv(&rettv);
1024 }
1025}
1026
1027/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001028 * Return from the current function.
1029 */
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001030 static int
Bram Moolenaar4c137212021-04-19 16:48:48 +02001031func_return(ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001032{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001033 int idx;
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01001034 int ret_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001035 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1036 + ectx->ec_dfunc_idx;
1037 int argcount = ufunc_argcount(dfunc->df_ufunc);
1038 int top = ectx->ec_frame_idx - argcount;
Bram Moolenaarc620c052020-07-08 15:16:19 +02001039 estack_T *entry;
Bram Moolenaar12d26532021-02-19 19:13:21 +01001040 int prev_dfunc_idx = STACK_TV(ectx->ec_frame_idx
1041 + STACK_FRAME_FUNC_OFF)->vval.v_number;
Bram Moolenaarb06b50d2021-04-26 21:39:25 +02001042 funclocal_T *floc;
1043#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +01001044 dfunc_T *prev_dfunc = ((dfunc_T *)def_functions.ga_data)
1045 + prev_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001046
Bram Moolenaar12d26532021-02-19 19:13:21 +01001047 if (do_profiling == PROF_YES)
1048 {
1049 ufunc_T *caller = prev_dfunc->df_ufunc;
1050
1051 if (dfunc->df_ufunc->uf_profiling
1052 || (caller != NULL && caller->uf_profiling))
1053 {
1054 profile_may_end_func(((profinfo_T *)profile_info_ga.ga_data)
1055 + profile_info_ga.ga_len - 1, dfunc->df_ufunc, caller);
1056 --profile_info_ga.ga_len;
1057 }
1058 }
1059#endif
Bram Moolenaar919c12c2021-12-14 14:29:16 +00001060
Bram Moolenaar1d84f762022-09-03 21:35:53 +01001061 if (dfunc->df_defer_var_idx > 0)
1062 invoke_defer_funcs(ectx);
1063
Bram Moolenaar919c12c2021-12-14 14:29:16 +00001064 // No check for uf_refcount being zero, cannot think of a way that would
1065 // happen.
Bram Moolenaarc970e422021-03-17 15:03:04 +01001066 --dfunc->df_ufunc->uf_calls;
1067
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001068 // execution context goes one level up
Bram Moolenaarc620c052020-07-08 15:16:19 +02001069 entry = estack_pop();
1070 if (entry != NULL)
Bram Moolenaarc70fe462021-04-17 17:59:19 +02001071 current_sctx = entry->es_save_sctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001072
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001073 if (handle_closure_in_use(ectx, TRUE) == FAIL)
1074 return FAIL;
1075
1076 // Clear the arguments.
1077 for (idx = top; idx < ectx->ec_frame_idx; ++idx)
1078 clear_tv(STACK_TV(idx));
1079
1080 // Clear local variables and temp values, but not the return value.
1081 for (idx = ectx->ec_frame_idx + STACK_FRAME_SIZE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001082 idx < ectx->ec_stack.ga_len - 1; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001083 clear_tv(STACK_TV(idx));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001084
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01001085 // The return value should be on top of the stack. However, when aborting
1086 // it may not be there and ec_frame_idx is the top of the stack.
1087 ret_idx = ectx->ec_stack.ga_len - 1;
Bram Moolenaar0186e582021-01-10 18:33:11 +01001088 if (ret_idx == ectx->ec_frame_idx + STACK_FRAME_IDX_OFF)
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01001089 ret_idx = 0;
1090
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001091 if (ectx->ec_outer_ref != NULL)
1092 {
1093 if (ectx->ec_outer_ref->or_outer_allocated)
1094 vim_free(ectx->ec_outer_ref->or_outer);
1095 partial_unref(ectx->ec_outer_ref->or_partial);
1096 vim_free(ectx->ec_outer_ref);
1097 }
Bram Moolenaar0186e582021-01-10 18:33:11 +01001098
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001099 // Restore the previous frame.
Bram Moolenaar12d26532021-02-19 19:13:21 +01001100 ectx->ec_dfunc_idx = prev_dfunc_idx;
Bram Moolenaar0186e582021-01-10 18:33:11 +01001101 ectx->ec_iidx = STACK_TV(ectx->ec_frame_idx
1102 + STACK_FRAME_IIDX_OFF)->vval.v_number;
Bram Moolenaar5930ddc2021-04-26 20:32:59 +02001103 ectx->ec_instr = (void *)STACK_TV(ectx->ec_frame_idx
1104 + STACK_FRAME_INSTR_OFF)->vval.v_string;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001105 ectx->ec_outer_ref = (void *)STACK_TV(ectx->ec_frame_idx
Bram Moolenaar0186e582021-01-10 18:33:11 +01001106 + STACK_FRAME_OUTER_OFF)->vval.v_string;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001107 floc = (void *)STACK_TV(ectx->ec_frame_idx
1108 + STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string;
Bram Moolenaar5366e1a2020-10-01 13:01:34 +02001109 // restoring ec_frame_idx must be last
Bram Moolenaar0186e582021-01-10 18:33:11 +01001110 ectx->ec_frame_idx = STACK_TV(ectx->ec_frame_idx
1111 + STACK_FRAME_IDX_OFF)->vval.v_number;
Bram Moolenaard386e922021-04-25 14:48:49 +02001112
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001113 if (floc == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02001114 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001115 else
1116 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02001117 ectx->ec_funclocal = *floc;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001118 vim_free(floc);
1119 }
1120
Bram Moolenaar34c54eb2020-11-25 19:15:19 +01001121 if (ret_idx > 0)
1122 {
1123 // Reset the stack to the position before the call, with a spot for the
1124 // return value, moved there from above the frame.
1125 ectx->ec_stack.ga_len = top + 1;
1126 *STACK_TV_BOT(-1) = *STACK_TV(ret_idx);
1127 }
1128 else
1129 // Reset the stack to the position before the call.
1130 ectx->ec_stack.ga_len = top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001131
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001132 funcdepth_decrement();
Bram Moolenaare99d4222021-06-13 14:01:26 +02001133 --ex_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001134 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001135}
1136
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001137/*
1138 * Prepare arguments and rettv for calling a builtin or user function.
1139 */
1140 static int
1141call_prepare(int argcount, typval_T *argvars, ectx_T *ectx)
1142{
1143 int idx;
1144 typval_T *tv;
1145
1146 // Move arguments from bottom of the stack to argvars[] and add terminator.
1147 for (idx = 0; idx < argcount; ++idx)
1148 argvars[idx] = *STACK_TV_BOT(idx - argcount);
1149 argvars[argcount].v_type = VAR_UNKNOWN;
1150
1151 // Result replaces the arguments on the stack.
1152 if (argcount > 0)
1153 ectx->ec_stack.ga_len -= argcount - 1;
Bram Moolenaar35578162021-08-02 19:10:38 +02001154 else if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001155 return FAIL;
1156 else
1157 ++ectx->ec_stack.ga_len;
1158
1159 // Default return value is zero.
1160 tv = STACK_TV_BOT(-1);
1161 tv->v_type = VAR_NUMBER;
1162 tv->vval.v_number = 0;
Bram Moolenaar24565cf2022-03-28 18:16:52 +01001163 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001164
1165 return OK;
1166}
1167
1168/*
1169 * Call a builtin function by index.
1170 */
1171 static int
1172call_bfunc(int func_idx, int argcount, ectx_T *ectx)
1173{
1174 typval_T argvars[MAX_FUNC_ARGS];
1175 int idx;
Bram Moolenaar171fb922020-10-28 16:54:47 +01001176 int did_emsg_before = did_emsg;
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001177 ectx_T *prev_ectx = current_ectx;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001178 char *save_func_name = ectx->ec_where.wt_func_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001179
1180 if (call_prepare(argcount, argvars, ectx) == FAIL)
1181 return FAIL;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001182 ectx->ec_where.wt_func_name = internal_func_name(func_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001183
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001184 // Call the builtin function. Set "current_ectx" so that when it
1185 // recursively invokes call_def_function() a closure context can be set.
1186 current_ectx = ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001187 call_internal_func_by_idx(func_idx, argvars, STACK_TV_BOT(-1));
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001188 current_ectx = prev_ectx;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001189 ectx->ec_where.wt_func_name = save_func_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001190
1191 // Clear the arguments.
1192 for (idx = 0; idx < argcount; ++idx)
1193 clear_tv(&argvars[idx]);
Bram Moolenaar015f4262020-05-05 21:25:22 +02001194
Bram Moolenaar57f799e2020-12-12 20:42:19 +01001195 if (did_emsg > did_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +02001196 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001197 return OK;
1198}
1199
1200/*
1201 * Execute a user defined function.
Bram Moolenaarab360522021-01-10 14:02:28 +01001202 * If the function is compiled this will add a stack frame and set the
1203 * instruction pointer at the start of the function.
1204 * Otherwise the function is called here.
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001205 * If "pt" is not null use "pt->pt_outer" for ec_outer_ref->or_outer.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001206 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001207 */
1208 static int
Bram Moolenaar0186e582021-01-10 18:33:11 +01001209call_ufunc(
1210 ufunc_T *ufunc,
1211 partial_T *pt,
1212 int argcount,
1213 ectx_T *ectx,
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001214 isn_T *iptr,
1215 dict_T *selfdict)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001216{
1217 typval_T argvars[MAX_FUNC_ARGS];
1218 funcexe_T funcexe;
1219 int error;
1220 int idx;
Bram Moolenaar28ee8922020-10-28 20:20:00 +01001221 int did_emsg_before = did_emsg;
Bram Moolenaar139575d2022-03-15 19:29:30 +00001222 compiletype_T compile_type = get_compile_type(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001223
Bram Moolenaare99d4222021-06-13 14:01:26 +02001224 if (func_needs_compiling(ufunc, compile_type)
1225 && compile_def_function(ufunc, FALSE, compile_type, NULL)
1226 == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02001227 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001228 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001229 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +01001230 error = check_user_func_argcount(ufunc, argcount);
Bram Moolenaar50824712020-12-20 21:10:17 +01001231 if (error != FCERR_UNKNOWN)
1232 {
1233 if (error == FCERR_TOOMANY)
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001234 semsg(_(e_too_many_arguments_for_function_str),
1235 printable_func_name(ufunc));
Bram Moolenaar50824712020-12-20 21:10:17 +01001236 else
Bram Moolenaare1242042021-12-16 20:56:57 +00001237 semsg(_(e_not_enough_arguments_for_function_str),
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001238 printable_func_name(ufunc));
Bram Moolenaar50824712020-12-20 21:10:17 +01001239 return FAIL;
1240 }
1241
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001242 // The function has been compiled, can call it quickly. For a function
1243 // that was defined later: we can call it directly next time.
1244 if (iptr != NULL)
1245 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001246 delete_instr(iptr);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001247 iptr->isn_type = ISN_DCALL;
1248 iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
1249 iptr->isn_arg.dfunc.cdf_argcount = argcount;
1250 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001251 return call_dfunc(ufunc->uf_dfunc_idx, pt, argcount, ectx);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001252 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001253
1254 if (call_prepare(argcount, argvars, ectx) == FAIL)
1255 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +02001256 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00001257 funcexe.fe_evaluate = TRUE;
1258 funcexe.fe_selfdict = selfdict != NULL ? selfdict : dict_stack_get_dict();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001259
1260 // Call the user function. Result goes in last position on the stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001261 error = call_user_func_check(ufunc, argcount, argvars,
Bram Moolenaar851f86b2021-12-13 14:26:44 +00001262 STACK_TV_BOT(-1), &funcexe, funcexe.fe_selfdict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001263
1264 // Clear the arguments.
1265 for (idx = 0; idx < argcount; ++idx)
1266 clear_tv(&argvars[idx]);
1267
1268 if (error != FCERR_NONE)
1269 {
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01001270 user_func_error(error, printable_func_name(ufunc),
1271 funcexe.fe_found_var);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001272 return FAIL;
1273 }
Bram Moolenaar28ee8922020-10-28 20:20:00 +01001274 if (did_emsg > did_emsg_before)
Bram Moolenaared677f52020-08-12 16:38:10 +02001275 // Error other than from calling the function itself.
1276 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001277 return OK;
1278}
1279
1280/*
Bram Moolenaara91a7132021-03-25 21:12:15 +01001281 * If command modifiers were applied restore them.
1282 */
1283 static void
1284may_restore_cmdmod(funclocal_T *funclocal)
1285{
1286 if (funclocal->floc_restore_cmdmod)
1287 {
1288 cmdmod.cmod_filter_regmatch.regprog = NULL;
1289 undo_cmdmod(&cmdmod);
1290 cmdmod = funclocal->floc_save_cmdmod;
1291 funclocal->floc_restore_cmdmod = FALSE;
1292 }
1293}
1294
1295/*
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001296 * Return TRUE if an error was given (not caught in try/catch) or CTRL-C was
1297 * pressed.
Bram Moolenaara1773442020-08-12 15:21:22 +02001298 */
1299 static int
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001300vim9_aborting(int prev_uncaught_emsg)
Bram Moolenaara1773442020-08-12 15:21:22 +02001301{
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001302 return uncaught_emsg > prev_uncaught_emsg || got_int || did_throw;
Bram Moolenaara1773442020-08-12 15:21:22 +02001303}
1304
1305/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001306 * Execute a function by "name".
1307 * This can be a builtin function or a user function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001308 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001309 * Returns FAIL if not found without an error message.
1310 */
1311 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001312call_by_name(
1313 char_u *name,
1314 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001315 ectx_T *ectx,
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001316 isn_T *iptr,
1317 dict_T *selfdict)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001318{
1319 ufunc_T *ufunc;
1320
1321 if (builtin_function(name, -1))
1322 {
1323 int func_idx = find_internal_func(name);
1324
Bram Moolenaar1983f1a2022-02-28 20:55:02 +00001325 if (func_idx < 0) // Impossible?
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001326 return FAIL;
Bram Moolenaar389df252020-07-09 21:20:47 +02001327 if (check_internal_func(func_idx, argcount) < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001328 return FAIL;
1329 return call_bfunc(func_idx, argcount, ectx);
1330 }
1331
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00001332 ufunc = find_func(name, FALSE);
Bram Moolenaara1773442020-08-12 15:21:22 +02001333
1334 if (ufunc == NULL)
1335 {
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001336 int prev_uncaught_emsg = uncaught_emsg;
Bram Moolenaara1773442020-08-12 15:21:22 +02001337
1338 if (script_autoload(name, TRUE))
1339 // loaded a package, search for the function again
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00001340 ufunc = find_func(name, FALSE);
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001341
1342 if (vim9_aborting(prev_uncaught_emsg))
Bram Moolenaara1773442020-08-12 15:21:22 +02001343 return FAIL; // bail out if loading the script caused an error
1344 }
1345
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001346 if (ufunc != NULL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001347 {
Bram Moolenaar6ce46b92021-08-07 15:35:36 +02001348 if (ufunc->uf_arg_types != NULL || ufunc->uf_va_type != NULL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001349 {
1350 int i;
1351 typval_T *argv = STACK_TV_BOT(0) - argcount;
1352
1353 // The function can change at runtime, check that the argument
1354 // types are correct.
1355 for (i = 0; i < argcount; ++i)
1356 {
Bram Moolenaare3ffcd92021-03-08 21:47:13 +01001357 type_T *type = NULL;
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001358
Bram Moolenaar6ce46b92021-08-07 15:35:36 +02001359 if (i < ufunc->uf_args.ga_len && ufunc->uf_arg_types != NULL)
Bram Moolenaare3ffcd92021-03-08 21:47:13 +01001360 type = ufunc->uf_arg_types[i];
1361 else if (ufunc->uf_va_type != NULL)
1362 type = ufunc->uf_va_type->tt_member;
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001363 if (type != NULL && check_typval_arg_type(type,
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001364 &argv[i], NULL, i + 1) == FAIL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001365 return FAIL;
1366 }
1367 }
1368
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001369 return call_ufunc(ufunc, NULL, argcount, ectx, iptr, selfdict);
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001370 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001371
1372 return FAIL;
1373}
1374
1375 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001376call_partial(
1377 typval_T *tv,
1378 int argcount_arg,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001379 ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001380{
Bram Moolenaara90afb92020-07-15 22:38:56 +02001381 int argcount = argcount_arg;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001382 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001383 int called_emsg_before = called_emsg;
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +01001384 int res = FAIL;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001385 dict_T *selfdict = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001386
1387 if (tv->v_type == VAR_PARTIAL)
1388 {
Bram Moolenaara90afb92020-07-15 22:38:56 +02001389 partial_T *pt = tv->vval.v_partial;
1390 int i;
1391
1392 if (pt->pt_argc > 0)
1393 {
1394 // Make space for arguments from the partial, shift the "argcount"
1395 // arguments up.
Bram Moolenaar35578162021-08-02 19:10:38 +02001396 if (GA_GROW_FAILS(&ectx->ec_stack, pt->pt_argc))
Bram Moolenaara90afb92020-07-15 22:38:56 +02001397 return FAIL;
1398 for (i = 1; i <= argcount; ++i)
1399 *STACK_TV_BOT(-i + pt->pt_argc) = *STACK_TV_BOT(-i);
1400 ectx->ec_stack.ga_len += pt->pt_argc;
1401 argcount += pt->pt_argc;
1402
1403 // copy the arguments from the partial onto the stack
1404 for (i = 0; i < pt->pt_argc; ++i)
1405 copy_tv(&pt->pt_argv[i], STACK_TV_BOT(-argcount + i));
1406 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001407 selfdict = pt->pt_dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001408
1409 if (pt->pt_func != NULL)
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001410 return call_ufunc(pt->pt_func, pt, argcount, ectx, NULL, selfdict);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001411
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001412 name = pt->pt_name;
1413 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001414 else if (tv->v_type == VAR_FUNC)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001415 name = tv->vval.v_string;
Bram Moolenaar95006e32020-08-29 17:47:08 +02001416 if (name != NULL)
1417 {
1418 char_u fname_buf[FLEN_FIXED + 1];
1419 char_u *tofree = NULL;
1420 int error = FCERR_NONE;
1421 char_u *fname;
1422
1423 // May need to translate <SNR>123_ to K_SNR.
1424 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
1425 if (error != FCERR_NONE)
1426 res = FAIL;
1427 else
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001428 res = call_by_name(fname, argcount, ectx, NULL, selfdict);
Bram Moolenaar95006e32020-08-29 17:47:08 +02001429 vim_free(tofree);
1430 }
1431
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +01001432 if (res == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001433 {
1434 if (called_emsg == called_emsg_before)
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001435 emsg_funcname(e_unknown_function_str,
Bram Moolenaar015f4262020-05-05 21:25:22 +02001436 name == NULL ? (char_u *)"[unknown]" : name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001437 return FAIL;
1438 }
1439 return OK;
1440}
1441
1442/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001443 * Check if "lock" is VAR_LOCKED or VAR_FIXED. If so give an error and return
1444 * TRUE.
1445 */
1446 static int
1447error_if_locked(int lock, char *error)
1448{
1449 if (lock & (VAR_LOCKED | VAR_FIXED))
1450 {
1451 emsg(_(error));
1452 return TRUE;
1453 }
1454 return FALSE;
1455}
1456
1457/*
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001458 * Give an error if "tv" is not a number and return FAIL.
1459 */
1460 static int
1461check_for_number(typval_T *tv)
1462{
1463 if (tv->v_type != VAR_NUMBER)
1464 {
1465 semsg(_(e_expected_str_but_got_str),
1466 vartype_name(VAR_NUMBER), vartype_name(tv->v_type));
1467 return FAIL;
1468 }
1469 return OK;
1470}
1471
1472/*
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001473 * Store "tv" in variable "name".
1474 * This is for s: and g: variables.
1475 */
1476 static void
1477store_var(char_u *name, typval_T *tv)
1478{
1479 funccal_entry_T entry;
Bram Moolenaard877a572021-04-01 19:42:48 +02001480 int flags = ASSIGN_DECL;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001481
Bram Moolenaard877a572021-04-01 19:42:48 +02001482 if (tv->v_lock)
1483 flags |= ASSIGN_CONST;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001484 save_funccal(&entry);
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001485 set_var_const(name, 0, NULL, tv, FALSE, flags, 0);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001486 restore_funccal();
1487}
1488
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001489/*
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001490 * Convert "tv" to a string.
1491 * Return FAIL if not allowed.
1492 */
1493 static int
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001494do_2string(typval_T *tv, int is_2string_any, int tolerant)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001495{
1496 if (tv->v_type != VAR_STRING)
1497 {
1498 char_u *str;
1499
1500 if (is_2string_any)
1501 {
1502 switch (tv->v_type)
1503 {
1504 case VAR_SPECIAL:
1505 case VAR_BOOL:
1506 case VAR_NUMBER:
1507 case VAR_FLOAT:
1508 case VAR_BLOB: break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001509
1510 case VAR_LIST:
1511 if (tolerant)
1512 {
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001513 char_u *s, *e, *p;
1514 garray_T ga;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001515
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001516 ga_init2(&ga, sizeof(char_u *), 1);
1517
1518 // Convert to NL separated items, then
1519 // escape the items and replace the NL with
1520 // a space.
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001521 str = typval2string(tv, TRUE);
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001522 if (str == NULL)
1523 return FAIL;
1524 s = str;
1525 while ((e = vim_strchr(s, '\n')) != NULL)
1526 {
1527 *e = NUL;
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +01001528 p = vim_strsave_fnameescape(s,
1529 VSE_NONE);
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001530 if (p != NULL)
1531 {
1532 ga_concat(&ga, p);
1533 ga_concat(&ga, (char_u *)" ");
1534 vim_free(p);
1535 }
1536 s = e + 1;
1537 }
1538 vim_free(str);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001539 clear_tv(tv);
1540 tv->v_type = VAR_STRING;
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001541 tv->vval.v_string = ga.ga_data;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001542 return OK;
1543 }
1544 // FALLTHROUGH
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001545 default: to_string_error(tv->v_type);
1546 return FAIL;
1547 }
1548 }
Bram Moolenaar34453202021-01-31 13:08:38 +01001549 str = typval_tostring(tv, TRUE);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001550 clear_tv(tv);
1551 tv->v_type = VAR_STRING;
1552 tv->vval.v_string = str;
1553 }
1554 return OK;
1555}
1556
1557/*
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001558 * When the value of "sv" is a null list of dict, allocate it.
1559 */
1560 static void
Bram Moolenaar859cc212022-03-28 15:22:35 +01001561allocate_if_null(svar_T *sv)
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001562{
Bram Moolenaar859cc212022-03-28 15:22:35 +01001563 typval_T *tv = sv->sv_tv;
1564
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001565 switch (tv->v_type)
1566 {
1567 case VAR_LIST:
Bram Moolenaar859cc212022-03-28 15:22:35 +01001568 if (tv->vval.v_list == NULL && sv->sv_type != &t_list_empty)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001569 (void)rettv_list_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001570 break;
1571 case VAR_DICT:
Bram Moolenaar859cc212022-03-28 15:22:35 +01001572 if (tv->vval.v_dict == NULL && sv->sv_type != &t_dict_empty)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001573 (void)rettv_dict_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001574 break;
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02001575 case VAR_BLOB:
Bram Moolenaar859cc212022-03-28 15:22:35 +01001576 if (tv->vval.v_blob == NULL && sv->sv_type != &t_blob_null)
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02001577 (void)rettv_blob_alloc(tv);
1578 break;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001579 default:
1580 break;
1581 }
1582}
Bram Moolenaard3aac292020-04-19 14:32:17 +02001583
Bram Moolenaare7525c52021-01-09 13:20:37 +01001584/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001585 * Return the character "str[index]" where "index" is the character index,
1586 * including composing characters.
1587 * If "index" is out of range NULL is returned.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001588 */
1589 char_u *
1590char_from_string(char_u *str, varnumber_T index)
1591{
1592 size_t nbyte = 0;
1593 varnumber_T nchar = index;
1594 size_t slen;
1595
1596 if (str == NULL)
1597 return NULL;
1598 slen = STRLEN(str);
1599
Bram Moolenaarff871402021-03-26 13:34:05 +01001600 // Do the same as for a list: a negative index counts from the end.
1601 // Optimization to check the first byte to be below 0x80 (and no composing
1602 // character follows) makes this a lot faster.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001603 if (index < 0)
1604 {
1605 int clen = 0;
1606
1607 for (nbyte = 0; nbyte < slen; ++clen)
Bram Moolenaarff871402021-03-26 13:34:05 +01001608 {
1609 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1610 ++nbyte;
1611 else if (enc_utf8)
1612 nbyte += utfc_ptr2len(str + nbyte);
1613 else
1614 nbyte += mb_ptr2len(str + nbyte);
1615 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001616 nchar = clen + index;
1617 if (nchar < 0)
1618 // unlike list: index out of range results in empty string
1619 return NULL;
1620 }
1621
1622 for (nbyte = 0; nchar > 0 && nbyte < slen; --nchar)
Bram Moolenaarff871402021-03-26 13:34:05 +01001623 {
1624 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1625 ++nbyte;
1626 else if (enc_utf8)
1627 nbyte += utfc_ptr2len(str + nbyte);
1628 else
1629 nbyte += mb_ptr2len(str + nbyte);
1630 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001631 if (nbyte >= slen)
1632 return NULL;
Bram Moolenaar0289a092021-03-14 18:40:19 +01001633 return vim_strnsave(str + nbyte, mb_ptr2len(str + nbyte));
Bram Moolenaare7525c52021-01-09 13:20:37 +01001634}
1635
1636/*
1637 * Get the byte index for character index "idx" in string "str" with length
Bram Moolenaar0289a092021-03-14 18:40:19 +01001638 * "str_len". Composing characters are included.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001639 * If going over the end return "str_len".
1640 * If "idx" is negative count from the end, -1 is the last character.
1641 * When going over the start return -1.
1642 */
1643 static long
1644char_idx2byte(char_u *str, size_t str_len, varnumber_T idx)
1645{
1646 varnumber_T nchar = idx;
1647 size_t nbyte = 0;
1648
1649 if (nchar >= 0)
1650 {
1651 while (nchar > 0 && nbyte < str_len)
1652 {
Bram Moolenaar0289a092021-03-14 18:40:19 +01001653 nbyte += mb_ptr2len(str + nbyte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001654 --nchar;
1655 }
1656 }
1657 else
1658 {
1659 nbyte = str_len;
1660 while (nchar < 0 && nbyte > 0)
1661 {
1662 --nbyte;
1663 nbyte -= mb_head_off(str, str + nbyte);
1664 ++nchar;
1665 }
1666 if (nchar < 0)
1667 return -1;
1668 }
1669 return (long)nbyte;
1670}
1671
1672/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001673 * Return the slice "str[first : last]" using character indexes. Composing
1674 * characters are included.
Bram Moolenaar6601b622021-01-13 21:47:15 +01001675 * "exclusive" is TRUE for slice().
Bram Moolenaare7525c52021-01-09 13:20:37 +01001676 * Return NULL when the result is empty.
1677 */
1678 char_u *
Bram Moolenaar6601b622021-01-13 21:47:15 +01001679string_slice(char_u *str, varnumber_T first, varnumber_T last, int exclusive)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001680{
1681 long start_byte, end_byte;
1682 size_t slen;
1683
1684 if (str == NULL)
1685 return NULL;
1686 slen = STRLEN(str);
1687 start_byte = char_idx2byte(str, slen, first);
1688 if (start_byte < 0)
1689 start_byte = 0; // first index very negative: use zero
Bram Moolenaar6601b622021-01-13 21:47:15 +01001690 if ((last == -1 && !exclusive) || last == VARNUM_MAX)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001691 end_byte = (long)slen;
1692 else
1693 {
1694 end_byte = char_idx2byte(str, slen, last);
Bram Moolenaar6601b622021-01-13 21:47:15 +01001695 if (!exclusive && end_byte >= 0 && end_byte < (long)slen)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001696 // end index is inclusive
Bram Moolenaar0289a092021-03-14 18:40:19 +01001697 end_byte += mb_ptr2len(str + end_byte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001698 }
1699
1700 if (start_byte >= (long)slen || end_byte <= start_byte)
1701 return NULL;
1702 return vim_strnsave(str + start_byte, end_byte - start_byte);
1703}
1704
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001705/*
1706 * Get a script variable for ISN_STORESCRIPT and ISN_LOADSCRIPT.
1707 * When "dfunc_idx" is negative don't give an error.
1708 * Returns NULL for an error.
1709 */
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001710 static svar_T *
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001711get_script_svar(scriptref_T *sref, int dfunc_idx)
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001712{
1713 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001714 dfunc_T *dfunc = dfunc_idx < 0 ? NULL
1715 : ((dfunc_T *)def_functions.ga_data) + dfunc_idx;
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001716 svar_T *sv;
1717
1718 if (sref->sref_seq != si->sn_script_seq)
1719 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001720 // The script was reloaded after the function was compiled, the
1721 // script_idx may not be valid.
1722 if (dfunc != NULL)
1723 semsg(_(e_script_variable_invalid_after_reload_in_function_str),
1724 printable_func_name(dfunc->df_ufunc));
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001725 return NULL;
1726 }
1727 sv = ((svar_T *)si->sn_var_vals.ga_data) + sref->sref_idx;
Bram Moolenaar6de22962022-09-09 21:35:36 +01001728 if (sv->sv_name == NULL)
1729 {
1730 if (dfunc != NULL)
1731 emsg(_(e_script_variable_was_deleted));
1732 return NULL;
1733 }
Bram Moolenaar60dc8272021-07-29 22:48:54 +02001734 if (!equal_type(sv->sv_type, sref->sref_type, 0))
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001735 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001736 if (dfunc != NULL)
1737 emsg(_(e_script_variable_type_changed));
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001738 return NULL;
1739 }
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001740
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01001741 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0
1742 && sref->sref_sid != current_sctx.sc_sid)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01001743 {
1744 if (dfunc != NULL)
1745 semsg(_(e_item_not_exported_in_script_str), sv->sv_name);
1746 return NULL;
1747 }
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001748 return sv;
1749}
1750
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001751/*
Bram Moolenaar20677332021-06-06 17:02:53 +02001752 * Function passed to do_cmdline() for splitting a script joined by NL
1753 * characters.
1754 */
1755 static char_u *
1756get_split_sourceline(
1757 int c UNUSED,
1758 void *cookie,
1759 int indent UNUSED,
1760 getline_opt_T options UNUSED)
1761{
1762 source_cookie_T *sp = (source_cookie_T *)cookie;
1763 char_u *p;
1764 char_u *line;
1765
Bram Moolenaar20677332021-06-06 17:02:53 +02001766 p = vim_strchr(sp->nextline, '\n');
1767 if (p == NULL)
1768 {
1769 line = vim_strsave(sp->nextline);
1770 sp->nextline += STRLEN(sp->nextline);
1771 }
1772 else
1773 {
1774 line = vim_strnsave(sp->nextline, p - sp->nextline);
1775 sp->nextline = p + 1;
1776 }
1777 return line;
1778}
1779
1780/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001781 * Execute a function by "name".
1782 * This can be a builtin function, user function or a funcref.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001783 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001784 */
1785 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001786call_eval_func(
1787 char_u *name,
1788 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001789 ectx_T *ectx,
1790 isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001791{
Bram Moolenaared677f52020-08-12 16:38:10 +02001792 int called_emsg_before = called_emsg;
1793 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001794
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001795 res = call_by_name(name, argcount, ectx, iptr, NULL);
Bram Moolenaared677f52020-08-12 16:38:10 +02001796 if (res == FAIL && called_emsg == called_emsg_before)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001797 {
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001798 dictitem_T *v;
1799
1800 v = find_var(name, NULL, FALSE);
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001801 if (v == NULL || (v->di_tv.v_type != VAR_PARTIAL
1802 && v->di_tv.v_type != VAR_FUNC))
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001803 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01001804 emsg_funcname(e_unknown_function_str, name);
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001805 return FAIL;
1806 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001807 return call_partial(&v->di_tv, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001808 }
Bram Moolenaared677f52020-08-12 16:38:10 +02001809 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001810}
1811
1812/*
Bram Moolenaarf112f302020-12-20 17:47:52 +01001813 * When a function reference is used, fill a partial with the information
1814 * needed, especially when it is used as a closure.
1815 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001816 int
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001817fill_partial_and_closure(
Bram Moolenaarcc341812022-09-19 15:54:34 +01001818 partial_T *pt,
1819 ufunc_T *ufunc,
1820 loopvarinfo_T *lvi,
1821 ectx_T *ectx)
Bram Moolenaarf112f302020-12-20 17:47:52 +01001822{
1823 pt->pt_func = ufunc;
1824 pt->pt_refcount = 1;
1825
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001826 if (ufunc->uf_flags & FC_CLOSURE)
Bram Moolenaarf112f302020-12-20 17:47:52 +01001827 {
1828 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1829 + ectx->ec_dfunc_idx;
1830
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001831 // The closure may need to find arguments and local variables of the
1832 // current function in the stack.
Bram Moolenaar0186e582021-01-10 18:33:11 +01001833 pt->pt_outer.out_stack = &ectx->ec_stack;
1834 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001835 if (ectx->ec_outer_ref != NULL)
1836 {
1837 // The current context already has a context, link to that one.
1838 pt->pt_outer.out_up = ectx->ec_outer_ref->or_outer;
1839 if (ectx->ec_outer_ref->or_partial != NULL)
1840 {
1841 pt->pt_outer.out_up_partial = ectx->ec_outer_ref->or_partial;
1842 ++pt->pt_outer.out_up_partial->pt_refcount;
1843 }
1844 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01001845
Bram Moolenaarcc341812022-09-19 15:54:34 +01001846 if (lvi != NULL)
1847 {
1848 int depth;
1849
1850 // The closure may need to find variables defined inside a loop,
1851 // for every nested loop. A new reference is made every time,
1852 // ISN_ENDLOOP will check if they are actually used.
1853 for (depth = 0; depth < lvi->lvi_depth; ++depth)
1854 {
1855 pt->pt_outer.out_loop[depth].stack = &ectx->ec_stack;
1856 pt->pt_outer.out_loop[depth].var_idx = ectx->ec_frame_idx
1857 + STACK_FRAME_SIZE + lvi->lvi_loop[depth].var_idx;
1858 pt->pt_outer.out_loop[depth].var_count =
1859 lvi->lvi_loop[depth].var_count;
1860 }
Bram Moolenaar6d313be2022-09-22 16:36:25 +01001861 pt->pt_outer.out_loop_size = lvi->lvi_depth;
Bram Moolenaarcc341812022-09-19 15:54:34 +01001862 }
Bram Moolenaar6d313be2022-09-22 16:36:25 +01001863 else
1864 pt->pt_outer.out_loop_size = 0;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001865
Bram Moolenaarb46c0832022-09-15 17:19:37 +01001866 // If the function currently executing returns and the closure is still
1867 // being referenced, we need to make a copy of the context (arguments
1868 // and local variables) so that the closure can use it later.
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001869 // Store a reference to the partial so we can handle that.
Bram Moolenaar35578162021-08-02 19:10:38 +02001870 if (GA_GROW_FAILS(&ectx->ec_funcrefs, 1))
Bram Moolenaarf112f302020-12-20 17:47:52 +01001871 {
1872 vim_free(pt);
1873 return FAIL;
1874 }
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001875 // Extra variable keeps the count of closures created in the current
1876 // function call.
Bram Moolenaarf112f302020-12-20 17:47:52 +01001877 ++(((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx
1878 + STACK_FRAME_SIZE + dfunc->df_varcount)->vval.v_number;
1879
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01001880 ((partial_T **)ectx->ec_funcrefs.ga_data)[ectx->ec_funcrefs.ga_len]
1881 = pt;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001882 ++pt->pt_refcount;
1883 ++ectx->ec_funcrefs.ga_len;
1884 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001885 ++ufunc->uf_refcount;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001886 return OK;
1887}
1888
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001889/*
1890 * Execute iptr->isn_arg.string as an Ex command.
1891 */
1892 static int
1893exec_command(isn_T *iptr)
1894{
1895 source_cookie_T cookie;
1896
1897 SOURCING_LNUM = iptr->isn_lnum;
1898 // Pass getsourceline to get an error for a missing ":end"
1899 // command.
1900 CLEAR_FIELD(cookie);
1901 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1902 if (do_cmdline(iptr->isn_arg.string,
1903 getsourceline, &cookie,
1904 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED) == FAIL
1905 || did_emsg)
1906 return FAIL;
1907 return OK;
1908}
1909
Bram Moolenaar89445512022-04-14 12:58:23 +01001910/*
1911 * If script "sid" is not loaded yet then load it now.
1912 * Caller must make sure "sid" is a valid script ID.
1913 * "loaded" is set to TRUE if the script had to be loaded.
1914 * Returns FAIL if loading fails, OK if already loaded or loaded now.
1915 */
1916 int
1917may_load_script(int sid, int *loaded)
1918{
1919 scriptitem_T *si = SCRIPT_ITEM(sid);
1920
1921 if (si->sn_state == SN_STATE_NOT_LOADED)
1922 {
1923 *loaded = TRUE;
1924 if (do_source(si->sn_name, FALSE, DOSO_NONE, NULL) == FAIL)
1925 {
1926 semsg(_(e_cant_open_file_str), si->sn_name);
1927 return FAIL;
1928 }
1929 }
1930 return OK;
1931}
1932
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001933// used for v_instr of typval of VAR_INSTR
1934struct instr_S {
1935 ectx_T *instr_ectx;
1936 isn_T *instr_instr;
1937};
1938
Bram Moolenaar4c137212021-04-19 16:48:48 +02001939// used for substitute_instr
1940typedef struct subs_expr_S {
1941 ectx_T *subs_ectx;
1942 isn_T *subs_instr;
1943 int subs_status;
1944} subs_expr_T;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001945
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001946// Set when calling do_debug().
1947static ectx_T *debug_context = NULL;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001948static int debug_var_count;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001949
1950/*
1951 * When debugging lookup "name" and return the typeval.
1952 * When not found return NULL.
1953 */
1954 typval_T *
1955lookup_debug_var(char_u *name)
1956{
1957 int idx;
1958 dfunc_T *dfunc;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001959 ufunc_T *ufunc;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001960 ectx_T *ectx = debug_context;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001961 int varargs_off;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001962
1963 if (ectx == NULL)
1964 return NULL;
1965 dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
1966
1967 // Go through the local variable names, from last to first.
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001968 for (idx = debug_var_count - 1; idx >= 0; --idx)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001969 {
Bram Moolenaare406ff82022-03-10 20:47:43 +00001970 char_u *varname = ((char_u **)dfunc->df_var_names.ga_data)[idx];
1971
1972 // the variable name may be NULL when not available in this block
1973 if (varname != NULL && STRCMP(varname, name) == 0)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001974 return STACK_TV_VAR(idx);
1975 }
1976
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001977 // Go through argument names.
1978 ufunc = dfunc->df_ufunc;
1979 varargs_off = ufunc->uf_va_name == NULL ? 0 : 1;
1980 for (idx = 0; idx < ufunc->uf_args.ga_len; ++idx)
1981 if (STRCMP(((char_u **)(ufunc->uf_args.ga_data))[idx], name) == 0)
1982 return STACK_TV(ectx->ec_frame_idx - ufunc->uf_args.ga_len
1983 - varargs_off + idx);
1984 if (ufunc->uf_va_name != NULL && STRCMP(ufunc->uf_va_name, name) == 0)
1985 return STACK_TV(ectx->ec_frame_idx - 1);
1986
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001987 return NULL;
1988}
1989
Bram Moolenaar26a44842021-09-02 18:49:06 +02001990/*
1991 * Return TRUE if there might be a breakpoint in "ufunc", which is when a
1992 * breakpoint was set in that function or when there is any expression.
1993 */
1994 int
1995may_break_in_function(ufunc_T *ufunc)
1996{
1997 return ufunc->uf_has_breakpoint || debug_has_expr_breakpoint();
1998}
1999
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002000 static void
2001handle_debug(isn_T *iptr, ectx_T *ectx)
2002{
2003 char_u *line;
2004 ufunc_T *ufunc = (((dfunc_T *)def_functions.ga_data)
2005 + ectx->ec_dfunc_idx)->df_ufunc;
2006 isn_T *ni;
2007 int end_lnum = iptr->isn_lnum;
2008 garray_T ga;
2009 int lnum;
2010
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02002011 if (ex_nesting_level > debug_break_level)
2012 {
2013 linenr_T breakpoint;
2014
Bram Moolenaar26a44842021-09-02 18:49:06 +02002015 if (!may_break_in_function(ufunc))
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02002016 return;
2017
2018 // check for the next breakpoint if needed
2019 breakpoint = dbg_find_breakpoint(FALSE, ufunc->uf_name,
Bram Moolenaar8cec9272021-06-23 20:20:53 +02002020 iptr->isn_arg.debug.dbg_break_lnum);
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02002021 if (breakpoint <= 0 || breakpoint > iptr->isn_lnum)
2022 return;
2023 }
2024
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002025 SOURCING_LNUM = iptr->isn_lnum;
2026 debug_context = ectx;
Bram Moolenaar8cec9272021-06-23 20:20:53 +02002027 debug_var_count = iptr->isn_arg.debug.dbg_var_names_len;
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002028
2029 for (ni = iptr + 1; ni->isn_type != ISN_FINISH; ++ni)
2030 if (ni->isn_type == ISN_DEBUG
2031 || ni->isn_type == ISN_RETURN
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002032 || ni->isn_type == ISN_RETURN_OBJECT
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002033 || ni->isn_type == ISN_RETURN_VOID)
2034 {
Bram Moolenaar112bed02021-11-23 22:16:34 +00002035 end_lnum = ni->isn_lnum + (ni->isn_type == ISN_DEBUG ? 0 : 1);
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002036 break;
2037 }
2038
2039 if (end_lnum > iptr->isn_lnum)
2040 {
2041 ga_init2(&ga, sizeof(char_u *), 10);
Bram Moolenaar310091d2021-12-23 21:14:37 +00002042 for (lnum = iptr->isn_lnum; lnum < end_lnum
2043 && lnum <= ufunc->uf_lines.ga_len; ++lnum)
Bram Moolenaar59b50c32021-06-17 22:27:48 +02002044 {
Bram Moolenaar303215d2021-07-07 20:10:43 +02002045 char_u *p = ((char_u **)ufunc->uf_lines.ga_data)[lnum - 1];
Bram Moolenaar59b50c32021-06-17 22:27:48 +02002046
Bram Moolenaar303215d2021-07-07 20:10:43 +02002047 if (p == NULL)
2048 continue; // left over from continuation line
2049 p = skipwhite(p);
Bram Moolenaar59b50c32021-06-17 22:27:48 +02002050 if (*p == '#')
2051 break;
Bram Moolenaar35578162021-08-02 19:10:38 +02002052 if (GA_GROW_OK(&ga, 1))
Bram Moolenaar59b50c32021-06-17 22:27:48 +02002053 ((char_u **)(ga.ga_data))[ga.ga_len++] = p;
2054 if (STRNCMP(p, "def ", 4) == 0)
2055 break;
2056 }
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002057 line = ga_concat_strings(&ga, " ");
2058 vim_free(ga.ga_data);
2059 }
2060 else
2061 line = ((char_u **)ufunc->uf_lines.ga_data)[iptr->isn_lnum - 1];
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002062
Dominique Pelle6e969552021-06-17 13:53:41 +02002063 do_debug(line == NULL ? (char_u *)"[empty]" : line);
Bram Moolenaar4cea5362021-06-16 22:24:40 +02002064 debug_context = NULL;
2065
2066 if (end_lnum > iptr->isn_lnum)
2067 vim_free(line);
2068}
2069
Bram Moolenaar4c137212021-04-19 16:48:48 +02002070/*
Bram Moolenaar65b0d162022-12-13 18:43:22 +00002071 * Store a value in a list, dict, blob or object variable.
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002072 * Returns OK, FAIL or NOTDONE (uncatchable error).
2073 */
2074 static int
2075execute_storeindex(isn_T *iptr, ectx_T *ectx)
2076{
2077 vartype_T dest_type = iptr->isn_arg.vartype;
2078 typval_T *tv;
2079 typval_T *tv_idx = STACK_TV_BOT(-2);
2080 typval_T *tv_dest = STACK_TV_BOT(-1);
2081 int status = OK;
2082
2083 // Stack contains:
2084 // -3 value to be stored
2085 // -2 index
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002086 // -1 dict, list, blob or object
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002087 tv = STACK_TV_BOT(-3);
2088 SOURCING_LNUM = iptr->isn_lnum;
2089 if (dest_type == VAR_ANY)
2090 {
2091 dest_type = tv_dest->v_type;
2092 if (dest_type == VAR_DICT)
2093 status = do_2string(tv_idx, TRUE, FALSE);
2094 else if (dest_type == VAR_LIST && tv_idx->v_type != VAR_NUMBER)
2095 {
2096 emsg(_(e_number_expected));
2097 status = FAIL;
2098 }
2099 }
Bram Moolenaare08be092022-02-17 13:08:26 +00002100
2101 if (status == OK)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002102 {
Bram Moolenaare08be092022-02-17 13:08:26 +00002103 if (dest_type == VAR_LIST)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002104 {
Bram Moolenaare08be092022-02-17 13:08:26 +00002105 long lidx = (long)tv_idx->vval.v_number;
2106 list_T *list = tv_dest->vval.v_list;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002107
Bram Moolenaare08be092022-02-17 13:08:26 +00002108 if (list == NULL)
2109 {
2110 emsg(_(e_list_not_set));
2111 return FAIL;
2112 }
2113 if (lidx < 0 && list->lv_len + lidx >= 0)
2114 // negative index is relative to the end
2115 lidx = list->lv_len + lidx;
2116 if (lidx < 0 || lidx > list->lv_len)
2117 {
2118 semsg(_(e_list_index_out_of_range_nr), lidx);
2119 return FAIL;
2120 }
2121 if (lidx < list->lv_len)
2122 {
2123 listitem_T *li = list_find(list, lidx);
2124
2125 if (error_if_locked(li->li_tv.v_lock,
Bram Moolenaar70c43d82022-01-26 21:01:15 +00002126 e_cannot_change_locked_list_item))
Bram Moolenaare08be092022-02-17 13:08:26 +00002127 return FAIL;
2128 // overwrite existing list item
2129 clear_tv(&li->li_tv);
2130 li->li_tv = *tv;
2131 }
2132 else
2133 {
2134 if (error_if_locked(list->lv_lock, e_cannot_change_locked_list))
2135 return FAIL;
2136 // append to list, only fails when out of memory
2137 if (list_append_tv(list, tv) == FAIL)
2138 return NOTDONE;
2139 clear_tv(tv);
2140 }
2141 }
2142 else if (dest_type == VAR_DICT)
2143 {
2144 char_u *key = tv_idx->vval.v_string;
2145 dict_T *dict = tv_dest->vval.v_dict;
2146 dictitem_T *di;
2147
2148 SOURCING_LNUM = iptr->isn_lnum;
2149 if (dict == NULL)
2150 {
2151 emsg(_(e_dictionary_not_set));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002152 return FAIL;
Bram Moolenaare08be092022-02-17 13:08:26 +00002153 }
2154 if (key == NULL)
2155 key = (char_u *)"";
2156 di = dict_find(dict, key, -1);
2157 if (di != NULL)
2158 {
2159 if (error_if_locked(di->di_tv.v_lock,
2160 e_cannot_change_dict_item))
2161 return FAIL;
2162 // overwrite existing value
2163 clear_tv(&di->di_tv);
2164 di->di_tv = *tv;
2165 }
2166 else
2167 {
2168 if (error_if_locked(dict->dv_lock, e_cannot_change_dict))
2169 return FAIL;
2170 // add to dict, only fails when out of memory
2171 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
2172 return NOTDONE;
2173 clear_tv(tv);
2174 }
2175 }
2176 else if (dest_type == VAR_BLOB)
2177 {
2178 long lidx = (long)tv_idx->vval.v_number;
2179 blob_T *blob = tv_dest->vval.v_blob;
Bram Moolenaar65b0d162022-12-13 18:43:22 +00002180 varnumber_T nr;
2181 int error = FALSE;
2182 int len;
Bram Moolenaare08be092022-02-17 13:08:26 +00002183
2184 if (blob == NULL)
2185 {
2186 emsg(_(e_blob_not_set));
2187 return FAIL;
2188 }
2189 len = blob_len(blob);
2190 if (lidx < 0 && len + lidx >= 0)
2191 // negative index is relative to the end
2192 lidx = len + lidx;
2193
2194 // Can add one byte at the end.
2195 if (lidx < 0 || lidx > len)
2196 {
2197 semsg(_(e_blob_index_out_of_range_nr), lidx);
2198 return FAIL;
2199 }
2200 if (value_check_lock(blob->bv_lock, (char_u *)"blob", FALSE))
2201 return FAIL;
2202 nr = tv_get_number_chk(tv, &error);
2203 if (error)
2204 return FAIL;
2205 blob_set_append(blob, lidx, nr);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002206 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002207 else if (dest_type == VAR_CLASS || dest_type == VAR_OBJECT)
2208 {
2209 long idx = (long)tv_idx->vval.v_number;
2210 object_T *obj = tv_dest->vval.v_object;
2211 typval_T *otv = (typval_T *)(obj + 1);
Bram Moolenaar65b0d162022-12-13 18:43:22 +00002212 clear_tv(&otv[idx]);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002213 otv[idx] = *tv;
2214 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002215 else
2216 {
Bram Moolenaare08be092022-02-17 13:08:26 +00002217 status = FAIL;
2218 semsg(_(e_cannot_index_str), vartype_name(dest_type));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002219 }
2220 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002221
2222 clear_tv(tv_idx);
2223 clear_tv(tv_dest);
2224 ectx->ec_stack.ga_len -= 3;
2225 if (status == FAIL)
2226 {
2227 clear_tv(tv);
2228 return FAIL;
2229 }
2230 return OK;
2231}
2232
2233/*
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002234 * Store a value in a list or blob range.
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002235 */
2236 static int
2237execute_storerange(isn_T *iptr, ectx_T *ectx)
2238{
2239 typval_T *tv;
2240 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2241 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2242 typval_T *tv_dest = STACK_TV_BOT(-1);
2243 int status = OK;
2244
2245 // Stack contains:
2246 // -4 value to be stored
2247 // -3 first index or "none"
2248 // -2 second index or "none"
2249 // -1 destination list or blob
2250 tv = STACK_TV_BOT(-4);
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002251 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002252 if (tv_dest->v_type == VAR_LIST)
2253 {
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002254 long n1;
2255 long n2;
2256 listitem_T *li1;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002257
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002258 n1 = (long)tv_get_number_chk(tv_idx1, NULL);
2259 if (tv_idx2->v_type == VAR_SPECIAL
2260 && tv_idx2->vval.v_number == VVAL_NONE)
2261 n2 = list_len(tv_dest->vval.v_list) - 1;
2262 else
2263 n2 = (long)tv_get_number_chk(tv_idx2, NULL);
2264
Bram Moolenaar22ebd172022-04-01 15:26:58 +01002265 li1 = check_range_index_one(tv_dest->vval.v_list, &n1, TRUE, FALSE);
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002266 if (li1 == NULL)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002267 status = FAIL;
2268 else
2269 {
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002270 status = check_range_index_two(tv_dest->vval.v_list,
2271 &n1, li1, &n2, FALSE);
2272 if (status != FAIL)
2273 status = list_assign_range(
2274 tv_dest->vval.v_list,
2275 tv->vval.v_list,
2276 n1,
2277 n2,
2278 tv_idx2->v_type == VAR_SPECIAL,
2279 (char_u *)"=",
2280 (char_u *)"[unknown]");
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002281 }
2282 }
2283 else if (tv_dest->v_type == VAR_BLOB)
2284 {
2285 varnumber_T n1;
2286 varnumber_T n2;
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002287 long bloblen;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002288
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002289 n1 = tv_get_number_chk(tv_idx1, NULL);
2290 if (tv_idx2->v_type == VAR_SPECIAL
2291 && tv_idx2->vval.v_number == VVAL_NONE)
2292 n2 = blob_len(tv_dest->vval.v_blob) - 1;
2293 else
2294 n2 = tv_get_number_chk(tv_idx2, NULL);
2295 bloblen = blob_len(tv_dest->vval.v_blob);
2296
2297 if (check_blob_index(bloblen, n1, FALSE) == FAIL
2298 || check_blob_range(bloblen, n1, n2, FALSE) == FAIL)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002299 status = FAIL;
2300 else
Bram Moolenaar2995e5c2022-03-18 21:41:47 +00002301 status = blob_set_range(tv_dest->vval.v_blob, n1, n2, tv);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002302 }
2303 else
2304 {
2305 status = FAIL;
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002306 emsg(_(e_list_or_blob_required));
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002307 }
2308
2309 clear_tv(tv_idx1);
2310 clear_tv(tv_idx2);
2311 clear_tv(tv_dest);
2312 ectx->ec_stack.ga_len -= 4;
2313 clear_tv(tv);
2314
2315 return status;
2316}
2317
2318/*
2319 * Unlet item in list or dict variable.
2320 */
2321 static int
2322execute_unletindex(isn_T *iptr, ectx_T *ectx)
2323{
2324 typval_T *tv_idx = STACK_TV_BOT(-2);
2325 typval_T *tv_dest = STACK_TV_BOT(-1);
2326 int status = OK;
2327
2328 // Stack contains:
2329 // -2 index
2330 // -1 dict or list
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002331 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002332 if (tv_dest->v_type == VAR_DICT)
2333 {
2334 // unlet a dict item, index must be a string
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002335 if (tv_idx->v_type != VAR_STRING && tv_idx->v_type != VAR_NUMBER)
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002336 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002337 semsg(_(e_expected_str_but_got_str),
2338 vartype_name(VAR_STRING),
2339 vartype_name(tv_idx->v_type));
2340 status = FAIL;
2341 }
2342 else
2343 {
2344 dict_T *d = tv_dest->vval.v_dict;
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002345 char_u *key;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002346 dictitem_T *di = NULL;
2347
2348 if (d != NULL && value_check_lock(
2349 d->dv_lock, NULL, FALSE))
2350 status = FAIL;
2351 else
2352 {
Bram Moolenaarea5c8982022-02-17 14:42:02 +00002353 if (tv_idx->v_type == VAR_STRING)
2354 {
2355 key = tv_idx->vval.v_string;
2356 if (key == NULL)
2357 key = (char_u *)"";
2358 }
2359 else
2360 {
2361 key = tv_get_string(tv_idx);
2362 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002363 if (d != NULL)
2364 di = dict_find(d, key, (int)STRLEN(key));
2365 if (di == NULL)
2366 {
2367 // NULL dict is equivalent to empty dict
2368 semsg(_(e_key_not_present_in_dictionary),
2369 key);
2370 status = FAIL;
2371 }
2372 else if (var_check_fixed(di->di_flags,
2373 NULL, FALSE)
2374 || var_check_ro(di->di_flags,
2375 NULL, FALSE))
2376 status = FAIL;
2377 else
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002378 dictitem_remove(d, di, "unlet");
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002379 }
2380 }
2381 }
2382 else if (tv_dest->v_type == VAR_LIST)
2383 {
2384 // unlet a List item, index must be a number
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002385 if (check_for_number(tv_idx) == FAIL)
2386 {
2387 status = FAIL;
2388 }
2389 else
2390 {
2391 list_T *l = tv_dest->vval.v_list;
2392 long n = (long)tv_idx->vval.v_number;
2393
2394 if (l != NULL && value_check_lock(
2395 l->lv_lock, NULL, FALSE))
2396 status = FAIL;
2397 else
2398 {
2399 listitem_T *li = list_find(l, n);
2400
2401 if (li == NULL)
2402 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002403 semsg(_(e_list_index_out_of_range_nr), n);
2404 status = FAIL;
2405 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002406 else
2407 listitem_remove(l, li);
2408 }
2409 }
2410 }
2411 else
2412 {
2413 status = FAIL;
2414 semsg(_(e_cannot_index_str),
2415 vartype_name(tv_dest->v_type));
2416 }
2417
2418 clear_tv(tv_idx);
2419 clear_tv(tv_dest);
2420 ectx->ec_stack.ga_len -= 2;
2421
2422 return status;
2423}
2424
2425/*
2426 * Unlet a range of items in a list variable.
2427 */
2428 static int
2429execute_unletrange(isn_T *iptr, ectx_T *ectx)
2430{
2431 // Stack contains:
2432 // -3 index1
2433 // -2 index2
2434 // -1 dict or list
2435 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2436 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2437 typval_T *tv_dest = STACK_TV_BOT(-1);
2438 int status = OK;
2439
2440 if (tv_dest->v_type == VAR_LIST)
2441 {
2442 // indexes must be a number
2443 SOURCING_LNUM = iptr->isn_lnum;
2444 if (check_for_number(tv_idx1) == FAIL
2445 || (tv_idx2->v_type != VAR_SPECIAL
2446 && check_for_number(tv_idx2) == FAIL))
2447 {
2448 status = FAIL;
2449 }
2450 else
2451 {
2452 list_T *l = tv_dest->vval.v_list;
2453 long n1 = (long)tv_idx1->vval.v_number;
2454 long n2 = tv_idx2->v_type == VAR_SPECIAL
2455 ? 0 : (long)tv_idx2->vval.v_number;
2456 listitem_T *li;
2457
2458 li = list_find_index(l, &n1);
2459 if (li == NULL)
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002460 {
2461 semsg(_(e_list_index_out_of_range_nr),
2462 (long)tv_idx1->vval.v_number);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002463 status = FAIL;
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002464 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002465 else
2466 {
2467 if (n1 < 0)
2468 n1 = list_idx_of_item(l, li);
2469 if (n2 < 0)
2470 {
2471 listitem_T *li2 = list_find(l, n2);
2472
2473 if (li2 == NULL)
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002474 {
2475 semsg(_(e_list_index_out_of_range_nr), n2);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002476 status = FAIL;
Bram Moolenaar6296d1e2022-02-17 16:30:11 +00002477 }
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002478 else
2479 n2 = list_idx_of_item(l, li2);
2480 }
2481 if (status != FAIL
2482 && tv_idx2->v_type != VAR_SPECIAL
2483 && n2 < n1)
2484 {
2485 semsg(_(e_list_index_out_of_range_nr), n2);
2486 status = FAIL;
2487 }
Bram Moolenaar6b8c7ba2022-03-20 17:46:06 +00002488 if (status != FAIL)
2489 list_unlet_range(l, li, n1,
2490 tv_idx2->v_type != VAR_SPECIAL, n2);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002491 }
2492 }
2493 }
2494 else
2495 {
2496 status = FAIL;
2497 SOURCING_LNUM = iptr->isn_lnum;
2498 semsg(_(e_cannot_index_str),
2499 vartype_name(tv_dest->v_type));
2500 }
2501
2502 clear_tv(tv_idx1);
2503 clear_tv(tv_idx2);
2504 clear_tv(tv_dest);
2505 ectx->ec_stack.ga_len -= 3;
2506
2507 return status;
2508}
2509
2510/*
2511 * Top of a for loop.
2512 */
2513 static int
2514execute_for(isn_T *iptr, ectx_T *ectx)
2515{
2516 typval_T *tv;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002517 int jump = FALSE;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002518 typval_T *ltv = STACK_TV_BOT(-1);
2519 typval_T *idxtv =
Bram Moolenaarcc341812022-09-19 15:54:34 +01002520 STACK_TV_VAR(iptr->isn_arg.forloop.for_loop_idx);
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002521
2522 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
2523 return FAIL;
2524 if (ltv->v_type == VAR_LIST)
2525 {
2526 list_T *list = ltv->vval.v_list;
2527
2528 // push the next item from the list
2529 ++idxtv->vval.v_number;
2530 if (list == NULL
2531 || idxtv->vval.v_number >= list->lv_len)
2532 {
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002533 jump = TRUE;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002534 }
2535 else if (list->lv_first == &range_list_item)
2536 {
2537 // non-materialized range() list
2538 tv = STACK_TV_BOT(0);
2539 tv->v_type = VAR_NUMBER;
2540 tv->v_lock = 0;
2541 tv->vval.v_number = list_find_nr(
2542 list, idxtv->vval.v_number, NULL);
2543 ++ectx->ec_stack.ga_len;
2544 }
2545 else
2546 {
2547 listitem_T *li = list_find(list,
2548 idxtv->vval.v_number);
2549
2550 copy_tv(&li->li_tv, STACK_TV_BOT(0));
2551 ++ectx->ec_stack.ga_len;
2552 }
2553 }
2554 else if (ltv->v_type == VAR_STRING)
2555 {
2556 char_u *str = ltv->vval.v_string;
2557
2558 // The index is for the last byte of the previous
2559 // character.
2560 ++idxtv->vval.v_number;
2561 if (str == NULL || str[idxtv->vval.v_number] == NUL)
2562 {
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002563 jump = TRUE;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002564 }
2565 else
2566 {
2567 int clen = mb_ptr2len(str + idxtv->vval.v_number);
2568
2569 // Push the next character from the string.
2570 tv = STACK_TV_BOT(0);
2571 tv->v_type = VAR_STRING;
2572 tv->vval.v_string = vim_strnsave(
2573 str + idxtv->vval.v_number, clen);
2574 ++ectx->ec_stack.ga_len;
2575 idxtv->vval.v_number += clen - 1;
2576 }
2577 }
2578 else if (ltv->v_type == VAR_BLOB)
2579 {
2580 blob_T *blob = ltv->vval.v_blob;
2581
2582 // When we get here the first time make a copy of the
2583 // blob, so that the iteration still works when it is
2584 // changed.
2585 if (idxtv->vval.v_number == -1 && blob != NULL)
2586 {
2587 blob_copy(blob, ltv);
2588 blob_unref(blob);
2589 blob = ltv->vval.v_blob;
2590 }
2591
2592 // The index is for the previous byte.
2593 ++idxtv->vval.v_number;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002594 if (blob == NULL || idxtv->vval.v_number >= blob_len(blob))
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002595 {
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002596 jump = TRUE;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002597 }
2598 else
2599 {
2600 // Push the next byte from the blob.
2601 tv = STACK_TV_BOT(0);
2602 tv->v_type = VAR_NUMBER;
2603 tv->vval.v_number = blob_get(blob,
2604 idxtv->vval.v_number);
2605 ++ectx->ec_stack.ga_len;
2606 }
2607 }
2608 else
2609 {
2610 semsg(_(e_for_loop_on_str_not_supported),
2611 vartype_name(ltv->v_type));
2612 return FAIL;
2613 }
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002614
2615 if (jump)
2616 {
2617 // past the end of the list/string/blob, jump to "endfor"
2618 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
2619 may_restore_cmdmod(&ectx->ec_funclocal);
2620 }
2621 else
2622 {
2623 // Store the current number of funcrefs, this may be used in
2624 // ISN_LOOPEND. The variable index is always one more than the loop
2625 // variable index.
Bram Moolenaarcc341812022-09-19 15:54:34 +01002626 tv = STACK_TV_VAR(iptr->isn_arg.forloop.for_loop_idx + 1);
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002627 tv->vval.v_number = ectx->ec_funcrefs.ga_len;
2628 }
2629
2630 return OK;
2631}
2632
2633/*
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002634 * Code for handling variables declared inside a loop and used in a closure.
2635 * This is very similar to what is done with funcstack_T. The difference is
2636 * that the funcstack_T has the scope of a function, while a loopvars_T has the
2637 * scope of the block inside a loop and each loop may have its own.
2638 */
2639
2640// Double linked list of loopvars_T in use.
2641static loopvars_T *first_loopvars = NULL;
2642
2643 static void
2644add_loopvars_to_list(loopvars_T *loopvars)
2645{
2646 // Link in list of loopvarss.
2647 if (first_loopvars != NULL)
2648 first_loopvars->lvs_prev = loopvars;
2649 loopvars->lvs_next = first_loopvars;
2650 loopvars->lvs_prev = NULL;
2651 first_loopvars = loopvars;
2652}
2653
2654 static void
2655remove_loopvars_from_list(loopvars_T *loopvars)
2656{
2657 if (loopvars->lvs_prev == NULL)
2658 first_loopvars = loopvars->lvs_next;
2659 else
2660 loopvars->lvs_prev->lvs_next = loopvars->lvs_next;
2661 if (loopvars->lvs_next != NULL)
2662 loopvars->lvs_next->lvs_prev = loopvars->lvs_prev;
2663}
2664
2665/*
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002666 * End of a for or while loop: Handle any variables used by a closure.
2667 */
2668 static int
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002669execute_endloop(isn_T *iptr, ectx_T *ectx)
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002670{
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002671 endloop_T *endloop = &iptr->isn_arg.endloop;
2672 typval_T *tv_refcount = STACK_TV_VAR(endloop->end_funcref_idx);
2673 int prev_closure_count = tv_refcount->vval.v_number;
Bram Moolenaarcc341812022-09-19 15:54:34 +01002674 int depth = endloop->end_depth;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002675 garray_T *gap = &ectx->ec_funcrefs;
2676 int closure_in_use = FALSE;
2677 loopvars_T *loopvars;
2678 typval_T *stack;
2679 int idx;
2680
Bram Moolenaarcc341812022-09-19 15:54:34 +01002681 // Check if any created closure is still being referenced and loopvars have
2682 // not been saved yet for the current depth.
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002683 for (idx = prev_closure_count; idx < gap->ga_len; ++idx)
2684 {
2685 partial_T *pt = ((partial_T **)gap->ga_data)[idx];
2686
Bram Moolenaarcc341812022-09-19 15:54:34 +01002687 if (pt->pt_refcount > 1 && pt->pt_loopvars[depth] == NULL)
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002688 {
2689 int refcount = pt->pt_refcount;
2690 int i;
2691
2692 // A Reference in a variable inside the loop doesn't count, it gets
2693 // unreferenced at the end of the loop.
2694 for (i = 0; i < endloop->end_var_count; ++i)
2695 {
2696 typval_T *stv = STACK_TV_VAR(endloop->end_var_idx + i);
2697
2698 if (stv->v_type == VAR_PARTIAL && pt == stv->vval.v_partial)
2699 --refcount;
2700 }
2701 if (refcount > 1)
2702 {
2703 closure_in_use = TRUE;
2704 break;
2705 }
2706 }
2707 }
2708
2709 // If no function reference were created since the start of the loop block
2710 // or it is no longer referenced there is nothing to do.
2711 if (!closure_in_use)
2712 return OK;
2713
2714 // A closure is using variables declared inside the loop.
2715 // Move them to the called function.
2716 loopvars = ALLOC_CLEAR_ONE(loopvars_T);
2717 if (loopvars == NULL)
2718 return FAIL;
2719
2720 loopvars->lvs_ga.ga_len = endloop->end_var_count;
2721 stack = ALLOC_CLEAR_MULT(typval_T, loopvars->lvs_ga.ga_len);
2722 loopvars->lvs_ga.ga_data = stack;
2723 if (stack == NULL)
2724 {
2725 vim_free(loopvars);
2726 return FAIL;
2727 }
2728 add_loopvars_to_list(loopvars);
2729
2730 // Move the variable values.
2731 for (idx = 0; idx < endloop->end_var_count; ++idx)
2732 {
2733 typval_T *tv = STACK_TV_VAR(endloop->end_var_idx + idx);
2734
2735 *(stack + idx) = *tv;
2736 tv->v_type = VAR_UNKNOWN;
2737 }
2738
2739 for (idx = prev_closure_count; idx < gap->ga_len; ++idx)
2740 {
2741 partial_T *pt = ((partial_T **)gap->ga_data)[idx];
2742
Bram Moolenaarcc341812022-09-19 15:54:34 +01002743 if (pt->pt_refcount > 1 && pt->pt_loopvars[depth] == NULL)
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002744 {
2745 ++loopvars->lvs_refcount;
Bram Moolenaarcc341812022-09-19 15:54:34 +01002746 pt->pt_loopvars[depth] = loopvars;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002747
Bram Moolenaarcc341812022-09-19 15:54:34 +01002748 pt->pt_outer.out_loop[depth].stack = &loopvars->lvs_ga;
2749 pt->pt_outer.out_loop[depth].var_idx -=
2750 ectx->ec_frame_idx + STACK_FRAME_SIZE + endloop->end_var_idx;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002751 }
2752 }
Bram Moolenaarb46c0832022-09-15 17:19:37 +01002753
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00002754 return OK;
2755}
2756
2757/*
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002758 * Called when a partial is freed or its reference count goes down to one. The
2759 * loopvars may be the only reference to the partials in the local variables.
2760 * Go over all of them, the funcref and can be freed if all partials
2761 * referencing the loopvars have a reference count of one.
Bram Moolenaarcc341812022-09-19 15:54:34 +01002762 * Return TRUE if it was freed.
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002763 */
Bram Moolenaarcc341812022-09-19 15:54:34 +01002764 int
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002765loopvars_check_refcount(loopvars_T *loopvars)
2766{
2767 int i;
2768 garray_T *gap = &loopvars->lvs_ga;
2769 int done = 0;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002770 typval_T *stack = gap->ga_data;
2771
Bram Moolenaarcc341812022-09-19 15:54:34 +01002772 if (loopvars->lvs_refcount > loopvars->lvs_min_refcount)
2773 return FALSE;
2774 for (i = 0; i < gap->ga_len; ++i)
2775 {
2776 typval_T *tv = ((typval_T *)gap->ga_data) + i;
2777
2778 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL
2779 && tv->vval.v_partial->pt_refcount == 1)
2780 {
2781 int depth;
2782
2783 for (depth = 0; depth < MAX_LOOP_DEPTH; ++depth)
2784 if (tv->vval.v_partial->pt_loopvars[depth] == loopvars)
2785 ++done;
2786 }
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002787 }
Bram Moolenaarcc341812022-09-19 15:54:34 +01002788 if (done != loopvars->lvs_min_refcount)
2789 return FALSE;
2790
2791 // All partials referencing the loopvars have a reference count of
2792 // one, thus the loopvars is no longer of use.
2793 stack = gap->ga_data;
2794 for (i = 0; i < gap->ga_len; ++i)
2795 clear_tv(stack + i);
2796 vim_free(stack);
2797 remove_loopvars_from_list(loopvars);
2798 vim_free(loopvars);
2799 return TRUE;
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01002800}
2801
2802/*
2803 * For garbage collecting: set references in all variables referenced by
2804 * all loopvars.
2805 */
2806 int
2807set_ref_in_loopvars(int copyID)
2808{
2809 loopvars_T *loopvars;
2810
2811 for (loopvars = first_loopvars; loopvars != NULL;
2812 loopvars = loopvars->lvs_next)
2813 {
2814 typval_T *stack = loopvars->lvs_ga.ga_data;
2815 int i;
2816
2817 for (i = 0; i < loopvars->lvs_ga.ga_len; ++i)
2818 if (set_ref_in_item(stack + i, copyID, NULL, NULL))
2819 return TRUE; // abort
2820 }
2821 return FALSE;
2822}
2823
2824/*
Bram Moolenaar06b77222022-01-25 15:51:56 +00002825 * Load instruction for w:/b:/g:/t: variable.
2826 * "isn_type" is used instead of "iptr->isn_type".
2827 */
2828 static int
2829load_namespace_var(ectx_T *ectx, isntype_T isn_type, isn_T *iptr)
2830{
2831 dictitem_T *di = NULL;
2832 hashtab_T *ht = NULL;
2833 char namespace;
2834
2835 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
2836 return NOTDONE;
2837 switch (isn_type)
2838 {
2839 case ISN_LOADG:
2840 ht = get_globvar_ht();
2841 namespace = 'g';
2842 break;
2843 case ISN_LOADB:
2844 ht = &curbuf->b_vars->dv_hashtab;
2845 namespace = 'b';
2846 break;
2847 case ISN_LOADW:
2848 ht = &curwin->w_vars->dv_hashtab;
2849 namespace = 'w';
2850 break;
2851 case ISN_LOADT:
2852 ht = &curtab->tp_vars->dv_hashtab;
2853 namespace = 't';
2854 break;
2855 default: // Cannot reach here
2856 return NOTDONE;
2857 }
2858 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
2859
Bram Moolenaar06b77222022-01-25 15:51:56 +00002860 if (di == NULL)
2861 {
Bram Moolenaarfe732552022-02-22 19:39:13 +00002862 if (isn_type == ISN_LOADG)
2863 {
2864 ufunc_T *ufunc = find_func(iptr->isn_arg.string, TRUE);
2865
2866 // g:Something could be a function
2867 if (ufunc != NULL)
2868 {
2869 typval_T *tv = STACK_TV_BOT(0);
2870
2871 ++ectx->ec_stack.ga_len;
2872 tv->v_type = VAR_FUNC;
2873 tv->vval.v_string = alloc(STRLEN(iptr->isn_arg.string) + 3);
2874 if (tv->vval.v_string == NULL)
2875 return FAIL;
2876 STRCPY(tv->vval.v_string, "g:");
2877 STRCPY(tv->vval.v_string + 2, iptr->isn_arg.string);
2878 return OK;
2879 }
2880 }
Bram Moolenaar06b77222022-01-25 15:51:56 +00002881 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarfe732552022-02-22 19:39:13 +00002882 if (vim_strchr(iptr->isn_arg.string, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar06b77222022-01-25 15:51:56 +00002883 // no check if the item exists in the script but
2884 // isn't exported, it is too complicated
Bram Moolenaarfe732552022-02-22 19:39:13 +00002885 semsg(_(e_item_not_found_in_script_str), iptr->isn_arg.string);
Bram Moolenaar06b77222022-01-25 15:51:56 +00002886 else
2887 semsg(_(e_undefined_variable_char_str),
Bram Moolenaarfe732552022-02-22 19:39:13 +00002888 namespace, iptr->isn_arg.string);
Bram Moolenaar06b77222022-01-25 15:51:56 +00002889 return FAIL;
2890 }
2891 else
2892 {
2893 copy_tv(&di->di_tv, STACK_TV_BOT(0));
2894 ++ectx->ec_stack.ga_len;
2895 }
2896 return OK;
2897}
2898
2899/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02002900 * Execute instructions in execution context "ectx".
2901 * Return OK or FAIL;
2902 */
2903 static int
2904exec_instructions(ectx_T *ectx)
2905{
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002906 int ret = FAIL;
2907 int save_trylevel_at_start = ectx->ec_trylevel_at_start;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02002908 int dict_stack_len_at_start = dict_stack.ga_len;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01002909
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02002910 // Start execution at the first instruction.
Bram Moolenaar4c137212021-04-19 16:48:48 +02002911 ectx->ec_iidx = 0;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01002912
Bram Moolenaarff652882021-05-16 15:24:49 +02002913 // Only catch exceptions in this instruction list.
2914 ectx->ec_trylevel_at_start = trylevel;
2915
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002916 for (;;)
2917 {
Bram Moolenaara7490192021-07-22 12:26:14 +02002918 static int breakcheck_count = 0; // using "static" makes it faster
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002919 isn_T *iptr;
Bram Moolenaara7490192021-07-22 12:26:14 +02002920 typval_T *tv;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002921
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002922 if (unlikely(++breakcheck_count >= 100))
Bram Moolenaar270d0382020-05-15 21:42:53 +02002923 {
2924 line_breakcheck();
2925 breakcheck_count = 0;
2926 }
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002927 if (unlikely(got_int))
Bram Moolenaar20431c92020-03-20 18:39:46 +01002928 {
2929 // Turn CTRL-C into an exception.
2930 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +01002931 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002932 goto theend;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002933 did_throw = TRUE;
2934 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002935
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002936 if (unlikely(did_emsg && msg_list != NULL && *msg_list != NULL))
Bram Moolenaara26b9702020-04-18 19:53:28 +02002937 {
2938 // Turn an error message into an exception.
2939 did_emsg = FALSE;
2940 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002941 goto theend;
Bram Moolenaara26b9702020-04-18 19:53:28 +02002942 did_throw = TRUE;
2943 *msg_list = NULL;
2944 }
2945
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002946 if (unlikely(did_throw))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002947 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002948 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002949 trycmd_T *trycmd = NULL;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002950 int index = trystack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002951
2952 // An exception jumps to the first catch, finally, or returns from
2953 // the current function.
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002954 while (index > 0)
2955 {
2956 trycmd = ((trycmd_T *)trystack->ga_data) + index - 1;
Bram Moolenaar834193a2021-06-30 20:39:15 +02002957 if (!trycmd->tcd_in_catch || trycmd->tcd_finally_idx != 0)
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002958 break;
2959 // In the catch and finally block of this try we have to go up
2960 // one level.
2961 --index;
2962 trycmd = NULL;
2963 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02002964 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002965 {
Bram Moolenaar834193a2021-06-30 20:39:15 +02002966 if (trycmd->tcd_in_catch)
2967 {
2968 // exception inside ":catch", jump to ":finally" once
2969 ectx->ec_iidx = trycmd->tcd_finally_idx;
2970 trycmd->tcd_finally_idx = 0;
2971 }
2972 else
2973 // jump to first ":catch"
2974 ectx->ec_iidx = trycmd->tcd_catch_idx;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002975 trycmd->tcd_in_catch = TRUE;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02002976 did_throw = FALSE; // don't come back here until :endtry
2977 trycmd->tcd_did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002978 }
2979 else
2980 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02002981 // Not inside try or need to return from current functions.
2982 // Push a dummy return value.
Bram Moolenaar35578162021-08-02 19:10:38 +02002983 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002984 goto theend;
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02002985 tv = STACK_TV_BOT(0);
2986 tv->v_type = VAR_NUMBER;
2987 tv->vval.v_number = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002988 ++ectx->ec_stack.ga_len;
2989 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002990 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02002991 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +01002992 need_rethrow = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002993 if (handle_closure_in_use(ectx, FALSE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002994 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002995 goto done;
2996 }
2997
Bram Moolenaar4c137212021-04-19 16:48:48 +02002998 if (func_return(ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002999 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003000 }
3001 continue;
3002 }
3003
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003004 /*
3005 * Big switch on the instruction. Most compilers will be turning this
3006 * into an efficient lookup table, since the "case" values are an enum
3007 * with sequential numbers. It may look ugly, but it should be the
3008 * most efficient way.
3009 */
Bram Moolenaar4c137212021-04-19 16:48:48 +02003010 iptr = &ectx->ec_instr[ectx->ec_iidx++];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003011 switch (iptr->isn_type)
3012 {
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00003013 // Constructor, first instruction in a new() method.
Bram Moolenaar00b28d62022-12-08 15:32:33 +00003014 case ISN_CONSTRUCT:
3015 // "this" is always the local variable at index zero
3016 tv = STACK_TV_VAR(0);
3017 tv->v_type = VAR_OBJECT;
3018 tv->vval.v_object = alloc_clear(
3019 iptr->isn_arg.construct.construct_size);
3020 tv->vval.v_object->obj_class =
3021 iptr->isn_arg.construct.construct_class;
Bram Moolenaard28d7b92022-12-08 20:42:00 +00003022 ++tv->vval.v_object->obj_class->class_refcount;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00003023 tv->vval.v_object->obj_refcount = 1;
Bram Moolenaard28d7b92022-12-08 20:42:00 +00003024 object_created(tv->vval.v_object);
Bram Moolenaar00b28d62022-12-08 15:32:33 +00003025 break;
3026
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003027 // execute Ex command line
3028 case ISN_EXEC:
Bram Moolenaaraacc9662021-08-13 19:40:51 +02003029 if (exec_command(iptr) == FAIL)
3030 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003031 break;
3032
Bram Moolenaar20677332021-06-06 17:02:53 +02003033 // execute Ex command line split at NL characters.
3034 case ISN_EXEC_SPLIT:
3035 {
3036 source_cookie_T cookie;
Bram Moolenaar518df272021-06-06 17:34:13 +02003037 char_u *line;
Bram Moolenaar20677332021-06-06 17:02:53 +02003038
3039 SOURCING_LNUM = iptr->isn_lnum;
3040 CLEAR_FIELD(cookie);
3041 cookie.sourcing_lnum = iptr->isn_lnum - 1;
3042 cookie.nextline = iptr->isn_arg.string;
Bram Moolenaar518df272021-06-06 17:34:13 +02003043 line = get_split_sourceline(0, &cookie, 0, 0);
3044 if (do_cmdline(line,
Bram Moolenaar20677332021-06-06 17:02:53 +02003045 get_split_sourceline, &cookie,
3046 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED)
3047 == FAIL
3048 || did_emsg)
Bram Moolenaar518df272021-06-06 17:34:13 +02003049 {
3050 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02003051 goto on_error;
Bram Moolenaar518df272021-06-06 17:34:13 +02003052 }
3053 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02003054 }
3055 break;
3056
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00003057 // execute Ex command line that is only a range
3058 case ISN_EXECRANGE:
3059 {
3060 exarg_T ea;
3061 char *error = NULL;
3062
3063 CLEAR_FIELD(ea);
3064 ea.cmdidx = CMD_SIZE;
3065 ea.addr_type = ADDR_LINES;
3066 ea.cmd = iptr->isn_arg.string;
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00003067 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00003068 parse_cmd_address(&ea, &error, FALSE);
Bram Moolenaar01a4dcb2021-12-04 13:15:10 +00003069 if (ea.cmd == NULL)
3070 goto on_error;
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00003071 // error is always NULL when using ADDR_LINES
3072 error = ex_range_without_command(&ea);
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00003073 if (error != NULL)
3074 {
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00003075 emsg(error);
3076 goto on_error;
3077 }
3078 }
3079 break;
3080
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02003081 // Evaluate an expression with legacy syntax, push it onto the
3082 // stack.
3083 case ISN_LEGACY_EVAL:
3084 {
3085 char_u *arg = iptr->isn_arg.string;
3086 int res;
3087 int save_flags = cmdmod.cmod_flags;
3088
Bram Moolenaar35578162021-08-02 19:10:38 +02003089 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003090 goto theend;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02003091 tv = STACK_TV_BOT(0);
3092 init_tv(tv);
3093 cmdmod.cmod_flags |= CMOD_LEGACY;
3094 res = eval0(arg, tv, NULL, &EVALARG_EVALUATE);
3095 cmdmod.cmod_flags = save_flags;
3096 if (res == FAIL)
3097 goto on_error;
3098 ++ectx->ec_stack.ga_len;
3099 }
3100 break;
3101
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003102 // push typeval VAR_INSTR with instructions to be executed
3103 case ISN_INSTR:
3104 {
Bram Moolenaar35578162021-08-02 19:10:38 +02003105 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003106 goto theend;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02003107 tv = STACK_TV_BOT(0);
3108 tv->vval.v_instr = ALLOC_ONE(instr_T);
3109 if (tv->vval.v_instr == NULL)
3110 goto on_error;
3111 ++ectx->ec_stack.ga_len;
3112
3113 tv->v_type = VAR_INSTR;
3114 tv->vval.v_instr->instr_ectx = ectx;
3115 tv->vval.v_instr->instr_instr = iptr->isn_arg.instr;
3116 }
3117 break;
3118
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003119 case ISN_SOURCE:
3120 {
Bram Moolenaar89445512022-04-14 12:58:23 +01003121 int notused;
LemonBoy77142312022-04-15 20:50:46 +01003122
Bram Moolenaar89445512022-04-14 12:58:23 +01003123 SOURCING_LNUM = iptr->isn_lnum;
3124 if (may_load_script((int)iptr->isn_arg.number, &notused)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003125 == FAIL)
Bram Moolenaar89445512022-04-14 12:58:23 +01003126 goto on_error;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003127 }
3128 break;
3129
Bram Moolenaar4c137212021-04-19 16:48:48 +02003130 // execute :substitute with an expression
3131 case ISN_SUBSTITUTE:
3132 {
3133 subs_T *subs = &iptr->isn_arg.subs;
3134 source_cookie_T cookie;
3135 struct subs_expr_S *save_instr = substitute_instr;
3136 struct subs_expr_S subs_instr;
3137 int res;
3138
3139 subs_instr.subs_ectx = ectx;
3140 subs_instr.subs_instr = subs->subs_instr;
3141 subs_instr.subs_status = OK;
3142 substitute_instr = &subs_instr;
3143
3144 SOURCING_LNUM = iptr->isn_lnum;
3145 // This is very much like ISN_EXEC
3146 CLEAR_FIELD(cookie);
3147 cookie.sourcing_lnum = iptr->isn_lnum - 1;
3148 res = do_cmdline(subs->subs_cmd,
3149 getsourceline, &cookie,
3150 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
3151 substitute_instr = save_instr;
3152
3153 if (res == FAIL || did_emsg
3154 || subs_instr.subs_status == FAIL)
3155 goto on_error;
3156 }
3157 break;
3158
3159 case ISN_FINISH:
3160 goto done;
3161
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02003162 case ISN_REDIRSTART:
3163 // create a dummy entry for var_redir_str()
3164 if (alloc_redir_lval() == FAIL)
3165 goto on_error;
3166
3167 // The output is stored in growarray "redir_ga" until
3168 // redirection ends.
3169 init_redir_ga();
3170 redir_vname = 1;
3171 break;
3172
3173 case ISN_REDIREND:
3174 {
3175 char_u *res = get_clear_redir_ga();
3176
3177 // End redirection, put redirected text on the stack.
3178 clear_redir_lval();
3179 redir_vname = 0;
3180
Bram Moolenaar35578162021-08-02 19:10:38 +02003181 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02003182 {
3183 vim_free(res);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003184 goto theend;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02003185 }
3186 tv = STACK_TV_BOT(0);
3187 tv->v_type = VAR_STRING;
3188 tv->vval.v_string = res;
3189 ++ectx->ec_stack.ga_len;
3190 }
3191 break;
3192
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003193 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02003194#ifdef FEAT_QUICKFIX
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003195 force_abort = TRUE;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003196 if (trigger_cexpr_autocmd(iptr->isn_arg.number) == FAIL)
3197 goto on_error;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003198 force_abort = FALSE;
Bram Moolenaarb7c97812021-05-05 22:51:39 +02003199#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003200 break;
3201
3202 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02003203#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003204 {
3205 exarg_T ea;
3206 int res;
3207
3208 CLEAR_FIELD(ea);
3209 ea.cmdidx = iptr->isn_arg.cexpr.cexpr_ref->cer_cmdidx;
3210 ea.forceit = iptr->isn_arg.cexpr.cexpr_ref->cer_forceit;
3211 ea.cmdlinep = &iptr->isn_arg.cexpr.cexpr_ref->cer_cmdline;
3212 --ectx->ec_stack.ga_len;
3213 tv = STACK_TV_BOT(0);
Bram Moolenaarbd683e32022-07-18 17:49:03 +01003214 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003215 res = cexpr_core(&ea, tv);
3216 clear_tv(tv);
3217 if (res == FAIL)
3218 goto on_error;
3219 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02003220#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02003221 break;
3222
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003223 // execute Ex command from pieces on the stack
3224 case ISN_EXECCONCAT:
3225 {
3226 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +02003227 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003228 int pass;
3229 int i;
3230 char_u *cmd = NULL;
3231 char_u *str;
3232
3233 for (pass = 1; pass <= 2; ++pass)
3234 {
3235 for (i = 0; i < count; ++i)
3236 {
3237 tv = STACK_TV_BOT(i - count);
3238 str = tv->vval.v_string;
3239 if (str != NULL && *str != NUL)
3240 {
3241 if (pass == 2)
3242 STRCPY(cmd + len, str);
3243 len += STRLEN(str);
3244 }
3245 if (pass == 2)
3246 clear_tv(tv);
3247 }
3248 if (pass == 1)
3249 {
3250 cmd = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003251 if (unlikely(cmd == NULL))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003252 goto theend;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003253 len = 0;
3254 }
3255 }
3256
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02003257 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003258 do_cmdline_cmd(cmd);
3259 vim_free(cmd);
3260 }
3261 break;
3262
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003263 // execute :echo {string} ...
3264 case ISN_ECHO:
3265 {
3266 int count = iptr->isn_arg.echo.echo_count;
3267 int atstart = TRUE;
3268 int needclr = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003269 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003270
3271 for (idx = 0; idx < count; ++idx)
3272 {
3273 tv = STACK_TV_BOT(idx - count);
3274 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
3275 &atstart, &needclr);
3276 clear_tv(tv);
3277 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01003278 if (needclr)
3279 msg_clr_eos();
Bram Moolenaar4c137212021-04-19 16:48:48 +02003280 ectx->ec_stack.ga_len -= count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003281 }
3282 break;
3283
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003284 // :execute {string} ...
3285 // :echomsg {string} ...
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01003286 // :echowindow {string} ...
Bram Moolenaar7de62622021-08-07 15:05:47 +02003287 // :echoconsole {string} ...
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003288 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01003289 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003290 case ISN_ECHOMSG:
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01003291 case ISN_ECHOWINDOW:
Bram Moolenaar7de62622021-08-07 15:05:47 +02003292 case ISN_ECHOCONSOLE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003293 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01003294 {
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01003295 int count;
Bram Moolenaarad39c092020-02-26 18:23:43 +01003296 garray_T ga;
3297 char_u buf[NUMBUFLEN];
3298 char_u *p;
3299 int len;
3300 int failed = FALSE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003301 int idx;
Bram Moolenaarad39c092020-02-26 18:23:43 +01003302
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01003303 if (iptr->isn_type == ISN_ECHOWINDOW)
3304 count = iptr->isn_arg.echowin.ewin_count;
3305 else
3306 count = iptr->isn_arg.number;
Bram Moolenaarad39c092020-02-26 18:23:43 +01003307 ga_init2(&ga, 1, 80);
3308 for (idx = 0; idx < count; ++idx)
3309 {
3310 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003311 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01003312 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003313 if (tv->v_type == VAR_CHANNEL
3314 || tv->v_type == VAR_JOB)
3315 {
3316 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar68db9962021-05-09 23:19:22 +02003317 semsg(_(e_using_invalid_value_as_string_str),
3318 vartype_name(tv->v_type));
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003319 break;
3320 }
3321 else
3322 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01003323 }
3324 else
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003325 p = tv_stringify(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01003326
3327 len = (int)STRLEN(p);
Bram Moolenaar35578162021-08-02 19:10:38 +02003328 if (GA_GROW_FAILS(&ga, len + 2))
Bram Moolenaarad39c092020-02-26 18:23:43 +01003329 failed = TRUE;
3330 else
3331 {
3332 if (ga.ga_len > 0)
3333 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
3334 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
3335 ga.ga_len += len;
3336 }
3337 clear_tv(tv);
3338 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003339 ectx->ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003340 if (failed)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003341 {
3342 ga_clear(&ga);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003343 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003344 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01003345
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02003346 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003347 {
3348 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02003349 {
3350 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003351 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01003352 if (did_emsg)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003353 {
3354 ga_clear(&ga);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01003355 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01003356 }
Bram Moolenaar430deb12020-08-23 16:29:11 +02003357 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003358 else
3359 {
3360 msg_sb_eol();
3361 if (iptr->isn_type == ISN_ECHOMSG)
3362 {
3363 msg_attr(ga.ga_data, echo_attr);
3364 out_flush();
3365 }
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01003366#ifdef HAS_MESSAGE_WINDOW
3367 else if (iptr->isn_type == ISN_ECHOWINDOW)
3368 {
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01003369 start_echowindow(
3370 iptr->isn_arg.echowin.ewin_time);
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01003371 msg_attr(ga.ga_data, echo_attr);
3372 end_echowindow();
3373 }
3374#endif
Bram Moolenaar7de62622021-08-07 15:05:47 +02003375 else if (iptr->isn_type == ISN_ECHOCONSOLE)
3376 {
3377 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data),
3378 TRUE);
3379 ui_write((char_u *)"\r\n", 2, TRUE);
3380 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003381 else
3382 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003383 SOURCING_LNUM = iptr->isn_lnum;
3384 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003385 }
3386 }
3387 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01003388 ga_clear(&ga);
3389 }
3390 break;
3391
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003392 // load local variable or argument
3393 case ISN_LOAD:
Bram Moolenaar35578162021-08-02 19:10:38 +02003394 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003395 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003396 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003397 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003398 break;
3399
3400 // load v: variable
3401 case ISN_LOADV:
Bram Moolenaar35578162021-08-02 19:10:38 +02003402 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003403 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003404 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003405 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003406 break;
3407
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003408 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003409 case ISN_LOADSCRIPT:
3410 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01003411 scriptref_T *sref = iptr->isn_arg.script.scriptref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003412 svar_T *sv;
3413
Bram Moolenaar6db660b2021-08-01 14:08:54 +02003414 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003415 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003416 goto theend;
Bram Moolenaar859cc212022-03-28 15:22:35 +01003417 allocate_if_null(sv);
Bram Moolenaar35578162021-08-02 19:10:38 +02003418 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003419 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003420 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003421 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003422 }
3423 break;
3424
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003425 // load s: variable in old script or autoload import
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003426 case ISN_LOADS:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003427 case ISN_LOADEXPORT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003428 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003429 int sid = iptr->isn_arg.loadstore.ls_sid;
3430 hashtab_T *ht = &SCRIPT_VARS(sid);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003431 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003432 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003433
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003434 if (di == NULL)
3435 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003436 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003437 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003438 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003439 }
3440 else
3441 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003442 if (iptr->isn_type == ISN_LOADEXPORT)
3443 {
3444 int idx = get_script_item_idx(sid, name, 0,
3445 NULL, NULL);
3446 svar_T *sv;
3447
3448 if (idx >= 0)
3449 {
3450 sv = ((svar_T *)SCRIPT_ITEM(sid)
3451 ->sn_var_vals.ga_data) + idx;
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003452 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003453 {
3454 SOURCING_LNUM = iptr->isn_lnum;
3455 semsg(_(e_item_not_exported_in_script_str),
3456 name);
3457 goto on_error;
3458 }
3459 }
3460 }
Bram Moolenaar35578162021-08-02 19:10:38 +02003461 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003462 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003463 copy_tv(&di->di_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003464 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003465 }
3466 }
3467 break;
3468
Bram Moolenaard3aac292020-04-19 14:32:17 +02003469 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003470 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02003471 case ISN_LOADB:
3472 case ISN_LOADW:
3473 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003474 {
Bram Moolenaar06b77222022-01-25 15:51:56 +00003475 int res = load_namespace_var(ectx, iptr->isn_type, iptr);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003476
Bram Moolenaar06b77222022-01-25 15:51:56 +00003477 if (res == NOTDONE)
Bram Moolenaarcbbc48f2022-01-18 12:58:28 +00003478 goto theend;
Bram Moolenaar06b77222022-01-25 15:51:56 +00003479 if (res == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003480 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003481 }
Bram Moolenaar06b77222022-01-25 15:51:56 +00003482
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003483 break;
3484
Bram Moolenaar03290b82020-12-19 16:30:44 +01003485 // load autoload variable
3486 case ISN_LOADAUTO:
3487 {
3488 char_u *name = iptr->isn_arg.string;
3489
Bram Moolenaar35578162021-08-02 19:10:38 +02003490 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003491 goto theend;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003492 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003493 if (eval_variable(name, (int)STRLEN(name), 0,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003494 STACK_TV_BOT(0), NULL, EVAL_VAR_VERBOSE) == FAIL)
Bram Moolenaar03290b82020-12-19 16:30:44 +01003495 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003496 ++ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003497 }
3498 break;
3499
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003500 // load g:/b:/w:/t: namespace
3501 case ISN_LOADGDICT:
3502 case ISN_LOADBDICT:
3503 case ISN_LOADWDICT:
3504 case ISN_LOADTDICT:
3505 {
3506 dict_T *d = NULL;
3507
3508 switch (iptr->isn_type)
3509 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02003510 case ISN_LOADGDICT: d = get_globvar_dict(); break;
3511 case ISN_LOADBDICT: d = curbuf->b_vars; break;
3512 case ISN_LOADWDICT: d = curwin->w_vars; break;
3513 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003514 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003515 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003516 }
Bram Moolenaar35578162021-08-02 19:10:38 +02003517 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003518 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003519 tv = STACK_TV_BOT(0);
3520 tv->v_type = VAR_DICT;
3521 tv->v_lock = 0;
3522 tv->vval.v_dict = d;
Bram Moolenaar1bd3cb22021-02-24 12:27:31 +01003523 ++d->dv_refcount;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003524 ++ectx->ec_stack.ga_len;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003525 }
3526 break;
3527
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003528 // load &option
3529 case ISN_LOADOPT:
3530 {
3531 typval_T optval;
3532 char_u *name = iptr->isn_arg.string;
3533
Bram Moolenaara8c17702020-04-01 21:17:24 +02003534 // This is not expected to fail, name is checked during
3535 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar35578162021-08-02 19:10:38 +02003536 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003537 goto theend;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003538 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003539 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003540 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003541 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003542 }
3543 break;
3544
3545 // load $ENV
3546 case ISN_LOADENV:
3547 {
3548 typval_T optval;
3549 char_u *name = iptr->isn_arg.string;
3550
Bram Moolenaar35578162021-08-02 19:10:38 +02003551 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003552 goto theend;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003553 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003554 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003555 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003556 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003557 }
3558 break;
3559
3560 // load @register
3561 case ISN_LOADREG:
Bram Moolenaar35578162021-08-02 19:10:38 +02003562 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003563 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003564 tv = STACK_TV_BOT(0);
3565 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003566 tv->v_lock = 0;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02003567 // This may result in NULL, which should be equivalent to an
3568 // empty string.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003569 tv->vval.v_string = get_reg_contents(
3570 iptr->isn_arg.number, GREG_EXPR_SRC);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003571 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003572 break;
3573
3574 // store local variable
3575 case ISN_STORE:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003576 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003577 tv = STACK_TV_VAR(iptr->isn_arg.number);
3578 clear_tv(tv);
3579 *tv = *STACK_TV_BOT(0);
3580 break;
3581
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003582 // store s: variable in old script or autoload import
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003583 case ISN_STORES:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003584 case ISN_STOREEXPORT:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003585 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003586 int sid = iptr->isn_arg.loadstore.ls_sid;
3587 hashtab_T *ht = &SCRIPT_VARS(sid);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003588 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003589 dictitem_T *di = find_var_in_ht(ht, 0,
3590 iptr->isn_type == ISN_STORES
3591 ? name + 2 : name, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003592
Bram Moolenaar4c137212021-04-19 16:48:48 +02003593 --ectx->ec_stack.ga_len;
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003594 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003595 if (di == NULL)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003596 {
3597 if (iptr->isn_type == ISN_STOREEXPORT)
3598 {
3599 semsg(_(e_undefined_variable_str), name);
Bram Moolenaard1d26842022-03-31 10:13:47 +01003600 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003601 goto on_error;
3602 }
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003603 store_var(name, STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003604 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003605 else
3606 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003607 if (iptr->isn_type == ISN_STOREEXPORT)
3608 {
3609 int idx = get_script_item_idx(sid, name, 0,
3610 NULL, NULL);
3611
Bram Moolenaar06651632022-04-27 17:54:25 +01003612 // can this ever fail?
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003613 if (idx >= 0)
3614 {
3615 svar_T *sv = ((svar_T *)SCRIPT_ITEM(sid)
3616 ->sn_var_vals.ga_data) + idx;
3617
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003618 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0)
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003619 {
3620 semsg(_(e_item_not_exported_in_script_str),
3621 name);
Bram Moolenaard1d26842022-03-31 10:13:47 +01003622 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01003623 goto on_error;
3624 }
3625 }
3626 }
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02003627 if (var_check_permission(di, name) == FAIL)
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003628 {
3629 clear_tv(STACK_TV_BOT(0));
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02003630 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003631 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003632 clear_tv(&di->di_tv);
3633 di->di_tv = *STACK_TV_BOT(0);
3634 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003635 }
3636 break;
3637
3638 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003639 case ISN_STORESCRIPT:
3640 {
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003641 scriptref_T *sref = iptr->isn_arg.script.scriptref;
3642 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003643
Bram Moolenaar6db660b2021-08-01 14:08:54 +02003644 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003645 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003646 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003647 --ectx->ec_stack.ga_len;
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003648
3649 // "const" and "final" are checked at compile time, locking
3650 // the value needs to be checked here.
3651 SOURCING_LNUM = iptr->isn_lnum;
3652 if (value_check_lock(sv->sv_tv->v_lock, sv->sv_name, FALSE))
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003653 {
3654 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003655 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02003656 }
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02003657
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003658 clear_tv(sv->sv_tv);
3659 *sv->sv_tv = *STACK_TV_BOT(0);
3660 }
3661 break;
3662
3663 // store option
3664 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003665 case ISN_STOREFUNCOPT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003666 {
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003667 char_u *opt_name = iptr->isn_arg.storeopt.so_name;
3668 int opt_flags = iptr->isn_arg.storeopt.so_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003669 long n = 0;
3670 char_u *s = NULL;
3671 char *msg;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003672 char_u numbuf[NUMBUFLEN];
3673 char_u *tofree = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003674
Bram Moolenaar4c137212021-04-19 16:48:48 +02003675 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003676 tv = STACK_TV_BOT(0);
3677 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01003678 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003679 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01003680 if (s == NULL)
3681 s = (char_u *)"";
3682 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003683 else if (iptr->isn_type == ISN_STOREFUNCOPT)
3684 {
3685 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003686 // If the option can be set to a function reference or
3687 // a lambda and the passed value is a function
3688 // reference, then convert it to the name (string) of
3689 // the function reference.
3690 s = tv2string(tv, &tofree, numbuf, 0);
3691 if (s == NULL || *s == NUL)
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003692 {
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003693 // cannot happen?
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003694 clear_tv(tv);
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003695 vim_free(tofree);
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003696 goto on_error;
3697 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003698 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003699 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02003700 // must be VAR_NUMBER, CHECKTYPE makes sure
3701 n = tv->vval.v_number;
Bram Moolenaardcb53be2021-12-09 14:23:43 +00003702 msg = set_option_value(opt_name, n, s, opt_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02003703 clear_tv(tv);
Bram Moolenaar2ef91562021-12-11 16:14:07 +00003704 vim_free(tofree);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003705 if (msg != NULL)
3706 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003707 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003708 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02003709 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003710 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003711 }
3712 break;
3713
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003714 // store $ENV
3715 case ISN_STOREENV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003716 --ectx->ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003717 tv = STACK_TV_BOT(0);
3718 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
3719 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003720 break;
3721
3722 // store @r
3723 case ISN_STOREREG:
3724 {
3725 int reg = iptr->isn_arg.number;
3726
Bram Moolenaar4c137212021-04-19 16:48:48 +02003727 --ectx->ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01003728 tv = STACK_TV_BOT(0);
Bram Moolenaar74f4a962021-06-17 21:03:07 +02003729 write_reg_contents(reg, tv_get_string(tv), -1, FALSE);
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01003730 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003731 }
3732 break;
3733
3734 // store v: variable
3735 case ISN_STOREV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003736 --ectx->ec_stack.ga_len;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003737 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
3738 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003739 // should not happen, type is checked when compiling
3740 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003741 break;
3742
Bram Moolenaard3aac292020-04-19 14:32:17 +02003743 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003744 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02003745 case ISN_STOREB:
3746 case ISN_STOREW:
3747 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003748 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003749 dictitem_T *di;
3750 hashtab_T *ht;
3751 char_u *name = iptr->isn_arg.string + 2;
3752
Bram Moolenaard3aac292020-04-19 14:32:17 +02003753 switch (iptr->isn_type)
3754 {
3755 case ISN_STOREG:
3756 ht = get_globvar_ht();
3757 break;
3758 case ISN_STOREB:
3759 ht = &curbuf->b_vars->dv_hashtab;
3760 break;
3761 case ISN_STOREW:
3762 ht = &curwin->w_vars->dv_hashtab;
3763 break;
3764 case ISN_STORET:
3765 ht = &curtab->tp_vars->dv_hashtab;
3766 break;
3767 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003768 goto theend;
Bram Moolenaard3aac292020-04-19 14:32:17 +02003769 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003770
Bram Moolenaar4c137212021-04-19 16:48:48 +02003771 --ectx->ec_stack.ga_len;
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003772 di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003773 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003774 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003775 else
3776 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003777 SOURCING_LNUM = iptr->isn_lnum;
3778 if (var_check_permission(di, name) == FAIL)
3779 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003780 clear_tv(&di->di_tv);
3781 di->di_tv = *STACK_TV_BOT(0);
3782 }
3783 }
3784 break;
3785
Bram Moolenaar03290b82020-12-19 16:30:44 +01003786 // store an autoload variable
3787 case ISN_STOREAUTO:
3788 SOURCING_LNUM = iptr->isn_lnum;
3789 set_var(iptr->isn_arg.string, STACK_TV_BOT(-1), TRUE);
3790 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003791 --ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003792 break;
3793
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003794 // store number in local variable
3795 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01003796 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003797 clear_tv(tv);
3798 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01003799 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003800 break;
3801
Bram Moolenaar590162c2022-12-24 21:24:06 +00003802 // Store a value in a list, dict, blob or object variable.
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01003803 case ISN_STOREINDEX:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003804 {
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003805 int res = execute_storeindex(iptr, ectx);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003806
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003807 if (res == FAIL)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02003808 goto on_error;
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003809 if (res == NOTDONE)
3810 goto theend;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003811 }
3812 break;
3813
Bram Moolenaarea5c8982022-02-17 14:42:02 +00003814 // store value in list or blob range
Bram Moolenaar68452172021-04-12 21:21:02 +02003815 case ISN_STORERANGE:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003816 if (execute_storerange(iptr, ectx) == FAIL)
3817 goto on_error;
Bram Moolenaar68452172021-04-12 21:21:02 +02003818 break;
3819
Bram Moolenaard505d172022-12-18 21:42:55 +00003820 case ISN_LOAD_CLASSMEMBER:
3821 {
3822 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
3823 goto theend;
3824 classmember_T *cm = &iptr->isn_arg.classmember;
3825 *STACK_TV_BOT(0) =
3826 cm->cm_class->class_members_tv[cm->cm_idx];
3827 ++ectx->ec_stack.ga_len;
3828 }
3829 break;
3830
3831 case ISN_STORE_CLASSMEMBER:
3832 {
3833 classmember_T *cm = &iptr->isn_arg.classmember;
3834 tv = &cm->cm_class->class_members_tv[cm->cm_idx];
3835 clear_tv(tv);
3836 *tv = *STACK_TV_BOT(-1);
3837 --ectx->ec_stack.ga_len;
3838 }
3839 break;
3840
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01003841 // Load or store variable or argument from outer scope.
Bram Moolenaar0186e582021-01-10 18:33:11 +01003842 case ISN_LOADOUTER:
3843 case ISN_STOREOUTER:
3844 {
3845 int depth = iptr->isn_arg.outer.outer_depth;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02003846 outer_T *outer = ectx->ec_outer_ref == NULL ? NULL
3847 : ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003848
3849 while (depth > 1 && outer != NULL)
3850 {
3851 outer = outer->out_up;
3852 --depth;
3853 }
3854 if (outer == NULL)
3855 {
3856 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar69c76172021-12-02 16:38:52 +00003857 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx
3858 || ectx->ec_outer_ref == NULL)
3859 // Possibly :def function called from legacy
3860 // context.
3861 emsg(_(e_closure_called_from_invalid_context));
3862 else
3863 iemsg("LOADOUTER depth more than scope levels");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003864 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003865 }
Bram Moolenaarcc341812022-09-19 15:54:34 +01003866 if (depth < 0)
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01003867 // Variable declared in loop. May be copied if the
3868 // loop block has already ended.
Bram Moolenaarcc341812022-09-19 15:54:34 +01003869 tv = ((typval_T *)outer->out_loop[-depth - 1]
3870 .stack->ga_data)
3871 + outer->out_loop[-depth - 1].var_idx
3872 + iptr->isn_arg.outer.outer_idx;
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01003873 else
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01003874 // Variable declared in a function. May be copied if
3875 // the function has already returned.
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01003876 tv = ((typval_T *)outer->out_stack->ga_data)
3877 + outer->out_frame_idx + STACK_FRAME_SIZE
3878 + iptr->isn_arg.outer.outer_idx;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003879 if (iptr->isn_type == ISN_LOADOUTER)
3880 {
Bram Moolenaar35578162021-08-02 19:10:38 +02003881 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003882 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003883 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003884 ++ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003885 }
3886 else
3887 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003888 --ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01003889 clear_tv(tv);
3890 *tv = *STACK_TV_BOT(0);
3891 }
3892 }
3893 break;
3894
Bram Moolenaar752fc692021-01-04 21:57:11 +01003895 // unlet item in list or dict variable
3896 case ISN_UNLETINDEX:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003897 if (execute_unletindex(iptr, ectx) == FAIL)
3898 goto on_error;
Bram Moolenaar752fc692021-01-04 21:57:11 +01003899 break;
3900
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01003901 // unlet range of items in list variable
3902 case ISN_UNLETRANGE:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00003903 if (execute_unletrange(iptr, ectx) == FAIL)
3904 goto on_error;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01003905 break;
3906
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003907 // push constant
3908 case ISN_PUSHNR:
3909 case ISN_PUSHBOOL:
3910 case ISN_PUSHSPEC:
3911 case ISN_PUSHF:
3912 case ISN_PUSHS:
3913 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003914 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003915 case ISN_PUSHCHANNEL:
3916 case ISN_PUSHJOB:
Bram Moolenaar35578162021-08-02 19:10:38 +02003917 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003918 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003919 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003920 tv->v_lock = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003921 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003922 switch (iptr->isn_type)
3923 {
3924 case ISN_PUSHNR:
3925 tv->v_type = VAR_NUMBER;
3926 tv->vval.v_number = iptr->isn_arg.number;
3927 break;
3928 case ISN_PUSHBOOL:
3929 tv->v_type = VAR_BOOL;
3930 tv->vval.v_number = iptr->isn_arg.number;
3931 break;
3932 case ISN_PUSHSPEC:
3933 tv->v_type = VAR_SPECIAL;
3934 tv->vval.v_number = iptr->isn_arg.number;
3935 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003936 case ISN_PUSHF:
3937 tv->v_type = VAR_FLOAT;
3938 tv->vval.v_float = iptr->isn_arg.fnumber;
3939 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003940 case ISN_PUSHBLOB:
3941 blob_copy(iptr->isn_arg.blob, tv);
3942 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003943 case ISN_PUSHFUNC:
3944 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01003945 if (iptr->isn_arg.string == NULL)
3946 tv->vval.v_string = NULL;
3947 else
3948 tv->vval.v_string =
3949 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003950 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003951 case ISN_PUSHCHANNEL:
3952#ifdef FEAT_JOB_CHANNEL
3953 tv->v_type = VAR_CHANNEL;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003954 tv->vval.v_channel = NULL;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003955#endif
3956 break;
3957 case ISN_PUSHJOB:
3958#ifdef FEAT_JOB_CHANNEL
3959 tv->v_type = VAR_JOB;
Bram Moolenaar397a87a2022-03-20 21:14:15 +00003960 tv->vval.v_job = NULL;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003961#endif
3962 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003963 default:
3964 tv->v_type = VAR_STRING;
Bram Moolenaarf8691002022-03-10 12:20:53 +00003965 tv->vval.v_string = iptr->isn_arg.string == NULL
3966 ? NULL : vim_strsave(iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003967 }
3968 break;
3969
Bram Moolenaar06b77222022-01-25 15:51:56 +00003970 case ISN_AUTOLOAD:
3971 {
3972 char_u *name = iptr->isn_arg.string;
3973
3974 (void)script_autoload(name, FALSE);
3975 if (find_func(name, TRUE))
3976 {
3977 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
3978 goto theend;
3979 tv = STACK_TV_BOT(0);
3980 tv->v_lock = 0;
3981 ++ectx->ec_stack.ga_len;
3982 tv->v_type = VAR_FUNC;
3983 tv->vval.v_string = vim_strsave(name);
3984 }
3985 else
3986 {
3987 int res = load_namespace_var(ectx, ISN_LOADG, iptr);
3988
3989 if (res == NOTDONE)
3990 goto theend;
3991 if (res == FAIL)
3992 goto on_error;
3993 }
3994 }
3995 break;
3996
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003997 case ISN_UNLET:
3998 if (do_unlet(iptr->isn_arg.unlet.ul_name,
3999 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02004000 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004001 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02004002 case ISN_UNLETENV:
LemonBoy77142312022-04-15 20:50:46 +01004003 vim_unsetenv_ext(iptr->isn_arg.unlet.ul_name);
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02004004 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004005
Bram Moolenaaraacc9662021-08-13 19:40:51 +02004006 case ISN_LOCKUNLOCK:
4007 {
4008 typval_T *lval_root_save = lval_root;
4009 int res;
4010
4011 // Stack has the local variable, argument the whole :lock
4012 // or :unlock command, like ISN_EXEC.
4013 --ectx->ec_stack.ga_len;
4014 lval_root = STACK_TV_BOT(0);
4015 res = exec_command(iptr);
4016 clear_tv(lval_root);
4017 lval_root = lval_root_save;
4018 if (res == FAIL)
4019 goto on_error;
4020 }
4021 break;
4022
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02004023 case ISN_LOCKCONST:
4024 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
4025 break;
4026
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004027 // create a list from items on the stack; uses a single allocation
4028 // for the list header and the items
4029 case ISN_NEWLIST:
Bram Moolenaar4c137212021-04-19 16:48:48 +02004030 if (exe_newlist(iptr->isn_arg.number, ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004031 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004032 break;
4033
4034 // create a dict from items on the stack
4035 case ISN_NEWDICT:
4036 {
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01004037 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004038
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01004039 SOURCING_LNUM = iptr->isn_lnum;
4040 res = exe_newdict(iptr->isn_arg.number, ectx);
4041 if (res == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004042 goto theend;
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01004043 if (res == MAYBE)
4044 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004045 }
4046 break;
4047
LemonBoy372bcce2022-04-25 12:43:20 +01004048 case ISN_CONCAT:
4049 if (exe_concat(iptr->isn_arg.number, ectx) == FAIL)
4050 goto theend;
4051 break;
4052
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004053 // create a partial with NULL value
4054 case ISN_NEWPARTIAL:
4055 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
4056 goto theend;
4057 ++ectx->ec_stack.ga_len;
4058 tv = STACK_TV_BOT(-1);
4059 tv->v_type = VAR_PARTIAL;
4060 tv->v_lock = 0;
4061 tv->vval.v_partial = NULL;
4062 break;
4063
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004064 // call a :def function
4065 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02004066 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01004067 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
4068 NULL,
4069 iptr->isn_arg.dfunc.cdf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02004070 ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02004071 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004072 break;
4073
4074 // call a builtin function
4075 case ISN_BCALL:
4076 SOURCING_LNUM = iptr->isn_lnum;
4077 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
4078 iptr->isn_arg.bfunc.cbf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02004079 ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004080 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004081 break;
4082
4083 // call a funcref or partial
4084 case ISN_PCALL:
4085 {
4086 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
4087 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02004088 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004089
4090 SOURCING_LNUM = iptr->isn_lnum;
4091 if (pfunc->cpf_top)
4092 {
4093 // funcref is above the arguments
4094 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
4095 }
4096 else
4097 {
4098 // Get the funcref from the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004099 --ectx->ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02004100 partial_tv = *STACK_TV_BOT(0);
4101 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004102 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004103 r = call_partial(tv, pfunc->cpf_argcount, ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02004104 if (tv == &partial_tv)
4105 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004106 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004107 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004108 }
4109 break;
4110
Bram Moolenaarbd5da372020-03-31 23:13:10 +02004111 case ISN_PCALL_END:
4112 // PCALL finished, arguments have been consumed and replaced by
4113 // the return value. Now clear the funcref from the stack,
4114 // and move the return value in its place.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004115 --ectx->ec_stack.ga_len;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02004116 clear_tv(STACK_TV_BOT(-1));
4117 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
4118 break;
4119
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004120 // call a user defined function or funcref/partial
4121 case ISN_UCALL:
4122 {
4123 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
4124
4125 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01004126 if (call_eval_func(cufunc->cuf_name, cufunc->cuf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02004127 ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004128 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004129 }
4130 break;
4131
Bram Moolenaar1d84f762022-09-03 21:35:53 +01004132 // :defer func(arg)
4133 case ISN_DEFER:
Bram Moolenaar806a2732022-09-04 15:40:36 +01004134 if (defer_command(iptr->isn_arg.defer.defer_var_idx,
Bram Moolenaar1d84f762022-09-03 21:35:53 +01004135 iptr->isn_arg.defer.defer_argcount, ectx) == FAIL)
4136 goto on_error;
4137 break;
4138
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004139 // Return from a :def function call without a value.
4140 // Return from a constructor.
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02004141 case ISN_RETURN_VOID:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004142 case ISN_RETURN_OBJECT:
Bram Moolenaar35578162021-08-02 19:10:38 +02004143 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004144 goto theend;
Bram Moolenaar299f3032021-01-08 20:53:09 +01004145 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004146 ++ectx->ec_stack.ga_len;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004147 if (iptr->isn_type == ISN_RETURN_VOID)
4148 {
4149 tv->v_type = VAR_VOID;
4150 tv->vval.v_number = 0;
4151 tv->v_lock = 0;
4152 }
4153 else
4154 {
4155 *tv = *STACK_TV_VAR(0);
4156 ++tv->vval.v_object->obj_refcount;
4157 }
Bram Moolenaar299f3032021-01-08 20:53:09 +01004158 // FALLTHROUGH
4159
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02004160 // return from a :def function call with what is on the stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004161 case ISN_RETURN:
4162 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004163 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01004164 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01004165
4166 if (trystack->ga_len > 0)
4167 trycmd = ((trycmd_T *)trystack->ga_data)
4168 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004169 if (trycmd != NULL
Bram Moolenaar4c137212021-04-19 16:48:48 +02004170 && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004171 {
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01004172 // jump to ":finally" or ":endtry"
4173 if (trycmd->tcd_finally_idx != 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004174 ectx->ec_iidx = trycmd->tcd_finally_idx;
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01004175 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02004176 ectx->ec_iidx = trycmd->tcd_endtry_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004177 trycmd->tcd_return = TRUE;
4178 }
4179 else
Bram Moolenaard032f342020-07-18 18:13:02 +02004180 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004181 }
4182 break;
4183
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004184 // push a partial, a reference to a compiled function
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004185 case ISN_FUNCREF:
4186 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004187 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
4188 ufunc_T *ufunc;
4189 funcref_T *funcref = &iptr->isn_arg.funcref;
4190 funcref_extra_T *extra = funcref->fr_extra;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004191
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004192 if (pt == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004193 goto theend;
Bram Moolenaar35578162021-08-02 19:10:38 +02004194 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02004195 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004196 vim_free(pt);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004197 goto theend;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02004198 }
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004199 if (extra == NULL || extra->fre_func_name == NULL)
Bram Moolenaar38453522021-11-28 22:00:12 +00004200 {
4201 dfunc_T *pt_dfunc = ((dfunc_T *)def_functions.ga_data)
4202 + funcref->fr_dfunc_idx;
4203
4204 ufunc = pt_dfunc->df_ufunc;
4205 }
4206 else
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004207 ufunc = find_func(extra->fre_func_name, FALSE);
Bram Moolenaar56acd1f2022-02-18 13:24:52 +00004208 if (ufunc == NULL)
4209 {
4210 SOURCING_LNUM = iptr->isn_lnum;
4211 iemsg("ufunc unexpectedly NULL for FUNCREF");
4212 goto theend;
4213 }
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004214 if (fill_partial_and_closure(pt, ufunc,
Bram Moolenaarcc341812022-09-19 15:54:34 +01004215 extra == NULL ? NULL : &extra->fre_loopvar_info,
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004216 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004217 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004218 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004219 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004220 tv->vval.v_partial = pt;
4221 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02004222 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004223 }
4224 break;
4225
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004226 // Create a global function from a lambda.
4227 case ISN_NEWFUNC:
4228 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004229 newfuncarg_T *arg = iptr->isn_arg.newfunc.nf_arg;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004230
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01004231 if (copy_lambda_to_global_func(arg->nfa_lambda,
Bram Moolenaarcc341812022-09-19 15:54:34 +01004232 arg->nfa_global, &arg->nfa_loopvar_info,
4233 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004234 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004235 }
4236 break;
4237
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004238 // List functions
4239 case ISN_DEF:
4240 if (iptr->isn_arg.string == NULL)
4241 list_functions(NULL);
4242 else
4243 {
Bram Moolenaar14336722022-01-08 16:02:59 +00004244 exarg_T ea;
4245 garray_T lines_to_free;
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004246
4247 CLEAR_FIELD(ea);
4248 ea.cmd = ea.arg = iptr->isn_arg.string;
Bram Moolenaar14336722022-01-08 16:02:59 +00004249 ga_init2(&lines_to_free, sizeof(char_u *), 50);
Bram Moolenaarffdaca92022-12-09 21:41:48 +00004250 define_function(&ea, NULL, &lines_to_free, FALSE);
Bram Moolenaar14336722022-01-08 16:02:59 +00004251 ga_clear_strings(&lines_to_free);
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004252 }
4253 break;
4254
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004255 // jump if a condition is met
4256 case ISN_JUMP:
4257 {
4258 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004259 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004260 int jump = TRUE;
4261
4262 if (when != JUMP_ALWAYS)
4263 {
4264 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004265 if (when == JUMP_IF_COND_FALSE
Bram Moolenaar13106602020-10-04 16:06:05 +02004266 || when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004267 || when == JUMP_IF_COND_TRUE)
4268 {
4269 SOURCING_LNUM = iptr->isn_lnum;
4270 jump = tv_get_bool_chk(tv, &error);
4271 if (error)
4272 goto on_error;
4273 }
4274 else
4275 jump = tv2bool(tv);
Bram Moolenaarf6ced982022-04-28 12:00:49 +01004276 if (when == JUMP_IF_FALSE || when == JUMP_IF_COND_FALSE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004277 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01004278 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004279 {
4280 // drop the value from the stack
4281 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004282 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004283 }
4284 }
4285 if (jump)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004286 ectx->ec_iidx = iptr->isn_arg.jump.jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004287 }
4288 break;
4289
Bram Moolenaarb46c0832022-09-15 17:19:37 +01004290 // "while": jump to end if a condition is false
4291 case ISN_WHILE:
4292 {
4293 int error = FALSE;
4294 int jump = TRUE;
4295
4296 tv = STACK_TV_BOT(-1);
4297 SOURCING_LNUM = iptr->isn_lnum;
4298 jump = !tv_get_bool_chk(tv, &error);
4299 if (error)
4300 goto on_error;
4301 // drop the value from the stack
4302 clear_tv(tv);
4303 --ectx->ec_stack.ga_len;
4304 if (jump)
4305 ectx->ec_iidx = iptr->isn_arg.whileloop.while_end;
4306
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +01004307 // Store the current funcref count, may be used by
Bram Moolenaarcc341812022-09-19 15:54:34 +01004308 // ISN_ENDLOOP later
Bram Moolenaarb46c0832022-09-15 17:19:37 +01004309 tv = STACK_TV_VAR(
4310 iptr->isn_arg.whileloop.while_funcref_idx);
4311 tv->vval.v_number = ectx->ec_funcrefs.ga_len;
4312 }
4313 break;
4314
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02004315 // Jump if an argument with a default value was already set and not
4316 // v:none.
4317 case ISN_JUMP_IF_ARG_SET:
Bram Moolenaar65b0d162022-12-13 18:43:22 +00004318 case ISN_JUMP_IF_ARG_NOT_SET:
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02004319 tv = STACK_TV_VAR(iptr->isn_arg.jumparg.jump_arg_off);
Bram Moolenaar65b0d162022-12-13 18:43:22 +00004320 int arg_set = tv->v_type != VAR_UNKNOWN
4321 && !(tv->v_type == VAR_SPECIAL
4322 && tv->vval.v_number == VVAL_NONE);
4323 if (iptr->isn_type == ISN_JUMP_IF_ARG_SET ? arg_set : !arg_set)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004324 ectx->ec_iidx = iptr->isn_arg.jumparg.jump_where;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02004325 break;
4326
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004327 // top of a for loop
4328 case ISN_FOR:
Bram Moolenaar058ee7c2022-01-23 20:00:42 +00004329 if (execute_for(iptr, ectx) == FAIL)
4330 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004331 break;
4332
Bram Moolenaarb46c0832022-09-15 17:19:37 +01004333 // end of a for or while loop
4334 case ISN_ENDLOOP:
4335 if (execute_endloop(iptr, ectx) == FAIL)
4336 goto theend;
4337 break;
4338
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004339 // start of ":try" block
4340 case ISN_TRY:
4341 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01004342 trycmd_T *trycmd = NULL;
4343
Bram Moolenaar35578162021-08-02 19:10:38 +02004344 if (GA_GROW_FAILS(&ectx->ec_trystack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004345 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004346 trycmd = ((trycmd_T *)ectx->ec_trystack.ga_data)
4347 + ectx->ec_trystack.ga_len;
4348 ++ectx->ec_trystack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004349 ++trylevel;
Bram Moolenaar8d4be892021-02-13 18:33:02 +01004350 CLEAR_POINTER(trycmd);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004351 trycmd->tcd_frame_idx = ectx->ec_frame_idx;
4352 trycmd->tcd_stack_len = ectx->ec_stack.ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01004353 trycmd->tcd_catch_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00004354 iptr->isn_arg.tryref.try_ref->try_catch;
Bram Moolenaara91a7132021-03-25 21:12:15 +01004355 trycmd->tcd_finally_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00004356 iptr->isn_arg.tryref.try_ref->try_finally;
Bram Moolenaara91a7132021-03-25 21:12:15 +01004357 trycmd->tcd_endtry_idx =
Bram Moolenaar0d807102021-12-21 09:42:09 +00004358 iptr->isn_arg.tryref.try_ref->try_endtry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004359 }
4360 break;
4361
4362 case ISN_PUSHEXC:
4363 if (current_exception == NULL)
4364 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004365 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004366 iemsg("Evaluating catch while current_exception is NULL");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004367 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004368 }
Bram Moolenaar35578162021-08-02 19:10:38 +02004369 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004370 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004371 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004372 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004373 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02004374 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004375 tv->vval.v_string = vim_strsave(
4376 (char_u *)current_exception->value);
4377 break;
4378
4379 case ISN_CATCH:
4380 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004381 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar06651632022-04-27 17:54:25 +01004382 trycmd_T *trycmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004383
Bram Moolenaar4c137212021-04-19 16:48:48 +02004384 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar06651632022-04-27 17:54:25 +01004385 trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004386 + trystack->ga_len - 1;
Bram Moolenaar06651632022-04-27 17:54:25 +01004387 trycmd->tcd_caught = TRUE;
4388 trycmd->tcd_did_throw = FALSE;
4389
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004390 did_emsg = got_int = did_throw = FALSE;
Bram Moolenaar1430cee2021-01-17 19:20:32 +01004391 force_abort = need_rethrow = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004392 catch_exception(current_exception);
4393 }
4394 break;
4395
Bram Moolenaarc150c092021-02-13 15:02:46 +01004396 case ISN_TRYCONT:
4397 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004398 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004399 trycont_T *trycont = &iptr->isn_arg.trycont;
4400 int i;
4401 trycmd_T *trycmd;
4402 int iidx = trycont->tct_where;
4403
4404 if (trystack->ga_len < trycont->tct_levels)
4405 {
4406 siemsg("TRYCONT: expected %d levels, found %d",
4407 trycont->tct_levels, trystack->ga_len);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004408 goto theend;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004409 }
4410 // Make :endtry jump to any outer try block and the last
4411 // :endtry inside the loop to the loop start.
4412 for (i = trycont->tct_levels; i > 0; --i)
4413 {
4414 trycmd = ((trycmd_T *)trystack->ga_data)
4415 + trystack->ga_len - i;
Bram Moolenaar2e34c342021-03-14 12:13:33 +01004416 // Add one to tcd_cont to be able to jump to
4417 // instruction with index zero.
4418 trycmd->tcd_cont = iidx + 1;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01004419 iidx = trycmd->tcd_finally_idx == 0
4420 ? trycmd->tcd_endtry_idx : trycmd->tcd_finally_idx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004421 }
4422 // jump to :finally or :endtry of current try statement
Bram Moolenaar4c137212021-04-19 16:48:48 +02004423 ectx->ec_iidx = iidx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004424 }
4425 break;
4426
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01004427 case ISN_FINALLY:
4428 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004429 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01004430 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
4431 + trystack->ga_len - 1;
4432
4433 // Reset the index to avoid a return statement jumps here
4434 // again.
4435 trycmd->tcd_finally_idx = 0;
4436 break;
4437 }
4438
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004439 // end of ":try" block
4440 case ISN_ENDTRY:
4441 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004442 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar06651632022-04-27 17:54:25 +01004443 trycmd_T *trycmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004444
Bram Moolenaar06651632022-04-27 17:54:25 +01004445 --trystack->ga_len;
4446 --trylevel;
4447 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len;
4448 if (trycmd->tcd_did_throw)
4449 did_throw = TRUE;
4450 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004451 {
Bram Moolenaar06651632022-04-27 17:54:25 +01004452 // discard the exception
4453 if (caught_stack == current_exception)
4454 caught_stack = caught_stack->caught;
4455 discard_current_exception();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004456 }
Bram Moolenaar06651632022-04-27 17:54:25 +01004457
4458 if (trycmd->tcd_return)
4459 goto func_return;
4460
4461 while (ectx->ec_stack.ga_len > trycmd->tcd_stack_len)
4462 {
4463 --ectx->ec_stack.ga_len;
4464 clear_tv(STACK_TV_BOT(0));
4465 }
4466 if (trycmd->tcd_cont != 0)
4467 // handling :continue: jump to outer try block or
4468 // start of the loop
4469 ectx->ec_iidx = trycmd->tcd_cont - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004470 }
4471 break;
4472
4473 case ISN_THROW:
Bram Moolenaar8f81b222021-01-14 21:47:06 +01004474 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004475 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02004476
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004477 if (trystack->ga_len == 0 && trylevel == 0 && emsg_silent)
4478 {
4479 // throwing an exception while using "silent!" causes
4480 // the function to abort but not display an error.
4481 tv = STACK_TV_BOT(-1);
4482 clear_tv(tv);
4483 tv->v_type = VAR_NUMBER;
4484 tv->vval.v_number = 0;
4485 goto done;
4486 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004487 --ectx->ec_stack.ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004488 tv = STACK_TV_BOT(0);
4489 if (tv->vval.v_string == NULL
4490 || *skipwhite(tv->vval.v_string) == NUL)
4491 {
4492 vim_free(tv->vval.v_string);
4493 SOURCING_LNUM = iptr->isn_lnum;
4494 emsg(_(e_throw_with_empty_string));
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004495 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004496 }
4497
4498 // Inside a "catch" we need to first discard the caught
4499 // exception.
4500 if (trystack->ga_len > 0)
4501 {
4502 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
4503 + trystack->ga_len - 1;
4504 if (trycmd->tcd_caught && current_exception != NULL)
4505 {
4506 // discard the exception
4507 if (caught_stack == current_exception)
4508 caught_stack = caught_stack->caught;
4509 discard_current_exception();
4510 trycmd->tcd_caught = FALSE;
4511 }
4512 }
4513
Bram Moolenaar90a57162022-02-12 14:23:17 +00004514 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004515 if (throw_exception(tv->vval.v_string, ET_USER, NULL)
4516 == FAIL)
4517 {
4518 vim_free(tv->vval.v_string);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004519 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01004520 }
4521 did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004522 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004523 break;
4524
4525 // compare with special values
4526 case ISN_COMPAREBOOL:
4527 case ISN_COMPARESPECIAL:
4528 {
4529 typval_T *tv1 = STACK_TV_BOT(-2);
4530 typval_T *tv2 = STACK_TV_BOT(-1);
4531 varnumber_T arg1 = tv1->vval.v_number;
4532 varnumber_T arg2 = tv2->vval.v_number;
4533 int res;
4534
Bram Moolenaar06651632022-04-27 17:54:25 +01004535 if (iptr->isn_arg.op.op_type == EXPR_EQUAL)
4536 res = arg1 == arg2;
4537 else
4538 res = arg1 != arg2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004539
Bram Moolenaar4c137212021-04-19 16:48:48 +02004540 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004541 tv1->v_type = VAR_BOOL;
4542 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4543 }
4544 break;
4545
Bram Moolenaar7a222242022-03-01 19:23:24 +00004546 case ISN_COMPARENULL:
4547 {
4548 typval_T *tv1 = STACK_TV_BOT(-2);
4549 typval_T *tv2 = STACK_TV_BOT(-1);
4550 int res;
4551
4552 res = typval_compare_null(tv1, tv2);
4553 if (res == MAYBE)
4554 goto on_error;
4555 if (iptr->isn_arg.op.op_type == EXPR_NEQUAL)
4556 res = !res;
4557 clear_tv(tv1);
4558 clear_tv(tv2);
4559 --ectx->ec_stack.ga_len;
4560 tv1->v_type = VAR_BOOL;
4561 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4562 }
4563 break;
4564
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004565 // Operation with two number arguments
4566 case ISN_OPNR:
4567 case ISN_COMPARENR:
4568 {
4569 typval_T *tv1 = STACK_TV_BOT(-2);
4570 typval_T *tv2 = STACK_TV_BOT(-1);
4571 varnumber_T arg1 = tv1->vval.v_number;
4572 varnumber_T arg2 = tv2->vval.v_number;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004573 varnumber_T res = 0;
4574 int div_zero = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004575
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004576 if (iptr->isn_arg.op.op_type == EXPR_LSHIFT
4577 || iptr->isn_arg.op.op_type == EXPR_RSHIFT)
4578 {
4579 if (arg2 < 0)
4580 {
4581 SOURCING_LNUM = iptr->isn_lnum;
dundargocc57b5bc2022-11-02 13:30:51 +00004582 emsg(_(e_bitshift_ops_must_be_positive));
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004583 goto on_error;
4584 }
4585 }
4586
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004587 switch (iptr->isn_arg.op.op_type)
4588 {
4589 case EXPR_MULT: res = arg1 * arg2; break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004590 case EXPR_DIV: if (arg2 == 0)
4591 div_zero = TRUE;
4592 else
4593 res = arg1 / arg2;
4594 break;
4595 case EXPR_REM: if (arg2 == 0)
4596 div_zero = TRUE;
4597 else
4598 res = arg1 % arg2;
4599 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004600 case EXPR_SUB: res = arg1 - arg2; break;
4601 case EXPR_ADD: res = arg1 + arg2; break;
4602
4603 case EXPR_EQUAL: res = arg1 == arg2; break;
4604 case EXPR_NEQUAL: res = arg1 != arg2; break;
4605 case EXPR_GREATER: res = arg1 > arg2; break;
4606 case EXPR_GEQUAL: res = arg1 >= arg2; break;
4607 case EXPR_SMALLER: res = arg1 < arg2; break;
4608 case EXPR_SEQUAL: res = arg1 <= arg2; break;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004609 case EXPR_LSHIFT: if (arg2 > MAX_LSHIFT_BITS)
4610 res = 0;
4611 else
Bram Moolenaar68e64d22022-05-22 22:07:52 +01004612 res = (uvarnumber_T)arg1 << arg2;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004613 break;
4614 case EXPR_RSHIFT: if (arg2 > MAX_LSHIFT_BITS)
4615 res = 0;
4616 else
Bram Moolenaar338bf582022-05-22 20:16:32 +01004617 res = (uvarnumber_T)arg1 >> arg2;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004618 break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004619 default: break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004620 }
4621
Bram Moolenaar4c137212021-04-19 16:48:48 +02004622 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004623 if (iptr->isn_type == ISN_COMPARENR)
4624 {
4625 tv1->v_type = VAR_BOOL;
4626 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4627 }
4628 else
4629 tv1->vval.v_number = res;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02004630 if (div_zero)
4631 {
4632 SOURCING_LNUM = iptr->isn_lnum;
4633 emsg(_(e_divide_by_zero));
4634 goto on_error;
4635 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004636 }
4637 break;
4638
4639 // Computation with two float arguments
4640 case ISN_OPFLOAT:
4641 case ISN_COMPAREFLOAT:
4642 {
4643 typval_T *tv1 = STACK_TV_BOT(-2);
4644 typval_T *tv2 = STACK_TV_BOT(-1);
4645 float_T arg1 = tv1->vval.v_float;
4646 float_T arg2 = tv2->vval.v_float;
4647 float_T res = 0;
4648 int cmp = FALSE;
4649
4650 switch (iptr->isn_arg.op.op_type)
4651 {
4652 case EXPR_MULT: res = arg1 * arg2; break;
4653 case EXPR_DIV: res = arg1 / arg2; break;
4654 case EXPR_SUB: res = arg1 - arg2; break;
4655 case EXPR_ADD: res = arg1 + arg2; break;
4656
4657 case EXPR_EQUAL: cmp = arg1 == arg2; break;
4658 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
4659 case EXPR_GREATER: cmp = arg1 > arg2; break;
4660 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
4661 case EXPR_SMALLER: cmp = arg1 < arg2; break;
4662 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
4663 default: cmp = 0; break;
4664 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004665 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004666 if (iptr->isn_type == ISN_COMPAREFLOAT)
4667 {
4668 tv1->v_type = VAR_BOOL;
4669 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
4670 }
4671 else
4672 tv1->vval.v_float = res;
4673 }
4674 break;
4675
4676 case ISN_COMPARELIST:
Bram Moolenaar265f8112021-12-19 12:33:05 +00004677 case ISN_COMPAREDICT:
4678 case ISN_COMPAREFUNC:
4679 case ISN_COMPARESTRING:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004680 case ISN_COMPAREBLOB:
4681 {
4682 typval_T *tv1 = STACK_TV_BOT(-2);
4683 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar265f8112021-12-19 12:33:05 +00004684 exprtype_T exprtype = iptr->isn_arg.op.op_type;
4685 int ic = iptr->isn_arg.op.op_ic;
4686 int res = FALSE;
4687 int status = OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004688
Bram Moolenaar265f8112021-12-19 12:33:05 +00004689 SOURCING_LNUM = iptr->isn_lnum;
4690 if (iptr->isn_type == ISN_COMPARELIST)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004691 {
Bram Moolenaar265f8112021-12-19 12:33:05 +00004692 status = typval_compare_list(tv1, tv2,
4693 exprtype, ic, &res);
4694 }
4695 else if (iptr->isn_type == ISN_COMPAREDICT)
4696 {
4697 status = typval_compare_dict(tv1, tv2,
4698 exprtype, ic, &res);
4699 }
4700 else if (iptr->isn_type == ISN_COMPAREFUNC)
4701 {
4702 status = typval_compare_func(tv1, tv2,
4703 exprtype, ic, &res);
4704 }
4705 else if (iptr->isn_type == ISN_COMPARESTRING)
4706 {
4707 status = typval_compare_string(tv1, tv2,
4708 exprtype, ic, &res);
4709 }
4710 else
4711 {
4712 status = typval_compare_blob(tv1, tv2, exprtype, &res);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004713 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004714 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004715 clear_tv(tv1);
4716 clear_tv(tv2);
4717 tv1->v_type = VAR_BOOL;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004718 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
4719 if (status == FAIL)
4720 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004721 }
4722 break;
4723
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004724 case ISN_COMPAREANY:
4725 {
4726 typval_T *tv1 = STACK_TV_BOT(-2);
4727 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar657137c2021-01-09 15:45:23 +01004728 exprtype_T exprtype = iptr->isn_arg.op.op_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004729 int ic = iptr->isn_arg.op.op_ic;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004730 int status;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004731
Bram Moolenaareb26f432020-09-14 16:50:05 +02004732 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004733 status = typval_compare(tv1, tv2, exprtype, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004734 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004735 --ectx->ec_stack.ga_len;
Bram Moolenaar265f8112021-12-19 12:33:05 +00004736 if (status == FAIL)
4737 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004738 }
4739 break;
4740
4741 case ISN_ADDLIST:
4742 case ISN_ADDBLOB:
4743 {
4744 typval_T *tv1 = STACK_TV_BOT(-2);
4745 typval_T *tv2 = STACK_TV_BOT(-1);
4746
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004747 // add two lists or blobs
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004748 if (iptr->isn_type == ISN_ADDLIST)
Bram Moolenaar07802042021-09-09 23:01:14 +02004749 {
4750 if (iptr->isn_arg.op.op_type == EXPR_APPEND
4751 && tv1->vval.v_list != NULL)
4752 list_extend(tv1->vval.v_list, tv2->vval.v_list,
4753 NULL);
4754 else
4755 eval_addlist(tv1, tv2);
4756 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004757 else
4758 eval_addblob(tv1, tv2);
4759 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004760 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004761 }
4762 break;
4763
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004764 case ISN_LISTAPPEND:
4765 {
4766 typval_T *tv1 = STACK_TV_BOT(-2);
4767 typval_T *tv2 = STACK_TV_BOT(-1);
4768 list_T *l = tv1->vval.v_list;
4769
4770 // add an item to a list
Bram Moolenaar1f4a3452022-01-01 18:29:21 +00004771 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004772 if (l == NULL)
4773 {
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004774 emsg(_(e_cannot_add_to_null_list));
4775 goto on_error;
4776 }
Bram Moolenaar1f4a3452022-01-01 18:29:21 +00004777 if (value_check_lock(l->lv_lock, NULL, FALSE))
4778 goto on_error;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004779 if (list_append_tv(l, tv2) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004780 goto theend;
Bram Moolenaar955347c2020-10-19 23:01:46 +02004781 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004782 --ectx->ec_stack.ga_len;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004783 }
4784 break;
4785
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02004786 case ISN_BLOBAPPEND:
4787 {
4788 typval_T *tv1 = STACK_TV_BOT(-2);
4789 typval_T *tv2 = STACK_TV_BOT(-1);
4790 blob_T *b = tv1->vval.v_blob;
4791 int error = FALSE;
4792 varnumber_T n;
4793
4794 // add a number to a blob
4795 if (b == NULL)
4796 {
4797 SOURCING_LNUM = iptr->isn_lnum;
4798 emsg(_(e_cannot_add_to_null_blob));
4799 goto on_error;
4800 }
4801 n = tv_get_number_chk(tv2, &error);
4802 if (error)
4803 goto on_error;
4804 ga_append(&b->bv_ga, (int)n);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004805 --ectx->ec_stack.ga_len;
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02004806 }
4807 break;
4808
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004809 // Computation with two arguments of unknown type
4810 case ISN_OPANY:
4811 {
4812 typval_T *tv1 = STACK_TV_BOT(-2);
4813 typval_T *tv2 = STACK_TV_BOT(-1);
4814 varnumber_T n1, n2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004815 float_T f1 = 0, f2 = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004816 int error = FALSE;
4817
4818 if (iptr->isn_arg.op.op_type == EXPR_ADD)
4819 {
4820 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
4821 {
4822 eval_addlist(tv1, tv2);
4823 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004824 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004825 break;
4826 }
4827 else if (tv1->v_type == VAR_BLOB
4828 && tv2->v_type == VAR_BLOB)
4829 {
4830 eval_addblob(tv1, tv2);
4831 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004832 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004833 break;
4834 }
4835 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004836 if (tv1->v_type == VAR_FLOAT)
4837 {
4838 f1 = tv1->vval.v_float;
4839 n1 = 0;
4840 }
4841 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004842 {
Bram Moolenaarf665e972020-12-05 19:17:16 +01004843 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004844 n1 = tv_get_number_chk(tv1, &error);
4845 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02004846 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004847 if (tv2->v_type == VAR_FLOAT)
4848 f1 = n1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004849 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004850 if (tv2->v_type == VAR_FLOAT)
4851 {
4852 f2 = tv2->vval.v_float;
4853 n2 = 0;
4854 }
4855 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004856 {
4857 n2 = tv_get_number_chk(tv2, &error);
4858 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02004859 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004860 if (tv1->v_type == VAR_FLOAT)
4861 f2 = n2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004862 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004863 // if there is a float on either side the result is a float
4864 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
4865 {
4866 switch (iptr->isn_arg.op.op_type)
4867 {
4868 case EXPR_MULT: f1 = f1 * f2; break;
4869 case EXPR_DIV: f1 = f1 / f2; break;
4870 case EXPR_SUB: f1 = f1 - f2; break;
4871 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004872 default: SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004873 emsg(_(e_cannot_use_percent_with_float));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004874 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004875 }
4876 clear_tv(tv1);
4877 clear_tv(tv2);
4878 tv1->v_type = VAR_FLOAT;
4879 tv1->vval.v_float = f1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004880 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004881 }
4882 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004883 {
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004884 int failed = FALSE;
4885
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004886 switch (iptr->isn_arg.op.op_type)
4887 {
4888 case EXPR_MULT: n1 = n1 * n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004889 case EXPR_DIV: n1 = num_divide(n1, n2, &failed);
4890 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01004891 goto on_error;
4892 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004893 case EXPR_SUB: n1 = n1 - n2; break;
4894 case EXPR_ADD: n1 = n1 + n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004895 default: n1 = num_modulus(n1, n2, &failed);
4896 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01004897 goto on_error;
4898 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004899 }
4900 clear_tv(tv1);
4901 clear_tv(tv2);
4902 tv1->v_type = VAR_NUMBER;
4903 tv1->vval.v_number = n1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004904 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004905 }
4906 }
4907 break;
4908
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004909 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004910 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004911 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004912 int is_slice = iptr->isn_type == ISN_STRSLICE;
4913 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004914 char_u *res;
4915
4916 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004917 // string slice: string is at stack-3, first index at
4918 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004919 if (is_slice)
4920 {
4921 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004922 n1 = tv->vval.v_number;
4923 }
4924
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004925 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004926 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004927
Bram Moolenaar4c137212021-04-19 16:48:48 +02004928 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004929 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004930 if (is_slice)
4931 // Slice: Select the characters from the string
Bram Moolenaar6601b622021-01-13 21:47:15 +01004932 res = string_slice(tv->vval.v_string, n1, n2, FALSE);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004933 else
4934 // Index: The resulting variable is a string of a
Bram Moolenaar0289a092021-03-14 18:40:19 +01004935 // single character (including composing characters).
4936 // If the index is too big or negative the result is
4937 // empty.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004938 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004939 vim_free(tv->vval.v_string);
4940 tv->vval.v_string = res;
4941 }
4942 break;
4943
4944 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02004945 case ISN_LISTSLICE:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004946 case ISN_BLOBINDEX:
4947 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004948 {
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004949 int is_slice = iptr->isn_type == ISN_LISTSLICE
4950 || iptr->isn_type == ISN_BLOBSLICE;
4951 int is_blob = iptr->isn_type == ISN_BLOBINDEX
4952 || iptr->isn_type == ISN_BLOBSLICE;
Bram Moolenaared591872020-08-15 22:14:53 +02004953 varnumber_T n1, n2;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004954 typval_T *val_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004955
4956 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02004957 // list slice: list is at stack-3, indexes at stack-2 and
4958 // stack-1
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004959 // Same for blob.
4960 val_tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004961
4962 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02004963 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004964 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02004965
4966 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004967 {
Bram Moolenaared591872020-08-15 22:14:53 +02004968 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02004969 n1 = tv->vval.v_number;
4970 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004971 }
Bram Moolenaared591872020-08-15 22:14:53 +02004972
Bram Moolenaar4c137212021-04-19 16:48:48 +02004973 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02004974 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02004975 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004976 if (is_blob)
4977 {
4978 if (blob_slice_or_index(val_tv->vval.v_blob, is_slice,
4979 n1, n2, FALSE, tv) == FAIL)
4980 goto on_error;
4981 }
4982 else
4983 {
4984 if (list_slice_or_index(val_tv->vval.v_list, is_slice,
4985 n1, n2, FALSE, tv, TRUE) == FAIL)
4986 goto on_error;
4987 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004988 }
4989 break;
4990
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004991 case ISN_ANYINDEX:
4992 case ISN_ANYSLICE:
4993 {
4994 int is_slice = iptr->isn_type == ISN_ANYSLICE;
4995 typval_T *var1, *var2;
4996 int res;
4997
4998 // index: composite is at stack-2, index at stack-1
4999 // slice: composite is at stack-3, indexes at stack-2 and
5000 // stack-1
5001 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02005002 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005003 if (check_can_index(tv, TRUE, TRUE) == FAIL)
5004 goto on_error;
5005 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
5006 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
Bram Moolenaar6601b622021-01-13 21:47:15 +01005007 res = eval_index_inner(tv, is_slice, var1, var2,
5008 FALSE, NULL, -1, TRUE);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005009 clear_tv(var1);
5010 if (is_slice)
5011 clear_tv(var2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005012 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02005013 if (res == FAIL)
5014 goto on_error;
5015 }
5016 break;
5017
Bram Moolenaar9af78762020-06-16 11:34:42 +02005018 case ISN_SLICE:
5019 {
5020 list_T *list;
5021 int count = iptr->isn_arg.number;
5022
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02005023 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02005024 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02005025 list = tv->vval.v_list;
5026
5027 // no error for short list, expect it to be checked earlier
5028 if (list != NULL && list->lv_len >= count)
5029 {
5030 list_T *newlist = list_slice(list,
5031 count, list->lv_len - 1);
5032
5033 if (newlist != NULL)
5034 {
5035 list_unref(list);
5036 tv->vval.v_list = newlist;
5037 ++newlist->lv_refcount;
5038 }
5039 }
5040 }
5041 break;
5042
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005043 case ISN_GETITEM:
5044 {
5045 listitem_T *li;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02005046 getitem_T *gi = &iptr->isn_arg.getitem;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005047
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02005048 // Get list item: list is at stack-1, push item.
5049 // List type and length is checked for when compiling.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02005050 tv = STACK_TV_BOT(-1 - gi->gi_with_op);
5051 li = list_find(tv->vval.v_list, gi->gi_index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005052
Bram Moolenaar35578162021-08-02 19:10:38 +02005053 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005054 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005055 ++ectx->ec_stack.ga_len;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005056 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
Bram Moolenaarf785aa12021-02-11 21:19:34 +01005057
5058 // Useful when used in unpack assignment. Reset at
5059 // ISN_DROP.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02005060 ectx->ec_where.wt_index = gi->gi_index + 1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005061 ectx->ec_where.wt_variable = TRUE;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02005062 }
5063 break;
5064
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005065 case ISN_MEMBER:
5066 {
5067 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005068 char_u *key;
5069 dictitem_T *di;
5070
5071 // dict member: dict is at stack-2, key at stack-1
5072 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02005073 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005074 dict = tv->vval.v_dict;
5075
5076 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02005077 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005078 key = tv->vval.v_string;
Bram Moolenaar086fc9a2020-10-30 18:33:02 +01005079 if (key == NULL)
5080 key = (char_u *)"";
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02005081
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005082 if ((di = dict_find(dict, key, -1)) == NULL)
5083 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005084 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005085 semsg(_(e_key_not_present_in_dictionary), key);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01005086
5087 // If :silent! is used we will continue, make sure the
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005088 // stack contents makes sense and the dict stack is
5089 // updated.
Bram Moolenaar4029cab2020-12-05 18:13:27 +01005090 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005091 --ectx->ec_stack.ga_len;
Bram Moolenaar4029cab2020-12-05 18:13:27 +01005092 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005093 (void) dict_stack_save(tv);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01005094 tv->v_type = VAR_NUMBER;
5095 tv->vval.v_number = 0;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01005096 goto on_fatal_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005097 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005098 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005099 --ectx->ec_stack.ga_len;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005100 // Put the dict used on the dict stack, it might be used by
5101 // a dict function later.
Bram Moolenaar50788ef2020-07-05 16:51:26 +02005102 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005103 if (dict_stack_save(tv) == FAIL)
5104 goto on_fatal_error;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02005105 copy_tv(&di->di_tv, tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02005106 }
5107 break;
5108
5109 // dict member with string key
5110 case ISN_STRINGMEMBER:
5111 {
5112 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005113 dictitem_T *di;
5114
5115 tv = STACK_TV_BOT(-1);
5116 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
5117 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005118 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00005119 emsg(_(e_dictionary_required));
Bram Moolenaard032f342020-07-18 18:13:02 +02005120 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005121 }
5122 dict = tv->vval.v_dict;
5123
5124 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
5125 == NULL)
5126 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005127 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar381692b2022-02-02 20:01:27 +00005128 semsg(_(e_key_not_present_in_dictionary),
5129 iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02005130 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005131 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005132 // Put the dict used on the dict stack, it might be used by
5133 // a dict function later.
5134 if (dict_stack_save(tv) == FAIL)
5135 goto on_fatal_error;
5136
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005137 copy_tv(&di->di_tv, tv);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005138 }
5139 break;
5140
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005141 case ISN_GET_OBJ_MEMBER:
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005142 {
5143 tv = STACK_TV_BOT(-1);
5144 if (tv->v_type != VAR_OBJECT)
5145 {
5146 SOURCING_LNUM = iptr->isn_lnum;
5147 garray_T type_list;
5148 ga_init2(&type_list, sizeof(type_T *), 10);
5149 type_T *type = typval2type(tv, get_copyID(),
5150 &type_list, TVTT_DO_MEMBER);
5151 char *tofree = NULL;
5152 char *typename = type_name(type, &tofree);
5153 semsg(_(e_object_required_found_str), typename);
5154 vim_free(tofree);
5155 clear_type_list(&type_list);
5156 goto on_error;
5157 }
5158 int idx = iptr->isn_arg.number;
5159 object_T *obj = tv->vval.v_object;
5160 // the members are located right after the object struct
5161 typval_T *mtv = ((typval_T *)(obj + 1)) + idx;
Bram Moolenaar590162c2022-12-24 21:24:06 +00005162 copy_tv(mtv, tv);
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005163
5164 // Unreference the object after getting the member, it may
5165 // be freed.
5166 object_unref(obj);
5167 }
5168 break;
5169
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00005170 case ISN_STORE_THIS:
5171 {
5172 int idx = iptr->isn_arg.number;
5173 object_T *obj = STACK_TV_VAR(0)->vval.v_object;
5174 // the members are located right after the object struct
5175 typval_T *mtv = ((typval_T *)(obj + 1)) + idx;
5176 clear_tv(mtv);
5177 *mtv = *STACK_TV_BOT(-1);
5178 --ectx->ec_stack.ga_len;
5179 }
5180 break;
5181
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005182 case ISN_CLEARDICT:
5183 dict_stack_drop();
5184 break;
5185
5186 case ISN_USEDICT:
5187 {
5188 typval_T *dict_tv = dict_stack_get_tv();
5189
5190 // Turn "dict.Func" into a partial for "Func" bound to
5191 // "dict". Don't do this when "Func" is already a partial
5192 // that was bound explicitly (pt_auto is FALSE).
5193 tv = STACK_TV_BOT(-1);
5194 if (dict_tv != NULL
5195 && dict_tv->v_type == VAR_DICT
5196 && dict_tv->vval.v_dict != NULL
5197 && (tv->v_type == VAR_FUNC
5198 || (tv->v_type == VAR_PARTIAL
5199 && (tv->vval.v_partial->pt_auto
5200 || tv->vval.v_partial->pt_dict == NULL))))
5201 dict_tv->vval.v_dict =
5202 make_partial(dict_tv->vval.v_dict, tv);
5203 dict_stack_drop();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005204 }
5205 break;
5206
5207 case ISN_NEGATENR:
5208 tv = STACK_TV_BOT(-1);
Bram Moolenaar0c7f2612022-02-17 19:44:07 +00005209 // CHECKTYPE should have checked the variable type
Bram Moolenaarc58164c2020-03-29 18:40:30 +02005210 if (tv->v_type == VAR_FLOAT)
5211 tv->vval.v_float = -tv->vval.v_float;
5212 else
Bram Moolenaarc58164c2020-03-29 18:40:30 +02005213 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005214 break;
5215
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005216 case ISN_CHECKTYPE:
5217 {
5218 checktype_T *ct = &iptr->isn_arg.type;
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01005219 int save_wt_variable = ectx->ec_where.wt_variable;
Bram Moolenaarb1040dc2022-05-18 11:00:48 +01005220 int r;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005221
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01005222 tv = STACK_TV_BOT((int)ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02005223 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005224 if (!ectx->ec_where.wt_variable)
5225 ectx->ec_where.wt_index = ct->ct_arg_idx;
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01005226 ectx->ec_where.wt_variable = ct->ct_is_var;
Bram Moolenaarb1040dc2022-05-18 11:00:48 +01005227 r = check_typval_type(ct->ct_type, tv, ectx->ec_where);
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01005228 ectx->ec_where.wt_variable = save_wt_variable;
Bram Moolenaarb1040dc2022-05-18 11:00:48 +01005229 if (r == FAIL)
5230 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005231 if (!ectx->ec_where.wt_variable)
5232 ectx->ec_where.wt_index = 0;
Bram Moolenaar5e654232020-09-16 15:22:00 +02005233
5234 // number 0 is FALSE, number 1 is TRUE
5235 if (tv->v_type == VAR_NUMBER
5236 && ct->ct_type->tt_type == VAR_BOOL
5237 && (tv->vval.v_number == 0
5238 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005239 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02005240 tv->v_type = VAR_BOOL;
5241 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02005242 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005243 }
5244 }
5245 break;
5246
Bram Moolenaar9af78762020-06-16 11:34:42 +02005247 case ISN_CHECKLEN:
5248 {
5249 int min_len = iptr->isn_arg.checklen.cl_min_len;
5250 list_T *list = NULL;
5251
5252 tv = STACK_TV_BOT(-1);
5253 if (tv->v_type == VAR_LIST)
5254 list = tv->vval.v_list;
5255 if (list == NULL || list->lv_len < min_len
5256 || (list->lv_len > min_len
5257 && !iptr->isn_arg.checklen.cl_more_OK))
5258 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02005259 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005260 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02005261 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02005262 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02005263 }
5264 }
5265 break;
5266
Bram Moolenaaraa210a32021-01-02 15:41:03 +01005267 case ISN_SETTYPE:
Bram Moolenaar381692b2022-02-02 20:01:27 +00005268 set_tv_type(STACK_TV_BOT(-1), iptr->isn_arg.type.ct_type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01005269 break;
5270
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005271 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005272 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005273 {
5274 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005275 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005276
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005277 if (iptr->isn_type == ISN_2BOOL)
5278 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005279 tv = STACK_TV_BOT(iptr->isn_arg.tobool.offset);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005280 n = tv2bool(tv);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005281 if (iptr->isn_arg.tobool.invert)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005282 n = !n;
5283 }
5284 else
5285 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005286 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02005287 SOURCING_LNUM = iptr->isn_lnum;
5288 n = tv_get_bool_chk(tv, &error);
5289 if (error)
5290 goto on_error;
5291 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005292 clear_tv(tv);
5293 tv->v_type = VAR_BOOL;
5294 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
5295 }
5296 break;
5297
5298 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02005299 case ISN_2STRING_ANY:
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01005300 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005301 if (do_2string(STACK_TV_BOT(iptr->isn_arg.tostring.offset),
5302 iptr->isn_type == ISN_2STRING_ANY,
5303 iptr->isn_arg.tostring.tolerant) == FAIL)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01005304 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005305 break;
5306
Bram Moolenaar08597872020-12-10 19:43:40 +01005307 case ISN_RANGE:
5308 {
5309 exarg_T ea;
5310 char *errormsg;
5311
Bram Moolenaarece0b872021-01-08 20:40:45 +01005312 ea.line2 = 0;
Bram Moolenaard1510ee2021-01-04 16:15:58 +01005313 ea.addr_count = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01005314 ea.addr_type = ADDR_LINES;
5315 ea.cmd = iptr->isn_arg.string;
Bram Moolenaarece0b872021-01-08 20:40:45 +01005316 ea.skip = FALSE;
Bram Moolenaar08597872020-12-10 19:43:40 +01005317 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaarece0b872021-01-08 20:40:45 +01005318 goto on_error;
Bram Moolenaara28639e2021-01-19 22:48:09 +01005319
Bram Moolenaar35578162021-08-02 19:10:38 +02005320 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005321 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005322 ++ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01005323 tv = STACK_TV_BOT(-1);
5324 tv->v_type = VAR_NUMBER;
5325 tv->v_lock = 0;
Bram Moolenaar06651632022-04-27 17:54:25 +01005326 tv->vval.v_number = ea.line2;
Bram Moolenaar08597872020-12-10 19:43:40 +01005327 }
5328 break;
5329
Bram Moolenaarc3516f72020-09-08 22:45:35 +02005330 case ISN_PUT:
5331 {
5332 int regname = iptr->isn_arg.put.put_regname;
5333 linenr_T lnum = iptr->isn_arg.put.put_lnum;
5334 char_u *expr = NULL;
5335 int dir = FORWARD;
5336
Bram Moolenaar08597872020-12-10 19:43:40 +01005337 if (lnum < -2)
5338 {
5339 // line number was put on the stack by ISN_RANGE
5340 tv = STACK_TV_BOT(-1);
5341 curwin->w_cursor.lnum = tv->vval.v_number;
5342 if (lnum == LNUM_VARIABLE_RANGE_ABOVE)
5343 dir = BACKWARD;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005344 --ectx->ec_stack.ga_len;
Bram Moolenaar08597872020-12-10 19:43:40 +01005345 }
5346 else if (lnum == -2)
Bram Moolenaarc3516f72020-09-08 22:45:35 +02005347 // :put! above cursor
5348 dir = BACKWARD;
5349 else if (lnum >= 0)
Bram Moolenaar4e713ba2022-02-07 15:31:37 +00005350 {
5351 curwin->w_cursor.lnum = lnum;
5352 if (lnum == 0)
5353 // check_cursor() below will move to line 1
5354 dir = BACKWARD;
5355 }
Bram Moolenaara28639e2021-01-19 22:48:09 +01005356
5357 if (regname == '=')
5358 {
5359 tv = STACK_TV_BOT(-1);
5360 if (tv->v_type == VAR_STRING)
5361 expr = tv->vval.v_string;
5362 else
5363 {
5364 expr = typval2string(tv, TRUE); // allocates value
5365 clear_tv(tv);
5366 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005367 --ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01005368 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02005369 check_cursor();
5370 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
5371 vim_free(expr);
5372 }
5373 break;
5374
Bram Moolenaar02194d22020-10-24 23:08:38 +02005375 case ISN_CMDMOD:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005376 ectx->ec_funclocal.floc_save_cmdmod = cmdmod;
5377 ectx->ec_funclocal.floc_restore_cmdmod = TRUE;
5378 ectx->ec_funclocal.floc_restore_cmdmod_stacklen =
5379 ectx->ec_stack.ga_len;
Bram Moolenaar02194d22020-10-24 23:08:38 +02005380 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
5381 apply_cmdmod(&cmdmod);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02005382 break;
5383
Bram Moolenaar02194d22020-10-24 23:08:38 +02005384 case ISN_CMDMOD_REV:
5385 // filter regprog is owned by the instruction, don't free it
5386 cmdmod.cmod_filter_regmatch.regprog = NULL;
5387 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005388 cmdmod = ectx->ec_funclocal.floc_save_cmdmod;
5389 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02005390 break;
5391
Bram Moolenaar792f7862020-11-23 08:31:18 +01005392 case ISN_UNPACK:
5393 {
5394 int count = iptr->isn_arg.unpack.unp_count;
5395 int semicolon = iptr->isn_arg.unpack.unp_semicolon;
5396 list_T *l;
5397 listitem_T *li;
5398 int i;
5399
5400 // Check there is a valid list to unpack.
5401 tv = STACK_TV_BOT(-1);
5402 if (tv->v_type != VAR_LIST)
5403 {
5404 SOURCING_LNUM = iptr->isn_lnum;
5405 emsg(_(e_for_argument_must_be_sequence_of_lists));
5406 goto on_error;
5407 }
5408 l = tv->vval.v_list;
5409 if (l == NULL
5410 || l->lv_len < (semicolon ? count - 1 : count))
5411 {
5412 SOURCING_LNUM = iptr->isn_lnum;
5413 emsg(_(e_list_value_does_not_have_enough_items));
5414 goto on_error;
5415 }
5416 else if (!semicolon && l->lv_len > count)
5417 {
5418 SOURCING_LNUM = iptr->isn_lnum;
5419 emsg(_(e_list_value_has_more_items_than_targets));
5420 goto on_error;
5421 }
5422
5423 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar35578162021-08-02 19:10:38 +02005424 if (GA_GROW_FAILS(&ectx->ec_stack, count - 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005425 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005426 ectx->ec_stack.ga_len += count - 1;
Bram Moolenaar792f7862020-11-23 08:31:18 +01005427
5428 // Variable after semicolon gets a list with the remaining
5429 // items.
5430 if (semicolon)
5431 {
5432 list_T *rem_list =
5433 list_alloc_with_items(l->lv_len - count + 1);
5434
5435 if (rem_list == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005436 goto theend;
Bram Moolenaar792f7862020-11-23 08:31:18 +01005437 tv = STACK_TV_BOT(-count);
5438 tv->vval.v_list = rem_list;
5439 ++rem_list->lv_refcount;
5440 tv->v_lock = 0;
5441 li = l->lv_first;
5442 for (i = 0; i < count - 1; ++i)
5443 li = li->li_next;
5444 for (i = 0; li != NULL; ++i)
5445 {
Bram Moolenaar61efa162022-03-18 13:10:48 +00005446 typval_T tvcopy;
5447
5448 copy_tv(&li->li_tv, &tvcopy);
5449 list_set_item(rem_list, i, &tvcopy);
Bram Moolenaar792f7862020-11-23 08:31:18 +01005450 li = li->li_next;
5451 }
5452 --count;
5453 }
5454
5455 // Produce the values in reverse order, first item last.
5456 li = l->lv_first;
5457 for (i = 0; i < count; ++i)
5458 {
5459 tv = STACK_TV_BOT(-i - 1);
5460 copy_tv(&li->li_tv, tv);
5461 li = li->li_next;
5462 }
5463
5464 list_unref(l);
5465 }
5466 break;
5467
Bram Moolenaarb2049902021-01-24 12:53:53 +01005468 case ISN_PROF_START:
5469 case ISN_PROF_END:
5470 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01005471#ifdef FEAT_PROFILE
Bram Moolenaarca16c602022-09-06 18:57:08 +01005472 funccall_T cookie;
5473 ufunc_T *cur_ufunc =
Bram Moolenaarb2049902021-01-24 12:53:53 +01005474 (((dfunc_T *)def_functions.ga_data)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02005475 + ectx->ec_dfunc_idx)->df_ufunc;
Bram Moolenaarb2049902021-01-24 12:53:53 +01005476
Bram Moolenaarca16c602022-09-06 18:57:08 +01005477 cookie.fc_func = cur_ufunc;
Bram Moolenaarb2049902021-01-24 12:53:53 +01005478 if (iptr->isn_type == ISN_PROF_START)
5479 {
5480 func_line_start(&cookie, iptr->isn_lnum);
5481 // if we get here the instruction is executed
5482 func_line_exec(&cookie);
5483 }
5484 else
5485 func_line_end(&cookie);
Bram Moolenaarf002a412021-01-24 13:34:18 +01005486#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01005487 }
5488 break;
5489
Bram Moolenaare99d4222021-06-13 14:01:26 +02005490 case ISN_DEBUG:
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02005491 handle_debug(iptr, ectx);
Bram Moolenaare99d4222021-06-13 14:01:26 +02005492 break;
5493
Bram Moolenaar389df252020-07-09 21:20:47 +02005494 case ISN_SHUFFLE:
5495 {
Bram Moolenaar792f7862020-11-23 08:31:18 +01005496 typval_T tmp_tv;
5497 int item = iptr->isn_arg.shuffle.shfl_item;
5498 int up = iptr->isn_arg.shuffle.shfl_up;
Bram Moolenaar389df252020-07-09 21:20:47 +02005499
5500 tmp_tv = *STACK_TV_BOT(-item);
5501 for ( ; up > 0 && item > 1; --up)
5502 {
5503 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
5504 --item;
5505 }
5506 *STACK_TV_BOT(-item) = tmp_tv;
5507 }
5508 break;
5509
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005510 case ISN_DROP:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005511 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005512 clear_tv(STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005513 ectx->ec_where.wt_index = 0;
5514 ectx->ec_where.wt_variable = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005515 break;
5516 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02005517 continue;
5518
Bram Moolenaard032f342020-07-18 18:13:02 +02005519func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02005520 // Restore previous function. If the frame pointer is where we started
5521 // then there is none and we are done.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005522 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02005523 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02005524
Bram Moolenaar4c137212021-04-19 16:48:48 +02005525 if (func_return(ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02005526 // only fails when out of memory
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005527 goto theend;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02005528 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02005529
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02005530on_error:
Bram Moolenaaraf0df472020-12-02 20:51:22 +01005531 // Jump here for an error that does not require aborting execution.
Bram Moolenaar56602ba2020-12-05 21:22:08 +01005532 // If "emsg_silent" is set then ignore the error, unless it was set
5533 // when calling the function.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005534 if (did_emsg_cumul + did_emsg == ectx->ec_did_emsg_before
Bram Moolenaar56602ba2020-12-05 21:22:08 +01005535 && emsg_silent && did_emsg_def == 0)
Bram Moolenaarf9041332021-01-21 19:41:16 +01005536 {
5537 // If a sequence of instructions causes an error while ":silent!"
5538 // was used, restore the stack length and jump ahead to restoring
5539 // the cmdmod.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005540 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaarf9041332021-01-21 19:41:16 +01005541 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005542 while (ectx->ec_stack.ga_len
5543 > ectx->ec_funclocal.floc_restore_cmdmod_stacklen)
Bram Moolenaarf9041332021-01-21 19:41:16 +01005544 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005545 --ectx->ec_stack.ga_len;
Bram Moolenaarf9041332021-01-21 19:41:16 +01005546 clear_tv(STACK_TV_BOT(0));
5547 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005548 while (ectx->ec_instr[ectx->ec_iidx].isn_type != ISN_CMDMOD_REV)
5549 ++ectx->ec_iidx;
Bram Moolenaarf9041332021-01-21 19:41:16 +01005550 }
Bram Moolenaarcd030c42020-10-30 21:49:40 +01005551 continue;
Bram Moolenaarf9041332021-01-21 19:41:16 +01005552 }
Bram Moolenaaraf0df472020-12-02 20:51:22 +01005553on_fatal_error:
5554 // Jump here for an error that messes up the stack.
Bram Moolenaar171fb922020-10-28 16:54:47 +01005555 // If we are not inside a try-catch started here, abort execution.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005556 if (trylevel <= ectx->ec_trylevel_at_start)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005557 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005558 }
5559
5560done:
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005561 ret = OK;
5562theend:
Bram Moolenaar58779852022-09-06 18:31:14 +01005563 may_invoke_defer_funcs(ectx);
Bram Moolenaar1d84f762022-09-03 21:35:53 +01005564
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005565 dict_stack_clear(dict_stack_len_at_start);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02005566 ectx->ec_trylevel_at_start = save_trylevel_at_start;
5567 return ret;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005568}
5569
5570/*
Bram Moolenaar806a2732022-09-04 15:40:36 +01005571 * Execute the instructions from a VAR_INSTR typval and put the result in
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005572 * "rettv".
5573 * Return OK or FAIL.
5574 */
5575 int
5576exe_typval_instr(typval_T *tv, typval_T *rettv)
5577{
5578 ectx_T *ectx = tv->vval.v_instr->instr_ectx;
5579 isn_T *save_instr = ectx->ec_instr;
5580 int save_iidx = ectx->ec_iidx;
5581 int res;
5582
LemonBoyf3b48952022-05-05 13:53:03 +01005583 // Initialize rettv so that it is safe for caller to invoke clear_tv(rettv)
5584 // even when the compilation fails.
5585 rettv->v_type = VAR_UNKNOWN;
5586
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005587 ectx->ec_instr = tv->vval.v_instr->instr_instr;
5588 res = exec_instructions(ectx);
5589 if (res == OK)
5590 {
5591 *rettv = *STACK_TV_BOT(-1);
5592 --ectx->ec_stack.ga_len;
5593 }
5594
5595 ectx->ec_instr = save_instr;
5596 ectx->ec_iidx = save_iidx;
5597
5598 return res;
5599}
5600
5601/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02005602 * Execute the instructions from an ISN_SUBSTITUTE command, which are in
5603 * "substitute_instr".
5604 */
5605 char_u *
5606exe_substitute_instr(void)
5607{
5608 ectx_T *ectx = substitute_instr->subs_ectx;
5609 isn_T *save_instr = ectx->ec_instr;
5610 int save_iidx = ectx->ec_iidx;
5611 char_u *res;
5612
5613 ectx->ec_instr = substitute_instr->subs_instr;
5614 if (exec_instructions(ectx) == OK)
Bram Moolenaar90193e62021-04-04 20:49:50 +02005615 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005616 typval_T *tv = STACK_TV_BOT(-1);
5617
Bram Moolenaar27523602021-06-05 21:36:19 +02005618 res = typval2string(tv, TRUE);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005619 --ectx->ec_stack.ga_len;
5620 clear_tv(tv);
Bram Moolenaar90193e62021-04-04 20:49:50 +02005621 }
5622 else
5623 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005624 substitute_instr->subs_status = FAIL;
5625 res = vim_strsave((char_u *)"");
Bram Moolenaar90193e62021-04-04 20:49:50 +02005626 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005627
Bram Moolenaar4c137212021-04-19 16:48:48 +02005628 ectx->ec_instr = save_instr;
5629 ectx->ec_iidx = save_iidx;
5630
5631 return res;
5632}
5633
5634/*
5635 * Call a "def" function from old Vim script.
5636 * Return OK or FAIL.
5637 */
5638 int
5639call_def_function(
5640 ufunc_T *ufunc,
5641 int argc_arg, // nr of arguments
5642 typval_T *argv, // arguments
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005643 int flags, // DEF_ flags
Bram Moolenaar4c137212021-04-19 16:48:48 +02005644 partial_T *partial, // optional partial for context
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005645 object_T *object, // object, e.g. for this.Func()
Bram Moolenaar58779852022-09-06 18:31:14 +01005646 funccall_T *funccal,
Bram Moolenaar4c137212021-04-19 16:48:48 +02005647 typval_T *rettv) // return value
5648{
5649 ectx_T ectx; // execution context
5650 int argc = argc_arg;
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005651 int partial_argc = partial == NULL
5652 || (flags & DEF_USE_PT_ARGV) == 0
5653 ? 0 : partial->pt_argc;
5654 int total_argc = argc + partial_argc;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005655 typval_T *tv;
5656 int idx;
5657 int ret = FAIL;
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005658 int defcount = ufunc->uf_args.ga_len - total_argc;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005659 sctx_T save_current_sctx = current_sctx;
5660 int did_emsg_before = did_emsg_cumul + did_emsg;
5661 int save_suppress_errthrow = suppress_errthrow;
5662 msglist_T **saved_msg_list = NULL;
5663 msglist_T *private_msg_list = NULL;
5664 int save_emsg_silent_def = emsg_silent_def;
5665 int save_did_emsg_def = did_emsg_def;
5666 int orig_funcdepth;
Bram Moolenaare99d4222021-06-13 14:01:26 +02005667 int orig_nesting_level = ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005668
5669// Get pointer to item in the stack.
5670#undef STACK_TV
5671#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
5672
5673// Get pointer to item at the bottom of the stack, -1 is the bottom.
5674#undef STACK_TV_BOT
5675#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
5676
5677// Get pointer to a local variable on the stack. Negative for arguments.
5678#undef STACK_TV_VAR
5679#define STACK_TV_VAR(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_frame_idx + STACK_FRAME_SIZE + idx)
5680
5681 if (ufunc->uf_def_status == UF_NOT_COMPILED
5682 || ufunc->uf_def_status == UF_COMPILE_ERROR
Bram Moolenaar139575d2022-03-15 19:29:30 +00005683 || (func_needs_compiling(ufunc, get_compile_type(ufunc))
5684 && compile_def_function(ufunc, FALSE,
5685 get_compile_type(ufunc), NULL) == FAIL))
Bram Moolenaar4c137212021-04-19 16:48:48 +02005686 {
5687 if (did_emsg_cumul + did_emsg == did_emsg_before)
5688 semsg(_(e_function_is_not_compiled_str),
5689 printable_func_name(ufunc));
5690 return FAIL;
5691 }
5692
5693 {
5694 // Check the function was really compiled.
5695 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5696 + ufunc->uf_dfunc_idx;
Bram Moolenaarcc341812022-09-19 15:54:34 +01005697 if (dfunc->df_ufunc == NULL)
5698 {
5699 semsg(_(e_function_was_deleted_str), printable_func_name(ufunc));
5700 return FAIL;
5701 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005702 if (INSTRUCTIONS(dfunc) == NULL)
5703 {
5704 iemsg("using call_def_function() on not compiled function");
5705 return FAIL;
5706 }
5707 }
5708
5709 // If depth of calling is getting too high, don't execute the function.
5710 orig_funcdepth = funcdepth_get();
5711 if (funcdepth_increment() == FAIL)
5712 return FAIL;
5713
5714 CLEAR_FIELD(ectx);
5715 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
5716 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
Bram Moolenaar35578162021-08-02 19:10:38 +02005717 if (GA_GROW_FAILS(&ectx.ec_stack, 20))
Bram Moolenaar4c137212021-04-19 16:48:48 +02005718 {
5719 funcdepth_decrement();
5720 return FAIL;
5721 }
5722 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
5723 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
5724 ectx.ec_did_emsg_before = did_emsg_before;
Bram Moolenaare99d4222021-06-13 14:01:26 +02005725 ++ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005726
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005727 idx = total_argc - ufunc->uf_args.ga_len;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005728 if (idx > 0 && ufunc->uf_va_name == NULL)
5729 {
Matvey Tarasovd14bb1a2022-06-29 13:18:27 +01005730 semsg(NGETTEXT(e_one_argument_too_many, e_nr_arguments_too_many,
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005731 idx), idx);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005732 goto failed_early;
5733 }
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005734 idx = total_argc - ufunc->uf_args.ga_len + ufunc->uf_def_args.ga_len;
Bram Moolenaarc6d71532021-06-05 18:49:38 +02005735 if (idx < 0)
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02005736 {
Matvey Tarasovd14bb1a2022-06-29 13:18:27 +01005737 semsg(NGETTEXT(e_one_argument_too_few, e_nr_arguments_too_few,
Bram Moolenaar98aff652022-09-06 21:02:35 +01005738 -idx), -idx);
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02005739 goto failed_early;
5740 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005741
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005742 // Put values from the partial and arguments on the stack, but no more than
5743 // what the function expects. A lambda can be called with more arguments
5744 // than it uses.
5745 for (idx = 0; idx < total_argc
Bram Moolenaar4c137212021-04-19 16:48:48 +02005746 && (ufunc->uf_va_name != NULL || idx < ufunc->uf_args.ga_len);
5747 ++idx)
5748 {
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005749 int argv_idx = idx - partial_argc;
5750
5751 tv = idx < partial_argc ? partial->pt_argv + idx : argv + argv_idx;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005752 if (idx >= ufunc->uf_args.ga_len - ufunc->uf_def_args.ga_len
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005753 && tv->v_type == VAR_SPECIAL
5754 && tv->vval.v_number == VVAL_NONE)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005755 {
5756 // Use the default value.
5757 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
5758 }
5759 else
5760 {
5761 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
5762 && check_typval_arg_type(
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005763 ufunc->uf_arg_types[idx], tv,
5764 NULL, argv_idx + 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005765 goto failed_early;
Bram Moolenaarc9c967d2022-09-07 16:48:46 +01005766 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005767 }
5768 ++ectx.ec_stack.ga_len;
5769 }
5770
5771 // Turn varargs into a list. Empty list if no args.
5772 if (ufunc->uf_va_name != NULL)
5773 {
5774 int vararg_count = argc - ufunc->uf_args.ga_len;
5775
5776 if (vararg_count < 0)
5777 vararg_count = 0;
5778 else
5779 argc -= vararg_count;
5780 if (exe_newlist(vararg_count, &ectx) == FAIL)
5781 goto failed_early;
5782
5783 // Check the type of the list items.
5784 tv = STACK_TV_BOT(-1);
5785 if (ufunc->uf_va_type != NULL
5786 && ufunc->uf_va_type != &t_list_any
5787 && ufunc->uf_va_type->tt_member != &t_any
5788 && tv->vval.v_list != NULL)
5789 {
5790 type_T *expected = ufunc->uf_va_type->tt_member;
5791 listitem_T *li = tv->vval.v_list->lv_first;
5792
5793 for (idx = 0; idx < vararg_count; ++idx)
5794 {
5795 if (check_typval_arg_type(expected, &li->li_tv,
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02005796 NULL, argc + idx + 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005797 goto failed_early;
5798 li = li->li_next;
5799 }
5800 }
5801
5802 if (defcount > 0)
5803 // Move varargs list to below missing default arguments.
5804 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
5805 --ectx.ec_stack.ga_len;
5806 }
5807
5808 // Make space for omitted arguments, will store default value below.
5809 // Any varargs list goes after them.
5810 if (defcount > 0)
5811 for (idx = 0; idx < defcount; ++idx)
5812 {
5813 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
5814 ++ectx.ec_stack.ga_len;
5815 }
5816 if (ufunc->uf_va_name != NULL)
5817 ++ectx.ec_stack.ga_len;
5818
5819 // Frame pointer points to just after arguments.
5820 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
5821 ectx.ec_initial_frame_idx = ectx.ec_frame_idx;
5822
5823 {
5824 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5825 + ufunc->uf_dfunc_idx;
5826 ufunc_T *base_ufunc = dfunc->df_ufunc;
5827
5828 // "uf_partial" is on the ufunc that "df_ufunc" points to, as is done
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01005829 // by copy_lambda_to_global_func().
Bram Moolenaar4c137212021-04-19 16:48:48 +02005830 if (partial != NULL || base_ufunc->uf_partial != NULL)
5831 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005832 ectx.ec_outer_ref = ALLOC_CLEAR_ONE(outer_ref_T);
5833 if (ectx.ec_outer_ref == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005834 goto failed_early;
5835 if (partial != NULL)
5836 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00005837 outer_T *outer = get_pt_outer(partial);
5838
Bram Moolenaar6d313be2022-09-22 16:36:25 +01005839 if (outer->out_stack == NULL && outer->out_loop_size == 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02005840 {
Bram Moolenaar6d313be2022-09-22 16:36:25 +01005841 // no stack was set
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00005842 if (current_ectx != NULL)
5843 {
5844 if (current_ectx->ec_outer_ref != NULL
5845 && current_ectx->ec_outer_ref->or_outer != NULL)
5846 ectx.ec_outer_ref->or_outer =
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005847 current_ectx->ec_outer_ref->or_outer;
Bram Moolenaarfe1bfc92022-02-06 13:55:03 +00005848 }
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005849 // else: should there be an error here?
Bram Moolenaar4c137212021-04-19 16:48:48 +02005850 }
5851 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005852 {
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00005853 ectx.ec_outer_ref->or_outer = outer;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005854 ++partial->pt_refcount;
5855 ectx.ec_outer_ref->or_partial = partial;
5856 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005857 }
5858 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005859 {
5860 ectx.ec_outer_ref->or_outer = &base_ufunc->uf_partial->pt_outer;
5861 ++base_ufunc->uf_partial->pt_refcount;
5862 ectx.ec_outer_ref->or_partial = base_ufunc->uf_partial;
5863 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005864 }
5865 }
5866
5867 // dummy frame entries
5868 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
5869 {
5870 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
5871 ++ectx.ec_stack.ga_len;
5872 }
5873
5874 {
5875 // Reserve space for local variables and any closure reference count.
5876 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
5877 + ufunc->uf_dfunc_idx;
5878
Bram Moolenaar5cd64792021-12-25 18:23:24 +00005879 // Initialize variables to zero. That avoids having to generate
5880 // initializing instructions for "var nr: number", "var x: any", etc.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005881 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaar5cd64792021-12-25 18:23:24 +00005882 {
5883 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
5884 STACK_TV_VAR(idx)->vval.v_number = 0;
5885 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005886 ectx.ec_stack.ga_len += dfunc->df_varcount;
Bram Moolenaarffdaca92022-12-09 21:41:48 +00005887
5888 if (object != NULL)
5889 {
5890 // the object is always the variable at index zero
5891 tv = STACK_TV_VAR(0);
5892 tv->v_type = VAR_OBJECT;
5893 tv->vval.v_object = object;
5894 }
5895
Bram Moolenaar4c137212021-04-19 16:48:48 +02005896 if (dfunc->df_has_closure)
5897 {
Bram Moolenaarcc341812022-09-19 15:54:34 +01005898 // Initialize the variable that counts how many closures were
5899 // created. This is used in handle_closure_in_use().
Bram Moolenaar4c137212021-04-19 16:48:48 +02005900 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
5901 STACK_TV_VAR(idx)->vval.v_number = 0;
5902 ++ectx.ec_stack.ga_len;
5903 }
5904
5905 ectx.ec_instr = INSTRUCTIONS(dfunc);
5906 }
5907
Bram Moolenaar58779852022-09-06 18:31:14 +01005908 // Store the execution context in funccal, used by invoke_all_defer().
5909 if (funccal != NULL)
5910 funccal->fc_ectx = &ectx;
5911
Bram Moolenaar4c137212021-04-19 16:48:48 +02005912 // Following errors are in the function, not the caller.
5913 // Commands behave like vim9script.
5914 estack_push_ufunc(ufunc, 1);
5915 current_sctx = ufunc->uf_script_ctx;
5916 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
5917
5918 // Use a specific location for storing error messages to be converted to an
5919 // exception.
5920 saved_msg_list = msg_list;
5921 msg_list = &private_msg_list;
5922
5923 // Do turn errors into exceptions.
5924 suppress_errthrow = FALSE;
5925
5926 // Do not delete the function while executing it.
5927 ++ufunc->uf_calls;
5928
5929 // When ":silent!" was used before calling then we still abort the
5930 // function. If ":silent!" is used in the function then we don't.
5931 emsg_silent_def = emsg_silent;
5932 did_emsg_def = 0;
5933
5934 ectx.ec_where.wt_index = 0;
5935 ectx.ec_where.wt_variable = FALSE;
5936
Bram Moolenaar5800c792022-09-22 17:34:01 +01005937 /*
5938 * Execute the instructions until done.
5939 */
Bram Moolenaar4c137212021-04-19 16:48:48 +02005940 ret = exec_instructions(&ectx);
5941 if (ret == OK)
5942 {
5943 // function finished, get result from the stack.
5944 if (ufunc->uf_ret_type == &t_void)
5945 {
5946 rettv->v_type = VAR_VOID;
5947 }
5948 else
5949 {
5950 tv = STACK_TV_BOT(-1);
5951 *rettv = *tv;
5952 tv->v_type = VAR_UNKNOWN;
5953 }
5954 }
5955
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01005956 // When failed need to unwind the call stack.
Bram Moolenaar58779852022-09-06 18:31:14 +01005957 unwind_def_callstack(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02005958
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005959 // Deal with any remaining closures, they may be in use somewhere.
5960 if (ectx.ec_funcrefs.ga_len > 0)
Bram Moolenaarf112f302020-12-20 17:47:52 +01005961 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005962 handle_closure_in_use(&ectx, FALSE);
Bram Moolenaardc7c3662021-12-20 15:04:29 +00005963 ga_clear(&ectx.ec_funcrefs);
Bram Moolenaarf112f302020-12-20 17:47:52 +01005964 }
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005965
Bram Moolenaaree8580e2020-08-28 17:19:07 +02005966 estack_pop();
5967 current_sctx = save_current_sctx;
5968
Bram Moolenaar2336c372021-12-06 15:06:54 +00005969 if (--ufunc->uf_calls <= 0 && ufunc->uf_refcount <= 0)
5970 // Function was unreferenced while being used, free it now.
5971 func_clear_free(ufunc, FALSE);
Bram Moolenaarc970e422021-03-17 15:03:04 +01005972
Bram Moolenaar352134b2020-10-17 22:04:08 +02005973 if (*msg_list != NULL && saved_msg_list != NULL)
5974 {
5975 msglist_T **plist = saved_msg_list;
5976
5977 // Append entries from the current msg_list (uncaught exceptions) to
5978 // the saved msg_list.
5979 while (*plist != NULL)
5980 plist = &(*plist)->next;
5981
5982 *plist = *msg_list;
5983 }
5984 msg_list = saved_msg_list;
5985
Bram Moolenaar4c137212021-04-19 16:48:48 +02005986 if (ectx.ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar02194d22020-10-24 23:08:38 +02005987 {
5988 cmdmod.cmod_filter_regmatch.regprog = NULL;
5989 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005990 cmdmod = ectx.ec_funclocal.floc_save_cmdmod;
Bram Moolenaar02194d22020-10-24 23:08:38 +02005991 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +01005992 emsg_silent_def = save_emsg_silent_def;
5993 did_emsg_def += save_did_emsg_def;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02005994
Bram Moolenaaree8580e2020-08-28 17:19:07 +02005995failed_early:
Bram Moolenaar0d1f55c2022-04-05 17:30:29 +01005996 // Free all arguments and local variables.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005997 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
Bram Moolenaar1936c762022-09-28 15:19:10 +01005998 {
5999 tv = STACK_TV(idx);
6000 if (tv->v_type != VAR_NUMBER && tv->v_type != VAR_UNKNOWN)
6001 clear_tv(tv);
6002 }
Bram Moolenaare99d4222021-06-13 14:01:26 +02006003 ex_nesting_level = orig_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02006004
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006005 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01006006 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006007 if (ectx.ec_outer_ref != NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +01006008 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02006009 if (ectx.ec_outer_ref->or_outer_allocated)
6010 vim_free(ectx.ec_outer_ref->or_outer);
6011 partial_unref(ectx.ec_outer_ref->or_partial);
6012 vim_free(ectx.ec_outer_ref);
Bram Moolenaar0186e582021-01-10 18:33:11 +01006013 }
6014
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02006015 // Not sure if this is necessary.
6016 suppress_errthrow = save_suppress_errthrow;
6017
Bram Moolenaarb5841b92021-07-15 18:09:53 +02006018 if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before
6019 && !need_rethrow)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006020 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02006021 printable_func_name(ufunc));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01006022 funcdepth_restore(orig_funcdepth);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006023 return ret;
6024}
6025
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006026/*
Bram Moolenaar58779852022-09-06 18:31:14 +01006027 * Called when a def function has finished (possibly failed).
6028 * Invoke all the function returns to clean up and invoke deferred functions,
6029 * except the toplevel one.
6030 */
6031 void
6032unwind_def_callstack(ectx_T *ectx)
6033{
6034 while (ectx->ec_frame_idx != ectx->ec_initial_frame_idx)
6035 func_return(ectx);
6036}
6037
6038/*
dundargocc57b5bc2022-11-02 13:30:51 +00006039 * Invoke any deferred functions for the top function in "ectx".
Bram Moolenaar58779852022-09-06 18:31:14 +01006040 */
6041 void
6042may_invoke_defer_funcs(ectx_T *ectx)
6043{
6044 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
6045
6046 if (dfunc->df_defer_var_idx > 0)
6047 invoke_defer_funcs(ectx);
6048}
6049
6050/*
Bram Moolenaarcc341812022-09-19 15:54:34 +01006051 * Return loopvarinfo in a printable form in allocated memory.
6052 */
6053 static char_u *
6054printable_loopvarinfo(loopvarinfo_T *lvi)
6055{
6056 garray_T ga;
6057 int depth;
6058
6059 ga_init2(&ga, 1, 100);
6060 for (depth = 0; depth < lvi->lvi_depth; ++depth)
6061 {
6062 if (ga_grow(&ga, 50) == FAIL)
6063 break;
6064 if (lvi->lvi_loop[depth].var_idx == 0)
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +01006065 STRCPY((char *)ga.ga_data + ga.ga_len, " -");
Bram Moolenaarcc341812022-09-19 15:54:34 +01006066 else
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +01006067 vim_snprintf((char *)ga.ga_data + ga.ga_len, 50, " $%d-$%d",
Bram Moolenaarcc341812022-09-19 15:54:34 +01006068 lvi->lvi_loop[depth].var_idx,
6069 lvi->lvi_loop[depth].var_idx
6070 + lvi->lvi_loop[depth].var_count - 1);
Bram Moolenaarc9e4a6f2022-09-19 16:08:04 +01006071 ga.ga_len = (int)STRLEN(ga.ga_data);
Bram Moolenaarcc341812022-09-19 15:54:34 +01006072 }
6073 return ga.ga_data;
6074}
6075
6076/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02006077 * List instructions "instr" up to "instr_count" or until ISN_FINISH.
6078 * "ufunc" has the source lines, NULL for the instructions of ISN_SUBSTITUTE.
6079 * "pfx" is prefixed to every line.
6080 */
6081 static void
6082list_instructions(char *pfx, isn_T *instr, int instr_count, ufunc_T *ufunc)
6083{
6084 int line_idx = 0;
6085 int prev_current = 0;
6086 int current;
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02006087 int def_arg_idx = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006088
6089 for (current = 0; current < instr_count; ++current)
6090 {
6091 isn_T *iptr = &instr[current];
6092 char *line;
6093
6094 if (ufunc != NULL)
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02006095 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02006096 while (line_idx < iptr->isn_lnum
6097 && line_idx < ufunc->uf_lines.ga_len)
6098 {
6099 if (current > prev_current)
6100 {
6101 msg_puts("\n\n");
6102 prev_current = current;
6103 }
6104 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
6105 if (line != NULL)
6106 msg(line);
6107 }
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02006108 if (iptr->isn_type == ISN_JUMP_IF_ARG_SET)
6109 {
6110 int first_def_arg = ufunc->uf_args.ga_len
6111 - ufunc->uf_def_args.ga_len;
6112
6113 if (def_arg_idx > 0)
6114 msg_puts("\n\n");
6115 msg_start();
6116 msg_puts(" ");
6117 msg_puts(((char **)(ufunc->uf_args.ga_data))[
6118 first_def_arg + def_arg_idx]);
6119 msg_puts(" = ");
6120 msg_puts(((char **)(ufunc->uf_def_args.ga_data))[def_arg_idx++]);
6121 msg_clr_eos();
6122 msg_end();
6123 }
6124 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006125
6126 switch (iptr->isn_type)
6127 {
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006128 case ISN_CONSTRUCT:
6129 smsg("%s%4d NEW %s size %d", pfx, current,
6130 iptr->isn_arg.construct.construct_class->class_name,
6131 (int)iptr->isn_arg.construct.construct_size);
6132 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006133 case ISN_EXEC:
6134 smsg("%s%4d EXEC %s", pfx, current, iptr->isn_arg.string);
6135 break;
Bram Moolenaar20677332021-06-06 17:02:53 +02006136 case ISN_EXEC_SPLIT:
6137 smsg("%s%4d EXEC_SPLIT %s", pfx, current, iptr->isn_arg.string);
6138 break;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00006139 case ISN_EXECRANGE:
6140 smsg("%s%4d EXECRANGE %s", pfx, current, iptr->isn_arg.string);
6141 break;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02006142 case ISN_LEGACY_EVAL:
6143 smsg("%s%4d EVAL legacy %s", pfx, current,
6144 iptr->isn_arg.string);
6145 break;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02006146 case ISN_REDIRSTART:
6147 smsg("%s%4d REDIR", pfx, current);
6148 break;
6149 case ISN_REDIREND:
6150 smsg("%s%4d REDIR END%s", pfx, current,
6151 iptr->isn_arg.number ? " append" : "");
6152 break;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006153 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02006154#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006155 smsg("%s%4d CEXPR pre %s", pfx, current,
6156 cexpr_get_auname(iptr->isn_arg.number));
Bram Moolenaarb7c97812021-05-05 22:51:39 +02006157#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006158 break;
6159 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02006160#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006161 {
6162 cexprref_T *cer = iptr->isn_arg.cexpr.cexpr_ref;
6163
6164 smsg("%s%4d CEXPR core %s%s \"%s\"", pfx, current,
6165 cexpr_get_auname(cer->cer_cmdidx),
6166 cer->cer_forceit ? "!" : "",
6167 cer->cer_cmdline);
6168 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02006169#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02006170 break;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006171 case ISN_INSTR:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006172 smsg("%s%4d INSTR", pfx, current);
6173 list_instructions(" ", iptr->isn_arg.instr, INT_MAX, NULL);
6174 msg(" -------------");
6175 break;
6176 case ISN_SOURCE:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006177 {
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006178 scriptitem_T *si = SCRIPT_ITEM(iptr->isn_arg.number);
6179
6180 smsg("%s%4d SOURCE %s", pfx, current, si->sn_name);
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006181 }
6182 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006183 case ISN_SUBSTITUTE:
6184 {
6185 subs_T *subs = &iptr->isn_arg.subs;
6186
6187 smsg("%s%4d SUBSTITUTE %s", pfx, current, subs->subs_cmd);
6188 list_instructions(" ", subs->subs_instr, INT_MAX, NULL);
6189 msg(" -------------");
6190 }
6191 break;
6192 case ISN_EXECCONCAT:
6193 smsg("%s%4d EXECCONCAT %lld", pfx, current,
6194 (varnumber_T)iptr->isn_arg.number);
6195 break;
6196 case ISN_ECHO:
6197 {
6198 echo_T *echo = &iptr->isn_arg.echo;
6199
6200 smsg("%s%4d %s %d", pfx, current,
6201 echo->echo_with_white ? "ECHO" : "ECHON",
6202 echo->echo_count);
6203 }
6204 break;
6205 case ISN_EXECUTE:
6206 smsg("%s%4d EXECUTE %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02006207 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006208 break;
6209 case ISN_ECHOMSG:
6210 smsg("%s%4d ECHOMSG %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02006211 (varnumber_T)(iptr->isn_arg.number));
6212 break;
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01006213 case ISN_ECHOWINDOW:
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01006214 if (iptr->isn_arg.echowin.ewin_time > 0)
6215 smsg("%s%4d ECHOWINDOW %d (%ld sec)", pfx, current,
6216 iptr->isn_arg.echowin.ewin_count,
6217 iptr->isn_arg.echowin.ewin_time);
6218 else
6219 smsg("%s%4d ECHOWINDOW %d", pfx, current,
6220 iptr->isn_arg.echowin.ewin_count);
Bram Moolenaar7d7ad7b2022-09-01 16:00:53 +01006221 break;
Bram Moolenaar7de62622021-08-07 15:05:47 +02006222 case ISN_ECHOCONSOLE:
6223 smsg("%s%4d ECHOCONSOLE %lld", pfx, current,
6224 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006225 break;
6226 case ISN_ECHOERR:
6227 smsg("%s%4d ECHOERR %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02006228 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006229 break;
6230 case ISN_LOAD:
6231 {
6232 if (iptr->isn_arg.number < 0)
6233 smsg("%s%4d LOAD arg[%lld]", pfx, current,
6234 (varnumber_T)(iptr->isn_arg.number
6235 + STACK_FRAME_SIZE));
6236 else
6237 smsg("%s%4d LOAD $%lld", pfx, current,
6238 (varnumber_T)(iptr->isn_arg.number));
6239 }
6240 break;
6241 case ISN_LOADOUTER:
6242 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006243 isn_outer_T *outer = &iptr->isn_arg.outer;
6244
6245 if (outer->outer_idx < 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02006246 smsg("%s%4d LOADOUTER level %d arg[%d]", pfx, current,
Bram Moolenaarcc341812022-09-19 15:54:34 +01006247 outer->outer_depth,
6248 outer->outer_idx + STACK_FRAME_SIZE);
6249 else if (outer->outer_depth < 0)
6250 smsg("%s%4d LOADOUTER $%d in loop level %d",
6251 pfx, current,
6252 outer->outer_idx,
6253 -outer->outer_depth);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006254 else
6255 smsg("%s%4d LOADOUTER level %d $%d", pfx, current,
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006256 outer->outer_depth,
6257 outer->outer_idx);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006258 }
6259 break;
6260 case ISN_LOADV:
6261 smsg("%s%4d LOADV v:%s", pfx, current,
6262 get_vim_var_name(iptr->isn_arg.number));
6263 break;
6264 case ISN_LOADSCRIPT:
6265 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006266 scriptref_T *sref = iptr->isn_arg.script.scriptref;
6267 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
6268 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006269
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006270 sv = get_script_svar(sref, -1);
6271 if (sv == NULL)
6272 smsg("%s%4d LOADSCRIPT [deleted] from %s",
6273 pfx, current, si->sn_name);
6274 else
6275 smsg("%s%4d LOADSCRIPT %s-%d from %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02006276 sv->sv_name,
6277 sref->sref_idx,
6278 si->sn_name);
6279 }
6280 break;
6281 case ISN_LOADS:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006282 case ISN_LOADEXPORT:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006283 {
6284 scriptitem_T *si = SCRIPT_ITEM(
6285 iptr->isn_arg.loadstore.ls_sid);
6286
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006287 smsg("%s%4d %s s:%s from %s", pfx, current,
6288 iptr->isn_type == ISN_LOADS ? "LOADS"
6289 : "LOADEXPORT",
6290 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006291 }
6292 break;
6293 case ISN_LOADAUTO:
6294 smsg("%s%4d LOADAUTO %s", pfx, current, iptr->isn_arg.string);
6295 break;
6296 case ISN_LOADG:
6297 smsg("%s%4d LOADG g:%s", pfx, current, iptr->isn_arg.string);
6298 break;
6299 case ISN_LOADB:
6300 smsg("%s%4d LOADB b:%s", pfx, current, iptr->isn_arg.string);
6301 break;
6302 case ISN_LOADW:
6303 smsg("%s%4d LOADW w:%s", pfx, current, iptr->isn_arg.string);
6304 break;
6305 case ISN_LOADT:
6306 smsg("%s%4d LOADT t:%s", pfx, current, iptr->isn_arg.string);
6307 break;
6308 case ISN_LOADGDICT:
6309 smsg("%s%4d LOAD g:", pfx, current);
6310 break;
6311 case ISN_LOADBDICT:
6312 smsg("%s%4d LOAD b:", pfx, current);
6313 break;
6314 case ISN_LOADWDICT:
6315 smsg("%s%4d LOAD w:", pfx, current);
6316 break;
6317 case ISN_LOADTDICT:
6318 smsg("%s%4d LOAD t:", pfx, current);
6319 break;
6320 case ISN_LOADOPT:
6321 smsg("%s%4d LOADOPT %s", pfx, current, iptr->isn_arg.string);
6322 break;
6323 case ISN_LOADENV:
6324 smsg("%s%4d LOADENV %s", pfx, current, iptr->isn_arg.string);
6325 break;
6326 case ISN_LOADREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006327 smsg("%s%4d LOADREG @%c", pfx, current,
6328 (int)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006329 break;
6330
6331 case ISN_STORE:
6332 if (iptr->isn_arg.number < 0)
6333 smsg("%s%4d STORE arg[%lld]", pfx, current,
6334 iptr->isn_arg.number + STACK_FRAME_SIZE);
6335 else
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006336 smsg("%s%4d STORE $%lld", pfx, current,
6337 iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006338 break;
6339 case ISN_STOREOUTER:
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006340 {
6341 isn_outer_T *outer = &iptr->isn_arg.outer;
6342
6343 if (outer->outer_depth == OUTER_LOOP_DEPTH)
6344 smsg("%s%4d STOREOUTER level 1 $%d in loop",
6345 pfx, current, outer->outer_idx);
6346 else
6347 smsg("%s%4d STOREOUTER level %d $%d", pfx, current,
6348 outer->outer_depth, outer->outer_idx);
6349 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006350 break;
6351 case ISN_STOREV:
6352 smsg("%s%4d STOREV v:%s", pfx, current,
6353 get_vim_var_name(iptr->isn_arg.number));
6354 break;
6355 case ISN_STOREAUTO:
6356 smsg("%s%4d STOREAUTO %s", pfx, current, iptr->isn_arg.string);
6357 break;
6358 case ISN_STOREG:
6359 smsg("%s%4d STOREG %s", pfx, current, iptr->isn_arg.string);
6360 break;
6361 case ISN_STOREB:
6362 smsg("%s%4d STOREB %s", pfx, current, iptr->isn_arg.string);
6363 break;
6364 case ISN_STOREW:
6365 smsg("%s%4d STOREW %s", pfx, current, iptr->isn_arg.string);
6366 break;
6367 case ISN_STORET:
6368 smsg("%s%4d STORET %s", pfx, current, iptr->isn_arg.string);
6369 break;
6370 case ISN_STORES:
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006371 case ISN_STOREEXPORT:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006372 {
6373 scriptitem_T *si = SCRIPT_ITEM(
6374 iptr->isn_arg.loadstore.ls_sid);
6375
Bram Moolenaarc0ceeeb2022-03-30 21:12:27 +01006376 smsg("%s%4d %s %s in %s", pfx, current,
6377 iptr->isn_type == ISN_STORES
6378 ? "STORES" : "STOREEXPORT",
Bram Moolenaar4c137212021-04-19 16:48:48 +02006379 iptr->isn_arg.loadstore.ls_name, si->sn_name);
6380 }
6381 break;
6382 case ISN_STORESCRIPT:
6383 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006384 scriptref_T *sref = iptr->isn_arg.script.scriptref;
6385 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
6386 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006387
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006388 sv = get_script_svar(sref, -1);
6389 if (sv == NULL)
6390 smsg("%s%4d STORESCRIPT [deleted] in %s",
6391 pfx, current, si->sn_name);
6392 else
6393 smsg("%s%4d STORESCRIPT %s-%d in %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02006394 sv->sv_name,
6395 sref->sref_idx,
6396 si->sn_name);
6397 }
6398 break;
6399 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00006400 case ISN_STOREFUNCOPT:
6401 smsg("%s%4d %s &%s", pfx, current,
6402 iptr->isn_type == ISN_STOREOPT ? "STOREOPT" : "STOREFUNCOPT",
Bram Moolenaar4c137212021-04-19 16:48:48 +02006403 iptr->isn_arg.storeopt.so_name);
6404 break;
6405 case ISN_STOREENV:
6406 smsg("%s%4d STOREENV $%s", pfx, current, iptr->isn_arg.string);
6407 break;
6408 case ISN_STOREREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006409 smsg("%s%4d STOREREG @%c", pfx, current,
6410 (int)iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006411 break;
6412 case ISN_STORENR:
6413 smsg("%s%4d STORE %lld in $%d", pfx, current,
6414 iptr->isn_arg.storenr.stnr_val,
6415 iptr->isn_arg.storenr.stnr_idx);
6416 break;
6417
6418 case ISN_STOREINDEX:
6419 smsg("%s%4d STOREINDEX %s", pfx, current,
6420 vartype_name(iptr->isn_arg.vartype));
6421 break;
6422
6423 case ISN_STORERANGE:
6424 smsg("%s%4d STORERANGE", pfx, current);
6425 break;
6426
Bram Moolenaard505d172022-12-18 21:42:55 +00006427 case ISN_LOAD_CLASSMEMBER:
6428 case ISN_STORE_CLASSMEMBER:
6429 {
6430 class_T *cl = iptr->isn_arg.classmember.cm_class;
6431 int idx = iptr->isn_arg.classmember.cm_idx;
6432 ocmember_T *ocm = &cl->class_class_members[idx];
6433 smsg("%s%4d %s CLASSMEMBER %s.%s", pfx, current,
6434 iptr->isn_type == ISN_LOAD_CLASSMEMBER
6435 ? "LOAD" : "STORE",
6436 cl->class_name, ocm->ocm_name);
6437 }
6438 break;
6439
Bram Moolenaar4c137212021-04-19 16:48:48 +02006440 // constants
6441 case ISN_PUSHNR:
6442 smsg("%s%4d PUSHNR %lld", pfx, current,
6443 (varnumber_T)(iptr->isn_arg.number));
6444 break;
6445 case ISN_PUSHBOOL:
6446 case ISN_PUSHSPEC:
6447 smsg("%s%4d PUSH %s", pfx, current,
6448 get_var_special_name(iptr->isn_arg.number));
6449 break;
6450 case ISN_PUSHF:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006451 smsg("%s%4d PUSHF %g", pfx, current, iptr->isn_arg.fnumber);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006452 break;
6453 case ISN_PUSHS:
6454 smsg("%s%4d PUSHS \"%s\"", pfx, current, iptr->isn_arg.string);
6455 break;
6456 case ISN_PUSHBLOB:
6457 {
6458 char_u *r;
6459 char_u numbuf[NUMBUFLEN];
6460 char_u *tofree;
6461
6462 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
6463 smsg("%s%4d PUSHBLOB %s", pfx, current, r);
6464 vim_free(tofree);
6465 }
6466 break;
6467 case ISN_PUSHFUNC:
6468 {
6469 char *name = (char *)iptr->isn_arg.string;
6470
6471 smsg("%s%4d PUSHFUNC \"%s\"", pfx, current,
6472 name == NULL ? "[none]" : name);
6473 }
6474 break;
6475 case ISN_PUSHCHANNEL:
6476#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar397a87a2022-03-20 21:14:15 +00006477 smsg("%s%4d PUSHCHANNEL 0", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006478#endif
6479 break;
6480 case ISN_PUSHJOB:
6481#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar397a87a2022-03-20 21:14:15 +00006482 smsg("%s%4d PUSHJOB \"no process\"", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006483#endif
6484 break;
6485 case ISN_PUSHEXC:
6486 smsg("%s%4d PUSH v:exception", pfx, current);
6487 break;
Bram Moolenaar06b77222022-01-25 15:51:56 +00006488 case ISN_AUTOLOAD:
6489 smsg("%s%4d AUTOLOAD %s", pfx, current, iptr->isn_arg.string);
6490 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006491 case ISN_UNLET:
6492 smsg("%s%4d UNLET%s %s", pfx, current,
6493 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
6494 iptr->isn_arg.unlet.ul_name);
6495 break;
6496 case ISN_UNLETENV:
6497 smsg("%s%4d UNLETENV%s $%s", pfx, current,
6498 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
6499 iptr->isn_arg.unlet.ul_name);
6500 break;
6501 case ISN_UNLETINDEX:
6502 smsg("%s%4d UNLETINDEX", pfx, current);
6503 break;
6504 case ISN_UNLETRANGE:
6505 smsg("%s%4d UNLETRANGE", pfx, current);
6506 break;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02006507 case ISN_LOCKUNLOCK:
6508 smsg("%s%4d LOCKUNLOCK %s", pfx, current, iptr->isn_arg.string);
6509 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006510 case ISN_LOCKCONST:
6511 smsg("%s%4d LOCKCONST", pfx, current);
6512 break;
6513 case ISN_NEWLIST:
6514 smsg("%s%4d NEWLIST size %lld", pfx, current,
Bram Moolenaar1936c762022-09-28 15:19:10 +01006515 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006516 break;
6517 case ISN_NEWDICT:
6518 smsg("%s%4d NEWDICT size %lld", pfx, current,
Bram Moolenaar1936c762022-09-28 15:19:10 +01006519 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006520 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00006521 case ISN_NEWPARTIAL:
6522 smsg("%s%4d NEWPARTIAL", pfx, current);
6523 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006524
6525 // function call
6526 case ISN_BCALL:
6527 {
6528 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
6529
6530 smsg("%s%4d BCALL %s(argc %d)", pfx, current,
6531 internal_func_name(cbfunc->cbf_idx),
6532 cbfunc->cbf_argcount);
6533 }
6534 break;
6535 case ISN_DCALL:
6536 {
6537 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
6538 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
6539 + cdfunc->cdf_idx;
6540
6541 smsg("%s%4d DCALL %s(argc %d)", pfx, current,
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006542 printable_func_name(df->df_ufunc),
6543 cdfunc->cdf_argcount);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006544 }
6545 break;
6546 case ISN_UCALL:
6547 {
6548 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
6549
6550 smsg("%s%4d UCALL %s(argc %d)", pfx, current,
6551 cufunc->cuf_name, cufunc->cuf_argcount);
6552 }
6553 break;
6554 case ISN_PCALL:
6555 {
6556 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
6557
6558 smsg("%s%4d PCALL%s (argc %d)", pfx, current,
6559 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
6560 }
6561 break;
6562 case ISN_PCALL_END:
6563 smsg("%s%4d PCALL end", pfx, current);
6564 break;
Bram Moolenaar1d84f762022-09-03 21:35:53 +01006565 case ISN_DEFER:
6566 smsg("%s%4d DEFER %d args", pfx, current,
6567 (int)iptr->isn_arg.defer.defer_argcount);
6568 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006569 case ISN_RETURN:
6570 smsg("%s%4d RETURN", pfx, current);
6571 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02006572 case ISN_RETURN_VOID:
6573 smsg("%s%4d RETURN void", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006574 break;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006575 case ISN_RETURN_OBJECT:
6576 smsg("%s%4d RETURN object", pfx, current);
6577 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006578 case ISN_FUNCREF:
6579 {
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006580 funcref_T *funcref = &iptr->isn_arg.funcref;
6581 funcref_extra_T *extra = funcref->fr_extra;
6582 char_u *name;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006583
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006584 if (extra == NULL || extra->fre_func_name == NULL)
Bram Moolenaar38453522021-11-28 22:00:12 +00006585 {
6586 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
6587 + funcref->fr_dfunc_idx;
6588 name = df->df_ufunc->uf_name;
6589 }
6590 else
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006591 name = extra->fre_func_name;
Bram Moolenaarcc341812022-09-19 15:54:34 +01006592 if (extra == NULL || extra->fre_loopvar_info.lvi_depth == 0)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006593 smsg("%s%4d FUNCREF %s", pfx, current, name);
6594 else
Bram Moolenaarcc341812022-09-19 15:54:34 +01006595 {
6596 char_u *info = printable_loopvarinfo(
6597 &extra->fre_loopvar_info);
6598
6599 smsg("%s%4d FUNCREF %s vars %s", pfx, current,
6600 name, info);
6601 vim_free(info);
6602 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006603 }
6604 break;
6605
6606 case ISN_NEWFUNC:
6607 {
Bram Moolenaarcc341812022-09-19 15:54:34 +01006608 newfuncarg_T *arg = iptr->isn_arg.newfunc.nf_arg;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006609
Bram Moolenaarcc341812022-09-19 15:54:34 +01006610 if (arg->nfa_loopvar_info.lvi_depth == 0)
Bram Moolenaar8fa745e2022-09-16 19:04:24 +01006611 smsg("%s%4d NEWFUNC %s %s", pfx, current,
6612 arg->nfa_lambda, arg->nfa_global);
6613 else
Bram Moolenaarcc341812022-09-19 15:54:34 +01006614 {
6615 char_u *info = printable_loopvarinfo(
6616 &arg->nfa_loopvar_info);
6617
6618 smsg("%s%4d NEWFUNC %s %s vars %s", pfx, current,
6619 arg->nfa_lambda, arg->nfa_global, info);
6620 vim_free(info);
6621 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02006622 }
6623 break;
6624
6625 case ISN_DEF:
6626 {
6627 char_u *name = iptr->isn_arg.string;
6628
6629 smsg("%s%4d DEF %s", pfx, current,
6630 name == NULL ? (char_u *)"" : name);
6631 }
6632 break;
6633
6634 case ISN_JUMP:
6635 {
6636 char *when = "?";
6637
6638 switch (iptr->isn_arg.jump.jump_when)
6639 {
6640 case JUMP_ALWAYS:
6641 when = "JUMP";
6642 break;
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02006643 case JUMP_NEVER:
6644 iemsg("JUMP_NEVER should not be used");
6645 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006646 case JUMP_AND_KEEP_IF_TRUE:
6647 when = "JUMP_AND_KEEP_IF_TRUE";
6648 break;
6649 case JUMP_IF_FALSE:
6650 when = "JUMP_IF_FALSE";
6651 break;
Bram Moolenaarb46c0832022-09-15 17:19:37 +01006652 case JUMP_WHILE_FALSE:
6653 when = "JUMP_WHILE_FALSE"; // unused
6654 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006655 case JUMP_IF_COND_FALSE:
6656 when = "JUMP_IF_COND_FALSE";
6657 break;
6658 case JUMP_IF_COND_TRUE:
6659 when = "JUMP_IF_COND_TRUE";
6660 break;
6661 }
6662 smsg("%s%4d %s -> %d", pfx, current, when,
6663 iptr->isn_arg.jump.jump_where);
6664 }
6665 break;
6666
6667 case ISN_JUMP_IF_ARG_SET:
6668 smsg("%s%4d JUMP_IF_ARG_SET arg[%d] -> %d", pfx, current,
6669 iptr->isn_arg.jumparg.jump_arg_off + STACK_FRAME_SIZE,
6670 iptr->isn_arg.jump.jump_where);
6671 break;
6672
Bram Moolenaar65b0d162022-12-13 18:43:22 +00006673 case ISN_JUMP_IF_ARG_NOT_SET:
6674 smsg("%s%4d JUMP_IF_ARG_NOT_SET arg[%d] -> %d", pfx, current,
6675 iptr->isn_arg.jumparg.jump_arg_off + STACK_FRAME_SIZE,
6676 iptr->isn_arg.jump.jump_where);
6677 break;
6678
Bram Moolenaar4c137212021-04-19 16:48:48 +02006679 case ISN_FOR:
6680 {
6681 forloop_T *forloop = &iptr->isn_arg.forloop;
6682
6683 smsg("%s%4d FOR $%d -> %d", pfx, current,
Bram Moolenaarcc341812022-09-19 15:54:34 +01006684 forloop->for_loop_idx, forloop->for_end);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006685 }
6686 break;
6687
Bram Moolenaarb46c0832022-09-15 17:19:37 +01006688 case ISN_ENDLOOP:
6689 {
6690 endloop_T *endloop = &iptr->isn_arg.endloop;
6691
Bram Moolenaarcc341812022-09-19 15:54:34 +01006692 smsg("%s%4d ENDLOOP ref $%d save $%d-$%d depth %d",
6693 pfx, current,
Bram Moolenaarb46c0832022-09-15 17:19:37 +01006694 endloop->end_funcref_idx,
6695 endloop->end_var_idx,
Bram Moolenaarcc341812022-09-19 15:54:34 +01006696 endloop->end_var_idx + endloop->end_var_count - 1,
6697 endloop->end_depth);
Bram Moolenaarb46c0832022-09-15 17:19:37 +01006698 }
6699 break;
6700
6701 case ISN_WHILE:
6702 {
6703 whileloop_T *whileloop = &iptr->isn_arg.whileloop;
6704
6705 smsg("%s%4d WHILE $%d -> %d", pfx, current,
6706 whileloop->while_funcref_idx,
6707 whileloop->while_end);
6708 }
6709 break;
6710
Bram Moolenaar4c137212021-04-19 16:48:48 +02006711 case ISN_TRY:
6712 {
Bram Moolenaar0d807102021-12-21 09:42:09 +00006713 try_T *try = &iptr->isn_arg.tryref;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006714
6715 if (try->try_ref->try_finally == 0)
6716 smsg("%s%4d TRY catch -> %d, endtry -> %d",
6717 pfx, current,
6718 try->try_ref->try_catch,
6719 try->try_ref->try_endtry);
6720 else
6721 smsg("%s%4d TRY catch -> %d, finally -> %d, endtry -> %d",
6722 pfx, current,
6723 try->try_ref->try_catch,
6724 try->try_ref->try_finally,
6725 try->try_ref->try_endtry);
6726 }
6727 break;
6728 case ISN_CATCH:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006729 smsg("%s%4d CATCH", pfx, current);
6730 break;
6731 case ISN_TRYCONT:
6732 {
6733 trycont_T *trycont = &iptr->isn_arg.trycont;
6734
6735 smsg("%s%4d TRY-CONTINUE %d level%s -> %d", pfx, current,
6736 trycont->tct_levels,
6737 trycont->tct_levels == 1 ? "" : "s",
6738 trycont->tct_where);
6739 }
6740 break;
6741 case ISN_FINALLY:
6742 smsg("%s%4d FINALLY", pfx, current);
6743 break;
6744 case ISN_ENDTRY:
6745 smsg("%s%4d ENDTRY", pfx, current);
6746 break;
6747 case ISN_THROW:
6748 smsg("%s%4d THROW", pfx, current);
6749 break;
6750
6751 // expression operations on number
6752 case ISN_OPNR:
6753 case ISN_OPFLOAT:
6754 case ISN_OPANY:
6755 {
6756 char *what;
6757 char *ins;
6758
6759 switch (iptr->isn_arg.op.op_type)
6760 {
6761 case EXPR_MULT: what = "*"; break;
6762 case EXPR_DIV: what = "/"; break;
6763 case EXPR_REM: what = "%"; break;
6764 case EXPR_SUB: what = "-"; break;
6765 case EXPR_ADD: what = "+"; break;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01006766 case EXPR_LSHIFT: what = "<<"; break;
6767 case EXPR_RSHIFT: what = ">>"; break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006768 default: what = "???"; break;
6769 }
6770 switch (iptr->isn_type)
6771 {
6772 case ISN_OPNR: ins = "OPNR"; break;
6773 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
6774 case ISN_OPANY: ins = "OPANY"; break;
6775 default: ins = "???"; break;
6776 }
6777 smsg("%s%4d %s %s", pfx, current, ins, what);
6778 }
6779 break;
6780
6781 case ISN_COMPAREBOOL:
6782 case ISN_COMPARESPECIAL:
Bram Moolenaar7a222242022-03-01 19:23:24 +00006783 case ISN_COMPARENULL:
Bram Moolenaar4c137212021-04-19 16:48:48 +02006784 case ISN_COMPARENR:
6785 case ISN_COMPAREFLOAT:
6786 case ISN_COMPARESTRING:
6787 case ISN_COMPAREBLOB:
6788 case ISN_COMPARELIST:
6789 case ISN_COMPAREDICT:
6790 case ISN_COMPAREFUNC:
6791 case ISN_COMPAREANY:
6792 {
6793 char *p;
6794 char buf[10];
6795 char *type;
6796
6797 switch (iptr->isn_arg.op.op_type)
6798 {
6799 case EXPR_EQUAL: p = "=="; break;
6800 case EXPR_NEQUAL: p = "!="; break;
6801 case EXPR_GREATER: p = ">"; break;
6802 case EXPR_GEQUAL: p = ">="; break;
6803 case EXPR_SMALLER: p = "<"; break;
6804 case EXPR_SEQUAL: p = "<="; break;
6805 case EXPR_MATCH: p = "=~"; break;
6806 case EXPR_IS: p = "is"; break;
6807 case EXPR_ISNOT: p = "isnot"; break;
6808 case EXPR_NOMATCH: p = "!~"; break;
6809 default: p = "???"; break;
6810 }
6811 STRCPY(buf, p);
6812 if (iptr->isn_arg.op.op_ic == TRUE)
6813 strcat(buf, "?");
6814 switch(iptr->isn_type)
6815 {
6816 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
6817 case ISN_COMPARESPECIAL:
6818 type = "COMPARESPECIAL"; break;
Bram Moolenaar7a222242022-03-01 19:23:24 +00006819 case ISN_COMPARENULL: type = "COMPARENULL"; break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006820 case ISN_COMPARENR: type = "COMPARENR"; break;
6821 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
6822 case ISN_COMPARESTRING:
6823 type = "COMPARESTRING"; break;
6824 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
6825 case ISN_COMPARELIST: type = "COMPARELIST"; break;
6826 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
6827 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
6828 case ISN_COMPAREANY: type = "COMPAREANY"; break;
6829 default: type = "???"; break;
6830 }
6831
6832 smsg("%s%4d %s %s", pfx, current, type, buf);
6833 }
6834 break;
6835
6836 case ISN_ADDLIST: smsg("%s%4d ADDLIST", pfx, current); break;
6837 case ISN_ADDBLOB: smsg("%s%4d ADDBLOB", pfx, current); break;
6838
6839 // expression operations
LemonBoy372bcce2022-04-25 12:43:20 +01006840 case ISN_CONCAT:
6841 smsg("%s%4d CONCAT size %lld", pfx, current,
6842 (varnumber_T)(iptr->isn_arg.number));
6843 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006844 case ISN_STRINDEX: smsg("%s%4d STRINDEX", pfx, current); break;
6845 case ISN_STRSLICE: smsg("%s%4d STRSLICE", pfx, current); break;
6846 case ISN_BLOBINDEX: smsg("%s%4d BLOBINDEX", pfx, current); break;
6847 case ISN_BLOBSLICE: smsg("%s%4d BLOBSLICE", pfx, current); break;
6848 case ISN_LISTAPPEND: smsg("%s%4d LISTAPPEND", pfx, current); break;
6849 case ISN_BLOBAPPEND: smsg("%s%4d BLOBAPPEND", pfx, current); break;
6850 case ISN_LISTINDEX: smsg("%s%4d LISTINDEX", pfx, current); break;
6851 case ISN_LISTSLICE: smsg("%s%4d LISTSLICE", pfx, current); break;
6852 case ISN_ANYINDEX: smsg("%s%4d ANYINDEX", pfx, current); break;
6853 case ISN_ANYSLICE: smsg("%s%4d ANYSLICE", pfx, current); break;
6854 case ISN_SLICE: smsg("%s%4d SLICE %lld",
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02006855 pfx, current, iptr->isn_arg.number); break;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02006856 case ISN_GETITEM: smsg("%s%4d ITEM %lld%s", pfx, current,
6857 iptr->isn_arg.getitem.gi_index,
6858 iptr->isn_arg.getitem.gi_with_op ?
6859 " with op" : ""); break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02006860 case ISN_MEMBER: smsg("%s%4d MEMBER", pfx, current); break;
6861 case ISN_STRINGMEMBER: smsg("%s%4d MEMBER %s", pfx, current,
6862 iptr->isn_arg.string); break;
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00006863 case ISN_GET_OBJ_MEMBER: smsg("%s%4d OBJ_MEMBER %d", pfx, current,
6864 (int)iptr->isn_arg.number); break;
6865 case ISN_STORE_THIS: smsg("%s%4d STORE_THIS %d", pfx, current,
Bram Moolenaarffdaca92022-12-09 21:41:48 +00006866 (int)iptr->isn_arg.number); break;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02006867 case ISN_CLEARDICT: smsg("%s%4d CLEARDICT", pfx, current); break;
6868 case ISN_USEDICT: smsg("%s%4d USEDICT", pfx, current); break;
6869
Bram Moolenaar4c137212021-04-19 16:48:48 +02006870 case ISN_NEGATENR: smsg("%s%4d NEGATENR", pfx, current); break;
6871
Bram Moolenaar4c137212021-04-19 16:48:48 +02006872 case ISN_CHECKTYPE:
6873 {
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01006874 checktype_T *ct = &iptr->isn_arg.type;
Bram Moolenaarc6951a72022-12-29 20:56:24 +00006875 char *tofree = NULL;
6876 char *typename;
6877
6878 if (ct->ct_type->tt_type == VAR_FLOAT
6879 && (ct->ct_type->tt_flags & TTFLAG_NUMBER_OK))
6880 typename = "float|number";
6881 else
6882 typename = type_name(ct->ct_type, &tofree);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006883
6884 if (ct->ct_arg_idx == 0)
6885 smsg("%s%4d CHECKTYPE %s stack[%d]", pfx, current,
Bram Moolenaarc6951a72022-12-29 20:56:24 +00006886 typename,
Bram Moolenaar4c137212021-04-19 16:48:48 +02006887 (int)ct->ct_off);
6888 else
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01006889 smsg("%s%4d CHECKTYPE %s stack[%d] %s %d",
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02006890 pfx, current,
Bram Moolenaarc6951a72022-12-29 20:56:24 +00006891 typename,
Bram Moolenaar4c137212021-04-19 16:48:48 +02006892 (int)ct->ct_off,
Bram Moolenaarbd3a9d22022-05-17 16:12:39 +01006893 ct->ct_is_var ? "var": "arg",
Bram Moolenaar4c137212021-04-19 16:48:48 +02006894 (int)ct->ct_arg_idx);
6895 vim_free(tofree);
6896 break;
6897 }
6898 case ISN_CHECKLEN: smsg("%s%4d CHECKLEN %s%d", pfx, current,
6899 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
6900 iptr->isn_arg.checklen.cl_min_len);
6901 break;
6902 case ISN_SETTYPE:
6903 {
6904 char *tofree;
6905
6906 smsg("%s%4d SETTYPE %s", pfx, current,
6907 type_name(iptr->isn_arg.type.ct_type, &tofree));
6908 vim_free(tofree);
6909 break;
6910 }
6911 case ISN_COND2BOOL: smsg("%s%4d COND2BOOL", pfx, current); break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006912 case ISN_2BOOL: if (iptr->isn_arg.tobool.invert)
6913 smsg("%s%4d INVERT %d (!val)", pfx, current,
6914 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006915 else
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006916 smsg("%s%4d 2BOOL %d (!!val)", pfx, current,
6917 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006918 break;
6919 case ISN_2STRING: smsg("%s%4d 2STRING stack[%lld]", pfx, current,
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006920 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006921 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006922 case ISN_2STRING_ANY: smsg("%s%4d 2STRING_ANY stack[%lld]",
6923 pfx, current,
6924 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02006925 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006926 case ISN_RANGE: smsg("%s%4d RANGE %s", pfx, current,
6927 iptr->isn_arg.string);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006928 break;
6929 case ISN_PUT:
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01006930 if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE)
Bram Moolenaar4c137212021-04-19 16:48:48 +02006931 smsg("%s%4d PUT %c above range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006932 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006933 else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE)
6934 smsg("%s%4d PUT %c range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02006935 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006936 else
6937 smsg("%s%4d PUT %c %ld", pfx, current,
6938 iptr->isn_arg.put.put_regname,
6939 (long)iptr->isn_arg.put.put_lnum);
6940 break;
6941
Bram Moolenaar4c137212021-04-19 16:48:48 +02006942 case ISN_CMDMOD:
6943 {
6944 char_u *buf;
6945 size_t len = produce_cmdmods(
6946 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
6947
6948 buf = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02006949 if (likely(buf != NULL))
Bram Moolenaar4c137212021-04-19 16:48:48 +02006950 {
6951 (void)produce_cmdmods(
6952 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
6953 smsg("%s%4d CMDMOD %s", pfx, current, buf);
6954 vim_free(buf);
6955 }
6956 break;
6957 }
6958 case ISN_CMDMOD_REV: smsg("%s%4d CMDMOD_REV", pfx, current); break;
6959
6960 case ISN_PROF_START:
Bram Moolenaare99d4222021-06-13 14:01:26 +02006961 smsg("%s%4d PROFILE START line %d", pfx, current,
6962 iptr->isn_lnum);
Bram Moolenaar4c137212021-04-19 16:48:48 +02006963 break;
6964
6965 case ISN_PROF_END:
6966 smsg("%s%4d PROFILE END", pfx, current);
6967 break;
6968
Bram Moolenaare99d4222021-06-13 14:01:26 +02006969 case ISN_DEBUG:
Bram Moolenaar8cec9272021-06-23 20:20:53 +02006970 smsg("%s%4d DEBUG line %d-%d varcount %lld", pfx, current,
6971 iptr->isn_arg.debug.dbg_break_lnum + 1,
6972 iptr->isn_lnum,
6973 iptr->isn_arg.debug.dbg_var_names_len);
Bram Moolenaare99d4222021-06-13 14:01:26 +02006974 break;
6975
Bram Moolenaar4c137212021-04-19 16:48:48 +02006976 case ISN_UNPACK: smsg("%s%4d UNPACK %d%s", pfx, current,
6977 iptr->isn_arg.unpack.unp_count,
6978 iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : "");
6979 break;
6980 case ISN_SHUFFLE: smsg("%s%4d SHUFFLE %d up %d", pfx, current,
6981 iptr->isn_arg.shuffle.shfl_item,
6982 iptr->isn_arg.shuffle.shfl_up);
6983 break;
6984 case ISN_DROP: smsg("%s%4d DROP", pfx, current); break;
6985
6986 case ISN_FINISH: // End of list of instructions for ISN_SUBSTITUTE.
6987 return;
6988 }
6989
6990 out_flush(); // output one line at a time
6991 ui_breakcheck();
6992 if (got_int)
6993 break;
6994 }
6995}
6996
6997/*
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02006998 * Handle command line completion for the :disassemble command.
6999 */
7000 void
7001set_context_in_disassemble_cmd(expand_T *xp, char_u *arg)
7002{
7003 char_u *p;
7004
7005 // Default: expand user functions, "debug" and "profile"
7006 xp->xp_context = EXPAND_DISASSEMBLE;
7007 xp->xp_pattern = arg;
7008
7009 // first argument already typed: only user function names
7010 if (*arg != NUL && *(p = skiptowhite(arg)) != NUL)
7011 {
7012 xp->xp_context = EXPAND_USER_FUNC;
7013 xp->xp_pattern = skipwhite(p);
7014 }
7015}
7016
7017/*
7018 * Function given to ExpandGeneric() to obtain the list of :disassemble
7019 * arguments.
7020 */
7021 char_u *
7022get_disassemble_argument(expand_T *xp, int idx)
7023{
7024 if (idx == 0)
7025 return (char_u *)"debug";
7026 if (idx == 1)
7027 return (char_u *)"profile";
7028 return get_user_func_name(xp, idx - 2);
7029}
7030
7031/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01007032 * ":disassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01007033 * We don't really need this at runtime, but we do have tests that require it,
7034 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007035 */
7036 void
7037ex_disassemble(exarg_T *eap)
7038{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01007039 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01007040 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007041 dfunc_T *dfunc;
Bram Moolenaardafef512022-05-21 21:55:55 +01007042 isn_T *instr = NULL; // init to shut up gcc warning
7043 int instr_count = 0; // init to shut up gcc warning
Bram Moolenaar5a01caa2022-05-21 18:56:58 +01007044 compiletype_T compile_type = CT_NONE;
Bram Moolenaare99d4222021-06-13 14:01:26 +02007045
Bram Moolenaarf79d9dd2022-05-21 15:39:02 +01007046 ufunc = find_func_by_name(arg, &compile_type);
Bram Moolenaara26b9702020-04-18 19:53:28 +02007047 if (ufunc == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007048 return;
Bram Moolenaare99d4222021-06-13 14:01:26 +02007049 if (func_needs_compiling(ufunc, compile_type)
7050 && compile_def_function(ufunc, FALSE, compile_type, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02007051 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02007052 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007053 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02007054 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007055 return;
7056 }
Bram Moolenaar6db660b2021-08-01 14:08:54 +02007057 msg((char *)printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007058
7059 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
Bram Moolenaare99d4222021-06-13 14:01:26 +02007060 switch (compile_type)
7061 {
7062 case CT_PROFILE:
Bram Moolenaarf002a412021-01-24 13:34:18 +01007063#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02007064 instr = dfunc->df_instr_prof;
7065 instr_count = dfunc->df_instr_prof_count;
7066 break;
Bram Moolenaarf002a412021-01-24 13:34:18 +01007067#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02007068 // FALLTHROUGH
7069 case CT_NONE:
7070 instr = dfunc->df_instr;
7071 instr_count = dfunc->df_instr_count;
7072 break;
7073 case CT_DEBUG:
7074 instr = dfunc->df_instr_debug;
7075 instr_count = dfunc->df_instr_debug_count;
7076 break;
7077 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007078
Bram Moolenaar4c137212021-04-19 16:48:48 +02007079 list_instructions("", instr, instr_count, ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007080}
7081
7082/*
Bram Moolenaar13106602020-10-04 16:06:05 +02007083 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007084 * list, etc. Mostly like what JavaScript does, except that empty list and
7085 * empty dictionary are FALSE.
7086 */
7087 int
7088tv2bool(typval_T *tv)
7089{
7090 switch (tv->v_type)
7091 {
7092 case VAR_NUMBER:
7093 return tv->vval.v_number != 0;
7094 case VAR_FLOAT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007095 return tv->vval.v_float != 0.0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007096 case VAR_PARTIAL:
7097 return tv->vval.v_partial != NULL;
7098 case VAR_FUNC:
7099 case VAR_STRING:
7100 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
7101 case VAR_LIST:
7102 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
7103 case VAR_DICT:
7104 return tv->vval.v_dict != NULL
7105 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
7106 case VAR_BOOL:
7107 case VAR_SPECIAL:
7108 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
7109 case VAR_JOB:
7110#ifdef FEAT_JOB_CHANNEL
7111 return tv->vval.v_job != NULL;
7112#else
7113 break;
7114#endif
7115 case VAR_CHANNEL:
7116#ifdef FEAT_JOB_CHANNEL
7117 return tv->vval.v_channel != NULL;
7118#else
7119 break;
7120#endif
7121 case VAR_BLOB:
7122 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
7123 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02007124 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007125 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02007126 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00007127 case VAR_CLASS:
7128 case VAR_OBJECT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007129 break;
7130 }
7131 return FALSE;
7132}
7133
Bram Moolenaarea2d4072020-11-12 12:08:51 +01007134 void
7135emsg_using_string_as(typval_T *tv, int as_number)
7136{
7137 semsg(_(as_number ? e_using_string_as_number_str
7138 : e_using_string_as_bool_str),
7139 tv->vval.v_string == NULL
7140 ? (char_u *)"" : tv->vval.v_string);
7141}
7142
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007143/*
7144 * If "tv" is a string give an error and return FAIL.
7145 */
7146 int
7147check_not_string(typval_T *tv)
7148{
7149 if (tv->v_type == VAR_STRING)
7150 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +01007151 emsg_using_string_as(tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007152 clear_tv(tv);
7153 return FAIL;
7154 }
7155 return OK;
7156}
7157
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007158
7159#endif // FEAT_EVAL